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 com.google.common.base.Joiner;
022import com.google.common.collect.HashMultimap;
023import com.google.common.collect.Multimap;
024import com.google.common.collect.Sets;
025import org.apache.jena.query.Query;
026import org.apache.jena.query.QueryFactory;
027import org.apache.jena.query.Syntax;
028import org.dllearner.core.StringRenderer;
029import org.dllearner.core.StringRenderer.Rendering;
030import org.semanticweb.owlapi.apibinding.OWLManager;
031import org.semanticweb.owlapi.model.*;
032import org.semanticweb.owlapi.util.DefaultPrefixManager;
033import org.semanticweb.owlapi.vocab.OWLFacet;
034
035import javax.annotation.Nonnull;
036import java.util.*;
037
038public class OWLClassExpressionToSPARQLConverter implements OWLClassExpressionVisitor, OWLPropertyExpressionVisitor, OWLDataRangeVisitor{
039        
040        private String sparql = "";
041        private Stack<String> variables = new Stack<>();
042        
043        private Multimap<Integer, OWLEntity> properties = HashMultimap.create();
044        
045        private Map<Integer, Boolean> intersection;
046        private Set<? extends OWLEntity> variableEntities = new HashSet<>();
047        
048        private VariablesMapping mapping;
049        private boolean ignoreGenericTypeStatements = true;
050        private OWLClassExpression expr;
051        
052        private int cnt;
053        
054        private String outerGroupBy;
055        
056        private Map<OWLClassExpression, Set<String>> groupingVars = new HashMap<>();
057        private Map<OWLClassExpression, Set<String>> havingConditions = new HashMap<>();
058        
059        Stack<String> parentVar = new Stack<>();
060        Stack<OWLClassExpression> parent = new Stack<>();
061        
062        public OWLClassExpressionToSPARQLConverter(VariablesMapping mapping) {
063                this.mapping = mapping;
064        }
065        
066        public OWLClassExpressionToSPARQLConverter() {
067                mapping = new VariablesMapping();
068        }
069        
070        public VariablesMapping getVariablesMapping() {
071                return mapping;
072        }
073
074        public String convert(String rootVariable, OWLClassExpression expr){
075                this.expr = expr;
076                reset();
077                variables.push(rootVariable);
078                parent.push(expr);
079                parentVar.push(rootVariable);
080                expr.accept(this);
081                return sparql;
082        }
083        
084        public String convert(String rootVariable, OWLPropertyExpression expr){
085                variables.push(rootVariable);
086                sparql = "";
087                expr.accept(this);
088                return sparql;
089        }
090        
091        public Query asQuery(String rootVariable, OWLClassExpression expr){
092                return asQuery(rootVariable, expr, Collections.<OWLEntity>emptySet());
093        }
094        
095        public Query asQuery(String rootVariable, OWLClassExpression expr, boolean countQuery){
096                String queryString = "SELECT ";
097                String triplePattern = convert(rootVariable, expr);
098                if(countQuery){
099                        queryString += "(COUNT(DISTINCT " + rootVariable + ") AS ?cnt) WHERE {";
100                } else {
101                        queryString += "DISTINCT " + rootVariable + " WHERE {";
102                }
103                queryString += triplePattern;
104                queryString += "}";
105                if(outerGroupBy != null) {
106                        queryString += outerGroupBy;
107                }
108                return QueryFactory.create(queryString, Syntax.syntaxARQ);
109        }
110        
111        public Query asCountQuery(OWLClassExpression expr){
112                String rootVariable = "?s";
113                String queryString = "SELECT (COUNT(DISTINCT " + rootVariable + ") AS ?cnt) WHERE {";
114                String triplePattern = convert(rootVariable, expr);
115                queryString += triplePattern;
116                queryString += "}";
117                return QueryFactory.create(queryString, Syntax.syntaxARQ);
118        }
119        
120        public Query asQuery(String rootVariable, OWLClassExpression expr, Set<? extends OWLEntity> variableEntities){
121                return asQuery(rootVariable, expr, variableEntities, false);
122        }
123        
124        public Query asQuery(String rootVariable, OWLClassExpression expr, Set<? extends OWLEntity> variableEntities, boolean count){
125                this.variableEntities = variableEntities;
126                String queryString = "SELECT DISTINCT ";
127                String triplePattern = convert(rootVariable, expr);
128                if(variableEntities.isEmpty()){
129                        queryString += rootVariable + " WHERE {";
130                } else {
131                        for (OWLEntity owlEntity : variableEntities) {
132                                String var = mapping.get(owlEntity);
133                                queryString += var + " ";
134                        }
135                        if(count){
136                                queryString += "(COUNT(DISTINCT " + rootVariable + ") AS ?cnt)"; 
137                        } else {
138                                queryString += rootVariable;
139                        }
140                        queryString += " WHERE {";
141                }
142                
143                queryString += triplePattern;
144                queryString += "}";
145                if(!variableEntities.isEmpty()){
146                        if(count){
147                                queryString += "GROUP BY ";
148                                for (OWLEntity owlEntity : variableEntities) {
149                                        String var = mapping.get(owlEntity);
150                                        queryString += var;
151                                }
152                                queryString += " ORDER BY DESC(?cnt)";
153                        }
154                }
155                
156                // append GROUP BY clause if necessary
157                Set<String> groupByVars = groupingVars.get(expr);
158                if(groupByVars != null) {
159                        queryString += "GROUP BY " + Joiner.on(" ").join(groupByVars);
160                        Set<String> conditions = havingConditions.get(expr);
161                        if(conditions != null) {
162                                queryString += " HAVING(";
163                                queryString += Joiner.on(" & ").join(conditions);
164                                queryString += ")";
165                        }
166                }
167                return QueryFactory.create(queryString, Syntax.syntaxARQ);
168        }
169        
170        private void reset(){
171                variables.clear();
172                properties.clear();
173                sparql = "";
174                intersection = new HashMap<>();
175                mapping.reset();
176                cnt = 1;
177        }
178        
179        private int modalDepth(){
180                return variables.size();
181        }
182        
183        private boolean inIntersection(){
184                return intersection.containsKey(modalDepth()) ? intersection.get(modalDepth()) : false;
185        }
186        
187        private void enterIntersection(){
188                intersection.put(modalDepth(), true);
189        }
190        
191        private void leaveIntersection(){
192                intersection.remove(modalDepth());
193        }
194        
195        private String triple(String subject, String predicate, String object){
196                return (subject.startsWith("?") ? subject : "<" + subject + ">") + " " + 
197                                (predicate.startsWith("?") || predicate.equals("a") ? predicate : "<" + predicate + ">") + " " +
198                                (object.startsWith("?") ? object : object) + ".\n";
199        }
200        
201        private String triple(String subject, String predicate, OWLLiteral object){
202                return (subject.startsWith("?") ? subject : "<" + subject + ">") + " " + 
203                                (predicate.startsWith("?") || predicate.equals("a") ? predicate : "<" + predicate + ">") + " " +
204                                render(object) + ".\n";
205        }
206        
207        private String triple(String subject, String predicate, OWLEntity object){
208                return (subject.startsWith("?") ? subject : "<" + subject + ">") + " " + 
209                                (predicate.startsWith("?") || predicate.equals("a") ? predicate : "<" + predicate + ">") + " " +
210                                render(object) + ".\n";
211        }
212        
213        private String triple(String subject, OWLEntity predicate, OWLEntity object){
214                return (subject.startsWith("?") ? subject : "<" + subject + ">") + " " + 
215                                render(predicate) + " " +
216                                render(object) + ".\n";
217        }
218        
219        private String triple(String subject, OWLEntity predicate, String object){
220                return (subject.startsWith("?") ? subject : "<" + subject + ">") + " " + 
221                                render(predicate) + " " +
222                                object + ".\n";
223        }
224        
225        private String triple(String subject, OWLEntity predicate, OWLLiteral object){
226                return (subject.startsWith("?") ? subject : "<" + subject + ">") + " " + 
227                                render(predicate) + " " +
228                                render(object) + ".\n";
229        }
230        
231        private String triple(String subject, String predicate, OWLIndividual object){
232                return (subject.startsWith("?") ? subject : "<" + subject + ">") + " " + 
233                                (predicate.startsWith("?") || predicate.equals("a") ? predicate : "<" + predicate + ">") + " " +
234                                "<" + object.toStringID() + ">.\n";
235        }
236        
237        private String render(OWLEntity entity){
238                String s;
239                if(variableEntities.contains(entity)){
240                        s = mapping.getVariable(entity);
241                } else {
242                        s = "<" + entity.toStringID() + ">";
243                }
244                if(entity.isOWLObjectProperty()){
245                        properties.put(modalDepth(), entity);
246                }
247                return s;
248        }
249        
250        private String render(OWLLiteral literal){
251                return "\"" + literal.getLiteral() + "\"^^<" + literal.getDatatype().toStringID() + ">";
252        }
253        
254        private String newCountVar() {
255                return "?cnt" + cnt++;
256        }
257
258        @Override
259        public void visit(OWLObjectProperty property) {}
260
261        @Override
262        public void visit(OWLObjectInverseOf property) {}
263
264        @Override
265        public void visit(OWLDataProperty property) {}
266
267        @Override
268        public void visit(@Nonnull OWLAnnotationProperty property) {}
269
270        @Override
271        public void visit(OWLClass ce) {
272                if(ce.equals(expr) || !ignoreGenericTypeStatements || !ce.isOWLThing()){
273                        sparql += triple(variables.peek(), "a", render(ce));
274                }
275        }
276
277        @Override
278        public void visit(OWLObjectIntersectionOf ce) {
279                enterIntersection();
280                List<OWLClassExpression> operands = ce.getOperandsAsList();
281                for (OWLClassExpression operand : operands) {
282                        operand.accept(this);
283                }
284                Collection<OWLEntity> props = properties.get(modalDepth());
285                if(props.size() > 1){
286                        Collection<String> vars = new TreeSet<>();
287                        for (OWLEntity p : props) {
288                                if(mapping.containsKey(p)){
289                                        vars.add(mapping.get(p));
290                                }
291                        }
292                        if(vars.size() == 2){
293                                List<String> varList = new ArrayList<>(vars);
294                                sparql += "FILTER(" + varList.get(0) + "!=" + varList.get(1) + ")";
295                        }
296                }
297                leaveIntersection();
298        }
299
300        @Override
301        public void visit(OWLObjectUnionOf ce) {
302                List<OWLClassExpression> operands = ce.getOperandsAsList();
303                for (int i = 0; i < operands.size()-1; i++) {
304                        sparql += "{";
305                        OWLClassExpression operand = operands.get(i);
306                        parent.push(operand);
307                        parentVar.push(variables.peek());
308                        operand.accept(this);
309                        parent.pop();
310                        sparql += "}";
311                        sparql += " UNION ";
312                }
313                sparql += "{";
314                operands.get(operands.size()-1).accept(this);
315                sparql += "}";
316        }
317
318        @Override
319        public void visit(OWLObjectComplementOf ce) {
320                String subject = variables.peek();
321                if(!inIntersection() && modalDepth() == 1){
322                        sparql += triple(subject, "?p", "?o");
323                } 
324                sparql += "FILTER NOT EXISTS {";
325                ce.getOperand().accept(this);
326                sparql += "}";
327        }
328
329        @Override
330        public void visit(OWLObjectSomeValuesFrom ce) {
331                String objectVariable = mapping.newIndividualVariable();
332                OWLObjectPropertyExpression propertyExpression = ce.getProperty();
333                if(propertyExpression.isAnonymous()){
334                        //property expression is inverse of a property
335                        sparql += triple(objectVariable, propertyExpression.getNamedProperty(), variables.peek());
336                } else {
337                        sparql += triple(variables.peek(), propertyExpression.getNamedProperty(), objectVariable);
338                }
339                OWLClassExpression filler = ce.getFiller();
340                variables.push(objectVariable);
341                filler.accept(this);
342                variables.pop();
343        }
344
345        @Override
346        public void visit(OWLObjectAllValuesFrom ce) {
347                String subject = variables.peek();
348                String objectVariable = mapping.newIndividualVariable();
349                OWLObjectPropertyExpression propertyExpression = ce.getProperty();
350                OWLObjectProperty predicate = propertyExpression.getNamedProperty();
351                OWLClassExpression filler = ce.getFiller();
352                
353                if(filler.isOWLThing()) {
354                        sparql += triple(variables.peek(), mapping.newPropertyVariable(), objectVariable);
355                } else {
356                        if(propertyExpression.isAnonymous()){ //property expression is inverse of a property
357                                sparql += triple(objectVariable, predicate, variables.peek());
358                        } else {
359                                sparql += triple(variables.peek(), predicate, objectVariable);
360                        }
361                        
362                        // restrict filler
363                                String var = mapping.newIndividualVariable();
364                                String cntVar1 = newCountVar();
365                                sparql += "{SELECT " + subject + " (COUNT(" + var + ") AS " + cntVar1 + ") WHERE {";
366                                sparql += triple(subject, predicate, var);
367                                variables.push(var);
368                                filler.accept(this);
369                                variables.pop();
370                                sparql += "} GROUP BY " + subject + "}";
371
372                                var = mapping.newIndividualVariable();
373                                String cntVar2 = newCountVar();
374                                sparql += "{SELECT " + subject + " (COUNT(" + var + ") AS " + cntVar2 + ") WHERE {";
375                                sparql += triple(subject, predicate, var);
376                                sparql += "} GROUP BY " + subject + "}";
377
378                                sparql += "FILTER(" + cntVar1 + "=" + cntVar2 + ")";
379                }
380                
381        }
382
383        @Override
384        public void visit(OWLObjectHasValue ce) {
385                OWLObjectPropertyExpression propertyExpression = ce.getProperty();
386                OWLNamedIndividual value = ce.getFiller().asOWLNamedIndividual();
387                if(propertyExpression.isAnonymous()){
388                        //property expression is inverse of a property
389                        sparql += triple(value.toStringID(), propertyExpression.getNamedProperty(), variables.peek());
390                } else {
391                        sparql += triple(variables.peek(), propertyExpression.getNamedProperty(), value);
392                }
393        }
394
395        @Override
396        public void visit(OWLObjectMinCardinality ce) {
397                processObjectCardinalityRestriction(ce);
398        }
399
400        @Override
401        public void visit(OWLObjectExactCardinality ce) {
402                processObjectCardinalityRestriction(ce);
403        }
404
405        @Override
406        public void visit(OWLObjectMaxCardinality ce) {
407                processObjectCardinalityRestriction(ce);
408        }
409        
410        public void processObjectCardinalityRestriction(OWLObjectCardinalityRestriction ce) {
411                String subjectVariable = variables.peek();
412                String objectVariable = mapping.newIndividualVariable();
413                OWLObjectPropertyExpression propertyExpression = ce.getProperty();
414                int cardinality = ce.getCardinality();
415                
416                String comparator;
417                if(ce instanceof OWLObjectMinCardinality) {
418                        comparator = ">=";
419                } else if(ce instanceof OWLObjectExactCardinality) {
420                        comparator = "=";
421                } else {
422                        comparator = "<=";
423                }
424                
425//              sparql += "{SELECT " + subjectVariable + " WHERE {";
426                
427                if(propertyExpression.isAnonymous()){
428                        //property expression is inverse of a property
429                        sparql += triple(objectVariable, propertyExpression.getNamedProperty(), subjectVariable);
430                } else {
431                        sparql += triple(subjectVariable, propertyExpression.getNamedProperty(), objectVariable);
432                }
433                
434                OWLClassExpression filler = ce.getFiller();
435                variables.push(objectVariable);
436                filler.accept(this);
437                variables.pop();
438                
439                String havingCondition = "COUNT(" + objectVariable + ")" + comparator + cardinality;
440                
441                groupingVars.put(parent.peek(), Sets.newHashSet(parentVar.peek()));
442                havingConditions.put(parent.peek(), Sets.newHashSet(havingCondition));
443                
444//              sparql += "} GROUP BY " + subjectVariable + " HAVING(" + havingCondition + ")}";
445        }
446        
447        public void processDataCardinalityRestriction(OWLDataCardinalityRestriction ce) {
448                String subjectVariable = variables.peek();
449                String objectVariable = mapping.newIndividualVariable();
450                OWLDataPropertyExpression propertyExpression = ce.getProperty();
451                int cardinality = ce.getCardinality();
452                
453                String comparator;
454                if(ce instanceof OWLDataMinCardinality) {
455                        comparator = ">=";
456                } else if(ce instanceof OWLDataExactCardinality) {
457                        comparator = "=";
458                } else {
459                        comparator = "<=";
460                }
461                
462                sparql += "{SELECT " + subjectVariable + " WHERE {";
463                
464                if(propertyExpression.isAnonymous()){
465                        //property expression is inverse of a property
466                        sparql += triple(objectVariable, propertyExpression.asOWLDataProperty(), subjectVariable);
467                } else {
468                        sparql += triple(subjectVariable, propertyExpression.asOWLDataProperty(), objectVariable);
469                }
470                
471                OWLDataRange filler = ce.getFiller();
472                variables.push(objectVariable);
473                filler.accept(this);
474                variables.pop();
475                
476                sparql += "} GROUP BY " + subjectVariable + " HAVING(COUNT(" + objectVariable + ")" + comparator + cardinality + ")}";
477        }
478
479        @Override
480        public void visit(OWLObjectHasSelf ce) {
481                String subject = variables.peek();
482                OWLObjectPropertyExpression property = ce.getProperty();
483                sparql += triple(subject, property.getNamedProperty(), subject);
484        }
485
486        @Override
487        public void visit(OWLObjectOneOf ce) {
488                String subject = variables.peek();
489                if(modalDepth() == 1){
490                        sparql += triple(subject, "?p", "?o");
491                } 
492                sparql += "FILTER(" + subject + " IN (";
493                String values = "";
494                for (OWLIndividual ind : ce.getIndividuals()) {
495                        if(!values.isEmpty()){
496                                values += ",";
497                        }
498                        values += "<" + ind.toStringID() + ">";
499                }
500                sparql += values;
501                sparql +=  "))"; 
502                
503        }
504
505        @Override
506        public void visit(OWLDataSomeValuesFrom ce) {
507                String objectVariable = mapping.newIndividualVariable();
508                OWLDataPropertyExpression propertyExpression = ce.getProperty();
509                sparql += triple(variables.peek(), propertyExpression.asOWLDataProperty(), objectVariable);
510                OWLDataRange filler = ce.getFiller();
511                variables.push(objectVariable);
512                filler.accept(this);
513                variables.pop();
514        }
515
516        @Override
517        public void visit(OWLDataAllValuesFrom ce) {
518                String subject = variables.peek();
519                String objectVariable = mapping.newIndividualVariable();
520                OWLDataPropertyExpression propertyExpression = ce.getProperty();
521                String predicate = propertyExpression.asOWLDataProperty().toStringID();
522                OWLDataRange filler = ce.getFiller();
523                sparql += triple(variables.peek(), predicate, objectVariable);
524                
525                String var = mapping.newIndividualVariable();
526                sparql += "{SELECT " + subject + " (COUNT(" + var + ") AS ?cnt1) WHERE {";
527                sparql += triple(subject, predicate, var);
528                variables.push(var);
529                filler.accept(this);
530                variables.pop();
531                sparql += "} GROUP BY " + subject + "}";
532                
533                var = mapping.newIndividualVariable();
534                sparql += "{SELECT " + subject + " (COUNT(" + var + ") AS ?cnt2) WHERE {";
535                sparql += triple(subject, predicate, var);
536                sparql += "} GROUP BY " + subject + "}";
537                
538                sparql += "FILTER(?cnt1=?cnt2)";
539        }
540
541        @Override
542        public void visit(OWLDataHasValue ce) {
543                OWLDataPropertyExpression propertyExpression = ce.getProperty();
544                OWLLiteral value = ce.getFiller();
545                sparql += triple(variables.peek(), propertyExpression.asOWLDataProperty(), value);
546        }
547
548        @Override
549        public void visit(OWLDataMinCardinality ce) {
550                processDataCardinalityRestriction(ce);
551        }
552
553        @Override
554        public void visit(OWLDataExactCardinality ce) {
555                processDataCardinalityRestriction(ce);
556        }
557
558        @Override
559        public void visit(OWLDataMaxCardinality ce) {
560                processDataCardinalityRestriction(ce);
561        }
562        
563        @Override
564        public void visit(OWLDatatype node) {
565                if(ignoreGenericTypeStatements && !node.isRDFPlainLiteral() && !node.isTopDatatype()){
566                        sparql += "FILTER(DATATYPE(" + variables.peek() + "=<" + node.getIRI().toString() + ">))";
567                }
568        }
569
570        @Override
571        public void visit(OWLDataOneOf node) {
572                String subject = variables.peek();
573                if(modalDepth() == 1){
574                        sparql += triple(subject, "?p", "?o");
575                } 
576                sparql += "FILTER(" + subject + " IN (";
577                String values = "";
578                for (OWLLiteral value : node.getValues()) {
579                        if(!values.isEmpty()){
580                                values += ",";
581                        }
582                        values += render(value);
583                }
584                sparql += values;
585                sparql +=  "))"; 
586        }
587
588        @Override
589        public void visit(OWLDataComplementOf node) {
590        }
591
592        @Override
593        public void visit(OWLDataIntersectionOf node) {
594        }
595
596        @Override
597        public void visit(OWLDataUnionOf node) {
598        }
599
600        @Override
601        public void visit(OWLDatatypeRestriction node) {
602                Set<OWLFacetRestriction> facets = node.getFacetRestrictions();
603                
604                for (OWLFacetRestriction facetRestriction : facets) {
605                        OWLFacet facet = facetRestriction.getFacet();
606                        OWLLiteral value = facetRestriction.getFacetValue();
607                        
608                        switch(facet) {
609                        case FRACTION_DIGITS:
610                                break;
611                        case LANG_RANGE:
612                                break;
613                        case LENGTH:
614                                break;
615                        case MAX_EXCLUSIVE: sparql += "FILTER(" + variables.peek() + " < " + "\"" + value.getLiteral() + "\"^^<" + value.getDatatype().toStringID() + ">)";
616                                break;
617                        case MAX_INCLUSIVE: sparql += "FILTER(" + variables.peek() + " <= " + "\"" + value.getLiteral() + "\"^^<" + value.getDatatype().toStringID() + ">)";
618                                break;
619                        case MAX_LENGTH:
620                                break;
621                        case MIN_EXCLUSIVE: sparql += "FILTER(" + variables.peek() + " > " + "\"" + value.getLiteral() + "\"^^<" + value.getDatatype().toStringID() + ">)";
622                                break;
623                        case MIN_INCLUSIVE: sparql += "FILTER(" + variables.peek() + " >= " + "\"" + value.getLiteral() + "\"^^<" + value.getDatatype().toStringID() + ">)";
624                                break;
625                        case MIN_LENGTH:
626                                break;
627                        case PATTERN:
628                                break;
629                        case TOTAL_DIGITS:
630                                break;
631                        default:
632                                break;
633                        }
634                }
635        }
636        
637        public static void main(String[] args) throws Exception {
638                StringRenderer.setRenderer(Rendering.DL_SYNTAX);
639                OWLClassExpressionToSPARQLConverter converter = new OWLClassExpressionToSPARQLConverter();
640                
641                OWLOntologyManager man = OWLManager.createOWLOntologyManager();
642                OWLDataFactory df = man.getOWLDataFactory();
643                PrefixManager pm = new DefaultPrefixManager();
644                pm.setDefaultPrefix("http://dbpedia.org/ontology/");
645                
646                OWLClass clsA = df.getOWLClass("A", pm);
647                OWLClass clsB = df.getOWLClass("B", pm);
648                OWLClass clsC = df.getOWLClass("C", pm);
649                
650                OWLObjectProperty propR = df.getOWLObjectProperty("r", pm);
651                OWLObjectProperty propS = df.getOWLObjectProperty("s", pm);
652                
653                OWLDataProperty dpT = df.getOWLDataProperty("t", pm);
654                OWLDataRange booleanRange = df.getBooleanOWLDatatype();
655                OWLLiteral lit = df.getOWLLiteral(1);
656                
657                OWLIndividual indA = df.getOWLNamedIndividual("a", pm);
658                OWLIndividual  indB = df.getOWLNamedIndividual("b", pm);
659                
660                String rootVar = "?x";
661                
662                OWLClassExpression expr = clsA;
663                String query = converter.asQuery(rootVar, expr).toString();
664                System.out.println(expr + "\n" + query);
665                
666                expr = df.getOWLObjectSomeValuesFrom(propR, clsB);
667                query = converter.asQuery(rootVar, expr).toString();
668                System.out.println(expr + "\n" + query);
669                
670                expr = df.getOWLObjectIntersectionOf(
671                                df.getOWLObjectSomeValuesFrom(propR, clsB),
672                                clsB);
673                query = converter.asQuery(rootVar, expr).toString();
674                System.out.println(expr + "\n" + query);
675                
676                expr = df.getOWLObjectUnionOf(
677                                clsA,
678                                clsB);
679                query = converter.asQuery(rootVar, expr).toString();
680                System.out.println(expr + "\n" + query);
681                
682                expr = df.getOWLObjectHasValue(propR, indA);
683                query = converter.asQuery(rootVar, expr).toString();
684                System.out.println(expr + "\n" + query);
685                
686                expr = df.getOWLObjectAllValuesFrom(propR, df.getOWLThing());
687                query = converter.asQuery(rootVar, expr).toString();
688                System.out.println(expr + "\n" + query);
689                
690                expr = df.getOWLObjectAllValuesFrom(propR, clsB);
691                query = converter.asQuery(rootVar, expr).toString();
692                System.out.println(expr + "\n" + query);
693                
694                expr = df.getOWLObjectAllValuesFrom(df.getOWLObjectProperty("language", pm), df.getOWLClass("Language", pm));
695                query = converter.asQuery(rootVar, expr).toString();
696                System.out.println(expr + "\n" + query);
697                
698                expr = df.getOWLObjectMinCardinality(2, df.getOWLObjectProperty("language", pm), df.getOWLClass("Language", pm));
699                query = converter.asQuery(rootVar, expr).toString();
700                System.out.println(expr + "\n" + query);
701                
702                expr = df.getOWLObjectIntersectionOf(
703                                df.getOWLClass("Place", pm),
704                                df.getOWLObjectMinCardinality(
705                                                2, 
706                                                df.getOWLObjectProperty("language", pm), 
707                                                df.getOWLClass("Language", pm)));
708                query = converter.asQuery(rootVar, expr).toString();
709                System.out.println(expr + "\n" + query);
710                
711                expr = df.getOWLObjectOneOf(indA, indB);
712                query = converter.asQuery(rootVar, expr).toString();
713                System.out.println(expr + "\n" + query);
714                
715                expr = df.getOWLObjectSomeValuesFrom(propR, df.getOWLObjectOneOf(indA, indB));
716                query = converter.asQuery(rootVar, expr).toString();
717                System.out.println(expr + "\n" + query);
718                
719                expr = df.getOWLObjectIntersectionOf(
720                                clsA, 
721                                df.getOWLObjectHasSelf(propR));
722                query = converter.asQuery(rootVar, expr).toString();
723                System.out.println(expr + "\n" + query);
724                
725                expr = df.getOWLObjectIntersectionOf(
726                                clsA, 
727                                df.getOWLDataSomeValuesFrom(dpT, booleanRange));
728                query = converter.asQuery(rootVar, expr).toString();
729                System.out.println(expr + "\n" + query);
730                
731                expr = df.getOWLObjectIntersectionOf(
732                                clsA, 
733                                df.getOWLDataHasValue(dpT, lit));
734                query = converter.asQuery(rootVar, expr).toString();
735                System.out.println(expr + "\n" + query);
736                
737                expr = df.getOWLObjectIntersectionOf(
738                                clsA, 
739                                df.getOWLDataMinCardinality(2, dpT, booleanRange));
740                query = converter.asQuery(rootVar, expr).toString();
741                System.out.println(expr + "\n" + query);
742                
743                expr = df.getOWLObjectComplementOf(clsB);
744                query = converter.asQuery(rootVar, expr).toString();
745                System.out.println(expr + "\n" + query);
746                
747                expr = df.getOWLObjectIntersectionOf(
748                                clsA, 
749                                df.getOWLObjectComplementOf(clsB));
750                query = converter.asQuery(rootVar, expr).toString();
751                System.out.println(expr + "\n" + query);
752                
753                expr = df.getOWLObjectSomeValuesFrom(propR, 
754                                df.getOWLObjectIntersectionOf(
755                                                clsA, 
756                                                df.getOWLObjectComplementOf(clsB)));
757                query = converter.asQuery(rootVar, expr).toString();
758                System.out.println(expr + "\n" + query);
759                
760                expr = df.getOWLDataAllValuesFrom(dpT, booleanRange);
761                query = converter.asQuery(rootVar, expr).toString();
762                System.out.println(expr + "\n" + query);
763                
764                expr = df.getOWLDataAllValuesFrom(dpT,df.getOWLDataOneOf(lit));
765                query = converter.asQuery(rootVar, expr).toString();
766                System.out.println(expr + "\n" + query);
767                
768                //variable entity
769                expr = df.getOWLObjectIntersectionOf(
770                                df.getOWLObjectSomeValuesFrom(propR, clsB),
771                                clsB, df.getOWLObjectSomeValuesFrom(propS, clsA));
772                query = converter.asQuery(rootVar, expr, Sets.newHashSet(propR, propS)).toString();
773                System.out.println(expr + "\n" + query);
774                
775                expr = df.getOWLObjectIntersectionOf(
776                                df.getOWLObjectSomeValuesFrom(propR, df.getOWLObjectIntersectionOf(df.getOWLObjectSomeValuesFrom(propS, clsA), clsC)),
777                                clsB);
778                query = converter.asQuery(rootVar, expr, Sets.newHashSet(propR, propS)).toString();
779                System.out.println(expr + "\n" + query);
780                
781                expr = df.getOWLObjectSomeValuesFrom(
782                                propS, 
783                                df.getOWLObjectMaxCardinality(
784                                                4, 
785                                                df.getOWLObjectProperty(IRI.create("http://dl-learner.org/carcinogenesis#hasAtom")), 
786                                                df.getOWLThing()
787                                )
788                                );
789                query = converter.asQuery(rootVar, expr).toString();
790                System.out.println(expr + "\n" + query);
791                
792                expr = df.getOWLObjectAllValuesFrom(propR, df.getOWLThing());
793                query = converter.asQuery(rootVar, expr).toString();
794                System.out.println(expr + "\n" + query);
795
796                expr = df.getOWLObjectSomeValuesFrom(propR, df.getOWLThing());
797                query = converter.asQuery(rootVar, expr).toString();
798                System.out.println(expr + "\n" + query);}
799}