001/**
002 * Copyright (C) 2007 - 2016, Jens Lehmann
003 *
004 * This file is part of DL-Learner.
005 *
006 * DL-Learner is free software; you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation; either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * DL-Learner is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package org.dllearner.utilities.owl;
020
021import org.semanticweb.owlapi.apibinding.OWLManager;
022import org.dllearner.core.StringRenderer;
023import org.dllearner.core.StringRenderer.Rendering;
024import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom;
025import org.semanticweb.owlapi.model.OWLAnnotationPropertyDomainAxiom;
026import org.semanticweb.owlapi.model.OWLAnnotationPropertyRangeAxiom;
027import org.semanticweb.owlapi.model.OWLAsymmetricObjectPropertyAxiom;
028import org.semanticweb.owlapi.model.OWLAxiom;
029import org.semanticweb.owlapi.model.OWLAxiomVisitor;
030import org.semanticweb.owlapi.model.OWLClass;
031import org.semanticweb.owlapi.model.OWLClassAssertionAxiom;
032import org.semanticweb.owlapi.model.OWLClassExpression;
033import org.semanticweb.owlapi.model.OWLDataFactory;
034import org.semanticweb.owlapi.model.OWLDataProperty;
035import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom;
036import org.semanticweb.owlapi.model.OWLDataPropertyDomainAxiom;
037import org.semanticweb.owlapi.model.OWLDataPropertyRangeAxiom;
038import org.semanticweb.owlapi.model.OWLDataRange;
039import org.semanticweb.owlapi.model.OWLDatatypeDefinitionAxiom;
040import org.semanticweb.owlapi.model.OWLDeclarationAxiom;
041import org.semanticweb.owlapi.model.OWLDifferentIndividualsAxiom;
042import org.semanticweb.owlapi.model.OWLDisjointClassesAxiom;
043import org.semanticweb.owlapi.model.OWLDisjointDataPropertiesAxiom;
044import org.semanticweb.owlapi.model.OWLDisjointObjectPropertiesAxiom;
045import org.semanticweb.owlapi.model.OWLDisjointUnionAxiom;
046import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom;
047import org.semanticweb.owlapi.model.OWLEquivalentDataPropertiesAxiom;
048import org.semanticweb.owlapi.model.OWLEquivalentObjectPropertiesAxiom;
049import org.semanticweb.owlapi.model.OWLFunctionalDataPropertyAxiom;
050import org.semanticweb.owlapi.model.OWLFunctionalObjectPropertyAxiom;
051import org.semanticweb.owlapi.model.OWLHasKeyAxiom;
052import org.semanticweb.owlapi.model.OWLIndividual;
053import org.semanticweb.owlapi.model.OWLInverseFunctionalObjectPropertyAxiom;
054import org.semanticweb.owlapi.model.OWLInverseObjectPropertiesAxiom;
055import org.semanticweb.owlapi.model.OWLIrreflexiveObjectPropertyAxiom;
056import org.semanticweb.owlapi.model.OWLLiteral;
057import org.semanticweb.owlapi.model.OWLNegativeDataPropertyAssertionAxiom;
058import org.semanticweb.owlapi.model.OWLNegativeObjectPropertyAssertionAxiom;
059import org.semanticweb.owlapi.model.OWLObjectProperty;
060import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom;
061import org.semanticweb.owlapi.model.OWLObjectPropertyDomainAxiom;
062import org.semanticweb.owlapi.model.OWLObjectPropertyRangeAxiom;
063import org.semanticweb.owlapi.model.OWLOntologyManager;
064import org.semanticweb.owlapi.model.OWLReflexiveObjectPropertyAxiom;
065import org.semanticweb.owlapi.model.OWLSameIndividualAxiom;
066import org.semanticweb.owlapi.model.OWLSubAnnotationPropertyOfAxiom;
067import org.semanticweb.owlapi.model.OWLSubClassOfAxiom;
068import org.semanticweb.owlapi.model.OWLSubDataPropertyOfAxiom;
069import org.semanticweb.owlapi.model.OWLSubObjectPropertyOfAxiom;
070import org.semanticweb.owlapi.model.OWLSubPropertyChainOfAxiom;
071import org.semanticweb.owlapi.model.OWLSymmetricObjectPropertyAxiom;
072import org.semanticweb.owlapi.model.OWLTransitiveObjectPropertyAxiom;
073import org.semanticweb.owlapi.model.PrefixManager;
074import org.semanticweb.owlapi.model.SWRLRule;
075import org.semanticweb.owlapi.util.DefaultPrefixManager;
076
077import org.apache.jena.query.Query;
078import org.apache.jena.query.QueryFactory;
079import org.apache.jena.query.Syntax;
080
081public class OWLAxiomToSPARQLConverter implements OWLAxiomVisitor{
082        
083        private String root = "?x";
084        private String sparql;
085        private OWLClassExpressionToSPARQLConverter expressionConverter;
086        
087        public String convert(String rootVariable, OWLAxiom axiom){
088                this.root = rootVariable;
089                sparql = "";
090                expressionConverter = new OWLClassExpressionToSPARQLConverter();
091                axiom.accept(this);
092                return sparql;
093        }
094        
095        public Query asQuery(String rootVariable, OWLAxiom axiom){
096                String queryString = "SELECT DISTINCT " + rootVariable + " WHERE {";
097                queryString += convert(rootVariable, axiom);
098                queryString += "}";
099                return QueryFactory.create(queryString, Syntax.syntaxARQ);
100        }
101
102        @Override
103        public void visit(OWLAnnotationAssertionAxiom axiom) {
104        }
105
106        @Override
107        public void visit(OWLSubAnnotationPropertyOfAxiom axiom) {
108        }
109
110        @Override
111        public void visit(OWLAnnotationPropertyDomainAxiom axiom) {
112        }
113
114        @Override
115        public void visit(OWLAnnotationPropertyRangeAxiom axiom) {
116        }
117
118        @Override
119        public void visit(OWLDeclarationAxiom axiom) {
120        }
121
122        @Override
123        public void visit(OWLSubClassOfAxiom axiom) {
124                OWLClassExpression subClass = axiom.getSubClass();
125                String subClassPattern = expressionConverter.convert(root, subClass);
126                sparql += subClassPattern;
127                
128                OWLClassExpression superClass = axiom.getSuperClass();
129                String superClassPattern = expressionConverter.convert(root, superClass);
130                sparql += superClassPattern;
131        }
132
133        @Override
134        public void visit(OWLNegativeObjectPropertyAssertionAxiom axiom) {
135        }
136
137        @Override
138        public void visit(OWLAsymmetricObjectPropertyAxiom axiom) {
139        }
140
141        @Override
142        public void visit(OWLReflexiveObjectPropertyAxiom axiom) {
143        }
144
145        @Override
146        public void visit(OWLDisjointClassesAxiom axiom) {
147        }
148
149        @Override
150        public void visit(OWLDataPropertyDomainAxiom axiom) {
151        }
152
153        @Override
154        public void visit(OWLObjectPropertyDomainAxiom axiom) {
155                OWLSubClassOfAxiom subClassOfAxiom = axiom.asOWLSubClassOfAxiom();
156        }
157
158        @Override
159        public void visit(OWLEquivalentObjectPropertiesAxiom axiom) {
160        }
161
162        @Override
163        public void visit(OWLNegativeDataPropertyAssertionAxiom axiom) {
164        }
165
166        @Override
167        public void visit(OWLDifferentIndividualsAxiom axiom) {
168        }
169
170        @Override
171        public void visit(OWLDisjointDataPropertiesAxiom axiom) {
172        }
173
174        @Override
175        public void visit(OWLDisjointObjectPropertiesAxiom axiom) {
176        }
177
178        @Override
179        public void visit(OWLObjectPropertyRangeAxiom axiom) {
180        }
181
182        @Override
183        public void visit(OWLObjectPropertyAssertionAxiom axiom) {
184        }
185
186        @Override
187        public void visit(OWLFunctionalObjectPropertyAxiom axiom) {
188        }
189
190        @Override
191        public void visit(OWLSubObjectPropertyOfAxiom axiom) {
192        }
193
194        @Override
195        public void visit(OWLDisjointUnionAxiom axiom) {
196        }
197
198        @Override
199        public void visit(OWLSymmetricObjectPropertyAxiom axiom) {
200        }
201
202        @Override
203        public void visit(OWLDataPropertyRangeAxiom axiom) {
204        }
205
206        @Override
207        public void visit(OWLFunctionalDataPropertyAxiom axiom) {
208        }
209
210        @Override
211        public void visit(OWLEquivalentDataPropertiesAxiom axiom) {
212        }
213
214        @Override
215        public void visit(OWLClassAssertionAxiom axiom) {
216        }
217
218        @Override
219        public void visit(OWLEquivalentClassesAxiom axiom) {
220        }
221
222        @Override
223        public void visit(OWLDataPropertyAssertionAxiom axiom) {
224        }
225
226        @Override
227        public void visit(OWLTransitiveObjectPropertyAxiom axiom) {
228        }
229
230        @Override
231        public void visit(OWLIrreflexiveObjectPropertyAxiom axiom) {
232        }
233
234        @Override
235        public void visit(OWLSubDataPropertyOfAxiom axiom) {
236        }
237
238        @Override
239        public void visit(OWLInverseFunctionalObjectPropertyAxiom axiom) {
240        }
241
242        @Override
243        public void visit(OWLSameIndividualAxiom axiom) {
244        }
245
246        @Override
247        public void visit(OWLSubPropertyChainOfAxiom axiom) {
248        }
249
250        @Override
251        public void visit(OWLInverseObjectPropertiesAxiom axiom) {
252        }
253
254        @Override
255        public void visit(OWLHasKeyAxiom axiom) {
256        }
257
258        @Override
259        public void visit(OWLDatatypeDefinitionAxiom axiom) {
260        }
261
262        @Override
263        public void visit(SWRLRule rule) {
264        }
265        
266        public static void main(String[] args) throws Exception {
267                StringRenderer.setRenderer(Rendering.DL_SYNTAX);
268                OWLAxiomToSPARQLConverter converter = new OWLAxiomToSPARQLConverter();
269                
270                OWLOntologyManager man = OWLManager.createOWLOntologyManager();
271                OWLDataFactory df = man.getOWLDataFactory();
272                PrefixManager pm = new DefaultPrefixManager();
273                pm.setDefaultPrefix("http://dbpedia.org/ontology/");
274                
275                OWLClass clsA = df.getOWLClass("A", pm);
276                OWLClass clsB = df.getOWLClass("B", pm);
277                OWLClass clsC = df.getOWLClass("C", pm);
278                
279                OWLObjectProperty propR = df.getOWLObjectProperty("r", pm);
280                OWLObjectProperty propS = df.getOWLObjectProperty("s", pm);
281                
282                OWLDataProperty dpT = df.getOWLDataProperty("t", pm);
283                OWLDataRange booleanRange = df.getBooleanOWLDatatype();
284                OWLLiteral lit = df.getOWLLiteral(1);
285                
286                OWLIndividual indA = df.getOWLNamedIndividual("a", pm);
287                OWLIndividual  indB = df.getOWLNamedIndividual("b", pm);
288                
289                String rootVar = "?x";
290                //NAMEDCLASS
291                OWLClassExpression subClass = clsA;
292                OWLClassExpression superClass = clsB;
293                OWLAxiom axiom = df.getOWLSubClassOfAxiom(subClass, superClass);
294                String query = converter.asQuery(rootVar, axiom).toString();
295                System.out.println(axiom + "\n" + query);
296                //EXISTENTIAL RESTRICTION
297                superClass = df.getOWLObjectSomeValuesFrom(propR, clsB);
298                axiom = df.getOWLSubClassOfAxiom(subClass, superClass);
299                query = converter.asQuery(rootVar, axiom).toString();
300                System.out.println(axiom + "\n" + query);
301                //INTERSECTION
302                superClass = df.getOWLObjectIntersectionOf(
303                                df.getOWLObjectSomeValuesFrom(propR, clsB),
304                                clsB);
305                axiom = df.getOWLSubClassOfAxiom(subClass, superClass);
306                query = converter.asQuery(rootVar, axiom).toString();
307                System.out.println(axiom + "\n" + query);
308                //UNION
309                superClass = df.getOWLObjectUnionOf(
310                                clsB,
311                                clsC);
312                axiom = df.getOWLSubClassOfAxiom(subClass, superClass);
313                query = converter.asQuery(rootVar, axiom).toString();
314                System.out.println(axiom + "\n" + query);
315                //HAS VALUE
316                superClass = df.getOWLObjectHasValue(propR, indA);
317                axiom = df.getOWLSubClassOfAxiom(subClass, superClass);
318                query = converter.asQuery(rootVar, axiom).toString();
319                System.out.println(axiom + "\n" + query);
320                //UNIVERSAL RESTRICTION
321                superClass = df.getOWLObjectAllValuesFrom(propR, clsB);
322                axiom = df.getOWLSubClassOfAxiom(subClass, superClass);
323                query = converter.asQuery(rootVar, axiom).toString();
324                System.out.println(axiom + "\n" + query);
325                // ONE OF
326                superClass = df.getOWLObjectOneOf(indA, indB);
327                axiom = df.getOWLSubClassOfAxiom(subClass, superClass);
328                query = converter.asQuery(rootVar, axiom).toString();
329                System.out.println(axiom + "\n" + query);
330                //existential restriction with one of filler
331                superClass = df.getOWLObjectSomeValuesFrom(propR, df.getOWLObjectOneOf(indA, indB));
332                axiom = df.getOWLSubClassOfAxiom(subClass, superClass);
333                query = converter.asQuery(rootVar, axiom).toString();
334                System.out.println(axiom + "\n" + query);
335                
336                
337                
338        }
339
340}