tuplesBySource = Lists.newArrayListWithCapacity(sourceConjuncts.size());//new ArrayList<List<List<GdlConstant>>>(sourceConjuncts.size());
varsChosenBySource = Lists.newArrayListWithCapacity(sourceConjuncts.size());//new ArrayList<List<Integer>>(sourceConjuncts.size());
putDontCheckBySource = Lists.newArrayListWithCapacity(sourceConjuncts.size());//new ArrayList<List<Boolean>>(sourceConjuncts.size());
for(int j = 0; j < sourceConjuncts.size(); j++) {
GdlSentence sourceConjunct = sourceConjuncts.get(j);
SentenceForm form = SimpleSentenceForm.create(sourceConjunct);
//flatten into a tuple
List<GdlTerm> conjunctTuple = GdlUtils.getTupleFromSentence(sourceConjunct);
//Go through the vars/constants in the tuple
List<Integer> constraintSlots = new ArrayList<Integer>();
List<GdlConstant> constraintValues = new ArrayList<GdlConstant>();
List<Integer> varsChosen = new ArrayList<Integer>();
List<Boolean> putDontCheck = new ArrayList<Boolean>();
for(int i = 0; i < conjunctTuple.size(); i++) {
GdlTerm term = conjunctTuple.get(i);
if(term instanceof GdlConstant) {
constraintSlots.add(i);
constraintValues.add((GdlConstant) term);
//TODO: What if tuple size ends up being 0?
//Need to keep that in mind
} else if(term instanceof GdlVariable) {
int varIndex = varsToAssign.indexOf(term);
varsChosen.add(varIndex);
if(sourceDefiningSlot.get(varIndex) == -1) {
//We define it
sourceDefiningSlot.set(varIndex, j);
putDontCheck.add(true);
} else {
//It's an overlap; we just check for consistency
putDontCheck.add(false);
}
} else {
throw new RuntimeException("Function returned in tuple");
}
}
varsChosenBySource.add(ImmutableList.copyOf(varsChosen));
putDontCheckBySource.add(ImmutableList.copyOf(putDontCheck));
//Now we put the tuples together
//We use constraintSlots and constraintValues to check that the
//tuples have compatible values
Collection<GdlSentence> sentences = completedSentenceFormValues.get(form);
List<ImmutableList<GdlConstant>> tuples = Lists.newArrayList();
byTuple: for(GdlSentence sentence : sentences) {
//Check that it doesn't conflict with our headAssignment
if (!headAssignment.isEmpty()) {
Map<GdlVariable, GdlConstant> tupleAssignment = GdlUtils.getAssignmentMakingLeftIntoRight(sourceConjunct, sentence);
for (GdlVariable var : headAssignment.keySet()) {
if (tupleAssignment.containsKey(var)
&& tupleAssignment.get(var) != headAssignment.get(var)) {
continue byTuple;
}
}
}
List<GdlConstant> longTuple = GdlUtils.getTupleFromGroundSentence(sentence);
List<GdlConstant> shortTuple = new ArrayList<GdlConstant>(varsChosen.size());
for(int c = 0; c < constraintSlots.size(); c++) {
int slot = constraintSlots.get(c);
GdlConstant value = constraintValues.get(c);
if(!longTuple.get(slot).equals(value))
continue byTuple;
}
int c = 0;
for(int s = 0; s < longTuple.size(); s++) {
//constraintSlots is sorted in ascending order
if(c < constraintSlots.size()
&& constraintSlots.get(c) == s)
c++;
else
shortTuple.add(longTuple.get(s));
}
//The tuple fits the source conjunct
tuples.add(ImmutableList.copyOf(shortTuple));
}
//sortTuples(tuples); //Needed? Useful? Not sure. Probably not?
tuplesBySource.add(ImmutableList.copyOf(tuples));
}
//We now want to see which we can give assignment functions to
valuesToCompute = new ArrayList<AssignmentFunction>(varsToAssign.size());
for(@SuppressWarnings("unused") GdlVariable var : varsToAssign) {
valuesToCompute.add(null);
}
indicesToChangeWhenNull = new ArrayList<Integer>(varsToAssign.size());
for(int i = 0; i < varsToAssign.size(); i++) {
//Change itself, why not?
//Actually, instead let's try -1, to catch bugs better
indicesToChangeWhenNull.add(-1);
}
//Now we have our functions already selected by the ordering
//bestOrdering.functionalConjunctIndices;
//Make AssignmentFunctions out of the ordering
List<GdlSentence> functionalConjuncts = bestOrdering.getFunctionalConjuncts();
// System.out.println("functionalConjuncts: " + functionalConjuncts);
for(int i = 0; i < functionalConjuncts.size(); i++) {
GdlSentence functionalConjunct = functionalConjuncts.get(i);
if(functionalConjunct != null) {
//These are the only ones that could be constant functions
SentenceForm conjForm = SimpleSentenceForm.create(functionalConjunct);
FunctionInfo functionInfo = null;
if(functionInfoMap != null)
functionInfo = functionInfoMap.get(conjForm);
if(functionInfo != null) {