Example #1
0
    def test_run_simulationd(self):
        project_id = str(uuid.uuid1())
        runtime = self.getRuntime(project_id)
        file_id = str(uuid.uuid1())
        with open("../models/abc-pert.ka") as kappa_file:
            data = kappa_file.read()
            file_content = str(data)
            file_metadata = kappa_common.FileMetadata(file_id,0)
            file_object = kappa_common.File(file_metadata,file_content)
            runtime.file_create(file_object)
            runtime.project_parse()
            pause_condition = "[T] > 10.0"
            simulation_parameter = kappa_common.SimulationParameter(0.1,"default",pause_condition)
            runtime.simulation_start(simulation_parameter)

            simulation_info = runtime.simulation_info()

            while simulation_info["simulation_info_progress"]["simulation_progress_is_running"] :
                time.sleep(1)
                simulation_info = runtime.simulation_info()

            # test that no limit returns all entries
            last_status = runtime.simulation_detail_plot()
            test_count = 101
            self.assertEqual(test_count, len(last_status['plot_detail_plot']['plot_time_series']))

            print(simulation_info)
            plot_limit_offset = 100
            test_time = 10.0
            test_count = 1
            limit = kappa_common.PlotLimit(plot_limit_offset)
            last_status = runtime.simulation_detail_plot(limit)
            self.assertEqual(test_count, len(last_status['plot_detail_plot']['plot_time_series']))
            self.assertEqual(test_time, last_status['plot_detail_plot']['plot_time_series'][0][0])

            plot_limit_offset = 10
            plot_limit_points = 1
            test_time = 1.0
            test_count = 1
            limit = kappa_common.PlotLimit(plot_limit_offset,plot_limit_points)
            last_status = runtime.simulation_detail_plot(limit)
            self.assertEqual(test_count, len(last_status['plot_detail_plot']['plot_time_series']))
            self.assertEqual(test_time, last_status['plot_detail_plot']['plot_time_series'][0][0])

            print(simulation_info)
            plot_limit_offset = 50
            test_time = 10.0
            test_count = 51
            limit = kappa_common.PlotLimit(plot_limit_offset)
            last_status = runtime.simulation_detail_plot(limit)
            self.assertEqual(test_count, len(last_status['plot_detail_plot']['plot_time_series']))
            self.assertEqual(test_time, last_status['plot_detail_plot']['plot_time_series'][0][0])
Example #2
0
    def parse_multiple_files(self):
        project_id = str(uuid.uuid1())
        runtime = self.getRuntime(project_id)
        file_1_id = str(uuid.uuid1())
        file_2_id = str(uuid.uuid1())
        test_dir = "../models/test_suite/compiler/file_order/"
        with open(test_dir+"file2.ka") as file_2:
            with open(test_dir+"file1.ka") as file_1:
                data_1 = file_1.read()
                file_1_metadata = kappa_common.FileMetadata(file_1_id,0)
                file_1_object = kappa_common.File(file_1_metadata,data_1)
                runtime.file_create(file_1_object)

                data_2 = file_2.read()
                file_2_metadata = kappa_common.FileMetadata(file_2_id,0)
                file_2_object = kappa_common.File(file_2_metadata,data_2)
                runtime.project_parse()
                with self.assertRaises(kappa_common.KappaError):
                    runtime.file_create(file_2_object)
                file_names = list(kappa_client.file_catalog_file_id(runtime.file_info()))
                self.assertIn(file_1_id,file_names)
                self.assertIn(file_2_id,file_names)
Example #3
0
 def test_file_crud(self):
     project_id = str(uuid.uuid1())
     runtime = self.getRuntime(project_id)
     file_id = str(uuid.uuid1())
     file_content = str("")
     file_metadata = kappa_common.FileMetadata(file_id,0)
     file_object = kappa_common.File(file_metadata,file_content)
     runtime.file_create(file_object)
     file_names = kappa_client.file_catalog_file_id(runtime.file_info())
     self.assertIn(file_id,file_names)
     self.assertEqual(runtime.file_get(file_id).get_content(),
                      file_content)
     runtime.file_delete(file_id)
     try:
         runtime.file_delete(file_id)
         self.fail()
     except:
         None
Example #4
0
    def load_file(self, input_file):
        # when the input file is null
        if input_file is None:
            return

        # when runtime could not be initiated
        if self.__runtime is None:
            return

        with open(input_file) as f:
            code = f.read()
            file_content = str(code)
            file_metadata = kappa_common.FileMetadata(input_file, 0)

            self._debug("file : {0}".format(file_metadata.toJSON()))

            file_object = kappa_common.File(file_metadata, file_content)
            self.__runtime.file_create(file_object)
            self.__runtime.project_parse()
Example #5
0
    def _run(self, pause_time):
        pause_condition = "[T] > {0}".format(str(pause_time))
        print "Pause condition: " + pause_condition
        simulation_parameter = kappa_common.SimulationParameter(
            self.__plot_period, self.__simulation_id, pause_condition,
            self.__seed)

        # if simulator is not started yet
        if self.simulator_status is SimulationStatus.NotStart:
            code = ""
            try:
                with open(self.__temp_files) as f:
                    code = f.read()
            except Exception:
                self._debug("")

            if code is not "":
                file_content = str(code)
                file_metadata = kappa_common.FileMetadata(self.__temp_files, 0)

                self._debug("file : {0}".format(file_metadata.toJSON()))

                file_object = kappa_common.File(file_metadata, file_content)
                self.__runtime.file_create(file_object)
                self.__runtime.project_parse()

            self.__runtime.simulation_start(simulation_parameter)
            self._debug("")
        # if simulator has been paused then start it again
        elif self.simulator_status is SimulationStatus.Paused:
            self.__runtime.simulation_continue(simulation_parameter)
            self._debug("")

        self.simulator_status = SimulationStatus.Running
        simulation_info = self.__runtime.simulation_info()

        while simulation_info["simulation_info_progress"][
                "simulation_progress_is_running"]:
            time.sleep(1)

            percentage = ""
            time_percentage = simulation_info["simulation_info_progress"][
                "simulation_progress_time_percentage"]
            event_percentage = simulation_info["simulation_info_progress"][
                "simulation_progress_event_percentage"]

            if time_percentage or time_percentage == 0:
                percentage = time_percentage
            if event_percentage or event_percentage == 0:
                percentage = event_percentage

            sys.stdout.write("..{0}.. ".format(percentage))
            sys.stdout.flush()
            simulation_info = self.__runtime.simulation_info()

        self.simulator_status = SimulationStatus.Paused

        simulation_progress_time = simulation_info["simulation_info_progress"][
            "simulation_progress_time"]
        self.__progress_time = round(simulation_progress_time, 2)

        print("progression time: {0}".format(self.__progress_time))
Example #6
0
def main():
    # command line
    argv = sys.argv[1:]
    cmd = "kappa_client.py"

    # default arguments
    inputfile = None  # if missing input file just get version
    url = "http://localhost:8080"
    pause_condition = "[false]"
    plot_period = 0.1
    seed = None

    help_line = (cmd + ' -k <kappafile> ' + ' -u <url or path to stdsim> ' +
                 ' -t <max_time> ' + ' -e <max_events> ' +
                 ' -pp <plot_period> ' + ' -s <random_seed> ')
    try:
        opts, args = getopt.getopt(argv, "h:k:u:t:e:pp:s", [
            "help", "kappafile=", "url=", "max_time=", "max_event=",
            "plot_period=", "seed="
        ])
    except:
        print(help_line)

        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
            print(help_line)
            sys.exit()
        elif opt in ("-k", "--kappafile"):
            inputfile = arg
        elif opt in ("-u", "--url"):
            url = arg
        elif opt in ("-t", "--max_time"):
            pause_condition = "[T]>" + arg + " || " + pause_condition
        elif opt in ("-e", "--max_events"):
            pause_condition = "[E]>" + arg + " || " + pause_condition
        elif opt in ("-pp", "--plot_period"):
            plot_period = float(arg)
        elif opt in ("-s", "--seed"):
            seed = int(arg)

    print('Input file is : {0} '.format(inputfile))
    print('Endpoint url : {0} '.format(url))
    print('Pause conditon : {0}'.format(pause_condition))
    print('Plot period : {0} '.format(plot_period))
    print('Random seed : {0} '.format(seed))

    try:
        project_id = "{0}-{1}".format(cmd, str(uuid.uuid1()))
        if url.startswith('http'):
            runtime = kappa_rest.KappaRest(url, project_id)
        else:
            runtime = kappa_std.KappaStd(url)
        print("project_id : {0}".format(project_id))
        if inputfile:
            with open(inputfile) as f:
                code = f.read()
                file_content = str(code)
                file_metadata = kappa_common.FileMetadata(inputfile, 0)
                file_object = kappa_common.File(file_metadata, file_content)
                runtime.file_create(file_object)
                runtime.project_parse()
                simulation_id = str(uuid.uuid1())
                print("simulation_id : {0}".format(simulation_id))

                end_time = 10.0
                simulation_parameter = kappa_common.SimulationParameter(
                    plot_period, simulation_id, pause_condition, seed)
                runtime.simulation_start(simulation_parameter)

                simulation_info = runtime.simulation_info()

                while simulation_info["simulation_info_progress"][
                        "simulation_progress_is_running"]:
                    time.sleep(1)

                    percentage = ""
                    time_percentage = simulation_info[
                        "simulation_info_progress"][
                            "simulation_progress_time_percentage"]
                    event_percentage = simulation_info[
                        "simulation_info_progress"][
                            "simulation_progress_event_percentage"]

                    if time_percentage or time_percentage == 0:
                        percentage = time_percentage
                    if event_percentage or event_percentage == 0:
                        percentage = event_percentage

                    sys.stdout.write("..{0}.. ".format(percentage))
                    sys.stdout.flush()
                    simulation_info = runtime.simulation_info()

                print("")
                print("info")
                print(simulation_info)
                plot_detail = runtime.simulation_detail_plot()
                print("plot")
                print(plot_detail)
        else:
            print(runtime.info())
    except kappa_common.KappaError as exception:
        print(exception.errors)
    return None
    None