From: <lor...@us...> - 2011-09-16 20:20:58
|
Revision: 3277 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3277&view=rev Author: lorenz_b Date: 2011-09-16 20:20:52 +0000 (Fri, 16 Sep 2011) Log Message: ----------- Added option to save all learned axioms into a ontology file by using the parameter -s. Fixed bug which always led to only one visited entity per type, if no resource was explicitly given. Modified Paths: -------------- trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java Modified: trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java 2011-09-16 20:17:22 UTC (rev 3276) +++ trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java 2011-09-16 20:20:52 UTC (rev 3277) @@ -21,10 +21,12 @@ import static java.util.Arrays.asList; +import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.io.PrintStream; import java.lang.reflect.InvocationTargetException; import java.math.BigInteger; @@ -83,6 +85,7 @@ import org.dllearner.core.EvaluatedDescription; import org.dllearner.core.LearningAlgorithm; import org.dllearner.core.LearningProblemUnsupportedException; +import org.dllearner.core.OntologyFormat; import org.dllearner.core.Score; import org.dllearner.core.config.ConfigHelper; import org.dllearner.core.config.ConfigOption; @@ -110,7 +113,12 @@ import org.dllearner.utilities.datastructures.SetManipulation; import org.dllearner.utilities.datastructures.SortedSetTuple; import org.dllearner.utilities.examples.AutomaticNegativeExampleFinderSPARQL2; +import org.dllearner.utilities.owl.DLLearnerAxiomConvertVisitor; +import org.dllearner.utilities.owl.OWLAPIAxiomConvertVisitor; +import org.dllearner.utilities.owl.OWLAPIConverter; +import org.dllearner.utilities.owl.OWLAPIDescriptionConvertVisitor; import org.semanticweb.owlapi.apibinding.OWLManager; +import org.semanticweb.owlapi.io.RDFXMLOntologyFormat; import org.semanticweb.owlapi.io.SystemOutDocumentTarget; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLAnnotation; @@ -183,8 +191,8 @@ private int maxExecutionTimeInSeconds = 10; // restrict tested number of entities per type (only for testing purposes); - // should be set to 0 in production mode - private int maxEntitiesPerType = 0; + // should be set to -1 in production mode + private int maxEntitiesPerType = -1; // number of axioms which will be learned/considered (only applies to // some learners) @@ -208,6 +216,8 @@ SparqlKnowledgeSource ksCached; AbstractReasonerComponent rcCached; + private Set<OWLAxiom> learnedOWLAxioms; + public Enrichment(SparqlEndpoint se, Entity resource, double threshold, boolean useInference, boolean verbose) { this.se = se; this.resource = resource; @@ -240,6 +250,8 @@ classAlgorithms.add(CELOE.class); algorithmRuns = new LinkedList<AlgorithmRun>(); + + learnedOWLAxioms = new HashSet<OWLAxiom>(); } public void start() throws ComponentInitException, IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, LearningProblemUnsupportedException, MalformedURLException { @@ -248,6 +260,13 @@ SparqlEndpointKS ks = new SparqlEndpointKS(se); ks.init(); + // common helper objects + SPARQLTasks st = new SPARQLTasks(se); + + //check if endpoint supports SPARQL 1.1 + boolean supportsSPARQL_1_1 = st.supportsSPARQL_1_1(); + ks.setSupportsSPARQL_1_1(supportsSPARQL_1_1); + if(useInference){ reasoner = new SPARQLReasoner(ks); System.out.print("Precomputing subsumption hierarchy ... "); @@ -256,9 +275,6 @@ System.out.println("done in " + (System.currentTimeMillis() - startTime) + " ms"); } - // common helper objects - SPARQLTasks st = new SPARQLTasks(se); - if(resource == null) { // loop over all entities and call appropriate algorithms @@ -267,16 +283,16 @@ for(NamedClass nc : classes) { runClassLearningAlgorithms(ks, nc); entities++; - if(entities > maxEntitiesPerType) { + if(maxEntitiesPerType != -1 && entities > maxEntitiesPerType) { break; - } + } } entities = 0; Set<ObjectProperty> objectProperties = st.getAllObjectProperties(); for(ObjectProperty property : objectProperties) { runObjectPropertyAlgorithms(ks, property); entities++; - if(entities > maxEntitiesPerType) { + if(maxEntitiesPerType != -1 && entities > maxEntitiesPerType) { break; } } @@ -285,9 +301,9 @@ for(DatatypeProperty property : dataProperties) { runDataPropertyAlgorithms(ks, property); entities++; - if(entities > maxEntitiesPerType) { + if(maxEntitiesPerType != -1 && entities > maxEntitiesPerType) { break; - } + } } } else { if(resource instanceof ObjectProperty) { @@ -449,8 +465,12 @@ System.out.println("done in " + runtime + " ms"); List<EvaluatedAxiom> learnedAxioms = learner .getCurrentlyBestEvaluatedAxioms(nrOfAxiomsToLearn, threshold); - System.out.println(prettyPrint(learnedAxioms)); + System.out.println(prettyPrint(learnedAxioms)); + for(EvaluatedAxiom evAx : learnedAxioms){ + learnedOWLAxioms.add(OWLAPIAxiomConvertVisitor.convertAxiom(evAx.getAxiom())); + } + algorithmRuns.add(new AlgorithmRun(learner.getClass(), learnedAxioms, ConfigHelper.getConfigOptionValues(learner))); return learnedAxioms; } @@ -626,6 +646,18 @@ return model; } + private OWLOntology getGeneratedOntology(){ + OWLOntology ontology = null; + try { + OWLOntologyManager man = OWLManager.createOWLOntologyManager(); + ontology = man.createOntology(learnedOWLAxioms); + } catch (OWLOntologyCreationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return ontology; + } + /* * Write axioms in Turtle syntax. */ @@ -692,7 +724,8 @@ .ofType(Double.class).defaultsTo(0.7); parser.acceptsAll(asList("i", "inference"), "Specifies whether to use inference. If yes, the schema will be loaded into a reasoner and used for computing the scores.").withOptionalArg().ofType(Boolean.class).defaultsTo(true); - + parser.acceptsAll(asList("s", "serialize"), "Specify a file where the ontology with all axioms can be written.") + .withRequiredArg().ofType(File.class); // parse options and display a message for the user in case of problems OptionSet options = null; try { @@ -822,7 +855,19 @@ } } } + //serialize ontology + if(options.has("s")){ + File file = (File)options.valueOf("s"); + try { + OWLOntology ontology = e.getGeneratedOntology(); + OutputStream os = new BufferedOutputStream(new FileOutputStream(file)); + OWLManager.createOWLOntologyManager().saveOntology(ontology, new RDFXMLOntologyFormat(), os); + } catch (OWLOntologyStorageException e1) { + throw new Error("Could not save ontology."); + } + } + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |