From: <jen...@us...> - 2011-08-11 11:31:01
|
Revision: 3029 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3029&view=rev Author: jenslehmann Date: 2011-08-11 11:30:49 +0000 (Thu, 11 Aug 2011) Log Message: ----------- - extended enrichment CLI - added class for standard prefixes Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/kb/sparql/SPARQLTasks.java trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java trunk/scripts/src/main/java/org/dllearner/scripts/evaluation/EnrichmentEvaluation.java Added Paths: ----------- trunk/components-core/src/main/java/org/dllearner/utilities/CommonPrefixMap.java Modified: trunk/components-core/src/main/java/org/dllearner/kb/sparql/SPARQLTasks.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/kb/sparql/SPARQLTasks.java 2011-08-11 11:23:56 UTC (rev 3028) +++ trunk/components-core/src/main/java/org/dllearner/kb/sparql/SPARQLTasks.java 2011-08-11 11:30:49 UTC (rev 3029) @@ -20,10 +20,15 @@ package org.dllearner.kb.sparql; import java.util.List; +import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; import org.apache.log4j.Logger; +import org.dllearner.core.owl.DatatypeProperty; +import org.dllearner.core.owl.Entity; +import org.dllearner.core.owl.NamedClass; +import org.dllearner.core.owl.ObjectProperty; import org.dllearner.utilities.datastructures.RDFNodeTuple; import org.dllearner.utilities.datastructures.StringTuple; import org.dllearner.utilities.owl.OWLVocabulary; @@ -597,6 +602,56 @@ return new SPARQLTasks( Cache.getDefaultCache(), SparqlEndpoint.getEndpointByName(endpointName) ); } + // tries to detect the type of the resource + public Entity guessResourceType(String resource) { + SortedSet<String> types = retrieveObjectsForSubjectAndRole(resource, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", 10000); + if(types.contains("http://www.w3.org/2002/07/owl#ObjectProperty")) { + return new ObjectProperty(resource); + } else if(types.contains("http://www.w3.org/2002/07/owl#DatatypeProperty")) { + return new DatatypeProperty(resource); + } else if(types.contains("http://www.w3.org/2002/07/owl#Class")) { + return new NamedClass(resource); + } else { + return null; + } + } + + public Set<ObjectProperty> getAllObjectProperties() { + Set<ObjectProperty> properties = new TreeSet<ObjectProperty>(); + String query = "PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT ?p WHERE {?p a owl:ObjectProperty}"; + SparqlQuery sq = new SparqlQuery(query, sparqlEndpoint); + ResultSet q = sq.send(); + while (q.hasNext()) { + QuerySolution qs = q.next(); + properties.add(new ObjectProperty(qs.getResource("p").getURI())); + } + return properties; + } + + public Set<DatatypeProperty> getAllDataProperties() { + Set<DatatypeProperty> properties = new TreeSet<DatatypeProperty>(); + String query = "PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT ?p WHERE {?p a owl:DatatypeProperty}"; + SparqlQuery sq = new SparqlQuery(query, sparqlEndpoint); + ResultSet q = sq.send(); + while (q.hasNext()) { + QuerySolution qs = q.next(); + properties.add(new DatatypeProperty(qs.getResource("p").getURI())); + } + return properties; + } + + public Set<NamedClass> getAllClasses() { + Set<NamedClass> classes = new TreeSet<NamedClass>(); + String query = "PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT ?c WHERE {?c a owl:Class}"; + SparqlQuery sq = new SparqlQuery(query, sparqlEndpoint); + ResultSet q = sq.send(); + while (q.hasNext()) { + QuerySolution qs = q.next(); + classes.add(new NamedClass(qs.getResource("c").getURI())); + } + return classes; + } + } /* Added: trunk/components-core/src/main/java/org/dllearner/utilities/CommonPrefixMap.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/utilities/CommonPrefixMap.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/utilities/CommonPrefixMap.java 2011-08-11 11:30:49 UTC (rev 3029) @@ -0,0 +1,39 @@ +/** + * Copyright (C) 2007-2011, Jens Lehmann + * + * 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.utilities; + +import java.util.HashMap; + +/** + * Can be used as base for a prefix map. + * + * @author Jens Lehmann + * + */ +public class CommonPrefixMap extends HashMap<String,String> { + + private static final long serialVersionUID = 5434065917532534702L; + + public CommonPrefixMap() { + put("dbp","http://dbpedia.org/property/"); + put("dbo","http://dbpedia.org/ontology/"); + } + +} Modified: trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java 2011-08-11 11:23:56 UTC (rev 3028) +++ trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java 2011-08-11 11:30:49 UTC (rev 3029) @@ -22,14 +22,53 @@ import static java.util.Arrays.asList; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.net.SocketTimeoutException; +import java.net.URI; import java.net.URL; import java.util.LinkedList; +import java.util.List; import joptsimple.OptionParser; import joptsimple.OptionSet; +import org.apache.log4j.ConsoleAppender; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.apache.log4j.SimpleLayout; +import org.dllearner.algorithms.DisjointClassesLearner; +import org.dllearner.algorithms.SimpleSubclassLearner; +import org.dllearner.algorithms.celoe.CELOE; +import org.dllearner.algorithms.properties.DataPropertyDomainAxiomLearner; +import org.dllearner.algorithms.properties.DataPropertyRangeAxiomLearner; +import org.dllearner.algorithms.properties.DisjointDataPropertyAxiomLearner; +import org.dllearner.algorithms.properties.DisjointObjectPropertyAxiomLearner; +import org.dllearner.algorithms.properties.EquivalentDataPropertyAxiomLearner; +import org.dllearner.algorithms.properties.EquivalentObjectPropertyAxiomLearner; +import org.dllearner.algorithms.properties.FunctionalDataPropertyAxiomLearner; +import org.dllearner.algorithms.properties.FunctionalObjectPropertyAxiomLearner; +import org.dllearner.algorithms.properties.InverseFunctionalObjectPropertyAxiomLearner; +import org.dllearner.algorithms.properties.ObjectPropertyDomainAxiomLearner; +import org.dllearner.algorithms.properties.ObjectPropertyRangeAxiomLearner; +import org.dllearner.algorithms.properties.SubDataPropertyOfAxiomLearner; +import org.dllearner.algorithms.properties.SubObjectPropertyOfAxiomLearner; +import org.dllearner.algorithms.properties.SymmetricObjectPropertyAxiomLearner; +import org.dllearner.algorithms.properties.TransitiveObjectPropertyAxiomLearner; +import org.dllearner.core.AxiomLearningAlgorithm; +import org.dllearner.core.ComponentInitException; +import org.dllearner.core.ComponentManager; +import org.dllearner.core.EvaluatedAxiom; +import org.dllearner.core.LearningAlgorithm; +import org.dllearner.core.config.ConfigHelper; +import org.dllearner.core.owl.DatatypeProperty; +import org.dllearner.core.owl.Entity; +import org.dllearner.core.owl.NamedClass; +import org.dllearner.core.owl.ObjectProperty; +import org.dllearner.kb.SparqlEndpointKS; +import org.dllearner.kb.sparql.SPARQLTasks; import org.dllearner.kb.sparql.SparqlEndpoint; import org.dllearner.kb.sparql.SparqlQuery; +import org.dllearner.utilities.CommonPrefixMap; import com.hp.hpl.jena.query.ResultSet; @@ -41,17 +80,158 @@ */ public class Enrichment { - public static void main(String[] args) throws IOException { + private static Logger logger = Logger.getLogger(Enrichment.class); + + // enrichment parameters + private SparqlEndpoint se; + private Entity resource; + private boolean verbose; + + // max. execution time for each learner for each entity + private int maxExecutionTimeInSeconds = 10; + + // number of axioms which will be learned/considered (only applies to + // some learners) + private int nrOfAxiomsToLearn = 10; + + // lists of algorithms to apply + private List<Class<? extends AxiomLearningAlgorithm>> objectPropertyAlgorithms; + private List<Class<? extends AxiomLearningAlgorithm>> dataPropertyAlgorithms; + private List<Class<? extends LearningAlgorithm>> classAlgorithms; + + private CommonPrefixMap prefixes = new CommonPrefixMap(); + + public Enrichment(SparqlEndpoint se, Entity resource, boolean verbose) { + this.se = se; + this.resource = resource; + this.verbose = verbose; + + objectPropertyAlgorithms = new LinkedList<Class<? extends AxiomLearningAlgorithm>>(); + objectPropertyAlgorithms.add(DisjointObjectPropertyAxiomLearner.class); + objectPropertyAlgorithms.add(EquivalentObjectPropertyAxiomLearner.class); + objectPropertyAlgorithms.add(FunctionalObjectPropertyAxiomLearner.class); + objectPropertyAlgorithms.add(InverseFunctionalObjectPropertyAxiomLearner.class); + objectPropertyAlgorithms.add(ObjectPropertyDomainAxiomLearner.class); + objectPropertyAlgorithms.add(ObjectPropertyRangeAxiomLearner.class); + objectPropertyAlgorithms.add(SubObjectPropertyOfAxiomLearner.class); + objectPropertyAlgorithms.add(SymmetricObjectPropertyAxiomLearner.class); + objectPropertyAlgorithms.add(TransitiveObjectPropertyAxiomLearner.class); + + dataPropertyAlgorithms = new LinkedList<Class<? extends AxiomLearningAlgorithm>>(); + dataPropertyAlgorithms.add(DisjointDataPropertyAxiomLearner.class); + dataPropertyAlgorithms.add(EquivalentDataPropertyAxiomLearner.class); + dataPropertyAlgorithms.add(FunctionalDataPropertyAxiomLearner.class); + dataPropertyAlgorithms.add(DataPropertyDomainAxiomLearner.class); + dataPropertyAlgorithms.add(DataPropertyRangeAxiomLearner.class); + dataPropertyAlgorithms.add(SubDataPropertyOfAxiomLearner.class); + + classAlgorithms = new LinkedList<Class<? extends LearningAlgorithm>>(); + classAlgorithms.add(DisjointClassesLearner.class); + classAlgorithms.add(SimpleSubclassLearner.class); + classAlgorithms.add(CELOE.class); + } + + public void start() throws ComponentInitException, IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { + + // sanity check that endpoint/graph returns at least one triple + String query = "SELECT * WHERE {?s ?p ?o} LIMIT 1"; + SparqlQuery sq = new SparqlQuery(query, se); + ResultSet q = sq.send(); + while (q.hasNext()) { + q.next(); + } + + // instantiate SPARQL endpoint wrapper component + SparqlEndpointKS ks = new SparqlEndpointKS(se); + ks.init(); + + if(resource == null) { + // TODO: automatically run over all resources if no specific resource was specified + SPARQLTasks st = new SPARQLTasks(se); + st.getAllClasses(); + st.getAllDataProperties(); + st.getAllObjectProperties(); + } else { + if(resource instanceof ObjectProperty) { + for (Class<? extends AxiomLearningAlgorithm> algorithmClass : objectPropertyAlgorithms) { + AxiomLearningAlgorithm learner = algorithmClass.getConstructor( + SparqlEndpointKS.class).newInstance(ks); + ConfigHelper.configure(learner, "propertyToDescribe", resource); + ConfigHelper.configure(learner, "maxExecutionTimeInSeconds", + maxExecutionTimeInSeconds); + learner.init(); + String algName = ComponentManager.getName(learner); + System.out.println("Applying " + algName + " on " + resource + " ... "); + long startTime = System.currentTimeMillis(); + try { + learner.start(); + } catch (Exception e) { + e.printStackTrace(); + if(e.getCause() instanceof SocketTimeoutException){ + System.out.println("Query timed out (endpoint possibly too slow)."); + } + } + long runtime = System.currentTimeMillis() - startTime; + System.out.println("runtime: " + runtime + "ms"); + List<EvaluatedAxiom> learnedAxioms = learner + .getCurrentlyBestEvaluatedAxioms(nrOfAxiomsToLearn); + for (EvaluatedAxiom learnedAxiom : learnedAxioms) { + System.out.println("suggested axiom: " + learnedAxiom); + } + } + } else if(resource instanceof DatatypeProperty) { + for (Class<? extends AxiomLearningAlgorithm> algorithmClass : dataPropertyAlgorithms) { + AxiomLearningAlgorithm learner = algorithmClass.getConstructor( + SparqlEndpointKS.class).newInstance(ks); + ConfigHelper.configure(learner, "propertyToDescribe", resource); + ConfigHelper.configure(learner, "maxExecutionTimeInSeconds", + maxExecutionTimeInSeconds); + learner.init(); + String algName = ComponentManager.getName(learner); + System.out.println("Applying " + algName + " on " + resource + " ... "); + long startTime = System.currentTimeMillis(); + try { + learner.start(); + } catch (Exception e) { + e.printStackTrace(); + if(e.getCause() instanceof SocketTimeoutException){ + System.out.println("Query timed out (endpoint possibly too slow)."); + } + } + long runtime = System.currentTimeMillis() - startTime; + System.out.println("runtime: " + runtime + "ms"); + List<EvaluatedAxiom> learnedAxioms = learner + .getCurrentlyBestEvaluatedAxioms(nrOfAxiomsToLearn); + for (EvaluatedAxiom learnedAxiom : learnedAxioms) { + System.out.println("suggested axiom: " + learnedAxiom); + } + } + } else if(resource instanceof NamedClass) { + throw new Error("not implemented"); + } else { + throw new Error("The type " + resource.getClass() + " of resource " + resource + " cannot be handled by this enrichment tool."); + } + } + } + + public static void main(String[] args) throws IOException, ComponentInitException, IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { + + SimpleLayout layout = new SimpleLayout(); + ConsoleAppender consoleAppender = new ConsoleAppender(layout); + logger.removeAllAppenders(); + logger.addAppender(consoleAppender); + logger.setLevel(Level.WARN); + OptionParser parser = new OptionParser(); parser.acceptsAll(asList("h", "?", "help"), "Show help."); - parser.acceptsAll(asList("v", "verbose"), "Verbosity level."); + parser.acceptsAll(asList("v", "verbose"), "Verbosity level.").withOptionalArg().ofType(Boolean.class).defaultsTo(false); parser.acceptsAll(asList("e", "endpoint"), "SPARQL endpoint URL to be used.") .withRequiredArg().ofType(URL.class); parser.acceptsAll(asList("g", "graph"), "URI of default graph for queries on SPARQL endpoint.").withOptionalArg() - .ofType(URL.class); + .ofType(URI.class); parser.acceptsAll(asList("r", "resource"), - "The resource for which enrichment axioms should be suggested.").withOptionalArg(); + "The resource for which enrichment axioms should be suggested.").withOptionalArg().ofType(URI.class); parser.acceptsAll(asList("o", "output"), "Specify a file where the output can be written.") .withOptionalArg(); parser.acceptsAll(asList("f", "format"), @@ -78,29 +258,32 @@ System.out.println(); System.out.println(addHelp); // main script - } else { + } else { // create SPARQL endpoint object URL endpoint = (URL) options.valueOf("endpoint"); - URL graph = (URL) options.valueOf("graph"); + URI graph = (URI) options.valueOf("graph"); LinkedList<String> defaultGraphURIs = new LinkedList<String>(); - defaultGraphURIs.add(graph.toString()); - SparqlEndpoint se = new SparqlEndpoint(endpoint, defaultGraphURIs, null); + if(graph != null) { + defaultGraphURIs.add(graph.toString()); + } + SparqlEndpoint se = new SparqlEndpoint(endpoint, defaultGraphURIs, new LinkedList<String>()); - // sanity check that endpoint/graph returns at least one triple - String query = "SELECT * WHERE {?s ?p ?o} LIMIT 1"; - SparqlQuery sq = new SparqlQuery(query, se); - ResultSet q = sq.send(); - while (q.hasNext()) { - q.next(); + // map resource to correct type + Entity resource = null; + if(options.valueOf("resource") != null) { + resource = new SPARQLTasks(se).guessResourceType(((URI)options.valueOf("resource")).toString()); + if(resource == null) { + throw new IllegalArgumentException("Could not determine the type (class, object property or data property) of input resource " + options.valueOf("resource")); + } } - // run an algorithm using the resource as input + boolean verbose = (Boolean) options.valueOf("v"); + Enrichment e = new Enrichment(se, resource, verbose); + e.start(); + + // TODO: print output in correct format - // TODO: detect type of the resource - // TODO: run all possible algorithms - // TODO: automatically run over all resources if no specific resource was specified - } } Modified: trunk/scripts/src/main/java/org/dllearner/scripts/evaluation/EnrichmentEvaluation.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/evaluation/EnrichmentEvaluation.java 2011-08-11 11:23:56 UTC (rev 3028) +++ trunk/scripts/src/main/java/org/dllearner/scripts/evaluation/EnrichmentEvaluation.java 2011-08-11 11:30:49 UTC (rev 3029) @@ -36,7 +36,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import java.util.TreeSet; import java.util.prefs.Preferences; import org.apache.log4j.Logger; @@ -54,9 +53,9 @@ import org.dllearner.algorithms.properties.InverseFunctionalObjectPropertyAxiomLearner; import org.dllearner.algorithms.properties.IrreflexiveObjectPropertyAxiomLearner; import org.dllearner.algorithms.properties.ObjectPropertyDomainAxiomLearner; -import org.dllearner.algorithms.properties.ObjectPropertyRangeAxiomLearner; import org.dllearner.algorithms.properties.SubDataPropertyOfAxiomLearner; import org.dllearner.algorithms.properties.SubObjectPropertyOfAxiomLearner; +import org.dllearner.algorithms.properties.ObjectPropertyRangeAxiomLearner; import org.dllearner.algorithms.properties.SymmetricObjectPropertyAxiomLearner; import org.dllearner.algorithms.properties.TransitiveObjectPropertyAxiomLearner; import org.dllearner.core.AxiomLearningAlgorithm; @@ -66,18 +65,15 @@ import org.dllearner.core.LearningAlgorithm; import org.dllearner.core.config.ConfigHelper; import org.dllearner.core.owl.DatatypeProperty; -import org.dllearner.core.owl.NamedClass; import org.dllearner.core.owl.ObjectProperty; import org.dllearner.kb.SparqlEndpointKS; +import org.dllearner.kb.sparql.SPARQLTasks; import org.dllearner.kb.sparql.SparqlEndpoint; -import org.dllearner.kb.sparql.SparqlQuery; +import org.dllearner.utilities.CommonPrefixMap; import org.dllearner.utilities.Files; import org.ini4j.IniPreferences; import org.ini4j.InvalidFileFormatException; -import com.hp.hpl.jena.query.QuerySolution; -import com.hp.hpl.jena.query.ResultSet; - /** * Evaluation of enrichment algorithms on DBpedia (Live). * @@ -104,13 +100,12 @@ private List<Class<? extends LearningAlgorithm>> classAlgorithms; private String baseURI = "http://dbpedia.org/resource/"; - private Map<String,String> prefixes; + private Map<String,String> prefixes = new CommonPrefixMap(); private Connection conn; private PreparedStatement ps; public EnrichmentEvaluation() { - initDBConnection(); prefixes = new HashMap<String,String>(); prefixes.put("dbp","http://dbpedia.org/property/"); @@ -213,7 +208,7 @@ } private void evaluateObjectProperties(SparqlEndpointKS ks)throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, ComponentInitException{ - Set<ObjectProperty> properties = getAllObjectProperties(ks.getEndpoint()); + Set<ObjectProperty> properties = new SPARQLTasks(ks.getEndpoint()).getAllObjectProperties(); for (Class<? extends AxiomLearningAlgorithm> algorithmClass : objectPropertyAlgorithms) { int objectProperties = 0; @@ -265,7 +260,7 @@ } private void evaluateDataProperties(SparqlEndpointKS ks) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, ComponentInitException{ - Set<DatatypeProperty> properties = getAllDataProperties(ks.getEndpoint()); + Set<DatatypeProperty> properties = new SPARQLTasks(ks.getEndpoint()).getAllDataProperties(); for (Class<? extends AxiomLearningAlgorithm> algorithmClass : dataPropertyAlgorithms) { int dataProperties = 0; @@ -316,51 +311,6 @@ } } - private Set<ObjectProperty> getAllObjectProperties(SparqlEndpoint se) { - Set<ObjectProperty> properties = new TreeSet<ObjectProperty>(); - String query = "PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT ?p WHERE {?p a owl:ObjectProperty}"; - SparqlQuery sq = new SparqlQuery(query, se); - // Claus' API - // Sparqler x = new SparqlerHttp(se.getURL().toString()); - // SelectPaginated q = new SelectPaginated(x, , 1000); - ResultSet q = sq.send(); - while (q.hasNext()) { - QuerySolution qs = q.next(); - properties.add(new ObjectProperty(qs.getResource("p").getURI())); - } - return properties; - } - - private Set<DatatypeProperty> getAllDataProperties(SparqlEndpoint se) { - Set<DatatypeProperty> properties = new TreeSet<DatatypeProperty>(); - String query = "PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT ?p WHERE {?p a owl:DatatypeProperty}"; - SparqlQuery sq = new SparqlQuery(query, se); - // Claus' API - // Sparqler x = new SparqlerHttp(se.getURL().toString()); - // SelectPaginated q = new SelectPaginated(x, , 1000); - ResultSet q = sq.send(); - while (q.hasNext()) { - QuerySolution qs = q.next(); - properties.add(new DatatypeProperty(qs.getResource("p").getURI())); - } - return properties; - } - - private Set<NamedClass> getAllClasses(SparqlEndpoint se) { - Set<NamedClass> classes = new TreeSet<NamedClass>(); - String query = "PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT ?c WHERE {?c a owl:Class}"; - SparqlQuery sq = new SparqlQuery(query, se); - // Claus' API - // Sparqler x = new SparqlerHttp(se.getURL().toString()); - // SelectPaginated q = new SelectPaginated(x, , 1000); - ResultSet q = sq.send(); - while (q.hasNext()) { - QuerySolution qs = q.next(); - classes.add(new NamedClass(qs.getResource("c").getURI())); - } - return classes; - } - public void printResultsPlain() { } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |