From: <lor...@us...> - 2011-08-11 11:24:03
|
Revision: 3028 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3028&view=rev Author: lorenz_b Date: 2011-08-11 11:23:56 +0000 (Thu, 11 Aug 2011) Log Message: ----------- Renamed algorithm. Added algorithm for irreflexive property axioms. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/InverseFunctionalObjectPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ReflexiveObjectPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/core/ComponentManager.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 trunk/components-core/src/test/java/org/dllearner/test/junit/PropertyLearningTest.java trunk/scripts/src/main/java/org/dllearner/scripts/evaluation/EnrichmentEvaluation.java Added Paths: ----------- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/IrreflexiveObjectPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyRangeAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/core/owl/AsymmetricObjectPropertyAxiom.java trunk/components-core/src/main/java/org/dllearner/core/owl/IrreflexiveObjectPropertyAxiom.java Removed Paths: ------------- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyRangeAxiomLearner.java Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/InverseFunctionalObjectPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/InverseFunctionalObjectPropertyAxiomLearner.java 2011-08-11 07:04:39 UTC (rev 3027) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/InverseFunctionalObjectPropertyAxiomLearner.java 2011-08-11 11:23:56 UTC (rev 3028) @@ -4,8 +4,8 @@ import java.util.Collections; import java.util.List; +import org.dllearner.core.AbstractComponent; 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; @@ -14,7 +14,7 @@ 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.InverseFunctionalObjectPropertyAxiom; import org.dllearner.core.owl.ObjectProperty; import org.dllearner.kb.SparqlEndpointKS; import org.dllearner.kb.sparql.ExtendedQueryEngineHTTP; @@ -84,7 +84,7 @@ 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()); + String query = String.format("ASK {<%s> a <%s>}", propertyToDescribe, OWL.InverseFunctionalProperty.getURI()); boolean declaredAsFunctional = executeAskQuery(query); if(declaredAsFunctional) { logger.info("Property is already declared as functional in knowledge base."); @@ -110,7 +110,7 @@ } if(all > 0){ double frac = (all - notFunctional) / (double)all; - currentlyBestAxioms.add(new EvaluatedAxiom(new FunctionalObjectPropertyAxiom(propertyToDescribe), new AxiomScore(frac))); + currentlyBestAxioms.add(new EvaluatedAxiom(new InverseFunctionalObjectPropertyAxiom(propertyToDescribe), new AxiomScore(frac))); } logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); Added: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/IrreflexiveObjectPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/IrreflexiveObjectPropertyAxiomLearner.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/IrreflexiveObjectPropertyAxiomLearner.java 2011-08-11 11:23:56 UTC (rev 3028) @@ -0,0 +1,175 @@ +package org.dllearner.algorithms.properties; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +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.IrreflexiveObjectPropertyAxiom; +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.OWL2; + +@ComponentAnn(name="irreflexive objectproperty axiom learner") +public class IrreflexiveObjectPropertyAxiomLearner extends AbstractComponent implements AxiomLearningAlgorithm { + + private static final Logger logger = LoggerFactory.getLogger(IrreflexiveObjectPropertyAxiomLearner.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 IrreflexiveObjectPropertyAxiomLearner(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 reflexive in knowledge base + String query = String.format("ASK {<%s> a <%s>}", propertyToDescribe, OWL2.IrreflexiveProperty.getURI()); + boolean declaredAsReflexive = executeAskQuery(query); + if(declaredAsReflexive) { + logger.info("Property is already declared as irreflexive in knowledge base."); + } + + //get all instance s with <s p o> + query = String.format("SELECT (COUNT(DISTINCT ?s) AS ?all) WHERE {?s <%s> ?o.}", propertyToDescribe); + ResultSet rs = executeQuery(query); + QuerySolution qs; + int all = 0; + while(rs.hasNext()){ + qs = rs.next(); + all = qs.getLiteral("all").getInt(); + + } + + //get number of instances s where not exists <s p s> + query = "SELECT (COUNT(DISTINCT ?s) AS ?irreflexive) WHERE {?s <%s> ?o. OPTIONAL{?s <%s> ?o1.FILTER(?s = ?o1)} FILTER(!BOUND(?o1))}"; + query = query.replace("%s", propertyToDescribe.getURI().toString()); + rs = executeQuery(query); + int irreflexive = 0; + while(rs.hasNext()){ + qs = rs.next(); + irreflexive = qs.getLiteral("irreflexive").getInt(); + } + + if(all > 0){ + double frac = irreflexive / (double)all; + currentlyBestAxioms.add(new EvaluatedAxiom(new IrreflexiveObjectPropertyAxiom(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/ObjectPropertyRangeAxiomLearner.java (from rev 3023, trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyRangeAxiomLearner.java) =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyRangeAxiomLearner.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyRangeAxiomLearner.java 2011-08-11 11:23:56 UTC (rev 3028) @@ -0,0 +1,246 @@ +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.ObjectPropertyRangeAxiom; +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 range learner") +public class ObjectPropertyRangeAxiomLearner extends AbstractComponent implements AxiomLearningAlgorithm { + + private static final Logger logger = LoggerFactory.getLogger(ObjectPropertyRangeAxiomLearner.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 ObjectPropertyRangeAxiomLearner(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 range + Description existingRange = reasoner.getRange(propertyToDescribe); + logger.debug("Existing range: " + existingRange); + + //get objects with types + Map<Individual, Set<NamedClass>> individual2Types = new HashMap<Individual, Set<NamedClass>>(); + Map<Individual, Set<NamedClass>> newIndividual2Types; + boolean repeat = true; + while(!terminationCriteriaSatisfied() && repeat){ + newIndividual2Types = getObjectsWithTypes(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 ObjectPropertyRangeAxiom(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>> getObjectsWithTypes(int offset){ + Map<Individual, Set<NamedClass>> individual2Types = new HashMap<Individual, Set<NamedClass>>(); + int limit = 1000; + String query = String.format("SELECT DISTINCT ?ind ?type WHERE {?s <%s> ?ind. ?ind a ?type.} LIMIT %d OFFSET %d", propertyToDescribe.getName(), 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; + } + + public static void main(String[] args) throws Exception{ + ObjectPropertyRangeAxiomLearner l = new ObjectPropertyRangeAxiomLearner(new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpediaLiveAKSW())); + l.setPropertyToDescribe(new ObjectProperty("http://dbpedia.org/ontology/aircraftElectronic")); + l.setMaxExecutionTimeInSeconds(0); + l.init(); + l.start(); + System.out.println(l.getCurrentlyBestEvaluatedAxioms(5)); + } + +} Deleted: 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-11 07:04:39 UTC (rev 3027) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyRangeAxiomLearner.java 2011-08-11 11:23:56 UTC (rev 3028) @@ -1,246 +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.ObjectPropertyRangeAxiom; -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 range learner") -public class PropertyRangeAxiomLearner extends AbstractComponent implements AxiomLearningAlgorithm { - - private static final Logger logger = LoggerFactory.getLogger(PropertyRangeAxiomLearner.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 PropertyRangeAxiomLearner(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 range - Description existingRange = reasoner.getRange(propertyToDescribe); - logger.debug("Existing range: " + existingRange); - - //get objects with types - Map<Individual, Set<NamedClass>> individual2Types = new HashMap<Individual, Set<NamedClass>>(); - Map<Individual, Set<NamedClass>> newIndividual2Types; - boolean repeat = true; - while(!terminationCriteriaSatisfied() && repeat){ - newIndividual2Types = getObjectsWithTypes(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)){System.out.println(entry.getKey());System.out.println(entry.getValue()); - evalAxiom = new EvaluatedAxiom(new ObjectPropertyRangeAxiom(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>> getObjectsWithTypes(int offset){ - Map<Individual, Set<NamedClass>> individual2Types = new HashMap<Individual, Set<NamedClass>>(); - int limit = 1000; - String query = String.format("SELECT DISTINCT ?ind ?type WHERE {?s <%s> ?ind. ?ind a ?type.} LIMIT %d OFFSET %d", propertyToDescribe.getName(), 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; - } - - public static void main(String[] args) throws Exception{ - PropertyRangeAxiomLearner l = new PropertyRangeAxiomLearner(new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpediaLiveAKSW())); - l.setPropertyToDescribe(new ObjectProperty("http://dbpedia.org/ontology/aircraftElectronic")); - l.setMaxExecutionTimeInSeconds(0); - l.init(); - l.start(); - System.out.println(l.getCurrentlyBestEvaluatedAxioms(5)); - } - -} Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ReflexiveObjectPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ReflexiveObjectPropertyAxiomLearner.java 2011-08-11 07:04:39 UTC (rev 3027) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ReflexiveObjectPropertyAxiomLearner.java 2011-08-11 11:23:56 UTC (rev 3028) @@ -28,7 +28,7 @@ import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP; import com.hp.hpl.jena.vocabulary.OWL2; -@ComponentAnn(name="reflexive property axiom learner") +@ComponentAnn(name="reflexive objectproperty axiom learner") public class ReflexiveObjectPropertyAxiomLearner extends AbstractComponent implements AxiomLearningAlgorithm { private static final Logger logger = LoggerFactory.getLogger(ReflexiveObjectPropertyAxiomLearner.class); Modified: trunk/components-core/src/main/java/org/dllearner/core/ComponentManager.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/core/ComponentManager.java 2011-08-11 07:04:39 UTC (rev 3027) +++ trunk/components-core/src/main/java/org/dllearner/core/ComponentManager.java 2011-08-11 11:23:56 UTC (rev 3028) @@ -108,8 +108,7 @@ "org.dllearner.algorithms.celoe.CELOE", "org.dllearner.algorithms.fuzzydll.FuzzyCELOE", //added by Josue "org.dllearner.algorithms.isle.ISLE", - "org.dllearner.algorithm.qtl.QTL", - "org.dllearner.algorithms.properties.SubPropertyOfAxiomLearner" + "org.dllearner.algorithm.qtl.QTL" } )); private static ComponentManager cm = null; Added: trunk/components-core/src/main/java/org/dllearner/core/owl/AsymmetricObjectPropertyAxiom.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/core/owl/AsymmetricObjectPropertyAxiom.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/core/owl/AsymmetricObjectPropertyAxiom.java 2011-08-11 11:23:56 UTC (rev 3028) @@ -0,0 +1,49 @@ +package org.dllearner.core.owl; + +import java.util.Map; + +public class AsymmetricObjectPropertyAxiom extends PropertyAxiom { + + /** + * + */ + private static final long serialVersionUID = 8698680348695324368L; + private ObjectProperty role; + + public AsymmetricObjectPropertyAxiom(ObjectProperty role) { + this.role = role; + } + + public ObjectProperty getRole() { + return role; + } + + public int getLength() { + return 1 + role.getLength(); + } + + public String toString(String baseURI, Map<String,String> prefixes) { + return "Asymmetric(" + role.toString(baseURI, prefixes) + ")"; + } + + public String toKBSyntaxString(String baseURI, Map<String,String> prefixes) { + return "Asymmetric(" + 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 "Asymmetric(" + role.toString(baseURI, prefixes) + ")"; + } +} Added: trunk/components-core/src/main/java/org/dllearner/core/owl/IrreflexiveObjectPropertyAxiom.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/core/owl/IrreflexiveObjectPropertyAxiom.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/core/owl/IrreflexiveObjectPropertyAxiom.java 2011-08-11 11:23:56 UTC (rev 3028) @@ -0,0 +1,49 @@ +package org.dllearner.core.owl; + +import java.util.Map; + +public class IrreflexiveObjectPropertyAxiom extends PropertyAxiom { + + /** + * + */ + private static final long serialVersionUID = -3877477886974844568L; + private ObjectPropertyExpression role; + + public IrreflexiveObjectPropertyAxiom(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 "Irreflexive(" + role.toString(baseURI, prefixes) + ")"; + } + + public String toKBSyntaxString(String baseURI, Map<String,String> prefixes) { + return "Irreflexive(" + 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 "Irreflexive(" + role.toManchesterSyntaxString(baseURI, prefixes) + ")"; + } +} 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-11 07:04:39 UTC (rev 3027) +++ trunk/components-core/src/main/java/org/dllearner/core/owl/PropertyAxiomVisitor.java 2011-08-11 11:23:56 UTC (rev 3028) @@ -37,10 +37,14 @@ public void visit(SymmetricObjectPropertyAxiom axiom); + public void visit(AsymmetricObjectPropertyAxiom axiom); + public void visit(TransitiveObjectPropertyAxiom axiom); public void visit(ReflexiveObjectPropertyAxiom axiom); + public void visit(IrreflexiveObjectPropertyAxiom axiom); + public void visit(SubObjectPropertyAxiom axiom); public void visit(SubDatatypePropertyAxiom axiom); 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-11 07:04:39 UTC (rev 3027) +++ trunk/components-core/src/main/java/org/dllearner/utilities/owl/OWLAPIAxiomConvertVisitor.java 2011-08-11 11:23:56 UTC (rev 3028) @@ -24,6 +24,7 @@ import java.util.HashSet; import java.util.Set; +import org.dllearner.core.owl.AsymmetricObjectPropertyAxiom; import org.dllearner.core.owl.Axiom; import org.dllearner.core.owl.AxiomVisitor; import org.dllearner.core.owl.BooleanDatatypePropertyAssertion; @@ -46,6 +47,7 @@ import org.dllearner.core.owl.Individual; import org.dllearner.core.owl.InverseFunctionalObjectPropertyAxiom; import org.dllearner.core.owl.InverseObjectPropertyAxiom; +import org.dllearner.core.owl.IrreflexiveObjectPropertyAxiom; import org.dllearner.core.owl.KB; import org.dllearner.core.owl.ObjectPropertyAssertion; import org.dllearner.core.owl.ObjectPropertyDomainAxiom; @@ -447,6 +449,24 @@ } + @Override + public void visit(AsymmetricObjectPropertyAxiom axiom) { + OWLObjectProperty role = factory.getOWLObjectProperty( + IRI.create(axiom.getRole().getName())); + OWLAxiom axiomOWLAPI = factory.getOWLAsymmetricObjectPropertyAxiom(role); + addAxiom(axiomOWLAPI); + + } + + @Override + public void visit(IrreflexiveObjectPropertyAxiom axiom) { + OWLObjectProperty role = factory.getOWLObjectProperty( + IRI.create(axiom.getRole().getName())); + OWLAxiom axiomOWLAPI = factory.getOWLIrreflexiveObjectPropertyAxiom(role); + addAxiom(axiomOWLAPI); + + } + Modified: trunk/components-core/src/test/java/org/dllearner/test/junit/PropertyLearningTest.java =================================================================== --- trunk/components-core/src/test/java/org/dllearner/test/junit/PropertyLearningTest.java 2011-08-11 07:04:39 UTC (rev 3027) +++ trunk/components-core/src/test/java/org/dllearner/test/junit/PropertyLearningTest.java 2011-08-11 11:23:56 UTC (rev 3028) @@ -3,7 +3,7 @@ import org.dllearner.algorithms.properties.EquivalentObjectPropertyAxiomLearner; import org.dllearner.algorithms.properties.FunctionalObjectPropertyAxiomLearner; import org.dllearner.algorithms.properties.ObjectPropertyDomainAxiomLearner; -import org.dllearner.algorithms.properties.PropertyRangeAxiomLearner; +import org.dllearner.algorithms.properties.ObjectPropertyRangeAxiomLearner; import org.dllearner.algorithms.properties.ReflexiveObjectPropertyAxiomLearner; import org.dllearner.algorithms.properties.SubObjectPropertyOfAxiomLearner; import org.dllearner.algorithms.properties.SymmetricObjectPropertyAxiomLearner; @@ -62,7 +62,7 @@ } public void testPropertyRangeAxiomLearning() throws Exception { - PropertyRangeAxiomLearner l = new PropertyRangeAxiomLearner(ks); + ObjectPropertyRangeAxiomLearner l = new ObjectPropertyRangeAxiomLearner(ks); l.setMaxExecutionTimeInSeconds(maxExecutionTimeInSeconds); l.setPropertyToDescribe(range); l.init(); Modified: trunk/scripts/src/main/java/org/dllearner/scripts/evaluation/EnrichmentEvaluation.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/evaluation/EnrichmentEvaluation.java 2011-08-11 07:04:39 UTC (rev 3027) +++ trunk/scripts/src/main/java/org/dllearner/scripts/evaluation/EnrichmentEvaluation.java 2011-08-11 11:23:56 UTC (rev 3028) @@ -52,8 +52,9 @@ import org.dllearner.algorithms.properties.FunctionalDataPropertyAxiomLearner; import org.dllearner.algorithms.properties.FunctionalObjectPropertyAxiomLearner; import org.dllearner.algorithms.properties.InverseFunctionalObjectPropertyAxiomLearner; +import org.dllearner.algorithms.properties.IrreflexiveObjectPropertyAxiomLearner; import org.dllearner.algorithms.properties.ObjectPropertyDomainAxiomLearner; -import org.dllearner.algorithms.properties.PropertyRangeAxiomLearner; +import org.dllearner.algorithms.properties.ObjectPropertyRangeAxiomLearner; import org.dllearner.algorithms.properties.SubDataPropertyOfAxiomLearner; import org.dllearner.algorithms.properties.SubObjectPropertyOfAxiomLearner; import org.dllearner.algorithms.properties.SymmetricObjectPropertyAxiomLearner; @@ -121,10 +122,11 @@ objectPropertyAlgorithms.add(FunctionalObjectPropertyAxiomLearner.class); objectPropertyAlgorithms.add(InverseFunctionalObjectPropertyAxiomLearner.class); objectPropertyAlgorithms.add(ObjectPropertyDomainAxiomLearner.class); - objectPropertyAlgorithms.add(PropertyRangeAxiomLearner.class); + objectPropertyAlgorithms.add(ObjectPropertyRangeAxiomLearner.class); objectPropertyAlgorithms.add(SubObjectPropertyOfAxiomLearner.class); objectPropertyAlgorithms.add(SymmetricObjectPropertyAxiomLearner.class); objectPropertyAlgorithms.add(TransitiveObjectPropertyAxiomLearner.class); + objectPropertyAlgorithms.add(IrreflexiveObjectPropertyAxiomLearner.class); dataPropertyAlgorithms = new LinkedList<Class<? extends AxiomLearningAlgorithm>>(); dataPropertyAlgorithms.add(DisjointDataPropertyAxiomLearner.class); @@ -204,7 +206,7 @@ evaluateObjectProperties(ks); - evaluateDataProperties(ks); +// evaluateDataProperties(ks); System.out.println("Overall runtime: " + (System.currentTimeMillis()-overallStartTime)/1000 + "s."); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |