From: <ku...@us...> - 2007-12-03 14:45:16
|
Revision: 310 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=310&view=rev Author: kurzum Date: 2007-12-03 06:45:11 -0800 (Mon, 03 Dec 2007) Log Message: ----------- changed and Formatted everything Delete old Cache files Examples will follow Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java trunk/src/dl-learner/org/dllearner/kb/sparql/ClassNode.java trunk/src/dl-learner/org/dllearner/kb/sparql/Configuration.java trunk/src/dl-learner/org/dllearner/kb/sparql/ExtractionAlgorithm.java trunk/src/dl-learner/org/dllearner/kb/sparql/InstanceNode.java trunk/src/dl-learner/org/dllearner/kb/sparql/Manager.java trunk/src/dl-learner/org/dllearner/kb/sparql/Manipulator.java trunk/src/dl-learner/org/dllearner/kb/sparql/Node.java trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedEndpoint.java trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedFilter.java trunk/src/dl-learner/org/dllearner/kb/sparql/PropertyNode.java trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryMaker.java trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryType.java trunk/src/dl-learner/org/dllearner/kb/sparql/SpecificSparqlEndpoint.java trunk/src/dl-learner/org/dllearner/kb/sparql/Test.java trunk/src/dl-learner/org/dllearner/kb/sparql/Tupel.java trunk/src/dl-learner/org/dllearner/kb/sparql/TypedSparqlQuery.java Modified: trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java 2007-12-03 14:45:11 UTC (rev 310) @@ -43,6 +43,7 @@ import org.dllearner.core.config.StringSetConfigOption; import org.dllearner.core.dl.KB; import org.dllearner.kb.sparql.Manager; +import org.dllearner.kb.sparql.Manipulator; import org.dllearner.kb.sparql.PredefinedEndpoint; import org.dllearner.kb.sparql.PredefinedFilter; import org.dllearner.kb.sparql.SparqlQueryType; @@ -52,94 +53,108 @@ import org.dllearner.reasoning.JenaOWLDIGConverter; /** - * Represents a SPARQL Endpoint. - * TODO: Is it necessary to create a class DBpediaSparqlEndpoint? + * Represents a SPARQL Endpoint. TODO: Is it necessary to create a class + * DBpediaSparqlEndpoint? * * @author Jens Lehmann * @author Sebastian Knappe */ public class SparqlEndpointRestructured extends KnowledgeSource { - - //ConfigOptions - private URL url; + + // ConfigOptions + private URL url; String host; private Set<String> instances; private URL dumpFile; private int numberOfRecursions; - private int predefinedFilter=0; - private int predefinedEndpoint=0; + private int predefinedFilter = 0; + private int predefinedEndpoint = 0; private Set<String> predList; private Set<String> objList; private Set<String> classList; private String format; private boolean dumpToFile; - private boolean useLits=false; - private boolean getAllBackground=false; - + private boolean useLits = false; + private boolean getAllBackground = false; + + private boolean learnDomain = false; + private String role; + private String blankNodeIdentifier = "bnode"; + /** - * Holds the results of the calculateSubjects method + * Holds the results of the calculateSubjects method */ private String[] subjects; - + /** - * Holds the results of the calculateTriples method + * Holds the results of the calculateTriples method */ private String[] triples; - + /** - * Holds the results of the calculateConceptSubjects method + * Holds the results of the calculateConceptSubjects method */ private String[] conceptSubjects; - + /** * if a method is running this becomes true */ - private boolean subjectThreadRunning=false; - - private boolean triplesThreadRunning=false; - - private boolean conceptThreadRunning=false; - + private boolean subjectThreadRunning = false; + + private boolean triplesThreadRunning = false; + + private boolean conceptThreadRunning = false; + /** * the Thread that is running a method */ private Thread subjectThread; - + private Thread triplesThread; - + private Thread conceptThread; - - //received ontology as array, used if format=Array(an element of the - //array consists of the subject, predicate and object separated by '<' + + // received ontology as array, used if format=Array(an element of the + // array consists of the subject, predicate and object separated by '<' private String[] ontArray; - - //received ontology as KB, the internal format + + // received ontology as KB, the internal format private KB kb; - + public static String getName() { return "SPARQL Endpoint Restructured"; } - + /** * sets the ConfigOptions for this KnowledgeSource + * * @return */ public static Collection<ConfigOption<?>> createConfigOptions() { - Collection<ConfigOption<?>> options = new LinkedList<ConfigOption<?>>(); + Collection<ConfigOption<?>> options = new LinkedList<ConfigOption<?>>(); options.add(new StringConfigOption("url", "URL of SPARQL Endpoint")); options.add(new StringConfigOption("host", "host of SPARQL Endpoint")); - options.add(new StringSetConfigOption("instances","relevant instances e.g. positive and negative examples in a learning problem")); - options.add(new IntegerConfigOption("numberOfRecursions","number of Recursions, the Sparql-Endpoint is asked")); - options.add(new IntegerConfigOption("predefinedFilter","the mode of the SPARQL Filter")); - options.add(new IntegerConfigOption("predefinedEndpoint","the mode of the SPARQL Filter")); - - options.add(new StringSetConfigOption("predList","a predicate list")); - options.add(new StringSetConfigOption("objList","an object list")); - options.add(new StringSetConfigOption("classList","a class list")); + options.add(new StringSetConfigOption("instances", + "relevant instances e.g. positive and negative examples in a learning problem")); + options.add(new IntegerConfigOption("numberOfRecursions", + "number of Recursions, the Sparql-Endpoint is asked")); + options.add(new IntegerConfigOption("predefinedFilter", "the mode of the SPARQL Filter")); + options.add(new IntegerConfigOption("predefinedEndpoint", "the mode of the SPARQL Filter")); + + options.add(new StringSetConfigOption("predList", "a predicate list")); + options.add(new StringSetConfigOption("objList", "an object list")); + options.add(new StringSetConfigOption("classList", "a class list")); options.add(new StringConfigOption("format", "N-TRIPLES or KB format")); - options.add(new BooleanConfigOption("dumpToFile", "wether Ontology from DBPedia is written to a file or not")); - options.add(new BooleanConfigOption("useLits","use Literals in SPARQL query")); - options.add(new BooleanConfigOption("getAllBackground","get")); + options.add(new BooleanConfigOption("dumpToFile", + "wether Ontology from DBPedia is written to a file or not")); + options.add(new BooleanConfigOption("useLits", "use Literals in SPARQL query")); + options.add(new BooleanConfigOption("getAllBackground", "get")); + + options.add(new BooleanConfigOption("learnDomain", "learns the Domain for a Role")); + options.add(new StringConfigOption("role", "role to learn Domain from")); + options.add(new StringConfigOption("blankNodeIdentifier", + "used to identify blanknodes in Tripels")); + return options; } @@ -147,7 +162,7 @@ * @see org.dllearner.core.Component#applyConfigEntry(org.dllearner.core.ConfigEntry) */ @Override - @SuppressWarnings({"unchecked"}) + @SuppressWarnings( { "unchecked" }) public <T> void applyConfigEntry(ConfigEntry<T> entry) throws InvalidConfigOptionValueException { String option = entry.getOptionName(); if (option.equals("url")) { @@ -155,103 +170,124 @@ try { url = new URL(s); } catch (MalformedURLException e) { - throw new InvalidConfigOptionValueException(entry.getOption(), entry.getValue(),"malformed URL " + s); + throw new InvalidConfigOptionValueException(entry.getOption(), entry.getValue(), + "malformed URL " + s); } - } else if(option.equals("host")) { + } else if (option.equals("host")) { host = (String) entry.getValue(); - } else if(option.equals("instances")) { + } else if (option.equals("instances")) { instances = (Set<String>) entry.getValue(); - } else if(option.equals("numberOfRecursions")){ - numberOfRecursions=(Integer)entry.getValue(); - } else if(option.equals("predList")) { + } else if (option.equals("numberOfRecursions")) { + numberOfRecursions = (Integer) entry.getValue(); + } else if (option.equals("predList")) { predList = (Set<String>) entry.getValue(); - } else if(option.equals("objList")) { + } else if (option.equals("objList")) { objList = (Set<String>) entry.getValue(); - } else if(option.equals("classList")) { + } else if (option.equals("classList")) { classList = (Set<String>) entry.getValue(); - } else if(option.equals("predefinedEndpoint")){ - predefinedEndpoint=(Integer)entry.getValue(); - } else if(option.equals("predefinedFilter")){ - predefinedFilter=(Integer)entry.getValue(); - } else if(option.equals("format")){ - format=(String)entry.getValue(); - } else if(option.equals("dumpToFile")){ - dumpToFile=(Boolean)entry.getValue(); - } else if(option.equals("useLits")){ - useLits=(Boolean)entry.getValue(); - } else if(option.equals("getAllBackground")){ - getAllBackground=(Boolean)entry.getValue(); + } else if (option.equals("predefinedEndpoint")) { + predefinedEndpoint = (Integer) entry.getValue(); + } else if (option.equals("predefinedFilter")) { + predefinedFilter = (Integer) entry.getValue(); + } else if (option.equals("format")) { + format = (String) entry.getValue(); + } else if (option.equals("dumpToFile")) { + dumpToFile = (Boolean) entry.getValue(); + } else if (option.equals("useLits")) { + useLits = (Boolean) entry.getValue(); + } else if (option.equals("getAllBackground")) { + getAllBackground = (Boolean) entry.getValue(); + } else if (option.equals("learnDomain")) { + learnDomain = (Boolean) entry.getValue(); + } else if (option.equals("role")) { + role = (String) entry.getValue(); + } else if (option.equals("blankNodeIdentifier")) { + blankNodeIdentifier = (String) entry.getValue(); } + } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see org.dllearner.core.Component#init() */ @Override public void init() { System.out.println("SparqlModul: Collecting Ontology"); - //SparqlOntologyCollector oc= - - - //new SparqlOntologyCollector(Datastructures.setToArray(instances), numberOfRecursions, filterMode, - //Datastructures.setToArray(predList),Datastructures.setToArray( objList),Datastructures.setToArray(classList),format,url,useLits); - Manager m=new Manager(); - SpecificSparqlEndpoint sse=null; - SparqlQueryType sqt=null; + // SparqlOntologyCollector oc= + // new SparqlOntologyCollector(Datastructures.setToArray(instances), + // numberOfRecursions, filterMode, + // Datastructures.setToArray(predList),Datastructures.setToArray( + // objList),Datastructures.setToArray(classList),format,url,useLits); + Manager m = new Manager(); + SpecificSparqlEndpoint sse = null; + SparqlQueryType sqt = null; + // get Options for Manipulator + Manipulator man = new Manipulator(blankNodeIdentifier); HashMap<String, String> parameters = new HashMap<String, String>(); parameters.put("default-graph-uri", "http://dbpedia.org"); parameters.put("format", "application/sparql-results.xml"); - - if(predefinedEndpoint>=1){ - sse=PredefinedEndpoint.getEndpoint(predefinedEndpoint); - + + // get Options for endpoints + if (predefinedEndpoint >= 1) { + sse = PredefinedEndpoint.getEndpoint(predefinedEndpoint); + } else { + sse = new SpecificSparqlEndpoint(url, host, parameters); } - else{ - SpecificSparqlEndpoint se=new SpecificSparqlEndpoint(url, host, parameters); - + + // get Options for Filters + if (predefinedFilter >= 1) { + sqt = PredefinedFilter.getFilter(predefinedFilter); + + } else { + sqt = new SparqlQueryType("forbid", objList, predList, useLits + ""); } - - if(predefinedFilter>=1){ - sqt=PredefinedFilter.getFilter(predefinedFilter); - + // give everything to the manager + m.useConfiguration(sqt, sse, man, numberOfRecursions, getAllBackground); + try { + String ont = ""; + // used to learn a domain of a role + if (learnDomain) { + instances = m.getDomainInstancesForRole(role); + // ad the role to the filter(a solution is always EXISTS + // role.TOP) + m.addPredicateFilter(role); + System.out.println(instances); } - else{ - sqt=new SparqlQueryType("forbid", objList,predList,useLits+""); - - - - } - m.useConfiguration(sqt, sse,numberOfRecursions,getAllBackground); - try - { - String ont=m.extract(instances); - - if (dumpToFile){ - String filename=System.currentTimeMillis()+".nt"; - String basedir="cache"+File.separator; - try{ - if(!new File(basedir).exists()) + // the actual extraction is started here + ont = m.extract(instances); + + if (dumpToFile) { + String filename = System.currentTimeMillis() + ".nt"; + String basedir = "cache" + File.separator; + try { + if (!new File(basedir).exists()) new File(basedir).mkdir(); - - FileWriter fw=new FileWriter(new File(basedir+filename),true); + + FileWriter fw = new FileWriter(new File(basedir + filename), true); fw.write(ont); fw.flush(); fw.close(); - - dumpFile=(new File(basedir+filename)).toURI().toURL(); - }catch (Exception e) {e.printStackTrace();} + + dumpFile = (new File(basedir + filename)).toURI().toURL(); + } catch (Exception e) { + e.printStackTrace(); + } } if (format.equals("KB")) { - try{ - kb=KBParser.parseKBFile(new StringReader(ont)); - } catch(Exception e) {e.printStackTrace();} + try { + kb = KBParser.parseKBFile(new StringReader(ont)); + } catch (Exception e) { + e.printStackTrace(); + } } - }catch(Exception e) { + } catch (Exception e) { e.printStackTrace(); } System.out.println("SparqlModul: ****Finished"); } - + /* * (non-Javadoc) * @@ -259,97 +295,94 @@ */ @Override public String toDIG(URI kbURI) { - if (format.equals("N-TRIPLES")) return JenaOWLDIGConverter.getTellsString(dumpFile, OntologyFormat.N_TRIPLES, kbURI); - else return DIGConverter.getDIGString(kb, kbURI).toString(); + if (format.equals("N-TRIPLES")) + return JenaOWLDIGConverter.getTellsString(dumpFile, OntologyFormat.N_TRIPLES, kbURI); + else + return DIGConverter.getDIGString(kb, kbURI).toString(); } - /* (non-Javadoc) - * @see org.dllearner.core.KnowledgeSource#export(java.io.File, org.dllearner.core.OntologyFormat) + /* + * (non-Javadoc) + * + * @see org.dllearner.core.KnowledgeSource#export(java.io.File, + * org.dllearner.core.OntologyFormat) */ @Override public void export(File file, OntologyFormat format) throws OntologyFormatUnsupportedException { - // currently no export functions implemented, so we just throw an exception + // currently no export functions implemented, so we just throw an + // exception throw new OntologyFormatUnsupportedException("export", format); } - + public URL getURL() { return url; } - + public String[] getOntArray() { return ontArray; } - - public void calculateSubjects(String label,int limit) - { + + public void calculateSubjects(String label, int limit) { System.out.println("SparqlModul: Collecting Subjects"); - SparqlOntologyCollector oc=new SparqlOntologyCollector(url); - try{ - subjects=oc.getSubjectsFromLabel(label,limit); - }catch (IOException e){ - subjects=new String[1]; - subjects[0]="[Error]Sparql Endpoint could not be reached."; + SparqlOntologyCollector oc = new SparqlOntologyCollector(url); + try { + subjects = oc.getSubjectsFromLabel(label, limit); + } catch (IOException e) { + subjects = new String[1]; + subjects[0] = "[Error]Sparql Endpoint could not be reached."; } System.out.println("SparqlModul: ****Finished"); } - + public void calculateTriples(String subject) { System.out.println("SparqlModul: Collecting Triples"); - SparqlOntologyCollector oc=new SparqlOntologyCollector(url); - try{ - triples=oc.collectTriples(subject); - }catch (IOException e){ - triples=new String[1]; - triples[0]="[Error]Sparql Endpoint could not be reached."; + SparqlOntologyCollector oc = new SparqlOntologyCollector(url); + try { + triples = oc.collectTriples(subject); + } catch (IOException e) { + triples = new String[1]; + triples[0] = "[Error]Sparql Endpoint could not be reached."; } System.out.println("SparqlModul: ****Finished"); } - - public void calculateConceptSubjects(String concept) - { + + public void calculateConceptSubjects(String concept) { System.out.println("SparqlModul: Collecting Subjects"); - SparqlOntologyCollector oc=new SparqlOntologyCollector(url); - try{ - conceptSubjects=oc.getSubjectsFromConcept(concept); - }catch (IOException e){ - conceptSubjects=new String[1]; - conceptSubjects[0]="[Error]Sparql Endpoint could not be reached."; + SparqlOntologyCollector oc = new SparqlOntologyCollector(url); + try { + conceptSubjects = oc.getSubjectsFromConcept(concept); + } catch (IOException e) { + conceptSubjects = new String[1]; + conceptSubjects[0] = "[Error]Sparql Endpoint could not be reached."; } System.out.println("SparqlModul: ****Finished"); } - - public boolean subjectThreadIsRunning() - { + + public boolean subjectThreadIsRunning() { return subjectThreadRunning; } - - public void setSubjectThreadRunning(boolean bool) - { - subjectThreadRunning=bool; + + public void setSubjectThreadRunning(boolean bool) { + subjectThreadRunning = bool; } - - public boolean triplesThreadIsRunning() - { + + public boolean triplesThreadIsRunning() { return triplesThreadRunning; } - - public void setTriplesThreadRunning(boolean bool) - { - triplesThreadRunning=bool; + + public void setTriplesThreadRunning(boolean bool) { + triplesThreadRunning = bool; } - - public boolean conceptThreadIsRunning() - { + + public boolean conceptThreadIsRunning() { return conceptThreadRunning; } - - public void setConceptThreadRunning(boolean bool) - { - conceptThreadRunning=bool; + + public void setConceptThreadRunning(boolean bool) { + conceptThreadRunning = bool; } - - public String[] getSubjects() - { + + public String[] getSubjects() { return subjects; } @@ -377,13 +410,11 @@ this.conceptThread = conceptThread; } - public String[] getTriples() - { + public String[] getTriples() { return triples; } - - public String[] getConceptSubjects() - { + + public String[] getConceptSubjects() { return conceptSubjects; } } Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,3 +1,22 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; import java.io.File; @@ -7,19 +26,20 @@ import java.io.ObjectOutputStream; import java.io.Serializable; import java.net.URLEncoder; +import java.util.HashMap; public class Cache implements Serializable { // Object can be the cache itself // or a cache object(one entry) + // it now uses a hashmap and can contain different queries at once final static long serialVersionUID = 104; transient String basedir = ""; transient String fileending = ".cache"; long timestamp; - String content = ""; long daysoffreshness = 15; long multiplier = 24 * 60 * 60 * 1000;// h m s ms - String sparqlquery = ""; + private HashMap<String, String> hm; // constructor for the cache itself public Cache(String path) { @@ -28,14 +48,15 @@ System.out.println(new File(path).mkdir()); ; } - } // constructor for single cache object(one entry) - public Cache(String c, String sparql) { - this.content = c; - this.sparqlquery = sparql; + public Cache(String sparql, String content) { + // this.content = c; + // this.sparqlquery = sparql; this.timestamp = System.currentTimeMillis(); + this.hm = new HashMap<String, String>(); + hm.put(sparql, content); } public String get(String key, String sparql) { @@ -49,20 +70,32 @@ if (!c.checkFreshness()) return null; // System.out.println("fresh"); - if (!c.validate(sparql)) + String xml = ""; + try { + xml = c.hm.get(sparql); + } catch (Exception e) { return null; + } + return xml; // System.out.println("valid"); - ret = c.content; + // ret = c.content; } catch (Exception e) { e.printStackTrace(); } return ret; }; - public void put(String key, String content, String sparql) { + public void put(String key, String sparql, String content) { // System.out.println("put into "+key); - Cache c = new Cache(content, sparql); - putIntoFile(makeFilename(key), c); + Cache c = readFromFile(makeFilename(key)); + if (c == null) { + c = new Cache(sparql, content); + putIntoFile(makeFilename(key), c); + } else { + c.hm.put(sparql, content); + putIntoFile(makeFilename(key), c); + } + } String makeFilename(String key) { @@ -83,14 +116,6 @@ return false; } - boolean validate(String sparql) { - if (this.sparqlquery.equals(sparql)) - // valid - return true; - else - return false; - } - public void checkFile(String Filename) { if (!new File(Filename).exists()) { try { Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/ClassNode.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/ClassNode.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/ClassNode.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,3 +1,22 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; import java.net.URI; @@ -6,6 +25,7 @@ import java.util.Set; import java.util.Vector; +// is a node in the graph that is a class public class ClassNode extends Node { Set<PropertyNode> properties = new HashSet<PropertyNode>(); @@ -29,6 +49,17 @@ ClassNode tmp = new ClassNode(new URI(t.b)); properties.add(new PropertyNode(new URI(m.subclass), this, tmp)); Nodes.add(tmp); + } else { + ClassNode tmp = new ClassNode(new URI(t.b)); + properties.add(new PropertyNode(new URI(t.a), this, tmp)); + // System.out.println(m.blankNodeIdentifier); + // System.out.println("XXXXX"+t.b); + if (t.b.startsWith(m.blankNodeIdentifier)) { + tmp.expand(tsq, m); + System.out.println(m.blankNodeIdentifier); + System.out.println("XXXXX" + t.b); + } + // Nodes.add(tmp); } } catch (Exception e) { System.out.println(t); @@ -40,6 +71,12 @@ } @Override + public Vector<Node> expandProperties(TypedSparqlQuery tsq, Manipulator m) { + // TODO return type doesn't make sense + return new Vector<Node>(); + } + + @Override public Set<String> toNTriple() { Set<String> s = new HashSet<String>(); s.add("<" + this.uri + "><" + "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" + "><" Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/Configuration.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/Configuration.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/Configuration.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,227 +1,71 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; -import java.io.File; -import java.net.URI; -import java.net.URL; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; - -import org.semanticweb.owl.apibinding.OWLManager; -import org.semanticweb.owl.model.OWLConstant; -import org.semanticweb.owl.model.OWLDataPropertyExpression; -import org.semanticweb.owl.model.OWLIndividual; -import org.semanticweb.owl.model.OWLObjectPropertyExpression; -import org.semanticweb.owl.model.OWLOntology; -import org.semanticweb.owl.model.OWLOntologyManager; - +//stores all configuration settings public class Configuration { - private SpecificSparqlEndpoint SparqlEndpoint; - private SparqlQueryType SparqlQueryType; - private Manipulator Manipulator; + private SpecificSparqlEndpoint specificSparqlEndpoint; + private SparqlQueryType sparqlQueryType; + private Manipulator manipulator; private int recursiondepth = 2; private boolean getAllBackground = true; - + private Configuration() { } - - public Configuration(SpecificSparqlEndpoint SparqlEndpoint, - SparqlQueryType SparqlQueryType, int recursiondepth, + public Configuration(SpecificSparqlEndpoint specificSparqlEndpoint, + SparqlQueryType sparqlQueryType, Manipulator manipulator, int recursiondepth, boolean getAllBackground) { - this.SparqlEndpoint = SparqlEndpoint; - this.SparqlQueryType = SparqlQueryType; - this.Manipulator=new Manipulator(); + this.specificSparqlEndpoint = specificSparqlEndpoint; + this.sparqlQueryType = sparqlQueryType; + this.manipulator = manipulator; this.recursiondepth = recursiondepth; this.getAllBackground = getAllBackground; - } - public static Configuration getConfiguration(URI uri) { - // public static String getTellsString(URL file, URI kbURI){//throws - // OWLOntologyCreationException{ - Configuration ret = new Configuration(); - try { - String file = "config/config.owl"; + public Configuration changeQueryType(SparqlQueryType sqt) { + // TODO must clone here + return new Configuration(this.specificSparqlEndpoint, sqt, this.manipulator, + this.recursiondepth, this.getAllBackground); - File f = new File(file); - String fileURL = "file:///" + f.getAbsolutePath(); - URL u = new URL(fileURL); - /* Load an ontology from a physical URI */ - OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); - OWLOntology ontology = manager.loadOntologyFromPhysicalURI(u.toURI()); - // System.out.println( ontology.containsIndividualReference(uri)); - // OWLIndividualImpl ind=new OWLIndividualImpl(); - // System.out.println(ontology.getReferencedIndividuals()); - Set<OWLIndividual> s = ontology.getReferencedIndividuals(); - // System.out.println(ontology.getReferencedClasses()); - // Set<OWLIndividualAxiom> s= ontology.getIndividualAxioms(); - Iterator<OWLIndividual> it = s.iterator(); - while (it.hasNext()) { - OWLIndividual tmp = (OWLIndividual) it.next(); - // tmp.getURI() - if (tmp.getURI().equals(uri)) { - OWLIndividual[] arr = getIndividualsForProperty("hasSparqlEndpoint", tmp - .getObjectPropertyValues(ontology)); - OWLIndividual sEndpoint = arr[0]; - ret.SparqlEndpoint = makeEndpoint(sEndpoint, ontology); - arr = getIndividualsForProperty("hasTypedQuery", tmp - .getObjectPropertyValues(ontology)); - OWLIndividual typedQuery = arr[0]; - ret.SparqlQueryType = makeSparqlQueryType(typedQuery, ontology); - - } - // {hasSparqlEndpoint=[dbpediaEndpoint]} - } - - ret.Manipulator = makeManipulator(); - } catch (Exception e) { - e.printStackTrace(); - } - - return ret; } - public static OWLIndividual[] getIndividualsForProperty(String propertyname, - Map<OWLObjectPropertyExpression, Set<OWLIndividual>> m) { - Set<OWLObjectPropertyExpression> s = m.keySet(); - - Iterator<OWLObjectPropertyExpression> it = s.iterator(); - while (it.hasNext()) { - OWLObjectPropertyExpression tmp = (OWLObjectPropertyExpression) it.next(); - // System.out.println(tmp); - // System.out.println(propertyname); - if (tmp.toString().equals(propertyname)) { - Object[] arr = ((Set<OWLIndividual>) m.get(tmp)).toArray(); - OWLIndividual[] o = new OWLIndividual[arr.length]; - for (int i = 0; i < o.length; i++) { - o[i] = (OWLIndividual) arr[i]; - } - - return o; - } - } - return null; - - } - - public static String getFirstValueForDataProperty(String propertyname, - Map<OWLDataPropertyExpression, Set<OWLConstant>> m) { - return getValuesForDataProperty(propertyname, m)[0]; - } - - public static String[] getValuesForDataProperty(String propertyname, - Map<OWLDataPropertyExpression, Set<OWLConstant>> m) { - Set<OWLDataPropertyExpression> s = m.keySet(); - - Iterator<OWLDataPropertyExpression> it = s.iterator(); - while (it.hasNext()) { - OWLDataPropertyExpression tmp = (OWLDataPropertyExpression) it.next(); - if (tmp.toString().equals(propertyname)) { - Object[] arr = ((Set<OWLConstant>) m.get(tmp)).toArray(); - String[] str = new String[arr.length]; - for (int i = 0; i < str.length; i++) { - str[i] = ((OWLConstant) arr[i]).getLiteral(); - } - return str; - } - } - return null; - - } - - public static SpecificSparqlEndpoint makeEndpoint(OWLIndividual sEndpoint, OWLOntology o) { - String host = getFirstValueForDataProperty("hasHost", sEndpoint.getDataPropertyValues(o)); - String port = getFirstValueForDataProperty("hasPort", sEndpoint.getDataPropertyValues(o)); - String hasAfterGET = getFirstValueForDataProperty("hasAfterGET", sEndpoint - .getDataPropertyValues(o)); - String hasQueryParameter = getFirstValueForDataProperty("hasQueryParameter", sEndpoint - .getDataPropertyValues(o)); - OWLIndividual[] para = getIndividualsForProperty("hasGETParameter", sEndpoint - .getObjectPropertyValues(o)); - // System.out.println("test"); - HashMap<String, String> parameters = new HashMap<String, String>(); - if (para == null) - return null;//new SpecificSparqlEndpoint(host, port, hasAfterGET, hasQueryParameter, parameters); - for (OWLIndividual p : para) { - // System.out.println("test2"); - String a1 = getFirstValueForDataProperty("hasParameterName", p.getDataPropertyValues(o)); - String a2 = getFirstValueForDataProperty("hasParameterContent", p - .getDataPropertyValues(o)); - parameters.put(a1, a2); - } - // System.out.println("test2"); - // System.out.println(host+port+ hasAfterGET+ hasQueryParameter+ - // parameters); - return null;//new SpecificSparqlEndpoint(host, port, hasAfterGET, hasQueryParameter, parameters); - - } - - public static SparqlQueryType makeSparqlQueryType(OWLIndividual typedQuery, OWLOntology o) { - String useLiterals = getFirstValueForDataProperty("usesLiterals", typedQuery - .getDataPropertyValues(o)); - String hasMode = getFirstValueForDataProperty("hasMode", typedQuery - .getDataPropertyValues(o)); - // String - // hasAfterGET=getValuesForDataProperty("hasAfterGET",sEndpoint.getDataPropertyValues(o)); - // String - // hasQueryParameter=getValuesForDataProperty("hasQueryParameter",sEndpoint.getDataPropertyValues(o)); - OWLIndividual[] objFilter = getIndividualsForProperty("hasObjectFilterSet", typedQuery - .getObjectPropertyValues(o)); - OWLIndividual[] predFilter = getIndividualsForProperty("hasPredicateFilterSet", typedQuery - .getObjectPropertyValues(o)); - - Set<String> objectFilter = new HashSet<String>(); - Set<String> predicateFilter = new HashSet<String>(); - - for (OWLIndividual of : objFilter) { - String[] tmp = getValuesForDataProperty("filtersURI", of.getDataPropertyValues(o)); - for (String s : tmp) { - objectFilter.add(s); - - } - } - - for (OWLIndividual pf : predFilter) { - String[] tmp = getValuesForDataProperty("filtersURI", pf.getDataPropertyValues(o)); - for (String s : tmp) { - predicateFilter.add(s); - - } - } - // System.out.println(predicateFilter); - // System.out.println(hasMode+objectFilter+predicateFilter+useLiterals); - return new SparqlQueryType(hasMode, objectFilter, predicateFilter, useLiterals); - - } - - public static Manipulator makeManipulator() { - return new Manipulator(); - } - public Manipulator getManipulator() { - return this.Manipulator; + return this.manipulator; } public SpecificSparqlEndpoint getSparqlEndpoint() { - return SparqlEndpoint; + return specificSparqlEndpoint; } public SparqlQueryType getSparqlQueryType() { - return SparqlQueryType; + return sparqlQueryType; } - public boolean isGetAllBackground() { - return getAllBackground; + return getAllBackground; } - public int getRecursiondepth() { - return recursiondepth; + return recursiondepth; } } Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/ExtractionAlgorithm.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/ExtractionAlgorithm.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/ExtractionAlgorithm.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,3 +1,22 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; import java.net.URI; @@ -3,4 +22,5 @@ import java.util.Vector; +// this class is used to extract the information recursively public class ExtractionAlgorithm { @@ -13,8 +33,8 @@ public ExtractionAlgorithm(Configuration Configuration) { this.configuration = Configuration; this.manipulator = Configuration.getManipulator(); - this.recursionDepth=Configuration.getRecursiondepth(); - this.getAllBackground=Configuration.isGetAllBackground(); + this.recursionDepth = Configuration.getRecursiondepth(); + this.getAllBackground = Configuration.isGetAllBackground(); } @@ -43,13 +63,14 @@ while (v.size() > 0) { Node tmpNode = v.remove(0); System.out.println("Expanding " + tmpNode); - //System.out.println(this.Manipulator); + // System.out.println(this.Manipulator); Vector<Node> tmpVec = tmpNode.expand(tsp, manipulator); tmp.addAll(tmpVec); } v = tmp; - System.out.println("Recursion counter: " + x + " with " + v.size()+" Nodes remaining"); + System.out + .println("Recursion counter: " + x + " with " + v.size() + " Nodes remaining"); } if (this.getAllBackground) { Vector<Node> classes = new Vector<Node>(); @@ -59,9 +80,9 @@ } } while (classes.size() > 0) { - System.out.println("Remaining classes: "+classes.size()); - Node next=classes.remove(0); - System.out.println("Expanding: "+next); + System.out.println("Remaining classes: " + classes.size()); + Node next = classes.remove(0); + System.out.println("Expanding: " + next); classes.addAll(next.expand(tsp, manipulator)); } Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/InstanceNode.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/InstanceNode.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/InstanceNode.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,3 +1,22 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; import java.net.URI; @@ -6,6 +25,7 @@ import java.util.Set; import java.util.Vector; +// a node in the graph that is an instance public class InstanceNode extends Node { Set<ClassNode> classes = new HashSet<ClassNode>(); @@ -24,7 +44,7 @@ Set<Tupel> s = tsq.query(uri); // Manipulation m.check(s, this); - //System.out.println("fffffff"+m); + // System.out.println("fffffff"+m); Vector<Node> Nodes = new Vector<Node>(); Iterator<Tupel> it = s.iterator(); @@ -53,6 +73,14 @@ } @Override + public Vector<Node> expandProperties(TypedSparqlQuery tsq, Manipulator m) { + for (PropertyNode one : properties) { + one.expandProperties(tsq, m); + } + return new Vector<Node>(); + } + + @Override public Set<String> toNTriple() { Set<String> s = new HashSet<String>(); s.add("<" + uri + "><" + "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" + "><" Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/Manager.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/Manager.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/Manager.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,3 +1,22 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; import java.net.URI; @@ -4,21 +23,53 @@ import java.util.HashSet; import java.util.Set; +// an object of this class encapsulates everything public class Manager { private Configuration configuration; private TypedSparqlQuery typedSparqlQuery; private ExtractionAlgorithm extractionAlgorithm; - + public void useConfiguration(SparqlQueryType SparqlQueryType, + SpecificSparqlEndpoint SparqlEndpoint, Manipulator manipulator, int recursiondepth, + boolean getAllBackground) { - public void useConfiguration(SparqlQueryType SparqlQueryType, SpecificSparqlEndpoint SparqlEndpoint, int recursiondepth,boolean getAllBackground) { - - this.configuration = new Configuration(SparqlEndpoint, SparqlQueryType,recursiondepth,getAllBackground); + this.configuration = new Configuration(SparqlEndpoint, SparqlQueryType, manipulator, + recursiondepth, getAllBackground); this.typedSparqlQuery = new TypedSparqlQuery(configuration); this.extractionAlgorithm = new ExtractionAlgorithm(configuration); } + public Set<String> getDomainInstancesForRole(String role) { + URI u = null; + try { + u = new URI(role); + } catch (Exception e) { + e.printStackTrace(); + } + Set<Tupel> t = this.typedSparqlQuery.getTupelsForRole(u); + Set<String> ret = new HashSet<String>(); + for (Tupel one : t) { + ret.add(one.a); + } + return ret; + } + + public Set<String> getRangeInstancesForRole(String role) { + URI u = null; + try { + u = new URI(role); + } catch (Exception e) { + e.printStackTrace(); + } + Set<Tupel> t = this.typedSparqlQuery.getTupelsForRole(u); + Set<String> ret = new HashSet<String>(); + for (Tupel one : t) { + ret.add(one.b); + } + return ret; + } + public String extract(URI uri) { // this.TypedSparqlQuery.query(uri); // System.out.println(ExtractionAlgorithm.getFirstNode(uri)); @@ -54,4 +105,9 @@ return nt; } + public void addPredicateFilter(String str) { + this.configuration.getSparqlQueryType().addPredicateFilter(str); + + } + } \ No newline at end of file Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/Manipulator.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/Manipulator.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/Manipulator.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,3 +1,22 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; import java.util.HashSet; @@ -4,9 +23,11 @@ import java.util.Iterator; import java.util.Set; +// used to manipulate retrieved tupels, identify blanknodes, etc. public class Manipulator { public String subclass = "http://www.w3.org/2000/01/rdf-schema#subClassOf"; public String type = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"; + public String blankNodeIdentifier = "bnode"; String objectProperty = "http://www.w3.org/2002/07/owl#ObjectProperty"; String classns = "http://www.w3.org/2002/07/owl#Class"; @@ -16,13 +37,10 @@ String[] defaultClasses = { "http://dbpedia.org/class/yago", "http://dbpedia.org/resource/Category:", "http://dbpedia.org/resource/Template:", - "http://www.w3.org/2004/02/skos/core", "http://dbpedia.org/class/" }; // TODO - // FEHLER - // hier - // fehlt - // yago + "http://www.w3.org/2004/02/skos/core", "http://dbpedia.org/class/" }; - public Manipulator() { + public Manipulator(String blankNodeIdentifier) { + this.blankNodeIdentifier = blankNodeIdentifier; Set<String> classproperties = new HashSet<String>(); classproperties.add(subclass); @@ -33,21 +51,22 @@ Iterator<Tupel> it = s.iterator(); while (it.hasNext()) { Tupel t = (Tupel) it.next(); + // System.out.println(t); // all classes with owl:type class if (t.a.equals(type) && t.b.equals(classns) && node instanceof ClassNode) { toRemove.add(t); } - + // all with type class if (t.b.equals(classns) && node instanceof ClassNode) { toRemove.add(t); } - + // all instances with owl:type thing if (t.a.equals(type) && t.b.equals(thing) && node instanceof InstanceNode) { toRemove.add(t); } - + } s.removeAll(toRemove); Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/Node.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/Node.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/Node.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,3 +1,22 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; import java.net.URI; @@ -4,29 +23,20 @@ import java.util.Set; import java.util.Vector; +// abstract class public abstract class Node { URI uri; protected String type; protected boolean expanded = false; - // Hashtable<String,Node> classes=new Hashtable<String,Node>(); - // Hashtable<String,Node> instances=new Hashtable<String,Node>();; - // Hashtable<String,Node> datatype=new Hashtable<String,Node>();; - public Node(URI u) { this.uri = u; - } - /* - * public void checkConsistency(){ if (type.equals("class") && ( - * instances.size()>0 || datatype.size()>0)){ System.out.println("Warning, - * inconsistent:"+this.toString()); } - * } - */ - public abstract Vector<Node> expand(TypedSparqlQuery tsq, Manipulator m); + public abstract Vector<Node> expandProperties(TypedSparqlQuery tsq, Manipulator m); + public abstract Set<String> toNTriple(); @Override Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedEndpoint.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedEndpoint.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedEndpoint.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,3 +1,22 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; import java.net.URL; @@ -3,26 +22,41 @@ import java.util.HashMap; +// holds some predefined endpoints public class PredefinedEndpoint { - public static SpecificSparqlEndpoint getEndpoint(int i) { + public static SpecificSparqlEndpoint getEndpoint(int i) { - switch (i) { - case 1: - return dbpediaEndpoint(); + switch (i) { + case 1: + return dbpediaEndpoint(); + case 2: + return localJoseki(); + } + return null; + } + public static SpecificSparqlEndpoint dbpediaEndpoint() { + URL u = null; + HashMap<String, String> m = new HashMap<String, String>(); + m.put("default-graph-uri", "http://dbpedia.org"); + m.put("format", "application/sparql-results.xml"); + try { + u = new URL("http://dbpedia.openlinksw.com:8890/sparql"); + } catch (Exception e) { + e.printStackTrace(); + } + return new SpecificSparqlEndpoint(u, "dbpedia.openlinksw.com", m); } - return null; - } - - public static SpecificSparqlEndpoint dbpediaEndpoint(){ - URL u = null; - HashMap<String, String> m = new HashMap<String, String>(); - m.put("default-graph-uri", "http://dbpedia.org"); - m.put("format", "application/sparql-results.xml"); - try { - u = new URL("http://dbpedia.openlinksw.com:8890/sparql"); - } catch (Exception e) { - e.printStackTrace(); + + public static SpecificSparqlEndpoint localJoseki() { + URL u = null; + HashMap<String, String> m = new HashMap<String, String>(); + // m.put("default-graph-uri", "http://dbpedia.org"); + // m.put("format", "application/sparql-results.xml"); + try { + u = new URL("http://localhost:2020/books"); + } catch (Exception e) { + e.printStackTrace(); + } + return new SpecificSparqlEndpoint(u, "localost", m); } - return new SpecificSparqlEndpoint(u, "dbpedia.openlinksw.com", m); } -} Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedFilter.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedFilter.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedFilter.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,3 +1,22 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; import java.util.HashSet; @@ -4,14 +23,15 @@ import java.util.Set; public class PredefinedFilter { - + public static SparqlQueryType getFilter(int i) { switch (i) { case 1: return YagoFilter(); - + case 2: + return CategoriesOnly(); } return null; } @@ -22,6 +42,7 @@ pred.add("http://www.w3.org/2004/02/skos/core"); pred.add("http://www.w3.org/2002/07/owl#sameAs"); pred.add("http://xmlns.com/foaf/0.1/"); + pred.add("http://dbpedia.org/property/reference"); pred.add("http://dbpedia.org/property/website"); pred.add("http://dbpedia.org/property/wikipage"); @@ -39,4 +60,36 @@ return new SparqlQueryType("forbid", obj, pred, "false"); } + + public static SparqlQueryType CategoriesOnly(){ + Set<String> pred = new HashSet<String>(); + pred.add("http://www.w3.org/2004/02/skos/core"); + pred.add("http://www.w3.org/2002/07/owl#sameAs"); + pred.add("http://xmlns.com/foaf/0.1/"); + + pred.add("http://dbpedia.org/property/reference"); + pred.add("http://dbpedia.org/property/website"); + pred.add("http://dbpedia.org/property/wikipage"); + + Set<String> obj = new HashSet<String>(); + obj.add("http://dbpedia.org/resource/Category:Wikipedia_"); + obj.add("http://dbpedia.org/resource/Category:Articles_"); + obj.add("http://xmlns.com/foaf/0.1/"); + obj.add("http://upload.wikimedia.org/wikipedia/commons"); + obj.add("http://upload.wikimedia.org/wikipedia"); + + obj.add("http://www.geonames.org"); + obj.add("http://www.w3.org/2006/03/wn/wn20/instances/synset"); + obj.add("http://www4.wiwiss.fu-berlin.de/flickrwrappr"); + obj.add("http://www.w3.org/2004/02/skos/core"); + + obj.add("http://dbpedia.org/class/yago"); + obj.add("http://dbpedia.org/resource/Template"); + + + return new SparqlQueryType("forbid", obj, pred, "false"); + } + + + } Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/PropertyNode.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/PropertyNode.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/PropertyNode.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,3 +1,22 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; import java.net.URI; @@ -47,6 +66,12 @@ } return Nodes; } + @Override + public Vector<Node> expandProperties(TypedSparqlQuery tsq, Manipulator m) { + b.expandProperties(tsq, m); + return this.expand(tsq, m); + } + public Node getB() { return b; Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryMaker.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryMaker.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryMaker.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,5 +1,25 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; +// can assemble sparql queries public class SparqlQueryMaker { private SparqlQueryType sparqlQueryType; @@ -8,9 +28,27 @@ this.sparqlQueryType = SparqlQueryType; } - public String makeQueryUsingFilters(String subject) { + public String makeSubjectQueryUsingFilters(String subject) { String lineend = "\n"; + String Filter = internalFilterAssemblySubject(); + String ret = "SELECT * WHERE { " + lineend + "<" + subject + "> ?predicate ?object. " + + lineend + "FILTER( " + lineend + "(" + Filter + ").}"; + // System.out.println(ret); + return ret; + } + public String makeRoleQueryUsingFilters(String role) { + String lineend = "\n"; + String Filter = internalFilterAssemblyRole(); + String ret = "SELECT * WHERE { " + lineend + "?subject <" + role + "> ?object. " + lineend + + "FILTER( " + lineend + "(" + Filter + ").}"; + // System.out.println(ret); + + return ret; + } + + private String internalFilterAssemblySubject() { + String lineend = "\n"; String Filter = ""; if (!this.sparqlQueryType.isLiterals()) Filter += "!isLiteral(?object))"; @@ -20,18 +58,32 @@ for (String o : sparqlQueryType.getObjectfilterlist()) { Filter += lineend + filterObject(o); } + return Filter; + } - String ret = "SELECT * WHERE { " + lineend + "<" + subject + "> ?predicate ?object. " - + lineend + "FILTER( " + lineend + "(" + Filter + ").}"; - // System.out.println(ret); - return ret; + private String internalFilterAssemblyRole() { + String lineend = "\n"; + String Filter = ""; + if (!this.sparqlQueryType.isLiterals()) + Filter += "!isLiteral(?object))"; + for (String s : sparqlQueryType.getObjectfilterlist()) { + Filter += lineend + filterSubject(s); + } + for (String o : sparqlQueryType.getObjectfilterlist()) { + Filter += lineend + filterObject(o); + } + return Filter; } - public String filterObject(String ns) { - return "&&( !regex(str(?object), '" + ns + "') )"; + public String filterSubject(String ns) { + return "&&( !regex(str(?subject), '" + ns + "') )"; } public String filterPredicate(String ns) { return "&&( !regex(str(?predicate), '" + ns + "') )"; } + + public String filterObject(String ns) { + return "&&( !regex(str(?object), '" + ns + "') )"; + } } Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryType.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryType.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryType.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,3 +1,22 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; import java.util.Set; @@ -2,4 +21,5 @@ +// is used to set the filter: configuration public class SparqlQueryType { - + // TODO make sets out of them private String mode = "forbid"; @@ -58,4 +78,15 @@ return predicatefilterlist; } + public void addPredicateFilter(String filter) { + String[] tmp = new String[predicatefilterlist.length + 1]; + int i = 0; + for (; i < predicatefilterlist.length; i++) { + tmp[i] = predicatefilterlist[i]; + + } + tmp[i] = filter; + + } + } Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/SpecificSparqlEndpoint.java... [truncated message content] |