def addSentencesTrueByRulesDifferentially(cls, sentencesByForm, domainModel, reasoner): """ generated source for method addSentencesTrueByRulesDifferentially """ model = domainModel constantForms = model.getConstantSentenceForms() # Find the part of the dependency graph dealing only with the constant forms. dependencySubgraph = Multimaps.filterKeys(model.getDependencyGraph(), Predicates.in_(constantForms)) dependencySubgraph = Multimaps.filterValues(model.getDependencyGraph(), Predicates.in_(constantForms)) dependencySubgraph = ImmutableMultimap.copyOf(dependencySubgraph) ordering = DependencyGraphs.toposortSafe(constantForms, dependencySubgraph) for stratum in ordering: # One non-differential pass, collecting the changes for form in stratum: for rule in model.getRules(form): if not reasoner.isSubsetOf(sentencesByForm, ruleResults): sentencesByForm = reasoner.getUnion(sentencesByForm, ruleResults) newlyTrueSentences = reasoner.getUnion(newlyTrueSentences, ruleResults) # Now a lot of differential passes to deal with recursion efficiently while somethingChanged: somethingChanged = False for form in stratum: for rule in model.getRules(form): if not reasoner.isSubsetOf(sentencesByForm, ruleResults): somethingChanged = True newStuffInThisPass = reasoner.getUnion(newStuffInThisPass, ruleResults) sentencesByForm = reasoner.getUnion(sentencesByForm, newStuffInThisPass) newlyTrueSentences = newStuffInThisPass
def createWithForwardChaining(cls, model): """ generated source for method createWithForwardChaining """ reasoner = GdlChainingReasoner.create(model) sentencesByForm = reasoner.getConstantSentences() addSentencesTrueByRulesDifferentially(sentencesByForm, model, reasoner) return ImmutableConstantChecker.create(model, Multimaps.filterKeys(sentencesByForm.getSentences(), Predicates.in_(model.getConstantSentenceForms())))