def __or__(self, other): """self | other Returns a new LTL with the disjunction with other""" if not isinstance(other, LTL): return AttributeError formula = Or([self.formula, other.formula]) variables = Variables(self.variables | other.variables) return LTL(formula=formula, variables=variables)
def extract_ltl_rules(context_rules: Dict) -> List[LTL]: """Dict: ltl_formula -> list_of_variables_involved""" ltl_list: List[LTL] = [] if "mutex" in context_rules: for cvars in context_rules["mutex"]: if len(cvars) > 0: variables: Variables = Variables() ltl = "G(" for vs in cvars: variables.extend(vs.variables) cvars_str = [n.formula for n in cvars] clauses = [] for vs_a in cvars_str: clause = [] clause.append(deepcopy(vs_a)) for vs_b in cvars_str: if vs_a is not vs_b: clause.append(Not(deepcopy(vs_b))) clauses.append(And(clause)) ltl += Or(clauses) ltl += ")" ltl_list.append(LTL(formula=ltl, variables=variables)) if "inclusion" in context_rules: for cvars in context_rules["inclusion"]: if len(cvars) > 0: variables: Variables = Variables() ltl = "G(" for i, vs in enumerate(cvars): variables.extend(vs.variables) ltl += str(vs) if i < (len(cvars) - 1): ltl += " -> " ltl += ")" ltl_list.append(LTL(formula=ltl, variables=variables)) if "liveness" in context_rules: for cvars in context_rules["liveness"]: variables: Variables = Variables() ltl = "G( F(" variables.extend(cvars.variables) ltl += str(cvars) ltl += "))" ltl_list.append(LTL(formula=ltl, variables=variables)) return ltl_list
def prioritize_goal(first_priority_goal, second_priority_goal): pass """ Makes the assumption of one goal dependent on the satisfiability of the assumptions of the second goal """ variables = [] stronger_assumptions_list = [] for contract in first_priority_goal.contracts: variables |= contract.variables stronger_assumptions_list.append(And(contract.assumptions)) for contract in second_priority_goal.contracts: contract.add_variables(variables) contract.add_assumptions(Not(Or(stronger_assumptions_list)))
def create_general_controller_from_goals(goals: List[CGTGoal], folder_path: str, type: str): assumptions = [] guarantees = [] inputs = set() outputs = set() for goal in goals: assum, guaran, ins, outs = generate_general_controller_inputs_from_goal( ap, rules, goal, complete=True) assumptions.extend(assum) guarantees.extend(guaran) inputs.update(set(ins)) outputs.update(set(outs)) controller_file_name = folder_path + "specification.txt" if type == "AND": save_to_file( generate_controller_input_text(assumptions, guarantees, list(inputs), list(outputs)), controller_file_name) elif type == "OR": save_to_file( generate_controller_input_text([Or(assumptions)], guarantees, list(inputs), list(outputs)), controller_file_name) else: raise Exception("type error, either AND or OR") controller_generated = False trivial = False exec_time = 0.0 try: controller_generated, exec_time = create_controller_if_exists( controller_file_name) except SynthesisException as e: if e.os_not_supported: print("Os not supported for synthesis. Only linux can run strix") elif e.trivial: trivial = True controller_generated = True print( "The assumptions are not satisfiable. The controller is trivial." ) return controller_generated, trivial, exec_time
def extract_saturated_guarantees_from( goals: Union[CGTGoal, List[CGTGoal]]) -> List[str]: if isinstance(goals, CGTGoal): goals = [goals] assumptions_goals = [] guarantees_goals = [] variables_goals = set() for goal in goals: goal_variables = set() goal_new_variables = set() goal_old_variables = set() assumptions_guarantee_pairs = [] for contract in goal.contracts: a_boolean, a_new_vars, a_old_vars = traslate_boolean( str(contract.assumptions.formula)) g_boolean, g_new_vars, g_old_vars = traslate_boolean( str(contract.guarantees.formula)) vars = [v.name for v in contract.variables.list] goal_variables.update(vars) a_new_vars.extend(g_old_vars) a_old_vars.extend(g_old_vars) goal_new_variables.update(a_new_vars) goal_old_variables.update(a_old_vars) assumptions_guarantee_pairs.append((a_boolean, g_boolean)) goal_variables = goal_variables - goal_old_variables goal_variables.update(goal_new_variables) assumptions_goal = Or([a for (a, goal) in assumptions_guarantee_pairs]) guarantees_goal = And( [Implies(a, goal) for (a, goal) in assumptions_guarantee_pairs]) guarantees_goals.append(guarantees_goal) variables_goals.update(goal_variables) return guarantees_goals
def __ior__(self, other): """self |= other Modifies self with the disjunction with other""" if not isinstance(other, LTL): return AttributeError if self.__formula == "TRUE": return self if other.formula == "FALSE": return self if other.formula == "TRUE": self.__formula = "TRUE" self.__cnf |= other.cnf return self self.__formula = Or([self.formula, other.formula]) self.__variables = Variables(self.variables | other.variables) # """TODO: maybe not needed""" # if not self.is_satisfiable(): # raise InconsistentException(self, other) return self
def extract_rules(rules: Dict) -> Dict: """Translates the rules in LTL formulae and returnes a dictionary of 5 cathegories""" """Dictionary to return""" rules_ltl = {} if "gridworld" in rules: rules_ltl["gridworld"] = [] for elem, adjacent in rules["gridworld"].items(): ltl = "G(" ltl += elem.formula + " -> X (" ltl += " | ".join([a.formula for a in adjacent]) ltl += "))" variables = Variables() variables |= elem.variables for a in adjacent: variables |= a.variables rules_ltl["gridworld"].append( LTL(formula=ltl, variables=variables, kind="gridworld")) if "context" in rules: rules_ltl["context"] = [] if "mutex" in rules["context"]: for mtx_elements in rules["context"]["mutex"]: if len(mtx_elements) > 0: variables: Variables = Variables() ltl = "G(" for vs in mtx_elements: variables |= vs.variables mtx_elements_str = [n.formula for n in mtx_elements] clauses = [] for vs_a in mtx_elements_str: clause = [deepcopy(vs_a)] for vs_b in mtx_elements_str: if vs_a is not vs_b: clause.append(Not(deepcopy(vs_b))) clauses.append(And(clause)) ltl += Or(clauses) ltl += ")" rules_ltl["context"].append( LTL(formula=ltl, variables=variables, kind="context")) if "inclusion" in rules["context"]: for pre, post in rules["context"]["inclusion"].items(): variables = Variables() variables |= pre.variables | post.variables ltl = "G((" + pre.formula + ") -> (" + post.formula + "))" rules_ltl["context"].append( LTL(formula=ltl, variables=variables, kind="context")) if "context_gridworld" in rules: rules_ltl["context_gridworld"] = [] for pre, post in rules["context_gridworld"].items(): variables = Variables() variables |= pre.variables | post.variables ltl = "G((" + pre.formula + ") -> (" + post.formula + "))" rules_ltl["context_gridworld"].append( LTL(formula=ltl, variables=variables, kind="context_gridworld")) if "constraints" in rules: rules_ltl["constraints"] = [] if "mutex" in rules["constraints"]: for mtx_elements in rules["constraints"]["mutex"]: if len(mtx_elements) > 0: variables: Variables = Variables() ltl = "G(" for vs in mtx_elements: variables |= vs.variables mtx_elements_str = [n.formula for n in mtx_elements] clauses = [] for vs_a in mtx_elements_str: clause = [deepcopy(vs_a)] for vs_b in mtx_elements_str: if vs_a is not vs_b: clause.append(Not(deepcopy(vs_b))) clauses.append(And(clause)) ltl += Or(clauses) ltl += ")" rules_ltl["constraints"].append( LTL(formula=ltl, variables=variables, kind="constraints")) if "inclusion" in rules["constraints"]: for pre, post in rules["constraints"]["inclusion"].items(): variables = Variables() variables |= pre.variables | post.variables ltl = "G((" + pre.formula + ") -> (" + post.formula + "))" rules_ltl["constraints"].append( LTL(formula=ltl, variables=variables, kind="constraints")) return rules_ltl