From: <lor...@us...> - 2011-08-03 13:54:34
|
Revision: 2979 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=2979&view=rev Author: lorenz_b Date: 2011-08-03 13:54:26 +0000 (Wed, 03 Aug 2011) Log Message: ----------- Added additional axioms type. Continued property learning algorithms. Added convenience class to configure components by analysing the annotations. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyDomainAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyRangeAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ReflexivePropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubPropertyOfAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SymmetricPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/TransitivePropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/core/AxiomLearningAlgorithm.java trunk/components-core/src/main/java/org/dllearner/core/owl/PropertyAxiomVisitor.java trunk/components-core/src/main/java/org/dllearner/utilities/owl/OWLAPIAxiomConvertVisitor.java Added Paths: ----------- trunk/components-core/src/main/java/org/dllearner/core/EvaluatedAxiom.java trunk/components-core/src/main/java/org/dllearner/core/config/ConfigHelper.java trunk/components-core/src/main/java/org/dllearner/core/owl/ReflexiveObjectPropertyAxiom.java trunk/components-core/src/main/java/org/dllearner/learningproblems/AxiomScore.java Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointPropertyAxiomLearner.java 2011-08-03 13:00:19 UTC (rev 2978) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointPropertyAxiomLearner.java 2011-08-03 13:54:26 UTC (rev 2979) @@ -5,6 +5,7 @@ import org.dllearner.core.AxiomLearningAlgorithm; import org.dllearner.core.Component; import org.dllearner.core.ComponentInitException; +import org.dllearner.core.EvaluatedAxiom; import org.dllearner.core.configurators.Configurator; import org.dllearner.core.owl.Axiom; import org.dllearner.kb.SparqlEndpointKS; @@ -49,4 +50,10 @@ } + @Override + public List<EvaluatedAxiom> getCurrentlyBestEvaluatedAxioms(int nrOfAxioms) { + // TODO Auto-generated method stub + return null; + } + } Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentPropertyAxiomLearner.java 2011-08-03 13:00:19 UTC (rev 2978) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentPropertyAxiomLearner.java 2011-08-03 13:54:26 UTC (rev 2979) @@ -5,6 +5,7 @@ import org.dllearner.core.AxiomLearningAlgorithm; import org.dllearner.core.Component; import org.dllearner.core.ComponentInitException; +import org.dllearner.core.EvaluatedAxiom; import org.dllearner.core.configurators.Configurator; import org.dllearner.core.owl.Axiom; import org.dllearner.kb.SparqlEndpointKS; @@ -49,4 +50,10 @@ } + @Override + public List<EvaluatedAxiom> getCurrentlyBestEvaluatedAxioms(int nrOfAxioms) { + // TODO Auto-generated method stub + return null; + } + } Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalPropertyAxiomLearner.java 2011-08-03 13:00:19 UTC (rev 2978) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalPropertyAxiomLearner.java 2011-08-03 13:54:26 UTC (rev 2979) @@ -1,41 +1,117 @@ package org.dllearner.algorithms.properties; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; import org.dllearner.core.AxiomLearningAlgorithm; import org.dllearner.core.Component; import org.dllearner.core.ComponentInitException; +import org.dllearner.core.EvaluatedAxiom; +import org.dllearner.core.config.ConfigOption; +import org.dllearner.core.config.IntegerEditor; +import org.dllearner.core.config.ObjectPropertyEditor; import org.dllearner.core.configurators.Configurator; import org.dllearner.core.owl.Axiom; +import org.dllearner.core.owl.FunctionalObjectPropertyAxiom; +import org.dllearner.core.owl.ObjectProperty; import org.dllearner.kb.SparqlEndpointKS; +import org.dllearner.learningproblems.AxiomScore; +import org.dllearner.reasoning.SPARQLReasoner; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import com.hp.hpl.jena.query.QuerySolution; +import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP; +import com.hp.hpl.jena.vocabulary.OWL; + public class FunctionalPropertyAxiomLearner extends Component implements AxiomLearningAlgorithm { - private String propertyToDescribe; +private static final Logger logger = LoggerFactory.getLogger(TransitivePropertyAxiomLearner.class); - public String getPropertyToDescribe() { + @ConfigOption(name="propertyToDescribe", description="", propertyEditorClass=ObjectPropertyEditor.class) + private ObjectProperty propertyToDescribe; + @ConfigOption(name="maxExecutionTimeInSeconds", description="", propertyEditorClass=IntegerEditor.class) + private int maxExecutionTimeInSeconds = 10; + @ConfigOption(name="maxFetchedRows", description="The maximum number of rows fetched from the endpoint to approximate the result.", propertyEditorClass=IntegerEditor.class) + private int maxFetchedRows = 0; + + private SPARQLReasoner reasoner; + private SparqlEndpointKS ks; + + private List<EvaluatedAxiom> currentlyBestAxioms; + private long startTime; + private int fetchedRows; + + + public FunctionalPropertyAxiomLearner(SparqlEndpointKS ks){ + this.ks = ks; + } + + public int getMaxExecutionTimeInSeconds() { + return maxExecutionTimeInSeconds; + } + + public void setMaxExecutionTimeInSeconds(int maxExecutionTimeInSeconds) { + this.maxExecutionTimeInSeconds = maxExecutionTimeInSeconds; + } + + public ObjectProperty getPropertyToDescribe() { return propertyToDescribe; } - public void setPropertyToDescribe(String propertyToDescribe) { + public void setPropertyToDescribe(ObjectProperty propertyToDescribe) { this.propertyToDescribe = propertyToDescribe; } + + public int getMaxFetchedRows() { + return maxFetchedRows; + } - public FunctionalPropertyAxiomLearner(SparqlEndpointKS ks){ - + public void setMaxFetchedRows(int maxFetchedRows) { + this.maxFetchedRows = maxFetchedRows; } @Override public void start() { - // TODO Auto-generated method stub - + logger.info("Start learning..."); + startTime = System.currentTimeMillis(); + fetchedRows = 0; + currentlyBestAxioms = new ArrayList<EvaluatedAxiom>(); + + //check if property is already declared as symmetric in knowledge base + String query = String.format("ASK {<%s> a <%s>}", propertyToDescribe, OWL.FunctionalProperty.getURI()); + boolean declaredAsFunctional = executeAskQuery(query); + if(declaredAsFunctional) { + logger.info("Property is already declared as functional in knowledge base."); + } + + //get fraction of instances s with <s p o> also exists <o p s> + query = "SELECT (COUNT(?s)) AS ?all ,(COUNT(?o1)) AS ?functional WHERE {?s <%s> ?o. OPTIONAL{?o <%s> ?s. ?o <%s> ?o1}}"; + query = query.replace("%s", propertyToDescribe.getURI().toString()); + ResultSet rs = executeQuery(query); + QuerySolution qs; + while(rs.hasNext()){ + qs = rs.next(); + int all = qs.getLiteral("all").getInt(); + int symmetric = qs.getLiteral("functional").getInt(); + double frac = symmetric / (double)all; + currentlyBestAxioms.add(new EvaluatedAxiom(new FunctionalObjectPropertyAxiom(propertyToDescribe), new AxiomScore(frac))); + } + + logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); } @Override public List<Axiom> getCurrentlyBestAxioms(int nrOfAxioms) { - // TODO Auto-generated method stub - return null; + return Collections.singletonList(currentlyBestAxioms.get(0).getAxiom()); } + + @Override + public List<EvaluatedAxiom> getCurrentlyBestEvaluatedAxioms(int nrOfAxioms) { + return Collections.singletonList(currentlyBestAxioms.get(0)); + } @Override public Configurator getConfigurator() { @@ -45,8 +121,34 @@ @Override public void init() throws ComponentInitException { - // TODO Auto-generated method stub + reasoner = new SPARQLReasoner(ks); + } + + private boolean executeAskQuery(String query){ + logger.info("Sending query \n {}", query); + QueryEngineHTTP queryExecution = new QueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + for (String dgu : ks.getEndpoint().getDefaultGraphURIs()) { + queryExecution.addDefaultGraph(dgu); + } + for (String ngu : ks.getEndpoint().getNamedGraphURIs()) { + queryExecution.addNamedGraph(ngu); + } + boolean result = queryExecution.execAsk(); + return result; } - + + private ResultSet executeQuery(String query){ + logger.info("Sending query \n {}", query); + + QueryEngineHTTP queryExecution = new QueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + for (String dgu : ks.getEndpoint().getDefaultGraphURIs()) { + queryExecution.addDefaultGraph(dgu); + } + for (String ngu : ks.getEndpoint().getNamedGraphURIs()) { + queryExecution.addNamedGraph(ngu); + } + ResultSet resultSet = queryExecution.execSelect(); + return resultSet; + } } Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyDomainAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyDomainAxiomLearner.java 2011-08-03 13:00:19 UTC (rev 2978) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyDomainAxiomLearner.java 2011-08-03 13:54:26 UTC (rev 2979) @@ -17,6 +17,8 @@ import org.dllearner.core.AxiomLearningAlgorithm; import org.dllearner.core.Component; import org.dllearner.core.ComponentInitException; +import org.dllearner.core.EvaluatedAxiom; +import org.dllearner.core.config.ConfigHelper; import org.dllearner.core.config.ConfigOption; import org.dllearner.core.config.IntegerEditor; import org.dllearner.core.config.ObjectPropertyEditor; @@ -29,6 +31,7 @@ import org.dllearner.core.owl.ObjectPropertyDomainAxiom; import org.dllearner.kb.SparqlEndpointKS; import org.dllearner.kb.sparql.SparqlEndpoint; +import org.dllearner.learningproblems.AxiomScore; import org.dllearner.reasoning.SPARQLReasoner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -51,7 +54,7 @@ private SPARQLReasoner reasoner; private SparqlEndpointKS ks; - private List<Axiom> currentlyBestAxioms; + private List<EvaluatedAxiom> currentlyBestAxioms; private long startTime; private int fetchedRows; @@ -88,7 +91,7 @@ logger.info("Start learning..."); startTime = System.currentTimeMillis(); fetchedRows = 0; - currentlyBestAxioms = new ArrayList<Axiom>(); + currentlyBestAxioms = new ArrayList<EvaluatedAxiom>(); //get existing domains Description existingDomain = reasoner.getDomain(propertyToDescribe); logger.debug("Existing domain: " + existingDomain); @@ -107,13 +110,22 @@ public List<Axiom> getCurrentlyBestAxioms(int nrOfAxioms) { List<Axiom> bestAxioms = new ArrayList<Axiom>(); - Iterator<Axiom> it = currentlyBestAxioms.iterator(); + Iterator<EvaluatedAxiom> it = currentlyBestAxioms.iterator(); while(bestAxioms.size() < nrOfAxioms && it.hasNext()){ - bestAxioms.add(it.next()); + bestAxioms.add(it.next().getAxiom()); } return bestAxioms; } + + @Override + public List<EvaluatedAxiom> getCurrentlyBestEvaluatedAxioms(int nrOfAxioms) { + int max = Math.min(currentlyBestAxioms.size(), nrOfAxioms); + + List<EvaluatedAxiom> bestAxioms = currentlyBestAxioms.subList(0, max-1); + + return bestAxioms; + } @Override public Configurator getConfigurator() { @@ -133,8 +145,8 @@ return timeLimitExceeded || resultLimitExceeded; } - private List<Axiom> buildBestAxioms(Map<Individual, Set<NamedClass>> individual2Types){ - List<Axiom> axioms = new ArrayList<Axiom>(); + private List<EvaluatedAxiom> buildBestAxioms(Map<Individual, Set<NamedClass>> individual2Types){ + List<EvaluatedAxiom> axioms = new ArrayList<EvaluatedAxiom>(); Map<NamedClass, Integer> result = new HashMap<NamedClass, Integer>(); for(Entry<Individual, Set<NamedClass>> entry : individual2Types.entrySet()){ for(NamedClass nc : entry.getValue()){ @@ -146,13 +158,19 @@ } } + EvaluatedAxiom evalAxiom; for(Entry<NamedClass, Integer> entry : sortByValues(result)){ - axioms.add(new ObjectPropertyDomainAxiom(propertyToDescribe, entry.getKey())); + evalAxiom = new EvaluatedAxiom(new ObjectPropertyDomainAxiom(propertyToDescribe, entry.getKey()), + new AxiomScore(entry.getValue() / (double)individual2Types.keySet().size())); + axioms.add(evalAxiom); } return axioms; } + /* + * Returns the entries of the map sorted by value. + */ private SortedSet<Entry<NamedClass, Integer>> sortByValues(Map<NamedClass, Integer> map){ SortedSet<Entry<NamedClass, Integer>> sortedSet = new TreeSet<Map.Entry<NamedClass,Integer>>(new Comparator<Entry<NamedClass, Integer>>() { @@ -174,7 +192,7 @@ private Map<Individual, Set<NamedClass>> getSubjectsWithTypes(int offset){ Map<Individual, Set<NamedClass>> individual2Types = new HashMap<Individual, Set<NamedClass>>(); int limit = 1000; - String query = String.format("SELECT ?ind ?type WHERE {?ind %s ?o. ?ind a ?type.} LIMIT %d OFFSET %d", inAngleBrackets(propertyToDescribe.getURI().toString()), limit, offset); + String query = String.format("SELECT ?ind ?type WHERE {?ind <%s> ?o. ?ind a ?type.} LIMIT %d OFFSET %d", propertyToDescribe.getURI().toString(), limit, offset); ResultSet rs = executeQuery(query); QuerySolution qs; Individual ind; @@ -192,10 +210,9 @@ return individual2Types; } - private String inAngleBrackets(String s){ - return "<" + s + ">"; - } - + /* + * Executes a SELECT query and returns the result. + */ private ResultSet executeQuery(String query){ logger.info("Sending query \n {}", query); @@ -218,6 +235,7 @@ PropertyDomainAxiomLearner l = new PropertyDomainAxiomLearner(new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpedia())); + Field[] fields = l.getClass().getDeclaredFields(); for(Field f : fields){ ConfigOption option = f.getAnnotation(ConfigOption.class); @@ -228,10 +246,10 @@ f.set(l, editor.getValue()); } } - + ConfigHelper.configurate(l, "propertyToDescribe", "test"); l.init(); l.start(); - System.out.println(l.getCurrentlyBestAxioms(3)); + System.out.println(l.getCurrentlyBestEvaluatedAxioms(3)); } } Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyRangeAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyRangeAxiomLearner.java 2011-08-03 13:00:19 UTC (rev 2978) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyRangeAxiomLearner.java 2011-08-03 13:54:26 UTC (rev 2979) @@ -5,6 +5,7 @@ import org.dllearner.core.AxiomLearningAlgorithm; import org.dllearner.core.Component; import org.dllearner.core.ComponentInitException; +import org.dllearner.core.EvaluatedAxiom; import org.dllearner.core.configurators.Configurator; import org.dllearner.core.owl.Axiom; import org.dllearner.kb.SparqlEndpointKS; @@ -49,4 +50,10 @@ } + @Override + public List<EvaluatedAxiom> getCurrentlyBestEvaluatedAxioms(int nrOfAxioms) { + // TODO Auto-generated method stub + return null; + } + } Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ReflexivePropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ReflexivePropertyAxiomLearner.java 2011-08-03 13:00:19 UTC (rev 2978) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ReflexivePropertyAxiomLearner.java 2011-08-03 13:54:26 UTC (rev 2979) @@ -1,41 +1,117 @@ package org.dllearner.algorithms.properties; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; import org.dllearner.core.AxiomLearningAlgorithm; import org.dllearner.core.Component; import org.dllearner.core.ComponentInitException; +import org.dllearner.core.EvaluatedAxiom; +import org.dllearner.core.config.ConfigOption; +import org.dllearner.core.config.IntegerEditor; +import org.dllearner.core.config.ObjectPropertyEditor; import org.dllearner.core.configurators.Configurator; import org.dllearner.core.owl.Axiom; +import org.dllearner.core.owl.ObjectProperty; +import org.dllearner.core.owl.ReflexiveObjectPropertyAxiom; import org.dllearner.kb.SparqlEndpointKS; +import org.dllearner.learningproblems.AxiomScore; +import org.dllearner.reasoning.SPARQLReasoner; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import com.hp.hpl.jena.query.QuerySolution; +import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP; +import com.hp.hpl.jena.vocabulary.OWL2; + public class ReflexivePropertyAxiomLearner extends Component implements AxiomLearningAlgorithm { - private String propertyToDescribe; + private static final Logger logger = LoggerFactory.getLogger(TransitivePropertyAxiomLearner.class); - public String getPropertyToDescribe() { + @ConfigOption(name="propertyToDescribe", description="", propertyEditorClass=ObjectPropertyEditor.class) + private ObjectProperty propertyToDescribe; + @ConfigOption(name="maxExecutionTimeInSeconds", description="", propertyEditorClass=IntegerEditor.class) + private int maxExecutionTimeInSeconds = 10; + @ConfigOption(name="maxFetchedRows", description="The maximum number of rows fetched from the endpoint to approximate the result.", propertyEditorClass=IntegerEditor.class) + private int maxFetchedRows = 0; + + private SPARQLReasoner reasoner; + private SparqlEndpointKS ks; + + private List<EvaluatedAxiom> currentlyBestAxioms; + private long startTime; + private int fetchedRows; + + + public ReflexivePropertyAxiomLearner(SparqlEndpointKS ks){ + this.ks = ks; + } + + public int getMaxExecutionTimeInSeconds() { + return maxExecutionTimeInSeconds; + } + + public void setMaxExecutionTimeInSeconds(int maxExecutionTimeInSeconds) { + this.maxExecutionTimeInSeconds = maxExecutionTimeInSeconds; + } + + public ObjectProperty getPropertyToDescribe() { return propertyToDescribe; } - public void setPropertyToDescribe(String propertyToDescribe) { + public void setPropertyToDescribe(ObjectProperty propertyToDescribe) { this.propertyToDescribe = propertyToDescribe; } + + public int getMaxFetchedRows() { + return maxFetchedRows; + } - public ReflexivePropertyAxiomLearner(SparqlEndpointKS ks){ - + public void setMaxFetchedRows(int maxFetchedRows) { + this.maxFetchedRows = maxFetchedRows; } @Override public void start() { - // TODO Auto-generated method stub - + logger.info("Start learning..."); + startTime = System.currentTimeMillis(); + fetchedRows = 0; + currentlyBestAxioms = new ArrayList<EvaluatedAxiom>(); + + //check if property is already declared as reflexive in knowledge base + String query = String.format("ASK {<%s> a <%s>}", propertyToDescribe, OWL2.ReflexiveProperty.getURI()); + boolean declaredAsReflexive = executeAskQuery(query); + if(declaredAsReflexive) { + logger.info("Property is already declared as reflexive in knowledge base."); + } + + //get fraction of instances s with <s p o> also exists <o p s> + query = "SELECT (COUNT(?s)) AS ?all ,(COUNT(?o1)) AS ?reflexiv WHERE {?s <%s> ?o. OPTIONAL{?o <%s> ?s. ?o <%s> ?o1}}"; + query = query.replace("%s", propertyToDescribe.getURI().toString()); + ResultSet rs = executeQuery(query); + QuerySolution qs; + while(rs.hasNext()){ + qs = rs.next(); + int all = qs.getLiteral("all").getInt(); + int reflexive = qs.getLiteral("reflexiv").getInt(); + double frac = reflexive / (double)all; + currentlyBestAxioms.add(new EvaluatedAxiom(new ReflexiveObjectPropertyAxiom(propertyToDescribe), new AxiomScore(frac))); + } + + logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); } @Override public List<Axiom> getCurrentlyBestAxioms(int nrOfAxioms) { - // TODO Auto-generated method stub - return null; + return Collections.singletonList(currentlyBestAxioms.get(0).getAxiom()); } + + @Override + public List<EvaluatedAxiom> getCurrentlyBestEvaluatedAxioms(int nrOfAxioms) { + return Collections.singletonList(currentlyBestAxioms.get(0)); + } @Override public Configurator getConfigurator() { @@ -45,8 +121,35 @@ @Override public void init() throws ComponentInitException { - // TODO Auto-generated method stub + reasoner = new SPARQLReasoner(ks); + } + + private boolean executeAskQuery(String query){ + logger.info("Sending query \n {}", query); + QueryEngineHTTP queryExecution = new QueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + for (String dgu : ks.getEndpoint().getDefaultGraphURIs()) { + queryExecution.addDefaultGraph(dgu); + } + for (String ngu : ks.getEndpoint().getNamedGraphURIs()) { + queryExecution.addNamedGraph(ngu); + } + boolean result = queryExecution.execAsk(); + return result; } + + private ResultSet executeQuery(String query){ + logger.info("Sending query \n {}", query); + + QueryEngineHTTP queryExecution = new QueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + for (String dgu : ks.getEndpoint().getDefaultGraphURIs()) { + queryExecution.addDefaultGraph(dgu); + } + for (String ngu : ks.getEndpoint().getNamedGraphURIs()) { + queryExecution.addNamedGraph(ngu); + } + ResultSet resultSet = queryExecution.execSelect(); + return resultSet; + } } Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubPropertyOfAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubPropertyOfAxiomLearner.java 2011-08-03 13:00:19 UTC (rev 2978) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubPropertyOfAxiomLearner.java 2011-08-03 13:54:26 UTC (rev 2979) @@ -11,6 +11,7 @@ import org.dllearner.core.AxiomLearningAlgorithm; import org.dllearner.core.Component; import org.dllearner.core.ComponentInitException; +import org.dllearner.core.EvaluatedAxiom; import org.dllearner.core.config.ConfigOption; import org.dllearner.core.config.IntegerEditor; import org.dllearner.core.config.ObjectPropertyEditor; @@ -126,5 +127,11 @@ l.start(); } + + @Override + public List<EvaluatedAxiom> getCurrentlyBestEvaluatedAxioms(int nrOfAxioms) { + // TODO Auto-generated method stub + return null; + } } Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SymmetricPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SymmetricPropertyAxiomLearner.java 2011-08-03 13:00:19 UTC (rev 2978) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SymmetricPropertyAxiomLearner.java 2011-08-03 13:54:26 UTC (rev 2979) @@ -1,41 +1,118 @@ package org.dllearner.algorithms.properties; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; import org.dllearner.core.AxiomLearningAlgorithm; import org.dllearner.core.Component; import org.dllearner.core.ComponentInitException; +import org.dllearner.core.EvaluatedAxiom; +import org.dllearner.core.config.ConfigOption; +import org.dllearner.core.config.IntegerEditor; +import org.dllearner.core.config.ObjectPropertyEditor; import org.dllearner.core.configurators.Configurator; import org.dllearner.core.owl.Axiom; +import org.dllearner.core.owl.ObjectProperty; +import org.dllearner.core.owl.ReflexiveObjectPropertyAxiom; +import org.dllearner.core.owl.SymmetricObjectPropertyAxiom; import org.dllearner.kb.SparqlEndpointKS; +import org.dllearner.learningproblems.AxiomScore; +import org.dllearner.reasoning.SPARQLReasoner; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import com.hp.hpl.jena.query.QuerySolution; +import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP; +import com.hp.hpl.jena.vocabulary.OWL2; + public class SymmetricPropertyAxiomLearner extends Component implements AxiomLearningAlgorithm { - private String propertyToDescribe; + private static final Logger logger = LoggerFactory.getLogger(TransitivePropertyAxiomLearner.class); - public String getPropertyToDescribe() { + @ConfigOption(name="propertyToDescribe", description="", propertyEditorClass=ObjectPropertyEditor.class) + private ObjectProperty propertyToDescribe; + @ConfigOption(name="maxExecutionTimeInSeconds", description="", propertyEditorClass=IntegerEditor.class) + private int maxExecutionTimeInSeconds = 10; + @ConfigOption(name="maxFetchedRows", description="The maximum number of rows fetched from the endpoint to approximate the result.", propertyEditorClass=IntegerEditor.class) + private int maxFetchedRows = 0; + + private SPARQLReasoner reasoner; + private SparqlEndpointKS ks; + + private List<EvaluatedAxiom> currentlyBestAxioms; + private long startTime; + private int fetchedRows; + + + public SymmetricPropertyAxiomLearner(SparqlEndpointKS ks){ + this.ks = ks; + } + + public int getMaxExecutionTimeInSeconds() { + return maxExecutionTimeInSeconds; + } + + public void setMaxExecutionTimeInSeconds(int maxExecutionTimeInSeconds) { + this.maxExecutionTimeInSeconds = maxExecutionTimeInSeconds; + } + + public ObjectProperty getPropertyToDescribe() { return propertyToDescribe; } - public void setPropertyToDescribe(String propertyToDescribe) { + public void setPropertyToDescribe(ObjectProperty propertyToDescribe) { this.propertyToDescribe = propertyToDescribe; } + + public int getMaxFetchedRows() { + return maxFetchedRows; + } - public SymmetricPropertyAxiomLearner(SparqlEndpointKS ks){ - + public void setMaxFetchedRows(int maxFetchedRows) { + this.maxFetchedRows = maxFetchedRows; } @Override public void start() { - // TODO Auto-generated method stub - + logger.info("Start learning..."); + startTime = System.currentTimeMillis(); + fetchedRows = 0; + currentlyBestAxioms = new ArrayList<EvaluatedAxiom>(); + + //check if property is already declared as symmetric in knowledge base + String query = String.format("ASK {<%s> a <%s>}", propertyToDescribe, OWL2.SymmetricProperty.getURI()); + boolean declaredAsSymmetric = executeAskQuery(query); + if(declaredAsSymmetric) { + logger.info("Property is already declared as symmetric in knowledge base."); + } + + //get fraction of instances s with <s p o> also exists <o p s> + query = "SELECT (COUNT(?s)) AS ?all ,(COUNT(?o1)) AS ?symmetric WHERE {?s <%s> ?o. OPTIONAL{?o <%s> ?s. ?o <%s> ?o1}}"; + query = query.replace("%s", propertyToDescribe.getURI().toString()); + ResultSet rs = executeQuery(query); + QuerySolution qs; + while(rs.hasNext()){ + qs = rs.next(); + int all = qs.getLiteral("all").getInt(); + int symmetric = qs.getLiteral("symmetric").getInt(); + double frac = symmetric / (double)all; + currentlyBestAxioms.add(new EvaluatedAxiom(new SymmetricObjectPropertyAxiom(propertyToDescribe), new AxiomScore(frac))); + } + + logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); } @Override public List<Axiom> getCurrentlyBestAxioms(int nrOfAxioms) { - // TODO Auto-generated method stub - return null; + return Collections.singletonList(currentlyBestAxioms.get(0).getAxiom()); } + + @Override + public List<EvaluatedAxiom> getCurrentlyBestEvaluatedAxioms(int nrOfAxioms) { + return Collections.singletonList(currentlyBestAxioms.get(0)); + } @Override public Configurator getConfigurator() { @@ -45,8 +122,35 @@ @Override public void init() throws ComponentInitException { - // TODO Auto-generated method stub + reasoner = new SPARQLReasoner(ks); + } + + private boolean executeAskQuery(String query){ + logger.info("Sending query \n {}", query); + QueryEngineHTTP queryExecution = new QueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + for (String dgu : ks.getEndpoint().getDefaultGraphURIs()) { + queryExecution.addDefaultGraph(dgu); + } + for (String ngu : ks.getEndpoint().getNamedGraphURIs()) { + queryExecution.addNamedGraph(ngu); + } + boolean result = queryExecution.execAsk(); + return result; } + + private ResultSet executeQuery(String query){ + logger.info("Sending query \n {}", query); + + QueryEngineHTTP queryExecution = new QueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + for (String dgu : ks.getEndpoint().getDefaultGraphURIs()) { + queryExecution.addDefaultGraph(dgu); + } + for (String ngu : ks.getEndpoint().getNamedGraphURIs()) { + queryExecution.addNamedGraph(ngu); + } + ResultSet resultSet = queryExecution.execSelect(); + return resultSet; + } } Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/TransitivePropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/TransitivePropertyAxiomLearner.java 2011-08-03 13:00:19 UTC (rev 2978) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/TransitivePropertyAxiomLearner.java 2011-08-03 13:54:26 UTC (rev 2979) @@ -1,41 +1,118 @@ package org.dllearner.algorithms.properties; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; import org.dllearner.core.AxiomLearningAlgorithm; import org.dllearner.core.Component; import org.dllearner.core.ComponentInitException; +import org.dllearner.core.EvaluatedAxiom; +import org.dllearner.core.config.ConfigOption; +import org.dllearner.core.config.IntegerEditor; +import org.dllearner.core.config.ObjectPropertyEditor; import org.dllearner.core.configurators.Configurator; import org.dllearner.core.owl.Axiom; +import org.dllearner.core.owl.ObjectProperty; +import org.dllearner.core.owl.TransitiveObjectPropertyAxiom; import org.dllearner.kb.SparqlEndpointKS; +import org.dllearner.kb.sparql.SparqlEndpoint; +import org.dllearner.learningproblems.AxiomScore; +import org.dllearner.reasoning.SPARQLReasoner; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import com.hp.hpl.jena.query.QuerySolution; +import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP; +import com.hp.hpl.jena.vocabulary.OWL; + public class TransitivePropertyAxiomLearner extends Component implements AxiomLearningAlgorithm { - private String propertyToDescribe; + private static final Logger logger = LoggerFactory.getLogger(TransitivePropertyAxiomLearner.class); - public String getPropertyToDescribe() { + @ConfigOption(name="propertyToDescribe", description="", propertyEditorClass=ObjectPropertyEditor.class) + private ObjectProperty propertyToDescribe; + @ConfigOption(name="maxExecutionTimeInSeconds", description="", propertyEditorClass=IntegerEditor.class) + private int maxExecutionTimeInSeconds = 10; + @ConfigOption(name="maxFetchedRows", description="The maximum number of rows fetched from the endpoint to approximate the result.", propertyEditorClass=IntegerEditor.class) + private int maxFetchedRows = 0; + + private SPARQLReasoner reasoner; + private SparqlEndpointKS ks; + + private List<EvaluatedAxiom> currentlyBestAxioms; + private long startTime; + private int fetchedRows; + + + public TransitivePropertyAxiomLearner(SparqlEndpointKS ks){ + this.ks = ks; + } + + public int getMaxExecutionTimeInSeconds() { + return maxExecutionTimeInSeconds; + } + + public void setMaxExecutionTimeInSeconds(int maxExecutionTimeInSeconds) { + this.maxExecutionTimeInSeconds = maxExecutionTimeInSeconds; + } + + public ObjectProperty getPropertyToDescribe() { return propertyToDescribe; } - public void setPropertyToDescribe(String propertyToDescribe) { + public void setPropertyToDescribe(ObjectProperty propertyToDescribe) { this.propertyToDescribe = propertyToDescribe; } + + public int getMaxFetchedRows() { + return maxFetchedRows; + } - public TransitivePropertyAxiomLearner(SparqlEndpointKS ks){ - + public void setMaxFetchedRows(int maxFetchedRows) { + this.maxFetchedRows = maxFetchedRows; } @Override public void start() { - //select COUNT(?o),COUNT(?o2) WHERE {?s dbpedia-owl:influencedBy ?o. ?o dbpedia-owl:influencedBy ?o1. OPTIONAL{?s dbpedia-owl:influencedBy ?o1. ?s dbpedia-owl:influencedBy ?o2}} - + logger.info("Start learning..."); + startTime = System.currentTimeMillis(); + fetchedRows = 0; + currentlyBestAxioms = new ArrayList<EvaluatedAxiom>(); + + //check if property is already declared as transitive in knowledge base + String query = String.format("ASK {<%s> a <%s>}", propertyToDescribe, OWL.TransitiveProperty.getURI()); + boolean declaredAsTransitive = executeAskQuery(query); + if(declaredAsTransitive) { + logger.info("Property is already declared as transitive in knowledge base."); + } + + //get fraction of instances s where for a chain <s p o> <o p o1> exists also <s p o1> + query = "SELECT (COUNT(?o)) AS ?all ,(COUNT(?o2)) AS ?transitive WHERE {?s <%s> ?o. ?o <%s> ?o1. OPTIONAL{?s <%s> ?o1. ?s <%s> ?o2}}"; + query = query.replace("%s", propertyToDescribe.getURI().toString()); + ResultSet rs = executeQuery(query); + QuerySolution qs; + while(rs.hasNext()){ + qs = rs.next(); + int all = qs.getLiteral("all").getInt(); + int transitive = qs.getLiteral("trans").getInt(); + double frac = transitive / (double)all; + currentlyBestAxioms.add(new EvaluatedAxiom(new TransitiveObjectPropertyAxiom(propertyToDescribe), new AxiomScore(frac))); + } + + logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); } @Override public List<Axiom> getCurrentlyBestAxioms(int nrOfAxioms) { - // TODO Auto-generated method stub - return null; + return Collections.singletonList(currentlyBestAxioms.get(0).getAxiom()); } + + @Override + public List<EvaluatedAxiom> getCurrentlyBestEvaluatedAxioms(int nrOfAxioms) { + return Collections.singletonList(currentlyBestAxioms.get(0)); + } @Override public Configurator getConfigurator() { @@ -45,8 +122,45 @@ @Override public void init() throws ComponentInitException { - // TODO Auto-generated method stub + reasoner = new SPARQLReasoner(ks); + } + + private boolean executeAskQuery(String query){ + logger.info("Sending query \n {}", query); + QueryEngineHTTP queryExecution = new QueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + for (String dgu : ks.getEndpoint().getDefaultGraphURIs()) { + queryExecution.addDefaultGraph(dgu); + } + for (String ngu : ks.getEndpoint().getNamedGraphURIs()) { + queryExecution.addNamedGraph(ngu); + } + boolean result = queryExecution.execAsk(); + return result; } + + private ResultSet executeQuery(String query){ + logger.info("Sending query \n {}", query); + + QueryEngineHTTP queryExecution = new QueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + for (String dgu : ks.getEndpoint().getDefaultGraphURIs()) { + queryExecution.addDefaultGraph(dgu); + } + for (String ngu : ks.getEndpoint().getNamedGraphURIs()) { + queryExecution.addNamedGraph(ngu); + } + ResultSet resultSet = queryExecution.execSelect(); + return resultSet; + } + + public static void main(String[] args) throws Exception{ + SparqlEndpointKS ks = new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpedia()); + TransitivePropertyAxiomLearner l = new TransitivePropertyAxiomLearner(ks); + l.setPropertyToDescribe(new ObjectProperty("http://dbpedia.org/ontology/influencedBy")); + l.init(); + l.start(); + System.out.println(l.getCurrentlyBestEvaluatedAxioms(1)); + } + } Modified: trunk/components-core/src/main/java/org/dllearner/core/AxiomLearningAlgorithm.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/core/AxiomLearningAlgorithm.java 2011-08-03 13:00:19 UTC (rev 2978) +++ trunk/components-core/src/main/java/org/dllearner/core/AxiomLearningAlgorithm.java 2011-08-03 13:54:26 UTC (rev 2979) @@ -8,8 +8,14 @@ /** * @param nrOfAxioms Limit for the number or returned axioms. - * @return The best axiom found by the learning algorithm so far. + * @return The best axioms found by the learning algorithm so far. */ public List<Axiom> getCurrentlyBestAxioms(int nrOfAxioms); + + /** + * @param nrOfAxioms Limit for the number or returned evaluated axioms. + * @return The best evaluated axioms found by the learning algorithm so far. + */ + public List<EvaluatedAxiom> getCurrentlyBestEvaluatedAxioms(int nrOfAxioms); } Added: trunk/components-core/src/main/java/org/dllearner/core/EvaluatedAxiom.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/core/EvaluatedAxiom.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/core/EvaluatedAxiom.java 2011-08-03 13:54:26 UTC (rev 2979) @@ -0,0 +1,30 @@ +package org.dllearner.core; + +import org.dllearner.core.owl.Axiom; + +public class EvaluatedAxiom { + + private Axiom axiom; + private Score score; + + public EvaluatedAxiom(Axiom axiom, Score score) { + this.axiom = axiom; + this.score = score; + } + + public Axiom getAxiom() { + return axiom; + } + + public Score getScore() { + return score; + } + + @Override + public String toString() { + return axiom + "(" + score.getAccuracy()+ ")"; + } + + + +} Added: trunk/components-core/src/main/java/org/dllearner/core/config/ConfigHelper.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/core/config/ConfigHelper.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/core/config/ConfigHelper.java 2011-08-03 13:54:26 UTC (rev 2979) @@ -0,0 +1,60 @@ +package org.dllearner.core.config; + +import java.beans.PropertyEditor; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; + +import org.dllearner.core.Component; + +public class ConfigHelper { + + /** + * Configures the given component by setting the value for the appropriate config option. + * @param component the component to be configured + * @param configName the name of the config option + * @param configValue the value of the config option + */ + public static void configurate(Component component, String configName, String configValue){ + Field[] fields = component.getClass().getDeclaredFields(); + for(Field f : fields){ + ConfigOption option = f.getAnnotation(ConfigOption.class); + if(option != null){ + if(option.name().equals(configName)){ + try { + PropertyEditor editor = (PropertyEditor) option.propertyEditorClass().newInstance(); + editor.setAsText(configValue); + f.set(component, editor.getValue()); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + + } + } + } + + /** + * Returns all config options for the given component. + * @param component + * @return + */ + public static List<ConfigOption> getConfigOptions(Component component){ + List<ConfigOption> options = new ArrayList<ConfigOption>(); + + Field[] fields = component.getClass().getDeclaredFields(); + for(Field f : fields){ + ConfigOption option = f.getAnnotation(ConfigOption.class); + if(option != null){ + options.add(option); + } + } + + return options; + } + +} Modified: trunk/components-core/src/main/java/org/dllearner/core/owl/PropertyAxiomVisitor.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/core/owl/PropertyAxiomVisitor.java 2011-08-03 13:00:19 UTC (rev 2978) +++ trunk/components-core/src/main/java/org/dllearner/core/owl/PropertyAxiomVisitor.java 2011-08-03 13:54:26 UTC (rev 2979) @@ -35,6 +35,8 @@ public void visit(TransitiveObjectPropertyAxiom axiom); + public void visit(ReflexiveObjectPropertyAxiom axiom); + public void visit(SubObjectPropertyAxiom axiom); void visit(DatatypePropertyDomainAxiom axiom); Added: trunk/components-core/src/main/java/org/dllearner/core/owl/ReflexiveObjectPropertyAxiom.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/core/owl/ReflexiveObjectPropertyAxiom.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/core/owl/ReflexiveObjectPropertyAxiom.java 2011-08-03 13:54:26 UTC (rev 2979) @@ -0,0 +1,49 @@ +package org.dllearner.core.owl; + +import java.util.Map; + +public class ReflexiveObjectPropertyAxiom extends PropertyAxiom { + + /** + * + */ + private static final long serialVersionUID = -3877477886974844568L; + private ObjectPropertyExpression role; + + public ReflexiveObjectPropertyAxiom(ObjectPropertyExpression role) { + this.role = role; + } + + public int getLength() { + return 1 + role.getLength(); + } + + public ObjectPropertyExpression getRole() { + return role; + } + + public String toString(String baseURI, Map<String,String> prefixes) { + return "Reflexive(" + role.toString(baseURI, prefixes) + ")"; + } + + public String toKBSyntaxString(String baseURI, Map<String,String> prefixes) { + return "Reflexive(" + role.toKBSyntaxString(baseURI, prefixes) + ")"; + } + + @Override + public void accept(AxiomVisitor visitor) { + visitor.visit(this); + } + + public void accept(KBElementVisitor visitor) { + visitor.visit(this); + } + + /* (non-Javadoc) + * @see org.dllearner.core.owl.KBElement#toManchesterSyntaxString(java.lang.String, java.util.Map) + */ + @Override + public String toManchesterSyntaxString(String baseURI, Map<String, String> prefixes) { + return "Transitive(" + role.toString(baseURI, prefixes) + ")"; + } +} Added: trunk/components-core/src/main/java/org/dllearner/learningproblems/AxiomScore.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/learningproblems/AxiomScore.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/learningproblems/AxiomScore.java 2011-08-03 13:54:26 UTC (rev 2979) @@ -0,0 +1,19 @@ +package org.dllearner.learningproblems; + +import org.dllearner.core.Score; + +public class AxiomScore extends Score{ + + private static final long serialVersionUID = 555252118489924570L; + private double accuracy; + + public AxiomScore(double accuracy) { + this.accuracy = accuracy; + } + + @Override + public double getAccuracy() { + return accuracy; + } + +} Modified: trunk/components-core/src/main/java/org/dllearner/utilities/owl/OWLAPIAxiomConvertVisitor.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/utilities/owl/OWLAPIAxiomConvertVisitor.java 2011-08-03 13:00:19 UTC (rev 2978) +++ trunk/components-core/src/main/java/org/dllearner/utilities/owl/OWLAPIAxiomConvertVisitor.java 2011-08-03 13:54:26 UTC (rev 2979) @@ -44,6 +44,7 @@ import org.dllearner.core.owl.ObjectPropertyAssertion; import org.dllearner.core.owl.ObjectPropertyDomainAxiom; import org.dllearner.core.owl.ObjectPropertyRangeAxiom; +import org.dllearner.core.owl.ReflexiveObjectPropertyAxiom; import org.dllearner.core.owl.StringDatatypePropertyAssertion; import org.dllearner.core.owl.SubClassAxiom; import org.dllearner.core.owl.SubObjectPropertyAxiom; @@ -235,6 +236,14 @@ OWLAxiom axiomOWLAPI = factory.getOWLTransitiveObjectPropertyAxiom(role); addAxiom(axiomOWLAPI); } + + @Override + public void visit(ReflexiveObjectPropertyAxiom axiom) { + OWLObjectProperty role = factory.getOWLObjectProperty( + IRI.create(axiom.getRole().getName())); + OWLAxiom axiomOWLAPI = factory.getOWLFunctionalObjectPropertyAxiom(role); + addAxiom(axiomOWLAPI); + } /* (non-Javadoc) * @see org.dllearner.core.owl.PropertyAxiomVisitor#visit(org.dllearner.core.owl.SubObjectPropertyAxiom) @@ -358,4 +367,6 @@ addAxiom(axiomOWLAPI); } + + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |