def expandTrue(self, base, index, workingSet, results): """ generated source for method expandTrue """ if base.arity() == index: results.add(GdlPool.getRule(GdlPool.getRelation(GdlPool.getConstant("true"), [None]*))) else: for term in (base.get(index)).getBody(): workingSet.addLast(term) self.expandTrue(base, index + 1, workingSet, results) workingSet.removeLast()
def initializeInstantiations(self, description): """ generated source for method initializeInstantiations """ trues = ArrayList() for gdl in description: if isinstance(gdl, (GdlSentence, )): if sentence.__name__.getValue() == "base": if sentence.arity() == 1: trues.add(GdlPool.getRule(GdlPool.getRelation(GdlPool.getConstant("true"), [None]*))) else: self.expandTrue(sentence, 1, LinkedList(), results) trues.addAll(results) instantiations = HashMap() instantiations.put(GdlPool.getConstant("true"), trues) return instantiations
def replaceVariablesInternal(cls, gdl, assignment): """ generated source for method replaceVariablesInternal """ if isinstance(gdl, (GdlProposition, )): return gdl elif isinstance(gdl, (GdlRelation, )): for term in relation.getBody(): newBody.add(cls.replaceVariables(term, assignment)) return GdlPool.getRelation(name, newBody) elif isinstance(gdl, (GdlConstant, )): return gdl elif isinstance(gdl, (GdlVariable, )): if assignment.containsKey(gdl): return assignment.get(gdl) else: return gdl elif isinstance(gdl, (GdlFunction, )): for term in function_.getBody(): newBody.add(cls.replaceVariables(term, assignment)) return GdlPool.getFunction(name, newBody) elif isinstance(gdl, (GdlDistinct, )): return GdlPool.getDistinct(arg1, arg2) elif isinstance(gdl, (GdlNot, )): return GdlPool.getNot(cls.replaceVariables(internal, assignment)) elif isinstance(gdl, (GdlOr, )): while i < or_.arity(): newInternals.add(cls.replaceVariables(or_.get(i), assignment)) i += 1 return GdlPool.getOr(newInternals) elif isinstance(gdl, (GdlRule, )): for conjunct in rule.getBody(): newBody.add(cls.replaceVariables(conjunct, assignment)) return GdlPool.getRule(newHead, newBody) else: raise RuntimeException("Unforeseen Gdl subtype " + gdl.__class__.getSimpleName())
def getInstantiationAux(self, gdl, varInstantiation): """ generated source for method getInstantiationAux """ if isinstance(gdl, (GdlRelation, )): while i < relation.arity(): body.add(self.getInstantiationAux(relation.get(i), varInstantiation)) i += 1 return GdlPool.getRelation(relation.__name__, body) elif isinstance(gdl, (GdlRule, )): while i < rule.arity(): body.add(self.getInstantiationAux(rule.get(i), varInstantiation)) i += 1 return GdlPool.getRule(head, body) elif isinstance(gdl, (GdlDistinct, )): return GdlPool.getDistinct(arg1, arg2) elif isinstance(gdl, (GdlNot, )): return GdlPool.getNot(body) elif isinstance(gdl, (GdlOr, )): while i < or_.arity(): body.add(self.getInstantiationAux(or_.get(i), varInstantiation)) i += 1 return GdlPool.getOr(body) elif isinstance(gdl, (GdlProposition, )): return gdl elif isinstance(gdl, (GdlConstant, )): return gdl elif isinstance(gdl, (GdlFunction, )): while i < func.arity(): body.add(self.getInstantiationAux(func.get(i), varInstantiation)) i += 1 return GdlPool.getFunction(func.__name__, body) elif isinstance(gdl, (GdlVariable, )): return varInstantiation.get(variable) else: raise RuntimeException("Someone went and extended the GDL hierarchy without updating this code.")
def findAndInstantiateBaseProps(self, gdl): """ generated source for method findAndInstantiateBaseProps """ if isinstance(gdl, (GdlRelation, )): if name == "init": if relation.arity() != 1: raise RuntimeException("Can't init more than one thing as far as I know.") if isinstance(template, (GdlConstant, )): body.add(template) self.baseRelations.add(toAdd) System.err.println("Weird init of constant") elif isinstance(template, (GdlVariable, )): System.err.println("Weird init of constant") body.add(self.universalDom) self.baseRelations.add(toAdd) System.err.println("Weird init of variable") elif isinstance(template, (GdlFunction, )): instantiateBaseProps(func.toSentence()) elif isinstance(gdl, (GdlRule, )): if name == "next": if head.arity() != 1: raise RuntimeException("Can't next more than one thing as far as I know.") if isinstance(, (GdlVariable, )): # weird case where you have rule like (next ?q) l.idx = 0 l.name = head.__name__ for c in dom.values: body.add(c) self.baseRelations.add(GdlPool.getRelation(self.baseConstant, body)) else: instantiateBasePropsWithRHS(head.get(0).toSentence(), rule.getBody())
def applyCondensation(cls, condensationSet, rule, sentenceNameSource): """ generated source for method applyCondensation """ varsInCondensationSet = HashSet() for literal in condensationSet: varsInCondensationSet.addAll(GdlUtils.getVariables(literal)) varsToKeep = HashSet() for literal in condensationSet: varsToKeep.addAll(GdlUtils.getVariables(literal)) varsToKeep2 = HashSet() varsToKeep2.addAll(GdlUtils.getVariables(rule.getHead())) for literal in rule.getBody(): if not condensationSet.contains(literal): varsToKeep2.addAll(GdlUtils.getVariables(literal)) varsToKeep.retainAll(varsToKeep2) orderedVars = ArrayList(varsToKeep) condenserName = sentenceNameSource.getNameWithPrefix(rule.getHead().__name__) condenserHead = GdlSentence() if orderedVars.isEmpty(): condenserHead = GdlPool.getProposition(condenserName) else: condenserHead = GdlPool.getRelation(condenserName, orderedVars) condenserBody = ArrayList(condensationSet) condenserRule = GdlPool.getRule(condenserHead, condenserBody) remainingLiterals = ArrayList() for literal in rule.getBody(): if not condensationSet.contains(literal): remainingLiterals.add(literal) remainingLiterals.add(condenserHead) modifiedRule = GdlPool.getRule(rule.getHead(), remainingLiterals) newRules = ArrayList(2) newRules.add(condenserRule) newRules.add(modifiedRule) return newRules
def addMissingInputs(self): """ generated source for method addMissingInputs """ addList = ArrayList() for proposition in propositions.values(): if isinstance(, (GdlRelation, )): if relation.__name__.getValue() == "legal": addList.add(proposition) for addItem in addList: self.components.add(getProposition(GdlPool.getRelation(GdlPool.getConstant("does"), relation.getBody())))
def substituteRelation(cls, relation, theta): """ generated source for method substituteRelation """ if relation.isGround(): return relation else: while i < relation.arity(): body.add(substituteTerm(relation.get(i), theta)) i += 1 return GdlPool.getRelation(name, body)
def checkValidity(self, theGame): """ generated source for method checkValidity """ try: sm.initialize(theGame.getRules()) if len(bases) == 0: raise ValidatorException("Could not find base propositions.") elif len(inputs) == 0: raise ValidatorException("Could not find input propositions.") for base in bases: truesFromBases.add(GdlPool.getRelation(self.TRUE, base.getBody())) for input in inputs: legalsFromInputs.add(GdlPool.getRelation(self.LEGAL, input.getBody())) if truesFromBases.isEmpty() and legalsFromInputs.isEmpty(): return ImmutableList.of() while System.currentTimeMillis() < startTime + self.millisecondsToTest: # Check state against bases, inputs if not truesFromBases.isEmpty(): if not truesFromBases.containsAll(state.getContents()): missingBases.addAll(state.getContents()) missingBases.removeAll(truesFromBases) raise ValidatorException("Found missing bases: " + missingBases) if not legalsFromInputs.isEmpty(): for role in sm.getRoles(): for move in legalMoves: legalSentences.add(GdlPool.getRelation(self.LEGAL, [None]*)) if not legalsFromInputs.containsAll(legalSentences): missingInputs.addAll(legalSentences) missingInputs.removeAll(legalsFromInputs) raise ValidatorException("Found missing inputs: " + missingInputs) state = sm.getRandomNextState(state) if sm.isTerminal(state): state = initialState except MoveDefinitionException as mde: raise ValidatorException("Could not find legal moves while simulating: " + mde) except TransitionDefinitionException as tde: raise ValidatorException("Could not find transition definition while simulating: " + tde) except RuntimeException as e: raise ValidatorException("Ran into a runtime exception while simulating: " + e) except StackOverflowError as e: raise ValidatorException("Ran into a stack overflow while simulating: " + e) except OutOfMemoryError as e: raise ValidatorException("Ran out of memory while simulating: " + e) return ImmutableList.of()
def cleanParentheses_1(cls, sentence): """ generated source for method cleanParentheses_1 """ if isinstance(sentence, (GdlProposition, )): return sentence cleanedBody = ArrayList() for term in sentence.getBody(): cleanedBody.add(cls.cleanParentheses(term)) if len(cleanedBody) == 0: return GdlPool.getProposition(sentence.__name__) else: return GdlPool.getRelation(sentence.__name__, cleanedBody)
def fixDisjunctions(self): """ generated source for method fixDisjunctions """ fixList = ArrayList() for proposition in propositions.values(): if proposition.getInputs().size() > 1: fixList.add(proposition) for fixItem in fixList: for input in fixItem.getInputs(): i += 1 if isinstance(, (GdlProposition, )): disjunct = Proposition(GdlPool.getProposition(GdlPool.getConstant(proposition.__name__.getValue() + "-" + i))) else: disjunct = Proposition(GdlPool.getRelation(GdlPool.getConstant(relation.__name__.getValue() + "-" + i), relation.getBody())) input.getOutputs().clear() link(input, disjunct) link(disjunct, or_) self.components.add(disjunct) fixItem.getInputs().clear() link(or_, fixItem) self.components.add(or_)
def instantiateBaseProps(self, template): """ generated source for method instantiateBaseProps """ body = ArrayList() body.add(template.__name__) i = 0 while i < template.arity(): if isinstance(arg, (GdlConstant, )): domBody.add(arg) body.add(dom) elif isinstance(arg, (GdlVariable, )): loc.idx = i loc.name = template.__name__ if varDom == None: raise RuntimeException("Unexpected domain: " + loc + " encountered.") domBody.addAll(varDom.values) body.add(dom) elif isinstance(arg, (GdlFunction, )): raise RuntimeException("Don't know how to deal with functions within next/init.") i += 1 toAdd = GdlPool.getRelation(self.baseConstant, body) self.baseRelations.add(toAdd)
def getSentenceFromTuple(self, tuple_): """ generated source for method getSentenceFromTuple """ if len(tuple_) != self.tupleSize: raise IllegalArgumentException("Passed tuple of the wrong size to a sentence form: " + "tuple was " + tuple_ + ", sentence form is " + self) if len(tuple_) < self.arity: raise IllegalStateException("Something is very wrong, probably fixable by the GdlCleaner; name: " + self.name + "; arity: " + self.arity + "; tupleSize: " + self.tupleSize) sentenceBody = Lists.newArrayList() curIndex = 0 i = 0 while i < self.arity: Preconditions.checkArgument(not (isinstance(term, (GdlFunction, )))) if self.functions.containsKey(i): sentenceBody.add(functionForm.getFunctionFromTuple(tuple_, curIndex)) curIndex += functionForm.getTupleSize() else: sentenceBody.add(term) curIndex += 1 i += 1 if self.arity == 0: return GdlPool.getProposition(self.name) else: return GdlPool.getRelation(self.name, sentenceBody)
def replaceVariableInternal(cls, gdl, toSubstitute, theReplacement): """ generated source for method replaceVariableInternal """ if isinstance(gdl, (GdlDistinct, )): return GdlPool.getDistinct(cls.replaceVariableInternal((gdl).getArg1(), toSubstitute, theReplacement), cls.replaceVariableInternal((gdl).getArg2(), toSubstitute, theReplacement)) elif isinstance(gdl, (GdlNot, )): return GdlPool.getNot(cls.replaceVariableInternal((gdl).getBody(), toSubstitute, theReplacement)) elif isinstance(gdl, (GdlOr, )): while i < or_.arity(): rval.add(cls.replaceVariableInternal(or_.get(i), toSubstitute, theReplacement)) i += 1 return GdlPool.getOr(rval) elif isinstance(gdl, (GdlProposition, )): return gdl elif isinstance(gdl, (GdlRelation, )): while i < rel.arity(): rval.add(cls.replaceVariableInternal(rel.get(i), toSubstitute, theReplacement)) i += 1 return GdlPool.getRelation(rel.__name__, rval) elif isinstance(gdl, (GdlRule, )): while i < rule.arity(): rval.add(cls.replaceVariableInternal(rule.get(i), toSubstitute, theReplacement)) i += 1 return GdlPool.getRule(cls.replaceVariableInternal(rule.getHead(), toSubstitute, theReplacement), rval) elif isinstance(gdl, (GdlConstant, )): return gdl elif isinstance(gdl, (GdlFunction, )): while i < func.arity(): rval.add(cls.replaceVariableInternal(func.get(i), toSubstitute, theReplacement)) i += 1 return GdlPool.getFunction(func.__name__, rval) elif isinstance(gdl, (GdlVariable, )): if gdl == toSubstitute: return theReplacement else: return gdl else: raise RuntimeException("Uh oh, gdl hierarchy must have been extended without updating this code.")
def toDoes(cls, role, move): """ generated source for method toDoes """ return GdlPool.getRelation(cls.DOES, [None]*)
def getLegalQuery(cls, role): """ generated source for method getLegalQuery """ return GdlPool.getRelation(cls.LEGAL, [None]*)
def toState(self, results): """ generated source for method toState """ trues = HashSet() for result in results: trues.add(GdlPool.getRelation(self.TRUE, [None]*)) return MachineState(trues)
def replaceHead(cls, sentence, newHead): """ generated source for method replaceHead """ return GdlPool.getRelation(newHead, sentence.getBody())