def newStep(self, plan, flaw): results = set() s_need, precondition = flaw.flaw #antecedents = self.GL.pre_dict[precondition.replaced_ID] antecedents = self.GL.id_dict[precondition.replaced_ID] for antenum in antecedents: ante = self.GL[antenum] if ante.stepnumber == plan.initial_dummy_step.stepnumber: continue new_plan = plan.deepcopy() antestep = ante.deepcopy(replace_internals=True) eff = self.IntegrateNewStep(new_plan, antestep, precondition) #add step, orderings and causal links, add flaws self.addStep(new_plan, s_add=antestep, s_need=new_plan.getElementById(s_need.ID), condition=Condition.subgraph(new_plan, eff), new=True) new_plan.flaws.addCndtsAndRisks(self.GL, antestep.root) results.add(new_plan) return results
def RetargetPrecondition(self, GL, plan, S_Old, precondition): effect_token = GL.getConsistentEffect(S_Old, precondition) if plan.name == 'disc': #retargetArgs() Eff = list(Condition.subgraph(S_Old, effect_token).Args) Pre = list(Condition.subgraph(plan, precondition).Args) if not isIdenticalElmsInArgs(Pre, Eff): return False pre_link = plan.RemoveSubgraph(precondition) #push plan.edges.remove(pre_link) #mutate pre_link.sink = effect_token #pop plan.edges.add(pre_link) return pre_link.sink
def IntegrateNewStep(self, new_plan, antestep, precondition): eff = self.GL.getConsistentEffect(antestep, precondition) if antestep.is_decomp: PArgs = list(precondition.Args) EArgs = list(Condition.subgraph(antestep, eff).Args) retargetArgs(antestep, EArgs, PArgs) retargetElmsInArgs(antestep.ground_subplan, EArgs, PArgs) to_add = set() for link in list(antestep.ground_subplan.CausalLinkGraph.edges): to_add.add(Edge(link.source, link.sink, Condition.subgraph(antestep.ground_subplan, link.label))) antestep.ground_subplan.CausalLinkGraph.edges = to_add new_plan.flaws.insert(self.GL, new_plan, DCF(antestep.ground_subplan, 'dcf')) eff_link = antestep.RemoveSubgraph(eff) eff_link.sink = new_plan.getElementById(precondition.root.ID) eff_link.sink.replaced_ID = eff.replaced_ID new_plan.edges.add(eff_link) new_plan.elements.update(antestep.elements) new_plan.edges.update(antestep.edges) return eff_link.sink
def loadAnteSteps(self, _step, _pre): Precondition = Condition.subgraph(_step, _pre) #if _step.stepnumber for gstep in self._gsteps: # Defense pattern count = 0 for _eff in gstep.effects: # Defense 2 if not _eff.isConsistent(_pre): # Defense 2.1 if not _eff.isOpposite(_pre): continue # Defense 2.2 Effect = Condition.subgraph(gstep, _eff) if Effect.Args != Precondition.Args: continue self.threat_dict[_step.stepnumber].add(gstep.stepnumber) continue # Defense 3 Effect = Condition.subgraph(gstep, _eff) if Effect.Args != Precondition.Args: continue # Create antestep antestep = copy.deepcopy(gstep) eff_link = antestep.RemoveSubgraph(_eff) antestep.replaceInternals() self.pre_dict[_pre.replaced_ID].add( Antestep(antestep, eff_link)) self.id_dict[_pre.replaced_ID].add(antestep.stepnumber) self.eff_dict[_pre.replaced_ID].add(eff_link.sink.replaced_ID) count += 1 if count > 0: self.ante_dict[_step.stepnumber].add(gstep.stepnumber)
def loadAnteSteps(self, _step, _pre): Precondition = Condition.subgraph(_step, _pre) #if _step.stepnumber for gstep in self._gsteps: # Defense pattern count = 0 for _eff in gstep.effects: # Defense 2 if not _eff.isConsistent(_pre): # Defense 2.1 if not _eff.isOpposite(_pre): continue # Defense 2.2 Effect = Condition.subgraph(gstep, _eff) if Effect.Args != Precondition.Args: continue self.threat_dict[_step.stepnumber].add(gstep.stepnumber) continue # Defense 3 Effect = Condition.subgraph(gstep,_eff) if Effect.Args != Precondition.Args: continue # Create antestep antestep = copy.deepcopy(gstep) eff_link = antestep.RemoveSubgraph(_eff) antestep.replaceInternals() self.pre_dict[_pre.replaced_ID].add(Antestep(antestep, eff_link)) self.id_dict[_pre.replaced_ID].add(antestep.stepnumber) self.eff_dict[_pre.replaced_ID].add(eff_link.sink.replaced_ID) count += 1 if count > 0: self.ante_dict[_step.stepnumber].add(gstep.stepnumber)
def reuse(self, plan, flaw): results = set() s_need, precondition = flaw.flaw #antecedents - a set of stepnumbers antecedents = self.GL.id_dict[precondition.replaced_ID] if len(antecedents) == 0: return set() for s_old in plan.Steps: if s_old.stepnumber not in antecedents: continue if s_old == s_need: continue new_plan = plan.deepcopy() Old = Action.subgraph(new_plan, s_old) effect_token = self.GL.getConsistentEffect(Old, precondition) #joint_literal = self.RetargetPrecondition(self.GL, new_plan, Old, precondition) if Old.is_decomp or s_need.is_decomp: if not isIdenticalElmsInArgs(precondition.Args, Condition.subgraph(Old, effect_token).Args): continue else: print("not identical") effect_edge = new_plan.ReplaceSubgraphs(precondition.root, effect_token) #add step, orderings, causal links, and create flaws self.addStep(new_plan, Old, s_need=new_plan.getElementById(s_need.ID), condition=Condition.subgraph(new_plan, effect_edge.sink), new=False) results.add(new_plan) return results
def AddLink(link, new_plan, UW, remove_flaw=True): Source = Action.subgraph(new_plan, UW[link.source.position]) new_d = Source.getElmByRID(link.label.replaced_ID) if new_d is None: Sink = Action.subgraph(new_plan, UW[link.sink.position]) new_d = Sink.getElmByRID(link.label.replaced_ID) D = Condition.subgraph(new_plan, new_d) new_plan.CausalLinkGraph.addEdge(UW[link.source.position], UW[link.sink.position],D) if remove_flaw: flaws = new_plan.flaws.flaws f = Flaw((UW[link.sink.position], D), 'opf') if f in flaws: new_plan.flaws.remove(f)
def Groundify(Planets, GL, has_links): print('...Groundify - Unifying Actions with GL') for i, Planet in enumerate(Planets): print("... Planet {}".format(i)) for Step in Planet.Step_Graphs: print('... Unifying {} with {}'.format(Step, GL[Step.stepnumber])) # Unify Actions (1) swaps step graphs with ground step Planet.UnifyActions(Step, GL[Step.stepnumber]) if not has_links: return Planets print('...Groundify - Creating Causal Links') Discovered_Planets = [] for Plan in Planets: #print(Plan) Libs = [LinkLib(i, link, GL) for i, link in enumerate(Plan.CausalLinkGraph.edges)] #LW = [plan1 [link1.condition, link2.condition,..., link-n.condition], # plan2 [link1.condition, link2.condition, ..., link-m.condition], # plan-k [l1,...,lz]] LW = productByPosition(Libs) for lw in LW: # create new Planet ("discovered planet") for each linkworld. NP = Plan.deepcopy() for _link in list(lw): pre_token = GL.getConsistentPrecondition(Action.subgraph(NP, _link.sink), _link.label) #label = NP.getElementByID(_link.label.ID) if pre_token != _link.label: NP.ReplaceSubgraphs(pre_token, _link.label) NP.CausalLinkGraph.edges.remove(_link) NP.CausalLinkGraph.edges.add(Edge(_link.source, _link.sink, Condition.subgraph(NP, _link.label))) Discovered_Planets.append(NP) return Discovered_Planets
def newStep(self, plan, flaw, GL): """ @param plan: @param flaw: @return: """ results = set() s_need, precondition = flaw.flaw antecedents = GL.pre_dict[precondition.replaced_ID] for ante in antecedents: if ante.action.name == 'dummy_init': continue #step 1 - make a copy cndt = copy.deepcopy(ante) #step 2 - replace its internals, to distinguish from other identical antesteps (anteaction, eff_link) = cndt anteaction.replaceInternals() # step 3 - make a copy of the plan new_plan = plan.deepcopy() if plan.name == 'disc': anteaction = GL[anteaction.stepnumber].deepcopy() anteaction.replaceInternals() eff = anteaction.getElmByRID(eff_link.sink.replaced_ID) PArgs = list(Condition.subgraph(new_plan, precondition).Args) EArgs = list(Condition.subgraph(anteaction, eff).Args) #Retarget args in discourse step. retargetArgs(anteaction, EArgs, PArgs) #Retarget elements in args in ground subplan retargetElmsInArgs(anteaction.ground_subplan, EArgs, PArgs) #Then, add a flaw which says, add ground subplan, first looking if elm.ID already exists new_plan.flaws.decomps.add( Flaw(anteaction.ground_subplan, 'dcf')) # for edge in anteaction.edges: # if edge.source == anteaction.root and edge.sink == eff: # eff_link = edge # break eff_link = anteaction.RemoveSubgraph(eff) #step 4 - set sink before replace internals preserve_original_id = eff_link.sink.replaced_ID eff_link.sink = new_plan.getElementById(precondition.ID) eff_link.sink.replaced_ID = preserve_original_id new_plan.edges.add(eff_link) #step 5 - add new stuff to new plan new_plan.elements.update(anteaction.elements) new_plan.edges.update(anteaction.edges) #step 6 - update orderings and causal links, add flaws self.addStep(new_plan, anteaction.root, new_plan.getElementById(s_need.ID), eff_link.sink, GL, new=True) new_plan.flaws.addCndtsAndRisks(GL, anteaction.root) #step 7 - add new_plan to open list results.add(new_plan) return results