From: <lor...@us...> - 2011-08-10 13:47:48
|
Revision: 3023 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3023&view=rev Author: lorenz_b Date: 2011-08-10 13:47:39 +0000 (Wed, 10 Aug 2011) Log Message: ----------- Renamed algorithms. Added algorithm for inversefunctional object property axioms. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyDomainAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyRangeAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointDataPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentDataPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalDataPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyRangeAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubDataPropertyOfAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/core/config/ConfigHelper.java trunk/components-core/src/main/java/org/dllearner/core/owl/PropertyAxiomVisitor.java trunk/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java trunk/components-core/src/main/java/org/dllearner/utilities/owl/OWLAPIAxiomConvertVisitor.java Added Paths: ----------- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentObjectPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalObjectPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/InverseFunctionalObjectPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyDomainAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ReflexiveObjectPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubObjectPropertyOfAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SymmetricObjectPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/TransitiveObjectPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/core/owl/InverseFunctionalObjectPropertyAxiom.java Removed Paths: ------------- 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/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 Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyDomainAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyDomainAxiomLearner.java 2011-08-10 09:04:00 UTC (rev 3022) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyDomainAxiomLearner.java 2011-08-10 13:47:39 UTC (rev 3023) @@ -38,7 +38,7 @@ import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.ResultSet; -@ComponentAnn(name="property domain axiom learner") +@ComponentAnn(name="dataproperty domain axiom learner") public class DataPropertyDomainAxiomLearner extends AbstractComponent implements AxiomLearningAlgorithm { private static final Logger logger = LoggerFactory.getLogger(DataPropertyDomainAxiomLearner.class); Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyRangeAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyRangeAxiomLearner.java 2011-08-10 09:04:00 UTC (rev 3022) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyRangeAxiomLearner.java 2011-08-10 13:47:39 UTC (rev 3023) @@ -38,7 +38,7 @@ import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.ResultSet; -@ComponentAnn(name="property range learner") +@ComponentAnn(name="dataproperty range learner") public class DataPropertyRangeAxiomLearner extends AbstractComponent implements AxiomLearningAlgorithm { private static final Logger logger = LoggerFactory.getLogger(DataPropertyRangeAxiomLearner.class); Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointDataPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointDataPropertyAxiomLearner.java 2011-08-10 09:04:00 UTC (rev 3022) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointDataPropertyAxiomLearner.java 2011-08-10 13:47:39 UTC (rev 3023) @@ -35,10 +35,10 @@ import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.ResultSet; -@ComponentAnn(name="disjoint property axiom learner") +@ComponentAnn(name="disjoint dataproperty axiom learner") public class DisjointDataPropertyAxiomLearner extends AbstractComponent implements AxiomLearningAlgorithm { - private static final Logger logger = LoggerFactory.getLogger(PropertyDomainAxiomLearner.class); + private static final Logger logger = LoggerFactory.getLogger(ObjectPropertyDomainAxiomLearner.class); @ConfigOption(name="propertyToDescribe", description="", propertyEditorClass=DataPropertyEditor.class) private DatatypeProperty propertyToDescribe; 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-10 09:04:00 UTC (rev 3022) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointPropertyAxiomLearner.java 2011-08-10 13:47:39 UTC (rev 3023) @@ -35,10 +35,10 @@ import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.ResultSet; -@ComponentAnn(name="disjoint property axiom learner") +@ComponentAnn(name="disjoint objectproperty axiom learner") public class DisjointPropertyAxiomLearner extends AbstractComponent implements AxiomLearningAlgorithm { -private static final Logger logger = LoggerFactory.getLogger(PropertyDomainAxiomLearner.class); +private static final Logger logger = LoggerFactory.getLogger(ObjectPropertyDomainAxiomLearner.class); @ConfigOption(name="propertyToDescribe", description="", propertyEditorClass=ObjectPropertyEditor.class) private ObjectProperty propertyToDescribe; Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentDataPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentDataPropertyAxiomLearner.java 2011-08-10 09:04:00 UTC (rev 3022) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentDataPropertyAxiomLearner.java 2011-08-10 13:47:39 UTC (rev 3023) @@ -32,7 +32,7 @@ import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.ResultSet; -@ComponentAnn(name="equivalent property axiom learner") +@ComponentAnn(name="equivalent dataproperty axiom learner") public class EquivalentDataPropertyAxiomLearner extends AbstractComponent implements AxiomLearningAlgorithm { private static final Logger logger = LoggerFactory.getLogger(EquivalentDataPropertyAxiomLearner.class); Copied: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentObjectPropertyAxiomLearner.java (from rev 3022, trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentPropertyAxiomLearner.java) =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentObjectPropertyAxiomLearner.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentObjectPropertyAxiomLearner.java 2011-08-10 13:47:39 UTC (rev 3023) @@ -0,0 +1,227 @@ +package org.dllearner.algorithms.properties; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.SortedSet; +import java.util.TreeSet; + +import org.dllearner.core.AbstractComponent; +import org.dllearner.core.AxiomLearningAlgorithm; +import org.dllearner.core.ComponentAnn; +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.EquivalentObjectPropertiesAxiom; +import org.dllearner.core.owl.ObjectProperty; +import org.dllearner.kb.SparqlEndpointKS; +import org.dllearner.kb.sparql.ExtendedQueryEngineHTTP; +import org.dllearner.kb.sparql.SparqlQuery; +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; + +@ComponentAnn(name="equivalent objectproperty axiom learner") +public class EquivalentObjectPropertyAxiomLearner extends AbstractComponent implements AxiomLearningAlgorithm { + + private static final Logger logger = LoggerFactory.getLogger(EquivalentObjectPropertyAxiomLearner.class); + + @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 EquivalentObjectPropertyAxiomLearner(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(ObjectProperty propertyToDescribe) { + this.propertyToDescribe = propertyToDescribe; + } + + public int getMaxFetchedRows() { + return maxFetchedRows; + } + + public void setMaxFetchedRows(int maxFetchedRows) { + this.maxFetchedRows = maxFetchedRows; + } + + @Override + public void start() { + logger.info("Start learning..."); + startTime = System.currentTimeMillis(); + fetchedRows = 0; + currentlyBestAxioms = new ArrayList<EvaluatedAxiom>(); + //get existing super properties + SortedSet<ObjectProperty> existingSuperProperties = reasoner.getSuperProperties(propertyToDescribe); + logger.debug("Existing super properties: " + existingSuperProperties); + + //get subjects with types + int limit = 1000; + int offset = 0; + String queryTemplate = "SELECT ?p COUNT(?s) AS ?count WHERE {?s ?p ?o." + + "{SELECT ?s ?o WHERE {?s <%s> ?o.} LIMIT %d OFFSET %d}" + + "}"; + String query; + Map<ObjectProperty, Integer> result = new HashMap<ObjectProperty, Integer>(); + ObjectProperty prop; + Integer oldCnt; + boolean repeat = true; + + while(!terminationCriteriaSatisfied() && repeat){ + query = String.format(queryTemplate, propertyToDescribe, limit, offset); + ResultSet rs = executeQuery(query); + QuerySolution qs; + repeat = false; + while(rs.hasNext()){ + qs = rs.next(); + prop = new ObjectProperty(qs.getResource("p").getURI()); + int newCnt = qs.getLiteral("count").getInt(); + oldCnt = result.get(prop); + if(oldCnt == null){ + oldCnt = Integer.valueOf(newCnt); + } + result.put(prop, oldCnt); + qs.getLiteral("count").getInt(); + repeat = true; + } + if(!result.isEmpty()){ + currentlyBestAxioms = buildAxioms(result); + offset += 1000; + } + + } + + logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); + } + + @Override + public List<Axiom> getCurrentlyBestAxioms(int nrOfAxioms) { + List<Axiom> bestAxioms = new ArrayList<Axiom>(); + + Iterator<EvaluatedAxiom> it = currentlyBestAxioms.iterator(); + while(bestAxioms.size() < nrOfAxioms && it.hasNext()){ + 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); + + return bestAxioms; + } + + @Override + public Configurator getConfigurator() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void init() throws ComponentInitException { + reasoner = new SPARQLReasoner(ks); + + } + + private boolean terminationCriteriaSatisfied(){ + boolean timeLimitExceeded = maxExecutionTimeInSeconds == 0 ? false : (System.currentTimeMillis() - startTime) >= maxExecutionTimeInSeconds * 1000; + boolean resultLimitExceeded = maxFetchedRows == 0 ? false : fetchedRows >= maxFetchedRows; + return timeLimitExceeded || resultLimitExceeded; + } + + private List<EvaluatedAxiom> buildAxioms(Map<ObjectProperty, Integer> property2Count){ + List<EvaluatedAxiom> axioms = new ArrayList<EvaluatedAxiom>(); + Integer all = property2Count.get(propertyToDescribe); + property2Count.remove(propertyToDescribe); + + EvaluatedAxiom evalAxiom; + for(Entry<ObjectProperty, Integer> entry : sortByValues(property2Count)){ + evalAxiom = new EvaluatedAxiom(new EquivalentObjectPropertiesAxiom(propertyToDescribe, entry.getKey()), + new AxiomScore(entry.getValue() / (double)all)); + axioms.add(evalAxiom); + } + + property2Count.put(propertyToDescribe, all); + return axioms; + } + + /* + * Returns the entries of the map sorted by value. + */ + private SortedSet<Entry<ObjectProperty, Integer>> sortByValues(Map<ObjectProperty, Integer> map){ + SortedSet<Entry<ObjectProperty, Integer>> sortedSet = new TreeSet<Map.Entry<ObjectProperty,Integer>>(new Comparator<Entry<ObjectProperty, Integer>>() { + + @Override + public int compare(Entry<ObjectProperty, Integer> value1, Entry<ObjectProperty, Integer> value2) { + if(value1.getValue() < value2.getValue()){ + return 1; + } else if(value2.getValue() < value1.getValue()){ + return -1; + } else { + return value1.getKey().compareTo(value2.getKey()); + } + } + }); + sortedSet.addAll(map.entrySet()); + return sortedSet; + } + + /* + * Executes a SELECT query and returns the result. + */ + private ResultSet executeQuery(String query){ + logger.info("Sending query \n {}", query); + + ExtendedQueryEngineHTTP queryExecution = new ExtendedQueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + queryExecution.setTimeout(maxExecutionTimeInSeconds * 1000); + for (String dgu : ks.getEndpoint().getDefaultGraphURIs()) { + queryExecution.addDefaultGraph(dgu); + } + for (String ngu : ks.getEndpoint().getNamedGraphURIs()) { + queryExecution.addNamedGraph(ngu); + } + ResultSet resultSet = queryExecution.execSelect(); + return resultSet; + } + +} Deleted: 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-10 09:04:00 UTC (rev 3022) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentPropertyAxiomLearner.java 2011-08-10 13:47:39 UTC (rev 3023) @@ -1,227 +0,0 @@ -package org.dllearner.algorithms.properties; - -import java.util.ArrayList; -import java.util.Comparator; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.SortedSet; -import java.util.TreeSet; - -import org.dllearner.core.AbstractComponent; -import org.dllearner.core.AxiomLearningAlgorithm; -import org.dllearner.core.ComponentAnn; -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.EquivalentObjectPropertiesAxiom; -import org.dllearner.core.owl.ObjectProperty; -import org.dllearner.kb.SparqlEndpointKS; -import org.dllearner.kb.sparql.ExtendedQueryEngineHTTP; -import org.dllearner.kb.sparql.SparqlQuery; -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; - -@ComponentAnn(name="equivalent property axiom learner") -public class EquivalentPropertyAxiomLearner extends AbstractComponent implements AxiomLearningAlgorithm { - - private static final Logger logger = LoggerFactory.getLogger(EquivalentPropertyAxiomLearner.class); - - @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 EquivalentPropertyAxiomLearner(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(ObjectProperty propertyToDescribe) { - this.propertyToDescribe = propertyToDescribe; - } - - public int getMaxFetchedRows() { - return maxFetchedRows; - } - - public void setMaxFetchedRows(int maxFetchedRows) { - this.maxFetchedRows = maxFetchedRows; - } - - @Override - public void start() { - logger.info("Start learning..."); - startTime = System.currentTimeMillis(); - fetchedRows = 0; - currentlyBestAxioms = new ArrayList<EvaluatedAxiom>(); - //get existing super properties - SortedSet<ObjectProperty> existingSuperProperties = reasoner.getSuperProperties(propertyToDescribe); - logger.debug("Existing super properties: " + existingSuperProperties); - - //get subjects with types - int limit = 1000; - int offset = 0; - String queryTemplate = "SELECT ?p COUNT(?s) AS ?count WHERE {?s ?p ?o." + - "{SELECT ?s ?o WHERE {?s <%s> ?o.} LIMIT %d OFFSET %d}" + - "}"; - String query; - Map<ObjectProperty, Integer> result = new HashMap<ObjectProperty, Integer>(); - ObjectProperty prop; - Integer oldCnt; - boolean repeat = true; - - while(!terminationCriteriaSatisfied() && repeat){ - query = String.format(queryTemplate, propertyToDescribe, limit, offset); - ResultSet rs = executeQuery(query); - QuerySolution qs; - repeat = false; - while(rs.hasNext()){ - qs = rs.next(); - prop = new ObjectProperty(qs.getResource("p").getURI()); - int newCnt = qs.getLiteral("count").getInt(); - oldCnt = result.get(prop); - if(oldCnt == null){ - oldCnt = Integer.valueOf(newCnt); - } - result.put(prop, oldCnt); - qs.getLiteral("count").getInt(); - repeat = true; - } - if(!result.isEmpty()){ - currentlyBestAxioms = buildAxioms(result); - offset += 1000; - } - - } - - logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); - } - - @Override - public List<Axiom> getCurrentlyBestAxioms(int nrOfAxioms) { - List<Axiom> bestAxioms = new ArrayList<Axiom>(); - - Iterator<EvaluatedAxiom> it = currentlyBestAxioms.iterator(); - while(bestAxioms.size() < nrOfAxioms && it.hasNext()){ - 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); - - return bestAxioms; - } - - @Override - public Configurator getConfigurator() { - // TODO Auto-generated method stub - return null; - } - - @Override - public void init() throws ComponentInitException { - reasoner = new SPARQLReasoner(ks); - - } - - private boolean terminationCriteriaSatisfied(){ - boolean timeLimitExceeded = maxExecutionTimeInSeconds == 0 ? false : (System.currentTimeMillis() - startTime) >= maxExecutionTimeInSeconds * 1000; - boolean resultLimitExceeded = maxFetchedRows == 0 ? false : fetchedRows >= maxFetchedRows; - return timeLimitExceeded || resultLimitExceeded; - } - - private List<EvaluatedAxiom> buildAxioms(Map<ObjectProperty, Integer> property2Count){ - List<EvaluatedAxiom> axioms = new ArrayList<EvaluatedAxiom>(); - Integer all = property2Count.get(propertyToDescribe); - property2Count.remove(propertyToDescribe); - - EvaluatedAxiom evalAxiom; - for(Entry<ObjectProperty, Integer> entry : sortByValues(property2Count)){ - evalAxiom = new EvaluatedAxiom(new EquivalentObjectPropertiesAxiom(propertyToDescribe, entry.getKey()), - new AxiomScore(entry.getValue() / (double)all)); - axioms.add(evalAxiom); - } - - property2Count.put(propertyToDescribe, all); - return axioms; - } - - /* - * Returns the entries of the map sorted by value. - */ - private SortedSet<Entry<ObjectProperty, Integer>> sortByValues(Map<ObjectProperty, Integer> map){ - SortedSet<Entry<ObjectProperty, Integer>> sortedSet = new TreeSet<Map.Entry<ObjectProperty,Integer>>(new Comparator<Entry<ObjectProperty, Integer>>() { - - @Override - public int compare(Entry<ObjectProperty, Integer> value1, Entry<ObjectProperty, Integer> value2) { - if(value1.getValue() < value2.getValue()){ - return 1; - } else if(value2.getValue() < value1.getValue()){ - return -1; - } else { - return value1.getKey().compareTo(value2.getKey()); - } - } - }); - sortedSet.addAll(map.entrySet()); - return sortedSet; - } - - /* - * Executes a SELECT query and returns the result. - */ - private ResultSet executeQuery(String query){ - logger.info("Sending query \n {}", query); - - ExtendedQueryEngineHTTP queryExecution = new ExtendedQueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); - queryExecution.setTimeout(maxExecutionTimeInSeconds * 1000); - 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/FunctionalDataPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalDataPropertyAxiomLearner.java 2011-08-10 09:04:00 UTC (rev 3022) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalDataPropertyAxiomLearner.java 2011-08-10 13:47:39 UTC (rev 3023) @@ -28,7 +28,7 @@ import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP; import com.hp.hpl.jena.vocabulary.OWL; -@ComponentAnn(name="functional property axiom learner") +@ComponentAnn(name="functional dataproperty axiom learner") public class FunctionalDataPropertyAxiomLearner extends AbstractComponent implements AxiomLearningAlgorithm { private static final Logger logger = LoggerFactory.getLogger(FunctionalDataPropertyAxiomLearner.class); Copied: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalObjectPropertyAxiomLearner.java (from rev 3022, trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalPropertyAxiomLearner.java) =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalObjectPropertyAxiomLearner.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalObjectPropertyAxiomLearner.java 2011-08-10 13:47:39 UTC (rev 3023) @@ -0,0 +1,171 @@ +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.AbstractComponent; +import org.dllearner.core.ComponentAnn; +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.kb.sparql.ExtendedQueryEngineHTTP; +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; + +@ComponentAnn(name="functional objectproperty axiom learner") +public class FunctionalObjectPropertyAxiomLearner extends AbstractComponent implements AxiomLearningAlgorithm { + + private static final Logger logger = LoggerFactory.getLogger(FunctionalObjectPropertyAxiomLearner.class); + + @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 FunctionalObjectPropertyAxiomLearner(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(ObjectProperty propertyToDescribe) { + this.propertyToDescribe = propertyToDescribe; + } + + public int getMaxFetchedRows() { + return maxFetchedRows; + } + + public void setMaxFetchedRows(int maxFetchedRows) { + this.maxFetchedRows = maxFetchedRows; + } + + @Override + public void start() { + 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 number of instances of s with <s p o> + query = String.format("SELECT (COUNT(DISTINCT ?s)) AS ?all WHERE {?s <%s> ?o.}", propertyToDescribe.getName()); + ResultSet rs = executeQuery(query); + QuerySolution qs; + int all = 1; + while(rs.hasNext()){ + qs = rs.next(); + all = qs.getLiteral("all").getInt(); + } + //get number of instances of s with <s p o> <s p o1> where o != o1 + query = "SELECT (COUNT(DISTINCT ?s)) AS ?notfunctional WHERE {?s <%s> ?o. ?s <%s> ?o1. FILTER(?o != ?o1) }"; + query = query.replace("%s", propertyToDescribe.getURI().toString()); + rs = executeQuery(query); + int notFunctional = 1; + while(rs.hasNext()){ + qs = rs.next(); + notFunctional = qs.getLiteral("notfunctional").getInt(); + } + if(all > 0){ + double frac = (all - notFunctional) / (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) { + return currentlyBestAxioms.isEmpty() ? Collections.<Axiom>emptyList() : Collections.singletonList(currentlyBestAxioms.get(0).getAxiom()); + } + + @Override + public List<EvaluatedAxiom> getCurrentlyBestEvaluatedAxioms(int nrOfAxioms) { + return currentlyBestAxioms; + } + + @Override + public Configurator getConfigurator() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void init() throws ComponentInitException { + 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; + } + + /* + * Executes a SELECT query and returns the result. + */ + private ResultSet executeQuery(String query){ + logger.info("Sending query \n {}", query); + + ExtendedQueryEngineHTTP queryExecution = new ExtendedQueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + queryExecution.setTimeout(maxExecutionTimeInSeconds * 1000); + for (String dgu : ks.getEndpoint().getDefaultGraphURIs()) { + queryExecution.addDefaultGraph(dgu); + } + for (String ngu : ks.getEndpoint().getNamedGraphURIs()) { + queryExecution.addNamedGraph(ngu); + } + ResultSet resultSet = queryExecution.execSelect(); + return resultSet; + } +} Deleted: 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-10 09:04:00 UTC (rev 3022) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalPropertyAxiomLearner.java 2011-08-10 13:47:39 UTC (rev 3023) @@ -1,171 +0,0 @@ -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.AbstractComponent; -import org.dllearner.core.ComponentAnn; -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.kb.sparql.ExtendedQueryEngineHTTP; -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; - -@ComponentAnn(name="functional property axiom learner") -public class FunctionalPropertyAxiomLearner extends AbstractComponent implements AxiomLearningAlgorithm { - - private static final Logger logger = LoggerFactory.getLogger(FunctionalPropertyAxiomLearner.class); - - @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(ObjectProperty propertyToDescribe) { - this.propertyToDescribe = propertyToDescribe; - } - - public int getMaxFetchedRows() { - return maxFetchedRows; - } - - public void setMaxFetchedRows(int maxFetchedRows) { - this.maxFetchedRows = maxFetchedRows; - } - - @Override - public void start() { - 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 number of instances of s with <s p o> - query = String.format("SELECT (COUNT(DISTINCT ?s)) AS ?all WHERE {?s <%s> ?o.}", propertyToDescribe.getName()); - ResultSet rs = executeQuery(query); - QuerySolution qs; - int all = 1; - while(rs.hasNext()){ - qs = rs.next(); - all = qs.getLiteral("all").getInt(); - } - //get number of instances of s with <s p o> <s p o1> where o != o1 - query = "SELECT (COUNT(DISTINCT ?s)) AS ?notfunctional WHERE {?s <%s> ?o. ?s <%s> ?o1. FILTER(?o != ?o1) }"; - query = query.replace("%s", propertyToDescribe.getURI().toString()); - rs = executeQuery(query); - int notFunctional = 1; - while(rs.hasNext()){ - qs = rs.next(); - notFunctional = qs.getLiteral("notfunctional").getInt(); - } - if(all > 0){ - double frac = (all - notFunctional) / (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) { - return currentlyBestAxioms.isEmpty() ? Collections.<Axiom>emptyList() : Collections.singletonList(currentlyBestAxioms.get(0).getAxiom()); - } - - @Override - public List<EvaluatedAxiom> getCurrentlyBestEvaluatedAxioms(int nrOfAxioms) { - return currentlyBestAxioms; - } - - @Override - public Configurator getConfigurator() { - // TODO Auto-generated method stub - return null; - } - - @Override - public void init() throws ComponentInitException { - 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; - } - - /* - * Executes a SELECT query and returns the result. - */ - private ResultSet executeQuery(String query){ - logger.info("Sending query \n {}", query); - - ExtendedQueryEngineHTTP queryExecution = new ExtendedQueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); - queryExecution.setTimeout(maxExecutionTimeInSeconds * 1000); - for (String dgu : ks.getEndpoint().getDefaultGraphURIs()) { - queryExecution.addDefaultGraph(dgu); - } - for (String ngu : ks.getEndpoint().getNamedGraphURIs()) { - queryExecution.addNamedGraph(ngu); - } - ResultSet resultSet = queryExecution.execSelect(); - return resultSet; - } -} Added: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/InverseFunctionalObjectPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/InverseFunctionalObjectPropertyAxiomLearner.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/InverseFunctionalObjectPropertyAxiomLearner.java 2011-08-10 13:47:39 UTC (rev 3023) @@ -0,0 +1,171 @@ +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.AbstractComponent; +import org.dllearner.core.ComponentAnn; +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.kb.sparql.ExtendedQueryEngineHTTP; +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; + +@ComponentAnn(name="inversefunctional objectproperty axiom learner") +public class InverseFunctionalObjectPropertyAxiomLearner extends AbstractComponent implements AxiomLearningAlgorithm { + + private static final Logger logger = LoggerFactory.getLogger(InverseFunctionalObjectPropertyAxiomLearner.class); + + @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 InverseFunctionalObjectPropertyAxiomLearner(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(ObjectProperty propertyToDescribe) { + this.propertyToDescribe = propertyToDescribe; + } + + public int getMaxFetchedRows() { + return maxFetchedRows; + } + + public void setMaxFetchedRows(int maxFetchedRows) { + this.maxFetchedRows = maxFetchedRows; + } + + @Override + public void start() { + 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 number of instances of s with <s p o> + query = String.format("SELECT (COUNT(DISTINCT ?o) AS ?all) WHERE {?s <%s> ?o.}", propertyToDescribe.getName()); + ResultSet rs = executeQuery(query); + QuerySolution qs; + int all = 1; + while(rs.hasNext()){ + qs = rs.next(); + all = qs.getLiteral("all").getInt(); + } + //get number of instances of s with <s p o> <s p o1> where o != o1 + query = "SELECT (COUNT(DISTINCT ?s) AS ?noninversefunctional) WHERE {?s1 <%s> ?o. ?s2 <%s> ?o. FILTER(?s1 != ?s2) }"; + query = query.replace("%s", propertyToDescribe.getURI().toString()); + rs = executeQuery(query); + int notFunctional = 1; + while(rs.hasNext()){ + qs = rs.next(); + notFunctional = qs.getLiteral("noninversefunctional").getInt(); + } + if(all > 0){ + double frac = (all - notFunctional) / (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) { + return currentlyBestAxioms.isEmpty() ? Collections.<Axiom>emptyList() : Collections.singletonList(currentlyBestAxioms.get(0).getAxiom()); + } + + @Override + public List<EvaluatedAxiom> getCurrentlyBestEvaluatedAxioms(int nrOfAxioms) { + return currentlyBestAxioms; + } + + @Override + public Configurator getConfigurator() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void init() throws ComponentInitException { + 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; + } + + /* + * Executes a SELECT query and returns the result. + */ + private ResultSet executeQuery(String query){ + logger.info("Sending query \n {}", query); + + ExtendedQueryEngineHTTP queryExecution = new ExtendedQueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + queryExecution.setTimeout(maxExecutionTimeInSeconds * 1000); + for (String dgu : ks.getEndpoint().getDefaultGraphURIs()) { + queryExecution.addDefaultGraph(dgu); + } + for (String ngu : ks.getEndpoint().getNamedGraphURIs()) { + queryExecution.addNamedGraph(ngu); + } + ResultSet resultSet = queryExecution.execSelect(); + return resultSet; + } +} Copied: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyDomainAxiomLearner.java (from rev 3022, trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyDomainAxiomLearner.java) =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyDomainAxiomLearner.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyDomainAxiomLearner.java 2011-08-10 13:47:39 UTC (rev 3023) @@ -0,0 +1,238 @@ +package org.dllearner.algorithms.properties; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; + +import org.dllearner.core.AbstractComponent; +import org.dllearner.core.AxiomLearningAlgorithm; +import org.dllearner.core.ComponentAnn; +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.DatatypeProperty; +import org.dllearner.core.owl.Description; +import org.dllearner.core.owl.Individual; +import org.dllearner.core.owl.NamedClass; +import org.dllearner.core.owl.ObjectProperty; +import org.dllearner.core.owl.ObjectPropertyDomainAxiom; +import org.dllearner.kb.SparqlEndpointKS; +import org.dllearner.kb.sparql.ExtendedQueryEngineHTTP; +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; + +@ComponentAnn(name="objectproperty domain axiom learner") +public class ObjectPropertyDomainAxiomLearner extends AbstractComponent implements AxiomLearningAlgorithm { + + private static final Logger logger = LoggerFactory.getLogger(ObjectPropertyDomainAxiomLearner.class); + + @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 ObjectPropertyDomainAxiomLearner(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(ObjectProperty propertyToDescribe) { + this.propertyToDescribe = propertyToDescribe; + } + + public int getMaxFetchedRows() { + return maxFetchedRows; + } + + public void setMaxFetchedRows(int maxFetchedRows) { + this.maxFetchedRows = maxFetchedRows; + } + + @Override + public void start() { + logger.info("Start learning..."); + startTime = System.currentTimeMillis(); + fetchedRows = 0; + currentlyBestAxioms = new ArrayList<EvaluatedAxiom>(); + //get existing domains + Description existingDomain = reasoner.getDomain(propertyToDescribe); + logger.info("Existing domain: " + existingDomain); + + //get subjects with types + Map<Individual, Set<NamedClass>> individual2Types = new HashMap<Individual, Set<NamedClass>>(); + Map<Individual, Set<NamedClass>> newIndividual2Types; + boolean repeat = true; + while(!terminationCriteriaSatisfied() && repeat){ + newIndividual2Types = getSubjectsWithTypes(fetchedRows); + individual2Types.putAll(newIndividual2Types); + currentlyBestAxioms = buildBestAxioms(individual2Types); + fetchedRows += 1000; + repeat = !newIndividual2Types.isEmpty(); + } + logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); + } + + @Override + public List<Axiom> getCurrentlyBestAxioms(int nrOfAxioms) { + List<Axiom> bestAxioms = new ArrayList<Axiom>(); + + Iterator<EvaluatedAxiom> it = currentlyBestAxioms.iterator(); + while(bestAxioms.size() < nrOfAxioms && it.hasNext()){ + 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); + + return bestAxioms; + } + + @Override + public Configurator getConfigurator() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void init() throws ComponentInitException { + reasoner = new SPARQLReasoner(ks); + + } + + private boolean terminationCriteriaSatisfied(){ + boolean timeLimitExceeded = maxExecutionTimeInSeconds == 0 ? false : (System.currentTimeMillis() - startTime) >= maxExecutionTimeInSeconds * 1000; + boolean resultLimitExceeded = maxFetchedRows == 0 ? false : fetchedRows >= maxFetchedRows; + return timeLimitExceeded || resultLimitExceeded; + } + + 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()){ + Integer cnt = result.get(nc); + if(cnt == null){ + cnt = Integer.valueOf(1); + } else { + cnt = Integer.valueOf(cnt + 1); + } + result.put(nc, cnt); + } + } + + EvaluatedAxiom evalAxiom; + for(Entry<NamedClass, Integer> entry : sortByValues(result)){ + 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>>() { + + @Override + public int compare(Entry<NamedClass, Integer> value1, Entry<NamedClass, Integer> value2) { + if(value1.getValue() < value2.getValue()){ + return 1; + } else if(value2.getValue() < value1.getValue()){ + return -1; + } else { + return value1.getKey().compareTo(value2.getKey()); + } + } + }); + sortedSet.addAll(map.entrySet()); + return sortedSet; + } + + 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", propertyToDescribe.getURI().toString(), limit, offset); + ResultSet rs = executeQuery(query); + QuerySolution qs; + Individual ind; + Set<NamedClass> types; + while(rs.hasNext()){ + qs = rs.next(); + ind = new Individual(qs.getResource("ind").getURI()); + types = individual2Types.get(ind); + if(types == null){ + types = new HashSet<NamedClass>(); + individual2Types.put(ind, types); + } + types.add(new NamedClass(qs.getResource("type").getURI())); + } + return individual2Types; + } + + /* + * Executes a SELECT query and returns the result. + */ + private ResultSet executeQuery(String query){ + logger.info("Sending query \n {}", query); + + ExtendedQueryEngineHTTP queryExecution = new ExtendedQueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + queryExecution.setTimeout(maxExecutionTimeInSeconds * 1000); + for (String dgu : ks.getEndpoint().getDefaultGraphURIs()) { + queryExecution.addDefaultGraph(dgu); + } + for (String ngu : ks.getEndpoint().getNamedGraphURIs()) { + queryExecution.addNamedGraph(ngu); + } + ResultSet resultSet = queryExecution.execSelect(); + return resultSet; + } + + +} Deleted: 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-10 09:04:00 UTC (rev 3022) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyDomainAxiomLearner.java 2011-08-10 13:47:39 UTC (rev 3023) @@ -1,238 +0,0 @@ -package org.dllearner.algorithms.properties; - -import java.util.ArrayList; -import java.util.Comparator; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; - -import org.dllearner.core.AbstractComponent; -import org.dllearner.core.AxiomLearningAlgorithm; -import org.dllearner.core.ComponentAnn; -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.DatatypeProperty; -import org.dllearner.core.owl.Description; -import org.dllearner.core.owl.Individual; -import org.dllearner.core.owl.NamedClass; -import org.dllearner.core.owl.ObjectProperty; -import org.dllearner.core.owl.ObjectPropertyDomainAxiom; -import org.dllearner.kb.SparqlEndpointKS; -import org.dllearner.kb.sparql.ExtendedQueryEngineHTTP; -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; - -@ComponentAnn(name="property domain axiom learner") -public class PropertyDomainAxiomLearner extends AbstractComponent implements AxiomLearningAlgorithm { - - private static final Logger logger = LoggerFactory.getLogger(PropertyDomainAxiomLearner.class); - - @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 PropertyDomainAxiomLearner(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(ObjectProperty propertyToDescribe) { - this.propertyToDescribe = propertyToDescribe; - } - - public int getMaxFetchedRows() { - return maxFetchedRows; - } - - public void setMaxFetchedRows(int maxFetchedRows) { - this.maxFetchedRows = maxFetchedRows; - } - - @Override - public void start() { - logger.info("Start learning..."); - startTime = System.currentTimeMillis(); - fetchedRows = 0; - currentlyBestAxioms = new ArrayList<EvaluatedAxiom>(); - //get existing domains - Description existingDomain = reasoner.getDomain(propertyToDescribe); - logger.info("Existing domain: " + existingDomain); - - //get subjects with types - Map<Individual, Set<NamedClass>> individual2Types = new HashMap<Individual, Set<NamedClass>>(); - Map<Individual, Set<NamedClass>> newIndividual2Types; - boolean repeat = true; - while(!terminationCriteriaSatisfied() && repeat){ - newIndividual2Types = getSubjectsWithTypes(fetchedRows); - individual2Types.putAll(newIndividual2Types); - currentlyBestAxioms = buildBestAxioms(individual2Types); - fetchedRows += 1000; - repeat = !newIndividual2Types.isEmpty(); - } - logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); - } - - @Override - public List<Axiom> getCurrentlyBestAxioms(int nrOfAxioms) { - List<Axiom> bestAxioms = new ArrayList<Axiom>(); - - Iterator<EvaluatedAxiom> it = currentlyBestAxioms.iterator(); - while(bestAxioms.size() < nrOfAxioms && it.hasNext()){ - 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); - - return bestAxioms; - } - - @Override - public Configurator getConfigurator() { - // TODO Auto-generated method stub - return null; - } - - @Override - public void init() throws ComponentInitException { - ... [truncated message content] |