From: <lor...@us...> - 2011-08-15 14:49:00
|
Revision: 3046 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3046&view=rev Author: lorenz_b Date: 2011-08-15 14:48:54 +0000 (Mon, 15 Aug 2011) Log Message: ----------- Started simple subclass axioms learner for SPARQL endpoints. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/algorithms/SimpleSubclassLearner.java Added Paths: ----------- trunk/components-core/src/main/java/org/dllearner/core/config/NamedClassEditor.java Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/SimpleSubclassLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/SimpleSubclassLearner.java 2011-08-15 14:22:11 UTC (rev 3045) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/SimpleSubclassLearner.java 2011-08-15 14:48:54 UTC (rev 3046) @@ -19,13 +19,37 @@ */ package org.dllearner.algorithms; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.SortedSet; +import java.util.TreeSet; +import org.dllearner.algorithms.properties.ObjectPropertyDomainAxiomLearner; import org.dllearner.core.ClassExpressionLearningAlgorithm; import org.dllearner.core.ComponentInitException; +import org.dllearner.core.EvaluatedAxiom; import org.dllearner.core.EvaluatedDescription; +import org.dllearner.core.Score; +import org.dllearner.core.config.ConfigOption; +import org.dllearner.core.config.IntegerEditor; +import org.dllearner.core.config.NamedClassEditor; +import org.dllearner.core.config.ObjectPropertyEditor; 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.SparqlQuery; +import org.dllearner.learningproblems.ClassScore; +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; + /** * Learns sub classes using SPARQL queries. * @@ -34,6 +58,26 @@ * */ public class SimpleSubclassLearner implements ClassExpressionLearningAlgorithm { + + private static final Logger logger = LoggerFactory.getLogger(SimpleSubclassLearner.class); + + @ConfigOption(name="classToDescribe", description="", propertyEditorClass=NamedClassEditor.class) + private NamedClass classToDescribe; + @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<EvaluatedDescription> currentlyBestEvaluatedDescriptions; + private long startTime; + private int fetchedRows; + + public SimpleSubclassLearner(SparqlEndpointKS ks) { + this.ks = ks; + } @Override public List<Description> getCurrentlyBestDescriptions(int nrOfDescriptions) { @@ -50,14 +94,97 @@ @Override public void start() { - // TODO Auto-generated method stub + logger.info("Start learning..."); + startTime = System.currentTimeMillis(); + fetchedRows = 0; + currentlyBestEvaluatedDescriptions = new ArrayList<EvaluatedDescription>(); + + Map<Individual, SortedSet<NamedClass>> ind2Types = new HashMap<Individual, SortedSet<NamedClass>>(); + int limit = 1000; + int offset = 0; + while(!terminationCriteriaSatisfied()){ + addIndividualsWithTypes(ind2Types, limit, offset); + } + + logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); } @Override public void init() throws ComponentInitException { - // TODO Auto-generated method stub + reasoner = new SPARQLReasoner(ks); + } + + public int getMaxExecutionTimeInSeconds() { + return maxExecutionTimeInSeconds; + } + public void setMaxExecutionTimeInSeconds(int maxExecutionTimeInSeconds) { + this.maxExecutionTimeInSeconds = maxExecutionTimeInSeconds; } + public NamedClass getPropertyToDescribe() { + return classToDescribe; + } + + public void setPropertyToDescribe(NamedClass classToDescribe) { + this.classToDescribe = classToDescribe; + } + + public int getMaxFetchedRows() { + return maxFetchedRows; + } + + public void setMaxFetchedRows(int maxFetchedRows) { + this.maxFetchedRows = maxFetchedRows; + } + + private void addIndividualsWithTypes(Map<Individual, SortedSet<NamedClass>> ind2Types, int limit, int offset){ + String query = String.format("SELECT ?ind ?type WHERE {?ind a <%s>. ?ind a ?type} LIMIT %d OFFSET %d", classToDescribe.getName(), limit, offset); + + ResultSet rs = new SparqlQuery(query, ks.getEndpoint()).send(); + Individual ind; + NamedClass newType; + QuerySolution qs; + SortedSet<NamedClass> types; + while(rs.hasNext()){ + 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<NamedClass>(); + ind2Types.put(ind, types); + } + types.add(newType); + } + } + + private void createEvaluatedDescriptions(Map<Individual, SortedSet<NamedClass>> ind2Types){ + + } + + private double computeScore(){ + return 0; + } + + private boolean terminationCriteriaSatisfied(){ + boolean timeLimitExceeded = maxExecutionTimeInSeconds == 0 ? false : (System.currentTimeMillis() - startTime) >= maxExecutionTimeInSeconds * 1000; + boolean resultLimitExceeded = maxFetchedRows == 0 ? false : fetchedRows >= maxFetchedRows; + return timeLimitExceeded || resultLimitExceeded; + } + + public static void main(String[] args) { + Map<String, SortedSet<String>> map = new HashMap<String, SortedSet<String>>(); + SortedSet<String> set = new TreeSet<String>(); + set.add("2");set.add("3"); + map.put("1", set); + + set = new TreeSet<String>(); + set.add("2");set.add("4"); + map.put("1", set); + + System.out.println(map); + } + } Added: trunk/components-core/src/main/java/org/dllearner/core/config/NamedClassEditor.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/core/config/NamedClassEditor.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/core/config/NamedClassEditor.java 2011-08-15 14:48:54 UTC (rev 3046) @@ -0,0 +1,90 @@ +package org.dllearner.core.config; + +import java.awt.Component; +import java.awt.Graphics; +import java.awt.Rectangle; +import java.beans.PropertyChangeListener; +import java.beans.PropertyEditor; + +import org.dllearner.core.owl.NamedClass; + +/** + * Created by IntelliJ IDEA. + * User: Chris + * Date: 7/26/11 + * Time: 9:42 PM + * <p/> + * Basic Property Editor for the Object Property DL-Learner class. Doesn't have GUI support yet but we could add that later if we wanted. + */ +public class NamedClassEditor implements PropertyEditor { + + + private NamedClass value; + + @Override + public void setValue(Object value) { + this.value = (NamedClass) value; + } + + @Override + public Object getValue() { + return value; + } + + @Override + public boolean isPaintable() { + /** Not right now, we're doing non gui work */ + return false; + } + + @Override + public void paintValue(Graphics gfx, Rectangle box) { + + } + + @Override + public String getJavaInitializationString() { + /** This returns the value needed to reconstitute the object from a string */ + return value.getName(); + } + + @Override + public String getAsText() { + /** Get the text value of this object - for displaying in GUIS, etc */ + return value.getName(); + } + + @Override + public void setAsText(String text) throws IllegalArgumentException { + value = new NamedClass(text); + } + + @Override + public String[] getTags() { + /** If there was a known set of values it had to have, we could add that list here */ + return new String[0]; + } + + @Override + public Component getCustomEditor() { + /** GUI stuff, if you wanted to edit it a custom way */ + return null; + } + + @Override + public boolean supportsCustomEditor() { + /** We don't support this right now, but maybe later */ + return false; + + } + + @Override + public void addPropertyChangeListener(PropertyChangeListener listener) { + /** More gui stuff, we don't need this for our basic example */ + } + + @Override + public void removePropertyChangeListener(PropertyChangeListener listener) { + /** More gui stuff, we don't need this for our basic example */ + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |