aboutsummaryrefslogtreecommitdiff
path: root/src/uk/ac/ox/cs/pagoda/rules/Program.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/rules/Program.java')
-rw-r--r--src/uk/ac/ox/cs/pagoda/rules/Program.java456
1 files changed, 227 insertions, 229 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/rules/Program.java b/src/uk/ac/ox/cs/pagoda/rules/Program.java
index 2e5302b..4e147bb 100644
--- a/src/uk/ac/ox/cs/pagoda/rules/Program.java
+++ b/src/uk/ac/ox/cs/pagoda/rules/Program.java
@@ -1,5 +1,6 @@
1package uk.ac.ox.cs.pagoda.rules; 1package uk.ac.ox.cs.pagoda.rules;
2 2
3import org.apache.commons.io.FilenameUtils;
3import org.semanticweb.HermiT.Configuration; 4import org.semanticweb.HermiT.Configuration;
4import org.semanticweb.HermiT.model.*; 5import org.semanticweb.HermiT.model.*;
5import org.semanticweb.HermiT.structural.OWLClausification; 6import org.semanticweb.HermiT.structural.OWLClausification;
@@ -24,78 +25,180 @@ public abstract class Program implements KnowledgeBase {
24 protected OWLOntology ontology; 25 protected OWLOntology ontology;
25 protected DLOntology dlOntology; 26 protected DLOntology dlOntology;
26 protected BottomStrategy botStrategy; 27 protected BottomStrategy botStrategy;
27
28 private String additionalDataFile = null;
29
30 protected Collection<DLClause> clauses = new HashSet<DLClause>(); 28 protected Collection<DLClause> clauses = new HashSet<DLClause>();
31// protected Set<DLClause> used = new HashSet<DLClause>(); 29// protected Set<DLClause> used = new HashSet<DLClause>();
32 protected PredicateDependency dependencyGraph; 30protected PredicateDependency dependencyGraph;
33 31 protected LinkedList<OWLTransitiveObjectPropertyAxiom> transitiveAxioms;
34 /** 32 protected LinkedList<DLClause> transitiveClauses;
35 * clone all information of another program after load() 33 protected LinkedList<OWLSubPropertyChainOfAxiom> subPropChainAxioms;
36 * 34 protected LinkedList<DLClause> subPropChainClauses;
37 * @param program 35 private String additionalDataFile = null;
38 */ 36
39 void clone(Program program) { 37 public static String toString(Collection<DLClause> clauses) {
40 this.ontologyDirectory = program.ontologyDirectory; 38 StringBuilder sb = new StringBuilder(DLClauseHelper.toString(clauses));
41 this.ontology = program.ontology; 39 sb.insert(0, MyPrefixes.PAGOdAPrefixes.prefixesText());
42 this.dlOntology = program.dlOntology; 40 return sb.toString();
43 this.botStrategy = program.botStrategy;
44 this.additionalDataFile = program.additionalDataFile;
45 this.transitiveAxioms = program.transitiveAxioms;
46 this.transitiveClauses = program.transitiveClauses;
47 this.subPropChainAxioms = program.subPropChainAxioms;
48 this.subPropChainClauses = program.subPropChainClauses;
49 } 41 }
50 42
51 public void load(OWLOntology o, BottomStrategy botStrategy) { 43 public void load(OWLOntology o, BottomStrategy botStrategy) {
52 this.botStrategy = botStrategy; 44 this.botStrategy = botStrategy;
53 RLPlusOntology owlOntology = new RLPlusOntology(); 45 RLPlusOntology owlOntology = new RLPlusOntology();
54 owlOntology.load(o, new NullaryBottom()); 46 owlOntology.load(o, new NullaryBottom());
55 owlOntology.simplify(); 47 owlOntology.simplify();
56 48
57 ontology = owlOntology.getTBox(); 49 ontology = owlOntology.getTBox();
58 String ontologyPath = OWLHelper.getOntologyPath(ontology); 50// String ontologyPath = OWLHelper.getOntologyPath(ontology);
59 ontologyDirectory = ontologyPath.substring(0, ontologyPath.lastIndexOf(Utility.JAVA_FILE_SEPARATOR)); 51// ontologyDirectory = ontologyPath.substring(0, ontologyPath.lastIndexOf(Utility.JAVA_FILE_SEPARATOR));
60 clausify(); 52 String ontologyPath = ontology.getOWLOntologyManager().getOntologyDocumentIRI(ontology).toURI().getPath();
61 53 ontologyDirectory = FilenameUtils.getFullPath(ontologyPath);
54 clausify();
55
62 String aboxOWLFile = owlOntology.getABoxPath(); 56 String aboxOWLFile = owlOntology.getABoxPath();
63 OWLOntology abox = OWLHelper.loadOntology(aboxOWLFile); 57 OWLOntology abox = OWLHelper.loadOntology(aboxOWLFile);
64 OWLOntologyManager manager = abox.getOWLOntologyManager(); 58 OWLOntologyManager manager = abox.getOWLOntologyManager();
65 OWLAxiom axiom; 59 OWLAxiom axiom;
66 for (Atom atom: dlOntology.getPositiveFacts()) { 60 for (Atom atom: dlOntology.getPositiveFacts()) {
67 if ((axiom = OWLHelper.getABoxAssertion(manager.getOWLDataFactory(), atom)) != null) 61 if ((axiom = OWLHelper.getABoxAssertion(manager.getOWLDataFactory(), atom)) != null)
68 manager.addAxiom(abox, axiom); 62 manager.addAxiom(abox, axiom);
69 } 63 }
70 64
71 try { 65 try {
72 FileOutputStream out = new FileOutputStream(aboxOWLFile); 66 FileOutputStream out = new FileOutputStream(aboxOWLFile);
73 manager.saveOntology(abox, out); 67 manager.saveOntology(abox, out);
74 out.close(); 68 out.close();
75 } catch (OWLOntologyStorageException e) { 69 } catch(IOException | OWLOntologyStorageException e) {
76 // TODO Auto-generated catch block
77 e.printStackTrace();
78 } catch (FileNotFoundException e) {
79 // TODO Auto-generated catch block
80 e.printStackTrace();
81 } catch (IOException e) {
82 // TODO Auto-generated catch block
83 e.printStackTrace(); 70 e.printStackTrace();
71 System.exit(1);
84 } 72 }
85 73
86 if (!abox.isEmpty()) { 74 if (!abox.isEmpty()) {
87 SimpleETL rewriter = new SimpleETL(owlOntology.getOntologyIRI(), aboxOWLFile); 75 SimpleETL rewriter = new SimpleETL(owlOntology.getOntologyIRI(), aboxOWLFile);
88 try { 76 try {
89 rewriter.rewrite(); 77 rewriter.rewrite();
90 } catch (Exception e) { 78 } catch (Exception e) {
91 e.printStackTrace(); 79 e.printStackTrace();
92 } 80 }
93 additionalDataFile = rewriter.getExportedFile(); 81 additionalDataFile = rewriter.getExportedFile();
94 new File(aboxOWLFile).delete(); 82 new File(aboxOWLFile).delete();
95 } 83 }
96 84
85 }
86
87 public String getAdditionalDataFile() {
88 return additionalDataFile;
89 }
90
91 @Override
92 public void transform() {
93 for(DLClause dlClause : dlOntology.getDLClauses()) {
94 DLClause simplifiedDLClause = DLClauseHelper.removeNominalConcept(dlClause);
95 simplifiedDLClause = removeAuxiliaryBodyAtoms(simplifiedDLClause);
96 simplifiedDLClause = DLClauseHelper.replaceWithDataValue(simplifiedDLClause);
97 convert(simplifiedDLClause);
98 }
99
100 addingTransitiveAxioms();
101 addingSubPropertyChainAxioms();
102
103 Collection<DLClause> botRelated = new LinkedList<DLClause>();
104 Variable X = Variable.create("X");
105 botRelated.add(DLClause.create(new Atom[0], new Atom[]{Atom.create(Inequality.INSTANCE, X, X)}));
106 clauses.addAll(botStrategy.process(botRelated));
107
108 if(this instanceof GeneralProgram)
109 Utility.logInfo("The number of rules: " + (clauses.size() - 1));
97 } 110 }
98 111
112 @Override
113 public void save() {
114 try {
115 BufferedWriter ruleWriter = new BufferedWriter(new OutputStreamWriter(
116 new FileOutputStream(getOutputPath())));
117 ruleWriter.write(toString());
118 ruleWriter.close();
119 } catch(IOException e) {
120 e.printStackTrace();
121 }
122 Utility.logDebug("The rules are saved in " + getOutputPath() + ".");
123 }
124
125 @Override
126 public String toString() {
127 return toString(clauses);
128 }
129
130 public final void convert(DLClause clause) {
131 Collection<DLClause> tempClauses = convert2Clauses(clause);
132 clauses.addAll(tempClauses);
133 }
134
135 public abstract Collection<DLClause> convert2Clauses(DLClause clause);
136
137 public abstract String getOutputPath();
138
139 public OWLOntology getOntology() {
140 return ontology;
141 }
142
143 public Collection<DLClause> getClauses() {
144 return clauses;
145 }
146
147 public Collection<DLClause> getClauses(DLClause queryClause) {
148// if (true) return new HashSet<DLClause>(clauses);
149 Set<DLPredicate> predicates = new HashSet<DLPredicate>();
150 predicates.addAll(dependencyGraph.collectPredicate(queryClause.getBodyAtoms()));
151
152 Set<DLPredicate> dependence = new HashSet<DLPredicate>();
153 for(DLPredicate predicate : predicates)
154 dependence.addAll(dependencyGraph.getAncesters(predicate));
155
156 Collection<DLClause> relevantClauses = new LinkedList<DLClause>();
157 for(DLClause clause : clauses) {
158 if(relevant(clause, dependence))
159 relevantClauses.add(clause);
160
161 }
162 return relevantClauses;
163 }
164
165 public PredicateDependency buildDependencyGraph() {
166 if(dependencyGraph == null)
167 return dependencyGraph = new PredicateDependency(clauses);
168 else
169 return dependencyGraph;
170 }
171
172 public void getDependencyGraph(PredicateDependency g) {
173 dependencyGraph = g;
174 }
175
176 public final String getDirectory() {
177 return Utility.getGlobalTempDirAbsolutePath();
178 }
179
180 public void deleteABoxTurtleFile() {
181 if(additionalDataFile != null)
182 new File(additionalDataFile).delete();
183 }
184
185 /**
186 * clone all information of another program after load()
187 *
188 * @param program
189 */
190 void clone(Program program) {
191 this.ontologyDirectory = program.ontologyDirectory;
192 this.ontology = program.ontology;
193 this.dlOntology = program.dlOntology;
194 this.botStrategy = program.botStrategy;
195 this.additionalDataFile = program.additionalDataFile;
196 this.transitiveAxioms = program.transitiveAxioms;
197 this.transitiveClauses = program.transitiveClauses;
198 this.subPropChainAxioms = program.subPropChainAxioms;
199 this.subPropChainClauses = program.subPropChainClauses;
200 }
201
99 private void clausify() { 202 private void clausify() {
100 Configuration conf = new Configuration(); 203 Configuration conf = new Configuration();
101 OWLClausification clausifier = new OWLClausification(conf); 204 OWLClausification clausifier = new OWLClausification(conf);
@@ -103,121 +206,94 @@ public abstract class Program implements KnowledgeBase {
103 OWLOntologyManager manager = ontology.getOWLOntologyManager(); 206 OWLOntologyManager manager = ontology.getOWLOntologyManager();
104 try { 207 try {
105 filteredOntology = manager.createOntology(); 208 filteredOntology = manager.createOntology();
106 } catch (OWLOntologyCreationException e) { 209 } catch(OWLOntologyCreationException e) {
107 e.printStackTrace(); 210 e.printStackTrace();
108 } 211 }
109 212
110 transitiveAxioms = new LinkedList<OWLTransitiveObjectPropertyAxiom>(); 213 transitiveAxioms = new LinkedList<OWLTransitiveObjectPropertyAxiom>();
111 subPropChainAxioms = new LinkedList<OWLSubPropertyChainOfAxiom>(); 214 subPropChainAxioms = new LinkedList<OWLSubPropertyChainOfAxiom>();
112 215
113 OWLDatatype date = ontology.getOWLOntologyManager().getOWLDataFactory().getOWLDatatype(IRI.create("http://www.w3.org/2001/XMLSchema#date")); 216 OWLDatatype date = ontology.getOWLOntologyManager()
114 int noOfDataPropertyRangeAxioms = 0, noOfAxioms = 0; 217 .getOWLDataFactory()
115 for (OWLOntology onto: ontology.getImportsClosure()) 218 .getOWLDatatype(IRI.create("http://www.w3.org/2001/XMLSchema#date"));
116 for (OWLAxiom axiom: onto.getAxioms()) { 219 int noOfDataPropertyRangeAxioms = 0, noOfAxioms = 0;
117 if (axiom instanceof OWLTransitiveObjectPropertyAxiom) 220 for(OWLOntology onto : ontology.getImportsClosure())
221 for(OWLAxiom axiom : onto.getAxioms()) {
222 if(axiom instanceof OWLTransitiveObjectPropertyAxiom)
118 transitiveAxioms.add((OWLTransitiveObjectPropertyAxiom) axiom); 223 transitiveAxioms.add((OWLTransitiveObjectPropertyAxiom) axiom);
119 else if (axiom instanceof OWLSubPropertyChainOfAxiom) 224 else if(axiom instanceof OWLSubPropertyChainOfAxiom)
120 subPropChainAxioms.add((OWLSubPropertyChainOfAxiom) axiom); 225 subPropChainAxioms.add((OWLSubPropertyChainOfAxiom) axiom);
121 // TODO to filter out datatype axioms 226 // TODO to filter out datatype axioms
122 else if (axiom instanceof OWLDataPropertyRangeAxiom) { 227 else if(axiom instanceof OWLDataPropertyRangeAxiom) {
123 ++noOfDataPropertyRangeAxioms; 228 ++noOfDataPropertyRangeAxioms;
124 Utility.logInfo("The axiom: " + axiom + " is being ignored."); 229 Utility.logInfo("The axiom: " + axiom + " is being ignored.");
125 } 230 }
126 else { 231 else {
127 if (axiom.getDatatypesInSignature().contains(date)) { 232 if(axiom.getDatatypesInSignature().contains(date)) {
128 Utility.logInfo("The axiom: " + axiom + " is being ignored."); 233 Utility.logInfo("The axiom: " + axiom + " is being ignored.");
129 } 234 }
130 else manager.addAxiom(filteredOntology, axiom); 235 else manager.addAxiom(filteredOntology, axiom);
131 } 236 }
132 237
133 if (axiom instanceof OWLAnnotationAssertionAxiom || 238 if(axiom instanceof OWLAnnotationAssertionAxiom ||
134 axiom instanceof OWLSubAnnotationPropertyOfAxiom || 239 axiom instanceof OWLSubAnnotationPropertyOfAxiom ||
135 axiom instanceof OWLDeclarationAxiom || 240 axiom instanceof OWLDeclarationAxiom ||
136 axiom instanceof OWLDataPropertyRangeAxiom) { 241 axiom instanceof OWLDataPropertyRangeAxiom) {
137 } 242 }
138 else { 243 else {
139// System.out.println(axiom); 244// System.out.println(axiom);
140 ++noOfAxioms; 245 ++noOfAxioms;
141 } 246 }
142 247
143 } 248 }
144 Utility.logInfo("The number of data property range axioms that are ignored: " + noOfDataPropertyRangeAxioms + "(" + noOfAxioms + ")"); 249 Utility.logInfo("The number of data property range axioms that are ignored: " + noOfDataPropertyRangeAxioms + "(" + noOfAxioms + ")");
145 250
146 dlOntology = (DLOntology)clausifier.preprocessAndClausify(filteredOntology, null)[1]; 251 dlOntology = (DLOntology) clausifier.preprocessAndClausify(filteredOntology, null)[1];
147 clausifier = null; 252 clausifier = null;
148 } 253 }
149
150 public String getAdditionalDataFile() {
151 return additionalDataFile;
152 }
153
154 protected LinkedList<OWLTransitiveObjectPropertyAxiom> transitiveAxioms;
155 protected LinkedList<DLClause> transitiveClauses;
156
157 protected LinkedList<OWLSubPropertyChainOfAxiom> subPropChainAxioms;
158 protected LinkedList<DLClause> subPropChainClauses;
159
160 @Override
161 public void transform() {
162 for (DLClause dlClause: dlOntology.getDLClauses()) {
163 DLClause simplifiedDLClause = DLClauseHelper.removeNominalConcept(dlClause);
164 simplifiedDLClause = removeAuxiliaryBodyAtoms(simplifiedDLClause);
165 simplifiedDLClause = DLClauseHelper.replaceWithDataValue(simplifiedDLClause);
166 convert(simplifiedDLClause);
167 }
168 254
169 addingTransitiveAxioms();
170 addingSubPropertyChainAxioms();
171
172 Collection<DLClause> botRelated = new LinkedList<DLClause>();
173 Variable X = Variable.create("X");
174 botRelated.add(DLClause.create(new Atom[0], new Atom[] {Atom.create(Inequality.INSTANCE, X, X)}));
175 clauses.addAll(botStrategy.process(botRelated));
176
177 if (this instanceof GeneralProgram)
178 Utility.logInfo("The number of rules: " + (clauses.size() - 1));
179 }
180
181 private DLClause removeAuxiliaryBodyAtoms(DLClause dlClause) { 255 private DLClause removeAuxiliaryBodyAtoms(DLClause dlClause) {
182 Collection<Atom> newBodyAtoms = new LinkedList<Atom>(); 256 Collection<Atom> newBodyAtoms = new LinkedList<Atom>();
183 DLPredicate p; 257 DLPredicate p;
184 for (Atom bodyAtom: dlClause.getBodyAtoms()) { 258 for(Atom bodyAtom : dlClause.getBodyAtoms()) {
185 p = bodyAtom.getDLPredicate(); 259 p = bodyAtom.getDLPredicate();
186 if (p instanceof AtomicConcept || 260 if(p instanceof AtomicConcept ||
187 p instanceof AtomicRole || p instanceof InverseRole || 261 p instanceof AtomicRole || p instanceof InverseRole ||
188 p instanceof Equality || p instanceof AnnotatedEquality || p instanceof Inequality) 262 p instanceof Equality || p instanceof AnnotatedEquality || p instanceof Inequality)
189 newBodyAtoms.add(bodyAtom); 263 newBodyAtoms.add(bodyAtom);
190 } 264 }
191 LinkedList<Atom> newHeadAtoms = new LinkedList<Atom>(); 265 LinkedList<Atom> newHeadAtoms = new LinkedList<Atom>();
192 Map<Variable, Term> assign = new HashMap<Variable, Term>(); 266 Map<Variable, Term> assign = new HashMap<Variable, Term>();
193 for (Atom headAtom: dlClause.getHeadAtoms()) { 267 for(Atom headAtom : dlClause.getHeadAtoms()) {
194 p = headAtom.getDLPredicate(); 268 p = headAtom.getDLPredicate();
195 if (p instanceof AtomicNegationDataRange) { 269 if(p instanceof AtomicNegationDataRange) {
196 AtomicDataRange positive = ((AtomicNegationDataRange) p).getNegatedDataRange(); 270 AtomicDataRange positive = ((AtomicNegationDataRange) p).getNegatedDataRange();
197 if (!(positive instanceof ConstantEnumeration)) 271 if(!(positive instanceof ConstantEnumeration))
198 newBodyAtoms.add(Atom.create(positive, headAtom.getArgument(0))); 272 newBodyAtoms.add(Atom.create(positive, headAtom.getArgument(0)));
199 else if (((ConstantEnumeration) positive).getNumberOfConstants() == 1) { 273 else if(((ConstantEnumeration) positive).getNumberOfConstants() == 1) {
200 assign.put((Variable) headAtom.getArgument(0), ((ConstantEnumeration) positive).getConstant(0)); 274 assign.put((Variable) headAtom.getArgument(0), ((ConstantEnumeration) positive).getConstant(0));
201// newBodyAtoms.add(Atom.create(Equality.INSTANCE, headAtom.getArgument(0), ((ConstantEnumeration) positive).getConstant(0))); 275// newBodyAtoms.add(Atom.create(Equality.INSTANCE, headAtom.getArgument(0), ((ConstantEnumeration) positive).getConstant(0)));
202 } 276 }
203 else newHeadAtoms.add(headAtom); 277 else newHeadAtoms.add(headAtom);
204 } 278 }
205 else 279 else
206 newHeadAtoms.add(headAtom); 280 newHeadAtoms.add(headAtom);
207 } 281 }
208 282
209 if (assign.isEmpty() && newHeadAtoms.isEmpty() && newBodyAtoms.size() == dlClause.getBodyLength()) 283 if(assign.isEmpty() && newHeadAtoms.isEmpty() && newBodyAtoms.size() == dlClause.getBodyLength())
210 return dlClause; 284 return dlClause;
211 285
212 Atom[] headArray = newHeadAtoms.size() == dlClause.getHeadLength() ? dlClause.getHeadAtoms() : newHeadAtoms.toArray(new Atom[0]); 286 Atom[] headArray =
213 Atom[] bodyArray = newBodyAtoms.size() == dlClause.getBodyLength() ? dlClause.getBodyAtoms() : newBodyAtoms.toArray(new Atom[0]); 287 newHeadAtoms.size() == dlClause.getHeadLength() ? dlClause.getHeadAtoms() : newHeadAtoms.toArray(new Atom[0]);
214 if (!assign.isEmpty()) { 288 Atom[] bodyArray =
215 for (int i = 0; i < headArray.length; ++i) 289 newBodyAtoms.size() == dlClause.getBodyLength() ? dlClause.getBodyAtoms() : newBodyAtoms.toArray(new Atom[0]);
216 headArray[i] = DLClauseHelper.getInstance(headArray[i], assign); 290 if(!assign.isEmpty()) {
217 for (int i = 0; i < bodyArray.length; ++i) 291 for(int i = 0; i < headArray.length; ++i)
218 bodyArray[i] = DLClauseHelper.getInstance(bodyArray[i], assign); 292 headArray[i] = DLClauseHelper.getInstance(headArray[i], assign);
293 for(int i = 0; i < bodyArray.length; ++i)
294 bodyArray[i] = DLClauseHelper.getInstance(bodyArray[i], assign);
219 } 295 }
220 return DLClause.create(headArray, bodyArray); 296 return DLClause.create(headArray, bodyArray);
221 } 297 }
222 298
223 private void addingTransitiveAxioms() { 299 private void addingTransitiveAxioms() {
@@ -225,136 +301,58 @@ public abstract class Program implements KnowledgeBase {
225 Atom headAtom; 301 Atom headAtom;
226 Variable X = Variable.create("X"), Y = Variable.create("Y"), Z = Variable.create("Z"); 302 Variable X = Variable.create("X"), Y = Variable.create("Y"), Z = Variable.create("Z");
227 transitiveClauses = new LinkedList<DLClause>(); 303 transitiveClauses = new LinkedList<DLClause>();
228 for (OWLTransitiveObjectPropertyAxiom axiom: transitiveAxioms) { 304 for(OWLTransitiveObjectPropertyAxiom axiom : transitiveAxioms) {
229 OWLObjectPropertyExpression objExp = axiom.getProperty(); 305 OWLObjectPropertyExpression objExp = axiom.getProperty();
230 headAtom = getAtom(objExp, X, Z); 306 headAtom = getAtom(objExp, X, Z);
231 Atom[] bodyAtoms = new Atom[2]; 307 Atom[] bodyAtoms = new Atom[2];
232 bodyAtoms[0] = getAtom(objExp, X, Y); 308 bodyAtoms[0] = getAtom(objExp, X, Y);
233 bodyAtoms[1] = getAtom(objExp, Y, Z); 309 bodyAtoms[1] = getAtom(objExp, Y, Z);
234 transitiveClause = DLClause.create(new Atom[] {headAtom}, bodyAtoms); 310 transitiveClause = DLClause.create(new Atom[]{headAtom}, bodyAtoms);
235 clauses.add(transitiveClause); 311 clauses.add(transitiveClause);
236 transitiveClauses.add(transitiveClause); 312 transitiveClauses.add(transitiveClause);
237 } 313 }
238 } 314 }
239 315
240 private Atom getAtom(OWLObjectPropertyExpression exp, Variable x, Variable y) { 316 private Atom getAtom(OWLObjectPropertyExpression exp, Variable x, Variable y) {
241 if (exp instanceof OWLObjectProperty) 317 if(exp instanceof OWLObjectProperty)
242 return Atom.create(AtomicRole.create(((OWLObjectProperty) exp).toStringID()), x, y); 318 return Atom.create(AtomicRole.create(((OWLObjectProperty) exp).toStringID()), x, y);
243 OWLObjectInverseOf inverseOf; 319 OWLObjectInverseOf inverseOf;
244 if (exp instanceof OWLObjectInverseOf && (inverseOf = (OWLObjectInverseOf) exp).getInverse() instanceof OWLObjectProperty) 320 if(exp instanceof OWLObjectInverseOf && (inverseOf =
321 (OWLObjectInverseOf) exp).getInverse() instanceof OWLObjectProperty)
245 return Atom.create(AtomicRole.create(((OWLObjectProperty) inverseOf).toStringID()), x, y); 322 return Atom.create(AtomicRole.create(((OWLObjectProperty) inverseOf).toStringID()), x, y);
246 return null; 323 return null;
247 } 324 }
248 325
249 private void addingSubPropertyChainAxioms() { 326 private void addingSubPropertyChainAxioms() {
250 DLClause dlClause; 327 DLClause dlClause;
251 subPropChainClauses = new LinkedList<DLClause>(); 328 subPropChainClauses = new LinkedList<DLClause>();
252 Atom headAtom; 329 Atom headAtom;
253 Iterator<OWLObjectPropertyExpression> iterExp; 330 Iterator<OWLObjectPropertyExpression> iterExp;
254 OWLObjectPropertyExpression objExp; 331 OWLObjectPropertyExpression objExp;
255 for (OWLSubPropertyChainOfAxiom axiom: subPropChainAxioms) { 332 for(OWLSubPropertyChainOfAxiom axiom : subPropChainAxioms) {
256 objExp = axiom.getSuperProperty(); 333 objExp = axiom.getSuperProperty();
257 List<OWLObjectPropertyExpression> objs = axiom.getPropertyChain(); 334 List<OWLObjectPropertyExpression> objs = axiom.getPropertyChain();
258 headAtom = getAtom(objExp, Variable.create("X"), Variable.create("X" + objs.size())); 335 headAtom = getAtom(objExp, Variable.create("X"), Variable.create("X" + objs.size()));
259 iterExp = objs.iterator(); 336 iterExp = objs.iterator();
260 int index = 1; 337 int index = 1;
261 Atom[] bodyAtoms = new Atom[objs.size()]; 338 Atom[] bodyAtoms = new Atom[objs.size()];
262 bodyAtoms[0] = getAtom(iterExp.next(), Variable.create("X"), Variable.create("X1")); 339 bodyAtoms[0] = getAtom(iterExp.next(), Variable.create("X"), Variable.create("X1"));
263 while (index < objs.size()) { 340 while(index < objs.size()) {
264 bodyAtoms[index] = getAtom(iterExp.next(), Variable.create("X" + index), Variable.create("X" + (index + 1))); 341 bodyAtoms[index] =
265 ++index; 342 getAtom(iterExp.next(), Variable.create("X" + index), Variable.create("X" + (index + 1)));
343 ++index;
266 } 344 }
267 dlClause = DLClause.create(new Atom[] {headAtom}, bodyAtoms); 345 dlClause = DLClause.create(new Atom[]{headAtom}, bodyAtoms);
268 clauses.add(dlClause); 346 clauses.add(dlClause);
269 subPropChainClauses.add(dlClause); 347 subPropChainClauses.add(dlClause);
270 } 348 }
271 } 349 }
272 350
273 @Override
274 public void save() {
275 try {
276 BufferedWriter ruleWriter = new BufferedWriter(new OutputStreamWriter(
277 new FileOutputStream(getOutputPath())));
278 ruleWriter.write(toString());
279 ruleWriter.close();
280 } catch (IOException e) {
281 e.printStackTrace();
282 }
283 Utility.logDebug("The rules are saved in " + getOutputPath() + ".");
284 }
285
286 @Override
287 public String toString() {
288 return toString(clauses);
289 }
290
291 public static String toString(Collection<DLClause> clauses) {
292 StringBuilder sb = new StringBuilder(DLClauseHelper.toString(clauses));
293 sb.insert(0, MyPrefixes.PAGOdAPrefixes.prefixesText());
294 return sb.toString();
295 }
296
297 public final void convert(DLClause clause) {
298 Collection<DLClause> tempClauses = convert2Clauses(clause);
299 clauses.addAll(tempClauses);
300 }
301
302 public abstract Collection<DLClause> convert2Clauses(DLClause clause);
303
304 public abstract String getOutputPath();
305
306
307 public OWLOntology getOntology() {
308 return ontology;
309 }
310
311 public Collection<DLClause> getClauses() {
312 return clauses;
313 }
314
315 public Collection<DLClause> getClauses(DLClause queryClause) {
316// if (true) return new HashSet<DLClause>(clauses);
317 Set<DLPredicate> predicates = new HashSet<DLPredicate>();
318 predicates.addAll(dependencyGraph.collectPredicate(queryClause.getBodyAtoms()));
319
320 Set<DLPredicate> dependence = new HashSet<DLPredicate>();
321 for (DLPredicate predicate: predicates)
322 dependence.addAll(dependencyGraph.getAncesters(predicate));
323
324 Collection<DLClause> relevantClauses = new LinkedList<DLClause>();
325 for (DLClause clause: clauses) {
326 if (relevant(clause, dependence))
327 relevantClauses.add(clause);
328
329 }
330 return relevantClauses;
331 }
332
333 private boolean relevant(DLClause clause, Set<DLPredicate> set) { 351 private boolean relevant(DLClause clause, Set<DLPredicate> set) {
334 for (DLPredicate p: dependencyGraph.collectPredicate(clause.getHeadAtoms())) 352 for(DLPredicate p : dependencyGraph.collectPredicate(clause.getHeadAtoms()))
335 if (set.contains(p)) 353 if(set.contains(p))
336 return true; 354 return true;
337 return false; 355 return false;
338 }
339
340 public PredicateDependency buildDependencyGraph() {
341 if (dependencyGraph == null)
342 return dependencyGraph = new PredicateDependency(clauses);
343 else
344 return dependencyGraph;
345 }
346
347 public void getDependencyGraph(PredicateDependency g) {
348 dependencyGraph = g;
349 }
350
351 public final String getDirectory() {
352 return Utility.getGlobalTempDirAbsolutePath();
353 }
354
355 public void deleteABoxTurtleFile() {
356 if (additionalDataFile != null)
357 new File(additionalDataFile).delete();
358 } 356 }
359 357
360} 358}