diff options
| author | yzhou <yujiao.zhou@gmail.com> | 2015-04-21 10:34:27 +0100 |
|---|---|---|
| committer | yzhou <yujiao.zhou@gmail.com> | 2015-04-21 10:34:27 +0100 |
| commit | 9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8 (patch) | |
| tree | 47511c0fb89dccff0db4b5990522e04f294d795b /src/uk/ac/ox/cs/pagoda/rules/LowerDatalogProgram.java | |
| parent | b1ac207612ee8b045244253fb94b866104bc34f2 (diff) | |
| download | ACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.tar.gz ACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.zip | |
initial version
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/rules/LowerDatalogProgram.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/rules/LowerDatalogProgram.java | 228 |
1 files changed, 228 insertions, 0 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/rules/LowerDatalogProgram.java b/src/uk/ac/ox/cs/pagoda/rules/LowerDatalogProgram.java new file mode 100644 index 0000000..6e17f02 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/rules/LowerDatalogProgram.java | |||
| @@ -0,0 +1,228 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.rules; | ||
| 2 | |||
| 3 | import java.util.Collection; | ||
| 4 | import java.util.Iterator; | ||
| 5 | import java.util.LinkedList; | ||
| 6 | import java.util.Set; | ||
| 7 | |||
| 8 | import org.semanticweb.HermiT.Reasoner; | ||
| 9 | import org.semanticweb.HermiT.model.Atom; | ||
| 10 | import org.semanticweb.HermiT.model.AtomicConcept; | ||
| 11 | import org.semanticweb.HermiT.model.AtomicRole; | ||
| 12 | import org.semanticweb.HermiT.model.DLClause; | ||
| 13 | import org.semanticweb.HermiT.model.Variable; | ||
| 14 | import org.semanticweb.owlapi.model.OWLClass; | ||
| 15 | import org.semanticweb.owlapi.model.OWLObjectInverseOf; | ||
| 16 | import org.semanticweb.owlapi.model.OWLObjectProperty; | ||
| 17 | import org.semanticweb.owlapi.model.OWLObjectPropertyExpression; | ||
| 18 | import org.semanticweb.owlapi.model.OWLOntology; | ||
| 19 | import org.semanticweb.owlapi.reasoner.Node; | ||
| 20 | |||
| 21 | import uk.ac.ox.cs.pagoda.constraints.BottomStrategy; | ||
| 22 | import uk.ac.ox.cs.pagoda.constraints.NullaryBottom; | ||
| 23 | import uk.ac.ox.cs.pagoda.constraints.UnaryBottom; | ||
| 24 | import uk.ac.ox.cs.pagoda.constraints.UpperUnaryBottom; | ||
| 25 | import uk.ac.ox.cs.pagoda.multistage.Normalisation; | ||
| 26 | import uk.ac.ox.cs.pagoda.multistage.RestrictedApplication; | ||
| 27 | import uk.ac.ox.cs.pagoda.util.Timer; | ||
| 28 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 29 | |||
| 30 | public class LowerDatalogProgram extends ApproxProgram implements IncrementalProgram { | ||
| 31 | |||
| 32 | boolean m_toClassify; | ||
| 33 | |||
| 34 | public LowerDatalogProgram() { | ||
| 35 | m_toClassify = true; | ||
| 36 | } | ||
| 37 | |||
| 38 | public LowerDatalogProgram(boolean toClassify) { | ||
| 39 | m_toClassify = toClassify; // false; // | ||
| 40 | } | ||
| 41 | |||
| 42 | void clone(Program program) { | ||
| 43 | super.clone(program); | ||
| 44 | if (botStrategy instanceof UpperUnaryBottom) | ||
| 45 | botStrategy = new UnaryBottom(); | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | @Override | ||
| 50 | public void transform() { | ||
| 51 | if (m_toClassify) { | ||
| 52 | ClassifyThread thread = new ClassifyThread(this); | ||
| 53 | thread.start(); | ||
| 54 | super.transform(); | ||
| 55 | try { | ||
| 56 | thread.join(5000); | ||
| 57 | } catch (InterruptedException e) { | ||
| 58 | return ; | ||
| 59 | } | ||
| 60 | if (!thread.isAlive()) thread.dispose(); | ||
| 61 | else thread.interrupt(); | ||
| 62 | } | ||
| 63 | else | ||
| 64 | super.transform(); | ||
| 65 | |||
| 66 | Normalisation norm = new Normalisation(dlOntology.getDLClauses(), ontology, new NullaryBottom()); | ||
| 67 | BottomStrategy tBottom = new NullaryBottom(); | ||
| 68 | norm.process(); | ||
| 69 | for (DLClause nClause: norm.getNormlisedClauses()) { | ||
| 70 | if (nClause.getHeadLength() != 1) | ||
| 71 | for (DLClause newClause: RestrictedApplication.addAddtionalDatalogRules(nClause, tBottom, norm)) { | ||
| 72 | // System.out.println(newClause); | ||
| 73 | if (newClause.getHeadAtom(0).getDLPredicate() instanceof AtomicConcept || newClause.getHeadAtom(0).getDLPredicate() instanceof AtomicRole) { | ||
| 74 | // System.out.println(newClause); | ||
| 75 | clauses.add(newClause); | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | if (nClause.getHeadLength() == 1 && (nClause.getHeadAtom(0).getDLPredicate() instanceof AtomicConcept || nClause.getHeadAtom(0).getDLPredicate() instanceof AtomicRole) && clauses.add(nClause)) { | ||
| 80 | // System.out.println(nClause); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | @Override | ||
| 86 | public String getOutputPath() { | ||
| 87 | return getDirectory() + "lower.dlog"; | ||
| 88 | } | ||
| 89 | |||
| 90 | // @Override | ||
| 91 | // public String getDirectory() { | ||
| 92 | // File dir = new File(ontologyDirectory + Utility.FILE_SEPARATOR + "datalog"); | ||
| 93 | // if (!dir.exists()) | ||
| 94 | // dir.mkdirs(); | ||
| 95 | // return dir.getPath(); | ||
| 96 | // } | ||
| 97 | |||
| 98 | @Override | ||
| 99 | public void enrich(Collection<DLClause> delta) { | ||
| 100 | synchronized (clauses) { | ||
| 101 | Iterator<DLClause> iter = delta.iterator(); | ||
| 102 | while (iter.hasNext()) | ||
| 103 | clauses.add(iter.next()); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | @Override | ||
| 108 | public String toString() { | ||
| 109 | String text; | ||
| 110 | synchronized (clauses) { | ||
| 111 | text = super.toString(); | ||
| 112 | } | ||
| 113 | return text; | ||
| 114 | } | ||
| 115 | |||
| 116 | @Override | ||
| 117 | protected void initApproximator() { | ||
| 118 | m_approx = new IgnoreBoth(); | ||
| 119 | } | ||
| 120 | |||
| 121 | } | ||
| 122 | |||
| 123 | class ClassifyThread extends Thread { | ||
| 124 | |||
| 125 | IncrementalProgram m_program; | ||
| 126 | Collection<DLClause> clauses = new LinkedList<DLClause>(); | ||
| 127 | |||
| 128 | Variable X = Variable.create("X"), Y = Variable.create("Y"); | ||
| 129 | |||
| 130 | ClassifyThread(IncrementalProgram program) { | ||
| 131 | m_program = program; | ||
| 132 | } | ||
| 133 | |||
| 134 | Reasoner hermitReasoner; | ||
| 135 | OWLOntology ontology; | ||
| 136 | |||
| 137 | @Override | ||
| 138 | public void run() { | ||
| 139 | ontology = m_program.getOntology(); | ||
| 140 | try { | ||
| 141 | hermitReasoner = new Reasoner(ontology); | ||
| 142 | Timer t = new Timer(); | ||
| 143 | hermitReasoner.classifyClasses(); | ||
| 144 | Utility.logInfo("HermiT classification done: " + t.duration()); | ||
| 145 | } catch (Exception e) { | ||
| 146 | // e.printStackTrace(); | ||
| 147 | Utility.logInfo("HermiT cannot classify the ontology."); | ||
| 148 | hermitReasoner = null; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | public void dispose() { | ||
| 153 | if (hermitReasoner == null) | ||
| 154 | return ; | ||
| 155 | Set<OWLClass> classes; | ||
| 156 | OWLClass lastClass = null, currentClass; | ||
| 157 | for (OWLClass subClass: ontology.getClassesInSignature()) { | ||
| 158 | Node<OWLClass> node = hermitReasoner.getEquivalentClasses(subClass); | ||
| 159 | if (!subClass.equals(node.getRepresentativeElement())) continue; | ||
| 160 | |||
| 161 | classes = node.getEntities(); | ||
| 162 | lastClass = subClass; | ||
| 163 | for (Iterator<OWLClass> iter = classes.iterator(); iter.hasNext(); ) { | ||
| 164 | currentClass = iter.next(); | ||
| 165 | if (currentClass.equals(subClass)) continue; | ||
| 166 | addClause(lastClass, currentClass); | ||
| 167 | lastClass = currentClass; | ||
| 168 | } | ||
| 169 | addClause(lastClass, subClass); | ||
| 170 | |||
| 171 | for (Node<OWLClass> tNode: hermitReasoner.getSuperClasses(subClass, true)) { | ||
| 172 | OWLClass superClass = tNode.getRepresentativeElement(); | ||
| 173 | addClause(subClass, superClass); | ||
| 174 | } | ||
| 175 | } | ||
| 176 | |||
| 177 | Set<OWLObjectPropertyExpression> properties; | ||
| 178 | OWLObjectPropertyExpression lastProperty, currentProperty; | ||
| 179 | for (OWLObjectProperty subProperty: ontology.getObjectPropertiesInSignature()) { | ||
| 180 | Node<OWLObjectPropertyExpression> node = hermitReasoner.getEquivalentObjectProperties(subProperty); | ||
| 181 | if (!subProperty.equals(node.getRepresentativeElement())) continue; | ||
| 182 | |||
| 183 | properties = node.getEntities(); | ||
| 184 | lastProperty = subProperty; | ||
| 185 | for (Iterator<OWLObjectPropertyExpression> iter = properties.iterator(); iter.hasNext(); ) { | ||
| 186 | currentProperty = iter.next(); | ||
| 187 | if (currentProperty.equals(subProperty)) continue; | ||
| 188 | addClause(lastProperty, currentProperty); | ||
| 189 | lastProperty = currentProperty; | ||
| 190 | } | ||
| 191 | addClause(lastProperty, subProperty); | ||
| 192 | |||
| 193 | for (Node<OWLObjectPropertyExpression> tNode: hermitReasoner.getSuperObjectProperties(subProperty, true)) { | ||
| 194 | OWLObjectPropertyExpression superProperty = tNode.getRepresentativeElement(); | ||
| 195 | addClause(subProperty, superProperty); | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | m_program.enrich(clauses); | ||
| 200 | Utility.logInfo("classification done and enriched lower bound rules."); | ||
| 201 | } | ||
| 202 | |||
| 203 | |||
| 204 | private void addClause(OWLObjectPropertyExpression subProperty, OWLObjectPropertyExpression superProperty) { | ||
| 205 | if (subProperty.equals(superProperty)) return ; | ||
| 206 | if (superProperty.toString().equals("owl:topObjectProperty")) return ; | ||
| 207 | clauses.add(DLClause.create(new Atom[] { getAtom(superProperty) }, new Atom[] { getAtom(subProperty) })); | ||
| 208 | } | ||
| 209 | |||
| 210 | private Atom getAtom(OWLObjectPropertyExpression p) { | ||
| 211 | if (p instanceof OWLObjectInverseOf) | ||
| 212 | return Atom.create(AtomicRole.create(((OWLObjectProperty) ((OWLObjectInverseOf) p).getInverse()).toStringID()), Y, X); | ||
| 213 | |||
| 214 | return Atom.create(AtomicRole.create(((OWLObjectProperty) p).toStringID()), X, Y); | ||
| 215 | } | ||
| 216 | |||
| 217 | private void addClause(OWLClass subClass, OWLClass superClass) { | ||
| 218 | if (subClass.equals(superClass)) return ; | ||
| 219 | if (subClass.toString().equals("owl:Nothing")) return ; | ||
| 220 | if (superClass.toString().equals("owl:Thing")) return ; | ||
| 221 | clauses.add(DLClause.create(new Atom[] { getAtom(superClass) }, new Atom[] { getAtom(subClass) })); | ||
| 222 | } | ||
| 223 | |||
| 224 | private Atom getAtom(OWLClass c) { | ||
| 225 | return Atom.create(AtomicConcept.create(c.toStringID()), X); | ||
| 226 | } | ||
| 227 | |||
| 228 | } \ No newline at end of file | ||
