From: <lor...@us...> - 2012-05-08 11:53:40
|
Revision: 3697 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3697&view=rev Author: lorenz_b Date: 2012-05-08 11:53:28 +0000 (Tue, 08 May 2012) Log Message: ----------- Added REGEX filter option. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyDomainAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointDataPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyDomainAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyRangeAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/core/AbstractAxiomLearningAlgorithm.java trunk/scripts/src/main/java/org/dllearner/scripts/evaluation/EnrichmentEvaluation.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 2012-05-07 14:28:49 UTC (rev 3696) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyDomainAxiomLearner.java 2012-05-08 11:53:28 UTC (rev 3697) @@ -21,6 +21,7 @@ import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -28,6 +29,9 @@ import java.util.SortedSet; import java.util.TreeSet; +import org.apache.log4j.ConsoleAppender; +import org.apache.log4j.Level; +import org.apache.log4j.SimpleLayout; import org.dllearner.core.AbstractAxiomLearningAlgorithm; import org.dllearner.core.ComponentAnn; import org.dllearner.core.EvaluatedAxiom; @@ -42,9 +46,11 @@ import org.dllearner.kb.SparqlEndpointKS; import org.dllearner.kb.sparql.SparqlEndpoint; import org.dllearner.reasoning.SPARQLReasoner; +import org.semanticweb.owlapi.model.IRI; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.hp.hpl.jena.query.ParameterizedSparqlString; import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.ResultSet; @@ -56,8 +62,14 @@ @ConfigOption(name="propertyToDescribe", description="", propertyEditorClass=DataPropertyEditor.class) private DatatypeProperty propertyToDescribe; + private static final ParameterizedSparqlString singleQueryTemplate = new ParameterizedSparqlString("SELECT ?type (COUNT(DISTINCT ?ind) AS ?cnt) WHERE {?ind <%s> ?o. ?ind a ?type.}"); + + private Map<Individual, SortedSet<Description>> individual2Types; + public DataPropertyDomainAxiomLearner(SparqlEndpointKS ks){ this.ks = ks; + super.iterativeQueryTemplate = new ParameterizedSparqlString("SELECT DISTINCT ?ind ?type WHERE {?ind ?p ?o. ?ind a ?type.}"); + } public DatatypeProperty getPropertyToDescribe() { @@ -70,6 +82,7 @@ @Override public void start() { + iterativeQueryTemplate.setIri("p", propertyToDescribe.getName()); logger.info("Start learning..."); startTime = System.currentTimeMillis(); fetchedRows = 0; @@ -92,20 +105,49 @@ } } - //get subjects with types - Map<Individual, SortedSet<Description>> individual2Types = new HashMap<Individual, SortedSet<Description>>(); - boolean repeat = true; - int limit = 1000; - while(!terminationCriteriaSatisfied() && repeat){ - int ret = addIndividualsWithTypes(individual2Types, limit, fetchedRows); - currentlyBestAxioms = buildEvaluatedAxioms(individual2Types); - fetchedRows += 1000; - repeat = (ret == limit); - } + runIterativeQueryMode(); logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); } + + private void runSingleQueryMode(){ + + } + + private void runIterativeQueryMode(){ + individual2Types = new HashMap<Individual, SortedSet<Description>>(); + while(!terminationCriteriaSatisfied() && !fullDataLoaded){ + ResultSet rs = fetchData(); + processData(rs); + buildEvaluatedAxioms(); + } + } + + private void processData(ResultSet rs){ + QuerySolution qs; + Individual ind; + Description type; + SortedSet<Description> types; + int cnt = 0; + while(rs.hasNext()){ + cnt++; + qs = rs.next(); + if(qs.get("type").isURIResource()){ + types = new TreeSet<Description>(); + ind = new Individual(qs.getResource("ind").getURI()); + type = new NamedClass(qs.getResource("type").getURI()); + types.add(type); + if(reasoner.isPrepared()){ + if(reasoner.getClassHierarchy().contains(type)){ + types.addAll(reasoner.getClassHierarchy().getSuperClasses(type)); + } + } + addToMap(individual2Types, ind, types); + } + } + lastRowCount = cnt; + } - private List<EvaluatedAxiom> buildEvaluatedAxioms(Map<Individual, SortedSet<Description>> individual2Types){ + private void buildEvaluatedAxioms(){ List<EvaluatedAxiom> axioms = new ArrayList<EvaluatedAxiom>(); Map<Description, Integer> result = new HashMap<Description, Integer>(); for(Entry<Individual, SortedSet<Description>> entry : individual2Types.entrySet()){ @@ -134,46 +176,14 @@ axioms.add(evalAxiom); } - return axioms; + currentlyBestAxioms = axioms; } - private int addIndividualsWithTypes(Map<Individual, SortedSet<Description>> ind2Types, int limit, int offset){ - String query = String.format("PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT DISTINCT ?ind ?type WHERE {?ind <%s> ?o. ?ind a ?type. ?type a owl:Class} LIMIT %d OFFSET %d", propertyToDescribe.getName(), limit, offset); + public static void main(String[] args) throws Exception{ + org.apache.log4j.Logger.getRootLogger().addAppender(new ConsoleAppender(new SimpleLayout())); + org.apache.log4j.Logger.getRootLogger().setLevel(Level.INFO); + org.apache.log4j.Logger.getLogger(DataPropertyDomainAxiomLearner.class).setLevel(Level.INFO); -// String query = String.format("SELECT DISTINCT ?ind ?type WHERE {?ind a ?type. {SELECT ?ind {?ind <%s> ?o.} LIMIT %d OFFSET %d}}", propertyToDescribe.getName(), limit, offset); - - ResultSet rs = executeSelectQuery(query); - Individual ind; - Description newType; - QuerySolution qs; - SortedSet<Description> types; - int cnt = 0; - while(rs.hasNext()){ - cnt++; - qs = rs.next(); - if(qs.get("type").isURIResource()){ - ind = new Individual(qs.getResource("ind").getURI()); - newType = new NamedClass(qs.getResource("type").getURI()); - types = ind2Types.get(ind); - if(types == null){ - types = new TreeSet<Description>(); - ind2Types.put(ind, types); - } - types.add(newType); - Set<Description> superClasses; - if(reasoner.isPrepared()){ - if(reasoner.getClassHierarchy().contains(newType)){ - superClasses = reasoner.getClassHierarchy().getSuperClasses(newType); - types.addAll(superClasses); - } - - } - } - } - return cnt; - } - - public static void main(String[] args) throws Exception{ SparqlEndpointKS ks = new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpediaLiveAKSW()); SPARQLReasoner reasoner = new SPARQLReasoner(ks); @@ -181,9 +191,10 @@ DataPropertyDomainAxiomLearner l = new DataPropertyDomainAxiomLearner(new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpediaLiveAKSW())); l.setReasoner(reasoner); - l.setPropertyToDescribe(new DatatypeProperty("http://dbpedia.org/ontology/birthDate")); + l.setPropertyToDescribe(new DatatypeProperty("http://dbpedia.org/ontology/AutomobileEngine/height")); l.setMaxExecutionTimeInSeconds(10); - l.setReturnOnlyNewAxioms(true); + l.addFilterNamespace("http://dbpedia.org/ontology/"); +// l.setReturnOnlyNewAxioms(true); l.init(); l.start(); System.out.println(l.getCurrentlyBestEvaluatedAxioms(5)); 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 2012-05-07 14:28:49 UTC (rev 3696) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointDataPropertyAxiomLearner.java 2012-05-08 11:53:28 UTC (rev 3697) @@ -255,8 +255,8 @@ public static void main(String[] args) throws Exception{ DisjointDataPropertyAxiomLearner l = new DisjointDataPropertyAxiomLearner(new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpediaLiveAKSW())); - l.setPropertyToDescribe(new DatatypeProperty("http://dbpedia.org/ontology/position")); - l.setMaxExecutionTimeInSeconds(20); + l.setPropertyToDescribe(new DatatypeProperty("http://dbpedia.org/ontology/accessDate")); + l.setMaxExecutionTimeInSeconds(10); l.init(); l.getReasoner().precomputeDataPropertyPopularity(); l.start(); Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyDomainAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyDomainAxiomLearner.java 2012-05-07 14:28:49 UTC (rev 3696) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyDomainAxiomLearner.java 2012-05-08 11:53:28 UTC (rev 3697) @@ -19,7 +19,6 @@ package org.dllearner.algorithms.properties; -import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -29,11 +28,15 @@ import java.util.SortedSet; import java.util.TreeSet; +import org.apache.log4j.ConsoleAppender; +import org.apache.log4j.Level; +import org.apache.log4j.SimpleLayout; import org.dllearner.core.AbstractAxiomLearningAlgorithm; import org.dllearner.core.ComponentAnn; import org.dllearner.core.EvaluatedAxiom; import org.dllearner.core.config.ConfigOption; import org.dllearner.core.config.ObjectPropertyEditor; +import org.dllearner.core.owl.DatatypePropertyDomainAxiom; import org.dllearner.core.owl.Description; import org.dllearner.core.owl.Individual; import org.dllearner.core.owl.NamedClass; @@ -41,11 +44,13 @@ import org.dllearner.core.owl.ObjectPropertyDomainAxiom; import org.dllearner.core.owl.Thing; import org.dllearner.kb.SparqlEndpointKS; +import org.dllearner.kb.sparql.ExtractionDBCache; import org.dllearner.kb.sparql.SparqlEndpoint; import org.dllearner.reasoning.SPARQLReasoner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.hp.hpl.jena.query.ParameterizedSparqlString; import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.ResultSet; @@ -54,11 +59,15 @@ private static final Logger logger = LoggerFactory.getLogger(ObjectPropertyDomainAxiomLearner.class); + private Map<Individual, SortedSet<Description>> individual2Types; + + @ConfigOption(name="propertyToDescribe", description="", propertyEditorClass=ObjectPropertyEditor.class) private ObjectProperty propertyToDescribe; public ObjectPropertyDomainAxiomLearner(SparqlEndpointKS ks){ this.ks = ks; + super.iterativeQueryTemplate = new ParameterizedSparqlString("SELECT DISTINCT ?ind ?type WHERE {?ind ?p ?o. ?ind a ?type.}"); } public ObjectProperty getPropertyToDescribe() { @@ -71,12 +80,13 @@ @Override public void start() { + iterativeQueryTemplate.setIri("p", propertyToDescribe.getName()); logger.info("Start learning..."); startTime = System.currentTimeMillis(); fetchedRows = 0; currentlyBestAxioms = new ArrayList<EvaluatedAxiom>(); - if(reasoner.isPrepared()){ + if(returnOnlyNewAxioms){ //get existing domains Description existingDomain = reasoner.getDomain(propertyToDescribe); if(existingDomain != null){ @@ -93,20 +103,49 @@ } } - //get subjects with types - Map<Individual, SortedSet<Description>> individual2Types = new HashMap<Individual, SortedSet<Description>>(); - boolean repeat = true; - int limit = 1000; - while(!terminationCriteriaSatisfied() && repeat){ - int ret = addIndividualsWithTypes(individual2Types, limit, fetchedRows); - currentlyBestAxioms = buildEvaluatedAxioms(individual2Types); - fetchedRows += 1000; - repeat = (ret == limit); - } + runIterativeQueryMode(); logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); } + + private void runSingleQueryMode(){ + + } + + private void runIterativeQueryMode(){ + individual2Types = new HashMap<Individual, SortedSet<Description>>(); + while(!terminationCriteriaSatisfied() && !fullDataLoaded){ + ResultSet rs = fetchData(); + processData(rs); + buildEvaluatedAxioms(); + } + } + + private void processData(ResultSet rs){ + QuerySolution qs; + Individual ind; + Description type; + SortedSet<Description> types; + int cnt = 0; + while(rs.hasNext()){ + cnt++; + qs = rs.next(); + if(qs.get("type").isURIResource()){ + types = new TreeSet<Description>(); + ind = new Individual(qs.getResource("ind").getURI()); + type = new NamedClass(qs.getResource("type").getURI()); + types.add(type); + if(reasoner.isPrepared()){ + if(reasoner.getClassHierarchy().contains(type)){ + types.addAll(reasoner.getClassHierarchy().getSuperClasses(type)); + } + } + addToMap(individual2Types, ind, types); + } + } + lastRowCount = cnt; + } - private List<EvaluatedAxiom> buildEvaluatedAxioms(Map<Individual, SortedSet<Description>> individual2Types){ + private void buildEvaluatedAxioms(){ List<EvaluatedAxiom> axioms = new ArrayList<EvaluatedAxiom>(); Map<Description, Integer> result = new HashMap<Description, Integer>(); for(Entry<Individual, SortedSet<Description>> entry : individual2Types.entrySet()){ @@ -135,57 +174,25 @@ axioms.add(evalAxiom); } - return axioms; + currentlyBestAxioms = axioms; } - private int addIndividualsWithTypes(Map<Individual, SortedSet<Description>> ind2Types, int limit, int offset){ - String query = String.format("PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT DISTINCT ?ind ?type WHERE {?ind <%s> ?o. ?ind a ?type. ?type a owl:Class} LIMIT %d OFFSET %d", propertyToDescribe.getName(), limit, offset); + public static void main(String[] args) throws Exception{ + org.apache.log4j.Logger.getRootLogger().addAppender(new ConsoleAppender(new SimpleLayout())); + org.apache.log4j.Logger.getRootLogger().setLevel(Level.INFO); + org.apache.log4j.Logger.getLogger(DataPropertyDomainAxiomLearner.class).setLevel(Level.INFO); -// String query = String.format("SELECT DISTINCT ?ind ?type WHERE {?ind a ?type. {SELECT ?ind {?ind <%s> ?o.} LIMIT %d OFFSET %d}}", propertyToDescribe.getName(), limit, offset); + SparqlEndpointKS ks = new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpediaLiveAKSW()); - ResultSet rs = executeSelectQuery(query); - Individual ind; - Description newType; - QuerySolution qs; - SortedSet<Description> types; - int cnt = 0; - while(rs.hasNext()){ - cnt++; - qs = rs.next(); - if(qs.get("type").isURIResource()){ - ind = new Individual(qs.getResource("ind").getURI()); - newType = new NamedClass(qs.getResource("type").getURI()); - types = ind2Types.get(ind); - if(types == null){ - types = new TreeSet<Description>(); - ind2Types.put(ind, types); - } - types.add(newType); - Set<Description> superClasses; - if(reasoner.isPrepared()){ - if(reasoner.getClassHierarchy().contains(newType)){ - superClasses = reasoner.getClassHierarchy().getSuperClasses(newType); - types.addAll(superClasses); - } - - } - } - - } - return cnt; - } - - public static void main(String[] args) throws Exception{ - SparqlEndpointKS ks = new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpedia()); - SPARQLReasoner reasoner = new SPARQLReasoner(ks); reasoner.prepareSubsumptionHierarchy(); ObjectPropertyDomainAxiomLearner l = new ObjectPropertyDomainAxiomLearner(ks); l.setReasoner(reasoner); - l.setPropertyToDescribe(new ObjectProperty("http://dbpedia.org/ontology/Automobile/fuelCapacity")); + l.setPropertyToDescribe(new ObjectProperty("http://dbpedia.org/ontology/currency")); l.setMaxExecutionTimeInSeconds(10); + l.addFilterNamespace("http://dbpedia.org/ontology/"); // l.setReturnOnlyNewAxioms(true); l.init(); l.start(); Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyRangeAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyRangeAxiomLearner.java 2012-05-07 14:28:49 UTC (rev 3696) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyRangeAxiomLearner.java 2012-05-08 11:53:28 UTC (rev 3697) @@ -24,7 +24,6 @@ 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; @@ -52,6 +51,7 @@ public class ObjectPropertyRangeAxiomLearner extends AbstractAxiomLearningAlgorithm { private static final Logger logger = LoggerFactory.getLogger(ObjectPropertyRangeAxiomLearner.class); + private Map<Individual, SortedSet<Description>> individual2Types; @ConfigOption(name="propertyToDescribe", description="", propertyEditorClass=ObjectPropertyEditor.class) private ObjectProperty propertyToDescribe; @@ -70,12 +70,13 @@ @Override public void start() { + iterativeQueryTemplate.setIri("p", propertyToDescribe.getName()); logger.info("Start learning..."); startTime = System.currentTimeMillis(); fetchedRows = 0; currentlyBestAxioms = new ArrayList<EvaluatedAxiom>(); - if(reasoner.isPrepared()){ + if(returnOnlyNewAxioms){ //get existing ranges Description existingRange = reasoner.getRange(propertyToDescribe); if(existingRange != null){ @@ -92,20 +93,49 @@ } } - //get objects with types - Map<Individual, SortedSet<Description>> individual2Types = new HashMap<Individual, SortedSet<Description>>(); - boolean repeat = true; - int limit = 1000; - while(!terminationCriteriaSatisfied() && repeat){ - int ret = addIndividualsWithTypes(individual2Types, limit, fetchedRows); - currentlyBestAxioms = buildEvaluatedAxioms(individual2Types); - fetchedRows += 1000; - repeat = (ret == limit); - } + runIterativeQueryMode(); logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); } - private List<EvaluatedAxiom> buildEvaluatedAxioms(Map<Individual, SortedSet<Description>> individual2Types){ + private void runSingleQueryMode(){ + + } + + private void runIterativeQueryMode(){ + individual2Types = new HashMap<Individual, SortedSet<Description>>(); + while(!terminationCriteriaSatisfied() && !fullDataLoaded){ + ResultSet rs = fetchData(); + processData(rs); + buildEvaluatedAxioms(); + } + } + + private void processData(ResultSet rs){ + QuerySolution qs; + Individual ind; + Description type; + SortedSet<Description> types; + int cnt = 0; + while(rs.hasNext()){ + cnt++; + qs = rs.next(); + if(qs.get("type").isURIResource()){ + types = new TreeSet<Description>(); + ind = new Individual(qs.getResource("ind").getURI()); + type = new NamedClass(qs.getResource("type").getURI()); + types.add(type); + if(reasoner.isPrepared()){ + if(reasoner.getClassHierarchy().contains(type)){ + types.addAll(reasoner.getClassHierarchy().getSuperClasses(type)); + } + } + addToMap(individual2Types, ind, types); + } + } + lastRowCount = cnt; + } + + private void buildEvaluatedAxioms(){ List<EvaluatedAxiom> axioms = new ArrayList<EvaluatedAxiom>(); Map<Description, Integer> result = new HashMap<Description, Integer>(); for(Entry<Individual, SortedSet<Description>> entry : individual2Types.entrySet()){ @@ -134,52 +164,18 @@ axioms.add(evalAxiom); } - return axioms; + currentlyBestAxioms = axioms; } - private int addIndividualsWithTypes(Map<Individual, SortedSet<Description>> ind2Types, int limit, int offset){ - String query = String.format("PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT DISTINCT ?ind ?type WHERE {?s <%s> ?ind. ?ind a ?type. ?type a owl:Class} LIMIT %d OFFSET %d", propertyToDescribe.getName(), limit, offset); - -// String query = String.format("SELECT DISTINCT ?ind ?type WHERE {?ind a ?type. {SELECT ?ind {?ind <%s> ?o.} LIMIT %d OFFSET %d}}", propertyToDescribe.getName(), limit, offset); - - ResultSet rs = executeSelectQuery(query); - Individual ind; - Description newType; - QuerySolution qs; - SortedSet<Description> types; - int cnt = 0; - while(rs.hasNext()){ - cnt++; - qs = rs.next(); - ind = new Individual(qs.getResource("ind").getURI()); - newType = new NamedClass(qs.getResource("type").getURI()); - types = ind2Types.get(ind); - if(types == null){ - types = new TreeSet<Description>(); - ind2Types.put(ind, types); - } - types.add(newType); - Set<Description> superClasses; - if(reasoner.isPrepared()){ - if(reasoner.getClassHierarchy().contains(newType)){ - superClasses = reasoner.getClassHierarchy().getSuperClasses(newType); - types.addAll(superClasses); - } - - } - } - return cnt; - } - public static void main(String[] args) throws Exception{ - SparqlEndpointKS ks = new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpediaLiveAKSW()); + SparqlEndpointKS ks = new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpedia()); SPARQLReasoner reasoner = new SPARQLReasoner(ks); reasoner.prepareSubsumptionHierarchy(); ObjectPropertyRangeAxiomLearner l = new ObjectPropertyRangeAxiomLearner(ks); l.setReasoner(reasoner); - l.setPropertyToDescribe(new ObjectProperty("http://dbpedia.org/ontology/routeTypeAbbreviation")); + l.setPropertyToDescribe(new ObjectProperty("http://dbpedia.org/ontology/ideology")); l.setMaxExecutionTimeInSeconds(10); // l.setReturnOnlyNewAxioms(true); l.init(); Modified: trunk/components-core/src/main/java/org/dllearner/core/AbstractAxiomLearningAlgorithm.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/core/AbstractAxiomLearningAlgorithm.java 2012-05-07 14:28:49 UTC (rev 3696) +++ trunk/components-core/src/main/java/org/dllearner/core/AbstractAxiomLearningAlgorithm.java 2012-05-08 11:53:28 UTC (rev 3697) @@ -21,6 +21,7 @@ import java.net.SocketTimeoutException; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -49,7 +50,10 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import com.hp.hpl.jena.graph.Node; import com.hp.hpl.jena.ontology.OntClass; +import com.hp.hpl.jena.query.ParameterizedSparqlString; +import com.hp.hpl.jena.query.Query; import com.hp.hpl.jena.query.QueryExecution; import com.hp.hpl.jena.query.QueryExecutionFactory; import com.hp.hpl.jena.query.ResultSet; @@ -57,7 +61,15 @@ import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP; import com.hp.hpl.jena.sparql.engine.http.QueryExceptionHTTP; +import com.hp.hpl.jena.sparql.expr.E_Equals; +import com.hp.hpl.jena.sparql.expr.E_Regex; +import com.hp.hpl.jena.sparql.expr.E_Str; +import com.hp.hpl.jena.sparql.expr.ExprVar; +import com.hp.hpl.jena.sparql.expr.NodeValue; import com.hp.hpl.jena.sparql.resultset.ResultSetMem; +import com.hp.hpl.jena.sparql.syntax.ElementFilter; +import com.hp.hpl.jena.sparql.syntax.ElementGroup; +import com.hp.hpl.jena.sparql.util.NodeFactory; import com.hp.hpl.jena.util.iterator.Filter; import com.hp.hpl.jena.vocabulary.OWL2; import com.hp.hpl.jena.vocabulary.RDF; @@ -93,6 +105,17 @@ protected boolean forceSPARQL_1_0_Mode = false; + protected int chunkCount = 0; + protected int chunkSize = 1000; + protected int offset = 0; + protected int lastRowCount = 0; + + protected boolean fullDataLoaded = false; + + private List<String> filterNamespaces = new ArrayList<String>(); + + protected ParameterizedSparqlString iterativeQueryTemplate; + public AbstractAxiomLearningAlgorithm() { existingAxioms = new TreeSet<Axiom>(new AxiomComparator()); } @@ -259,7 +282,7 @@ } } - protected ResultSet executeSelectQuery(String query) { + protected ResultSet executeSelectQuery(String query) {System.out.println(query); logger.debug("Sending query\n{} ...", query); if(ks.isRemote()){ SparqlEndpoint endpoint = ((SparqlEndpointKS) ks).getEndpoint(); @@ -274,7 +297,12 @@ return rs; } catch (QueryExceptionHTTP e) { if(e.getCause() instanceof SocketTimeoutException){ - logger.warn("Got timeout"); + if(timeout){ + logger.warn("Got timeout"); + } else { + logger.trace("Got local timeout"); + } + } else { logger.error("Exception executing query", e); } @@ -381,6 +409,87 @@ return 2 * precision * recall / (precision + recall); } + protected ResultSet fetchData(){ + setChunkConditions(); + if(!fullDataLoaded){ + Query query = buildQuery(); + offset += chunkSize; + ResultSet rs = executeSelectQuery(query.toString()); + chunkCount++; + return rs; + } + return new ResultSetMem(); + } + + private void setChunkConditions() { + // adapt chunk size if needed + if (chunkCount == 1 && lastRowCount < chunkSize) { + logger.info("Adapting chunk size from " + chunkSize + " to " + lastRowCount); + chunkSize = lastRowCount; + offset = lastRowCount; + } + + // check if full data was loaded + if(chunkCount != 0){ + fullDataLoaded = (lastRowCount == 0) || (lastRowCount < chunkSize); + if (fullDataLoaded) { + logger.info("Loaded whole data. Early termination."); + } + } + } + + private Query buildQuery(){ + Query query = iterativeQueryTemplate.asQuery(); + for(String ns : filterNamespaces){ + ((ElementGroup)query.getQueryPattern()).addElementFilter( + new ElementFilter( + new E_Regex( + new E_Str(new ExprVar(Node.createVariable("type"))), + ns, ""))); + } + query.setLimit(chunkSize); + query.setOffset(offset); + return query; + } + + public void addFilterNamespace(String namespace){ + filterNamespaces.add(namespace); + } + + protected <K,T extends Set<V>, V> void addToMap(Map<K, T> map, K key, V value ){ + T values = map.get(key); + if(values == null){ + try { + values = (T) values.getClass().newInstance(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + values.add(value); + } + values.add(value); + } + + protected <K,T extends Set<V>, V> void addToMap(Map<K, T> map, K key, Collection<V> newValues ){ + T values = map.get(key); + if(values == null){ + try { + values = (T) newValues.getClass().newInstance(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + map.put(key, values); + } + values.addAll(newValues); + } + + private void adaptChunkCount(){ + + } + class OWLFilter extends Filter<OntClass>{ @Override Modified: trunk/scripts/src/main/java/org/dllearner/scripts/evaluation/EnrichmentEvaluation.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/evaluation/EnrichmentEvaluation.java 2012-05-07 14:28:49 UTC (rev 3696) +++ trunk/scripts/src/main/java/org/dllearner/scripts/evaluation/EnrichmentEvaluation.java 2012-05-08 11:53:28 UTC (rev 3697) @@ -219,6 +219,8 @@ private OWLReasoner reasoner; private OWLDataFactory factory = new OWLDataFactoryImpl(); + private static final String NAMESPACE = "http://dbpedia.org/ontology"; + private SPARQLReasoner sparqlReasoner; public EnrichmentEvaluation(SparqlEndpoint endpoint) { @@ -370,6 +372,7 @@ AxiomLearningAlgorithm learner = algorithmClass.getConstructor( SparqlEndpointKS.class).newInstance(ks); ((AbstractAxiomLearningAlgorithm)learner).setReasoner(sparqlReasoner); + ((AbstractAxiomLearningAlgorithm)learner).addFilterNamespace(NAMESPACE); ConfigHelper.configure(learner, "propertyToDescribe", property.toString()); ConfigHelper.configure(learner, "maxExecutionTimeInSeconds", maxExecutionTimeInSeconds); @@ -459,6 +462,7 @@ AxiomLearningAlgorithm learner = algorithmClass.getConstructor( SparqlEndpointKS.class).newInstance(ks); ((AbstractAxiomLearningAlgorithm)learner).setReasoner(sparqlReasoner); + ((AbstractAxiomLearningAlgorithm)learner).addFilterNamespace(NAMESPACE); ConfigHelper.configure(learner, "propertyToDescribe", property.toString()); ConfigHelper.configure(learner, "maxExecutionTimeInSeconds", maxExecutionTimeInSeconds); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |