From: <ku...@us...> - 2011-11-21 14:44:06
|
Revision: 3421 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3421&view=rev Author: kurzum Date: 2011-11-21 14:43:55 +0000 (Mon, 21 Nov 2011) Log Message: ----------- added sparql capabilities to owlfile Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/kb/OWLFile.java Added Paths: ----------- trunk/examples/sparql/new_approach.conf Modified: trunk/components-core/src/main/java/org/dllearner/kb/OWLFile.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/kb/OWLFile.java 2011-11-21 12:46:47 UTC (rev 3420) +++ trunk/components-core/src/main/java/org/dllearner/kb/OWLFile.java 2011-11-21 14:43:55 UTC (rev 3421) @@ -25,6 +25,7 @@ import java.net.URL; import java.util.Collection; import java.util.LinkedList; +import java.util.List; import org.apache.log4j.Logger; import org.dllearner.core.AbstractKnowledgeSource; @@ -32,131 +33,144 @@ import org.dllearner.core.ComponentInitException; import org.dllearner.core.OntologyFormat; import org.dllearner.core.OntologyFormatUnsupportedException; +import org.dllearner.core.config.ListStringEditor; import org.dllearner.core.options.ConfigEntry; import org.dllearner.core.options.ConfigOption; import org.dllearner.core.options.InvalidConfigOptionValueException; import org.dllearner.core.options.URLConfigOption; import org.dllearner.core.owl.KB; import org.dllearner.reasoning.OWLAPIDIGConverter; +import org.dllearner.utilities.URLencodeUTF8; /** * @author Jens Lehmann - * + * @author Sebastian Hellmann + * <p/> + * SH: I added SPARQL capabilities. Either URL is set directly or the basedir and filename is set or the URL and the SPARQL query is set */ @ComponentAnn(name = "OWL File", shortName = "owlfile", version = 0.9) public class OWLFile extends AbstractKnowledgeSource { - private static Logger logger = Logger.getLogger(OWLFile.class); - - // TODO: turn this into a config option - private URL url; + private static Logger logger = Logger.getLogger(OWLFile.class); + + // TODO: turn this into a config option + private URL url; private String fileName; - private String baseDir; -// private URL url; -// private OWLFileConfigurator configurator ; -// @Override -// public OWLFileConfigurator getConfigurator(){ -// return configurator; -// } + private String baseDir; - public static String getName() { - return "OWL file"; - } - - public OWLFile(){ + private String sparql = null; + private List<String> defaultGraphURIs = new LinkedList<String>(); + private List<String> namedGraphURIs = new LinkedList<String>(); - } - - public OWLFile(URL url) { - this.url = url; - } + public static String getName() { + return "OWL file"; + } - public OWLFile(String filename) { - try { - url = new File(filename).toURI().toURL(); - } catch (MalformedURLException e) { - e.printStackTrace(); - } - } - - public static Collection<ConfigOption<?>> createConfigOptions() { - Collection<ConfigOption<?>> options = new LinkedList<ConfigOption<?>>(); - URLConfigOption urlOption = new URLConfigOption("url", "URL pointing to the OWL file", null, true, true); - urlOption.setRefersToFile(true); - options.add(urlOption); - return options; - } + public OWLFile() { - /* - * @see org.dllearner.core.Component#applyConfigEntry(org.dllearner.core.ConfigEntry) - */ - @Override - public <T> void applyConfigEntry(ConfigEntry<T> entry) throws InvalidConfigOptionValueException { - - } + } - /* (non-Javadoc) - * @see org.dllearner.core.Component#init() - */ - @Override - public void init() throws ComponentInitException { - if(url == null) { + public OWLFile(URL url) { + this.url = url; + } + + public OWLFile(String filename) { + try { + url = new File(filename).toURI().toURL(); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + } + + public static Collection<ConfigOption<?>> createConfigOptions() { + Collection<ConfigOption<?>> options = new LinkedList<ConfigOption<?>>(); + URLConfigOption urlOption = new URLConfigOption("url", "URL pointing to the OWL file", null, true, true); + urlOption.setRefersToFile(true); + options.add(urlOption); + return options; + } + + /* + * @see org.dllearner.core.Component#applyConfigEntry(org.dllearner.core.ConfigEntry) + */ + @Override + public <T> void applyConfigEntry(ConfigEntry<T> entry) throws InvalidConfigOptionValueException { + + } + + /* (non-Javadoc) + * @see org.dllearner.core.Component#init() + */ + @Override + public void init() throws ComponentInitException { + if (sparql != null) { + StringBuilder sb = new StringBuilder(); + + //make URL + sb.append(url.toString()); + sb.append("?query=").append(URLencodeUTF8.encode(sparql)); + sb.append("&format=application%2Frdf%2Bxml"); + + for (String graph : defaultGraphURIs) { + sb.append("&default-graph-uri=").append(URLencodeUTF8.encode(graph)); + } + for (String graph : namedGraphURIs) { + sb.append("&named-graph-uri=").append(URLencodeUTF8.encode(graph)); + } + logger.info(sb.toString()); + try { - url = new URL("file://"+ baseDir + "/" + fileName); + url = new URL(sb.toString()); } catch (MalformedURLException e) { throw new RuntimeException(e); } + + } else if (url == null) { + try { + url = new URL("file://" + baseDir + "/" + fileName); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } } - - /* - try { - url = new URL(configurator.getUrl()); - } catch (MalformedURLException e) { - logger.error(e.getMessage()); - //throw new InvalidConfigOptionValueException(entry.getOption(), entry.getValue(),"malformed URL " + configurator.getUrl()); - } - */ - - } - - /* - * (non-Javadoc) - * - * @see org.dllearner.core.KnowledgeSource#toDIG() - */ - @Override - public String toDIG(URI kbURI) { - // TODO: need some handling for cases where the URL was not set - return OWLAPIDIGConverter.getTellsString(url, OntologyFormat.RDF_XML, kbURI); - } + } - public URL getURL() { -// return configurator.getUrl(); - return url; - } - - public void setURL(URL url) { - this.url = url; -// configurator.setUrl(url); - } - /* (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 - throw new OntologyFormatUnsupportedException("export", format); - } + /* + * (non-Javadoc) + * + * @see org.dllearner.core.KnowledgeSource#toDIG() + */ + @Override + public String toDIG(URI kbURI) { + // TODO: need some handling for cases where the URL was not set + return OWLAPIDIGConverter.getTellsString(url, OntologyFormat.RDF_XML, kbURI); + } - /* (non-Javadoc) - * @see org.dllearner.core.KnowledgeSource#toKB() - */ - @Override - public KB toKB() { - throw new Error("OWL -> KB conversion not implemented yet."); - } + public URL getURL() { + return url; + } + public void setURL(URL url) { + this.url = url; + } + + /* (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 + throw new OntologyFormatUnsupportedException("export", format); + } + + /* (non-Javadoc) + * @see org.dllearner.core.KnowledgeSource#toKB() + */ + @Override + public KB toKB() { + throw new Error("OWL -> KB conversion not implemented yet."); + } + public String getBaseDir() { return baseDir; } @@ -165,13 +179,13 @@ this.baseDir = baseDir; } - public URL getUrl() { - return url; - } + public URL getUrl() { + return url; + } - public void setUrl(URL url) { - this.url = url; - } + public void setUrl(URL url) { + this.url = url; + } public String getFileName() { return fileName; @@ -180,4 +194,28 @@ public void setFileName(String fileName) { this.fileName = fileName; } + + public String getSparql() { + return sparql; + } + + public void setSparql(String sparql) { + this.sparql = sparql; + } + + public List<String> getDefaultGraphURIs() { + return defaultGraphURIs; + } + + public void setDefaultGraphURIs(List<String> defaultGraphURIs) { + this.defaultGraphURIs = defaultGraphURIs; + } + + public List<String> getNamedGraphURIs() { + return namedGraphURIs; + } + + public void setNamedGraphURIs(List<String> namedGraphURIs) { + this.namedGraphURIs = namedGraphURIs; + } } Added: trunk/examples/sparql/new_approach.conf =================================================================== --- trunk/examples/sparql/new_approach.conf (rev 0) +++ trunk/examples/sparql/new_approach.conf 2011-11-21 14:43:55 UTC (rev 3421) @@ -0,0 +1,40 @@ + + +// knowledge source definition +ks1.type = "OWL File" +ks1.url = "http://dbpedia.org/sparql" +ks1.defaultGraphURIs= {"http://dbpedia.org"} +ks1.sparql = " Construct { ?s1 ?p1 ?o1. ?o1 ?p2 ?o2 . ?p1 a owl:ObjectProperty . ?p2 a owl:ObjectProperty . ?s1 a owl:Thing . ?o1 a owl:Thing . ?o2 a owl:Thing . } { ?s1 ?p1 ?o1. OPTIONAL{ ?o1 ?p2 ?o2.} Filter (! (?p1 IN (rdf:type , <http://purl.org/dc/terms/subject>, owl:sameAs, <http://dbpedia.org/ontology/wikiPageExternalLink> ) )) . Filter (! (?p2 IN (rdf:type , <http://purl.org/dc/terms/subject>, owl:sameAs, <http://dbpedia.org/ontology/wikiPageExternalLink> ) )) . Filter (!isLiteral(?o1) && !isLiteral(?o2) ) . Filter ( ?s1 IN (<http://dbpedia.org/resource/London> , <http://dbpedia.org/resource/Berlin>)) } " + + +ks2.type = "OWL File" +ks2.url = "http://dbpedia.org/sparql" +ks2.defaultGraphURIs= {"http://dbpedia.org"} +ks2.sparql = "Construct { ?s1 a ?cl1 . ?s1 a owl:Thing . ?cl1 a owl:Class . ?o2 a ?cl2. ?o2 a owl:Thing . ?cl2 a owl:Class . } { ?s1 ?p1 ?o1 . OPTIONAL { ?o1 ?p2 ?o2. } Filter (! (?p1 IN (rdf:type , <http://purl.org/dc/terms/subject> ) )) . Filter (! (?p2 IN (rdf:type , <http://purl.org/dc/terms/subject> ) )) . Filter (!isLiteral(?o1) && !isLiteral(?o2) ) . ?s1 a ?cl1 . OPTIONAL {?o2 a ?cl2. } Filter ( ?cl1 LIKE <http://dbpedia.org/ontology/%> ) . Filter ( ?cl2 LIKE <http://dbpedia.org/ontology/%> ) . } " + +ks2.type = "OWL File" +ks2.url = "http://dbpedia.org/sparql" +ks2.defaultGraphURIs= {"http://dbpedia.org"} +ks2.sparql = "Construct { ?s1 a ?cl1 . ?s1 a owl:Thing . ?cl1 a owl:Class . ?o2 a ?cl2. ?o2 a owl:Thing . ?cl2 a owl:Class . } { ?s1 ?p1 ?o1 . OPTIONAL { ?o1 ?p2 ?o2. } Filter (! (?p1 IN (rdf:type , <http://purl.org/dc/terms/subject> ) )) . Filter (! (?p2 IN (rdf:type , <http://purl.org/dc/terms/subject> ) )) . Filter (!isLiteral(?o1) && !isLiteral(?o2) ) . ?s1 a ?cl1 . OPTIONAL {?o2 a ?cl2. } Filter ( ?cl1 LIKE <http://dbpedia.org/ontology/%> ) . Filter ( ?cl2 LIKE <http://dbpedia.org/ontology/%> ) . } " + + +reasoner.type = "fast instance checker" +reasoner.sources = {ks1, ks2} + +lp.type = "posNegStandard" +lp.positiveExamples = {"http://dbpedia.org/resource/Berlin"} +lp.negativeExamples = {"http://dbpedia.org/resource/London"} +lp.reasoner = reasoner + +// create a refinement operator and configure it +op.type = "rho" +op.useNegation = false +op.useAllConstructor = false +op.useCardinalityRestrictions = false +op.reasoner = reasoner + +// we use the OCEL algorithm +alg.type = "ocel" +alg.reasoner = reasoner +alg.learningProblem = lp +alg.operator = op This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |