def realize_to_controller(self): """Realize the goal into a Controller object""" if self.controller is not None: return if self.session_name is None: folder_name = f"{self.s_controller_folder_name}/{self.goal_folder_name}" else: folder_name = f"{self.session_name}/{self.s_controller_folder_name}/{self.goal_folder_name}" try: controller_info = self.specification.get_controller_info() a, g, i, o = controller_info.get_strix_inputs() controller_synthesis_input = StringMng.get_controller_synthesis_str( controller_info) Store.save_to_file(controller_synthesis_input, "controller.txt", folder_name) realized, kiss_mealy, time = Strix.generate_controller(a, g, i, o) if not realized: controller_info = self.specification.get_controller_info( world_ts=self.__world.typeset) a, g, i, o = controller_info.get_strix_inputs() controller_synthesis_input = StringMng.get_controller_synthesis_str( controller_info) Store.save_to_file(controller_synthesis_input, "controller.txt", folder_name) realized, kiss_mealy, time = Strix.generate_controller( a, g, i, o) self.__realizable = realized if realized: Store.save_to_file(kiss_mealy, "controller_kiss", folder_name) # Store.generate_eps_from_dot(dot_mealy, "controller", folder_name) else: Store.save_to_file(kiss_mealy, "controller_inverted_kiss", folder_name) # Store.generate_eps_from_dot(dot_mealy, "controller_inverted", folder_name) self.__controller = Controller(mealy_machine=kiss_mealy, world=self.world, name=self.name, synth_time=time) print(f"NAME:\t{self.__name} ({self.__id})") print(self.__controller) Store.save_to_file(str(self.__controller), "controller_table", folder_name) except ControllerException as e: raise GoalSynthesisFail(self, e)
def generate_buchi(specification: str, name: str, path: str = None): try: print(f"Generating Buchi for...\n{specification}") if platform.system() != "Linux": command = f"docker run pmallozzi/ltltools ltl2tgba -B {specification} -d" result = subprocess.check_output( [command], shell=True, encoding='UTF-8', stderr=subprocess.DEVNULL).splitlines() else: result = subprocess.check_output( ["ltl2tgba", "-B", specification, "-d"], encoding='UTF-8', stderr=subprocess.DEVNULL).splitlines() result = [x for x in result if not ('[Büchi]' in x)] result = "".join(result) Store.save_to_file(specification, f"{name}_specs.txt", path) Store.save_to_file(result, f"{name}_dot.txt", path) Store.generate_eps_from_dot(result, name, path) print(f"Buchi generated") except Exception as e: raise e
def create_transition_controller(self, start: Types, finish: Types, t_trans: int) -> Controller: t_controller_name = f"TRANS_{start.name}->{finish.name}" if self.session_name is None: folder_name = f"{self.t_controllers_folder_name}/{t_controller_name}" else: folder_name = f"{self.session_name}/{self.t_controllers_folder_name}/{t_controller_name}" typeset = Typeset({start, finish}) realizable = False for n_steps in range(1, t_trans): trans_spec_str = Logic.and_([start.name, Logic.xn_(finish.name, n_steps)]) trans_spec = Atom(formula=(trans_spec_str, typeset)) trans_contract = Contract(guarantees=trans_spec) try: controller_info = trans_contract.get_controller_info(world_ts=self.world.typeset) a, g, i, o = controller_info.get_strix_inputs() controller_synthesis_input = StringMng.get_controller_synthesis_str(controller_info) Store.save_to_file(controller_synthesis_input, f"t_controller_{start.name}_{finish.name}_specs.txt", folder_name) realized, kiss_mealy, time = Strix.generate_controller(a, g, i, o) if realized: realizable = True break except ControllerException as e: raise TransSynthesisFail(self, e) if not realizable: raise Exception( f"Controller [{start.name}, {finish.name}] cannot be synthetized in {t_trans} steps") else: Store.save_to_file(kiss_mealy, f"{start.name}_{finish.name}_mealy", folder_name) # Store.generate_eps_from_dot(dot_mealy, f"{start.name}_{finish.name}_dot", # folder_name) t_controller = Controller(mealy_machine=kiss_mealy, world=self.world, name=t_controller_name, synth_time=time) Store.save_to_file(str(t_controller), f"{start.name}_{finish.name}_table", folder_name) return t_controller
from tools.persistence import Persistence from running_example import output_folder_name from tools.storage import Store """Load CGG""" cgg = Persistence.load_cgg(output_folder_name) """Launch a simulation of n_steps where each contexts does change for at least t_min_context """ run = cgg.orchestrate(n_steps=50, t_min_context=6) print(run) """Save simulation as text file""" Store.save_to_file(str(run), file_name="simulation.txt", output_folder_name=output_folder_name)
t_t_controllers = 0 n_t_controllers = 0 for key, controller in cgg.t_controllers.items(): t_t_controllers += controller.synth_time n_t_controllers += 1 res = "" res += f"TIME CGG BUILD \t= {t_cgg}\n" res += f"NUMBER OF S-CTRL\t= {n_s_controllers}\n" res += f"NUMBER OF STATES\t= {', '.join(states)}\n" res += f"NUMBER OF TRANSITIONS\t= {', '.join(transitions)}\n" res += f"\nNUMBER OF T-CTRL\t= {n_t_controllers}\n" res += f"TIME S-CTRL \t= {t_s_controllers}\n" res += f"TIME T-CTRL \t= {t_t_controllers}\n" res += f"TIME TOTAL \t= {t_cgg + t_s_controllers + t_t_controllers}\n\n" print(f"\n\nRESULTS:\n{res}") """Launch a simulation of n_steps where each contexts does change for at least t_min_context """ run = cgg.orchestrate(n_steps=50, t_min_context=6) print(run) Store.save_to_file(f"{res}", f"results.txt", absolute_folder_path=f"{result_folder}") """Save simulation as text file""" Store.save_to_file(str(run), file_name="run.txt", absolute_folder_path=f"{result_folder}") except CGGException as e: raise e
def save(self): Store.save_to_file(str(self), "cgg.txt", self.session_name)
from tools.strings import StringMng from tools.strix import Strix path = os.path.abspath(os.path.dirname(__file__)) if len(sys.argv) > 1: controller_name = sys.argv[1] else: controller_name = "5" print(f"controller selected: {path}/controller_specs/{controller_name}.txt") a, g, i, o = StringMng.parse_controller_specification_from_file( f"{path}/controller_specs/{controller_name}.txt") realizable, kiss_format, exec_time = Strix.generate_controller(a, g, i, o) controller = Controller(mealy_machine=kiss_format) Persistence.dump_controller(controller, f"{path}/controller_specs", controller_name) print("\n~~~MEALY MACHINE~~~\n" + str(controller)) Store.save_to_file(str(controller), f"{controller_name}_table.txt", absolute_folder_path=f"{path}/controller_specs") run = controller.simulate() print("\n\n\n~~~SIMULATION OF A RUN~~~\n" + run) Store.save_to_file(str(run), f"{controller_name}_run.txt", absolute_folder_path=f"{path}/controller_specs")