コード例 #1
0
ファイル: CondensationIsolator.py プロジェクト: hobson/ggpy
 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
コード例 #2
0
 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()
コード例 #3
0
 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
コード例 #4
0
ファイル: PropNetFlattener.py プロジェクト: hobson/ggpy
 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.")
コード例 #5
0
ファイル: PropNetFlattener.py プロジェクト: hobson/ggpy
 def getAllInstantiations(self):
     """ generated source for method getAllInstantiations """
     rval = ArrayList()
     for gdl in description:
         if isinstance(gdl, (GdlRelation, )):
             if name == "base":
                 continue 
             rval.add(GdlPool.getRule(relation))
     for d in domains.values():
         for r in d.ruleRefs:
             for varInstantiation in varInstantiations:
                 if varInstantiation.containsValue(None):
                     raise RuntimeException("Shouldn't instantiate anything to null.")
                 rval.add(getInstantiation(r.originalRule, varInstantiation))
                 if rval.get(len(rval) - 1).__str__().contains("null"):
                     raise RuntimeException("Shouldn't instantiate anything to null: " + rval.get(len(rval) - 1).__str__())
     for ruleRef in extraRefs:
         for c in ruleRef.conditions:
             if c.dom == None:
                 c.updateDom()
             if c.dom != None:
                 newConditions.add(c)
         if len(newConditions) != len(ruleRef.conditions):
             ruleRef.conditions = newConditions
     for r in extraRefs:
         for varInstantiation in varInstantiations:
             if varInstantiation.containsValue(None):
                 raise RuntimeException("Shouldn't instantiate anything to null.")
             rval.add(getInstantiation(r.originalRule, varInstantiation))
             if rval.get(len(rval) - 1).__str__().contains("null"):
                 raise RuntimeException("Shouldn't instantiate anything to null.")
         if len(varInstantiations) == 0:
             rval.add(getInstantiation(r.originalRule, HashMap()))
     return rval
コード例 #6
0
ファイル: CommonTransforms.py プロジェクト: hobson/ggpy
 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())
コード例 #7
0
ファイル: GdlCleaner.py プロジェクト: hobson/ggpy
 def cleanParentheses(cls, rule):
     """ generated source for method cleanParentheses """
     cleanedHead = cls.cleanParentheses(rule.getHead())
     cleanedBody = ArrayList()
     for literal in rule.getBody():
         cleanedBody.add(cls.cleanParentheses(literal))
     return GdlPool.getRule(cleanedHead, cleanedBody)
コード例 #8
0
ファイル: GdlCleaner.py プロジェクト: hobson/ggpy
 def removeNotDistinctLiteral(cls, rule, notDistinctLiteral):
     """ generated source for method removeNotDistinctLiteral """
     # Figure out the substitution we want...
     # If we have two constants: Either remove one or
     # maybe get rid of the ___?
     # One is a variable: Replace the variable with the other thing
     # throughout the rule
     distinct = notDistinctLiteral.getBody()
     arg1 = distinct.getArg1()
     arg2 = distinct.getArg2()
     if arg1 == arg2:
         # Just remove that literal
         newBody.addAll(rule.getBody())
         newBody.remove(notDistinctLiteral)
         return GdlPool.getRule(rule.getHead(), newBody)
     if isinstance(arg1, (GdlVariable, )):
         # What we return will still have the not-distinct literal,
         # but it will get replaced in the next pass.
         # (Even if we have two variables, they will be equal next time through.)
         return CommonTransforms.replaceVariable(rule, arg1, arg2)
     if isinstance(arg2, (GdlVariable, )):
         return CommonTransforms.replaceVariable(rule, arg2, arg1)
     if isinstance(arg1, (GdlConstant, )) or isinstance(arg2, (GdlConstant, )):
         # We have two non-equal constants, or a constant and a function.
         # The rule should have no effect.
         return None
     # We have two functions. Complicated! (Have to replace them with unified version.)
     # We pass on this case for now.
     # TODO: Implement correctly.
     raise UnsupportedOperationException("We can't currently handle (not (distinct <function> <function>)).")
コード例 #9
0
ファイル: Substituter.py プロジェクト: hobson/ggpy
 def substituteRule(cls, rule, theta):
     """ generated source for method substituteRule """
     head = cls.substitute(rule.getHead(), theta)
     body = ArrayList()
     for literal in rule.getBody():
         body.add(cls.substituteLiteral(literal, theta))
     return GdlPool.getRule(head, body)
コード例 #10
0
ファイル: DeORer.py プロジェクト: hobson/ggpy
 def run(cls, description):
     """ generated source for method run """
     newDesc = ArrayList()
     for gdl in description:
         if isinstance(gdl, (GdlRule, )):
             for body in newBodies:
                 newDesc.add(GdlPool.getRule(rule.getHead(), body))
         else:
             newDesc.add(gdl)
     return newDesc
コード例 #11
0
ファイル: CommonTransforms.py プロジェクト: hobson/ggpy
 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.")
コード例 #12
0
ファイル: DistinctAndNotMover.py プロジェクト: hobson/ggpy
 def reorderRule(cls, oldRule):
     """ generated source for method reorderRule """
     newBody = Lists.newArrayList(oldRule.getBody())
     rearrangeDistinctsAndNots(newBody)
     return GdlPool.getRule(oldRule.getHead(), newBody)