def varOccur(self): occur = {} for j, t in enumerate(self.terms): if t.__class__ == Var: util.setMapAdd(occur, t, self.relationDomain(j)) else: util.setMapMerge(occur, t.varOccur()) return occur
def processKIF(self, sents): initTerms = [] varOccur = [] for s in sents: if s.__class__ == Struct: functor = s.functor() if functor == ROLE: self.roles.append(s.terms[0]) self.rules.append(s) elif functor == INIT: initTerms.append(s.terms[0]) self.initRules.append(s) elif functor == DISTINCT or \ functor == TRUE or \ functor == NEXT: raise Exception('Unexpected functor: ' + str(functor)) else: if functor != LEGAL and \ functor != TERMINAL: setMapAdd(self.relationals, s.functor(), s) self.rules.append(s) elif s.__class__ == LogicalSentence: varOccur.extend(s.varOccur().values()) self.rules.append(s) self.processFunctions(s) self.initialState = State([self.stateIndex(t) for t in initTerms]) self.domains[RelationDomain(TRUE, 0)] = self.stateVars self.domains[RelationDomain(NEXT, 0)] = self.stateVars self.domains[RelationDomain(DOES, 1)] = self.actions self.domains[RelationDomain(LEGAL, 1)] = self.actions self.domains[RelationDomain(GOAL, 1)] = self.goals roleNames = set([t.functor() for t in self.roles]) self.domains[RelationDomain(ROLE, 0)] = roleNames self.domains[RelationDomain(DOES, 0)] = roleNames self.domains[RelationDomain(LEGAL, 0)] = roleNames self.domains[RelationDomain(GOAL, 0)] = roleNames for s in varOccur: current = set() for rd in s: if rd in self.domains: current.add(rd) if rd in self.sharedDomains: for rd2 in self.sharedDomains[rd]: current.add(rd2) for rd in current: self.sharedDomains[rd] = current uniqueDomains = [] for srd in self.sharedDomains.values(): if srd not in uniqueDomains: uniqueDomains.append(srd) self.uniteSet(srd) self.goals = set( [int(f.name) for f in self.domains[RelationDomain(GOAL, 1)]])
def processKIF(self, sents): initTerms = [] varOccur = [] for s in sents: if s.__class__ == Struct: functor = s.functor() if functor == ROLE: self.roles.append(s.terms[0]) self.rules.append(s) elif functor == INIT: initTerms.append(s.terms[0]) self.initRules.append(s) elif functor == DISTINCT or \ functor == TRUE or \ functor == NEXT: raise Exception('Unexpected functor: ' + str(functor)) else: if functor != LEGAL and \ functor != TERMINAL: setMapAdd(self.relationals, s.functor(), s) self.rules.append(s) elif s.__class__ == LogicalSentence: varOccur.extend(s.varOccur().values()) self.rules.append(s) self.processFunctions(s) self.initialState = State([self.stateIndex(t) for t in initTerms]) self.domains[RelationDomain(TRUE, 0)] = self.stateVars self.domains[RelationDomain(NEXT, 0)] = self.stateVars self.domains[RelationDomain(DOES, 1)] = self.actions self.domains[RelationDomain(LEGAL, 1)] = self.actions self.domains[RelationDomain(GOAL, 1)] = self.goals roleNames = set([t.functor() for t in self.roles]) self.domains[RelationDomain(ROLE, 0)] = roleNames self.domains[RelationDomain(DOES, 0)] = roleNames self.domains[RelationDomain(LEGAL, 0)] = roleNames self.domains[RelationDomain(GOAL, 0)] = roleNames for s in varOccur: current = set() for rd in s: if rd in self.domains: current.add(rd) if rd in self.sharedDomains: for rd2 in self.sharedDomains[rd]: current.add(rd2) for rd in current: self.sharedDomains[rd] = current uniqueDomains = [] for srd in self.sharedDomains.values(): if srd not in uniqueDomains: uniqueDomains.append(srd) self.uniteSet(srd) self.goals = set([int(f.name) for f in self.domains[RelationDomain(GOAL, 1)]])
def processFunctionalTerm(self, ft): functor = ft.functor() if functor not in self.functions: if functor in self.relations: raise Exception('Function already exists as relation: ' + str(functor)) if ft.arity() == 0: self.objects.add(functor) else: self.functions.add(functor) for rd in functor.relationDomains(): self.domains[rd] = set() for i, t in enumerate(ft.terms): if t.__class__ == Struct: setMapAdd(self.domains, ft.relationDomain(i), t.functor()) self.processFunctionalTerm(t)
def processFunctions(self, s): if s.__class__ == LogicalSentence: for sent in s.sents: self.processFunctions(sent) elif s.__class__ == Struct: functor = s.functor() if functor == INIT or \ functor == TRUE or \ functor == NEXT: t = s.terms[0] if t.__class__ == Struct: self.stateVars.add(t.functor()) elif functor == DOES or \ functor == LEGAL: t = s.terms[1] if t.__class__ == Struct: self.actions.add(t.functor()) elif functor == GOAL: t = s.terms[1] if t.__class__ == Struct: self.goals.add(t.functor()) elif functor == TERMINAL or \ functor == DISTINCT or \ functor == ROLE: pass else: if functor not in self.relations: if functor in self.functions: raise Exception('Relation exists as function') self.relations.add(functor) for rd in functor.relationDomains(): self.domains[rd] = set() for i in range(s.arity()): t = s.terms[i] if t.__class__ == Struct: setMapAdd(self.domains, s.relationDomain(i), t.functor()) for t in s.terms: if t.__class__ == Struct: self.processFunctionalTerm(t)