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