Map<Variable,Set<AtomicConcept>> z2concepts=new HashMap<Variable, Set<AtomicConcept>>();
Map<Variable,Set<AtomicRole>> x2yRoles=new HashMap<Variable, Set<AtomicRole>>();
Map<Variable,Set<AtomicRole>> y2xRoles=new HashMap<Variable, Set<AtomicRole>>();
// Each atom in the antecedent is of the form A(x), R(x,x), R(x,yi), R(yi,x), A(yi), or A(zj).
for (int i=0;i<dlClause.getBodyLength();i++) {
Atom atom=dlClause.getBodyAtom(i);
DLPredicate predicate=atom.getDLPredicate();
Variable var1=atom.getArgumentVariable(0);
if (predicate instanceof AtomicConcept) {
if (var1==X) {
xConcepts.add((AtomicConcept)predicate);
} else if (var1.getName().startsWith("Y")) {
ys.add(var1);
if (y2concepts.containsKey(var1)) {
y2concepts.get(var1).add((AtomicConcept)predicate);
} else {
Set<AtomicConcept> concepts=new HashSet<AtomicConcept>();
concepts.add((AtomicConcept)predicate);
y2concepts.put(var1, concepts);
}
} else if (var1.getName().startsWith("Z")) {
if (z2concepts.containsKey(var1)) {
Set<AtomicConcept> concepts=z2concepts.get(var1);
concepts.add((AtomicConcept)predicate);
} else {
Set<AtomicConcept> concepts=new HashSet<AtomicConcept>();
concepts.add((AtomicConcept)predicate);
z2concepts.put(var1, concepts);
}
} else {
throw new IllegalStateException("Internal error: Clause premise contained variables other than X, Yi, and Zi in a concept atom. ");
}
} else if (predicate instanceof AtomicRole) {
Variable var2=atom.getArgumentVariable(1);
if (var1==X) {
if (var2==X) {
x2xRoles.add((AtomicRole)atom.getDLPredicate());
} else if (var2.getName().startsWith("Y")) {
ys.add(var2);
if (x2yRoles.containsKey(var2)) {
x2yRoles.get(var2).add((AtomicRole)predicate);
} else {
Set<AtomicRole> roles=new HashSet<AtomicRole>();
roles.add((AtomicRole)predicate);
x2yRoles.put(var2,roles);
}
} else {
throw new IllegalStateException("Internal error: Clause premise contains a role atom with virales other than X and Yi. ");
}
} else if (var2==X) {
if (var1.getName().startsWith("Y")) {
ys.add(var1);
if (y2xRoles.containsKey(var1)) {
y2xRoles.get(var1).add((AtomicRole)predicate);
} else {
Set<AtomicRole> roles=new HashSet<AtomicRole>();
roles.add((AtomicRole)predicate);
y2xRoles.put(var1,roles);
}
} else {
throw new IllegalStateException("Internal error: Clause premise contains a role atom with virales other than X and Yi. ");
}
} else {
throw new IllegalStateException("Internal error: Clause premise contained variables other than X and Yi in a role atom. ");
}
}
}
AtomicConcept[] noConcepts=new AtomicConcept[0];
AtomicRole[] noRoles=new AtomicRole[0];
Variable[] noVariables=new Variable[0];
// Variable X
m_xNode=null;
m_xConcepts=xConcepts.toArray(noConcepts);
m_x2xRoles=x2xRoles.toArray(noRoles);
// Variable Y
m_yVariables=ys.toArray(noVariables);
m_yNodes=new Node[m_yVariables.length];
m_yConstraints=new YConstraint[m_yVariables.length];
m_x2yRetrievals=new Retrieval[x2yRoles.size()];
m_x2yRoles=new AtomicRole[x2yRoles.size()];
m_y2xRetrievals=new Retrieval[y2xRoles.size()];
m_y2xRoles=new AtomicRole[y2xRoles.size()];
int i=0;
int num_xyRoles=0;
for (i=0;i<m_yVariables.length;i++) {
Variable y=m_yVariables[i];
Set<AtomicConcept> yConcepts=y2concepts.get(y);
Set<AtomicRole> xyRoles=x2yRoles.get(y);
if (xyRoles!=null) {
assert xyRoles.size()==1;
assert m_y2xRetrievals.length<m_x2yRetrievals.length;
m_x2yRetrievals[num_xyRoles]=extensionManager.getTernaryExtensionTable().createRetrieval(new boolean[] { true,true,false },ExtensionTable.View.TOTAL);
m_x2yRoles[num_xyRoles]=xyRoles.iterator().next();
num_xyRoles++;
}
Set<AtomicRole> yxRoles=y2xRoles.get(y);
if (yxRoles!=null) {
assert yxRoles.size()==1;
assert i-num_xyRoles>=0;
assert i-num_xyRoles<m_y2xRetrievals.length;
m_y2xRetrievals[i-num_xyRoles]=extensionManager.getTernaryExtensionTable().createRetrieval(new boolean[] { true,false,true },ExtensionTable.View.TOTAL);
m_y2xRoles[i-num_xyRoles]=yxRoles.iterator().next();
}
m_yConstraints[i]=new YConstraint(yConcepts!=null?yConcepts.toArray(noConcepts):noConcepts, xyRoles!=null?xyRoles.toArray(noRoles):noRoles, yxRoles!=null?yxRoles.toArray(noRoles):noRoles);
}
// Variable Z
m_zVariables=z2concepts.keySet().toArray(noVariables);
m_zNodes=new Node[m_zVariables.length];
m_zConcepts=new AtomicConcept[m_zNodes.length][];
for (int varIndex=0;varIndex<m_zVariables.length;varIndex++) {
m_zConcepts[varIndex]=z2concepts.get(m_zVariables[varIndex]).toArray(noConcepts);
}
m_zRetrievals=new Retrieval[m_zNodes.length];
for (i=0;i<m_zRetrievals.length;i++) {
m_zRetrievals[i]=extensionManager.getBinaryExtensionTable().createRetrieval(new boolean[] { true,false },ExtensionTable.View.TOTAL);
}
// Consequences
m_consequencesForBlockedX=new ConsequenceAtom[dlClause.getHeadLength()];
m_consequencesForNonblockedX=new ConsequenceAtom[dlClause.getHeadLength()];
//Each atom in the consequent is of the form B(x), >= h S.B(x), B(yi), R(x, x),
// R(x,yi), R(yi,x), R(x,zj), R(zj,x), x==zj, or yi==yj @^x_{<=h S.B}.
for (i=0;i<dlClause.getHeadLength();i++) {
Atom atom=dlClause.getHeadAtom(i);
DLPredicate predicate=atom.getDLPredicate();
Variable var1=atom.getArgumentVariable(0);
Variable var2=null;
if (predicate.getArity()==2) var2=atom.getArgumentVariable(1);
if (predicate instanceof AtomicConcept) {
// B(x), B(yi)
ArgumentType argType=ArgumentType.YVAR;
int argIndex=getIndexFor(m_yVariables, var1);
if (argIndex==-1) {
assert var1==X;
argIndex=0;
argType=ArgumentType.XVAR;
}
m_consequencesForBlockedX[i]=new SimpleConsequenceAtom(predicate,new ArgumentType[] { argType },new int[] { argIndex });
if (argType==ArgumentType.XVAR) {
m_consequencesForNonblockedX[i]=m_consequencesForBlockedX[i];
} else {
m_consequencesForNonblockedX[i]=new MirroredYConsequenceAtom((AtomicConcept)predicate,argIndex);
}
} else if (predicate instanceof AtLeastConcept) {
// >= h S.B(x)
assert var1==X;
m_consequencesForBlockedX[i]=new SimpleConsequenceAtom(predicate,new ArgumentType[] { ArgumentType.XVAR },new int[] { 0 });
m_consequencesForNonblockedX[i]=m_consequencesForBlockedX[i];
} else if (predicate==Equality.INSTANCE) {
// x==zi or y==zi or yi===yj for datatype assertions
if (var1==X || var2==X) {
// x==zi
if (var2==X) {
Variable tmp=var1;
var1=var2;
var2=tmp;
}
assert var2.getName().startsWith("Z");
int var2Index=getIndexFor(m_zVariables, var2);
assert var1==X && var2Index!=-1;
m_consequencesForBlockedX[i]=new SimpleConsequenceAtom(predicate,new ArgumentType[] { ArgumentType.XVAR,ArgumentType.ZVAR },new int[] { 0,var2Index });
m_consequencesForNonblockedX[i]=m_consequencesForBlockedX[i];
} else if (var1.getName().startsWith("Z") || var2.getName().startsWith("Z")) {
// y==zi
if (var2.getName().startsWith("Y")) {
Variable tmp=var1;
var1=var2;
var2=tmp;
}
assert var2.getName().startsWith("Z");
int var2Index=getIndexFor(m_zVariables, var2);
int var1Index=getIndexFor(m_yVariables, var1);
assert var1Index>-1 && var2Index>-1;
m_consequencesForBlockedX[i]=new SimpleConsequenceAtom(predicate,new ArgumentType[] { ArgumentType.YVAR,ArgumentType.ZVAR },new int[] { var1Index,var2Index });
m_consequencesForNonblockedX[i]=m_consequencesForBlockedX[i];
} else if (var1.getName().startsWith("Y") && var2.getName().startsWith("Y")) {
// yi==yj
int var1Index=getIndexFor(m_yVariables, var1);
int var2Index=getIndexFor(m_yVariables, var2);
assert var1Index>-1 && var2Index>-1;
m_consequencesForBlockedX[i]=new SimpleConsequenceAtom(predicate,new ArgumentType[] { ArgumentType.YVAR,ArgumentType.YVAR },new int[] { var1Index,var2Index });
m_consequencesForNonblockedX[i]=m_consequencesForBlockedX[i];
} else {
throw new IllegalArgumentException("Internal error: The clause "+dlClause+" is not an HT clause. ");
}
} else if (predicate instanceof AnnotatedEquality) {
// (yi==yj)@^x_{<=h S.B})(X)
// arity 3
var1=atom.getArgumentVariable(0);
var2=atom.getArgumentVariable(1);
int var1Index=getIndexFor(m_yVariables, var1);
int var2Index=getIndexFor(m_yVariables, var2);
assert var1Index!=-1 && var2Index!=-1;
m_consequencesForBlockedX[i]=new SimpleConsequenceAtom(predicate,new ArgumentType[] { ArgumentType.YVAR,ArgumentType.YVAR,ArgumentType.XVAR },new int[] { var1Index,var2Index,0 });
m_consequencesForNonblockedX[i]=m_consequencesForBlockedX[i];