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.kb.sparql;
020
021import java.util.HashSet;
022import java.util.Set;
023import java.util.SortedSet;
024import java.util.TreeSet;
025
026import org.apache.log4j.Logger;
027import org.semanticweb.owlapi.model.IRI;
028import org.semanticweb.owlapi.model.OWLClassExpression;
029
030import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl;
031import uk.ac.manchester.cs.owl.owlapi.OWLObjectUnionOfImpl;
032
033
034/**
035 * @author Sebastian Hellmann
036 * Enables RDFS reasoning for the DL2SPARQL class
037 * by concept rewriting
038 * 
039 */
040public class SparqlQueryDescriptionConvertRDFS {
041
042        //LOGGER: SparqlQueryDescriptionConvertVisitor
043        static Logger logger = Logger.getLogger(SparqlQueryDescriptionConvertRDFS.class);
044
045        /**
046         * 
047         * replaces each String representing a concept in descriptionKBSyntax with a
048         * union of the subclasses ex: (c sub b); (b sub a ) then: (a AND b) will be
049         * ((a OR b OR c) AND (b OR a))
050         * 
051         * @param descriptionKBSyntax the description in KB syntax
052         * @param maxDepth
053         *            determines the depth of retrieval, if 1 classes are replaced by direct subclasses only,
054         *            1 is HIGHLY RECOMMENDED FOR LARGE HIERARCHIES)
055         * @return the altered String
056         */
057        public static String conceptRewrite(String descriptionKBSyntax, SPARQLTasks st,
058                        int maxDepth) {
059                String quote = "\"";
060                String returnValue = "";
061                String currentconcept = "";
062                int lastPos = 0;
063                SortedSet<String> subclasses = new TreeSet<>();
064
065                // searches for everything in "", but backwards
066                while ((lastPos = descriptionKBSyntax.lastIndexOf(quote)) != -1) {
067                        returnValue = descriptionKBSyntax.substring(lastPos + 1, descriptionKBSyntax.length())
068                                        + returnValue;
069                        descriptionKBSyntax = descriptionKBSyntax.substring(0, lastPos);
070                        // System.out.println(description);
071                        lastPos = descriptionKBSyntax.lastIndexOf(quote);
072                        currentconcept = descriptionKBSyntax.substring(lastPos + 1, descriptionKBSyntax
073                                        .length());
074                        descriptionKBSyntax = descriptionKBSyntax.substring(0, lastPos);
075                        // replace
076                        // currentconcept="\"blabla\"";
077                        // System.out.println(currentconcept);
078
079                        // subclasses are retrieved
080                        subclasses = st.getSubClasses(currentconcept, maxDepth);
081
082                        // if only one then keep
083                        if (subclasses.size() == 1)
084                                currentconcept = "\"" + currentconcept + "\"";
085                        // replace with union
086                        else {
087                                Set<OWLClassExpression> nc = new HashSet<>();
088                                for (String one : subclasses) {
089                                        nc.add(new OWLClassImpl(IRI.create(one)));
090                                }
091                                currentconcept = new OWLObjectUnionOfImpl(nc).toString();
092                        }
093
094                        returnValue = currentconcept + returnValue;
095                        // ret+=description;
096                }
097                returnValue = descriptionKBSyntax + returnValue;
098                // System.out.println(ret);
099                return returnValue;
100        }
101
102        
103
104}