Beispiel #1
0
 def getNTermModels(cls, arity):
     """ generated source for method getNTermModels """
     result = Lists.newArrayListWithCapacity(arity)
     i = 0
     while i < arity:
         result.add(TermModel())
         i += 1
     return result
Beispiel #2
0
 def getNUnderscores(cls, numTerms):
     """ generated source for method getNUnderscores """
     underscore = GdlPool.UNDERSCORE
     terms = Lists.newArrayListWithCapacity(numTerms)
     i = 0
     while i < numTerms:
         terms.add(underscore)
         i += 1
     return terms
Beispiel #3
0
 def run(cls, oldRules):
     """ generated source for method run """
     oldRules = DeORer.run(oldRules)
     newRules = Lists.newArrayListWithCapacity(len(oldRules))
     for gdl in oldRules:
         if isinstance(gdl, (GdlRule, )):
             newRules.add(reorderRule(rule))
         else:
             newRules.add(gdl)
     return newRules
Beispiel #4
0
 def __init__(self, headAssignment, rule, varDomains, functionInfoMap, completedSentenceFormValues):
     """ generated source for method __init__ """
     super(AssignmentsImpl, self).__init__()
     self.empty = False
     self.headAssignment = headAssignment
     # We first have to find the remaining variables in the body
     self.varsToAssign = GdlUtils.getVariables(rule)
     # Remove all the duplicates; we do, however, want to keep the ordering
     newVarsToAssign = ArrayList()
     for v in varsToAssign:
         if not newVarsToAssign.contains(v):
             newVarsToAssign.add(v)
     self.varsToAssign = newVarsToAssign
     self.varsToAssign.removeAll(headAssignment.keySet())
     # varsToAssign is set at this point
     # We see if iterating over entire tuples will give us a
     # better result, and we look for the best way of doing that.
     # Let's get the domains of the variables
     # Map<GdlVariable, Set<GdlConstant>> varDomains = model.getVarDomains(rule);
     # Since we're looking at a particular rule, we can do this one step better
     # by looking at the domain of the head, which may be more restrictive
     # and taking the intersections of the two domains where applicable
     # Map<GdlVariable, Set<GdlConstant>> headVarDomains = model.getVarDomainsInSentence(rule.getHead());
     # We can run the A* search for a good set of source conjuncts
     # at this point, then use the result to build the rest.
     completedSentenceFormSizes = HashMap()
     if completedSentenceFormValues != None:
         for form in completedSentenceFormValues.keySet():
             completedSentenceFormSizes.put(form, completedSentenceFormValues.get(form).size())
     varDomainSizes = HashMap()
     for var in varDomains.keySet():
         varDomainSizes.put(var, varDomains.get(var).size())
     bestOrdering = IterationOrderCandidate()
     bestOrdering = getBestIterationOrderCandidate(rule, varDomains, functionInfoMap, completedSentenceFormSizes, headAssignment, False)# model,
     # TODO: True here?
     # Want to replace next few things with order
     # Need a few extra things to handle the use of iteration over existing tuples
     self.varsToAssign = bestOrdering.getVariableOrdering()
     # For each of these vars, we have to find one or the other.
     # Let's start by finding all the domains, a task already done.
     self.valuesToIterate = Lists.newArrayListWithCapacity(len(self.varsToAssign))
     for var in varsToAssign:
         if varDomains.containsKey(var):
             if not varDomains.get(var).isEmpty():
                 self.valuesToIterate.add(ImmutableList.copyOf(varDomains.get(var)))
             else:
                 self.valuesToIterate.add(ImmutableList.of(GdlPool.getConstant("0")))
         else:
             self.valuesToIterate.add(ImmutableList.of(GdlPool.getConstant("0")))
     # Okay, the iteration-over-domain is done.
     # Now let's look at sourced iteration.
     self.sourceDefiningSlot = ArrayList(len(self.varsToAssign))
     i = 0
     while i < len(self.varsToAssign):
         self.sourceDefiningSlot.add(-1)
         i += 1
     # We also need to convert values into tuples
     # We should do so while constraining to any constants in the conjunct
     # Let's convert the conjuncts
     sourceConjuncts = bestOrdering.getSourceConjuncts()
     self.tuplesBySource = Lists.newArrayListWithCapacity(len(sourceConjuncts))
     # new ArrayList<List<List<GdlConstant>>>(len(sourceConjuncts));
     self.varsChosenBySource = Lists.newArrayListWithCapacity(len(sourceConjuncts))
     # new ArrayList<List<Integer>>(len(sourceConjuncts));
     self.putDontCheckBySource = Lists.newArrayListWithCapacity(len(sourceConjuncts))
     # new ArrayList<List<Boolean>>(len(sourceConjuncts));
     j = 0
     while j < len(sourceConjuncts):
         # flatten into a tuple
         # Go through the vars/constants in the tuple
         while i < len(conjunctTuple):
             if isinstance(term, (GdlConstant, )):
                 constraintSlots.add(i)
                 constraintValues.add(term)
                 # TODO: What if tuple size ends up being 0?
                 # Need to keep that in mind
             elif isinstance(term, (GdlVariable, )):
                 varsChosen.add(varIndex)
                 if self.sourceDefiningSlot.get(varIndex) == -1:
                     # We define it
                     self.sourceDefiningSlot.set(varIndex, j)
                     putDontCheck.add(True)
                 else:
                     # It's an overlap; we just check for consistency
                     putDontCheck.add(False)
             else:
                 raise RuntimeException("Function returned in tuple")
             i += 1
         self.varsChosenBySource.add(ImmutableList.copyOf(varsChosen))
         self.putDontCheckBySource.add(ImmutableList.copyOf(putDontCheck))
         # Now we put the tuples together
         # We use constraintSlots and constraintValues to check that the
         # tuples have compatible values
         for sentence in sentences:
             # Check that it doesn't conflict with our headAssignment
             if not headAssignment.isEmpty():
                 for var in headAssignment.keySet():
                     if tupleAssignment.containsKey(var) and tupleAssignment.get(var) != headAssignment.get(var):
                         continue 
             while c < len(constraintSlots):
                 if not longTuple.get(slot) == value:
                     continue 
                 c += 1
             while s < len(longTuple):
                 # constraintSlots is sorted in ascending order
                 if c < len(constraintSlots) and constraintSlots.get(c) == s:
                     c += 1
                 else:
                     shortTuple.add(longTuple.get(s))
                 s += 1
             # The tuple fits the source conjunct
             tuples.add(ImmutableList.copyOf(shortTuple))
         # sortTuples(tuples); //Needed? Useful? Not sure. Probably not?
         self.tuplesBySource.add(ImmutableList.copyOf(tuples))
         j += 1
     # We now want to see which we can give assignment functions to
     self.valuesToCompute = ArrayList(len(self.varsToAssign))
     for var in varsToAssign:
         self.valuesToCompute.add(None)
     self.indicesToChangeWhenNull = ArrayList(len(self.varsToAssign))
     i = 0
     while i < len(self.varsToAssign):
         # Change itself, why not?
         # Actually, instead let's try -1, to catch bugs better
         self.indicesToChangeWhenNull.add(-1)
         i += 1
     # Now we have our functions already selected by the ordering
     # bestOrdering.functionalConjunctIndices;
     # Make AssignmentFunctions out of the ordering
     functionalConjuncts = bestOrdering.getFunctionalConjuncts()
     # 		print "functionalConjuncts: " + functionalConjuncts;
     i = 0
     while i < len(functionalConjuncts):
         if functionalConjunct != None:
             # These are the only ones that could be constant functions
             if functionInfoMap != None:
                 functionInfo = functionInfoMap.get(conjForm)
             if functionInfo != None:
                 # Now we need to figure out which variables are involved
                 # and which are suitable as functional outputs.
                 # 1) Which vars are in this conjunct?
                 # 2) Of these vars, which is "rightmost"?
                 # 3) Is it only used once in the relation?
                 if Collections.frequency(varsInSentence, rightmostVar) != 1:
                     continue 
                 # Can't use it
                 # 4) Which slot is it used in in the relation?
                 # 5) Build an AssignmentFunction if appropriate.
                 #    This should be able to translate from values of
                 #    the other variables to the value of the wanted
                 #    variable.
                 # We don't guarantee that this works until we check
                 if not function_.functional():
                     continue 
                 self.valuesToCompute.set(index, function_)
                 remainingVarsInSentence.remove(rightmostVar)
                 self.indicesToChangeWhenNull.set(index, self.varsToAssign.indexOf(nextRightmostVar))
         i += 1
     # We now have the remainingVars also assigned their domains
     # We also cover the distincts here
     # Assume these are just variables and constants
     self.distincts = ArrayList()
     for literal in rule.getBody():
         if isinstance(literal, (GdlDistinct, )):
             self.distincts.add(literal)
     computeVarsToChangePerDistinct()
     # Need to add "distinct" restrictions to head assignment, too...
     checkDistinctsAgainstHead()
Beispiel #5
0
 def deepCopyOf(cls, original):
     """ generated source for method deepCopyOf """
     copy = Lists.newArrayListWithCapacity(len(original))
     for originalTermModel in original:
         copy.add(cls.TermModel.copyOf(originalTermModel))
     return copy