diff options
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderDisjVar2.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderDisjVar2.java | 242 |
1 files changed, 242 insertions, 0 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderDisjVar2.java b/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderDisjVar2.java new file mode 100644 index 0000000..d257de3 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderDisjVar2.java | |||
| @@ -0,0 +1,242 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.tracking; | ||
| 2 | |||
| 3 | import java.util.Collection; | ||
| 4 | import java.util.HashSet; | ||
| 5 | import java.util.LinkedList; | ||
| 6 | import java.util.Set; | ||
| 7 | |||
| 8 | import org.semanticweb.HermiT.model.AnnotatedEquality; | ||
| 9 | import org.semanticweb.HermiT.model.AtLeastConcept; | ||
| 10 | import org.semanticweb.HermiT.model.Atom; | ||
| 11 | import org.semanticweb.HermiT.model.AtomicConcept; | ||
| 12 | import org.semanticweb.HermiT.model.AtomicNegationConcept; | ||
| 13 | import org.semanticweb.HermiT.model.AtomicRole; | ||
| 14 | import org.semanticweb.HermiT.model.DLClause; | ||
| 15 | import org.semanticweb.HermiT.model.DLPredicate; | ||
| 16 | import org.semanticweb.HermiT.model.Equality; | ||
| 17 | import org.semanticweb.HermiT.model.Inequality; | ||
| 18 | import org.semanticweb.HermiT.model.InverseRole; | ||
| 19 | import org.semanticweb.HermiT.model.Variable; | ||
| 20 | |||
| 21 | import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper; | ||
| 22 | import uk.ac.ox.cs.pagoda.multistage.Normalisation; | ||
| 23 | import uk.ac.ox.cs.pagoda.query.QueryRecord; | ||
| 24 | import uk.ac.ox.cs.pagoda.reasoner.light.BasicQueryEngine; | ||
| 25 | import uk.ac.ox.cs.pagoda.rules.OverApproxExist; | ||
| 26 | import uk.ac.ox.cs.pagoda.rules.UpperDatalogProgram; | ||
| 27 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 28 | |||
| 29 | public class TrackingRuleEncoderDisjVar2 extends TrackingRuleEncoderWithGap { | ||
| 30 | |||
| 31 | public TrackingRuleEncoderDisjVar2(UpperDatalogProgram program, BasicQueryEngine store) { | ||
| 32 | super(program, store); | ||
| 33 | } | ||
| 34 | |||
| 35 | private Set<DLClause> disjunctiveRules = new HashSet<DLClause>(); | ||
| 36 | |||
| 37 | @Override | ||
| 38 | public boolean encodingRules() { | ||
| 39 | if (ruleEncoded) return false; | ||
| 40 | ruleEncoded = true; | ||
| 41 | |||
| 42 | for (DLClause clause: program.getClauses()) { | ||
| 43 | encodingRule(clause); | ||
| 44 | } | ||
| 45 | |||
| 46 | if (disjunctiveRules.isEmpty()) | ||
| 47 | return true; | ||
| 48 | |||
| 49 | processDisjunctiveRules(); | ||
| 50 | return false; | ||
| 51 | } | ||
| 52 | |||
| 53 | @Override | ||
| 54 | protected void encodingRule(DLClause clause) { | ||
| 55 | DLClause original = program.getCorrespondingClause(clause); | ||
| 56 | if (original.getHeadLength() <= 1) { | ||
| 57 | super.encodingRule(clause); | ||
| 58 | } | ||
| 59 | else { | ||
| 60 | if (!DLClauseHelper.hasSubsetBodyAtoms(clause, original)) | ||
| 61 | super.encodingRule(clause); | ||
| 62 | addDisjunctiveRule(original); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | private void processDisjunctiveRules() { | ||
| 67 | for (DLClause clause: disjunctiveRules) | ||
| 68 | encodingDisjunctiveRule(clause); | ||
| 69 | } | ||
| 70 | |||
| 71 | private Atom getAuxiliaryAtom(Atom headAtom) { | ||
| 72 | DLPredicate p = headAtom.getDLPredicate(); | ||
| 73 | if (p instanceof AtLeastConcept) { | ||
| 74 | return Atom.create(generateAuxiliaryRule((AtLeastConcept) p), headAtom.getArgument(0)); | ||
| 75 | } | ||
| 76 | if (p instanceof AtomicConcept) | ||
| 77 | return Atom.create(generateAuxiliaryRule((AtomicConcept) p), headAtom.getArgument(0)); | ||
| 78 | if (p instanceof AtomicRole) | ||
| 79 | return Atom.create(generateAuxiliaryRule((AtomicRole) p), headAtom.getArgument(0), headAtom.getArgument(1)); | ||
| 80 | if (p instanceof Equality || p instanceof AnnotatedEquality) | ||
| 81 | return Atom.create(generateAuxiliaryRule(Equality.INSTANCE), headAtom.getArgument(0), headAtom.getArgument(1)); | ||
| 82 | if (p instanceof Inequality) | ||
| 83 | return Atom.create(generateAuxiliaryRule((Inequality) p), headAtom.getArgument(0), headAtom.getArgument(1)); | ||
| 84 | |||
| 85 | return null; | ||
| 86 | } | ||
| 87 | |||
| 88 | private Atom getGapAtom(Atom headAtom) { | ||
| 89 | DLPredicate p = headAtom.getDLPredicate(); | ||
| 90 | if (p instanceof AtLeastConcept) { | ||
| 91 | return Atom.create(getGapDLPredicate(AtomicConcept.create(Normalisation.getAuxiliaryConcept4Disjunct((AtLeastConcept) p))), headAtom.getArgument(0)); | ||
| 92 | } | ||
| 93 | if (p instanceof AtomicConcept) | ||
| 94 | return Atom.create(getGapDLPredicate((AtomicConcept) p), headAtom.getArgument(0)); | ||
| 95 | if (p instanceof AtomicRole) | ||
| 96 | return Atom.create(getGapDLPredicate((AtomicRole) p), headAtom.getArgument(0), headAtom.getArgument(1)); | ||
| 97 | if (p instanceof Equality || p instanceof AnnotatedEquality) | ||
| 98 | return Atom.create(getGapDLPredicate(Equality.INSTANCE), headAtom.getArgument(0), headAtom.getArgument(1)); | ||
| 99 | if (p instanceof Inequality) | ||
| 100 | return Atom.create(getGapDLPredicate((Inequality) p), headAtom.getArgument(0), headAtom.getArgument(1)); | ||
| 101 | |||
| 102 | return null; | ||
| 103 | } | ||
| 104 | |||
| 105 | private void encodingDisjunctiveRule(DLClause clause) { | ||
| 106 | int headLength = clause.getHeadLength(); | ||
| 107 | |||
| 108 | Atom[] auxAtoms = new Atom[headLength]; | ||
| 109 | for (int i = 0; i < headLength; ++i) | ||
| 110 | auxAtoms[i] = getAuxiliaryAtom(clause.getHeadAtom(i)); | ||
| 111 | |||
| 112 | Atom[] gapAtoms = new Atom[headLength]; | ||
| 113 | for (int i = 0; i < headLength; ++i) | ||
| 114 | gapAtoms[i] = getGapAtom(clause.getHeadAtom(i)); | ||
| 115 | |||
| 116 | Atom[] bodyAtoms = clause.getBodyAtoms(); | ||
| 117 | |||
| 118 | LinkedList<Atom> newHeadAtoms = new LinkedList<Atom>(); | ||
| 119 | DLPredicate selected = AtomicConcept.create(getSelectedPredicate()); | ||
| 120 | newHeadAtoms.add(Atom.create(selected, getIndividual4GeneralRule(clause))); | ||
| 121 | |||
| 122 | for (Atom atom: bodyAtoms) { | ||
| 123 | Atom newAtom = Atom.create( | ||
| 124 | getTrackingDLPredicate(atom.getDLPredicate()), | ||
| 125 | DLClauseHelper.getArguments(atom)); | ||
| 126 | newHeadAtoms.add(newAtom); | ||
| 127 | } | ||
| 128 | |||
| 129 | DLClause newClause; | ||
| 130 | for (int j = 0; j < headLength; ++j) { | ||
| 131 | Atom[] newBodyAtoms = new Atom[headLength + bodyAtoms.length + 1]; | ||
| 132 | newBodyAtoms[0] = gapAtoms[j]; | ||
| 133 | |||
| 134 | for (int i = 0; i < headLength; ++i) | ||
| 135 | // newBodyAtoms[i] = auxAtoms[i]; | ||
| 136 | newBodyAtoms[i + 1] = auxAtoms[i]; | ||
| 137 | |||
| 138 | for (int i = 0; i < bodyAtoms.length; ++i) | ||
| 139 | // newBodyAtoms[i + headLength] = bodyAtoms[i]; | ||
| 140 | newBodyAtoms[i + headLength + 1] = bodyAtoms[i]; | ||
| 141 | |||
| 142 | for (Atom atom: newHeadAtoms) { | ||
| 143 | newClause = DLClause.create(new Atom[] {atom}, newBodyAtoms); | ||
| 144 | addTrackingClause(newClause); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | private void addTrackingClause(DLClause clause) { | ||
| 150 | trackingClauses.add(clause); | ||
| 151 | } | ||
| 152 | |||
| 153 | private void addDisjunctiveRule(DLClause clause) { | ||
| 154 | disjunctiveRules.add(clause); | ||
| 155 | } | ||
| 156 | |||
| 157 | protected DLPredicate generateAuxiliaryRule(AtLeastConcept p) { | ||
| 158 | AtomicConcept ac = AtomicConcept.create(Normalisation.getAuxiliaryConcept4Disjunct(p)); | ||
| 159 | int num = p.getNumber(); | ||
| 160 | Variable X = Variable.create("X"); | ||
| 161 | Variable[] Ys = new Variable[num]; | ||
| 162 | for (int i = 0; i < num; ++i) Ys[i] = Variable.create("Y" + (i + 1)); | ||
| 163 | Collection<Atom> expandedAtom = new LinkedList<Atom>(); | ||
| 164 | Collection<Atom> representativeAtom = new LinkedList<Atom>(); | ||
| 165 | if (p.getOnRole() instanceof AtomicRole) { | ||
| 166 | AtomicRole r = (AtomicRole) p.getOnRole(); | ||
| 167 | for (int i = 0; i < num; ++i) | ||
| 168 | expandedAtom.add(Atom.create(r, X, Ys[i])); | ||
| 169 | representativeAtom.add(Atom.create(r, X, Ys[0])); | ||
| 170 | } | ||
| 171 | else { | ||
| 172 | AtomicRole r = ((InverseRole) p.getOnRole()).getInverseOf(); | ||
| 173 | for (int i = 0; i < num; ++i) | ||
| 174 | expandedAtom.add(Atom.create(r, Ys[i], X)); | ||
| 175 | representativeAtom.add(Atom.create(r, Ys[0], X)); | ||
| 176 | |||
| 177 | } | ||
| 178 | |||
| 179 | if (num > 1) { | ||
| 180 | representativeAtom.add(Atom.create(Inequality.INSTANCE, Ys[0], Ys[1])); | ||
| 181 | } | ||
| 182 | for (int i = 0; i < num; ++i) | ||
| 183 | for (int j = i + 1; j < num; ++i) | ||
| 184 | expandedAtom.add(Atom.create(Inequality.INSTANCE, Ys[i], Ys[j])); | ||
| 185 | |||
| 186 | if (!p.getToConcept().equals(AtomicConcept.THING)) { | ||
| 187 | AtomicConcept c; | ||
| 188 | if (p.getToConcept() instanceof AtomicConcept) | ||
| 189 | c = (AtomicConcept) p.getToConcept(); | ||
| 190 | else | ||
| 191 | c = OverApproxExist.getNegationConcept(((AtomicNegationConcept) p.getToConcept()).getNegatedAtomicConcept()); | ||
| 192 | for (int i = 0; i < num; ++i) | ||
| 193 | expandedAtom.add(Atom.create(c, Ys[i])); | ||
| 194 | representativeAtom.add(Atom.create(c, Ys[0])); | ||
| 195 | } | ||
| 196 | |||
| 197 | DLPredicate auxPredicate = getTrackingDLPredicate(ac); | ||
| 198 | DLPredicate gapPredicate = getGapDLPredicate(ac); | ||
| 199 | for (Atom atom: representativeAtom) { | ||
| 200 | Atom[] bodyAtoms = new Atom[expandedAtom.size() + 1]; | ||
| 201 | bodyAtoms[0] = getAuxiliaryAtom(atom); | ||
| 202 | int i = 0; | ||
| 203 | for (Atom bodyAtom: expandedAtom) | ||
| 204 | bodyAtoms[++i] = bodyAtom; | ||
| 205 | addTrackingClause(DLClause.create(new Atom[] {Atom.create(auxPredicate, X)}, bodyAtoms)); | ||
| 206 | |||
| 207 | bodyAtoms = new Atom[expandedAtom.size() + 1]; | ||
| 208 | if (atom.getArity() == 1) | ||
| 209 | bodyAtoms[0] = Atom.create(getGapDLPredicate(atom.getDLPredicate()), atom.getArgument(0)); | ||
| 210 | else | ||
| 211 | bodyAtoms[0] = Atom.create(getGapDLPredicate(atom.getDLPredicate()), atom.getArgument(0), atom.getArgument(1)); | ||
| 212 | i = 0; | ||
| 213 | for (Atom bodyAtom: expandedAtom) | ||
| 214 | bodyAtoms[++i] = bodyAtom; | ||
| 215 | addTrackingClause(DLClause.create(new Atom[] {Atom.create(gapPredicate, X)}, bodyAtoms)); | ||
| 216 | } | ||
| 217 | |||
| 218 | return auxPredicate; | ||
| 219 | } | ||
| 220 | |||
| 221 | private DLPredicate generateAuxiliaryRule(AtomicConcept p) { | ||
| 222 | return getTrackingDLPredicate(p); | ||
| 223 | } | ||
| 224 | |||
| 225 | private DLPredicate generateAuxiliaryRule(AtomicRole p) { | ||
| 226 | return getTrackingDLPredicate(p); | ||
| 227 | } | ||
| 228 | |||
| 229 | protected DLPredicate generateAuxiliaryRule(Equality instance) { | ||
| 230 | return getTrackingDLPredicate(AtomicRole.create(Namespace.EQUALITY)); | ||
| 231 | } | ||
| 232 | |||
| 233 | protected DLPredicate generateAuxiliaryRule(Inequality instance) { | ||
| 234 | return getTrackingDLPredicate(AtomicRole.create(Namespace.INEQUALITY)); | ||
| 235 | } | ||
| 236 | |||
| 237 | @Override | ||
| 238 | protected void encodingAtomicQuery(QueryRecord[] botQuerRecords) { | ||
| 239 | encodingAtomicQuery(botQuerRecords, true); | ||
| 240 | } | ||
| 241 | |||
| 242 | } | ||
