From: <lor...@us...> - 2011-08-02 10:53:03
|
Revision: 2973 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=2973&view=rev Author: lorenz_b Date: 2011-08-02 10:52:56 +0000 (Tue, 02 Aug 2011) Log Message: ----------- Changed dependency scope. Extended reasoner. Modified Paths: -------------- trunk/components-core/pom.xml trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyDomainAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubPropertyOfAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/reasoning/ProtegeReasoner.java trunk/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java Modified: trunk/components-core/pom.xml =================================================================== --- trunk/components-core/pom.xml 2011-07-28 13:02:01 UTC (rev 2972) +++ trunk/components-core/pom.xml 2011-08-02 10:52:56 UTC (rev 2973) @@ -251,7 +251,6 @@ <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>3.0.5.RELEASE</version> - <scope>test</scope> </dependency> </dependencies> </project> Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyDomainAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyDomainAxiomLearner.java 2011-07-28 13:02:01 UTC (rev 2972) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyDomainAxiomLearner.java 2011-08-02 10:52:56 UTC (rev 2973) @@ -1,33 +1,80 @@ package org.dllearner.algorithms.properties; +import java.beans.PropertyEditor; +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.SortedSet; import org.dllearner.core.AxiomLearningAlgorithm; import org.dllearner.core.Component; import org.dllearner.core.ComponentInitException; +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.Description; +import org.dllearner.core.owl.Individual; +import org.dllearner.core.owl.NamedClass; +import org.dllearner.core.owl.ObjectProperty; import org.dllearner.kb.SparqlEndpointKS; +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.QuerySolution; +import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP; + public class PropertyDomainAxiomLearner extends Component implements AxiomLearningAlgorithm { - private String propertyToDescribe; + private static final Logger logger = LoggerFactory.getLogger(PropertyDomainAxiomLearner.class); - public String getPropertyToDescribe() { + @ConfigOption(name="propertyToDescribe", description="", propertyEditorClass=ObjectPropertyEditor.class) + private ObjectProperty propertyToDescribe; + @ConfigOption(name="maxExecutionTimeInSeconds", description="", propertyEditorClass=IntegerEditor.class) + private int maxExecutionTimeInSeconds; + + private SPARQLReasoner reasoner; + private SparqlEndpointKS ks; + + 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(String propertyToDescribe) { + public void setPropertyToDescribe(ObjectProperty propertyToDescribe) { this.propertyToDescribe = propertyToDescribe; } - - public PropertyDomainAxiomLearner(SparqlEndpointKS ks){ - - } @Override public void start() { - // TODO Auto-generated method stub + //get existing domains + Description existingDomain = reasoner.getDomain(propertyToDescribe); + logger.debug("Existing domain: " + existingDomain); + + //get subjects with types + Map<Individual, Set<NamedClass>> individual2Types = getSubjectsWithTypes(); + + //get subjects of property + Map<Individual, SortedSet<Individual>> members = reasoner.getPropertyMembers(propertyToDescribe); + } @@ -45,8 +92,76 @@ @Override public void init() throws ComponentInitException { - // TODO Auto-generated method stub + reasoner = new SPARQLReasoner(ks); } + + private Map<Individual, Set<NamedClass>> getSubjectsWithTypes(){ + Map<Individual, Set<NamedClass>> individual2Types = new HashMap<Individual, Set<NamedClass>>(); + int limit = 1000; + int offset = 135000; + boolean executeAgain = true; + + while(executeAgain){ + String query = String.format("SELECT ?ind ?type WHERE {?ind %s ?o. ?ind a ?type.} LIMIT %d OFFSET %d", inAngleBrackets(propertyToDescribe.getURI().toString()), limit, offset); + ResultSet rs = executeQuery(query); + QuerySolution qs; + Individual ind; + Set<NamedClass> types; + executeAgain = rs.hasNext(); + while(executeAgain && rs.hasNext()){ + qs = rs.next(); + ind = new Individual(qs.getResource("ind").getURI()); + types = individual2Types.get(ind); + if(types == null){ + types = new HashSet<NamedClass>(); + } + types.add(new NamedClass(qs.getResource("type").getURI())); + } + offset += 1000; + } + + return individual2Types; + } + + private String inAngleBrackets(String s){ + return "<" + s + ">"; + } + + private ResultSet executeQuery(String query){ + logger.info("Sending query \n {}", query); + + QueryEngineHTTP queryExecution = new QueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + for (String dgu : ks.getEndpoint().getDefaultGraphURIs()) { + queryExecution.addDefaultGraph(dgu); + } + for (String ngu : ks.getEndpoint().getNamedGraphURIs()) { + queryExecution.addNamedGraph(ngu); + } + ResultSet resultSet = queryExecution.execSelect(); + return resultSet; + } + + public static void main(String[] args) throws Exception{ + Map<String, String> propertiesMap = new HashMap<String, String>(); + propertiesMap.put("propertyToDescribe", "http://dbpedia.org/ontology/league"); + propertiesMap.put("maxExecutionTimeInSeconds", "20"); + + PropertyDomainAxiomLearner l = new PropertyDomainAxiomLearner(new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpedia())); + + Field[] fields = l.getClass().getDeclaredFields(); + for(Field f : fields){ + ConfigOption option = f.getAnnotation(ConfigOption.class); + if(option != null){ + String configValue = propertiesMap.get(option.name()); + PropertyEditor editor = (PropertyEditor) option.propertyEditorClass().newInstance(); + editor.setAsText(configValue); + f.set(l, editor.getValue()); + } + } + + l.init(); + l.start(); + } } Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubPropertyOfAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubPropertyOfAxiomLearner.java 2011-07-28 13:02:01 UTC (rev 2972) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubPropertyOfAxiomLearner.java 2011-08-02 10:52:56 UTC (rev 2973) @@ -3,8 +3,10 @@ import java.beans.PropertyEditor; import java.lang.reflect.Field; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import org.dllearner.core.AxiomLearningAlgorithm; import org.dllearner.core.Component; @@ -17,8 +19,12 @@ import org.dllearner.core.owl.ObjectProperty; import org.dllearner.kb.SparqlEndpointKS; import org.dllearner.kb.sparql.SparqlEndpoint; -import org.springframework.beans.propertyeditors.CustomNumberEditor; +import org.dllearner.reasoning.SPARQLReasoner; +import com.hp.hpl.jena.query.QuerySolution; +import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP; + public class SubPropertyOfAxiomLearner extends Component implements AxiomLearningAlgorithm { @ConfigOption(name="propertyToDescribe", description="", propertyEditorClass=ObjectPropertyEditor.class) @@ -26,7 +32,7 @@ @ConfigOption(name="maxExecutionTimeInSeconds", description="", propertyEditorClass=IntegerEditor.class) private int maxExecutionTimeInSeconds; - + private SPARQLReasoner reasoner; private SparqlEndpointKS ks; public SubPropertyOfAxiomLearner(SparqlEndpointKS ks){ @@ -35,8 +41,17 @@ @Override public void start() { - // TODO Auto-generated method stub - + //get + Set<ObjectProperty> properties = new HashSet<ObjectProperty>(); + String query = String.format("SELECT ?p ?p1 WHERE {?s %s ?o. ?s ?p ?o1. ?s1 ?p1 ?o.}", inAngleBrackets(propertyToDescribe.getURI().toString())); + ResultSet rs = executeQuery(query); + QuerySolution qs; + while(rs.hasNext()){ + qs = rs.next(); + properties.add(new ObjectProperty(qs.getResource("p").getURI())); + properties.add(new ObjectProperty(qs.getResource("p1").getURI())); + } + System.out.println(properties); } @Override @@ -53,8 +68,7 @@ @Override public void init() throws ComponentInitException { - // TODO Auto-generated method stub - + reasoner = new SPARQLReasoner(ks); } public int getMaxExecutionTimeInSeconds() { @@ -73,9 +87,26 @@ this.propertyToDescribe = propertyToDescribe; } + private String inAngleBrackets(String s){ + return "<" + s + ">"; + } + + private ResultSet executeQuery(String query){ + System.out.println(query); + QueryEngineHTTP queryExecution = new QueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + for (String dgu : ks.getEndpoint().getDefaultGraphURIs()) { + queryExecution.addDefaultGraph(dgu); + } + for (String ngu : ks.getEndpoint().getNamedGraphURIs()) { + queryExecution.addNamedGraph(ngu); + } + ResultSet resultset = queryExecution.execSelect(); + return resultset; + } + public static void main(String[] args) throws Exception{ Map<String, String> propertiesMap = new HashMap<String, String>(); - propertiesMap.put("propertyToDescribe", "http://dbpedia.org/ontology/locatedIn"); + propertiesMap.put("propertyToDescribe", "http://dbpedia.org/ontology/league"); propertiesMap.put("maxExecutionTimeInSeconds", "20"); SubPropertyOfAxiomLearner l = new SubPropertyOfAxiomLearner(new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpediaLiveAKSW())); @@ -91,8 +122,9 @@ } } - System.out.println(l.getPropertyToDescribe()); - System.out.println(l.getMaxExecutionTimeInSeconds()); + l.init(); + l.start(); + } } Modified: trunk/components-core/src/main/java/org/dllearner/reasoning/ProtegeReasoner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/reasoning/ProtegeReasoner.java 2011-07-28 13:02:01 UTC (rev 2972) +++ trunk/components-core/src/main/java/org/dllearner/reasoning/ProtegeReasoner.java 2011-08-02 10:52:56 UTC (rev 2973) @@ -748,7 +748,9 @@ Set<OWLNamedIndividual> individuals = reasoner.getInstances(d, false).getFlattened(); SortedSet<Individual> inds = new TreeSet<Individual>(); for(OWLNamedIndividual ind : individuals) - inds.add(new Individual(ind.toStringID())); + if(ind != null){ + inds.add(new Individual(ind.toStringID())); + } return inds; } @@ -1280,8 +1282,16 @@ } // take one element from the set and ignore the rest // (TODO: we need to make sure we always ignore the same concepts) - OWLObjectPropertyExpression property = node.getRepresentativeElement(); - roles.add(new ObjectProperty(property.asOWLObjectProperty().toStringID())); + + for(OWLObjectPropertyExpression property : node.getEntities()){ + if(!property.isAnonymous()){ + roles.add(new ObjectProperty(property.asOWLObjectProperty().toStringID())); + break; + } + } + //TODO: We get a problem when the returned representative element is anonymous, so we now use the code above +// OWLObjectPropertyExpression property = node.getRepresentativeElement(); +// roles.add(new ObjectProperty(property.asOWLObjectProperty().toStringID())); } roles.remove(new ObjectProperty(factory.getOWLTopObjectProperty().toStringID())); roles.remove(new ObjectProperty(factory.getOWLBottomObjectProperty().toStringID())); Modified: trunk/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java 2011-07-28 13:02:01 UTC (rev 2972) +++ trunk/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java 2011-08-02 10:52:56 UTC (rev 2973) @@ -9,6 +9,7 @@ import java.util.SortedSet; import java.util.TreeSet; +import org.dllearner.algorithms.properties.PropertyDomainAxiomLearner; import org.dllearner.core.IndividualReasoner; import org.dllearner.core.SchemaReasoner; import org.dllearner.core.owl.ClassHierarchy; @@ -25,6 +26,8 @@ import org.dllearner.kb.SparqlEndpointKS; import org.dllearner.kb.sparql.SparqlEndpoint; import org.dllearner.utilities.datastructures.SortedSetTuple; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.clarkparsia.owlapiv3.XSD; import com.hp.hpl.jena.query.QuerySolution; @@ -36,6 +39,8 @@ public class SPARQLReasoner implements SchemaReasoner, IndividualReasoner{ + private static final Logger logger = LoggerFactory.getLogger(PropertyDomainAxiomLearner.class); + private SparqlEndpointKS ks; public SPARQLReasoner(SparqlEndpointKS ks) { @@ -117,7 +122,7 @@ @Override public Map<ObjectProperty, Set<Individual>> getObjectPropertyRelationships(Individual individual) { Map<ObjectProperty, Set<Individual>> prop2individuals = new HashMap<ObjectProperty, Set<Individual>>(); - String query = String.format("select ?prop ?ind WHERE {" + + String query = String.format("SELECT ?prop ?ind WHERE {" + "%s ?prop ?ind." + " FILTER(isIRI(?ind) && ?prop != %s && ?prop != %s)}", inAngleBrackets(individual.getName()), inAngleBrackets(RDF.type.getURI()), inAngleBrackets(OWL.sameAs.getURI())); @@ -145,7 +150,7 @@ @Override public Map<Individual, SortedSet<Individual>> getPropertyMembers(ObjectProperty objectProperty) { Map<Individual, SortedSet<Individual>> subject2objects = new HashMap<Individual, SortedSet<Individual>>(); - String query = String.format("select ?s ?o WHERE {" + + String query = String.format("SELECT ?s ?o WHERE {" + "?s %s ?o." + " FILTER(isIRI(?o))}", inAngleBrackets(objectProperty.getName())); @@ -179,7 +184,7 @@ @Override public Map<Individual, SortedSet<Double>> getDoubleDatatypeMembers(DatatypeProperty datatypeProperty) { Map<Individual, SortedSet<Double>> subject2objects = new HashMap<Individual, SortedSet<Double>>(); - String query = String.format("select ?s ?o WHERE {" + + String query = String.format("SELECT ?s ?o WHERE {" + "?s %s ?o." + " FILTER(DATATYPE(?o) = %s)}", inAngleBrackets(datatypeProperty.getName()), inAngleBrackets(XSD.DOUBLE.toStringID())); @@ -207,7 +212,7 @@ @Override public Map<Individual, SortedSet<Integer>> getIntDatatypeMembers(DatatypeProperty datatypeProperty) { Map<Individual, SortedSet<Integer>> subject2objects = new HashMap<Individual, SortedSet<Integer>>(); - String query = String.format("select ?s ?o WHERE {" + + String query = String.format("SELECT ?s ?o WHERE {" + "?s %s ?o." + " FILTER(DATATYPE(?o) = %s)}", inAngleBrackets(datatypeProperty.getName()), inAngleBrackets(XSD.INT.toStringID())); @@ -235,7 +240,7 @@ @Override public Map<Individual, SortedSet<Boolean>> getBooleanDatatypeMembers(DatatypeProperty datatypeProperty) { Map<Individual, SortedSet<Boolean>> subject2objects = new HashMap<Individual, SortedSet<Boolean>>(); - String query = String.format("select ?s ?o WHERE {" + + String query = String.format("SELECT ?s ?o WHERE {" + "?s %s ?o." + " FILTER(DATATYPE(?o) = %s)}", inAngleBrackets(datatypeProperty.getName()), inAngleBrackets(XSD.BOOLEAN.toStringID())); @@ -263,7 +268,7 @@ @Override public SortedSet<Individual> getTrueDatatypeMembers(DatatypeProperty datatypeProperty) { SortedSet<Individual> members = new TreeSet<Individual>(); - String query = String.format("select ?ind WHERE {" + + String query = String.format("SELECT ?ind WHERE {" + "?ind %s ?o." + " FILTER(isLiteral(?o) && DATATYPE(?o) = %s && ?o=%s)}", inAngleBrackets(datatypeProperty.getName()), inAngleBrackets(XSD.BOOLEAN.toStringID()), @@ -282,7 +287,7 @@ @Override public SortedSet<Individual> getFalseDatatypeMembers(DatatypeProperty datatypeProperty) { SortedSet<Individual> members = new TreeSet<Individual>(); - String query = String.format("select ?ind WHERE {" + + String query = String.format("SELECT ?ind WHERE {" + "?ind %s ?o." + " FILTER(isLiteral(?o) && DATATYPE(?o) = %s && ?o=%s)}", inAngleBrackets(datatypeProperty.getName()), inAngleBrackets(XSD.BOOLEAN.toStringID()), @@ -311,7 +316,7 @@ @Override public Description getDomain(ObjectProperty objectProperty) { - String query = String.format("select ?domain WHERE {" + + String query = String.format("SELECT ?domain WHERE {" + "%s %s ?domain. FILTER(isIRI(?domain))" + "}", inAngleBrackets(objectProperty.getName()), inAngleBrackets(RDFS.domain.getURI())); @@ -334,7 +339,7 @@ @Override public Description getDomain(DatatypeProperty datatypeProperty) { - String query = String.format("select ?domain WHERE {" + + String query = String.format("SELECT ?domain WHERE {" + "%s %s ?domain. FILTER(isIRI(?domain))" + "}", inAngleBrackets(datatypeProperty.getName()), inAngleBrackets(RDFS.domain.getURI())); @@ -357,7 +362,7 @@ @Override public Description getRange(ObjectProperty objectProperty) { - String query = String.format("select ?range WHERE {" + + String query = String.format("SELECT ?range WHERE {" + "%s %s ?range. FILTER(isIRI(?range))" + "}", inAngleBrackets(objectProperty.getName()), inAngleBrackets(RDFS.range.getURI())); @@ -413,7 +418,7 @@ @Override public Set<Description> getAssertedDefinitions(NamedClass namedClass) { Set<Description> definitions = new HashSet<Description>(); - String query = String.format("SELECT ?class {%s %s ?class. FILTER(isIRI(?class))} UNION {?class. %s %s. FILTER(isIRI(?class))}", + String query = String.format("SELECT ?class { {%s %s ?class. FILTER(isIRI(?class))} UNION {?class. %s %s. FILTER(isIRI(?class))} }", inAngleBrackets(namedClass.getURI().toString()), inAngleBrackets(OWL.equivalentClass.getURI()), inAngleBrackets(OWL.equivalentClass.getURI()), @@ -535,14 +540,35 @@ @Override public SortedSet<DatatypeProperty> getSuperProperties(DatatypeProperty dataProperty) { - // TODO Auto-generated method stub - return null; + SortedSet<DatatypeProperty> superProperties = new TreeSet<DatatypeProperty>(); + String query = String.format("SELECT ?sup {%s %s ?sup. FILTER(isIRI(?sup))}", + inAngleBrackets(dataProperty.getURI().toString()), + inAngleBrackets(RDFS.subPropertyOf.getURI()) + ); + ResultSet rs = executeQuery(query); + QuerySolution qs; + while(rs.hasNext()){ + qs = rs.next(); + superProperties.add(new DatatypeProperty(qs.getResource("sup").getURI())); + } + return superProperties; } @Override public SortedSet<DatatypeProperty> getSubProperties(DatatypeProperty dataProperty) { - // TODO Auto-generated method stub - return null; + SortedSet<DatatypeProperty> subProperties = new TreeSet<DatatypeProperty>(); + String query = String.format("SELECT ?sub {?sub %s %s. FILTER(isIRI(?sub))}", + inAngleBrackets(RDFS.subPropertyOf.getURI()), + inAngleBrackets(dataProperty.getURI().toString()) + + ); + ResultSet rs = executeQuery(query); + QuerySolution qs; + while(rs.hasNext()){ + qs = rs.next(); + subProperties.add(new DatatypeProperty(qs.getResource("sub").getURI())); + } + return subProperties; } @Override @@ -556,7 +582,7 @@ } private ResultSet executeQuery(String query){ - System.out.println(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); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |