diff --git a/.gitignore b/.gitignore index 942867a445..22a15a143a 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ target deptree.txt cache log +*.idea +*.iml diff --git a/components-core/pom.xml b/components-core/pom.xml index e155e86014..3e991156e3 100644 --- a/components-core/pom.xml +++ b/components-core/pom.xml @@ -10,113 +10,114 @@ org.dllearner dllearner-parent - 1.1 + 1.2 - + - - release - - - - - org.apache.maven.plugins - maven-surefire-plugin - - - - - - - org/dllearner/test/* - org/dllearner/test/junit/TestOntologies.java - org/dllearner/test/junit/PropertyLearningTest.java - org/dllearner/test/junit/DisjointClassesLearningTest.java - - **/*$* - - - - - - org.apache.maven.plugins - maven-jar-plugin - - - - test-jar - - - - - - - - - - - - - org.codehaus.mojo - license-maven-plugin - 1.8 - - gpl_v3 - AKSW - 2007 - - src/main/java - src/test/java - - true - - - - download-licenses - - download-licenses - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - -
- public - true - doc/javadoc - DL-Learner Javadoc - true - false - false - false - false - false - src/etc/overview.html - ${compiler.version} - src/main/java - true - src/etc/javadoc2.css - true - true - true - <img style='float:right' - src='https://raw.githubusercontent.com/github/media/master/octocats/blacktocat-16.png' - border='0' alt='Github.com Logo' /> DL-Learner is licenced - under the terms of the GNU General Public License.<br - />Copyright &#169; 2007-2015 Jens Lehmann - ISO-8859-1 - DL-Learner Javadoc - -Xdoclint:none -
-
-
-
-
-
+ + release + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + + + + + org/dllearner/test/* + org/dllearner/test/junit/TestOntologies.java + org/dllearner/test/junit/PropertyLearningTest.java + org/dllearner/test/junit/DisjointClassesLearningTest.java + + **/*$* + + -Dlog4j.configuration=log4j.properties + + + + + org.apache.maven.plugins + maven-jar-plugin + + + + test-jar + + + + + + + + + + + + + org.codehaus.mojo + license-maven-plugin + 1.8 + + gpl_v3 + AKSW + 2007 + + src/main/java + src/test/java + + true + + + + download-licenses + + download-licenses + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + +
+ public + true + doc/javadoc + DL-Learner Javadoc + true + false + false + false + false + false + src/etc/overview.html + ${compiler.version} + src/main/java + true + src/etc/javadoc2.css + true + true + true + <img style='float:right' + src='https://raw.githubusercontent.com/github/media/master/octocats/blacktocat-16.png' + border='0' alt='Github.com Logo' /> DL-Learner is licenced + under the terms of the GNU General Public License.<br + />Copyright &#169; 2007-2015 Jens Lehmann + ISO-8859-1 + DL-Learner Javadoc + -Xdoclint:none +
+
+
+
+
+ @@ -137,6 +138,7 @@ **/*$* + -Dlog4j.configuration=log4j.properties + com.github.github + site-maven-plugin + 0.12 + + + + site + + + site-deploy + + + + github + + + Creating site for ${project.name} ${project.version} + + ${project.version} + + true + true + + + + @@ -160,17 +200,17 @@ org.ini4j ini4j - + mysql mysql-connector-java - + net.sourceforge.owlapi owlapi-distribution - + net.sourceforge.owlapi owlapi-api @@ -182,6 +222,10 @@ net.sourceforge.owlapi owlapi-impl + + net.sourceforge.owlapi + owlapitools-concurrentimpl + com.clarkparsia.pellet @@ -189,35 +233,35 @@ pom - + - - + + org.json json - + com.google.code.factplusplus @@ -230,10 +274,32 @@ - org.semanticweb.hermit - hermit + net.sourceforge.owlapi + org.semanticweb.hermit + + org.semanticweb.elk + elk-owlapi + + + + de.tudresden.inf.lat.cel + reasoner + 0.5.1 + + + + net.sourceforge.owlapi jfact @@ -256,17 +322,17 @@ 1.0 - - - - - + + + + + - org.fuzzy - fuzzyowl - 1.0 - + org.fuzzy + fuzzyowl + 1.0 + - - org.semanticweb.elk - elk-owlapi - - - de.tudresden.inf.lat.cel - reasoner - 0.5.1 - - - - eu.trowl - trowl-core - + - - uk.ac.shef.wit - simmetrics - 1.6.2 - - - com.thoughtworks.xstream - xstream - 1.4.3 - - + + uk.ac.shef.wit + simmetrics + 1.6.2 + + + com.thoughtworks.xstream + xstream + 1.4.3 + + - - com.google.guava - guava - + + com.google.guava + guava + - - org.aksw.jena-sparql-api - jena-sparql-api-core - - - slf4j-ext - org.slf4j - - - - - org.aksw.jena-sparql-api - jena-sparql-api-cache-h2 - + + org.aksw.jena-sparql-api + jena-sparql-api-core + + + slf4j-ext + org.slf4j + + + + + org.aksw.jena-sparql-api + jena-sparql-api-cache-h2 + org.apache.commons commons-math3 - + + + + + org.aksw + semlibsvm + + + + edu.berkeley.compbio + jlibsvm + + + + com.martiansoftware + jsap + 2.1 + + + + + org.reflections + reflections + 0.9.9-RC1 + + + - com.h2database - h2 + net.sf.jopt-simple + jopt-simple + test - - --> - - - org.aksw - semlibsvm + org.apache.commons + commons-email + 1.4 + test - org.reflections - reflections - 0.9.9-RC1 + com.panayotis.gnuplot + javaplot + 0.5.0 + test + diff --git a/components-core/src/main/java/org/dllearner/algorithms/CELOEWrapper.java b/components-core/src/main/java/org/dllearner/algorithms/CELOEWrapper.java index 2291345c7a..fc299f7da0 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/CELOEWrapper.java +++ b/components-core/src/main/java/org/dllearner/algorithms/CELOEWrapper.java @@ -1,13 +1,29 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.util.Set; -import java.util.SortedSet; - +import com.google.common.collect.Sets; +import com.hp.hpl.jena.query.ParameterizedSparqlString; +import com.hp.hpl.jena.query.QueryExecution; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.Statement; +import com.hp.hpl.jena.rdf.model.StmtIterator; import org.aksw.jena_sparql_api.pagination.core.QueryExecutionFactoryPaginated; import org.dllearner.algorithms.celoe.CELOE; import org.dllearner.core.AbstractAxiomLearningAlgorithm; @@ -21,30 +37,23 @@ import org.dllearner.reasoning.ClosedWorldReasoner; import org.dllearner.utilities.OwlApiJenaUtils; import org.dllearner.utilities.owl.OWLEntityTypeAdder; -import org.semanticweb.owlapi.io.RDFXMLOntologyFormat; -//import org.dllearner.utilities.OwlApiJenaUtils; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLClassAxiom; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyStorageException; - +import org.semanticweb.owlapi.formats.RDFXMLDocumentFormat; +import org.semanticweb.owlapi.model.*; import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl; -import com.google.common.collect.Sets; -import com.hp.hpl.jena.query.ParameterizedSparqlString; -import com.hp.hpl.jena.query.QueryExecution; -import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.Statement; -import com.hp.hpl.jena.rdf.model.StmtIterator; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.util.Set; +import java.util.SortedSet; + +//import org.dllearner.utilities.OwlApiJenaUtils; /** * A wrapper class for CELOE that allows for returning the result in forms of OWL axioms. * @author Lorenz Buehmann * */ +// not for conf public class CELOEWrapper extends AbstractAxiomLearningAlgorithm { private boolean equivalence = true; @@ -104,7 +113,7 @@ protected void learnAxioms() { OWLOntology fragment = buildFragment(posExamples, negExamples); try { - fragment.getOWLOntologyManager().saveOntology(fragment, new RDFXMLOntologyFormat(), new FileOutputStream("/tmp/ont.owl")); + fragment.getOWLOntologyManager().saveOntology(fragment, new RDFXMLDocumentFormat(), new FileOutputStream("/tmp/ont.owl")); } catch (OWLOntologyStorageException | FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); diff --git a/components-core/src/main/java/org/dllearner/algorithms/DisjointClassesLearner.java b/components-core/src/main/java/org/dllearner/algorithms/DisjointClassesLearner.java index b3c57e3025..2dc211fe84 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/DisjointClassesLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/DisjointClassesLearner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,11 +16,9 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms; import java.util.ArrayList; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -35,6 +33,8 @@ import org.dllearner.core.EvaluatedAxiom; import org.dllearner.core.EvaluatedDescription; import org.dllearner.core.Score; +import org.dllearner.core.annotations.Unused; +import org.dllearner.core.config.ConfigOption; import org.dllearner.core.owl.ClassHierarchy; import org.dllearner.kb.LocalModelBasedSparqlEndpointKS; import org.dllearner.kb.SparqlEndpointKS; @@ -57,9 +57,6 @@ import com.hp.hpl.jena.query.ResultSet; import com.hp.hpl.jena.query.ResultSetFactory; import com.hp.hpl.jena.query.ResultSetRewindable; -import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.ModelFactory; -import com.hp.hpl.jena.rdf.model.Resource; /** * Learns disjoint classes using SPARQL queries. @@ -82,13 +79,16 @@ public class DisjointClassesLearner extends AbstractAxiomLearningAlgorithm> currentlyBestEvaluatedDescriptions; private SortedSet subClasses; + @Unused private boolean useWordNetDistance = false; + @ConfigOption(description = "only keep most general classes in suggestions", defaultValue = "true") private boolean suggestMostGeneralClasses = true; + @ConfigOption(description = "include instance count / popularity when computing scores", defaultValue = "true") private boolean useClassPopularity = true; private Set allClasses; @@ -162,14 +162,14 @@ private void run() { int nrOfNegExamples = popularity - nrOfPosExamples; currentlyBestAxioms.add( - new EvaluatedAxiom( - df.getOWLDisjointClassesAxiom(entityToDescribe, cls), + new EvaluatedAxiom<>( + df.getOWLDisjointClassesAxiom(entityToDescribe, cls), new AxiomScore(score, score, nrOfPosExamples, nrOfNegExamples, useSampling))); } } /** - * In this method we try to compute the overlap with each property in one single SPARQL query. + * In this method we try to compute the overlap with each class in one single SPARQL query. * This method might be much slower as the query is much more complex. */ protected void runBatched() { @@ -198,20 +198,20 @@ protected void runBatched() { int nrOfNegExamples = popularity - nrOfPosExamples; currentlyBestAxioms.add( - new EvaluatedAxiom( - df.getOWLDisjointClassesAxiom(entityToDescribe, candidate), + new EvaluatedAxiom<>( + df.getOWLDisjointClassesAxiom(entityToDescribe, candidate), new AxiomScore(score, score, nrOfPosExamples, nrOfNegExamples, useSampling))); } } /** - * Returns the candidate properties for comparison. - * @return + * Returns the candidate classes for comparison. + * @return the candidate classes */ private SortedSet getCandidates(){ SortedSet candidates; - if (strictOWLMode) { + if (strictOWLMode) { candidates = reasoner.getOWLClasses(); } else { candidates = reasoner.getClasses(); @@ -225,7 +225,7 @@ private SortedSet getCandidates(){ return candidates; } - public double computeScore(int candidatePopularity, int popularity, int overlap){ + private double computeScore(int candidatePopularity, int popularity, int overlap){ // compute the estimated precision double precision = Heuristics.getConfidenceInterval95WaldAverage(candidatePopularity, overlap); @@ -254,6 +254,14 @@ public void setSuggestMostGeneralClasses(boolean suggestMostGeneralClasses) { this.suggestMostGeneralClasses = suggestMostGeneralClasses; } + public boolean isUseClassPopularity() { + return useClassPopularity; + } + + public void setUseClassPopularity(boolean useClassPopularity) { + this.useClassPopularity = useClassPopularity; + } + /* (non-Javadoc) * @see org.dllearner.core.AbstractAxiomLearningAlgorithm#getSampleQuery() */ @@ -284,131 +292,9 @@ protected void getExistingAxioms() { } } - private void runSingleQueryMode() { - //compute the overlap if exist - Map class2Overlap = new HashMap(); - String query = String.format("SELECT ?type (COUNT(*) AS ?cnt) WHERE {?s a <%s>. ?s a ?type.} GROUP BY ?type", - entityToDescribe.toStringID()); - ResultSet rs = executeSelectQuery(query); - QuerySolution qs; - while (rs.hasNext()) { - qs = rs.next(); - OWLClass cls = df.getOWLClass(IRI.create(qs.getResource("type").getURI())); - int cnt = qs.getLiteral("cnt").getInt(); - class2Overlap.put(cls, cnt); - } - //for each class in knowledge base - for (OWLClass cls : allClasses) { - //get the popularity - int otherPopularity = reasoner.getPopularity(cls); - if (otherPopularity == 0) {//skip empty properties - continue; - } - System.out.println(cls); - //get the overlap - int overlap = class2Overlap.containsKey(cls) ? class2Overlap.get(cls) : 0; - //compute the estimated precision - // compute the estimated precision - double precision = Heuristics.getConfidenceInterval95WaldAverage(otherPopularity, overlap); - - // compute the estimated recall - double recall = Heuristics.getConfidenceInterval95WaldAverage(popularity, overlap); - - // compute the final score - double score = 1 - Heuristics.getFScore(recall, precision); - - currentlyBestEvaluatedDescriptions.add(new EvaluatedDescription(cls, new AxiomScore(score))); - } - } - - private void runSPARQL1_0_Mode() { - Model model = ModelFactory.createDefaultModel(); - int limit = 1000; - int offset = 0; - String baseQuery = "CONSTRUCT {?s a <%s>. ?s a ?type.} WHERE {?s a <%s>. ?s a ?type.} LIMIT %d OFFSET %d"; - String query = String.format(baseQuery, entityToDescribe.toStringID(), entityToDescribe.toStringID(), limit, - offset); - Model newModel = executeConstructQuery(query); - Map result = new HashMap(); - OWLClass cls; - while (!terminationCriteriaSatisfied() && newModel.size() != 0) { - model.add(newModel); - //get total number of distinct instances - query = "SELECT (COUNT(DISTINCT ?s) AS ?count) WHERE {?s a ?type.}"; - ResultSet rs = executeSelectQuery(query, model); - int total = rs.next().getLiteral("count").getInt(); - - // get number of instances of s with - query = "SELECT ?type (COUNT(?s) AS ?count) WHERE {?s a ?type.}" + " GROUP BY ?type"; - rs = executeSelectQuery(query, model); - QuerySolution qs; - while (rs.hasNext()) { - qs = rs.next(); - if (qs.getResource("type") != null && !qs.getResource("type").isAnon()) { - cls = df.getOWLClass(IRI.create(qs.getResource("type").getURI())); - int newCnt = qs.getLiteral("count").getInt(); - result.put(cls, newCnt); - } - - } - - if (!result.isEmpty()) { - currentlyBestEvaluatedDescriptions = buildEvaluatedClassDescriptions(result, allClasses, total); - } - - offset += limit; - query = String.format(baseQuery, entityToDescribe.toStringID(), entityToDescribe.toStringID(), limit, offset); - newModel = executeConstructQuery(query); - } - - } - - private void runSPARQL1_1_Mode() { - int limit = 1000; - int offset = 0; - String queryTemplate = "PREFIX owl: SELECT ?type (COUNT(?s) AS ?count) WHERE {?s a ?type. ?type a owl:Class" - + "{SELECT ?s WHERE {?s a <%s>.} LIMIT %d OFFSET %d} " + "} GROUP BY ?type"; - String query; - Map result = new HashMap(); - OWLClass cls; - Integer oldCnt; - boolean repeat = true; - - while (!terminationCriteriaSatisfied() && repeat) { - query = String.format(queryTemplate, entityToDescribe, limit, offset); - ResultSet rs = executeSelectQuery(query); - QuerySolution qs; - repeat = false; - Resource res; - while (rs.hasNext()) { - qs = rs.next(); - res = qs.getResource("type"); - if (res != null && !res.isAnon()) { - cls = df.getOWLClass(IRI.create(qs.getResource("type").getURI())); - int newCnt = qs.getLiteral("count").getInt(); - oldCnt = result.get(cls); - if (oldCnt == null) { - oldCnt = Integer.valueOf(newCnt); - } else { - oldCnt += newCnt; - } - - result.put(cls, oldCnt); - repeat = true; - } - - } - if (!result.isEmpty()) { - currentlyBestEvaluatedDescriptions = buildEvaluatedClassDescriptions(result, allClasses, - result.get(entityToDescribe)); - offset += 1000; - } - } - } - @Override public List getCurrentlyBestDescriptions(int nrOfDescriptions) { - List bestDescriptions = new ArrayList(); + List bestDescriptions = new ArrayList<>(); for (EvaluatedDescription evDesc : getCurrentlyBestEvaluatedDescriptions(nrOfDescriptions)) { bestDescriptions.add(evDesc.getDescription()); } @@ -421,36 +307,6 @@ public List> getCurrentlyBestEva return currentlyBestEvaluatedDescriptions.subList(0, max); } - @Override - public List getCurrentlyBestAxioms(int nrOfAxioms) { - List bestAxioms = new ArrayList(); - - for (EvaluatedAxiom evAx : getCurrentlyBestEvaluatedAxioms(nrOfAxioms)) { - bestAxioms.add(evAx.getAxiom()); - } - - return bestAxioms; - } - - @Override - public List> getCurrentlyBestEvaluatedAxioms() { - return getCurrentlyBestEvaluatedAxioms(currentlyBestEvaluatedDescriptions.size()); - } - - @Override - public List> getCurrentlyBestEvaluatedAxioms(int nrOfAxioms) { - List> axioms = new ArrayList>(); - Set descriptions; - for (EvaluatedDescription ed : getCurrentlyBestEvaluatedDescriptions(nrOfAxioms)) { - descriptions = new TreeSet(); - descriptions.add(entityToDescribe); - descriptions.add(ed.getDescription()); - axioms.add(new EvaluatedAxiom(df.getOWLDisjointClassesAxiom(descriptions), - new AxiomScore(ed.getAccuracy()))); - } - return axioms; - } - private List> buildEvaluatedClassDescriptions(Map class2Count, Set allClasses, int total) { List> evalDescs = new ArrayList<>(); @@ -460,7 +316,7 @@ private List> buildEvaluatedClassDescripti class2Count.remove(entityToDescribe); //get complete disjoint classes - Set completeDisjointclasses = new TreeSet(allClasses); + Set completeDisjointclasses = new TreeSet<>(allClasses); completeDisjointclasses.removeAll(class2Count.keySet()); // we remove the asserted subclasses here @@ -507,7 +363,7 @@ private List> buildEvaluatedClassDescripti evalDescs.add(evalDesc); } - //secondly, create disjoint classexpressions with score 1 - (#occurence/#all) + //secondly, create disjoint class expressions with score 1 - (#occurence/#all) OWLClass cls; for (Entry entry : sortByValues(class2Count)) { cls = entry.getKey(); @@ -551,7 +407,7 @@ private void keepMostGeneralClasses(Set classes) { if (ks.isRemote()) { if (reasoner.isPrepared()) { ClassHierarchy h = reasoner.getClassHierarchy(); - for (OWLClass nc : new HashSet(classes)) { + for (OWLClass nc : new HashSet<>(classes)) { classes.removeAll(h.getSubClasses(nc)); } } @@ -561,14 +417,14 @@ private void keepMostGeneralClasses(Set classes) { // Set topClasses = new HashSet(); // for(OntClass cls : model.listOWLClasses().toSet()){ // Set superClasses = cls.listSuperClasses().toSet(); - // if(superClasses.isEmpty() || + // if(superClasses.isEmpty() || // (superClasses.size() == 1 && superClasses.contains(model.getOntClass(com.hp.hpl.jena.vocabulary.OWL.Thing.getURI())))){ // topClasses.add(df.getOWLClass(IRI.create(cls.getURI())); // } - // + // // } // classes.retainAll(topClasses); - for (OWLClass nc : new HashSet(classes)) {//System.out.print(nc + "::"); + for (OWLClass nc : new HashSet<>(classes)) {//System.out.print(nc + "::"); for (OntClass cls : model.getOntClass(nc.toStringID()).listSubClasses().toSet()) {//System.out.print(cls + "|"); classes.remove(df.getOWLClass(IRI.create(cls.getURI()))); } @@ -595,7 +451,7 @@ private void computeAllDisjointClassAxiomsOptimized() { } private Set computeDisjointessOfSiblings(OWLClass cls) { - Set evaluatedDescriptions = new HashSet(); + Set evaluatedDescriptions = new HashSet<>(); //get number of instances of A int instanceCountA = reasoner.getPopularity(cls); @@ -635,17 +491,15 @@ public EvaluatedAxiom computeDisjointess(OWLClass clsA, //if clsA = clsB if (clsA.equals(clsB)) { - return new EvaluatedAxiom(df.getOWLDisjointClassesAxiom(clsA, clsB), + return new EvaluatedAxiom<>(df.getOWLDisjointClassesAxiom(clsA, clsB), new AxiomScore(0d, 1d)); } - ; - //if the classes are connected via subsumption we assume that they are not disjoint + //if the classes are connected via subsumption we assume that they are not disjoint if (reasoner.isSuperClassOf(clsA, clsB) || reasoner.isSuperClassOf(clsB, clsA)) { - return new EvaluatedAxiom(df.getOWLDisjointClassesAxiom(clsA, clsB), + return new EvaluatedAxiom<>(df.getOWLDisjointClassesAxiom(clsA, clsB), new AxiomScore(0d, 1d)); } - ; double scoreValue = 0; @@ -672,11 +526,11 @@ public EvaluatedAxiom computeDisjointess(OWLClass clsA, AxiomScore score = new AxiomScore(scoreValue); - return new EvaluatedAxiom(df.getOWLDisjointClassesAxiom(clsA, clsB), score); + return new EvaluatedAxiom<>(df.getOWLDisjointClassesAxiom(clsA, clsB), score); } public Set> computeSchemaDisjointness() { - Set> axioms = new HashSet>(); + Set> axioms = new HashSet<>(); Set classes = reasoner.getOWLClasses("http://dbpedia.org/ontology/"); computeDisjointness(classes); @@ -693,7 +547,7 @@ public Set> computeSchemaDisjointness() } public Set> computeDisjointness(Set classes) { - Set> axioms = new HashSet>(); + Set> axioms = new HashSet<>(); for (OWLClass clsA : classes) { for (OWLClass clsB : classes) { @@ -705,7 +559,7 @@ public Set> computeDisjointness(Set asOWLClasses(Set descriptions) { - Set classes = new TreeSet(); + Set classes = new TreeSet<>(); for (OWLClassExpression description : descriptions) { if (!description.isAnonymous()) { classes.add(description.asOWLClass()); @@ -715,10 +569,16 @@ public static Set asOWLClasses(Set descriptions) { } public static void main(String[] args) throws Exception { - DisjointClassesLearner la = new DisjointClassesLearner(new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpedia())); + SparqlEndpointKS ks = new SparqlEndpointKS(SparqlEndpoint.create("http://sake.informatik.uni-leipzig.de:8890/sparql", "http://dbpedia.org")); + ks.init(); + + DisjointClassesLearner la = new DisjointClassesLearner(ks); la.setEntityToDescribe(new OWLClassImpl(IRI.create("http://dbpedia.org/ontology/Actor"))); la.setUseSampling(false); la.init(); + la.start(); + + la.getCurrentlyBestAxioms(10); } } \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/algorithms/NaiveALLearner.java b/components-core/src/main/java/org/dllearner/algorithms/NaiveALLearner.java index 3b0e5e51fe..596d46fb0a 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/NaiveALLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/NaiveALLearner.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms; import java.util.ArrayList; @@ -12,6 +30,7 @@ import org.dllearner.core.ComponentAnn; import org.dllearner.core.ComponentInitException; import org.dllearner.core.EvaluatedDescription; +import org.dllearner.core.config.ConfigOption; import org.dllearner.core.owl.OWLObjectIntersectionOfImplExt; import org.dllearner.learningproblems.EvaluatedDescriptionPosNeg; import org.dllearner.learningproblems.ScorePosNeg; @@ -28,6 +47,7 @@ public class NaiveALLearner extends AbstractCELA{ private Map> generatedDescriptions; private boolean running = false; + @ConfigOption(defaultValue = "4", description = "maximum length of class expression") private int maxLength = 4; private OWLClassExpression bestDescription; @@ -60,7 +80,7 @@ public void start() { @Override public void init() throws ComponentInitException { - generatedDescriptions = new HashMap>(); + generatedDescriptions = new HashMap<>(); // start with owl:Thing bestDescription = OWL_THING; @@ -104,7 +124,7 @@ public EvaluatedDescription getCurrentlyBestEvaluatedDescription() { * - all concepts of length `length`-2 are extended by limited * existential quantification/value restriction * - * @param length + * @param length the length */ private void generateDescriptions(int length) { generatedDescriptions.put(length, new ArrayList()); diff --git a/components-core/src/main/java/org/dllearner/algorithms/SimpleSubclassLearner.java b/components-core/src/main/java/org/dllearner/algorithms/SimpleSubclassLearner.java index 33b5a7b975..be54c7c17e 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/SimpleSubclassLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/SimpleSubclassLearner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms; import java.util.ArrayList; @@ -70,7 +69,7 @@ public List> getCurrentlyBestEvaluatedAxioms( @Override public List getCurrentlyBestDescriptions(int nrOfDescriptions) { - List bestDescriptions = new ArrayList(); + List bestDescriptions = new ArrayList<>(); for (EvaluatedDescription evDesc : getCurrentlyBestEvaluatedDescriptions(nrOfDescriptions)) { bestDescriptions.add(evDesc.getDescription()); } @@ -85,7 +84,7 @@ public List> getCurrentlyBestEva @Override public List getCurrentlyBestAxioms(int nrOfAxioms) { - List bestAxioms = new ArrayList(); + List bestAxioms = new ArrayList<>(); for (EvaluatedAxiom evAx : getCurrentlyBestEvaluatedAxioms(nrOfAxioms)) { bestAxioms.add(evAx.getAxiom()); @@ -96,12 +95,12 @@ public List getCurrentlyBestAxioms(int nrOfAxioms) { @Override public List> getCurrentlyBestEvaluatedAxioms(int nrOfAxioms) { - currentlyBestAxioms = new TreeSet>(); + currentlyBestAxioms = new TreeSet<>(); for (EvaluatedDescription ed : getCurrentlyBestEvaluatedDescriptions(nrOfAxioms)) { - currentlyBestAxioms.add(new EvaluatedAxiom(df.getOWLSubClassOfAxiom(entityToDescribe, + currentlyBestAxioms.add(new EvaluatedAxiom<>(df.getOWLSubClassOfAxiom(entityToDescribe, ed.getDescription()), new AxiomScore(ed.getAccuracy()))); } - return new ArrayList>(currentlyBestAxioms); + return new ArrayList<>(currentlyBestAxioms); } @Override @@ -129,7 +128,7 @@ protected void getExistingAxioms() { //get existing super classes SortedSet existingSuperClasses = reasoner.getSuperClasses(entityToDescribe); if (!existingSuperClasses.isEmpty()) { - SortedSet inferredSuperClasses = new TreeSet(); + SortedSet inferredSuperClasses = new TreeSet<>(); for (OWLClassExpression assertedSup : existingSuperClasses) { if (reasoner.isPrepared()) { if (reasoner.getClassHierarchy().contains(assertedSup)) { @@ -214,7 +213,7 @@ private boolean addIndividualsWithTypes(Map(); + types = new TreeSet<>(); ind2Types.put(ind, types); } types.add(newType); @@ -235,7 +234,7 @@ private boolean addIndividualsWithTypes(Map> individual2Types) { currentlyBestEvaluatedDescriptions.clear(); - Map result = new HashMap(); + Map result = new HashMap<>(); for (Entry> entry : individual2Types.entrySet()) { for (OWLClassExpression nc : entry.getValue()) { Integer cnt = result.get(nc); diff --git a/components-core/src/main/java/org/dllearner/algorithms/celoe/CELOE.java b/components-core/src/main/java/org/dllearner/algorithms/celoe/CELOE.java index f28086ae51..08e9663ded 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/celoe/CELOE.java +++ b/components-core/src/main/java/org/dllearner/algorithms/celoe/CELOE.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,27 +16,12 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.celoe; -import java.io.File; -import java.util.Collection; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; - -import org.dllearner.core.AbstractCELA; -import org.dllearner.core.AbstractClassExpressionLearningProblem; -import org.dllearner.core.AbstractHeuristic; -import org.dllearner.core.AbstractKnowledgeSource; -import org.dllearner.core.AbstractReasonerComponent; -import org.dllearner.core.ComponentAnn; -import org.dllearner.core.ComponentInitException; -import org.dllearner.core.EvaluatedDescription; -import org.dllearner.core.Score; +import com.google.common.collect.Sets; +import com.jamonapi.Monitor; +import com.jamonapi.MonitorFactory; +import org.dllearner.core.*; import org.dllearner.core.config.ConfigOption; import org.dllearner.core.owl.ClassHierarchy; import org.dllearner.core.owl.DatatypePropertyHierarchy; @@ -50,70 +35,53 @@ import org.dllearner.reasoning.OWLAPIReasoner; import org.dllearner.reasoning.ReasonerImplementation; import org.dllearner.reasoning.SPARQLReasoner; -import org.dllearner.refinementoperators.CustomHierarchyRefinementOperator; -import org.dllearner.refinementoperators.CustomStartRefinementOperator; -import org.dllearner.refinementoperators.LengthLimitedRefinementOperator; -import org.dllearner.refinementoperators.OperatorInverter; -import org.dllearner.refinementoperators.ReasoningBasedRefinementOperator; -import org.dllearner.refinementoperators.RhoDRDown; +import org.dllearner.refinementoperators.*; import org.dllearner.utilities.Files; import org.dllearner.utilities.Helper; import org.dllearner.utilities.OWLAPIUtils; -import org.dllearner.utilities.owl.ConceptTransformation; -import org.dllearner.utilities.owl.EvaluatedDescriptionSet; -import org.dllearner.utilities.owl.OWLAPIRenderers; -import org.dllearner.utilities.owl.OWLClassExpressionMinimizer; -import org.dllearner.utilities.owl.OWLClassExpressionUtils; -import org.dllearner.utilities.owl.PropertyContext; +import org.dllearner.utilities.TreeUtils; +import org.dllearner.utilities.datastructures.SearchTree; +import org.dllearner.utilities.owl.*; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLNaryBooleanClassExpression; -import org.semanticweb.owlapi.model.OWLOntology; +import org.semanticweb.owlapi.model.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.Marker; import org.slf4j.MarkerFactory; import org.springframework.beans.factory.annotation.Autowired; - import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl; import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; -import com.google.common.collect.Sets; -import com.jamonapi.Monitor; -import com.jamonapi.MonitorFactory; +import java.io.File; +import java.util.*; /** * The CELOE (Class Expression Learner for Ontology Engineering) algorithm. * It adapts and extends the standard supervised learning algorithm for the - * ontology engineering use case. + * ontology engineering use case. * * @author Jens Lehmann * */ +@SuppressWarnings("CloneDoesntCallSuperClone") @ComponentAnn(name="CELOE", shortName="celoe", version=1.0, description="CELOE is an adapted and extended version of the OCEL algorithm applied for the ontology engineering use case. See http://jens-lehmann.org/files/2011/celoe.pdf for reference.") public class CELOE extends AbstractCELA implements Cloneable{ - private static Logger logger = LoggerFactory.getLogger(CELOE.class); - private static Marker sparql_debug = MarkerFactory.getMarker("SD"); + private static final Logger logger = LoggerFactory.getLogger(CELOE.class); + private static final Marker sparql_debug = MarkerFactory.getMarker("SD"); private boolean isRunning = false; - private boolean stop = false; + private boolean stop = false; // private OEHeuristicStable heuristicStable = new OEHeuristicStable(); // private OEHeuristicRuntime heuristicRuntime = new OEHeuristicRuntime(); + @ConfigOption(description = "the refinement operator instance to use") private LengthLimitedRefinementOperator operator; - - // all nodes in the search tree (used for selecting most promising node) - private TreeSet nodes; + + private SearchTree searchTree; @ConfigOption(name="heuristic", defaultValue="celoe_heuristic") private AbstractHeuristic heuristic; // = new OEHeuristicRuntime(); - // root of search tree - private OENode startNode; // the class with which we start the refinement process @ConfigOption( name = "startClass", @@ -146,7 +114,7 @@ public class CELOE extends AbstractCELA implements Cloneable{ // important parameters (non-config options but internal) private double noise; - private boolean filterFollowsFromKB = false; + private boolean filterFollowsFromKB = false; // less important parameters // forces that one solution cannot be subexpression of another expression; this option is useful to get diversity @@ -176,7 +144,7 @@ public class CELOE extends AbstractCELA implements Cloneable{ @ConfigOption(name = "replaceSearchTree", defaultValue="false", description="specifies whether to replace the search tree in the log file after each run or append the new search tree") private boolean replaceSearchTree = false; - @ConfigOption(name = "maxNrOfResults", defaultValue="10", description="Sets the maximum number of results one is interested in. (Setting this to a lower value may increase performance as the learning algorithm has to store/evaluate/beautify less descriptions).") + @ConfigOption(name = "maxNrOfResults", defaultValue="10", description="Sets the maximum number of results one is interested in. (Setting this to a lower value may increase performance as the learning algorithm has to store/evaluate/beautify less descriptions).") private int maxNrOfResults = 10; @ConfigOption(name = "noisePercentage", defaultValue="0.0", description="the (approximated) percentage of noise within the examples") @@ -194,9 +162,6 @@ public class CELOE extends AbstractCELA implements Cloneable{ @ConfigOption(name = "maxClassExpressionTestsAfterImprovement", defaultValue="0", description = "The maximum number of candidate hypothesis the algorithm is allowed after an improvement in accuracy (0 = no limit). The algorithm will stop afterwards. (The real number of tests can be slightly higher, because this criterion usually won't be checked after each single test.)") private int maxClassExpressionTestsAfterImprovement = 0; - @ConfigOption(defaultValue = "10", name = "maxExecutionTimeInSeconds", description = "maximum execution of the algorithm in seconds") - private int maxExecutionTimeInSeconds = 10; - @ConfigOption(defaultValue = "0", name = "maxExecutionTimeInSecondsAfterImprovement", description = "maximum execution of the algorithm in seconds") private int maxExecutionTimeInSecondsAfterImprovement = 0; @@ -214,6 +179,7 @@ public class CELOE extends AbstractCELA implements Cloneable{ @SuppressWarnings("unused") private long timeLastImprovement = 0; + @ConfigOption(defaultValue = "false", description = "whether to try and refine solutions which already have accuracy value of 1") private boolean expandAccuracy100Nodes = false; private double currentHighestAccuracy; @@ -239,12 +205,12 @@ public CELOE(CELOE celoe){ setMaxClassExpressionTests(celoe.maxClassExpressionTests); setMaxClassExpressionTestsAfterImprovement(celoe.maxClassExpressionTestsAfterImprovement); setMaxDepth(celoe.maxDepth); - setMaxExecutionTimeInSeconds(celoe.maxExecutionTimeInSeconds); + setMaxExecutionTimeInSeconds(celoe.getMaxExecutionTimeInSeconds()); setMaxExecutionTimeInSecondsAfterImprovement(celoe.maxExecutionTimeInSecondsAfterImprovement); setMaxNrOfResults(celoe.maxNrOfResults); setNoisePercentage(celoe.noisePercentage); - RhoDRDown op = new RhoDRDown((RhoDRDown)celoe.operator); + LengthLimitedRefinementOperator op = new RhoDRDown((RhoDRDown)celoe.operator); try { op.init(); } catch (ComponentInitException e) { @@ -269,7 +235,7 @@ public CELOE(AbstractClassExpressionLearningProblem problem, AbstractReasonerCom } public static Collection> supportedLearningProblems() { - Collection> problems = new LinkedList>(); + Collection> problems = new LinkedList<>(); problems.add(AbstractClassExpressionLearningProblem.class); return problems; } @@ -277,22 +243,17 @@ public static Collection public static String getName() { return "CELOE"; } - + @Override public void init() throws ComponentInitException { baseURI = reasoner.getBaseURI(); - prefixes = reasoner.getPrefixes(); + prefixes = reasoner.getPrefixes(); if(maxExecutionTimeInSeconds != 0 && maxExecutionTimeInSecondsAfterImprovement != 0) { maxExecutionTimeInSeconds = Math.min(maxExecutionTimeInSeconds, maxExecutionTimeInSecondsAfterImprovement); } - // compute used concepts/roles from allowed/ignored - // concepts/roles - - - // copy class hierarchy and modify it such that each class is only - // reachable via a single path + // TODO add comment ClassHierarchy classHierarchy = initClassHierarchy(); ObjectPropertyHierarchy objectPropertyHierarchy = initObjectPropertyHierarchy(); DatatypePropertyHierarchy datatypePropertyHierarchy = initDataPropertyHierarchy(); @@ -300,13 +261,22 @@ public void init() throws ComponentInitException { // if no one injected a heuristic, we use a default one if(heuristic == null) { heuristic = new OEHeuristicRuntime(); + heuristic.init(); } minimizer = new OWLClassExpressionMinimizer(dataFactory, reasoner); + if (writeSearchTree) { + File f = new File(searchTreeFile); + if (f.getParentFile() != null) { + f.getParentFile().mkdirs(); + } + Files.clearFile(f); + } + // start at owl:Thing by default - if(startClass == null) { - startClass = dataFactory.getOWLThing(); + if (startClass == null) { + startClass = computeStartClass(); } else { try { this.startClass = OWLAPIUtils.classExpressionPropertyExpander(this.startClass, reasoner, dataFactory); @@ -314,34 +284,7 @@ public void init() throws ComponentInitException { logger.warn("Error parsing start class.", e); logger.warn("Using owl:Thing instead."); this.startClass = dataFactory.getOWLThing(); - } - } - - // create a refinement operator and pass all configuration - // variables to it - if(operator == null) { - // we use a default operator and inject the class hierarchy for now - operator = new RhoDRDown(); - if(operator instanceof CustomStartRefinementOperator) { - ((CustomStartRefinementOperator)operator).setStartClass(startClass); - } - if(operator instanceof ReasoningBasedRefinementOperator) { - ((ReasoningBasedRefinementOperator)operator).setReasoner(reasoner); - } - operator.init(); - } - if(operator instanceof CustomHierarchyRefinementOperator) { - ((CustomHierarchyRefinementOperator)operator).setClassHierarchy(classHierarchy); - ((CustomHierarchyRefinementOperator)operator).setObjectPropertyHierarchy(objectPropertyHierarchy); - ((CustomHierarchyRefinementOperator)operator).setDataPropertyHierarchy(datatypePropertyHierarchy); - } - - if(writeSearchTree) { - File f = new File(searchTreeFile ); - if(f.getParentFile() != null){ - f.getParentFile().mkdirs(); } - Files.clearFile(f); } bestEvaluatedDescriptions = new EvaluatedDescriptionSet(maxNrOfResults); @@ -350,18 +293,11 @@ public void init() throws ComponentInitException { // we put important parameters in class variables noise = noisePercentage/100d; -// System.out.println("noise " + noise); -// maxDepth = configurator.getMaxDepth(); + // (filterFollowsFromKB is automatically set to false if the problem // is not a class learning problem filterFollowsFromKB = filterDescriptionsFollowingFromKB && isClassLearningProblem; -// Set concepts = operator.refine(Thing.instance, 5); -// for(OWLClassExpression concept : concepts) { -// System.out.println(concept); -// } -// System.out.println("refinements of thing: " + concepts.size()); - // actions specific to ontology engineering if(isClassLearningProblem) { ClassLearningProblem problem = (ClassLearningProblem) learningProblem; @@ -369,10 +305,100 @@ public void init() throws ComponentInitException { isEquivalenceProblem = problem.isEquivalenceProblem(); examples = reasoner.getIndividuals(classToDescribe); + } else if(learningProblem instanceof PosOnlyLP) { + examples = ((PosOnlyLP)learningProblem).getPositiveExamples(); + } else if(learningProblem instanceof PosNegLP) { + examples = Sets.union(((PosNegLP)learningProblem).getPositiveExamples(),((PosNegLP)learningProblem).getNegativeExamples()); + } + + // create a refinement operator and pass all configuration + // variables to it + if (operator == null) { + // we use a default operator and inject the class hierarchy for now + operator = new RhoDRDown(); + ((CustomStartRefinementOperator) operator).setStartClass(startClass); + ((ReasoningBasedRefinementOperator) operator).setReasoner(reasoner); + operator.init(); + } + if (operator instanceof CustomHierarchyRefinementOperator) { + ((CustomHierarchyRefinementOperator) operator).setClassHierarchy(classHierarchy); + ((CustomHierarchyRefinementOperator) operator).setObjectPropertyHierarchy(objectPropertyHierarchy); + ((CustomHierarchyRefinementOperator) operator).setDataPropertyHierarchy(datatypePropertyHierarchy); + } + } + + @Override + public void start() { + stop = false; + isRunning = true; + reset(); + nanoStartTime = System.nanoTime(); + + currentHighestAccuracy = 0.0; + OENode nextNode; + + logger.info("start class:" + startClass); + addNode(startClass, null); + + while (!terminationCriteriaSatisfied()) { + showIfBetterSolutionsFound(); + + // chose best node according to heuristics + nextNode = getNextNodeToExpand(); + int horizExp = nextNode.getHorizontalExpansion(); + + // apply refinement operator + TreeSet refinements = refineNode(nextNode); + + while(!refinements.isEmpty() && !terminationCriteriaSatisfied()) { + // pick element from set + OWLClassExpression refinement = refinements.pollFirst(); + + // get length of class expression + int length = OWLClassExpressionUtils.getLength(refinement); + + // we ignore all refinements with lower length and too high depth + // (this also avoids duplicate node children) + if(length > horizExp && OWLClassExpressionUtils.getDepth(refinement) <= maxDepth) { + // add node to search tree + addNode(refinement, nextNode); + } + } + + showIfBetterSolutionsFound(); - // start class: intersection of super classes for definitions (since it needs to - // capture all instances), but owl:Thing for learning subclasses (since it is - // superfluous to add super classes in this case) + // update the global min and max horizontal expansion values + updateMinMaxHorizExp(nextNode); + + // write the search tree (if configured) + if (writeSearchTree) { + writeSearchTree(refinements); + } + } + + if(singleSuggestionMode) { + bestEvaluatedDescriptions.add(bestDescription, bestAccuracy, learningProblem); + } + + // print some stats + printAlgorithmRunStats(); + + // print solution(s) + logger.info("solutions:\n" + getSolutionString()); + + isRunning = false; + } + + /* + * Compute the start class in the search space from which the refinement will start. + * We use the intersection of super classes for definitions (since it needs to + * capture all instances), but owl:Thing for learning subclasses (since it is + * superfluous to add super classes in this case) + */ + private OWLClassExpression computeStartClass() { + OWLClassExpression startClass = dataFactory.getOWLThing(); + + if(isClassLearningProblem) { if(isEquivalenceProblem) { Set existingDefinitions = reasoner.getAssertedDefinitions(classToDescribe); if(reuseExistingDescription && (existingDefinitions.size() > 0)) { @@ -389,13 +415,13 @@ public void init() throws ComponentInitException { } } - LinkedList startClassCandidates = new LinkedList(); + LinkedList startClassCandidates = new LinkedList<>(); startClassCandidates.add(existingDefinition); // hack for RhoDRDown if(operator instanceof RhoDRDown) { ((RhoDRDown)operator).setDropDisjuncts(true); } - LengthLimitedRefinementOperator upwardOperator = (LengthLimitedRefinementOperator) new OperatorInverter(operator); + LengthLimitedRefinementOperator upwardOperator = new OperatorInverter(operator); // use upward refinement until we find an appropriate start class boolean startClassFound = false; @@ -406,7 +432,7 @@ public void init() throws ComponentInitException { // add upward refinements to list Set refinements = upwardOperator.refine(candidate, OWLClassExpressionUtils.getLength(candidate)); // System.out.println("ref: " + refinements); - LinkedList refinementList = new LinkedList(refinements); + LinkedList refinementList = new LinkedList<>(refinements); // Collections.reverse(refinementList); // System.out.println("list: " + refinementList); startClassCandidates.addAll(refinementList); @@ -423,10 +449,6 @@ public void init() throws ComponentInitException { logger.info("Generalised existing class expression " + OWLAPIRenderers.toManchesterOWLSyntax(existingDefinition) + " to " + OWLAPIRenderers.toManchesterOWLSyntax(startClass) + ", which is used as start class for the learning algorithm."); } -// System.out.println("start class: " + startClass); -// System.out.println("existing def: " + existingDefinition); -// System.out.println(reasoner.getIndividuals(existingDefinition)); - if(operator instanceof RhoDRDown) { ((RhoDRDown)operator).setDropDisjuncts(false); } @@ -441,92 +463,27 @@ public void init() throws ComponentInitException { startClass = dataFactory.getOWLThing(); logger.warn(classToDescribe + " is equivalent to owl:Thing. Usually, it is not " + "sensible to learn a class expression in this case."); - } - } - } - } else if(learningProblem instanceof PosOnlyLP) { - examples = ((PosOnlyLP)learningProblem).getPositiveExamples(); - } else if(learningProblem instanceof PosNegLP) { - examples = Helper.union(((PosNegLP)learningProblem).getPositiveExamples(),((PosNegLP)learningProblem).getNegativeExamples()); - } - } - - @Override - public void start() { - stop = false; - isRunning = true; - reset(); - nanoStartTime = System.nanoTime(); - - currentHighestAccuracy = 0.0; - OENode nextNode; - - logger.info("start class:" + startClass); - addNode(startClass, null); - - while (!terminationCriteriaSatisfied()) { - showIfBetterSolutionsFound(); - - // chose best node according to heuristics - nextNode = getNextNodeToExpand(); - int horizExp = nextNode.getHorizontalExpansion(); - - // apply refinement operator - TreeSet refinements = refineNode(nextNode); - - while(!refinements.isEmpty() && !terminationCriteriaSatisfied()) { - // pick element from set - OWLClassExpression refinement = refinements.pollFirst(); - - // get length of class expression - int length = OWLClassExpressionUtils.getLength(refinement); - - // we ignore all refinements with lower length and too high depth - // (this also avoids duplicate node children) - if(length > horizExp && OWLClassExpressionUtils.getDepth(refinement) <= maxDepth) { - // add node to search tree - addNode(refinement, nextNode); + } } } - - showIfBetterSolutionsFound(); - - // update the global min and max horizontal expansion values - updateMinMaxHorizExp(nextNode); - - // write the search tree (if configured) - if (writeSearchTree) { - writeSearchTree(refinements); - } } - - if(singleSuggestionMode) { - bestEvaluatedDescriptions.add(bestDescription, bestAccuracy, learningProblem); - } - - // print some stats - printAlgorithmRunStats(); - - // print solution(s) - logger.info("solutions:\n" + getSolutionString()); - - isRunning = false; + return startClass; } private OENode getNextNodeToExpand() { // we expand the best node of those, which have not achieved 100% accuracy // already and have a horizontal expansion equal to their length // (rationale: further extension is likely to add irrelevant syntactical constructs) - Iterator it = nodes.descendingIterator(); - if (logger.isDebugEnabled()) { - for (OENode N:nodes) { - logger.debug(sparql_debug,"`getnext:"+N); + Iterator it = searchTree.descendingIterator(); + if (logger.isTraceEnabled()) { + for (OENode N:searchTree.getNodeSet()) { + logger.trace(sparql_debug,"`getnext:"+N); } } while(it.hasNext()) { OENode node = it.next(); - logger.debug(sparql_debug,"``"+node+node.getAccuracy()); + logger.trace(sparql_debug,"``"+node+node.getAccuracy()); if (isExpandAccuracy100Nodes() && node.getHorizontalExpansion() < OWLClassExpressionUtils.getLength(node.getDescription())) { return node; } else { @@ -538,24 +495,24 @@ private OENode getNextNodeToExpand() { // this should practically never be called, since for any reasonable learning // task, we will always have at least one node with less than 100% accuracy - return null;//nodes.last(); + throw new RuntimeException("CELOE could not find any node with lesser accuracy."); } // expand node horizontically private TreeSet refineNode(OENode node) { - logger.debug(sparql_debug,"REFINE NODE " + node); + logger.trace(sparql_debug,"REFINE NODE " + node); MonitorFactory.getTimeMonitor("refineNode").start(); // we have to remove and add the node since its heuristic evaluation changes through the expansion // (you *must not* include any criteria in the heuristic which are modified outside of this method, // otherwise you may see rarely occurring but critical false ordering in the nodes set) - nodes.remove(node); + searchTree.updatePrepare(node); int horizExp = node.getHorizontalExpansion(); TreeSet refinements = (TreeSet) operator.refine(node.getDescription(), horizExp+1); // System.out.println("refinements: " + refinements); node.incHorizontalExpansion(); node.setRefinementCount(refinements.size()); // System.out.println("refined node: " + node); - nodes.add(node); + searchTree.updateDone(node); MonitorFactory.getTimeMonitor("refineNode").stop(); return refinements; } @@ -566,33 +523,33 @@ private TreeSet refineNode(OENode node) { */ private boolean addNode(OWLClassExpression description, OENode parentNode) { String sparql_debug_out = ""; - if (logger.isDebugEnabled()) sparql_debug_out = "DESC: " + description; + if (logger.isTraceEnabled()) sparql_debug_out = "DESC: " + description; MonitorFactory.getTimeMonitor("addNode").start(); // redundancy check (return if redundant) boolean nonRedundant = descriptions.add(description); if(!nonRedundant) { - logger.debug(sparql_debug, sparql_debug_out + "REDUNDANT"); + logger.trace(sparql_debug, sparql_debug_out + "REDUNDANT"); return false; } // check whether the class expression is allowed if(!isDescriptionAllowed(description, parentNode)) { - logger.debug(sparql_debug, sparql_debug_out + "NOT ALLOWED"); + logger.trace(sparql_debug, sparql_debug_out + "NOT ALLOWED"); return false; } // quality of class expression (return if too weak) Monitor mon = MonitorFactory.start("lp"); - logger.debug(sparql_debug, sparql_debug_out); + logger.trace(sparql_debug, sparql_debug_out); double accuracy = learningProblem.getAccuracyOrTooWeak(description, noise); - logger.debug(sparql_debug, "`acc:"+accuracy); + logger.trace(sparql_debug, "`acc:"+accuracy); mon.stop(); // issue a warning if accuracy is not between 0 and 1 or -1 (too weak) if(accuracy > 1.0 || (accuracy < 0.0 && accuracy != -1)) { - logger.warn("Invalid accuracy value " + accuracy + " for class expression " + description + ". This could be caused by a bug in the heuristic measure and should be reported to the DL-Learner bug tracker."); - System.exit(0); + throw new RuntimeException("Invalid accuracy value " + accuracy + " for class expression " + description + + ". This could be caused by a bug in the heuristic measure and should be reported to the DL-Learner bug tracker."); } expressionTests++; @@ -602,15 +559,8 @@ private boolean addNode(OWLClassExpression description, OENode parentNode) { return false; } - OENode node = new OENode(parentNode, description, accuracy); - - // link to parent (unless start node) - if(parentNode == null) { - startNode = node; - } else { - parentNode.addChild(node); - } - nodes.add(node); + OENode node = new OENode(description, accuracy); + searchTree.addNode(parentNode, node); // in some cases (e.g. mutation) fully evaluating even a single class expression is too expensive // due to the high number of examples -- so we just stick to the approximate accuracy @@ -618,37 +568,35 @@ private boolean addNode(OWLClassExpression description, OENode parentNode) { if(accuracy > bestAccuracy) { bestAccuracy = accuracy; bestDescription = description; - logger.info("more accurate (" + dfPercent.format(bestAccuracy) + ") class expression found: " + descriptionToString(bestDescription)); // + getTemporaryString(bestDescription)); + logger.info("more accurate (" + dfPercent.format(bestAccuracy) + ") class expression found: " + descriptionToString(bestDescription)); // + getTemporaryString(bestDescription)); } return true; - } + } // maybe add to best descriptions (method keeps set size fixed); // we need to make sure that this does not get called more often than // necessary since rewriting is expensive boolean isCandidate = !bestEvaluatedDescriptions.isFull(); if(!isCandidate) { - EvaluatedDescription worst = bestEvaluatedDescriptions.getWorst(); + EvaluatedDescription worst = bestEvaluatedDescriptions.getWorst(); double accThreshold = worst.getAccuracy(); - isCandidate = + isCandidate = (accuracy > accThreshold || (accuracy >= accThreshold && OWLClassExpressionUtils.getLength(description) < worst.getDescriptionLength())); } if(isCandidate) { OWLClassExpression niceDescription = rewrite(node.getExpression()); - ConceptTransformation.transformToOrderedForm(niceDescription); - + if(niceDescription.equals(classToDescribe)) { return false; } -// System.err.println(node);System.out.println(niceDescription); if(!isDescriptionAllowed(niceDescription, node)) { return false; } - // another test: none of the other suggested descriptions should be + // another test: none of the other suggested descriptions should be // a subdescription of this one unless accuracy is different // => comment: on the one hand, this appears to be too strict, because once A is a solution then everything containing // A is not a candidate; on the other hand this suppresses many meaningless extensions of A @@ -660,7 +608,7 @@ private boolean addNode(OWLClassExpression description, OENode parentNode) { shorterDescriptionExists = true; break; } - } + } } // System.out.println("shorter description? " + shorterDescriptionExists + " nice: " + niceDescription); @@ -680,7 +628,7 @@ private boolean addNode(OWLClassExpression description, OENode parentNode) { } return true; - } + } // checks whether the class expression is allowed private boolean isDescriptionAllowed(OWLClassExpression description, OENode parentNode) { @@ -696,7 +644,7 @@ private boolean isDescriptionAllowed(OWLClassExpression description, OENode pare } else { // none of the superclasses of the class to learn must appear on the // outermost property level - TreeSet toTest = new TreeSet(); + TreeSet toTest = new TreeSet<>(); toTest.add(classToDescribe); while(!toTest.isEmpty()) { OWLClassExpression d = toTest.pollFirst(); @@ -705,13 +653,13 @@ private boolean isDescriptionAllowed(OWLClassExpression description, OENode pare } toTest.addAll(reasoner.getClassHierarchy().getSuperClasses(d)); } - } + } } else if (learningProblem instanceof ClassAsInstanceLearningProblem) { return true; } // perform forall sanity tests - if (parentNode != null && + if (parentNode != null && (ConceptTransformation.getForallOccurences(description) > ConceptTransformation.getForallOccurences(parentNode.getDescription()))) { // we have an additional \forall construct, so we now fetch the contexts // in which it occurs @@ -745,7 +693,7 @@ private boolean isDescriptionAllowed(OWLClassExpression description, OENode pare return false; } } - } + } // we do not want to have negations of sibling classes on the outermost level // (they are expressed more naturally by saying that the siblings are disjoint, @@ -759,14 +707,10 @@ private boolean isDescriptionAllowed(OWLClassExpression description, OENode pare // determine whether a named class occurs on the outermost level, i.e. property depth 0 // (it can still be at higher depth, e.g. if intersections are nested in unions) private boolean occursOnFirstLevel(OWLClassExpression description, OWLClassExpression cls) { - if(cls.isOWLThing()) { - return false; - } - return (description instanceof OWLNaryBooleanClassExpression && - ((OWLNaryBooleanClassExpression)description).getOperands().contains(cls)); + return !cls.isOWLThing() && (description instanceof OWLNaryBooleanClassExpression && ((OWLNaryBooleanClassExpression) description).getOperands().contains(cls)); // return description.containsConjunct(cls) || // (description instanceof OWLObjectUnionOf && ((OWLObjectUnionOf) description).getOperands().contains(cls)); - } + } // determine whether a named class occurs on the outermost level, i.e. property depth 0 // (it can still be at higher depth, e.g. if intersections are nested in unions) @@ -774,9 +718,9 @@ private boolean occursOnSecondLevel(OWLClassExpression description, OWLClassExpr // SortedSet superClasses = reasoner.getSuperClasses(cls); // if(description instanceof OWLObjectIntersectionOf) { // List operands = ((OWLObjectIntersectionOf) description).getOperandsAsList(); -// +// // for (OWLClassExpression op : operands) { -// if(superClasses.contains(op) || +// if(superClasses.contains(op) || // (op instanceof OWLObjectUnionOf && !Sets.intersection(((OWLObjectUnionOf)op).getOperands(),superClasses).isEmpty())) { // for (OWLClassExpression op2 : operands) { // if((op2 instanceof OWLObjectUnionOf && ((OWLObjectUnionOf)op2).getOperands().contains(cls))) { @@ -785,7 +729,7 @@ private boolean occursOnSecondLevel(OWLClassExpression description, OWLClassExpr // } // } // } -// +// // for (OWLClassExpression op1 : operands) { // for (OWLClassExpression op2 : operands) { // if(!op1.isAnonymous() && op2 instanceof OWLObjectUnionOf) { @@ -809,12 +753,12 @@ private boolean occursOnSecondLevel(OWLClassExpression description, OWLClassExpr } private boolean terminationCriteriaSatisfied() { - return - stop || + return + stop || (maxClassExpressionTestsAfterImprovement != 0 && (expressionTests - expressionTestCountLastImprovement >= maxClassExpressionTestsAfterImprovement)) || (maxClassExpressionTests != 0 && (expressionTests >= maxClassExpressionTests)) || - (maxExecutionTimeInSecondsAfterImprovement != 0 && ((System.nanoTime() - nanoStartTime) >= (maxExecutionTimeInSecondsAfterImprovement*1000000000l))) || - (maxExecutionTimeInSeconds != 0 && ((System.nanoTime() - nanoStartTime) >= (maxExecutionTimeInSeconds*1000000000l))) || + (maxExecutionTimeInSecondsAfterImprovement != 0 && ((System.nanoTime() - nanoStartTime) >= (maxExecutionTimeInSecondsAfterImprovement* 1000000000L))) || + (maxExecutionTimeInSeconds != 0 && ((System.nanoTime() - nanoStartTime) >= (maxExecutionTimeInSeconds* 1000000000L))) || (terminateOnNoiseReached && (100*getCurrentlyBestAccuracy()>=100-noisePercentage)) || (stopOnFirstDefinition && (getCurrentlyBestAccuracy() >= 1)); } @@ -822,18 +766,18 @@ private boolean terminationCriteriaSatisfied() { private void reset() { // set all values back to their default values (used for running // the algorithm more than once) - nodes = new TreeSet(heuristic); - descriptions = new TreeSet(); + searchTree = new SearchTree<>(heuristic); + descriptions = new TreeSet<>(); bestEvaluatedDescriptions.getSet().clear(); expressionTests = 0; } private void printAlgorithmRunStats() { if (stop) { - logger.info("Algorithm stopped ("+expressionTests+" descriptions tested). " + nodes.size() + " nodes in the search tree.\n"); + logger.info("Algorithm stopped ("+expressionTests+" descriptions tested). " + searchTree.size() + " nodes in the search tree.\n"); } else { totalRuntimeNs = System.nanoTime()-nanoStartTime; - logger.info("Algorithm terminated successfully (time: " + Helper.prettyPrintNanoSeconds(totalRuntimeNs) + ", "+expressionTests+" descriptions tested, " + nodes.size() + " nodes in the search tree).\n"); + logger.info("Algorithm terminated successfully (time: " + Helper.prettyPrintNanoSeconds(totalRuntimeNs) + ", "+expressionTests+" descriptions tested, " + searchTree.size() + " nodes in the search tree).\n"); logger.info(reasoner.toString()); } } @@ -857,7 +801,7 @@ private void writeSearchTree(TreeSet refinements) { treeString.append(" ").append(ref).append("\n"); } } - treeString.append(startNode.toTreeString(baseURI, prefixes)).append("\n"); + treeString.append(TreeUtils.toTreeString(searchTree, baseURI, prefixes)).append("\n"); // replace or append if (replaceSearchTree) { @@ -877,13 +821,13 @@ private void updateMinMaxHorizExp(OENode node) { // we need to check whether it was the last one if(minHorizExp == newHorizExp - 1) { - // the best accuracy that a node can achieve + // the best accuracy that a node can achieve double scoreThreshold = heuristic.getNodeScore(node) + 1 - node.getAccuracy(); - for(OENode n : nodes.descendingSet()) { + for(OENode n : searchTree.descendingSet()) { if(n != node) { if(n.getHorizontalExpansion() == minHorizExp) { - // we can stop instantly when another node with min. + // we can stop instantly when another node with min. return; } if(heuristic.getNodeScore(n) < scoreThreshold) { @@ -912,14 +856,14 @@ public List getCurrentlyBestDescriptions() { } @Override - public EvaluatedDescription getCurrentlyBestEvaluatedDescription() { + public EvaluatedDescription getCurrentlyBestEvaluatedDescription() { return bestEvaluatedDescriptions.getBest(); - } + } @Override - public TreeSet> getCurrentlyBestEvaluatedDescriptions() { + public NavigableSet> getCurrentlyBestEvaluatedDescriptions() { return bestEvaluatedDescriptions.getSet(); - } + } public double getCurrentlyBestAccuracy() { return bestEvaluatedDescriptions.getBest().getAccuracy(); @@ -928,21 +872,13 @@ public double getCurrentlyBestAccuracy() { @Override public boolean isRunning() { return isRunning; - } + } @Override public void stop() { stop = true; } - public OENode getSearchTreeRoot() { - return startNode; - } - - public TreeSet getNodes() { - return nodes; - } - public int getMaximumHorizontalExpansion() { return maxHorizExp; } @@ -1023,22 +959,6 @@ public void setReplaceSearchTree(boolean replaceSearchTree) { this.replaceSearchTree = replaceSearchTree; } - public int getMaxClassDescriptionTests() { - return maxClassExpressionTests; - } - - public void setMaxClassDescriptionTests(int maxClassDescriptionTests) { - this.maxClassExpressionTests = maxClassDescriptionTests; - } - - public int getMaxExecutionTimeInSeconds() { - return maxExecutionTimeInSeconds; - } - - public void setMaxExecutionTimeInSeconds(int maxExecutionTimeInSeconds) { - this.maxExecutionTimeInSeconds = maxExecutionTimeInSeconds; - } - public boolean isTerminateOnNoiseReached() { return terminateOnNoiseReached; } @@ -1064,15 +984,6 @@ public void setHeuristic(AbstractHeuristic heuristic) { this.heuristic = heuristic; } - public int getMaxClassExpressionTestsWithoutImprovement() { - return maxClassExpressionTestsAfterImprovement; - } - - public void setMaxClassExpressionTestsWithoutImprovement( - int maxClassExpressionTestsWithoutImprovement) { - this.maxClassExpressionTestsAfterImprovement = maxClassExpressionTestsWithoutImprovement; - } - public int getMaxExecutionTimeInSecondsAfterImprovement() { return maxExecutionTimeInSecondsAfterImprovement; } @@ -1080,7 +991,7 @@ public int getMaxExecutionTimeInSecondsAfterImprovement() { public void setMaxExecutionTimeInSecondsAfterImprovement( int maxExecutionTimeInSecondsAfterImprovement) { this.maxExecutionTimeInSecondsAfterImprovement = maxExecutionTimeInSecondsAfterImprovement; - } + } public boolean isSingleSuggestionMode() { return singleSuggestionMode; @@ -1196,7 +1107,7 @@ public static void main(String[] args) throws Exception{ df.getOWLThing()) ), df.getOWLObjectAllValuesFrom( - df.getOWLObjectProperty(IRI.create("http://example.com/father#hasChild")), + df.getOWLObjectProperty(IRI.create("http://example.com/father#hasChild")), df.getOWLThing() ) ); diff --git a/components-core/src/main/java/org/dllearner/algorithms/celoe/OEHeuristicRuntime.java b/components-core/src/main/java/org/dllearner/algorithms/celoe/OEHeuristicRuntime.java index 8fba820f61..ccfbe6301d 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/celoe/OEHeuristicRuntime.java +++ b/components-core/src/main/java/org/dllearner/algorithms/celoe/OEHeuristicRuntime.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.celoe; import org.dllearner.core.AbstractHeuristic; @@ -36,12 +35,12 @@ public class OEHeuristicRuntime extends AbstractHeuristic{ - // strong penalty for long descriptions + @ConfigOption(description = "penalty for long descriptions (horizontal expansion) (strong by default)", defaultValue = "0.1") private double expansionPenaltyFactor = 0.1; - // bonus for being better than parent node + @ConfigOption(description = "bonus for being better than parent node", defaultValue = "0.3") private double gainBonusFactor = 0.3; - // penalty if a node OWLClassExpression has very many refinements since exploring - // such a node is computationally very expensive + @ConfigOption(description = "penalty if a node OWLClassExpression has very many refinements since exploring such a node is computationally very expensive", + defaultValue = "0.0001") private double nodeRefinementPenalty = 0.0001; @ConfigOption(name = "startNodeBonus", defaultValue="0.1") @@ -54,8 +53,9 @@ public OEHeuristicRuntime() { @Override public void init() throws ComponentInitException { - } + } + @Override public double getNodeScore(OENode node) { // accuracy as baseline double score = node.getAccuracy(); @@ -106,5 +106,4 @@ public void setStartNodeBonus(double startNodeBonus) { this.startNodeBonus = startNodeBonus; } - } \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/algorithms/celoe/OENode.java b/components-core/src/main/java/org/dllearner/algorithms/celoe/OENode.java index 0882c76f61..82d5b2f3a5 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/celoe/OENode.java +++ b/components-core/src/main/java/org/dllearner/algorithms/celoe/OENode.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,15 +16,13 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.celoe; import java.text.DecimalFormat; -import java.util.LinkedList; -import java.util.List; import java.util.Map; -import org.dllearner.algorithms.SearchTreeNode; +import org.dllearner.core.AbstractSearchTreeNode; +import org.dllearner.utilities.datastructures.SearchTreeNode; import org.dllearner.utilities.owl.OWLAPIRenderers; import org.dllearner.utilities.owl.OWLClassExpressionUtils; import org.semanticweb.owlapi.model.OWLClassExpression; @@ -38,23 +36,20 @@ * - only evaluated nodes are stored * - too weak nodes are not stored * - redundant nodes are not stored (?) - * - only accuracy is stored to make the node structure reusable for different + * - only accuracy is stored to make the node structure reusable for different * learning problems and -algorithms * * @author Jens Lehmann * */ -public class OENode implements SearchTreeNode { +public class OENode extends AbstractSearchTreeNode implements SearchTreeNode { protected OWLClassExpression description; protected double accuracy; protected int horizontalExpansion; - - protected OENode parent; - protected List children = new LinkedList(); - + // the refinement count corresponds to the number of refinements of the // OWLClassExpression in this node - it is a better heuristic indicator than child count // (and avoids the problem that adding children changes the heuristic value) @@ -62,16 +57,16 @@ public class OENode implements SearchTreeNode { private static DecimalFormat dfPercent = new DecimalFormat("0.00%"); - public OENode(OENode parentNode, OWLClassExpression description, double accuracy) { - this.parent = parentNode; + public OENode(OWLClassExpression description, double accuracy) { this.description = description; this.accuracy = accuracy; - horizontalExpansion = OWLClassExpressionUtils.getLength(description) - 1; - } - - public void addChild(OENode node) { - children.add(node); + this.horizontalExpansion = OWLClassExpressionUtils.getLength(description) - 1; } + +// public OENode(OENode parentNode, OWLClassExpression description, double accuracy) { +// this(description, accuracy); +// this.setParent(parentNode); +// } public void incHorizontalExpansion() { horizontalExpansion++; @@ -88,9 +83,10 @@ public OWLClassExpression getDescription() { return description; } + @Override public OWLClassExpression getExpression() { return getDescription(); - } + } /** * @return the accuracy @@ -99,20 +95,6 @@ public double getAccuracy() { return accuracy; } - /** - * @return the parent - */ - public OENode getParent() { - return parent; - } - - /** - * @return the children - */ - public List getChildren() { - return children; - } - /** * @return the horizontalExpansion */ @@ -139,44 +121,6 @@ public String getShortDescription(String baseURI, Map prefixes) public String toString() { return getShortDescription(null); } - - public String toTreeString() { - return toTreeString(0, null).toString(); - } - - public String toTreeString(String baseURI) { - return toTreeString(0, baseURI).toString(); - } - - public String toTreeString(String baseURI, Map prefixes) { - return toTreeString(0, baseURI, prefixes).toString(); - } - - private StringBuilder toTreeString(int depth, String baseURI) { - StringBuilder treeString = new StringBuilder(); - for(int i=0; i "); - treeString.append(getShortDescription(baseURI)+"\n"); - for(OENode child : children) { - treeString.append(child.toTreeString(depth+1,baseURI)); - } - return treeString; - } - - private StringBuilder toTreeString(int depth, String baseURI, Map prefixes) { - StringBuilder treeString = new StringBuilder(); - for(int i=0; i "); - treeString.append(getShortDescription(baseURI, prefixes)+"\n"); - for(OENode child : children) { - treeString.append(child.toTreeString(depth+1,baseURI,prefixes)); - } - return treeString; - } /** * @return the refinementCount diff --git a/components-core/src/main/java/org/dllearner/algorithms/celoe/PCELOE.java b/components-core/src/main/java/org/dllearner/algorithms/celoe/PCELOE.java new file mode 100644 index 0000000000..760a40ec8d --- /dev/null +++ b/components-core/src/main/java/org/dllearner/algorithms/celoe/PCELOE.java @@ -0,0 +1,1221 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.algorithms.celoe; + +import com.google.common.collect.Sets; +import com.jamonapi.Monitor; +import com.jamonapi.MonitorFactory; +import org.apache.log4j.FileAppender; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.apache.log4j.PatternLayout; +import org.dllearner.core.*; +import org.dllearner.core.config.ConfigOption; +import org.dllearner.core.owl.ClassHierarchy; +import org.dllearner.core.owl.DatatypePropertyHierarchy; +import org.dllearner.core.owl.ObjectPropertyHierarchy; +import org.dllearner.kb.OWLFile; +import org.dllearner.learningproblems.ClassAsInstanceLearningProblem; +import org.dllearner.learningproblems.ClassLearningProblem; +import org.dllearner.learningproblems.PosNegLP; +import org.dllearner.learningproblems.PosOnlyLP; +import org.dllearner.reasoning.ClosedWorldReasoner; +import org.dllearner.reasoning.SPARQLReasoner; +import org.dllearner.refinementoperators.*; +import org.dllearner.utilities.Files; +import org.dllearner.utilities.Helper; +import org.dllearner.utilities.OWLAPIUtils; +import org.dllearner.utilities.TreeUtils; +import org.dllearner.utilities.datastructures.SynchronizedSearchTree; +import org.dllearner.utilities.owl.*; +import org.semanticweb.owlapi.model.*; +import org.springframework.beans.factory.annotation.Autowired; +import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl; + +import java.io.File; +import java.util.*; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +/** + * The PCELOE is an experimental, parallel implementation of the CELOE algorithm. + * + * @author Lorenz Buehmann + * + */ +@ComponentAnn(name="PCELOE", shortName="pceloe", version=1.0, description="PCELOE is an experimental, parallel implementation of the CELOE algorithm.") +public class PCELOE extends AbstractCELA { + + private static Logger logger = Logger.getLogger(PCELOE.class); + + private boolean isRunning = false; + private boolean stop = false; + + @ConfigOption(description = "the refinement operator instance to use") + private LengthLimitedRefinementOperator operator; + + private SynchronizedSearchTree searchTree; + @ConfigOption(name="heuristic", defaultValue="celoe_heuristic") + private AbstractHeuristic heuristic; + // the class with which we start the refinement process + @ConfigOption(name = "startClass", defaultValue="owl:Thing", description="You can specify a start class for the algorithm. To do this, you have to use Manchester OWL syntax without using prefixes.") + private OWLClassExpression startClass; + + // all descriptions in the search tree plus those which were too weak (for fast redundancy check) +// private TreeSet descriptions; + private SortedSet descriptions; + + + // if true, then each solution is evaluated exactly instead of approximately + // private boolean exactBestDescriptionEvaluation = false; + @ConfigOption(name = "singleSuggestionMode", defaultValue="false", description="Use this if you are interested in only one suggestion and your learning problem has many (more than 1000) examples.") + private boolean singleSuggestionMode; + private OWLClassExpression bestDescription; + private double bestAccuracy = Double.MIN_VALUE; + + private OWLClass classToDescribe; + // examples are either 1.) instances of the class to describe 2.) positive examples + // 3.) union of pos.+neg. examples depending on the learning problem at hand + private Set examples; + + // CELOE was originally created for learning classes in ontologies, but also + // works for other learning problem types + private boolean isClassLearningProblem; + private boolean isEquivalenceProblem; + + private long nanoStartTime; + + // important parameters (non-config options but internal) + private double noise; + + private boolean filterFollowsFromKB; + + // less important parameters + // forces that one solution cannot be subexpression of another expression; this option is useful to get diversity + // but it can also suppress quite useful expressions + private boolean forceMutualDifference = false; + + + // statistical variables + private int expressionTests = 0; + private int minHorizExp = 0; + private int maxHorizExp = 0; + private long totalRuntimeNs = 0; + + // TODO: turn those into config options + + // important: do not initialise those with empty sets + // null = no settings for allowance / ignorance + // empty set = allow / ignore nothing (it is often not desired to allow no class!) + Set allowedConcepts = null; + Set ignoredConcepts = null; + + // important: do not initialise those with empty sets + // null = no settings for allowance / ignorance + // empty set = allow / ignore nothing (it is often not desired to allow no class!) + @ConfigOption(name = "writeSearchTree", defaultValue="false", description="specifies whether to write a search tree") + private boolean writeSearchTree = false; + + @ConfigOption(name = "searchTreeFile", defaultValue="log/searchTree.txt", description="file to use for the search tree") + private String searchTreeFile = "log/searchTree.txt"; + + @ConfigOption(name = "replaceSearchTree", defaultValue="false", description="specifies whether to replace the search tree in the log file after each run or append the new search tree") + private boolean replaceSearchTree = false; + + @ConfigOption(name = "maxNrOfResults", defaultValue="10", description="Sets the maximum number of results one is interested in. (Setting this to a lower value may increase performance as the learning algorithm has to store/evaluate/beautify less descriptions).") + private int maxNrOfResults = 10; + + @ConfigOption(name = "noisePercentage", defaultValue="0.0", description="the (approximated) percentage of noise within the examples") + private double noisePercentage = 0.0; + + @ConfigOption(name = "filterDescriptionsFollowingFromKB", defaultValue="false", description="If true, then the results will not contain suggestions, which already follow logically from the knowledge base. Be careful, since this requires a potentially expensive consistency check for candidate solutions.") + private boolean filterDescriptionsFollowingFromKB = false; + + @ConfigOption(name = "reuseExistingDescription", defaultValue="false", description="If true, the algorithm tries to find a good starting point close to an existing definition/super class of the given class in the knowledge base.") + private boolean reuseExistingDescription = false; + + @ConfigOption(name = "maxClassExpressionTests", defaultValue="0", description="The maximum number of candidate hypothesis the algorithm is allowed to test (0 = no limit). The algorithm will stop afterwards. (The real number of tests can be slightly higher, because this criterion usually won't be checked after each single test.)") + private int maxClassExpressionTests = 0; + + @ConfigOption(name = "maxClassExpressionTestsAfterImprovement", defaultValue="0", description = "The maximum number of candidate hypothesis the algorithm is allowed after an improvement in accuracy (0 = no limit). The algorithm will stop afterwards. (The real number of tests can be slightly higher, because this criterion usually won't be checked after each single test.)") + private int maxClassExpressionTestsAfterImprovement = 0; + + @ConfigOption(defaultValue = "10", name = "maxExecutionTimeInSeconds", description = "maximum execution of the algorithm in seconds") + private int maxExecutionTimeInSeconds = 10; + + @ConfigOption(defaultValue = "0", name = "maxExecutionTimeInSecondsAfterImprovement", description = "maximum execution of the algorithm in seconds") + private int maxExecutionTimeInSecondsAfterImprovement = 0; + + @ConfigOption(name = "terminateOnNoiseReached", defaultValue="false", description="specifies whether to terminate when noise criterion is met") + private boolean terminateOnNoiseReached = false; + + @ConfigOption(name = "maxDepth", defaultValue="7", description="maximum depth of description") + private double maxDepth = 7; + + @ConfigOption(name = "stopOnFirstDefinition", defaultValue="false", description="algorithm will terminate immediately when a correct definition is found") + private boolean stopOnFirstDefinition = false; + + + @ConfigOption(defaultValue = "false", description = "whether to try and refine solutions which already have accuracy value of 1") + private boolean expandAccuracy100Nodes = false; + private double currentHighestAccuracy; + + @ConfigOption(name = "nrOfThreads", defaultValue="2", description="number of threads running in parallel") + private int nrOfThreads = 2; + + private int expressionTestCountLastImprovement; + private long timeLastImprovement = 0; + + private Set currentlyProcessedNodes = Collections.synchronizedSet(new HashSet()); + private volatile double highestAccuracy = 0.0; + + public PCELOE() {} + + public PCELOE(PCELOE celoe){ + setReasoner(celoe.reasoner); + setLearningProblem(celoe.learningProblem); + + setAllowedConcepts(celoe.getAllowedConcepts()); + setAllowedObjectProperties(celoe.getAllowedObjectProperties()); + setAllowedDataProperties(celoe.getAllowedDataProperties()); + + setIgnoredConcepts(celoe.ignoredConcepts); + setIgnoredObjectProperties(celoe.getIgnoredObjectProperties()); + setIgnoredDataProperties(celoe.getIgnoredDataProperties()); + + setExpandAccuracy100Nodes(celoe.expandAccuracy100Nodes); + setFilterDescriptionsFollowingFromKB(celoe.filterDescriptionsFollowingFromKB); + setHeuristic(celoe.heuristic); + + setMaxClassExpressionTests(celoe.maxClassExpressionTests); + setMaxClassExpressionTestsAfterImprovement(celoe.maxClassExpressionTestsAfterImprovement); + setMaxDepth(celoe.maxDepth); + setMaxExecutionTimeInSeconds(celoe.maxExecutionTimeInSeconds); + setMaxExecutionTimeInSecondsAfterImprovement(celoe.maxExecutionTimeInSecondsAfterImprovement); + setMaxNrOfResults(celoe.maxNrOfResults); + setNoisePercentage(celoe.noisePercentage); + + RhoDRDown op = new RhoDRDown((RhoDRDown)celoe.operator); + try { + op.init(); + } catch (ComponentInitException e) { + e.printStackTrace(); + } + setOperator(op); + + + setReuseExistingDescription(celoe.reuseExistingDescription); + setSingleSuggestionMode(celoe.singleSuggestionMode); + setStartClass(celoe.startClass); + setStopOnFirstDefinition(celoe.stopOnFirstDefinition); + setTerminateOnNoiseReached(celoe.terminateOnNoiseReached); + setUseMinimizer(celoe.isUseMinimizer()); + + setWriteSearchTree(celoe.writeSearchTree); + setReplaceSearchTree(celoe.replaceSearchTree); + } + + public PCELOE(AbstractClassExpressionLearningProblem problem, AbstractReasonerComponent reasoner) { + super(problem, reasoner); + } + + public static String getName() { + return "PCELOE"; + } + + @Override + public void init() throws ComponentInitException { + baseURI = reasoner.getBaseURI(); + prefixes = reasoner.getPrefixes(); + + if(maxExecutionTimeInSeconds != 0 && maxExecutionTimeInSecondsAfterImprovement != 0) { + maxExecutionTimeInSeconds = Math.min(maxExecutionTimeInSeconds, maxExecutionTimeInSecondsAfterImprovement); + } + + // TODO add comment + ClassHierarchy classHierarchy = initClassHierarchy(); + ObjectPropertyHierarchy objectPropertyHierarchy = initObjectPropertyHierarchy(); + DatatypePropertyHierarchy datatypePropertyHierarchy = initDataPropertyHierarchy(); + + // if no one injected a heuristic, we use a default one + if(heuristic == null) { + heuristic = new OEHeuristicRuntime(); + heuristic.init(); + } + + minimizer = new OWLClassExpressionMinimizer(dataFactory, reasoner); + + if (writeSearchTree) { + File f = new File(searchTreeFile); + if (f.getParentFile() != null) { + f.getParentFile().mkdirs(); + } + Files.clearFile(f); + } + + // start at owl:Thing by default + if (startClass == null) { + startClass = computeStartClass(); + } else { + try { + this.startClass = OWLAPIUtils.classExpressionPropertyExpander(this.startClass, reasoner, dataFactory); + } catch (Exception e) { + logger.warn("Error parsing start class.", e); + logger.warn("Using owl:Thing instead."); + this.startClass = dataFactory.getOWLThing(); + } + } + + bestEvaluatedDescriptions = new EvaluatedDescriptionSet(maxNrOfResults); + + isClassLearningProblem = (learningProblem instanceof ClassLearningProblem); + + // we put important parameters in class variables + noise = noisePercentage/100d; + + // (filterFollowsFromKB is automatically set to false if the problem + // is not a class learning problem + filterFollowsFromKB = filterDescriptionsFollowingFromKB && isClassLearningProblem; + + // actions specific to ontology engineering + if(isClassLearningProblem) { + ClassLearningProblem problem = (ClassLearningProblem) learningProblem; + classToDescribe = problem.getClassToDescribe(); + isEquivalenceProblem = problem.isEquivalenceProblem(); + + examples = reasoner.getIndividuals(classToDescribe); + } else if(learningProblem instanceof PosOnlyLP) { + examples = ((PosOnlyLP)learningProblem).getPositiveExamples(); + } else if(learningProblem instanceof PosNegLP) { + examples = Sets.union(((PosNegLP)learningProblem).getPositiveExamples(),((PosNegLP)learningProblem).getNegativeExamples()); + } + + // create a refinement operator and pass all configuration + // variables to it + if (operator == null) { + // we use a default operator and inject the class hierarchy for now + operator = new RhoDRDown(); + if (operator instanceof CustomStartRefinementOperator) { + ((CustomStartRefinementOperator) operator).setStartClass(startClass); + } + if (operator instanceof ReasoningBasedRefinementOperator) { + ((ReasoningBasedRefinementOperator) operator).setReasoner(reasoner); + } + operator.init(); + } + if (operator instanceof CustomHierarchyRefinementOperator) { + ((CustomHierarchyRefinementOperator) operator).setClassHierarchy(classHierarchy); + ((CustomHierarchyRefinementOperator) operator).setObjectPropertyHierarchy(objectPropertyHierarchy); + ((CustomHierarchyRefinementOperator) operator).setDataPropertyHierarchy(datatypePropertyHierarchy); + } + + reasoner.setSynchronized(); + operator = new SynchronizedRefinementOperator(operator); + } + + @Override + public void start() { + logger.info("starting PCELOE with " + nrOfThreads + " threads"); +// System.out.println(configurator.getMaxExecutionTimeInSeconds()); + + stop = false; + isRunning = true; + reset(); + nanoStartTime = System.nanoTime(); + + + + addNode(startClass, null); + + int nrOfWorkers = nrOfThreads; + if(nrOfWorkers == 0){ + nrOfWorkers = Runtime.getRuntime().availableProcessors(); + } + ExecutorService service = Executors.newFixedThreadPool(nrOfWorkers); + + List tasks = new ArrayList<>(); + + boolean shareRefinementOperator = false; + + for(int i = 0; i < nrOfWorkers; i++){ + PCELOEWorker worker; + if(shareRefinementOperator) { + ClosedWorldReasoner reasonerCopy = new ClosedWorldReasoner(); + reasonerCopy.setSources(reasoner.getSources()); + try { + reasonerCopy.init(); + } catch (ComponentInitException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + if(operator instanceof SynchronizedRefinementOperator) { + operator = ((SynchronizedRefinementOperator) operator).getDelegate(); + } + RhoDRDown op = new RhoDRDown((RhoDRDown) operator); + op.setReasoner(reasonerCopy); + try { + op.init(); + } catch (ComponentInitException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + worker = new PCELOEWorker(op); + } else { + worker = new PCELOEWorker(); + + } + tasks.add(worker); + } + nanoStartTime = System.nanoTime(); + + //needed to block until all threads have been finished, because otherwise the main thread outputs the result to early + List futures = new ArrayList<>(); + for(Runnable task : tasks){ + futures.add(service.submit(task)); + } + for(Future future : futures){ + try { + future.get(); + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + } + + if (stop) { + logger.info("Algorithm stopped ("+expressionTests+" descriptions tested). " + searchTree.size() + " nodes in the search tree.\n"); + } else { + logger.info("Algorithm terminated successfully (time: " + Helper.prettyPrintNanoSeconds(System.nanoTime()-nanoStartTime) + ", "+expressionTests+" descriptions tested, " + searchTree.size() + " nodes in the search tree).\n"); + logger.info(reasoner.toString()); + } + + if(singleSuggestionMode) { + bestEvaluatedDescriptions.add(bestDescription, bestAccuracy, learningProblem); + } + + // print solution(s) + logger.info("solutions:\n" + getSolutionString()); + +// System.out.println(startNode.toTreeString(baseURI)); + + isRunning = false; + service.shutdown(); +// System.out.println("isRunning: " + isRunning); + } + + /* + * Compute the start class in the search space from which the refinement will start. + * We use the intersection of super classes for definitions (since it needs to + * capture all instances), but owl:Thing for learning subclasses (since it is + * superfluous to add super classes in this case) + */ + private OWLClassExpression computeStartClass() { + OWLClassExpression startClass = dataFactory.getOWLThing(); + + if(isClassLearningProblem) { + if(isEquivalenceProblem) { + Set existingDefinitions = reasoner.getAssertedDefinitions(classToDescribe); + if(reuseExistingDescription && (existingDefinitions.size() > 0)) { + // the existing definition is reused, which in the simplest case means to + // use it as a start class or, if it is already too specific, generalise it + + // pick the longest existing definition as candidate + OWLClassExpression existingDefinition = null; + int highestLength = 0; + for(OWLClassExpression exDef : existingDefinitions) { + if(OWLClassExpressionUtils.getLength(exDef) > highestLength) { + existingDefinition = exDef; + highestLength = OWLClassExpressionUtils.getLength(exDef); + } + } + + LinkedList startClassCandidates = new LinkedList<>(); + startClassCandidates.add(existingDefinition); + // hack for RhoDRDown + if(operator instanceof RhoDRDown) { + ((RhoDRDown)operator).setDropDisjuncts(true); + } + LengthLimitedRefinementOperator upwardOperator = new OperatorInverter(operator); + + // use upward refinement until we find an appropriate start class + boolean startClassFound = false; + OWLClassExpression candidate; + do { + candidate = startClassCandidates.pollFirst(); + if(((ClassLearningProblem)learningProblem).getRecall(candidate)<1.0) { + // add upward refinements to list + Set refinements = upwardOperator.refine(candidate, OWLClassExpressionUtils.getLength(candidate)); +// System.out.println("ref: " + refinements); + LinkedList refinementList = new LinkedList<>(refinements); +// Collections.reverse(refinementList); +// System.out.println("list: " + refinementList); + startClassCandidates.addAll(refinementList); +// System.out.println("candidates: " + startClassCandidates); + } else { + startClassFound = true; + } + } while(!startClassFound); + startClass = candidate; + + if(startClass.equals(existingDefinition)) { + logger.info("Reusing existing class expression " + OWLAPIRenderers.toManchesterOWLSyntax(startClass) + " as start class for learning algorithm."); + } else { + logger.info("Generalised existing class expression " + OWLAPIRenderers.toManchesterOWLSyntax(existingDefinition) + " to " + OWLAPIRenderers.toManchesterOWLSyntax(startClass) + ", which is used as start class for the learning algorithm."); + } + + if(operator instanceof RhoDRDown) { + ((RhoDRDown)operator).setDropDisjuncts(false); + } + + } else { + Set superClasses = reasoner.getClassHierarchy().getSuperClasses(classToDescribe, true); + if(superClasses.size() > 1) { + startClass = dataFactory.getOWLObjectIntersectionOf(superClasses); + } else if(superClasses.size() == 1){ + startClass = (OWLClassExpression) superClasses.toArray()[0]; + } else { + startClass = dataFactory.getOWLThing(); + logger.warn(classToDescribe + " is equivalent to owl:Thing. Usually, it is not " + + "sensible to learn a class expression in this case."); + } + } + } + } + return startClass; + } + + private OENode getNextNodeToExpand() { + // we expand the best node of those, which have not achieved 100% accuracy + // already and have a horizontal expansion equal to their length + // (rationale: further extension is likely to add irrelevant syntactical constructs) + synchronized (searchTree) { + Iterator it = searchTree.descendingIterator(); + + while(it.hasNext()) { + OENode node = it.next(); + if (currentlyProcessedNodes.contains(node)) continue; + + if (isExpandAccuracy100Nodes() && node.getHorizontalExpansion() < OWLClassExpressionUtils.getLength(node.getDescription())) { + currentlyProcessedNodes.add(node); + return node; + } else { + if(node.getAccuracy() < 1.0 || node.getHorizontalExpansion() < OWLClassExpressionUtils.getLength(node.getDescription())) { + currentlyProcessedNodes.add(node); + return node; + } + } + } + } + + // this should practically never be called, since for any reasonable learning + // task, we will always have at least one node with less than 100% accuracy + return null;//nodes.last(); + } + + // expand node horizontically + private TreeSet refineNode(LengthLimitedRefinementOperator operator, OENode node) { + MonitorFactory.getTimeMonitor("refineNode").start(); + // we have to remove and add the node since its heuristic evaluation changes through the expansion + // (you *must not* include any criteria in the heuristic which are modified outside of this method, + // otherwise you may see rarely occurring but critical false ordering in the nodes set) + searchTree.updatePrepare(node); + int horizExp = node.getHorizontalExpansion(); + TreeSet refinements = (TreeSet) operator.refine(node.getDescription(), horizExp+1); +// System.out.println("refinements: " + refinements); + node.incHorizontalExpansion(); + node.setRefinementCount(refinements.size()); +// System.out.println("refined node: " + node); + searchTree.updateDone(node); + MonitorFactory.getTimeMonitor("refineNode").stop(); + return refinements; + } + + /** + * Add node to search tree if it is not too weak. + * @return TRUE if node was added and FALSE otherwise + */ + private boolean addNode(OWLClassExpression description, OENode parentNode) { + MonitorFactory.getTimeMonitor("addNode").start(); + + // redundancy check (return if redundant) + boolean nonRedundant = descriptions.add(description); + if(!nonRedundant) { + return false; + } + + // check whether the class expression is allowed + if(!isDescriptionAllowed(description, parentNode)) { + return false; + } + + // quality of class expression (return if too weak) + Monitor mon = MonitorFactory.start("lp"); + double accuracy = learningProblem.getAccuracyOrTooWeak(description, noise); + mon.stop(); + + // issue a warning if accuracy is not between 0 and 1 or -1 (too weak) + if(accuracy > 1.0 || (accuracy < 0.0 && accuracy != -1)) { + throw new RuntimeException("Invalid accuracy value " + accuracy + " for class expression " + description + + ". This could be caused by a bug in the heuristic measure and should be reported to the DL-Learner bug tracker."); + } + + expressionTests++; + + // return FALSE if 'too weak' + if(accuracy == -1) { + return false; + } + + OENode node = new OENode(description, accuracy); + + // link to parent (unless start node) + searchTree.addNode(parentNode, node); + + // in some cases (e.g. mutation) fully evaluating even a single class expression is too expensive + // due to the high number of examples -- so we just stick to the approximate accuracy + if(singleSuggestionMode) { + if(accuracy > bestAccuracy) { + bestAccuracy = accuracy; + bestDescription = description; + logger.info("more accurate (" + dfPercent.format(bestAccuracy) + ") class expression found: " + descriptionToString(bestDescription)); // + getTemporaryString(bestDescription)); + } + return true; + } + + // maybe add to best descriptions (method keeps set size fixed); + // we need to make sure that this does not get called more often than + // necessary since rewriting is expensive + boolean isCandidate = !bestEvaluatedDescriptions.isFull(); + if(!isCandidate) { + EvaluatedDescription worst = bestEvaluatedDescriptions.getWorst(); + double accThreshold = worst.getAccuracy(); + isCandidate = + (accuracy > accThreshold || + (accuracy >= accThreshold && OWLClassExpressionUtils.getLength(description) < worst.getDescriptionLength())); + } + + if(isCandidate) { + OWLClassExpression niceDescription = node.getExpression();//rewrite(node.getExpression()); + + if(niceDescription.equals(classToDescribe)) { + return false; + } + + if(!isDescriptionAllowed(niceDescription, node)) { + return false; + } + + // another test: none of the other suggested descriptions should be + // a subdescription of this one unless accuracy is different + // => comment: on the one hand, this appears to be too strict, because once A is a solution then everything containing + // A is not a candidate; on the other hand this suppresses many meaningless extensions of A + boolean shorterDescriptionExists = false; + if(forceMutualDifference) { + for(EvaluatedDescription ed : bestEvaluatedDescriptions.getSet()) { + if(Math.abs(ed.getAccuracy()-accuracy) <= 0.00001 && ConceptTransformation.isSubdescription(niceDescription, ed.getDescription())) { +// System.out.println("shorter: " + ed.getDescription()); + shorterDescriptionExists = true; + break; + } + } + } + +// System.out.println("shorter description? " + shorterDescriptionExists + " nice: " + niceDescription); + + if(!shorterDescriptionExists) { + if(!filterFollowsFromKB || !((ClassLearningProblem)learningProblem).followsFromKB(niceDescription)) { +// System.out.println(node + "->" + niceDescription); + bestEvaluatedDescriptions.add(niceDescription, accuracy, learningProblem); +// System.out.println("acc: " + accuracy); +// System.out.println(bestEvaluatedDescriptions); + } + } + +// bestEvaluatedDescriptions.add(node.getDescription(), accuracy, learningProblem); + +// System.out.println(bestEvaluatedDescriptions.getSet().size()); + } + + return true; + } + + // checks whether the class expression is allowed + private boolean isDescriptionAllowed(OWLClassExpression description, OENode parentNode) { + if(isClassLearningProblem) { + if(isEquivalenceProblem) { + // the class to learn must not appear on the outermost property level + if(occursOnFirstLevel(description, classToDescribe)) { + return false; + } + if(occursOnSecondLevel(description, classToDescribe)) { + return false; + } + } else { + // none of the superclasses of the class to learn must appear on the + // outermost property level + TreeSet toTest = new TreeSet<>(); + toTest.add(classToDescribe); + while(!toTest.isEmpty()) { + OWLClassExpression d = toTest.pollFirst(); + if(occursOnFirstLevel(description, d)) { + return false; + } + toTest.addAll(reasoner.getClassHierarchy().getSuperClasses(d)); + } + } + } else if (learningProblem instanceof ClassAsInstanceLearningProblem) { + return true; + } + + // perform forall sanity tests + if (parentNode != null && + (ConceptTransformation.getForallOccurences(description) > ConceptTransformation.getForallOccurences(parentNode.getDescription()))) { + // we have an additional \forall construct, so we now fetch the contexts + // in which it occurs + SortedSet contexts = ConceptTransformation.getForallContexts(description); + SortedSet parentContexts = ConceptTransformation.getForallContexts(parentNode.getDescription()); + contexts.removeAll(parentContexts); +// System.out.println("parent description: " + parentNode.getDescription()); +// System.out.println("description: " + description); +// System.out.println("contexts: " + contexts); + // we now have to perform sanity checks: if \forall is used, then there + // should be at least on class instance which has a filler at the given context + for(PropertyContext context : contexts) { + // transform [r,s] to \exists r.\exists s.\top + OWLClassExpression existentialContext = context.toExistentialContext(); + boolean fillerFound = false; + if(reasoner.getClass().isAssignableFrom(SPARQLReasoner.class)) { + SortedSet individuals = reasoner.getIndividuals(existentialContext); + fillerFound = !Sets.intersection(individuals, examples).isEmpty(); + } else { + for(OWLIndividual instance : examples) { + if(reasoner.hasType(existentialContext, instance)) { + fillerFound = true; + break; + } + } + } + + // if we do not find a filler, this means that putting \forall at + // that position is not meaningful + if(!fillerFound) { + return false; + } + } + } + + // we do not want to have negations of sibling classes on the outermost level + // (they are expressed more naturally by saying that the siblings are disjoint, + // so it is reasonable not to include them in solutions) +// Set siblingClasses = reasoner.getClassHierarchy().getSiblingClasses(classToDescribe); +// for now, we just disable negation + + return true; + } + + // determine whether a named class occurs on the outermost level, i.e. property depth 0 + // (it can still be at higher depth, e.g. if intersections are nested in unions) + private boolean occursOnFirstLevel(OWLClassExpression description, OWLClassExpression cls) { + if(cls.isOWLThing()) { + return false; + } + return (description instanceof OWLNaryBooleanClassExpression && + ((OWLNaryBooleanClassExpression)description).getOperands().contains(cls)); +// return description.containsConjunct(cls) || +// (description instanceof OWLObjectUnionOf && ((OWLObjectUnionOf) description).getOperands().contains(cls)); + } + + // determine whether a named class occurs on the outermost level, i.e. property depth 0 + // (it can still be at higher depth, e.g. if intersections are nested in unions) + private boolean occursOnSecondLevel(OWLClassExpression description, OWLClassExpression cls) { +// SortedSet superClasses = reasoner.getSuperClasses(cls); +// if(description instanceof OWLObjectIntersectionOf) { +// List operands = ((OWLObjectIntersectionOf) description).getOperandsAsList(); +// +// for (OWLClassExpression op : operands) { +// if(superClasses.contains(op) || +// (op instanceof OWLObjectUnionOf && !Sets.intersection(((OWLObjectUnionOf)op).getOperands(),superClasses).isEmpty())) { +// for (OWLClassExpression op2 : operands) { +// if((op2 instanceof OWLObjectUnionOf && ((OWLObjectUnionOf)op2).getOperands().contains(cls))) { +// return true; +// } +// } +// } +// } +// +// for (OWLClassExpression op1 : operands) { +// for (OWLClassExpression op2 : operands) { +// if(!op1.isAnonymous() && op2 instanceof OWLObjectUnionOf) { +// for (OWLClassExpression op3 : ((OWLObjectUnionOf)op2).getOperands()) { +// if(!op3.isAnonymous()) {// A AND B with Disj(A,B) +// if(reasoner.isDisjoint(op1.asOWLClass(), op3.asOWLClass())) { +// return true; +// } +// } else {// A AND NOT A +// if(op3 instanceof OWLObjectComplementOf && ((OWLObjectComplementOf)op3).getOperand().equals(op1)) { +// return true; +// } +// } +// } +// } +// } +// } +// } + + return false; + } + + + private boolean terminationCriteriaSatisfied() { + return + stop || + (maxClassExpressionTestsAfterImprovement != 0 && (expressionTests - expressionTestCountLastImprovement >= maxClassExpressionTestsAfterImprovement)) || + (maxClassExpressionTests != 0 && (expressionTests >= maxClassExpressionTests)) || + (maxExecutionTimeInSecondsAfterImprovement != 0 && ((System.nanoTime() - nanoStartTime) >= (maxExecutionTimeInSecondsAfterImprovement* 1000000000L))) || + (maxExecutionTimeInSeconds != 0 && ((System.nanoTime() - nanoStartTime) >= (maxExecutionTimeInSeconds* 1000000000L))) || + (terminateOnNoiseReached && (100*getCurrentlyBestAccuracy()>=100-noisePercentage)) || + (stopOnFirstDefinition && (getCurrentlyBestAccuracy() >= 1)); + } + + private void reset() { + // set all values back to their default values (used for running + // the algorithm more than once) +// nodes = new TreeSet(heuristic); + searchTree = new SynchronizedSearchTree(heuristic); + //Sets.synchronizedNavigableSet(new TreeSet(Collections.reverseOrder(heuristic))); + descriptions = Collections.synchronizedSortedSet(new TreeSet()); + bestEvaluatedDescriptions.getSet().clear(); + expressionTests = 0; + highestAccuracy = 0.0; + } + + private void printAlgorithmRunStats() { + if (stop) { + logger.info("Algorithm stopped ("+expressionTests+" descriptions tested). " + searchTree.size() + " nodes in the search tree.\n"); + } else { + totalRuntimeNs = System.nanoTime()-nanoStartTime; + logger.info("Algorithm terminated successfully (time: " + Helper.prettyPrintNanoSeconds(totalRuntimeNs) + ", "+expressionTests+" descriptions tested, " + searchTree.size() + " nodes in the search tree).\n"); + logger.info(reasoner.toString()); + } + } + + private void showIfBetterSolutionsFound() { + if(!singleSuggestionMode && bestEvaluatedDescriptions.getBestAccuracy() > currentHighestAccuracy) { + currentHighestAccuracy = bestEvaluatedDescriptions.getBestAccuracy(); + expressionTestCountLastImprovement = expressionTests; + timeLastImprovement = System.nanoTime(); + long durationInMillis = getCurrentRuntimeInMilliSeconds(); + String durationStr = getDurationAsString(durationInMillis); + logger.info("more accurate (" + dfPercent.format(currentHighestAccuracy) + ") class expression found after " + durationStr + ": " + descriptionToString(bestEvaluatedDescriptions.getBest().getDescription())); + } + } + + private void writeSearchTree(TreeSet refinements) { + StringBuilder treeString = new StringBuilder("best node: ").append(bestEvaluatedDescriptions.getBest()).append("\n"); + if (refinements.size() > 1) { + treeString.append("all expanded nodes:\n"); + for (OWLClassExpression ref : refinements) { + treeString.append(" ").append(ref).append("\n"); + } + } + treeString.append(TreeUtils.toTreeString(searchTree, baseURI, prefixes)).append("\n"); + + // replace or append + if (replaceSearchTree) { + Files.createFile(new File(searchTreeFile), treeString.toString()); + } else { + Files.appendToFile(new File(searchTreeFile), treeString.toString()); + } + } + + private void updateMinMaxHorizExp(OENode node) { + int newHorizExp = node.getHorizontalExpansion(); + + // update maximum value + maxHorizExp = Math.max(maxHorizExp, newHorizExp); + + // we just expanded a node with minimum horizontal expansion; + // we need to check whether it was the last one + if(minHorizExp == newHorizExp - 1) { + + // the best accuracy that a node can achieve + double scoreThreshold = heuristic.getNodeScore(node) + 1 - node.getAccuracy(); + + for(OENode n : searchTree.descendingSet()) { + if(n != node) { + if(n.getHorizontalExpansion() == minHorizExp) { + // we can stop instantly when another node with min. + return; + } + if(heuristic.getNodeScore(n) < scoreThreshold) { + // we can stop traversing nodes when their score is too low + break; + } + } + } + + // inc. minimum since we found no other node which also has min. horiz. exp. + minHorizExp++; + +// System.out.println("minimum horizontal expansion is now " + minHorizExp); + } + } + + @Override + public OWLClassExpression getCurrentlyBestDescription() { + EvaluatedDescription ed = getCurrentlyBestEvaluatedDescription(); + return ed == null ? null : ed.getDescription(); + } + + @Override + public List getCurrentlyBestDescriptions() { + return bestEvaluatedDescriptions.toDescriptionList(); + } + + @Override + public EvaluatedDescription getCurrentlyBestEvaluatedDescription() { + return bestEvaluatedDescriptions.getBest(); + } + + @Override + public NavigableSet> getCurrentlyBestEvaluatedDescriptions() { + return bestEvaluatedDescriptions.getSet(); + } + + public double getCurrentlyBestAccuracy() { + return bestEvaluatedDescriptions.getBest().getAccuracy(); + } + + @Override + public boolean isRunning() { + return isRunning; + } + + @Override + public void stop() { + stop = true; + } + + public int getMaximumHorizontalExpansion() { + return maxHorizExp; + } + + public int getMinimumHorizontalExpansion() { + return minHorizExp; + } + + /** + * @return the expressionTests + */ + public int getClassExpressionTests() { + return expressionTests; + } + + public LengthLimitedRefinementOperator getOperator() { + return operator; + } + + @Autowired(required=false) + public void setOperator(LengthLimitedRefinementOperator operator) { + this.operator = operator; + } + + public OWLClassExpression getStartClass() { + return startClass; + } + + public void setStartClass(OWLClassExpression startClass) { + this.startClass = startClass; + } + + public boolean isWriteSearchTree() { + return writeSearchTree; + } + + public void setWriteSearchTree(boolean writeSearchTree) { + this.writeSearchTree = writeSearchTree; + } + + public String getSearchTreeFile() { + return searchTreeFile; + } + + public void setSearchTreeFile(String searchTreeFile) { + this.searchTreeFile = searchTreeFile; + } + + public int getMaxNrOfResults() { + return maxNrOfResults; + } + + public void setMaxNrOfResults(int maxNrOfResults) { + this.maxNrOfResults = maxNrOfResults; + } + + public double getNoisePercentage() { + return noisePercentage; + } + + public void setNoisePercentage(double noisePercentage) { + this.noisePercentage = noisePercentage; + } + + public boolean isFilterDescriptionsFollowingFromKB() { + return filterDescriptionsFollowingFromKB; + } + + public void setFilterDescriptionsFollowingFromKB(boolean filterDescriptionsFollowingFromKB) { + this.filterDescriptionsFollowingFromKB = filterDescriptionsFollowingFromKB; + } + + public boolean isReplaceSearchTree() { + return replaceSearchTree; + } + + public void setReplaceSearchTree(boolean replaceSearchTree) { + this.replaceSearchTree = replaceSearchTree; + } + + @Deprecated + public int getMaxClassDescriptionTests() { + return maxClassExpressionTests; + } + + @Deprecated + public void setMaxClassDescriptionTests(int maxClassDescriptionTests) { + this.maxClassExpressionTests = maxClassDescriptionTests; + } + + @Override + public int getMaxExecutionTimeInSeconds() { + return maxExecutionTimeInSeconds; + } + + @Override + public void setMaxExecutionTimeInSeconds(int maxExecutionTimeInSeconds) { + this.maxExecutionTimeInSeconds = maxExecutionTimeInSeconds; + } + + public boolean isTerminateOnNoiseReached() { + return terminateOnNoiseReached; + } + + public void setTerminateOnNoiseReached(boolean terminateOnNoiseReached) { + this.terminateOnNoiseReached = terminateOnNoiseReached; + } + + public boolean isReuseExistingDescription() { + return reuseExistingDescription; + } + + public void setReuseExistingDescription(boolean reuseExistingDescription) { + this.reuseExistingDescription = reuseExistingDescription; + } + + public AbstractHeuristic getHeuristic() { + return heuristic; + } + + @Autowired(required=false) + public void setHeuristic(AbstractHeuristic heuristic) { + this.heuristic = heuristic; + } + + @Deprecated + public int getMaxClassExpressionTestsWithoutImprovement() { + return maxClassExpressionTestsAfterImprovement; + } + + @Deprecated + public void setMaxClassExpressionTestsWithoutImprovement( + int maxClassExpressionTestsWithoutImprovement) { + this.maxClassExpressionTestsAfterImprovement = maxClassExpressionTestsWithoutImprovement; + } + + public int getMaxExecutionTimeInSecondsAfterImprovement() { + return maxExecutionTimeInSecondsAfterImprovement; + } + + public void setMaxExecutionTimeInSecondsAfterImprovement( + int maxExecutionTimeInSecondsAfterImprovement) { + this.maxExecutionTimeInSecondsAfterImprovement = maxExecutionTimeInSecondsAfterImprovement; + } + + public boolean isSingleSuggestionMode() { + return singleSuggestionMode; + } + + public void setSingleSuggestionMode(boolean singleSuggestionMode) { + this.singleSuggestionMode = singleSuggestionMode; + } + + public int getMaxClassExpressionTests() { + return maxClassExpressionTests; + } + + public void setMaxClassExpressionTests(int maxClassExpressionTests) { + this.maxClassExpressionTests = maxClassExpressionTests; + } + + public int getMaxClassExpressionTestsAfterImprovement() { + return maxClassExpressionTestsAfterImprovement; + } + + public void setMaxClassExpressionTestsAfterImprovement( + int maxClassExpressionTestsAfterImprovement) { + this.maxClassExpressionTestsAfterImprovement = maxClassExpressionTestsAfterImprovement; + } + + public double getMaxDepth() { + return maxDepth; + } + + public void setMaxDepth(double maxDepth) { + this.maxDepth = maxDepth; + } + + public boolean isStopOnFirstDefinition() { + return stopOnFirstDefinition; + } + + public void setStopOnFirstDefinition(boolean stopOnFirstDefinition) { + this.stopOnFirstDefinition = stopOnFirstDefinition; + } + + public long getTotalRuntimeNs() { + return totalRuntimeNs; + } + + /** + * @return the expandAccuracy100Nodes + */ + public boolean isExpandAccuracy100Nodes() { + return expandAccuracy100Nodes; + } + + /** + * @param expandAccuracy100Nodes the expandAccuracy100Nodes to set + */ + public void setExpandAccuracy100Nodes(boolean expandAccuracy100Nodes) { + this.expandAccuracy100Nodes = expandAccuracy100Nodes; + } + + /* (non-Javadoc) + * @see java.lang.Object#clone() + */ + @Override + public Object clone() throws CloneNotSupportedException { + return new PCELOE(this); + } + + public int getNrOfThreads() { + return nrOfThreads; + } + + public void setNrOfThreads(int nrOfThreads) { + this.nrOfThreads = nrOfThreads; + } + + private synchronized double getHighestAccuracy(){ + return highestAccuracy; + } + + class PCELOEWorker implements Runnable{ + + private LengthLimitedRefinementOperator operator; + + public PCELOEWorker() { + operator = PCELOE.this.operator; + } + + public PCELOEWorker(LengthLimitedRefinementOperator operator) { + this.operator = operator; + } + + @Override + public void run() { + OENode nextNode; + while (!terminationCriteriaSatisfied()) { +// String threadName = Thread.currentThread().getName(); + + nextNode = getNextNodeToExpand(); +// System.out.println(threadName + " processing " + nextNode); + + if(nextNode != null){ + int horizExp = nextNode.getHorizontalExpansion(); + + // apply refinement operator + TreeSet refinements = refineNode(operator, nextNode); + + while(!refinements.isEmpty() && !terminationCriteriaSatisfied()) { + // pick element from set + OWLClassExpression refinement = refinements.pollFirst(); +// System.out.println(threadName + " analysing " + refinement); + + // get length of class expression + int length = OWLClassExpressionUtils.getLength(refinement); + + // we ignore all refinements with lower length and too high depth + // (this also avoids duplicate node children) + if(length > horizExp && OWLClassExpressionUtils.getDepth(refinement) <= maxDepth) { + // add node to search tree + boolean added = addNode(refinement, nextNode); + if(added) { +// System.out.println(threadName + " put " + refinement); + } + } + } + currentlyProcessedNodes.remove(nextNode); +// showIfBetterSolutionsFound(); + } + + } + } + } + + public static void main(String[] args) throws Exception{ +// StringRenderer.setRenderer(Rendering.DL_SYNTAX); + Logger.getRootLogger().setLevel(Level.INFO); + Logger.getLogger(PCELOE.class).setLevel(Level.DEBUG); + Logger.getLogger(PCELOE.class).addAppender(new FileAppender(new PatternLayout( "[%t] %c: %m%n" ), "log/parallel_run.txt", false)); + + + AbstractKnowledgeSource ks = new OWLFile("../examples/family/father_oe.owl"); + ks.init(); + + AbstractReasonerComponent rc = new ClosedWorldReasoner(ks); + rc.init(); + + ClassLearningProblem lp = new ClassLearningProblem(rc); + lp.setClassToDescribe(new OWLClassImpl(IRI.create("http://example.com/father#father"))); + lp.setCheckConsistency(false); + lp.init(); + + PCELOE alg = new PCELOE(lp, rc); + alg.setMaxExecutionTimeInSeconds(10); + alg.setNrOfThreads(4); +// alg.setMaxClassDescriptionTests(200); + alg.init(); + + alg.start(); + } + +} diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/DSTTDTClassifier.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/DSTTDTClassifier.java index f756bb09ee..800c4e09b5 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/DSTTDTClassifier.java +++ b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/DSTTDTClassifier.java @@ -1,45 +1,37 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.decisiontrees.dsttdt; import java.util.ArrayList; +//import knowledgeBasesHandler.KnowledgeBase; import java.util.List; import java.util.Set; import java.util.SortedSet; import java.util.Stack; - -//import knowledgeBasesHandler.KnowledgeBase; - - - - - - - - - - - - import java.util.TreeSet; - - - - - - - - - - - - import org.dllearner.algorithms.decisiontrees.dsttdt.dst.MassFunction; import org.dllearner.algorithms.decisiontrees.dsttdt.models.DSTDLTree; import org.dllearner.algorithms.decisiontrees.dsttdt.models.EvidentialModel; import org.dllearner.algorithms.decisiontrees.heuristics.TreeInductionHeuristics; -import org.dllearner.algorithms.decisiontrees.refinementoperators.*; -import org.dllearner.algorithms.decisiontrees.tdt.model.DLTree; +import org.dllearner.algorithms.decisiontrees.refinementoperators.DLTreesRefinementOperator; import org.dllearner.algorithms.decisiontrees.utils.Couple; import org.dllearner.algorithms.decisiontrees.utils.Npla; import org.dllearner.algorithms.decisiontrees.utils.Split; @@ -49,13 +41,15 @@ import org.dllearner.core.ComponentAnn; import org.dllearner.core.ComponentInitException; import org.dllearner.core.EvaluatedDescription; +import org.dllearner.core.annotations.OutVariable; +import org.dllearner.core.annotations.Unused; import org.dllearner.core.config.ConfigOption; +import org.dllearner.learningproblems.PosNegUndLP; import org.dllearner.refinementoperators.RefinementOperator; import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLIndividual; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.dllearner.learningproblems.PosNegUndLP; @@ -63,15 +57,20 @@ public class DSTTDTClassifier extends AbstractCELA{ //static final double THRESHOLD = 0.05; - //static final double M = 3; + //static final double M = 3; private static Logger logger= LoggerFactory.getLogger(DSTTDTClassifier.class); + + @OutVariable private DSTDLTree currentmodel; // model induced from the procedure private boolean stop; + @Unused protected OWLClassExpression classToDescribe; //target concept - protected TreeInductionHeuristics heuristic; // heuristic + @ConfigOption(description = "instance of heuristic to use", defaultValue = "TreeInductionHeuristics") + protected TreeInductionHeuristics heuristic; // heuristic //protected LengthLimitedRefinementOperator operator ;// refinement operator + @ConfigOption(description = "refinement operator instance to use", defaultValue = "DLTreesRefinementOperator") protected RefinementOperator operator; //private KnowledgeBase kb; @@ -109,7 +108,7 @@ public void setNonSpecifityControl(boolean nonSpecifityControl) { protected double prPos; protected double prNeg; //protected OWLClassExpression classToDescribe; //target concept - //protected DLTreeHeuristics heuristic; // heuristic + //protected DLTreeHeuristics heuristic; // heuristic //protected LengthLimitedRefinementOperator operator ;// refinement operator //protected RefinementOperator operator; @@ -176,12 +175,13 @@ public void setOperator(RefinementOperator operator) { + @Override public void init() throws ComponentInitException{ - //inizialization + //inizialization // TODO Auto-generated method stub - baseURI = reasoner.getBaseURI(); - prefixes = reasoner.getPrefixes(); + baseURI = reasoner.getBaseURI(); + prefixes = reasoner.getPrefixes(); // if no one injected a heuristic, we use a default one if(heuristic == null) { @@ -193,9 +193,9 @@ public void init() throws ComponentInitException{ } if(operator == null) { - // default operator + // default operator operator= new DLTreesRefinementOperator((PosNegUndLP)super.learningProblem, getReasoner(), 4); - //operator = new DLTreesRefinementOperator( this.learningProblem,reasoner,4); + //operator = new DLTreesRefinementOperator( this.learningProblem,reasoner,4); ((DLTreesRefinementOperator)operator).setReasoner(reasoner); ((DLTreesRefinementOperator)operator).setBeam(4); // default value //System.out.println("Refinement operator"+operator); @@ -222,19 +222,19 @@ public void init() throws ComponentInitException{ @SuppressWarnings({ "unchecked", "rawtypes" }) public DSTDLTree induceDSTDLTree - (SortedSet posExs, SortedSet negExs, SortedSet undExs) { + (SortedSet posExs, SortedSet negExs, SortedSet undExs) { - Npla,SortedSet,SortedSet, Integer, Double, Double> examples = new Npla,SortedSet,SortedSet, Integer, Double, Double>(posExs, negExs, undExs, beam, prPos, prNeg); + Npla,SortedSet,SortedSet, Integer, Double, Double> examples = new Npla<>(posExs, negExs, undExs, beam, prPos, prNeg); DSTDLTree tree = new DSTDLTree(); // new (sub)tree - Stack,SortedSet,SortedSet, Integer, Double, Double>>> stack= new Stack,SortedSet,SortedSet, Integer, Double, Double>>>(); - Couple,SortedSet,SortedSet, Integer, Double, Double>> toInduce= new Couple,SortedSet,SortedSet, Integer, Double, Double>>(); + Stack,SortedSet,SortedSet, Integer, Double, Double>>> stack= new Stack<>(); + Couple,SortedSet,SortedSet, Integer, Double, Double>> toInduce= new Couple<>(); toInduce.setFirstElement(tree); toInduce.setSecondElement(examples); stack.push(toInduce); - Stack lastTrees= new Stack(); // for refine hierarchically a concept + Stack lastTrees= new Stack<>(); // for refine hierarchically a concept while (!stack.isEmpty()){ @@ -250,24 +250,24 @@ public void init() throws ComponentInitException{ int psize = posExs.size(); int nsize = negExs.size(); int usize = undExs.size(); - System.out.printf("Learning problem\t p:%d\t n:%d\t u:%d\t prPos:%4f\t prNeg:%4f\n", + System.out.printf("Learning problem\t p:%d\t n:%d\t u:%d\t prPos:%4f\t prNeg:%4f\n", psize, nsize, usize, prPos, prNeg); //build the BBA for the current node - ArrayList frame = new ArrayList(); + ArrayList frame = new ArrayList<>(); frame.add(-1); frame.add(1); MassFunction mass= new MassFunction(frame); - ArrayList positive= new ArrayList(); + ArrayList positive= new ArrayList<>(); positive.add(1); double positiveValue = (double)psize/(psize+ nsize+usize); if( (psize+ nsize+usize)==0){ positiveValue= prPos; } mass.setValues(positive, positiveValue); - ArrayList negative= new ArrayList(); + ArrayList negative= new ArrayList<>(); negative.add(-1); double negativeValue = (double)nsize/(psize+ nsize+usize); if( (psize+ nsize+usize)==0){ @@ -307,11 +307,11 @@ public void init() throws ComponentInitException{ currentTree.setRoot(dataFactory.getOWLThing(), mass); // set positive lea // return tree; } - else if (perPos==0 && perNeg > puritythreshold) { // no positive + else if (perPos==0 && perNeg > puritythreshold) { // no positive // System.out.println("NoThing as leaf"); currentTree.setRoot(dataFactory.getOWLNothing(), mass); // set negative leaf // return tree; - } + } else{ //System.out.println("Non specificity: "+nonSpecifityControl); if (!nonSpecifityControl){ @@ -330,7 +330,7 @@ else if (perPos==0 && perNeg > puritythreshold) { // no positive // dlTreesRefinementOperator.refine(dataFactory.getOWLThing(), posExs, negExs); System.out.println("Refinement:"+refine); - ArrayList generateNewConcepts = new ArrayList(refine); // a generic refinement operator + ArrayList generateNewConcepts = new ArrayList<>(refine); // a generic refinement operator OWLClassExpression[] cConcepts = new OWLClassExpression[generateNewConcepts.size()]; cConcepts= generateNewConcepts.toArray(cConcepts); @@ -344,19 +344,19 @@ else if (perPos==0 && perNeg > puritythreshold) { // no positive else newRootConcept= heuristic.selectWorstConceptDST(cConcepts, posExs, negExs, undExs, prPos, prNeg); MassFunction refinementMass = newRootConcept.getSecondElement(); - //System.out.println(newRootConcept.getFirstElement()+"----"+refinementMass); - SortedSet posExsT = new TreeSet();; - SortedSet negExsT =new TreeSet();; - SortedSet undExsT =new TreeSet(); - SortedSet posExsF =new TreeSet(); - SortedSet negExsF =new TreeSet(); - SortedSet undExsF = new TreeSet(); + //System.out.println(newRootConcept.getFirstElement()+"----"+refinementMass); + SortedSet posExsT = new TreeSet<>(); + SortedSet negExsT = new TreeSet<>(); + SortedSet undExsT = new TreeSet<>(); + SortedSet posExsF = new TreeSet<>(); + SortedSet negExsF = new TreeSet<>(); + SortedSet undExsF = new TreeSet<>(); Split.split(newRootConcept.getFirstElement(), dataFactory, reasoner, posExs, negExs, undExs, posExsT, negExsT, undExsT, posExsF, negExsF, undExsF); // select node concept - currentTree.setRoot(newRootConcept.getFirstElement(), refinementMass); + currentTree.setRoot(newRootConcept.getFirstElement(), refinementMass); // undExsT = union(undExsT, // tree.setPosTree(induceDSTDLTree(posExsT, negExsT, undExsT, dim, prPos, prNeg)); @@ -366,13 +366,13 @@ else if (perPos==0 && perNeg > puritythreshold) { // no positive DSTDLTree negTree= new DSTDLTree(); // recursive calls simulation currentTree.setPosTree(posTree); currentTree.setNegTree(negTree); - Npla, SortedSet, SortedSet, Integer, Double, Double> npla1 = new Npla, SortedSet,SortedSet, Integer, Double, Double>(posExsT, negExsT, undExsT, beam, perPos, perNeg); - Npla, SortedSet,SortedSet, Integer, Double, Double> npla2 = new Npla, SortedSet,SortedSet, Integer, Double, Double>(posExsF, negExsF, undExsF, beam, perPos, perNeg); - Couple, SortedSet,SortedSet, Integer, Double, Double>> pos= new Couple, SortedSet,SortedSet, Integer, Double, Double>>(); + Npla, SortedSet, SortedSet, Integer, Double, Double> npla1 = new Npla<>(posExsT, negExsT, undExsT, beam, perPos, perNeg); + Npla, SortedSet,SortedSet, Integer, Double, Double> npla2 = new Npla<>(posExsF, negExsF, undExsF, beam, perPos, perNeg); + Couple, SortedSet,SortedSet, Integer, Double, Double>> pos= new Couple<>(); pos.setFirstElement(posTree); pos.setSecondElement(npla1); // negative branch - Couple, SortedSet,SortedSet, Integer, Double, Double>> neg= new Couple, SortedSet,SortedSet, Integer, Double, Double>>(); + Couple, SortedSet,SortedSet, Integer, Double, Double>> neg= new Couple<>(); neg.setFirstElement(negTree); neg.setSecondElement(npla2); stack.push(neg); @@ -380,12 +380,12 @@ else if (perPos==0 && perNeg > puritythreshold) { // no positive lastTrees.push(currentTree); } - else if(mass.getNonSpecificityMeasureValue()<0.1){ + else if(mass.getNonSpecificityMeasureValue()<0.1){ //System.out.println(); DLTreesRefinementOperator dlTreesRefinementOperator = (DLTreesRefinementOperator)operator; //System.out.println("is null?: "+dlTreesRefinementOperator==null); - //Set + //Set //refine = dlTreesRefinementOperator.refine(dataFactory.getOWLThing(), posExs, negExs); Set refine = null; @@ -405,25 +405,25 @@ else if(mass.getNonSpecificityMeasureValue()<0.1){ if (dlTreesRefinementOperator.getRo() ==DLTreesRefinementOperator.ORIGINAL) // 3: the original refinement operator for terminological trees newRootConcept= heuristic.selectBestConceptDST(cConcepts, posExs, negExs, undExs, prPos, prNeg); else - newRootConcept= heuristic.selectWorstConceptDST(cConcepts, posExs, negExs, undExs, prPos, prNeg); // otherwise select the worst concept + newRootConcept= heuristic.selectWorstConceptDST(cConcepts, posExs, negExs, undExs, prPos, prNeg); // otherwise select the worst concept //heuristic.selectBestConceptDST(cConcepts, posExs, negExs, undExs, prPos, prNeg); MassFunction refinementMass = newRootConcept.getSecondElement(); - //logger.debug(newRootConcept.getFirstElement()+"----"+refinementMass); - SortedSet posExsT = new TreeSet(); - SortedSet negExsT = new TreeSet(); - SortedSet undExsT = new TreeSet(); - SortedSet posExsF = new TreeSet(); - SortedSet negExsF = new TreeSet(); - SortedSet undExsF = new TreeSet(); + //logger.debug(newRootConcept.getFirstElement()+"----"+refinementMass); + SortedSet posExsT = new TreeSet<>(); + SortedSet negExsT = new TreeSet<>(); + SortedSet undExsT = new TreeSet<>(); + SortedSet posExsF = new TreeSet<>(); + SortedSet negExsF = new TreeSet<>(); + SortedSet undExsF = new TreeSet<>(); //split(newRootConcept.getFirstElement(), posExs, negExs, undExs, posExsT, negExsT, undExsT, posExsF, negExsF, undExsF); Split.split(newRootConcept.getFirstElement(), dataFactory, reasoner, posExs, negExs, undExs, posExsT, negExsT, undExsT, posExsF, negExsF, undExsF); // select node concept - //tree.setRoot(newRootConcept.getFirstElement(), refinementMass); + //tree.setRoot(newRootConcept.getFirstElement(), refinementMass); currentTree.setRoot(newRootConcept.getFirstElement(), refinementMass); // undExsT = union(undExsT, // tree.setPosTree(induceDSTDLTree(posExsT, negExsT, undExsT, dim, prPos, prNeg)); @@ -436,13 +436,13 @@ else if(mass.getNonSpecificityMeasureValue()<0.1){ currentTree.setPosTree(posTree); currentTree.setNegTree(negTree); - Npla, SortedSet, SortedSet, Integer, Double, Double> npla1 = new Npla,SortedSet,SortedSet, Integer, Double, Double>(posExsT, negExsT, undExsT, beam, perPos, perNeg); - Npla, SortedSet, SortedSet, Integer, Double, Double> npla2 = new Npla,SortedSet,SortedSet, Integer, Double, Double>(posExsF, negExsF, undExsF, beam, perPos, perNeg); - Couple,SortedSet,SortedSet, Integer, Double, Double>> pos= new Couple,SortedSet,SortedSet, Integer, Double, Double>>(); + Npla, SortedSet, SortedSet, Integer, Double, Double> npla1 = new Npla<>(posExsT, negExsT, undExsT, beam, perPos, perNeg); + Npla, SortedSet, SortedSet, Integer, Double, Double> npla2 = new Npla<>(posExsF, negExsF, undExsF, beam, perPos, perNeg); + Couple,SortedSet,SortedSet, Integer, Double, Double>> pos= new Couple<>(); pos.setFirstElement(posTree); pos.setSecondElement(npla1); // negative branch - Couple,SortedSet,SortedSet, Integer, Double, Double>> neg= new Couple,SortedSet,SortedSet, Integer, Double, Double>>(); + Couple,SortedSet,SortedSet, Integer, Double, Double>> neg= new Couple<>(); neg.setFirstElement(negTree); neg.setSecondElement(npla2); stack.push(neg); @@ -454,15 +454,15 @@ else if(mass.getNonSpecificityMeasureValue()<0.1){ currentTree.setRoot(dataFactory.getOWLThing(), mass); // set positive leaf // return tree; } - else {// no positive + else {// no positive currentTree.setRoot(dataFactory.getOWLNothing(), mass); // set negative leaf // return tree; - } + } } } - } + } } @@ -482,7 +482,7 @@ private void classifyExampleDST(List>> list // System.out.println("BBA "+m); - Stack stack= new Stack(); + Stack stack= new Stack<>(); stack.push(tree); @@ -490,11 +490,11 @@ private void classifyExampleDST(List>> list while (!stack.isEmpty()){ DSTDLTree currenttree=stack.pop(); - OWLClassExpression rootClass = currenttree.getRoot(); + OWLClassExpression rootClass = currenttree.getRoot(); MassFunction m= currenttree.getRootBBA(); if (rootClass.equals(dataFactory.getOWLThing())){ // System.out.println("Caso 1"); - Couple> result=new Couple>(); + Couple> result= new Couple<>(); result.setFirstElement(+1); result.setSecondElement(m); list.add(result); @@ -511,23 +511,23 @@ private void classifyExampleDST(List>> list } else if (rootClass.equals(dataFactory.getOWLNothing())){ // System.out.println("Caso 2"); - Couple> result=new Couple>(); + Couple> result= new Couple<>(); result.setFirstElement(-1); result.setSecondElement(m); list.add(result); - } + } else if (reasoner.hasType(rootClass, indTestEx)){ //System.out.println(kb.getReasoner().hasType(kb.getIndividuals()[indTestEx], rootClass)); if (currenttree.getPosSubTree()!=null){ -// classifyExampleDST( list, indTestEx, tree.getPosSubTree()); +// classifyExampleDST( list, indTestEx, tree.getPosSubTree()); stack.push(currenttree.getPosSubTree()); // System.out.println("------"); } else{ // System.out.println("Caso 4"); - Couple> result=new Couple>(); + Couple> result= new Couple<>(); result.setFirstElement(+1); result.setSecondElement(m); list.add(result); @@ -543,7 +543,7 @@ else if (reasoner.hasType(dataFactory.getOWLObjectComplementOf(rootClass), indTe } else{ // System.out.println("Caso 6"+ tree); - Couple> result=new Couple>(); + Couple> result= new Couple<>(); result.setFirstElement(-1); result.setSecondElement(m); list.add(result); @@ -561,10 +561,10 @@ else if (reasoner.hasType(dataFactory.getOWLObjectComplementOf(rootClass), indTe } else{ // System.out.println("Caso 8"); - Couple> result=new Couple>(); + Couple> result= new Couple<>(); result.setFirstElement(+1); result.setSecondElement(m); - list.add(result); + list.add(result); // System.out.println("ADdded"); } //System.out.println("---->"); @@ -574,7 +574,7 @@ else if (reasoner.hasType(dataFactory.getOWLObjectComplementOf(rootClass), indTe stack.push(currenttree.getNegSubTree()); } else{ - Couple> result=new Couple>(); + Couple> result= new Couple<>(); result.setFirstElement(-1); result.setSecondElement(m); list.add(result); @@ -621,7 +621,7 @@ public MassFunction getBBA(OWLIndividual indTestEx, EvidentialModel tre DSTDLTree model= (DSTDLTree) tree; // only classification with DST trees are supported for now ArrayList>> list; // System.out.println("Tree \n"+ model); - list= new ArrayList>>(); + list= new ArrayList<>(); classifyExampleDST(list,indTestEx, model); // BBA from the reached leaves // System.out.println("Lista di foglie"); @@ -648,22 +648,20 @@ public MassFunction getBBA(OWLIndividual indTestEx, EvidentialModel tre /** - * Implementation of forcing criterion for the final class assignement - * @param results - * @param c + * Implementation of forcing criterion for the final class assignement * @param bba */ private int predict(MassFunction bba) { - ArrayList hypothesis= new ArrayList(); + ArrayList hypothesis= new ArrayList<>(); hypothesis.add(+1); double confirmationFunctionValuePos = bba.getConfirmationFunctionValue(hypothesis); // double confirmationFunctionValuePos = bba.calcolaBeliefFunction(ipotesi); // not concept - ArrayList hypothesis2= new ArrayList(); + ArrayList hypothesis2= new ArrayList<>(); hypothesis2.add(-1); double confirmationFunctionValueNeg = bba.getConfirmationFunctionValue(hypothesis2); // double confirmationFunctionValueNeg = bba.calcolaBeliefFunction(ipotesi2); - ArrayList hypothesis3= new ArrayList(); + ArrayList hypothesis3= new ArrayList<>(); hypothesis3.add(-1); hypothesis3.add(+1); double confirmationFunctionValueUnc = bba.getConfirmationFunctionValue(hypothesis3); @@ -696,7 +694,7 @@ public void start() { PosNegUndLP learningProblem2 = (PosNegUndLP)learningProblem; SortedSet posExs = (SortedSet)learningProblem2.getPositiveExample(); SortedSet negExs = (SortedSet)learningProblem2.getNegativeExample(); - SortedSet undExs = (SortedSet)learningProblem2.getUncertainExample(); + SortedSet undExs = (SortedSet)learningProblem2.getUncertainExample(); //System.out.printf("--- Query Concept #%d \n",c); diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/dst/DSTUtils.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/dst/DSTUtils.java index 99b81b83d1..ef99834009 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/dst/DSTUtils.java +++ b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/dst/DSTUtils.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.decisiontrees.dsttdt.dst; import java.util.ArrayList; @@ -13,14 +31,14 @@ public class DSTUtils { public static MassFunction getBBA(int posExs, int negExs,int undExs) { - ArrayList set = new ArrayList(); + ArrayList set = new ArrayList<>(); set.add(-1); set.add(1); - MassFunction mass= new MassFunction(set); - ArrayList positive= new ArrayList(); + MassFunction mass= new MassFunction<>(set); + ArrayList positive= new ArrayList<>(); positive.add(1); mass.setValues(positive,(double) posExs/(posExs+ negExs+undExs)); - ArrayList negative= new ArrayList(); + ArrayList negative= new ArrayList<>(); negative.add(-1); mass.setValues(negative, (double)negExs/(posExs+ negExs+undExs)); mass.setValues(set, (double)undExs/(posExs+ negExs+undExs)); diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/dst/MassFunction.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/dst/MassFunction.java index c5706a02d4..9f9b742d89 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/dst/MassFunction.java +++ b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/dst/MassFunction.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.decisiontrees.dsttdt.dst; @@ -10,7 +28,6 @@ * A class for representing a BBA * @author Giuseppe Rizzo * - * @param * @param */ public class MassFunction > { @@ -76,7 +93,7 @@ public void setValues(List label,double value){ /** * Returns the value of a BBA for a specific element of class - * @param class + * @param label * @return the value of a bba or NaN */ public double getValue(List label){ diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/dst/package-info.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/dst/package-info.java deleted file mode 100644 index 236f570fa2..0000000000 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/dst/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * - */ -/** - * @author Utente - * - */ -package org.dllearner.algorithms.decisiontrees.dsttdt.dst; \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/models/DSTDLTree.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/models/DSTDLTree.java index e2891f42ed..be81160d71 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/models/DSTDLTree.java +++ b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/models/DSTDLTree.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.decisiontrees.dsttdt.models; @@ -65,7 +83,7 @@ public DSTDLTree (OWLClassExpression c, MassFunction m) { } /** - * @param root the root to set + * @param concept the root concept to set */ @SuppressWarnings("rawtypes") public void setRoot(OWLClassExpression concept, MassFunction m) { @@ -138,7 +156,7 @@ public Object clone(){ private double getNodes(){ - ArrayList list = new ArrayList(); + ArrayList list = new ArrayList<>(); double num=0; if(root!=null){ list.add(root); @@ -184,8 +202,8 @@ private static void associate(DSTDLTree tree, OWLDataFactory df, OWLClass leaf, else{ //OWLDataFactory dataFactory = new OWLDataFactoryImpl(); // tail recursive calls - associate(tree.getPosSubTree(),df,leaf, (OWLClassExpression)(df.getOWLObjectIntersectionOf(currentConceptDescription, tree.root.concept)),set); - associate(tree.getNegSubTree(),df, leaf, (OWLClassExpression)(df.getOWLObjectIntersectionOf(currentConceptDescription, tree.root.concept)),set); + associate(tree.getPosSubTree(),df,leaf, df.getOWLObjectIntersectionOf(currentConceptDescription, tree.root.concept),set); + associate(tree.getNegSubTree(),df, leaf, df.getOWLObjectIntersectionOf(currentConceptDescription, tree.root.concept),set); } } @@ -199,7 +217,7 @@ private static void associate(DSTDLTree tree, OWLDataFactory df, OWLClass leaf, */ public static OWLClassExpression deriveDefinition(DSTDLTree tree, boolean conceptFromPositiveIstances){ - HashSet exp= new HashSet(); + HashSet exp= new HashSet<>(); OWLDataFactory dataFactory = new OWLDataFactoryImpl(); if (conceptFromPositiveIstances) diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/models/EvidentialModel.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/models/EvidentialModel.java index 9b937d1cd4..05f595d2e7 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/models/EvidentialModel.java +++ b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/models/EvidentialModel.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.decisiontrees.dsttdt.models; public interface EvidentialModel { diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/models/package-info.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/models/package-info.java deleted file mode 100644 index 9bb02b2bc4..0000000000 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/models/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * - */ -/** - * @author Giuseppe Rizzo - * - */ -package org.dllearner.algorithms.decisiontrees.dsttdt.models; \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/package-info.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/package-info.java deleted file mode 100644 index 5b0775122a..0000000000 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/dsttdt/package-info.java +++ /dev/null @@ -1,5 +0,0 @@ -/** - * DST Terminological Decision Tree Inducer - * - */ -package org.dllearner.algorithms.decisiontrees.dsttdt; \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/etrf/package-info.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/etrf/package-info.java deleted file mode 100644 index 4799eabff2..0000000000 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/etrf/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * Evidential Terminological Random Forest Inducer - */ -package org.dllearner.algorithms.decisiontrees.etrf; \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/heuristics/TreeInductionHeuristics.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/heuristics/TreeInductionHeuristics.java index 9f7b8b2004..bcc861e578 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/heuristics/TreeInductionHeuristics.java +++ b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/heuristics/TreeInductionHeuristics.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.decisiontrees.heuristics; import java.util.ArrayList; @@ -239,17 +257,17 @@ private int[] getSplitCounts(OWLClassExpression concept, SortedSet undExs) { int[] counts = new int[9]; - SortedSet posExsT = new TreeSet(); - SortedSet negExsT = new TreeSet(); - SortedSet undExsT = new TreeSet(); + SortedSet posExsT = new TreeSet<>(); + SortedSet negExsT = new TreeSet<>(); + SortedSet undExsT = new TreeSet<>(); - SortedSet posExsF = new TreeSet(); - SortedSet negExsF = new TreeSet(); - SortedSet undExsF = new TreeSet(); + SortedSet posExsF = new TreeSet<>(); + SortedSet negExsF = new TreeSet<>(); + SortedSet undExsF = new TreeSet<>(); - SortedSet posExsU = new TreeSet(); - SortedSet negExsU = new TreeSet(); - SortedSet undExsU = new TreeSet(); + SortedSet posExsU = new TreeSet<>(); + SortedSet negExsU = new TreeSet<>(); + SortedSet undExsU = new TreeSet<>(); splitGroup(concept,posExs,posExsT,posExsF,posExsU); splitGroup(concept,negExs,negExsT,negExsF,negExsU); @@ -275,9 +293,9 @@ protected void split(OWLClassExpression concept, SortedSet posExs SortedSet posExsT, SortedSet negExsT, SortedSet undExsT, SortedSet posExsF, SortedSet negExsF, SortedSet undExsF) { - SortedSet posExsU = new TreeSet(); - SortedSet negExsU = new TreeSet(); - SortedSet undExsU = new TreeSet(); + SortedSet posExsU = new TreeSet<>(); + SortedSet negExsU = new TreeSet<>(); + SortedSet undExsU = new TreeSet<>(); splitGroup(concept,posExs,posExsT,posExsF,posExsU); splitGroup(concept,negExs,negExsT,negExsF,negExsU); @@ -363,7 +381,7 @@ public Couple selectBestConceptDST(OWLClassEx } logger.debug("best gain: %f \t split #%d\n", bestNonSpecificity, bestConceptIndex); - Couple name = new Couple(); + Couple name = new Couple<>(); name.setFirstElement(concepts[bestConceptIndex]); name.setSecondElement(bestBba); return name; @@ -429,7 +447,7 @@ public Couple selectWorstConceptDST(OWLClassE } logger.debug("best gain: %f \t split #%d\n", bestNonSpecificity, bestConceptIndex); - Couple name = new Couple(); + Couple name = new Couple<>(); name.setFirstElement(concepts[bestConceptIndex]); name.setSecondElement(bestBba); return name; diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/heuristics/package-info.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/heuristics/package-info.java deleted file mode 100644 index 1d3f881797..0000000000 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/heuristics/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * - */ -/** - * @author Giuseppe Rizzo - * - */ -package org.dllearner.algorithms.decisiontrees.heuristics; \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/package-info.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/package-info.java deleted file mode 100644 index b6beba14b4..0000000000 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * - */ -/** - * @author Lorenz Buehmann - * - */ -package org.dllearner.algorithms.decisiontrees; \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/refinementoperators/DLTreesRefinementOperator.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/refinementoperators/DLTreesRefinementOperator.java index 04e0a3a7e2..43e050f77e 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/refinementoperators/DLTreesRefinementOperator.java +++ b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/refinementoperators/DLTreesRefinementOperator.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.decisiontrees.refinementoperators; import java.util.ArrayList; @@ -43,8 +61,11 @@ public class DLTreesRefinementOperator implements InstanceBasedRefinementOperato //KnowledgeBase kb; private static final double d = 0.5; + // @NoConfigOption private ArrayList allConcepts; + // @NoConfigOption private ArrayList allRoles; + @ConfigOption(description = "the learning problem instance to use") private PosNegLP lp; private Random generator; public static final int ORIGINAL=3; //predefined constants @@ -52,7 +73,8 @@ public class DLTreesRefinementOperator implements InstanceBasedRefinementOperato public static final int PSI=2; //private OWLDataFactory dataFactory; - private Reasoner r; + @ConfigOption(description = "the reasoner instance to use") + private Reasoner reasoner; protected OWLDataFactory dataFactory = new OWLDataFactoryImpl(); @ConfigOption(defaultValue = "5", name = "beam") @@ -60,9 +82,9 @@ public class DLTreesRefinementOperator implements InstanceBasedRefinementOperato @ConfigOption(defaultValue = "1", name = "kindOperator") private int ro; // the name of a refinement operator - // // - // + // + // public int getRo() { return ro; } @@ -83,11 +105,11 @@ public DLTreesRefinementOperator() { public DLTreesRefinementOperator(PosNegLP lp, AbstractReasonerComponent reasoner, int beam) { super(); // TODO Auto-generated constructor stub - r=reasoner; + this.reasoner=reasoner; //System.out.println("is Reasoner null? "+reasoner==null); - allConcepts=new ArrayList(reasoner.getClasses()); + allConcepts= new ArrayList<>(reasoner.getClasses()); //System.out.println("all+ Concepts: "+allConcepts.size()); - allRoles= new ArrayList(reasoner.getObjectProperties()); + allRoles= new ArrayList<>(reasoner.getObjectProperties()); //this.beam=beam; // set the maximum number of candidates that can be generated this.lp=lp; generator= new Random(2); @@ -130,7 +152,7 @@ public void setAllRoles(ArrayList allRoles) { /** * Random concept generation - * @return + * @return */ public OWLClassExpression getRandomConcept() { @@ -155,12 +177,12 @@ public OWLClassExpression getRandomConcept() { else newConcept = dataFactory.getOWLObjectSomeValuesFrom(role, newConceptBase); } - else + else newConcept = dataFactory.getOWLObjectComplementOf(newConceptBase); } } - } while (!(r.getIndividuals(newConcept).size()>0) ); + } while (!(reasoner.getIndividuals(newConcept).size()>0) ); } else{ @@ -180,19 +202,19 @@ public OWLClassExpression getRandomConcept() { newConcept = dataFactory.getOWLObjectSomeValuesFrom(role, newConceptBase); } } // else ext - else //if (KnowledgeBase.generator.nextDouble() > 0.8) { + else //if (KnowledgeBase.generator.nextDouble() > 0.8) { newConcept = dataFactory.getOWLObjectComplementOf(newConcept); - } while (!(r.getIndividuals(newConcept).size()>0)); + } while (!(reasoner.getIndividuals(newConcept).size()>0)); } //System.out.println("*********"); - return newConcept; + return newConcept; } public SortedSetgenerateNewConcepts(SortedSet posExs, SortedSet negExs, boolean seed) { logger.info("Generating node concepts "); - TreeSet rConcepts = new TreeSet(); + TreeSet rConcepts = new TreeSet<>(); OWLClassExpression newConcept=null; boolean emptyIntersection; @@ -207,12 +229,12 @@ public OWLClassExpression getRandomConcept() { logger.info(c+"- New Concept: "+newConcept); SortedSet individuals; - individuals = (r.getIndividuals(newConcept)); + individuals = (reasoner.getIndividuals(newConcept)); Iterator instIterator = individuals.iterator(); while (emptyIntersection && instIterator.hasNext()) { Node nextInd = (Node) instIterator.next(); int index = -1; - ArrayList individuals2 = new ArrayList(r.getIndividuals()); + ArrayList individuals2 = new ArrayList<>(reasoner.getIndividuals()); for (int i=0; index<0 && i(reasoner.getClasses()); + allConcepts= new ArrayList<>(reasoner.getClasses()); //System.out.println("all+ Concepts: "+allConcepts.size()); if (allRoles==null) - allRoles= new ArrayList(reasoner.getObjectProperties()); + allRoles= new ArrayList<>(reasoner.getObjectProperties()); } + @Override public void setReasoner(Reasoner reasoner) { // TODO Auto-generated method stub - this.r= reasoner; + this.reasoner= reasoner; if (allConcepts==null) - allConcepts=new ArrayList(reasoner.getClasses()); + allConcepts= new ArrayList<>(reasoner.getClasses()); //System.out.println("all+ Concepts: "+allConcepts.size()); if (allRoles==null) - allRoles= new ArrayList(reasoner.getObjectProperties()); + allRoles= new ArrayList<>(reasoner.getObjectProperties()); } @@ -306,12 +329,13 @@ public void setReasoner(Reasoner reasoner) { + @Override public Set refine(OWLClassExpression definition, SortedSet posExs, SortedSet negExs) { Set children; int n=-1; // initialization - ArrayList childrenList=new ArrayList();; + ArrayList childrenList= new ArrayList<>(); OWLClassExpression def= definition; if ((!definition.isOWLThing())&&(!definition.isOWLNothing())){ @@ -321,7 +345,7 @@ public Set refine(OWLClassExpression definition, SortedSet0){ n= rg.nextInt(children.size()); - childrenList=new ArrayList(children); + childrenList= new ArrayList<>(children); } } @@ -329,11 +353,11 @@ public Set refine(OWLClassExpression definition, SortedSet refine(OWLClassExpression definition, SortedSet refine = psiDown.refine(definition); + Set refine = psiDown.refine(definition); return refine; } - else + else return (generateNewConcepts(posExs, negExs, false)); diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/refinementoperators/InstanceBasedRefinementOperator.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/refinementoperators/InstanceBasedRefinementOperator.java index e844bc0238..2c60cead89 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/refinementoperators/InstanceBasedRefinementOperator.java +++ b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/refinementoperators/InstanceBasedRefinementOperator.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.decisiontrees.refinementoperators; diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/refinementoperators/package-info.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/refinementoperators/package-info.java deleted file mode 100644 index 3960840d1d..0000000000 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/refinementoperators/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * - */ -/** - * @author Utente - * - */ -package org.dllearner.algorithms.decisiontrees.refinementoperators; \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/tdt/AbstractTDTClassifier.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/tdt/AbstractTDTClassifier.java index 67d3416d36..4c36117711 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/tdt/AbstractTDTClassifier.java +++ b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/tdt/AbstractTDTClassifier.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.decisiontrees.tdt; import java.util.ArrayList; @@ -5,14 +23,10 @@ import java.util.List; import java.util.SortedSet; import java.util.Stack; -import java.util.TreeSet; -import org.apache.commons.collections.SetUtils; -import org.dllearner.algorithms.celoe.CELOE; import org.dllearner.algorithms.decisiontrees.heuristics.TreeInductionHeuristics; import org.dllearner.algorithms.decisiontrees.refinementoperators.DLTreesRefinementOperator; import org.dllearner.algorithms.decisiontrees.tdt.model.DLTree; -import org.dllearner.algorithms.decisiontrees.utils.Split; import org.dllearner.core.AbstractCELA; import org.dllearner.core.AbstractClassExpressionLearningProblem; import org.dllearner.core.AbstractReasonerComponent; @@ -22,7 +36,6 @@ import org.dllearner.refinementoperators.RefinementOperator; import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLObjectComplementOf; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -96,10 +109,10 @@ public void setOperator(RefinementOperator operator) { @ConfigOption(defaultValue = "0.05", name = "PURITY_THRESHOLD", description = "Purity threshold for setting a leaf") protected double puritythreshold; - //@ConfigOption(defaultValue = "4", name = "beam", description = "value for limiting the number of generated concepts") + //@ConfigOption(defaultValue = "4", name = "beam") //protected int beam; - @ConfigOption(defaultValue = "false", name = "BINARYCLASSIFICATION", description = "value for limiting the number of generated concepts") + @ConfigOption(defaultValue = "false", name = "BINARYCLASSIFICATION", description = "if it is a binary classification problem") protected boolean binaryClassification; @ConfigOption(defaultValue = "false", name = "ccp", description = "value for limiting the number of generated concepts") @@ -118,10 +131,13 @@ public void setCcp(boolean ccp) { protected boolean missingValueTreatmentForTDT; protected double prPos; protected double prNeg; + @ConfigOption(description = "concept for splitting undefined examples into positive and negative for binary classification problems") protected OWLClassExpression classToDescribe; //target concept - protected TreeInductionHeuristics heuristic; // heuristic + @ConfigOption(description = "the heuristic instance to use", defaultValue = "TreeInductionHeuristics") + protected TreeInductionHeuristics heuristic; // heuristic //protected LengthLimitedRefinementOperator operator ;// refinement operator + @ConfigOption(description = "the refinement operator instance to use", defaultValue = "DLTreesRefinementOperator") protected RefinementOperator operator; @@ -159,8 +175,8 @@ public void start() { @Override public void init() throws ComponentInitException { // TODO Auto-generated method stub - baseURI = reasoner.getBaseURI(); - prefixes = reasoner.getPrefixes(); + baseURI = reasoner.getBaseURI(); + prefixes = reasoner.getPrefixes(); // if no one injected a heuristic, we use a default one if(heuristic == null) { @@ -175,11 +191,11 @@ public void init() throws ComponentInitException { if(operator == null) { System.out.println("OPERATOR:"+operator==null); -// // default operator - operator = new DLTreesRefinementOperator(); +// // default operator + operator = new DLTreesRefinementOperator(); ((DLTreesRefinementOperator)operator).setReasoner(reasoner); ((DLTreesRefinementOperator)operator).setBeam(10); // default value -//// +//// //// if(operator instanceof CustomStartRefinementOperator) { //// ((CustomStartRefinementOperator)operator).setStartClass(startClass); //// } @@ -189,8 +205,8 @@ public void init() throws ComponentInitException { operator.init(); //System.out.println(operator==null); -// -// +// +// } //start to learn the new current concept description @@ -226,7 +242,7 @@ public int classify(OWLIndividual indTestEx, DLTree trees) { //int length = testConcepts!=null?testConcepts.length:1; //for (int c=0; c < length; c++) { if (missingValueTreatmentForTDT){ - ArrayList list= new ArrayList(); + ArrayList list= new ArrayList<>(); return classifyExample(list,indTestEx, trees); } @@ -243,7 +259,7 @@ public int classify(OWLIndividual indTestEx, DLTree trees) { public int classifyExample(OWLIndividual indTestEx, DLTree tree) { - Stack stack= new Stack(); + Stack stack= new Stack<>(); //OWLDataFactory dataFactory = kb.getDataFactory(); stack.add(tree); int result=0; @@ -270,7 +286,7 @@ else if (reasoner.hasType(dataFactory.getOWLObjectComplementOf(rootClass), indTe stack.push(currentTree.getNegSubTree()); else { stop=true; - result=0; + result=0; } @@ -292,7 +308,7 @@ else if (rootClass.equals(dataFactory.getOWLNothing())){ }else if (reasoner.hasType(rootClass, indTestEx)) stack.push(currentTree.getPosSubTree()); - else + else stack.push(currentTree.getNegSubTree()); // for those kb having no full complement } @@ -311,7 +327,7 @@ else if (rootClass.equals(dataFactory.getOWLNothing())){ * @return */ public int classifyExample(List list, OWLIndividual indTestEx, DLTree tree) { - Stack stack= new Stack(); + Stack stack= new Stack<>(); //OWLDataFactory dataFactory = kb.getDataFactory(); stack.add(tree); int result=0; @@ -338,12 +354,12 @@ else if (reasoner.hasType(dataFactory.getOWLObjectComplementOf(rootClass), indTe stack.push(currentTree.getNegSubTree()); else { // stop=true; - result=0; + result=0; stack.push(currentTree.getPosSubTree()); stack.push(currentTree.getNegSubTree()); } - }; + } int posFr= Collections.frequency(list, +1); int negFr= Collections.frequency(list, -1); @@ -366,7 +382,7 @@ else if (reasoner.hasType(dataFactory.getOWLObjectComplementOf(rootClass), indTe int bestConceptIndex = 0; counts = getSplitCounts(concepts[0], posExs, negExs, undExs); - System.out.printf("%4s\t p:%d n:%d u:%d\t p:%d n:%d u:%d\t p:%d n:%d u:%d\t ", + System.out.printf("%4s\t p:%d n:%d u:%d\t p:%d n:%d u:%d\t p:%d n:%d u:%d\t ", "#"+0, counts[0], counts[1], counts[2], counts[3], counts[4], counts[5], counts[6], counts[7], counts[8]); double bestGain = gain(counts, prPos, prNeg); @@ -378,7 +394,7 @@ else if (reasoner.hasType(dataFactory.getOWLObjectComplementOf(rootClass), indTe for (int c=1; c undExsU = new ArrayList(); splitGroup(concept,posExs,posExsT,posExsF,posExsU); - splitGroup(concept,negExs,negExsT,negExsF,negExsU); - splitGroup(concept,undExs,undExsT,undExsF,undExsU); + splitGroup(concept,negExs,negExsT,negExsF,negExsU); + splitGroup(concept,undExs,undExsT,undExsF,undExsU); - counts[POSITIVE_INSTANCE_CHECK_TRUE] = posExsT.size(); - counts[NEGATIVE_INSTANCE_CHECK_TRUE] = negExsT.size(); - counts[UNCERTAIN_INSTANCE_CHECK_TRUE] = undExsT.size(); - counts[POSITIVE_INSTANCE_CHECK_FALSE] = posExsF.size(); + counts[POSITIVE_INSTANCE_CHECK_TRUE] = posExsT.size(); + counts[NEGATIVE_INSTANCE_CHECK_TRUE] = negExsT.size(); + counts[UNCERTAIN_INSTANCE_CHECK_TRUE] = undExsT.size(); + counts[POSITIVE_INSTANCE_CHECK_FALSE] = posExsF.size(); counts[NEGATIVE_INSTANCE_CHECK_FALSE] = negExsF.size(); counts[UNCERTAIN_INSTANCE_CHECK_FALSE] = undExsF.size(); - counts[POSITIVE_INSTANCE_CHECK_UNC] = posExsU.size(); + counts[POSITIVE_INSTANCE_CHECK_UNC] = posExsU.size(); counts[NEGATIVE_INSTANCE_CHECK_UNC] = negExsU.size(); counts[UNCERTAIN_INSTANCE_CHECK_UNC] = undExsU.size(); // for(int i=0; i. + */ package org.dllearner.algorithms.decisiontrees.tdt; import java.util.ArrayList; @@ -83,15 +101,15 @@ public DLTree induceDLTree(SortedSet posExs, SortedSet, SortedSet, SortedSet, Integer, Double, Double> examples = new Npla,SortedSet,SortedSet, Integer, Double, Double>(posExs, negExs, undExs, 10, prPos, prNeg); + Npla, SortedSet, SortedSet, Integer, Double, Double> examples = new Npla<>(posExs, negExs, undExs, 10, prPos, prNeg); DLTree tree = new DLTree(); // new (sub)tree - Stack,SortedSet,SortedSet, Integer, Double, Double>>> stack= new Stack, SortedSet,SortedSet, Integer, Double, Double>>>(); - Couple,SortedSet,SortedSet, Integer, Double, Double>> toInduce= new Couple,SortedSet,SortedSet, Integer, Double, Double>>(); + Stack,SortedSet,SortedSet, Integer, Double, Double>>> stack= new Stack<>(); + Couple,SortedSet,SortedSet, Integer, Double, Double>> toInduce= new Couple<>(); toInduce.setFirstElement(tree); toInduce.setSecondElement(examples); stack.push(toInduce); - Stack lastTrees= new Stack(); // for refine hierarchically a concept + Stack lastTrees= new Stack<>(); // for refine hierarchically a concept while(!stack.isEmpty()){ //System.out.printf("Stack: %d \n",stack.size()); @@ -142,7 +160,7 @@ public DLTree induceDLTree(SortedSet posExs, SortedSet cConceptsL = new ArrayList(refine); + ArrayList cConceptsL = new ArrayList<>(refine); // cConceptsL= getRandomSelection(cConceptsL); // random selection of feature set @@ -155,12 +173,12 @@ public DLTree induceDLTree(SortedSet posExs, SortedSet posExsT = new TreeSet(); - SortedSet negExsT = new TreeSet(); - SortedSet undExsT = new TreeSet(); - SortedSet posExsF = new TreeSet(); - SortedSet negExsF = new TreeSet(); - SortedSet undExsF = new TreeSet(); + SortedSet posExsT = new TreeSet<>(); + SortedSet negExsT = new TreeSet<>(); + SortedSet undExsT = new TreeSet<>(); + SortedSet posExsF = new TreeSet<>(); + SortedSet negExsF = new TreeSet<>(); + SortedSet undExsF = new TreeSet<>(); Split.split(newRootConcept, dataFactory, reasoner, posExs, negExs, undExs, posExsT, negExsT, undExsT, posExsF, negExsF, undExsF); // select node concept @@ -172,14 +190,14 @@ public DLTree induceDLTree(SortedSet posExs, SortedSet, SortedSet, SortedSet, Integer, Double, Double> npla1 = new Npla,SortedSet,SortedSet, Integer, Double, Double>(posExsT, negExsT, undExsT, 10, perPos, perNeg); - Npla,SortedSet,SortedSet, Integer, Double, Double> npla2 = new Npla,SortedSet,SortedSet, Integer, Double, Double>(posExsF, negExsF, undExsF, 10, perPos, perNeg); - Couple,SortedSet,SortedSet, Integer, Double, Double>> pos= new Couple,SortedSet,SortedSet, Integer, Double, Double>>(); + Npla, SortedSet, SortedSet, Integer, Double, Double> npla1 = new Npla<>(posExsT, negExsT, undExsT, 10, perPos, perNeg); + Npla,SortedSet,SortedSet, Integer, Double, Double> npla2 = new Npla<>(posExsF, negExsF, undExsF, 10, perPos, perNeg); + Couple,SortedSet,SortedSet, Integer, Double, Double>> pos= new Couple<>(); pos.setFirstElement(posTree); pos.setSecondElement(npla1); // negative branch - Couple,SortedSet,SortedSet, Integer, Double, Double>> neg= new Couple,SortedSet,SortedSet, Integer, Double, Double>>(); + Couple,SortedSet,SortedSet, Integer, Double, Double>> neg= new Couple<>(); neg.setFirstElement(negTree); neg.setSecondElement(npla2); stack.push(neg); @@ -223,7 +241,7 @@ public void start() { System.out.println(negExs.size()); System.out.println(undExs.size()); if (binaryClassification){ - SortedSet allExamples= new TreeSet(); + SortedSet allExamples= new TreeSet<>(); allExamples.addAll(posExs); allExamples.addAll(negExs); allExamples.addAll(undExs); diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/tdt/model/AbstractModel.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/tdt/model/AbstractModel.java index b3a23fcc85..333b2f868e 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/tdt/model/AbstractModel.java +++ b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/tdt/model/AbstractModel.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.decisiontrees.tdt.model; public abstract class AbstractModel { diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/tdt/model/AbstractTree.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/tdt/model/AbstractTree.java index c0a30cfebc..44417acad0 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/tdt/model/AbstractTree.java +++ b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/tdt/model/AbstractTree.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.decisiontrees.tdt.model; diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/tdt/model/DLTree.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/tdt/model/DLTree.java index f42cbb8db8..f9dfa6811f 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/tdt/model/DLTree.java +++ b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/tdt/model/DLTree.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.decisiontrees.tdt.model; @@ -123,7 +141,7 @@ public DLTree (OWLClassExpression c) { } /** - * @param root the root to set + * @param concept the root concept to set */ public void setRoot(OWLClassExpression concept) { this.root = new DLNode(concept); @@ -158,7 +176,7 @@ public String toString() { // return root.concept.toString() + " ["+root.pos.toString()+" "+root.neg.toString()+"]"; String string=""; - Stack stack= new Stack(); + Stack stack= new Stack<>(); stack.push(this); DLTree currenttree=null; while(!stack.isEmpty()){ @@ -194,7 +212,7 @@ public DLTree getNegSubTree() { private double getNodes(){ - ArrayList list = new ArrayList(); + ArrayList list = new ArrayList<>(); double num=0; if(root!=null){ list.add(root); @@ -230,9 +248,9 @@ public double getComplexityMeasure() { public List getLeaves(){ - ArrayList leaves= new ArrayList(); + ArrayList leaves= new ArrayList<>(); - ArrayList list = new ArrayList(); + ArrayList list = new ArrayList<>(); if(root!=null){ list.add(this); @@ -271,8 +289,8 @@ private static void associate(DLTree tree, OWLDataFactory df, OWLClass leaf, OWL else{ //OWLDataFactory dataFactory = new OWLDataFactoryImpl(); // tail recursive calls - associate(tree.getPosSubTree(),df,leaf, (OWLClassExpression)(df.getOWLObjectIntersectionOf(currentConceptDescription, tree.root.concept)),set); - associate(tree.getNegSubTree(),df, leaf, (OWLClassExpression)(df.getOWLObjectIntersectionOf(currentConceptDescription, tree.root.concept)),set); + associate(tree.getPosSubTree(),df,leaf, df.getOWLObjectIntersectionOf(currentConceptDescription, tree.root.concept),set); + associate(tree.getNegSubTree(),df, leaf, df.getOWLObjectIntersectionOf(currentConceptDescription, tree.root.concept),set); } } @@ -285,7 +303,7 @@ private static void associate(DLTree tree, OWLDataFactory df, OWLClass leaf, OWL */ public static OWLClassExpression deriveDefinition(DLTree tree, boolean conceptFromPositiveIstances){ - HashSet exp= new HashSet(); + HashSet exp= new HashSet<>(); OWLDataFactory dataFactory = new OWLDataFactoryImpl(); if (conceptFromPositiveIstances) diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/tdt/model/package-info.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/tdt/model/package-info.java deleted file mode 100644 index d608c2a61c..0000000000 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/tdt/model/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * - */ -/** - * @author Giuseppe Rizzo - * - */ -package org.dllearner.algorithms.decisiontrees.tdt.model; \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/tdt/package-info.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/tdt/package-info.java deleted file mode 100644 index c455707a76..0000000000 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/tdt/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * Terminological Decision Tree Inducer - */ -package org.dllearner.algorithms.decisiontrees.tdt; \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/trf/package-info.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/trf/package-info.java deleted file mode 100644 index 3b178d6d84..0000000000 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/trf/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * Terminological Random Forests Inducer - */ -package org.dllearner.algorithms.decisiontrees.trf; \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Combination.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Combination.java index 0ff689b3fd..78f1727ebb 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Combination.java +++ b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Combination.java @@ -1,11 +1,21 @@ -/*\ - * Copyright 2006 Klaus Rogall, Hamburg, Germany (klaus.rogall@web.de). All rights reserved. - * _____________________________________________________________________________________________________________________ - * - * This class is "Open Source" as defined by the Open Source Initiative (OSI). You can redistribute it and/or modify it - * under the terms of the BSD License. The license text is appended to the end of this file. -\*/ - +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.decisiontrees.utils; import java.util.ArrayList; @@ -18,8 +28,7 @@ * Methoden zur Erzeugung von Kombinationen. */ -public class Combination extends Object -{ +public class Combination { /* ______________________________________________________________________________________________________________ *\ \* Konstanten */ @@ -72,7 +81,7 @@ private Combination() @SuppressWarnings({ "rawtypes", "unchecked" }) public static > List> findCombinations(Collection elements) { - List> result = new ArrayList>(); + List> result = new ArrayList<>(); for (int i = 0; i <= elements.size(); i++) result.addAll(findCombinations(elements, i)); @@ -98,7 +107,7 @@ public static > List> findCombinations(C public static > List> findCombinations(Collection elements, int n) { - List> result = new ArrayList>(); + List> result = new ArrayList<>(); if (n == 0) { @@ -117,7 +126,7 @@ public static > List> findCombinations(C continue; } - List list = new ArrayList(); + List list = new ArrayList<>(); list.addAll(combination); diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Couple.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Couple.java index c63ebd497b..42d6b6e3f6 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Couple.java +++ b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Couple.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.decisiontrees.utils; public class Couple { diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Generator.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Generator.java index 5d8248e837..773968be8e 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Generator.java +++ b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Generator.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.decisiontrees.utils; import java.util.Random; diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/MathUtils.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/MathUtils.java index cf40c99b25..87acfdfa3e 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/MathUtils.java +++ b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/MathUtils.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.decisiontrees.utils; diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Npla.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Npla.java index 73a4cf828e..f5cd8bcfa0 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Npla.java +++ b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Npla.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.decisiontrees.utils; /** * A class for represent a sixth-pla diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/SetUtils.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/SetUtils.java index 83462b916b..b12e198b12 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/SetUtils.java +++ b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/SetUtils.java @@ -1,5 +1,26 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.decisiontrees.utils; +import com.google.common.collect.Lists; +import org.apache.commons.collections15.ListUtils; + import java.util.ArrayList; import java.util.List; @@ -38,7 +59,7 @@ public static boolean isIn(T elem, List list){ public static List intersection(List list1, List list2){ // check the maximum lenght between list1 and list1 if(list1.size()<=list2.size()){ - List intersection= new ArrayList(); + List intersection= new ArrayList<>(); // if the element of list2 are contained in lista 2, list2 is the intersection for(Object elem: list2){ if(isIn((T)elem, list1)){ @@ -61,7 +82,6 @@ public static List intersection(List list1, List list2){ * @return */ public static List union(List list1,List list2){ - List result= intersection(list1, list2);// take the common elements between list 2 and list 2 for(T elem:list1){ //add the element that are in list1 but not in result yet @@ -86,11 +106,8 @@ public static List union(List list1,List list2){ * @return */ public static boolean areEquals(Listl1, List l2){ - // due insiemi sono uguali se uno � incluso nell'altro - if (l1.containsAll(l2)&&l2.containsAll(l1)) - return true; - - return false; + return l1.containsAll(l2) && l2.containsAll(l1); + } /** @@ -152,7 +169,7 @@ public static List[] getSubsets(List[] powerSet, int cardinality){ if(cardinality>Math.log10(powerSet.length)/Math.log10(2)) throw new RuntimeException("La cardinalit� � maggiore di"+(Math.log10(powerSet.length)/Math.log10(2))); - List> sottoinsiemi= new ArrayList>(); + List> sottoinsiemi= new ArrayList<>(); for(List elem:powerSet){ if(elem.size()==cardinality) sottoinsiemi.add(elem); diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Split.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Split.java index 0d74608438..d3ec83f12b 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Split.java +++ b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Split.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.decisiontrees.utils; import java.util.ArrayList; @@ -22,9 +40,9 @@ public static void split(OWLClassExpression concept, OWLDataFactory df, Abstrac SortedSet posExsT, SortedSet negExsT, SortedSet undExsT, SortedSet posExsF, SortedSet negExsF, SortedSet undExsF) { - SortedSet posExsU = new TreeSet(); - SortedSet negExsU = new TreeSet(); - SortedSet undExsU = new TreeSet(); + SortedSet posExsU = new TreeSet<>(); + SortedSet negExsU = new TreeSet<>(); + SortedSet undExsU = new TreeSet<>(); splitGroup(concept,df, reasoner, posExs,posExsT,posExsF,posExsU); splitGroup(concept,df, reasoner, negExs,negExsT,negExsF,negExsU); diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Triple.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Triple.java index 134075053c..23067597c8 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Triple.java +++ b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Triple.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.decisiontrees.utils; /** diff --git a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Value.java b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Value.java index 846a4b4dd0..0eb6ed2645 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Value.java +++ b/components-core/src/main/java/org/dllearner/algorithms/decisiontrees/utils/Value.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.decisiontrees.utils; public class Value { diff --git a/components-core/src/main/java/org/dllearner/algorithms/el/DisjunctiveHeuristic.java b/components-core/src/main/java/org/dllearner/algorithms/el/DisjunctiveHeuristic.java index 9a39ebfaec..6e68491d78 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/el/DisjunctiveHeuristic.java +++ b/components-core/src/main/java/org/dllearner/algorithms/el/DisjunctiveHeuristic.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,17 +16,18 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.el; +import org.dllearner.core.ComponentAnn; import org.dllearner.core.ComponentInitException; +@ComponentAnn(name = "DisjunctiveHeuristic", shortName = "disjunctive_heuristic", version = 0.1) public class DisjunctiveHeuristic implements ELHeuristic { ELDescriptionTreeComparator edt = new ELDescriptionTreeComparator(); public int compare(SearchTreeNode tree1, SearchTreeNode tree2) { - double diff = tree1.getScore()-tree2.getScore(); + double diff = tree1.getScore().getAccuracy()-tree2.getScore().getAccuracy(); if(diff < 0.00001 && diff > -0.00001) { return edt.compare(tree1.getDescriptionTree(), tree2.getDescriptionTree()); } else if(diff > 0){ diff --git a/components-core/src/main/java/org/dllearner/algorithms/el/ELDescriptionEdge.java b/components-core/src/main/java/org/dllearner/algorithms/el/ELDescriptionEdge.java index 3434f249bc..146941d1a2 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/el/ELDescriptionEdge.java +++ b/components-core/src/main/java/org/dllearner/algorithms/el/ELDescriptionEdge.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.el; import org.semanticweb.owlapi.model.OWLProperty; diff --git a/components-core/src/main/java/org/dllearner/algorithms/el/ELDescriptionEdgeComparator.java b/components-core/src/main/java/org/dllearner/algorithms/el/ELDescriptionEdgeComparator.java index b822a82536..fa269320a0 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/el/ELDescriptionEdgeComparator.java +++ b/components-core/src/main/java/org/dllearner/algorithms/el/ELDescriptionEdgeComparator.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.el; import java.util.Comparator; diff --git a/components-core/src/main/java/org/dllearner/algorithms/el/ELDescriptionNode.java b/components-core/src/main/java/org/dllearner/algorithms/el/ELDescriptionNode.java index c2330e5084..389e4561c8 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/el/ELDescriptionNode.java +++ b/components-core/src/main/java/org/dllearner/algorithms/el/ELDescriptionNode.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.el; import java.util.Arrays; @@ -61,9 +60,9 @@ public class ELDescriptionNode { // the reference tree for storing values, must not be null protected ELDescriptionTree tree; - protected TreeSet label = new TreeSet(); + protected TreeSet label = new TreeSet<>(); - protected List edges = new LinkedList(); + protected List edges = new LinkedList<>(); protected int level; @@ -72,12 +71,12 @@ public class ELDescriptionNode { protected ELDescriptionNode parent = null; // simulation information (list or set?) - protected Set in = new HashSet(); - protected Set inSC1 = new HashSet(); - protected Set inSC2 = new HashSet(); - protected Set out = new HashSet(); - protected Set outSC1 = new HashSet(); - protected Set outSC2 = new HashSet(); + protected Set in = new HashSet<>(); + protected Set inSC1 = new HashSet<>(); + protected Set inSC2 = new HashSet<>(); + protected Set out = new HashSet<>(); + protected Set outSC1 = new HashSet<>(); + protected Set outSC2 = new HashSet<>(); protected boolean isClassNode; protected OWLDataRange dataRange; @@ -100,7 +99,7 @@ public ELDescriptionNode(ELDescriptionTree tree) { // convenience constructor public ELDescriptionNode(ELDescriptionTree tree, OWLClass... label) { - this(tree, new TreeSet(Arrays.asList(label))); + this(tree, new TreeSet<>(Arrays.asList(label))); } /** @@ -109,7 +108,7 @@ public ELDescriptionNode(ELDescriptionTree tree, OWLClass... label) { */ public ELDescriptionNode(ELDescriptionTree tree, TreeSet label) { this.label = label; - this.edges = new LinkedList(); + this.edges = new LinkedList<>(); this.tree = tree; level = 1; parent = null; @@ -122,12 +121,13 @@ public ELDescriptionNode(ELDescriptionTree tree, TreeSet label) { } /** - * Constructs an EL OWLClassExpression tree given its root label. - * @param label Label of the root node. + * Constructs an EL description tree node given a description tree and the data range. + * @param tree the description tree + * @param dataRange the data range */ public ELDescriptionNode(ELDescriptionTree tree, OWLDataRange dataRange) { this.dataRange = dataRange; - this.edges = new LinkedList(); + this.edges = new LinkedList<>(); this.tree = tree; level = 1; parent = null; @@ -141,14 +141,14 @@ public ELDescriptionNode(ELDescriptionTree tree, OWLDataRange dataRange) { // convenience constructor public ELDescriptionNode(ELDescriptionNode parentNode, OWLProperty parentProperty, OWLClass... label) { - this(parentNode, parentProperty, new TreeSet(Arrays.asList(label))); + this(parentNode, parentProperty, new TreeSet<>(Arrays.asList(label))); } public ELDescriptionNode(ELDescriptionNode parentNode, OWLProperty parentProperty, Set label) { // this.label = label; // we first need to add the edge and update the simulation and then add // all classes iteratively to the label (each time updating the simulation again) - this.edges = new LinkedList(); + this.edges = new LinkedList<>(); parent = parentNode; // the reference tree is the same as for the parent tree tree = parentNode.tree; @@ -163,7 +163,7 @@ public ELDescriptionNode(ELDescriptionNode parentNode, OWLProperty parentPropert // simulation update // Monitor mon = MonitorFactory.start("simulation update"); // the nodes, which need to be updated - Set update = new HashSet(); + Set update = new HashSet<>(); // loop over all nodes on the same level, which are not in the in set Set nodes = tree.getNodesOnLevel(level); @@ -230,7 +230,7 @@ public ELDescriptionNode(ELDescriptionNode parentNode, OWLProperty parentPropert // this.label = label; // we first need to add the edge and update the simulation and then add // all classes iteratively to the label (each time updating the simulation again) - this.edges = new LinkedList(); + this.edges = new LinkedList<>(); parent = parentNode; // the reference tree is the same as for the parent tree tree = parentNode.tree; @@ -245,7 +245,7 @@ public ELDescriptionNode(ELDescriptionNode parentNode, OWLProperty parentPropert // simulation update // Monitor mon = MonitorFactory.start("simulation update"); // the nodes, which need to be updated - Set update = new HashSet(); + Set update = new HashSet<>(); // loop over all nodes on the same level, which are not in the in set Set nodes = tree.getNodesOnLevel(level); @@ -373,13 +373,13 @@ public int computeLevel() { /** * This method transform the tree to an EL description. The - * node labels are transformed to an {@link Intersection} - * of {@link NamedClass}. Each edge is transformed to an - * {@link ObjectSomeRestriction}, where the property is the edge - * label and the child OWLClassExpression the subtree the edge points + * node labels are transformed to an {@link org.semanticweb.owlapi.model.OWLObjectIntersectionOf} + * of {@link org.semanticweb.owlapi.model.OWLClass}. Each edge is transformed to an + * {@link org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom}, where the property is the edge + * label and the child description the subtree the edge points * to. Edges are also added to the intersection. If the intersection - * is empty, {@link Thing} is returned. - * @return The OWLClassExpression corresponding to this EL OWLClassExpression tree. + * is empty, {@link OWLDataFactory#getOWLThing()} is returned. + * @return The description corresponding to this EL description tree. */ public OWLClassExpression transformToDescription() { int nrOfElements = label.size() + edges.size(); @@ -404,7 +404,7 @@ public OWLClassExpression transformToDescription() { } // return an intersection of labels and edges } else { - Set operands = new TreeSet(); + Set operands = new TreeSet<>(); for(OWLClass nc : label) { operands.add(nc); } @@ -485,7 +485,7 @@ public void extendLabel(OWLClass newClass) { private void labelSimulationUpdate() { // Monitor mon = MonitorFactory.start("simulation update"); // compute the nodes, which need to be updated - Set update = new HashSet(); + Set update = new HashSet<>(); Set tmp = tree.getNodesOnLevel(level); for(ELDescriptionNode w : tmp) { @@ -552,7 +552,7 @@ public void refineEdge(int edgeNumber, OWLProperty op) { // Monitor mon = MonitorFactory.start("simulation update"); // compute the nodes, which need to be updated - Set update = new HashSet(); + Set update = new HashSet<>(); update.add(this); /* diff --git a/components-core/src/main/java/org/dllearner/algorithms/el/ELDescriptionNodeComparator.java b/components-core/src/main/java/org/dllearner/algorithms/el/ELDescriptionNodeComparator.java index bb978c46fc..8628eb2739 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/el/ELDescriptionNodeComparator.java +++ b/components-core/src/main/java/org/dllearner/algorithms/el/ELDescriptionNodeComparator.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.el; import java.util.Comparator; diff --git a/components-core/src/main/java/org/dllearner/algorithms/el/ELDescriptionTree.java b/components-core/src/main/java/org/dllearner/algorithms/el/ELDescriptionTree.java index c6609947ce..47f92dc6c4 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/el/ELDescriptionTree.java +++ b/components-core/src/main/java/org/dllearner/algorithms/el/ELDescriptionTree.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.el; import java.util.Collection; @@ -71,10 +70,10 @@ public class ELDescriptionTree implements Cloneable { protected ELDescriptionNode rootNode; // the set of all nodes in the tree - private Collection nodes = new LinkedList(); + private Collection nodes = new LinkedList<>(); // nodes on a given level of the tree - private Map> levelNodeMapping = new HashMap>(); + private Map> levelNodeMapping = new HashMap<>(); // the background knowledge (we need to have it explicitly here, // since we store simulation information in the tree and simulation @@ -202,7 +201,7 @@ protected void addNodeToLevel(ELDescriptionNode node, int level) { if (level <= maxLevel) { levelNodeMapping.get(level).add(node); } else if (level == maxLevel + 1) { - Set set = new HashSet(); + Set set = new HashSet<>(); set.add(node); levelNodeMapping.put(level, set); maxLevel++; @@ -238,15 +237,15 @@ public ELDescriptionNode getNode(int[] position) { // logger.trace(Helper.arrayContent(position)); // logger.trace(this); ELDescriptionNode currentNode = rootNode; - for (int i = 0; i < position.length; i++) { - currentNode = currentNode.getEdges().get(position[i]).getNode(); + for (int aPosition : position) { + currentNode = currentNode.getEdges().get(aPosition).getNode(); } return currentNode; } protected void updateSimulation(Set nUpdate) { // create a stack and initialize it with the nodes to be updated - LinkedList list = new LinkedList(); + LinkedList list = new LinkedList<>(); list.addAll(nUpdate); while(list.size() != 0) { @@ -478,7 +477,7 @@ public ELDescriptionTree clone() { // (hash map should be fast here, but one could also // experiment with TreeMap) Map cloneMap = - new HashMap(); + new HashMap<>(); // create a new (empty) node for each node in the tree // (we loop through the level mapping, because it is cheaper @@ -543,7 +542,7 @@ public ELDescriptionTree clone() { treeClone.size = size; // nodes - treeClone.nodes = new LinkedList(); + treeClone.nodes = new LinkedList<>(); for(ELDescriptionNode oldNode : nodes) { treeClone.nodes.add(cloneMap.get(oldNode)); } @@ -551,7 +550,7 @@ public ELDescriptionTree clone() { // level node mapping for(int i=1; i<=maxLevel; i++) { Set oldNodes = levelNodeMapping.get(i); - Set newNodes = new HashSet(); + Set newNodes = new HashSet<>(); for(ELDescriptionNode oldNode : oldNodes) { newNodes.add(cloneMap.get(oldNode)); } @@ -566,7 +565,7 @@ public ELDescriptionTree cloneOld() { // create a new reference tree ELDescriptionTree treeClone = new ELDescriptionTree(rs); // create a root node attached to this reference tree - ELDescriptionNode rootNodeClone = new ELDescriptionNode(treeClone, new TreeSet( + ELDescriptionNode rootNodeClone = new ELDescriptionNode(treeClone, new TreeSet<>( rootNode.getLabel())); cloneRecursively(rootNode, rootNodeClone); return treeClone; @@ -577,7 +576,7 @@ private void cloneRecursively(ELDescriptionNode node, ELDescriptionNode nodeClon // loop through all edges and clone the subtrees for (ELDescriptionEdge edge : node.getEdges()) { ELDescriptionNode tmp = new ELDescriptionNode(nodeClone, edge.getLabel(), - new TreeSet(edge.getNode().getLabel())); + new TreeSet<>(edge.getNode().getLabel())); cloneRecursively(edge.getNode(), tmp); } } diff --git a/components-core/src/main/java/org/dllearner/algorithms/el/ELDescriptionTreeComparator.java b/components-core/src/main/java/org/dllearner/algorithms/el/ELDescriptionTreeComparator.java index 38d6fdb299..a374b42d4b 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/el/ELDescriptionTreeComparator.java +++ b/components-core/src/main/java/org/dllearner/algorithms/el/ELDescriptionTreeComparator.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.el; import java.util.Comparator; diff --git a/components-core/src/main/java/org/dllearner/algorithms/el/ELHeuristic.java b/components-core/src/main/java/org/dllearner/algorithms/el/ELHeuristic.java index 5568b48e27..9fdc4dcc61 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/el/ELHeuristic.java +++ b/components-core/src/main/java/org/dllearner/algorithms/el/ELHeuristic.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.el; import java.util.Comparator; diff --git a/components-core/src/main/java/org/dllearner/algorithms/el/ELLearningAlgorithm.java b/components-core/src/main/java/org/dllearner/algorithms/el/ELLearningAlgorithm.java index 674d929c54..c14341304d 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/el/ELLearningAlgorithm.java +++ b/components-core/src/main/java/org/dllearner/algorithms/el/ELLearningAlgorithm.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,46 +16,31 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.el; -import java.io.File; -import java.util.Collection; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; -import java.util.TreeSet; - +import com.jamonapi.Monitor; +import com.jamonapi.MonitorFactory; import org.apache.log4j.Logger; -import org.dllearner.core.AbstractCELA; -import org.dllearner.core.AbstractClassExpressionLearningProblem; -import org.dllearner.core.AbstractReasonerComponent; -import org.dllearner.core.ComponentAnn; -import org.dllearner.core.ComponentInitException; +import org.dllearner.core.*; import org.dllearner.core.config.ConfigOption; import org.dllearner.core.owl.ClassHierarchy; +import org.dllearner.core.owl.DatatypePropertyHierarchy; +import org.dllearner.core.owl.ObjectPropertyHierarchy; import org.dllearner.learningproblems.ClassLearningProblem; -import org.dllearner.learningproblems.EvaluatedDescriptionPosNeg; -import org.dllearner.learningproblems.PosNegLP; -import org.dllearner.learningproblems.ScorePosNeg; -import org.dllearner.refinementoperators.ELDown3; +import org.dllearner.refinementoperators.ELDown; import org.dllearner.utilities.Files; import org.dllearner.utilities.Helper; import org.dllearner.utilities.OWLAPIUtils; -import org.dllearner.utilities.owl.DLSyntaxObjectRenderer; import org.dllearner.utilities.owl.EvaluatedDescriptionSet; import org.dllearner.utilities.owl.OWLClassExpressionUtils; -import org.semanticweb.owlapi.expression.ParserException; -import org.semanticweb.owlapi.io.OWLObjectRenderer; -import org.semanticweb.owlapi.io.ToStringRenderer; +import org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxParserException; import org.semanticweb.owlapi.model.OWLClass; import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLNamedIndividual; -import org.semanticweb.owlapi.util.DefaultPrefixManager; -import com.jamonapi.Monitor; -import com.jamonapi.MonitorFactory; +import java.io.File; +import java.util.List; +import java.util.Set; +import java.util.TreeSet; /** * A learning algorithm for EL, which is based on an @@ -71,10 +56,6 @@ public class ELLearningAlgorithm extends AbstractCELA { private static Logger logger = Logger.getLogger(ELLearningAlgorithm.class); - @ConfigOption(name="treeSearchTimeSeconds",description="Specifies how long the algorithm should search for a partial solution (a tree).",defaultValue="10.0") - private double treeSearchTimeSeconds = 10.0; - private long treeStartTime; - @ConfigOption(name="instanceBasedDisjoints", required=false, defaultValue="true", description="Specifies whether to use real disjointness checks or instance based ones (no common instances) in the refinement operator.") private boolean instanceBasedDisjoints = true; @@ -114,7 +95,7 @@ public class ELLearningAlgorithm extends AbstractCELA { @ConfigOption(name="heuristic", defaultValue="StableHeuristic", description="The heuristic variable to use for ELTL") private ELHeuristic heuristic; private TreeSet candidates; - private ELDown3 operator; + private ELDown operator; private boolean isEquivalenceProblem = true; private Monitor timeMonitor; @@ -122,8 +103,6 @@ public class ELLearningAlgorithm extends AbstractCELA { double max = -1d; OWLClassExpression maxDescription; - private OWLObjectRenderer renderer; - public ELLearningAlgorithm() {} public ELLearningAlgorithm(AbstractClassExpressionLearningProblem problem, AbstractReasonerComponent reasoner) { @@ -134,12 +113,6 @@ public static String getName() { return "standard EL learning algorithm"; } - public static Collection> supportedLearningProblems() { - Collection> problems = new LinkedList>(); - problems.add(PosNegLP.class); - return problems; - } - @Override public void init() throws ComponentInitException { // currently we use the stable heuristic @@ -147,30 +120,21 @@ public void init() throws ComponentInitException { heuristic = new StableHeuristic(); } - candidates = new TreeSet(heuristic); + candidates = new TreeSet<>(heuristic); - if(ignoredConcepts != null) { - Set usedConcepts = Helper.computeConceptsUsingIgnoreList(reasoner, ignoredConcepts); - // copy class hierarchy and modify it such that each class is only - // reachable via a single path - ClassHierarchy classHierarchy = (ClassHierarchy) reasoner.getClassHierarchy().cloneAndRestrict(new HashSet(usedConcepts)); - classHierarchy.thinOutSubsumptionHierarchy(); - } + ClassHierarchy classHierarchy = initClassHierarchy(); + ObjectPropertyHierarchy obHierarchy = initObjectPropertyHierarchy(); + DatatypePropertyHierarchy dpHierarchy = initDataPropertyHierarchy(); - operator = new ELDown3(reasoner, instanceBasedDisjoints); + operator = new ELDown(reasoner, instanceBasedDisjoints, classHierarchy, obHierarchy, dpHierarchy); operator.setMaxClassExpressionDepth(maxClassExpressionDepth); + operator.init(); noise = noisePercentage/100d; bestEvaluatedDescriptions = new EvaluatedDescriptionSet(maxNrOfResults); timeMonitor = MonitorFactory.getTimeMonitor("eltl-time"); - - renderer = new DLSyntaxObjectRenderer(); - DefaultPrefixManager pm = new DefaultPrefixManager(baseURI); - renderer.setShortFormProvider(pm); - - ToStringRenderer.getInstance().setRenderer(renderer); } @Override @@ -178,7 +142,6 @@ public void start() { stop = false; isRunning = true; reset(); - treeStartTime = System.nanoTime(); nanoStartTime = System.nanoTime(); // create start node @@ -187,11 +150,12 @@ public void start() { } else { try { this.startClass = OWLAPIUtils.classExpressionPropertyExpander(startClass, reasoner, dataFactory); - } catch (ParserException e) { + } catch (ManchesterOWLSyntaxParserException e) { logger.info("Error parsing startClass: " + e.getMessage()); this.startClass = dataFactory.getOWLThing(); } } + logger.info("Start class: " + startClass); ELDescriptionTree top = new ELDescriptionTree(reasoner, startClass); addDescriptionTree(top, null); @@ -214,7 +178,7 @@ public void start() { // logging if(logger.isTraceEnabled()) { - logger.trace("Choosen node " + best); + logger.trace("Chosen node " + best); logger.trace(startNode.getTreeString(renderer)); logger.trace("Loop " + loop + " completed."); } @@ -233,7 +197,7 @@ public void start() { } // print solution(s) - logger.info("solutions[time: " + Helper.prettyPrintNanoSeconds(System.nanoTime()-treeStartTime) + "]\n" + getSolutionString()); + logger.info("solutions[time: " + Helper.prettyPrintNanoSeconds(System.nanoTime()-nanoStartTime) + "]\n" + getSolutionString()); isRunning = false; } @@ -246,12 +210,12 @@ private void addDescriptionTree(ELDescriptionTree descriptionTree, SearchTreeNod // convert tree to standard class expression OWLClassExpression classExpression = descriptionTree.transformToClassExpression(); - if(isDescriptionAllowed(classExpression)){ + if(classExpression.equals(startClass) || isDescriptionAllowed(classExpression)){ // rewrite class expression classExpression = getNiceDescription(classExpression); // compute score - ScorePosNeg score = (ScorePosNeg) learningProblem.computeScore(classExpression, noise); + Score score = learningProblem.computeScore(classExpression, noise); // compute accuracy double accuracy = score.getAccuracy(); @@ -259,12 +223,9 @@ private void addDescriptionTree(ELDescriptionTree descriptionTree, SearchTreeNod if(accuracy == -1) { node.setTooWeak(); } else { - // set covered pos and neg examples - node.setCoveredPositives(score.getCoveredPositives().size()); - node.setCoveredNegatives(score.getCoveredNegatives().size()); + node.setScore(score); } node.setAccuracy(accuracy); - node.setScore(accuracy); // link to parent (unless start node) if(parentNode == null) { @@ -283,13 +244,15 @@ private void addDescriptionTree(ELDescriptionTree descriptionTree, SearchTreeNod // the class expression has a chance to make it in the set if it has // at least as high accuracy - if not we can save the reasoner calls // for fully computing the evaluated description - if(bestEvaluatedDescriptions.size() == 0 || ((EvaluatedDescriptionPosNeg)bestEvaluatedDescriptions.getWorst()).getCoveredNegatives().size() >= node.getCoveredNegatives()) { - EvaluatedDescriptionPosNeg ed = new EvaluatedDescriptionPosNeg(classExpression, score); - bestEvaluatedDescriptions.add(ed); -// System.out.println("Add " + ed); - } else { - EvaluatedDescriptionPosNeg ed = new EvaluatedDescriptionPosNeg(classExpression, score); -// System.out.println("reject " + ed); + if(classToDescribe == null || !classToDescribe.equals(classExpression)) { + if(bestEvaluatedDescriptions.size() == 0 || bestEvaluatedDescriptions.getWorst().getAccuracy() < node.getAccuracy()) { + EvaluatedDescription ed = new EvaluatedDescription<>(classExpression, score); + bestEvaluatedDescriptions.add(ed); +// System.out.println("Add " + ed); + } else { +// EvaluatedDescriptionPosNeg ed = new EvaluatedDescriptionPosNeg(classExpression, score); +// System.out.println("reject " + ed); + } } } } @@ -303,18 +266,15 @@ private boolean stoppingCriteriaSatisfied() { } // stop when max time is reached - long runTime = System.nanoTime() - treeStartTime; - double runTimeSeconds = runTime / (double) 1000000000; - - if(runTimeSeconds >= treeSearchTimeSeconds) { + boolean timeout = isTimeExpired(); + if(timeout) { logger.info("Stopping algorithm: Max. execution time was reached."); return true; } // stop if we have a node covering all positives and none of the negatives SearchTreeNode bestNode = candidates.last(); - boolean perfectDefinitionFound = ((PosNegLP)learningProblem).getPositiveExamples().size() == bestNode.getCoveredPositives() - && (bestNode.getCoveredNegatives() == 0); + boolean perfectDefinitionFound = bestNode.getAccuracy() == 1.0; if(stopOnFirstDefinition && perfectDefinitionFound) { logger.info("Stopping algorithm: Perfect definition found."); return true; @@ -323,9 +283,11 @@ private boolean stoppingCriteriaSatisfied() { return false; } + /* + * set all values back to their default values (used for running + * the algorithm more than once) + */ private void reset() { - // set all values back to their default values (used for running - // the algorithm more than once) candidates.clear(); bestEvaluatedDescriptions.getSet().clear(); } @@ -339,7 +301,7 @@ private boolean isDescriptionAllowed(OWLClassExpression description) { } //non of the equivalent classes must occur on the first level - TreeSet toTest = new TreeSet(); + TreeSet toTest = new TreeSet<>(); if(classToDescribe != null){ toTest.add(classToDescribe); } @@ -353,7 +315,7 @@ private boolean isDescriptionAllowed(OWLClassExpression description) { } else { // none of the superclasses of the class to learn must appear on the // outermost property level - TreeSet toTest = new TreeSet(); + TreeSet toTest = new TreeSet<>(); if(classToDescribe != null){ toTest.add(classToDescribe); } @@ -366,6 +328,11 @@ private boolean isDescriptionAllowed(OWLClassExpression description) { } } return true; + } else { + // the class to learn must not appear on the outermost property level + if(classToDescribe != null && OWLClassExpressionUtils.occursOnFirstLevel(description, classToDescribe)) { + return false; + } } return true; @@ -413,49 +380,49 @@ public boolean isStopOnFirstDefinition() { } /** - * @return the startNode + * @return the start node */ public SearchTreeNode getStartNode() { return startNode; } /** - * @return the noisePercentage + * @return the noise in percentage */ public double getNoisePercentage() { return noisePercentage; } /** - * @param noisePercentage the noisePercentage to set + * @param noisePercentage the noise in percentage to set */ public void setNoisePercentage(double noisePercentage) { this.noisePercentage = noisePercentage; } /** - * @param startClass the startClass to set + * @param startClass the start class to set */ public void setStartClass(OWLClassExpression startClass) { this.startClass = startClass; } /** - * @return the startClass + * @return the start class */ public OWLClassExpression getStartClass() { return startClass; } /** - * @param ignoredConcepts the ignoredConcepts to set + * @param ignoredConcepts the ignored concepts to set */ public void setIgnoredConcepts(Set ignoredConcepts) { this.ignoredConcepts = ignoredConcepts; } /** - * @return the ignoredConcepts + * @return the ignored concepts */ public Set getIgnoredConcepts() { return ignoredConcepts; @@ -468,13 +435,6 @@ public void setClassToDescribe(OWLClass classToDescribe) { this.classToDescribe = classToDescribe; } - /** - * @param treeSearchTimeSeconds the treeSearchTimeSeconds to set - */ - public void setTreeSearchTimeSeconds(double treeSearchTimeSeconds) { - this.treeSearchTimeSeconds = treeSearchTimeSeconds; - } - /** * @param maxNrOfResults the maxNrOfResults to set */ @@ -483,7 +443,7 @@ public void setMaxNrOfResults(int maxNrOfResults) { } /** - * @param maxClassExpressionDepth the maxClassExpressionDepth to set + * @param maxClassExpressionDepth the maximum class expression depth to set */ public void setMaxClassExpressionDepth(int maxClassExpressionDepth) { this.maxClassExpressionDepth = maxClassExpressionDepth; diff --git a/components-core/src/main/java/org/dllearner/algorithms/el/ELLearningAlgorithmDisjunctive.java b/components-core/src/main/java/org/dllearner/algorithms/el/ELLearningAlgorithmDisjunctive.java index ffa48673d6..56887ddb66 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/el/ELLearningAlgorithmDisjunctive.java +++ b/components-core/src/main/java/org/dllearner/algorithms/el/ELLearningAlgorithmDisjunctive.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,35 +16,24 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.el; -import java.text.DecimalFormat; -import java.util.Collection; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; -import java.util.TreeSet; - import org.apache.log4j.Logger; -import org.dllearner.core.AbstractCELA; -import org.dllearner.core.AbstractClassExpressionLearningProblem; -import org.dllearner.core.AbstractReasonerComponent; -import org.dllearner.core.ComponentAnn; -import org.dllearner.core.ComponentInitException; -import org.dllearner.core.EvaluatedDescription; -import org.dllearner.core.Score; +import org.dllearner.core.*; import org.dllearner.core.config.ConfigOption; import org.dllearner.learningproblems.PosNegLP; -import org.dllearner.refinementoperators.ELDown3; +import org.dllearner.learningproblems.ScoreSimple; +import org.dllearner.refinementoperators.ELDown; import org.dllearner.utilities.OWLAPIUtils; import org.dllearner.utilities.owl.OWLAPIRenderers; import org.dllearner.utilities.owl.OWLClassExpressionMinimizer; -import org.semanticweb.owlapi.expression.ParserException; +import org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxParserException; import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLIndividual; +import java.text.DecimalFormat; +import java.util.*; + /** * A learning algorithm for EL, which will based on an * ideal refinement operator. @@ -86,7 +75,7 @@ public class ELLearningAlgorithmDisjunctive extends AbstractCELA { private static Logger logger = Logger.getLogger(ELLearningAlgorithmDisjunctive.class); - private ELDown3 operator; + private ELDown operator; private OWLClassExpressionMinimizer minimizer; private SearchTreeNode startNode; @@ -113,7 +102,7 @@ public class ELLearningAlgorithmDisjunctive extends AbstractCELA { private OWLClassExpression startClass; // private double noise = 0; - private List currentSolution = new LinkedList(); + private List currentSolution = new LinkedList<>(); private EvaluatedDescription bestEvaluatedDescription; // how important not to cover negatives private double posWeight = 1.2; // 2; @@ -122,7 +111,7 @@ public class ELLearningAlgorithmDisjunctive extends AbstractCELA { private Set currentPosExamples; private Set currentNegExamples; private SearchTreeNode bestCurrentNode; - private double bestCurrentScore = 0; + private Score bestCurrentScore = new ScoreSimple(0); private long treeStartTime; // minimum score a tree must have to be part of the solution private double minimumTreeScore = -1; @@ -143,7 +132,7 @@ public static String getName() { } public static Collection> supportedLearningProblems() { - Collection> problems = new LinkedList>(); + Collection> problems = new LinkedList<>(); problems.add(PosNegLP.class); return problems; } @@ -151,20 +140,21 @@ public static Collection @Override public void init() throws ComponentInitException { heuristic = new DisjunctiveHeuristic(); - candidates = new TreeSet(heuristic); - trees = new TreeSet(new ELDescriptionTreeComparator()); + candidates = new TreeSet<>(heuristic); + trees = new TreeSet<>(new ELDescriptionTreeComparator()); if(startClass == null) { startClass = dataFactory.getOWLThing(); } else { try { this.startClass = OWLAPIUtils.classExpressionPropertyExpander(startClass, reasoner, dataFactory); - } catch (ParserException e) { + } catch (ManchesterOWLSyntaxParserException e) { logger.info("Error parsing startClass: " + e.getMessage()); this.startClass = dataFactory.getOWLThing(); } } - operator = new ELDown3(reasoner, instanceBasedDisjoints); + operator = new ELDown(reasoner, instanceBasedDisjoints); + operator.init(); baseURI = reasoner.getBaseURI(); prefixes = reasoner.getPrefixes(); @@ -189,7 +179,7 @@ public void start() { ELDescriptionTree startTree = new ELDescriptionTree(reasoner, startClass); addDescriptionTree(startTree, null); // bestCurrentTree = top; - bestCurrentScore = Double.NEGATIVE_INFINITY; + bestCurrentScore = new ScoreSimple(0);//ScoreSimple.MIN; // main loop int loop = 0; @@ -222,7 +212,7 @@ public void start() { // System.out.println("=="); } - if(bestCurrentScore > minimumTreeScore) { + if(Double.compare(bestCurrentScore.getAccuracy(), minimumTreeScore) > 0) { // we found a tree (partial solution) currentSolution.add(bestCurrentNode.getDescriptionTree()); OWLClassExpression bestDescription = bestCurrentNode.getDescriptionTree().transformToClassExpression(); @@ -232,7 +222,7 @@ public void start() { bestEvaluatedDescription = learningProblem.evaluate(bestDescription); bestEvaluatedDescriptions.add(bestEvaluatedDescription); } else { - if(!bestEvaluatedDescription.equals(dataFactory.getOWLThing())){ + if(!bestEvaluatedDescription.getDescription().equals(dataFactory.getOWLThing())){ bestCombinedDescription = dataFactory.getOWLObjectUnionOf(bestEvaluatedDescription.getDescription(), bestDescription); } bestEvaluatedDescription = learningProblem.evaluate(bestCombinedDescription); @@ -288,7 +278,7 @@ public void start() { isRunning = false; } - // evaluates a OWLClassExpression in tree form + // evaluates a class expression in tree form private void addDescriptionTree(ELDescriptionTree descriptionTree, SearchTreeNode parentNode) { // redundancy check @@ -301,7 +291,7 @@ private void addDescriptionTree(ELDescriptionTree descriptionTree, SearchTreeNod SearchTreeNode node = new SearchTreeNode(descriptionTree); // compute score - double score = getTreeScore(descriptionTree); + Score score = getTreeScore(descriptionTree); node.setScore(score); // link to parent (unless start node) @@ -313,18 +303,18 @@ private void addDescriptionTree(ELDescriptionTree descriptionTree, SearchTreeNod // TODO: define "too weak" as a coverage on negative examples, which is // too high for the tree to be considered - if(score != Double.NEGATIVE_INFINITY) { + if(score.getAccuracy() != Double.NEGATIVE_INFINITY) { candidates.add(node); } // check whether this is the best tree - if(score > bestCurrentScore) { + if(Double.compare(score.getAccuracy(), bestCurrentScore.getAccuracy()) > 0) { bestCurrentNode = node; bestCurrentScore = score; } } - private double getTreeScore(ELDescriptionTree tree) { + private Score getTreeScore(ELDescriptionTree tree) { OWLClassExpression d = tree.transformToClassExpression(); @@ -345,7 +335,7 @@ private double getTreeScore(ELDescriptionTree tree) { // score -= 100; // further refining such a tree will not cover more positives // => reject - return Double.NEGATIVE_INFINITY; + return ScoreSimple.MIN; } // test coverage on current negative examples @@ -369,7 +359,7 @@ private double getTreeScore(ELDescriptionTree tree) { // System.out.println("score: " + score); - return score; + return new ScoreSimple(score); } private boolean treeCriteriaSatisfied() { @@ -398,7 +388,7 @@ private boolean stoppingCriteriaSatisfied() { // we stop when the score of the last tree added is too low // (indicating that the algorithm could not find anything appropriate // in the timeframe set) - if(bestCurrentScore <= minimumTreeScore) { + if(Double.compare(bestCurrentScore.getAccuracy(), minimumTreeScore) <= 0) { return true; } @@ -419,8 +409,8 @@ private void reset() { currentSolution.clear(); bestEvaluatedDescription = learningProblem.evaluate(dataFactory.getOWLThing()); // we need to clone in order not to modify the learning problem - currentPosExamples = new TreeSet(((PosNegLP)getLearningProblem()).getPositiveExamples()); - currentNegExamples = new TreeSet(((PosNegLP)getLearningProblem()).getNegativeExamples()); + currentPosExamples = new TreeSet<>(((PosNegLP) getLearningProblem()).getPositiveExamples()); + currentNegExamples = new TreeSet<>(((PosNegLP) getLearningProblem()).getNegativeExamples()); startPosExamplesSize = currentPosExamples.size(); // startNegExamplesSize = currentNegExamples.size(); } @@ -477,4 +467,12 @@ public double getNoisePercentage() { public void setNoisePercentage(double noisePercentage) { this.noisePercentage = noisePercentage; } + + public boolean isStopOnFirstDefinition() { + return stopOnFirstDefinition; + } + + public void setStopOnFirstDefinition(boolean stopOnFirstDefinition) { + this.stopOnFirstDefinition = stopOnFirstDefinition; + } } diff --git a/components-core/src/main/java/org/dllearner/algorithms/el/SearchTreeNode.java b/components-core/src/main/java/org/dllearner/algorithms/el/SearchTreeNode.java index f21d6f0d55..166339519f 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/el/SearchTreeNode.java +++ b/components-core/src/main/java/org/dllearner/algorithms/el/SearchTreeNode.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,13 +16,12 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.el; import java.util.LinkedList; import java.util.List; -import java.util.Map; +import org.dllearner.core.Score; import org.semanticweb.owlapi.io.OWLObjectRenderer; @@ -36,13 +35,13 @@ public class SearchTreeNode { private ELDescriptionTree descriptionTree; - private List children = new LinkedList(); + private List children = new LinkedList<>(); private int coveredPositives; private int coveredNegatives; private boolean tooWeak = false; - private double score; + private Score score; protected double accuracy; public SearchTreeNode(ELDescriptionTree descriptionTree) { @@ -50,14 +49,14 @@ public SearchTreeNode(ELDescriptionTree descriptionTree) { } /** - * @return the tooWeak + * @return whether the node is too weak */ public boolean isTooWeak() { return tooWeak; } /** - * @param tooWeak the tooWeak to set + * set if the node is too weak */ public void setTooWeak() { tooWeak = true; @@ -143,18 +142,24 @@ private StringBuilder getTreeString(int depth, OWLObjectRenderer renderer) { treeString.append(" "); if(depth!=0) treeString.append("|--> "); - treeString.append(toString(renderer)+"\n"); + treeString.append(toString(renderer)).append("\n"); for(SearchTreeNode child : children) { treeString.append(child.getTreeString(depth+1, renderer)); } return treeString; } - public double getScore() { + /** + * @return the score of the node + */ + public Score getScore() { return score; } - public void setScore(double score) { + /** + * @param score the score of the node to set + */ + public void setScore(Score score) { this.score = score; } diff --git a/components-core/src/main/java/org/dllearner/algorithms/el/Simulation.java b/components-core/src/main/java/org/dllearner/algorithms/el/Simulation.java index ea385a2817..e8cf7ecdd5 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/el/Simulation.java +++ b/components-core/src/main/java/org/dllearner/algorithms/el/Simulation.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.el; import java.util.HashMap; @@ -42,9 +41,9 @@ public class Simulation { private Map> out; public Simulation() { - relation = new LinkedList(); - in = new HashMap>(); - out = new HashMap>(); + relation = new LinkedList<>(); + in = new HashMap<>(); + out = new HashMap<>(); } /** @@ -59,7 +58,7 @@ public void addTuple(TreeTuple tuple) { if(in.containsKey(tuple.getTree2())) { in.get(tuple.getTree2()).add(tuple.getTree1()); } else { - List list = new LinkedList(); + List list = new LinkedList<>(); list.add(tuple.getTree1()); in.put(tuple.getTree2(), list); } @@ -67,7 +66,7 @@ public void addTuple(TreeTuple tuple) { if(out.containsKey(tuple.getTree1())) { out.get(tuple.getTree1()).add(tuple.getTree2()); } else { - List list = new LinkedList(); + List list = new LinkedList<>(); list.add(tuple.getTree2()); out.put(tuple.getTree1(), list); } diff --git a/components-core/src/main/java/org/dllearner/algorithms/el/StableHeuristic.java b/components-core/src/main/java/org/dllearner/algorithms/el/StableHeuristic.java index 7875652f42..c2db32e87b 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/el/StableHeuristic.java +++ b/components-core/src/main/java/org/dllearner/algorithms/el/StableHeuristic.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,9 +16,10 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.el; +import com.google.common.collect.ComparisonChain; +import org.dllearner.core.ComponentAnn; import org.dllearner.core.ComponentInitException; @@ -31,39 +32,23 @@ * @author Jens Lehmann * */ +@ComponentAnn(name = "Stable Heuristic", shortName = "el_heuristic", version = 0.1) public class StableHeuristic implements ELHeuristic { - private ELDescriptionTreeComparator cmp = new ELDescriptionTreeComparator(); + private final ELDescriptionTreeComparator cmp = new ELDescriptionTreeComparator(); @Override public int compare(SearchTreeNode o1, SearchTreeNode o2) { - - int diff = o2.getCoveredNegatives() - o1.getCoveredNegatives(); - diff = Double.compare(o1.getScore(), o2.getScore()); - if(diff>0) { - return 1; - } else if(diff<0) { - return -1; - } else { - - double sizeDiff = o2.getDescriptionTree().size - o1.getDescriptionTree().size; - - if(sizeDiff == 0) { - return cmp.compare(o1.getDescriptionTree(), o2.getDescriptionTree()); - } else if(sizeDiff>0) { - return 1; - } else { - return -1; - } - - } + return ComparisonChain.start() + .compare(o1.getScore().getAccuracy(), o2.getScore().getAccuracy()) + .compare(o2.getDescriptionTree().size, o1.getDescriptionTree().size) + .compare(o1.getDescriptionTree(), o2.getDescriptionTree(), cmp) + .result(); } /* (non-Javadoc) * @see org.dllearner.core.Component#init() */ @Override - public void init() throws ComponentInitException { - } - + public void init() throws ComponentInitException {} } diff --git a/components-core/src/main/java/org/dllearner/algorithms/el/TreeAndRoleSet.java b/components-core/src/main/java/org/dllearner/algorithms/el/TreeAndRoleSet.java index 946a42416e..4f2e92b7bf 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/el/TreeAndRoleSet.java +++ b/components-core/src/main/java/org/dllearner/algorithms/el/TreeAndRoleSet.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.el; import java.util.Set; diff --git a/components-core/src/main/java/org/dllearner/algorithms/el/TreeAndRoleSetComparator.java b/components-core/src/main/java/org/dllearner/algorithms/el/TreeAndRoleSetComparator.java index 55dc141291..c0eb3a3f08 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/el/TreeAndRoleSetComparator.java +++ b/components-core/src/main/java/org/dllearner/algorithms/el/TreeAndRoleSetComparator.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.el; import java.util.Comparator; diff --git a/components-core/src/main/java/org/dllearner/algorithms/el/TreeTuple.java b/components-core/src/main/java/org/dllearner/algorithms/el/TreeTuple.java index ee9361d7f4..4ea85d1480 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/el/TreeTuple.java +++ b/components-core/src/main/java/org/dllearner/algorithms/el/TreeTuple.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.el; /** diff --git a/components-core/src/main/java/org/dllearner/algorithms/gp/ADC.java b/components-core/src/main/java/org/dllearner/algorithms/gp/ADC.java index 1970cf187b..73900b274c 100755 --- a/components-core/src/main/java/org/dllearner/algorithms/gp/ADC.java +++ b/components-core/src/main/java/org/dllearner/algorithms/gp/ADC.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ ///** // * Copyright (C) 2007-2011, Jens Lehmann // * diff --git a/components-core/src/main/java/org/dllearner/algorithms/gp/GP.java b/components-core/src/main/java/org/dllearner/algorithms/gp/GP.java index 1921853376..34b5b6808a 100755 --- a/components-core/src/main/java/org/dllearner/algorithms/gp/GP.java +++ b/components-core/src/main/java/org/dllearner/algorithms/gp/GP.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ ///** // * Copyright (C) 2007-2011, Jens Lehmann // * diff --git a/components-core/src/main/java/org/dllearner/algorithms/gp/GPUtilities.java b/components-core/src/main/java/org/dllearner/algorithms/gp/GPUtilities.java index 09671b5b18..6e0c86f268 100755 --- a/components-core/src/main/java/org/dllearner/algorithms/gp/GPUtilities.java +++ b/components-core/src/main/java/org/dllearner/algorithms/gp/GPUtilities.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ ///** // * Copyright (C) 2007-2011, Jens Lehmann // * diff --git a/components-core/src/main/java/org/dllearner/algorithms/gp/OWLClassExpressionTree.java b/components-core/src/main/java/org/dllearner/algorithms/gp/OWLClassExpressionTree.java index f15f6f7181..3ce78992ff 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/gp/OWLClassExpressionTree.java +++ b/components-core/src/main/java/org/dllearner/algorithms/gp/OWLClassExpressionTree.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.gp; diff --git a/components-core/src/main/java/org/dllearner/algorithms/gp/Program.java b/components-core/src/main/java/org/dllearner/algorithms/gp/Program.java index cc7dbcb977..57b558b4b1 100755 --- a/components-core/src/main/java/org/dllearner/algorithms/gp/Program.java +++ b/components-core/src/main/java/org/dllearner/algorithms/gp/Program.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.gp; import org.dllearner.learningproblems.ScorePosNeg; diff --git a/components-core/src/main/java/org/dllearner/algorithms/hybridgp/GeneticRefinementOperator.java b/components-core/src/main/java/org/dllearner/algorithms/hybridgp/GeneticRefinementOperator.java index 334321895c..2bf4faff25 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/hybridgp/GeneticRefinementOperator.java +++ b/components-core/src/main/java/org/dllearner/algorithms/hybridgp/GeneticRefinementOperator.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,13 +16,12 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.hybridgp; import org.dllearner.algorithms.gp.Program; public interface GeneticRefinementOperator { - public Program applyOperator(Program program); + Program applyOperator(Program program); } diff --git a/components-core/src/main/java/org/dllearner/algorithms/hybridgp/Psi.java b/components-core/src/main/java/org/dllearner/algorithms/hybridgp/Psi.java index 7f84c7b594..73b3bbcf3a 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/hybridgp/Psi.java +++ b/components-core/src/main/java/org/dllearner/algorithms/hybridgp/Psi.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.hybridgp; import java.util.Random; @@ -44,12 +43,12 @@ public class Psi implements GeneticRefinementOperator { Random random; // Cache, damit keine Konzepte doppelt ausgewertet werden - public SortedMap evalCache = new TreeMap(); + public SortedMap evalCache = new TreeMap<>(); // Cache, damit PsiDown bzw. PsiUp nicht mehrfach für gleiches Konzept // aufgerufen werden - public SortedMap> pdCache = new TreeMap>(); - public SortedMap> puCache = new TreeMap>(); + public SortedMap> pdCache = new TreeMap<>(); + public SortedMap> puCache = new TreeMap<>(); // Statistiken int conceptCacheHits = 0; @@ -158,7 +157,7 @@ else if(downwardProbability<0) // ein refinement zufällig auswählen - OWLClassExpression[] array = refinements.toArray(new OWLClassExpression[0]); + OWLClassExpression[] array = refinements.toArray(new OWLClassExpression[refinements.size()]); // kein refinement gefunden if(array.length==0) { if(debug) { @@ -200,12 +199,11 @@ public Program applyOperator(Program program) { // sich lohnt Operatoren zu definieren, die keine Negationsnormalform // erfordern) - OWLClassExpression conceptMod = ConceptTransformation.transformToNegationNormalForm(concept); + OWLClassExpression conceptMod = concept.getNNF(); // um mehr Cache Hits zu bekommen, wird noch vereinfach und geordnet OWLClassExpression conceptModForCache = ConceptTransformation.applyEquivalenceRules(conceptMod); - ConceptTransformation.transformToOrderedForm(conceptModForCache); ScorePosNeg score = program.getScore(); // Eval-Cache füllen @@ -224,7 +222,6 @@ public Program applyOperator(Program program) { /////////// TESTCODE: umwandeln des erhaltenen Konzepts // someTimeStart = System.nanoTime(); OWLClassExpression newConceptMod = ConceptTransformation.applyEquivalenceRules(newConcept); - ConceptTransformation.transformToOrderedForm(newConceptMod); // someTime += System.nanoTime() - someTimeStart; /////////// diff --git a/components-core/src/main/java/org/dllearner/algorithms/ocel/ExampleBasedHeuristic.java b/components-core/src/main/java/org/dllearner/algorithms/ocel/ExampleBasedHeuristic.java index b6bc9783ee..cc0fc501c3 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/ocel/ExampleBasedHeuristic.java +++ b/components-core/src/main/java/org/dllearner/algorithms/ocel/ExampleBasedHeuristic.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.ocel; import java.util.Comparator; diff --git a/components-core/src/main/java/org/dllearner/algorithms/ocel/ExampleBasedNode.java b/components-core/src/main/java/org/dllearner/algorithms/ocel/ExampleBasedNode.java index 2e00f48636..6bb5fa8e8d 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/ocel/ExampleBasedNode.java +++ b/components-core/src/main/java/org/dllearner/algorithms/ocel/ExampleBasedNode.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,31 +16,33 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.ocel; +import org.dllearner.core.AbstractSearchTreeNode; +import org.dllearner.learningproblems.AccMethodTwoValued; +import org.dllearner.utilities.datastructures.SearchTreeNode; +import org.dllearner.utilities.datastructures.WeakSearchTreeNode; +import org.dllearner.utilities.owl.OWLAPIRenderers; +import org.semanticweb.owlapi.model.OWLClassExpression; +import org.semanticweb.owlapi.model.OWLIndividual; + import java.text.DecimalFormat; import java.util.Map; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; -import org.dllearner.algorithms.SearchTreeNode; -import org.dllearner.utilities.owl.OWLAPIRenderers; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLIndividual; - /** * * Represents a node in the search tree. A node consists of * the following parts: * - * ... (see paper) ... + * ... (see paper) ... * * @author Jens Lehmann * */ -public class ExampleBasedNode implements SearchTreeNode { +public class ExampleBasedNode extends AbstractSearchTreeNode implements SearchTreeNode, WeakSearchTreeNode { // public static long exampleMemoryCounter = 0; @@ -51,11 +53,13 @@ public class ExampleBasedNode implements SearchTreeNode { // example based variables private Set coveredPositives; private Set coveredNegatives; + // private int coveredPositiveSize; // private int coveredNegativeSize; // the method by which quality was evaluated in this node - public enum QualityEvaluationMethod { START, REASONER, TOO_WEAK_LIST, OVERLY_GENERAL_LIST }; + public enum QualityEvaluationMethod { START, REASONER, TOO_WEAK_LIST, OVERLY_GENERAL_LIST } + private QualityEvaluationMethod qualityEvaluationMethod = QualityEvaluationMethod.START; // all properties of a node in the search tree @@ -71,17 +75,15 @@ public enum QualityEvaluationMethod { START, REASONER, TOO_WEAK_LIST, OVERLY_GEN private double startNodeBonus; private double expansionPenaltyFactor; private int negationPenalty; - private static NodeComparatorStable nodeComparator = new NodeComparatorStable(); - // link to parent in search tree - private ExampleBasedNode parent = null; - private SortedSet children = new TreeSet(nodeComparator); // apart from the child nodes, we also keep child concepts - private SortedSet childConcepts = new TreeSet(); + private SortedSet childConcepts = new TreeSet<>(); // a flag whether this could be a solution for a posonly learning problem private boolean isPosOnlyCandidate = true; + + private AccMethodTwoValued accuracyMethod; public ExampleBasedNode(OWLClassExpression concept, double negativeWeight, double startNodeBonus, double expansionPenaltyFactor, int negationPenalty) { // this.configurator = configurator; @@ -109,11 +111,10 @@ public void setTooWeak(boolean isTooWeak) { isQualityEvaluated = true; } - public boolean addChild(ExampleBasedNode child) { - // child.setParent(this); - child.parent = this; + @Override + public void addChild(ExampleBasedNode child) { + super.addChild(child); childConcepts.add(child.concept); - return children.add(child); } public void setQualityEvaluationMethod(QualityEvaluationMethod qualityEvaluationMethod) { @@ -128,7 +129,11 @@ public void setCoveredExamples(Set coveredPositives, Set prefixes) { return getTreeString(nrOfPositiveExamples, nrOfNegativeExamples, 0,baseURI, prefixes).toString(); - } + } private StringBuilder getTreeString(int nrOfPositiveExamples, int nrOfNegativeExamples, int depth, String baseURI, Map prefixes) { StringBuilder treeString = new StringBuilder(); @@ -170,7 +175,7 @@ private StringBuilder getTreeString(int nrOfPositiveExamples, int nrOfNegativeEx if(depth!=0) // treeString.append("|-→ "); treeString.append("|--> "); - treeString.append(getShortDescription(nrOfPositiveExamples, nrOfNegativeExamples, baseURI, prefixes)+"\n"); + treeString.append(getShortDescription(nrOfPositiveExamples, nrOfNegativeExamples, baseURI, prefixes)).append("\n"); for(ExampleBasedNode child : children) { treeString.append(child.getTreeString(nrOfPositiveExamples, nrOfNegativeExamples, depth+1,baseURI, prefixes)); } @@ -183,8 +188,8 @@ public String getShortDescription(int nrOfPositiveExamples, int nrOfNegativeExam if(isTooWeak) ret += "q:tw"; else { - double accuracy = 100 * (coveredPositives.size() + nrOfNegativeExamples - coveredNegatives.size())/(double)(nrOfPositiveExamples+nrOfNegativeExamples); - ret += "acc:" + df.format(accuracy) + "% "; + double accuracy = 100 * this.getAccuracy(nrOfPositiveExamples, nrOfNegativeExamples); + ret += "acc:" + df.format(accuracy) + "% "; // comment this out to display the heuristic score with default parameters double heuristicScore = MultiHeuristic.getNodeScore(this, nrOfPositiveExamples, nrOfNegativeExamples, negativeWeight, startNodeBonus, expansionPenaltyFactor, negationPenalty); @@ -206,8 +211,8 @@ public String getShortDescriptionHTML(int nrOfPositiveExamples, int nrOfNegative if(isTooWeak) ret += "q:tw"; else { - double accuracy = 100 * (coveredPositives.size() + nrOfNegativeExamples - coveredNegatives.size())/(double)(nrOfPositiveExamples+nrOfNegativeExamples); - ret += "acc: " + df.format(accuracy) + "% "; + double accuracy = 100 * this.getAccuracy(nrOfPositiveExamples, nrOfNegativeExamples); + ret += "acc: " + df.format(accuracy) + "% "; // comment this out to display the heuristic score with default parameters double heuristicScore = MultiHeuristic.getNodeScore(this, nrOfPositiveExamples, nrOfNegativeExamples, negativeWeight, startNodeBonus, expansionPenaltyFactor, negationPenalty); @@ -221,7 +226,7 @@ public String getShortDescriptionHTML(int nrOfPositiveExamples, int nrOfNegative ret += " c:" + children.size() + "]"; return ret + ""; - } + } //TODO integrate this method with the one above public String getStats(int nrOfPositiveExamples, int nrOfNegativeExamples) { @@ -230,8 +235,8 @@ public String getStats(int nrOfPositiveExamples, int nrOfNegativeExamples) { if(isTooWeak) ret += "q:tw"; else { - double accuracy = 100 * (coveredPositives.size() + nrOfNegativeExamples - coveredNegatives.size())/(double)(nrOfPositiveExamples+nrOfNegativeExamples); - ret += "acc:" + df.format(accuracy) + "% "; + double accuracy = 100 * this.getAccuracy(nrOfPositiveExamples, nrOfNegativeExamples); + ret += "acc:" + df.format(accuracy) + "% "; // comment this out to display the heuristic score with default parameters double heuristicScore = MultiHeuristic.getNodeScore(this, nrOfPositiveExamples, nrOfNegativeExamples, negativeWeight, startNodeBonus, expansionPenaltyFactor, negationPenalty); @@ -248,7 +253,15 @@ public String getStats(int nrOfPositiveExamples, int nrOfNegativeExamples) { } public double getAccuracy(int nrOfPositiveExamples, int nrOfNegativeExamples) { - return (coveredPositives.size() + nrOfNegativeExamples - coveredNegatives.size())/(double)(nrOfPositiveExamples+nrOfNegativeExamples); + int tp = coveredPositives.size(); + int fp = coveredNegatives.size(); + int tn = nrOfNegativeExamples - fp; + int fn = nrOfPositiveExamples - tp; + + double accuracy = this.accuracyMethod.getAccOrTooWeak2(tp, fn, fp, tn, 1); + if (accuracy == -1 && !isTooWeak) + throw new RuntimeException("Accuracy says weak but node is not marked as such."); + return accuracy; } /** @@ -262,15 +275,11 @@ public int getCovPosMinusCovNeg() { public Set getCoveredPositives() { return coveredPositives; - } + } public Set getCoveredNegatives() { return coveredNegatives; } - - public SortedSet getChildren() { - return children; - } public SortedSet getChildConcepts() { return childConcepts; @@ -278,15 +287,16 @@ public SortedSet getChildConcepts() { public OWLClassExpression getConcept() { return concept; - } + } + @Override public OWLClassExpression getExpression() { return getConcept(); - } + } public QualityEvaluationMethod getQualityEvaluationMethod() { return qualityEvaluationMethod; - } + } public int getHorizontalExpansion() { return horizontalExpansion; @@ -301,13 +311,6 @@ public boolean isTooWeak() { return isTooWeak; } - /** - * @return the parent - */ - public ExampleBasedNode getParent() { - return parent; - } - public boolean isPosOnlyCandidate() { return isPosOnlyCandidate; } @@ -316,4 +319,8 @@ public void setPosOnlyCandidate(boolean isPosOnlyCandidate) { this.isPosOnlyCandidate = isPosOnlyCandidate; } + public void setAccuracyMethod(AccMethodTwoValued accuracyMethod) { + this.accuracyMethod = accuracyMethod; + } + } \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/algorithms/ocel/FlexibleHeuristic.java b/components-core/src/main/java/org/dllearner/algorithms/ocel/FlexibleHeuristic.java index 196d7ff62a..9ba4a7d7cc 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/ocel/FlexibleHeuristic.java +++ b/components-core/src/main/java/org/dllearner/algorithms/ocel/FlexibleHeuristic.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,10 +16,11 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.ocel; +import org.dllearner.core.ComponentAnn; import org.dllearner.core.ComponentInitException; +import org.dllearner.core.config.ConfigOption; import org.dllearner.utilities.owl.OWLClassExpressionUtils; /** @@ -44,18 +45,40 @@ * @author Jens Lehmann * */ +@ComponentAnn(name = "Flexible Heuristic", shortName = "flexheuristic", version = 0.1) public class FlexibleHeuristic implements ExampleBasedHeuristic { + @ConfigOption(description = "the number of negative examples") private int nrOfNegativeExamples; + @ConfigOption(description = "score percent to deduct per expression length", required = true) private double percentPerLengthUnit; - // 5% sind eine Verlängerung um 1 wert // double percentPerLengthUnit = 0.05; + + public int getNrOfNegativeExamples() { + return nrOfNegativeExamples; + } + + public void setNrOfNegativeExamples(int nrOfNegativeExamples) { + this.nrOfNegativeExamples = nrOfNegativeExamples; + } + + public double getPercentPerLengthUnit() { + return percentPerLengthUnit; + } + + public void setPercentPerLengthUnit(double percentPerLengthUnit) { + this.percentPerLengthUnit = percentPerLengthUnit; + } + public FlexibleHeuristic(int nrOfNegativeExamples, double percentPerLengthUnit) { this.nrOfNegativeExamples = nrOfNegativeExamples; this.percentPerLengthUnit = percentPerLengthUnit; } + + public FlexibleHeuristic() { + } // implementiert einfach die Definition in der Diplomarbeit public int compare(ExampleBasedNode n1, ExampleBasedNode n2) { diff --git a/components-core/src/main/java/org/dllearner/algorithms/ocel/LexicographicHeuristic.java b/components-core/src/main/java/org/dllearner/algorithms/ocel/LexicographicHeuristic.java index ab6b177866..c04e30b226 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/ocel/LexicographicHeuristic.java +++ b/components-core/src/main/java/org/dllearner/algorithms/ocel/LexicographicHeuristic.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,12 +16,12 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.ocel; +import org.dllearner.core.ComponentAnn; import org.dllearner.core.ComponentInitException; - +@ComponentAnn(name = "Lexicograpgic Heuristic", shortName = "lexheuristic", version = 0.1) public class LexicographicHeuristic implements ExampleBasedHeuristic { // implementiert einfach die Definition in der Diplomarbeit diff --git a/components-core/src/main/java/org/dllearner/algorithms/ocel/MultiHeuristic.java b/components-core/src/main/java/org/dllearner/algorithms/ocel/MultiHeuristic.java index 342024749b..e026cb43a0 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/ocel/MultiHeuristic.java +++ b/components-core/src/main/java/org/dllearner/algorithms/ocel/MultiHeuristic.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,11 +16,8 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.ocel; -import java.util.Set; - import org.dllearner.core.Component; import org.dllearner.core.ComponentAnn; import org.dllearner.core.ComponentInitException; @@ -29,6 +26,8 @@ import org.semanticweb.owlapi.model.OWLDataSomeValuesFrom; import org.semanticweb.owlapi.model.OWLObjectComplementOf; +import java.util.Set; + /** * This heuristic combines the following criteria to assign a * double score value to a node: diff --git a/components-core/src/main/java/org/dllearner/algorithms/ocel/NodeComparatorStable.java b/components-core/src/main/java/org/dllearner/algorithms/ocel/NodeComparatorStable.java index 0bde1dafd4..7a8547fe08 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/ocel/NodeComparatorStable.java +++ b/components-core/src/main/java/org/dllearner/algorithms/ocel/NodeComparatorStable.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.ocel; import java.util.Comparator; diff --git a/components-core/src/main/java/org/dllearner/algorithms/ocel/OCEL.java b/components-core/src/main/java/org/dllearner/algorithms/ocel/OCEL.java index a78c5002f2..23fa7e94aa 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/ocel/OCEL.java +++ b/components-core/src/main/java/org/dllearner/algorithms/ocel/OCEL.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,50 +16,34 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.ocel; -import java.io.File; -import java.util.Collection; -import java.util.LinkedList; -import java.util.List; -import java.util.TreeSet; - import org.apache.log4j.Level; import org.apache.log4j.Logger; -import org.dllearner.core.AbstractCELA; -import org.dllearner.core.AbstractClassExpressionLearningProblem; -import org.dllearner.core.AbstractReasonerComponent; -import org.dllearner.core.ComponentAnn; -import org.dllearner.core.ComponentInitException; -import org.dllearner.core.options.BooleanConfigOption; +import org.dllearner.core.*; +import org.dllearner.core.config.ConfigOption; import org.dllearner.core.options.CommonConfigOptions; -import org.dllearner.core.options.ConfigOption; -import org.dllearner.core.options.DoubleConfigOption; -import org.dllearner.core.options.IntegerConfigOption; -import org.dllearner.core.options.StringConfigOption; import org.dllearner.core.owl.ClassHierarchy; import org.dllearner.core.owl.DatatypePropertyHierarchy; import org.dllearner.core.owl.ObjectPropertyHierarchy; -import org.dllearner.learningproblems.EvaluatedDescriptionPosNeg; -import org.dllearner.learningproblems.PosNegLP; -import org.dllearner.learningproblems.PosNegLPStandard; -import org.dllearner.learningproblems.PosOnlyLP; -import org.dllearner.learningproblems.ScorePosNeg; +import org.dllearner.learningproblems.*; import org.dllearner.reasoning.ReasonerType; -import org.dllearner.refinementoperators.CustomHierarchyRefinementOperator; -import org.dllearner.refinementoperators.CustomStartRefinementOperator; -import org.dllearner.refinementoperators.LengthLimitedRefinementOperator; -import org.dllearner.refinementoperators.ReasoningBasedRefinementOperator; -import org.dllearner.refinementoperators.RhoDRDown; +import org.dllearner.refinementoperators.*; import org.dllearner.utilities.Files; +import org.dllearner.utilities.owl.OWLClassExpressionLengthMetric; import org.semanticweb.owlapi.model.OWLClass; import org.semanticweb.owlapi.model.OWLClassExpression; import org.springframework.beans.factory.annotation.Autowired; +import java.io.File; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; +import java.util.TreeSet; + /** * The DL-Learner learning algorithm component for the example - * based refinement operator approach. It handles all + * based refinement operator approach. It handles all * configuration options, creates the corresponding objects and * passes them to the actual refinement operator, heuristic, and * learning algorithm implementations. @@ -93,10 +77,10 @@ public class OCEL extends AbstractCELA { // actual algorithm private ROLearner2 algorithm; private static Logger logger = Logger.getLogger(OCEL.class); - private String logLevel = CommonConfigOptions.logLevelDefault; + private String logLevel = CommonConfigOptions.logLevelDefault; // dependencies - private LengthLimitedRefinementOperator operator; + private LengthLimitedRefinementOperator operator; private ExampleBasedHeuristic heuristic; // configuration options @@ -107,7 +91,7 @@ public class OCEL extends AbstractCELA { // private String heuristicStr = "multi"; // private boolean applyAllFilter = true; -// private boolean applyExistsFilter = true; +// private boolean applyExistsFilter = true; private boolean useTooWeakList = true; private boolean useOverlyGeneralList = true; private boolean useShortConceptConstruction = true; @@ -146,6 +130,9 @@ public class OCEL extends AbstractCELA { // Variablen zur Einstellung der Protokollierung // boolean quiet = false; boolean showBenchmarkInformation = false; + + @ConfigOption(description = "adjust the weights of class expression length in refinement", defaultValue = "OCEL default metric") + private OWLClassExpressionLengthMetric lengthMetric; // boolean createTreeString = false; // String searchTree = new String(); // private int cardinalityLimit = 5; @@ -154,82 +141,28 @@ public class OCEL extends AbstractCELA { // Konfiguration des Algorithmus // Faktor für horizontale Erweiterung (notwendig für completeness) - // double horizontalExpansionFactor = 0.6; + // double horizontalExpansionFactor = 0.6; - public OCEL(){ - - } + public OCEL(){} + // soll später einen Operator und eine Heuristik entgegennehmen // public ROLearner(LearningProblem learningProblem, LearningProblem learningProblem2) { public OCEL(PosNegLP learningProblem, AbstractReasonerComponent reasoningService) { super(learningProblem, reasoningService); -// this.configurator = new OCELConfigurator(this); } public OCEL(PosOnlyLP learningProblem, AbstractReasonerComponent reasoningService) { super(learningProblem, reasoningService); -// this.configurator = new OCELConfigurator(this); } public static Collection> supportedLearningProblems() { - Collection> problems = new LinkedList>(); + Collection> problems = new LinkedList<>(); problems.add(PosNegLP.class); problems.add(PosOnlyLP.class); return problems; } - public static Collection> createConfigOptions() { - Collection> options = new LinkedList>(); - options.add(new BooleanConfigOption("writeSearchTree", "specifies whether to write a search tree", false)); - options.add(new StringConfigOption("searchTreeFile","file to use for the search tree", defaultSearchTreeFile)); - options.add(new BooleanConfigOption("replaceSearchTree","specifies whether to replace the search tree in the log file after each run or append the new search tree", false)); - StringConfigOption heuristicOption = new StringConfigOption("heuristic", "specifiy the heuristic to use", "lexicographic"); - heuristicOption.setAllowedValues(new String[] {"lexicographic", "flexible"}); - options.add(heuristicOption); - options.add(new BooleanConfigOption("applyAllFilter", "usage of equivalence ALL R.C AND ALL R.D = ALL R.(C AND D)", true)); - options.add(new BooleanConfigOption("applyExistsFilter", "usage of equivalence EXISTS R.C OR EXISTS R.D = EXISTS R.(C OR D)", true)); - options.add(new BooleanConfigOption("useTooWeakList", "try to filter out too weak concepts without sending them to the reasoner", true)); - options.add(new BooleanConfigOption("useOverlyGeneralList", "try to find overly general concept without sending them to the reasoner", true)); - options.add(new BooleanConfigOption("useShortConceptConstruction", "shorten concept to see whether they already exist", true)); - DoubleConfigOption horizExp = new DoubleConfigOption("horizontalExpansionFactor", "horizontal expansion factor (see publication for description)", 0.6); - horizExp.setLowerLimit(0.0); - horizExp.setUpperLimit(1.0); - options.add(horizExp); - options.add(new BooleanConfigOption("improveSubsumptionHierarchy", "simplify subsumption hierarchy to reduce search space (see publication for description)", true)); - options.add(CommonConfigOptions.allowedConcepts()); - options.add(CommonConfigOptions.ignoredConcepts()); - // allowed/ignored roles are an unstable/untested feature - options.add(CommonConfigOptions.allowedRoles()); - options.add(CommonConfigOptions.ignoredRoles()); - options.add(CommonConfigOptions.useAllConstructor()); - options.add(CommonConfigOptions.useExistsConstructor()); - options.add(CommonConfigOptions.useHasValueConstructor()); - options.add(CommonConfigOptions.useDataHasValueConstructor()); - options.add(CommonConfigOptions.valueFreqencyThreshold()); - options.add(CommonConfigOptions.useCardinalityRestrictions()); - options.add(CommonConfigOptions.cardinalityLimit()); - options.add(CommonConfigOptions.useNegation()); - options.add(CommonConfigOptions.useBooleanDatatypes()); - options.add(CommonConfigOptions.useStringDatatypes()); - options.add(CommonConfigOptions.maxExecutionTimeInSeconds()); - options.add(CommonConfigOptions.minExecutionTimeInSeconds()); - options.add(CommonConfigOptions.guaranteeXgoodDescriptions()); - options.add(CommonConfigOptions.maxClassDescriptionTests()); - options.add(CommonConfigOptions.getLogLevel()); - options.add(new BooleanConfigOption("usePropernessChecks", "specifies whether to check for equivalence (i.e. discard equivalent refinements)",usePropernessChecksDefault)); - options.add(CommonConfigOptions.getNoisePercentage()); - options.add(CommonConfigOptions.getTerminateOnNoiseReached()); - options.add(new StringConfigOption("startClass", "the named class which should be used to start the algorithm (GUI: needs a widget for selecting a class)")); - options.add(new BooleanConfigOption("forceRefinementLengthIncrease", "specifies whether nodes should be expanded until only longer refinements are reached")); - options.add(new DoubleConfigOption("negativeWeight", "Used to penalise errors on negative examples different from those of positive examples (lower = less importance for negatives).",1.0)); - options.add(new DoubleConfigOption("startNodeBonus", "You can use this to give a heuristic bonus on the start node (= initially broader exploration of search space).",0.0)); - options.add(new IntegerConfigOption("negationPenalty", "Penalty on negations (TODO: better explanation).", 0)); - options.add(CommonConfigOptions.getExpansionPenaltyFactor(0.02)); - options.add(CommonConfigOptions.getInstanceBasedDisjoints()); - return options; - } - /* (non-Javadoc) * @see org.dllearner.core.Component#init() */ @@ -259,9 +192,9 @@ public void init() throws ComponentInitException { // heuristic = new MultiHeuristic(((PosOnlyLP) getLearningProblem()).getPositiveExamples().size(),0, negativeWeight, startNodeBonus, expansionPenaltyFactor, negationPenalty); } else { heuristic = new MultiHeuristic(((PosNegLP) getLearningProblem()).getPositiveExamples().size(),((PosNegLP) getLearningProblem()).getNegativeExamples().size(), negativeWeight, startNodeBonus, expansionPenaltyFactor, negationPenalty); - } + } - // OLD CODE below: in the new framework we assume that the + // OLD CODE below: in the new framework we assume that the // heuristic is always injected as object (not as string) // if(heuristicStr == "lexicographic") // heuristic = new LexicographicHeuristic(); @@ -289,21 +222,21 @@ public void init() throws ComponentInitException { int nrNegEx = ((PosNegLP) getLearningProblem()).getNegativeExamples().size(); if(mh.getNrOfExamples() == 0) { mh.setNrOfExamples(nrPosEx + nrNegEx); - } + } if(mh.getNrOfNegativeExamples() == 0) { mh.setNrOfNegativeExamples(nrNegEx); - } + } + } else if (heuristic instanceof FlexibleHeuristic) { + FlexibleHeuristic h2 = (FlexibleHeuristic) heuristic; + if(h2.getNrOfNegativeExamples() == 0) { + h2.setNrOfNegativeExamples(((PosNegLP) getLearningProblem()).getNegativeExamples().size()); + } } + } // warn the user if he/she sets any non-standard heuristic, because it will just be ignored if(learningProblem instanceof PosNegLPStandard) { - if(((PosNegLPStandard)learningProblem).isUseApproximations()) { - System.err.println("You actived approximations for the considered learning problem, but OCEL does not support it. Option will be ignored. (Recommendation: Use CELOE instead.)"); - } - if(!((PosNegLPStandard)learningProblem).getAccuracyMethod().equals("predacc")) { - System.err.println("You have chosen a non-standard (predictive accuracy) heuristic in your learning problem, but OCEL does not support it. Option will be ignored. (Recommendation: Use CELOE instead.)"); - } } // compute used concepts/roles from allowed/ignored @@ -347,7 +280,14 @@ public void init() throws ComponentInitException { ((CustomHierarchyRefinementOperator)operator).setObjectPropertyHierarchy(objectPropertyHierarchy); ((CustomHierarchyRefinementOperator)operator).setDataPropertyHierarchy(datatypePropertyHierarchy); } - + + if (lengthMetric == null) { + lengthMetric = OWLClassExpressionLengthMetric.getOCELMetric(); + } + if(operator instanceof LengthLimitedRefinementOperator) { + ((LengthLimitedRefinementOperator)operator).setLengthMetric(lengthMetric); + } + // create an algorithm object and pass all configuration // options to it algorithm = new ROLearner2( @@ -359,7 +299,7 @@ public void init() throws ComponentInitException { startClass, // usedConcepts, // usedRoles, - noisePercentage/(double)100, + noisePercentage/100, writeSearchTree, replaceSearchTree, searchTreeFile, @@ -374,10 +314,12 @@ public void init() throws ComponentInitException { maxClassDescriptionTests, forceRefinementLengthIncrease, terminateOnNoiseReached, - negativeWeight, - startNodeBonus, - expansionPenaltyFactor, - negationPenalty + negativeWeight, + startNodeBonus, + expansionPenaltyFactor, + negationPenalty, + lengthMetric, + renderer ); // note: used concepts and roles do not need to be passed // as argument, because it is sufficient to prepare the @@ -411,7 +353,7 @@ public OWLClassExpression getCurrentlyBestDescription() { @Override public synchronized List getCurrentlyBestDescriptions() { return algorithm.getCurrentlyBestDescriptions(); - } + } @Override public EvaluatedDescriptionPosNeg getCurrentlyBestEvaluatedDescription() { @@ -551,10 +493,12 @@ public void setForceRefinementLengthIncrease(boolean forceRefinementLengthIncrea this.forceRefinementLengthIncrease = forceRefinementLengthIncrease; } + @Override public int getMaxExecutionTimeInSeconds() { return maxExecutionTimeInSeconds; } + @Override public void setMaxExecutionTimeInSeconds(int maxExecutionTimeInSeconds) { this.maxExecutionTimeInSeconds = maxExecutionTimeInSeconds; } @@ -639,6 +583,18 @@ public void setTerminateOnNoiseReached(boolean terminateOnNoiseReached) { this.terminateOnNoiseReached = terminateOnNoiseReached; } + public OWLClassExpressionLengthMetric getLengthMetric() { + return lengthMetric; + } + + @Autowired(required = false) + public void setLengthMetric(OWLClassExpressionLengthMetric lengthMetric) { + this.lengthMetric = lengthMetric; + if (operator != null && operator instanceof LengthLimitedRefinementOperator) { + ((LengthLimitedRefinementOperator)operator).setLengthMetric(lengthMetric); + } + } + @Autowired(required=false) public void setHeuristic(ExampleBasedHeuristic heuristic) { this.heuristic = heuristic; diff --git a/components-core/src/main/java/org/dllearner/algorithms/ocel/ROLearner2.java b/components-core/src/main/java/org/dllearner/algorithms/ocel/ROLearner2.java index 7a51d99118..f2014f7cd0 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/ocel/ROLearner2.java +++ b/components-core/src/main/java/org/dllearner/algorithms/ocel/ROLearner2.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,25 +16,14 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.ocel; -import java.io.File; -import java.text.DecimalFormat; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.NavigableSet; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; -import java.util.concurrent.ConcurrentSkipListSet; - +import com.google.common.collect.Sets; +import com.jamonapi.Monitor; import org.apache.log4j.Logger; import org.dllearner.core.AbstractClassExpressionLearningProblem; import org.dllearner.core.AbstractReasonerComponent; +import org.dllearner.learningproblems.AccMethodNoWeakness; import org.dllearner.learningproblems.EvaluatedDescriptionPosNeg; import org.dllearner.learningproblems.PosNegLP; import org.dllearner.learningproblems.ScorePosNeg; @@ -43,19 +32,16 @@ import org.dllearner.utilities.Files; import org.dllearner.utilities.Helper; import org.dllearner.utilities.JamonMonitorLogger; -import org.dllearner.utilities.owl.ConceptTransformation; -import org.dllearner.utilities.owl.EvaluatedDescriptionPosNegComparator; -import org.dllearner.utilities.owl.OWLAPIRenderers; -import org.dllearner.utilities.owl.OWLClassExpressionUtils; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLObjectIntersectionOf; -import org.semanticweb.owlapi.model.OWLObjectUnionOf; - +import org.dllearner.utilities.datastructures.SearchTreeNonWeak; +import org.dllearner.utilities.datastructures.SearchTreeNonWeakPartialSet; +import org.dllearner.utilities.owl.*; +import org.semanticweb.owlapi.io.OWLObjectRenderer; +import org.semanticweb.owlapi.model.*; import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; -import com.jamonapi.Monitor; +import java.io.File; +import java.text.DecimalFormat; +import java.util.*; /** * Implements the 2nd version of the refinement operator based learning approach. @@ -66,7 +52,7 @@ public class ROLearner2 { private static Logger logger = Logger.getLogger(ROLearner2.class); -// private OCELConfigurator configurator; + private final OWLObjectRenderer renderer; // basic setup: learning problem and reasoning service private AbstractReasonerComponent rs; @@ -118,8 +104,8 @@ public class ROLearner2 { // if this variable is set to true, then the refinement operator // is applied until all concept of equal length have been found // e.g. TOP -> A1 -> A2 -> A3 is found in one loop; the disadvantage - // are potentially more method calls, but the advantage is that - // the algorithm is better in locating relevant concept in the + // are potentially more method calls, but the advantage is that + // the algorithm is better in locating relevant concept in the // subsumption hierarchy (otherwise, if the most general concept // is not promising, it may never get expanded) private boolean forceRefinementLengthIncrease; @@ -135,16 +121,13 @@ public class ROLearner2 { private boolean stop = false; private boolean isRunning = false; - // node from which algorithm has started - private ExampleBasedNode startNode; - // solution protocol - private List solutions = new LinkedList(); + private List solutions = new LinkedList<>(); // used refinement operator and heuristic (exchangeable) private RhoDRDown operator; // private RefinementOperator operator; - // private ExampleBasedHeuristic heuristic; + private ExampleBasedHeuristic heuristic; // specifies whether to compute and log benchmark information private boolean computeBenchmarkInformation = false; @@ -152,12 +135,10 @@ public class ROLearner2 { // comparator used to maintain a stable ordering of nodes, i.e. // an ordering which does not change during the run of the algorithm private NodeComparatorStable nodeComparatorStable = new NodeComparatorStable(); - // stable candidate set; it has no functional part in the algorithm, - // but is a list of the currently best concepts found; - // it is very important to use a concurrent set here as other threads will - // access it (usual iterating is likely to throw a ConcurrentModificationException) - private NavigableSet candidatesStable = new ConcurrentSkipListSet( - nodeComparatorStable); + // node from which algorithm has started + private SearchTreeNonWeak searchTreeStable; + private SearchTreeNonWeakPartialSet searchTree; + // evaluated descriptions // private EvaluatedDescriptionSet evaluatedDescriptions = new EvaluatedDescriptionSet(LearningAlgorithm.MAX_NR_OF_RESULTS); @@ -167,22 +148,15 @@ public class ROLearner2 { // utility variables private DecimalFormat df = new DecimalFormat(); - // candidates for refinement (used for directly accessing - // nodes in the search tree) - private TreeSet candidates; - - // new nodes found during a run of the algorithm - private List newCandidates = new LinkedList(); - // all concepts which have been evaluated as being proper refinements - private SortedSet properRefinements = new TreeSet(); + private SortedSet properRefinements = new TreeSet<>(); // blacklists - private SortedSet tooWeakList = new TreeSet(); - private SortedSet overlyGeneralList = new TreeSet(); + private SortedSet tooWeakList = new TreeSet<>(); + private SortedSet overlyGeneralList = new TreeSet<>(); // set of expanded nodes (TODO: better explanation) - TreeSet expandedNodes = new TreeSet(nodeComparatorStable); + TreeSet expandedNodes = new TreeSet<>(nodeComparatorStable); // statistic variables private int maxRecDepth = 0; @@ -222,6 +196,7 @@ public class ROLearner2 { private int negationPenalty; private OWLDataFactory dataFactory = new OWLDataFactoryImpl(); + private OWLClassExpressionLengthMetric lengthMetric; public ROLearner2( // OCELConfigurator configurator, @@ -238,7 +213,8 @@ public ROLearner2( int maxPosOnlyExpansion, int maxExecutionTimeInSeconds, int minExecutionTimeInSeconds, int guaranteeXgoodDescriptions, int maxClassDescriptionTests, boolean forceRefinementLengthIncrease, boolean terminateOnNoiseReached, - double negativeWeight, double startNodeBonus, double expansionPenaltyFactor, int negationPenalty) { + double negativeWeight, double startNodeBonus, double expansionPenaltyFactor, int negationPenalty, + OWLClassExpressionLengthMetric lengthMetric, OWLObjectRenderer renderer) { PosNegLP lp = (PosNegLP) learningProblem; @@ -263,8 +239,7 @@ public ROLearner2( this.rs = rs; this.operator = (RhoDRDown) operator; this.startDescription = startDescription; - // initialise candidate set with heuristic as ordering - candidates = new TreeSet(heuristic); + this.heuristic = heuristic; this.noise = noise; this.writeSearchTree = writeSearchTree; this.replaceSearchTree = replaceSearchTree; @@ -280,7 +255,8 @@ public ROLearner2( this.guaranteeXgoodDescriptions = guaranteeXgoodDescriptions; this.maxClassDescriptionTests = maxClassDescriptionTests; this.forceRefinementLengthIncrease = forceRefinementLengthIncrease; - + this.lengthMetric = lengthMetric; + this.renderer = renderer; // logger.setLevel(Level.DEBUG); } @@ -290,13 +266,12 @@ public void start() { runtime = System.currentTimeMillis(); // reset values (algorithms may be started several times) - candidates.clear(); - candidatesStable.clear(); - newCandidates.clear(); + searchTree = new SearchTreeNonWeakPartialSet<>(heuristic); + searchTreeStable = new SearchTreeNonWeak<>(nodeComparatorStable); solutions.clear(); maxExecutionTimeAlreadyReached = false; minExecutionTimeAlreadyReached = false; - guaranteeXgoodAlreadyReached = false; + guaranteeXgoodAlreadyReached = false; propernessTestsReasoner = 0; propernessTestsAvoidedByShortConceptConstruction = 0; propernessTestsAvoidedByTooWeakList = 0; @@ -372,23 +347,27 @@ public void start() { allowedMisclassifications = (int) Math.round(noise * nrOfExamples); // start search with start class + ExampleBasedNode startNode; if (startDescription == null) { startNode = new ExampleBasedNode(dataFactory.getOWLThing(), negativeWeight, startNodeBonus, expansionPenaltyFactor, negationPenalty); startNode.setCoveredExamples(positiveExamples, negativeExamples); + startNode.setAccuracyMethod(learningProblem.getAccuracyMethod()); } else { startNode = new ExampleBasedNode(startDescription, negativeWeight, startNodeBonus, expansionPenaltyFactor, negationPenalty); Set coveredNegatives = rs.hasType(startDescription, negativeExamples); Set coveredPositives = rs.hasType(startDescription, positiveExamples); startNode.setCoveredExamples(coveredPositives, coveredNegatives); + startNode.setAccuracyMethod(learningProblem.getAccuracyMethod()); } - candidates.add(startNode); - candidatesStable.add(startNode); + searchTree.addNode(null, startNode); + searchTreeStable.addNode(null, startNode); + ExampleBasedNode previousBestNode = startNode; ExampleBasedNode bestNode = startNode; ExampleBasedNode bestNodeStable = startNode; - logger.info("starting top down refinement with: " + OWLAPIRenderers.toManchesterOWLSyntax(startNode.getConcept()) + " (" + df.format(100*startNode.getAccuracy(nrOfPositiveExamples, nrOfNegativeExamples)) + "% accuracy)"); + logger.info("starting top down refinement with: " + renderer.render(startNode.getConcept()) + " (" + df.format(100*startNode.getAccuracy(nrOfPositiveExamples, nrOfNegativeExamples)) + "% accuracy)"); int loop = 0; @@ -397,8 +376,8 @@ public void start() { long lastTreeTraversalTime = System.nanoTime(); long lastReductionTime = System.nanoTime(); // try a traversal after x seconds - long traversalInterval = 300l * 1000000000l; - long reductionInterval = 300l * 1000000000l; + long traversalInterval = 300L * 1000000000L; + long reductionInterval = 300L * 1000000000L; long currentTime; while (!isTerminationCriteriaReached()) { @@ -406,7 +385,7 @@ public void start() { // print statistics at most once a second currentTime = System.nanoTime(); - if (currentTime - lastPrintTime > 3000000000l) { + if (currentTime - lastPrintTime > 3000000000L) { printStatistics(false); lastPrintTime = currentTime; logger.debug("--- loop " + loop + " started ---"); @@ -427,37 +406,29 @@ public void start() { // Logger.getRootLogger().setLevel(Level.TRACE); } - // System.out.println("next expanded: " + - // candidates.last().getShortDescription(nrOfPositiveExamples, - // nrOfNegativeExamples, baseURI)); - // we record when a more accurate node is found and log it - if (bestNodeStable.getCovPosMinusCovNeg() < candidatesStable.last() + if (bestNodeStable.getCovPosMinusCovNeg() < searchTreeStable.best() .getCovPosMinusCovNeg()) { - String acc = new DecimalFormat( ".00%" ).format((candidatesStable.last().getAccuracy(nrOfPositiveExamples, nrOfNegativeExamples))); + String acc = new DecimalFormat( ".00%" ).format((searchTreeStable.best().getAccuracy(nrOfPositiveExamples, nrOfNegativeExamples))); // no handling needed, it will just look ugly in the output - logger.info("more accurate ("+acc+") class expression found: " + OWLAPIRenderers.toManchesterOWLSyntax(candidatesStable.last().getConcept())); + logger.info("more accurate ("+acc+") class expression found: " + renderer.render(searchTreeStable.best().getConcept())); if(logger.isTraceEnabled()){ - logger.trace(Helper.difference(positiveExamples,bestNodeStable.getCoveredNegatives())); - logger.trace(Helper.difference(negativeExamples,bestNodeStable.getCoveredNegatives())); + logger.trace(Sets.difference(positiveExamples,bestNodeStable.getCoveredNegatives())); + logger.trace(Sets.difference(negativeExamples,bestNodeStable.getCoveredNegatives())); } printBestSolutions(5, false); printStatistics(false); - bestNodeStable = candidatesStable.last(); + bestNodeStable = searchTreeStable.best(); } // chose best node according to heuristics - bestNode = candidates.last(); - // extend best node - newCandidates.clear(); + bestNode = searchTree.best(); // best node is removed temporarily, because extending it can // change its evaluation - candidates.remove(bestNode); + searchTree.updatePrepare(bestNode); extendNodeProper(bestNode, bestNode.getHorizontalExpansion() + 1); - candidates.add(bestNode); - // newCandidates has been filled during node expansion - candidates.addAll(newCandidates); - candidatesStable.addAll(newCandidates); + searchTree.updateDone(bestNode); + previousBestNode = bestNode; // System.out.println("done"); @@ -496,8 +467,8 @@ public void start() { int show = 1; String manchester = "MANCHESTER:\n"; for (ExampleBasedNode c : solutions) { - logger.info(show + ": " + OWLAPIRenderers.toManchesterOWLSyntax(c.getConcept()) - + " (accuracy " + df.format(100*c.getAccuracy(nrOfPositiveExamples, nrOfNegativeExamples)) + "%, length " + logger.info(show + ": " + renderer.render(c.getConcept()) + + " (accuracy " + df.format(100*c.getAccuracy(nrOfPositiveExamples, nrOfNegativeExamples)) + "%, length " + OWLClassExpressionUtils.getLength(c.getConcept()) + ", depth " + OWLClassExpressionUtils.getDepth(c.getConcept()) + ")"); // manchester += show + ": " + c.toManchesterSyntaxString(baseURI, prefixes) + "\n"; @@ -511,7 +482,7 @@ public void start() { logger.info("no appropriate solutions found (try increasing the noisePercentage parameter to what was reported as most accurate expression found above)"); } - logger.debug("size of candidate set: " + candidates.size()); + logger.debug("size of candidate set: " + searchTree.size()); boolean showOrderedSolutions = false; printBestSolutions(20, showOrderedSolutions); @@ -523,7 +494,7 @@ public void start() { } else { logger.info("Algorithm terminated successfully ("+conceptTests+" descriptions tested).\n"); logger.info(rs.toString()); - } + } totalLearningTime.stop(); isRunning = false; @@ -576,66 +547,44 @@ private void extendNodeProper(ExampleBasedNode node, OWLClassExpression concept, if (refinements.size() > maxNrOfRefinements) maxNrOfRefinements = refinements.size(); + // remove all refinements that are already children of the node long childConceptsDeletionTimeNsStart = System.nanoTime(); - // entferne aus den refinements alle Konzepte, die bereits Kinder des - // Knotens sind - // for(Node n : node.getChildren()) { - // refinements.remove(n.getConcept()); - // } - - // das ist viel schneller, allerdings bekommt man ein anderes candidate - // set(??) refinements.removeAll(node.getChildConcepts()); - childConceptsDeletionTimeNs += System.nanoTime() - childConceptsDeletionTimeNsStart; - // if(refinements.size()<30) { - // // System.out.println("refinements: " + refinements); - // for(OWLClassExpression refinement: refinements) - // System.out.println("refinement: " + refinement); - // } - + // evaluate all concepts whose length is bigger than the horizontal expansion of the node long evaluateSetCreationTimeNsStart = System.nanoTime(); - - // alle Konzepte, die länger als horizontal expansion sind, müssen - // ausgewertet - // werden - TreeSet toEvaluateConcepts = new TreeSet(); + Set toEvaluateConcepts = new TreeSet<>(); Iterator it = refinements.iterator(); - // for(Concept refinement : refinements) { + while (it.hasNext()) { OWLClassExpression refinement = it.next(); - if (OWLClassExpressionUtils.getLength(refinement) > node.getHorizontalExpansion()) { - // sagt aus, ob festgestellt wurde, ob refinement proper ist - // (sagt nicht aus, dass das refinement proper ist!) - boolean propernessDetected = false; + if (OWLClassExpressionUtils.getLength(refinement, lengthMetric) > node.getHorizontalExpansion()) { + // TRUE means that improperness was detected, but FALSE does not mean that the refinement is proper + boolean impropernessDetected = false; // 1. short concept construction if (useShortConceptConstruction) { - // kurzes Konzept konstruieren OWLClassExpression shortConcept = ConceptTransformation.getShortConcept(refinement); + // compare with original concept int n = shortConcept.compareTo(concept); - // Konzepte sind gleich also Refinement improper + // concepts are equal, i.e. refinement is improper if (n == 0) { propernessTestsAvoidedByShortConceptConstruction++; - propernessDetected = true; - -// System.out.println("refinement " + refinement + -// " can be shortened"); -// System.exit(0); + impropernessDetected = true; } } // 2. too weak test - if (!propernessDetected && useTooWeakList) { + if (!impropernessDetected && useTooWeakList) { if (refinement instanceof OWLObjectIntersectionOf) { boolean tooWeakElement = containsTooWeakElement((OWLObjectIntersectionOf) refinement); if (tooWeakElement) { propernessTestsAvoidedByTooWeakList++; conceptTestsTooWeakList++; - propernessDetected = true; + impropernessDetected = true; // tooWeakList.add(refinement); // Knoten wird direkt erzeugt (es ist buganfällig @@ -647,10 +596,9 @@ private void extendNodeProper(ExampleBasedNode node, OWLClassExpression concept, tooWeakList.add(refinement); ExampleBasedNode newNode = new ExampleBasedNode(refinement, negativeWeight, startNodeBonus, expansionPenaltyFactor, negationPenalty); - newNode.setHorizontalExpansion(OWLClassExpressionUtils.getLength(refinement) - 1); + newNode.setHorizontalExpansion(OWLClassExpressionUtils.getLength(refinement, lengthMetric) - 1); newNode.setTooWeak(true); - newNode - .setQualityEvaluationMethod(ExampleBasedNode.QualityEvaluationMethod.TOO_WEAK_LIST); + newNode.setQualityEvaluationMethod(ExampleBasedNode.QualityEvaluationMethod.TOO_WEAK_LIST); node.addChild(newNode); // Refinement muss gelöscht werden, da es proper ist @@ -660,16 +608,8 @@ private void extendNodeProper(ExampleBasedNode node, OWLClassExpression concept, } // properness konnte nicht vorher ermittelt werden - if (!propernessDetected) { + if (!impropernessDetected) { toEvaluateConcepts.add(refinement); - // if(!res) { - // System.out.println("already in: " + refinement); - // Comparator comp = toEvaluateConcepts.comparator(); - // for(OWLClassExpression d : toEvaluateConcepts) { - // if(comp.compare(d,refinement)==0) - // System.out.println("see: " + d); - // } - // } } } @@ -721,7 +661,7 @@ private void extendNodeProper(ExampleBasedNode node, OWLClassExpression concept, // System.out.println("refinements: " + refinements); // else // System.out.println("refinements: more than 10"); - // + // // System.out.println("improper concepts: " + improperConcepts); for (OWLClassExpression refinement : properConcepts) { @@ -740,7 +680,7 @@ private void extendNodeProper(ExampleBasedNode node, OWLClassExpression concept, ExampleBasedNode newNode = new ExampleBasedNode(refinement, negativeWeight, startNodeBonus, expansionPenaltyFactor, negationPenalty); // die -1 ist wichtig, da sonst keine gleich langen Refinements // für den neuen Knoten erlaubt wären z.B. person => male - newNode.setHorizontalExpansion(OWLClassExpressionUtils.getLength(refinement) - 1); + newNode.setHorizontalExpansion(OWLClassExpressionUtils.getLength(refinement, lengthMetric) - 1); boolean qualityKnown = false; int quality = -2; @@ -755,6 +695,7 @@ private void extendNodeProper(ExampleBasedNode node, OWLClassExpression concept, newNode .setQualityEvaluationMethod(ExampleBasedNode.QualityEvaluationMethod.OVERLY_GENERAL_LIST); newNode.setCoveredExamples(positiveExamples, negativeExamples); + newNode.setAccuracyMethod(learningProblem.getAccuracyMethod()); } } @@ -769,7 +710,7 @@ private void extendNodeProper(ExampleBasedNode node, OWLClassExpression concept, // determine individuals which have not been covered yet // (more efficient than full retrieval) Set coveredPositives = node.getCoveredPositives(); - Set newlyCoveredPositives = new HashSet(); + Set newlyCoveredPositives = new HashSet<>(); // calculate how many pos. examples are not covered by the // parent node of the refinement @@ -802,7 +743,7 @@ private void extendNodeProper(ExampleBasedNode node, OWLClassExpression concept, Set newlyCoveredNegatives = null; if (quality != -1) { Set coveredNegatives = node.getCoveredNegatives(); - newlyCoveredNegatives = new HashSet(); + newlyCoveredNegatives = new HashSet<>(); for (OWLIndividual i : coveredNegatives) { boolean covered = rs.hasType(refinement, i); @@ -814,12 +755,20 @@ private void extendNodeProper(ExampleBasedNode node, OWLClassExpression concept, propernessCalcReasoningTimeNs += System.nanoTime() - propCalcReasoningStart2; newNode .setQualityEvaluationMethod(ExampleBasedNode.QualityEvaluationMethod.REASONER); + if (quality != -1 && !(learningProblem.getAccuracyMethod() instanceof AccMethodNoWeakness) && + learningProblem.getAccuracyMethod().getAccOrTooWeak2( + newlyCoveredPositives.size(), nrOfPositiveExamples - newlyCoveredPositives.size(), + newlyCoveredNegatives.size(), nrOfNegativeExamples - newlyCoveredNegatives.size(), + 1) == -1) + quality = -1; + if (quality != -1) { // quality is the number of misclassifications (if it is // not too weak) quality = (nrOfPositiveExamples - newlyCoveredPositives.size()) + newlyCoveredNegatives.size(); newNode.setCoveredExamples(newlyCoveredPositives, newlyCoveredNegatives); + newNode.setAccuracyMethod(learningProblem.getAccuracyMethod()); } } @@ -834,8 +783,6 @@ private void extendNodeProper(ExampleBasedNode node, OWLClassExpression concept, solutions.add(newNode); } - newCandidates.add(newNode); - // we need to make sure that all positives are covered // before adding something to the overly general list if ((newNode.getCoveredPositives().size() == nrOfPositiveExamples) @@ -850,11 +797,11 @@ private void extendNodeProper(ExampleBasedNode node, OWLClassExpression concept, // it is often useful to continue expanding until a longer node is // reached (to replace atomic concepts with more specific ones) if(forceRefinementLengthIncrease && !newNode.isTooWeak()) { - // extend node again if its concept has the same length - if(OWLClassExpressionUtils.getLength(node.getConcept()) == OWLClassExpressionUtils.getLength(newNode.getConcept())) { + // extend node again if its concept has the same length + if(OWLClassExpressionUtils.getLength(node.getConcept(), lengthMetric) == OWLClassExpressionUtils.getLength(newNode.getConcept(), lengthMetric)) { extendNodeProper(newNode, refinement, maxLength, recDepth + 1); } - } + } } } @@ -911,7 +858,7 @@ private void printStatistics(boolean finalStats) { if (!finalStats) { - ExampleBasedNode bestNode = candidatesStable.last(); + ExampleBasedNode bestNode = searchTreeStable.best(); // double accuracy = 100 * ((bestNode.getCoveredPositives().size() // + nrOfNegativeExamples - // bestNode.getCoveredNegatives().size())/(double)nrOfExamples); @@ -919,7 +866,7 @@ private void printStatistics(boolean finalStats) { // String bestNodeString = "currently best node: " + bestNode + " // accuracy: " + df.format(accuracy) + "%"; logger.debug("start node: " - + startNode.getShortDescription(nrOfPositiveExamples, nrOfNegativeExamples, + + searchTreeStable.getRoot().getShortDescription(nrOfPositiveExamples, nrOfNegativeExamples, baseURI, prefixes)); String bestNodeString = "currently best node: " + bestNode.getShortDescription(nrOfPositiveExamples, nrOfNegativeExamples, @@ -931,12 +878,12 @@ private void printStatistics(boolean finalStats) { if (bestNode.getCoveredNegatives().size() <= 5) logger.trace("covered negs: " + bestNode.getCoveredNegatives()); String expandedNodeString = "next expanded node: " - + candidates.last().getShortDescription(nrOfPositiveExamples, + + searchTree.best().getShortDescription(nrOfPositiveExamples, nrOfNegativeExamples, baseURI, prefixes); // searchTree += expandedNodeString + "\n"; logger.debug(expandedNodeString); logger.debug("algorithm runtime " + Helper.prettyPrintNanoSeconds(algorithmRuntime)); - logger.debug("size of candidate set: " + candidates.size()); + logger.debug("size of candidate set: " + searchTree.size()); // System.out.println("properness max recursion depth: " + // maxRecDepth); // System.out.println("max. number of one-step refinements: " + @@ -1065,12 +1012,12 @@ private void traverseTree() { logger.debug("tree traversal start accuracy: " + currentAccuracy); int i = 0; // start from the most promising nodes - NavigableSet reverseView = candidatesStable.descendingSet(); + SortedSet reverseView = searchTreeStable.descendingSet(); for (ExampleBasedNode currNode : reverseView) { // compute covered positives and negatives - SortedSet newCoveredPositives = new TreeSet(currentCoveredPos); + SortedSet newCoveredPositives = new TreeSet<>(currentCoveredPos); newCoveredPositives.retainAll(currNode.getCoveredPositives()); - SortedSet newCoveredNegatives = new TreeSet(currentCoveredNeg); + SortedSet newCoveredNegatives = new TreeSet<>(currentCoveredNeg); newCoveredNegatives.retainAll(currNode.getCoveredNegatives()); // compute the accuracy we would get by adding this node @@ -1102,7 +1049,7 @@ private void traverseTree() { OWLClassExpression mc = dataFactory.getOWLObjectIntersectionOf(currentDescription, currNode.getConcept()); mc = ConceptTransformation.cleanConceptNonRecursive(mc); - ConceptTransformation.transformToOrderedNegationNormalFormNonRecursive(mc); + mc = mc.getNNF(); // System.out.println("extended concept to: " + mc); logger.debug("misclassifications: " + misclassifications); @@ -1137,7 +1084,7 @@ private ExampleBasedNode findBestTraversalStartNode() { int currScore = 0; int i = 0; ExampleBasedNode currNode = null; - NavigableSet reverseView = candidatesStable.descendingSet(); + SortedSet reverseView = searchTreeStable.descendingSet(); for (ExampleBasedNode node : reverseView) { int score = 2 * node.getCoveredPositives().size() + (nrOfNegativeExamples - node.getCoveredNegatives().size()); @@ -1154,8 +1101,8 @@ private ExampleBasedNode findBestTraversalStartNode() { } private void reduceCandidates() { - Iterator it = candidatesStable.descendingIterator(); - Set promisingNodes = new HashSet(); + Iterator it = searchTreeStable.descendingIterator(); + Set promisingNodes = new HashSet<>(); int i = 0; while (it.hasNext() && promisingNodes.size() < candidatePostReductionSize) { ExampleBasedNode node = it.next(); @@ -1183,11 +1130,13 @@ private void reduceCandidates() { } i++; } - candidates.retainAll(promisingNodes); + searchTree.retainAll(promisingNodes); logger.debug("searched " + i + " nodes and picked the following promising descriptions:"); - for (ExampleBasedNode node : promisingNodes) - logger.debug(node.getShortDescription(nrOfPositiveExamples, nrOfNegativeExamples, - baseURI, prefixes)); + if (logger.isDebugEnabled()) { + for (ExampleBasedNode node : promisingNodes) + logger.debug(node.getShortDescription(nrOfPositiveExamples, nrOfNegativeExamples, + baseURI, prefixes)); + } } /* @@ -1205,14 +1154,14 @@ public void stop() { } public OWLClassExpression getBestSolution() { - return candidatesStable.last().getConcept(); + return searchTreeStable.best().getConcept(); } public List getCurrentlyBestDescriptions() { - List best = new LinkedList(); + List best = new LinkedList<>(); int i = 0; int nrOfSolutions = 200; - for (ExampleBasedNode n : candidatesStable.descendingSet()) { + for (ExampleBasedNode n : searchTreeStable.descendingSet()) { best.add(n.getConcept()); if (i == nrOfSolutions) return best; @@ -1222,9 +1171,9 @@ public List getCurrentlyBestDescriptions() { } public TreeSet getCurrentlyBestEvaluatedDescriptions() { - Iterator it = candidatesStable.descendingIterator(); + Iterator it = searchTreeStable.descendingIterator(); int count = 0; - TreeSet cbd = new TreeSet(edComparator); + TreeSet cbd = new TreeSet<>(edComparator); while(it.hasNext()) { ExampleBasedNode eb = it.next(); cbd.add(new EvaluatedDescriptionPosNeg(eb.getConcept(), getScore(eb.getConcept()))); @@ -1245,9 +1194,9 @@ public void printBestSolutions(int nrOfSolutions, boolean showOrderedSolutions) } // if(!logger.getLevel().toString().equalsIgnoreCase("TRACE"))return; if (nrOfSolutions == 0) - nrOfSolutions = candidatesStable.size(); + nrOfSolutions = searchTreeStable.size(); int i = 0; - for (ExampleBasedNode n : candidatesStable.descendingSet()) { + for (ExampleBasedNode n : searchTreeStable.descendingSet()) { if (n.getAccuracy(nrOfPositiveExamples, nrOfNegativeExamples) < 1) break; logger.trace("best: " @@ -1260,11 +1209,11 @@ public void printBestSolutions(int nrOfSolutions, boolean showOrderedSolutions) if (showOrderedSolutions) { logger.trace("ordered by generality (most special solutions first):"); SubsumptionComparator sc = new SubsumptionComparator(rs); - SortedSet solutionsOrderedBySubsumption = new TreeSet(sc); + SortedSet solutionsOrderedBySubsumption = new TreeSet<>(sc); // solutionsOrderedBySubsumption.addAll(solutions); for (OWLClassExpression d : solutionsOrderedBySubsumption) logger.trace("special: " + d); - throw new Error("implementation needs to be updated to show ordered solutions"); + throw new Error("implementation needs to be updated to show ordered solutions"); } /* * for (int j = 0; j < solutions.size(); j++) { OWLClassExpression d = @@ -1274,15 +1223,15 @@ public void printBestSolutions(int nrOfSolutions, boolean showOrderedSolutions) } public ScorePosNeg getSolutionScore() { - return (ScorePosNeg) learningProblem.computeScore(getBestSolution()); + return learningProblem.computeScore(getBestSolution()); } private ScorePosNeg getScore(OWLClassExpression d) { - return (ScorePosNeg) learningProblem.computeScore(d); + return learningProblem.computeScore(d); } public ExampleBasedNode getStartNode() { - return startNode; + return searchTreeStable.getRoot(); } /** @@ -1302,10 +1251,11 @@ public ExampleBasedNode getStartNode() { * @return true if the algorithm should stop, this is mostly indepent of the question if a solution was found */ private boolean isTerminationCriteriaReached(){ + // algorithm was stopped from outside if(this.stop){ return true; } -// System.out.println("ssssss"); + long totalTimeNeeded = System.currentTimeMillis() - this.runtime; long maxMilliSeconds = maxExecutionTimeInSeconds * 1000; long minMilliSeconds = minExecutionTimeInSeconds * 1000; @@ -1328,7 +1278,7 @@ else if (maxMilliSeconds < totalTimeNeeded) { } //ignore default - if(maxClassDescriptionTests == 0) + if(maxClassDescriptionTests == 0) result = false; //test else if(conceptTests >= maxClassDescriptionTests){ @@ -1337,7 +1287,7 @@ else if(conceptTests >= maxClassDescriptionTests){ return true; } - // we stop if sufficiently many solutions (concepts fitting the noise parameter) have been + // we stop if sufficiently many solutions (concepts fitting the noise parameter) have been // reached - unless this termination criterion is switched off using terminateOnNoiseReached = false if (guaranteeXgoodAlreadyReached){ result = true; @@ -1363,7 +1313,7 @@ else if(conceptTests >= maxClassDescriptionTests){ result = result && true; }else { result = false; - } + } return result; diff --git a/components-core/src/main/java/org/dllearner/algorithms/ocel/SubsumptionComparator.java b/components-core/src/main/java/org/dllearner/algorithms/ocel/SubsumptionComparator.java index e73a223f6c..7f60c491f0 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/ocel/SubsumptionComparator.java +++ b/components-core/src/main/java/org/dllearner/algorithms/ocel/SubsumptionComparator.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.ocel; import java.util.Comparator; diff --git a/components-core/src/main/java/org/dllearner/algorithms/pattern/AbstractRenderer.java b/components-core/src/main/java/org/dllearner/algorithms/pattern/AbstractRenderer.java index cc3ebd0bb2..7e3d0084a7 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/pattern/AbstractRenderer.java +++ b/components-core/src/main/java/org/dllearner/algorithms/pattern/AbstractRenderer.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.pattern; /* @@ -46,9 +64,9 @@ import java.util.List; import java.util.StringTokenizer; -import org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax; import org.semanticweb.owlapi.io.OWLRendererException; import org.semanticweb.owlapi.io.OWLRendererIOException; +import org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax; import org.semanticweb.owlapi.util.ShortFormProvider; /** @@ -77,7 +95,7 @@ public class AbstractRenderer { public AbstractRenderer(Writer writer, ShortFormProvider shortFormProvider) { this.writer = writer; this.shortFormProvider = shortFormProvider; - tabs = new ArrayList(); + tabs = new ArrayList<>(); pushTab(0); } diff --git a/components-core/src/main/java/org/dllearner/algorithms/pattern/FragmentExtractionStrategy.java b/components-core/src/main/java/org/dllearner/algorithms/pattern/FragmentExtractionStrategy.java index 7b4a4603dd..8707aa0bdd 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/pattern/FragmentExtractionStrategy.java +++ b/components-core/src/main/java/org/dllearner/algorithms/pattern/FragmentExtractionStrategy.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.pattern; diff --git a/components-core/src/main/java/org/dllearner/algorithms/pattern/FragmentExtractor.java b/components-core/src/main/java/org/dllearner/algorithms/pattern/FragmentExtractor.java index 8ea6d05473..bacebcd8c2 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/pattern/FragmentExtractor.java +++ b/components-core/src/main/java/org/dllearner/algorithms/pattern/FragmentExtractor.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.pattern; @@ -14,9 +29,11 @@ public interface FragmentExtractor { /** - * @param cls - * @param maxFragmentDepth - * @return + * Extracts a fragment of the knowledge base for the given class C with a max. depth of of triples starting from + * instances of C. + * @param cls the class + * @param maxFragmentDepth the maximum depth + * @return the fragment */ Model extractFragment(OWLClass cls, int maxFragmentDepth); diff --git a/components-core/src/main/java/org/dllearner/algorithms/pattern/FullIRIEntityShortFromProvider.java b/components-core/src/main/java/org/dllearner/algorithms/pattern/FullIRIEntityShortFromProvider.java index 62b77d0893..748a65a885 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/pattern/FullIRIEntityShortFromProvider.java +++ b/components-core/src/main/java/org/dllearner/algorithms/pattern/FullIRIEntityShortFromProvider.java @@ -1,16 +1,37 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.pattern; import org.semanticweb.owlapi.model.OWLEntity; import org.semanticweb.owlapi.util.ShortFormProvider; +import javax.annotation.Nonnull; + public class FullIRIEntityShortFromProvider implements ShortFormProvider{ @Override public void dispose() { } + @Nonnull @Override - public String getShortForm(OWLEntity entity) { + public String getShortForm(@Nonnull OWLEntity entity) { return entity.toStringID(); } diff --git a/components-core/src/main/java/org/dllearner/algorithms/pattern/IndividualBasedFragmentExtractor.java b/components-core/src/main/java/org/dllearner/algorithms/pattern/IndividualBasedFragmentExtractor.java index b0105bd13b..cc9d507e4d 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/pattern/IndividualBasedFragmentExtractor.java +++ b/components-core/src/main/java/org/dllearner/algorithms/pattern/IndividualBasedFragmentExtractor.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.pattern; @@ -86,7 +101,7 @@ public Model extractFragment(OWLClass cls, int maxFragmentDepth) { } private Set getRandomIndividuals(OWLClass cls){ - Set individuals = new HashSet(); + Set individuals = new HashSet<>(); String query = "SELECT ?s WHERE {?s a <" + cls.toStringID() + ">} LIMIT " + maxNrOfIndividuals; QueryExecution qe = qef.createQueryExecution(query); diff --git a/components-core/src/main/java/org/dllearner/algorithms/pattern/MaximumModalDepthDetector.java b/components-core/src/main/java/org/dllearner/algorithms/pattern/MaximumModalDepthDetector.java index 5c28f682a6..a28762459e 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/pattern/MaximumModalDepthDetector.java +++ b/components-core/src/main/java/org/dllearner/algorithms/pattern/MaximumModalDepthDetector.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.pattern; diff --git a/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomPatternFinder.java b/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomPatternFinder.java index 86a846a857..f4ab6d65da 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomPatternFinder.java +++ b/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomPatternFinder.java @@ -1,59 +1,54 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.pattern; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.io.StringWriter; -import java.net.URI; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Queue; -import java.util.Set; -import java.util.prefs.Preferences; - -import org.coode.owlapi.functionalparser.OWLFunctionalSyntaxOWLParser; +import com.google.common.collect.HashMultiset; +import com.google.common.collect.Multiset; import org.dllearner.kb.dataset.OWLOntologyDataset; import org.dllearner.kb.repository.OntologyRepository; import org.dllearner.kb.repository.OntologyRepositoryEntry; +import org.dllearner.utilities.owl.ManchesterOWLSyntaxOWLObjectRendererImplExt; import org.ini4j.IniPreferences; -import org.ini4j.InvalidFileFormatException; import org.semanticweb.owlapi.apibinding.OWLManager; +import org.semanticweb.owlapi.functional.parser.OWLFunctionalSyntaxOWLParser; +import org.semanticweb.owlapi.functional.renderer.FunctionalSyntaxObjectRenderer; import org.semanticweb.owlapi.io.OWLObjectRenderer; import org.semanticweb.owlapi.io.StringDocumentSource; import org.semanticweb.owlapi.io.UnparsableOntologyException; -import org.semanticweb.owlapi.model.AxiomType; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLFunctionalDataPropertyAxiom; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException; -import org.semanticweb.owlapi.model.OWLOntologyCreationException; -import org.semanticweb.owlapi.model.OWLOntologyLoaderConfiguration; -import org.semanticweb.owlapi.model.OWLOntologyManager; - -import uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.ManchesterOWLSyntaxOWLObjectRendererImpl; +import org.semanticweb.owlapi.model.*; +import org.semanticweb.owlapi.model.parameters.Imports; -import com.google.common.collect.HashMultiset; -import com.google.common.collect.Multiset; +import java.io.IOException; +import java.io.InputStream; +import java.io.StringWriter; +import java.net.URI; +import java.sql.*; +import java.util.*; +import java.util.prefs.Preferences; public class OWLAxiomPatternFinder { - private static Queue classVarQueue = new LinkedList(); - private static Queue propertyVarQueue = new LinkedList(); - private static Queue individualVarQueue = new LinkedList(); - private static Queue datatypeVarQueue = new LinkedList(); + private static Queue classVarQueue = new LinkedList<>(); + private static Queue propertyVarQueue = new LinkedList<>(); + private static Queue individualVarQueue = new LinkedList<>(); + private static Queue datatypeVarQueue = new LinkedList<>(); static{ for(int i = 65; i <= 90; i++){ @@ -66,8 +61,8 @@ public class OWLAxiomPatternFinder { propertyVarQueue.add(String.valueOf((char)i)); } - }; - + } + private OntologyRepository repository; private OWLOntologyManager manager; private OWLDataFactory dataFactory; @@ -80,7 +75,7 @@ public class OWLAxiomPatternFinder { private PreparedStatement insertPatternIdPs; private PreparedStatement insertOntologyPatternPs; - private OWLObjectRenderer axiomRenderer = new ManchesterOWLSyntaxOWLObjectRendererImpl(); + private OWLObjectRenderer axiomRenderer = new ManchesterOWLSyntaxOWLObjectRendererImplExt(); private boolean randomOrder = false; @@ -127,7 +122,7 @@ private String render(OWLAxiom axiom){ OWLOntology ontology = man.createOntology(); man.addAxiom(ontology, axiom); StringWriter sw = new StringWriter(); - org.coode.owlapi.functionalrenderer.OWLObjectRenderer r = new org.coode.owlapi.functionalrenderer.OWLObjectRenderer(man, ontology, sw); + FunctionalSyntaxObjectRenderer r = new FunctionalSyntaxObjectRenderer(ontology, sw); axiom.accept(r); return sw.toString(); } catch (OWLOntologyCreationException e) { @@ -148,15 +143,7 @@ private void initDBConnection() { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://" + dbServer + "/" + dbName; conn = DriverManager.getConnection(url, dbUser, dbPass); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } catch (SQLException e) { - e.printStackTrace(); - } catch (InvalidFileFormatException e) { - e.printStackTrace(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { + } catch (ClassNotFoundException | IOException | SQLException e) { e.printStackTrace(); } } @@ -306,18 +293,18 @@ private int addOntology(URI physicalURI, OWLOntology ontology){ insertOntologyPs.setString(1, url); insertOntologyPs.setString(2, ontologyIRI); insertOntologyPs.setString(3, repository.getName()); - Set tbox = ontology.getTBoxAxioms(true); - Set rbox = ontology.getRBoxAxioms(true); - Set abox = ontology.getABoxAxioms(true); + Set tbox = ontology.getTBoxAxioms(Imports.INCLUDED); + Set rbox = ontology.getRBoxAxioms(Imports.INCLUDED); + Set abox = ontology.getABoxAxioms(Imports.INCLUDED); insertOntologyPs.setInt(4, tbox.size() + rbox.size() + abox.size()); insertOntologyPs.setInt(5, tbox.size()); insertOntologyPs.setInt(6, rbox.size()); insertOntologyPs.setInt(7, abox.size()); - insertOntologyPs.setInt(8, ontology.getClassesInSignature(true).size()); - insertOntologyPs.setInt(9, ontology.getObjectPropertiesInSignature(true).size()); - insertOntologyPs.setInt(10, ontology.getDataPropertiesInSignature(true).size()); - insertOntologyPs.setInt(11, ontology.getIndividualsInSignature(true).size()); + insertOntologyPs.setInt(8, ontology.getClassesInSignature(Imports.INCLUDED).size()); + insertOntologyPs.setInt(9, ontology.getObjectPropertiesInSignature(Imports.INCLUDED).size()); + insertOntologyPs.setInt(10, ontology.getDataPropertiesInSignature(Imports.INCLUDED).size()); + insertOntologyPs.setInt(11, ontology.getIndividualsInSignature(Imports.INCLUDED).size()); insertOntologyPs.execute(); } catch (SQLException e) { e.printStackTrace(); @@ -355,7 +342,7 @@ public void start() { OWLAxiomRenamer renamer = new OWLAxiomRenamer(dataFactory); Collection entries = repository.getEntries(); if(randomOrder){ - List entryList = new ArrayList(repository.getEntries()); + List entryList = new ArrayList<>(repository.getEntries()); Collections.shuffle(entryList); entries = entryList; } @@ -371,10 +358,10 @@ public void start() { manager = OWLManager.createOWLOntologyManager(); OWLOntology ontology = manager.loadOntology(IRI.create(uri)); Multiset axiomPatterns = HashMultiset.create(); - Set logicalAxioms = new HashSet(); + Set logicalAxioms = new HashSet<>(); for (AxiomType type : AxiomType.AXIOM_TYPES) { if(type.isLogical()){ - logicalAxioms.addAll(ontology.getAxioms(type, true)); + logicalAxioms.addAll(ontology.getAxioms(type, Imports.INCLUDED)); } } System.out.println(" (" + logicalAxioms.size() + " axioms)"); @@ -410,7 +397,7 @@ public static void main(String[] args) throws Exception { OWLOntology ontology = man.createOntology(); man.addAxiom(ontology, axiom); StringWriter sw = new StringWriter(); - org.coode.owlapi.functionalrenderer.OWLObjectRenderer r = new org.coode.owlapi.functionalrenderer.OWLObjectRenderer(man, ontology, sw); + FunctionalSyntaxObjectRenderer r = new FunctionalSyntaxObjectRenderer(ontology, sw); axiom.accept(r); System.out.println(sw.toString()); StringDocumentSource s = new StringDocumentSource("Ontology(" + sw.toString() + ")"); diff --git a/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomRenamer.java b/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomRenamer.java index 2d4fcc1f30..d5ea186f19 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomRenamer.java +++ b/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomRenamer.java @@ -1,65 +1,26 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.pattern; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TreeSet; - -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom; -import org.semanticweb.owlapi.model.OWLAnnotationPropertyDomainAxiom; -import org.semanticweb.owlapi.model.OWLAnnotationPropertyRangeAxiom; -import org.semanticweb.owlapi.model.OWLAsymmetricObjectPropertyAxiom; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLAxiomVisitor; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLClassAssertionAxiom; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom; -import org.semanticweb.owlapi.model.OWLDataPropertyDomainAxiom; -import org.semanticweb.owlapi.model.OWLDataPropertyExpression; -import org.semanticweb.owlapi.model.OWLDataPropertyRangeAxiom; -import org.semanticweb.owlapi.model.OWLDataRange; -import org.semanticweb.owlapi.model.OWLDatatypeDefinitionAxiom; -import org.semanticweb.owlapi.model.OWLDeclarationAxiom; -import org.semanticweb.owlapi.model.OWLDifferentIndividualsAxiom; -import org.semanticweb.owlapi.model.OWLDisjointClassesAxiom; -import org.semanticweb.owlapi.model.OWLDisjointDataPropertiesAxiom; -import org.semanticweb.owlapi.model.OWLDisjointObjectPropertiesAxiom; -import org.semanticweb.owlapi.model.OWLDisjointUnionAxiom; -import org.semanticweb.owlapi.model.OWLEntity; -import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom; -import org.semanticweb.owlapi.model.OWLEquivalentDataPropertiesAxiom; -import org.semanticweb.owlapi.model.OWLEquivalentObjectPropertiesAxiom; -import org.semanticweb.owlapi.model.OWLFunctionalDataPropertyAxiom; -import org.semanticweb.owlapi.model.OWLFunctionalObjectPropertyAxiom; -import org.semanticweb.owlapi.model.OWLHasKeyAxiom; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLInverseFunctionalObjectPropertyAxiom; -import org.semanticweb.owlapi.model.OWLInverseObjectPropertiesAxiom; -import org.semanticweb.owlapi.model.OWLIrreflexiveObjectPropertyAxiom; -import org.semanticweb.owlapi.model.OWLLiteral; -import org.semanticweb.owlapi.model.OWLNegativeDataPropertyAssertionAxiom; -import org.semanticweb.owlapi.model.OWLNegativeObjectPropertyAssertionAxiom; -import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom; -import org.semanticweb.owlapi.model.OWLObjectPropertyDomainAxiom; -import org.semanticweb.owlapi.model.OWLObjectPropertyExpression; -import org.semanticweb.owlapi.model.OWLObjectPropertyRangeAxiom; -import org.semanticweb.owlapi.model.OWLPropertyExpression; -import org.semanticweb.owlapi.model.OWLReflexiveObjectPropertyAxiom; -import org.semanticweb.owlapi.model.OWLSameIndividualAxiom; -import org.semanticweb.owlapi.model.OWLSubAnnotationPropertyOfAxiom; -import org.semanticweb.owlapi.model.OWLSubClassOfAxiom; -import org.semanticweb.owlapi.model.OWLSubDataPropertyOfAxiom; -import org.semanticweb.owlapi.model.OWLSubObjectPropertyOfAxiom; -import org.semanticweb.owlapi.model.OWLSubPropertyChainOfAxiom; -import org.semanticweb.owlapi.model.OWLSymmetricObjectPropertyAxiom; -import org.semanticweb.owlapi.model.OWLTransitiveObjectPropertyAxiom; -import org.semanticweb.owlapi.model.SWRLRule; +import org.semanticweb.owlapi.model.*; + +import java.util.*; public class OWLAxiomRenamer implements OWLAxiomVisitor { @@ -75,7 +36,7 @@ public OWLAxiomRenamer(OWLDataFactory df) { } public OWLAxiom rename(OWLAxiom axiom){ - Map renaming = new HashMap(); + Map renaming = new HashMap<>(); expressionRenamer = new OWLClassExpressionRenamer(df, renaming); axiom.accept(this); return renamedAxiom; @@ -118,7 +79,7 @@ public void visit(OWLReflexiveObjectPropertyAxiom axiom) { @Override public void visit(OWLDisjointClassesAxiom axiom) { Set classExpressions = axiom.getClassExpressions(); - Set renamedClassExpressions = new HashSet(); + Set renamedClassExpressions = new HashSet<>(); for (OWLClassExpression classExpression : classExpressions) { renamedClassExpressions.add(expressionRenamer.rename(classExpression)); } @@ -146,7 +107,7 @@ public void visit(OWLObjectPropertyDomainAxiom axiom) { @Override public void visit(OWLEquivalentObjectPropertiesAxiom axiom) { Set properties = axiom.getProperties(); - Set renamedProperties = new HashSet(); + Set renamedProperties = new HashSet<>(); for (OWLObjectPropertyExpression property : properties) { renamedProperties.add(expressionRenamer.rename(property)); } @@ -166,7 +127,7 @@ public void visit(OWLNegativeDataPropertyAssertionAxiom axiom) { @Override public void visit(OWLDifferentIndividualsAxiom axiom) { - Set renamedIndividuals = new HashSet(); + Set renamedIndividuals = new HashSet<>(); if(normalizeABoxAxioms){ renamedIndividuals.add(df.getOWLNamedIndividual(IRI.create("http://dl-learner.org/pattern/a"))); renamedIndividuals.add(df.getOWLNamedIndividual(IRI.create("http://dl-learner.org/pattern/b"))); @@ -181,7 +142,7 @@ public void visit(OWLDifferentIndividualsAxiom axiom) { @Override public void visit(OWLDisjointDataPropertiesAxiom axiom) { Set properties = axiom.getProperties(); - Set renamedProperties = new HashSet(); + Set renamedProperties = new HashSet<>(); for (OWLDataPropertyExpression property : properties) { renamedProperties.add(expressionRenamer.rename(property)); } @@ -191,7 +152,7 @@ public void visit(OWLDisjointDataPropertiesAxiom axiom) { @Override public void visit(OWLDisjointObjectPropertiesAxiom axiom) { Set properties = axiom.getProperties(); - Set renamedProperties = new HashSet(); + Set renamedProperties = new HashSet<>(); for (OWLObjectPropertyExpression property : properties) { renamedProperties.add(expressionRenamer.rename(property)); } @@ -239,7 +200,7 @@ public void visit(OWLDisjointUnionAxiom axiom) { OWLClass cls = axiom.getOWLClass(); cls = expressionRenamer.rename(cls).asOWLClass(); Set classExpressions = axiom.getClassExpressions(); - Set renamedClassExpressions = new HashSet(); + Set renamedClassExpressions = new HashSet<>(); for (OWLClassExpression classExpression : classExpressions) { renamedClassExpressions.add(expressionRenamer.rename(classExpression)); } @@ -272,7 +233,7 @@ public void visit(OWLFunctionalDataPropertyAxiom axiom) { @Override public void visit(OWLEquivalentDataPropertiesAxiom axiom) { Set properties = axiom.getProperties(); - Set renamedProperties = new HashSet(); + Set renamedProperties = new HashSet<>(); for (OWLDataPropertyExpression property : properties) { renamedProperties.add(expressionRenamer.rename(property)); } @@ -291,11 +252,11 @@ public void visit(OWLClassAssertionAxiom axiom) { @Override public void visit(OWLEquivalentClassesAxiom axiom) { List classExpressions = axiom.getClassExpressionsAsList(); - List renamedClassExpressions = new ArrayList(); + List renamedClassExpressions = new ArrayList<>(); for (OWLClassExpression expr : classExpressions) { renamedClassExpressions.add(expressionRenamer.rename(expr)); } - renamedAxiom = df.getOWLEquivalentClassesAxiom(new TreeSet(renamedClassExpressions)); + renamedAxiom = df.getOWLEquivalentClassesAxiom(new TreeSet<>(renamedClassExpressions)); } @Override @@ -341,7 +302,7 @@ public void visit(OWLInverseFunctionalObjectPropertyAxiom axiom) { @Override public void visit(OWLSameIndividualAxiom axiom) { - Set renamedIndividuals = new HashSet(); + Set renamedIndividuals = new HashSet<>(); if(normalizeABoxAxioms){ renamedIndividuals.add(df.getOWLNamedIndividual(IRI.create("http://dl-learner.org/pattern/a"))); renamedIndividuals.add(df.getOWLNamedIndividual(IRI.create("http://dl-learner.org/pattern/b"))); @@ -355,7 +316,7 @@ public void visit(OWLSameIndividualAxiom axiom) { @Override public void visit(OWLSubPropertyChainOfAxiom axiom) { - List renamedSubPropertyChain = new ArrayList(); + List renamedSubPropertyChain = new ArrayList<>(); for (OWLObjectPropertyExpression owlObjectPropertyExpression : axiom.getPropertyChain()) { renamedSubPropertyChain.add(expressionRenamer.rename(owlObjectPropertyExpression)); } @@ -377,9 +338,9 @@ public void visit(OWLInverseObjectPropertiesAxiom axiom) { public void visit(OWLHasKeyAxiom axiom) { OWLClassExpression classExpression = axiom.getClassExpression(); classExpression = expressionRenamer.rename(classExpression); - Set> propertyExpressions = axiom.getPropertyExpressions(); - Set> renamedPropertyExpressions = new HashSet>(); - for (OWLPropertyExpression owlPropertyExpression : propertyExpressions) { + Set propertyExpressions = axiom.getPropertyExpressions(); + Set renamedPropertyExpressions = new HashSet<>(); + for (OWLPropertyExpression owlPropertyExpression : propertyExpressions) { renamedPropertyExpressions.add(expressionRenamer.rename(owlPropertyExpression)); } renamedAxiom = df.getOWLHasKeyAxiom(classExpression, renamedPropertyExpressions); diff --git a/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLClassExpressionOrderingComparator.java b/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLClassExpressionOrderingComparator.java index b9295067aa..72e49fe05e 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLClassExpressionOrderingComparator.java +++ b/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLClassExpressionOrderingComparator.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.pattern; import java.util.Arrays; diff --git a/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLClassExpressionRenamer.java b/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLClassExpressionRenamer.java index 0c979c570b..699d878d89 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLClassExpressionRenamer.java +++ b/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLClassExpressionRenamer.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.pattern; import java.util.ArrayList; @@ -56,6 +74,8 @@ import org.semanticweb.owlapi.model.OWLPropertyExpression; import org.semanticweb.owlapi.model.OWLPropertyExpressionVisitor; +import javax.annotation.Nonnull; + public class OWLClassExpressionRenamer implements OWLClassExpressionVisitor, OWLPropertyExpressionVisitor, OWLIndividualVisitor, OWLDataRangeVisitor { private static final String NS = "http://dl-learner.org/pattern/"; @@ -65,9 +85,9 @@ public class OWLClassExpressionRenamer implements OWLClassExpressionVisitor, OWL private OWLObject renamedOWLObject; // private OWLClassExpressionOrderingComparator comparator = new OWLClassExpressionOrderingComparator(); private OWLObjectComparator comparator = new OWLObjectComparator(); - private Queue classVarQueue = new LinkedList(); - private Queue propertyVarQueue = new LinkedList(); - private Queue individualVarQueue = new LinkedList(); + private Queue classVarQueue = new LinkedList<>(); + private Queue propertyVarQueue = new LinkedList<>(); + private Queue individualVarQueue = new LinkedList<>(); private OWLLiteralRenamer literalRenamer; @@ -131,7 +151,7 @@ public OWLLiteral rename(OWLLiteral lit){ public void visit(OWLObjectIntersectionOf desc) { List operands = desc.getOperandsAsList(); Collections.sort(operands, comparator); - SortedSet renamedOperands = new TreeSet(comparator); + SortedSet renamedOperands = new TreeSet<>(comparator); for(OWLClassExpression expr : operands){ renamedOperands.add(rename(expr)); } @@ -142,7 +162,7 @@ public void visit(OWLObjectIntersectionOf desc) { public void visit(OWLObjectUnionOf desc) { List operands = desc.getOperandsAsList(); Collections.sort(operands, comparator); - SortedSet renamedOperands = new TreeSet(comparator); + SortedSet renamedOperands = new TreeSet<>(comparator); for(OWLClassExpression expr : operands){ renamedOperands.add(rename(expr)); } @@ -153,7 +173,7 @@ public void visit(OWLObjectUnionOf desc) { public void visit(OWLDataHasValue desc) { OWLDataPropertyExpression property = desc.getProperty(); property = rename(property); - OWLLiteral value = desc.getValue(); + OWLLiteral value = desc.getFiller(); value = rename(value); renamedOWLObject = df.getOWLDataHasValue(property, value); } @@ -187,7 +207,7 @@ public void visit(OWLObjectAllValuesFrom desc) { public void visit(OWLObjectHasValue desc) { OWLObjectPropertyExpression property = desc.getProperty(); property = rename(property); - OWLIndividual value = desc.getValue(); + OWLIndividual value = desc.getFiller(); value = rename(value); renamedOWLObject = df.getOWLObjectHasValue(property, value); } @@ -241,7 +261,7 @@ public void visit(OWLObjectHasSelf desc) { @Override public void visit(OWLObjectOneOf desc) { Set individuals = desc.getIndividuals(); - Set renamedIndividuals = new TreeSet(); + Set renamedIndividuals = new TreeSet<>(); for (OWLIndividual ind : individuals) { renamedIndividuals.add(rename(ind)); } @@ -322,7 +342,7 @@ public void visit(OWLClass desc) { newEntity = df.getOWLClass(getIRI(classVarQueue.poll())); renaming.put(desc, newEntity); } - renamedOWLObject = (OWLClass)newEntity; + renamedOWLObject = newEntity; } } @@ -336,7 +356,7 @@ public void visit(OWLObjectProperty op) { newEntity = df.getOWLObjectProperty(getIRI(propertyVarQueue.poll())); renaming.put(op, newEntity); } - renamedOWLObject = (OWLObjectProperty)newEntity; + renamedOWLObject = newEntity; } } @@ -350,10 +370,15 @@ public void visit(OWLDataProperty dp) { newEntity = df.getOWLDataProperty(getIRI(propertyVarQueue.poll())); renaming.put(dp, newEntity); } - renamedOWLObject = (OWLDataProperty)newEntity; + renamedOWLObject = newEntity; } } + @Override + public void visit(@Nonnull OWLAnnotationProperty property) { + + } + @Override public void visit(OWLNamedIndividual ind) { OWLEntity newEntity = renaming.get(ind); @@ -361,7 +386,7 @@ public void visit(OWLNamedIndividual ind) { newEntity = df.getOWLNamedIndividual(getIRI(individualVarQueue.poll())); renaming.put(ind, newEntity); } - renamedOWLObject = (OWLNamedIndividual)newEntity; + renamedOWLObject = newEntity; } private IRI getIRI(String var){ @@ -375,7 +400,7 @@ public void visit(OWLAnonymousIndividual ind) { newEntity = df.getOWLNamedIndividual(getIRI(individualVarQueue.poll())); // renaming.put(ind, newEntity); } - renamedOWLObject = (OWLNamedIndividual)newEntity; + renamedOWLObject = newEntity; } @Override @@ -386,7 +411,7 @@ public void visit(OWLDatatype dt) { @Override public void visit(OWLDataOneOf desc) { Set literals = desc.getValues(); - Set renamedLiterals = new TreeSet(); + Set renamedLiterals = new TreeSet<>(); for (OWLLiteral lit : literals) { renamedLiterals.add(rename(lit)); } @@ -402,9 +427,9 @@ public void visit(OWLDataComplementOf desc) { @Override public void visit(OWLDataIntersectionOf desc) { - List operands = new ArrayList(desc.getOperands()); + List operands = new ArrayList<>(desc.getOperands()); Collections.sort(operands, comparator); - SortedSet renamedOperands = new TreeSet(comparator); + SortedSet renamedOperands = new TreeSet<>(comparator); for(OWLDataRange expr : operands){ renamedOperands.add(rename(expr)); } @@ -413,9 +438,9 @@ public void visit(OWLDataIntersectionOf desc) { @Override public void visit(OWLDataUnionOf desc) { - List operands = new ArrayList(desc.getOperands()); + List operands = new ArrayList<>(desc.getOperands()); Collections.sort(operands, comparator); - SortedSet renamedOperands = new TreeSet(comparator); + SortedSet renamedOperands = new TreeSet<>(comparator); for(OWLDataRange expr : operands){ renamedOperands.add(rename(expr)); } diff --git a/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLLiteralRenamer.java b/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLLiteralRenamer.java index ff2140fd58..66f907721d 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLLiteralRenamer.java +++ b/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLLiteralRenamer.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.pattern; import org.semanticweb.owlapi.model.OWLDataFactory; diff --git a/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLObjectComparator.java b/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLObjectComparator.java index d384c94fbe..3a32422c2d 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLObjectComparator.java +++ b/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLObjectComparator.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.pattern; import java.util.Comparator; diff --git a/components-core/src/main/java/org/dllearner/algorithms/pattern/PatternBasedAxiomLearningAlgorithm.java b/components-core/src/main/java/org/dllearner/algorithms/pattern/PatternBasedAxiomLearningAlgorithm.java index e03cfca17a..1b1e04db28 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/pattern/PatternBasedAxiomLearningAlgorithm.java +++ b/components-core/src/main/java/org/dllearner/algorithms/pattern/PatternBasedAxiomLearningAlgorithm.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.pattern; @@ -136,7 +151,7 @@ protected void learnAxioms() { } private Set applyPattern(OWLAxiom pattern, OWLClass cls, Model fragment) { - Map axioms2Score = new HashMap(); + Map axioms2Score = new HashMap<>(); OWLClassExpression patternSubClass = null; OWLClassExpression patternSuperClass = null; @@ -165,13 +180,13 @@ private Set applyPattern(OWLAxiom pattern, OWLClass cls, Model fragmen Map variablesMapping = converter.getVariablesMapping(); com.hp.hpl.jena.query.ResultSet rs = QueryExecutionFactory.create(query, fragment).execSelect(); QuerySolution qs; - Set resources = new HashSet(); + Set resources = new HashSet<>(); Multiset instantiations = HashMultiset.create(); while (rs.hasNext()) { qs = rs.next(); resources.add(qs.getResource("x").getURI()); // get the IRIs for each variable - Map entity2IRIMap = new HashMap(); + Map entity2IRIMap = new HashMap<>(); entity2IRIMap.put(patternSubClass.asOWLClass(), cls.getIRI()); boolean skip = false; for (OWLEntity entity : signature) { @@ -212,7 +227,7 @@ private Set applyPattern(OWLAxiom pattern, OWLClass cls, Model fragmen } private Set asAnnotatedAxioms(Map axioms2Score){ - Set annotatedAxioms = new HashSet(); + Set annotatedAxioms = new HashSet<>(); for (Entry entry : axioms2Score.entrySet()) { OWLAxiom axiom = entry.getKey(); Score score = entry.getValue(); @@ -227,7 +242,8 @@ private Set asAnnotatedAxioms(Map axioms2Score){ public static void main(String[] args) throws Exception { OWLDataFactoryImpl df = new OWLDataFactoryImpl(); - PrefixManager pm = new DefaultPrefixManager("http://dllearner.org/pattern#"); + PrefixManager pm = new DefaultPrefixManager(); + pm.setDefaultPrefix("http://dllearner.org/pattern#"); Model model = ModelFactory.createDefaultModel(); String triples = diff --git a/components-core/src/main/java/org/dllearner/algorithms/pattern/TimeBasedFragmentExtractor.java b/components-core/src/main/java/org/dllearner/algorithms/pattern/TimeBasedFragmentExtractor.java index f6d424ac9f..ddfc4f7a0c 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/pattern/TimeBasedFragmentExtractor.java +++ b/components-core/src/main/java/org/dllearner/algorithms/pattern/TimeBasedFragmentExtractor.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.pattern; diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/AsymmetricObjectPropertyAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/AsymmetricObjectPropertyAxiomLearner.java index bc42e79975..c85ebe3432 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/AsymmetricObjectPropertyAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/AsymmetricObjectPropertyAxiomLearner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.properties; import java.net.URL; diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/AxiomAlgorithms.java b/components-core/src/main/java/org/dllearner/algorithms/properties/AxiomAlgorithms.java index ce1e241d1b..02cedaf066 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/AxiomAlgorithms.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/AxiomAlgorithms.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.properties; @@ -93,8 +108,8 @@ public class AxiomAlgorithms { /** set of tbox and rbox axiom types */ public static final Set> TBoxAndRBoxAxiomTypes = tboxAndRbox(); - private static final Set> tboxAndRbox() { - Set> axioms = new HashSet>(TBoxAxiomTypes); + private static Set> tboxAndRbox() { + Set> axioms = new HashSet<>(TBoxAxiomTypes); axioms.addAll(RBoxAxiomTypes); return axioms; } @@ -176,8 +191,8 @@ public String toString() { new ParameterizedSparqlString("CONSTRUCT{?s a ?entity . ?s a ?cls1 .} WHERE {?s a ?entity . OPTIONAL {?s a ?cls1 .}")); } - private static final Map>> entityType2AxiomTypes = - new HashMap>>(); + private static final Map>> entityType2AxiomTypes = + new HashMap<>(); static { // class axiom types @@ -198,8 +213,8 @@ public String toString() { FUNCTIONAL_DATA_PROPERTY, DATA_PROPERTY_DOMAIN, DATA_PROPERTY_RANGE)); } - private static final Map> sameSampleCluster = - new HashMap>(); + private static final Map> sameSampleCluster = + new HashMap<>(); static { // object properties diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyAxiomLearner.java index d6466492a7..86639052e9 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyAxiomLearner.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.properties; diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyDomainAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyDomainAxiomLearner.java index 4ce43d70a1..30f4a068fd 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyDomainAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyDomainAxiomLearner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.properties; import java.util.Set; @@ -144,8 +143,8 @@ protected void run(){ AxiomScore score = computeScore(popularity, cntB, cntAB); currentlyBestAxioms.add( - new EvaluatedAxiom( - df.getOWLDataPropertyDomainAxiom(entityToDescribe, candidate), + new EvaluatedAxiom<>( + df.getOWLDataPropertyDomainAxiom(entityToDescribe, candidate), score)); } } @@ -210,8 +209,8 @@ private void runBatched(){ double score = Heuristics.getFScore(recall, precision, beta); currentlyBestAxioms.add( - new EvaluatedAxiom( - df.getOWLDataPropertyDomainAxiom(entityToDescribe, candidate), + new EvaluatedAxiom<>( + df.getOWLDataPropertyDomainAxiom(entityToDescribe, candidate), new AxiomScore(score, useSampling))); } diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyHierarchyAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyHierarchyAxiomLearner.java index b84a1251e8..bd3a42eb6a 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyHierarchyAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyHierarchyAxiomLearner.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.properties; @@ -121,8 +136,8 @@ protected void run() { int nrOfNegExamples = popularity - nrOfPosExamples; currentlyBestAxioms.add( - new EvaluatedAxiom( - getAxiom(entityToDescribe, p), + new EvaluatedAxiom<>( + getAxiom(entityToDescribe, p), new AxiomScore(score, score, nrOfPosExamples, nrOfNegExamples, useSampling))); } } @@ -168,8 +183,8 @@ protected void runBatched() { double score = computeScore(candidatePopularity, popularity, overlap); currentlyBestAxioms.add( - new EvaluatedAxiom( - getAxiom(entityToDescribe, candidate), + new EvaluatedAxiom<>( + getAxiom(entityToDescribe, candidate), new AxiomScore(score))); } } @@ -186,16 +201,24 @@ public double computeScore(int candidatePopularity, int popularity, int overlap) return score; } - + + /** + * Returns the hierarchy axiom based on the axiom type of the current learning algorithm between both + * properties. The relationship between both properties can be (1) subsumption, (2) equivalence or (3) + * disjointness. + * @param property the first property + * @param otherProperty the second property + * @return the hierarchy axiom + */ public abstract T getAxiom(OWLDataProperty property, OWLDataProperty otherProperty); /** * Returns the candidate properties for comparison. - * @return + * @return the candidate properties */ protected SortedSet getCandidates(){ // get the candidates - SortedSet candidates = new TreeSet(); + SortedSet candidates = new TreeSet<>(); if (strictMode) { // that have the same domain and range // get rdfs:domain of the property @@ -242,7 +265,7 @@ public Set getPositiveExamples(EvaluatedAxiom } posExamplesQueryTemplate.setIri("p_other", otherProperty.toStringID()); - Set posExamples = new TreeSet(); + Set posExamples = new TreeSet<>(); ResultSet rs = executeSelectQuery(posExamplesQueryTemplate.toString()); @@ -270,7 +293,7 @@ public Set getNegativeExamples(EvaluatedAxiom } negExamplesQueryTemplate.setIri("p_other", otherProperty.toStringID()); - Set negExamples = new TreeSet(); + Set negExamples = new TreeSet<>(); ResultSet rs = executeSelectQuery(negExamplesQueryTemplate.toString()); diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyRangeAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyRangeAxiomLearner.java index 14960c8926..15edc8aca6 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyRangeAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyRangeAxiomLearner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.properties; import java.util.Set; @@ -106,7 +105,7 @@ protected void run() { int nrOfNegExamples = popularity - nrOfPosExamples; - currentlyBestAxioms.add(new EvaluatedAxiom( + currentlyBestAxioms.add(new EvaluatedAxiom<>( df.getOWLDataPropertyRangeAxiom(entityToDescribe, df.getOWLDatatype(IRI.create(datatypeURI))), new AxiomScore(score, score, nrOfPosExamples, nrOfNegExamples, useSampling))); diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointDataPropertyAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointDataPropertyAxiomLearner.java index ae1423bc42..fff98f5b0a 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointDataPropertyAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointDataPropertyAxiomLearner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.properties; import java.util.SortedSet; diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointObjectPropertyAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointObjectPropertyAxiomLearner.java index 381a67945c..ca4d84ed83 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointObjectPropertyAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointObjectPropertyAxiomLearner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.properties; import java.util.SortedSet; @@ -70,5 +69,5 @@ public OWLDisjointObjectPropertiesAxiom getAxiom(OWLObjectProperty property, OWL public double computeScore(int candidatePopularity, int popularity, int overlap) { return 1 - super.computeScore(candidatePopularity, popularity, overlap); - }; + } } diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentDataPropertyAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentDataPropertyAxiomLearner.java index a3df3613ed..fb7baefb7d 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentDataPropertyAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentDataPropertyAxiomLearner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.properties; import java.util.SortedSet; diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentObjectPropertyAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentObjectPropertyAxiomLearner.java index 10f134b930..9ddaae8223 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentObjectPropertyAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentObjectPropertyAxiomLearner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.properties; import java.util.SortedSet; diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalDataPropertyAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalDataPropertyAxiomLearner.java index d6e86ce596..21927873e9 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalDataPropertyAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalDataPropertyAxiomLearner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.properties; import org.dllearner.core.ComponentAnn; @@ -82,8 +81,8 @@ protected void run() { int frequency = getCountValue(POS_FREQUENCY_QUERY.toString()); - currentlyBestAxioms.add(new EvaluatedAxiom( - df.getOWLFunctionalDataPropertyAxiom(entityToDescribe), + currentlyBestAxioms.add(new EvaluatedAxiom<>( + df.getOWLFunctionalDataPropertyAxiom(entityToDescribe), computeScore(popularity, frequency, useSampling), declared)); } diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalObjectPropertyAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalObjectPropertyAxiomLearner.java index 672266ec9c..94d32e31d1 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalObjectPropertyAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalObjectPropertyAxiomLearner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.properties; import java.util.List; @@ -78,7 +77,7 @@ public Set getNegativeExamples( OWLFunctionalObjectPropertyAxiom axiom = evaluatedAxiom.getAxiom(); negExamplesQueryTemplate.setIri("p", axiom.getProperty().asOWLObjectProperty().toStringID()); - Set negExamples = new TreeSet(); + Set negExamples = new TreeSet<>(); ResultSet rs = executeSelectQuery(negExamplesQueryTemplate.toString()); diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/InverseFunctionalObjectPropertyAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/InverseFunctionalObjectPropertyAxiomLearner.java index 4f2818fec2..e2b9905014 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/InverseFunctionalObjectPropertyAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/InverseFunctionalObjectPropertyAxiomLearner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.properties; import java.util.List; @@ -77,7 +76,7 @@ public Set getNegativeExamples( OWLInverseFunctionalObjectPropertyAxiom axiom = evaluatedAxiom.getAxiom(); negExamplesQueryTemplate.setIri("p", axiom.getProperty().asOWLObjectProperty().toStringID()); - Set negExamples = new TreeSet(); + Set negExamples = new TreeSet<>(); ResultSet rs = executeSelectQuery(negExamplesQueryTemplate.toString()); diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/InverseObjectPropertyAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/InverseObjectPropertyAxiomLearner.java index 2c427c02c6..b4754cf26a 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/InverseObjectPropertyAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/InverseObjectPropertyAxiomLearner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.properties; import java.util.SortedSet; @@ -111,9 +110,9 @@ protected void run() { // score AxiomScore score = computeScore(popularity, frequency, useSampling); - currentlyBestAxioms.add(new EvaluatedAxiom(df + currentlyBestAxioms.add(new EvaluatedAxiom<>(df .getOWLInverseObjectPropertiesAxiom(entityToDescribe, candidate), score - )); + )); } } diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/IrreflexiveObjectPropertyAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/IrreflexiveObjectPropertyAxiomLearner.java index 995317eb09..10bd82b75c 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/IrreflexiveObjectPropertyAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/IrreflexiveObjectPropertyAxiomLearner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.properties; import org.dllearner.core.ComponentAnn; diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/MultiPropertyAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/MultiPropertyAxiomLearner.java index 05298af316..0a2a132d77 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/MultiPropertyAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/MultiPropertyAxiomLearner.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.properties; @@ -188,7 +203,7 @@ public Map, List>> getCur } public List> getCurrentlyBestEvaluatedAxioms(AxiomType axiomType) { - return new ArrayList>(results.get(axiomType)); + return new ArrayList<>(results.get(axiomType)); } public List> getCurrentlyBestEvaluatedAxioms(AxiomType axiomType, double accuracyThreshold) { @@ -201,7 +216,7 @@ public List> getCurrentlyBestEvaluatedAxioms(AxiomType< } // get all axioms above threshold - List> bestAxioms = new ArrayList>(); + List> bestAxioms = new ArrayList<>(); for (EvaluatedAxiom axiom : result) { if(axiom.getScore().getAccuracy() >= accuracyThreshold){ bestAxioms.add(axiom); @@ -299,14 +314,16 @@ public void setMultiThreaded(boolean multiThreaded) { } /** - * @param maxNrOfThreads the maxNrOfThreads to set + * @param maxNrOfThreads the max. nr of threads */ public void setMaxNrOfThreads(int maxNrOfThreads) { this.maxNrOfThreads = maxNrOfThreads; } /** - * @param maxExecutionTimeMilliseconds the maxExecutionTimeMilliseconds to set + * Set the maximum execution time. + * @param executionTimeDuration the execution time + * @param executionTimeUnit the time unit */ public void setMaxExecutionTime(long executionTimeDuration, TimeUnit executionTimeUnit) { this.maxExecutionTimeMilliseconds = executionTimeUnit.toMillis(executionTimeDuration); diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyAxiomLearner.java index fbb4d95cfa..7fda0a54d2 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyAxiomLearner.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.properties; diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyCharacteristicsAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyCharacteristicsAxiomLearner.java index 942a4075fb..991e2a2588 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyCharacteristicsAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyCharacteristicsAxiomLearner.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.properties; @@ -96,8 +111,8 @@ protected void run() { int frequency = getPositiveExamplesFrequency(); - currentlyBestAxioms.add(new EvaluatedAxiom( - getAxiom(entityToDescribe), + currentlyBestAxioms.add(new EvaluatedAxiom<>( + getAxiom(entityToDescribe), computeScore(popularity, frequency, useSampling), declared)); } @@ -117,7 +132,7 @@ public Set getPositiveExamples(EvaluatedAxiom posExamples = new TreeSet(); + Set posExamples = new TreeSet<>(); ResultSet rs = executeSelectQuery(posExamplesQueryTemplate.toString()); @@ -138,7 +153,7 @@ public Set getNegativeExamples(EvaluatedAxiom negExamples = new TreeSet(); + Set negExamples = new TreeSet<>(); ResultSet rs = executeSelectQuery(negExamplesQueryTemplate.toString()); diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyDomainAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyDomainAxiomLearner.java index 0b5413b843..e0d20b48cf 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyDomainAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyDomainAxiomLearner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.properties; import java.util.Set; @@ -167,8 +166,8 @@ protected void run(){ AxiomScore score = computeScore(popularity, cntB, cntAB); currentlyBestAxioms.add( - new EvaluatedAxiom( - df.getOWLObjectPropertyDomainAxiom(entityToDescribe, candidate), + new EvaluatedAxiom<>( + df.getOWLObjectPropertyDomainAxiom(entityToDescribe, candidate), score)); mon.stop(); @@ -235,8 +234,8 @@ private void runBatched(){ double score = Heuristics.getFScore(recall, precision, beta); currentlyBestAxioms.add( - new EvaluatedAxiom( - df.getOWLObjectPropertyDomainAxiom(entityToDescribe, candidate), + new EvaluatedAxiom<>( + df.getOWLObjectPropertyDomainAxiom(entityToDescribe, candidate), new AxiomScore(score, useSampling))); } diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyHierarchyAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyHierarchyAxiomLearner.java index 094b522336..72f80cf3eb 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyHierarchyAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyHierarchyAxiomLearner.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.properties; @@ -117,8 +132,8 @@ protected void run() { int nrOfNegExamples = popularity - nrOfPosExamples; currentlyBestAxioms.add( - new EvaluatedAxiom( - getAxiom(entityToDescribe, p), + new EvaluatedAxiom<>( + getAxiom(entityToDescribe, p), new AxiomScore(score, score, nrOfPosExamples, nrOfNegExamples, useSampling))); } } @@ -168,8 +183,8 @@ protected void runBatched() { int nrOfNegExamples = popularity - nrOfPosExamples; currentlyBestAxioms.add( - new EvaluatedAxiom( - getAxiom(entityToDescribe, candidate), + new EvaluatedAxiom<>( + getAxiom(entityToDescribe, candidate), new AxiomScore(score, score, nrOfPosExamples, nrOfNegExamples, useSampling))); } } @@ -191,11 +206,11 @@ public double computeScore(int candidatePopularity, int popularity, int overlap) /** * Returns the candidate properties for comparison. - * @return + * @return the candidate properties */ protected SortedSet getCandidates(){ // get the candidates - SortedSet candidates = new TreeSet(); + SortedSet candidates = new TreeSet<>(); if (strictMode) { // that have the same domain and range // get rdfs:domain of the property @@ -242,7 +257,7 @@ public Set getPositiveExamples(EvaluatedAxiom posExamples = new TreeSet(); + Set posExamples = new TreeSet<>(); ResultSet rs = executeSelectQuery(posExamplesQueryTemplate.toString()); @@ -270,7 +285,7 @@ public Set getNegativeExamples(EvaluatedAxiom negExamples = new TreeSet(); + Set negExamples = new TreeSet<>(); ResultSet rs = executeSelectQuery(negExamplesQueryTemplate.toString()); diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyRangeAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyRangeAxiomLearner.java index 49ddbef78c..92d27256c4 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyRangeAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyRangeAxiomLearner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.properties; import java.util.Set; @@ -155,7 +154,7 @@ protected void run(){ AxiomScore score = computeScore(popularity, cntB, cntAB); currentlyBestAxioms.add( - new EvaluatedAxiom( + new EvaluatedAxiom<>( df.getOWLObjectPropertyRangeAxiom(entityToDescribe, candidate), score)); } @@ -222,8 +221,8 @@ private void runBatched(){ double score = Heuristics.getFScore(recall, precision, beta); currentlyBestAxioms.add( - new EvaluatedAxiom( - df.getOWLObjectPropertyRangeAxiom(entityToDescribe, candidate), + new EvaluatedAxiom<>( + df.getOWLObjectPropertyRangeAxiom(entityToDescribe, candidate), new AxiomScore(score, useSampling))); } diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyAxiomLearner.java index 384daa592b..45617460c3 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyAxiomLearner.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.properties; @@ -94,9 +109,9 @@ protected int getCountValue(String query){ /** * Return the integer value of a SPARQL query that just returns a single COUNT value. * It is assumed the the variable of the COUNT value is ?cnt. - * @param query - * @param model - * @return + * @param query the SPARQL query + * @param model the model containing the data + * @return the count value */ protected int getCountValue(String query, Model model){ ResultSet rs = executeSelectQuery(query, model); diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/ReflexiveObjectPropertyAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/ReflexiveObjectPropertyAxiomLearner.java index 922386dd59..7bbdbdcffe 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/ReflexiveObjectPropertyAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/ReflexiveObjectPropertyAxiomLearner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.properties; import org.dllearner.core.ComponentAnn; diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/SubDataPropertyOfAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/SubDataPropertyOfAxiomLearner.java index e39793c598..13997d123d 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/SubDataPropertyOfAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/SubDataPropertyOfAxiomLearner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.properties; import java.util.SortedSet; diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/SubObjectPropertyOfAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/SubObjectPropertyOfAxiomLearner.java index 91c7d7bdf2..fa9fb164bb 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/SubObjectPropertyOfAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/SubObjectPropertyOfAxiomLearner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.properties; import java.util.SortedSet; diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/SymmetricObjectPropertyAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/SymmetricObjectPropertyAxiomLearner.java index a0b38620b3..21a6663de2 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/SymmetricObjectPropertyAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/SymmetricObjectPropertyAxiomLearner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.properties; import org.dllearner.core.ComponentAnn; diff --git a/components-core/src/main/java/org/dllearner/algorithms/properties/TransitiveObjectPropertyAxiomLearner.java b/components-core/src/main/java/org/dllearner/algorithms/properties/TransitiveObjectPropertyAxiomLearner.java index 0e3a8db148..757f370f9f 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/properties/TransitiveObjectPropertyAxiomLearner.java +++ b/components-core/src/main/java/org/dllearner/algorithms/properties/TransitiveObjectPropertyAxiomLearner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.properties; import java.util.List; @@ -89,7 +88,7 @@ public Set getNegativeExamples( OWLTransitiveObjectPropertyAxiom axiom = evaluatedAxiom.getAxiom(); negExamplesQueryTemplate.setIri("p", axiom.getProperty().asOWLObjectProperty().toStringID()); - Set negExamples = new TreeSet(); + Set negExamples = new TreeSet<>(); ResultSet rs = executeSelectQuery(negExamplesQueryTemplate.toString()); diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/QTL2Disjunctive.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/QTL2Disjunctive.java index a4c10811f2..8ae5889072 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/QTL2Disjunctive.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/QTL2Disjunctive.java @@ -1,5 +1,25 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl; +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; import gnu.trove.map.TObjectIntMap; import gnu.trove.map.hash.TObjectIntHashMap; @@ -20,9 +40,9 @@ import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; +import java.util.concurrent.TimeUnit; import org.aksw.jena_sparql_api.core.QueryExecutionFactory; -import org.apache.log4j.Logger; import org.dllearner.algorithms.qtl.datastructures.impl.EvaluatedRDFResourceTree; import org.dllearner.algorithms.qtl.datastructures.impl.QueryTreeImpl.LiteralNodeConversionStrategy; import org.dllearner.algorithms.qtl.datastructures.impl.QueryTreeImpl.LiteralNodeSubsumptionStrategy; @@ -40,8 +60,9 @@ import org.dllearner.core.ComponentInitException; import org.dllearner.core.EvaluatedDescription; import org.dllearner.core.KnowledgeSource; -import org.dllearner.core.LearningProblemUnsupportedException; import org.dllearner.core.Score; +import org.dllearner.core.StringRenderer; +import org.dllearner.core.StringRenderer.Rendering; import org.dllearner.core.config.ConfigOption; import org.dllearner.kb.OWLAPIOntology; import org.dllearner.kb.OWLFile; @@ -51,14 +72,13 @@ import org.dllearner.learningproblems.Heuristics; import org.dllearner.learningproblems.PosNegLP; import org.dllearner.learningproblems.QueryTreeScore; -import org.semanticweb.owlapi.io.ToStringRenderer; import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLIndividual; import org.semanticweb.owlapi.util.SimpleShortFormProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.ManchesterOWLSyntaxOWLObjectRendererImpl; - import com.google.common.collect.Sets; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; @@ -67,8 +87,8 @@ @ComponentAnn(name="query tree learner with noise (disjunctive)", shortName="qtl2dis", version=0.8) public class QTL2Disjunctive extends AbstractCELA implements Cloneable{ - private static final Logger logger = Logger.getLogger(QTL2Disjunctive.class); - private final DecimalFormat dFormat = new DecimalFormat("0.00"); + private static final Logger logger = LoggerFactory.getLogger(QTL2Disjunctive.class); + private final DecimalFormat dFormat = new DecimalFormat("0.00"); private SparqlEndpointKS ks; @@ -83,14 +103,13 @@ public class QTL2Disjunctive extends AbstractCELA implements Cloneable{ private double bestCurrentScore = 0d; - private List currentPosExampleTrees = new ArrayList(); - private List currentNegExampleTrees = new ArrayList(); - private Set currentPosExamples = new HashSet(); - private Set currentNegExamples = new HashSet(); - - private Map tree2Individual = new HashMap(); - private Map individual2Tree = new HashMap(); + private List currentPosExampleTrees = new ArrayList<>(); + private List currentNegExampleTrees = new ArrayList<>(); + private Set currentPosExamples = new HashSet<>(); + private Set currentNegExamples = new HashSet<>(); + private BiMap tree2Individual = HashBiMap.create(); + private PosNegLP lp; private Model model; @@ -107,8 +126,6 @@ public class QTL2Disjunctive extends AbstractCELA implements Cloneable{ //Parameters @ConfigOption(name = "noisePercentage", defaultValue="0.0", description="the (approximated) percentage of noise within the examples") private double noisePercentage = 0.0; - @ConfigOption(defaultValue = "10", name = "maxExecutionTimeInSeconds", description = "maximum execution of the algorithm in seconds") - private int maxExecutionTimeInSeconds = 60; private double coverageWeight = 0.8; private double specifityWeight = 0.1; @@ -135,12 +152,14 @@ public class QTL2Disjunctive extends AbstractCELA implements Cloneable{ // the (approximated) value of noise within the examples private double noise = 0.0; - private long startTime; private long partialSolutionStartTime; private double startPosExamplesSize; private int expressionTests = 0; + // the time needed until the best solution was found + private long timeBestSolutionFound = -1; + LiteralNodeConversionStrategy[] strategies = new LiteralNodeConversionStrategy[]{ LiteralNodeConversionStrategy.MIN, LiteralNodeConversionStrategy.MAX, @@ -157,7 +176,7 @@ public class QTL2Disjunctive extends AbstractCELA implements Cloneable{ public QTL2Disjunctive() {} - public QTL2Disjunctive(PosNegLP learningProblem, AbstractReasonerComponent reasoner) throws LearningProblemUnsupportedException{ + public QTL2Disjunctive(PosNegLP learningProblem, AbstractReasonerComponent reasoner) { super(learningProblem, reasoner); loadModel(); } @@ -179,7 +198,7 @@ public QTL2Disjunctive(PosNegLP lp, SparqlEndpointKS ks) { /** * Copy constructor. - * @param qtl + * @param qtl the QTL2Disjunctive instance */ public QTL2Disjunctive(QTL2Disjunctive qtl) { super(qtl.getLearningProblem(), qtl.getReasoner()); @@ -232,8 +251,8 @@ public void init() throws ComponentInitException { startPosExamplesSize = currentPosExampleTrees.size(); //console rendering of class expressions - ToStringRenderer.getInstance().setRenderer(new ManchesterOWLSyntaxOWLObjectRendererImpl()); - ToStringRenderer.getInstance().setShortFormProvider(new SimpleShortFormProvider()); + StringRenderer.setRenderer(Rendering.MANCHESTER_SYNTAX); + StringRenderer.setShortFormProvider(new SimpleShortFormProvider()); //compute the LGG for all examples //this allows us to prune all other trees because we can omit paths in trees which are contained in all positive @@ -267,7 +286,7 @@ private void generateQueryTrees(){ tree2Individual.put(queryTree, ind); currentPosExampleTrees.add(queryTree); currentPosExamples.add(ind); - logger.debug(ind); + logger.debug(ind.toStringID()); logger.debug(queryTree.getStringRepresentation()); } catch (Exception e) { logger.error("Failed to generate tree for resource " + ind, e); @@ -285,7 +304,7 @@ private void generateQueryTrees(){ tree2Individual.put(queryTree, ind); currentNegExampleTrees.add(queryTree); currentNegExamples.add(ind); - logger.debug(ind); + logger.debug(ind.toStringID()); logger.debug(queryTree.getStringRepresentation()); } catch (Exception e) { logger.error("Failed to generate tree for resource " + ind, e); @@ -303,7 +322,6 @@ private void generateQueryTrees(){ public void start() { printSetup(); logger.info("Running..."); - startTime = System.currentTimeMillis(); reset(); @@ -352,18 +370,18 @@ public void start() { logger.info(message); } - }; - + } + isRunning = false; postProcess(); - long endTime = System.currentTimeMillis(); - logger.info("Finished in " + (endTime-startTime) + "ms."); + long nanoEndTime = System.nanoTime(); + logger.info("Finished in " + TimeUnit.NANOSECONDS.toMillis(nanoEndTime - nanoStartTime) + "ms."); logger.info(expressionTests +" descriptions tested"); if(currentBestSolution != null) { logger.info("Combined solution:" + currentBestSolution.getDescription().toString().replace("\n", "")); - logger.info(currentBestSolution.getScore()); + logger.info(currentBestSolution.getScore().toString()); } else { logger.info("Could not find a solution in the given time."); } @@ -374,7 +392,7 @@ public void start() { */ private void postProcess() { logger.trace("Post processing ..."); - // pick solutions with same accuracy, i.e. in the pos only case + // pick solutions with same accuracy, i.e. in the pos only case // covering the same number of positive examples SortedSet solutions = getSolutions(); // pick solutions with accuracy above @@ -398,7 +416,7 @@ private void postProcess() { /** * Compute a (partial) solution that covers as much positive examples as possible. - * @return + * @return a (partial) solution */ private EvaluatedRDFResourceTree computeBestPartialSolution(){ logger.info("Computing best partial solution..."); @@ -411,12 +429,12 @@ private EvaluatedRDFResourceTree computeBestPartialSolution(){ RDFResourceTree currentTree; // generate id for each pos and neg example tree - TObjectIntMap index = new TObjectIntHashMap(this.currentPosExampleTrees.size() + this.currentNegExampleTrees.size()); + TObjectIntMap index = new TObjectIntHashMap<>(this.currentPosExampleTrees.size() + this.currentNegExampleTrees.size()); int id = 1; for (RDFResourceTree posTree : currentPosExampleTrees) { index.put(posTree, id++); } - Set> processedCombinations = new HashSet<>(); + Set> processedCombinations = new HashSet<>(); while(!partialSolutionTerminationCriteriaSatisfied()){ logger.trace("ToDo list size: " + todoList.size()); @@ -424,22 +442,23 @@ private EvaluatedRDFResourceTree computeBestPartialSolution(){ currentElement = todoList.poll(); currentTree = currentElement.getTree(); - logger.trace("Next tree: " + currentElement.getTreeScore() + "\n" + solutionAsString(currentElement.getEvaluatedDescription())); + logger.trace("Next tree: {} ({})", currentElement.getBaseQueryTrees(), currentElement.getTreeScore()); - // generate the LGG between the chosen tree and each uncovered positive example + // generate the LGG between the chosen tree and each false negative resp. uncovered positive example Collection falseNegatives = currentElement.getFalseNegatives(); if(falseNegatives.isEmpty()) { // if the current solution covers already all pos examples - bestPartialSolutionTree = currentElement; - addToSolutions(bestPartialSolutionTree); +// addToSolutions(bestPartialSolutionTree); +// bestPartialSolutionTree = currentElement; } Iterator it = falseNegatives.iterator(); - while (it.hasNext() && !isPartialSolutionTimeExpired() && !isTimeExpired()) { + while (it.hasNext() && !(useDisjunction && isPartialSolutionTimeExpired()) && !isTimeExpired()) { RDFResourceTree uncoveredTree = it.next(); + logger.trace("Uncovered tree: " + uncoveredTree); // we should avoid the computation of lgg(t2,t1) if we already did lgg(t1,t2) - Set baseQueryTrees = Sets.newHashSet(currentElement.getBaseQueryTrees()); + Set baseQueryTrees = Sets.newTreeSet(currentElement.getBaseQueryTrees()); baseQueryTrees.add(uncoveredTree); // String s = ""; // for (RDFResourceTree queryTree : baseQueryTrees) { @@ -448,27 +467,36 @@ private EvaluatedRDFResourceTree computeBestPartialSolution(){ // System.err.println(s); if(!processedCombinations.add(baseQueryTrees)) { // System.err.println("skipping"); - continue; +// continue; } // compute the LGG MonitorFactory.getTimeMonitor("lgg").start(); RDFResourceTree lgg = lggGenerator.getLGG(currentTree, uncoveredTree); MonitorFactory.getTimeMonitor("lgg").stop(); +// logger.info("LGG: " + lgg.getStringRepresentation()); + + // redundancy check + boolean redundant = isRedundant(lgg); + if(redundant) { + logger.trace("redundant"); + continue; + } // evaluate the LGG Set solutions = evaluate(lgg, true); for (EvaluatedRDFResourceTree solution : solutions) { solution.setBaseQueryTrees(baseQueryTrees); - + logger.trace("solution: {} ({})", solution.getBaseQueryTrees(), solution.getTreeScore()); expressionTests++; double score = solution.getScore(); double mas = heuristic.getMaximumAchievableScore(solution); if(score >= bestCurrentScore){ if(score > bestCurrentScore){ - logger.info("\tGot better solution:" + solution.getTreeScore()); - logger.info("\t" + solutionAsString(solution.getEvaluatedDescription())); + timeBestSolutionFound = getCurrentRuntimeInMilliSeconds(); + logger.info("\tGot better solution after {}ms:" + solution.getTreeScore(), timeBestSolutionFound); + logger.info("\t" + solutionAsString(solution.asEvaluatedDescription())); bestCurrentScore = score; bestPartialSolutionTree = solution; } @@ -479,7 +507,7 @@ private EvaluatedRDFResourceTree computeBestPartialSolution(){ } else if(bestCurrentScore == 1.0 || mas >= bestCurrentScore){ // add to ToDo list if max. achievable score is higher // todo(solution); } else { - logger.trace("Too weak:" + solution.getTreeScore()); + logger.trace("Too weak: {}", solution.getTreeScore()); // System.err.println(solution.getEvaluatedDescription()); // System.out.println("Too general"); // System.out.println("MAS=" + mas + "\nBest=" + bestCurrentScore); @@ -494,7 +522,7 @@ private EvaluatedRDFResourceTree computeBestPartialSolution(){ long endTime = System.currentTimeMillis(); logger.info("...finished computing best partial solution in " + (endTime-partialSolutionStartTime) + "ms."); - EvaluatedDescription bestPartialSolution = bestPartialSolutionTree.getEvaluatedDescription(); + EvaluatedDescription bestPartialSolution = bestPartialSolutionTree.asEvaluatedDescription(); logger.info("Best partial solution: " + solutionAsString(bestPartialSolution) + "\n(" + bestPartialSolution.getScore() + ")"); @@ -510,7 +538,7 @@ private EvaluatedRDFResourceTree computeBestPartialSolution(){ } private String solutionAsString(EvaluatedDescription ed) { - return ed.getDescription().toString().replace("\n", "").replaceAll("\\\\s{2,}", " "); + return renderer.render(ed.getDescription()).replace("\n", "").replaceAll("\\\\s{2,}", " "); } private boolean addToSolutions(EvaluatedRDFResourceTree solution) { @@ -531,13 +559,13 @@ private boolean addToSolutions(EvaluatedRDFResourceTree solution) { * @param negExamples the negative example trees */ private void initTodoList(List posExamples, List negExamples){ - todoList = new PriorityQueue(); - currentPartialSolutions = new TreeSet(); + todoList = new PriorityQueue<>(); + currentPartialSolutions = new TreeSet<>(); // EvaluatedRDFResourceTree dummy = new EvaluatedRDFResourceTree(new QueryTreeImpl((N)"TOP"), trees, 0d); // todoList.add(dummy); // compute distinct trees, i.e. check if some of the trees already cover others - Collection distinctTrees = new ArrayList(); + Collection distinctTrees = new ArrayList<>(); for (RDFResourceTree queryTree : posExamples) { boolean distinct = true; for (RDFResourceTree otherTree : distinctTrees) { @@ -562,28 +590,34 @@ private void initTodoList(List posExamples, List uncoveredPositiveExampleTrees = getUncoveredTrees(tree, currentPosExampleTrees); - Set uncoveredPosExamples = new TreeSet(); + Set uncoveredPosExamples = new TreeSet<>(); for (RDFResourceTree queryTree : uncoveredPositiveExampleTrees) { uncoveredPosExamples.add(tree2Individual.get(queryTree)); } //compute negative examples which are covered by LGG Collection coveredNegativeExampleTrees = getCoveredTrees(tree, currentNegExampleTrees); - Set coveredNegExamples = new TreeSet(); + Set coveredNegExamples = new TreeSet<>(); for (RDFResourceTree queryTree : coveredNegativeExampleTrees) { coveredNegExamples.add(tree2Individual.get(queryTree)); } //compute score int coveredPositiveExamples = currentPosExampleTrees.size() - uncoveredPositiveExampleTrees.size(); double recall = coveredPositiveExamples / (double)currentPosExampleTrees.size(); - double precision = (coveredNegativeExampleTrees.size() + coveredPositiveExamples == 0) - ? 0 + double precision = (coveredNegativeExampleTrees.size() + coveredPositiveExamples == 0) + ? 0 : coveredPositiveExamples / (double)(coveredPositiveExamples + coveredNegativeExampleTrees.size()); double coverageScore = Heuristics.getFScore(recall, precision, beta); @@ -626,12 +660,12 @@ private EvaluatedRDFResourceTree evaluateSimple(RDFResourceTree tree, boolean us //3.compute the total score double score = coverageWeight * coverageScore + specifityWeight * specifityScore; - QueryTreeScore queryTreeScore = new QueryTreeScore(score, coverageScore, - new TreeSet(Sets.difference(currentPosExamples, uncoveredPosExamples)), uncoveredPosExamples, - coveredNegExamples, new TreeSet(Sets.difference(currentNegExamples, coveredNegExamples)), + QueryTreeScore queryTreeScore = new QueryTreeScore(score, coverageScore, + new TreeSet<>(Sets.difference(currentPosExamples, uncoveredPosExamples)), uncoveredPosExamples, + coveredNegExamples, new TreeSet<>(Sets.difference(currentNegExamples, coveredNegExamples)), specifityScore, nrOfSpecificNodes); -// QueryTreeScore queryTreeScore = new QueryTreeScore(score, coverageScore, +// QueryTreeScore queryTreeScore = new QueryTreeScore(score, coverageScore, // null,null,null,null, // specifityScore, nrOfSpecificNodes); @@ -646,42 +680,46 @@ private EvaluatedRDFResourceTree evaluateSimple(RDFResourceTree tree, boolean us } /** - * Returns a set of evaluated query trees. A set is returned because there are several ways how to convert literal nodes. - * @param tree - * @param useSpecifity - * @return + * Evaluated a query tree such that it returns a set of evaluated query trees. + * A set is returned because there are several ways how to convert literal nodes. + * @param tree the query tree + * @param useSpecifity whether to use SPECIFITY as measure + * @return a set of evaluated query trees */ private Set evaluate(RDFResourceTree tree, boolean useSpecifity){ - Set evaluatedTrees = new TreeSet(); + Set evaluatedTrees = new TreeSet<>(); LiteralNodeSubsumptionStrategy[] strategies = LiteralNodeSubsumptionStrategy.values(); strategies = new LiteralNodeSubsumptionStrategy[]{ - LiteralNodeSubsumptionStrategy.DATATYPE, -// LiteralNodeSubsumptionStrategy.INTERVAL, + LiteralNodeSubsumptionStrategy.DATATYPE, +// LiteralNodeSubsumptionStrategy.INTERVAL, // LiteralNodeSubsumptionStrategy.MIN, // LiteralNodeSubsumptionStrategy.MAX, }; for (LiteralNodeSubsumptionStrategy strategy : strategies) { // 1. get a score for the coverage = recall oriented - List uncoveredPositiveExampleTrees = new ArrayList(); - List coveredNegativeExampleTrees = new ArrayList(); + List uncoveredPositiveExampleTrees = new ArrayList<>(); + List coveredNegativeExampleTrees = new ArrayList<>(); // compute positive examples which are not covered by LGG for (RDFResourceTree posTree : currentPosExampleTrees) { // System.out.print(currentPosExampleTrees.indexOf(posTree) + ":"); - if(!QueryTreeUtils.isSubsumedBy(posTree, tree, strategy)){ + if(!QueryTreeUtils.isSubsumedBy(posTree, tree, entailment, reasoner)){ +// System.err.println(posTree.getStringRepresentation(true));System.err.println(tree.getStringRepresentation(true)); // System.out.println("FALSE"); uncoveredPositiveExampleTrees.add(posTree); } else { // System.out.println("TRUE"); } } + // compute negative examples which are covered by LGG for (RDFResourceTree negTree : currentNegExampleTrees) { - if(QueryTreeUtils.isSubsumedBy(negTree, tree, strategy)){ + if(QueryTreeUtils.isSubsumedBy(negTree, tree, entailment, reasoner)){ coveredNegativeExampleTrees.add(negTree); } } + // convert to individuals Set uncoveredPosExamples = asIndividuals(uncoveredPositiveExampleTrees); Set coveredNegExamples = asIndividuals(coveredNegativeExampleTrees); @@ -689,8 +727,8 @@ private Set evaluate(RDFResourceTree tree, boolean use // compute score int coveredPositiveExamples = currentPosExampleTrees.size() - uncoveredPositiveExampleTrees.size(); double recall = coveredPositiveExamples / (double)currentPosExampleTrees.size(); - double precision = (coveredNegativeExampleTrees.size() + coveredPositiveExamples == 0) - ? 0 + double precision = (coveredNegativeExampleTrees.size() + coveredPositiveExamples == 0) + ? 0 : coveredPositiveExamples / (double)(coveredPositiveExamples + coveredNegativeExampleTrees.size()); double coverageScore = Heuristics.getFScore(recall, precision, beta); @@ -710,9 +748,9 @@ private Set evaluate(RDFResourceTree tree, boolean use // 3.compute the total score double score = coverageWeight * coverageScore + specifityWeight * specifityScore; - QueryTreeScore queryTreeScore = new QueryTreeScore(score, coverageScore, - new TreeSet(Sets.difference(currentPosExamples, uncoveredPosExamples)), uncoveredPosExamples, - coveredNegExamples, new TreeSet(Sets.difference(currentNegExamples, coveredNegExamples)), + QueryTreeScore queryTreeScore = new QueryTreeScore(score, coverageScore, + new TreeSet<>(Sets.difference(currentPosExamples, uncoveredPosExamples)), uncoveredPosExamples, + coveredNegExamples, new TreeSet<>(Sets.difference(currentNegExamples, coveredNegExamples)), specifityScore, nrOfSpecificNodes); EvaluatedRDFResourceTree evaluatedTree = new EvaluatedRDFResourceTree(tree, uncoveredPositiveExampleTrees, coveredNegativeExampleTrees, queryTreeScore); @@ -727,18 +765,19 @@ private Set evaluate(RDFResourceTree tree, boolean use return evaluatedTrees; } - + /** - * Returns a set of evaluated query trees. A set is returned because there are several ways how to convert literal nodes. - * @param tree - * @param useSpecifity - * @return + * Evaluated a query tree such that it returns a set of evaluated query trees. + * A set is returned because there are several ways how to convert literal nodes. + * @param tree the query tree + * @param useSpecifity whether to use SPECIFITY as measure + * @return a set of evaluated query trees */ private Set evaluate2(RDFResourceTree tree, boolean useSpecifity){ - Set evaluatedTrees = new TreeSet(); + Set evaluatedTrees = new TreeSet<>(); //test different strategies on the conversion of literal nodes - Set combinations = new HashSet(); + Set combinations = new HashSet<>(); for (LiteralNodeConversionStrategy strategy : strategies) { OWLClassExpression ce = QueryTreeUtils.toOWLClassExpression(tree); @@ -750,15 +789,15 @@ private Set evaluate2(RDFResourceTree tree, boolean us for (OWLClassExpression c : combinations) { //convert to individuals SortedSet coveredExamples = reasoner.getIndividuals(c); - Set coveredPosExamples = new TreeSet(Sets.intersection(currentPosExamples, coveredExamples)); - Set uncoveredPosExamples = new TreeSet(Sets.difference(currentPosExamples, coveredExamples)); - Set coveredNegExamples = new TreeSet(Sets.intersection(currentNegExamples, coveredExamples)); - Set uncoveredNegExamples = new TreeSet(Sets.difference(currentNegExamples, coveredExamples)); + Set coveredPosExamples = new TreeSet<>(Sets.intersection(currentPosExamples, coveredExamples)); + Set uncoveredPosExamples = new TreeSet<>(Sets.difference(currentPosExamples, coveredExamples)); + Set coveredNegExamples = new TreeSet<>(Sets.intersection(currentNegExamples, coveredExamples)); + Set uncoveredNegExamples = new TreeSet<>(Sets.difference(currentNegExamples, coveredExamples)); //compute score double recall = coveredPosExamples.size() / (double)currentPosExamples.size(); - double precision = (coveredNegExamples.size() + coveredPosExamples.size() == 0) - ? 0 + double precision = (coveredNegExamples.size() + coveredPosExamples.size() == 0) + ? 0 : coveredPosExamples.size() / (double)(coveredPosExamples.size() + coveredNegExamples.size()); double coverageScore = Heuristics.getFScore(recall, precision, beta); @@ -779,13 +818,13 @@ private Set evaluate2(RDFResourceTree tree, boolean us double score = coverageWeight * coverageScore + specifityWeight * specifityScore; QueryTreeScore queryTreeScore = new QueryTreeScore( - score, coverageScore, + score, coverageScore, coveredPosExamples, uncoveredPosExamples, coveredNegExamples, uncoveredNegExamples, specifityScore, nrOfSpecificNodes); //TODO use only the heuristic to compute the score - EvaluatedRDFResourceTree evaluatedTree = new EvaluatedRDFResourceTree(tree, + EvaluatedRDFResourceTree evaluatedTree = new EvaluatedRDFResourceTree(tree, asQueryTrees(uncoveredPosExamples), asQueryTrees(coveredNegExamples), queryTreeScore); score = heuristic.getScore(evaluatedTree); queryTreeScore.setScore(score); @@ -809,17 +848,17 @@ private EvaluatedDescription buildCombinedSolution(){ for (LiteralNodeConversionStrategy strategy : strategies) { EvaluatedDescription combinedSolution; if(partialSolutions.size() == 1){ - combinedSolution = partialSolutions.get(0).getEvaluatedDescription(); + combinedSolution = partialSolutions.get(0).asEvaluatedDescription(); } else { - Set disjuncts = new TreeSet(); + Set disjuncts = new TreeSet<>(); - Set posCovered = new HashSet(); - Set negCovered = new HashSet(); + Set posCovered = new HashSet<>(); + Set negCovered = new HashSet<>(); //build the union of all class expressions OWLClassExpression partialDescription; for (EvaluatedRDFResourceTree partialSolution : partialSolutions) { - partialDescription = partialSolution.getEvaluatedDescription().getDescription(); + partialDescription = partialSolution.asEvaluatedDescription().getDescription(); disjuncts.add(partialDescription); posCovered.addAll(partialSolution.getTreeScore().getCoveredPositives()); negCovered.addAll(partialSolution.getTreeScore().getCoveredNegatives()); @@ -831,8 +870,8 @@ private EvaluatedDescription buildCombinedSolution(){ //compute the coverage double recall = posCovered.size() / (double)lp.getPositiveExamples().size(); - double precision = (posCovered.size() + negCovered.size() == 0) - ? 0 + double precision = (posCovered.size() + negCovered.size() == 0) + ? 0 : posCovered.size() / (double)(posCovered.size() + negCovered.size()); double coverageScore = Heuristics.getFScore(recall, precision, beta); @@ -852,15 +891,16 @@ private EvaluatedDescription buildCombinedSolution(){ } private void reset(){ - currentBestSolution = null; - partialSolutions = new ArrayList(); - stop = false; isRunning = true; - MonitorFactory.getTimeMonitor("lgg").reset(); + currentBestSolution = null; + partialSolutions = new ArrayList<>(); bestCurrentScore = minimumTreeScore; + + MonitorFactory.getTimeMonitor("lgg").reset(); + nanoStartTime = System.nanoTime(); } /* (non-Javadoc) @@ -901,11 +941,12 @@ public boolean isRunning() { // } // @Autowired + @Override public void setReasoner(AbstractReasonerComponent reasoner){ super.setReasoner(reasoner); // loadModel(); } - + private void loadModel(){ model = ModelFactory.createDefaultModel(); for (KnowledgeSource ks : reasoner.getSources()) { @@ -928,7 +969,7 @@ private void loadModel(){ } private Set asIndividuals(Collection trees){ - Set individuals = new HashSet(trees.size()); + Set individuals = new HashSet<>(trees.size()); for (RDFResourceTree queryTree : trees) { individuals.add(tree2Individual.get(queryTree)); } @@ -936,24 +977,23 @@ private Set asIndividuals(Collection trees){ } private Set asQueryTrees(Collection individuals){ - Set trees = new HashSet(individuals.size()); + Set trees = new HashSet<>(individuals.size()); for (OWLIndividual ind : individuals) { - trees.add(individual2Tree.get(ind)); + trees.add(tree2Individual.inverse().get(ind)); } return trees; } /** - * Return all trees from the given list {@code allTrees} which are not already subsumed by {@code tree}. - * @param tree - * @param allTrees - * @return + * Computes all trees from the given list {@code allTrees} which are subsumed by {@code tree}. + * @param tree the tree + * @param trees all trees + * @return all trees from the given list {@code allTrees} which are subsumed by {@code tree} */ private List getCoveredTrees(RDFResourceTree tree, List trees){ - List coveredTrees = new ArrayList(); + List coveredTrees = new ArrayList<>(); for (RDFResourceTree queryTree : trees) { - boolean subsumed = QueryTreeUtils.isSubsumedBy(queryTree, tree); - if(subsumed){ + if(QueryTreeUtils.isSubsumedBy(queryTree, tree)){ coveredTrees.add(queryTree); } } @@ -961,16 +1001,15 @@ private List getCoveredTrees(RDFResourceTree tree, List getUncoveredTrees(RDFResourceTree tree, List trees){ - List uncoveredTrees = new ArrayList(); + List uncoveredTrees = new ArrayList<>(); for (RDFResourceTree queryTree : trees) { - boolean subsumed = QueryTreeUtils.isSubsumedBy(queryTree, tree); - if(!subsumed){ + if(!QueryTreeUtils.isSubsumedBy(queryTree, tree)){ uncoveredTrees.add(queryTree); } } @@ -1005,19 +1044,13 @@ private boolean terminationCriteriaSatisfied() { } private boolean partialSolutionTerminationCriteriaSatisfied(){ - return stop || todoList.isEmpty() || currentPosExampleTrees.isEmpty() || isPartialSolutionTimeExpired() || isTimeExpired(); - } - - private boolean isTimeExpired(){ - return maxExecutionTimeInSeconds > 0 && (System.currentTimeMillis() - startTime) / 1000d >= maxExecutionTimeInSeconds; + return stop || todoList.isEmpty() || currentPosExampleTrees.isEmpty() || (useDisjunction && isPartialSolutionTimeExpired()) || isTimeExpired(); } private boolean isPartialSolutionTimeExpired(){ - return maxTreeComputationTimeInSeconds <= 0 ? false : (System.currentTimeMillis() - partialSolutionStartTime)/1000d >= maxTreeComputationTimeInSeconds; + return maxTreeComputationTimeInSeconds > 0 && (System.currentTimeMillis() - partialSolutionStartTime) / 1000d >= maxTreeComputationTimeInSeconds; } - - /** * Shows the current setup of the algorithm. */ @@ -1046,8 +1079,10 @@ public void setNoise(double noise) { } /** - * @param maxExecutionTimeInSeconds the maxExecutionTimeInSeconds to set + * @param maxExecutionTimeInSeconds the maximum execution time in seconds until the + * algorithm will terminate gracefully */ + @Override public void setMaxExecutionTimeInSeconds(int maxExecutionTimeInSeconds) { this.maxExecutionTimeInSeconds = maxExecutionTimeInSeconds; } @@ -1097,9 +1132,8 @@ public SortedSet getSolutions(){ } public List getSolutionsAsList(){ - ArrayList list = new ArrayList<>(currentPartialSolutions); -// Collections.sort(list, Collections.reverseOrder()); - return list; + // Collections.sort(list, Collections.reverseOrder()); + return new ArrayList<>(currentPartialSolutions); } /** @@ -1107,7 +1141,7 @@ public List getSolutionsAsList(){ */ public void setPositiveExampleTrees(Map positiveExampleTrees) { this.currentPosExampleTrees = new ArrayList<>(positiveExampleTrees.values()); - this.currentPosExamples = new HashSet(positiveExampleTrees.keySet()); + this.currentPosExamples = new HashSet<>(positiveExampleTrees.keySet()); for (Entry entry : positiveExampleTrees.entrySet()) { OWLIndividual ind = entry.getKey(); @@ -1121,7 +1155,7 @@ public void setPositiveExampleTrees(Map positiveE */ public void setNegativeExampleTrees(Map negativeExampleTrees) { this.currentNegExampleTrees = new ArrayList<>(negativeExampleTrees.values()); - this.currentNegExamples = new HashSet(negativeExampleTrees.keySet()); + this.currentNegExamples = new HashSet<>(negativeExampleTrees.keySet()); for (Entry entry : negativeExampleTrees.entrySet()) { OWLIndividual ind = entry.getKey(); @@ -1139,17 +1173,26 @@ public void setKs(SparqlEndpointKS ks) { } /** - * @param maxTreeDepth the maxTreeDepth to set + * @param maxTreeDepth the maximum depth of the trees, if those have to be generated + * first. The default depth is 2. */ public void setMaxTreeDepth(int maxTreeDepth) { this.maxTreeDepth = maxTreeDepth; } + /** + * @return the runtime until the best solution was found + */ + public long getTimeBestSolutionFound() { + return timeBestSolutionFound; + } + /* (non-Javadoc) * @see java.lang.Object#clone() */ @Override public Object clone() throws CloneNotSupportedException { + super.clone(); return new QTL2Disjunctive(this); } } diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/QueryTreeUtils.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/QueryTreeUtils.java index bcf24d03b9..a7cd497b9d 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/QueryTreeUtils.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/QueryTreeUtils.java @@ -1,65 +1,70 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; - +import com.google.common.collect.HashMultimap; +import com.google.common.collect.Multimap; +import com.hp.hpl.jena.datatypes.RDFDatatype; +import com.hp.hpl.jena.graph.Node; +import com.hp.hpl.jena.graph.NodeFactory; +import com.hp.hpl.jena.query.Query; +import com.hp.hpl.jena.query.QueryFactory; +import com.hp.hpl.jena.query.Syntax; +import com.hp.hpl.jena.rdf.model.*; +import com.hp.hpl.jena.reasoner.Reasoner; +import com.hp.hpl.jena.reasoner.ReasonerRegistry; +import com.hp.hpl.jena.shared.PrefixMapping; +import com.hp.hpl.jena.sparql.expr.*; +import com.hp.hpl.jena.sparql.serializer.SerializationContext; +import com.hp.hpl.jena.sparql.util.FmtUtils; +import com.hp.hpl.jena.vocabulary.OWL; +import com.hp.hpl.jena.vocabulary.RDF; +import com.hp.hpl.jena.vocabulary.RDFS; import org.dllearner.algorithms.qtl.datastructures.QueryTree; import org.dllearner.algorithms.qtl.datastructures.impl.QueryTreeImpl.LiteralNodeConversionStrategy; import org.dllearner.algorithms.qtl.datastructures.impl.QueryTreeImpl.LiteralNodeSubsumptionStrategy; import org.dllearner.algorithms.qtl.datastructures.impl.QueryTreeImpl.NodeType; import org.dllearner.algorithms.qtl.datastructures.impl.RDFResourceTree; +import org.dllearner.algorithms.qtl.datastructures.rendering.Edge; +import org.dllearner.algorithms.qtl.datastructures.rendering.Vertex; import org.dllearner.algorithms.qtl.util.Entailment; import org.dllearner.algorithms.qtl.util.VarGenerator; import org.dllearner.core.AbstractReasonerComponent; import org.dllearner.reasoning.SPARQLReasoner; import org.dllearner.utilities.OwlApiJenaUtils; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLDataProperty; -import org.semanticweb.owlapi.model.OWLLiteral; - +import org.jgrapht.DirectedGraph; +import org.jgrapht.ext.EdgeNameProvider; +import org.jgrapht.ext.GraphMLExporter; +import org.jgrapht.ext.VertexNameProvider; +import org.jgrapht.graph.DefaultDirectedGraph; +import org.semanticweb.owlapi.model.*; +import org.xml.sax.SAXException; import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl; import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; +import uk.ac.manchester.cs.owl.owlapi.OWLDataPropertyImpl; import uk.ac.manchester.cs.owl.owlapi.OWLObjectPropertyImpl; -import com.hp.hpl.jena.datatypes.RDFDatatype; -import com.hp.hpl.jena.graph.Node; -import com.hp.hpl.jena.graph.NodeFactory; -import com.hp.hpl.jena.query.Query; -import com.hp.hpl.jena.query.QueryFactory; -import com.hp.hpl.jena.query.Syntax; -import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.ModelFactory; -import com.hp.hpl.jena.rdf.model.Property; -import com.hp.hpl.jena.rdf.model.RDFNode; -import com.hp.hpl.jena.rdf.model.Resource; -import com.hp.hpl.jena.rdf.model.ResourceFactory; -import com.hp.hpl.jena.rdf.model.Statement; -import com.hp.hpl.jena.rdf.model.StmtIterator; -import com.hp.hpl.jena.reasoner.Reasoner; -import com.hp.hpl.jena.reasoner.ReasonerRegistry; -import com.hp.hpl.jena.shared.PrefixMapping; -import com.hp.hpl.jena.sparql.expr.E_Datatype; -import com.hp.hpl.jena.sparql.expr.E_Equals; -import com.hp.hpl.jena.sparql.expr.E_LogicalAnd; -import com.hp.hpl.jena.sparql.expr.ExprNode; -import com.hp.hpl.jena.sparql.expr.ExprVar; -import com.hp.hpl.jena.sparql.expr.NodeValue; -import com.hp.hpl.jena.sparql.serializer.SerializationContext; -import com.hp.hpl.jena.sparql.util.FmtUtils; -import com.hp.hpl.jena.vocabulary.RDF; -import com.hp.hpl.jena.vocabulary.RDFS; +import javax.xml.transform.TransformerConfigurationException; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.*; /** * @author Lorenz Buehmann @@ -69,7 +74,7 @@ public class QueryTreeUtils { private static final VarGenerator varGen = new VarGenerator("x"); private static final String TRIPLE_PATTERN_TEMPLATE = "%s %s %s ."; - private static final OWLDataFactory df = new OWLDataFactoryImpl(false, false); + private static final OWLDataFactory df = new OWLDataFactoryImpl(); public static String EMPTY_QUERY_TREE_QUERY = "SELECT ?s WHERE {?s ?p ?o.}"; @@ -85,7 +90,7 @@ public static List getPathToRoot(RDFResourceTree tree, RDFResou if(node.isRoot()) { return Collections.singletonList(node); } - List path = new ArrayList(); + List path = new ArrayList<>(); // add node itself path.add(node); @@ -116,11 +121,11 @@ public static String printPathToRoot(RDFResourceTree tree, RDFResourceTree node) Iterator iterator = path.iterator(); RDFResourceTree child = iterator.next(); - sb.append(child + "(" + child.getID() + ")"); + sb.append(child).append("(").append(child.getID()).append(")"); while (iterator.hasNext()) { RDFResourceTree parent = iterator.next(); sb.append(" <").append(parent.getEdgeToChild(child)).append("> "); - sb.append(parent + "(" + parent.getID() + ")"); + sb.append(parent).append("(").append(parent.getID()).append(")"); child = parent; } return sb.toString(); @@ -149,7 +154,7 @@ public static List> getNodes(QueryTree tree, NodeType nodeTy // filter by type Iterator> iterator = nodes.iterator(); while (iterator.hasNext()) { - QueryTree node = (QueryTree) iterator.next(); + QueryTree node = iterator.next(); if(node.getNodeType() != nodeType) { iterator.remove(); } @@ -269,7 +274,7 @@ public static boolean isSubsumedBy(QueryTree tree1, QueryTree tree2) { * @return */ public static List getNodes(RDFResourceTree tree) { - List nodes = new ArrayList(); + List nodes = new ArrayList<>(); nodes.add(tree); for (RDFResourceTree child : tree.getChildren()) { @@ -285,7 +290,7 @@ public static List getNodes(RDFResourceTree tree) { * @return */ public static List getLeafs(RDFResourceTree tree) { - List leafs = new ArrayList(); + List leafs = new ArrayList<>(); for (RDFResourceTree node : getNodes(tree)) { if(node.isLeaf()) { @@ -406,10 +411,12 @@ public static boolean isSubsumedBy(RDFResourceTree tree1, RDFResourceTree tree2, } // check if each statement of m1 is contained in m2 - StmtIterator iterator = m1closure.listStatements(); + StmtIterator iterator = m2closure.listStatements(); while (iterator.hasNext()) { Statement st = iterator.next(); - if (!st.getSubject().isAnon() && !st.getObject().isAnon() && !m2closure.contains(st)) { + if (!st.getSubject().isAnon() && !st.getObject().isAnon() + && !(st.getPredicate().equals(RDFS.subClassOf) && st.getSubject().equals(st.getObject())) + && !m1closure.contains(st)) { return false; } } @@ -491,7 +498,7 @@ public static boolean isSubsumedBy(RDFResourceTree tree1, RDFResourceTree tree2, } if(typeNode) { - return isSubsumedBy(tree1, tree2, Entailment.RDFS); +// return isSubsumedBy(tree1, tree2, Entailment.RDFS); } // 2. compare the children @@ -520,7 +527,8 @@ public static boolean isSubsumedBy(RDFResourceTree tree1, RDFResourceTree tree2, * @param trees * @return */ - public static boolean sameTrees(QueryTree... trees) { + @SafeVarargs + public static boolean sameTrees(QueryTree... trees) { for(int i = 0; i < trees.length; i++) { QueryTree tree1 = trees[i]; for(int j = i; j < trees.length; j++) { @@ -547,7 +555,10 @@ public static boolean sameTrees(QueryTree tree1, QueryTree tree2) { } public static boolean sameTrees(RDFResourceTree tree1, RDFResourceTree tree2) { - return isSubsumedBy(tree1, tree2) && isSubsumedBy(tree2, tree1); + return + tree1.getData().equals(tree2.getData()) && // root(t1) == root(t2) + tree1.getNumberOfChildren() == tree2.getNumberOfChildren() && // #children(t1) == #children(t2) + isSubsumedBy(tree1, tree2) && isSubsumedBy(tree2, tree1); // t1 <= t2 && t2 <= t1 } public static Model toModel(RDFResourceTree tree) { @@ -563,15 +574,15 @@ public static Model toModel(RDFResourceTree tree, Resource subject) { } private static void buildModel(Model model, RDFResourceTree tree, Resource subject) { + int i = 0; for (Node edge : tree.getEdges()) { Property p = model.getProperty(edge.getURI()); for (RDFResourceTree child : tree.getChildren(edge)) { - RDFNode object = child.isVarNode() ? model.asRDFNode(NodeFactory.createAnon()).asResource() : model - .asRDFNode(child.getData()); + RDFNode object = child.isVarNode() ? model.asRDFNode(NodeFactory.createAnon()) : model.asRDFNode(child.getData()); model.add(subject, p, object); - if (child.isVarNode()) { +// if (child.isVarNode()) { buildModel(model, child, object.asResource()); - } +// } } } } @@ -585,7 +596,7 @@ public static OWLClassExpression toOWLClassExpression(RDFResourceTree tree, Lite } private static OWLClassExpression buildOWLClassExpression(RDFResourceTree tree, LiteralNodeConversionStrategy literalConversion) { - Set classExpressions = new HashSet(); + Set classExpressions = new HashSet<>(); for(Node edge : tree.getEdges()) { for (RDFResourceTree child : tree.getChildren(edge)) { if(edge.equals(RDF.type.asNode()) || edge.equals(RDFS.subClassOf.asNode())) { @@ -709,6 +720,81 @@ public static String toSPARQLQueryString(RDFResourceTree tree, String baseIRI, P return query.toString(); } + + private static int buildGraph(Integer parentId, DirectedGraph graph, RDFResourceTree tree, SerializationContext context){ + Vertex parent = new Vertex(parentId, FmtUtils.stringForNode(tree.getData(), context)); + graph.addVertex(parent); + + int childId = parentId; + + for (Node edgeNode : tree.getEdges()) { + String edgeLabel = FmtUtils.stringForNode(edgeNode, context); + for (RDFResourceTree child : tree.getChildren(edgeNode)) { + childId++; + String childLabel = FmtUtils.stringForNode(child.getData(), context); + + Vertex childVertex = new Vertex(childId, childLabel); + graph.addVertex(childVertex); + + Edge edge = new Edge(Long.valueOf(parentId + "0" + childId), edgeLabel); + graph.addEdge(parent, childVertex, edge); + System.err.println(edgeLabel); + System.err.println(graph.getEdgeSource(edge).getId()); + System.err.println(graph.getEdgeTarget(edge).getId()); + System.err.println(childId + "::" + childLabel); + + childId = buildGraph(childId, graph, child, context); + } + } + + return childId; + } + + public static void asGraph(RDFResourceTree tree, String baseIRI, PrefixMapping pm, File outputFile) { + SerializationContext context = new SerializationContext(pm); + context.setBaseIRI(baseIRI); + + final DirectedGraph graph = new DefaultDirectedGraph<>(Edge.class); + buildGraph(0, graph, tree, context); + VertexNameProvider vertexIDProvider = new VertexNameProvider() { + @Override + public String getVertexName(Vertex vertex) { + return String.valueOf(vertex.getId()); + } + }; + + VertexNameProvider vertexNameProvider = new VertexNameProvider() { + @Override + public String getVertexName(Vertex vertex) { + return vertex.getLabel(); + } + }; + + EdgeNameProvider edgeIDProvider = new EdgeNameProvider() { + @Override + public String getEdgeName(Edge edge) { + return String.valueOf(edge.getId()); + } + }; + + EdgeNameProvider edgeLabelProvider = new EdgeNameProvider() { + @Override + public String getEdgeName(Edge edge) { + return edge.getLabel(); + } + }; + GraphMLExporter exporter = new GraphMLExporter<>(vertexIDProvider, + vertexNameProvider, edgeIDProvider, edgeLabelProvider); + try { + exporter.export(new FileWriter(outputFile), graph); + } catch (TransformerConfigurationException e) { + e.printStackTrace(); + } catch (SAXException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } private static void buildSPARQLQueryString(RDFResourceTree tree, String subjectStr, StringBuilder sb, Collection filters, @@ -765,15 +851,15 @@ private static void buildSPARQLQueryString(RDFResourceTree tree, * of used statements. * * - * @param tree - * @param entailment + * @param tree the tree + * @param entailment the entailment regime */ public static void prune(RDFResourceTree tree, AbstractReasonerComponent reasoner, Entailment entailment) { // remove trivial statements - for(Node edge : new TreeSet(tree.getEdges())) { + for(Node edge : new TreeSet<>(tree.getEdges())) { if(edge.equals(RDF.type.asNode())) { // check outgoing rdf:type edges - List children = new ArrayList(tree.getChildren(edge)); + List children = new ArrayList<>(tree.getChildren(edge)); for (Iterator iterator = children.iterator(); iterator.hasNext();) { RDFResourceTree child = iterator.next(); if(!isNonTrivial(child, entailment)) { @@ -789,7 +875,7 @@ public static void prune(RDFResourceTree tree, AbstractReasonerComponent reasone } } - if(entailment == entailment.RDFS) { + if(entailment == Entailment.RDFS) { // // 1. rdfs:domain: // // remove rdf:type edges if this is implicitly given by the other outgoing edges // // 2. rdfs:range: @@ -846,7 +932,7 @@ public static void prune(RDFResourceTree tree, AbstractReasonerComponent reasone // we have to run the subsumption check one more time to prune the tree for (Node edge : tree.getEdges()) { - Set children2Remove = new HashSet(); + Set children2Remove = new HashSet<>(); List children = tree.getChildren(edge); for(int i = 0; i < children.size(); i++) { RDFResourceTree child1 = children.get(i); @@ -900,8 +986,7 @@ public static void prune(RDFResourceTree tree, AbstractReasonerComponent reasone /** * Recursively removes edges that lead to a leaf node which is a variable. - * @param tree - * @param entailment + * @param tree the tree */ public static boolean removeVarLeafs(RDFResourceTree tree) { SortedSet edges = new TreeSet<>(tree.getEdges()); @@ -947,4 +1032,178 @@ public static boolean isNonTrivial(RDFResourceTree tree, Entailment entailment) } return false; } + + /** + * @param posTree + * @param tree + * @param entailment + * @param strategy + * @return + */ + public static boolean isSubsumedBy(RDFResourceTree tree1, RDFResourceTree tree2, Entailment entailment, + AbstractReasonerComponent reasoner) { + + if(entailment == Entailment.SIMPLE) { + return isSubsumedBy(tree1, tree2); + } + + // 1.compare the root nodes + + // (T_1 != ?) and (T_2 != ?) --> T_1 = T_2 + if (!tree1.isVarNode() && !tree2.isVarNode()) { + if (tree1.isResourceNode() && tree2.isResourceNode()) { + return tree1.getData().equals(tree2.getData()); + } else if(tree1.isLiteralNode() && tree2.isLiteralNode()) { + if(tree1.isLiteralValueNode()) { + if(tree2.isLiteralValueNode()) { + return tree1.getData().equals(tree2.getData()); + } else { + RDFDatatype d1 = tree1.getData().getLiteralDatatype(); + return tree2.getDatatype().equals(d1); + } + } else { + if(tree2.isLiteralValueNode()) { + return false; + } else { + RDFDatatype d1 = tree1.getDatatype(); + return tree2.getDatatype().equals(d1); + } + } + + } + } + + // (T_1 = ?) and (T_2 != ?) --> FALSE + if (tree1.isVarNode() && !tree2.isVarNode()) { + return false; + } + + // 2. compare the children + for (Node edge2 : tree2.getEdges()) { + + // get sub properties + OWLObjectProperty prop2 = OwlApiJenaUtils.asOWLEntity(edge2, EntityType.OBJECT_PROPERTY); + SortedSet subProperties = reasoner.getSubProperties(prop2); + subProperties.add(prop2); + + // for each subtree T2_sub in T2 + for (RDFResourceTree child2 : tree2.getChildren(edge2)) { + boolean childSubsumed = false; + + // for each sub edge + for(OWLObjectProperty subProp : subProperties) { + // check if there is a child in T_1 that is subsumed by + Node edge1 = OwlApiJenaUtils.asNode(subProp); + List children1 = tree1.getChildren(edge1); + + if(children1 != null) { + for (RDFResourceTree child1 : children1) { + if (QueryTreeUtils.isSubsumedBy(child1, child2, entailment, reasoner)) { + childSubsumed = true; + break; + } + } + } + if(childSubsumed) { + break; + } + } + + // we found no subtree in T1 that is subsumed by t2_sub + if(!childSubsumed) { + return false; + } + } + } + + + // 2. compare the children +// for (Node edge2 : tree2.getEdges()) { +// +// // get sub properties +// OWLObjectProperty prop2 = OwlApiJenaUtils.asOWLEntity(edge2, EntityType.OBJECT_PROPERTY); +// SortedSet subProperties = reasoner.getSubProperties(prop2); +// subProperties.add(prop2); +// +// boolean edgeSubsumed = false; +// +// Iterator iterator = subProperties.iterator(); +// while (!edgeSubsumed && iterator.hasNext()) { +// OWLObjectProperty subProp = iterator.next(); +// +// Node edge1 = OwlApiJenaUtils.asNode(subProp); +// +// List children1 = tree1.getChildren(edge1); +// +// if (children1 != null) { +// +// boolean childrenSubsumed = true; +// for (RDFResourceTree child2 : tree2.getChildren(edge2)) { +// boolean childSubsumed = false; +// +// for (RDFResourceTree child1 : children1) { +// if (QueryTreeUtils.isSubsumedBy(child1, child2, entailment, reasoner)) { +// childSubsumed = true; +// break; +// } +// } +// if(!childSubsumed) { +// childrenSubsumed = false; +// } +// } +// +// if(childrenSubsumed) { +// edgeSubsumed = true; +// } +// } +// } +// +// if(!edgeSubsumed) { +// System.err.println("edge not subsumed"); +// return false; +// } +// } + return true; + } + + /* + * For each edge in tree 1 we compute the related edges in tree 2. + */ + private static Multimap getRelatedEdges(RDFResourceTree tree1, RDFResourceTree tree2, AbstractReasonerComponent reasoner) { + Multimap relatedEdges = HashMultimap.create(); + + for(Node edge1 : tree1.getEdges()) { + // trivial + if(tree2.getEdges().contains(edge1)) { + relatedEdges.put(edge1, edge1); + } + // check if it's not a built-in properties + if (!edge1.getNameSpace().equals(RDF.getURI()) + && !edge1.getNameSpace().equals(RDFS.getURI()) + && !edge1.getNameSpace().equals(OWL.getURI())) { + + // get related edges by subsumption + OWLProperty prop; + if(tree1.isObjectPropertyEdge(edge1)) { + prop = new OWLObjectPropertyImpl(IRI.create(edge1.getURI())); + } else { + prop = new OWLDataPropertyImpl(IRI.create(edge1.getURI())); + } + + for (OWLProperty p : reasoner.getSuperProperties(prop)) { + Node edge = NodeFactory.createURI(p.toStringID()); + if(tree2.getEdges().contains(edge)) { + relatedEdges.put(edge1, edge); + } + } + for (OWLProperty p : reasoner.getSubProperties(prop)) { + Node edge = NodeFactory.createURI(p.toStringID()); + if(tree2.getEdges().contains(edge)) { + relatedEdges.put(edge1, edge); + } + } + } + } + return relatedEdges; + } } diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/NodeRenderer.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/NodeRenderer.java index be58bd40a1..0cdf15c8f4 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/NodeRenderer.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/NodeRenderer.java @@ -1,8 +1,8 @@ /** - * Copyright (C) 2007-2010, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. - * + * * DL-Learner is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or @@ -15,7 +15,6 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * */ package org.dllearner.algorithms.qtl.datastructures; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/QueryTree.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/QueryTree.java index bce7ecd388..f9cb0e0a2b 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/QueryTree.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/QueryTree.java @@ -1,8 +1,8 @@ /** - * Copyright (C) 2007-2010, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. - * + * * DL-Learner is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or @@ -15,7 +15,6 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * */ package org.dllearner.algorithms.qtl.datastructures; @@ -51,8 +50,8 @@ public interface QueryTree { void setUserObject(N userObject); /** - * - * @param id + * Set the ID of the current node + * @param id the ID */ void setId(int id); @@ -133,7 +132,7 @@ public interface QueryTree { void dump(); - public String getStringRepresentation(); + String getStringRepresentation(); void dump(PrintWriter writer); diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/impl/EvaluatedRDFResourceTree.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/impl/EvaluatedRDFResourceTree.java index 74f45c42d3..d513643324 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/impl/EvaluatedRDFResourceTree.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/impl/EvaluatedRDFResourceTree.java @@ -1,18 +1,43 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.datastructures.impl; +import com.google.common.collect.ComparisonChain; import gnu.trove.set.TIntSet; import gnu.trove.set.hash.TIntHashSet; - import java.util.Collection; import java.util.HashSet; import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; import org.dllearner.algorithms.qtl.QueryTreeUtils; import org.dllearner.core.EvaluatedDescription; import org.dllearner.core.Score; import org.dllearner.learningproblems.QueryTreeScore; -import com.google.common.collect.ComparisonChain; +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +import gnu.trove.set.TIntSet; +import gnu.trove.set.hash.TIntHashSet; public class EvaluatedRDFResourceTree implements Comparable{ @@ -39,8 +64,8 @@ public class EvaluatedRDFResourceTree implements Comparable description; - // the query trees of which the underlying query tree was generated from - private Set baseQueryTrees = new HashSet<>(); + // the query trees of which this query tree was generated from + private Set baseQueryTrees = new TreeSet<>(); public EvaluatedRDFResourceTree(RDFResourceTree tree, Collection falseNegatives, Collection falsePositives, QueryTreeScore score) { @@ -127,16 +152,6 @@ public QueryTreeScore getTreeScore() { return score; } - @Override - public int compareTo(EvaluatedRDFResourceTree other) { - return ComparisonChain.start() -// .compare(this.getScore(), other.getScore()) - .compare(other.getScore(), this.getScore()) - .compare(this.asEvaluatedDescription(), other.asEvaluatedDescription()) - .result(); - } - - /** * @return the query tree as OWL class expression */ @@ -144,19 +159,32 @@ public EvaluatedDescription getEvaluatedDescription() { return asEvaluatedDescription(); } /** - * @param OWLClassExpression the OWLClassExpression to set + * @param description the description to set */ public void setDescription(EvaluatedDescription description) { this.description = description; } + /** + * @return the query tree as OWL class expression with score + */ public EvaluatedDescription asEvaluatedDescription(){ + // lazy generation if(description == null){ description = new EvaluatedDescription(QueryTreeUtils.toOWLClassExpression(getTree()), score); } return description; } + @Override + public int compareTo(EvaluatedRDFResourceTree other) { + return ComparisonChain.start() + .compare(other.getScore(), this.getScore()) // score + .compare(this.baseQueryTrees.toString(), other.baseQueryTrees.toString()) // base query trees + .compare(this.asEvaluatedDescription(), other.asEvaluatedDescription()) // class expression representation + .result(); + } + /* (non-Javadoc) * @see java.lang.Object#toString() */ diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/impl/GeneralisedQueryTree.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/impl/GeneralisedQueryTree.java index f7e16f0b2e..2d132cf138 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/impl/GeneralisedQueryTree.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/impl/GeneralisedQueryTree.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.datastructures.impl; import java.util.ArrayList; @@ -13,7 +31,7 @@ public class GeneralisedQueryTree { public GeneralisedQueryTree(QueryTree tree){ this.tree = tree; - changes = new ArrayList(); + changes = new ArrayList<>(); } public GeneralisedQueryTree(QueryTree tree, List changes){ diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/impl/GenericTree.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/impl/GenericTree.java index 4f22a08cd1..b5767411e5 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/impl/GenericTree.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/impl/GenericTree.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.datastructures.impl; import java.util.ArrayList; import java.util.List; @@ -10,7 +28,7 @@ public class GenericTree> { protected T data; protected V parent; - protected List children = new ArrayList(); + protected List children = new ArrayList<>(); public GenericTree() {} @@ -22,16 +40,26 @@ public void setParent(V parent) { this.parent = parent; } + /** + * @return the parent node of this tree, or null if this + * tree is the root node + */ public V getParent() { return parent; } + /** + * @return all direct children of this tree + */ public List getChildren() { return this.children; } + /** + * @return all leaf nodes of this tree + */ public List getLeafs() { - List leafs = new ArrayList(); + List leafs = new ArrayList<>(); for(V child : children) { if(child.isLeaf()) { leafs.add(child); @@ -42,10 +70,16 @@ public List getLeafs() { return leafs; } + /** + * @return whether this is the root node + */ public boolean isRoot() { return parent == null; } + /** + * @return whether this is a leaf node + */ public boolean isLeaf() { return children.isEmpty(); } @@ -84,7 +118,7 @@ public void addChildAt(int index, V child) throws IndexOutOfBoundsException { } public void removeChildren() { - this.children = new ArrayList(); + this.children = new ArrayList<>(); } public void removeChildAt(int index) throws IndexOutOfBoundsException { diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/impl/QueryTreeChange.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/impl/QueryTreeChange.java index e46aed9a0f..9bcad85262 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/impl/QueryTreeChange.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/impl/QueryTreeChange.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.datastructures.impl; @@ -6,7 +24,7 @@ public class QueryTreeChange { public enum ChangeType{ REPLACE_LABEL, - REMOVE_NODE; + REMOVE_NODE } private int nodeId; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/impl/QueryTreeImpl.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/impl/QueryTreeImpl.java index 0134ca9cbb..7e83e9cd58 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/impl/QueryTreeImpl.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/impl/QueryTreeImpl.java @@ -1,8 +1,8 @@ /** - * Copyright (C) 2007-2010, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. - * + * * DL-Learner is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or @@ -15,33 +15,28 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * */ package org.dllearner.algorithms.qtl.datastructures.impl; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.io.PrintWriter; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.TreeSet; -import java.util.regex.Pattern; - -import javax.xml.bind.DatatypeConverter; -import javax.xml.transform.TransformerConfigurationException; - +import com.google.common.collect.Sets; +import com.hp.hpl.jena.datatypes.BaseDatatype; +import com.hp.hpl.jena.datatypes.RDFDatatype; +import com.hp.hpl.jena.datatypes.xsd.XSDDatatype; +import com.hp.hpl.jena.graph.Node; +import com.hp.hpl.jena.graph.NodeFactory; +import com.hp.hpl.jena.graph.Triple; +import com.hp.hpl.jena.query.Query; +import com.hp.hpl.jena.query.QueryFactory; +import com.hp.hpl.jena.query.Syntax; +import com.hp.hpl.jena.rdf.model.Literal; +import com.hp.hpl.jena.sparql.core.TriplePath; +import com.hp.hpl.jena.sparql.syntax.ElementGroup; +import com.hp.hpl.jena.sparql.syntax.ElementPathBlock; +import com.hp.hpl.jena.sparql.syntax.ElementTriplesBlock; +import com.hp.hpl.jena.sparql.syntax.ElementVisitorBase; +import com.hp.hpl.jena.vocabulary.OWL; +import com.hp.hpl.jena.vocabulary.RDF; +import com.hp.hpl.jena.vocabulary.RDFS; import org.dllearner.algorithms.qtl.datastructures.NodeRenderer; import org.dllearner.algorithms.qtl.datastructures.QueryTree; import org.dllearner.algorithms.qtl.datastructures.rendering.Edge; @@ -56,39 +51,21 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLDataProperty; -import org.semanticweb.owlapi.model.OWLDataRange; -import org.semanticweb.owlapi.model.OWLDatatype; -import org.semanticweb.owlapi.model.OWLFacetRestriction; -import org.semanticweb.owlapi.model.OWLLiteral; -import org.semanticweb.owlapi.model.OWLObjectProperty; +import org.semanticweb.owlapi.model.*; import org.semanticweb.owlapi.vocab.OWL2Datatype; import org.semanticweb.owlapi.vocab.OWLFacet; import org.xml.sax.SAXException; - import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; -import com.google.common.collect.Sets; -import com.hp.hpl.jena.datatypes.BaseDatatype; -import com.hp.hpl.jena.datatypes.RDFDatatype; -import com.hp.hpl.jena.datatypes.xsd.XSDDatatype; -import com.hp.hpl.jena.graph.Node; -import com.hp.hpl.jena.graph.Triple; -import com.hp.hpl.jena.query.Query; -import com.hp.hpl.jena.query.QueryFactory; -import com.hp.hpl.jena.query.Syntax; -import com.hp.hpl.jena.rdf.model.Literal; -import com.hp.hpl.jena.sparql.core.TriplePath; -import com.hp.hpl.jena.sparql.syntax.ElementGroup; -import com.hp.hpl.jena.sparql.syntax.ElementPathBlock; -import com.hp.hpl.jena.sparql.syntax.ElementTriplesBlock; -import com.hp.hpl.jena.sparql.syntax.ElementVisitorBase; -import com.hp.hpl.jena.vocabulary.OWL; -import com.hp.hpl.jena.vocabulary.RDF; -import com.hp.hpl.jena.vocabulary.RDFS; +import javax.xml.bind.DatatypeConverter; +import javax.xml.transform.TransformerConfigurationException; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.*; +import java.util.Map.Entry; +import java.util.regex.Pattern; /** * @@ -98,8 +75,8 @@ public class QueryTreeImpl implements QueryTree{ public enum NodeType{ - RESOURCE, LITERAL, BLANK, VARIABLE; - } + RESOURCE, LITERAL, BLANK, VARIABLE + } public enum LiteralNodeSubsumptionStrategy { DATATYPE, @@ -150,15 +127,15 @@ public enum LiteralNodeConversionStrategy{ private boolean isResourceNode = false; private boolean isBlankNode = false; - private Set literals = new HashSet(); + private Set literals = new HashSet<>(); private NodeType nodeType; public QueryTreeImpl(N userObject) { this.userObject = userObject; - children = new ArrayList>(); - child2EdgeMap = new HashMap, Object>(); - edge2ChildrenMap = new HashMap>>(); + children = new ArrayList<>(); + child2EdgeMap = new HashMap<>(); + edge2ChildrenMap = new HashMap<>(); toStringRenderer = new NodeRenderer() { public String render(QueryTree object) { String label = object.toString() + "(" + object.getId() + ")"; @@ -191,9 +168,9 @@ public QueryTreeImpl(N userObject, NodeType nodeType, int id) { this.userObject = userObject; this.nodeType = nodeType; this.id = id; - children = new ArrayList>(); - child2EdgeMap = new HashMap, Object>(); - edge2ChildrenMap = new HashMap>>(); + children = new ArrayList<>(); + child2EdgeMap = new HashMap<>(); + edge2ChildrenMap = new HashMap<>(); toStringRenderer = new NodeRenderer() { public String render(QueryTree object) { String label = object.toString() + "(" + object.getId() + ")"; @@ -251,7 +228,7 @@ public QueryTreeImpl(QueryTree tree){ setId(tree.getId()); QueryTreeImpl subTree; for(QueryTree child : tree.getChildren()){ - subTree = new QueryTreeImpl(child); + subTree = new QueryTreeImpl<>(child); subTree.setId(child.getId()); subTree.setIsLiteralNode(child.isLiteralNode()); subTree.setIsResourceNode(child.isResourceNode()); @@ -398,7 +375,7 @@ public void addChild(QueryTreeImpl child, Object edge) { List> children = edge2ChildrenMap.get(edge); if(children == null){ - children = new ArrayList>(); + children = new ArrayList<>(); edge2ChildrenMap.put((String)edge, children); } children.add(child); @@ -411,7 +388,7 @@ public void addChild(QueryTreeImpl child, Object edge, int position) { List> children = edge2ChildrenMap.get(edge); if(children == null){ - children = new ArrayList>(); + children = new ArrayList<>(); edge2ChildrenMap.put((String)edge, children); } children.add(child); @@ -463,7 +440,7 @@ public Object getEdge(QueryTree child) { } public Set getEdges(){ - return new TreeSet(child2EdgeMap.values()); + return new TreeSet<>(child2EdgeMap.values()); } @@ -473,7 +450,7 @@ public void sortChildren(Comparator> comparator) { public void clearChildren() { - for (QueryTreeImpl child : new ArrayList>(children)) { + for (QueryTreeImpl child : new ArrayList<>(children)) { removeChild(child); } } @@ -498,9 +475,9 @@ public List> getChildren(Object edge) { // return children; List> children = edge2ChildrenMap.get(edge); if(children == null){ - children = new ArrayList>(); + children = new ArrayList<>(); } - return new ArrayList>(children); + return new ArrayList<>(children); } public int getChildCount() { @@ -741,7 +718,7 @@ public QueryTree getRoot() { } public List> getLeafs(){ - List> leafs = new LinkedList>(); + List> leafs = new LinkedList<>(); if(isLeaf()){ leafs.add(this); } else { @@ -754,7 +731,7 @@ public List> getLeafs(){ public List> getPathToRoot() { - List> path = new ArrayList>(); + List> path = new ArrayList<>(); path.add(0, this); QueryTree par = parent; while (par != null) { @@ -768,7 +745,7 @@ public List> getPathToRoot() { public List getUserObjectPathToRoot() { - List path = new ArrayList(); + List path = new ArrayList<>(); path.add(0, this.getUserObject()); QueryTree par = parent; while (par != null) { @@ -779,7 +756,7 @@ public List getUserObjectPathToRoot() { } public List> getChildrenClosure() { - List> children = new ArrayList>(); + List> children = new ArrayList<>(); getChildrenClosure(this, children); return children; } @@ -793,7 +770,7 @@ private void getChildrenClosure(QueryTree tree, List> bin) { public Set getUserObjectClosure() { - Set objects = new HashSet(); + Set objects = new HashSet<>(); getUserObjectClosure(this, objects); return objects; } @@ -823,7 +800,7 @@ public QueryTree getSPARQLQueryTree(){ } private QueryTree createSPARQLQueryTree(QueryTree tree){ - QueryTree copy = new QueryTreeImpl(tree.getUserObject()); + QueryTree copy = new QueryTreeImpl<>(tree.getUserObject()); if(tree.getUserObject().equals("?")){ for(QueryTree child : tree.getChildren()){ copy.addChild((QueryTreeImpl) createSPARQLQueryTree(child), tree.getEdge(child)); @@ -854,8 +831,8 @@ public String getStringRepresentation(){ /** * Prints the query tree and shows children of resources only if enabled. - * @param stopWhenLeafNode - * @return + * @param stopIfChildIsResourceNode whether to stop if child is resource + * @return the query tree string */ public String getStringRepresentation(boolean stopIfChildIsResourceNode){ int depth = getPathToRoot().size(); @@ -878,7 +855,7 @@ public String getStringRepresentation(boolean stopIfChildIsResourceNode){ sb.append(edge); sb.append(" ---> "); } - sb.append(((QueryTreeImpl)child).getStringRepresentation(stopIfChildIsResourceNode)); + sb.append(child.getStringRepresentation(stopIfChildIsResourceNode)); } } return sb.toString(); @@ -979,7 +956,7 @@ private boolean meaningful(QueryTree tree){ } public List fillDepthFirst() { - List results = new ArrayList(); + List results = new ArrayList<>(); fillDepthFirst(this, results); return results; } @@ -1032,7 +1009,7 @@ private int getMaxDepth(QueryTree tree) { @Override public Object clone() throws CloneNotSupportedException { - QueryTreeImpl copy = new QueryTreeImpl(this.userObject, this.nodeType); + QueryTreeImpl copy = new QueryTreeImpl<>(this.userObject, this.nodeType); copy.setIsResourceNode(isResourceNode); copy.setIsLiteralNode(isLiteralNode); for(QueryTreeImpl child : children){ @@ -1105,7 +1082,7 @@ public String toSPARQLQueryString() { cnt = 0; StringBuilder sb = new StringBuilder(); sb.append("SELECT DISTINCT ?x0 WHERE {\n"); - List filters = new ArrayList(); + List filters = new ArrayList<>(); buildSPARQLQueryString(this, sb, true, false, filters); for(String filter : filters){ sb.append(filter).append("\n"); @@ -1125,7 +1102,7 @@ public String toSPARQLQueryString(boolean filterMeaninglessProperties, boolean u } cnt = 0; StringBuilder sb = new StringBuilder(); - List filters = new ArrayList(); + List filters = new ArrayList<>(); sb.append("SELECT DISTINCT ?x0 WHERE {\n"); buildSPARQLQueryString(this, sb, filterMeaninglessProperties, useNumericalFilters, filters); for(String filter : filters){ @@ -1394,7 +1371,7 @@ private Literal getMax(Set literals){ public Query toQuery(){ Query query = QueryFactory.make(); query.setQuerySelectType(); - query.addResultVar(Node.createVariable("x0")); + query.addResultVar(NodeFactory.createVariable("x0")); query.setDistinct(true); query.setPrefix("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); query.setPrefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#"); @@ -1417,34 +1394,34 @@ public Query toQuery(){ } private List buildTriples(QueryTree tree){ - List triples = new ArrayList(); + List triples = new ArrayList<>(); Pattern pattern = Pattern.compile("^^", Pattern.LITERAL); - Node subject = tree.getUserObject().equals("?") ? Node.createVariable("x" + tree.getId()) : Node.createURI((String) tree.getUserObject()); + Node subject = tree.getUserObject().equals("?") ? NodeFactory.createVariable("x" + tree.getId()) : NodeFactory.createURI((String) tree.getUserObject()); Node predicate = null; Node object = null; String objectLabel = null; for(QueryTree child : tree.getChildren()){ - predicate = Node.createURI((String) tree.getEdge(child)); + predicate = NodeFactory.createURI((String) tree.getEdge(child)); objectLabel = (String) child.getUserObject(); if(objectLabel.equals("?")){ - object = Node.createVariable("x" + child.getId()); + object = NodeFactory.createVariable("x" + child.getId()); } else if(objectLabel.startsWith("http:")){ - object = Node.createURI(objectLabel); + object = NodeFactory.createURI(objectLabel); } else { // System.out.println(objectLabel); String[] split = objectLabel.split("@"); // System.out.println(Arrays.toString(split)); if(split.length == 2){ - object = Node.createLiteral(split[0], split[1], null); + object = NodeFactory.createLiteral(split[0], split[1], null); } else { split = pattern.split(objectLabel); if(split.length == 2){ - object = Node.createLiteral(split[0], null, new BaseDatatype(split[1])); + object = NodeFactory.createLiteral(split[0], null, new BaseDatatype(split[1])); } else { - object = Node.createLiteral(objectLabel); + object = NodeFactory.createLiteral(objectLabel); } } @@ -1514,7 +1491,7 @@ private Set buildOWLClassExpressions(OWLDataFactory df, Quer } // process children - Set classExpressions = new HashSet(); + Set classExpressions = new HashSet<>(); for(QueryTree child : children){ String childLabel = (String) child.getUserObject(); String predicateString = (String) tree.getEdge(child); @@ -1633,7 +1610,7 @@ private OWLDataRange asDataOneOf(OWLDataFactory df, Set literals){ } private Set asOWLLiterals(OWLDataFactory df, Set literals){ - Set owlLiterals = new HashSet(literals.size()); + Set owlLiterals = new HashSet<>(literals.size()); for (Literal literal : literals) { owlLiterals.add(asOWLLiteral(df, literal)); } @@ -1721,7 +1698,7 @@ private String prefixed(Map prefixes, String uri){ } public void asGraph() { - final DirectedGraph graph = new DefaultDirectedGraph(Edge.class); + final DirectedGraph graph = new DefaultDirectedGraph<>(Edge.class); buildGraph(graph, this); VertexNameProvider vertexIDProvider = new VertexNameProvider() { @Override @@ -1750,15 +1727,11 @@ public String getEdgeName(Edge edge) { return edge.getLabel(); } }; - GraphMLExporter exporter = new GraphMLExporter(vertexIDProvider, - vertexNameProvider, edgeIDProvider, edgeLabelProvider); + GraphMLExporter exporter = new GraphMLExporter<>(vertexIDProvider, + vertexNameProvider, edgeIDProvider, edgeLabelProvider); try { exporter.export(new FileWriter(new File("tree.graphml")), graph); - } catch (TransformerConfigurationException e) { - e.printStackTrace(); - } catch (SAXException e) { - e.printStackTrace(); - } catch (IOException e) { + } catch (TransformerConfigurationException | SAXException | IOException e) { e.printStackTrace(); } } diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/impl/RDFResourceTree.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/impl/RDFResourceTree.java index 64b9925247..c5888a585e 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/impl/RDFResourceTree.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/impl/RDFResourceTree.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.datastructures.impl; @@ -18,10 +33,12 @@ import java.util.TreeMap; import java.util.TreeSet; +import org.dllearner.algorithms.qtl.QueryTreeUtils; import org.dllearner.algorithms.qtl.datastructures.impl.QueryTreeImpl.NodeType; import org.dllearner.algorithms.qtl.util.PrefixCCPrefixMapping; import com.google.common.collect.BiMap; +import com.google.common.collect.ComparisonChain; import com.google.common.collect.Sets; import com.hp.hpl.jena.datatypes.BaseDatatype; import com.hp.hpl.jena.datatypes.RDFDatatype; @@ -39,7 +56,7 @@ * @author Lorenz Buehmann * */ -public class RDFResourceTree extends GenericTree implements Serializable{ +public class RDFResourceTree extends GenericTree implements Serializable, Comparable{ public enum Rendering { INDENTED, BRACES @@ -54,7 +71,7 @@ public enum Rendering { private RDFDatatype datatype; private Map child2Edge = new HashMap<>(); - private NavigableMap> edge2Children = new TreeMap>(new NodeComparator()); + private NavigableMap> edge2Children = new TreeMap<>(new NodeComparator()); // private TreeMultimap edge2Children = TreeMultimap.create( // new NodeComparator(), Ordering.arbitrary()); @@ -87,17 +104,16 @@ public RDFResourceTree(Node data) { /** * Create empty literal node with given datatype. - * @param id - * @param datatype + * @param datatype the datatype */ public RDFResourceTree(RDFDatatype datatype) { this(0, datatype); } /** - * Create empty literal node with given datatype. - * @param id - * @param datatype + * Create empty literal node with given ID and datatype. + * @param id the ID + * @param datatype the datatype */ public RDFResourceTree(int id, RDFDatatype datatype) { super(DEFAULT_LITERAL_NODE); @@ -106,9 +122,10 @@ public RDFResourceTree(int id, RDFDatatype datatype) { } /** - * Create empty literal node with given datatype. - * @param id - * @param datatype + * Create literal node with given ID, datatype and a set of literal values. + * @param id the ID + * @param datatype the datatype + * @param literals the literal values */ public RDFResourceTree(int id, RDFDatatype datatype, Set literals) { super(DEFAULT_LITERAL_NODE); @@ -140,7 +157,7 @@ public void addChild(RDFResourceTree child, Node edge) { super.addChild(child); List childrenForEdge = edge2Children.get(edge); if(childrenForEdge == null) { - childrenForEdge = new ArrayList(); + childrenForEdge = new ArrayList<>(); edge2Children.put(edge, childrenForEdge); } childrenForEdge.add(child); @@ -152,7 +169,7 @@ public void addChildren(List children, Node edge) { super.addChildren(children); List childrenForEdge = edge2Children.get(edge); if(childrenForEdge == null) { - childrenForEdge = new ArrayList(); + childrenForEdge = new ArrayList<>(); edge2Children.put(edge, childrenForEdge); } childrenForEdge.addAll(children); @@ -179,10 +196,16 @@ public void removeChild(RDFResourceTree child, Node edge) { child2Edge.remove(child); } + @Override public List getChildren() { return super.getChildren(); } + /** + * @param edge the edge + * @return all children for the specified edge, or null if + * there is no child for the edge + */ public List getChildren(Node edge) { return edge2Children.get(edge); } @@ -192,19 +215,27 @@ public Node getEdgeToChild(RDFResourceTree child) { } /** - * Returns all outgoing different edges. - * @return + * @param edge + * the edge from the root node to the possible child nodes + * @return TRUE if there is at least one child connected by the given edge, + * otherwise FALSE + */ + public boolean hasChildren(Node edge) { + return edge2Children.get(edge) != null; + } + + /** + * @return all distinct outgoing edges. */ public SortedSet getEdges() { return edge2Children.navigableKeySet(); } /** - * Returns all outgoing different edges. - * @return + * @return all distinct outgoing edges to children of the given node type */ public SortedSet getEdges(NodeType nodeType) { - SortedSet edges = new TreeSet(new NodeComparator()); + SortedSet edges = new TreeSet<>(new NodeComparator()); for (Entry> entry : edge2Children.entrySet()) { Node edge = entry.getKey(); List children = entry.getValue(); @@ -270,12 +301,25 @@ public String getStringRepresentation(String baseIRI, PrefixMapping pm) { public String getStringRepresentation(Rendering syntax, String baseIRI, PrefixMapping pm) { return getStringRepresentation(false, syntax, baseIRI, pm); } + + /** + * Prints the query tree and shows children of resources only if enabled. + * + * @param stopIfChildIsResourceNode do not show children of nodes that are resources + * @return the query tree + */ + public String getStringRepresentation(boolean stopIfChildIsResourceNode) { + return getStringRepresentation(stopIfChildIsResourceNode, null, null, PrefixCCPrefixMapping.Full); + } /** * Prints the query tree and shows children of resources only if enabled. * - * @param stopWhenLeafNode - * @return + * @param stopIfChildIsResourceNode if a child node is not a variable, children will not be rendered + * @param syntax the syntax used for rendering + * @param baseIRI the base IRI + * @param pm the prefix mapping + * @return a rendered string representation of the tree */ public String getStringRepresentation(boolean stopIfChildIsResourceNode, Rendering syntax, String baseIRI, PrefixMapping pm) { StringBuilder sb = new StringBuilder(); @@ -399,7 +443,7 @@ private void writeObject(final ObjectOutputStream out) throws IOException { private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { child2Edge = new HashMap<>(); - edge2Children = new TreeMap>(new NodeComparator()); + edge2Children = new TreeMap<>(new NodeComparator()); // ID int id = ois.readInt(); @@ -437,4 +481,16 @@ private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IO } } + + /* (non-Javadoc) + * @see java.lang.Comparable#compareTo(java.lang.Object) + */ + @Override + public int compareTo(RDFResourceTree other) { + return ComparisonChain.start(). + compare(this.getData(), other.getData(), new NodeComparator()). // root node + compare(this.getNumberOfChildren(), other.getNumberOfChildren()). // number of direct children + compare(QueryTreeUtils.toOWLClassExpression(this), QueryTreeUtils.toOWLClassExpression(other)). // class expression representation + result(); + } } diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/rendering/Edge.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/rendering/Edge.java index f05b599764..aee4404015 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/rendering/Edge.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/rendering/Edge.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.datastructures.rendering; public class Edge { @@ -40,9 +58,7 @@ public boolean equals(Object obj) { if (getClass() != obj.getClass()) return false; Edge other = (Edge) obj; - if (id != other.id) - return false; - return true; + return id == other.id; } diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/rendering/Vertex.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/rendering/Vertex.java index 33cf0c130e..a870bc432c 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/rendering/Vertex.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/datastructures/rendering/Vertex.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.datastructures.rendering; public class Vertex { @@ -40,8 +58,6 @@ public boolean equals(Object obj) { if (getClass() != obj.getClass()) return false; Vertex other = (Vertex) obj; - if (id != other.id) - return false; - return true; + return id == other.id; } } \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/exception/EmptyLGGException.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/exception/EmptyLGGException.java index 31afcccb3e..619825c097 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/exception/EmptyLGGException.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/exception/EmptyLGGException.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.exception; public class EmptyLGGException extends QTLException { diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/exception/NBRException.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/exception/NBRException.java index d14d754510..1677d452ab 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/exception/NBRException.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/exception/NBRException.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.exception; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/exception/NegativeTreeCoverageExecption.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/exception/NegativeTreeCoverageExecption.java index df0984bff9..61880504a7 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/exception/NegativeTreeCoverageExecption.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/exception/NegativeTreeCoverageExecption.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.exception; public class NegativeTreeCoverageExecption extends QTLException { diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/exception/QTLException.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/exception/QTLException.java index 98aca5e1ea..fd4ffdb97e 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/exception/QTLException.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/exception/QTLException.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.exception; import org.dllearner.exceptions.DLLearnerException; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/exception/TimeOutException.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/exception/TimeOutException.java index eaae9802ae..6639945c0a 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/exception/TimeOutException.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/exception/TimeOutException.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.exception; public class TimeOutException extends QTLException { diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/ExactMatchFilter.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/ExactMatchFilter.java index 661b1cae4d..7ea5ff3ad5 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/ExactMatchFilter.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/ExactMatchFilter.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.filters; import java.util.Set; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/Filter.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/Filter.java index 14e254793d..9428c3ab46 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/Filter.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/Filter.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.filters; public interface Filter { diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/Filters.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/Filters.java index cf455cfee9..cae7f8a653 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/Filters.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/Filters.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.filters; import java.util.ArrayList; @@ -11,11 +29,10 @@ public class Filters { public static List getSkosFilterProperties(){ - List filters = new ArrayList(); + List filters = new ArrayList<>(); - filters.add(SKOSVocabulary.COMMENT.getIRI().toString()); +// filters.add(SKOSVocabulary.COMMENT.getIRI().toString()); filters.add(SKOSVocabulary.DEFINITION.getIRI().toString()); - filters.add(SKOSVocabulary.COMMENT.getIRI().toString()); filters.add(SKOSVocabulary.PREFLABEL.getIRI().toString()); filters.add(SKOSVocabulary.ALTLABEL.getIRI().toString()); @@ -23,7 +40,7 @@ public static List getSkosFilterProperties(){ } public static List getRDFSFilterProperties(){ - List filters = new ArrayList(); + List filters = new ArrayList<>(); filters.add(RDFS.comment.toString()); filters.add(RDFS.label.toString()); @@ -34,7 +51,7 @@ public static List getRDFSFilterProperties(){ } public static List getDBPediaFilterProperties(){ - List filters = new ArrayList(); + List filters = new ArrayList<>(); filters.add("http://dbpedia.org/property/pageId"); filters.add("http://dbpedia.org/property/revisionId"); @@ -46,7 +63,7 @@ public static List getDBPediaFilterProperties(){ } public static List getFOAFFilterProperties(){ - List filters = new ArrayList(); + List filters = new ArrayList<>(); filters.add(FOAF.page.toString()); filters.add(FOAF.homepage.toString()); @@ -63,7 +80,7 @@ public static List getFOAFFilterProperties(){ } public static List getPurlFilterProperties(){ - List filters = new ArrayList(); + List filters = new ArrayList<>(); filters.add("http://purl.org/dc/elements/1.1/language"); filters.add("http://purl.org/dc/elements/1.1/rights"); @@ -72,7 +89,7 @@ public static List getPurlFilterProperties(){ } public static List getAllFilterProperties(){ - List filters = new ArrayList(); + List filters = new ArrayList<>(); filters.addAll(Filters.getDBPediaFilterProperties()); filters.addAll(Filters.getSkosFilterProperties()); diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/I_Sub.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/I_Sub.java index 541b6bfae1..83b687c66a 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/I_Sub.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/I_Sub.java @@ -1,17 +1,20 @@ -/* Copyright 2004-2011 by the National and Technical University of Athens - - This program is free software: you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.filters; @@ -130,7 +133,7 @@ public double score(String s1, String s2, boolean normaliseStrings) { } double commonality = 0; - double scaledCommon = (double)(2*common)/(L1+L2); + double scaledCommon = 2*common /(L1+L2); commonality = scaledCommon; double winklerImprovement = winklerImprovement(inputStr1, inputStr2, commonality); diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/KeywordBasedQueryTreeFilter.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/KeywordBasedQueryTreeFilter.java index 4fc5591885..44d646b548 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/KeywordBasedQueryTreeFilter.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/KeywordBasedQueryTreeFilter.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.filters; import java.util.Collection; @@ -35,7 +53,7 @@ public KeywordBasedQueryTreeFilter(Collection questionWords){ @Override public QueryTree getFilteredQueryTree(QueryTree tree){ - QueryTree copy = new QueryTreeImpl(tree); + QueryTree copy = new QueryTreeImpl<>(tree); filterTree(copy); return copy; } @@ -77,7 +95,7 @@ private boolean areSimiliar(String s1, String s2){ } private boolean isSimlarWithSubstringMetrik(String s){ - SortedSet values = new TreeSet(Collections.reverseOrder()); + SortedSet values = new TreeSet<>(Collections.reverseOrder()); for(String word : questionWords){ double v = substringMetric.score(word, s, true); if(v >= threshold){ @@ -97,7 +115,7 @@ private boolean isSimlarWithSubstringMetrik(String s){ } private Set getTopK(SortedSet values){ - Set top = new HashSet(); + Set top = new HashSet<>(); int k = 0; for(Double v : values){ if(k == topK){ diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/KeywordBasedStatementFilter.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/KeywordBasedStatementFilter.java index 5722ebc15d..a822949c83 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/KeywordBasedStatementFilter.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/KeywordBasedStatementFilter.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.filters; import java.util.Collections; @@ -33,9 +51,9 @@ public class KeywordBasedStatementFilter extends Filter { private int topK = 3; private double topKSumThreshold = 0.8; - private Map statement2Similarity = new HashMap(); + private Map statement2Similarity = new HashMap<>(); - private Map cache = new HashMap(); + private Map cache = new HashMap<>(); int cnt = 0; @@ -63,7 +81,7 @@ private boolean areSimiliar(String s1, String s2, Statement st){ } private boolean isSimlarWithSubstringMetrik(String s){ - SortedSet values = new TreeSet(Collections.reverseOrder()); + SortedSet values = new TreeSet<>(Collections.reverseOrder()); for(String word : questionWords){ double v = substringMetric.score(word, s, true); if(v >= threshold){ @@ -83,7 +101,7 @@ private boolean isSimlarWithSubstringMetrik(String s){ } private Set getTopK(SortedSet values){ - Set top = new HashSet(); + Set top = new HashSet<>(); int k = 0; for(Double v : values){ if(k == topK){ @@ -190,9 +208,9 @@ public double getThreshold(){ } public Set getStatementsBelowThreshold(double threshold){ - Set statements = new HashSet(); + Set statements = new HashSet<>(); for(Entry entry : statement2Similarity.entrySet()){ - if(entry.getValue().doubleValue() < threshold){ + if(entry.getValue() < threshold){ statements.add(entry.getKey()); } } diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/KeywordBasedStatementFilter2.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/KeywordBasedStatementFilter2.java index f18c02c8f9..76302e6e71 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/KeywordBasedStatementFilter2.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/KeywordBasedStatementFilter2.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.filters; import java.util.Collections; @@ -33,13 +51,13 @@ public class KeywordBasedStatementFilter2 extends Filter { private int topK = 3; private double topKSumThreshold = 0.8; - private Map statement2Similarity = new HashMap(); + private Map statement2Similarity = new HashMap<>(); - private Map cache = new HashMap(); + private Map cache = new HashMap<>(); - private Map statement2TokenMap = new HashMap(); + private Map statement2TokenMap = new HashMap<>(); - private Map resource2TokenMap = new HashMap(); + private Map resource2TokenMap = new HashMap<>(); int cnt = 0; @@ -69,7 +87,7 @@ private boolean areSimiliar(String s1, String s2, Statement st){ } private boolean isSimilarWithSubstringMetrik(String s, Statement st){ - SortedSet values = new TreeSet(Collections.reverseOrder()); + SortedSet values = new TreeSet<>(Collections.reverseOrder()); for(String word : questionWords){ double v = substringMetric.score(word, s, true); if(v >= threshold){statement2TokenMap.put(st, word);resource2TokenMap.put(s, word); @@ -92,7 +110,7 @@ private boolean isSimilarWithSubstringMetrik(String s, Statement st){ } private Set getTopK(SortedSet values){ - Set top = new HashSet(); + Set top = new HashSet<>(); int k = 0; for(Double v : values){ if(k == topK){ @@ -246,9 +264,9 @@ public double getThreshold(){ } public Set getStatementsBelowThreshold(double threshold){ - Set statements = new HashSet(); + Set statements = new HashSet<>(); for(Entry entry : statement2Similarity.entrySet()){ - if(entry.getValue().doubleValue() < threshold){ + if(entry.getValue() < threshold){ statements.add(entry.getKey()); } } diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/QueryTreeFilter.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/QueryTreeFilter.java index e55ade92e8..0ecfd1a5d2 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/QueryTreeFilter.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/QueryTreeFilter.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.filters; import org.dllearner.algorithms.qtl.datastructures.QueryTree; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/QuestionBasedQueryTreeFilterAggressive.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/QuestionBasedQueryTreeFilterAggressive.java index f3b2139d63..1bde3c78ac 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/QuestionBasedQueryTreeFilterAggressive.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/QuestionBasedQueryTreeFilterAggressive.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.filters; import java.util.Collections; @@ -26,7 +44,7 @@ public class QuestionBasedQueryTreeFilterAggressive implements QueryTreeFilter{ private int topK = 3; private double topKSumThreshold = 0.8; - private Set numbers = new HashSet(); + private Set numbers = new HashSet<>(); public QuestionBasedQueryTreeFilterAggressive(Set questionWords){ this.questionWords = questionWords; @@ -42,7 +60,7 @@ public QueryTree getFilteredQueryTree(QueryTree tree){ if(tree.getChildren().isEmpty()){ return tree; } - QueryTree copy = new QueryTreeImpl(tree); + QueryTree copy = new QueryTreeImpl<>(tree); filterTree(copy); return copy; } @@ -158,7 +176,7 @@ private boolean areSimiliar(String s1, String s2){ } private boolean isSimlarWithSubstringMetrik(String s){ - SortedSet values = new TreeSet(Collections.reverseOrder()); + SortedSet values = new TreeSet<>(Collections.reverseOrder()); for(String word : questionWords){ double v = substringMetric.score(word, s, true); if(v >= threshold){ @@ -178,7 +196,7 @@ private boolean isSimlarWithSubstringMetrik(String s){ } private Set getTopK(SortedSet values){ - Set top = new HashSet(); + Set top = new HashSet<>(); int k = 0; for(Double v : values){ if(k == topK){ diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/QuestionBasedStatementSelector.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/QuestionBasedStatementSelector.java index 56f59d650a..722947b3d9 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/QuestionBasedStatementSelector.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/QuestionBasedStatementSelector.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.filters; import java.util.Set; @@ -34,11 +52,8 @@ public boolean test(Statement s) { } else if(s.getObject().isLiteral()){ object = s.getObject().asLiteral().getLexicalForm(); } - if(isSimiliar2QuestionWord(object) || isSimiliar2QuestionWord(predicate)){ - return true; - } - - return false; + return isSimiliar2QuestionWord(object) || isSimiliar2QuestionWord(predicate); + } private boolean isSimiliar2QuestionWord(String s){ diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/ZeroFilter.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/ZeroFilter.java index 38c8b997e6..f6cf6bd61f 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/ZeroFilter.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/filters/ZeroFilter.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.filters; public class ZeroFilter implements Filter{ diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/heuristics/QueryTreeEditDistance.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/heuristics/QueryTreeEditDistance.java index dcdddbabfa..9d8331409f 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/heuristics/QueryTreeEditDistance.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/heuristics/QueryTreeEditDistance.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.heuristics; import java.util.List; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/heuristics/QueryTreeHeuristic.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/heuristics/QueryTreeHeuristic.java index c961bbf5cd..4dc7146c71 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/heuristics/QueryTreeHeuristic.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/heuristics/QueryTreeHeuristic.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.heuristics; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/heuristics/QueryTreeHeuristicComplex.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/heuristics/QueryTreeHeuristicComplex.java index 27645e2b9e..90ea407d10 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/heuristics/QueryTreeHeuristicComplex.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/heuristics/QueryTreeHeuristicComplex.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ /** * */ diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/heuristics/QueryTreeHeuristicSimple.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/heuristics/QueryTreeHeuristicSimple.java index 303ce3b84f..5630800cb9 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/heuristics/QueryTreeHeuristicSimple.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/heuristics/QueryTreeHeuristicSimple.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.heuristics; @@ -52,8 +67,13 @@ public double getScore(EvaluatedRDFResourceTree tree){ double pn = fn / total; score = pp * Math.log(pp) + pn * Math.log(pn); break;} - case MATTHEWS_CORRELATION : - score = (tp * tn - fp * fn) / Math.sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn));break; + case MATTHEWS_CORRELATION : // a measure between -1 and 1 + double denominator = Math.sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn)); + if(denominator == 0) { // 0 means not better than random prediction + return 0; +// denominator = 1; + } + score = (tp * tn - fp * fn) / denominator;break; case YOUDEN_INDEX : score = tp / (tp + fn) + tn / (fp + tn) - 1;break; default: break; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/heuristics/QueryTreeInformativeness.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/heuristics/QueryTreeInformativeness.java index 61d1c570e4..69fa195c3d 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/heuristics/QueryTreeInformativeness.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/heuristics/QueryTreeInformativeness.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.heuristics; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/impl/QueryTreeFactory.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/impl/QueryTreeFactory.java index e86bfcee6d..84a11f758c 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/impl/QueryTreeFactory.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/impl/QueryTreeFactory.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.impl; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/impl/QueryTreeFactoryBase.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/impl/QueryTreeFactoryBase.java index a32a766973..4ad953e2c6 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/impl/QueryTreeFactoryBase.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/impl/QueryTreeFactoryBase.java @@ -1,8 +1,8 @@ /** - * Copyright (C) 2007-2010, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. - * + * * DL-Learner is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or @@ -15,7 +15,6 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * */ package org.dllearner.algorithms.qtl.impl; @@ -62,11 +61,11 @@ public class QueryTreeFactoryBase implements QueryTreeFactory { private int nodeId; - private Comparator comparator = new StatementComparator(); + private final Comparator comparator = new StatementComparator(); private int maxDepth = 3; - private Set> dropFilters = new HashSet>(); + private Set> dropFilters = new HashSet<>(); public QueryTreeFactoryBase() { } @@ -121,7 +120,7 @@ public void addDropFilters(Filter... dropFilters) { private RDFResourceTree createTree(Resource resource, Model model, int maxDepth) { nodeId = 0; - Map> resource2Statements = new HashMap>(); + Map> resource2Statements = new HashMap<>(); fillMap(resource, model, resource2Statements); @@ -149,7 +148,7 @@ private void fillMap(Resource s, Model model, Map SortedSet statements = resource2Statements.get(s); if (statements == null) { - statements = new TreeSet(comparator); + statements = new TreeSet<>(comparator); resource2Statements.put(s, statements); } @@ -211,7 +210,7 @@ public int compare(Statement s1, Statement s2) { public static String encode(String s) { char[] htmlChars = s.toCharArray(); - StringBuffer encodedHtml = new StringBuffer(); + StringBuilder encodedHtml = new StringBuilder(); for (int i = 0; i < htmlChars.length; i++) { switch (htmlChars[i]) { case '<': diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/impl/QueryTreeFactoryCache.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/impl/QueryTreeFactoryCache.java index 5ca4476a14..404e10e370 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/impl/QueryTreeFactoryCache.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/impl/QueryTreeFactoryCache.java @@ -1,8 +1,8 @@ /** - * Copyright (C) 2007-2010, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. - * + * * DL-Learner is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or @@ -15,7 +15,6 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * */ package org.dllearner.algorithms.qtl.impl; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/FilterVisitor.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/FilterVisitor.java index ce109c4c7f..5dbcfa4fee 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/FilterVisitor.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/FilterVisitor.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.operations; import java.util.ArrayList; @@ -13,7 +31,7 @@ public class FilterVisitor extends OpVisitorBase { - private List ops = new ArrayList(); + private List ops = new ArrayList<>(); diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/Generalisation.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/Generalisation.java index d27b78a6b5..313aced97b 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/Generalisation.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/Generalisation.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.operations; import java.util.ArrayList; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/NBR.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/NBR.java index fe279da918..6d89332cfb 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/NBR.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/NBR.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.operations; import java.sql.SQLException; @@ -20,6 +38,7 @@ import javax.xml.ws.http.HTTPException; +import com.hp.hpl.jena.graph.NodeFactory; import org.aksw.jena_sparql_api.cache.core.QueryExecutionFactoryCacheEx; import org.aksw.jena_sparql_api.cache.extra.CacheBackend; import org.aksw.jena_sparql_api.cache.extra.CacheFrontend; @@ -102,7 +121,7 @@ public NBR(SparqlEndpoint endpoint){ public NBR(SparqlEndpoint endpoint, String cacheDirectory){ this.endpoint = endpoint; - noSequences = new ArrayList>(); + noSequences = new ArrayList<>(); qef = new QueryExecutionFactoryHttp(endpoint.getURL().toString(), endpoint.getDefaultGraphURIs()); if(cacheDirectory != null){ @@ -115,7 +134,7 @@ public NBR(SparqlEndpoint endpoint, String cacheDirectory){ public NBR(Model model){ this.model = model; - noSequences = new ArrayList>(); + noSequences = new ArrayList<>(); qef = new QueryExecutionFactoryModel(model); } @@ -126,7 +145,7 @@ public void setMaxExecutionTimeInSeconds(int maxExecutionTimeInSeconds){ private Map, List> createMatrix(QueryTree tree, List> negTrees){ - Map, List> matrix = new HashMap, List>(); + Map, List> matrix = new HashMap<>(); for(int i = 0; i < negTrees.size(); i++){ checkTree(matrix, tree, negTrees.get(i), i); } @@ -134,7 +153,7 @@ private Map, List> createMatrix(QueryTree tree, List getDeterminingNodeIds(QueryTree lgg, List> trees){ - List nodeIds = new ArrayList(); + List nodeIds = new ArrayList<>(); boolean parentIsResource = false; boolean childIsResource = false; @@ -157,16 +176,16 @@ public String getQuery(){ } private List> getLeafsOrderedByRowSum(QueryTree tree, Map, List> matrix){ - List> leafs = new ArrayList>(); + List> leafs = new ArrayList<>(); - SortedMap>> map = new TreeMap>>(); + SortedMap>> map = new TreeMap<>(); int rowSum; List> treeList; for(Entry, List> entry : matrix.entrySet()){ rowSum = sum(entry.getValue()); treeList = map.get(rowSum); if(treeList == null){ - treeList = new ArrayList>(); + treeList = new ArrayList<>(); map.put(rowSum, treeList); } treeList.add(entry.getKey()); @@ -225,7 +244,7 @@ private void checkTree(Map, List> matrix, QueryTree pos private void setMatrixEntry(Map, List> matrix, QueryTree row, int column, int entry){ List list = matrix.get(row); if(list == null){ - list = new ArrayList(); + list = new ArrayList<>(); matrix.put(row, list); } try { @@ -256,7 +275,7 @@ private String printTreeWithValues(QueryTree tree, Map, List child : tree.getChildren()) { for (int i = 0; i < depth; i++) { @@ -300,9 +319,9 @@ private String computeQuestionBetterPerformance(QueryTree lgg, List postGen; if(endpoint != null){ - postGen = new PostLGG(endpoint); + postGen = new PostLGG<>(endpoint); } else { - postGen = new PostLGG(); + postGen = new PostLGG<>(); } postGen.simplifyTree(postLGG, negTrees); @@ -323,9 +342,9 @@ private String computeQuestionBetterPerformance(QueryTree lgg, List> queue = null; if(generalizeSortedByNegatives){ - queue = getAllowedGeneralisationsSortedByMatrix(new GeneralisedQueryTree(postLGG), negTrees); + queue = getAllowedGeneralisationsSortedByMatrix(new GeneralisedQueryTree<>(postLGG), negTrees); } else { - queue = getAllowedGeneralisationsSorted2(new GeneralisedQueryTree(postLGG)); + queue = getAllowedGeneralisationsSorted2(new GeneralisedQueryTree<>(postLGG)); } logger.debug(getQueueLogInfo(queue)); @@ -335,7 +354,7 @@ private String computeQuestionBetterPerformance(QueryTree lgg, List> gens; List> neededGeneralisations; while(!queue.isEmpty()){ - neededGeneralisations = new ArrayList>(); + neededGeneralisations = new ArrayList<>(); logger.debug("Selecting first tree from queue"); // tree1 = queue.remove(0); tree1 = getGeneralisedQueryTreeNotContainingNoSequence(queue); @@ -396,11 +415,11 @@ private String computeQuestionBetterPerformance(QueryTree lgg, List firstChanges = new ArrayList(neededGeneralisations.get(0).getChanges()); + List firstChanges = new ArrayList<>(neededGeneralisations.get(0).getChanges()); while(firstChanges.size() > 1){ firstChanges.remove(firstChanges.size()-1); - neededGeneralisations.add(0, new GeneralisedQueryTree(getTreeByChanges(lgg, firstChanges), firstChanges)); - firstChanges = new ArrayList(firstChanges); + neededGeneralisations.add(0, new GeneralisedQueryTree<>(getTreeByChanges(lgg, firstChanges), firstChanges)); + firstChanges = new ArrayList<>(firstChanges); } newResource = findMostSpecificResourceTree2(neededGeneralisations, knownResources, 0, neededGeneralisations.size()-1); logger.debug("binary search for most specific query returning a resource - completed"); @@ -439,7 +458,7 @@ private boolean userAnsweredWithNo(){ } private SortedSet getAllResources(String query){ - SortedSet resources = new TreeSet(); + SortedSet resources = new TreeSet<>(); query = query + " LIMIT 1000"; QueryExecution qe = qef.createQueryExecution(query); @@ -492,13 +511,13 @@ private String findMostSpecificResourceTree2(List> trees public List> getAllowedGeneralisations(GeneralisedQueryTree tree){ logger.debug("Computing allowed generalisations..."); - List> gens = new LinkedList>(); + List> gens = new LinkedList<>(); gens.addAll(computeAllowedGeneralisations(tree, tree.getLastChange())); return gens; } private List> getPossibleNodes2Change(QueryTree tree){ - List> nodes = new ArrayList>(); + List> nodes = new ArrayList<>(); for(QueryTree child : tree.getChildren()){ if(child.isLeaf()){ nodes.add(child); @@ -521,7 +540,7 @@ private List> getAllowedGeneralisationsSortedByMatrix(Ge for(Entry, List> entry : matrix.entrySet()){ logger.debug(entry.getKey().getId() + ": " + entry.getValue()); } - List> gens = new ArrayList>(); + List> gens = new ArrayList<>(); if(logger.isDebugEnabled()){ String treeString; if(endpoint instanceof SPARQLEndpointEx){ @@ -534,7 +553,7 @@ private List> getAllowedGeneralisationsSortedByMatrix(Ge } - Map, Integer> genTree2Sum = new HashMap, Integer>(); + Map, Integer> genTree2Sum = new HashMap<>(); QueryTree queryTree = tree.getQueryTree(); QueryTreeChange lastChange = tree.getLastChange(); @@ -552,7 +571,7 @@ private List> getAllowedGeneralisationsSortedByMatrix(Ge if(lastChange.getType() == ChangeType.REMOVE_NODE){ if(node.getUserObject().equals("?") && node.getId() < lastChange.getNodeId()){ int pos = parent.removeChild((QueryTreeImpl) node); - genTree = new GeneralisedQueryTree(new QueryTreeImpl(queryTree)); + genTree = new GeneralisedQueryTree<>(new QueryTreeImpl<>(queryTree)); genTree.addChanges(changes); genTree.addChange(new QueryTreeChange(node.getId(), ChangeType.REMOVE_NODE)); genTree2Sum.put(genTree, sum(matrix.get(node))); @@ -561,7 +580,7 @@ private List> getAllowedGeneralisationsSortedByMatrix(Ge } else { if(node.getUserObject().equals("?")){ int pos = parent.removeChild((QueryTreeImpl) node); - genTree = new GeneralisedQueryTree(new QueryTreeImpl(queryTree)); + genTree = new GeneralisedQueryTree<>(new QueryTreeImpl<>(queryTree)); genTree.addChanges(changes); genTree.addChange(new QueryTreeChange(node.getId(), ChangeType.REMOVE_NODE)); genTree2Sum.put(genTree, sum(matrix.get(node))); @@ -569,7 +588,7 @@ private List> getAllowedGeneralisationsSortedByMatrix(Ge } else if(lastChange.getNodeId() < node.getId()){ node.setUserObject((N) "?"); node.setVarNode(true); - genTree = new GeneralisedQueryTree(new QueryTreeImpl(queryTree)); + genTree = new GeneralisedQueryTree<>(new QueryTreeImpl<>(queryTree)); genTree.addChanges(changes); genTree.addChange(new QueryTreeChange(node.getId(), ChangeType.REPLACE_LABEL)); genTree2Sum.put(genTree, sum(matrix.get(node))); @@ -579,7 +598,7 @@ private List> getAllowedGeneralisationsSortedByMatrix(Ge } } } - List, Integer>> entries = new ArrayList,Integer>>(genTree2Sum.entrySet()); + List, Integer>> entries = new ArrayList<>(genTree2Sum.entrySet()); Collections.sort(entries, new NegativeTreeOccurenceComparator()); for(Entry, Integer> entry : entries){ gens.add(entry.getKey()); @@ -610,7 +629,7 @@ private List> getAllowedGeneralisationsSorted2(Generalis * and a leaf node, it can be removed. */ private List> computeAllowedGeneralisations(GeneralisedQueryTree tree, QueryTreeChange lastChange){ - List> gens = new LinkedList>(); + List> gens = new LinkedList<>(); QueryTree queryTree = tree.getQueryTree(); List changes = tree.getChanges(); @@ -632,13 +651,13 @@ private List> computeAllowedGeneralisations(GeneralisedQ } if(parent.getChildren(edge).size() >= 2){ int pos = parent.removeChild((QueryTreeImpl) child); - genTree = new GeneralisedQueryTree(new QueryTreeImpl(queryTree)); + genTree = new GeneralisedQueryTree<>(new QueryTreeImpl<>(queryTree)); genTree.addChanges(changes); genTree.addChange(new QueryTreeChange(child.getId(), ChangeType.REPLACE_LABEL)); gens.add(genTree); parent.addChild((QueryTreeImpl) child, edge, pos); } else { - Map node2Label = new HashMap(); + Map node2Label = new HashMap<>(); for(QueryTree c : child.getChildren()){ if(determiningNodeIds.contains(c.getId())){ node2Label.put(Integer.valueOf(c.getId()), c.getUserObject()); @@ -647,7 +666,7 @@ private List> computeAllowedGeneralisations(GeneralisedQ } child.setUserObject((N) "?"); child.setVarNode(true); - genTree = new GeneralisedQueryTree(new QueryTreeImpl(queryTree)); + genTree = new GeneralisedQueryTree<>(new QueryTreeImpl<>(queryTree)); genTree.addChanges(changes); genTree.addChange(new QueryTreeChange(child.getId(), ChangeType.REPLACE_LABEL)); gens.add(genTree); @@ -678,16 +697,16 @@ private List> computeAllowedGeneralisations(GeneralisedQ continue; } int pos = parent.removeChild((QueryTreeImpl) child); - genTree = new GeneralisedQueryTree(new QueryTreeImpl(queryTree)); + genTree = new GeneralisedQueryTree<>(new QueryTreeImpl<>(queryTree)); genTree.addChanges(changes); genTree.addChange(new QueryTreeChange(child.getId(), ChangeType.REMOVE_NODE)); gens.add(genTree); parent.addChild((QueryTreeImpl) child, edge, pos); } else { int pos = parent.removeChild((QueryTreeImpl) child); - for(GeneralisedQueryTree subTree : computeAllowedGeneralisations(new GeneralisedQueryTree(child), tree.getLastChange())){ + for(GeneralisedQueryTree subTree : computeAllowedGeneralisations(new GeneralisedQueryTree<>(child), tree.getLastChange())){ parent.addChild((QueryTreeImpl) subTree.getQueryTree(), edge, pos); - genTree = new GeneralisedQueryTree(new QueryTreeImpl(queryTree)); + genTree = new GeneralisedQueryTree<>(new QueryTreeImpl<>(queryTree)); genTree.addChanges(changes); genTree.addChanges(subTree.getChanges()); // System.out.println(genTree.getChanges()); @@ -709,7 +728,7 @@ private boolean hasAlwaysSameParent(QueryTree node, List> trees) List path = getPathFromRootToNode(node); List> nodes; for(QueryTree tree : trees){ - nodes = getNodesByPath(tree, new ArrayList(path)); + nodes = getNodesByPath(tree, new ArrayList<>(path)); if(!nodes.isEmpty()){ for(QueryTree otherNode : nodes){ if(nodeLabel.equals(otherNode.getUserObject()) && !otherNode.getParent().getUserObject().equals(parentLabel)){ @@ -722,19 +741,19 @@ private boolean hasAlwaysSameParent(QueryTree node, List> trees) } private List> getNodesByPath(QueryTree tree, List path){ - List> nodes = new ArrayList>(); + List> nodes = new ArrayList<>(); for(QueryTree child : tree.getChildren(path.remove(0))){ if(path.isEmpty()){ nodes.add(child); } else { - nodes.addAll(getNodesByPath(child, new ArrayList(path))); + nodes.addAll(getNodesByPath(child, new ArrayList<>(path))); } } return nodes; } private List getPathFromRootToNode(QueryTree node){ - List path = new ArrayList(); + List path = new ArrayList<>(); QueryTree parent = node.getParent(); path.add(parent.getEdge(node)); if(!parent.isRoot()){ @@ -745,7 +764,7 @@ private List getPathFromRootToNode(QueryTree node){ } private SortedSet getResources(String query, int limit, int offset){ - SortedSet resources = new TreeSet(); + SortedSet resources = new TreeSet<>(); this.query = query; if(logger.isDebugEnabled()){ logger.debug("Testing query\n" + getLimitedQuery(query, limit, offset) + "\n"); @@ -806,7 +825,7 @@ private QueryTree getFilteredTree(QueryTree tree){ } private QueryTree createFilteredTree(QueryTree tree){ - QueryTree filteredTree = new QueryTreeImpl(tree.getUserObject()); + QueryTree filteredTree = new QueryTreeImpl<>(tree.getUserObject()); filteredTree.setId(nodeId); QueryTree subTree; Object predicate; @@ -960,7 +979,7 @@ private void limitEqualEdgesToLeafs(QueryTree tree, int maxEqualEdgeCount){ if(tree.getChildren().isEmpty()){ return; } - Set> parents = new HashSet>(); + Set> parents = new HashSet<>(); for(QueryTree leaf : tree.getLeafs()){ if(leaf.getUserObject().equals("?")){ parents.add(leaf.getParent()); @@ -1012,11 +1031,11 @@ private boolean isTerminationCriteriaReached(){ private String fSparql(QueryTree tree, List changes){ logger.debug("fSparql uses:" + changes); - QueryTree copy = new QueryTreeImpl(tree); + QueryTree copy = new QueryTreeImpl<>(tree); StringBuilder query = new StringBuilder(); StringBuilder triples = new StringBuilder(); - List optionals = new ArrayList(); - List filters = new ArrayList(); + List optionals = new ArrayList<>(); + List filters = new ArrayList<>(); query.append("SELECT DISTINCT ?x0 WHERE{\n"); buildSPARQLQueryString(copy, changes, triples, optionals, filters); if(triples.toString().isEmpty()){ @@ -1024,7 +1043,7 @@ private String fSparql(QueryTree tree, List changes){ } query.append(triples.toString()); for(String optional : optionals){ - query.append("OPTIONAL{").append(optional + "}\n"); + query.append("OPTIONAL{").append(optional).append("}\n"); } if(filters.size() > 0){ query.append("FILTER("); @@ -1044,12 +1063,12 @@ private String fSparql(QueryTree tree, List changes){ private String fSparql2(QueryTree tree, List changes){ logger.debug("fSparql uses:" + changes);//getSPARQLQueries(tree, changes); - QueryTree copy = new QueryTreeImpl(tree); + QueryTree copy = new QueryTreeImpl<>(tree); StringBuilder query = new StringBuilder(); StringBuilder triples = new StringBuilder(); - List optionals = new ArrayList(); - Map filters = new HashMap(); - List bounds = new ArrayList(); + List optionals = new ArrayList<>(); + Map filters = new HashMap<>(); + List bounds = new ArrayList<>(); query.append("SELECT DISTINCT ?x0 WHERE{\n"); buildSPARQLQueryString2(copy, changes, triples, optionals, filters, bounds); if(triples.toString().isEmpty()){ @@ -1057,9 +1076,9 @@ private String fSparql2(QueryTree tree, List changes){ } query.append(triples.toString()); for(String optional : optionals){ - query.append("OPTIONAL{").append(optional + "}\n"); + query.append("OPTIONAL{").append(optional).append("}\n"); } - List filterParts = new ArrayList(); + List filterParts = new ArrayList<>(); filterParts.addAll(filters.keySet()); filterParts.addAll(bounds); if(filterParts.size() > 0){ @@ -1085,7 +1104,7 @@ private String fSparql2(QueryTree tree, List changes){ } for(String f : filters.keySet()){ if(!filterParts.get(i).equals(f)){ - query.append(f + "=" + filters.get(f)); + query.append(f).append("=").append(filters.get(f)); if(cnt < filters.keySet().size()){ query.append(" && "); } @@ -1232,7 +1251,7 @@ private void buildSPARQLQueryString2(QueryTree tree, List ch } private List getSPARQLQueries(QueryTree tree, List changes){ - List queries = new ArrayList(); + List queries = new ArrayList<>(); String originalQuery = tree.toSPARQLQueryString(); @@ -1303,7 +1322,7 @@ private String getSPARQLQuery(String queryString, int nodeId, String label){ if(current.getObject().toString().equals(label)){ node = current.getObject(); position = ((ElementTriplesBlock) el).getPattern().getList().indexOf(current); - add = Triple.create(current.getSubject(), current.getPredicate(), Node.createVariable("x" + nodeId)); + add = Triple.create(current.getSubject(), current.getPredicate(), NodeFactory.createVariable("x" + nodeId)); iter.remove(); } } @@ -1375,7 +1394,7 @@ private String getSPARQLQuery(String queryString, int nodeId, String label){ // } private QueryTree getTreeByChanges(QueryTree originalTree, List changes){ - QueryTree copy = new QueryTreeImpl(originalTree); + QueryTree copy = new QueryTreeImpl<>(originalTree); QueryTree node; for(QueryTreeChange change : changes){ node = copy.getNodeById(change.getNodeId()); diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/PostLGG.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/PostLGG.java index 1a0af41110..86c882aa7a 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/PostLGG.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/PostLGG.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.operations; import java.util.ArrayList; @@ -66,7 +84,7 @@ public void simplifyTree(QueryTree tree, List> negTrees){ // if(leaf.getParent().getUserObject().equals("?")){ pathExists = true; for(QueryTree negTree : negTrees){ - if(!pathExists(leaf, new ArrayList(path), negTree)){ + if(!pathExists(leaf, new ArrayList<>(path), negTree)){ pathExists = false; break; } @@ -96,7 +114,7 @@ private void checkSameEdgeOccurences(QueryTree tree, List> negTr N label1; N label2; QueryTree parent; - Set removedNodesIds = new HashSet(); + Set removedNodesIds = new HashSet<>(); for(QueryTree leaf : tree.getLeafs()){ if(!removedNodesIds.contains(leaf.getId())){ parent = leaf.getParent(); @@ -110,7 +128,7 @@ private void checkSameEdgeOccurences(QueryTree tree, List> negTr boolean remove = false; for(QueryTree negTree : negTrees){ - int ret = containsEdgeCombination(negTree, new ArrayList(path1), new ArrayList(path2), label1, label2); + int ret = containsEdgeCombination(negTree, new ArrayList<>(path1), new ArrayList<>(path2), label1, label2); if(ret == -1){ remove = false; break; @@ -232,7 +250,7 @@ private boolean pathExists(QueryTree leaf, List path, QueryTree tr } private List getPathFromRootToNode(QueryTree node){ - List path = new ArrayList(); + List path = new ArrayList<>(); QueryTree parent = node.getParent(); path.add(parent.getEdge(node)); if(!parent.isRoot()){ @@ -246,7 +264,7 @@ private List> getNodesByPath(QueryTree tree, List path){ if(path.isEmpty()){ return Collections.singletonList(tree); } - List> nodes = new ArrayList>(); + List> nodes = new ArrayList<>(); Object edge = path.remove(0); for(QueryTree child : tree.getChildren(edge)){ if(path.isEmpty()){ @@ -272,7 +290,7 @@ private boolean existsPath(List path, QueryTree tree){ Object edge = path.remove(0); if(!path.isEmpty()){ for(QueryTree child : tree.getChildren(edge)){ - if(existsPath(new ArrayList(path), child)){ + if(existsPath(new ArrayList<>(path), child)){ return true; } } diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/lcs/LCS.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/lcs/LCS.java index 503dd77884..14245e84f5 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/lcs/LCS.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/lcs/LCS.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.operations.lcs; @@ -26,7 +41,7 @@ */ public class LCS { - private Map, RootedRDFGraph> cache = new HashMap, LCS.RootedRDFGraph>(); + private Map, RootedRDFGraph> cache = new HashMap<>(); public RootedRDFGraph computeLCS(RootedRDFGraph g1, RootedRDFGraph g2) throws LCSException{ @@ -45,7 +60,7 @@ public RootedRDFGraph computeLCS(RootedRDFGraph g1, RootedRDFGraph g2) throws LC x = NodeFactory.createAnon(); // a new set of triples - Set triples = new HashSet(); + Set triples = new HashSet<>(); // add to result to avoid recomputation RootedRDFGraph g = new RootedRDFGraph(x, triples); @@ -69,7 +84,7 @@ public RootedRDFGraph computeLCS(RootedRDFGraph g1, RootedRDFGraph g2) throws LC } private Set connectedTriples(Node node, Set triples) { - Set connectedTriples = new HashSet(); + Set connectedTriples = new HashSet<>(); for (Triple triple : triples) { if(isRDFConnected(node, triple, triples)) { @@ -79,13 +94,15 @@ private Set connectedTriples(Node node, Set triples) { return connectedTriples; } - + /** * Check if there is an RDF-path from source to target. - * @param source the source node - * @param target the target node + * + * @param source the source node + * @param target the target node * @param triples the set of triples in the graph - * @return + * @return whether both nodes are RDF-connected by the given set of triples, i.e. if there is an RDF-path from + * source to target. */ public static boolean isRDFConnected(Node source, Node target, Set triples) { // trivial case: node is always RDF-connected to itself @@ -142,7 +159,7 @@ public Set getTriples() { */ @Override public String toString() { - return super.toString(); + return root + "" + triples; } } diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/lgg/AbstractLGGGenerator.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/lgg/AbstractLGGGenerator.java index 9afd6808b3..14ff8e0c9d 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/lgg/AbstractLGGGenerator.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/lgg/AbstractLGGGenerator.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.operations.lgg; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/lgg/EvaluatedQueryTree.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/lgg/EvaluatedQueryTree.java index 7290509d53..8e389cd9cc 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/lgg/EvaluatedQueryTree.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/lgg/EvaluatedQueryTree.java @@ -1,18 +1,34 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.operations.lgg; +import com.google.common.collect.ComparisonChain; import gnu.trove.set.TIntSet; import gnu.trove.set.hash.TIntHashSet; - -import java.util.Collection; -import java.util.HashSet; -import java.util.Set; - import org.dllearner.algorithms.qtl.datastructures.QueryTree; import org.dllearner.algorithms.qtl.datastructures.impl.QueryTreeImpl.LiteralNodeConversionStrategy; import org.dllearner.core.EvaluatedDescription; import org.dllearner.learningproblems.QueryTreeScore; -import com.google.common.collect.ComparisonChain; +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; public class EvaluatedQueryTree implements Comparable>{ @@ -144,7 +160,7 @@ public EvaluatedDescription getEvaluatedDescription() { return asEvaluatedDescription(); } /** - * @param OWLClassExpression the OWLClassExpression to set + * @param description the description to set */ public void setDescription(EvaluatedDescription description) { this.description = description; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/lgg/LGGGenerator.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/lgg/LGGGenerator.java index 8e3ce4639e..40f14f1b80 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/lgg/LGGGenerator.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/lgg/LGGGenerator.java @@ -1,8 +1,8 @@ /** - * Copyright (C) 2007-2010, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. - * + * * DL-Learner is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or @@ -15,7 +15,6 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * */ package org.dllearner.algorithms.qtl.operations.lgg; @@ -31,35 +30,35 @@ public interface LGGGenerator { /** - * Returns the Least General Generalization of 2 RDF resource trees. - * @param tree1 - * @param tree2 - * @return + * Returns the Least General Generalization of two RDF resource trees. + * @param tree1 the first tree + * @param tree2 the second tree + * @return the Least General Generalization */ RDFResourceTree getLGG(RDFResourceTree tree1, RDFResourceTree tree2); /** - * Returns the Least General Generalization of 2 RDF resource trees. - * @param tree1 - * @param tree2 - * @return + * Returns the Least General Generalization of two RDF resource trees. + * @param tree1 the first tree + * @param tree2 the second tree + * @return the Least General Generalization */ RDFResourceTree getLGG(RDFResourceTree tree1, RDFResourceTree tree2, boolean learnFilters); /** - * Returns the Least General Generalization of 2 RDF resource trees. - * @param tree1 - * @param tree2 - * @return + * Returns the Least General Generalization of a list of RDF resource trees. + * @param trees the trees + * @return the Least General Generalization */ RDFResourceTree getLGG(List trees); /** - * Returns the Least General Generalization of 2 RDF resource trees. - * @param tree1 - * @param tree2 - * @return - */ + * Returns the Least General Generalization of a list of RDF resource trees. It can be forced to learn filters + * on literal values. + * + * @param trees the trees + * @return the Least General Generalization + *///todo add better explanation RDFResourceTree getLGG(List trees, boolean learnFilters); } diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/lgg/LGGGeneratorRDFS.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/lgg/LGGGeneratorRDFS.java index 427b584462..c526e73c1e 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/lgg/LGGGeneratorRDFS.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/lgg/LGGGeneratorRDFS.java @@ -1,8 +1,8 @@ /** - * Copyright (C) 2007-2010, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. - * + * * DL-Learner is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or @@ -15,7 +15,6 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * */ package org.dllearner.algorithms.qtl.operations.lgg; @@ -29,8 +28,8 @@ import java.util.concurrent.TimeUnit; import org.aksw.jena_sparql_api.cache.h2.CacheUtilsH2; +import org.aksw.jena_sparql_api.core.FluentQueryExecutionFactory; import org.aksw.jena_sparql_api.core.QueryExecutionFactory; -import org.aksw.jena_sparql_api.core.SparqlServiceBuilder; import org.dllearner.algorithms.qtl.QueryTreeUtils; import org.dllearner.algorithms.qtl.datastructures.impl.RDFResourceTree; import org.dllearner.algorithms.qtl.impl.QueryTreeFactory; @@ -48,15 +47,14 @@ import org.dllearner.kb.sparql.ConciseBoundedDescriptionGeneratorImpl; import org.dllearner.kb.sparql.SparqlEndpoint; import org.dllearner.reasoning.SPARQLReasoner; +import org.dllearner.core.StringRenderer; +import org.dllearner.core.StringRenderer.Rendering; +import org.dllearner.utilities.OwlApiJenaUtils; import org.semanticweb.owlapi.io.ToStringRenderer; +import org.semanticweb.owlapi.model.EntityType; import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLObjectProperty; import org.semanticweb.owlapi.model.OWLProperty; -import uk.ac.manchester.cs.owl.owlapi.OWLDataPropertyImpl; -import uk.ac.manchester.cs.owl.owlapi.OWLObjectPropertyImpl; -import uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntaxObjectRenderer; - import com.google.common.collect.HashMultimap; import com.google.common.collect.Lists; import com.google.common.collect.Multimap; @@ -71,13 +69,19 @@ import com.hp.hpl.jena.vocabulary.RDF; import com.hp.hpl.jena.vocabulary.RDFS; +import uk.ac.manchester.cs.owl.owlapi.OWLDataPropertyImpl; +import uk.ac.manchester.cs.owl.owlapi.OWLObjectPropertyImpl; + /** - * + * An LGG generator with RDFS entailment enabled. * @author Lorenz Bühmann * */ public class LGGGeneratorRDFS extends AbstractLGGGenerator { - + + /** + * @param reasoner the underlying reasoner used for RDFS entailment + */ public LGGGeneratorRDFS(AbstractReasonerComponent reasoner) { this.reasoner = reasoner; this.entailment = Entailment.RDFS; @@ -115,21 +119,45 @@ protected RDFResourceTree computeLGG(RDFResourceTree tree1, RDFResourceTree tree // get edges of tree 2 connected via subsumption Multimap relatedEdges = getRelatedEdges(tree1, tree2); for (Entry> entry : relatedEdges.asMap().entrySet()){ - Node edge1 = entry.getKey(); + Node edge1 = entry.getKey();//System.out.println("e1:" + edge1); Collection edges2 = entry.getValue(); - Set addedChildren = new HashSet(); - - + Set addedChildren = new HashSet<>(); + // loop over children of first tree - for(RDFResourceTree child1 : tree1.getChildren(edge1)){ + for(RDFResourceTree child1 : tree1.getChildren(edge1)){//System.out.println("c1:" + child1); // for all related edges of tree 2 - for (Node edge2 : edges2) { + for (Node edge2 : edges2) {//System.out.println("e2:" + edge2); // loop over children of second tree - for(RDFResourceTree child2 : tree2.getChildren(edge2)){ - // compute the LGG - RDFResourceTree lggChild = computeLGG(child1, child2, learnFilters); - + for(RDFResourceTree child2 : tree2.getChildren(edge2)){//System.out.println("c2:" + child2); + RDFResourceTree lggChild; + + // special case: rdf:type relation + if(edge1.equals(RDF.type.asNode())) { + if(QueryTreeUtils.isSubsumedBy(child1, child2, Entailment.RDFS)) { + lggChild = child2; + } else if(QueryTreeUtils.isSubsumedBy(child2, child1, Entailment.RDFS)) { + lggChild = child1; + } else { + lggChild = computeLGG(child1, child2, learnFilters); + } + } else { + // compute the LGG + lggChild = computeLGG(child1, child2, learnFilters); + + Node moreGeneralEdge; + // get the more general edge + if (reasoner.isSubPropertyOf( + OwlApiJenaUtils.asOWLEntity(edge1, EntityType.OBJECT_PROPERTY), + OwlApiJenaUtils.asOWLEntity(edge2, EntityType.OBJECT_PROPERTY))) { + + moreGeneralEdge = edge2; + } else { + moreGeneralEdge = edge1; + } +// System.out.println("e_gen:" + moreGeneralEdge); + } + // check if there was already a more specific child computed before // and if so don't add the current one boolean add = true; @@ -146,12 +174,23 @@ protected RDFResourceTree computeLGG(RDFResourceTree tree1, RDFResourceTree tree // logger.trace("Removing child node: {} is subsumed by previously added child {}.", // lggChild.getStringRepresentation(), // addedChild.getStringRepresentation()); - lgg.removeChild(addedChild, edge1); + lgg.removeChild(addedChild, lgg.getEdgeToChild(addedChild)); it.remove(); } } if(add){ - lgg.addChild(lggChild, edge1); + Node edge; + // get the more general edge + if (reasoner.isSubPropertyOf( + OwlApiJenaUtils.asOWLEntity(edge1, EntityType.OBJECT_PROPERTY), + OwlApiJenaUtils.asOWLEntity(edge2, EntityType.OBJECT_PROPERTY))) { + + edge = edge2; + } else { + edge = edge1; + } + + lgg.addChild(lggChild, edge); addedChildren.add(lggChild); // logger.trace("Adding child {}", lggChild.getStringRepresentation()); } @@ -174,10 +213,13 @@ private Multimap getRelatedEdges(RDFResourceTree tree1, RDFResourceT if(tree2.getEdges().contains(edge1)) { relatedEdges.put(edge1, edge1); } - // check if it's not a built-in properties - if (!edge1.getNameSpace().equals(RDF.getURI()) - && !edge1.getNameSpace().equals(RDFS.getURI()) - && !edge1.getNameSpace().equals(OWL.getURI())) { + + // check if it's a built-in property + boolean builtIn = edge1.getNameSpace().equals(RDF.getURI()) + && edge1.getNameSpace().equals(RDFS.getURI()) + && edge1.getNameSpace().equals(OWL.getURI()); + + if (!builtIn) { // get related edges by subsumption OWLProperty prop; @@ -205,13 +247,13 @@ private Multimap getRelatedEdges(RDFResourceTree tree1, RDFResourceT } public static void main(String[] args) throws Exception { - ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); + StringRenderer.setRenderer(Rendering.DL_SYNTAX); // knowledge base SparqlEndpoint endpoint = SparqlEndpoint.getEndpointDBpedia(); - QueryExecutionFactory qef = SparqlServiceBuilder - .http(endpoint.getURL().toString(), endpoint.getDefaultGraphURIs()) + QueryExecutionFactory qef = FluentQueryExecutionFactory + .http(endpoint.getURL().toString(), endpoint.getDefaultGraphURIs()).config() .withCache(CacheUtilsH2.createCacheFrontend("/tmp/cache", false, TimeUnit.DAYS.toMillis(60))) - .withPagination(10000).withDelay(50, TimeUnit.MILLISECONDS).create(); + .withPagination(10000).withDelay(50, TimeUnit.MILLISECONDS).end().create(); // tree generation ConciseBoundedDescriptionGenerator cbdGenerator = new ConciseBoundedDescriptionGeneratorImpl(qef); @@ -230,7 +272,7 @@ public static void main(String[] args) throws Exception { new NamespaceDropStatementFilter(Sets.newHashSet("http://dbpedia.org/property/", "http://purl.org/dc/terms/", "http://dbpedia.org/class/yago/", "http://www.w3.org/2003/01/geo/wgs84_pos#", "http://www.georss.org/georss/", FOAF.getURI()))); - List trees = new ArrayList(); + List trees = new ArrayList<>(); List resources = Lists.newArrayList("http://dbpedia.org/resource/Leipzig", "http://dbpedia.org/resource/Dresden"); for (String resource : resources) { diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/lgg/LGGGeneratorSimple.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/lgg/LGGGeneratorSimple.java index 8e965fee5d..e422e1a656 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/lgg/LGGGeneratorSimple.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/lgg/LGGGeneratorSimple.java @@ -1,8 +1,8 @@ /** - * Copyright (C) 2007-2010, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. - * + * * DL-Learner is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or @@ -15,7 +15,6 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * */ package org.dllearner.algorithms.qtl.operations.lgg; @@ -27,8 +26,8 @@ import java.util.concurrent.TimeUnit; import org.aksw.jena_sparql_api.cache.h2.CacheUtilsH2; +import org.aksw.jena_sparql_api.core.FluentQueryExecutionFactory; import org.aksw.jena_sparql_api.core.QueryExecutionFactory; -import org.aksw.jena_sparql_api.core.SparqlServiceBuilder; import org.dllearner.algorithms.qtl.QueryTreeUtils; import org.dllearner.algorithms.qtl.datastructures.impl.RDFResourceTree; import org.dllearner.algorithms.qtl.impl.QueryTreeFactory; @@ -52,7 +51,9 @@ /** - * + * An LGG generator based on syntax and structure only, i.e. without taking into account any type of + * Semantics. + * * @author Lorenz Bühmann * */ @@ -84,7 +85,7 @@ protected RDFResourceTree computeLGG(RDFResourceTree tree1, RDFResourceTree tree // 2. compare the edges // we only have to compare edges contained in both trees for(Node edge : Sets.intersection(tree1.getEdges(), tree2.getEdges())){ - Set addedChildren = new HashSet(); + Set addedChildren = new HashSet<>(); // loop over children of first tree for(RDFResourceTree child1 : tree1.getChildren(edge)){ // loop over children of second tree @@ -127,10 +128,10 @@ protected RDFResourceTree computeLGG(RDFResourceTree tree1, RDFResourceTree tree public static void main(String[] args) throws Exception { // knowledge base SparqlEndpoint endpoint = SparqlEndpoint.getEndpointDBpedia(); - QueryExecutionFactory qef = SparqlServiceBuilder - .http(endpoint.getURL().toString(), endpoint.getDefaultGraphURIs()) + QueryExecutionFactory qef = FluentQueryExecutionFactory + .http(endpoint.getURL().toString(), endpoint.getDefaultGraphURIs()).config() .withCache(CacheUtilsH2.createCacheFrontend("/tmp/cache", false, TimeUnit.DAYS.toMillis(60))) - .withPagination(10000).withDelay(50, TimeUnit.MILLISECONDS).create(); + .withPagination(10000).withDelay(50, TimeUnit.MILLISECONDS).end().create(); // tree generation ConciseBoundedDescriptionGenerator cbdGenerator = new ConciseBoundedDescriptionGeneratorImpl(qef); @@ -154,7 +155,7 @@ public static void main(String[] args) throws Exception { ) ) ); - List trees = new ArrayList(); + List trees = new ArrayList<>(); List resources = Lists.newArrayList("http://dbpedia.org/resource/Leipzig", "http://dbpedia.org/resource/Dresden"); for(String resource : resources){ try { diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/nbr/NBRGenerator.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/nbr/NBRGenerator.java index 589d36a50d..e8691820b8 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/nbr/NBRGenerator.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/nbr/NBRGenerator.java @@ -1,8 +1,8 @@ /** - * Copyright (C) 2007-2010, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. - * + * * DL-Learner is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or @@ -15,7 +15,6 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * */ package org.dllearner.algorithms.qtl.operations.nbr; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/nbr/NBRGeneratorImpl.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/nbr/NBRGeneratorImpl.java index d33dee5eac..7738cec8d6 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/nbr/NBRGeneratorImpl.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/nbr/NBRGeneratorImpl.java @@ -1,8 +1,8 @@ /** - * Copyright (C) 2007-2010, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. - * + * * DL-Learner is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or @@ -15,7 +15,6 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * */ package org.dllearner.algorithms.qtl.operations.nbr; @@ -30,7 +29,6 @@ /** * * @author Lorenz Bühmann - * @param * */ public class NBRGeneratorImpl implements NBRGenerator{ diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/nbr/strategy/BruteForceNBRStrategy.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/nbr/strategy/BruteForceNBRStrategy.java index 96b43cc684..76e1e06650 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/nbr/strategy/BruteForceNBRStrategy.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/nbr/strategy/BruteForceNBRStrategy.java @@ -1,8 +1,8 @@ /** - * Copyright (C) 2007-2010, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. - * + * * DL-Learner is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or @@ -15,7 +15,6 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * */ package org.dllearner.algorithms.qtl.operations.nbr.strategy; @@ -55,7 +54,7 @@ public RDFResourceTree computeNBR(RDFResourceTree posExampleTree, List tested = new HashSet(); + Set tested = new HashSet<>(); Node edge; RDFResourceTree parent; while(!(tested.size() == nbr.getLeafs().size()) ){ @@ -98,7 +97,7 @@ public List computeNBRs(RDFResourceTree posExampleTree, return Collections.singletonList(posExampleTree); } - List nbrs = new ArrayList(); + List nbrs = new ArrayList<>(); compute(posExampleTree, negExampleTrees, nbrs); @@ -119,7 +118,7 @@ private void compute(RDFResourceTree posExampleTree, } if(!subsumesTrees(nbr, negExampleTrees)){ - Set tested = new HashSet(); + Set tested = new HashSet<>(); Node edge; RDFResourceTree parent; while(!(tested.size() == nbr.getLeafs().size()) ){ diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/nbr/strategy/GreedyNBRStrategy.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/nbr/strategy/GreedyNBRStrategy.java index 261d3ad79b..6e6aa96b55 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/nbr/strategy/GreedyNBRStrategy.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/nbr/strategy/GreedyNBRStrategy.java @@ -1,8 +1,8 @@ /** - * Copyright (C) 2007-2010, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. - * + * * DL-Learner is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or @@ -15,7 +15,6 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * */ package org.dllearner.algorithms.qtl.operations.nbr.strategy; @@ -65,7 +64,7 @@ public RDFResourceTree computeNBR(RDFResourceTree posExampleTree, mon.start(); RDFResourceTree nbr = new RDFResourceTree(posExampleTree); - Map> matrix = new HashMap>(); + Map> matrix = new HashMap<>(); for(int i = 0; i < negExampleTrees.size(); i++){ checkTree(matrix, nbr, negExampleTrees.get(i), i); @@ -73,7 +72,7 @@ public RDFResourceTree computeNBR(RDFResourceTree posExampleTree, int negTreeSize = negExampleTrees.size(); - Map rowValues = new HashMap(); + Map rowValues = new HashMap<>(); double value; for(Entry> entry : matrix.entrySet()){ value = (sum(entry.getValue())+1.0)/(negTreeSize+2.0); @@ -81,7 +80,7 @@ public RDFResourceTree computeNBR(RDFResourceTree posExampleTree, } - List candidates2Remove = new ArrayList(); + List candidates2Remove = new ArrayList<>(); if(useWeakGeneralisation){ for(Entry entry : rowValues.entrySet()){ if(random.nextDouble() < entry.getValue()){ @@ -159,7 +158,7 @@ private boolean coversNegativeTree(RDFResourceTree posTree, List candidates2Remove){ - for(RDFResourceTree leaf : new ArrayList(nbr.getLeafs())){ + for(RDFResourceTree leaf : new ArrayList<>(nbr.getLeafs())){ if(candidates2Remove.contains(leaf)){ logger.info("REMOVE " + leaf); leaf.getParent().removeChild(leaf); @@ -189,7 +188,7 @@ private String printTreeWithValues(RDFResourceTree tree, Map> matrix, RDFResourceTr private void setMatrixEntry(Map> matrix, RDFResourceTree row, int column, int entry){ List list = matrix.get(row); if(list == null){ - list = new ArrayList(); + list = new ArrayList<>(); matrix.put(row, list); } try { diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/nbr/strategy/NBRStrategy.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/nbr/strategy/NBRStrategy.java index 995a2f9d5c..25a67ef809 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/nbr/strategy/NBRStrategy.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/nbr/strategy/NBRStrategy.java @@ -1,8 +1,8 @@ /** - * Copyright (C) 2007-2010, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. - * + * * DL-Learner is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or @@ -15,7 +15,6 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * */ package org.dllearner.algorithms.qtl.operations.nbr.strategy; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/nbr/strategy/TagNonSubsumingPartsNBRStrategy.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/nbr/strategy/TagNonSubsumingPartsNBRStrategy.java index 059372d54f..7f5dc0455a 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/nbr/strategy/TagNonSubsumingPartsNBRStrategy.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/operations/nbr/strategy/TagNonSubsumingPartsNBRStrategy.java @@ -1,8 +1,8 @@ /** - * Copyright (C) 2007-2010, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. - * + * * DL-Learner is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or @@ -15,7 +15,6 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * */ package org.dllearner.algorithms.qtl.operations.nbr.strategy; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/package-info.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/package-info.java index 017ca227ef..8a3e25a1a5 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/package-info.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/package-info.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ /** * Learning algorithm based on so-called query trees * which are a tree based representation of a (set of) RDF resource(s) diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/ClassExpressionLiteralCombination.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/ClassExpressionLiteralCombination.java index ea61d00079..6ec5d4f714 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/ClassExpressionLiteralCombination.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/ClassExpressionLiteralCombination.java @@ -1,12 +1,28 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.util; import java.util.HashSet; import java.util.Set; -import org.semanticweb.owlapi.io.ToStringRenderer; +import org.dllearner.core.StringRenderer; +import org.dllearner.core.StringRenderer.Rendering; import org.semanticweb.owlapi.model.OWLClass; import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx; @@ -44,7 +60,9 @@ import org.semanticweb.owlapi.vocab.OWLFacet; import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; -import uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.ManchesterOWLSyntaxOWLObjectRendererImpl; +import org.dllearner.utilities.owl.ManchesterOWLSyntaxOWLObjectRendererImplExt; + +import javax.annotation.Nonnull; /** * @author Lorenz Buehmann @@ -57,9 +75,10 @@ public class ClassExpressionLiteralCombination implements OWLClassExpressionVisi /* (non-Javadoc) * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLClass) */ + @Nonnull @Override - public Set visit(OWLClass ce) { - Set expressions = new HashSet(1); + public Set visit(@Nonnull OWLClass ce) { + Set expressions = new HashSet<>(1); expressions.add(ce); return expressions; } @@ -67,20 +86,21 @@ public Set visit(OWLClass ce) { /* (non-Javadoc) * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectIntersectionOf) */ + @Nonnull @Override - public Set visit(OWLObjectIntersectionOf ce) { - Set expressions = new HashSet(); - Set> combinations = new HashSet>(); + public Set visit(@Nonnull OWLObjectIntersectionOf ce) { + Set expressions = new HashSet<>(); + Set> combinations = new HashSet<>(); for (int i = 0; i < ce.getOperands().size(); i++) { - Set tmp = new HashSet(); + Set tmp = new HashSet<>(); combinations.add(tmp); } for (OWLClassExpression operand : ce.getOperands()) { - Set> combinationsTmp = new HashSet>(); + Set> combinationsTmp = new HashSet<>(); Set newOperands = operand.accept(this); for (Set set : combinations) { for (OWLClassExpression newOp : newOperands) { - Set tmp = new HashSet(); + Set tmp = new HashSet<>(); tmp.addAll(set); tmp.add(newOp); combinationsTmp.add(tmp); @@ -97,25 +117,28 @@ public Set visit(OWLObjectIntersectionOf ce) { /* (non-Javadoc) * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectUnionOf) */ + @Nonnull @Override - public Set visit(OWLObjectUnionOf ce) { + public Set visit(@Nonnull OWLObjectUnionOf ce) { return null; } /* (non-Javadoc) * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectComplementOf) */ + @Nonnull @Override - public Set visit(OWLObjectComplementOf ce) { + public Set visit(@Nonnull OWLObjectComplementOf ce) { return null; } /* (non-Javadoc) * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom) */ + @Nonnull @Override - public Set visit(OWLObjectSomeValuesFrom ce) { - Set expressions = new HashSet(); + public Set visit(@Nonnull OWLObjectSomeValuesFrom ce) { + Set expressions = new HashSet<>(); Set newFillers = ce.getFiller().accept(this); for (OWLClassExpression newFiller : newFillers) { expressions.add(df.getOWLObjectSomeValuesFrom(ce.getProperty(), newFiller)); @@ -126,17 +149,19 @@ public Set visit(OWLObjectSomeValuesFrom ce) { /* (non-Javadoc) * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectAllValuesFrom) */ + @Nonnull @Override - public Set visit(OWLObjectAllValuesFrom ce) { + public Set visit(@Nonnull OWLObjectAllValuesFrom ce) { return null; } /* (non-Javadoc) * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectHasValue) */ + @Nonnull @Override - public Set visit(OWLObjectHasValue ce) { - Set expressions = new HashSet(); + public Set visit(@Nonnull OWLObjectHasValue ce) { + Set expressions = new HashSet<>(); expressions.add(ce); return expressions; } @@ -144,49 +169,55 @@ public Set visit(OWLObjectHasValue ce) { /* (non-Javadoc) * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectMinCardinality) */ + @Nonnull @Override - public Set visit(OWLObjectMinCardinality ce) { + public Set visit(@Nonnull OWLObjectMinCardinality ce) { return null; } /* (non-Javadoc) * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectExactCardinality) */ + @Nonnull @Override - public Set visit(OWLObjectExactCardinality ce) { + public Set visit(@Nonnull OWLObjectExactCardinality ce) { return null; } /* (non-Javadoc) * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectMaxCardinality) */ + @Nonnull @Override - public Set visit(OWLObjectMaxCardinality ce) { + public Set visit(@Nonnull OWLObjectMaxCardinality ce) { return null; } /* (non-Javadoc) * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectHasSelf) */ + @Nonnull @Override - public Set visit(OWLObjectHasSelf ce) { + public Set visit(@Nonnull OWLObjectHasSelf ce) { return null; } /* (non-Javadoc) * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectOneOf) */ + @Nonnull @Override - public Set visit(OWLObjectOneOf ce) { + public Set visit(@Nonnull OWLObjectOneOf ce) { return null; } /* (non-Javadoc) * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataSomeValuesFrom) */ + @Nonnull @Override - public Set visit(OWLDataSomeValuesFrom ce) { - Set expressions = new HashSet(); + public Set visit(@Nonnull OWLDataSomeValuesFrom ce) { + Set expressions = new HashSet<>(); Set newDataRanges = ce.getFiller().accept(this); for (OWLDataRange newDataRange : newDataRanges) { expressions.add(df.getOWLDataSomeValuesFrom(ce.getProperty(), newDataRange)); @@ -197,17 +228,19 @@ public Set visit(OWLDataSomeValuesFrom ce) { /* (non-Javadoc) * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataAllValuesFrom) */ + @Nonnull @Override - public Set visit(OWLDataAllValuesFrom ce) { + public Set visit(@Nonnull OWLDataAllValuesFrom ce) { return null; } /* (non-Javadoc) * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataHasValue) */ + @Nonnull @Override - public Set visit(OWLDataHasValue ce) { - Set expressions = new HashSet(); + public Set visit(@Nonnull OWLDataHasValue ce) { + Set expressions = new HashSet<>(); expressions.add(ce); return expressions; } @@ -215,33 +248,37 @@ public Set visit(OWLDataHasValue ce) { /* (non-Javadoc) * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataMinCardinality) */ + @Nonnull @Override - public Set visit(OWLDataMinCardinality ce) { + public Set visit(@Nonnull OWLDataMinCardinality ce) { return null; } /* (non-Javadoc) * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataExactCardinality) */ + @Nonnull @Override - public Set visit(OWLDataExactCardinality ce) { + public Set visit(@Nonnull OWLDataExactCardinality ce) { return null; } /* (non-Javadoc) * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataMaxCardinality) */ + @Nonnull @Override - public Set visit(OWLDataMaxCardinality ce) { + public Set visit(@Nonnull OWLDataMaxCardinality ce) { return null; } /* (non-Javadoc) * @see org.semanticweb.owlapi.model.OWLDataRangeVisitorEx#visit(org.semanticweb.owlapi.model.OWLDatatype) */ + @Nonnull @Override - public Set visit(OWLDatatype dr) { - Set dataRanges = new HashSet(); + public Set visit(@Nonnull OWLDatatype dr) { + Set dataRanges = new HashSet<>(); dataRanges.add(dr); return dataRanges; } @@ -249,9 +286,10 @@ public Set visit(OWLDatatype dr) { /* (non-Javadoc) * @see org.semanticweb.owlapi.model.OWLDataRangeVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataOneOf) */ + @Nonnull @Override - public Set visit(OWLDataOneOf dr) { - Set dataRanges = new HashSet(); + public Set visit(@Nonnull OWLDataOneOf dr) { + Set dataRanges = new HashSet<>(); dataRanges.add(dr); return dataRanges; } @@ -259,9 +297,10 @@ public Set visit(OWLDataOneOf dr) { /* (non-Javadoc) * @see org.semanticweb.owlapi.model.OWLDataRangeVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataComplementOf) */ + @Nonnull @Override - public Set visit(OWLDataComplementOf dr) { - Set dataRanges = new HashSet(); + public Set visit(@Nonnull OWLDataComplementOf dr) { + Set dataRanges = new HashSet<>(); dataRanges.add(dr); return dataRanges; } @@ -269,9 +308,10 @@ public Set visit(OWLDataComplementOf dr) { /* (non-Javadoc) * @see org.semanticweb.owlapi.model.OWLDataRangeVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataIntersectionOf) */ + @Nonnull @Override - public Set visit(OWLDataIntersectionOf dr) { - Set dataRanges = new HashSet(); + public Set visit(@Nonnull OWLDataIntersectionOf dr) { + Set dataRanges = new HashSet<>(); dataRanges.add(dr); return dataRanges; } @@ -279,9 +319,10 @@ public Set visit(OWLDataIntersectionOf dr) { /* (non-Javadoc) * @see org.semanticweb.owlapi.model.OWLDataRangeVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataUnionOf) */ + @Nonnull @Override - public Set visit(OWLDataUnionOf dr) { - Set dataRanges = new HashSet(); + public Set visit(@Nonnull OWLDataUnionOf dr) { + Set dataRanges = new HashSet<>(); dataRanges.add(dr); return dataRanges; } @@ -289,9 +330,10 @@ public Set visit(OWLDataUnionOf dr) { /* (non-Javadoc) * @see org.semanticweb.owlapi.model.OWLDataRangeVisitorEx#visit(org.semanticweb.owlapi.model.OWLDatatypeRestriction) */ + @Nonnull @Override - public Set visit(OWLDatatypeRestriction dr) { - Set dataRanges = new HashSet(); + public Set visit(@Nonnull OWLDatatypeRestriction dr) { + Set dataRanges = new HashSet<>(); Set facetRestrictions = dr.getFacetRestrictions(); OWLLiteral min = null; OWLLiteral max = null; @@ -314,9 +356,10 @@ public Set visit(OWLDatatypeRestriction dr) { public static void main(String[] args) throws Exception { - ToStringRenderer.getInstance().setRenderer(new ManchesterOWLSyntaxOWLObjectRendererImpl()); + StringRenderer.setRenderer(Rendering.MANCHESTER_SYNTAX); OWLDataFactoryImpl df = new OWLDataFactoryImpl(); - PrefixManager pm = new DefaultPrefixManager(":"); + PrefixManager pm = new DefaultPrefixManager(); + pm.setDefaultPrefix(":"); OWLClass A = df.getOWLClass("A", pm ); OWLDataProperty s = df.getOWLDataProperty("s", pm); OWLDataProperty t = df.getOWLDataProperty("t", pm); diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/Entailment.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/Entailment.java index f30f7025e9..e4ceb0869a 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/Entailment.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/Entailment.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.util; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/InformativenessMeasures.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/InformativenessMeasures.java index 3dd8d32cc3..e3b30ae5bd 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/InformativenessMeasures.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/InformativenessMeasures.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.util; @@ -18,6 +33,10 @@ import com.hp.hpl.jena.query.QueryExecution; /** + * Contains a set of measures to compute the informativeness of a triple based on the work proposed in + * REWOrD: Semantic Relatedness, AAAI 2012. + * + * * @author Lorenz Buehmann * */ @@ -30,6 +49,23 @@ public InformativenessMeasures(QueryExecutionFactory qef) { this.qef = qef; } + /** + *

+ * The inverse triple frequency ITF(p), considers how many times a predicate + * is used in some RDF triple w.r.t. the total number of triples, and is + * defined as: + *

+ * + *

log(|T|/|T(p)|)

+ * + *

+ * where |T| is the total number of triples in the knowledge base and |T(p)| + * the total number of triples having p as a predicate. + *

+ * + * @param property the predicate + * @return the inverse triple frequency + */ public double getInverseTripleFrequency(OWLProperty property) { // total number of triples String query = "SELECT (COUNT(*) AS ?cnt) WHERE {?s ?p ?o .}"; @@ -43,11 +79,22 @@ public double getInverseTripleFrequency(OWLProperty property) { int frequency = qe.execSelect().next().getLiteral("cnt").getInt(); qe.close(); + double itf = Math.log(total / (double) frequency); return itf; } + /** + * Predicate Frequency(PF) quantifies the informativeness of a predicate p + * in the context of a URI u. With context we mean the RDF triples where p + * and u appear together. + * + * @param individual + * @param property the predicate + * @param outgoing + * @return + */ public double getPredicateFrequency(OWLIndividual individual, OWLProperty property, boolean outgoing) { String query = outgoing ? "SELECT (COUNT(*) AS ?cnt) WHERE {<%s> <%s> ?o .}" : "SELECT (COUNT(*) AS ?cnt) WHERE {?s <%s> <%s> .}"; query = String.format(query, individual.toStringID(), property.toStringID()); @@ -66,7 +113,7 @@ public double getPF_ITF(OWLIndividual individual, OWLProperty property, boolean public static void main(String[] args) throws Exception { SparqlEndpointKS ks = new SparqlEndpointKS(new SparqlEndpoint( - new URL("http://sake.informatik.uni-leipzig.de:8890/sparql"), + new URL("http://dbpedia.org/sparql"), "http://dbpedia.org")); ks.init(); diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/PrefixCCPrefixMapping.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/PrefixCCPrefixMapping.java index 04456bdc44..f675e7b0cc 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/PrefixCCPrefixMapping.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/PrefixCCPrefixMapping.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.util; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/Prefixes.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/Prefixes.java index b6f0685c81..ec7d13dab2 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/Prefixes.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/Prefixes.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.util; import java.util.HashMap; @@ -7,7 +25,7 @@ public class Prefixes { public static Map getPrefixes(){ - Map prefixes = new HashMap(); + Map prefixes = new HashMap<>(); prefixes.put("dbo","http://dbpedia.org/ontology/"); prefixes.put("dbprop","http://dbpedia.org/property/"); prefixes.put("rdfs","http://www.w3.org/2000/01/rdf-schema#"); diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/QueryTreeConverter.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/QueryTreeConverter.java index cd4806806e..2963b9fa35 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/QueryTreeConverter.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/QueryTreeConverter.java @@ -1,71 +1,44 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.util; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Set; -import java.util.Stack; - -import javax.xml.bind.DatatypeConverter; - +import com.hp.hpl.jena.datatypes.RDFDatatype; +import com.hp.hpl.jena.datatypes.xsd.XSDDatatype; +import com.hp.hpl.jena.rdf.model.Literal; +import com.hp.hpl.jena.vocabulary.OWL; +import com.hp.hpl.jena.vocabulary.RDF; +import com.hp.hpl.jena.vocabulary.RDFS; import org.dllearner.algorithms.qtl.datastructures.QueryTree; import org.dllearner.algorithms.qtl.datastructures.impl.QueryTreeImpl; import org.dllearner.algorithms.qtl.datastructures.impl.QueryTreeImpl.LiteralNodeConversionStrategy; import org.dllearner.algorithms.qtl.datastructures.impl.QueryTreeImpl.NodeType; +import org.dllearner.core.StringRenderer; +import org.dllearner.core.StringRenderer.Rendering; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.io.ToStringRenderer; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLClassExpressionVisitor; -import org.semanticweb.owlapi.model.OWLDataAllValuesFrom; -import org.semanticweb.owlapi.model.OWLDataComplementOf; -import org.semanticweb.owlapi.model.OWLDataExactCardinality; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLDataHasValue; -import org.semanticweb.owlapi.model.OWLDataIntersectionOf; -import org.semanticweb.owlapi.model.OWLDataMaxCardinality; -import org.semanticweb.owlapi.model.OWLDataMinCardinality; -import org.semanticweb.owlapi.model.OWLDataOneOf; -import org.semanticweb.owlapi.model.OWLDataProperty; -import org.semanticweb.owlapi.model.OWLDataRange; -import org.semanticweb.owlapi.model.OWLDataRangeVisitor; -import org.semanticweb.owlapi.model.OWLDataSomeValuesFrom; -import org.semanticweb.owlapi.model.OWLDataUnionOf; -import org.semanticweb.owlapi.model.OWLDatatype; -import org.semanticweb.owlapi.model.OWLDatatypeRestriction; -import org.semanticweb.owlapi.model.OWLFacetRestriction; -import org.semanticweb.owlapi.model.OWLLiteral; -import org.semanticweb.owlapi.model.OWLObjectAllValuesFrom; -import org.semanticweb.owlapi.model.OWLObjectComplementOf; -import org.semanticweb.owlapi.model.OWLObjectExactCardinality; -import org.semanticweb.owlapi.model.OWLObjectHasSelf; -import org.semanticweb.owlapi.model.OWLObjectHasValue; -import org.semanticweb.owlapi.model.OWLObjectIntersectionOf; -import org.semanticweb.owlapi.model.OWLObjectMaxCardinality; -import org.semanticweb.owlapi.model.OWLObjectMinCardinality; -import org.semanticweb.owlapi.model.OWLObjectOneOf; -import org.semanticweb.owlapi.model.OWLObjectProperty; -import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom; -import org.semanticweb.owlapi.model.OWLObjectUnionOf; -import org.semanticweb.owlapi.model.OWLOntologyManager; -import org.semanticweb.owlapi.model.PrefixManager; +import org.semanticweb.owlapi.model.*; import org.semanticweb.owlapi.util.DefaultPrefixManager; import org.semanticweb.owlapi.vocab.OWL2Datatype; import org.semanticweb.owlapi.vocab.OWLFacet; - import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; -import uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntaxObjectRenderer; -import com.hp.hpl.jena.datatypes.RDFDatatype; -import com.hp.hpl.jena.datatypes.xsd.XSDDatatype; -import com.hp.hpl.jena.rdf.model.Literal; -import com.hp.hpl.jena.vocabulary.OWL; -import com.hp.hpl.jena.vocabulary.RDF; -import com.hp.hpl.jena.vocabulary.RDFS; +import javax.xml.bind.DatatypeConverter; +import java.util.*; /** * Converts query trees into OWL class expressions and vice versa. @@ -76,12 +49,12 @@ public class QueryTreeConverter implements OWLClassExpressionVisitor, OWLDataRan OWLDataFactory df = new OWLDataFactoryImpl(); - Stack> stack = new Stack>(); + Stack> stack = new Stack<>(); int id = 0; /** - * Returns a OWL class expression of the union of the given query trees. - * @param queryTrees + * Returns a OWL class expression of the given query trees. + * @param tree the query tree */ public OWLClassExpression asOWLClassExpression(QueryTree tree){ Set classExpressions = asOWLClassExpressions(tree); @@ -97,15 +70,15 @@ public OWLClassExpression asOWLClassExpression(QueryTree tree){ } /** - * Returns a OWL class expression representation of the given query tree. - * @param queryTrees + * Returns a set of OWL class expression representations of the given query tree. + * @param tree the query tree */ public Set asOWLClassExpressions(QueryTree tree){ - Set classExpressions = new HashSet(); + Set classExpressions = new HashSet<>(); List> children = tree.getChildren(); for(QueryTree child : children){ - String childLabel = (String) child.getUserObject(); + String childLabel = child.getUserObject(); String predicateString = (String) tree.getEdge(child); if(predicateString.equals(RDF.type.getURI()) || predicateString.equals(RDFS.subClassOf.getURI())){//A if(child.isVarNode()){ @@ -223,7 +196,7 @@ private OWLDataRange asDataOneOf(OWLDataFactory df, Set literals){ } private Set asOWLLiterals(OWLDataFactory df, Set literals){ - Set owlLiterals = new HashSet(literals.size()); + Set owlLiterals = new HashSet<>(literals.size()); for (Literal literal : literals) { owlLiterals.add(asOWLLiteral(df, literal)); } @@ -311,7 +284,7 @@ private void fireUnsupportedFeatureException(OWLClassExpression expression) { */ @Override public void visit(OWLClass cls) { - stack.peek().addChild(new QueryTreeImpl(cls.toStringID(), NodeType.RESOURCE, id++), RDF.type.getURI()); + stack.peek().addChild(new QueryTreeImpl<>(cls.toStringID(), NodeType.RESOURCE, id++), RDF.type.getURI()); } /* (non-Javadoc) @@ -320,7 +293,7 @@ public void visit(OWLClass cls) { @Override public void visit(OWLObjectIntersectionOf expr) { boolean root = stack.isEmpty(); - stack.push(new QueryTreeImpl("?", NodeType.VARIABLE, id++)); + stack.push(new QueryTreeImpl<>("?", NodeType.VARIABLE, id++)); for (OWLClassExpression op : expr.getOperandsAsList()) { op.accept(this); } @@ -353,12 +326,12 @@ public void visit(OWLObjectSomeValuesFrom expr) { OWLClassExpression filler = expr.getFiller(); if(filler.isAnonymous()){ if(!(filler instanceof OWLObjectIntersectionOf)){ - stack.push(new QueryTreeImpl("?", NodeType.VARIABLE, id++)); + stack.push(new QueryTreeImpl<>("?", NodeType.VARIABLE, id++)); } expr.getFiller().accept(this); child = stack.pop(); } else { - child = new QueryTreeImpl(filler.asOWLClass().toStringID(), NodeType.RESOURCE, id++); + child = new QueryTreeImpl<>(filler.asOWLClass().toStringID(), NodeType.RESOURCE, id++); } parent.addChild(child, expr.getProperty().asOWLObjectProperty().toStringID()); } @@ -377,7 +350,7 @@ public void visit(OWLObjectAllValuesFrom expr) { @Override public void visit(OWLObjectHasValue expr) { QueryTree tree = stack.peek(); - tree.addChild(new QueryTreeImpl(expr.getValue().asOWLNamedIndividual().toStringID(), NodeType.RESOURCE, id++), expr.getProperty().asOWLObjectProperty().toStringID()); + tree.addChild(new QueryTreeImpl<>(expr.getFiller().asOWLNamedIndividual().toStringID(), NodeType.RESOURCE, id++), expr.getProperty().asOWLObjectProperty().toStringID()); } /* (non-Javadoc) @@ -511,10 +484,11 @@ public void visit(OWLDatatypeRestriction arg0) { } public static void main(String[] args) throws Exception { - ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); + StringRenderer.setRenderer(Rendering.DL_SYNTAX); OWLOntologyManager man = OWLManager.createOWLOntologyManager(); OWLDataFactory df = man.getOWLDataFactory(); - PrefixManager pm = new DefaultPrefixManager("http://example.org/"); + PrefixManager pm = new DefaultPrefixManager(); + pm.setDefaultPrefix("http://example.org/"); OWLClassExpression ce = df.getOWLObjectIntersectionOf( df.getOWLClass("A", pm), df.getOWLObjectSomeValuesFrom( diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/QueryTreePoint.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/QueryTreePoint.java index a4f1e4462e..2fb3ac9bca 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/QueryTreePoint.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/QueryTreePoint.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.util; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/SPARQLEndpointEx.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/SPARQLEndpointEx.java index 44713adecf..b3c81d097f 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/SPARQLEndpointEx.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/SPARQLEndpointEx.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.util; import java.net.URL; @@ -63,15 +81,14 @@ public Set getPredicateFilters(){ @Override public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("ENDPOINT\n"); - sb.append("Label: ").append(getLabel()).append("\n"); - sb.append("URL: ").append(getURL()).append("\n"); - sb.append("Default Graph URI: ").append(getDefaultGraphURIs()).append("\n"); - sb.append("Named Graph URI: ").append(getNamedGraphURIs()).append("\n"); - sb.append("Predicate Filters: ").append(getPredicateFilters()).append("\n"); - - return sb.toString(); + String sb = "ENDPOINT\n" + + "Label: " + getLabel() + "\n" + + "URL: " + getURL() + "\n" + + "Default Graph URI: " + getDefaultGraphURIs() + "\n" + + "Named Graph URI: " + getNamedGraphURIs() + "\n" + + "Predicate Filters: " + getPredicateFilters() + "\n"; + + return sb; } @Override diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/StopURIsDBpedia.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/StopURIsDBpedia.java index d7235af446..04e370b413 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/StopURIsDBpedia.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/StopURIsDBpedia.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.util; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/StopURIsOWL.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/StopURIsOWL.java index e297f99375..86793b575d 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/StopURIsOWL.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/StopURIsOWL.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.util; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/StopURIsRDFS.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/StopURIsRDFS.java index 67d63f68cb..6664ef4036 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/StopURIsRDFS.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/StopURIsRDFS.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.util; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/StopURIsSKOS.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/StopURIsSKOS.java index a5f4a88373..b7477c9afe 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/StopURIsSKOS.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/StopURIsSKOS.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.util; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/TreeHelper.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/TreeHelper.java index 3e3854fef6..7868fa1201 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/TreeHelper.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/TreeHelper.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.util; import java.util.Map; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/VarGenerator.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/VarGenerator.java index 024d9d44fb..69bc066e4d 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/VarGenerator.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/VarGenerator.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.util; import com.hp.hpl.jena.sparql.core.Var; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/filters/NamespaceDropStatementFilter.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/filters/NamespaceDropStatementFilter.java index a8e47f3f81..057dccebf1 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/filters/NamespaceDropStatementFilter.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/filters/NamespaceDropStatementFilter.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.util.filters; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/filters/ObjectDropStatementFilter.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/filters/ObjectDropStatementFilter.java index 3debd33034..7ca97763b0 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/filters/ObjectDropStatementFilter.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/filters/ObjectDropStatementFilter.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.util.filters; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/filters/PredicateDropStatementFilter.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/filters/PredicateDropStatementFilter.java index de3983dc72..eeb6666e0a 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/filters/PredicateDropStatementFilter.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/filters/PredicateDropStatementFilter.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.util.filters; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/filters/PredicateExistenceFilter.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/filters/PredicateExistenceFilter.java index bce9c74b30..247386b344 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/filters/PredicateExistenceFilter.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/filters/PredicateExistenceFilter.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.util.filters; @@ -21,7 +36,7 @@ public class PredicateExistenceFilter { - private Set existentialMeaninglessProperties = new HashSet(); + private Set existentialMeaninglessProperties = new HashSet<>(); public PredicateExistenceFilter() { } @@ -39,8 +54,8 @@ public void setExistentialMeaninglessProperties(Set existentialMeaningless /** * Returns a new tree based on the input tree. - * @param tree - * @return + * @param tree the input tree + * @return a filtered new tree */ public RDFResourceTree filter(RDFResourceTree tree) { RDFResourceTree newTree; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/filters/PredicateExistenceFilterDBpedia.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/filters/PredicateExistenceFilterDBpedia.java index 8300cee639..300555ebd3 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/filters/PredicateExistenceFilterDBpedia.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/filters/PredicateExistenceFilterDBpedia.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.util.filters; @@ -33,7 +48,7 @@ public class PredicateExistenceFilterDBpedia extends PredicateExistenceFilter{ public PredicateExistenceFilterDBpedia(SparqlEndpointKS ks) { this.ks = ks; - Set existentialMeaninglessProperties = new TreeSet(new NodeComparator()); + Set existentialMeaninglessProperties = new TreeSet<>(new NodeComparator()); try { List lines = Files.readLines(new File(this.getClass().getClassLoader().getResource(PATH).toURI()), Charsets.UTF_8); @@ -49,7 +64,7 @@ public PredicateExistenceFilterDBpedia(SparqlEndpointKS ks) { } private void analyze() { - Set existentialMeaninglessProperties = new TreeSet(new NodeComparator()); + Set existentialMeaninglessProperties = new TreeSet<>(new NodeComparator()); StringBuilder sb = new StringBuilder(); // check data properties diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/filters/package-info.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/filters/package-info.java index 6cac61543c..d4e5bb0c40 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/filters/package-info.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/filters/package-info.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ /** * */ diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/statistics/TimeMonitors.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/statistics/TimeMonitors.java index 924a900dff..1780ffb058 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/statistics/TimeMonitors.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/statistics/TimeMonitors.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.util.statistics; diff --git a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/statistics/package-info.java b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/statistics/package-info.java index cb8e9f9e4d..35c7a92c0e 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/qtl/util/statistics/package-info.java +++ b/components-core/src/main/java/org/dllearner/algorithms/qtl/util/statistics/package-info.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ /** * */ diff --git a/components-core/src/main/java/org/dllearner/algorithms/refinement/Heuristic.java b/components-core/src/main/java/org/dllearner/algorithms/refinement/Heuristic.java index 7aa349893d..8eb23d599c 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/refinement/Heuristic.java +++ b/components-core/src/main/java/org/dllearner/algorithms/refinement/Heuristic.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.refinement; import java.util.Comparator; diff --git a/components-core/src/main/java/org/dllearner/algorithms/refinement/Node.java b/components-core/src/main/java/org/dllearner/algorithms/refinement/Node.java index a8a321dc09..716099b8a0 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/refinement/Node.java +++ b/components-core/src/main/java/org/dllearner/algorithms/refinement/Node.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.refinement; import java.util.Set; @@ -27,8 +26,8 @@ public class Node { // TOP ist einfach das TOP-Konzept, also das einzige welches nicht evaluiert wird - public enum QualityEvaluationMethod { TOP, REASONER, TOO_WEAK_LIST, OVERLY_GENERAL_LIST }; - + public enum QualityEvaluationMethod { TOP, REASONER, TOO_WEAK_LIST, OVERLY_GENERAL_LIST } + private QualityEvaluationMethod qualityEvaluationMethod = QualityEvaluationMethod.TOP; // alle Eigenschaften eines Knotens im Suchbaum @@ -44,9 +43,9 @@ public enum QualityEvaluationMethod { TOP, REASONER, TOO_WEAK_LIST, OVERLY_GENER // Einbettung in Suchbaum private Node parent = null; // private Set children = new HashSet(); - private Set children = new TreeSet(nodeComparator); + private Set children = new TreeSet<>(nodeComparator); // es wird auch eine Liste von Kindern gehalten - private Set childConcepts = new TreeSet(); + private Set childConcepts = new TreeSet<>(); // verwendeter Operator für Expansion des Knotens // private RefinementOperator operator; @@ -138,7 +137,7 @@ private StringBuilder getTreeString(int depth) { if(depth!=0) // treeString.append("|-→ "); treeString.append("|--> "); - treeString.append(getShortDescription()+"\n"); + treeString.append(getShortDescription()).append("\n"); for(Node child : children) { treeString.append(child.getTreeString(depth+1)); } diff --git a/components-core/src/main/java/org/dllearner/algorithms/refinement/NodeComparator.java b/components-core/src/main/java/org/dllearner/algorithms/refinement/NodeComparator.java index 20f372c956..8c9f40e96e 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/refinement/NodeComparator.java +++ b/components-core/src/main/java/org/dllearner/algorithms/refinement/NodeComparator.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.refinement; diff --git a/components-core/src/main/java/org/dllearner/algorithms/refinement/NodeComparator2.java b/components-core/src/main/java/org/dllearner/algorithms/refinement/NodeComparator2.java index 5ee9804b20..85ee476605 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/refinement/NodeComparator2.java +++ b/components-core/src/main/java/org/dllearner/algorithms/refinement/NodeComparator2.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.refinement; import org.dllearner.utilities.owl.OWLClassExpressionUtils; diff --git a/components-core/src/main/java/org/dllearner/algorithms/refinement/NodeComparatorStable.java b/components-core/src/main/java/org/dllearner/algorithms/refinement/NodeComparatorStable.java index 682ec75c57..a2e6ae9653 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/refinement/NodeComparatorStable.java +++ b/components-core/src/main/java/org/dllearner/algorithms/refinement/NodeComparatorStable.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.algorithms.refinement; import java.util.Comparator; diff --git a/components-core/src/main/java/org/dllearner/algorithms/schema/AbstractSchemaGenerator.java b/components-core/src/main/java/org/dllearner/algorithms/schema/AbstractSchemaGenerator.java index 7934fe1cf2..b03b0539c3 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/schema/AbstractSchemaGenerator.java +++ b/components-core/src/main/java/org/dllearner/algorithms/schema/AbstractSchemaGenerator.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.schema; @@ -32,6 +47,7 @@ import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.rdf.model.Statement; +import org.semanticweb.owlapi.profiles.Profiles; /** @@ -91,16 +107,16 @@ public void setAxiomTypes(Set> axiomTypes) { /** * Set the types of axioms that are generated by giving an OWL profile. - * @param axiomTypes the axiom types to set + * @param owlProfile the OWL profile */ public void setAxiomTypes(OWLProfile owlProfile) { - if(owlProfile.getIRI().equals(OWLProfile.OWL2_EL)){ - - } else if(owlProfile.getIRI().equals(OWLProfile.OWL2_RL)){ - - } else if(owlProfile.getIRI().equals(OWLProfile.OWL2_QL)){ + if(owlProfile.equals(Profiles.OWL2_EL.getOWLProfile())){ - } else if(owlProfile.getIRI().equals(OWLProfile.OWL2_DL)){ + } else if(owlProfile.equals(Profiles.OWL2_RL.getOWLProfile())){ + + } else if(owlProfile.equals(Profiles.OWL2_QL.getOWLProfile())){ + + } else if(owlProfile.equals(Profiles.OWL2_DL.getOWLProfile())){ } else { throw new IllegalArgumentException("OWL profile " + owlProfile.getName() + " not supported."); @@ -123,7 +139,7 @@ public void setEntities(SortedSet entities) { */ protected SortedSet getEntities(){ if(entities == null){ - entities = new TreeSet(); + entities = new TreeSet<>(); for (EntityType entityType : entityTypes) { if(entityType == EntityType.CLASS){ entities.addAll(reasoner.getOWLClasses()); @@ -180,11 +196,11 @@ protected List applyLearningAlgorithm(OWLEntity entity, AxiomType axioms) { Set statements = OwlApiJenaUtils.asStatements(axioms); - model.add(new ArrayList(statements)); + model.add(new ArrayList<>(statements)); } } diff --git a/components-core/src/main/java/org/dllearner/algorithms/schema/EntityDependencyMatrix.java b/components-core/src/main/java/org/dllearner/algorithms/schema/EntityDependencyMatrix.java index 845364722d..ed0c609813 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/schema/EntityDependencyMatrix.java +++ b/components-core/src/main/java/org/dllearner/algorithms/schema/EntityDependencyMatrix.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.schema; @@ -14,6 +29,7 @@ import org.semanticweb.owlapi.model.OWLObjectProperty; import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom; import org.semanticweb.owlapi.model.OWLOntology; +import org.semanticweb.owlapi.model.parameters.Imports; import org.semanticweb.owlapi.reasoner.NodeSet; import org.semanticweb.owlapi.reasoner.OWLReasoner; import org.semanticweb.owlapi.reasoner.structural.StructuralReasonerFactory; @@ -33,18 +49,18 @@ private EntityDependencyMatrix() { } - public static void EntityDependencyMatrix(OWLOntology ontology) { + public static void getEntityDependencyMatrix(OWLOntology ontology) { OWLReasoner reasoner = new StructuralReasonerFactory().createNonBufferingReasoner(ontology); // how often are individuals of class A related to individuals of class B - Set classes = ontology.getClassesInSignature(true); + Set classes = ontology.getClassesInSignature(Imports.INCLUDED); for (OWLClass clsA : classes) { for (OWLClass clsB : classes) { if(!clsA.equals(clsB)) { Set instancesA = reasoner.getInstances(clsA, false).getFlattened(); Set instancesB = reasoner.getInstances(clsB, false).getFlattened(); - Set objectsOfInstancesFromA = new HashSet(); + Set objectsOfInstancesFromA = new HashSet<>(); for (OWLNamedIndividual ind : instancesA) { Set axioms = ontology.getObjectPropertyAssertionAxioms(ind); for (OWLObjectPropertyAssertionAxiom axiom : axioms) { @@ -52,7 +68,7 @@ public static void EntityDependencyMatrix(OWLOntology ontology) { } } - Set objectsOfInstancesFromB = new HashSet(); + Set objectsOfInstancesFromB = new HashSet<>(); for (OWLNamedIndividual ind : instancesB) { Set axioms = ontology.getObjectPropertyAssertionAxioms(ind); for (OWLObjectPropertyAssertionAxiom axiom : axioms) { @@ -76,11 +92,12 @@ public static void EntityDependencyMatrix(OWLOntology ontology) { * entity1 depends on entity2 might be different from what * entity2 depends on entity1. * - * @param entity1 - * @param entity2 + * @param entity1 the first entity + * @param entity2 the second entity + * @return the dependency value */ - public void getDependency(OWLEntity entity1, OWLEntity entity2){ - + public double getDependency(OWLEntity entity1, OWLEntity entity2){ + return 0; } } diff --git a/components-core/src/main/java/org/dllearner/algorithms/schema/SchemaGenerator.java b/components-core/src/main/java/org/dllearner/algorithms/schema/SchemaGenerator.java index 994292626d..3bde65a392 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/schema/SchemaGenerator.java +++ b/components-core/src/main/java/org/dllearner/algorithms/schema/SchemaGenerator.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.schema; diff --git a/components-core/src/main/java/org/dllearner/algorithms/schema/SimpleSchemaGenerator.java b/components-core/src/main/java/org/dllearner/algorithms/schema/SimpleSchemaGenerator.java index 8f3eb928d8..4433d94dfa 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/schema/SimpleSchemaGenerator.java +++ b/components-core/src/main/java/org/dllearner/algorithms/schema/SimpleSchemaGenerator.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.schema; diff --git a/components-core/src/main/java/org/dllearner/algorithms/semkernel/SemKernel.java b/components-core/src/main/java/org/dllearner/algorithms/semkernel/SemKernel.java index 6300b6db30..26b00b1d6a 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/semkernel/SemKernel.java +++ b/components-core/src/main/java/org/dllearner/algorithms/semkernel/SemKernel.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.semkernel; import java.io.BufferedOutputStream; @@ -19,14 +37,15 @@ import edu.berkeley.compbio.jlibsvm.multi.MultiClassModel.OneVsAllMode; public class SemKernel extends AbstractComponent { - public static enum SvmType { + public enum SvmType { C_SVC, NU_SVC, ONE_CLASS, EPSILON_SVR, NU_SVR - }; - public static enum ScalingMode { NONE, LINEAR, ZSCORE } + } + + public enum ScalingMode { NONE, LINEAR, ZSCORE } private boolean useCrossValidation; private static final Float UNSPECIFIED_GAMMA = -1F; diff --git a/components-core/src/main/java/org/dllearner/algorithms/tdts/package-info.java b/components-core/src/main/java/org/dllearner/algorithms/tdts/package-info.java index 24d92e6f04..f3f9480f1b 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/tdts/package-info.java +++ b/components-core/src/main/java/org/dllearner/algorithms/tdts/package-info.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ /** * */ diff --git a/components-core/src/main/java/org/dllearner/core/AbstractAxiomLearningAlgorithm.java b/components-core/src/main/java/org/dllearner/core/AbstractAxiomLearningAlgorithm.java index f6f26d1f0d..aa127d8b95 100644 --- a/components-core/src/main/java/org/dllearner/core/AbstractAxiomLearningAlgorithm.java +++ b/components-core/src/main/java/org/dllearner/core/AbstractAxiomLearningAlgorithm.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; import java.net.SocketTimeoutException; @@ -37,6 +36,7 @@ import org.aksw.jena_sparql_api.core.QueryExecutionFactory; import org.aksw.jena_sparql_api.model.QueryExecutionFactoryModel; import org.dllearner.algorithms.properties.ObjectPropertyCharacteristicsAxiomLearner; +import org.dllearner.core.annotations.Unused; import org.dllearner.core.config.ConfigOption; import org.dllearner.core.owl.ClassHierarchy; import org.dllearner.kb.LocalModelBasedSparqlEndpointKS; @@ -83,19 +83,20 @@ */ public abstract class AbstractAxiomLearningAlgorithm extends AbstractComponent implements AxiomLearningAlgorithm{ + @Unused protected LearningProblem learningProblem; protected final Logger logger; protected NumberFormat format = DecimalFormat.getPercentInstance(); - @ConfigOption(name="maxExecutionTimeInSeconds", defaultValue="10", description="") + @ConfigOption(name="maxExecutionTimeInSeconds", defaultValue="10", description="maximum execution of the algorithm in seconds (abstract)") protected int maxExecutionTimeInSeconds = 10; - @ConfigOption(name="returnOnlyNewAxioms", defaultValue="false", description="") + @ConfigOption(name="returnOnlyNewAxioms", defaultValue="false", description="omit axioms already existing in the knowledge base") protected boolean returnOnlyNewAxioms; @ConfigOption(name="maxFetchedRows", description="The maximum number of rows fetched from the endpoint to approximate the result.") protected int maxFetchedRows; - + @ConfigOption(description = "the sparql endpoint knowledge source") protected SparqlEndpointKS ks; // the instances which are set @@ -103,6 +104,7 @@ public abstract class AbstractAxiomLearningAlgorithm allowedNamespaces = new ArrayList(); + protected List allowedNamespaces = new ArrayList<>(); protected ParameterizedSparqlString iterativeQueryTemplate; @@ -140,13 +143,15 @@ public abstract class AbstractAxiomLearningAlgorithm axiomType; + @ConfigOption(description = "the OWL entity to learn about") protected E entityToDescribe; + @Unused protected boolean useSampling = true; protected int popularity; public AbstractAxiomLearningAlgorithm() { - existingAxioms = new TreeSet(); + existingAxioms = new TreeSet<>(); logger = LoggerFactory.getLogger(this.getClass()); } @@ -164,7 +169,7 @@ public LearningProblem getLearningProblem() { @Override public void setLearningProblem(LearningProblem learningProblem) { this.learningProblem = learningProblem; - } + } /** * @param entityToDescribe the entityToDescribe to set @@ -233,11 +238,11 @@ public void setForceSPARQL_1_0_Mode(boolean forceSPARQL_1_0_Mode) { @Override public void start() { - logger.info("Started learning of " + axiomType.getName() + " axioms for " + + logger.info("Started learning of " + axiomType.getName() + " axioms for " + OWLAPIUtils.getPrintName(entityToDescribe.getEntityType()) + " " + entityToDescribe.toStringID() + "..."); startTime = System.currentTimeMillis(); - currentlyBestAxioms = new TreeSet>(); + currentlyBestAxioms = new TreeSet<>(); popularity = reasoner.getPopularity(entityToDescribe); if(popularity == 0){ @@ -266,7 +271,7 @@ public void start() { progressMonitor.learningStopped(axiomType); } - logger.info("...finished learning of " + axiomType.getName() + logger.info("...finished learning of " + axiomType.getName() + " axioms for " + OWLAPIUtils.getPrintName(entityToDescribe.getEntityType()) + " " + entityToDescribe.toStringID() + " in {}ms.", (System.currentTimeMillis()-startTime)); if(this instanceof ObjectPropertyCharacteristicsAxiomLearner){ @@ -287,7 +292,7 @@ private void generateSample(){ qef = new QueryExecutionFactoryModel(sample); reasoner = new SPARQLReasoner(qef); - // get the page size + // get the page size //TODO put to base class long pageSize = 10000;//PaginationUtils.adjustPageSize(globalQef, 10000); @@ -334,8 +339,7 @@ public void init() throws ComponentInitException { } /** - * Get the defined axioms in the knowledge base. - * @return + * Compute the defined axioms in the knowledge base. */ protected abstract void getExistingAxioms(); @@ -346,7 +350,7 @@ public void init() throws ComponentInitException { /** * The SPARQL CONSTRUCT query used to generate a sample for the given axiom type and entity. - * @return + * @return the SPARQL query */ protected abstract ParameterizedSparqlString getSampleQuery(); @@ -355,6 +359,7 @@ public List getCurrentlyBestAxioms() { return getCurrentlyBestAxioms(Integer.MAX_VALUE); } + @Override public List getCurrentlyBestAxioms(int nrOfAxioms) { return getCurrentlyBestAxioms(nrOfAxioms, 0.0); } @@ -369,7 +374,7 @@ public boolean isTimeout(){ public List getCurrentlyBestAxioms(int nrOfAxioms, double accuracyThreshold) { - List bestAxioms = new ArrayList(); + List bestAxioms = new ArrayList<>(); for(EvaluatedAxiom evAx : getCurrentlyBestEvaluatedAxioms(nrOfAxioms, accuracyThreshold)){ bestAxioms.add(evAx.getAxiom()); } @@ -377,7 +382,7 @@ public List getCurrentlyBestAxioms(int nrOfAxioms, } public List getCurrentlyBestAxioms(double accuracyThreshold) { - List bestAxioms = new ArrayList(); + List bestAxioms = new ArrayList<>(); for(EvaluatedAxiom evAx : getCurrentlyBestEvaluatedAxioms(accuracyThreshold)){ bestAxioms.add(evAx.getAxiom()); } @@ -392,10 +397,12 @@ public EvaluatedAxiom getCurrentlyBestEvaluatedAxiom() { return currentlyBestEvaluatedAxioms.get(0); } + @Override public List> getCurrentlyBestEvaluatedAxioms() { - return new ArrayList>(currentlyBestAxioms); + return new ArrayList<>(currentlyBestAxioms); } + @Override public List> getCurrentlyBestEvaluatedAxioms(int nrOfAxioms) { return getCurrentlyBestEvaluatedAxioms(nrOfAxioms, 0.0); } @@ -404,9 +411,10 @@ public List> getCurrentlyBestEvaluatedAxioms(double accuracyTh return getCurrentlyBestEvaluatedAxioms(Integer.MAX_VALUE, accuracyThreshold); } + @Override public List> getCurrentlyBestEvaluatedAxioms(int nrOfAxioms, double accuracyThreshold) { - List> returnList = new ArrayList>(); + List> returnList = new ArrayList<>(); //get the currently best evaluated axioms List> currentlyBestEvAxioms = getCurrentlyBestEvaluatedAxioms(); @@ -428,16 +436,16 @@ public List> getCurrentlyBestEvaluatedAxioms(int nrOfAxioms, public EvaluatedAxiom getBestEvaluatedAxiom(){ if(!currentlyBestAxioms.isEmpty()){ - return new TreeSet>(currentlyBestAxioms).last(); + return new TreeSet<>(currentlyBestAxioms).last(); } return null; } protected Set getAllClasses() { if(ks.isRemote()){ - return new SPARQLTasks(((SparqlEndpointKS) ks).getEndpoint()).getAllClasses(); + return new SPARQLTasks(ks.getEndpoint()).getAllClasses(); } else { - Set classes = new TreeSet(); + Set classes = new TreeSet<>(); for(OntClass cls : ((LocalModelBasedSparqlEndpointKS)ks).getModel().listClasses().filterDrop(new OWLFilter()).filterDrop(new RDFSFilter()).filterDrop(new RDFFilter()).toList()){ if(!cls.isAnon()){ classes.add(df.getOWLClass(IRI.create(cls.getURI()))); @@ -507,7 +515,7 @@ protected boolean executeAskQuery(String query){ } protected > List> sortByValues(Map map){ - List> entries = new ArrayList>(map.entrySet()); + List> entries = new ArrayList<>(map.entrySet()); Collections.sort(entries, new Comparator>() { @Override @@ -523,12 +531,11 @@ protected long getRemainingRuntimeInMilliSeconds(){ } protected boolean terminationCriteriaSatisfied(){ - boolean timeLimitExceeded = maxExecutionTimeInSeconds == 0 ? false : getRemainingRuntimeInMilliSeconds() <= 0; - return timeLimitExceeded ; + return maxExecutionTimeInSeconds != 0 && getRemainingRuntimeInMilliSeconds() <= 0; } protected List> sortByValues(Map map, final boolean useHierachy){ - List> entries = new ArrayList>(map.entrySet()); + List> entries = new ArrayList<>(map.entrySet()); final ClassHierarchy hierarchy = reasoner.getClassHierarchy(); Collections.sort(entries, new Comparator>() { @@ -554,7 +561,7 @@ public int compare(Entry o1, Entry getPositiveExamples(EvaluatedAxiom axiom){ ResultSet rs = executeSelectQuery(posExamplesQueryTemplate.toString()); - Set posExamples = new TreeSet(); + Set posExamples = new TreeSet<>(); RDFNode node; while(rs.hasNext()){ @@ -616,7 +623,7 @@ public Set getNegativeExamples(EvaluatedAxiom axiom){ ResultSet rs = executeSelectQuery(negExamplesQueryTemplate.toString()); - Set negExamples = new TreeSet(); + Set negExamples = new TreeSet<>(); while(rs.hasNext()){ RDFNode node = rs.next().get("s"); @@ -658,8 +665,8 @@ public long getEvaluatedFramentSize(){ /** * Converts a JENA API Literal object into an OWL API OWLLiteral object. * - * @param lit - * @return + * @param lit the JENA API literal + * @return the OWL API literal */ protected OWLLiteral convertLiteral(Literal lit) { String datatypeURI = lit.getDatatypeURI(); @@ -673,7 +680,7 @@ protected OWLLiteral convertLiteral(Literal lit) { } public static void printSubset(Collection collection, int maxSize){ - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); int i = 0; Iterator iter = collection.iterator(); while(iter.hasNext() && i < maxSize){ @@ -693,8 +700,7 @@ protected , V> void addToMap(Map map, K key, V value ){ values = (J) value.getClass().newInstance(); values.add(value); } - catch (InstantiationException e) {e.printStackTrace();return;} - catch (IllegalAccessException e) {e.printStackTrace();return;} + catch (InstantiationException | IllegalAccessException e) {e.printStackTrace();return;} } values.add(value); } @@ -704,9 +710,7 @@ protected , V> void addToMap(Map map, K key, Collection if(values == null){ try { values = (J) newValues.getClass().newInstance(); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { + } catch (InstantiationException | IllegalAccessException e) { e.printStackTrace(); } map.put(key, values); @@ -714,10 +718,6 @@ protected , V> void addToMap(Map map, K key, Collection values.addAll(newValues); } - private void adaptChunkCount(){ - - } - @Autowired public void setKs(SparqlEndpointKS ks) { this.ks = ks; diff --git a/components-core/src/main/java/org/dllearner/core/AbstractAxiomScoreCalculator.java b/components-core/src/main/java/org/dllearner/core/AbstractAxiomScoreCalculator.java index f418c33ef1..3a97715212 100644 --- a/components-core/src/main/java/org/dllearner/core/AbstractAxiomScoreCalculator.java +++ b/components-core/src/main/java/org/dllearner/core/AbstractAxiomScoreCalculator.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.core; import org.aksw.jena_sparql_api.core.QueryExecutionFactory; diff --git a/components-core/src/main/java/org/dllearner/core/AbstractCELA.java b/components-core/src/main/java/org/dllearner/core/AbstractCELA.java index 1b555b75ea..ac84533406 100644 --- a/components-core/src/main/java/org/dllearner/core/AbstractCELA.java +++ b/components-core/src/main/java/org/dllearner/core/AbstractCELA.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; import java.text.DecimalFormat; @@ -25,6 +24,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.NavigableSet; import java.util.Set; import java.util.TreeSet; import java.util.concurrent.TimeUnit; @@ -33,11 +33,14 @@ import org.dllearner.core.owl.ClassHierarchy; import org.dllearner.core.owl.DatatypePropertyHierarchy; import org.dllearner.core.owl.ObjectPropertyHierarchy; -import org.dllearner.learningproblems.PosNegLPStandard; +import org.dllearner.learningproblems.AccMethodFMeasure; +import org.dllearner.learningproblems.AccMethodPredAcc; +import org.dllearner.learningproblems.AccMethodTwoValued; +import org.dllearner.learningproblems.PosNegLP; import org.dllearner.utilities.Helper; +import org.dllearner.utilities.ReasoningUtils; import org.dllearner.utilities.datastructures.DescriptionSubsumptionTree; import org.dllearner.utilities.owl.ConceptTransformation; -import org.dllearner.utilities.owl.DLSyntaxObjectRenderer; import org.dllearner.utilities.owl.EvaluatedDescriptionSet; import org.dllearner.utilities.owl.OWLClassExpressionMinimizer; import org.joda.time.Period; @@ -49,11 +52,14 @@ import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLDataFactory; import org.semanticweb.owlapi.model.OWLDataProperty; +import org.semanticweb.owlapi.model.OWLIndividual; import org.semanticweb.owlapi.model.OWLObjectIntersectionOf; import org.semanticweb.owlapi.model.OWLObjectProperty; import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom; import org.semanticweb.owlapi.util.OWLObjectDuplicator; import org.semanticweb.owlapi.vocab.OWLRDFVocabulary; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl; @@ -79,7 +85,9 @@ */ public abstract class AbstractCELA extends AbstractComponent implements ClassExpressionLearningAlgorithm, StoppableLearningAlgorithm { - protected OWLObjectRenderer renderer = new DLSyntaxObjectRenderer(); + private static final Logger logger = LoggerFactory.getLogger(AbstractCELA.class); + + protected OWLObjectRenderer renderer = StringRenderer.getRenderer(); protected EvaluatedDescriptionSet bestEvaluatedDescriptions = new EvaluatedDescriptionSet(AbstractCELA.MAX_NR_OF_RESULTS); protected DecimalFormat dfPercent = new DecimalFormat("0.00%"); @@ -100,6 +108,9 @@ public abstract class AbstractCELA extends AbstractComponent implements ClassExp @ConfigOption(name="useMinimizer", defaultValue="true", description="Specifies whether returned expressions should be minimised by removing those parts, which are not needed. (Basically the minimiser tries to find the shortest expression which is equivalent to the learned expression). Turning this feature off may improve performance.") private boolean useMinimizer = true; + + @ConfigOption(defaultValue = "10", name = "maxExecutionTimeInSeconds", description = "maximum execution of the algorithm in seconds") + protected int maxExecutionTimeInSeconds = 10; /** * The learning problem variable, which must be used by @@ -133,9 +144,8 @@ public abstract class AbstractCELA extends AbstractComponent implements ClassExp /** * Default Constructor */ - public AbstractCELA(){ - - } + public AbstractCELA(){} + /** * Each learning algorithm gets a learning problem and * a reasoner as input. @@ -146,15 +156,15 @@ public AbstractCELA(){ public AbstractCELA(AbstractClassExpressionLearningProblem learningProblem, AbstractReasonerComponent reasoningService) { this.learningProblem = learningProblem; this.reasoner = reasoningService; -// +// // baseURI = reasoner.getBaseURI(); -// prefixes = reasoner.getPrefixes(); +// prefixes = reasoner.getPrefixes(); } /** * Call this when you want to change the learning problem, but * leave everything else as is. Method can be used to apply - * a configured algorithm to different learning problems. + * a configured algorithm to different learning problems. * Implementations, which do not only use the provided learning * algorithm variable, must make sure that a call to this method * indeed changes the learning problem. @@ -167,7 +177,7 @@ public void changeLearningProblem(AbstractClassExpressionLearningProblem learnin /** * Call this when you want to change the reasoning service, but * leave everything else as is. Method can be used to use - * a configured algorithm with different reasoners. + * a configured algorithm with different reasoners. * Implementations, which do not only use the provided reasoning * service class variable, must make sure that a call to this method * indeed changes the reasoning service. @@ -179,9 +189,9 @@ public void changeReasonerComponent(AbstractReasonerComponent reasoningService) /** * This is the maximum number of results, which the learning - * algorithms are asked to store. (Note, that algorithms are not + * algorithms are asked to store. (Note, that algorithms are not * required to store any results except the best one, so this limit - * is used to limit the performance cost for those which + * is used to limit the performance cost for those which * choose to store results.) */ public static final int MAX_NR_OF_RESULTS = 100; @@ -217,6 +227,7 @@ public List getCurrentlyBestDescriptions() { * @param nrOfDescriptions Limit for the number or returned descriptions. * @return The best class descriptions found by the learning algorithm so far. */ + @Override public synchronized List getCurrentlyBestDescriptions(int nrOfDescriptions) { return getCurrentlyBestDescriptions(nrOfDescriptions, false); } @@ -224,13 +235,13 @@ public synchronized List getCurrentlyBestDescriptions(int nr /** * @see #getCurrentlyBestEvaluatedDescriptions(int,double,boolean) * @param nrOfDescriptions Limit for the number or returned descriptions. - * @param filterNonMinimalDescriptions Remove non-minimal descriptions (e.g. those which can be shortened + * @param filterNonMinimalDescriptions Remove non-minimal descriptions (e.g. those which can be shortened * to an equivalent concept) from the returned set. * @return The best class descriptions found by the learning algorithm so far. */ public synchronized List getCurrentlyBestDescriptions(int nrOfDescriptions, boolean filterNonMinimalDescriptions) { List currentlyBest = getCurrentlyBestDescriptions(); - List returnList = new LinkedList(); + List returnList = new LinkedList<>(); for(OWLClassExpression ed : currentlyBest) { if(returnList.size() >= nrOfDescriptions) { return returnList; @@ -242,7 +253,7 @@ public synchronized List getCurrentlyBestDescriptions(int nr } return returnList; - } + } /** * Returns the best descriptions obtained so far. @@ -258,7 +269,7 @@ public EvaluatedDescription getCurrentlyBestEvaluatedDescriptio * last. (In Java, iterators traverse a SortedSet in ascending order.) * @return Best class descriptions found so far. */ - public TreeSet> getCurrentlyBestEvaluatedDescriptions() { + public NavigableSet> getCurrentlyBestEvaluatedDescriptions() { return bestEvaluatedDescriptions.getSet(); } @@ -270,10 +281,10 @@ public TreeSet> getCurrentlyBest * * @param accuracyThreshold Minimum accuracy. All class descriptions with lower * accuracy are disregarded. Specify a value between 0.0 and 1.0. Use 0.0 if - * you do not want this filter to be active. + * you do not want this filter to be active. * - * @param filterNonMinimalDescriptions If true, non-minimal descriptions are - * filtered, e.g. ALL r.TOP (being equivalent to TOP), male AND male (can be + * @param filterNonMinimalDescriptions If true, non-minimal descriptions are + * filtered, e.g. ALL r.TOP (being equivalent to TOP), male AND male (can be * shortened to male). Currently, non-minimal descriptions are just skipped, * i.e. they are completely omitted from the return list. Later, implementation * might be changed to return shortened versions of those descriptions. @@ -281,7 +292,7 @@ public TreeSet> getCurrentlyBest * @return A list of currently best class descriptions. */ public synchronized List> getCurrentlyBestEvaluatedDescriptions(int nrOfDescriptions, double accuracyThreshold, boolean filterNonMinimalDescriptions) { - TreeSet> currentlyBest = getCurrentlyBestEvaluatedDescriptions(); + NavigableSet> currentlyBest = getCurrentlyBestEvaluatedDescriptions(); List> returnList = new LinkedList<>(); for(EvaluatedDescription ed : currentlyBest.descendingSet()) { // once we hit a OWLClassExpression with a below threshold accuracy, we simply return @@ -322,6 +333,7 @@ public synchronized List> getCur * @param nrOfDescriptions Maximum number of descriptions returned. * @return Return value is getCurrentlyBestDescriptions(nrOfDescriptions, 0.0, false). */ + @Override public synchronized List> getCurrentlyBestEvaluatedDescriptions(int nrOfDescriptions) { return getCurrentlyBestEvaluatedDescriptions(nrOfDescriptions, 0.0, false); } @@ -344,16 +356,16 @@ public synchronized List> getCu /** * Returns all learning problems supported by this component. This can be used to indicate that, e.g. - * an algorithm is only suitable for positive only learning. + * an algorithm is only suitable for positive only learning. * @return All classes implementing learning problems, which are supported by this learning algorithm. */ public static Collection> supportedLearningProblems() { - return new LinkedList>(); + return new LinkedList<>(); } // central function for printing description protected String descriptionToString(OWLClassExpression description) { - return description.toString();//OWLAPIRenderers.toManchesterOWLSyntax(description); + return renderer.render(description); } @@ -364,8 +376,21 @@ protected String getSolutionString() { // temporary code OWLClassExpression description = ed.getDescription(); String descriptionString = descriptionToString(description); - if(learningProblem instanceof PosNegLPStandard) { - str += current + ": " + descriptionString + " (pred. acc.: " + dfPercent.format(((PosNegLPStandard)learningProblem).getPredAccuracyOrTooWeakExact(description,1)) + ", F-measure: "+ dfPercent.format(((PosNegLPStandard)learningProblem).getFMeasureOrTooWeakExact(description,1)) + ")\n"; + if(learningProblem instanceof PosNegLP) { + Set positiveExamples = ((PosNegLP)learningProblem).getPositiveExamples(); + Set negativeExamples = ((PosNegLP)learningProblem).getNegativeExamples(); + ReasoningUtils reasoningUtil = learningProblem.getReasoningUtil(); + + str += current + ": " + descriptionString + " (pred. acc.: " + + dfPercent.format(reasoningUtil.getAccuracyOrTooWeak2(new AccMethodPredAcc(true), description, positiveExamples, negativeExamples, 1)) + + ", F-measure: "+ dfPercent.format(reasoningUtil.getAccuracyOrTooWeak2(new AccMethodFMeasure(true), description, positiveExamples, negativeExamples, 1)); + + AccMethodTwoValued accuracyMethod = ((PosNegLP)learningProblem).getAccuracyMethod(); + if ( !(accuracyMethod instanceof AccMethodPredAcc) + && !(accuracyMethod instanceof AccMethodFMeasure) ) { + str += ", " + AnnComponentManager.getName(accuracyMethod) + ": " + dfPercent.format(ed.getAccuracy()); + } + str += ")\n"; } else { str += current + ": " + descriptionString + " " + dfPercent.format(ed.getAccuracy()) + "\n"; // System.out.println(ed); @@ -375,7 +400,29 @@ protected String getSolutionString() { return str; } + /** + * Computes an internal class hierarchy that only contains classes + * that are allowed. + * @return optimized class hierarchy + */ protected ClassHierarchy initClassHierarchy() { + // we ignore all unsatisfiable classes + Set unsatisfiableClasses = null; + try { + unsatisfiableClasses = reasoner.getInconsistentClasses(); + } catch (UnsupportedOperationException e) { + logger.warn("Ignoring unsatisfiable check due to "+e.getStackTrace()[0]); + } + if(unsatisfiableClasses != null && !unsatisfiableClasses.isEmpty()) { + logger.warn("Ignoring unsatsifiable classes " + unsatisfiableClasses); + if(ignoredConcepts == null) { + ignoredConcepts = unsatisfiableClasses; + } else { + ignoredConcepts.addAll(unsatisfiableClasses); + } + } + + Set usedConcepts; if(allowedConcepts != null) { // sanity check to control if no non-existing concepts are in the list @@ -392,6 +439,11 @@ protected ClassHierarchy initClassHierarchy() { return hierarchy; } + /** + * Computes an internal object property hierarchy that only contains + * object properties that are allowed. + * @return optimized object property hierarchy + */ protected ObjectPropertyHierarchy initObjectPropertyHierarchy() { Set usedProperties; if(allowedObjectProperties != null) { @@ -410,6 +462,11 @@ protected ObjectPropertyHierarchy initObjectPropertyHierarchy() { return hierarchy; } + /** + * Computes an internal data property hierarchy that only contains + * data properties that are allowed. + * @return optimized data property hierarchy + */ protected DatatypePropertyHierarchy initDataPropertyHierarchy() { Set usedProperties; if(allowedDataProperties != null) { @@ -427,6 +484,10 @@ protected DatatypePropertyHierarchy initDataPropertyHierarchy() { return hierarchy; } + protected boolean isTimeExpired() { + return getCurrentRuntimeInMilliSeconds() >= TimeUnit.SECONDS.toMillis(maxExecutionTimeInSeconds); + } + protected long getCurrentRuntimeInMilliSeconds() { return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - nanoStartTime); } @@ -466,14 +527,14 @@ protected OWLClassExpression rewrite(OWLClassExpression ce) { * all learning algorithm implementations. */ @Override - public AbstractClassExpressionLearningProblem getLearningProblem() { + public AbstractClassExpressionLearningProblem getLearningProblem() { return learningProblem; } @Autowired @Override public void setLearningProblem(LearningProblem learningProblem) { - this.learningProblem = (AbstractClassExpressionLearningProblem) learningProblem; + this.learningProblem = (AbstractClassExpressionLearningProblem)learningProblem; } /** @@ -500,7 +561,7 @@ public void stop() { @Override public boolean isRunning() { return isRunning; - } + } public Set getAllowedConcepts() { return allowedConcepts; @@ -581,24 +642,40 @@ public boolean isUseMinimizer() { public void setUseMinimizer(boolean useMinimizer) { this.useMinimizer = useMinimizer; } + + public int getMaxExecutionTimeInSeconds() { + return maxExecutionTimeInSeconds; + } + + public void setMaxExecutionTimeInSeconds(int maxExecutionTimeInSeconds) { + this.maxExecutionTimeInSeconds = maxExecutionTimeInSeconds; + } + + /** + * @param renderer the renderer of OWL objects to set + */ + public void setRenderer(OWLObjectRenderer renderer) { + this.renderer = renderer; + } /** - * Replace role fillers with the range of the property, if exists. - * @param d - * @return + * The goal of this method is to rewrite the class expression CE, to get a more informative one by e.g. + *
  • replacing the role fillers in CE with the range of the property, if exists.
+ * @param ce the class expression + * @return the modified class expression */ - protected OWLClassExpression getNiceDescription(OWLClassExpression d){ - OWLClassExpression rewrittenClassExpression = d; - if(d instanceof OWLObjectIntersectionOf){ - Set newOperands = new TreeSet(((OWLObjectIntersectionOf) d).getOperands()); - for (OWLClassExpression operand : ((OWLObjectIntersectionOf) d).getOperands()) { + protected OWLClassExpression getNiceDescription(OWLClassExpression ce){ + OWLClassExpression rewrittenClassExpression = ce; + if(ce instanceof OWLObjectIntersectionOf){ + Set newOperands = new TreeSet<>(((OWLObjectIntersectionOf) ce).getOperands()); + for (OWLClassExpression operand : ((OWLObjectIntersectionOf) ce).getOperands()) { newOperands.add(getNiceDescription(operand)); } rewrittenClassExpression = dataFactory.getOWLObjectIntersectionOf(newOperands); - } else if(d instanceof OWLObjectSomeValuesFrom) { + } else if(ce instanceof OWLObjectSomeValuesFrom) { // \exists r.\bot \equiv \bot - OWLObjectProperty property = ((OWLObjectSomeValuesFrom) d).getProperty().asOWLObjectProperty(); - OWLClassExpression filler = ((OWLObjectSomeValuesFrom) d).getFiller(); + OWLObjectProperty property = ((OWLObjectSomeValuesFrom) ce).getProperty().asOWLObjectProperty(); + OWLClassExpression filler = ((OWLObjectSomeValuesFrom) ce).getFiller(); if(filler.isOWLThing()) { OWLClassExpression range = reasoner.getRange(property); filler = range; diff --git a/components-core/src/main/java/org/dllearner/core/AbstractClassExpressionLearningProblem.java b/components-core/src/main/java/org/dllearner/core/AbstractClassExpressionLearningProblem.java index 2e2129b5c4..b551a5a28f 100644 --- a/components-core/src/main/java/org/dllearner/core/AbstractClassExpressionLearningProblem.java +++ b/components-core/src/main/java/org/dllearner/core/AbstractClassExpressionLearningProblem.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,30 +16,80 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; +import org.dllearner.utilities.ReasoningUtils; import org.semanticweb.owlapi.model.OWLClassExpression; +import org.semanticweb.owlapi.model.OWLDataFactory; +import org.springframework.beans.factory.annotation.Autowired; +import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; /** * Base class for all class expression learning problems. * - * @author Lorent Buehmann + * @author Lorenz Buehmann * */ public abstract class AbstractClassExpressionLearningProblem extends AbstractLearningProblem> implements LearningProblem { - + + protected OWLDataFactory dataFactory = new OWLDataFactoryImpl(); + public AbstractClassExpressionLearningProblem(){ } + + protected ReasoningUtils reasoningUtil; + protected static Class reasoningUtilsClass = ReasoningUtils.class; + + private void setReasonerAndUtil(AbstractReasonerComponent reasoner) { + if (this.reasoner != reasoner) { + if (this.reasoningUtil == null) { + this.reasoningUtil = newReasoningUtils(reasoner); + } else { + this.reasoningUtil.setReasoner(reasoner); + } + + this.reasoningUtil.init(); + } + this.reasoner = reasoner; + } + + /** + * Factory method to get problem-specific reasoning utils + * @param reasoner the current reasoner + * @return a reasoning util (by default the generic one) + */ + protected ReasoningUtils newReasoningUtils(AbstractReasonerComponent reasoner) { + return new ReasoningUtils(reasoner); + } + /** * Constructs a learning problem using a reasoning service for - * querying the background knowledge. It can be used for + * querying the background knowledge. It can be used for * evaluating solution candidates. - * @param reasoner The reasoning service used as + * @param reasoner The reasoning service used as * background knowledge. */ public AbstractClassExpressionLearningProblem(AbstractReasonerComponent reasoner) { - this.reasoner = reasoner; + setReasonerAndUtil(reasoner); + } + + @Override + @Autowired(required=false) + public void setReasoner(AbstractReasonerComponent reasoner) { + setReasonerAndUtil(reasoner); + } + + @Override + public void changeReasonerComponent(AbstractReasonerComponent reasoner) { + setReasonerAndUtil(reasoner); + } + + public ReasoningUtils getReasoningUtil() { + return reasoningUtil; + } + + public void setReasoningUtil(ReasoningUtils reasoningUtil) { + this.reasoningUtil = reasoningUtil; } } diff --git a/components-core/src/main/java/org/dllearner/core/AbstractComponent.java b/components-core/src/main/java/org/dllearner/core/AbstractComponent.java index 5507b29256..809234bdcf 100644 --- a/components-core/src/main/java/org/dllearner/core/AbstractComponent.java +++ b/components-core/src/main/java/org/dllearner/core/AbstractComponent.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,16 +16,9 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; -import java.util.Collection; -import java.util.LinkedList; -import org.dllearner.core.options.ConfigEntry; -import org.dllearner.core.options.ConfigOption; -import org.dllearner.core.options.DoubleConfigOption; -import org.dllearner.core.options.InvalidConfigOptionValueException; /** * Base class of all components. See also http://dl-learner.org/wiki/Architecture. @@ -44,22 +37,10 @@ public boolean isInitialized() { return initialized; } -// protected Configurator configurator; - - /** - * For each component, a configurator class is generated in package - * org.dllearner.core.configurators using the script - * { org.dllearner.scripts.ConfigJavaGenerator}. The configurator - * provides set and get methods for the configuration options of - * a component. - * @return An object allowing to configure this component. - */ -// public abstract Configurator getConfigurator(); - /** - * Returns the name of this component. By default, "unnamed + * Returns the name of this component. By default, "unnamed * component" is returned, but all implementations of components - * are strongly encouraged to provide a static method returning + * are strongly encouraged to provide a static method returning * the name. * * Use the DLComponent annotation instead of setting a name through this method. @@ -71,43 +52,4 @@ public static String getName() { return "unnamed component"; } - /** - * Returns all configuration options supported by this component. - * @return A list of supported configuration options for this - * component. - */ - public static Collection> createConfigOptions() { - return new LinkedList>(); - } - - /** - * Applies a configuration option to this component. Implementations - * of components should use option and value of the config entry to - * perform an action (usually setting an internal variable to - * an appropriate value). - * - * Since the availability of configurators, it is optional for - * components to implement this method. Instead of using this method - * to take an action based on a configuration value, components can - * also use the getters defined in the components configurator. - * - * Important note: Never call this method directly. All calls are - * done via the {@link org.dllearner.core.ComponentManager}. - * - * @see #getConfigurator() - * @param Type of the config entry (Integer, String etc.). - * @param entry A configuration entry. - * @throws InvalidConfigOptionValueException This exception is thrown if the - * value of the config entry is not valid. For instance, a config option - * may only accept values, which are within intervals 0.1 to 0.3 or 0.5 to 0.8. - * If the value is outside of those intervals, an exception is thrown. Note - * that many of the common cases are already caught in the constructor of - * ConfigEntry (for instance for a {@link DoubleConfigOption} you can specify - * an interval for the value). This means that, as a component developer, you - * often do not need to implement further validity checks. - */ - protected void applyConfigEntry(ConfigEntry entry) throws InvalidConfigOptionValueException { - - } - } diff --git a/components-core/src/main/java/org/dllearner/core/AbstractHeuristic.java b/components-core/src/main/java/org/dllearner/core/AbstractHeuristic.java index 2d26307cd7..65be692996 100644 --- a/components-core/src/main/java/org/dllearner/core/AbstractHeuristic.java +++ b/components-core/src/main/java/org/dllearner/core/AbstractHeuristic.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,13 +16,11 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; import java.util.Comparator; import org.dllearner.algorithms.celoe.OENode; -import org.dllearner.core.config.ConfigOption; import com.google.common.collect.ComparisonChain; @@ -34,28 +32,14 @@ * @author Jens Lehmann * */ -@ComponentAnn(name = "OEHeuristicRuntime", shortName = "celoe_heuristic", version = 0.5) public abstract class AbstractHeuristic extends AbstractComponent implements Heuristic, Comparator{ - // strong penalty for long descriptions - private double expansionPenaltyFactor = 0.1; - // bonus for being better than parent node - private double gainBonusFactor = 0.3; - // penalty if a node OWLClassExpression has very many refinements since exploring - // such a node is computationally very expensive - private double nodeRefinementPenalty = 0.0001; - - @ConfigOption(name = "startNodeBonus", defaultValue="0.1") - private double startNodeBonus = 0.1; - - public AbstractHeuristic() { - - } + public AbstractHeuristic() {} @Override public void init() throws ComponentInitException { - } + } @Override public int compare(OENode node1, OENode node2) { @@ -67,35 +51,4 @@ public int compare(OENode node1, OENode node2) { public abstract double getNodeScore(OENode node); - public double getExpansionPenaltyFactor() { - return expansionPenaltyFactor; - } - - public double getGainBonusFactor() { - return gainBonusFactor; - } - - public void setGainBonusFactor(double gainBonusFactor) { - this.gainBonusFactor = gainBonusFactor; - } - - public double getNodeRefinementPenalty() { - return nodeRefinementPenalty; - } - - public void setNodeRefinementPenalty(double nodeRefinementPenalty) { - this.nodeRefinementPenalty = nodeRefinementPenalty; - } - - public void setExpansionPenaltyFactor(double expansionPenaltyFactor) { - this.expansionPenaltyFactor = expansionPenaltyFactor; - } - - public double getStartNodeBonus() { - return startNodeBonus; - } - - public void setStartNodeBonus(double startNodeBonus) { - this.startNodeBonus = startNodeBonus; - } } diff --git a/components-core/src/main/java/org/dllearner/core/AbstractKnowledgeSource.java b/components-core/src/main/java/org/dllearner/core/AbstractKnowledgeSource.java index 89a290e7fa..9fc5f32d11 100644 --- a/components-core/src/main/java/org/dllearner/core/AbstractKnowledgeSource.java +++ b/components-core/src/main/java/org/dllearner/core/AbstractKnowledgeSource.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; import java.io.File; diff --git a/components-core/src/main/java/org/dllearner/core/AbstractLearningProblem.java b/components-core/src/main/java/org/dllearner/core/AbstractLearningProblem.java index 92ef0aeeb5..5971989d6c 100644 --- a/components-core/src/main/java/org/dllearner/core/AbstractLearningProblem.java +++ b/components-core/src/main/java/org/dllearner/core/AbstractLearningProblem.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; import org.dllearner.core.config.ConfigOption; @@ -25,8 +24,8 @@ /** * Base class for all learning problems. - * See also the wiki page for - * DL-Learner-Architecture. + * See also the wiki page for + * DL-Learner-Architecture. * * @author Jens Lehmann * @@ -41,9 +40,9 @@ public AbstractLearningProblem(){ } /** * Constructs a learning problem using a reasoning service for - * querying the background knowledge. It can be used for + * querying the background knowledge. It can be used for * evaluating solution candidates. - * @param reasoner The reasoning service used as + * @param reasoner The reasoning service used as * background knowledge. */ public AbstractLearningProblem(AbstractReasonerComponent reasoner) { @@ -55,7 +54,7 @@ public AbstractLearningProblem(AbstractReasonerComponent reasoner) { * problem. * Implementations, which do not only use the provided reasoning * service class variable, must make sure that a call to this method - * indeed changes the reasoning service. + * indeed changes the reasoning service. * @param reasoner New reasoning service. */ public void changeReasonerComponent(AbstractReasonerComponent reasoner) { @@ -96,7 +95,7 @@ public T computeScore(V hypothesis) { * evaluated hypothesis of the correct type (ClassLearningProblem * returns EvaluatedDescriptionClass instead of generic EvaluatedDescription). * @param hypothesis Hypothesis to evaluate. - * @return + * @return an evaluated hypothesis */ public W evaluate(V hypothesis){ return evaluate(hypothesis, 1.0); @@ -108,7 +107,7 @@ public W evaluate(V hypothesis){ * returns EvaluatedDescriptionClass instead of generic EvaluatedDescription). * @param hypothesis Hypothesis to evaluate. * @param noise the (approximated) value of noise within the examples - * @return + * @return an evaluated hypothesis */ public W evaluate(V hypothesis, double noise) { return null; @@ -118,43 +117,32 @@ public W evaluate(V hypothesis, double noise) { * This method returns a value, which indicates how accurate a * hypothesis solves a learning problem. There can be different * ways to compute accuracy depending on the type of learning problem - * and other factors. However, all implementations are required to + * and other factors. However, all implementations are required to * return a value between 0 and 1, where 1 stands for the highest * possible accuracy and 0 for the lowest possible accuracy. * * @return A value between 0 and 1 indicating the quality (of a hypothesis). - */ - public double getAccuracy(V object) { - return getAccuracy(object, 0.0); + * or -1 as described above. + */ + public double getAccuracyOrTooWeak(V object) { + return getAccuracyOrTooWeak(object, 0.0); } /** - * This method returns a value, which indicates how accurate a - * hypothesis solves a learning problem. There can be different - * ways to compute accuracy depending on the type of learning problem - * and other factors. However, all implementations are required to - * return a value between 0 and 1, where 1 stands for the highest - * possible accuracy and 0 for the lowest possible accuracy. + * This method computes the accuracy and returns -1 instead of the accuracy if + * + *
    + *
  1. the accuracy of the hypothesis is below the given threshold and
  2. + *
  3. the accuracy of all more special w.r.t. subsumption hypotheses is below the given threshold.
  4. + *
* - * @param hypothesis Hypothesis to evaluate. - * @param noise the (approximated) value of noise within the examples - * - * @return A value between 0 and 1 indicating the quality (of a hypothesis). - */ - public abstract double getAccuracy(V hypothesis, double noise); - - /** - * This method computes the accuracy as {@link #getAccuracy(V)}, - * but returns -1 instead of the accuracy if 1.) the accuracy of the - * hypothesis is below the given threshold and 2.) the accuracy of all - * more special w.r.t. subsumption hypotheses is below the given threshold. * This is used for efficiency reasons, i.e. -1 can be returned instantly if - * it is clear that the hypothesis and all its refinements are not + * it is clear that the hypothesis and all its refinements are not * sufficiently accurate. * * @return A value between 0 and 1 indicating the quality (of a hypothesis) * or -1 as described above. - */ + */ public abstract double getAccuracyOrTooWeak(V hypothesis, double noise); /** diff --git a/components-core/src/main/java/org/dllearner/core/AbstractReasonerComponent.java b/components-core/src/main/java/org/dllearner/core/AbstractReasonerComponent.java index 87703e7479..dddbd09423 100644 --- a/components-core/src/main/java/org/dllearner/core/AbstractReasonerComponent.java +++ b/components-core/src/main/java/org/dllearner/core/AbstractReasonerComponent.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,24 +16,13 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; -import java.text.NumberFormat; -import java.text.ParseException; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeMap; -import java.util.TreeSet; - +import com.google.common.collect.HashMultimap; +import com.google.common.collect.Lists; +import com.google.common.collect.Multimap; +import com.google.common.collect.Sets; +import org.dllearner.core.config.ConfigOption; import org.dllearner.core.owl.ClassHierarchy; import org.dllearner.core.owl.DatatypePropertyHierarchy; import org.dllearner.core.owl.ObjectPropertyHierarchy; @@ -43,29 +32,16 @@ import org.dllearner.utilities.OWLAPIUtils; import org.dllearner.utilities.datastructures.SortedSetTuple; import org.dllearner.utilities.owl.OWLVocabulary; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLDataProperty; -import org.semanticweb.owlapi.model.OWLDataRange; -import org.semanticweb.owlapi.model.OWLDatatype; -import org.semanticweb.owlapi.model.OWLEntity; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLLiteral; -import org.semanticweb.owlapi.model.OWLObjectProperty; -import org.semanticweb.owlapi.model.OWLProperty; -import org.semanticweb.owlapi.vocab.OWL2Datatype; +import org.semanticweb.owlapi.model.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; - import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; -import com.google.common.collect.HashMultimap; -import com.google.common.collect.Multimap; -import com.google.common.collect.Sets; +import java.text.NumberFormat; +import java.text.ParseException; +import java.util.*; +import java.util.Map.Entry; /** * Abstract component representing a reasoner. Only a few reasoning operations @@ -101,6 +77,8 @@ public abstract class AbstractReasonerComponent extends AbstractComponent implem public static Logger logger = LoggerFactory.getLogger(AbstractReasonerComponent.class); private static final NumberFormat numberFormat = NumberFormat.getInstance(); + @ConfigOption(description = "whether to use single instance checks", defaultValue = "false") + protected boolean useInstanceChecks = false; // statistical data for particular reasoning operations private long instanceCheckReasoningTimeNs = 0; @@ -132,19 +110,23 @@ public abstract class AbstractReasonerComponent extends AbstractComponent implem protected ClassHierarchy subsumptionHierarchy = null; protected ObjectPropertyHierarchy roleHierarchy = null; protected DatatypePropertyHierarchy datatypePropertyHierarchy = null; - + + @ConfigOption(description = "if class hierarchy should be precomputed", defaultValue = "true") protected boolean precomputeClassHierarchy = true; + @ConfigOption(defaultValue = "true") protected boolean precomputeObjectPropertyHierarchy = true; + @ConfigOption(defaultValue = "true") protected boolean precomputeDataPropertyHierarchy = true; protected OWLDataFactory df = new OWLDataFactoryImpl(); protected Multimap datatype2Properties = HashMultimap.create(); - protected Map dataproperty2datatype = new HashMap(); + protected Map dataproperty2datatype = new HashMap<>(); /** * The underlying knowledge sources. */ + @ConfigOption(description = "the underlying knowledge sources", required = true) protected Set sources; @@ -181,7 +163,7 @@ public void setSources(Set sources){ @Autowired public void setSources(KnowledgeSource... sources) { - this.sources = new HashSet(Arrays.asList(sources)); + this.sources = new HashSet<>(Arrays.asList(sources)); } /** @@ -385,7 +367,7 @@ public final Set isSuperClassOf(Set supe protected Set isSuperClassOfImpl(Set superConcepts, OWLClassExpression subConcept) throws ReasoningMethodUnsupportedException { - Set returnSet = new HashSet(); + Set returnSet = new HashSet<>(); for (OWLClassExpression superConcept : superConcepts) { if (isSuperClassOf(superConcept, subConcept)) returnSet.add(superConcept); @@ -506,7 +488,7 @@ public final SortedSet hasType(OWLClassExpression concept, Set hasTypeImpl(OWLClassExpression concept, Set individuals) throws ReasoningMethodUnsupportedException { - SortedSet returnSet = new TreeSet(); + SortedSet returnSet = new TreeSet<>(); for (OWLIndividual individual : individuals) { if (hasType(concept, individual)) returnSet.add(individual); @@ -696,10 +678,10 @@ public final Map> getDoubleDatatypeMembers( protected Map> getDoubleDatatypeMembersImpl( OWLDataProperty datatypeProperty) throws ReasoningMethodUnsupportedException { Map> mapping = getDatatypeMembersImpl(datatypeProperty); - Map> ret = new TreeMap>(); + Map> ret = new TreeMap<>(); for (Entry> e : mapping.entrySet()) { SortedSet values = e.getValue(); - SortedSet valuesDouble = new TreeSet(); + SortedSet valuesDouble = new TreeSet<>(); for (OWLLiteral lit : values) { if(OWLAPIUtils.floatDatatypes.contains(lit.getDatatype())){ valuesDouble.add(Double.parseDouble(lit.getLiteral())); @@ -724,10 +706,10 @@ public final Map> getNumericDatat protected Map> getNumericDatatypeMembersImpl( OWLDataProperty datatypeProperty, Class clazz) throws ReasoningMethodUnsupportedException { Map> mapping = getDatatypeMembersImpl(datatypeProperty); - Map> ret = new TreeMap>(); + Map> ret = new TreeMap<>(); for (Entry> e : mapping.entrySet()) { SortedSet values = e.getValue(); - SortedSet numericValues = new TreeSet(); + SortedSet numericValues = new TreeSet<>(); for (OWLLiteral lit : values) { try { numericValues.add((T) numberFormat.parse(lit.getLiteral())); @@ -754,11 +736,11 @@ public final > Map> protected > Map> getNumericDatatypeMembersImpl( OWLDataProperty datatypeProperty) throws ReasoningMethodUnsupportedException { Map> mapping = getDatatypeMembersImpl(datatypeProperty); - Map> ret = new TreeMap>(); + Map> ret = new TreeMap<>(); for (Entry> entry : mapping.entrySet()) { OWLIndividual ind = entry.getKey(); SortedSet values = entry.getValue(); - SortedSet numericValues = new TreeSet(); + SortedSet numericValues = new TreeSet<>(); for (OWLLiteral lit : values) { if(OWLAPIUtils.isIntegerDatatype(lit)) { numericValues.add((T) Integer.valueOf(lit.parseInteger())); @@ -800,10 +782,10 @@ public final Map> getIntDatatypeMembers( protected Map> getIntDatatypeMembersImpl( OWLDataProperty datatypeProperty) throws ReasoningMethodUnsupportedException { Map> mapping = getDatatypeMembersImpl(datatypeProperty); - Map> ret = new TreeMap>(); + Map> ret = new TreeMap<>(); for (Entry> e : mapping.entrySet()) { SortedSet values = e.getValue(); - SortedSet valuesInt = new TreeSet(); + SortedSet valuesInt = new TreeSet<>(); for (OWLLiteral lit : values) { if(OWLAPIUtils.isIntegerDatatype(lit)){ valuesInt.add(lit.parseInteger()); @@ -828,10 +810,10 @@ public final Map> getBooleanDatatypeMembers( protected Map> getBooleanDatatypeMembersImpl( OWLDataProperty datatypeProperty) throws ReasoningMethodUnsupportedException { Map> mapping = getDatatypeMembersImpl(datatypeProperty); - Map> ret = new TreeMap>(); + Map> ret = new TreeMap<>(); for (Entry> e : mapping.entrySet()) { SortedSet values = e.getValue(); - SortedSet valuesBoolean = new TreeSet(); + SortedSet valuesBoolean = new TreeSet<>(); for (OWLLiteral c : values) { String s = c.getLiteral(); if (s.equalsIgnoreCase("true")) { @@ -861,7 +843,7 @@ public final SortedSet getTrueDatatypeMembers(OWLDataProperty dat protected SortedSet getTrueDatatypeMembersImpl(OWLDataProperty datatypeProperty) throws ReasoningMethodUnsupportedException { Map> mapping = getDatatypeMembersImpl(datatypeProperty); - SortedSet ret = new TreeSet(); + SortedSet ret = new TreeSet<>(); for (Entry> e : mapping.entrySet()) { SortedSet values = e.getValue(); if (values.size() > 1) { @@ -889,7 +871,7 @@ public final SortedSet getFalseDatatypeMembers(OWLDataProperty da protected SortedSet getFalseDatatypeMembersImpl(OWLDataProperty datatypeProperty) throws ReasoningMethodUnsupportedException { Map> mapping = getDatatypeMembersImpl(datatypeProperty); - SortedSet ret = new TreeSet(); + SortedSet ret = new TreeSet<>(); for (Entry> e : mapping.entrySet()) { SortedSet values = e.getValue(); if (values.size() > 1) { @@ -918,10 +900,10 @@ public final Map> getStringDatatypeMembers( protected Map> getStringDatatypeMembersImpl( OWLDataProperty datatypeProperty) throws ReasoningMethodUnsupportedException { Map> mapping = getDatatypeMembersImpl(datatypeProperty); - Map> ret = new TreeMap>(); + Map> ret = new TreeMap<>(); for (Entry> e : mapping.entrySet()) { SortedSet values = e.getValue(); - SortedSet valuesString = new TreeSet(); + SortedSet valuesString = new TreeSet<>(); for (OWLLiteral c : values) { valuesString.add(c.getLiteral()); } @@ -1281,10 +1263,10 @@ public final SortedSet getMostSpecialDatatypeProperties() { * create the hierarchy is not supported by the underlying reasoner. */ public ClassHierarchy prepareSubsumptionHierarchy() throws ReasoningMethodUnsupportedException { - TreeMap> subsumptionHierarchyUp = new TreeMap>( - ); - TreeMap> subsumptionHierarchyDown = new TreeMap>( - ); + TreeMap> subsumptionHierarchyUp = new TreeMap<>( + ); + TreeMap> subsumptionHierarchyDown = new TreeMap<>( + ); // parents/children of top ... SortedSet tmp = getSubClassesImpl(df.getOWLThing()); @@ -1342,10 +1324,10 @@ public final ClassHierarchy getClassHierarchy() { public ObjectPropertyHierarchy prepareObjectPropertyHierarchy() throws ReasoningMethodUnsupportedException { - TreeMap> roleHierarchyUp = new TreeMap>( - ); - TreeMap> roleHierarchyDown = new TreeMap>( - ); + TreeMap> roleHierarchyUp = new TreeMap<>( + ); + TreeMap> roleHierarchyDown = new TreeMap<>( + ); Set atomicRoles = getObjectProperties(); for (OWLObjectProperty role : atomicRoles) { @@ -1373,9 +1355,9 @@ public final ObjectPropertyHierarchy getObjectPropertyHierarchy() { public boolean isSubPropertyOf(OWLProperty subProperty, OWLProperty superProperty){ if(subProperty.isOWLObjectProperty() && superProperty.isOWLObjectProperty()){ - return roleHierarchy.isSubpropertyOf((OWLObjectProperty)subProperty, (OWLObjectProperty)superProperty); + return getObjectPropertyHierarchy().isSubpropertyOf((OWLObjectProperty)subProperty, (OWLObjectProperty)superProperty); } else if(subProperty.isOWLDataProperty() && superProperty.isOWLDataProperty()){ - return datatypePropertyHierarchy.isSubpropertyOf((OWLDataProperty)subProperty, (OWLDataProperty)superProperty); + return getDatatypePropertyHierarchy().isSubpropertyOf((OWLDataProperty)subProperty, (OWLDataProperty)superProperty); } return false; } @@ -1392,10 +1374,10 @@ public boolean isSubPropertyOf(OWLProperty subProperty, OWLProperty superPropert public DatatypePropertyHierarchy prepareDatatypePropertyHierarchy() throws ReasoningMethodUnsupportedException { - TreeMap> datatypePropertyHierarchyUp = new TreeMap>( - ); - TreeMap> datatypePropertyHierarchyDown = new TreeMap>( - ); + TreeMap> datatypePropertyHierarchyUp = new TreeMap<>( + ); + TreeMap> datatypePropertyHierarchyDown = new TreeMap<>( + ); Set datatypeProperties = getDatatypeProperties(); for (OWLDataProperty role : datatypeProperties) { @@ -1422,26 +1404,17 @@ public final DatatypePropertyHierarchy getDatatypePropertyHierarchy() { public List getAtomicConceptsList() { if (atomicConceptsList == null) - atomicConceptsList = new LinkedList(getClasses()); + atomicConceptsList = new LinkedList<>(getClasses()); return atomicConceptsList; } public List getAtomicConceptsList(boolean removeOWLThing) { - if (!removeOWLThing) { - return getAtomicConceptsList(); - } else { - List l = new LinkedList(); - for (OWLClass class1 : getAtomicConceptsList()) { - if (class1.compareTo(df.getOWLClass(IRI.create(OWLVocabulary.OWL_NOTHING))) == 0 - || class1.compareTo(df.getOWLClass(IRI.create(OWLVocabulary.OWL_THING))) == 0) { - ;// do nothing - } else { - l.add(class1); - } - } - return l; + List classes = Lists.newArrayList(getAtomicConceptsList()); + if (removeOWLThing) { + classes.remove(df.getOWLThing()); + classes.remove(df.getOWLNothing()); } - + return classes; } public void setSubsumptionHierarchy(ClassHierarchy subsumptionHierarchy) { @@ -1450,7 +1423,7 @@ public void setSubsumptionHierarchy(ClassHierarchy subsumptionHierarchy) { public List getAtomicRolesList() { if (atomicRolesList == null) - atomicRolesList = new LinkedList(getObjectProperties()); + atomicRolesList = new LinkedList<>(getObjectProperties()); return atomicRolesList; } @@ -1630,8 +1603,23 @@ protected double hasTypeFuzzyMembershipImpl(OWLClassExpression concept, FuzzyInd } /** - * @param dp - * @return + * Returns the datatype of the data property, i.e. the range if it is a datatype. + * @param dp the data property + * @return the datatype of the data property */ public abstract OWLDatatype getDatatype(OWLDataProperty dp); + + /** + * Enabled a synchronized mode such that all reasoner methods are supposed + * to be thread safe. + */ + public abstract void setSynchronized(); + + public boolean isUseInstanceChecks() { + return useInstanceChecks; + } + + public void setUseInstanceChecks(boolean useInstanceChecks) { + this.useInstanceChecks = useInstanceChecks; + } } \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/core/AbstractSearchTreeNode.java b/components-core/src/main/java/org/dllearner/core/AbstractSearchTreeNode.java new file mode 100644 index 0000000000..a02fb5c826 --- /dev/null +++ b/components-core/src/main/java/org/dllearner/core/AbstractSearchTreeNode.java @@ -0,0 +1,105 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.core; + +import java.util.Collection; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; + +import org.dllearner.utilities.datastructures.AbstractSearchTree; +import org.dllearner.utilities.datastructures.SearchTreeNode; +import org.semanticweb.owlapi.model.OWLClassExpression; + +public abstract class AbstractSearchTreeNode implements SearchTreeNode { + + protected Set< AbstractSearchTree > trees = new HashSet<>(); + protected T parent; + protected List children = new LinkedList<>(); + + @Override + public abstract OWLClassExpression getExpression(); + + /** + * add a child node to this node + * @param node the child node + */ + public void addChild(T node) { + node.setParent(this); + children.add(node); + node.notifyTrees(this.trees); + } + + /** + * set the parent of this node + * @param node parent node + */ + protected void setParent(T node) { + if (this.parent == node) { + return; + } else if (this.parent != null) { + throw new Error("Parent already set on node"); + } + this.parent = node; + } + + /** + * internally used by the tree<->node contract to add this node to a set of trees + * @param trees the set of owning trees + */ + public void notifyTrees( Collection> trees ) { + updatePrepareTree(); + this.trees.addAll(trees); + notifyTree(); + } + + public void notifyTree( AbstractSearchTree tree ) { + updatePrepareTree(); + this.trees.add(tree); + notifyTree(); + } + + private void notifyTree() { + for(AbstractSearchTree tree : trees) { + tree.notifyNode((T)this); + } + } + + private void updatePrepareTree() { + for(AbstractSearchTree tree : trees) { + tree.updatePrepare((T)this); + } + } + + /** + * @return the parent + */ + public T getParent() { + return parent; + } + + /** + * @return the children + */ + @Override + public Collection getChildren() { + return children; + } +} diff --git a/components-core/src/main/java/org/dllearner/core/ActiveLearningAlgorithm.java b/components-core/src/main/java/org/dllearner/core/ActiveLearningAlgorithm.java index 0eada16428..19a51c1214 100644 --- a/components-core/src/main/java/org/dllearner/core/ActiveLearningAlgorithm.java +++ b/components-core/src/main/java/org/dllearner/core/ActiveLearningAlgorithm.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; @@ -33,6 +32,6 @@ public interface ActiveLearningAlgorithm extends LearningAlgorithm { * an oracle object is passed to the algorithm. * @param oracle The oracle to be used by the learning algorithm. */ - public void setOracle(Oracle oracle); + void setOracle(Oracle oracle); } diff --git a/components-core/src/main/java/org/dllearner/core/AnnComponentManager.java b/components-core/src/main/java/org/dllearner/core/AnnComponentManager.java index ecde618271..a7eaeabf01 100644 --- a/components-core/src/main/java/org/dllearner/core/AnnComponentManager.java +++ b/components-core/src/main/java/org/dllearner/core/AnnComponentManager.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,30 +16,24 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Comparator; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; - +import com.google.common.collect.Sets; import org.apache.commons.collections15.BidiMap; import org.apache.commons.collections15.bidimap.DualHashBidiMap; import org.apache.log4j.Level; import org.dllearner.core.config.ConfigOption; +import org.dllearner.learningproblems.AccMethod; import org.dllearner.refinementoperators.RefinementOperator; import org.reflections.Reflections; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.collect.Sets; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.*; /** @@ -136,7 +130,7 @@ private AnnComponentManager() { reflectionScanner = new Reflections("org.dllearner"); } Set> componentClasses = reflectionScanner.getSubTypesOf(Component.class); - Set> componentAnnClasses = reflectionScanner.getTypesAnnotatedWith(ComponentAnn.class); + Set> componentAnnClasses = reflectionScanner.getTypesAnnotatedWith(ComponentAnn.class); for (Class clazz : Sets.intersection( componentClasses, @@ -154,16 +148,16 @@ private AnnComponentManager() { } } // conversion of class strings to objects - components = new TreeSet>(new Comparator>() { + components = new TreeSet<>(new Comparator>() { @Override public int compare(Class o1, - Class o2) { + Class o2) { return o1.getName().compareTo(o2.getName()); } }); - componentNames = new DualHashBidiMap, String>(); - componentNamesShort = new DualHashBidiMap, String>(); + componentNames = new DualHashBidiMap<>(); + componentNamesShort = new DualHashBidiMap<>(); for (String componentClassName : componentClassNames) { try { Class component = Class.forName(componentClassName).asSubclass(Component.class); @@ -267,7 +261,7 @@ public Class getComponentClass(String componentName) { */ public Collection> getComponentsOfType(Class type) { - Collection> result = new ArrayList>(); + Collection> result = new ArrayList<>(); for (Class component : components) { if (type.isAssignableFrom(component)) { result.add(component); @@ -299,6 +293,51 @@ public BidiMap, String> getComponentsNamedShort() { return componentNamesShort; } + /** + * Applies a config entry to a component. If the entry is not valid, the method + * prints an exception and returns false. + * @param Type of the config option. + * @param component A component object. + * @param optionName the option name + * @param value the value to set + * @return True if the config entry could be applied succesfully, otherwise false. + */ + @Deprecated + public static boolean applyConfigEntry(AbstractComponent component, String optionName, T value) { + List childComponents = new LinkedList<>(); + for (Method m : component.getClass().getMethods()) { + if (m.getName().equals("set" + optionName.substring(0, 1).toUpperCase() + optionName.substring(1))) { + try { + m.invoke(component, value); + } catch (IllegalAccessException | IllegalArgumentException + | InvocationTargetException e) { + logger.debug("Error setting " + optionName + " to " + value + " on " + component + ": ", e); + return false; + } + return true; + } else if (m.getName().startsWith("get") + && AbstractComponent.class.isAssignableFrom(m.getReturnType())) { + Object cc; + try { + cc = m.invoke(component); + childComponents.add((AbstractComponent) cc); + } catch (IllegalAccessException | IllegalArgumentException + | InvocationTargetException e) { + logger.trace("Error querying " + m.getName() + " for subcomponent in " + component, e); + } + } + } + for (AbstractComponent cc : childComponents) { + if (cc != null) { + boolean try_inv = applyConfigEntry(cc, optionName, value); + if (try_inv) { + return true; + } + } + } + return false; + } + public final static Class[] coreComponentClasses = { KnowledgeSource.class, LearningAlgorithm.class, @@ -307,7 +346,8 @@ public BidiMap, String> getComponentsNamedShort() { LearningProblem.class, ReasonerComponent.class, RefinementOperator.class, - Heuristic.class + Heuristic.class, + AccMethod.class }; /** @@ -318,7 +358,7 @@ public BidiMap, String> getComponentsNamedShort() { * @return The list of core interfaces the component implemnets. */ public static List> getCoreComponentTypes(Class component) { - List> types = new LinkedList>(); + List> types = new LinkedList<>(); for(Class c : coreComponentClasses) { if(c.isAssignableFrom(component)) { types.add(c); @@ -329,7 +369,7 @@ public static List> getCoreComponentTypes(Class component){ @@ -342,7 +382,7 @@ public static String getName(Class component){ /** * Returns the name of a DL-Learner component. - * @param component + * @param component the component * @return Name of the component. */ public static String getName(Component component){ @@ -351,7 +391,7 @@ public static String getName(Component component){ /** * Returns the name of a DL-Learner component. - * @param component + * @param component the component * @return Name of the component. */ public static String getShortName(Class component){ @@ -364,7 +404,7 @@ public static String getShortName(Class component){ /** * Returns the short name of a DL-Learner component. - * @param component + * @param component the component * @return Short name of the component. */ public static String getShortName(Component component){ @@ -373,7 +413,7 @@ public static String getShortName(Component component){ /** * Returns the name of a DL-Learner component. - * @param component + * @param component the component * @return Name of the component. */ public static String getDescription(Class component){ @@ -383,7 +423,7 @@ public static String getDescription(Class component){ /** * Returns the description of a DL-Learner component. - * @param component + * @param component the component * @return OWLClassExpression of the component. */ public static String getDescription(Component component){ @@ -392,16 +432,16 @@ public static String getDescription(Component component){ /** * Returns the config options of a DL-Learner component. - * @param component + * @param component the component * @return OWLClassExpression of the component. */ - public static Set getConfigOptions(Class component){ - Set set = new HashSet<>(); + public static Set getConfigOptions(Class component){ + Set set = new HashSet<>(); Class c = component; while (c != null) { - for (java.lang.reflect.Field field : c.getDeclaredFields()) { + for (Field field : c.getDeclaredFields()) { if (field.isAnnotationPresent(ConfigOption.class)) { - set.add(field.getAnnotation(ConfigOption.class)); + set.add(field); } } c = c.getSuperclass(); @@ -411,7 +451,7 @@ public static Set getConfigOptions(Class comp /** * Returns the version of a DL-Learner component. - * @param component + * @param component the component * @return Version of the component. */ public static double getVersion(Class component){ @@ -421,7 +461,7 @@ public static double getVersion(Class component){ /** * Returns the version of a DL-Learner component. - * @param component + * @param component the component * @return Version of the component. */ public static double getVersion(Component component){ @@ -431,4 +471,14 @@ public static double getVersion(Component component){ public static boolean addComponentClassName(String e) { return componentClassNames.add(e); } + + /** + * Returns the name of a config option + * @param f the Reflection field of the option + * @return name of the option + */ + public static String getName(Field f) { + f.getAnnotation(ConfigOption.class); + return f.getName(); + } } diff --git a/components-core/src/main/java/org/dllearner/core/AxiomLearningAlgorithm.java b/components-core/src/main/java/org/dllearner/core/AxiomLearningAlgorithm.java index f96cea4a77..c0119e0834 100644 --- a/components-core/src/main/java/org/dllearner/core/AxiomLearningAlgorithm.java +++ b/components-core/src/main/java/org/dllearner/core/AxiomLearningAlgorithm.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; import java.util.List; @@ -30,26 +29,26 @@ public interface AxiomLearningAlgorithm extends LearningAlgo /** * @return The best axioms found by the learning algorithm so far. */ - public List getCurrentlyBestAxioms(); + List getCurrentlyBestAxioms(); /** * @param nrOfAxioms Limit for the number or returned axioms. * @return The best axioms found by the learning algorithm so far. */ - public List getCurrentlyBestAxioms(int nrOfAxioms); + List getCurrentlyBestAxioms(int nrOfAxioms); /** * @return The best evaluated axioms found by the learning algorithm so far. */ - public List> getCurrentlyBestEvaluatedAxioms(); + List> getCurrentlyBestEvaluatedAxioms(); /** * @param nrOfAxioms Limit for the number or returned evaluated axioms. * @return The best evaluated axioms found by the learning algorithm so far. */ - public List> getCurrentlyBestEvaluatedAxioms(int nrOfAxioms); + List> getCurrentlyBestEvaluatedAxioms(int nrOfAxioms); - public List> getCurrentlyBestEvaluatedAxioms(int nrOfAxioms, - double accuracyThreshold); + List> getCurrentlyBestEvaluatedAxioms(int nrOfAxioms, + double accuracyThreshold); } diff --git a/components-core/src/main/java/org/dllearner/core/AxiomLearningProgressMonitor.java b/components-core/src/main/java/org/dllearner/core/AxiomLearningProgressMonitor.java index 8c60d1a2cd..9b7fcb3fbb 100644 --- a/components-core/src/main/java/org/dllearner/core/AxiomLearningProgressMonitor.java +++ b/components-core/src/main/java/org/dllearner/core/AxiomLearningProgressMonitor.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.core; @@ -14,9 +29,9 @@ * preprocessing and learning.
* Tasks are executed sequentially. Nested tasks are not supported.
* The general contract is that the learning algorithm will call - * {@link #learningStarted(String)}, then call either - * {@link #learningTaskBusy()} or {@link #learningProgressChanged(int, int)} - * any number of times and finally call {@link #learningStopped()} when the + * {@link #learningStarted(AxiomType)}, then call either + * {@link #learningTaskBusy(AxiomType)} or {@link #learningProgressChanged(AxiomType, int, int)} + * any number of times and finally call {@link #learningStopped(AxiomType)} when the * task ends or has been interrupted. This cycle may then be repeated. * * @@ -45,9 +60,9 @@ public interface AxiomLearningProgressMonitor { /** * Indicates that some learning algorithm has started. When the learning algorithm has - * finished the {@link #learningStopped()} method will be called. Once + * finished the {@link #learningStopped(AxiomType)} method will be called. Once * this method has been called it will not be called again unless the - * {@link #learningStopped()} method has been called.
+ * {@link #learningStopped(AxiomType)} method has been called.
* Note that this method may be called from a thread that is not the event * dispatch thread. * @@ -58,7 +73,7 @@ public interface AxiomLearningProgressMonitor { /** * Indicates that a previously started learning algorithm has now stopped. This method will - * only be called after the {@link #learningStarted(String)} method has + * only be called after the {@link #learningStarted(AxiomType)} method has * been called.
* Note that this method may be called from a thread that is not the event * dispatch thread. @@ -67,9 +82,9 @@ public interface AxiomLearningProgressMonitor { /** * Indicates that the learning algorithm is part way through its task. This method - * will only be called after the {@link #learningStarted(String)} method + * will only be called after the {@link #learningStarted(AxiomType)} method * has been called. It will not be called after the - * {@link #learningStopped()} method has been called.
+ * {@link #learningStopped(AxiomType)} method has been called.
* Note that this method may be called from a thread that is not the event * dispatch thread. * @@ -83,8 +98,8 @@ public interface AxiomLearningProgressMonitor { /** * Indicates that the learning algorithm is busy performing a task whose size cannot * be determined. This method will only be called after the - * {@link #learningStarted(String)} method has been called. It will not - * be called after the {@link #learningStopped()} method has been + * {@link #learningStarted(AxiomType)} method has been called. It will not + * be called after the {@link #learningStopped(AxiomType)} method has been * called.
* Note that this method may be called from a thread that is not the event * dispatch thread. diff --git a/components-core/src/main/java/org/dllearner/core/AxiomScoreCalculator.java b/components-core/src/main/java/org/dllearner/core/AxiomScoreCalculator.java index f20e28b212..ee9dcb7bd4 100644 --- a/components-core/src/main/java/org/dllearner/core/AxiomScoreCalculator.java +++ b/components-core/src/main/java/org/dllearner/core/AxiomScoreCalculator.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.core; import org.dllearner.learningproblems.AxiomScore; diff --git a/components-core/src/main/java/org/dllearner/core/BaseReasoner.java b/components-core/src/main/java/org/dllearner/core/BaseReasoner.java index 5e0c693da0..9424db563a 100644 --- a/components-core/src/main/java/org/dllearner/core/BaseReasoner.java +++ b/components-core/src/main/java/org/dllearner/core/BaseReasoner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; import java.util.Map; @@ -49,51 +48,51 @@ public interface BaseReasoner { * Checks consistency of the knowledge. * @return True if the knowledge base is consistent and false otherwise. */ - public boolean isSatisfiable(); + boolean isSatisfiable(); /** * Checks whether adding the specified axiom leads to an inconsistency. * @param axiom The axiom to be added to the knowledge base. * @return True of the knowledge base including the axiom is satisfiable. False otherwise. */ - public boolean remainsSatisfiable(OWLAxiom axiom); + boolean remainsSatisfiable(OWLAxiom axiom); /** * Gets all named classes in the knowledge base, e.g. Person, City, Car. * @return All named classes in KB. */ - public Set getClasses(); + Set getClasses(); /** * Gets all object properties in the knowledge base, e.g. hasChild, isCapitalOf, hasEngine. * @return All object properties in KB. */ - public Set getObjectProperties(); + Set getObjectProperties(); /** * Gets all data properties in the knowledge base, e.g. hasIncome, height. * @return All data properties in KB. */ - public Set getDatatypeProperties(); + Set getDatatypeProperties(); /** * Gets all data properties with range xsd:boolean. * @return Boolean data properties in KB. */ - public Set getBooleanDatatypeProperties(); + Set getBooleanDatatypeProperties(); /** * Gets all data properties with a range that describes floating point values, i.e. * xsd:float, xsd:double and xsd:decimal. * @return Floating point data properties in KB. */ - public Set getDoubleDatatypeProperties(); + Set getDoubleDatatypeProperties(); /** * Gets all data properties with a numeric range * @return Numeric data properties in KB. */ - public Set getNumericDataProperties(); + Set getNumericDataProperties(); /** * Gets all integer type data properties, i.e. with range @@ -103,29 +102,29 @@ public interface BaseReasoner { * @see org.dllearner.utilities.OWLAPIUtils#intDatatypes * @return Integer data properties in KB. */ - public Set getIntDatatypeProperties(); + Set getIntDatatypeProperties(); /** * Gets all data properties with range xsd:string. * TODO We could extend this to all types, which can be parsed into * strings and even include the properties without any specified datatype. - * @see org.dllearner.core.owl.Datatype#String + * @see OWLDataProperty * @return String data properties in KB. */ - public Set getStringDatatypeProperties(); + Set getStringDatatypeProperties(); /** * Gets all individuals in the knowledge base, e.g. Eric, London, Car829. * @return All individuals in KB. - */ - public SortedSet getIndividuals(); + */ + SortedSet getIndividuals(); /** * Returns the base URI of the knowledge base. If several knowledge sources are * used, we only pick one of their base URIs. * @return The base URI, e.g. http://dbpedia.org/resource/. */ - public String getBaseURI(); + String getBaseURI(); /** * Returns the prefixes used in the knowledge base, e.g. foaf for @@ -134,13 +133,13 @@ public interface BaseReasoner { * values, we pick one of those.) * @return The prefix mapping. */ - public Map getPrefixes(); + Map getPrefixes(); /** * Returns the RDFS labels of an entity. * @param entity An entity, e.g. Machine. * @return All values of rdfs:label for the entity, e.g. {"Machine"@en, "Maschine"@de}. */ - public Set getLabel(OWLEntity entity); + Set getLabel(OWLEntity entity); } diff --git a/components-core/src/main/java/org/dllearner/core/ClassExpressionLearningAlgorithm.java b/components-core/src/main/java/org/dllearner/core/ClassExpressionLearningAlgorithm.java index 9b06bf6723..aebb05ac08 100644 --- a/components-core/src/main/java/org/dllearner/core/ClassExpressionLearningAlgorithm.java +++ b/components-core/src/main/java/org/dllearner/core/ClassExpressionLearningAlgorithm.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; import java.util.List; @@ -36,7 +35,7 @@ public interface ClassExpressionLearningAlgorithm extends LearningAlgorithm { * @param nrOfDescriptions Limit for the number or returned descriptions. * @return The best class descriptions found by the learning algorithm so far. */ - public List getCurrentlyBestDescriptions(int nrOfDescriptions); + List getCurrentlyBestDescriptions(int nrOfDescriptions); /** * Return the best currently found concepts up to some maximum @@ -44,6 +43,6 @@ public interface ClassExpressionLearningAlgorithm extends LearningAlgorithm { * @param nrOfDescriptions Maximum number of descriptions returned. * @return Return value is getCurrentlyBestDescriptions(nrOfDescriptions, 0.0, false). */ - public List getCurrentlyBestEvaluatedDescriptions(int nrOfDescriptions); + List getCurrentlyBestEvaluatedDescriptions(int nrOfDescriptions); } diff --git a/components-core/src/main/java/org/dllearner/core/Component.java b/components-core/src/main/java/org/dllearner/core/Component.java index 49610926c3..b676b95491 100644 --- a/components-core/src/main/java/org/dllearner/core/Component.java +++ b/components-core/src/main/java/org/dllearner/core/Component.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,9 +16,10 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; +import javax.annotation.PostConstruct; + /** * Base interface of all components. See also http://dl-learner.org/wiki/Architecture. * @@ -35,10 +36,11 @@ public interface Component { * @throws ComponentInitException This exception is thrown if any * exceptions occur within the initialisation process of this * component. As component developer, you are encouraged to - * rethrow occuring exception as ComponentInitException and - * giving an error message as well as the actualy exception by - * using the constructor {@link ComponentInitException#ComponentInitException(String, Throwable)}. + * re-throw occurring exception as ComponentInitException and + * giving an error message as well as the actually exception by + * using the constructor {@link ComponentInitException#ComponentInitException(String, Throwable)}. */ - public abstract void init() throws ComponentInitException; + @PostConstruct + void init() throws ComponentInitException; } diff --git a/components-core/src/main/java/org/dllearner/core/ComponentAnn.java b/components-core/src/main/java/org/dllearner/core/ComponentAnn.java index 91d87c69ea..a5fdf9ef9e 100644 --- a/components-core/src/main/java/org/dllearner/core/ComponentAnn.java +++ b/components-core/src/main/java/org/dllearner/core/ComponentAnn.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; import java.lang.annotation.Retention; @@ -35,7 +34,6 @@ /** * The name of this component. - * @see ComponentManager#getName(Component) * @return The name of this component. */ String name(); diff --git a/components-core/src/main/java/org/dllearner/core/ComponentInitException.java b/components-core/src/main/java/org/dllearner/core/ComponentInitException.java index d41dfa1599..5229548273 100644 --- a/components-core/src/main/java/org/dllearner/core/ComponentInitException.java +++ b/components-core/src/main/java/org/dllearner/core/ComponentInitException.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; /** diff --git a/components-core/src/main/java/org/dllearner/core/ComponentManager.java b/components-core/src/main/java/org/dllearner/core/ComponentManager.java deleted file mode 100644 index 9feec2b66d..0000000000 --- a/components-core/src/main/java/org/dllearner/core/ComponentManager.java +++ /dev/null @@ -1,720 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.core; - -import java.io.BufferedReader; -import java.io.DataInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.*; -import java.util.Map.Entry; - -import org.apache.log4j.Logger; -import org.dllearner.core.options.ConfigEntry; -import org.dllearner.core.options.ConfigOption; -import org.dllearner.core.options.InvalidConfigOptionValueException; -import org.dllearner.utilities.datastructures.Maps; - -/** - * Central manager class for DL-Learner. There are currently four types of - * components in DL-Learner: knowledge sources, reasoners, learning problems, - * and learning algorithms. For accessing these components you should create - * instances and configure them using this class. The component manager is - * implemented as a Singleton and will read the components file (containing a - * list of all components) at startup. This allows interfaces (command line, - * graphical, web service) to easily query the available components, set and get - * their configuration options, and run the algorithm. - * - * @author Jens Lehmann - * - */ -@Deprecated -public final class ComponentManager { - - private static Logger logger = Logger - .getLogger(ComponentManager.class); - - private ComponentPool pool = new ComponentPool(); - - // these variables are valid for the complete lifetime of a DL-Learner session - private static Collection> components; - private static Collection> knowledgeSources; - private static Collection> reasonerComponents; - private static Collection> learningProblems; - private static Collection> learningAlgorithms; - // you can either use the components.ini file or directly specify the classes to use - @Deprecated - private static String componentsFile = "org/dllearner/components.ini"; - private static List componentClasses = new ArrayList ( Arrays.asList(new String[]{ - "org.dllearner.kb.OWLFile", - "org.dllearner.kb.sparql.SparqlKnowledgeSource", - "org.dllearner.kb.sparql.simple.SparqlSimpleExtractor", - "org.dllearner.kb.OWLAPIOntology", -// "org.dllearner.kb.SparqlEndpointKS", -//reasoners - "org.dllearner.reasoning.OWLAPIReasoner", -// "org.dllearner.reasoning.fuzzydll.FuzzyOWLAPIReasoner", // added by Josue -//learning problems - "org.dllearner.learningproblems.PosNegLPStandard", -// "org.dllearner.learningproblems.FuzzyPosNegLPStandard", // added by Josue -// "org.dllearner.learningproblems.PosNegLPStrict", - "org.dllearner.learningproblems.PosOnlyLP", - "org.dllearner.learningproblems.ClassLearningProblem", -//learning algorithms -// "org.dllearner.algorithms.RandomGuesser", -// "org.dllearner.algorithms.BruteForceLearner", -// "org.dllearner.algorithms.ocel.ROLearner2", - "org.dllearner.algorithms.ocel.OCEL", -// "org.dllearner.algorithms.gp.GP", - "org.dllearner.algorithms.el.ELLearningAlgorithm", - "org.dllearner.algorithms.el.ELLearningAlgorithmDisjunctive", - "org.dllearner.algorithms.celoe.CELOE", -// "org.dllearner.algorithms.fuzzydll.FuzzyCELOE" //added by Josue - } )); - - private static ComponentManager cm = null; - - // list of all configuration options of all components - private static Map, String> componentNames; - private static Map, List>> componentOptions; - private static Map, Map>> componentOptionsByName; - private static Map, Collection>> algorithmProblemsMapping; - private static Map, Collection>> problemAlgorithmsMapping; - - // list of default values of config options -// private static Map,Object> configOptionDefaults; - - private Comparator> classComparator = new Comparator>() { - - public int compare(Class c1, Class c2) { - return c1.getName().compareTo(c2.getName()); - } - - }; - - @SuppressWarnings("unchecked") - private ComponentManager() { - - // read in components file - /*REMOVED THE BLOCK*/ - /*List componentsString; - if(componentClasses.length > 0) { - componentsString = Arrays.asList(componentClasses); - } else { - componentsString = readComponentsFile(); - }*/ - //List componentsString2 = Arrays.asList(componentClasses); - - // component list - components = new TreeSet>(classComparator); - knowledgeSources = new TreeSet>(classComparator); - reasonerComponents = new TreeSet>(classComparator); - learningProblems = new TreeSet>(classComparator); - learningAlgorithms = new TreeSet>(classComparator); - algorithmProblemsMapping = new TreeMap, Collection>>( - classComparator); - - // create classes from strings - for (String componentString : componentClasses) { - try { - Class component = Class.forName(componentString).asSubclass( - AbstractComponent.class); - components.add(component); - - if (AbstractKnowledgeSource.class.isAssignableFrom(component)) { - knowledgeSources.add((Class) component); - } else if (AbstractReasonerComponent.class.isAssignableFrom(component)) { - reasonerComponents.add((Class) component); - } else if (AbstractClassExpressionLearningProblem.class.isAssignableFrom(component)) { - learningProblems.add((Class) component); - } else if (AbstractCELA.class.isAssignableFrom(component)) { - Class learningAlgorithmClass = (Class) component; - learningAlgorithms.add(learningAlgorithmClass); - Collection> problems = (Collection>) invokeStaticMethod( - learningAlgorithmClass, "supportedLearningProblems"); - algorithmProblemsMapping.put(learningAlgorithmClass, problems); - } - - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } - } - problemAlgorithmsMapping = Maps.revertCollectionMap(algorithmProblemsMapping); - - componentNames = new HashMap, String>(); - // read in all configuration options - componentOptions = new HashMap, List>>(); - componentOptionsByName = new HashMap, Map>>(); -// configOptionDefaults = new HashMap,Object>(); - - for (Class component : components) { - - String name = (String) invokeStaticMethod(component, "getName"); - componentNames.put(component, name); - - // assign options to components - List> options = (List>) invokeStaticMethod(component, - "createConfigOptions"); - componentOptions.put(component, options); - - // make config options accessible by name - Map> byName = new HashMap>(); - for (ConfigOption option : options) { - byName.put(option.getName(), option); - } - componentOptionsByName.put(component, byName); - - } - - } - - /** - * Gets the singleton instance of ComponentManager. - * @return The singleton ComponentManager instance. - */ - public static ComponentManager getInstance() { - if(cm == null) { - cm = new ComponentManager(); - } - return cm; - } - - /** - * Set the classes, which can be used as components. By default, - * this is read from components.ini, but this method can be used - * to set the components programmatically. This method must be - * used before the first call to {@link #getInstance()}, otherwise - * it has no effect. - * - * @param componentClasses A list of class names, e.g. - * org.dllearner.refinement.ROLearner. - */ - public static void setComponentClasses(String[] componentClasses) { - ComponentManager.componentClasses = new ArrayList (Arrays.asList(componentClasses)); - } - - @Deprecated - private static List readComponentsFile() { - List componentStrings = new LinkedList(); - - try { - - InputStream is = ComponentManager.class.getClassLoader().getResourceAsStream(componentsFile); - DataInputStream in = new DataInputStream(is); - BufferedReader br = new BufferedReader(new InputStreamReader(in)); - String line; - - while ((line = br.readLine()) != null) { - if (!(line.startsWith("#") || line.startsWith("//") || line.startsWith("%") || line - .length() <= 1)) { - componentStrings.add(line); - } - } - - in.close(); - } catch (IOException e) { - e.printStackTrace(); - } - - return componentStrings; - } - - /** - * Convenience method for testing purposes. If you know that the type of the - * value is correct, it is preferable to create a ConfigEntry object and - * apply it to the component (no type checking necessary). - * - * @param Type of the config option (Integer, String etc.). - * @param component A component. - * @param optionName The name of the config option. - * @param value The value of the config option. - */ - @SuppressWarnings("unchecked") - public void applyConfigEntry(AbstractComponent component, String optionName, T value) { - logger.trace(component); - logger.trace(optionName); - logger.trace(value); - logger.trace(value.getClass()); - // first we look whether the component is registered - if (components.contains(component.getClass())) { - - // look for a config option with the specified name - ConfigOption option = (ConfigOption) componentOptionsByName.get( - component.getClass()).get(optionName); - if (option != null) { - // check whether the given object has the correct type - if (!option.checkType(value)) { - System.out.println("Warning: value " + value + " is not valid for option " - + optionName + " in component " + component - + ". It does not have the correct type."); - return; - } - - // we have checked the type, hence it should now be safe to - // typecast and - // create a ConfigEntry object - ConfigEntry entry = null; - try { - entry = new ConfigEntry((ConfigOption) option, value); - component.applyConfigEntry(entry); - pool.addConfigEntry(component, entry, true); - } catch (InvalidConfigOptionValueException e) { - pool.addConfigEntry(component, entry, false); - System.out.println("Warning: value " + value + " is not valid for option " - + optionName + " in component " + component); - } - } else { - logger.warn("Warning: undefined option " + optionName + " in component " - + component); - } - } else { - logger.warn("Warning: unregistered component " + component); - } - } - - public ComponentPool getPool() { - return pool; - } - - /** - * Applies a config entry to a component. If the entry is not valid, the method - * prints an exception and returns false. - * @param Type of the config option. - * @param component A component object. - * @param entry The configuration entry to set. - * @return True if the config entry could be applied succesfully, otherwise false. - */ - public boolean applyConfigEntry(AbstractComponent component, ConfigEntry entry) { - try { - component.applyConfigEntry(entry); - pool.addConfigEntry(component, entry, true); - return true; - } catch (InvalidConfigOptionValueException e) { - pool.addConfigEntry(component, entry, false); - e.printStackTrace(); - return false; - } - } - - /** - * Factory method for creating a knowledge source. - * - * @param The type of this method is a subclass of knowledge source. - * @param source A registered knowledge source component. - * @return An instance of the given knowledge source class. - */ - public T knowledgeSource(Class source) { - if (!knowledgeSources.contains(source)) { - logger.warn("Warning: knowledge source " + source - + " is not a registered knowledge source component."); - } - - T ks = invokeConstructor(source, new Class[] {}, new Object[] {}); - pool.registerComponent(ks); - return ks; - } - - /** - * Factory method for creating a reasoner component from a single - * knowledge source. Example - * call: reasoner(OWLAPIReasoner.class, ks) where ks is a - * knowledge source object. - * @see #reasoner(Class, Set) - * @param The type of this method is a subclass of reasoner component. - * @param reasoner A class object, where the class is subclass of ReasonerComponent. - * @param source A knowledge source. - * @return A reasoner component. - */ - public T reasoner(Class reasoner, - AbstractKnowledgeSource source) { - Set sources = new HashSet(); - sources.add(source); - return reasoner(reasoner, sources); - } - - /** - * Factory method for creating a reasoner component from a set of - * knowledge sources. - * @see #reasoner(Class, AbstractKnowledgeSource) - * @param The type of this method is a subclass of reasoner component. - * @param reasoner A class object, where the class is subclass of ReasonerComponent. - * @param sources A set of knowledge sources. - * @return A reasoner component. - */ - public T reasoner(Class reasoner, - Set sources) { - if (!reasonerComponents.contains(reasoner)) { - System.err.println("Warning: reasoner component " + reasoner - + " is not a registered reasoner component."); - } - - T rc = invokeConstructor(reasoner, new Class[] { Set.class }, - new Object[] { sources }); - pool.registerComponent(rc); - return rc; - } - - /** - * Factory method for creating a reasoner component from a set of - * knowledge sources. - * @see #reasoner(Class, AbstractKnowledgeSource) - * @param The type of this method is a subclass of reasoner component. - * @param reasoner A class object, where the class is subclass of ReasonerComponent. - * @param sources A set of knowledge sources. - * @return A reasoner component. - */ - public T reasoner(Class reasoner, - AbstractKnowledgeSource ... sources) { - Set s = new HashSet(); - Collections.addAll(s, sources); - return reasoner(reasoner, s); - } - - /** - * This method returns an instance of ReasonerComponent. The - * difference between ReasonerComponent and ReasonerComponent - * is that the former delegates all calls to the latter and collects statistics - * while doing this. This means that the reasoning service enables the - * collection of query information, while the ReasonerComponent - * implements the actual reasoning methods defined by the Reasoner - * interface. - * - * @param reasoner A reasoner component. - * @return The reasoning service encapsulating the reasoner. - */ -// public ReasonerComponent reasoningService(ReasonerComponent reasoner) { -// return new ReasonerComponent(reasoner); -// } - - /** - * Factory method for creating a learning problem component. - * @param The type of this method is a subclass of learning problem. - * @param lpClass A class object, where the class is a subclass of learning problem. - * @param reasoner A reasoning service object. - * @return A learning problem component. - */ - public T learningProblem(Class lpClass, AbstractReasonerComponent reasoner) { - if (!learningProblems.contains(lpClass)) { - System.err.println("Warning: learning problem " + lpClass - + " is not a registered learning problem component."); - } - - T lp = invokeConstructor(lpClass, new Class[] { AbstractReasonerComponent.class }, - new Object[] { reasoner }); - pool.registerComponent(lp); - return lp; - } - - /** - * Factory method for creating a learning algorithm, which - * automagically calls the right constructor for the given problem. - * @param The type of this method is a subclass of learning algorithm. - * @param laClass A class object, where the class is subclass of learning algorithm. - * @param lp A learning problem, which the algorithm should try to solve. - * @param rs A reasoning service for querying the background knowledge of this learning problem. - * @return A learning algorithm component. - * @throws LearningProblemUnsupportedException Thrown when the learning problem and - * the learning algorithm are not compatible. - */ - public T learningAlgorithm(Class laClass, AbstractClassExpressionLearningProblem lp, AbstractReasonerComponent rs) throws LearningProblemUnsupportedException { - if (!learningAlgorithms.contains(laClass)) { - System.err.println("Warning: learning algorithm " + laClass - + " is not a registered learning algorithm component."); - } - - // find the right constructor: use the one that is registered and - // has the class of the learning problem as a subclass - Class constructorArgument = null; - for (Class problemClass : algorithmProblemsMapping.get(laClass)) { - if (problemClass.isAssignableFrom(lp.getClass())) { - constructorArgument = problemClass; - } - } - - if (constructorArgument == null) { - throw new LearningProblemUnsupportedException(lp.getClass(), laClass, algorithmProblemsMapping.get(laClass)); -// System.err.println("Warning: No suitable constructor registered for algorithm " -// + laClass.toStringID() + " and problem " + lp.getClass().toStringID() -// + ". Registered constructors for " + laClass.toStringID() + ": " -// + algorithmProblemsMapping.get(laClass) + "."); -// return null; - } - - T la = invokeConstructor(laClass, new Class[] { constructorArgument, AbstractReasonerComponent.class }, new Object[] { lp, rs }); - pool.registerComponent(la); - return la; - } -// -// public T learningAlgorithm(Class laClass, KnowledgeSource ks) { -// T la = invokeConstructor(laClass, new Class[] { KnowledgeSource.class }, new Object[] { ks }); -// return la; -// } - - /** - * The ComponentManager factory methods produce component - * instances, which can be freed using this method. Calling the factory - * methods without freeing components when they are not used anymore - * can (in theory) cause memory problems. - * - * @param component The component to free. - */ - public void freeComponent(AbstractComponent component) { - pool.unregisterComponent(component); - } - - /** - * Frees all references to components created by ComponentManager. - * @see #freeComponent(AbstractComponent) - */ - public synchronized void freeAllComponents() { - pool.clearComponents(); - } - - /** - * Gets the value of a config option of the specified component. - * This is done by first checking, which value the given option - * was set to using {@link #applyConfigEntry(AbstractComponent, ConfigEntry)}. - * If the value has not been changed, the default value for this - * option is returned. Note, that this method will not work properly - * if the component options are changed internally surpassing the - * component manager (which is discouraged). - * - * @param The type of the config option, e.g. String, boolean, integer. - * @param component The component, which has the specified option. - * @param option The option for which we want to know its value. - * @return The value of the specified option in the specified component. - */ - public T getConfigOptionValue(AbstractComponent component, ConfigOption option) { - T object = pool.getLastValidConfigValue(component, option); - if(object==null) { - return option.getDefaultValue(); - } else { - return object; - } - } - - /** - * Works as {@link #getConfigOptionValue(AbstractComponent, ConfigOption)}, - * but using the name of the option instead of a ConfigOption - * object. - * @see #getConfigOptionValue(AbstractComponent, ConfigOption) - * @param component A component. - * @param optionName A valid option name for this component. - * @return The value of the specified option in the specified component. - */ - public Object getConfigOptionValue(AbstractComponent component, String optionName) { - ConfigOption option = (ConfigOption) componentOptionsByName.get( - component.getClass()).get(optionName); - return getConfigOptionValue(component, option); - } - - // convenience method for invoking a static method; - // used as a central point for exception handling for Java reflection - // static method calls - private static Object invokeStaticMethod(Class clazz, String methodName, Object... args) { - // unfortunately Java does not seem to offer a way to call - // a static method given a class object directly, so we have - // to use reflection - try { - Method method = clazz.getMethod(methodName); - return method.invoke(null, args); - } catch (SecurityException e) { - e.printStackTrace(); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } - - return null; - } - - // convenience method for invoking a constructor; - // used as a central point for exception handling for Java reflection - // constructor calls - private T invokeConstructor(Class clazz, Class[] argumentClasses, - Object[] argumentObjects) { - try { - Constructor constructor = clazz.getConstructor(argumentClasses); - return constructor.newInstance(argumentObjects); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } catch (SecurityException e) { - e.printStackTrace(); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } - return null; - } - - /** - * Returns the available options of the specified component. - * @param componentClass The class object of a component. - * @return A list of available configuration options of the specified component. - */ - public static List> getConfigOptions(Class componentClass) { - if (!components.contains(componentClass)) { - System.err.println("Warning: component " + componentClass - + " is not a registered component. [ComponentManager.getConfigOptions]"); - } - return componentOptions.get(componentClass); - } - - /** - * Returns a ConfigOption object given a component and - * the option name. - * @param component A component class object. - * @param name A valid configuration option name for the component. - * @return A ConfigOption object for the specified component class and option name. - */ - public ConfigOption getConfigOption(Class component, String name) { - return componentOptionsByName.get(component).get(name); - } - - /** - * Returns the name of a component. - * @param component A component class object. - * @return The name of the component. - */ - public String getComponentName(Class component) { - return componentNames.get(component); - } - - /** - * Returns a list of all available components in this instance - * of ComponentManager. - * @return the components A list of component classes available in this - * instance of ComponentManager. - */ - public List> getComponents() { - return new LinkedList>(components); - } - - /** - * Returns a list of all available knowledge sources in this instance - * of ComponentManager. - * @return the components A list of knowledge source component classes available in this - * instance of ComponentManager. - */ - public List> getKnowledgeSources() { - return new LinkedList>(knowledgeSources); - } - - /** - * Returns a list of all available reasoners in this instance - * of ComponentManager. - * @return the components A list of reasoner component classes available in this - * instance of ComponentManager. - */ - public List> getReasonerComponents() { - return new LinkedList>(reasonerComponents); - } - - /** - * Returns a list of all available learning problems in this instance - * of ComponentManager. - * @return the components A list of learning problem classes available in this - * instance of ComponentManager. - */ - public List> getLearningProblems() { - return new LinkedList>(learningProblems); - } - - /** - * Returns the set of learning algorithms, which support the given learning problem type. - * @param learningProblem A learning problem type. - * @return The set of learning algorithms applicable for this learning problem. - */ - public List> getApplicableLearningAlgorithms(Class learningProblem) { - List> algorithms = new LinkedList>(); - for(Entry,Collection>> entry : problemAlgorithmsMapping.entrySet()) { - Class prob = entry.getKey(); - if(prob.isAssignableFrom(learningProblem)) { - algorithms.addAll(entry.getValue()); - } - } -// System.out.println(learningProblem + ": " + algorithms); - return algorithms; - } - - /** - * Returns a list of all available learning algorithms in this instance - * of ComponentManager. - * @return the components A list of learning algorithm classes available in this - * instance of ComponentManager. - */ - public List> getLearningAlgorithms() { - return new LinkedList>(learningAlgorithms); - } - - - /** - * Retuns a list of all instanciated and registered Components - * @return Currently active components. - */ - public List getLiveComponents(){ - return pool.getComponents(); - } - - /** - * Retuns a list of all instanciated and registered LearningAlgorithm - * @return Currently active learning algorithms. - */ - public List getLiveLearningAlgorithms(){ - List list = new ArrayList(); - for (AbstractComponent component : cm.getLiveComponents()) { - if(component instanceof AbstractCELA){ - list.add((AbstractCELA) component); - } - - } - return list; - } - - /** - * Retuns a list of all instanciated and registered KnowledgeSource - * @return Currently active knowledge sources. - */ - public List getLiveKnowledgeSources(){ - List list = new ArrayList(); - for (AbstractComponent component : cm.getLiveComponents()) { - if(component instanceof AbstractKnowledgeSource){ - list.add((AbstractKnowledgeSource) component); - } - - } - return list; - } - -} diff --git a/components-core/src/main/java/org/dllearner/core/ComponentPool.java b/components-core/src/main/java/org/dllearner/core/ComponentPool.java deleted file mode 100644 index 308e9b4e2c..0000000000 --- a/components-core/src/main/java/org/dllearner/core/ComponentPool.java +++ /dev/null @@ -1,124 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.core; - -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -import org.apache.log4j.Logger; -import org.dllearner.core.options.ConfigEntry; -import org.dllearner.core.options.ConfigOption; - -/** - * Stores all live components and the configuration options, which were applied - * to them. This allows to detect, which components are currently active, which - * values are assigned to specific options, and to collect statistics (e.g. in - * a web service scenario). - * - * @author Jens Lehmann - * - */ -public final class ComponentPool { - - private static Logger logger = Logger.getLogger(ComponentPool.class); - - // stores all components, which are live (components which are - // no longer used have to be deregistered) - private List components = new LinkedList(); - - // stores the last value which was set for a particular - // config option - private Map, Object>> lastValidConfigValue = new HashMap, Object>>(); - // complete history of all made config entries for a component - private Map>> configEntryHistory = new HashMap>>(); - - /** - * Registers a component instance in the pool. - * @param component The component to add to the pool. - */ - public void registerComponent(AbstractComponent component) { - components.add(component); - Map, Object> emptyMap = new HashMap, Object>(); - lastValidConfigValue.put(component, emptyMap); - configEntryHistory.put(component, new LinkedList>()); - logger.debug("Component instance " + component + " added to component pool."); - } - - /** - * Unregisters a component instance. This method should be used if the - * component will not be used anymore. It frees the memory for - * storing the component and its configuration options. - * @param component The component to remove from the pool. - */ - public void unregisterComponent(AbstractComponent component) { - configEntryHistory.remove(component); - lastValidConfigValue.remove(component); - components.remove(component); - logger.debug("Component instance " + component + " removed from component pool."); - } - - /** - * Gets the last valid config value set for this component. - * @param The type of the value of the config option (String, Integer etc.). - * @param component The component to query. - * @param option The option for which one wants to get the value. - * @return The last value set for this option or null if the value hasn't been - * set using the {@link ComponentManager}. In this case, the value is - * usually at the default value (or has been set internally surpassing the - * component architecture, which is not recommended). - */ - @SuppressWarnings("unchecked") - protected T getLastValidConfigValue(AbstractComponent component, ConfigOption option) { - return (T) lastValidConfigValue.get(component).get(option); - } - - /** - * Add a config entry change for the specified component. - * @param component The component, where the config entry has been set. - * @param entry The set config entry. - * @param valid A boolean value indicating whether the value was valid or not. - */ - protected void addConfigEntry(AbstractComponent component, ConfigEntry entry, boolean valid) { - configEntryHistory.get(component).add(entry); - if (valid) { - lastValidConfigValue.get(component).put(entry.getOption(), entry.getValue()); - } - logger.trace("Config entry " + entry + " has been set for component " + component + " (validity: " + valid + ")."); - } - - /** - * Unregisters all components. - */ - protected void clearComponents() { - components = new LinkedList(); - lastValidConfigValue = new HashMap, Object>>(); - configEntryHistory = new HashMap>>(); - } - - /** - * @return The components in this pool. - */ - public List getComponents(){ - return components; - } - -} diff --git a/components-core/src/main/java/org/dllearner/core/ConsoleAxiomLearningProgressMonitor.java b/components-core/src/main/java/org/dllearner/core/ConsoleAxiomLearningProgressMonitor.java index a4d50fcb9a..41bba3eecc 100644 --- a/components-core/src/main/java/org/dllearner/core/ConsoleAxiomLearningProgressMonitor.java +++ b/components-core/src/main/java/org/dllearner/core/ConsoleAxiomLearningProgressMonitor.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.core; @@ -10,6 +25,9 @@ import org.semanticweb.owlapi.model.OWLAxiom; /** + * A progress monitor for axiom learning algorithms which prints to the console. + * + * @see org.dllearner.core.AxiomLearningProgressMonitor * @author Lorenz Buehmann * */ diff --git a/components-core/src/main/java/org/dllearner/core/EvaluatedAxiom.java b/components-core/src/main/java/org/dllearner/core/EvaluatedAxiom.java index 4f0a69e1c9..8e2498577e 100644 --- a/components-core/src/main/java/org/dllearner/core/EvaluatedAxiom.java +++ b/components-core/src/main/java/org/dllearner/core/EvaluatedAxiom.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,34 +16,21 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TreeSet; - +import com.google.common.collect.ComparisonChain; import org.apache.commons.codec.digest.DigestUtils; import org.dllearner.learningproblems.AxiomScore; import org.dllearner.utilities.EnrichmentVocabulary; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAnnotation; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLNamedIndividual; +import org.semanticweb.owlapi.manchestersyntax.renderer.ManchesterOWLSyntaxObjectRenderer; +import org.semanticweb.owlapi.manchestersyntax.renderer.ManchesterOWLSyntaxPrefixNameShortFormProvider; +import org.semanticweb.owlapi.model.*; import org.semanticweb.owlapi.util.DefaultPrefixManager; - import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; -import uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.ManchesterOWLSyntaxObjectRenderer; -import uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.ManchesterOWLSyntaxPrefixNameShortFormProvider; -import com.google.common.collect.ComparisonChain; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.*; public class EvaluatedAxiom extends EvaluatedHypothesis{ @@ -76,7 +63,7 @@ public String toString() { } public Map> toRDF(String defaultNamespace){ - Map> ind2Axioms = new HashMap>(); + Map> ind2Axioms = new HashMap<>(); OWLDataFactory f = new OWLDataFactoryImpl(); String id = DigestUtils.md5Hex(hypothesis.toString()) + score.getAccuracy(); @@ -94,7 +81,7 @@ public Map> toRDF(String defaultNamespace){ // OWLAxiom ax2 = ax.getAnnotatedAxiom(Collections.singleton(anno)); OWLAxiom ax3 = f.getOWLDataPropertyAssertionAxiom(EnrichmentVocabulary.confidence, ind, score.getAccuracy()); - List axioms = new ArrayList(); + List axioms = new ArrayList<>(); axioms.add(ax1); axioms.add(ax2); axioms.add(ax3); @@ -144,10 +131,10 @@ public static List> getBestEvaluatedAxiom public static List> getBestEvaluatedAxioms(Set> evaluatedAxioms, int nrOfAxioms, double accuracyThreshold) { - List> returnList = new ArrayList>(); + List> returnList = new ArrayList<>(); //get the currently best evaluated axioms - Set> orderedEvaluatedAxioms = new TreeSet>(evaluatedAxioms); + Set> orderedEvaluatedAxioms = new TreeSet<>(evaluatedAxioms); for(EvaluatedAxiom evAx : orderedEvaluatedAxioms){ if(evAx.getScore().getAccuracy() >= accuracyThreshold && returnList.size() < nrOfAxioms){ diff --git a/components-core/src/main/java/org/dllearner/core/EvaluatedDescription.java b/components-core/src/main/java/org/dllearner/core/EvaluatedDescription.java index 08c4e5938f..e044ee85d7 100644 --- a/components-core/src/main/java/org/dllearner/core/EvaluatedDescription.java +++ b/components-core/src/main/java/org/dllearner/core/EvaluatedDescription.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; import org.dllearner.utilities.owl.OWLAPIRenderers; @@ -44,7 +43,7 @@ public EvaluatedDescription(OWLClassExpression description, S score) { } /** - * @see org.dllearner.core.owl.Description#getLength() + * @see OWLClassExpressionUtils#getLength(OWLClassExpression) * @return Length of the description. */ public int getDescriptionLength() { @@ -52,7 +51,7 @@ public int getDescriptionLength() { } /** - * @see org.dllearner.core.owl.Description#getDepth() + * @see OWLClassExpressionUtils#getDepth(OWLClassExpression) * @return Depth of the description. */ public int getDescriptionDepth() { diff --git a/components-core/src/main/java/org/dllearner/core/EvaluatedHypothesis.java b/components-core/src/main/java/org/dllearner/core/EvaluatedHypothesis.java index 5353172382..ee41d908a3 100644 --- a/components-core/src/main/java/org/dllearner/core/EvaluatedHypothesis.java +++ b/components-core/src/main/java/org/dllearner/core/EvaluatedHypothesis.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,12 +16,12 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; import java.io.Serializable; import java.text.DecimalFormat; +import org.jetbrains.annotations.NotNull; import org.semanticweb.owlapi.model.OWLObject; import com.google.common.collect.ComparisonChain; @@ -76,7 +76,7 @@ public void setDescription(T hypothesis) { } /** - * @see org.dllearner.core.Score#getScoreValue() + * @see Score#getAccuracy() * @return Value in this score system. */ public double getAccuracy() { @@ -92,7 +92,7 @@ public String toString() { * @see java.lang.Comparable#compareTo(java.lang.Object) */ @Override - public int compareTo(EvaluatedHypothesis o) { + public int compareTo(@NotNull EvaluatedHypothesis o) { return ComparisonChain.start() .compare(score.getAccuracy(), o.score.getAccuracy()) .compare(hypothesis, o.getDescription()) diff --git a/components-core/src/main/java/org/dllearner/core/FuzzyClassExpressionLearningAlgorithm.java b/components-core/src/main/java/org/dllearner/core/FuzzyClassExpressionLearningAlgorithm.java index 011595c007..cc1151c5e8 100644 --- a/components-core/src/main/java/org/dllearner/core/FuzzyClassExpressionLearningAlgorithm.java +++ b/components-core/src/main/java/org/dllearner/core/FuzzyClassExpressionLearningAlgorithm.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; import java.util.List; @@ -38,7 +37,7 @@ public interface FuzzyClassExpressionLearningAlgorithm extends LearningAlgorithm * @param nrOfDescriptions Limit for the number or returned descriptions. * @return The best class descriptions found by the learning algorithm so far. */ - public List getCurrentlyBestDescriptions(int nrOfDescriptions); + List getCurrentlyBestDescriptions(int nrOfDescriptions); /** * Return the best currently found concepts up to some maximum @@ -46,6 +45,6 @@ public interface FuzzyClassExpressionLearningAlgorithm extends LearningAlgorithm * @param nrOfDescriptions Maximum number of descriptions returned. * @return Return value is getCurrentlyBestDescriptions(nrOfDescriptions, 0.0, false). */ - public List getCurrentlyBestEvaluatedDescriptions(int nrOfDescriptions); + List getCurrentlyBestEvaluatedDescriptions(int nrOfDescriptions); } diff --git a/components-core/src/main/java/org/dllearner/core/Heuristic.java b/components-core/src/main/java/org/dllearner/core/Heuristic.java index bb5d12a8ab..2e2ba27e84 100644 --- a/components-core/src/main/java/org/dllearner/core/Heuristic.java +++ b/components-core/src/main/java/org/dllearner/core/Heuristic.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.core; diff --git a/components-core/src/main/java/org/dllearner/core/IndividualReasoner.java b/components-core/src/main/java/org/dllearner/core/IndividualReasoner.java index 5ea0d57ad7..3ece806f5b 100644 --- a/components-core/src/main/java/org/dllearner/core/IndividualReasoner.java +++ b/components-core/src/main/java/org/dllearner/core/IndividualReasoner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; import java.util.Map; @@ -46,45 +45,45 @@ public interface IndividualReasoner { * @param individual An individual in the knowledge base. * @return Types this individual is instance of. */ - public Set getTypes(OWLIndividual individual); + Set getTypes(OWLIndividual individual); /** * Checks whether individual is instance of description. * For instance, "Leipzig" may be an instance of "City". * - * @param OWLClassExpression An OWL class description. + * @param description An OWL class description. * @param individual An individual. * @return True if the instance has the OWLClassExpression as type and false otherwise. */ - public boolean hasType(OWLClassExpression description, OWLIndividual individual); + boolean hasType(OWLClassExpression description, OWLIndividual individual); /** * Performs instance checks on a set of instances (reasoners might be more * efficient than handling each check separately). - * @param OWLClassExpression An OWL class description. + * @param description An OWL class description. * @param individuals An individual. * @return The subset of those instances, which have the given type. */ - public SortedSet hasType(OWLClassExpression description, Set individuals); + SortedSet hasType(OWLClassExpression description, Set individuals); /** - * Gets all instances of a given class OWLClassExpression in the knowledge base. - * @param OWLClassExpression An OWL class description. + * Gets all instances of a given class expression in the knowledge base. + * @param description An OWL class description. * @return All instances of the class description. */ - public SortedSet getIndividuals(OWLClassExpression description); + SortedSet getIndividuals(OWLClassExpression description); /** - * Performs a query for all instances of the given class OWLClassExpression and + * Performs a query for all instances of the given class expression and * its negation. (Note that in OWL it is possible that the reasoner can * neither deduce that an individual is instance of a class nor its * negation.) This method might be more efficient that performing a * two retrievals. * - * @param OWLClassExpression An OWL class description. + * @param description An OWL class description. * @return All instances of the class OWLClassExpression and its negation. */ - public SortedSetTuple doubleRetrieval(OWLClassExpression description); + SortedSetTuple doubleRetrieval(OWLClassExpression description); /** * Returns the set of individuals, which are connect to the given individual @@ -93,17 +92,17 @@ public interface IndividualReasoner { * @param objectProperty An object property, e.g. hasChild. * @return A set of individuals, e.g. {anna, maria}. */ - public Set getRelatedIndividuals(OWLIndividual individual, - OWLObjectProperty objectProperty); + Set getRelatedIndividuals(OWLIndividual individual, + OWLObjectProperty objectProperty); /** * Returns the set of individuals, which are connect to the given individual * with the specified data property. * @param individual An individual, e.g. eric. - * @param datatyoeProperty A data property, e.g. hasIncome. + * @param datatypeProperty A data property, e.g. hasIncome. * @return A set of individuals, e.g. {48000^^xsd:int}. - */ - public Set getRelatedValues(OWLIndividual individual, OWLDataProperty datatypeProperty); + */ + Set getRelatedValues(OWLIndividual individual, OWLDataProperty datatypeProperty); /** * A map of properties related to an individual, e.g. @@ -113,7 +112,7 @@ public Set getRelatedIndividuals(OWLIndividual individual, * @return A map of of properties connected to the individual as keys and the individuals * they point to as values. */ - public Map> getObjectPropertyRelationships(OWLIndividual individual); + Map> getObjectPropertyRelationships(OWLIndividual individual); /** * Computes and returns all connections between individuals through the specified @@ -121,7 +120,7 @@ public Set getRelatedIndividuals(OWLIndividual individual, * @param objectProperty An object property. * @return The mapping of individuals to other individuals through this object property. */ - public Map> getPropertyMembers(OWLObjectProperty objectProperty); + Map> getPropertyMembers(OWLObjectProperty objectProperty); /** * Computes and returns all connections between individuals and values through the @@ -129,7 +128,7 @@ public Set getRelatedIndividuals(OWLIndividual individual, * @param datatypeProperty A data property. * @return The mapping between individuals and values through the given property. */ - public Map> getDatatypeMembers(OWLDataProperty datatypeProperty); + Map> getDatatypeMembers(OWLDataProperty datatypeProperty); /** * Convenience method, which can be used if it is known that the property has @@ -139,7 +138,7 @@ public Set getRelatedIndividuals(OWLIndividual individual, * @param datatypeProperty A data property. * @return The mapping between individuals and double values through the given property. */ - public Map> getDoubleDatatypeMembers(OWLDataProperty datatypeProperty); + Map> getDoubleDatatypeMembers(OWLDataProperty datatypeProperty); /** * Convenience method, which can be used if it is known that the property has @@ -149,7 +148,7 @@ public Set getRelatedIndividuals(OWLIndividual individual, * @param datatypeProperty A data property. * @return The mapping between individuals and integer values through the given property. */ - public Map> getIntDatatypeMembers(OWLDataProperty datatypeProperty); + Map> getIntDatatypeMembers(OWLDataProperty datatypeProperty); /** * Convenience method, which can be used if it is known that the property has @@ -160,11 +159,15 @@ public Set getRelatedIndividuals(OWLIndividual individual, * @return The mapping between individuals and numeric values of given type through the given property. */ Map> getNumericDatatypeMembers(OWLDataProperty datatypeProperty, Class clazz); + /** - * @param datatypeProperty - * @return + * Computes and returns all connections between individuals and numeric values through the + * specified property, e.g. {eric => {48000^^xsd:int}, sarah => {56000^^xsd:int}}. + * @param datatypeProperty A data property. + * @return The mapping between individuals and numeric values through the given property. */ > Map> getNumericDatatypeMembers(OWLDataProperty datatypeProperty); + /** * Convenience method, which can be used if it is known that the property has * values which can be parsed as boolean value. Only "true" or "false" are @@ -173,7 +176,7 @@ public Set getRelatedIndividuals(OWLIndividual individual, * @param datatypeProperty A data property. * @return The mapping between individuals and boolean values through the given property. */ - public Map> getBooleanDatatypeMembers(OWLDataProperty datatypeProperty); + Map> getBooleanDatatypeMembers(OWLDataProperty datatypeProperty); /** * Convenience method, which can be used to get all individuals, which have value @@ -184,7 +187,7 @@ public Set getRelatedIndividuals(OWLIndividual individual, * @param datatypeProperty A data property. * @return The set of individuals for which the boolean property holds. */ - public SortedSet getTrueDatatypeMembers(OWLDataProperty datatypeProperty); + SortedSet getTrueDatatypeMembers(OWLDataProperty datatypeProperty); /** * Convenience method, which can be used to get all individuals, which have value @@ -195,7 +198,7 @@ public Set getRelatedIndividuals(OWLIndividual individual, * @param datatypeProperty A data property. * @return The set of individuals for which the boolean property does not hold. */ - public SortedSet getFalseDatatypeMembers(OWLDataProperty datatypeProperty); + SortedSet getFalseDatatypeMembers(OWLDataProperty datatypeProperty); /** * Convenience method, which can be used which returns the property values as @@ -204,11 +207,15 @@ public Set getRelatedIndividuals(OWLIndividual individual, * @param datatypeProperty A data property. * @return The mapping between individuals and string values through the given property. */ - public Map> getStringDatatypeMembers(OWLDataProperty datatypeProperty); + Map> getStringDatatypeMembers(OWLDataProperty datatypeProperty); /** - * @param individual - * @return + * A map of data properties related to values, e.g. + * {birthDate => {eric, "1980-10-10"^^xsd:date}, height => {Mount_Everest, 8880}}. + * + * @param individual An individual. + * @return A map of of data properties connected to the individual as keys and the literals + * they point to as values. */ Map> getDataPropertyRelationships(OWLIndividual individual); diff --git a/components-core/src/main/java/org/dllearner/core/KnowledgeSource.java b/components-core/src/main/java/org/dllearner/core/KnowledgeSource.java index 33cdd86986..85ef9891b6 100644 --- a/components-core/src/main/java/org/dllearner/core/KnowledgeSource.java +++ b/components-core/src/main/java/org/dllearner/core/KnowledgeSource.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; /** diff --git a/components-core/src/main/java/org/dllearner/core/LearningAlgorithm.java b/components-core/src/main/java/org/dllearner/core/LearningAlgorithm.java index 5e2bea7c21..75e46a57bd 100644 --- a/components-core/src/main/java/org/dllearner/core/LearningAlgorithm.java +++ b/components-core/src/main/java/org/dllearner/core/LearningAlgorithm.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; import org.springframework.beans.factory.annotation.Autowired; @@ -34,19 +33,19 @@ public interface LearningAlgorithm extends Component { * Starts the algorithm. It runs until paused, stopped, or * a termination criterion has been reached. */ - public abstract void start(); + void start(); /** * Get underlying learning problem. * @return Underlying learning problem. */ - public LearningProblem getLearningProblem(); + LearningProblem getLearningProblem(); /** * Set the learning problem, which the algorithm should solve. * @param learningProblem The learning problem to solve. */ @Autowired - public void setLearningProblem(LearningProblem learningProblem); + void setLearningProblem(LearningProblem learningProblem); } diff --git a/components-core/src/main/java/org/dllearner/core/LearningProblem.java b/components-core/src/main/java/org/dllearner/core/LearningProblem.java index e09fb2ca06..129fbd346e 100644 --- a/components-core/src/main/java/org/dllearner/core/LearningProblem.java +++ b/components-core/src/main/java/org/dllearner/core/LearningProblem.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; /** diff --git a/components-core/src/main/java/org/dllearner/core/LearningProblemUnsupportedException.java b/components-core/src/main/java/org/dllearner/core/LearningProblemUnsupportedException.java index e237510ef8..b53354a8b0 100644 --- a/components-core/src/main/java/org/dllearner/core/LearningProblemUnsupportedException.java +++ b/components-core/src/main/java/org/dllearner/core/LearningProblemUnsupportedException.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; import java.util.Collection; diff --git a/components-core/src/main/java/org/dllearner/core/OntologyFormat.java b/components-core/src/main/java/org/dllearner/core/OntologyFormat.java index ca4892965c..aecfadfc8d 100644 --- a/components-core/src/main/java/org/dllearner/core/OntologyFormat.java +++ b/components-core/src/main/java/org/dllearner/core/OntologyFormat.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; public enum OntologyFormat { diff --git a/components-core/src/main/java/org/dllearner/core/OntologyFormatUnsupportedException.java b/components-core/src/main/java/org/dllearner/core/OntologyFormatUnsupportedException.java index 92903b9130..09c3571164 100644 --- a/components-core/src/main/java/org/dllearner/core/OntologyFormatUnsupportedException.java +++ b/components-core/src/main/java/org/dllearner/core/OntologyFormatUnsupportedException.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; diff --git a/components-core/src/main/java/org/dllearner/core/Oracle.java b/components-core/src/main/java/org/dllearner/core/Oracle.java index c597baf54b..ce4a80a795 100644 --- a/components-core/src/main/java/org/dllearner/core/Oracle.java +++ b/components-core/src/main/java/org/dllearner/core/Oracle.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; import java.util.List; @@ -63,6 +62,6 @@ public interface Oracle { * returned. * */ - public List classifyIndividuals(List individuals); + List classifyIndividuals(List individuals); } \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/core/ProbabilityBasedAxiomScoreCalculator.java b/components-core/src/main/java/org/dllearner/core/ProbabilityBasedAxiomScoreCalculator.java index 65a82fcbd9..92046ab6e5 100644 --- a/components-core/src/main/java/org/dllearner/core/ProbabilityBasedAxiomScoreCalculator.java +++ b/components-core/src/main/java/org/dllearner/core/ProbabilityBasedAxiomScoreCalculator.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.core; import org.aksw.jena_sparql_api.core.QueryExecutionFactory; diff --git a/components-core/src/main/java/org/dllearner/core/Reasoner.java b/components-core/src/main/java/org/dllearner/core/Reasoner.java index b6c6d27003..4d40bcd5db 100755 --- a/components-core/src/main/java/org/dllearner/core/Reasoner.java +++ b/components-core/src/main/java/org/dllearner/core/Reasoner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; import org.dllearner.core.fuzzydll.FuzzyIndividualReasoner; diff --git a/components-core/src/main/java/org/dllearner/core/ReasonerComponent.java b/components-core/src/main/java/org/dllearner/core/ReasonerComponent.java index 7122875842..95483948e7 100644 --- a/components-core/src/main/java/org/dllearner/core/ReasonerComponent.java +++ b/components-core/src/main/java/org/dllearner/core/ReasonerComponent.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; /** diff --git a/components-core/src/main/java/org/dllearner/core/ReasoningMethodUnsupportedException.java b/components-core/src/main/java/org/dllearner/core/ReasoningMethodUnsupportedException.java index 64a10bfbf2..31fa0da937 100755 --- a/components-core/src/main/java/org/dllearner/core/ReasoningMethodUnsupportedException.java +++ b/components-core/src/main/java/org/dllearner/core/ReasoningMethodUnsupportedException.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; /** diff --git a/components-core/src/main/java/org/dllearner/core/ResumableLearningAlgorithm.java b/components-core/src/main/java/org/dllearner/core/ResumableLearningAlgorithm.java index 1550e0684b..9e667d6aff 100644 --- a/components-core/src/main/java/org/dllearner/core/ResumableLearningAlgorithm.java +++ b/components-core/src/main/java/org/dllearner/core/ResumableLearningAlgorithm.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; /** @@ -31,7 +30,7 @@ public interface ResumableLearningAlgorithm { * Pauses the algorithm (not all algorithms need to implement * this operation). */ - public void pause(); + void pause(); /** * Resumes the algorithm (not all algorithms need to implement @@ -40,6 +39,6 @@ public interface ResumableLearningAlgorithm { * reached. It will run until paused, stopped, or terminated * again. */ - public void resume(); + void resume(); } diff --git a/components-core/src/main/java/org/dllearner/core/SchemaReasoner.java b/components-core/src/main/java/org/dllearner/core/SchemaReasoner.java index 9050205cbd..30352a281e 100644 --- a/components-core/src/main/java/org/dllearner/core/SchemaReasoner.java +++ b/components-core/src/main/java/org/dllearner/core/SchemaReasoner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; import java.util.Set; @@ -46,7 +45,7 @@ public interface SchemaReasoner { * have instances. * @return The set of inconsistent classes. */ - public Set getInconsistentClasses(); + Set getInconsistentClasses(); /** * Returns the domain of this object property. (Theoretically, there could @@ -55,28 +54,28 @@ public interface SchemaReasoner { * @param objectProperty An object property in the knowledge base. * @return The rdfs:domain of objectProperty */ - public OWLClassExpression getDomain(OWLObjectProperty objectProperty); + OWLClassExpression getDomain(OWLObjectProperty objectProperty); /** * Returns the domain of this data property. * @param datatypeProperty An data property in the knowledge base. * @return The rdfs:domain of datatypeProperty - */ - public OWLClassExpression getDomain(OWLDataProperty datatypeProperty); + */ + OWLClassExpression getDomain(OWLDataProperty datatypeProperty); /** * Returns the range of this object property. * @param objectProperty An object property in the knowledge base. * @return The rdfs:range of objectProperty - */ - public OWLClassExpression getRange(OWLObjectProperty objectProperty); + */ + OWLClassExpression getRange(OWLObjectProperty objectProperty); /** * Returns the range of this data property. * @param datatypeProperty An data property in the knowledge base. * @return The rdfs:range of datatypeProperty - */ - public OWLDataRange getRange(OWLDataProperty datatypeProperty); + */ + OWLDataRange getRange(OWLDataProperty datatypeProperty); /** * Checks whether superClass is a super class of subClass. @@ -84,7 +83,7 @@ public interface SchemaReasoner { * @param subClass The (supposed) sub class. * @return Whether superClass is a super class of subClass. */ - public boolean isSuperClassOf(OWLClassExpression superClass, OWLClassExpression subClass); + boolean isSuperClassOf(OWLClassExpression superClass, OWLClassExpression subClass); /** * Checks whether class1 is equivalent to class2. @@ -92,7 +91,7 @@ public interface SchemaReasoner { * @param class2 The second class2. * @return Whether class1 is equivalent to class2. */ - public boolean isEquivalentClass(OWLClassExpression class1, OWLClassExpression class2); + boolean isEquivalentClass(OWLClassExpression class1, OWLClassExpression class2); /** * Checks whether class1 is disjoint with class2. @@ -100,14 +99,14 @@ public interface SchemaReasoner { * @param class2 The second class2. * @return Whether class1 is disjoint with class2. */ - public boolean isDisjoint(OWLClass class1, OWLClass class2); + boolean isDisjoint(OWLClass class1, OWLClass class2); /** * Returns all asserted owl:equivalence class axioms for the given class. * @param namedClass A named class in the background knowledge. * @return A set of descriptions asserted to be equal to the named class. */ - public Set getAssertedDefinitions(OWLClass namedClass); + Set getAssertedDefinitions(OWLClass namedClass); /** * Checks which of superClasses are super classes of subClass @@ -115,14 +114,14 @@ public interface SchemaReasoner { * @param subClasses The (supposed) sub class. * @return The subset of superClasses, which satisfy the superclass-subclass relationship. */ - public Set isSuperClassOf(Set superClasses, OWLClassExpression subClasses); + Set isSuperClassOf(Set superClasses, OWLClassExpression subClasses); /** * Computes and returns the class hierarchy of the knowledge base. * * @return The subsumption hierarchy of this knowledge base. */ - public Hierarchy getClassHierarchy(); + Hierarchy getClassHierarchy(); /** * Returns direct super classes in the class hierarchy. @@ -131,7 +130,7 @@ public interface SchemaReasoner { * Atomic concept, top, or bottom. * @return A set of more general concepts. */ - public SortedSet getSuperClasses(OWLClassExpression description); + SortedSet getSuperClasses(OWLClassExpression description); /** * Returns direct sub classes in the class hierarchy. @@ -140,96 +139,98 @@ public interface SchemaReasoner { * Atomic concept, top, or bottom. * @return A set of more special concepts. */ - public SortedSet getSubClasses(OWLClassExpression description); + SortedSet getSubClasses(OWLClassExpression description); /** * Computes and returns the object property hierarchy of the knowledge base. * @return The object property hierarchy of the knowlege base. */ - public ObjectPropertyHierarchy getObjectPropertyHierarchy(); + ObjectPropertyHierarchy getObjectPropertyHierarchy(); /** * Returns more general concepts in the subsumption hierarchy. * - * @see OWLObjectPropertyHierarchy#getMoreGeneralRoles(OWLObjectProperty) + * @see ObjectPropertyHierarchy#getMoreGeneralRoles(OWLObjectProperty) * @param objectProperty * Atomic concept, top, or bottom. * @return A set of more general concepts. */ - public SortedSet getSuperProperties(OWLObjectProperty objectProperty); + SortedSet getSuperProperties(OWLObjectProperty objectProperty); /** * Returns more special concepts in the subsumption hierarchy. * - * @see OWLObjectPropertyHierarchy#getMoreSpecialRoles(OWLObjectProperty) + * @see ObjectPropertyHierarchy#getMoreSpecialRoles(OWLObjectProperty) * @param objectProperty * Atomic concept, top, or bottom. * @return A set of more special concepts. */ - public SortedSet getSubProperties(OWLObjectProperty objectProperty); + SortedSet getSubProperties(OWLObjectProperty objectProperty); /** * TODO Outdated in OWL 2, because the universal role is the most general. - * @see OWLObjectPropertyHierarchy#getMostGeneralRoles() + * @see ObjectPropertyHierarchy#getMostGeneralRoles() * @return The most general roles. */ - public SortedSet getMostGeneralProperties(); + SortedSet getMostGeneralProperties(); /** * TODO Outdated in OWL, because the bottom role is the most specific. - * @see OWLObjectPropertyHierarchy#getMostSpecialRoles() + * @see ObjectPropertyHierarchy#getMostSpecialRoles() * @return The most special roles. */ - public SortedSet getMostSpecialProperties(); + SortedSet getMostSpecialProperties(); /** * Computes and returns the data property hierarchy of the knowledge base. * @return The data property hierarchy of the knowlege base. - */ - public DatatypePropertyHierarchy getDatatypePropertyHierarchy(); + */ + DatatypePropertyHierarchy getDatatypePropertyHierarchy(); /** * Returns more general concepts in the subsumption hierarchy. * - * @see OWLObjectPropertyHierarchy#getMoreGeneralRoles(OWLObjectProperty) + * @see DatatypePropertyHierarchy#getMoreGeneralRoles(OWLDataProperty) * @param dataProperty * Atomic concept, top, or bottom. * @return A set of more general concepts. */ - public SortedSet getSuperProperties(OWLDataProperty dataProperty); + SortedSet getSuperProperties(OWLDataProperty dataProperty); /** * Returns more special concepts in the subsumption hierarchy. * - * @see OWLObjectPropertyHierarchy#getMoreSpecialRoles(OWLObjectProperty) + * @see DatatypePropertyHierarchy#getMoreSpecialRoles(OWLDataProperty) * @param dataProperty * Atomic concept, top, or bottom. * @return A set of more special concepts. */ - public SortedSet getSubProperties(OWLDataProperty dataProperty); + SortedSet getSubProperties(OWLDataProperty dataProperty); /** - * @see OWLObjectPropertyHierarchy#getMostGeneralRoles() + * @see DatatypePropertyHierarchy#getMostGeneralRoles() * @return The most general roles. */ - public SortedSet getMostGeneralDatatypeProperties(); + SortedSet getMostGeneralDatatypeProperties(); /** - * @see OWLObjectPropertyHierarchy#getMostSpecialRoles() + * @see DatatypePropertyHierarchy#getMostSpecialRoles() * @return The most special roles. */ - public SortedSet getMostSpecialDatatypeProperties(); + SortedSet getMostSpecialDatatypeProperties(); /** - * @param role - * @return + * Computes all super properties for the given property. + * @param property the property + * @return all super properties */ - SortedSet getSuperProperties(T role); + SortedSet getSuperProperties(T property); /** - * @param role - * @return + * Computes all sub properties for the given property. + * @param property the property + * @return all sub properties */ - SortedSet getSubProperties(T role); + SortedSet getSubProperties(T property); } diff --git a/components-core/src/main/java/org/dllearner/core/Score.java b/components-core/src/main/java/org/dllearner/core/Score.java index 3a19265ac5..861c0e3c21 100644 --- a/components-core/src/main/java/org/dllearner/core/Score.java +++ b/components-core/src/main/java/org/dllearner/core/Score.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,16 +16,17 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; +import org.semanticweb.owlapi.model.OWLObject; + import java.io.Serializable; /** - * The score class is used to store how well a class OWLClassExpression did + * The score class is used to store how well a hypothesis did * on a learning problem. Depending on the learning problem at hand, * different criteria can be used. (Similar learning problems probably - * score class descriptions/hypothesis in a similar way.) + * score hypothesis in a similar way.) * * TODO: Maybe we don't really need a score, but only EvaluatedDescription. * @@ -34,17 +35,14 @@ */ public abstract class Score implements Serializable{ - /** - * - */ private static final long serialVersionUID = -6479328496461875019L; /** * This method returns a value, which indicates how accurate a - * class OWLClassExpression solves a learning problem. + * hypothesis solves a learning problem. * - * @see AbstractClassExpressionLearningProblem#getAccuracy(OWLClassExpression) - * @return A value between 0 and 1 indicating the quality (of a class description). + * @see AbstractLearningProblem#getAccuracyOrTooWeak(OWLObject, double) + * @return A value between 0 and 1 indicating the quality (of a hypothesis). */ public abstract double getAccuracy(); diff --git a/components-core/src/main/java/org/dllearner/core/SilentAxiomLearningProgressMonitor.java b/components-core/src/main/java/org/dllearner/core/SilentAxiomLearningProgressMonitor.java index cf52fe19ed..f503f59757 100644 --- a/components-core/src/main/java/org/dllearner/core/SilentAxiomLearningProgressMonitor.java +++ b/components-core/src/main/java/org/dllearner/core/SilentAxiomLearningProgressMonitor.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.core; @@ -8,41 +23,24 @@ /** * An axiom learning progress monitor that is doing nothing. + * * @author Lorenz Buehmann * */ public class SilentAxiomLearningProgressMonitor implements AxiomLearningProgressMonitor{ @Override - public void learningStarted(AxiomType axiomType) { - // TODO Auto-generated method stub - - } + public void learningStarted(AxiomType axiomType) {} @Override - public void learningStopped(AxiomType axiomType) { - // TODO Auto-generated method stub - - } + public void learningStopped(AxiomType axiomType) {} @Override - public void learningProgressChanged(AxiomType axiomType, - int value, int max) { - // TODO Auto-generated method stub - - } + public void learningProgressChanged(AxiomType axiomType, int value, int max) {} @Override - public void learningTaskBusy(AxiomType axiomType) { - // TODO Auto-generated method stub - - } + public void learningTaskBusy(AxiomType axiomType) {} @Override - public void learningFailed(AxiomType axiomType) { - // TODO Auto-generated method stub - - } - - + public void learningFailed(AxiomType axiomType) {} } diff --git a/components-core/src/main/java/org/dllearner/core/SparqlQueryLearningAlgorithm.java b/components-core/src/main/java/org/dllearner/core/SparqlQueryLearningAlgorithm.java index 665886e535..674ba06606 100644 --- a/components-core/src/main/java/org/dllearner/core/SparqlQueryLearningAlgorithm.java +++ b/components-core/src/main/java/org/dllearner/core/SparqlQueryLearningAlgorithm.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; import java.util.List; @@ -36,9 +35,9 @@ public interface SparqlQueryLearningAlgorithm extends LearningAlgorithm { * @param nrOfSPARQLQueries Limit for the number or returned SPARQL queries. * @return The best SPARQL queries found by the learning algorithm so far. */ - public List getCurrentlyBestSPARQLQueries(int nrOfSPARQLQueries); + List getCurrentlyBestSPARQLQueries(int nrOfSPARQLQueries); - public String getBestSPARQLQuery(); + String getBestSPARQLQuery(); diff --git a/components-core/src/main/java/org/dllearner/core/StoppableLearningAlgorithm.java b/components-core/src/main/java/org/dllearner/core/StoppableLearningAlgorithm.java index 2d978928ce..f1d45734b8 100644 --- a/components-core/src/main/java/org/dllearner/core/StoppableLearningAlgorithm.java +++ b/components-core/src/main/java/org/dllearner/core/StoppableLearningAlgorithm.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core; /** @@ -34,7 +33,7 @@ public interface StoppableLearningAlgorithm extends LearningAlgorithm { * Stops the algorithm gracefully. A stopped algorithm cannot be resumed anymore. * Use this method for cleanup and freeing memory. */ - public abstract void stop(); + void stop(); /** * Returns whether the learning algorithm is running. Implementation @@ -42,6 +41,6 @@ public interface StoppableLearningAlgorithm extends LearningAlgorithm { * the start and resume methods. * @return True if the algorithm is running, false otherwise. */ - public abstract boolean isRunning(); + boolean isRunning(); } diff --git a/components-core/src/main/java/org/dllearner/core/StringRenderer.java b/components-core/src/main/java/org/dllearner/core/StringRenderer.java new file mode 100644 index 0000000000..13b4ed6ae0 --- /dev/null +++ b/components-core/src/main/java/org/dllearner/core/StringRenderer.java @@ -0,0 +1,90 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.core; + +import org.dllearner.utilities.owl.DLSyntaxObjectRenderer; +import org.dllearner.utilities.owl.ManchesterOWLSyntaxOWLObjectRendererImplExt; +import org.semanticweb.owlapi.io.OWLObjectRenderer; +import org.semanticweb.owlapi.io.ToStringRenderer; +import org.semanticweb.owlapi.util.ShortFormProvider; +import org.semanticweb.owlapi.util.SimpleRenderer; + +/** + * A renderer for OWL objects. Different syntaxes are supported, e.g., DL syntax or Manchester OWL syntax. + * + * @author Simon Bin + */ +public final class StringRenderer { + public enum Rendering { + OWLAPI_SYNTAX("owlapi", new SimpleRenderer()), + DL_SYNTAX("dlsyntax", new DLSyntaxObjectRenderer()), + MANCHESTER_SYNTAX("manchester", new ManchesterOWLSyntaxOWLObjectRendererImplExt(true, false)), + MANCHESTER_SYNTAX_NL("manchester_nl", new ManchesterOWLSyntaxOWLObjectRendererImplExt(true, true)); + + String name; + OWLObjectRenderer renderer; + + Rendering(String name, OWLObjectRenderer render) { + this.name = name; + this.renderer = render; + } + + public String getName() { + return name; + } + + public OWLObjectRenderer getRenderer() { + return renderer; + } + } + + static OWLObjectRenderer renderer; + static { + setRenderer(Rendering.MANCHESTER_SYNTAX.getRenderer()); + } + + public static void setRenderer(Rendering r) { + setRenderer(r.getRenderer()); + } + + @SuppressWarnings("deprecation") + public static void setRenderer(OWLObjectRenderer r) { + ToStringRenderer.getInstance().setRenderer(r); + renderer = r; + } + + public static void setRenderer(String syntax) { + for (Rendering r : Rendering.values()) { + if(syntax.equals(r.getName())) { + setRenderer(r); + return; + } + } + throw new IllegalArgumentException("No such Renderer: "+syntax); + } + + public static OWLObjectRenderer getRenderer() { + return renderer; + } + + public static void setShortFormProvider( + ShortFormProvider shortFormProvider) { + ToStringRenderer.getInstance().setShortFormProvider(shortFormProvider); + } +} diff --git a/components-core/src/main/java/org/dllearner/core/annotations/OutVariable.java b/components-core/src/main/java/org/dllearner/core/annotations/OutVariable.java new file mode 100644 index 0000000000..41b939eea2 --- /dev/null +++ b/components-core/src/main/java/org/dllearner/core/annotations/OutVariable.java @@ -0,0 +1,32 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.core.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Non-config option with output semantics (query algorithm result) + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface OutVariable { +} diff --git a/components-core/src/main/java/org/dllearner/core/annotations/Unused.java b/components-core/src/main/java/org/dllearner/core/annotations/Unused.java new file mode 100644 index 0000000000..cd280dcfc7 --- /dev/null +++ b/components-core/src/main/java/org/dllearner/core/annotations/Unused.java @@ -0,0 +1,32 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.core.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Unused settings + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface Unused { +} diff --git a/components-core/src/main/java/org/dllearner/core/config/ConfigOption.java b/components-core/src/main/java/org/dllearner/core/config/ConfigOption.java index 909a33d876..e2f4b93709 100644 --- a/components-core/src/main/java/org/dllearner/core/config/ConfigOption.java +++ b/components-core/src/main/java/org/dllearner/core/config/ConfigOption.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,10 +16,8 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core.config; -import java.beans.PropertyEditor; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -41,11 +39,11 @@ * The name of this config option. * @return The name of this config option. */ - String name(); + String name() default "ZZZZZZZZ"; /** * The description of this config option - * @return + * @return The description of this config option */ String description() default "no description available"; @@ -59,11 +57,11 @@ /** * Returns the default value of this config option. Default values should be set for all - * optional values. + * optional values. * It is an overhead to describe the default value both in the source code and in the - * annotation. There are two reasons for this: a) the value of the field cannot easily be accessed + * annotation. There are two reasons for this: a) the value of the field cannot easily be accessed * without creating an instance of the component and b) for more complex structures the default - * may only be created in the constructor or init method. + * may only be created in the constructor or init method. * @return The default value of this option. */ String defaultValue() default ""; diff --git a/components-core/src/main/java/org/dllearner/core/fuzzydll/FuzzyIndividualReasoner.java b/components-core/src/main/java/org/dllearner/core/fuzzydll/FuzzyIndividualReasoner.java index 8080d31a68..aea75d21b2 100644 --- a/components-core/src/main/java/org/dllearner/core/fuzzydll/FuzzyIndividualReasoner.java +++ b/components-core/src/main/java/org/dllearner/core/fuzzydll/FuzzyIndividualReasoner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core.fuzzydll; import java.util.SortedSet; @@ -36,11 +35,11 @@ public interface FuzzyIndividualReasoner { * Checks the fuzzy membership degree of individual over description. * For instance, "Peter" may be an instance of "TallPerson" with fuzzy membership degree = 0.8. * individual - * @param OWLClassExpression An OWL class description. + * @param description An OWL class description. * @param individual An individual. * @return fuzzy membership degree of individual satisfying description [0-1]. */ - public double hasTypeFuzzyMembership(OWLClassExpression description, FuzzyIndividual individual); + double hasTypeFuzzyMembership(OWLClassExpression description, FuzzyIndividual individual); - public SortedSet getFuzzyIndividuals(OWLClassExpression concept); + SortedSet getFuzzyIndividuals(OWLClassExpression concept); } diff --git a/components-core/src/main/java/org/dllearner/core/fuzzydll/FuzzyUnsupportedCodeException.java b/components-core/src/main/java/org/dllearner/core/fuzzydll/FuzzyUnsupportedCodeException.java index e73edc6f94..3c21bd7e72 100644 --- a/components-core/src/main/java/org/dllearner/core/fuzzydll/FuzzyUnsupportedCodeException.java +++ b/components-core/src/main/java/org/dllearner/core/fuzzydll/FuzzyUnsupportedCodeException.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core.fuzzydll; /** diff --git a/components-core/src/main/java/org/dllearner/core/options/BooleanConfigOption.java b/components-core/src/main/java/org/dllearner/core/options/BooleanConfigOption.java deleted file mode 100644 index a773ac0b60..0000000000 --- a/components-core/src/main/java/org/dllearner/core/options/BooleanConfigOption.java +++ /dev/null @@ -1,90 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.core.options; - - -/** - * A configuration option representing a boolean value. - * - * @author Jens Lehmann - * - */ -public class BooleanConfigOption extends ConfigOption { - - public BooleanConfigOption(String name, String description) { - super(name, description); - - } - - public BooleanConfigOption(String name, String description, Boolean defaultValue) { - super(name, description, defaultValue); - - } - - public BooleanConfigOption(String name, String description, Boolean defaultValue, boolean mandatory, boolean requiresInit) { - super(name, description, defaultValue, mandatory, requiresInit); - - } - - /* (non-Javadoc) - * @see org.dllearner.core.config.ConfigOption#getValueTypeAsJavaString() - */ - @Override - public String getValueTypeAsJavaString(){ - return "boolean"; - } - - /* - * (non-Javadoc) - * - * @see org.dllearner.core.ConfigOption#checkType(java.lang.Object) - */ - @Override - public boolean checkType(Object object) { - return (object instanceof Boolean); - } - - /* - * (non-Javadoc) - * - * @see org.dllearner.core.ConfigOption#isValidValue(java.lang.Object) - */ - @Override - public boolean isValidValue(Boolean value) { - return true; - } - - /* - * (non-Javadoc) - * - * @see org.dllearner.core.config.ConfigOption#getValueFormatting(java.lang.Object) - */ - @Override - public String getValueFormatting(Boolean value) { - if (value != null) { - if (value) - return "true;"; - else - return "false;"; - } else - return null; - } - -} diff --git a/components-core/src/main/java/org/dllearner/core/options/CommonConfigMappings.java b/components-core/src/main/java/org/dllearner/core/options/CommonConfigMappings.java deleted file mode 100644 index d5d9bb7e9a..0000000000 --- a/components-core/src/main/java/org/dllearner/core/options/CommonConfigMappings.java +++ /dev/null @@ -1,77 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.core.options; - -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; - -import org.dllearner.core.options.fuzzydll.FuzzyExample; -import org.dllearner.core.owl.fuzzydll.FuzzyIndividual; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLObjectProperty; - -import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; - -/** - * @author Jens Lehmann - * - */ -public class CommonConfigMappings { - - private static final OWLDataFactory df = new OWLDataFactoryImpl(); - - - public static SortedSet getIndividualSet(Set individuals) { - SortedSet set = new TreeSet(); - for(String individual : individuals){ - set.add(df.getOWLNamedIndividual(IRI.create(individual))); - } - return set; - } - - public static SortedSet getAtomicConceptSet(Set atomicConcepts) { - SortedSet set = new TreeSet(); - for(String atomicConcept : atomicConcepts) { - set.add(df.getOWLClass(IRI.create(atomicConcept))); - } - return set; - } - - public static SortedSet getAtomicRoleSet(Set atomicRoles) { - SortedSet set = new TreeSet(); - for(String atomicRole : atomicRoles){ - set.add(df.getOWLObjectProperty(IRI.create(atomicRole))); - } - return set; - } - - // added by Josue - public static SortedSet getFuzzyIndividualSet(Set examples) { - SortedSet set = new TreeSet(); - for(FuzzyExample example : examples){ - set.add(new FuzzyIndividual(example.getExampleName(), example.getFuzzyDegree())); - } - return set; - } -} diff --git a/components-core/src/main/java/org/dllearner/core/options/CommonConfigOptions.java b/components-core/src/main/java/org/dllearner/core/options/CommonConfigOptions.java index 26bc076366..7f4b2f7aca 100644 --- a/components-core/src/main/java/org/dllearner/core/options/CommonConfigOptions.java +++ b/components-core/src/main/java/org/dllearner/core/options/CommonConfigOptions.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,16 +16,14 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core.options; -import org.dllearner.core.AbstractCELA; /** * Contains methods for creating common configuration options, i.e. options - * which are or may be of use for several components. + * which are or may be of use for several components. * * @author Jens Lehmann * @@ -35,7 +33,7 @@ public final class CommonConfigOptions { // some default values //public static boolean applyAllFilterDefault = true; - //public static boolean applyExistsFilterDefault = true; + //public static boolean applyExistsFilterDefault = true; //public static boolean useTooWeakListDefault = true; //public static boolean useOverlyGeneralListDefault = true; //public static boolean useShortConceptConstructionDefault = true; @@ -60,153 +58,4 @@ public final class CommonConfigOptions { public static boolean terminateOnNoiseReachedDefault = true; public static boolean instanceBasedDisjointsDefault = true; - public static StringConfigOption getVerbosityOption() { - StringConfigOption verbosityOption = new StringConfigOption("verbosity", "control verbosity of output for this component", "warning"); - String[] allowedValues = new String[] {"quiet", "error", "warning", "notice", "info", "debug"}; - verbosityOption.setAllowedValues(allowedValues); - return verbosityOption; - } - - public static DoubleConfigOption getNoisePercentage() { - DoubleConfigOption noisePercentage = new DoubleConfigOption("noisePercentage", "the (approximated) percentage of noise within the examples",noisePercentageDefault); - noisePercentage.setLowerLimit(0); - noisePercentage.setUpperLimit(100); - return noisePercentage; - } - - public static BooleanConfigOption getTerminateOnNoiseReached() { - return new BooleanConfigOption("terminateOnNoiseReached", "specifies whether to terminate when noise criterion is met", terminateOnNoiseReachedDefault); - } - - public static BooleanConfigOption getTerminateOnNoiseReached(boolean defaultValue) { - return new BooleanConfigOption("terminateOnNoiseReached", "specifies whether to terminate when noise criterion is met", defaultValue); - } - - public static IntegerConfigOption getMaxDepth(int defaultValue) { - return new IntegerConfigOption("maxDepth", "maximum depth of description", defaultValue); - } - - public static DoubleConfigOption getPercentPerLenghtUnitOption(double defaultValue) { - DoubleConfigOption option = new DoubleConfigOption("percentPerLenghtUnit", "describes the reduction in classification accuracy in percent one is willing to accept for reducing the length of the concept by one", defaultValue); - option.setLowerLimit(0.0); - option.setUpperLimit(1.0); - return option; - } - - public static DoubleConfigOption getExpansionPenaltyFactor(double defaultValue) { - DoubleConfigOption option = new DoubleConfigOption("expansionPenaltyFactor", "describes the reduction in heuristic score one is willing to accept for reducing the length of the concept by one", defaultValue); - return option; - } - - public static StringConfigOption getReturnType() { - return new StringConfigOption("returnType", "Specifies the type which the solution has to belong to (if already) known. This means we inform the learning algorithm that the solution is a subclass of this type."); - } - - public static BooleanConfigOption getUNA() { - return new BooleanConfigOption("una", "unique names assumption", false); - } - - public static BooleanConfigOption getOWA() { - return new BooleanConfigOption("owa", "open world assumption (if set to false, we try to close the world", true); - } - - public static StringSetConfigOption allowedConcepts() { - return new StringSetConfigOption("allowedConcepts", "concepts the algorithm is allowed to use"); - } - - public static StringSetConfigOption allowedRoles() { - return new StringSetConfigOption("allowedRoles", "roles the algorithm is allowed to use"); - } - - public static StringSetConfigOption ignoredConcepts() { - return new StringSetConfigOption("ignoredConcepts", "concepts the algorithm must ignore"); - } - - public static StringSetConfigOption ignoredRoles() { - return new StringSetConfigOption("ignoredRoles", "roles the algorithm must ignore"); - } - - public static BooleanConfigOption useAllConstructor() { - return new BooleanConfigOption("useAllConstructor", "specifies whether the universal concept constructor is used in the learning algorithm",useAllConstructorDefault); - } - - public static BooleanConfigOption useExistsConstructor() { - return new BooleanConfigOption("useExistsConstructor", "specifies whether the existential concept constructor is used in the learning algorithm",useExistsConstructorDefault); - } - - public static BooleanConfigOption useHasValueConstructor() { - return new BooleanConfigOption("useHasValueConstructor", "specifies whether the hasValue constructor is used in the learning algorithm",useHasValueConstructorDefault); - } - - public static BooleanConfigOption useDataHasValueConstructor() { - return new BooleanConfigOption("useDataHasValueConstructor", "specifies whether the hasValue constructor is used in the learning algorithm in combination with data properties",useDataHasValueConstructorDefault); - } - - public static IntegerConfigOption valueFreqencyThreshold() { - return new IntegerConfigOption("valueFrequencyThreshold", "specifies how often an object must occur as value in order to be considered for hasValue restrictions",valueFrequencyThresholdDefault); - } - - public static BooleanConfigOption useCardinalityRestrictions() { - return new BooleanConfigOption("useCardinalityRestrictions", "specifies whether CardinalityRestrictions is used in the learning algorithm",useCardinalityRestrictionsDefault); - } - - public static IntegerConfigOption cardinalityLimit() { - return new IntegerConfigOption("cardinalityLimit", "Gives the maximum number used in cardinality restrictions.",cardinalityLimitDefault); - } - - public static BooleanConfigOption useNegation() { - return new BooleanConfigOption("useNegation", "specifies whether negation is used in the learning algorothm",useNegationDefault); - } - - public static BooleanConfigOption useNegation(boolean defaultValue) { - return new BooleanConfigOption("useNegation", "specifies whether negation is used in the learning algorothm",defaultValue); - } - - public static BooleanConfigOption useBooleanDatatypes() { - return new BooleanConfigOption("useBooleanDatatypes", "specifies whether boolean datatypes are used in the learning algorothm",useBooleanDatatypesDefault); - } - - public static BooleanConfigOption useDoubleDatatypes() { - return new BooleanConfigOption("useDoubleDatatypes", "specifies whether double datatypes are used in the learning algorothm",useDoubleDatatypesDefault); - } - - public static BooleanConfigOption useStringDatatypes() { - return new BooleanConfigOption("useStringDatatypes", "specifies whether string datatypes are used in the learning algorothm",useStringDatatypesDefault); - } - - public static IntegerConfigOption maxExecutionTimeInSeconds() { - return new IntegerConfigOption("maxExecutionTimeInSeconds", "algorithm will stop after specified seconds",maxExecutionTimeInSecondsDefault); - } - - public static IntegerConfigOption maxExecutionTimeInSeconds(int defaultValue) { - return new IntegerConfigOption("maxExecutionTimeInSeconds", "algorithm will stop after specified seconds",defaultValue); - } - - public static IntegerConfigOption minExecutionTimeInSeconds() { - return new IntegerConfigOption("minExecutionTimeInSeconds", "algorithm will run at least specified seconds",minExecutionTimeInSecondsDefault); - } - - public static IntegerConfigOption guaranteeXgoodDescriptions() { - return new IntegerConfigOption("guaranteeXgoodDescriptions", "algorithm will run until X good (100%) concept descritpions are found",guaranteeXgoodDescriptionsDefault); - } - - public static IntegerConfigOption maxNrOfResults(int defaultValue) { - IntegerConfigOption opt = new IntegerConfigOption("maxNrOfResults", "Sets the maximum number of results one is interested in. (Setting this to a lower value may increase performance as the learning algorithm has to store/evaluate/beautify less descriptions).", defaultValue); - opt.setLowerLimit(1); - opt.setUpperLimit(AbstractCELA.MAX_NR_OF_RESULTS); - return opt; - } - - public static IntegerConfigOption maxClassDescriptionTests() { - return new IntegerConfigOption("maxClassDescriptionTests", "The maximum number of candidate hypothesis the algorithm is allowed to test (0 = no limit). The algorithm will stop afterwards. " + - "(The real number of tests can be slightly higher, because this criterion usually won't be checked after each single test.)",maxClassDescriptionTestsDefault); - } - - public static StringConfigOption getLogLevel() { - return new StringConfigOption("logLevel", "determines the logLevel for this component, can be {TRACE, DEBUG, INFO}",logLevelDefault); - } - - public static BooleanConfigOption getInstanceBasedDisjoints() { - return new BooleanConfigOption("instanceBasedDisjoints", "Specifies whether to use real disjointness checks or instance based ones (no common instances) in the refinement operator.", instanceBasedDisjointsDefault); - } } diff --git a/components-core/src/main/java/org/dllearner/core/options/ConfigEntry.java b/components-core/src/main/java/org/dllearner/core/options/ConfigEntry.java deleted file mode 100644 index c27ce62b23..0000000000 --- a/components-core/src/main/java/org/dllearner/core/options/ConfigEntry.java +++ /dev/null @@ -1,73 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.core.options; - -/** - * A config entry is a configuration option and a value for the option. - * - * @author Jens Lehmann - * - */ -public class ConfigEntry { - - private ConfigOption option; - private T value; - - public ConfigEntry(ConfigOption option, T value) throws InvalidConfigOptionValueException { - if (!option.isValidValue(value)) { - throw new InvalidConfigOptionValueException(option, value); - } else { - this.option = option; - this.value = value; - } - } - - public ConfigOption getOption() { - return option; - } - - public String getOptionName() { - return option.getName(); - } - - public T getValue() { - return value; - } - - /** - * Get a string to save into a configuration file. - * - * @return a formatted string - */ - public String toConfString(String componentName) { - if (option.getName().equalsIgnoreCase("positiveExamples")) { - return option.getValueFormatting(value); - } else if (option.getName().equalsIgnoreCase("negativeExamples")) { - return option.getValueFormatting(value); - } - return componentName.toString() + "." + option.getName() + " = " - + option.getValueFormatting(value); - } - - @Override - public String toString() { - return option.name + " = " + value; - } -} diff --git a/components-core/src/main/java/org/dllearner/core/options/ConfigOption.java b/components-core/src/main/java/org/dllearner/core/options/ConfigOption.java deleted file mode 100644 index 34cd58aef6..0000000000 --- a/components-core/src/main/java/org/dllearner/core/options/ConfigOption.java +++ /dev/null @@ -1,240 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.core.options; - -import java.util.SortedSet; -import java.util.TreeSet; - -/** - * This class represents a configuration option (without a value for the - * option). - * - * Note: Currently, handling the type of a configuration option is not - * straightforward to implement, because Java Generics information is erased at - * runtime. This will be fixed in Java 7, in particular JSR 308, which is due at - * approx. the beginning of 2009. - * - * @param The type of the config option, e.g. Integer, String etc. - * @author Jens Lehmann - * - */ -public abstract class ConfigOption { - - /** - * Name of this option. - */ - protected String name; - - /** - * A short OWLClassExpression explaining the effect of this option. - */ - protected String description; - - /** - * The default value of this option. - */ - protected T defaultValue; - - /** - * Specifies whether this option is mandatory for a component, - * e.g. if a value other than the default value needs to be given. - */ - protected boolean mandatory = false; - - /** - * Specifies whether a change of the value of the option requires - * running the init method of the component again. For some options - * (e.g. url of an OWL file component) a new run of init is needed, - * while others can be changed without the need to re-init the - * component. - */ - protected boolean requiresInit = false; - - /** - * Calls this(name, description, null, false, true). - * @param name Name of config option. - * @param OWLClassExpression Explanation of option. - */ - public ConfigOption(String name, String description) { - this(name, description, null, false, true); - } - - /** - * Calls this(name, description, defaultValue, false, true). - * @param name Name of config option. - * @param OWLClassExpression Explanation of option. - * @param defaultValue Standard value of option. - */ - public ConfigOption(String name, String description, T defaultValue) { - this(name, description, defaultValue, false, true); - } - - /** - * Constructs a component configuration option. - * @param name Name of config option. - * @param OWLClassExpression Explanation of option. - * @param defaultValue Standard value of option. - * @param mandatory Specifies whether assigning a value to the option is required. - * @param requiresInit Says whether init() has to be called again when the option is changed. - */ - public ConfigOption(String name, String description, T defaultValue, boolean mandatory, boolean requiresInit) { - this.name = name; - this.description = description; - this.defaultValue = defaultValue; - this.mandatory = mandatory; - this.requiresInit = requiresInit; - } - - /** - * - * @return The name of this option. - */ - public String getName() { - return name; - } - - /** - * - * @return The textual OWLClassExpression of this option. - */ - public String getDescription() { - return description; - } - - /** - * @return the defaultValue - */ - public T getDefaultValue() { - return defaultValue; - } - - /** - * @return the defaultValue - */ - public String getDefaultValueInJava() { - return defaultValue+""; - } - - /** - * Specifies whether this option is mandatory for the component. - * (Mandatory means that this component cannot be initialised - * without setting a value for this option.) - * @return True if option is mandatory for component, false otherwise. - */ - public boolean isMandatory() { - return mandatory; - } - - /** - * Specifies whether setting this option requires that the - * component or any components depending on this component - * need to be (re-)initialised. - * @return True if option requires init, false otherwise. - */ - public boolean requiresInit() { - return requiresInit; - } - - /** - * TODO Method should be removed and a mapping table in ConfigJavaGenerator - * created instead. - * gets java imports - * @return Java imports. - */ - public SortedSet getJavaImports() { - return new TreeSet(); - } - - /** - * Checks whether the object has the correct type to be used as a value for - * this option (this method is necessary, because generic information is - * erased at runtime in Java). - * - * @param object - * The object to check. - * @return True of the type is correct, false otherwise. - */ - public abstract boolean checkType(Object object); - - /** - * Checks whether the value is valid, e.g. passing 1985 as - * integer value for an option, which requires values between - * 0 and 1, is not valid. - * @param value A value for the option. - * @return True if the value is valid and false otherwise. - */ - public abstract boolean isValidValue(T value); - - public abstract String getValueTypeAsJavaString(); - - //TODO maybe change the function getClass in the options to get simpleName - public String getAllowedValuesDescription() { - return getValueTypeAsJavaString(); - } - - /** - * @param defaultValue the defaultValue to set - */ - public void setDefaultValue(T defaultValue) { - this.defaultValue = defaultValue; - } - - /** - * @param mandatory the mandatory to set - */ - public void setMandatory(boolean mandatory) { - this.mandatory = mandatory; - } - - /** - * @param requiresInit the requiresInit to set - */ - public void setRequiresInit(boolean requiresInit) { - this.requiresInit = requiresInit; - } - - @Override - public String toString() { - String mand = (isMandatory())?" (mandatory)":""; - String defval = (defaultValue==null)?"not set":defaultValue+""; - return "option name: " + name + "\n" + - "description: " + description + "\n" + - "allowed values: " + getAllowedValuesDescription() + "\n" + - //"values: " + getValueTypeAsJavaString() + "\n" + - "default value: " + defval + mand+ "\n"; - } - - public String getJavaDocString() { - String line = "* @param " + name + " " + description + ".\n"; - line += "* mandatory: "+isMandatory()+"| reinit necessary: "+requiresInit()+"\n"; - line += "* default value: " + defaultValue + "\n"; - return line; - } - - /** - * Get a formatted value to put into configuration file. - * - * @param value Option value. - * - * @return A string to put into a conf file. - */ - public abstract String getValueFormatting(T value); - -} diff --git a/components-core/src/main/java/org/dllearner/core/options/DoubleConfigOption.java b/components-core/src/main/java/org/dllearner/core/options/DoubleConfigOption.java deleted file mode 100644 index a8aa7ee182..0000000000 --- a/components-core/src/main/java/org/dllearner/core/options/DoubleConfigOption.java +++ /dev/null @@ -1,127 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.core.options; - - -/** - * Represents a configuration option with values of type value. Similar to the - * integer option a minimum and a maximum value can specified. - * - * @author Jens Lehmann - * - */ -public class DoubleConfigOption extends ConfigOption { - - private double lowerLimit = Double.MIN_VALUE; - private double upperLimit = Double.MAX_VALUE; - - public DoubleConfigOption(String name, String description, Double defaultValue) { - super(name, description, defaultValue); - - } - - public DoubleConfigOption(String name, String description) { - super(name, description); - - } - - /* (non-Javadoc) - * @see org.dllearner.core.config.ConfigOption#getValueTypeAsJavaString() - */ - @Override - public String getValueTypeAsJavaString(){ - return "double"; - } - - /* - * (non-Javadoc) - * - * @see org.dllearner.core.ConfigOption#isValidValue(java.lang.Object) - */ - @Override - public boolean isValidValue(Double value) { - double tolerance = 0.0001; - return ((value >= lowerLimit-tolerance) && (value <= upperLimit+tolerance)); - } - - /** - * @return the The lowest possible value for this configuration option. - */ - public double getLowerLimit() { - return lowerLimit; - } - - /** - * @param lowerLimit - * The lowest possible value for this configuration option. - */ - public void setLowerLimit(double lowerLimit) { - this.lowerLimit = lowerLimit; - } - - /** - * @return the The highest possible value for this configuration option. - */ - public double getUpperLimit() { - return upperLimit; - } - - /** - * @param upperLimit - * The highest possible value for this configuration option. - */ - public void setUpperLimit(double upperLimit) { - this.upperLimit = upperLimit; - } - - /* - * (non-Javadoc) - * - * @see org.dllearner.core.ConfigOption#checkType(java.lang.Object) - */ - @Override - public boolean checkType(Object object) { - return (object instanceof Double); - } - - @Override - public String getAllowedValuesDescription() { - String str = getValueTypeAsJavaString()+" ";//getClass().toString(); - if (lowerLimit != Double.MIN_VALUE) - str += " min " + lowerLimit; - if (upperLimit != Double.MAX_VALUE) - str += " max " + upperLimit; - return str; - } - - /* - * (non-Javadoc) - * - * @see org.dllearner.core.config.ConfigOption#getValueFormatting(java.lang.Object) - */ - @Override - public String getValueFormatting(Double value) { - if (value != null) - return value.toString() + ";"; - else - return null; - } - -} diff --git a/components-core/src/main/java/org/dllearner/core/options/IntegerConfigOption.java b/components-core/src/main/java/org/dllearner/core/options/IntegerConfigOption.java deleted file mode 100644 index 2bf45aebdb..0000000000 --- a/components-core/src/main/java/org/dllearner/core/options/IntegerConfigOption.java +++ /dev/null @@ -1,135 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.core.options; - - -/** - * A configuration option, which allows values of type integer. A minimum and - * maximum value of the argument can optionally be specified. - * - * @author Jens Lehmann - * - */ -public class IntegerConfigOption extends ConfigOption { - - private int lowerLimit = Integer.MIN_VALUE; - private int upperLimit = Integer.MAX_VALUE; - - - - public IntegerConfigOption(String name, String description, Integer defaultValue, boolean mandatory, boolean requiresInit) { - super(name, description, defaultValue, mandatory, requiresInit); - - } - - public IntegerConfigOption(String name, String description, Integer defaultValue) { - super(name, description, defaultValue); - - } - - public IntegerConfigOption(String name, String description) { - super(name, description); - - } - - /* (non-Javadoc) - * @see org.dllearner.core.config.ConfigOption#getValueTypeAsJavaString() - */ - @Override - public String getValueTypeAsJavaString(){ - return "int"; - } - - /* - * (non-Javadoc) - * - * @see org.dllearner.core.ConfigOption#isValidValue(java.lang.Object) - */ - @Override - public boolean isValidValue(Integer value) { - if (value >= lowerLimit && value <= upperLimit) - return true; - else - return false; - } - - /** - * @return the The lowest possible value for this configuration option. - */ - public int getLowerLimit() { - return lowerLimit; - } - - /** - * @param lowerLimit - * The lowest possible value for this configuration option. - */ - public void setLowerLimit(int lowerLimit) { - this.lowerLimit = lowerLimit; - } - - /** - * @return the The highest possible value for this configuration option. - */ - public int getUpperLimit() { - return upperLimit; - } - - /** - * @param upperLimit - * The highest possible value for this configuration option. - */ - public void setUpperLimit(int upperLimit) { - this.upperLimit = upperLimit; - } - - /* - * (non-Javadoc) - * - * @see org.dllearner.core.ConfigOption#checkType(java.lang.Object) - */ - @Override - public boolean checkType(Object object) { - return (object instanceof Integer); - } - - @Override - public String getAllowedValuesDescription() { - String str = getValueTypeAsJavaString()+" "; - if (lowerLimit != Integer.MIN_VALUE) - str += " min " + lowerLimit; - if (upperLimit != Integer.MAX_VALUE) - str += " max " + upperLimit; - return str; - } - - /* - * (non-Javadoc) - * - * @see org.dllearner.core.config.ConfigOption#getValueFormatting(java.lang.Object) - */ - @Override - public String getValueFormatting(Integer value) { - if (value != null) - return value.toString() + ";"; - else - return null; - } -} diff --git a/components-core/src/main/java/org/dllearner/core/options/StringConfigOption.java b/components-core/src/main/java/org/dllearner/core/options/StringConfigOption.java deleted file mode 100644 index 28ddeefd54..0000000000 --- a/components-core/src/main/java/org/dllearner/core/options/StringConfigOption.java +++ /dev/null @@ -1,128 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.core.options; - -import java.util.Arrays; -import java.util.Set; -import java.util.TreeSet; - -/** - * A configuration option, which allows values of type String. Optionally a set - * of allowed strings can be set. By default all strings are allowed. - * - * @author Jens Lehmann - * - */ -public class StringConfigOption extends ConfigOption { - - private Set allowedValues = new TreeSet(); - - public StringConfigOption(String name, String description) { - super(name, description); - } - - public StringConfigOption(String name, String description, String defaultValue) { - super(name, description, defaultValue); - } - - public StringConfigOption(String name, String description, String defaultValue, boolean mandatory, boolean requiresInit) { - super(name, description, defaultValue, mandatory, requiresInit); - } - - /* (non-Javadoc) - * @see org.dllearner.core.config.ConfigOption#getDefaultValue() - */ - @Override - public String getDefaultValueInJava() { - return (defaultValue == null)?null:"\""+defaultValue+"\""; - } - - /* (non-Javadoc) - * @see org.dllearner.core.config.ConfigOption#getValueTypeAsJavaString() - */ - @Override - public String getValueTypeAsJavaString(){ - return "String"; - } - - /* - * (non-Javadoc) - * - * @see org.dllearner.core.ConfigOption#isValidValue(java.lang.Object) - */ - @Override - public boolean isValidValue(String value) { - if (allowedValues.size() == 0 || allowedValues.contains(value)) - return true; - else - return false; - } - - /** - * @return the allowedValues - */ - public Set getAllowedValues() { - return allowedValues; - } - - /* (non-Javadoc) - * @see org.dllearner.core.options.ConfigOption#getAllowedValuesDescription() - */ - @Override - public String getAllowedValuesDescription() { - return getValueTypeAsJavaString() + " "+allowedValues+" "; - } - - /** - * @param allowedValues - * the allowedValues to set - */ - public void setAllowedValues(Set allowedValues) { - this.allowedValues = allowedValues; - } - - public void setAllowedValues(String[] allowedValues) { - this.allowedValues = new TreeSet(Arrays.asList(allowedValues)); - } - - /* - * (non-Javadoc) - * - * @see org.dllearner.core.ConfigOption#checkType(java.lang.Object) - */ - @Override - public boolean checkType(Object object) { - return (object instanceof String); - } - - /* - * (non-Javadoc) - * - * @see org.dllearner.core.config.ConfigOption#getValueFormatting(java.lang.Object) - */ - @Override - public String getValueFormatting(String value) { - if (value != null) - return "\"" + value.toString() + "\";"; - else - return null; - } - -} diff --git a/components-core/src/main/java/org/dllearner/core/options/StringSetConfigOption.java b/components-core/src/main/java/org/dllearner/core/options/StringSetConfigOption.java deleted file mode 100644 index 5d1d40c370..0000000000 --- a/components-core/src/main/java/org/dllearner/core/options/StringSetConfigOption.java +++ /dev/null @@ -1,137 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.core.options; - -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; - -/** - * A set of strings. - * - * @author Jens Lehmann - * - */ -public class StringSetConfigOption extends ConfigOption> { - - public StringSetConfigOption(String name, String description) { - super(name, description); - - } - - public StringSetConfigOption(String name, String description, Set defaultValue) { - super(name, description, defaultValue); - - } - - public StringSetConfigOption(String name, String description, Set defaultValue, boolean mandatory, boolean requiresInit) { - super(name, description, defaultValue, mandatory, requiresInit); - } - - /* (non-Javadoc) - * @see org.dllearner.core.config.ConfigOption#getValueTypeAsJavaString() - */ - @Override - public String getValueTypeAsJavaString(){ - return "Set"; - } - - /* (non-Javadoc) - * @see org.dllearner.core.config.ConfigOption#getJavaImports() - */ - @Override - public SortedSet getJavaImports() { - SortedSet ret = new TreeSet(); - ret.add("java.util.Set"); - return ret; - - } - - /* - * (non-Javadoc) - * - * @see org.dllearner.core.ConfigOption#isValidValue(java.lang.Object) - */ - @Override - public boolean isValidValue(Set value) { - return true; - } - - /* - * (non-Javadoc) - * - * @see org.dllearner.core.ConfigOption#checkType(java.lang.Object) - */ - @Override - public boolean checkType(Object object) { - if (!(object instanceof Set)) - return false; - - Set set = (Set) object; - for (Object element : set) { - if (!(element instanceof String)) - return false; - } - - return true; - } - - /* - * (non-Javadoc) - * - * @see org.dllearner.core.config.ConfigOption#getValueFormatting(java.lang.Object) - */ - @Override - public String getValueFormatting(Set value) { - String back = ""; - if (value != null && !name.equals("positiveExamples") && !name.equals("negativeExamples")) { - Integer count = 0; - back = "{"; - for (String i : value) { - if (count > 0) - back += ","; - back += "\n\"" + i + "\""; - count++; - } - back += "};"; - return back; - } - // positive examples - if (value != null && name.equals("positiveExamples")) { - for (String i : value) { - back += "\n+\"" + i + "\""; - } - return back + "\n"; - } - // negative examples - if (value != null && name.equals("negativeExamples")) { - int count = 0; - for (String i : value) { - count++; - if (count == 1) - back += "-\"" + i + "\""; - else - back += "\n-\"" + i + "\""; - } - return back + "\n"; - } - return null; - } -} diff --git a/components-core/src/main/java/org/dllearner/core/options/StringTupleListConfigOption.java b/components-core/src/main/java/org/dllearner/core/options/StringTupleListConfigOption.java deleted file mode 100644 index 71d2235251..0000000000 --- a/components-core/src/main/java/org/dllearner/core/options/StringTupleListConfigOption.java +++ /dev/null @@ -1,122 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.core.options; - -import java.util.List; -import java.util.SortedSet; -import java.util.TreeSet; - -import org.dllearner.utilities.datastructures.StringTuple; - -/** - * A list if string tuples, for instance for specifying several parameters or - * replacement rules. - * - * @author Jens Lehmann - */ -public class StringTupleListConfigOption extends ConfigOption> { - - - public StringTupleListConfigOption(String name, String description, List defaultValue, boolean mandatory, boolean requiresInit) { - super(name, description, defaultValue, mandatory, requiresInit); - - } - - public StringTupleListConfigOption(String name, String description, List defaultValue) { - super(name, description, defaultValue); - - } - - public StringTupleListConfigOption(String name, String description) { - super(name, description); - - } - - /* (non-Javadoc) - * @see org.dllearner.core.config.ConfigOption#getValueTypeAsJavaString() - */ - @Override - public String getValueTypeAsJavaString(){ - return "List"; - } - - /* (non-Javadoc) - * @see org.dllearner.core.config.ConfigOption#getJavaImports() - */ - @Override - public SortedSet getJavaImports() { - SortedSet ret = new TreeSet(); - ret.add("java.util.List"); - ret.add("org.dllearner.utilities.datastructures.StringTuple"); - return ret; - } - - /* - * (non-Javadoc) - * - * @see org.dllearner.core.config.ConfigOption#checkType(java.lang.Object) - */ - @Override - public boolean checkType(Object object) { - if (!(object instanceof List)) - return false; - - List set = (List) object; - for (Object element : set) { - if (!(element instanceof StringTuple)) - return false; - } - - return true; - } - - /* - * (non-Javadoc) - * - * @see org.dllearner.core.config.ConfigOption#isValidValue(java.lang.Object) - */ - @Override - public boolean isValidValue(List value) { - return true; - } - - /* - * (non-Javadoc) - * - * @see org.dllearner.core.config.ConfigOption#getValueFormatting(java.lang.Object) - */ - @Override - public String getValueFormatting(List value) { - Integer count = 0; - if (value != null) { - String back = "["; - for (StringTuple i : value) { - if (count > 0) - back += ","; - back += "\n(\"" + i.a + "\",\"" + i.b + "\")"; - count++; - } - back += "];"; - return back; - } else - return null; - } - -} diff --git a/components-core/src/main/java/org/dllearner/core/options/URLConfigOption.java b/components-core/src/main/java/org/dllearner/core/options/URLConfigOption.java deleted file mode 100644 index 12f10826d6..0000000000 --- a/components-core/src/main/java/org/dllearner/core/options/URLConfigOption.java +++ /dev/null @@ -1,130 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.core.options; - -import java.net.URL; -import java.util.SortedSet; -import java.util.TreeSet; - -/** - * Option which has an URL as value. - * - * @author Jens Lehmann - * - */ -public class URLConfigOption extends ConfigOption { - - private boolean refersToFile = false; - - private boolean refersToOWLClass = false; - - public URLConfigOption(String name, String description) { - super(name, description); - } - - public URLConfigOption(String name, String description, URL defaultValue) { - super(name, description, defaultValue); - } - - public URLConfigOption(String name, String description, URL defaultValue, boolean mandatory, - boolean requiresInit) { - super(name, description, defaultValue, mandatory, requiresInit); - } - - /** - * Returns whether the URI can refer to a file or not, e.g. the - * URL of an OWL knowledge source does refer to a file whereas - * the URL of a SPARQL endpoint cannot refer to a file. The distinction - * can be useful in GUIs (e.g. they may offer to choose a local file). - * @return the refersToFile - */ - public boolean refersToFile() { - return refersToFile; - } - - /** - * @param refersToFile Set whether this option can refer to a file. - */ - public void setRefersToFile(boolean refersToFile) { - this.refersToFile = refersToFile; - } - - /** - * Returns whether the URI can refer to a class in a loaded - * ontology. This can be used in user interfaces for this component. - * @return the refersToOWLClass Whether it refers to an OWL class. - */ - public boolean refersToOWLClass() { - return refersToOWLClass; - } - - /** - * @param refersToFile Set whether this option refers to an OWL class in the - * loaded ontology. - */ - public void setRefersToOWLClass(boolean refersToOWLClass) { - this.refersToOWLClass = refersToOWLClass; - } - - /* (non-Javadoc) - * @see org.dllearner.core.config.ConfigOption#checkType(java.lang.Object) - */ - @Override - public boolean checkType(Object object) { - return (object instanceof URL); - } - - /* (non-Javadoc) - * @see org.dllearner.core.config.ConfigOption#getValueFormatting(java.lang.Object, java.lang.Integer) - */ - @Override - public String getValueFormatting(URL value) { - return "\"" + value.toString() + "\";"; - } - - /* (non-Javadoc) - * @see org.dllearner.core.config.ConfigOption#getValueTypeAsJavaString() - */ - @Override - public String getValueTypeAsJavaString() { - return "URL"; - } - - /* (non-Javadoc) - * @see org.dllearner.core.config.ConfigOption#isValidValue(java.lang.Object) - */ - @Override - public boolean isValidValue(URL value) { - return true; - } - - /* (non-Javadoc) - * @see org.dllearner.core.config.ConfigOption#getJavaImports() - */ - @Override - public SortedSet getJavaImports() { - SortedSet ret = new TreeSet(); - ret.add("java.net.URL"); - return ret; - } - - - -} diff --git a/components-core/src/main/java/org/dllearner/core/options/UnknownConfigOptionException.java b/components-core/src/main/java/org/dllearner/core/options/UnknownConfigOptionException.java deleted file mode 100644 index ee89a481ba..0000000000 --- a/components-core/src/main/java/org/dllearner/core/options/UnknownConfigOptionException.java +++ /dev/null @@ -1,41 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.core.options; - -import org.dllearner.core.AbstractComponent; -import org.dllearner.core.AnnComponentManager; - -/** - * @author Jens Lehmann - * - */ -public class UnknownConfigOptionException extends Exception { - - private static final long serialVersionUID = -7808637210577591687L; - - public UnknownConfigOptionException(Class componentClass, String optionName) { - super("Option " + optionName + " unknown in component " + AnnComponentManager.getName(componentClass) + "(" + componentClass.getName() + ")"); - } - - public UnknownConfigOptionException(Class componentClass, ConfigOption option) { - super("Option " + option.getName() + " unknown in component " + AnnComponentManager.getName(componentClass) + "(" + componentClass.getName() + ")"); - } - -} diff --git a/components-core/src/main/java/org/dllearner/core/options/fuzzydll/FuzzyExample.java b/components-core/src/main/java/org/dllearner/core/options/fuzzydll/FuzzyExample.java deleted file mode 100644 index a2e7e934d9..0000000000 --- a/components-core/src/main/java/org/dllearner/core/options/fuzzydll/FuzzyExample.java +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.core.options.fuzzydll; - -public class FuzzyExample implements Comparable { - private String exampleName; - private double fuzzyDegree; - - public FuzzyExample(String i, double d) { - this.exampleName = i; - this.fuzzyDegree = d; - } - - @Override - public int compareTo(FuzzyExample fe) { - return this.getExampleName().compareTo(fe.getExampleName()); - } - - public String getExampleName() { - return exampleName; - } - - public void setExampleName(String individual) { - this.exampleName = individual; - } - - public double getFuzzyDegree() { - return fuzzyDegree; - } - - public void setFuzzyDegree(double fuzzyDegree) { - this.fuzzyDegree = fuzzyDegree; - } -} diff --git a/components-core/src/main/java/org/dllearner/core/options/fuzzydll/ObjectSetConfigOption.java b/components-core/src/main/java/org/dllearner/core/options/fuzzydll/ObjectSetConfigOption.java deleted file mode 100644 index e9b965e992..0000000000 --- a/components-core/src/main/java/org/dllearner/core/options/fuzzydll/ObjectSetConfigOption.java +++ /dev/null @@ -1,142 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.core.options.fuzzydll; - -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; - -import org.dllearner.core.options.ConfigOption; - -/** - * A set of strings. - * - * @author Josue Iglesias - * - */ -public class ObjectSetConfigOption extends ConfigOption> { - - public ObjectSetConfigOption(String name, String description) { - super(name, description); - - } - - public ObjectSetConfigOption(String name, String description, Set defaultValue) { - super(name, description, defaultValue); - - } - - public ObjectSetConfigOption(String name, String description, Set defaultValue, boolean mandatory, boolean requiresInit) { - super(name, description, defaultValue, mandatory, requiresInit); - } - - /* (non-Javadoc) - * @see org.dllearner.core.config.ConfigOption#getValueTypeAsJavaString() - */ - @Override - public String getValueTypeAsJavaString(){ - return "Set"; - } - - /* (non-Javadoc) - * @see org.dllearner.core.config.ConfigOption#getJavaImports() - */ - @Override - public SortedSet getJavaImports() { - SortedSet ret = new TreeSet(); - ret.add("java.util.Set"); - return ret; - - } - - /* - * (non-Javadoc) - * - * @see org.dllearner.core.ConfigOption#isValidValue(java.lang.Object) - */ - @Override - public boolean isValidValue(Set value) { - return true; - } - - /* - * (non-Javadoc) - * - * @see org.dllearner.core.ConfigOption#checkType(java.lang.Object) - */ - @Override - public boolean checkType(Object object) { - if (!(object instanceof Set)) - return false; - - Set set = (Set) object; - for (Object element : set) { - if (!(element instanceof Object)) - return false; - } - - return true; - } - - /* - * (non-Javadoc) - * - * @see org.dllearner.core.config.ConfigOption#getValueFormatting(java.lang.Object) - */ - @Override - // - // TODO this method has been copied from StringSetConfigOption class; it should be addapted to FuzzyExample - // - public String getValueFormatting(Set value) { - String back = ""; - if (value != null && !name.equals("positiveExamples") && !name.equals("negativeExamples")) { - Integer count = 0; - back = "{"; - for (Object i : value) { - if (count > 0) - back += ","; - back += "\n\"" + i + "\""; - count++; - } - back += "};"; - return back; - } - // positive examples - if (value != null && name.equals("positiveExamples")) { - for (Object i : value) { - back += "\n+\"" + i + "\""; - } - return back + "\n"; - } - // negative examples - if (value != null && name.equals("negativeExamples")) { - int count = 0; - for (Object i : value) { - count++; - if (count == 1) - back += "-\"" + i + "\""; - else - back += "\n-\"" + i + "\""; - } - return back + "\n"; - } - return null; - } -} diff --git a/components-core/src/main/java/org/dllearner/core/options/package.html b/components-core/src/main/java/org/dllearner/core/options/package.html deleted file mode 100644 index c245527f17..0000000000 --- a/components-core/src/main/java/org/dllearner/core/options/package.html +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - -

Classes for managing the configuration options of DL-Learner -components.

- - \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/core/owl/AbstractHierarchy.java b/components-core/src/main/java/org/dllearner/core/owl/AbstractHierarchy.java index 396c73d1fd..770e72ea80 100644 --- a/components-core/src/main/java/org/dllearner/core/owl/AbstractHierarchy.java +++ b/components-core/src/main/java/org/dllearner/core/owl/AbstractHierarchy.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.core.owl; @@ -27,8 +42,8 @@ public abstract class AbstractHierarchy implements Hierarch private SortedMap> hierarchyUp; private SortedMap> hierarchyDown; - private SortedSet rootEntities = new TreeSet(); - private SortedSet leafEntities = new TreeSet(); + private SortedSet rootEntities = new TreeSet<>(); + private SortedSet leafEntities = new TreeSet<>(); public AbstractHierarchy(SortedMap> hierarchyUp, SortedMap> hierarchyDown) { @@ -65,18 +80,18 @@ public SortedSet getChildren(T entity, boolean direct) { if(result == null) { logger.debug("Query for " + entity + " in hierarchy, but the entity is not contained in the (downward) hierarchy, e.g. because the entity does not exist or is ignored. Returning empty result instead."); - return new TreeSet(); + return new TreeSet<>(); } result.remove(entity); if(!direct) { // get transitive children - SortedSet tmp = new TreeSet(); + SortedSet tmp = new TreeSet<>(); for(T child : result){ tmp.addAll(getChildren(child, direct)); } result.addAll(tmp); } - return new TreeSet(result); + return new TreeSet<>(result); } /* (non-Javadoc) @@ -96,19 +111,19 @@ public SortedSet getParents(T entity, boolean direct) { if(result == null) { logger.debug("Query for " + entity + " in hierarchy, but the entity is not contained in the (upward) hierarchy, e.g. because the entity does not exist or is ignored. Returning empty result instead."); - return new TreeSet(); + return new TreeSet<>(); } result.remove(entity); if(!direct) { - SortedSet tmp = new TreeSet(); + SortedSet tmp = new TreeSet<>(); for(T parent : result){ tmp.addAll(getParents(parent, direct)); } result.addAll(tmp); } - return new TreeSet(result); + return new TreeSet<>(result); } /* (non-Javadoc) @@ -116,7 +131,7 @@ public SortedSet getParents(T entity, boolean direct) { */ @Override public SortedSet getSiblings(T entity) { - SortedSet siblings = new TreeSet(); + SortedSet siblings = new TreeSet<>(); Set parents = getParents(entity); for(T parent : parents) { @@ -164,7 +179,7 @@ public boolean isParentOf(T entity1, T entity2) { */ @Override public SortedSet getRoots() { - SortedSet roots = new TreeSet(); + SortedSet roots = new TreeSet<>(); for(T child : getChildren(getTopConcept())){ SortedSet parents = getParents(child); @@ -205,10 +220,10 @@ public boolean contains(T entity) { * refinement respectively. */ public void thinOutSubsumptionHierarchy() { - SortedMap> hierarchyDownNew = new TreeMap>(); - SortedMap> hierarchyUpNew = new TreeMap>(); + SortedMap> hierarchyDownNew = new TreeMap<>(); + SortedMap> hierarchyUpNew = new TreeMap<>(); - Set conceptsInSubsumptionHierarchy = new TreeSet(); + Set conceptsInSubsumptionHierarchy = new TreeSet<>(); conceptsInSubsumptionHierarchy.addAll(hierarchyUp.keySet()); conceptsInSubsumptionHierarchy.addAll(hierarchyDown.keySet()); @@ -262,14 +277,14 @@ public void thinOutSubsumptionHierarchy() { public AbstractHierarchy cloneAndRestrict(Set allowedEntities) { // currently TOP and BOTTOM are always allowed // (TODO would be easier if Thing/Nothing were declared as named classes) - Set allowed = new TreeSet(); + Set allowed = new TreeSet<>(); allowed.addAll(allowedEntities); allowed.add(getTopConcept()); allowed.add(getBottomConcept()); // create new maps - SortedMap> subsumptionHierarchyUpNew = new TreeMap>(); - SortedMap> subsumptionHierarchyDownNew = new TreeMap>(); + SortedMap> subsumptionHierarchyUpNew = new TreeMap<>(); + SortedMap> subsumptionHierarchyDownNew = new TreeMap<>(); for(Entry> entry : hierarchyUp.entrySet()) { T key = entry.getKey(); @@ -277,9 +292,9 @@ public AbstractHierarchy cloneAndRestrict(Set allowedEntities) { if(allowed.contains(key)) { // copy the set of all parents (we consume them until // they are empty) - TreeSet parents = new TreeSet(entry.getValue()); + TreeSet parents = new TreeSet<>(entry.getValue()); // storage for new parents - TreeSet newParents = new TreeSet(); + TreeSet newParents = new TreeSet<>(); while(!parents.isEmpty()) { // pick and remove the first element @@ -304,15 +319,18 @@ public AbstractHierarchy cloneAndRestrict(Set allowedEntities) { for(Entry> entry : hierarchyDown.entrySet()) { T key = entry.getKey(); if(allowed.contains(key)) { - TreeSet children = new TreeSet(entry.getValue()); - TreeSet newChildren = new TreeSet(); + TreeSet children = new TreeSet<>(entry.getValue()); + TreeSet newChildren = new TreeSet<>(); while(!children.isEmpty()) { T d = children.pollFirst(); if(allowed.contains(d)) { newChildren.add(d); } else { - children.addAll(hierarchyDown.get(d)); + SortedSet tmp = hierarchyDown.get(d); + if(tmp != null) { + children.addAll(tmp); + } } } diff --git a/components-core/src/main/java/org/dllearner/core/owl/ClassHierarchy.java b/components-core/src/main/java/org/dllearner/core/owl/ClassHierarchy.java index a237674de6..29e9c52de5 100644 --- a/components-core/src/main/java/org/dllearner/core/owl/ClassHierarchy.java +++ b/components-core/src/main/java/org/dllearner/core/owl/ClassHierarchy.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core.owl; import java.util.HashSet; @@ -51,7 +50,7 @@ public class ClassHierarchy extends AbstractHierarchy { private static final OWLClass OWL_NOTHING = new OWLClassImpl( OWLRDFVocabulary.OWL_NOTHING.getIRI()); - private OWLDataFactory df = new OWLDataFactoryImpl(false, false); + private OWLDataFactory df = new OWLDataFactoryImpl(); /** * The arguments specify the superclasses and subclasses of each class. This @@ -66,33 +65,45 @@ public ClassHierarchy( } /** - * Returns the all super classes. - * @param concept - * @return + * Returns the all superclasses for the given class. + * @param concept the class + * @return all superclasses */ public SortedSet getSuperClasses(OWLClassExpression concept) { return getSuperClasses(concept, false); } - + + /** + * Returns the all superclasses for the given class. + * @param concept the class + * @param direct whether to return only direct superclasses or not + * @return all superclasses + */ public SortedSet getSuperClasses(OWLClassExpression concept, boolean direct) { if(concept.isOWLThing()) { - return new TreeSet(); + return new TreeSet<>(); } return getParents(concept, direct); } /** * Returns the all subclasses. - * @param concept - * @return + * @param concept the class + * @return all subclasses */ public SortedSet getSubClasses(OWLClassExpression concept) { return getSubClasses(concept, false); } - + + /** + * Returns the all subclasses. + * @param concept the class + * @param direct whether to return only direct subclasses or not + * @return all subclasses + */ public SortedSet getSubClasses(OWLClassExpression concept, boolean direct) { if(concept.isOWLNothing()) { - return new TreeSet(); + return new TreeSet<>(); } return getChildren(concept, direct); } @@ -119,7 +130,7 @@ public Set toOWLAxioms(){ } public Set toOWLAxioms(OWLClassExpression concept){ - Set axioms = new HashSet(); + Set axioms = new HashSet<>(); Set subConcepts = getChildren(concept); if (subConcepts != null) { for (OWLClassExpression sub : subConcepts){ diff --git a/components-core/src/main/java/org/dllearner/core/owl/ContextDescription.java b/components-core/src/main/java/org/dllearner/core/owl/ContextDescription.java index 789e06a02d..ce75ccaa26 100644 --- a/components-core/src/main/java/org/dllearner/core/owl/ContextDescription.java +++ b/components-core/src/main/java/org/dllearner/core/owl/ContextDescription.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core.owl; import org.semanticweb.owlapi.model.OWLClassExpression; diff --git a/components-core/src/main/java/org/dllearner/core/owl/DatatypePropertyHierarchy.java b/components-core/src/main/java/org/dllearner/core/owl/DatatypePropertyHierarchy.java index 815b80744a..6850534f07 100644 --- a/components-core/src/main/java/org/dllearner/core/owl/DatatypePropertyHierarchy.java +++ b/components-core/src/main/java/org/dllearner/core/owl/DatatypePropertyHierarchy.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core.owl; import java.util.SortedMap; @@ -51,11 +50,11 @@ public DatatypePropertyHierarchy( } public SortedSet getMoreGeneralRoles(OWLDataProperty role) { - return new TreeSet(getParents(role)); + return new TreeSet<>(getParents(role)); } public SortedSet getMoreSpecialRoles(OWLDataProperty role) { - return new TreeSet(getChildren(role)); + return new TreeSet<>(getChildren(role)); } public boolean isSubpropertyOf(OWLDataProperty subProperty, OWLDataProperty superProperty) { diff --git a/components-core/src/main/java/org/dllearner/core/owl/FlatABox.java b/components-core/src/main/java/org/dllearner/core/owl/FlatABox.java index 2d0b335df1..4faae6c1bc 100755 --- a/components-core/src/main/java/org/dllearner/core/owl/FlatABox.java +++ b/components-core/src/main/java/org/dllearner/core/owl/FlatABox.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core.owl; import java.util.HashMap; @@ -32,19 +31,19 @@ */ public class FlatABox { - public SortedSet roles = new TreeSet(); - public SortedSet concepts = new TreeSet(); - public SortedSet domain = new TreeSet(); - public SortedSet top = new TreeSet(); - public SortedSet bottom = new TreeSet(); + public SortedSet roles = new TreeSet<>(); + public SortedSet concepts = new TreeSet<>(); + public SortedSet domain = new TreeSet<>(); + public SortedSet top = new TreeSet<>(); + public SortedSet bottom = new TreeSet<>(); - public Map> atomicConceptsPos = new HashMap>(); - public Map> atomicConceptsNeg = new HashMap>(); - public Map>> rolesPos = new HashMap>>(); - public Map>> rolesNeg = new HashMap>>(); + public Map> atomicConceptsPos = new HashMap<>(); + public Map> atomicConceptsNeg = new HashMap<>(); + public Map>> rolesPos = new HashMap<>(); + public Map>> rolesNeg = new HashMap<>(); - public Map> exampleConceptsPos = new HashMap>(); - public Map> exampleConceptsNeg = new HashMap>(); + public Map> exampleConceptsPos = new HashMap<>(); + public Map> exampleConceptsNeg = new HashMap<>(); public FlatABox() { diff --git a/components-core/src/main/java/org/dllearner/core/owl/Hierarchy.java b/components-core/src/main/java/org/dllearner/core/owl/Hierarchy.java index 42b1732d1b..945245f69e 100644 --- a/components-core/src/main/java/org/dllearner/core/owl/Hierarchy.java +++ b/components-core/src/main/java/org/dllearner/core/owl/Hierarchy.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.core.owl; @@ -31,8 +46,8 @@ public interface Hierarchy { /** * Checks whether the entity is contained in the hierarchy. - * @param entity - * @return + * @param entity the entity + * @return whether the entity is contained in the hierarchy or not */ boolean contains(T entity); diff --git a/components-core/src/main/java/org/dllearner/core/owl/LazyClassHierarchy.java b/components-core/src/main/java/org/dllearner/core/owl/LazyClassHierarchy.java index 4eb097b557..392d59a2a4 100644 --- a/components-core/src/main/java/org/dllearner/core/owl/LazyClassHierarchy.java +++ b/components-core/src/main/java/org/dllearner/core/owl/LazyClassHierarchy.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core.owl; import java.util.Set; diff --git a/components-core/src/main/java/org/dllearner/core/owl/NNF.java b/components-core/src/main/java/org/dllearner/core/owl/NNF.java new file mode 100644 index 0000000000..92e614d00e --- /dev/null +++ b/components-core/src/main/java/org/dllearner/core/owl/NNF.java @@ -0,0 +1,680 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.core.owl; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom; +import org.semanticweb.owlapi.model.OWLAnnotationPropertyDomainAxiom; +import org.semanticweb.owlapi.model.OWLAnnotationPropertyRangeAxiom; +import org.semanticweb.owlapi.model.OWLAsymmetricObjectPropertyAxiom; +import org.semanticweb.owlapi.model.OWLAxiom; +import org.semanticweb.owlapi.model.OWLAxiomVisitorEx; +import org.semanticweb.owlapi.model.OWLClass; +import org.semanticweb.owlapi.model.OWLClassAssertionAxiom; +import org.semanticweb.owlapi.model.OWLClassExpression; +import org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx; +import org.semanticweb.owlapi.model.OWLDataAllValuesFrom; +import org.semanticweb.owlapi.model.OWLDataComplementOf; +import org.semanticweb.owlapi.model.OWLDataExactCardinality; +import org.semanticweb.owlapi.model.OWLDataFactory; +import org.semanticweb.owlapi.model.OWLDataHasValue; +import org.semanticweb.owlapi.model.OWLDataIntersectionOf; +import org.semanticweb.owlapi.model.OWLDataMaxCardinality; +import org.semanticweb.owlapi.model.OWLDataMinCardinality; +import org.semanticweb.owlapi.model.OWLDataOneOf; +import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom; +import org.semanticweb.owlapi.model.OWLDataPropertyDomainAxiom; +import org.semanticweb.owlapi.model.OWLDataPropertyRangeAxiom; +import org.semanticweb.owlapi.model.OWLDataRange; +import org.semanticweb.owlapi.model.OWLDataSomeValuesFrom; +import org.semanticweb.owlapi.model.OWLDataUnionOf; +import org.semanticweb.owlapi.model.OWLDataVisitorEx; +import org.semanticweb.owlapi.model.OWLDatatype; +import org.semanticweb.owlapi.model.OWLDatatypeDefinitionAxiom; +import org.semanticweb.owlapi.model.OWLDatatypeRestriction; +import org.semanticweb.owlapi.model.OWLDeclarationAxiom; +import org.semanticweb.owlapi.model.OWLDifferentIndividualsAxiom; +import org.semanticweb.owlapi.model.OWLDisjointClassesAxiom; +import org.semanticweb.owlapi.model.OWLDisjointDataPropertiesAxiom; +import org.semanticweb.owlapi.model.OWLDisjointObjectPropertiesAxiom; +import org.semanticweb.owlapi.model.OWLDisjointUnionAxiom; +import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom; +import org.semanticweb.owlapi.model.OWLEquivalentDataPropertiesAxiom; +import org.semanticweb.owlapi.model.OWLEquivalentObjectPropertiesAxiom; +import org.semanticweb.owlapi.model.OWLFacetRestriction; +import org.semanticweb.owlapi.model.OWLFunctionalDataPropertyAxiom; +import org.semanticweb.owlapi.model.OWLFunctionalObjectPropertyAxiom; +import org.semanticweb.owlapi.model.OWLHasKeyAxiom; +import org.semanticweb.owlapi.model.OWLInverseFunctionalObjectPropertyAxiom; +import org.semanticweb.owlapi.model.OWLInverseObjectPropertiesAxiom; +import org.semanticweb.owlapi.model.OWLIrreflexiveObjectPropertyAxiom; +import org.semanticweb.owlapi.model.OWLLiteral; +import org.semanticweb.owlapi.model.OWLNegativeDataPropertyAssertionAxiom; +import org.semanticweb.owlapi.model.OWLNegativeObjectPropertyAssertionAxiom; +import org.semanticweb.owlapi.model.OWLObjectAllValuesFrom; +import org.semanticweb.owlapi.model.OWLObjectComplementOf; +import org.semanticweb.owlapi.model.OWLObjectExactCardinality; +import org.semanticweb.owlapi.model.OWLObjectHasSelf; +import org.semanticweb.owlapi.model.OWLObjectHasValue; +import org.semanticweb.owlapi.model.OWLObjectIntersectionOf; +import org.semanticweb.owlapi.model.OWLObjectMaxCardinality; +import org.semanticweb.owlapi.model.OWLObjectMinCardinality; +import org.semanticweb.owlapi.model.OWLObjectOneOf; +import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom; +import org.semanticweb.owlapi.model.OWLObjectPropertyDomainAxiom; +import org.semanticweb.owlapi.model.OWLObjectPropertyRangeAxiom; +import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom; +import org.semanticweb.owlapi.model.OWLObjectUnionOf; +import org.semanticweb.owlapi.model.OWLReflexiveObjectPropertyAxiom; +import org.semanticweb.owlapi.model.OWLSameIndividualAxiom; +import org.semanticweb.owlapi.model.OWLSubAnnotationPropertyOfAxiom; +import org.semanticweb.owlapi.model.OWLSubClassOfAxiom; +import org.semanticweb.owlapi.model.OWLSubDataPropertyOfAxiom; +import org.semanticweb.owlapi.model.OWLSubObjectPropertyOfAxiom; +import org.semanticweb.owlapi.model.OWLSubPropertyChainOfAxiom; +import org.semanticweb.owlapi.model.OWLSymmetricObjectPropertyAxiom; +import org.semanticweb.owlapi.model.OWLTransitiveObjectPropertyAxiom; +import org.semanticweb.owlapi.model.SWRLRule; + +/** + * + * A modified version of the NNF computation contained in the OWL API. The + * main difference is in the usage of DL-Learner version of intersection and + * union which allow for redundant usage of sub expression, e.g. (A and A) + * + * @author Lorenz Buehmann + * @author Matthew Horridge, The University Of Manchester, Information Management + * Group, Date: 06-Jun-2008 + */ +public class NNF implements OWLClassExpressionVisitorEx, + OWLDataVisitorEx, OWLAxiomVisitorEx { + + private boolean negated; + private final OWLDataFactory dataFactory; + + /** + * @param dataFactory + * datafactory to use + */ + public NNF(OWLDataFactory dataFactory) { + this.dataFactory = dataFactory; + } + + /** reset the negation. */ + public void reset() { + negated = false; + } + + private OWLClassExpression getNegation(OWLClassExpression classExpression) { + return dataFactory.getOWLObjectComplementOf(classExpression); + } + + @Override + public OWLClassExpression visit(OWLClass desc) { + if (negated) { + if (desc.isOWLNothing()) { + return dataFactory.getOWLThing(); + } else if (desc.isOWLThing()) { + return dataFactory.getOWLNothing(); + } else { + return getNegation(desc); + } + } else { + return desc; + } + } + + @Override + public OWLClassExpression visit(OWLObjectIntersectionOf desc) { + List ops = new ArrayList<>(); + for (OWLClassExpression op : desc.getOperandsAsList()) { + ops.add(op.accept(this)); + } + if (negated) { + return new OWLObjectUnionOfImplExt(ops); + } else { + return new OWLObjectIntersectionOfImplExt(ops); + } + } + + @Override + public OWLClassExpression visit(OWLObjectUnionOf desc) { + List ops = new ArrayList<>(); + for (OWLClassExpression op : desc.getOperandsAsList()) { + ops.add(op.accept(this)); + } + if (negated) { + // Flip to an intersection + return new OWLObjectIntersectionOfImplExt(ops); + } else { + return new OWLObjectUnionOfImplExt(ops); + } + } + + @Override + public OWLClassExpression visit(OWLObjectComplementOf desc) { + if (negated) { + // Cancels out. + // Save and then restore. + boolean neg = negated; + negated = false; + OWLClassExpression negDesc = desc.getOperand().accept(this); + negated = neg; + return negDesc; + } else { + // Save and then restore + boolean neg = negated; + negated = true; + OWLClassExpression negDesc = desc.getOperand().accept(this); + negated = neg; + return negDesc; + } + } + + @Override + public OWLClassExpression visit(OWLObjectSomeValuesFrom desc) { + OWLClassExpression filler = desc.getFiller().accept(this); + if (negated) { + return dataFactory.getOWLObjectAllValuesFrom(desc.getProperty(), + filler); + } else { + return dataFactory.getOWLObjectSomeValuesFrom(desc.getProperty(), + filler); + } + } + + @Override + public OWLClassExpression visit(OWLObjectAllValuesFrom desc) { + OWLClassExpression filler = desc.getFiller().accept(this); + if (negated) { + return dataFactory.getOWLObjectSomeValuesFrom(desc.getProperty(), + filler); + } else { + return dataFactory.getOWLObjectAllValuesFrom(desc.getProperty(), + filler); + } + } + + @Override + public OWLClassExpression visit(OWLObjectHasValue desc) { + return desc.asSomeValuesFrom().accept(this); + } + + @Override + public OWLClassExpression visit(OWLObjectMinCardinality desc) { + boolean neg = negated; + int card = desc.getCardinality(); + if (negated) { + card = desc.getCardinality() - 1; + if (card < 0) { + card = 0; + } + } + negated = false; + + OWLClassExpression filler = desc.getFiller().accept(this); + + OWLClassExpression nnf; + if (neg) { + nnf = dataFactory.getOWLObjectMaxCardinality(card, desc.getProperty(), filler); + } else { + nnf = dataFactory.getOWLObjectMinCardinality(card, desc.getProperty(), filler); + } + negated = neg; + return nnf; + } + + @Override + public OWLClassExpression visit(OWLObjectExactCardinality desc) { + return desc.asIntersectionOfMinMax().accept(this); + } + + @Override + public OWLClassExpression visit(OWLObjectMaxCardinality desc) { + boolean neg = negated; + int card = desc.getCardinality(); + if (negated) { + card = desc.getCardinality() + 1; + } + negated = false; + OWLClassExpression filler = desc.getFiller().accept(this); + + OWLClassExpression nnf; + if (neg) { + nnf = dataFactory.getOWLObjectMinCardinality(card, + desc.getProperty(), filler); + } else { + nnf = dataFactory.getOWLObjectMaxCardinality(card, + desc.getProperty(), filler); + } + negated = neg; + return nnf; + } + + @Override + public OWLClassExpression visit(OWLObjectHasSelf desc) { + if (negated) { + return getNegation(desc); + } else { + return desc; + } + } + + @Override + public OWLClassExpression visit(OWLObjectOneOf desc) { + if (desc.getIndividuals().size() == 1) { + if (negated) { + return getNegation(desc); + } else { + return desc; + } + } else { + return desc.asObjectUnionOf().accept(this); + } + } + + @Override + public OWLClassExpression visit(OWLDataSomeValuesFrom desc) { + OWLDataRange filler = desc.getFiller().accept(this); + if (negated) { + return dataFactory.getOWLDataAllValuesFrom(desc.getProperty(), + filler); + } else { + return dataFactory.getOWLDataSomeValuesFrom(desc.getProperty(), + filler); + } + } + + @Override + public OWLClassExpression visit(OWLDataAllValuesFrom desc) { + OWLDataRange filler = desc.getFiller().accept(this); + if (negated) { + return dataFactory.getOWLDataSomeValuesFrom(desc.getProperty(), + filler); + } else { + return dataFactory.getOWLDataAllValuesFrom(desc.getProperty(), + filler); + } + } + + @Override + public OWLClassExpression visit(OWLDataHasValue desc) { + return desc.asSomeValuesFrom().accept(this); + } + + @Override + public OWLClassExpression visit(OWLDataExactCardinality desc) { + return desc.asIntersectionOfMinMax().accept(this); + } + + @Override + public OWLClassExpression visit(OWLDataMaxCardinality desc) { + boolean neg = negated; + int card = desc.getCardinality(); + if (negated) { + card = desc.getCardinality() + 1; + } + negated = false; + OWLDataRange filler = desc.getFiller().accept(this); + OWLClassExpression nnf; + if (neg) { + nnf = dataFactory.getOWLDataMinCardinality(card, + desc.getProperty(), filler); + } else { + nnf = dataFactory.getOWLDataMaxCardinality(card, + desc.getProperty(), filler); + } + negated = neg; + return nnf; + } + + @Override + public OWLClassExpression visit(OWLDataMinCardinality desc) { + boolean neg = negated; + int card = desc.getCardinality(); + if (negated) { + card = desc.getCardinality() - 1; + if (card < 0) { + card = 0; + } + } + negated = false; + OWLDataRange filler = desc.getFiller().accept(this); + OWLClassExpression nnf; + if (neg) { + nnf = dataFactory.getOWLDataMaxCardinality(card, + desc.getProperty(), filler); + } else { + nnf = dataFactory.getOWLDataMinCardinality(card, + desc.getProperty(), filler); + } + negated = neg; + return nnf; + } + + // ///////////////////////////////////////////////////////////////////////////////////////////////////////// + @Override + public OWLDataRange visit(OWLDatatype node) { + if (negated) { + return dataFactory.getOWLDataComplementOf(node); + } else { + return node; + } + } + + @Override + public OWLDataRange visit(OWLDataComplementOf node) { + if (negated) { + return node.getDataRange(); + } else { + return node; + } + } + + @Override + public OWLDataRange visit(OWLDataOneOf node) { + if (node.getValues().size() == 1) { + if (negated) { + return dataFactory.getOWLDataComplementOf(node); + } else { + return node; + } + } else { + // Encode as a data union of and return result + Set oneOfs = new HashSet<>(); + for (OWLLiteral lit : node.getValues()) { + oneOfs.add(dataFactory.getOWLDataOneOf(lit)); + } + return dataFactory.getOWLDataUnionOf(oneOfs).accept(this); + } + } + + @Override + public OWLDataRange visit(OWLDataIntersectionOf node) { + Set ops = new HashSet<>(); + for (OWLDataRange op : node.getOperands()) { + ops.add(op.accept(this)); + } + if (negated) { + return dataFactory.getOWLDataUnionOf(ops); + } else { + return dataFactory.getOWLDataIntersectionOf(ops); + } + } + + @Override + public OWLDataRange visit(OWLDataUnionOf node) { + Set ops = new HashSet<>(); + for (OWLDataRange op : node.getOperands()) { + ops.add(op.accept(this)); + } + if (negated) { + // Flip to an intersection + return dataFactory.getOWLDataIntersectionOf(ops); + } else { + return dataFactory.getOWLDataUnionOf(ops); + } + } + + @Override + public OWLAxiom visit(OWLHasKeyAxiom axiom) { + return null; + } + + @Override + public OWLDataRange visit(OWLDatatypeRestriction node) { + if (negated) { + return dataFactory.getOWLDataComplementOf(node); + } else { + return node; + } + } + + @Override + public OWLDataRange visit(OWLLiteral node) { + return null; + } + + @Override + public OWLDataRange visit(OWLFacetRestriction node) { + return null; + } + + // ////////////////////////////////////////////////////////////////////////////////////////////// + // + // Conversion of non-class expressions to NNF + // + // ///////////////////////////////////////////////////////////////////////////////////////////// + @Override + public OWLAxiom visit(OWLSubClassOfAxiom axiom) { + return dataFactory.getOWLSubClassOfAxiom( + axiom.getSubClass().accept(this), + axiom.getSuperClass().accept(this)); + } + + @Override + public OWLAxiom visit(OWLNegativeObjectPropertyAssertionAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLAsymmetricObjectPropertyAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLReflexiveObjectPropertyAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLDisjointClassesAxiom axiom) { + Set ops = new HashSet<>(); + for (OWLClassExpression op : axiom.getClassExpressions()) { + ops.add(op.accept(this)); + } + return dataFactory.getOWLDisjointClassesAxiom(ops); + } + + @Override + public OWLAxiom visit(OWLDataPropertyDomainAxiom axiom) { + return dataFactory.getOWLDataPropertyDomainAxiom(axiom.getProperty(), + axiom.getDomain().accept(this)); + } + + @Override + public OWLAxiom visit(OWLObjectPropertyDomainAxiom axiom) { + return dataFactory.getOWLObjectPropertyDomainAxiom(axiom.getProperty(), + axiom.getDomain().accept(this)); + } + + @Override + public OWLAxiom visit(OWLEquivalentObjectPropertiesAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLNegativeDataPropertyAssertionAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLDifferentIndividualsAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLDisjointDataPropertiesAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLDisjointObjectPropertiesAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLObjectPropertyRangeAxiom axiom) { + return dataFactory.getOWLObjectPropertyRangeAxiom(axiom.getProperty(), + axiom.getRange().accept(this)); + } + + @Override + public OWLAxiom visit(OWLObjectPropertyAssertionAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLFunctionalObjectPropertyAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLSubObjectPropertyOfAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLDisjointUnionAxiom axiom) { + Set descs = new HashSet<>(); + for (OWLClassExpression op : axiom.getClassExpressions()) { + descs.add(op.accept(this)); + } + return dataFactory.getOWLDisjointUnionAxiom(axiom.getOWLClass(), descs); + } + + @Override + public OWLAxiom visit(OWLDeclarationAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLAnnotationAssertionAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLSymmetricObjectPropertyAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLDataPropertyRangeAxiom axiom) { + return dataFactory.getOWLDataPropertyRangeAxiom(axiom.getProperty(), + axiom.getRange().accept(this)); + } + + @Override + public OWLAxiom visit(OWLFunctionalDataPropertyAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLEquivalentDataPropertiesAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLClassAssertionAxiom axiom) { + if (axiom.getClassExpression().isAnonymous()) { + return dataFactory.getOWLClassAssertionAxiom(axiom + .getClassExpression().accept(this), axiom.getIndividual()); + } else { + return axiom; + } + } + + @Override + public OWLAxiom visit(OWLEquivalentClassesAxiom axiom) { + Set ops = new HashSet<>(); + for (OWLClassExpression op : axiom.getClassExpressions()) { + ops.add(op.accept(this)); + } + return dataFactory.getOWLEquivalentClassesAxiom(ops); + } + + @Override + public OWLAxiom visit(OWLDataPropertyAssertionAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLTransitiveObjectPropertyAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLIrreflexiveObjectPropertyAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLSubDataPropertyOfAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLInverseFunctionalObjectPropertyAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLSameIndividualAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLSubPropertyChainOfAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLInverseObjectPropertiesAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(SWRLRule rule) { + return rule; + } + + @Override + public OWLAxiom visit(OWLAnnotationPropertyDomainAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLAnnotationPropertyRangeAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLSubAnnotationPropertyOfAxiom axiom) { + return axiom; + } + + @Override + public OWLAxiom visit(OWLDatatypeDefinitionAxiom axiom) { + return axiom; + } +} diff --git a/components-core/src/main/java/org/dllearner/core/owl/OWLNaryBooleanClassExpressionImplExt.java b/components-core/src/main/java/org/dllearner/core/owl/OWLNaryBooleanClassExpressionImplExt.java index 8ad52264af..63d00feb00 100644 --- a/components-core/src/main/java/org/dllearner/core/owl/OWLNaryBooleanClassExpressionImplExt.java +++ b/components-core/src/main/java/org/dllearner/core/owl/OWLNaryBooleanClassExpressionImplExt.java @@ -1,40 +1,20 @@ -/* - * This file is part of the OWL API. - * - * The contents of this file are subject to the LGPL License, Version 3.0. +/** + * Copyright (C) 2007 - 2016, Jens Lehmann * - * Copyright (C) 2011, The University of Manchester + * This file is part of DL-Learner. * - * This program is free software: you can redistribute it and/or modify + * DL-Learner is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or + * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * - * This program is distributed in the hope that it will be useful, + * DL-Learner is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - * - * - * Alternatively, the contents of this file may be used under the terms of the Apache License, Version 2.0 - * in which case, the provisions of the Apache License Version 2.0 are applicable instead of those above. - * - * Copyright 2011, University of Manchester - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * along with this program. If not, see . */ package org.dllearner.core.owl; @@ -43,9 +23,7 @@ import java.util.Set; import java.util.TreeSet; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLNaryBooleanClassExpression; -import org.semanticweb.owlapi.model.OWLObject; +import org.semanticweb.owlapi.model.*; import org.semanticweb.owlapi.util.CollectionFactory; import uk.ac.manchester.cs.owl.owlapi.OWLAnonymousClassExpressionImpl; @@ -75,7 +53,7 @@ public abstract class OWLNaryBooleanClassExpressionImplExt extends public OWLNaryBooleanClassExpressionImplExt( Set operands) { super(); - this.operands = new ArrayList(new TreeSet(operands)); + this.operands = new ArrayList<>(new TreeSet<>(operands)); } /** @@ -85,12 +63,26 @@ public OWLNaryBooleanClassExpressionImplExt( public OWLNaryBooleanClassExpressionImplExt( List operands) { super(); - this.operands = new ArrayList(operands); + this.operands = new ArrayList<>(operands); + } + + @Override + public void addSignatureEntitiesToSet(Set entities) { + for (OWLClassExpression operand : operands) { + addSignatureEntitiesToSetForValue(entities, operand); + } + } + + @Override + public void addAnonymousIndividualsToSet(Set anons) { + for (OWLClassExpression operand : operands) { + addAnonymousIndividualsToSetForValue(anons, operand); + } } @Override public List getOperandsAsList() { - return new ArrayList(operands); + return new ArrayList<>(operands); } @Override diff --git a/components-core/src/main/java/org/dllearner/core/owl/OWLObjectIntersectionOfImplExt.java b/components-core/src/main/java/org/dllearner/core/owl/OWLObjectIntersectionOfImplExt.java index b57bd5c19f..fe689f465f 100644 --- a/components-core/src/main/java/org/dllearner/core/owl/OWLObjectIntersectionOfImplExt.java +++ b/components-core/src/main/java/org/dllearner/core/owl/OWLObjectIntersectionOfImplExt.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.core.owl; @@ -14,6 +29,7 @@ import org.semanticweb.owlapi.model.OWLObjectIntersectionOf; import org.semanticweb.owlapi.model.OWLObjectVisitor; import org.semanticweb.owlapi.model.OWLObjectVisitorEx; +import org.semanticweb.owlapi.util.OWLObjectTypeIndexProvider; /** * @@ -31,6 +47,11 @@ public class OWLObjectIntersectionOfImplExt extends private static final long serialVersionUID = 30406L; + @Override + protected int index() { + return OWLObjectTypeIndexProvider.CLASS_EXPRESSION_TYPE_INDEX_BASE + 1; + } + /** * @param operands * operands @@ -84,7 +105,7 @@ public O accept(OWLClassExpressionVisitorEx visitor) { @Override public Set asConjunctSet() { - Set conjuncts = new HashSet(); + Set conjuncts = new HashSet<>(); for (OWLClassExpression op : getOperands()) { conjuncts.addAll(op.asConjunctSet()); } diff --git a/components-core/src/main/java/org/dllearner/core/owl/OWLObjectUnionOfImplExt.java b/components-core/src/main/java/org/dllearner/core/owl/OWLObjectUnionOfImplExt.java index 8c98a1581a..3ebcd8dbce 100644 --- a/components-core/src/main/java/org/dllearner/core/owl/OWLObjectUnionOfImplExt.java +++ b/components-core/src/main/java/org/dllearner/core/owl/OWLObjectUnionOfImplExt.java @@ -1,40 +1,20 @@ -/* - * This file is part of the OWL API. - * - * The contents of this file are subject to the LGPL License, Version 3.0. +/** + * Copyright (C) 2007 - 2016, Jens Lehmann * - * Copyright (C) 2011, The University of Manchester + * This file is part of DL-Learner. * - * This program is free software: you can redistribute it and/or modify + * DL-Learner is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or + * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * - * This program is distributed in the hope that it will be useful, + * DL-Learner is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - * - * - * Alternatively, the contents of this file may be used under the terms of the Apache License, Version 2.0 - * in which case, the provisions of the Apache License Version 2.0 are applicable instead of those above. - * - * Copyright 2011, University of Manchester - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * along with this program. If not, see . */ package org.dllearner.core.owl; @@ -49,6 +29,7 @@ import org.semanticweb.owlapi.model.OWLObjectUnionOf; import org.semanticweb.owlapi.model.OWLObjectVisitor; import org.semanticweb.owlapi.model.OWLObjectVisitorEx; +import org.semanticweb.owlapi.util.OWLObjectTypeIndexProvider; /** * @author Matthew Horridge, The University Of Manchester, Bio-Health Informatics @@ -59,6 +40,11 @@ public class OWLObjectUnionOfImplExt extends OWLNaryBooleanClassExpressionImplEx private static final long serialVersionUID = 30406L; + @Override + protected int index() { + return OWLObjectTypeIndexProvider.CLASS_EXPRESSION_TYPE_INDEX_BASE + 2; + } + /** * @param operands * operands @@ -90,7 +76,7 @@ public boolean equals(Object obj) { @Override public Set asDisjunctSet() { - Set disjuncts = new HashSet(); + Set disjuncts = new HashSet<>(); for (OWLClassExpression op : getOperands()) { disjuncts.addAll(op.asDisjunctSet()); } diff --git a/components-core/src/main/java/org/dllearner/core/owl/ObjectPropertyHierarchy.java b/components-core/src/main/java/org/dllearner/core/owl/ObjectPropertyHierarchy.java index 3fc630663c..d455d5005e 100644 --- a/components-core/src/main/java/org/dllearner/core/owl/ObjectPropertyHierarchy.java +++ b/components-core/src/main/java/org/dllearner/core/owl/ObjectPropertyHierarchy.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core.owl; import java.util.Set; @@ -49,11 +48,11 @@ public ObjectPropertyHierarchy( } public SortedSet getMoreGeneralRoles(OWLObjectProperty role) { - return new TreeSet(getParents(role)); + return new TreeSet<>(getParents(role)); } public SortedSet getMoreSpecialRoles(OWLObjectProperty role) { - return new TreeSet(getChildren(role)); + return new TreeSet<>(getChildren(role)); } public boolean isSubpropertyOf(OWLObjectProperty subProperty, OWLObjectProperty superProperty) { diff --git a/components-core/src/main/java/org/dllearner/core/owl/UnsupportedLanguageException.java b/components-core/src/main/java/org/dllearner/core/owl/UnsupportedLanguageException.java index 2dd0d6d290..140eed2279 100644 --- a/components-core/src/main/java/org/dllearner/core/owl/UnsupportedLanguageException.java +++ b/components-core/src/main/java/org/dllearner/core/owl/UnsupportedLanguageException.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core.owl; /** diff --git a/components-core/src/main/java/org/dllearner/core/owl/fuzzydll/FuzzyIndividual.java b/components-core/src/main/java/org/dllearner/core/owl/fuzzydll/FuzzyIndividual.java index fea7c86c09..de5744c762 100644 --- a/components-core/src/main/java/org/dllearner/core/owl/fuzzydll/FuzzyIndividual.java +++ b/components-core/src/main/java/org/dllearner/core/owl/fuzzydll/FuzzyIndividual.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.core.owl.fuzzydll; import org.semanticweb.owlapi.model.IRI; @@ -47,12 +46,25 @@ public int compareTo(FuzzyIndividual o) { else return d; } - + @Override public boolean equals(Object o) { - if(o==null) { - return false; - } - return (compareTo((FuzzyIndividual)o)==0); + if (this == o) return true; + if (!(o instanceof FuzzyIndividual)) return false; + if (!super.equals(o)) return false; + + FuzzyIndividual that = (FuzzyIndividual) o; + + return Double.compare(that.truthDegree, truthDegree) == 0; + + } + + @Override + public int hashCode() { + int result = super.hashCode(); + long temp; + temp = Double.doubleToLongBits(truthDegree); + result = 31 * result + (int) (temp ^ (temp >>> 32)); + return result; } } diff --git a/components-core/src/main/java/org/dllearner/core/ref/ALCLearningAlgorithm.java b/components-core/src/main/java/org/dllearner/core/ref/ALCLearningAlgorithm.java index 6c64339f14..d7608f330f 100644 --- a/components-core/src/main/java/org/dllearner/core/ref/ALCLearningAlgorithm.java +++ b/components-core/src/main/java/org/dllearner/core/ref/ALCLearningAlgorithm.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.core.ref; @@ -12,6 +27,7 @@ * @author Lorenz Buehmann * */ +// not for conf public class ALCLearningAlgorithm extends RefinementOperatorBasedLearningAlgorithmBase{ public ALCLearningAlgorithm(OWLOntology ontology, OWLReasoner reasoner) { diff --git a/components-core/src/main/java/org/dllearner/core/ref/ClassExpressionRefinementOperator.java b/components-core/src/main/java/org/dllearner/core/ref/ClassExpressionRefinementOperator.java index 45e99de361..974a87fae5 100644 --- a/components-core/src/main/java/org/dllearner/core/ref/ClassExpressionRefinementOperator.java +++ b/components-core/src/main/java/org/dllearner/core/ref/ClassExpressionRefinementOperator.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.core.ref; diff --git a/components-core/src/main/java/org/dllearner/core/ref/ClassExpressionRefinementOperatorBase.java b/components-core/src/main/java/org/dllearner/core/ref/ClassExpressionRefinementOperatorBase.java index 7cf8e18d42..6a17033da2 100644 --- a/components-core/src/main/java/org/dllearner/core/ref/ClassExpressionRefinementOperatorBase.java +++ b/components-core/src/main/java/org/dllearner/core/ref/ClassExpressionRefinementOperatorBase.java @@ -1,9 +1,25 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.core.ref; import java.util.SortedSet; +import java.util.TreeSet; import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLDataFactory; @@ -21,6 +37,7 @@ public abstract class ClassExpressionRefinementOperatorBase extends protected OWLDataFactory dataFactory; public ClassExpressionRefinementOperatorBase(OWLReasoner reasoner, OWLDataFactory dataFactory) { + super(new TreeSet()); this.reasoner = reasoner; this.dataFactory = dataFactory; } diff --git a/components-core/src/main/java/org/dllearner/core/ref/RefinementOperator.java b/components-core/src/main/java/org/dllearner/core/ref/RefinementOperator.java index c2047355c0..438bbb578e 100644 --- a/components-core/src/main/java/org/dllearner/core/ref/RefinementOperator.java +++ b/components-core/src/main/java/org/dllearner/core/ref/RefinementOperator.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.core.ref; diff --git a/components-core/src/main/java/org/dllearner/core/ref/RefinementOperatorALC.java b/components-core/src/main/java/org/dllearner/core/ref/RefinementOperatorALC.java index abf367c2a7..3e0a27d188 100644 --- a/components-core/src/main/java/org/dllearner/core/ref/RefinementOperatorALC.java +++ b/components-core/src/main/java/org/dllearner/core/ref/RefinementOperatorALC.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.core.ref; @@ -38,7 +53,7 @@ public RefinementOperatorALC(OWLReasoner reasoner, OWLDataFactory dataFactory) { */ @Override public SortedSet visit(OWLClass ce) { - SortedSet refinements = new TreeSet(); + SortedSet refinements = new TreeSet<>(); refinements.addAll(reasoner.getSubClasses(ce, true).getFlattened()); @@ -50,14 +65,14 @@ public SortedSet visit(OWLClass ce) { */ @Override public SortedSet visit(OWLObjectIntersectionOf ce) { - SortedSet refinements = new TreeSet(); + SortedSet refinements = new TreeSet<>(); for (OWLClassExpression operand : ce.getOperands()) { // refine operand SortedSet operandRefinements = refineNode(operand); for (OWLClassExpression operandRefinement : operandRefinements) { - Set newOperands = new HashSet(ce.getOperands()); + Set newOperands = new HashSet<>(ce.getOperands()); newOperands.remove(operand); newOperands.add(operandRefinement); @@ -73,14 +88,14 @@ public SortedSet visit(OWLObjectIntersectionOf ce) { */ @Override public SortedSet visit(OWLObjectUnionOf ce) { - SortedSet refinements = new TreeSet(); + SortedSet refinements = new TreeSet<>(); for (OWLClassExpression operand : ce.getOperands()) { // refine operand SortedSet operandRefinements = refineNode(operand); for (OWLClassExpression operandRefinement : operandRefinements) { - Set newOperands = new HashSet(ce.getOperands()); + Set newOperands = new HashSet<>(ce.getOperands()); newOperands.remove(operand); newOperands.add(operandRefinement); @@ -96,7 +111,7 @@ public SortedSet visit(OWLObjectUnionOf ce) { */ @Override public SortedSet visit(OWLObjectSomeValuesFrom ce) { - SortedSet refinements = new TreeSet(); + SortedSet refinements = new TreeSet<>(); OWLObjectPropertyExpression property = ce.getProperty(); OWLClassExpression filler = ce.getFiller(); @@ -121,7 +136,7 @@ public SortedSet visit(OWLObjectSomeValuesFrom ce) { */ @Override public SortedSet visit(OWLObjectAllValuesFrom ce) { - SortedSet refinements = new TreeSet(); + SortedSet refinements = new TreeSet<>(); OWLObjectPropertyExpression property = ce.getProperty(); OWLClassExpression filler = ce.getFiller(); @@ -146,7 +161,7 @@ public SortedSet visit(OWLObjectAllValuesFrom ce) { */ @Override public SortedSet visit(OWLDataSomeValuesFrom ce) { - SortedSet refinements = new TreeSet(); + SortedSet refinements = new TreeSet<>(); OWLDataPropertyExpression property = ce.getProperty(); OWLDataRange filler = ce.getFiller(); @@ -168,7 +183,7 @@ public SortedSet visit(OWLDataSomeValuesFrom ce) { */ @Override public SortedSet visit(OWLDataAllValuesFrom ce) { - SortedSet refinements = new TreeSet(); + SortedSet refinements = new TreeSet<>(); OWLDataPropertyExpression property = ce.getProperty(); OWLDataRange filler = ce.getFiller(); diff --git a/components-core/src/main/java/org/dllearner/core/ref/RefinementOperatorBasedLearningAlgorithmBase.java b/components-core/src/main/java/org/dllearner/core/ref/RefinementOperatorBasedLearningAlgorithmBase.java index 9105d8a236..53fe17aa31 100644 --- a/components-core/src/main/java/org/dllearner/core/ref/RefinementOperatorBasedLearningAlgorithmBase.java +++ b/components-core/src/main/java/org/dllearner/core/ref/RefinementOperatorBasedLearningAlgorithmBase.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.core.ref; @@ -40,7 +55,7 @@ public void start() { preProcess(); // start with empty search tree - searchTree = new SearchTree>(heuristic); + searchTree = new SearchTree<>(heuristic); // add start node to search tree searchTree.addNode(startNode); @@ -70,9 +85,9 @@ protected SortedSet refineNode(SearchTreeNode node) { * Checks whether the refinement is allowed to be added to the search tree * first by calling {@link #isValid(T refinement)}}, and if yes creates a * new node which is added to the search tree. - * @param refinement - * @param parentNode - * @return + * @param refinement the refinement + * @param parentNode the parent node + * @return whether the refinement is allowed to be added to the search tree */ protected boolean addToSearchTree(T refinement, SearchTreeNode parentNode) { // check if the refinement is allowed @@ -92,13 +107,13 @@ protected boolean addToSearchTree(T refinement, SearchTreeNode parentNode) { } protected SearchTreeNode createNode(T refinement, SearchTreeNode parentNode) { - return new SearchTreeNodeSimple(refinement, parentNode); + return new SearchTreeNodeSimple<>(refinement, parentNode); } - protected void preProcess() {}; + protected void preProcess() {} + + protected void postProcess() {} - protected void postProcess() {}; - protected abstract SearchTreeNode computeStartNode(); protected abstract SearchTreeNode getNextNodeToExpand(); @@ -106,14 +121,14 @@ protected SearchTreeNode createNode(T refinement, SearchTreeNode parentNod /** * Checks whether the object is valid for further refinement - * @param refinement - * @return + * @param refinement the refinement + * @return whether the object is valid for further refinement */ protected abstract boolean isValid(T refinement); /** * Checks whether the algorithm has to be terminated. - * @return + * @return whether the algorithm has to be terminated */ protected abstract boolean terminationCriteriaSatisfied(); diff --git a/components-core/src/main/java/org/dllearner/core/ref/SearchTree.java b/components-core/src/main/java/org/dllearner/core/ref/SearchTree.java index 5669456207..888e759818 100644 --- a/components-core/src/main/java/org/dllearner/core/ref/SearchTree.java +++ b/components-core/src/main/java/org/dllearner/core/ref/SearchTree.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.core.ref; import java.util.Comparator; @@ -13,7 +31,7 @@ public class SearchTree> { private SortedSet tree; public SearchTree(Comparator comparator) { - tree = new TreeSet(comparator); + tree = new TreeSet<>(comparator); } public boolean addNode(T node) { diff --git a/components-core/src/main/java/org/dllearner/core/ref/SearchTreeHeuristic.java b/components-core/src/main/java/org/dllearner/core/ref/SearchTreeHeuristic.java index b5288b5ea2..7f864c654f 100644 --- a/components-core/src/main/java/org/dllearner/core/ref/SearchTreeHeuristic.java +++ b/components-core/src/main/java/org/dllearner/core/ref/SearchTreeHeuristic.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.core.ref; diff --git a/components-core/src/main/java/org/dllearner/core/ref/SearchTreeNode.java b/components-core/src/main/java/org/dllearner/core/ref/SearchTreeNode.java index 8f97850b43..aec55d36c5 100644 --- a/components-core/src/main/java/org/dllearner/core/ref/SearchTreeNode.java +++ b/components-core/src/main/java/org/dllearner/core/ref/SearchTreeNode.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.core.ref; diff --git a/components-core/src/main/java/org/dllearner/core/ref/SearchTreeNodeSimple.java b/components-core/src/main/java/org/dllearner/core/ref/SearchTreeNodeSimple.java index 6d2905de93..74bd1cb759 100644 --- a/components-core/src/main/java/org/dllearner/core/ref/SearchTreeNodeSimple.java +++ b/components-core/src/main/java/org/dllearner/core/ref/SearchTreeNodeSimple.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.core.ref; diff --git a/components-core/src/main/java/org/dllearner/exceptions/DLLearnerException.java b/components-core/src/main/java/org/dllearner/exceptions/DLLearnerException.java index 6e96bb286d..af58381b82 100644 --- a/components-core/src/main/java/org/dllearner/exceptions/DLLearnerException.java +++ b/components-core/src/main/java/org/dllearner/exceptions/DLLearnerException.java @@ -1,9 +1,25 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.exceptions; /** + * An exception thrown by the DL-Learner framework. * @author Lorenz Buehmann * */ diff --git a/components-core/src/main/java/org/dllearner/exceptions/LCSException.java b/components-core/src/main/java/org/dllearner/exceptions/LCSException.java index 3484919345..c576aa39a2 100644 --- a/components-core/src/main/java/org/dllearner/exceptions/LCSException.java +++ b/components-core/src/main/java/org/dllearner/exceptions/LCSException.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.exceptions; diff --git a/components-core/src/main/java/org/dllearner/exceptions/package-info.java b/components-core/src/main/java/org/dllearner/exceptions/package-info.java index 48fec6c2b8..a25034edd4 100644 --- a/components-core/src/main/java/org/dllearner/exceptions/package-info.java +++ b/components-core/src/main/java/org/dllearner/exceptions/package-info.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ /** * */ diff --git a/components-core/src/main/java/org/dllearner/kb/KBFile.java b/components-core/src/main/java/org/dllearner/kb/KBFile.java index a86c351c68..a6acfa96c9 100644 --- a/components-core/src/main/java/org/dllearner/kb/KBFile.java +++ b/components-core/src/main/java/org/dllearner/kb/KBFile.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,10 +16,8 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb; -import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.net.MalformedURLException; @@ -27,15 +25,12 @@ import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Collection; -import java.util.LinkedList; import org.apache.log4j.Logger; import org.dllearner.core.AbstractKnowledgeSource; import org.dllearner.core.ComponentAnn; import org.dllearner.core.ComponentInitException; import org.dllearner.core.config.ConfigOption; -import org.dllearner.core.options.URLConfigOption; import org.dllearner.parser.KBParser; import org.dllearner.parser.ParseException; import org.semanticweb.owlapi.model.OWLOntology; @@ -73,10 +68,8 @@ public KBFile() { } /** - * Constructor allowing you to treat an already existing KB object - * as a KBFile knowledge source. Use it sparingly, because the - * standard way to create components is via - * {@link org.dllearner.core.ComponentManager}. + * Constructor allowing you to treat an already existing OWL ontology + * as a KBFile knowledge source. * * @param kb A KB object. */ @@ -84,15 +77,6 @@ public KBFile(OWLOntology kb) { this.kb = kb; } - public static Collection> createConfigOptions() { - Collection> options = new LinkedList>(); -// options.add(new StringConfigOption("filename", "pointer to the KB file on local file system",null, true, true)); - URLConfigOption urlOption = new URLConfigOption("url", "URL pointer to the KB file", null, false, true); - urlOption.setRefersToFile(true); - options.add(urlOption); - return options; - } - public static String getName() { return "KB file"; } diff --git a/components-core/src/main/java/org/dllearner/kb/LocalModelBasedSparqlEndpointKS.java b/components-core/src/main/java/org/dllearner/kb/LocalModelBasedSparqlEndpointKS.java index da0e25085c..fd8c9ddfaa 100644 --- a/components-core/src/main/java/org/dllearner/kb/LocalModelBasedSparqlEndpointKS.java +++ b/components-core/src/main/java/org/dllearner/kb/LocalModelBasedSparqlEndpointKS.java @@ -1,8 +1,23 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb; -import java.net.MalformedURLException; -import java.net.URL; - import org.aksw.jena_sparql_api.cache.h2.CacheUtilsH2; import org.aksw.jena_sparql_api.core.QueryExecutionFactory; import org.aksw.jena_sparql_api.model.QueryExecutionFactoryModel; @@ -17,56 +32,43 @@ public class LocalModelBasedSparqlEndpointKS extends SparqlEndpointKS { private OntModel model; -// private String fileName; -// private String baseDir; -// private boolean enableReasoning = false; - public LocalModelBasedSparqlEndpointKS() { - } + public LocalModelBasedSparqlEndpointKS() {} -// public LocalModelBasedSparqlEndpointKS(String ontologyURL) throws MalformedURLException { -// this(new URL(ontologyURL)); -// } -// -// public LocalModelBasedSparqlEndpointKS(URL ontologyURL) { -// this.fileName = ontologyURL.toString(); -// } -// + /** + * Create new Sparql Endpoint based on Jena Ontology + * @param model ontology model + */ public LocalModelBasedSparqlEndpointKS(OntModel model) { this.model = model; } + /** + * Create new Sparql Endpoint based on Jena model + * @param model rdf model + * @param reasoningEnabled whether to use Jena RDFS inferencing + */ public LocalModelBasedSparqlEndpointKS(Model model, boolean reasoningEnabled) { this(model, reasoningEnabled ? OntModelSpec.OWL_MEM_RDFS_INF : OntModelSpec.OWL_MEM); } + /** + * Create new Sparql Endpoint based on Jena Model and reasoning spec + * @param model rdf model + * @param reasoning type of reasoning + */ public LocalModelBasedSparqlEndpointKS(Model model, OntModelSpec reasoning) { this.model = ModelFactory.createOntologyModel(reasoning, model); } /** - * No reasoning enabled by default. - * @param model + * Create new Sparql Endpoint based on Jena Model. No reasoning enabled by default. + * @param model rdf model */ public LocalModelBasedSparqlEndpointKS(Model model) { this(model, false); } -// @Override -// public void init() throws ComponentInitException { -// Model baseModel = ModelFactory.createDefaultModel(); -// // use the FileManager to find the input file -// InputStream in = FileManager.get().open(baseDir + File.separator + fileName); -// if (in == null) { -// throw new IllegalArgumentException( -// "File: " + fileName + " not found"); -// } -// // read the RDF/XML file -// baseModel.read(in, null); -// -// model = ModelFactory.createOntologyModel(enableReasoning ? OntModelSpec.OWL_MEM : OntModelSpec.OWL_MEM_RDFS_INF, baseModel); -// } - /* (non-Javadoc) * @see org.dllearner.kb.SparqlEndpointKS#buildQueryExecutionFactory() */ @@ -80,30 +82,6 @@ protected QueryExecutionFactory buildQueryExecutionFactory() { return qef; } -// public void setFileName(String fileName) { -// this.fileName = fileName; -// } -// -// public String getFileName() { -// return fileName; -// } -// -// public String getBaseDir() { -// return baseDir; -// } -// -// public void setBaseDir(String baseDir) { -// this.baseDir = baseDir; -// } -// -// public void setEnableReasoning(boolean enableReasoning) { -// this.enableReasoning = enableReasoning; -// } -// -// public boolean isEnableReasoning() { -// return enableReasoning; -// } - public OntModel getModel() { return model; } @@ -117,4 +95,11 @@ public boolean isRemote() { public boolean supportsSPARQL_1_1() { return true; } + + @Override + public String toString() { + String out = String.format("%-15s %-25s%n", "Endpoint:", "in-memory model"); + out += String.format("%-15s %-25s%n", "Profile:", model.getProfile()); + return out; + } } diff --git a/components-core/src/main/java/org/dllearner/kb/OWLAPIOntology.java b/components-core/src/main/java/org/dllearner/kb/OWLAPIOntology.java index dc0c391442..458dddb8b7 100644 --- a/components-core/src/main/java/org/dllearner/kb/OWLAPIOntology.java +++ b/components-core/src/main/java/org/dllearner/kb/OWLAPIOntology.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,14 +16,9 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb; -import java.util.Collections; - import org.dllearner.core.AbstractKnowledgeSource; -import org.dllearner.core.options.ConfigEntry; -import org.dllearner.core.options.InvalidConfigOptionValueException; import org.dllearner.utilities.owl.OntologyToByteConverter; import org.dllearner.utilities.owl.SimpleOntologyToByteConverter; import org.semanticweb.owlapi.model.IRI; @@ -31,10 +26,13 @@ import org.semanticweb.owlapi.model.OWLOntologyCreationException; import org.semanticweb.owlapi.model.OWLOntologyManager; +import java.util.Collections; + /** * This class provides a wrapper around a single OWL Ontology. However, due to threading issues it is not safe * to allow access to ontologies created with an Ontology Manager which we do not control. */ +// not for conf public class OWLAPIOntology extends AbstractKnowledgeSource implements OWLOntologyKnowledgeSource{ private byte[] ontologyBytes; @@ -59,7 +57,7 @@ public OWLOntology createOWLOntology(OWLOntologyManager manager) { if(ontology.getOntologyID().isAnonymous()){ iri = IRI.generateDocumentIRI(); } else { - iri = ontology.getOntologyID().getOntologyIRI(); + iri = ontology.getOntologyID().getOntologyIRI().orNull(); if(iri == null) { iri = IRI.generateDocumentIRI(); } @@ -71,12 +69,6 @@ public OWLOntology createOWLOntology(OWLOntologyManager manager) { // return converter.convert(ontologyBytes, manager); return copy; } - - @Override - public void applyConfigEntry(ConfigEntry entry) throws InvalidConfigOptionValueException - { - - } @Override public void init() diff --git a/components-core/src/main/java/org/dllearner/kb/OWLFile.java b/components-core/src/main/java/org/dllearner/kb/OWLFile.java index 272cc2ca11..80b5b56e2c 100644 --- a/components-core/src/main/java/org/dllearner/kb/OWLFile.java +++ b/components-core/src/main/java/org/dllearner/kb/OWLFile.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb; import java.io.File; @@ -45,7 +44,7 @@ * @author Jens Lehmann * @author Sebastian Hellmann *

- * SH: I added SPARQL capabilities. Either URL is set directly or the basedir and filename is set or the URL and the SPARQL query is set + * SH added SPARQL capabilities. Either URL is set directly or the basedir and filename is set or the URL and the SPARQL query is set */ @ComponentAnn(name = "OWL File", shortName = "owlfile", version = 0.9) public class OWLFile extends AbstractKnowledgeSource implements OWLOntologyKnowledgeSource { @@ -63,9 +62,9 @@ public class OWLFile extends AbstractKnowledgeSource implements OWLOntologyKnowl @ConfigOption(name = "sparql", description = "SPARQL CONSTRUCT expression to download from Endpoint") private String sparql = null; @ConfigOption(name = "defaultGraphURIs", description = "a list of default graph URIs to query from the Endpoint") - private List defaultGraphURIs = new LinkedList(); + private List defaultGraphURIs = new LinkedList<>(); @ConfigOption(name = "namedGraphURIs", description = "a list of named graph URIs to query from the Endpoint") - private List namedGraphURIs = new LinkedList(); + private List namedGraphURIs = new LinkedList<>(); private OntModelSpec reasoning = OntModelSpec.OWL_MEM; @ConfigOption(name = "reasoningString", defaultValue = "false", description = "Enable JENA reasoning on the Ontology Model." @@ -141,9 +140,7 @@ public OWLOntology createOWLOntology(OWLOntologyManager manager) { try { OWLOntology ontology = manager.loadOntologyFromOntologyDocument(IRI.create(getURL().toURI())); return ontology; - } catch (OWLOntologyCreationException e) { - throw new RuntimeException(e); - } catch (URISyntaxException e) { + } catch (OWLOntologyCreationException | URISyntaxException e) { throw new RuntimeException(e); } } @@ -192,7 +189,7 @@ public void setSparql(String sparql) { public void setSparql(String sparql, boolean autoQuote) { if (autoQuote) { - // quote IRIs + // quote IRIs sparql = sparql.replaceAll("(?<=^|\\s|\\()((?:([^<(:/?#\\s]*):)(?://([^/?#]*?))?([^?#]*?)(?:\\?([^#]*?))?(?:#(.*?))?)(?=(,|\\.|;|)(\\)|\\s|$))", "<$1>"); } this.sparql = sparql; diff --git a/components-core/src/main/java/org/dllearner/kb/OWLOntologyKnowledgeSource.java b/components-core/src/main/java/org/dllearner/kb/OWLOntologyKnowledgeSource.java index 8862eb55a9..105195c7ae 100644 --- a/components-core/src/main/java/org/dllearner/kb/OWLOntologyKnowledgeSource.java +++ b/components-core/src/main/java/org/dllearner/kb/OWLOntologyKnowledgeSource.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb; import org.dllearner.core.KnowledgeSource; @@ -20,5 +38,5 @@ public interface OWLOntologyKnowledgeSource extends KnowledgeSource{ * @param manager The manager to associate the new ontology with. * @return The result ontology */ - public OWLOntology createOWLOntology(OWLOntologyManager manager); + OWLOntology createOWLOntology(OWLOntologyManager manager); } diff --git a/components-core/src/main/java/org/dllearner/kb/SparqlEndpointKS.java b/components-core/src/main/java/org/dllearner/kb/SparqlEndpointKS.java index dc43df99f3..cdc8aac813 100644 --- a/components-core/src/main/java/org/dllearner/kb/SparqlEndpointKS.java +++ b/components-core/src/main/java/org/dllearner/kb/SparqlEndpointKS.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb; import java.net.URL; @@ -28,12 +27,14 @@ import org.aksw.jena_sparql_api.cache.h2.CacheUtilsH2; import org.aksw.jena_sparql_api.core.QueryExecutionFactory; import org.aksw.jena_sparql_api.delay.core.QueryExecutionFactoryDelay; -import org.aksw.jena_sparql_api.http.QueryExecutionFactoryHttp; +import org.aksw.jena_sparql_api.pagination.core.QueryExecutionFactoryPaginated; import org.aksw.jena_sparql_api.retry.core.QueryExecutionFactoryRetry; import org.dllearner.core.AbstractKnowledgeSource; import org.dllearner.core.ComponentAnn; import org.dllearner.core.ComponentInitException; +import org.dllearner.core.KnowledgeSource; import org.dllearner.core.config.ConfigOption; +import org.dllearner.kb.sparql.QueryExecutionFactoryHttp; import org.dllearner.kb.sparql.SPARQLTasks; import org.dllearner.kb.sparql.SparqlEndpoint; import org.slf4j.Logger; @@ -62,10 +63,10 @@ public class SparqlEndpointKS extends AbstractKnowledgeSource { private URL url; @ConfigOption(name = "defaultGraphURIs", description="a list of default graph URIs", defaultValue="{}", required=false) - private List defaultGraphURIs = new LinkedList(); + private List defaultGraphURIs = new LinkedList<>(); @ConfigOption(name = "namedGraphURIs", description="a list of named graph URIs", defaultValue="{}", required=false) - private List namedGraphURIs = new LinkedList(); + private List namedGraphURIs = new LinkedList<>(); // some parameters for the query execution @ConfigOption(name = "queryDelay", defaultValue = "50", description = "Use this setting to avoid overloading the endpoint with a sudden burst of queries. A value below 0 means no delay.", required = false) @@ -87,12 +88,19 @@ public class SparqlEndpointKS extends AbstractKnowledgeSource { protected QueryExecutionFactory qef; private long pageSize = 10000; + + private KnowledgeSource schema; public SparqlEndpointKS() {} public SparqlEndpointKS(SparqlEndpoint endpoint) { this.endpoint = endpoint; } + + public SparqlEndpointKS(SparqlEndpoint endpoint, KnowledgeSource schema) { + this.endpoint = endpoint; + this.schema = schema; + } public SparqlEndpointKS(QueryExecutionFactory qef) { this.qef = qef; @@ -138,7 +146,7 @@ public void init() throws ComponentInitException { } protected QueryExecutionFactory buildQueryExecutionFactory() { - QueryExecutionFactory qef = new QueryExecutionFactoryHttp( + QueryExecutionFactory qef = new org.aksw.jena_sparql_api.http.QueryExecutionFactoryHttp( endpoint.getURL().toString(), endpoint.getDefaultGraphURIs()); @@ -147,7 +155,7 @@ protected QueryExecutionFactory buildQueryExecutionFactory() { qef = CacheUtilsH2.createQueryExecutionFactory(qef, cacheDir, false, cacheTTL ); } else { // use in-memory cache -// qef = CacheUtilsH2.createQueryExecutionFactory(qef, cacheDir, true, cacheTTL); + qef = CacheUtilsH2.createQueryExecutionFactory(qef, cacheDir, true, cacheTTL); } // add some delay @@ -158,7 +166,7 @@ protected QueryExecutionFactory buildQueryExecutionFactory() { } // add pagination to avoid incomplete result sets due to limitations of the endpoint -// qef = new QueryExecutionFactoryPaginated(qef, pageSize); + qef = new QueryExecutionFactoryPaginated(qef, pageSize); return qef; } @@ -247,6 +255,13 @@ public void setCacheDir(String cacheDir) { public void setCacheTTL(long cacheTTL) { this.cacheTTL = cacheTTL; } + + /** + * @return if exists, a knowledge source which contains the schema + */ + public KnowledgeSource getSchema() { + return schema; + } @Override public String toString() { diff --git a/components-core/src/main/java/org/dllearner/kb/aquisitors/BlankNodeCollector.java b/components-core/src/main/java/org/dllearner/kb/aquisitors/BlankNodeCollector.java index 2523a311e8..e1a821f0c0 100644 --- a/components-core/src/main/java/org/dllearner/kb/aquisitors/BlankNodeCollector.java +++ b/components-core/src/main/java/org/dllearner/kb/aquisitors/BlankNodeCollector.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.aquisitors; import java.util.ArrayList; @@ -41,7 +40,7 @@ public static synchronized int getNextGlobalBNodeId(){ return ret; } - private static Map> blankNodes = new HashMap>(); + private static Map> blankNodes = new HashMap<>(); public static void addBlankNode(int id, RDFNodeTuple t){ if(blankNodes.get(id)==null){ @@ -67,7 +66,7 @@ public static Map> getBlankNodeMap(){ * @return true if there are more blanknodes */ public static boolean testResultSet(ResultSetRewindable rsw, int depth){ - List vars = new ArrayList(); + List vars = new ArrayList<>(); vars.add("o0"); for (int i = 1; i <= depth; i++) { vars.add("o"+i); @@ -125,7 +124,7 @@ public static String makeQuery(String uri, String predicate, int maxDepth){ String currentO = "?o"+currentDepth; String nextP = "?p"+(currentDepth+1); String nextO = "?o"+(currentDepth+1); - sq.append(" OPTIONAL { "+currentO+" "+nextP+" "+nextO+". }"); + sq.append(" OPTIONAL { ").append(currentO).append(" ").append(nextP).append(" ").append(nextO).append(". }"); } diff --git a/components-core/src/main/java/org/dllearner/kb/aquisitors/LinkedDataTupleAquisitor.java b/components-core/src/main/java/org/dllearner/kb/aquisitors/LinkedDataTupleAquisitor.java deleted file mode 100644 index f18ca8c8be..0000000000 --- a/components-core/src/main/java/org/dllearner/kb/aquisitors/LinkedDataTupleAquisitor.java +++ /dev/null @@ -1,71 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.kb.aquisitors; - -import java.util.SortedSet; - -import org.apache.log4j.Logger; -import org.dllearner.kb.extraction.Configuration; -import org.dllearner.utilities.datastructures.RDFNodeTuple; - -/** - * Can execute different queries. - * - * @author Sebastian Hellmann - * - */ -public class LinkedDataTupleAquisitor extends TupleAquisitor { - - @SuppressWarnings("unused") - private static Logger logger = Logger.getLogger(LinkedDataTupleAquisitor.class); - @SuppressWarnings("unused") - private Configuration configuration; - - - public LinkedDataTupleAquisitor(Configuration configuration) { - this.configuration = configuration; - } - - // standard query get a tupels (p,o) for subject s - @Override - public SortedSet retrieveTupel(String uri){ - throw new RuntimeException("Not Implemented yet"); - } - @Override - public SortedSet retrieveClassesForInstances(String uri){ - throw new RuntimeException("Not Implemented yet"); - } - @Override - public SortedSet retrieveTuplesForClassesOnly(String uri){ - throw new RuntimeException("Not Implemented yet"); - } - @Override - protected void disambiguateBlankNodes(String uri, SortedSet resultSet){ - throw new RuntimeException("Not Implemented yet"); - } - @Override - public SortedSet getBlankNode(int id){ - throw new RuntimeException("Not Implemented yet"); - } - - - - -} diff --git a/components-core/src/main/java/org/dllearner/kb/aquisitors/RDFBlankNode.java b/components-core/src/main/java/org/dllearner/kb/aquisitors/RDFBlankNode.java index e09ba32d09..158cbc60f5 100644 --- a/components-core/src/main/java/org/dllearner/kb/aquisitors/RDFBlankNode.java +++ b/components-core/src/main/java/org/dllearner/kb/aquisitors/RDFBlankNode.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.aquisitors; import com.hp.hpl.jena.graph.Node; diff --git a/components-core/src/main/java/org/dllearner/kb/aquisitors/SparqlTupleAquisitor.java b/components-core/src/main/java/org/dllearner/kb/aquisitors/SparqlTupleAquisitor.java index 4118001c69..45cd80e5c0 100644 --- a/components-core/src/main/java/org/dllearner/kb/aquisitors/SparqlTupleAquisitor.java +++ b/components-core/src/main/java/org/dllearner/kb/aquisitors/SparqlTupleAquisitor.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.aquisitors; import java.util.ArrayList; @@ -155,22 +154,23 @@ private void dissolveBlankNodes(int currentId, String uri, RDFNodeTuple tuple){ assignIds( currentId, rsw, lastDepth); }catch (Exception e) { logger.info("An error occurred while dissolving blanknodes"); + logger.debug("Error was:", e); } } //takes the resultset and assigns internal ids private void assignIds(int currentId, ResultSetRewindable rsw, int lastDepth){ //prepare variables according to last depth - List vars = new ArrayList(); + List vars = new ArrayList<>(); vars.add("o0"); for (int i = 1; i <= lastDepth; i++) { vars.add("p"+i); vars.add("o"+i); } - final List tmpVars = new ArrayList(); + final List tmpVars = new ArrayList<>(); - Map lastNodes = new HashMap(); + Map lastNodes = new HashMap<>(); // the resultset first variable is o0 // iteration over each tuple of the set while (rsw.hasNext()){ @@ -182,7 +182,6 @@ private void assignIds(int currentId, ResultSetRewindable rsw, int lastDepth){ // could be two different blank nodes here, but unlikely if(!q.get("o0").isAnon()){ lastNodes.put(q.get("o0").toString(), currentId); - continue; }else{ // remove the first node diff --git a/components-core/src/main/java/org/dllearner/kb/aquisitors/SparqlTupleAquisitorImproved.java b/components-core/src/main/java/org/dllearner/kb/aquisitors/SparqlTupleAquisitorImproved.java index 8c8988a5fe..499d20ca26 100644 --- a/components-core/src/main/java/org/dllearner/kb/aquisitors/SparqlTupleAquisitorImproved.java +++ b/components-core/src/main/java/org/dllearner/kb/aquisitors/SparqlTupleAquisitorImproved.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.aquisitors; import java.util.HashMap; @@ -44,7 +43,7 @@ public class SparqlTupleAquisitorImproved extends SparqlTupleAquisitor { private static Logger logger = Logger.getLogger(SparqlTupleAquisitorImproved.class); - private Map> resources = new HashMap>(); + private Map> resources = new HashMap<>(); int recursionDepth; @@ -147,7 +146,7 @@ private void addToLocalCache(String uri, RDFNodeTuple tuple){ if(set==null){ - set = new TreeSet(); + set = new TreeSet<>(); set.add(tuple); resources.put(uri, set ); diff --git a/components-core/src/main/java/org/dllearner/kb/aquisitors/TupleAquisitor.java b/components-core/src/main/java/org/dllearner/kb/aquisitors/TupleAquisitor.java index 32c4b12f97..fefe06edb3 100644 --- a/components-core/src/main/java/org/dllearner/kb/aquisitors/TupleAquisitor.java +++ b/components-core/src/main/java/org/dllearner/kb/aquisitors/TupleAquisitor.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.aquisitors; import java.net.URI; @@ -51,7 +50,7 @@ public abstract class TupleAquisitor { /** * TODO: this function is still used somewhere, but should be replaced - * @return + * @return whether blank nodes are dissolved */ public boolean isDissolveBlankNodes(){ return dissolveBlankNodes; @@ -73,7 +72,7 @@ public final SortedSet getTupelForResource(String uri){ }catch(Exception e){ logger.warn("Caught exception in tupleaquisitor, ignoring it: "+e.toString()); e.printStackTrace(); - return new TreeSet(); + return new TreeSet<>(); } } diff --git a/components-core/src/main/java/org/dllearner/kb/dataset/AbstractOWLOntologyDataset.java b/components-core/src/main/java/org/dllearner/kb/dataset/AbstractOWLOntologyDataset.java index e2655e0f14..3b6568fdd5 100644 --- a/components-core/src/main/java/org/dllearner/kb/dataset/AbstractOWLOntologyDataset.java +++ b/components-core/src/main/java/org/dllearner/kb/dataset/AbstractOWLOntologyDataset.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb.dataset; import java.io.File; @@ -40,10 +58,10 @@ public abstract class AbstractOWLOntologyDataset implements OWLOntologyDataset{ - protected Collection ontologies = new TreeSet(); - protected Collection correctOntologies = new TreeSet(); - protected Collection incoherentOntologies = new TreeSet(); - protected Collection inconsistentOntologies = new TreeSet(); + protected Collection ontologies = new TreeSet<>(); + protected Collection correctOntologies = new TreeSet<>(); + protected Collection incoherentOntologies = new TreeSet<>(); + protected Collection inconsistentOntologies = new TreeSet<>(); protected String name; @@ -57,9 +75,9 @@ public abstract class AbstractOWLOntologyDataset implements OWLOntologyDataset{ protected OWLReasonerFactory reasonerFactory = PelletReasonerFactory.getInstance(); OWLOntologyManager man = OWLManager.createOWLOntologyManager(); - protected Map ontologyURLs = new HashMap(); + protected Map ontologyURLs = new HashMap<>(); - private final int nrOfThreads = 1; + private static final int nrOfThreads = 1; private boolean analyze = false; public AbstractOWLOntologyDataset(String name, boolean analyze) { @@ -98,10 +116,10 @@ private boolean analyzed(URL url){ } private Set load403Errors(){ - Set errors = new HashSet(); + Set errors = new HashSet<>(); try { if(new File(directory, "403.txt").exists()){ - errors = new HashSet(Files.readLines(new File(directory, "403.txt"), Charset.defaultCharset())); + errors = new HashSet<>(Files.readLines(new File(directory, "403.txt"), Charset.defaultCharset())); } } catch (IOException e) { e.printStackTrace(); @@ -119,7 +137,7 @@ public void initialize(){ if(!analyzed){ Set errors = load403Errors(); ExecutorService threadPool = Executors.newFixedThreadPool(nrOfThreads); - List> urlList = new ArrayList>(ontologyURLs.entrySet()); + List> urlList = new ArrayList<>(ontologyURLs.entrySet()); Collections.shuffle(urlList); for (java.util.Map.Entry entry : urlList) { URL url = entry.getKey(); @@ -231,7 +249,7 @@ protected OWLOntology loadOWLOntology(URL url) { private OWLOntology loadFromLocal(URL url){ String filename = getFilename(url); - for(File parent : Arrays.asList(directory)){ + for(File parent : Collections.singletonList(directory)){ File file = new File(parent, filename); if(file.exists()){ try { @@ -281,9 +299,7 @@ protected File downloadFile(URL url){ out.close(); System.out.println("done."); return file; - } catch (MalformedURLException e) { - e.printStackTrace(); - } catch (FileNotFoundException e) { + } catch (MalformedURLException | FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); diff --git a/components-core/src/main/java/org/dllearner/kb/dataset/BioPortalDataset.java b/components-core/src/main/java/org/dllearner/kb/dataset/BioPortalDataset.java index c48d649fac..780c093a46 100644 --- a/components-core/src/main/java/org/dllearner/kb/dataset/BioPortalDataset.java +++ b/components-core/src/main/java/org/dllearner/kb/dataset/BioPortalDataset.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb.dataset; import java.io.UnsupportedEncodingException; @@ -23,9 +41,7 @@ protected void addOntologyURLs() { try { String name = URLEncoder.encode(entry.getOntologyShortName(), "UTF-8"); super.ontologyURLs.put(entry.getPhysicalURI().toURL(), name); - } catch (MalformedURLException e) { - e.printStackTrace(); - } catch (UnsupportedEncodingException e) { + } catch (MalformedURLException | UnsupportedEncodingException e) { e.printStackTrace(); } } diff --git a/components-core/src/main/java/org/dllearner/kb/dataset/OWLOntologyDataset.java b/components-core/src/main/java/org/dllearner/kb/dataset/OWLOntologyDataset.java index 98e577d0ed..a98ed6bf83 100644 --- a/components-core/src/main/java/org/dllearner/kb/dataset/OWLOntologyDataset.java +++ b/components-core/src/main/java/org/dllearner/kb/dataset/OWLOntologyDataset.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb.dataset; import java.io.File; @@ -7,7 +25,7 @@ public interface OWLOntologyDataset { - static File datasetDirectory = new File("dataset"); + File datasetDirectory = new File("dataset"); Collection loadOntologies(); Collection loadIncoherentOntologies(); diff --git a/components-core/src/main/java/org/dllearner/kb/dataset/TONESDataset.java b/components-core/src/main/java/org/dllearner/kb/dataset/TONESDataset.java index 137a655035..7d65426e03 100644 --- a/components-core/src/main/java/org/dllearner/kb/dataset/TONESDataset.java +++ b/components-core/src/main/java/org/dllearner/kb/dataset/TONESDataset.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb.dataset; import java.io.UnsupportedEncodingException; @@ -23,9 +41,7 @@ protected void addOntologyURLs() { try { String name = URLEncoder.encode(entry.getOntologyShortName(), "UTF-8"); ontologyURLs.put(entry.getPhysicalURI().toURL(), name); - } catch (MalformedURLException e) { - e.printStackTrace(); - } catch (UnsupportedEncodingException e) { + } catch (MalformedURLException | UnsupportedEncodingException e) { e.printStackTrace(); } } diff --git a/components-core/src/main/java/org/dllearner/kb/extraction/BlankNode.java b/components-core/src/main/java/org/dllearner/kb/extraction/BlankNode.java index 819b5cc3e0..6e65d0368e 100644 --- a/components-core/src/main/java/org/dllearner/kb/extraction/BlankNode.java +++ b/components-core/src/main/java/org/dllearner/kb/extraction/BlankNode.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.extraction; import java.util.ArrayList; @@ -48,9 +47,9 @@ public class BlankNode extends Node { String inboundEdge; - private List blankNodes =new ArrayList(); - private SortedSet otherNodes = new TreeSet (); - private List datatypeProperties = new ArrayList(); + private List blankNodes = new ArrayList<>(); + private SortedSet otherNodes = new TreeSet<>(); + private List datatypeProperties = new ArrayList<>(); //private List objectProperties = new ArrayList(); //private List datatypeProperties = new ArrayList(); @@ -67,7 +66,7 @@ public BlankNode(RDFBlankNode bNode, String inboundEdge){ @Override public List expand(TupleAquisitor tupleAquisitor, Manipulator manipulator) { - List newNodes = new ArrayList(); + List newNodes = new ArrayList<>(); SortedSet s = tupleAquisitor.getBlankNode(bNode.getBNodeId()); //System.out.println("entering "+bNode.getBNodeId()); @@ -100,12 +99,12 @@ public List expand(TupleAquisitor tupleAquisitor, @Override public List expandProperties(TupleAquisitor TupelAquisitor, Manipulator manipulator, boolean dissolveBlankNodes) { - return new ArrayList(); + return new ArrayList<>(); } @Override public SortedSet toNTriple() { - SortedSet returnSet = new TreeSet(); + SortedSet returnSet = new TreeSet<>(); //String subject = getNTripleForm(); /*for (OWLObjectPropertyNode one : objectProperties) { returnSet.add(subject + one.getNTripleForm() + one.getBPart().getNTripleForm()+" . "); @@ -151,8 +150,8 @@ public OWLClassExpression getAnonymousClass(OWLAPIOntologyCollector owlAPIOntolo (inboundEdge.equals(OWLVocabulary.OWL_complementOf))|| (inboundEdge.equals(OWLVocabulary.OWL_unionOf)) ){ - Set target = new HashSet(); - List tmp = new ArrayList(); + Set target = new HashSet<>(); + List tmp = new ArrayList<>(); tmp.add(this); while(!tmp.isEmpty()){ BlankNode next = tmp.remove(0); @@ -200,7 +199,7 @@ public OWLClassExpression getAnonymousClass(OWLAPIOntologyCollector owlAPIOntolo tail("more than one complement"+target); }else{ - return factory.getOWLObjectComplementOf(new ArrayList(target).remove(0)); + return factory.getOWLObjectComplementOf(new ArrayList<>(target).remove(0)); } }else{ printAll(); diff --git a/components-core/src/main/java/org/dllearner/kb/extraction/ClassNode.java b/components-core/src/main/java/org/dllearner/kb/extraction/ClassNode.java index 19b25663b3..5bdb9bfd75 100644 --- a/components-core/src/main/java/org/dllearner/kb/extraction/ClassNode.java +++ b/components-core/src/main/java/org/dllearner/kb/extraction/ClassNode.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.extraction; import java.util.ArrayList; @@ -46,9 +45,9 @@ public class ClassNode extends Node { private static Logger logger = Logger .getLogger(ClassNode.class); - List classProperties = new ArrayList(); - List datatypeProperties = new ArrayList(); - List blankNodes = new ArrayList(); + List classProperties = new ArrayList<>(); + List datatypeProperties = new ArrayList<>(); + List blankNodes = new ArrayList<>(); public ClassNode(String uri) { super(uri); @@ -62,7 +61,7 @@ public List expand(TupleAquisitor tupelAquisitor, Manipulator manipulator) // see manipulator newTuples = manipulator.manipulate(this, newTuples); - List newNodes = new ArrayList(); + List newNodes = new ArrayList<>(); Node tmp; for (RDFNodeTuple tuple : newTuples) { if((tmp = processTuple(tuple,tupelAquisitor.isDissolveBlankNodes()))!= null) { @@ -114,7 +113,7 @@ private Node processTuple( RDFNodeTuple tuple, boolean dissolveBlankNodes) { // gets the types for properties recursively @Override public List expandProperties(TupleAquisitor tupelAquisitor, Manipulator manipulator, boolean dissolveBlankNodes) { - return new ArrayList(); + return new ArrayList<>(); } @@ -126,7 +125,7 @@ public List expandProperties(TupleAquisitor tupelAquisitor, Manipula */ @Override public SortedSet toNTriple() { - SortedSet returnSet = new TreeSet(); + SortedSet returnSet = new TreeSet<>(); String subject = getNTripleForm(); returnSet.add(subject+"<" + OWLVocabulary.RDF_TYPE + "><" + OWLVocabulary.OWL_CLASS + ">."); diff --git a/components-core/src/main/java/org/dllearner/kb/extraction/Configuration.java b/components-core/src/main/java/org/dllearner/kb/extraction/Configuration.java index b4c82b1e96..2ba0aedfff 100644 --- a/components-core/src/main/java/org/dllearner/kb/extraction/Configuration.java +++ b/components-core/src/main/java/org/dllearner/kb/extraction/Configuration.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.extraction; import org.dllearner.kb.aquisitors.TupleAquisitor; diff --git a/components-core/src/main/java/org/dllearner/kb/extraction/DatatypePropertyNode.java b/components-core/src/main/java/org/dllearner/kb/extraction/DatatypePropertyNode.java index 24376362da..18b778d7a3 100644 --- a/components-core/src/main/java/org/dllearner/kb/extraction/DatatypePropertyNode.java +++ b/components-core/src/main/java/org/dllearner/kb/extraction/DatatypePropertyNode.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.extraction; import java.util.ArrayList; @@ -47,9 +46,9 @@ public class DatatypePropertyNode extends PropertyNode { // specialtypes like owl:symmetricproperty - private SortedSet specialTypes = new TreeSet(); - private SortedSet propertyInformation = new TreeSet(); - private List blankNodes = new ArrayList(); + private SortedSet specialTypes = new TreeSet<>(); + private SortedSet propertyInformation = new TreeSet<>(); + private List blankNodes = new ArrayList<>(); public DatatypePropertyNode(String uri, Node a, LiteralNode b) { super(uri, a, b); @@ -65,7 +64,7 @@ public List expand(TupleAquisitor tupelAquisitor, Manipulator manipulator) // gets the types for properties recursively @Override public List expandProperties(TupleAquisitor tupelAquisitor, Manipulator manipulator, boolean dissolveBlankNodes) { - List ret = new ArrayList(); + List ret = new ArrayList<>(); //ret.addAll(b.expandProperties(tupelAquisitor, manipulator)); SortedSet newTypes = tupelAquisitor.getTupelForResource(uri); @@ -110,7 +109,7 @@ public String getNTripleFormOfB() { @Override public SortedSet toNTriple() { - SortedSet s = new TreeSet(); + SortedSet s = new TreeSet<>(); s.add(getNTripleForm()+"<" + OWLVocabulary.RDF_TYPE + "><" + OWLVocabulary.OWL_DATATYPPROPERTY + ">."); diff --git a/components-core/src/main/java/org/dllearner/kb/extraction/ExtractionAlgorithm.java b/components-core/src/main/java/org/dllearner/kb/extraction/ExtractionAlgorithm.java index 144486fb69..52790efdb0 100644 --- a/components-core/src/main/java/org/dllearner/kb/extraction/ExtractionAlgorithm.java +++ b/components-core/src/main/java/org/dllearner/kb/extraction/ExtractionAlgorithm.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.extraction; import java.util.ArrayList; @@ -40,7 +39,7 @@ public class ExtractionAlgorithm { private Configuration configuration; - private SortedSet alreadyQueriedSuperClasses = new TreeSet(); + private SortedSet alreadyQueriedSuperClasses = new TreeSet<>(); private boolean stop = false; @@ -70,7 +69,7 @@ private Node getFirstNode(String uri) { @SuppressWarnings("unused") private List expandAll(String[] uris, TupleAquisitor tupelAquisitor) { - List nodeList = new ArrayList(); + List nodeList = new ArrayList<>(); for (String oneURI : uris) { nodeList.add(expandNode(oneURI, tupelAquisitor)); } @@ -91,9 +90,9 @@ public Node expandNode(String uri, TupleAquisitor tupleAquisitor) { } Node seedNode = getFirstNode(uri); - List newNodes = new ArrayList(); - List collectNodes = new ArrayList(); - List tmp = new ArrayList(); + List newNodes = new ArrayList<>(); + List collectNodes = new ArrayList<>(); + List tmp = new ArrayList<>(); logger.info("Seed Node: "+seedNode); @@ -178,7 +177,7 @@ public Node expandNode(String uri, TupleAquisitor tupleAquisitor) { } private List expandBlankNodes(List blankNodes, TupleAquisitor tupelAquisitor) { - List newNodes = new ArrayList(); + List newNodes = new ArrayList<>(); while (!blankNodes.isEmpty()&& !stopCondition()) { Node next = blankNodes.remove(0); List l = next.expand(tupelAquisitor, configuration.getManipulator()); @@ -193,7 +192,7 @@ private List expandBlankNodes(List blankNodes, TupleAquisitor t private List expandCloseAfterRecursion(List instanceNodes, TupleAquisitor tupelAquisitor) { - List newNodes = new ArrayList(); + List newNodes = new ArrayList<>(); tupelAquisitor.setNextTaskToClassesForInstances(); while (!instanceNodes.isEmpty() && !stopCondition()) { logger.trace("Getting classes for remaining instances: " @@ -216,7 +215,7 @@ private List expandCloseAfterRecursion(List instanceNodes, T private void expandAllSuperClassesOfANode(List allClassNodes, TupleAquisitor tupelAquisitor) { - List newClasses = new ArrayList(); + List newClasses = new ArrayList<>(); newClasses.addAll(allClassNodes); //TODO LinkedData incompatibility @@ -228,9 +227,9 @@ private void expandAllSuperClassesOfANode(List allClassNodes, TupleAq logger.trace("Getting Superclasses for: " + next); - if (!alreadyQueriedSuperClasses.contains(next.getURIString().toString())) { + if (!alreadyQueriedSuperClasses.contains(next.getURIString())) { logger.trace("" + next+" not in cache retrieving"); - alreadyQueriedSuperClasses.add(next.getURIString().toString()); + alreadyQueriedSuperClasses.add(next.getURIString()); tupelAquisitor.setNextTaskToClassInformation(); newClasses.addAll(next.expand(tupelAquisitor, configuration.getManipulator())); @@ -254,7 +253,7 @@ private void expandAllSuperClassesOfANode(List allClassNodes, TupleAq } private static List getClassNodes(List l ){ - List retList = new ArrayList(); + List retList = new ArrayList<>(); for (Node node : l) { if (node instanceof ClassNode) { retList.add( (ClassNode) node); @@ -267,7 +266,7 @@ private static List getClassNodes(List l ){ private static List getInstanceNodes(List l ){ - List retList = new ArrayList(); + List retList = new ArrayList<>(); for (Node node : l) { if (node instanceof InstanceNode) { retList.add( (InstanceNode) node); @@ -279,7 +278,7 @@ private static List getInstanceNodes(List l ){ } private static List getBlankNodes(List l ){ - List retList = new ArrayList(); + List retList = new ArrayList<>(); for (Node node : l) { if (node instanceof BlankNode) { retList.add( (BlankNode) node); @@ -291,7 +290,7 @@ private static List getBlankNodes(List l ){ } private static List getObjectPropertyNodes(List l ){ - List properties = new ArrayList(); + List properties = new ArrayList<>(); for (Node node : l) { if (node instanceof InstanceNode) { properties.addAll(( (InstanceNode) node).getObjectProperties()); @@ -303,7 +302,7 @@ private static List getObjectPropertyNodes(List l ){ } private static List getDatatypeProperties(List l ){ - List properties = new ArrayList(); + List properties = new ArrayList<>(); for (Node node : l) { if (node instanceof InstanceNode) { properties.addAll(( (InstanceNode) node).getDatatypePropertyNode()); diff --git a/components-core/src/main/java/org/dllearner/kb/extraction/InstanceNode.java b/components-core/src/main/java/org/dllearner/kb/extraction/InstanceNode.java index 8fdd1ff441..fd2a140fa5 100644 --- a/components-core/src/main/java/org/dllearner/kb/extraction/InstanceNode.java +++ b/components-core/src/main/java/org/dllearner/kb/extraction/InstanceNode.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.extraction; import java.net.URI; @@ -52,10 +51,10 @@ public class InstanceNode extends Node { private static Logger logger = Logger .getLogger(InstanceNode.class); - private List classes = new ArrayList(); + private List classes = new ArrayList<>(); //SortedSet datatypes = new TreeSet(); - private List objectProperties = new ArrayList(); - private List datatypeProperties = new ArrayList(); + private List objectProperties = new ArrayList<>(); + private List datatypeProperties = new ArrayList<>(); public InstanceNode(String uri) { super(uri); @@ -69,7 +68,7 @@ public List expand(TupleAquisitor tupelAquisitor, Manipulator manipulator) // see Manipulator newTuples = manipulator.manipulate(this, newTuples); - List newNodes = new ArrayList(); + List newNodes = new ArrayList<>(); Node tmp; for (RDFNodeTuple tuple : newTuples) { @@ -145,7 +144,7 @@ public Node processTuple( RDFNodeTuple tuple) { // gets the types for properties recursively @Override public List expandProperties(TupleAquisitor tupelAquisitor, Manipulator manipulator, boolean dissolveBlankNodes) { - List ret = new ArrayList(); + List ret = new ArrayList<>(); for (ObjectPropertyNode one : objectProperties) { ret.addAll(one.expandProperties(tupelAquisitor, manipulator, dissolveBlankNodes)); } @@ -159,7 +158,7 @@ public List expandProperties(TupleAquisitor tupelAquisitor, Manipulat @Override public SortedSet toNTriple() { - SortedSet returnSet = new TreeSet(); + SortedSet returnSet = new TreeSet<>(); returnSet.add("<" + uri + "><" + OWLVocabulary.RDF_TYPE + "><" + OWLVocabulary.OWL_THING + ">."); for (ClassNode one : classes) { returnSet.add("<" + uri + "><" + OWLVocabulary.RDF_TYPE + "><" + one.getURIString() + ">."); @@ -201,7 +200,7 @@ public void toOWLOntology( OWLAPIOntologyCollector owlAPIOntologyCollector){ if(one.getURIString().equals(OWLVocabulary.OWL_DIFFERENT_FROM)){ OWLIndividual o = factory.getOWLNamedIndividual(one.getBPart().getIRI()); - ax = factory.getOWLDifferentIndividualsAxiom(new OWLIndividual[]{me,o}); + ax = factory.getOWLDifferentIndividualsAxiom(me, o); }else{ //create axiom diff --git a/components-core/src/main/java/org/dllearner/kb/extraction/LiteralNode.java b/components-core/src/main/java/org/dllearner/kb/extraction/LiteralNode.java index 39e3233320..aa5938678d 100644 --- a/components-core/src/main/java/org/dllearner/kb/extraction/LiteralNode.java +++ b/components-core/src/main/java/org/dllearner/kb/extraction/LiteralNode.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.extraction; import java.util.ArrayList; @@ -63,7 +62,7 @@ public Literal getLiteral(){ // expands all directly connected nodes @Override public List expand(TupleAquisitor tupelAquisitor, Manipulator manipulator) { - return new ArrayList(); + return new ArrayList<>(); } @@ -71,12 +70,12 @@ public List expand(TupleAquisitor tupelAquisitor, Manipulator manipulator) // gets the types for properties recursively @Override public List expandProperties(TupleAquisitor tupelAquisitor, Manipulator manipulator, boolean dissolveBlankNodes) { - return new ArrayList(); + return new ArrayList<>(); } @Override public SortedSet toNTriple() { - return new TreeSet(); + return new TreeSet<>(); } @@ -104,33 +103,15 @@ public void toOWLOntology( OWLAPIOntologyCollector owlAPIOntologyCollector){ public boolean isDouble(){ try{ - if(l.getDatatypeURI().contains("double")){return true;} - else{return false;} - - //l.getFloat(); - - //l.getDouble(); - //return true; - }catch (Exception e) { - return false; - }/* - try{ - l.getDouble(); - return true; + return l.getDatatypeURI() != null && l.getDatatypeURI().contains("double"); }catch (Exception e) { return false; - }*/ + } } public boolean isFloat(){ try{ - if(l.getDatatypeURI().contains("float")){return true;} - else{return false;} - - //l.getFloat(); - - //l.getDouble(); - //return true; + return l.getDatatypeURI() != null && l.getDatatypeURI().contains("float"); }catch (Exception e) { return false; } @@ -138,45 +119,18 @@ public boolean isFloat(){ public boolean isInt(){ try{ - if(l.getDatatypeURI().contains("int")){return true;} - else{return false;} - - //l.getFloat(); - - //l.getDouble(); - //return true; + return l.getDatatypeURI() != null && l.getDatatypeURI().contains("int"); }catch (Exception e) { return false; } - - - /*try{ - l.getInt(); - return true; - }catch (Exception e) { - return false; - }*/ } public boolean isBoolean(){ try{ - if(l.getDatatypeURI().contains("boolean")){return true;} - else{return false;} - - //l.getFloat(); - - //l.getDouble(); - //return true; + return l.getDatatypeURI() != null && l.getDatatypeURI().contains("boolean"); }catch (Exception e) { return false; } - - /*try{ - l.getBoolean(); - return true; - }catch (Exception e) { - return false; - }*/ } public boolean isString(){ diff --git a/components-core/src/main/java/org/dllearner/kb/extraction/Manager.java b/components-core/src/main/java/org/dllearner/kb/extraction/Manager.java index dc8ee5bc28..723d586c9c 100644 --- a/components-core/src/main/java/org/dllearner/kb/extraction/Manager.java +++ b/components-core/src/main/java/org/dllearner/kb/extraction/Manager.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.extraction; import java.net.MalformedURLException; @@ -46,7 +45,7 @@ public class Manager { private Configuration configuration; private ExtractionAlgorithm extractionAlgorithm; private int nrOfExtractedTriples = 0; - private List seedNodes = new ArrayList(); + private List seedNodes = new ArrayList<>(); private boolean stop = false; private ProgressMonitor mon; @@ -90,7 +89,7 @@ private void reset(){ public List extract(Set instances) { - List allExtractedNodes = new ArrayList(); + List allExtractedNodes = new ArrayList<>(); logger.info("Start extracting "+instances.size() + " instances "); if(mon != null){ mon.setNote("Start extracting "+instances.size() + " instances "); @@ -151,7 +150,7 @@ public String getNTripleForAllExtractedNodes(){ } public String getNTripleForNodes(List nodes){ - SortedSet tripleCollector = new TreeSet(); + SortedSet tripleCollector = new TreeSet<>(); for (Node n : nodes) { tripleCollector.addAll(n.toNTriple()); } @@ -160,7 +159,7 @@ public String getNTripleForNodes(List nodes){ Object[] arr = tripleCollector.toArray(); nrOfExtractedTriples = arr.length; for (int i = 0; i < arr.length; i++) { - nt.append((String) arr[i] + "\n"); + nt.append((String) arr[i]).append("\n"); if (i % 1000 == 0) logger.info(i + " of " + arr.length + " triples done"); } diff --git a/components-core/src/main/java/org/dllearner/kb/extraction/Node.java b/components-core/src/main/java/org/dllearner/kb/extraction/Node.java index 7ea38b2e0b..e166ea49b6 100644 --- a/components-core/src/main/java/org/dllearner/kb/extraction/Node.java +++ b/components-core/src/main/java/org/dllearner/kb/extraction/Node.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.extraction; import java.util.List; @@ -54,7 +53,7 @@ public Node(String uri) { * Nodes are expanded with a certain context, given by the typedSparqlQuery * and the manipulator * - * @param manipulator + * @param manipulator the manipulator * @return Vector all Nodes that are new because of expansion */ public abstract List expand( @@ -63,7 +62,7 @@ public abstract List expand( /** * gets type defs for properties like rdf:type SymmetricProperties * - * @param manipulator + * @param manipulator the manipulator */ public abstract List expandProperties( TupleAquisitor TupelAquisitor, Manipulator manipulator, boolean dissolveBlankNodes); diff --git a/components-core/src/main/java/org/dllearner/kb/extraction/OWLAPIOntologyCollector.java b/components-core/src/main/java/org/dllearner/kb/extraction/OWLAPIOntologyCollector.java index 5495f31077..73d28f0516 100644 --- a/components-core/src/main/java/org/dllearner/kb/extraction/OWLAPIOntologyCollector.java +++ b/components-core/src/main/java/org/dllearner/kb/extraction/OWLAPIOntologyCollector.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,23 +16,15 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.extraction; -import java.io.File; - import org.apache.log4j.Logger; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.model.AddAxiom; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyChangeException; -import org.semanticweb.owlapi.model.OWLOntologyCreationException; -import org.semanticweb.owlapi.model.OWLOntologyManager; +import org.semanticweb.owlapi.model.*; import org.semanticweb.owlapi.util.SimpleIRIMapper; +import java.io.File; + public class OWLAPIOntologyCollector { private static Logger logger = Logger.getLogger(OWLAPIOntologyCollector.class); @@ -53,7 +45,7 @@ public OWLAPIOntologyCollector(String ontologyIRI, String physicalIRI){ this.ontologyIRI = IRI.create(ontologyIRI); this.physicalIRI = IRI.create(new File(physicalIRI)); SimpleIRIMapper mapper = new SimpleIRIMapper(this.ontologyIRI, this.physicalIRI); - this.manager.addIRIMapper(mapper); + this.manager.getIRIMappers().add(mapper); try{ this.currentOntology = manager.createOntology(this.ontologyIRI); }catch(OWLOntologyCreationException e){ diff --git a/components-core/src/main/java/org/dllearner/kb/extraction/ObjectPropertyNode.java b/components-core/src/main/java/org/dllearner/kb/extraction/ObjectPropertyNode.java index 4e80b3e9ab..fce811b884 100644 --- a/components-core/src/main/java/org/dllearner/kb/extraction/ObjectPropertyNode.java +++ b/components-core/src/main/java/org/dllearner/kb/extraction/ObjectPropertyNode.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.extraction; import java.util.ArrayList; @@ -52,9 +51,9 @@ public class ObjectPropertyNode extends PropertyNode { // specialtypes like owl:symmetricproperty - private SortedSet specialTypes = new TreeSet(); - private SortedSet propertyInformation = new TreeSet(); - private List blankNodes = new ArrayList(); + private SortedSet specialTypes = new TreeSet<>(); + private SortedSet propertyInformation = new TreeSet<>(); + private List blankNodes = new ArrayList<>(); public ObjectPropertyNode(String propertyURI, Node a, Node b) { super(propertyURI, a, b); @@ -70,7 +69,7 @@ public List expand(TupleAquisitor tupelAquisitor, Manipulator manipulator) // gets the types for properties recursively @Override public List expandProperties(TupleAquisitor tupelAquisitor, Manipulator manipulator, boolean dissolveBlankNodes) { - List ret = new ArrayList(); + List ret = new ArrayList<>(); ret.addAll(b.expandProperties(tupelAquisitor, manipulator, dissolveBlankNodes)); SortedSet newTypes = tupelAquisitor.getTupelForResource(uri); for (RDFNodeTuple tuple : newTypes) { @@ -108,7 +107,7 @@ public List expandProperties(TupleAquisitor tupelAquisitor, Manipulat @Override public SortedSet toNTriple() { - SortedSet s = new TreeSet(); + SortedSet s = new TreeSet<>(); s.add(getNTripleForm()+"<" + OWLVocabulary.RDF_TYPE + "><" + OWLVocabulary.OWL_OBJECTPROPERTY + ">."); for (String one : specialTypes) { @@ -147,7 +146,7 @@ public void toOWLOntology( OWLAPIOntologyCollector owlAPIOntologyCollector){ owlAPIOntologyCollector.addAxiom(factory.getOWLInverseObjectPropertiesAxiom(me, p)); }else if(one.aPartContains(OWLVocabulary.OWL_equivalentProperty)){ OWLObjectProperty p = factory.getOWLObjectProperty(IRI.create(one.b.toString())); - Set tmp = new HashSet(); + Set tmp = new HashSet<>(); tmp.add(me);tmp.add(p); owlAPIOntologyCollector.addAxiom(factory.getOWLEquivalentObjectPropertiesAxiom(tmp)); @@ -165,17 +164,23 @@ public void toOWLOntology( OWLAPIOntologyCollector owlAPIOntologyCollector){ } for (String one : specialTypes) { - - if(one.equals(OWLVocabulary.OWL_FunctionalProperty)){ - owlAPIOntologyCollector.addAxiom(factory.getOWLFunctionalObjectPropertyAxiom(me)); - }else if(one.equals(OWLVocabulary.OWL_InverseFunctionalProperty)){ - owlAPIOntologyCollector.addAxiom(factory.getOWLInverseFunctionalObjectPropertyAxiom(me)); - }else if(one.equals(OWLVocabulary.OWL_TransitiveProperty)){ - owlAPIOntologyCollector.addAxiom(factory.getOWLTransitiveObjectPropertyAxiom(me)); - }else if(one.equals(OWLVocabulary.OWL_SymmetricProperty)){ - owlAPIOntologyCollector.addAxiom(factory.getOWLSymmetricObjectPropertyAxiom(me)); - }else{ - tail("conversion to ontology: special types: " + one); + + switch (one) { + case OWLVocabulary.OWL_FunctionalProperty: + owlAPIOntologyCollector.addAxiom(factory.getOWLFunctionalObjectPropertyAxiom(me)); + break; + case OWLVocabulary.OWL_InverseFunctionalProperty: + owlAPIOntologyCollector.addAxiom(factory.getOWLInverseFunctionalObjectPropertyAxiom(me)); + break; + case OWLVocabulary.OWL_TransitiveProperty: + owlAPIOntologyCollector.addAxiom(factory.getOWLTransitiveObjectPropertyAxiom(me)); + break; + case OWLVocabulary.OWL_SymmetricProperty: + owlAPIOntologyCollector.addAxiom(factory.getOWLSymmetricObjectPropertyAxiom(me)); + break; + default: + tail("conversion to ontology: special types: " + one); + break; } } for (BlankNode bn : blankNodes) { diff --git a/components-core/src/main/java/org/dllearner/kb/extraction/PropertyNode.java b/components-core/src/main/java/org/dllearner/kb/extraction/PropertyNode.java index f388c534f2..1769b4625d 100644 --- a/components-core/src/main/java/org/dllearner/kb/extraction/PropertyNode.java +++ b/components-core/src/main/java/org/dllearner/kb/extraction/PropertyNode.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.extraction; import org.apache.log4j.Logger; diff --git a/components-core/src/main/java/org/dllearner/kb/manipulator/AddAllStringsAsClasses.java b/components-core/src/main/java/org/dllearner/kb/manipulator/AddAllStringsAsClasses.java index 9f2e91aedc..316410954e 100644 --- a/components-core/src/main/java/org/dllearner/kb/manipulator/AddAllStringsAsClasses.java +++ b/components-core/src/main/java/org/dllearner/kb/manipulator/AddAllStringsAsClasses.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.manipulator; import java.net.URLEncoder; @@ -36,9 +35,8 @@ public class AddAllStringsAsClasses extends Rule{ /** - * @param month + * @param month the month * @param resourceNamespace ns for the created uris - * @param limit does not convert strings that are longer than a specific value, zero means convert all */ public AddAllStringsAsClasses(Months month, String resourceNamespace) { super(month); @@ -54,7 +52,7 @@ public AddAllStringsAsClasses(Months month, String resourceNamespace) { @Override public SortedSet applyRule(Node subject, SortedSet tuples){ - SortedSet keep = new TreeSet(); + SortedSet keep = new TreeSet<>(); for (RDFNodeTuple tuple : tuples) { //System.out.println(tuple); //System.exit(0); diff --git a/components-core/src/main/java/org/dllearner/kb/manipulator/DBPediaNavigatorCityLocatorRule.java b/components-core/src/main/java/org/dllearner/kb/manipulator/DBPediaNavigatorCityLocatorRule.java index f53239b533..c6a6df5eb4 100644 --- a/components-core/src/main/java/org/dllearner/kb/manipulator/DBPediaNavigatorCityLocatorRule.java +++ b/components-core/src/main/java/org/dllearner/kb/manipulator/DBPediaNavigatorCityLocatorRule.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.manipulator; import java.util.HashMap; @@ -31,7 +30,7 @@ public class DBPediaNavigatorCityLocatorRule extends Rule{ - Map map=new HashMap(); + Map map= new HashMap<>(); public DBPediaNavigatorCityLocatorRule(Months month){ @@ -53,7 +52,7 @@ public DBPediaNavigatorCityLocatorRule(Months month){ public SortedSet applyRule(Node subject, SortedSet tuples){ String uri; - if(( uri = map.get(subject.getURIString().toString()) ) == null) { + if(( uri = map.get(subject.getURIString()) ) == null) { return tuples; }else { tuples.add(new RDFNodeTuple(new ResourceImpl(OWLVocabulary.RDFS_SUBCLASS_OF), new ResourceImpl(uri))); diff --git a/components-core/src/main/java/org/dllearner/kb/manipulator/DBpediaNavigatorFilterRule.java b/components-core/src/main/java/org/dllearner/kb/manipulator/DBpediaNavigatorFilterRule.java index 67ce1e1df4..7a16e8936d 100644 --- a/components-core/src/main/java/org/dllearner/kb/manipulator/DBpediaNavigatorFilterRule.java +++ b/components-core/src/main/java/org/dllearner/kb/manipulator/DBpediaNavigatorFilterRule.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.manipulator; import java.util.LinkedList; @@ -40,7 +39,7 @@ public DBpediaNavigatorFilterRule(Months month){ public SortedSet applyRule(Node subject, SortedSet tuples){ // RDFNode clazz = null; RDFNodeTuple typeTuple = null; - List toRemove=new LinkedList(); + List toRemove= new LinkedList<>(); for (RDFNodeTuple tuple : tuples) { if (tuple.a.toString().equals(OWLVocabulary.RDF_TYPE)){ diff --git a/components-core/src/main/java/org/dllearner/kb/manipulator/DBpediaNavigatorOtherRule.java b/components-core/src/main/java/org/dllearner/kb/manipulator/DBpediaNavigatorOtherRule.java index 434002a757..95c3ad7f6c 100644 --- a/components-core/src/main/java/org/dllearner/kb/manipulator/DBpediaNavigatorOtherRule.java +++ b/components-core/src/main/java/org/dllearner/kb/manipulator/DBpediaNavigatorOtherRule.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.manipulator; import java.util.SortedSet; diff --git a/components-core/src/main/java/org/dllearner/kb/manipulator/Manipulator.java b/components-core/src/main/java/org/dllearner/kb/manipulator/Manipulator.java index ce16d3dc7d..5c6e7504a6 100644 --- a/components-core/src/main/java/org/dllearner/kb/manipulator/Manipulator.java +++ b/components-core/src/main/java/org/dllearner/kb/manipulator/Manipulator.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.manipulator; import java.util.ArrayList; @@ -43,7 +42,7 @@ public class Manipulator { @SuppressWarnings("unused") private static Logger logger = Logger.getLogger(Manipulator.class); - private List rules = new ArrayList(); + private List rules = new ArrayList<>(); private Manipulator() { } @@ -134,7 +133,7 @@ private void addDefaultRules(Months month){ public synchronized void addRule(Rule newRule){ rules.add(newRule); - List l = new ArrayList(); + List l = new ArrayList<>(); for (Months month : Rule.MONTHS) { for (Rule rule : rules) { diff --git a/components-core/src/main/java/org/dllearner/kb/manipulator/ObjectReplacementRule.java b/components-core/src/main/java/org/dllearner/kb/manipulator/ObjectReplacementRule.java index d34c3d0155..bc11fcbe35 100644 --- a/components-core/src/main/java/org/dllearner/kb/manipulator/ObjectReplacementRule.java +++ b/components-core/src/main/java/org/dllearner/kb/manipulator/ObjectReplacementRule.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.manipulator; import java.util.SortedSet; @@ -43,7 +42,7 @@ public ObjectReplacementRule(Months month, String oldObject, String newObject) { @Override public SortedSet applyRule(Node subject, SortedSet tuples){ - SortedSet keep = new TreeSet(); + SortedSet keep = new TreeSet<>(); for (RDFNodeTuple tuple : tuples) { if(tuple.bPartContains(oldObject)){ String tmp = tuple.b.toString().replace(oldObject, newObject); diff --git a/components-core/src/main/java/org/dllearner/kb/manipulator/PredicateReplacementRule.java b/components-core/src/main/java/org/dllearner/kb/manipulator/PredicateReplacementRule.java index 908ce81ee3..147380d148 100644 --- a/components-core/src/main/java/org/dllearner/kb/manipulator/PredicateReplacementRule.java +++ b/components-core/src/main/java/org/dllearner/kb/manipulator/PredicateReplacementRule.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.manipulator; import java.util.SortedSet; @@ -43,7 +42,7 @@ public PredicateReplacementRule(Months month, String oldPredicate, String newPre @Override public SortedSet applyRule(Node subject, SortedSet tuples){ - SortedSet keep = new TreeSet(); + SortedSet keep = new TreeSet<>(); for (RDFNodeTuple tuple : tuples) { if(tuple.aPartContains(oldPredicate)){ tuple.a = new ResourceImpl(newPredicate); diff --git a/components-core/src/main/java/org/dllearner/kb/manipulator/Rule.java b/components-core/src/main/java/org/dllearner/kb/manipulator/Rule.java index 7aba99051a..c1313525c5 100644 --- a/components-core/src/main/java/org/dllearner/kb/manipulator/Rule.java +++ b/components-core/src/main/java/org/dllearner/kb/manipulator/Rule.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.manipulator; import java.util.ArrayList; @@ -30,11 +29,11 @@ public abstract class Rule { - public static final List MONTHS = new ArrayList(Arrays.asList(Months.values())); + public static final List MONTHS = new ArrayList<>(Arrays.asList(Months.values())); public enum Months { JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, - AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER; + AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER } Months month; @@ -57,7 +56,7 @@ public static void main(String[] args) { System.out.println(Months.values()[i]); } - System.out.println(Months.values()); + System.out.println(Arrays.toString(Months.values())); } public abstract void logJamon(); diff --git a/components-core/src/main/java/org/dllearner/kb/manipulator/SimpleObjectFilterRule.java b/components-core/src/main/java/org/dllearner/kb/manipulator/SimpleObjectFilterRule.java index 25789eec70..846f606a17 100644 --- a/components-core/src/main/java/org/dllearner/kb/manipulator/SimpleObjectFilterRule.java +++ b/components-core/src/main/java/org/dllearner/kb/manipulator/SimpleObjectFilterRule.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.manipulator; import java.util.SortedSet; @@ -38,7 +37,7 @@ public SimpleObjectFilterRule(Months month, String objectFilter) { @Override public SortedSet applyRule(Node subject, SortedSet tuples){ - SortedSet keep = new TreeSet(); + SortedSet keep = new TreeSet<>(); for (RDFNodeTuple tuple : tuples) { if(!tuple.bPartContains(objectFilter)){ keep.add(tuple); diff --git a/components-core/src/main/java/org/dllearner/kb/manipulator/SimplePredicateFilterRule.java b/components-core/src/main/java/org/dllearner/kb/manipulator/SimplePredicateFilterRule.java index d0d3df4df2..b7d84bdbda 100644 --- a/components-core/src/main/java/org/dllearner/kb/manipulator/SimplePredicateFilterRule.java +++ b/components-core/src/main/java/org/dllearner/kb/manipulator/SimplePredicateFilterRule.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.manipulator; import java.util.SortedSet; @@ -39,7 +38,7 @@ public SimplePredicateFilterRule(Months month, String predicateFilter) { @Override public SortedSet applyRule(Node subject, SortedSet tuples){ - SortedSet keep = new TreeSet(); + SortedSet keep = new TreeSet<>(); for (RDFNodeTuple tuple : tuples) { if(!tuple.aPartContains(predicateFilter)){ keep.add(tuple); diff --git a/components-core/src/main/java/org/dllearner/kb/manipulator/StringToResource.java b/components-core/src/main/java/org/dllearner/kb/manipulator/StringToResource.java index ebb6eeaf21..1d54258fe6 100644 --- a/components-core/src/main/java/org/dllearner/kb/manipulator/StringToResource.java +++ b/components-core/src/main/java/org/dllearner/kb/manipulator/StringToResource.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.manipulator; import java.net.URLEncoder; @@ -37,7 +36,7 @@ public class StringToResource extends Rule{ /** - * @param month + * @param month the month * @param resourceNamespace ns for the created uris * @param limit does not convert strings that are longer than a specific value, zero means convert all */ @@ -55,7 +54,7 @@ public StringToResource(Months month, String resourceNamespace, int limit) { @Override public SortedSet applyRule(Node subject, SortedSet tuples){ - SortedSet keep = new TreeSet(); + SortedSet keep = new TreeSet<>(); for (RDFNodeTuple tuple : tuples) { // do nothing if the object contains http:// diff --git a/components-core/src/main/java/org/dllearner/kb/manipulator/TypeFilterRule.java b/components-core/src/main/java/org/dllearner/kb/manipulator/TypeFilterRule.java index 593ca2afba..896f9026d0 100644 --- a/components-core/src/main/java/org/dllearner/kb/manipulator/TypeFilterRule.java +++ b/components-core/src/main/java/org/dllearner/kb/manipulator/TypeFilterRule.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.manipulator; import java.util.SortedSet; @@ -37,7 +36,7 @@ public class TypeFilterRule extends Rule{ private String predicateFilter; private String objectFilter; private Nodes requiredNodeType; - public enum Nodes {CLASSNODE, INSTANCENODE, LITERALNODE}; + public enum Nodes {CLASSNODE, INSTANCENODE, LITERALNODE} public TypeFilterRule(Months month, String predicateFilter, String objectFilter, Nodes requiredNodeType) { @@ -51,7 +50,7 @@ public TypeFilterRule(Months month, String predicateFilter, String objectFilter, @Override public SortedSet applyRule(Node subject, SortedSet tuples){ - SortedSet keep = new TreeSet(); + SortedSet keep = new TreeSet<>(); for (RDFNodeTuple tuple : tuples) { //String a = tuple.a.toString(); //String b = tuple.b.toString(); diff --git a/components-core/src/main/java/org/dllearner/kb/repository/OntologyRepository.java b/components-core/src/main/java/org/dllearner/kb/repository/OntologyRepository.java index 26015666f3..e54ca1ef53 100644 --- a/components-core/src/main/java/org/dllearner/kb/repository/OntologyRepository.java +++ b/components-core/src/main/java/org/dllearner/kb/repository/OntologyRepository.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb.repository; import java.util.Collection; diff --git a/components-core/src/main/java/org/dllearner/kb/repository/OntologyRepositoryEntry.java b/components-core/src/main/java/org/dllearner/kb/repository/OntologyRepositoryEntry.java index ceae9e35c1..c4e8dd90fd 100644 --- a/components-core/src/main/java/org/dllearner/kb/repository/OntologyRepositoryEntry.java +++ b/components-core/src/main/java/org/dllearner/kb/repository/OntologyRepositoryEntry.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb.repository; import java.net.URI; diff --git a/components-core/src/main/java/org/dllearner/kb/repository/OntologyRepositoryManager.java b/components-core/src/main/java/org/dllearner/kb/repository/OntologyRepositoryManager.java index 8030c05d0e..a687785af2 100644 --- a/components-core/src/main/java/org/dllearner/kb/repository/OntologyRepositoryManager.java +++ b/components-core/src/main/java/org/dllearner/kb/repository/OntologyRepositoryManager.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb.repository; import java.util.ArrayList; @@ -11,7 +29,7 @@ public class OntologyRepositoryManager { private List repositories; private OntologyRepositoryManager() { - repositories = new ArrayList(); + repositories = new ArrayList<>(); } public static synchronized OntologyRepositoryManager getManager() { diff --git a/components-core/src/main/java/org/dllearner/kb/repository/bioportal/BioPortalRepository.java b/components-core/src/main/java/org/dllearner/kb/repository/bioportal/BioPortalRepository.java index 417efd5344..869404edf1 100644 --- a/components-core/src/main/java/org/dllearner/kb/repository/bioportal/BioPortalRepository.java +++ b/components-core/src/main/java/org/dllearner/kb/repository/bioportal/BioPortalRepository.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb.repository.bioportal; import java.io.IOException; @@ -20,12 +38,12 @@ public class BioPortalRepository implements OntologyRepository { private static final Logger log = Logger.getLogger(BioPortalRepository.class); - private String apiKey = "8fadfa2c-47de-4487-a1f5-b7af7378d693"; - private String serviceURL = "http://rest.bioontology.org/bioportal/ontologies"; + private static final String apiKey = "8fadfa2c-47de-4487-a1f5-b7af7378d693"; + private static final String serviceURL = "http://rest.bioontology.org/bioportal/ontologies"; private boolean initialized = false; - private List entries = new ArrayList(); + private List entries = new ArrayList<>(); @Override public String getName() { @@ -70,7 +88,7 @@ private void fillRepository(){ return; } List beans = success.getData().getOntologyBeans(); - entries = new ArrayList(); + entries = new ArrayList<>(); for(OntologyBean bean : beans){ URI physicalURI = URI.create(withAPIKey(serviceURL + "/download/" + bean.getId())); String shortName = bean.getDisplayLabel(); diff --git a/components-core/src/main/java/org/dllearner/kb/repository/bioportal/Data.java b/components-core/src/main/java/org/dllearner/kb/repository/bioportal/Data.java index 89a5daa2fe..6fea536e57 100644 --- a/components-core/src/main/java/org/dllearner/kb/repository/bioportal/Data.java +++ b/components-core/src/main/java/org/dllearner/kb/repository/bioportal/Data.java @@ -1,10 +1,28 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb.repository.bioportal; import java.util.ArrayList; import java.util.List; public class Data { - List list = new ArrayList(); + List list = new ArrayList<>(); public void setOntologyBeans(List list) { this.list = list; diff --git a/components-core/src/main/java/org/dllearner/kb/repository/bioportal/OntologyAclEntry.java b/components-core/src/main/java/org/dllearner/kb/repository/bioportal/OntologyAclEntry.java index b5d69e3d10..6100ea636a 100644 --- a/components-core/src/main/java/org/dllearner/kb/repository/bioportal/OntologyAclEntry.java +++ b/components-core/src/main/java/org/dllearner/kb/repository/bioportal/OntologyAclEntry.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb.repository.bioportal; public class OntologyAclEntry { diff --git a/components-core/src/main/java/org/dllearner/kb/repository/bioportal/OntologyBean.java b/components-core/src/main/java/org/dllearner/kb/repository/bioportal/OntologyBean.java index 9e6420fe23..2276c0ea1b 100644 --- a/components-core/src/main/java/org/dllearner/kb/repository/bioportal/OntologyBean.java +++ b/components-core/src/main/java/org/dllearner/kb/repository/bioportal/OntologyBean.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb.repository.bioportal; import java.util.ArrayList; @@ -23,7 +41,7 @@ public class OntologyBean implements Comparable { private Integer id; private Integer ontologyId; // virtual view id(s) on the virtual ontology - private List virtualViewIds = new ArrayList(0); + private List virtualViewIds = new ArrayList<>(0); private Integer internalVersionNumber; private List userIds; private String versionNumber; @@ -63,18 +81,18 @@ public class OntologyBean implements Comparable { private String viewingRestriction; - private List userAcl = new ArrayList(0); + private List userAcl = new ArrayList<>(0); private boolean isView = false; // category id(s) - private List categoryIds = new ArrayList(0); + private List categoryIds = new ArrayList<>(0); // group id(s) - private List groupIds = new ArrayList(0); + private List groupIds = new ArrayList<>(0); // file name(s) - private List filenames = new ArrayList(0); + private List filenames = new ArrayList<>(0); // source fileItem // private FileItem fileItem; @@ -86,10 +104,10 @@ public class OntologyBean implements Comparable { private String downloadLocation; // views on this ontology version - private List hasViews = new ArrayList(0); + private List hasViews = new ArrayList<>(0); // view specific properties - private List viewOnOntologyVersionId = new ArrayList(0); + private List viewOnOntologyVersionId = new ArrayList<>(0); private String viewDefinition; private String viewDefinitionLanguage; private String viewGenerationEngine; diff --git a/components-core/src/main/java/org/dllearner/kb/repository/bioportal/Success.java b/components-core/src/main/java/org/dllearner/kb/repository/bioportal/Success.java index ccf53f26ae..1769ca5abe 100644 --- a/components-core/src/main/java/org/dllearner/kb/repository/bioportal/Success.java +++ b/components-core/src/main/java/org/dllearner/kb/repository/bioportal/Success.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb.repository.bioportal; public class Success { diff --git a/components-core/src/main/java/org/dllearner/kb/repository/bioportal/UserAcl.java b/components-core/src/main/java/org/dllearner/kb/repository/bioportal/UserAcl.java index 719a2d9b8e..ca1fed79b4 100644 --- a/components-core/src/main/java/org/dllearner/kb/repository/bioportal/UserAcl.java +++ b/components-core/src/main/java/org/dllearner/kb/repository/bioportal/UserAcl.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb.repository.bioportal; import java.util.List; diff --git a/components-core/src/main/java/org/dllearner/kb/repository/bioportal/UserEntry.java b/components-core/src/main/java/org/dllearner/kb/repository/bioportal/UserEntry.java index eaca80a3a4..076b371d41 100644 --- a/components-core/src/main/java/org/dllearner/kb/repository/bioportal/UserEntry.java +++ b/components-core/src/main/java/org/dllearner/kb/repository/bioportal/UserEntry.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb.repository.bioportal; public class UserEntry { diff --git a/components-core/src/main/java/org/dllearner/kb/repository/oxford/OxfordRepository.java b/components-core/src/main/java/org/dllearner/kb/repository/oxford/OxfordRepository.java index 09f8b57d93..656a9517e7 100644 --- a/components-core/src/main/java/org/dllearner/kb/repository/oxford/OxfordRepository.java +++ b/components-core/src/main/java/org/dllearner/kb/repository/oxford/OxfordRepository.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb.repository.oxford; import java.net.URI; @@ -23,13 +41,13 @@ public class OxfordRepository implements OntologyRepository{ private List entries; - int numberOfEntries = 793; + int numberOfEntries = 797; DecimalFormat df = new DecimalFormat("00000"); public OxfordRepository() { - entries = new ArrayList(); + entries = new ArrayList<>(); } @Override @@ -38,29 +56,34 @@ public void initialize() { } - public String getName() { + @Override + public String getName() { return repositoryName; } - public String getLocation() { + @Override + public String getLocation() { return repositoryLocation.toString(); } - public void refresh() { + @Override + public void refresh() { fillRepository(); } - public Collection getEntries() { - List ret = new ArrayList(); + @Override + public Collection getEntries() { + List ret = new ArrayList<>(); ret.addAll(entries); return ret; } - public List getMetaDataKeys() { + @Override + public List getMetaDataKeys() { return Collections.emptyList(); } @@ -97,22 +120,26 @@ public RepositoryEntry(URI ontologyIRI) { } - public String getOntologyShortName() { + @Override + public String getOntologyShortName() { return shortName; } - public URI getOntologyURI() { + @Override + public URI getOntologyURI() { return ontologyURI; } - public URI getPhysicalURI() { + @Override + public URI getPhysicalURI() { return physicalURI; } - public String getMetaData(Object key) { + @Override + public String getMetaData(Object key) { return null; } diff --git a/components-core/src/main/java/org/dllearner/kb/repository/tones/TONESRepository.java b/components-core/src/main/java/org/dllearner/kb/repository/tones/TONESRepository.java index 3a62a53173..1f41fa25cb 100644 --- a/components-core/src/main/java/org/dllearner/kb/repository/tones/TONESRepository.java +++ b/components-core/src/main/java/org/dllearner/kb/repository/tones/TONESRepository.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb.repository.tones; import java.io.BufferedReader; @@ -31,7 +49,7 @@ public class TONESRepository implements OntologyRepository{ public TONESRepository() { - entries = new ArrayList(); + entries = new ArrayList<>(); iriMapper = new RepositoryIRIMapper(); } @@ -57,7 +75,7 @@ public void refresh() { public Collection getEntries() { - List ret = new ArrayList(); + List ret = new ArrayList<>(); ret.addAll(entries); return ret; } diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/AbstractSampleGenerator.java b/components-core/src/main/java/org/dllearner/kb/sparql/AbstractSampleGenerator.java index 77dfb0aa3d..fd6c9c04cb 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/AbstractSampleGenerator.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/AbstractSampleGenerator.java @@ -1,8 +1,24 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.kb.sparql; +import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -16,9 +32,14 @@ import com.google.common.collect.Lists; import com.google.common.collect.Sets; +import com.hp.hpl.jena.datatypes.RDFDatatype; +import com.hp.hpl.jena.datatypes.xsd.XSDDatatype; import com.hp.hpl.jena.query.QueryExecution; +import com.hp.hpl.jena.rdf.model.Literal; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; +import com.hp.hpl.jena.rdf.model.Statement; +import com.hp.hpl.jena.rdf.model.StmtIterator; import com.hp.hpl.jena.vocabulary.OWL; /** @@ -59,12 +80,19 @@ public void addAllowedObjectNamespaces(Set namespaces) { cbdGen.addAllowedObjectNamespaces(namespaces); } + public void addIgnoredProperties(Set ignoredProperties) { + cbdGen.addPropertiesToIgnore(ignoredProperties); + } + + public void setLoadRelatedSchema(boolean loadRelatedSchema) { + this.loadRelatedSchema = loadRelatedSchema; + } + /** * Computes a sample of the knowledge base, i.e. it contains only facts * about the positive and negative individuals. - * @param posExamples - * @param negExamples - * @return + * @param individuals the individuals + * @return a sample ontology of the knowledge bas */ public OWLOntology getSample(Set individuals) { return OwlApiJenaUtils.getOWLOntology(getSampleModel(individuals)); @@ -93,12 +121,33 @@ protected Model getSampleModel(Set individuals) { model.add(cbd); } + StmtIterator iterator = model.listStatements(); + List toAdd = new ArrayList<>(); + while(iterator.hasNext()) { + Statement st = iterator.next(); + if(st.getObject().isLiteral()) { + Literal lit = st.getObject().asLiteral(); + RDFDatatype datatype = lit.getDatatype(); + + if(datatype != null) { + if(datatype.equals(XSDDatatype.XSDdouble) && lit.getLexicalForm().equals("NAN")) { + iterator.remove(); + toAdd.add(model.createLiteralStatement(st.getSubject(), st.getPredicate(), Double.NaN)); + } else if(datatype.equals(XSDDatatype.XSDgYear) && st.getPredicate().getURI().equals("http://dbpedia.org/ontology/birthDate")) { + iterator.remove(); + toAdd.add(model.createStatement(st.getSubject(), st.getPredicate(), model.createTypedLiteral("2000-01-01", XSDDatatype.XSDdate))); + } + } + } + } + model.add(toAdd); + // infer entity types, e.g. object or data property OWLEntityTypeAdder.addEntityTypes(model); // load related schema information if(loadRelatedSchema) { - loadRelatedSchema(model); +// loadRelatedSchema(model); } return model; diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/BlanknodeResolvingCBDGenerator.java b/components-core/src/main/java/org/dllearner/kb/sparql/BlanknodeResolvingCBDGenerator.java index 3ce7839c51..e921d66ad3 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/BlanknodeResolvingCBDGenerator.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/BlanknodeResolvingCBDGenerator.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.kb.sparql; @@ -68,13 +83,9 @@ public Model getConciseBoundedDescription(String resourceURI, int depth, boolean for(int i = 1; i <= depth; i++){ triplesTemplate.append("}"); } - - - StringBuilder queryString = new StringBuilder("prefix : "); - queryString.append("CONSTRUCT{").append(constructTemplate).append("}"); - queryString.append(" WHERE {").append(triplesTemplate).append("}"); - - ParameterizedSparqlString query = new ParameterizedSparqlString(queryString.toString()); + + + ParameterizedSparqlString query = new ParameterizedSparqlString("prefix : " + "CONSTRUCT{" + constructTemplate + "}" + " WHERE {" + triplesTemplate + "}"); query.setIri("s0", resourceURI); System.out.println(query); QueryExecution qe = qef.createQueryExecution(query.toString()); diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/Cache.java b/components-core/src/main/java/org/dllearner/kb/sparql/Cache.java index db6b25b68b..9661559bb3 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/Cache.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/Cache.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.sparql; import java.io.File; @@ -162,8 +161,8 @@ private String getHash(String string) { byte[] result = md5.digest(); StringBuffer hexString = new StringBuffer(); - for (int i = 0; i < result.length; i++) { - hexString.append(Integer.toHexString(0xFF & result[i])); + for (byte aResult : result) { + hexString.append(Integer.toHexString(0xFF & aResult)); } String str = hexString.toString(); hashTime.stop(); @@ -201,14 +200,11 @@ private String getCacheEntry(String sparqlQuery) { ObjectInputStream o = new ObjectInputStream(fos); entry = (LinkedList) o.readObject(); o.close(); - } catch (IOException e) { - e.printStackTrace(); - if(Files.debug){System.exit(0);} - } catch (ClassNotFoundException e) { + } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); if(Files.debug){System.exit(0);} } - + // TODO: we need to check whether the query is correct // (may not always be the case due to md5 hashing) @@ -241,7 +237,7 @@ private void addToCache(String sparqlQuery, String result) { long timestamp = System.currentTimeMillis(); // create the object which will be serialised - LinkedList list = new LinkedList(); + LinkedList list = new LinkedList<>(); list.add(timestamp); list.add(sparqlQuery); list.add(result); @@ -352,9 +348,9 @@ public void clearCache() { File f = new File(cacheDir); String[] files = f.list(); - for (int i = 0; i < files.length; i++) { - Files.deleteFile(new File(cacheDir+"/"+files[i])); - } + for (String file : files) { + Files.deleteFile(new File(cacheDir + "/" + file)); + } } /** diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/CachingConciseBoundedDescriptionGenerator.java b/components-core/src/main/java/org/dllearner/kb/sparql/CachingConciseBoundedDescriptionGenerator.java index 11ae8120b0..dc463b9773 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/CachingConciseBoundedDescriptionGenerator.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/CachingConciseBoundedDescriptionGenerator.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb.sparql; import java.util.HashMap; @@ -14,7 +32,7 @@ public class CachingConciseBoundedDescriptionGenerator implements ConciseBounded public CachingConciseBoundedDescriptionGenerator(ConciseBoundedDescriptionGenerator cbdGen) { this.delegatee = cbdGen; - cache = new HashMap(); + cache = new HashMap<>(); } public Model getConciseBoundedDescription(String resourceURI){ diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/ClassBasedSampleGenerator.java b/components-core/src/main/java/org/dllearner/kb/sparql/ClassBasedSampleGenerator.java index 42ae7ad700..5b5e280f40 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/ClassBasedSampleGenerator.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/ClassBasedSampleGenerator.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.kb.sparql; @@ -11,6 +26,7 @@ import java.util.TreeSet; import org.aksw.jena_sparql_api.core.QueryExecutionFactory; +import org.dllearner.kb.SparqlEndpointKS; import org.dllearner.utilities.examples.AutomaticNegativeExampleFinderSPARQL2; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLClass; @@ -31,6 +47,8 @@ */ public class ClassBasedSampleGenerator extends InstanceBasedSampleGenerator{ + private Random rnd = new Random(12345); + private int maxNrOfPosExamples = 20; private int maxNrOfNegExamples = 20; @@ -38,14 +56,16 @@ public class ClassBasedSampleGenerator extends InstanceBasedSampleGenerator{ private AutomaticNegativeExampleFinderSPARQL2 negExamplesFinder; - private Random rnd = new Random(12345); + private Set posExamples; + private Set negExamples; - public ClassBasedSampleGenerator(QueryExecutionFactory qef) { - super(qef); + public ClassBasedSampleGenerator(SparqlEndpointKS ks) { + super(ks); negExamplesFinder = new AutomaticNegativeExampleFinderSPARQL2(qef); } + /** * Computes a sample fragment of the knowledge base by using instances of the * given OWL class and also, if enabled, use some instances that do not belong to the class. @@ -53,12 +73,13 @@ public ClassBasedSampleGenerator(QueryExecutionFactory qef) { * @return a sample fragment */ public OWLOntology getSample(OWLClass cls) { - // get pos examples - Set posExamples = computePosExamples(cls); + // get positive examples + posExamples = computePosExamples(cls); - // get neg examples if enabled - Set negExamples = computeNegExamples(cls, posExamples); + // get negative examples if enabled + negExamples = computeNegExamples(cls, posExamples); + // compute sample based on positive (and negative) examples return getSample(Sets.union(posExamples, negExamples)); } @@ -69,6 +90,22 @@ public void setUseNegExamples(boolean useNegExamples) { this.useNegExamples = useNegExamples; } + /** + * @return the positive examples, i.e. instances of the class, used to + * generate the sample + */ + public Set getPositiveExamples() { + return posExamples; + } + + /** + * @return the negative examples, i.e. individuals that do not belong to the class, used to + * generate the sample + */ + public Set getNegativeExamples() { + return negExamples; + } + private Set computePosExamples(OWLClass cls) { List posExamples = new ArrayList<>(); diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/ConciseBoundedDescriptionGenerator.java b/components-core/src/main/java/org/dllearner/kb/sparql/ConciseBoundedDescriptionGenerator.java index 319fa87b89..c6411118dd 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/ConciseBoundedDescriptionGenerator.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/ConciseBoundedDescriptionGenerator.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb.sparql; import java.util.Set; @@ -39,26 +57,26 @@ public interface ConciseBoundedDescriptionGenerator { /** * @return the CBD of depth 1 for the given resource */ - public Model getConciseBoundedDescription(String resourceURI); + Model getConciseBoundedDescription(String resourceURI); /** * @return the CBD of given depth for the given resource */ - public Model getConciseBoundedDescription(String resourceURI, int depth); + Model getConciseBoundedDescription(String resourceURI, int depth); /** * @return the CBD of given depth for the given resource with optionally additional * information about the types of leaf nodes */ - public Model getConciseBoundedDescription(String resourceURI, int depth, boolean withTypesForLeafs); + Model getConciseBoundedDescription(String resourceURI, int depth, boolean withTypesForLeafs); - public void addAllowedPropertyNamespaces(Set namespaces); + void addAllowedPropertyNamespaces(Set namespaces); - public void addAllowedObjectNamespaces(Set namespaces); + void addAllowedObjectNamespaces(Set namespaces); - public void addPropertiesToIgnore(Set properties); + void addPropertiesToIgnore(Set properties); - public void setRecursionDepth(int maxRecursionDepth); + void setRecursionDepth(int maxRecursionDepth); diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/ConciseBoundedDescriptionGeneratorImpl.java b/components-core/src/main/java/org/dllearner/kb/sparql/ConciseBoundedDescriptionGeneratorImpl.java index 2fe9b4e784..27cbf903eb 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/ConciseBoundedDescriptionGeneratorImpl.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/ConciseBoundedDescriptionGeneratorImpl.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb.sparql; import java.util.Iterator; @@ -42,7 +60,7 @@ public class ConciseBoundedDescriptionGeneratorImpl implements ConciseBoundedDes private Model baseModel; private QueryExecutionFactory qef; - private Set propertyBlacklist = new TreeSet(); + private Set propertyBlacklist = new TreeSet<>(); public ConciseBoundedDescriptionGeneratorImpl(SparqlEndpoint endpoint, CacheFrontend cache) { qef = new QueryExecutionFactoryHttp(endpoint.getURL().toString(), endpoint.getDefaultGraphURIs()); @@ -123,8 +141,8 @@ public void setRecursionDepth(int maxRecursionDepth) { /** * A SPARQL CONSTRUCT query is created, to get a RDF graph for the given example with a specific recursion depth. - * @param example The example resource for which a CONSTRUCT query is created. - * @return The JENA ARQ Query object. + * @param resource The example resource for which a CONSTRUCT query is created. + * @return the SPARQL query */ private String generateQuery(String resource, int depth, boolean withTypesForLeafs){ int lastIndex = Math.max(0, depth - 1); diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/ExtractionDBCache.java b/components-core/src/main/java/org/dllearner/kb/sparql/ExtractionDBCache.java index c719621ce7..3c33e596f4 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/ExtractionDBCache.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/ExtractionDBCache.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.sparql; import java.io.ByteArrayOutputStream; @@ -102,16 +101,10 @@ public ExtractionDBCache(String cacheDir) { // create cache table if it does not exist Statement stmt = conn.createStatement(); stmt.execute("CREATE TABLE IF NOT EXISTS QUERY_CACHE(QUERYHASH BINARY PRIMARY KEY,QUERY VARCHAR(20000), TRIPLES CLOB, STORE_TIME TIMESTAMP)"); - } catch(NoSuchAlgorithmException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (ClassNotFoundException e) { + } catch(NoSuchAlgorithmException | ClassNotFoundException | SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); - } catch (SQLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + } } public void setFreshnessInMilliseconds(long freshnessInMilliseconds) { diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/InstanceBasedSampleGenerator.java b/components-core/src/main/java/org/dllearner/kb/sparql/InstanceBasedSampleGenerator.java index 8087e7b2eb..615d2846f2 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/InstanceBasedSampleGenerator.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/InstanceBasedSampleGenerator.java @@ -1,9 +1,24 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.kb.sparql; -import org.aksw.jena_sparql_api.core.QueryExecutionFactory; +import org.dllearner.kb.SparqlEndpointKS; /** * @author Lorenz Buehmann @@ -11,7 +26,7 @@ */ public class InstanceBasedSampleGenerator extends AbstractSampleGenerator{ - public InstanceBasedSampleGenerator(QueryExecutionFactory qef) { - super(qef); + public InstanceBasedSampleGenerator(SparqlEndpointKS ks) { + super(ks); } } diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/QueryEngineHTTP.java b/components-core/src/main/java/org/dllearner/kb/sparql/QueryEngineHTTP.java index 5093d25530..4a28243de4 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/QueryEngineHTTP.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/QueryEngineHTTP.java @@ -1,21 +1,21 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. * - * http://www.apache.org/licenses/LICENSE-2.0 + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ - package org.dllearner.kb.sparql; import java.io.ByteArrayInputStream; @@ -329,8 +329,8 @@ public boolean isUsingBasicAuthentication() { * Note that it may be more flexible to *

* - * @param user - * @param password + * @param user the username + * @param password the password */ public void setBasicAuthentication(String user, char[] password) { this.authenticator = new SimpleAuthenticator(user, password); @@ -426,7 +426,7 @@ public Iterator execDescribeTriples() { private Model execModel(Model model) { checkNotClosed() ; HttpQuery httpQuery = makeHttpQuery(); - httpQuery.setAccept(WebContent.contentTypeRDFXML); + httpQuery.setAccept(modelContentType); InputStream in = httpQuery.exec(); // Don't assume the endpoint actually gives back the content type we @@ -709,7 +709,7 @@ public String toString() { * Sets the Content Type for SELECT queries provided that the format is * supported * - * @param contentType + * @param contentType the content type */ public void setSelectContentType(String contentType) { boolean ok = false; @@ -729,7 +729,7 @@ public void setSelectContentType(String contentType) { * Sets the Content Type for ASK queries provided that the format is * supported * - * @param contentType + * @param contentType the content type */ public void setAskContentType(String contentType) { boolean ok = false; @@ -748,7 +748,7 @@ public void setAskContentType(String contentType) { * Sets the Content Type for CONSTRUCT/DESCRIBE queries provided that the * format is supported * - * @param contentType + * @param contentType the content type */ public void setModelContentType(String contentType) { // Check that this is a valid setting @@ -799,19 +799,19 @@ private static String initConstructContentTypes() { // Or use WebContent.defaultGraphAcceptHeader which is slightly // narrower. Here, we have a tuned setting for SPARQL operations. StringBuilder sBuff = new StringBuilder() ; - accumulateContentTypeString(sBuff, WebContent.contentTypeTurtle, 1.0); - accumulateContentTypeString(sBuff, WebContent.contentTypeNTriples, 1.0); - accumulateContentTypeString(sBuff, WebContent.contentTypeRDFXML, 0.9); - - accumulateContentTypeString(sBuff, WebContent.contentTypeTurtleAlt1, 0.8); - accumulateContentTypeString(sBuff, WebContent.contentTypeTurtleAlt2, 0.8); - - accumulateContentTypeString(sBuff, WebContent.contentTypeN3, 0.7); - accumulateContentTypeString(sBuff, WebContent.contentTypeN3Alt1, 0.6); - accumulateContentTypeString(sBuff, WebContent.contentTypeN3Alt2, 0.6); - - accumulateContentTypeString(sBuff, WebContent.contentTypeNTriplesAlt, 0.5); - accumulateContentTypeString(sBuff, "*/*", 0.1) ; +// accumulateContentTypeString(sBuff, WebContent.contentTypeTurtle, 1.0); + accumulateContentTypeString(sBuff, WebContent.contentTypeTextPlain, 1.0); +// accumulateContentTypeString(sBuff, WebContent.contentTypeRDFXML, 0.9); +// +// accumulateContentTypeString(sBuff, WebContent.contentTypeTurtleAlt1, 0.8); +// accumulateContentTypeString(sBuff, WebContent.contentTypeTurtleAlt2, 0.8); +// +// accumulateContentTypeString(sBuff, WebContent.contentTypeN3, 0.7); +// accumulateContentTypeString(sBuff, WebContent.contentTypeN3Alt1, 0.6); +// accumulateContentTypeString(sBuff, WebContent.contentTypeN3Alt2, 0.6); +// +// accumulateContentTypeString(sBuff, WebContent.contentTypeNTriplesAlt, 0.5); +// accumulateContentTypeString(sBuff, "*/*", 0.1) ; return sBuff.toString(); } diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/QueryExecutionFactoryHttp.java b/components-core/src/main/java/org/dllearner/kb/sparql/QueryExecutionFactoryHttp.java index 43080e2a37..c344ea88f5 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/QueryExecutionFactoryHttp.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/QueryExecutionFactoryHttp.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb.sparql; import java.util.ArrayList; @@ -21,7 +39,7 @@ public class QueryExecutionFactoryHttp { private String service; - private List defaultGraphs = new ArrayList(); + private List defaultGraphs = new ArrayList<>(); public QueryExecutionFactoryHttp(String service) { this(service, Collections.emptySet()); @@ -33,7 +51,7 @@ public QueryExecutionFactoryHttp(String service, String defaultGraphName) { public QueryExecutionFactoryHttp(String service, Collection defaultGraphs) { this.service = service; - this.defaultGraphs = new ArrayList(defaultGraphs); + this.defaultGraphs = new ArrayList<>(defaultGraphs); Collections.sort(this.defaultGraphs); } diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/SPARQLQueryUtils.java b/components-core/src/main/java/org/dllearner/kb/sparql/SPARQLQueryUtils.java index dd5fbfb2ef..e2430846b2 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/SPARQLQueryUtils.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/SPARQLQueryUtils.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.kb.sparql; @@ -9,7 +24,7 @@ */ public class SPARQLQueryUtils { - public static final String PREFIXES = + public static final String PREFIXES = "PREFIX owl: " + "PREFIX rdf: " + "PREFIX rdfs: "; @@ -20,7 +35,7 @@ public class SPARQLQueryUtils { public static final String SELECT_DATA_PROPERTIES_QUERY = PREFIXES + "SELECT ?var1 WHERE {?var1 a owl:DatatypeProperty .}"; public static final String SELECT_INDIVIDUALS_QUERY = PREFIXES + "SELECT ?var1 WHERE {?var1 a owl:NamedIndividual .}"; - public static final String SELECT_CLASSES_QUERY_ALT = PREFIXES + "SELECT ?var1 WHERE {[] a ?var1 .}"; + public static final String SELECT_CLASSES_QUERY_ALT = PREFIXES + "SELECT DISTINCT ?var1 WHERE {[] a ?var1 .}"; public static final String SELECT_INDIVIDUALS_QUERY_ALT = PREFIXES + "SELECT ?var1 WHERE {?var1 a [] . \n" + "OPTIONAL { ?s1 a ?var1. } \n" + "OPTIONAL { ?s2 ?var1 []. } \n" @@ -31,28 +46,28 @@ public class SPARQLQueryUtils { + "SELECT ?var1 WHERE {?var1 a owl:DatatypeProperty . ?var1 rdfs:range <%s> . }"; // class hierarchy queries - public static final String SELECT_TOP_LEVEL_OWL_CLASSES = PREFIXES + - "SELECT ?var1\n" + - "WHERE { ?var1 a owl:Class .\n" + - "FILTER ( ?var1 != owl:Thing && ?var1 != owl:Nothing ) .\n" + - "OPTIONAL { ?var1 rdfs:subClassOf ?super .\n" + - "FILTER ( ?super != owl:Thing && ?super != ?var1 && ?super != rdfs:Resource) } .\n" + + public static final String SELECT_TOP_LEVEL_OWL_CLASSES = PREFIXES + + "SELECT ?var1\n" + + "WHERE { ?var1 a owl:Class .\n" + + "FILTER ( ?var1 != owl:Thing && ?var1 != owl:Nothing ) .\n" + + "OPTIONAL { ?var1 rdfs:subClassOf ?super .\n" + + "FILTER ( ?super != owl:Thing && ?super != ?var1 && ?super != rdfs:Resource) } .\n" + "FILTER ( !BOUND(?super) ) }"; - public static final String SELECT_LEAF_CLASSES_OWL = PREFIXES + - "SELECT ?var1\n" + - "WHERE { ?var1 a owl:Class .\n" + - "FILTER ( ?var1 != owl:Thing && ?var1 != owl:Nothing ) .\n" + - "OPTIONAL { ?sub rdfs:subClassOf ?var1 .\n" + - "FILTER ( ?sub != owl:Nothing && ?sub != ?var1 ) } .\n" + + public static final String SELECT_LEAF_CLASSES_OWL = PREFIXES + + "SELECT ?var1\n" + + "WHERE { ?var1 a owl:Class .\n" + + "FILTER ( ?var1 != owl:Thing && ?var1 != owl:Nothing ) .\n" + + "OPTIONAL { ?sub rdfs:subClassOf ?var1 .\n" + + "FILTER ( ?sub != owl:Nothing && ?sub != ?var1 ) } .\n" + "FILTER ( !BOUND(?sub) ) }"; - public static final String SELECT_LEAF_CLASSES = PREFIXES + - "SELECT ?var1\n" + - "WHERE { ?var1 a owl:Class .\n" + - "FILTER ( ?var1 != owl:Thing && ?var1 != owl:Nothing ) .\n" + - "OPTIONAL { ?var1 rdfs:subClassOf ?sub .\n" + - "FILTER ( ?sub != owl:Nothing && ?sub != ?var1 ) } .\n" + + public static final String SELECT_LEAF_CLASSES = PREFIXES + + "SELECT ?var1\n" + + "WHERE { ?var1 a owl:Class .\n" + + "FILTER ( ?var1 != owl:Thing && ?var1 != owl:Nothing ) .\n" + + "OPTIONAL { ?var1 rdfs:subClassOf ?sub .\n" + + "FILTER ( ?sub != owl:Nothing && ?sub != ?var1 ) } .\n" + "FILTER ( !BOUND(?sub) ) }"; public static final String SELECT_SUPERCLASS_OF_QUERY_INF = PREFIXES + "SELECT ?var1 WHERE { " + @@ -62,28 +77,28 @@ public class SPARQLQueryUtils { "<%s> rdfs:subClassOf* ?var1 .}"; public static final String SELECT_SUBCLASS_OF_QUERY = PREFIXES + "SELECT ?var1 WHERE {?var1 rdfs:subClassOf <%s> .}"; - public static final String SELECT_DIRECT_SUBCLASS_OF_QUERY = PREFIXES + - "SELECT ?var1 {\n" + - " BIND( <%s> as ?concept )\n" + - " ?var1 rdfs:subClassOf ?concept .\n" + - " OPTIONAL {\n" + - " ?concept rdfs:subClassOf ?inbetweener .\n" + - " ?var1 rdfs:subClassOf ?inbetweener .\n" + - " FILTER( ?inbetweener != ?concept && ?inbetweener != ?var1 )\n" + - " }\n" + - " FILTER( ! BOUND(?inbetweener) && ?var1 != ?concept)\n" + + public static final String SELECT_DIRECT_SUBCLASS_OF_QUERY = PREFIXES + + "SELECT ?var1 {\n" + + " BIND( <%s> as ?concept )\n" + + " ?var1 rdfs:subClassOf ?concept .\n" + + " OPTIONAL {\n" + + " ?concept rdfs:subClassOf ?inbetweener .\n" + + " ?var1 rdfs:subClassOf ?inbetweener .\n" + + " FILTER( ?inbetweener != ?concept && ?inbetweener != ?var1 )\n" + + " }\n" + + " FILTER( ! BOUND(?inbetweener) && ?var1 != ?concept)\n" + " }"; public static final String SELECT_SUPERCLASS_OF_QUERY = PREFIXES + "SELECT ?var1 WHERE {<%s> rdfs:subClassOf ?var1 .}"; - public static final String SELECT_DIRECT_SUPERCLASS_OF_QUERY = PREFIXES + - "SELECT ?var1 {\n" + - " BIND( <%s> as ?concept )\n" + - " ?concept rdfs:subClassOf ?var1 .\n" + - " OPTIONAL {\n" + - " ?concept rdfs:subClassOf ?inbetweener .\n" + - " ?inbetweener rdfs:subClassOf ?var1 .\n" + - " FILTER( ?inbetweener != ?concept && ?inbetweener != ?var1 )\n" + - " }\n" + - " FILTER( ! BOUND(?inbetweener) && ?var1 != ?concept)\n" + + public static final String SELECT_DIRECT_SUPERCLASS_OF_QUERY = PREFIXES + + "SELECT ?var1 {\n" + + " BIND( <%s> as ?concept )\n" + + " ?concept rdfs:subClassOf ?var1 .\n" + + " OPTIONAL {\n" + + " ?concept rdfs:subClassOf ?inbetweener .\n" + + " ?inbetweener rdfs:subClassOf ?var1 .\n" + + " FILTER( ?inbetweener != ?concept && ?inbetweener != ?var1 )\n" + + " }\n" + + " FILTER( ! BOUND(?inbetweener) && ?var1 != ?concept)\n" + " }"; public static final String SELECT_EQUIVALENT_CLASSES_QUERY = PREFIXES + "SELECT ?var1 WHERE {" + "{?var1 owl:equivalentClass <%s> .} UNION {<%s> owl:equivalentClass ?var1 .}}"; diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/SPARQLTasks.java b/components-core/src/main/java/org/dllearner/kb/sparql/SPARQLTasks.java index 420cf29a23..fa36d79aee 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/SPARQLTasks.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/SPARQLTasks.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.sparql; import java.util.List; @@ -134,11 +133,11 @@ private SortedSet getRecursiveSuperOrSubClasses( // TODO check for quotes in uris int depth = maxDepth; - final SortedSet toBeRetrieved = new TreeSet(); + final SortedSet toBeRetrieved = new TreeSet<>(); toBeRetrieved.add(classURI); - final SortedSet returnSet = new TreeSet(); - final SortedSet tmpSet = new TreeSet(); + final SortedSet returnSet = new TreeSet<>(); + final SortedSet tmpSet = new TreeSet<>(); // collect super/subclasses for the depth for (; (depth > 0) && (!toBeRetrieved.isEmpty()); depth--) { @@ -177,7 +176,8 @@ private SortedSet getRecursiveSuperOrSubClasses( * conceptRewrite(String descriptionKBSyntax, SparqlEndpoint se, Cache * c, boolean simple ) * @param classURI An URI string with no quotes - * @param maxDepth + * @param maxDepth determines the depth of retrieval, if only direct subclasses are retrieved, + * 1 is HIGHLY RECOMMENDED FOR LARGE HIERARCHIES) * @return TreeSet of subclasses including classURI */ public SortedSet getSubClasses(final String classURI, @@ -406,7 +406,7 @@ public SortedSet queryAsTuple(String sparqlQueryString, String var1 @SuppressWarnings("unchecked") public SortedSet queryAsRDFNodeTuple(String sparqlQueryString, String var1, String var2) { ResultSetRewindable rsw = null; - SortedSet returnSet = new TreeSet(); + SortedSet returnSet = new TreeSet<>(); try { String jsonString = query(sparqlQueryString); @@ -471,7 +471,7 @@ public ResultSetRewindable queryAsResultSet(String sparqlQueryString) { /** * variable must be ?count - * @param sparqlQueryString + * @param sparqlQueryString the SPARQL query * @return -1 on failure count on success */ public int queryAsCount(String sparqlQueryString) { @@ -540,7 +540,7 @@ private String limit(int sparqlResultLimit) { public static SortedSet getStringSetForVariableFromResultSet( ResultSetRewindable rs, String variable) { - final SortedSet result = new TreeSet(); + final SortedSet result = new TreeSet<>(); @SuppressWarnings("unchecked") final List l = ResultSetFormatter.toList(rs); @@ -555,7 +555,7 @@ public static SortedSet getStringSetForVariableFromResultSet( private static SortedSet getTuplesFromResultSet( ResultSetRewindable rs, String predicate, String object) { - final SortedSet returnSet = new TreeSet(); + final SortedSet returnSet = new TreeSet<>(); //SimpleClock sc = new SimpleClock(); @SuppressWarnings("unchecked") final List l = ResultSetFormatter.toList(rs); @@ -663,7 +663,7 @@ public OWLEntity guessResourceType(String resource, boolean byTriples) { } public Set getAllObjectProperties() { - Set properties = new TreeSet(); + Set properties = new TreeSet<>(); String query = "PREFIX owl: SELECT ?p WHERE {?p a owl:ObjectProperty}"; SparqlQuery sq = new SparqlQuery(query, sparqlEndpoint); ResultSet q = sq.send(false); @@ -675,7 +675,7 @@ public Set getAllObjectProperties() { } public Set getAllDataProperties() { - Set properties = new TreeSet(); + Set properties = new TreeSet<>(); String query = "PREFIX owl: SELECT ?p WHERE {?p a owl:DatatypeProperty}"; SparqlQuery sq = new SparqlQuery(query, sparqlEndpoint); ResultSet q = sq.send(false); @@ -687,7 +687,7 @@ public Set getAllDataProperties() { } public Set getAllClasses() { - Set classes = new TreeSet(); + Set classes = new TreeSet<>(); String query = "SELECT ?c WHERE {?c a } LIMIT 1000"; /* * String query = "PREFIX owl: PREFIX rdfs: " + diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/SparqlEndpoint.java b/components-core/src/main/java/org/dllearner/kb/sparql/SparqlEndpoint.java index b3d0171dfa..d98009cf94 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/SparqlEndpoint.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/SparqlEndpoint.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.sparql; import java.net.MalformedURLException; @@ -41,8 +40,8 @@ public class SparqlEndpoint { public SparqlEndpoint(URL u) { this.url = u; - this.defaultGraphURIs=new LinkedList(); - this.namedGraphURIs=new LinkedList(); + this.defaultGraphURIs= new LinkedList<>(); + this.namedGraphURIs= new LinkedList<>(); } public SparqlEndpoint(URL u, List defaultGraphURIs, List namedGraphURIs) { @@ -88,28 +87,29 @@ public static SparqlEndpoint getEndpointByName(String name) { name = name.toUpperCase(); - if (name.equals("DBPEDIA")) { - return getEndpointDBpedia(); - } else if (name.equals("GOVTRACK")) { - return getEndpointGovTrack(); - } else if (name.equals("SPARQLETTE")) { - return getEndpointSparqlette(); - } else if (name.equals("SWCONFERENCE")) { - return getEndpointSWConference(); - } else if (name.equals("REVYU")) { - return getEndpointRevyu(); - } else if (name.equals("MYOPENLINK")) { - return getEndpointMyOpenlink(); - } else if (name.equals("MUSICBRAINZ")) { - return getEndpointMusicbrainz(); - } else { - return null; + switch (name) { + case "DBPEDIA": + return getEndpointDBpedia(); + case "GOVTRACK": + return getEndpointGovTrack(); + case "SPARQLETTE": + return getEndpointSparqlette(); + case "SWCONFERENCE": + return getEndpointSWConference(); + case "REVYU": + return getEndpointRevyu(); + case "MYOPENLINK": + return getEndpointMyOpenlink(); + case "MUSICBRAINZ": + return getEndpointMusicbrainz(); + default: + return null; } } public static List listEndpoints() { - LinkedList ll =new LinkedList(); + LinkedList ll = new LinkedList<>(); ll.add(getEndpointDBpedia()); ll.add(getEndpointDOAPspace()); ll.add(getEndpointGovTrack()); @@ -129,7 +129,7 @@ public static SparqlEndpoint getEndpointDBpedia() { } catch (Exception e) { e.printStackTrace(); } - LinkedList defaultGraphURIs=new LinkedList(); + LinkedList defaultGraphURIs= new LinkedList<>(); defaultGraphURIs.add("http://dbpedia.org"); return new SparqlEndpoint(u, defaultGraphURIs, new LinkedList()); } @@ -142,7 +142,7 @@ public static SparqlEndpoint getEndpointDBpediaLiveAKSW() { } catch (Exception e) { e.printStackTrace(); } - LinkedList defaultGraphURIs=new LinkedList(); + LinkedList defaultGraphURIs= new LinkedList<>(); defaultGraphURIs.add("http://dbpedia.org"); return new SparqlEndpoint(u, defaultGraphURIs, new LinkedList()); } @@ -154,7 +154,7 @@ public static SparqlEndpoint getEndpointDBpediaHanne() { } catch (Exception e) { e.printStackTrace(); } - LinkedList defaultGraphURIs=new LinkedList(); + LinkedList defaultGraphURIs= new LinkedList<>(); defaultGraphURIs.add("http://dbpedia.org"); return new SparqlEndpoint(u, defaultGraphURIs, new LinkedList()); } @@ -166,7 +166,7 @@ public static SparqlEndpoint getEndpointDBpediaLiveOpenLink() { } catch (Exception e) { e.printStackTrace(); } - LinkedList defaultGraphURIs=new LinkedList(); + LinkedList defaultGraphURIs= new LinkedList<>(); defaultGraphURIs.add("http://dbpedia.org"); return new SparqlEndpoint(u, defaultGraphURIs, new LinkedList()); } @@ -178,7 +178,7 @@ public static SparqlEndpoint getEndpointLOD2Cloud() { } catch (Exception e) { e.printStackTrace(); } - LinkedList defaultGraphURIs=new LinkedList(); + LinkedList defaultGraphURIs= new LinkedList<>(); // defaultGraphURIs.add("http://dbpedia.org"); return new SparqlEndpoint(u, defaultGraphURIs, new LinkedList()); } @@ -190,7 +190,7 @@ public static SparqlEndpoint getEndpointDBpediaLOD2Cloud() { } catch (Exception e) { e.printStackTrace(); } - LinkedList defaultGraphURIs=new LinkedList(); + LinkedList defaultGraphURIs= new LinkedList<>(); defaultGraphURIs.add("http://dbpedia.org"); return new SparqlEndpoint(u, defaultGraphURIs, new LinkedList()); } @@ -202,7 +202,7 @@ public static SparqlEndpoint getEndpointLinkedGeoData() { } catch (Exception e) { e.printStackTrace(); } - LinkedList defaultGraphURIs=new LinkedList(); + LinkedList defaultGraphURIs= new LinkedList<>(); //TODO defaultGraphURIs.add("http://geonames.org"); return new SparqlEndpoint(u, defaultGraphURIs, new LinkedList()); } @@ -214,7 +214,7 @@ public static SparqlEndpoint getEndpointLinkedMDB() { } catch (Exception e) { e.printStackTrace(); } - LinkedList defaultGraphURIs=new LinkedList(); + LinkedList defaultGraphURIs= new LinkedList<>(); //TODO defaultGraphURIs.add("http://geonames.org"); return new SparqlEndpoint(u, defaultGraphURIs, new LinkedList()); } @@ -246,7 +246,7 @@ public static SparqlEndpoint getEndpointMyOpenlink() { } catch (Exception e) { e.printStackTrace(); } - LinkedList defaultGraphURIs=new LinkedList(); + LinkedList defaultGraphURIs= new LinkedList<>(); defaultGraphURIs.add("http://myopenlink.net/dataspace"); return new SparqlEndpoint(u, defaultGraphURIs, new LinkedList()); @@ -291,7 +291,7 @@ public static SparqlEndpoint getEndpointUSCensus() { } catch (Exception e) { e.printStackTrace(); } - LinkedList defaultGraphURIs=new LinkedList(); + LinkedList defaultGraphURIs= new LinkedList<>(); defaultGraphURIs.add("http://www.rdfabout.com/rdf/schema/census/"); return new SparqlEndpoint(u, defaultGraphURIs, new LinkedList()); diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/SparqlKnowledgeSource.java b/components-core/src/main/java/org/dllearner/kb/sparql/SparqlKnowledgeSource.java index 9bb5e86e95..554ea1fb52 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/SparqlKnowledgeSource.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/SparqlKnowledgeSource.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.sparql; import java.io.File; @@ -33,16 +32,6 @@ import org.apache.log4j.Logger; import org.dllearner.core.AbstractKnowledgeSource; import org.dllearner.core.ComponentAnn; -import org.dllearner.core.options.BooleanConfigOption; -import org.dllearner.core.options.CommonConfigOptions; -import org.dllearner.core.options.ConfigEntry; -import org.dllearner.core.options.ConfigOption; -import org.dllearner.core.options.IntegerConfigOption; -import org.dllearner.core.options.InvalidConfigOptionValueException; -import org.dllearner.core.options.StringConfigOption; -import org.dllearner.core.options.StringSetConfigOption; -import org.dllearner.core.options.StringTupleListConfigOption; -import org.dllearner.core.options.URLConfigOption; import org.dllearner.kb.OWLOntologyKnowledgeSource; import org.dllearner.kb.aquisitors.SparqlTupleAquisitor; import org.dllearner.kb.aquisitors.SparqlTupleAquisitorImproved; @@ -83,18 +72,8 @@ public class SparqlKnowledgeSource extends AbstractKnowledgeSource implements OW private byte[] ontologyBytes; private OntologyToByteConverter converter = new SimpleOntologyToByteConverter(); -// private SparqlKnowledgeSourceConfigurator configurator; - - /** - * @return the configurator for this Knowledgesource - */ -// public SparqlKnowledgeSourceConfigurator getConfigurator() { -// return configurator; -// } - public SparqlKnowledgeSource() { -// this.configurator = new SparqlKnowledgeSourceConfigurator(this); - } + public SparqlKnowledgeSource() {} public SparqlKnowledgeSource(URL url, Set instances) { this.url = url; @@ -145,9 +124,9 @@ public SparqlKnowledgeSource(URL url, Set instances) { private String predefinedEndpoint; - private Collection defaultGraphURIs = new LinkedList(); + private Collection defaultGraphURIs = new LinkedList<>(); - private Collection namedGraphURIs = new LinkedList(); + private Collection namedGraphURIs = new LinkedList<>(); private boolean useCache = true; @@ -157,19 +136,19 @@ public SparqlKnowledgeSource(URL url, Set instances) { private String predefinedFilter; - private Set objList = new TreeSet(); + private Set objList = new TreeSet<>(); - private Set predList = new TreeSet() ; + private Set predList = new TreeSet<>() ; private boolean useLits = true; private String predefinedManipulator; - private List replacePredicate = new LinkedList(); + private List replacePredicate = new LinkedList<>(); private boolean useImprovedSparqlTupelAquisitor; - private List replaceObject = new LinkedList(); + private List replaceObject = new LinkedList<>(); public static String getName() { @@ -179,98 +158,6 @@ public static String getName() { private static Logger logger = Logger .getLogger(SparqlKnowledgeSource.class); - /** - * Specifies the configuration options for this knowledge source. - * - * @see org.dllearner.core.AbstractComponent#createConfigOptions() - * @return Options of this component. - */ - public static Collection> createConfigOptions() { - Collection> options = new LinkedList>(); - options.add(new URLConfigOption("url", "URL of SPARQL Endpoint", - null, true, true)); - options.add(new StringConfigOption("cacheDir", "dir of cache", "cache", - false, true)); - options.add(new BooleanConfigOption("useCache", - "If true a Cache is used", true, false, true)); - options.add(new BooleanConfigOption("useCacheDatabase", "If true, H2 database is used, otherwise one file per query is written.", false)); - options - .add(new StringSetConfigOption( - "instances", - "relevant instances e.g. positive and negative examples in a learning problem", - null, true, true)); - options.add(new IntegerConfigOption("recursionDepth", - "recursion depth of KB fragment selection", 1, false, true)); - options - .add(new StringConfigOption( - "predefinedFilter", - "the mode of the SPARQL Filter, use one of YAGO,SKOS,YAGOSKOS , YAGOSPECIALHIERARCHY, TEST", - null, false, true)); - options - .add(new StringConfigOption( - "predefinedEndpoint", - "the mode of the SPARQL Filter, use one of DBPEDIA, LOCAL, GOVTRACK, REVYU, MYOPENLINK", - null, false, true)); - options - .add(new StringConfigOption( - "predefinedManipulator", - "the mode of the Manipulator, use one of STANDARD, DBPEDIA-NAVIGATOR", - null, false, true)); - options.add(new StringSetConfigOption("predList", - "list of all ignored roles", new TreeSet(), false, true)); - options.add(new StringSetConfigOption("objList", - "list of all ignored objects", new TreeSet(), false, true)); - options - .add(new BooleanConfigOption( - "saveExtractedFragment", - "Specifies whether the extracted ontology is written to a file or not. " + - "The OWL file is written to the cache dir." + - "Some DBpedia URI will make the XML invalid", - false, false, true)); - options.add(new StringTupleListConfigOption("replacePredicate", - "rule for replacing predicates", new ArrayList(), false, true)); - options.add(new StringTupleListConfigOption("replaceObject", - "rule for replacing predicates", new ArrayList(), false, true)); - options.add(new IntegerConfigOption("breakSuperClassRetrievalAfter", - "stops a cyclic hierarchy after specified number of classes", - 1000, false, true)); - - options.add(new BooleanConfigOption("useLits", - "use Literals in SPARQL query", true, false, true)); - options - .add(new BooleanConfigOption( - "getAllSuperClasses", - "If true then all superclasses are retrieved until the most general class (owl:Thing) is reached.", - true, false, true)); - options.add(new BooleanConfigOption("closeAfterRecursion", - "gets all classes for all instances", true, false, true)); - options.add(new BooleanConfigOption("getPropertyInformation", - "gets all types for extracted ObjectProperties", false, false, - true)); - options.add(new BooleanConfigOption("dissolveBlankNodes", - "determines whether Blanknodes are dissolved. This is a costly function.", true, false, - true)); - options.add(new BooleanConfigOption("useImprovedSparqlTupelAquisitor", - "uses deeply nested SparqlQueries, according to recursion depth, still EXPERIMENTAL", false, false, - true)); - options.add(CommonConfigOptions.getVerbosityOption()); - - options.add(new StringSetConfigOption("defaultGraphURIs", - "a list of all default Graph URIs", new TreeSet(), false, true)); - options.add(new StringSetConfigOption("namedGraphURIs", - "a list of all named Graph URIs", new TreeSet(), false, true)); - return options; - } - - /* - * @see org.dllearner.core.Component#applyConfigEntry(org.dllearner.core.ConfigEntry) - */ - @Override - public void applyConfigEntry(ConfigEntry entry) - throws InvalidConfigOptionValueException { - //TODO remove this function - - } /* * (non-Javadoc) @@ -309,7 +196,7 @@ public void init() { // the actual extraction is started here Monitor extractionTime = JamonMonitorLogger.getTimeMonitor(SparqlKnowledgeSource.class, "total extraction time").start(); - List seedNodes=new ArrayList(); + List seedNodes= new ArrayList<>(); //if(!threaded){ seedNodes = m.extract(instances); @@ -424,9 +311,9 @@ public SparqlQuery sparqlQuery(String query) { public SparqlEndpoint getSparqlEndpoint(){ if(endpoint==null) { if (predefinedEndpoint == null) { - endpoint = new SparqlEndpoint(getURL(), new LinkedList( + endpoint = new SparqlEndpoint(getURL(), new LinkedList<>( defaultGraphURIs), - new LinkedList(namedGraphURIs)); + new LinkedList<>(namedGraphURIs)); } else { endpoint = SparqlEndpoint.getEndpointByName(predefinedEndpoint); // System.out.println(endpoint); diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/SparqlQuery.java b/components-core/src/main/java/org/dllearner/kb/sparql/SparqlQuery.java index f7f36a1497..ef7d128b30 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/SparqlQuery.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/SparqlQuery.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.sparql; import java.io.ByteArrayInputStream; diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/SparqlQueryDescriptionConvertRDFS.java b/components-core/src/main/java/org/dllearner/kb/sparql/SparqlQueryDescriptionConvertRDFS.java index 14bcabffc6..1f6ea94b18 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/SparqlQueryDescriptionConvertRDFS.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/SparqlQueryDescriptionConvertRDFS.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.sparql; import java.util.HashSet; @@ -49,7 +48,7 @@ public class SparqlQueryDescriptionConvertRDFS { * union of the subclasses ex: (c sub b); (b sub a ) then: (a AND b) will be * ((a OR b OR c) AND (b OR a)) * - * @param descriptionKBSyntax + * @param descriptionKBSyntax the description in KB syntax * @param maxDepth * determines the depth of retrieval, if 1 classes are replaced by direct subclasses only, * 1 is HIGHLY RECOMMENDED FOR LARGE HIERARCHIES) @@ -61,7 +60,7 @@ public static String conceptRewrite(String descriptionKBSyntax, SPARQLTasks st, String returnValue = ""; String currentconcept = ""; int lastPos = 0; - SortedSet subclasses = new TreeSet(); + SortedSet subclasses = new TreeSet<>(); // searches for everything in "", but backwards while ((lastPos = descriptionKBSyntax.lastIndexOf(quote)) != -1) { @@ -85,7 +84,7 @@ public static String conceptRewrite(String descriptionKBSyntax, SPARQLTasks st, currentconcept = "\"" + currentconcept + "\""; // replace with union else { - Set nc = new HashSet(); + Set nc = new HashSet<>(); for (String one : subclasses) { nc.add(new OWLClassImpl(IRI.create(one))); } diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/SparqlQueryException.java b/components-core/src/main/java/org/dllearner/kb/sparql/SparqlQueryException.java index 12baca8c60..f4754df7b3 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/SparqlQueryException.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/SparqlQueryException.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.sparql; public class SparqlQueryException extends Exception{ diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/SparqlQueryMaker.java b/components-core/src/main/java/org/dllearner/kb/sparql/SparqlQueryMaker.java index 4c3167f3e3..07363b7fa5 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/SparqlQueryMaker.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/SparqlQueryMaker.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.kb.sparql; import java.util.ArrayList; @@ -67,7 +66,7 @@ public SparqlQueryMaker(Set objectFilterList, super(); this.objectFilterList = objectFilterList; this.predicateFilterList = predicateFilterList; - this.predicateobjectFilterList = new TreeSet(); + this.predicateobjectFilterList = new TreeSet<>(); this.literals = literals; } @@ -137,22 +136,20 @@ public String makeSubjectQueryLevel(String subject, int level) { : " "; StringBuffer sbuff = new StringBuffer(1400); - sbuff.append("SELECT * WHERE { " + lineend + "{<" + subject - + "> ?predicate0 ?object0 ." + lineend); - sbuff.append(filtertmp + "} " + lineend); + sbuff.append("SELECT * WHERE { " + lineend + "{<").append(subject).append("> ?predicate0 ?object0 .").append(lineend); + sbuff.append(filtertmp).append("} ").append(lineend); // " + lineend + filter +" } "; for (int i = 1; i < level; i++) { sbuff.append("OPTIONAL { "); - sbuff.append("?object" + (i - 1) + " ?predicate" + i + " ?object" - + i + " . " + lineend); + sbuff.append("?object").append(i - 1).append(" ?predicate").append(i).append(" ?object").append(i).append(" . ").append(lineend); filtertmp = internalFilterAssemblySubject("predicate" + i, "object" + i); filtertmp = (filtertmp.length() > 0) ? "FILTER " + filtertmp + ". " : " "; - sbuff.append(filtertmp + " }"); + sbuff.append(filtertmp).append(" }"); } sbuff.append(lineend + "} "); @@ -167,12 +164,12 @@ private String internalFilterAssemblySubject(String predicateVariable, objectVariable = (objectVariable.startsWith("?")) ? objectVariable : "?" + objectVariable; - List terms = new ArrayList(); + List terms = new ArrayList<>(); String not = (isAllowMode()) ? "" : "!"; /*new filter type */ for (StringTuple tuple : getPredicateObjectFilterList()) { - List tmpterms = new ArrayList(); + List tmpterms = new ArrayList<>(); tmpterms.add(not + "regex(str(" + predicateVariable + "), '" + tuple.a+ "')"); tmpterms.add(not + "regex(str(" + objectVariable + "), '" + tuple.b+ "')"); terms.add(assembleTerms(tmpterms, "&&")); @@ -189,7 +186,7 @@ private String internalFilterAssemblySubject(String predicateVariable, } String assembled = assembleTerms(terms, getOperator()); - terms = new ArrayList(); + terms = new ArrayList<>(); // the next line could be removed as it is included in assemble terms if(!assembled.isEmpty()){ terms.add(assembled); @@ -223,7 +220,7 @@ else if (terms.size() == 1) String first = terms.remove(0); sbuf.append(brackets(first)); for (String term : terms) { - sbuf.append(lineend + operator); + sbuf.append(lineend).append(operator); sbuf.append(brackets(term)); } return brackets(sbuf.toString()); diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/SymmetricConciseBoundedDescriptionGeneratorImpl.java b/components-core/src/main/java/org/dllearner/kb/sparql/SymmetricConciseBoundedDescriptionGeneratorImpl.java index dfd9534244..df1c09f021 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/SymmetricConciseBoundedDescriptionGeneratorImpl.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/SymmetricConciseBoundedDescriptionGeneratorImpl.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb.sparql; import java.io.UnsupportedEncodingException; @@ -71,9 +89,7 @@ private Model getModelChunkedResourceIsObject(String resource, int depth){ all.add(model); i++; } - } catch (UnsupportedEncodingException e) { - logger.error(e); - } catch (SQLException e) { + } catch (UnsupportedEncodingException | SQLException e) { logger.error(e); } return all; @@ -102,9 +118,7 @@ private Model getModelChunkedResourceIsSubject(String resource, int depth){ all.add(model); i++; } - } catch (UnsupportedEncodingException e) { - logger.error(e); - } catch (SQLException e) { + } catch (UnsupportedEncodingException | SQLException e) { logger.error(e); } return all; diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/simple/ABoxQueryGenerator.java b/components-core/src/main/java/org/dllearner/kb/sparql/simple/ABoxQueryGenerator.java index 0cf7f2f778..a06eca0381 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/simple/ABoxQueryGenerator.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/simple/ABoxQueryGenerator.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ /** * */ @@ -11,6 +29,7 @@ /** * @author didierc */ +@SuppressWarnings("ALL") public class ABoxQueryGenerator { public String createQuery(Set individuals, String aboxfilter) { @@ -31,7 +50,7 @@ public String createQuery(Set individuals, String aboxfilter) { public static StringBuilder makeInFilter(String var, Set uris) { StringBuilder builder = new StringBuilder(); - builder.append(" FILTER (" + var + " IN( "); + builder.append(" FILTER (").append(var).append(" IN( "); for (String uri : uris) { if (!uri.startsWith("<")) builder.append("<"); builder.append(uri); diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/simple/ClassIndexer.java b/components-core/src/main/java/org/dllearner/kb/sparql/simple/ClassIndexer.java index 427058b2d8..a94bf57dbd 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/simple/ClassIndexer.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/simple/ClassIndexer.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ /***************************************************************************/ /* Copyright (C) 2010-2011, Sebastian Hellmann */ /* Note: If you need parts of NLP2RDF in another licence due to licence */ @@ -53,12 +71,12 @@ public class ClassIndexer { private String language = null; //Not implemented - private Map transform = new HashMap(); + private Map transform = new HashMap<>(); //Not implemented - private Set remove = new HashSet(); + private Set remove = new HashSet<>(); //internal variables - private Map classUriToClassHierarchy = new HashMap(); + private Map classUriToClassHierarchy = new HashMap<>(); public ClassIndexer() { } @@ -164,7 +182,7 @@ public Tree(OntClass me) { this.uri = me.getURI(); label = me.getLabel(language); comment = me.getComment(language); - parents = new ArrayList(); + parents = new ArrayList<>(); Set superClasses = me.listSuperClasses(true).toSet(); for (OntClass s : superClasses) { diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/simple/JenaToOwlapiConverter.java b/components-core/src/main/java/org/dllearner/kb/sparql/simple/JenaToOwlapiConverter.java index 8cd59b24e6..48a097f0d1 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/simple/JenaToOwlapiConverter.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/simple/JenaToOwlapiConverter.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb.sparql.simple; import java.io.StringReader; diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/simple/QueryExecutor.java b/components-core/src/main/java/org/dllearner/kb/sparql/simple/QueryExecutor.java index 6983f4b574..613c234867 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/simple/QueryExecutor.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/simple/QueryExecutor.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ /** * */ @@ -41,8 +59,6 @@ public OntModel executeQuery(String queryString, String endpoint, log.debug("Qexec: {}", qExec.getQuery()); qExec.execConstruct(model); - } catch (QueryParseException e) { - log.warn("Query failed (skipping):\n" + queryString, e); } catch (JenaException e) { log.warn("Query failed (skipping):\n" + queryString, e); } diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/simple/SchemaIndexer.java b/components-core/src/main/java/org/dllearner/kb/sparql/simple/SchemaIndexer.java index e276b79b6d..940a582632 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/simple/SchemaIndexer.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/simple/SchemaIndexer.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb.sparql.simple; import java.util.HashSet; @@ -19,7 +37,7 @@ public class SchemaIndexer { //static instantiation private static ClassIndexer classIndexer = new ClassIndexer(); //remember ontologies - private static Set alreadyIndexed = new HashSet(); + private static Set alreadyIndexed = new HashSet<>(); //set or list of urls for the ontologies private List ontologySchemaUrls; diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/simple/SparqlSimpleExtractor.java b/components-core/src/main/java/org/dllearner/kb/sparql/simple/SparqlSimpleExtractor.java index 332b0a389a..4264429278 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/simple/SparqlSimpleExtractor.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/simple/SparqlSimpleExtractor.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb.sparql.simple; import java.io.File; @@ -66,16 +84,12 @@ public SparqlSimpleExtractor() { model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM); } - /** - * @param args - * @throws ComponentInitException - */ public static void main(String[] args) throws ComponentInitException { SparqlSimpleExtractor extractor = new SparqlSimpleExtractor(); extractor.setEndpointURL("http://live.dbpedia.org/sparql"); extractor.setRecursionDepth(1); extractor.setDefaultGraphURI("http://dbpedia.org"); - List instances = new ArrayList(7); + List instances = new ArrayList<>(7); instances.add("http://dbpedia.org/resource/Democritus"); instances.add("http://dbpedia.org/resource/Zeno_of_Elea"); instances.add("http://dbpedia.org/resource/Plato"); @@ -86,7 +100,7 @@ public static void main(String[] args) throws ComponentInitException { extractor.setInstances(instances); extractor.init(); - List individuals = new LinkedList(); + List individuals = new LinkedList<>(); individuals.add("People"); individuals.add("Animals"); extractor.setInstances(individuals); @@ -94,8 +108,8 @@ public static void main(String[] args) throws ComponentInitException { } public Set difference(Set alreadyQueriedIndividuals, OntModel model) { - Set candidates = new HashSet(); - Set result = new HashSet(); + Set candidates = new HashSet<>(); + Set result = new HashSet<>(); for (ResIterator it = model.listSubjects(); it.hasNext(); ) { candidates.add(it.next().getURI()); } @@ -149,8 +163,8 @@ public void init() throws ComponentInitException { Monitor monQueryingABox; QueryExecutor executor = new QueryExecutor(); String queryString; - Set instancesSet = new HashSet(instances); - Set alreadyQueried = new HashSet(); + Set instancesSet = new HashSet<>(instances); + Set alreadyQueried = new HashSet<>(); Monitor typizeModel; if (sparqlQuery == null) { ABoxQueryGenerator aGenerator = new ABoxQueryGenerator(); diff --git a/components-core/src/main/java/org/dllearner/kb/sparql/simple/TBoxQueryGenerator.java b/components-core/src/main/java/org/dllearner/kb/sparql/simple/TBoxQueryGenerator.java index 94c93d4ea8..4e59615f44 100644 --- a/components-core/src/main/java/org/dllearner/kb/sparql/simple/TBoxQueryGenerator.java +++ b/components-core/src/main/java/org/dllearner/kb/sparql/simple/TBoxQueryGenerator.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ /** * */ @@ -41,7 +59,7 @@ public String createQuery(Set individuals, String filter) { public static void main(String... args) { TBoxQueryGenerator generator = new TBoxQueryGenerator(); OntModel model = ModelFactory.createOntologyModel(); - Set individuals = new HashSet(); + Set individuals = new HashSet<>(); individuals.add("http://dbpedia.org/resource/JB_Carlson"); diff --git a/components-core/src/main/java/org/dllearner/learningproblems/AccMethod.java b/components-core/src/main/java/org/dllearner/learningproblems/AccMethod.java new file mode 100644 index 0000000000..74e98feefc --- /dev/null +++ b/components-core/src/main/java/org/dllearner/learningproblems/AccMethod.java @@ -0,0 +1,9 @@ +package org.dllearner.learningproblems; + +import org.dllearner.core.Component; + +/** + * Created by Simon Bin on 16-1-26. + */ +public interface AccMethod extends Component { +} diff --git a/components-core/src/main/java/org/dllearner/learningproblems/AccMethodAMeasure.java b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodAMeasure.java new file mode 100644 index 0000000000..98dba12c1b --- /dev/null +++ b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodAMeasure.java @@ -0,0 +1,50 @@ +package org.dllearner.learningproblems; + +import org.dllearner.core.Component; +import org.dllearner.core.ComponentAnn; +import org.dllearner.core.ComponentInitException; + +@ComponentAnn(name = "AMeasure", shortName = "ameasure", version = 0.1) +public class AccMethodAMeasure implements Component, AccMethodTwoValued, AccMethodWithBeta { + + protected double beta = 0; + + public AccMethodAMeasure() {} + + public AccMethodAMeasure(boolean init) throws ComponentInitException { + if(init)init(); + } + + @Override + public void init() { + } + + @Override + public double getAccOrTooWeak2(int tp, int fn, int fp, int tn, double noise) { + double recall = Heuristics.divideOrZero( tp , tp+fn ); + double precision = Heuristics.divideOrZero( tp , tp+fp ); + + // best reachable concept has same recall and precision 1: + // 1/t+1 * (t*r + 1) + if (beta == 0) { + if (recall + 1 < 1 - noise) { + return -1; + } + } else { + if ((beta * recall + 1) / (beta + 1) < 1 - noise) { + return -1; + } + } + + if (beta == 0) { + return Heuristics.getAScore(recall, precision); + } else { + return Heuristics.getAScore(recall, precision, beta); + } + } + + @Override + public void setBeta(double beta) { + this.beta = beta; + } +} diff --git a/components-core/src/main/java/org/dllearner/learningproblems/AccMethodAMeasureApprox.java b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodAMeasureApprox.java new file mode 100644 index 0000000000..b07a57c324 --- /dev/null +++ b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodAMeasureApprox.java @@ -0,0 +1,180 @@ +package org.dllearner.learningproblems; + +import org.dllearner.core.ComponentAnn; +import org.dllearner.core.Reasoner; +import org.semanticweb.owlapi.model.OWLClassExpression; +import org.semanticweb.owlapi.model.OWLIndividual; + +import java.util.Collection; + +@ComponentAnn(name = "AMeasure Approximate", shortName = "approx.ameasure", version = 0.1) +public class AccMethodAMeasureApprox extends AccMethodAMeasure implements AccMethodTwoValuedApproximate { + + private Reasoner reasoner; + private double approxDelta; + + @Override + public double getApproxDelta() { + return approxDelta; + } + + @Override + public void setApproxDelta(double approxDelta) { + this.approxDelta = approxDelta; + } + + @Override + public void setReasoner(Reasoner reasoner) { + this.reasoner = reasoner; + } + + @Override + public double getAccApprox2(OWLClassExpression description, + Collection classInstances, + Collection superClassInstances, + double noise) { + // we abort when there are too many uncovered positives + int maxNotCovered = (int) Math.ceil(noise*classInstances.size()); + int instancesCovered = 0; + int instancesNotCovered = 0; + int total = 0; + boolean estimatedA = false; + + double lowerBorderA = 0; + int lowerEstimateA = 0; + double upperBorderA = 1; + int upperEstimateA = classInstances.size(); + + for(OWLIndividual ind : classInstances) { + if(reasoner.hasType(description, ind)) { + instancesCovered++; + } else { + instancesNotCovered ++; + if(instancesNotCovered > maxNotCovered) { + return -1; + } + } + + // approximation step (starting after 10 tests) + total = instancesCovered + instancesNotCovered; + if(total > 10) { + // compute confidence interval + double p1 = Heuristics.p1(instancesCovered, total); + double p2 = Heuristics.p3(p1, total); + lowerBorderA = Math.max(0, p1 - p2); + upperBorderA = Math.min(1, p1 + p2); + double size = upperBorderA - lowerBorderA; + // if the interval has a size smaller than 10%, we can be confident + if(size < 2 * approxDelta) { + // we have to distinguish the cases that the accuracy limit is + // below, within, or above the limit and that the mean is below + // or above the limit + double mean = instancesCovered/(double)total; + + // we can estimate the best possible concept to reach with downward refinement + // by setting precision to 1 and recall = mean stays as it is + double optimumEstimate = beta == 0 ? mean : (beta*mean+1)/(beta+1); + + // if the mean is greater than the required minimum, we can accept; + // we also accept if the interval is small and close to the minimum + // (worst case is to accept a few inaccurate descriptions) + if(optimumEstimate > 1-noise-0.03) { + // || (upperBorderA > mean && size < 0.03)) { + instancesCovered = (int) (instancesCovered/(double)total * classInstances.size()); + upperEstimateA = (int) (upperBorderA * classInstances.size()); + lowerEstimateA = (int) (lowerBorderA * classInstances.size()); + estimatedA = true; + break; + } + + // reject only if the upper border is far away (we are very + // certain not to lose a potential solution) + // if(upperBorderA + 0.1 < 1-noise) { + double optimumEstimateUpperBorder = beta == 0 ? upperBorderA+0.1 : (beta*(upperBorderA+0.1)+1)/(beta+1); + if(optimumEstimateUpperBorder < 1 - noise) { + return -1; + } + } + } + } + + double recall = instancesCovered/(double)classInstances.size(); + + // MonitorFactory.add("estimatedA","count", estimatedA ? 1 : 0); + // MonitorFactory.add("aInstances","count", total); + + // we know that a definition candidate is always subclass of the + // intersection of all super classes, so we test only the relevant instances + // (leads to undesired effects for descriptions not following this rule, + // but improves performance a lot); + // for learning a superclass of a defined class, similar observations apply; + + + int testsPerformed = 0; + int instancesDescription = 0; + // boolean estimatedB = false; + + for(OWLIndividual ind : superClassInstances) { + + if(reasoner.hasType(description, ind)) { + instancesDescription++; + } + + testsPerformed++; + + if(testsPerformed > 10) { + + // compute confidence interval + double p1 = Heuristics.p1(instancesDescription, testsPerformed); + double p2 = Heuristics.p3(p1, testsPerformed); + double lowerBorder = Math.max(0, p1 - p2); + double upperBorder = Math.min(1, p1 + p2); + int lowerEstimate = (int) (lowerBorder * superClassInstances.size()); + int upperEstimate = (int) (upperBorder * superClassInstances.size()); + + double size; + if(estimatedA) { + // size = 1/(coverageFactor+1) * (coverageFactor * (upperBorderA-lowerBorderA) + Math.sqrt(upperEstimateA/(upperEstimateA+lowerEstimate)) + Math.sqrt(lowerEstimateA/(lowerEstimateA+upperEstimate))); + if (beta == 0) { + size = Heuristics.getAScore(upperBorderA, upperEstimateA / (double) (upperEstimateA + lowerEstimate)) - Heuristics.getAScore(lowerBorderA, lowerEstimateA / (double) (lowerEstimateA + upperEstimate)); + } else { + size = Heuristics.getAScore(upperBorderA, upperEstimateA / (double) (upperEstimateA + lowerEstimate), beta) - Heuristics.getAScore(lowerBorderA, lowerEstimateA / (double) (lowerEstimateA + upperEstimate), beta); + } + } else { + if (beta == 0) { + size = Heuristics.getAScore(recall, instancesCovered / (double) (instancesCovered + lowerEstimate)) - Heuristics.getAScore(recall, instancesCovered / (double) (instancesCovered + upperEstimate)); + } else { + // size = 1/(coverageFactor+1) * (coverageFactor * coverage + Math.sqrt(instancesCovered/(instancesCovered+lowerEstimate)) + Math.sqrt(instancesCovered/(instancesCovered+upperEstimate))); + size = Heuristics.getAScore(recall, instancesCovered / (double) (instancesCovered + lowerEstimate), beta) - Heuristics.getAScore(recall, instancesCovered / (double) (instancesCovered + upperEstimate), beta); + } + } + + if(size < 0.1) { + // System.out.println(instancesDescription + " of " + testsPerformed); + // System.out.println("interval from " + lowerEstimate + " to " + upperEstimate); + // System.out.println("size: " + size); + + // estimatedB = true; + // calculate total number of instances + instancesDescription = (int) (instancesDescription/(double)testsPerformed * superClassInstances.size()); + break; + } + } + } + + // since we measured/estimated accuracy only on instances outside A (superClassInstances + // does not include instances of A), we need to add it in the denominator + double precision = instancesCovered/(double)(instancesDescription+instancesCovered); + if(instancesCovered + instancesDescription == 0) { + precision = 0; + } + + if (beta == 0) { + return Heuristics.getAScore(recall, precision); + } else { + return Heuristics.getAScore(recall, precision, beta); + } + + } + +} diff --git a/components-core/src/main/java/org/dllearner/learningproblems/AccMethodApproximate.java b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodApproximate.java new file mode 100644 index 0000000000..56bfe52bec --- /dev/null +++ b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodApproximate.java @@ -0,0 +1,45 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.learningproblems; + +import org.dllearner.core.Component; +import org.dllearner.core.Reasoner; + +/** + * Implementation of approximate accuracy calculation. + * These methods require the reasoner to incrementally query the knowledge source. + */ +public interface AccMethodApproximate extends Component { + double getApproxDelta(); + + /** + * set the approximation delta; + * this option should be exposed via @ConfigOption + * @param approxDelta delta + */ + void setApproxDelta(double approxDelta); + + /** + * set the reasoner; + * consumers of the accuracy method should call this method on all approximate components + * @param reasoner the problem reasoner + */ + void setReasoner(Reasoner reasoner); + +} diff --git a/components-core/src/main/java/org/dllearner/learningproblems/AccMethodFMeasure.java b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodFMeasure.java new file mode 100644 index 0000000000..b4b5ffc905 --- /dev/null +++ b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodFMeasure.java @@ -0,0 +1,66 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.learningproblems; + +import org.dllearner.core.ComponentAnn; + +@ComponentAnn(name = "FMeasure", shortName = "fmeasure", version = 0) +public class AccMethodFMeasure implements AccMethodTwoValued, AccMethodWithBeta { + + protected double beta = 0; + + @Override + public void init() { + } + + public AccMethodFMeasure() { + } + + public AccMethodFMeasure(boolean init) { + if (init) init(); + } + + @Override + public double getAccOrTooWeak2(int tp, int fn, int fp, int tn, double noise) { + double recall = Heuristics.divideOrZero( tp , tp+fn ); + + if (beta == 0) { + if (recall == 0 || recall < 1 - noise) { + return -1; + } + } else { + if (recall == 0 || ((1 + Math.sqrt(beta)) * recall) / (Math.sqrt(beta) + 1) < 1 - noise) { + return -1; + } + } + + double precision = Heuristics.divideOrZero( tp , tp+fp ); + + if (beta == 0) { + return Heuristics.getFScore(recall, precision); + } else { + return Heuristics.getFScore(recall, precision, beta); + } + } + + @Override + public void setBeta(double beta) { + this.beta = beta; + } +} diff --git a/components-core/src/main/java/org/dllearner/learningproblems/AccMethodFMeasureApprox.java b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodFMeasureApprox.java new file mode 100644 index 0000000000..629737877b --- /dev/null +++ b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodFMeasureApprox.java @@ -0,0 +1,108 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.learningproblems; + +import org.dllearner.core.AbstractReasonerComponent; +import org.dllearner.core.ComponentAnn; +import org.dllearner.core.Reasoner; +import org.dllearner.core.config.ConfigOption; +import org.semanticweb.owlapi.model.OWLClassExpression; +import org.semanticweb.owlapi.model.OWLIndividual; + +import java.util.Collection; + +@ComponentAnn(name = "FMeasure Approximate", shortName = "approx.fmeasure", version = 0) +public class AccMethodFMeasureApprox extends AccMethodFMeasure implements AccMethodTwoValuedApproximate, AccMethodWithBeta { + @ConfigOption(description = "The Approximate Delta", defaultValue = "0.05", required = false) + private double approxDelta = 0.05; + private Reasoner reasoner; + + public AccMethodFMeasureApprox(boolean init, AbstractReasonerComponent reasoner) { + this.setReasoner(reasoner); + if(init) { + init(); + } + } + + public AccMethodFMeasureApprox() { + } + + @Override + public double getAccApprox2(OWLClassExpression description, + Collection positiveExamples, + Collection negativeExamples, double noise) { + // System.out.println("Testing " + description); + + // we abort when there are too many uncovered positives + int maxNotCovered = (int) Math.ceil(noise*positiveExamples.size()); + int instancesCovered = 0; + int instancesNotCovered = 0; + + for(OWLIndividual ind : positiveExamples) { + if(reasoner.hasType(description, ind)) { + instancesCovered++; + } else { + instancesNotCovered ++; + if(instancesNotCovered > maxNotCovered) { + return -1; + } + } + } + + double recall = instancesCovered/(double)positiveExamples.size(); + + int testsPerformed = 0; + int instancesDescription = 0; + + for(OWLIndividual ind : negativeExamples) { + + if(reasoner.hasType(description, ind)) { + instancesDescription++; + } + testsPerformed++; + + // check whether approximation is sufficiently accurate + double[] approx = Heuristics.getFScoreApproximation(instancesCovered, recall, (beta == 0 ? 1 : beta), negativeExamples.size(), testsPerformed, instancesDescription); + if(approx[1]. + */ +package org.dllearner.learningproblems; + +import org.dllearner.core.ComponentAnn; +import org.dllearner.core.config.ConfigOption; + +@ComponentAnn(name = "Weighted FMeasure", shortName = "weighted.fmeasure", version = 0) +public class AccMethodFMeasureWeighted implements AccMethodTwoValued, AccMethodWithBeta { + + @ConfigOption(defaultValue = "false", description = "balance the weights to relative set size") + private boolean balanced = false; + @ConfigOption(defaultValue = "1", description = "weight on the positive examples") + private double posWeight = 1; + @ConfigOption(defaultValue = "1", description = "weight on the negative examples") + private double negWeight = 1; + private double beta = 0; + + @Override + public void init() { + } + + public AccMethodFMeasureWeighted() { + } + + public AccMethodFMeasureWeighted(boolean init) { + if (init) init(); + } + + @Override + public double getAccOrTooWeak2(int tp, int fn, int fp, int tn, double noise) { + int posExamples = tp + fn; + int negExamples = fp + tn; + + double recall = Heuristics.divideOrZero( tp , tp+fn ); + + if(recall == 0 || recall < 1 - noise) { + return -1; + } + + if (balanced) { + posWeight = 1/(double)posExamples; + negWeight = 1/(double)negExamples; + } + double precision = tp == 0 ? 0 : ( tp*posWeight ) / ( tp*posWeight+fp*negWeight ); + if (beta == 0) { + return Heuristics.getFScore(recall, precision); + } else { + return Heuristics.getFScore(recall, precision, beta); + } + } + + + public boolean isBalanced() { + return balanced; + } + + public void setBalanced(boolean balanced) { + this.balanced = balanced; + } + + public double getPosWeight() { + return posWeight; + } + + public void setPosWeight(double posWeight) { + this.posWeight = posWeight; + } + + public double getNegWeight() { + return negWeight; + } + + public void setNegWeight(double negWeight) { + this.negWeight = negWeight; + } + + @Override + public void setBeta(double beta) { + this.beta = beta; + } +} diff --git a/components-core/src/main/java/org/dllearner/learningproblems/AccMethodGenFMeasure.java b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodGenFMeasure.java new file mode 100644 index 0000000000..7400a8f99c --- /dev/null +++ b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodGenFMeasure.java @@ -0,0 +1,37 @@ +package org.dllearner.learningproblems; + +import org.dllearner.core.ComponentAnn; + +@ComponentAnn(name = "Generalised FMeasure", shortName = "gen_fmeasure", version = 0.1) +public class AccMethodGenFMeasure implements AccMethodThreeValued, AccMethodWithBeta { + + private double beta = 0; + + @Override + public double getAccOrTooWeak3(int pos1, int neg1, int icPos, int icNeg, int posEx, int negatedPosEx, double noise) { + // Cn(I_C) \cap D_C is the same set if we ignore Cn ... + int tmp1Size = pos1 + neg1; // true positives (correct examples) + + int icSize = icPos + icNeg; + double prec = Heuristics.divideOrZero(tmp1Size, icSize); + double rec = tmp1Size / (double) (posEx + negatedPosEx); + + // we only return too weak if there is no recall + if(rec <= 0.0000001) { + return -1; + } + + return Heuristics.getFScoreBalanced(rec,prec,beta); + } + + @Override + public void init() { + } + + @Override + public void setBeta(double beta) { + this.beta = beta; + + } + +} diff --git a/components-core/src/main/java/org/dllearner/learningproblems/AccMethodJaccard.java b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodJaccard.java new file mode 100644 index 0000000000..897952ccc9 --- /dev/null +++ b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodJaccard.java @@ -0,0 +1,26 @@ +package org.dllearner.learningproblems; + +import org.dllearner.core.ComponentAnn; + +@ComponentAnn(name = "Jaccard Coefficient", shortName = "jaccard", version = 0.1) +public class AccMethodJaccard implements AccMethodTwoValued { + + public AccMethodJaccard() {} + + public AccMethodJaccard(boolean init) { + if(init)init(); + } + + @Override + public void init() { + } + + @Override + public double getAccOrTooWeak2(int tp, int fn, int fp, int tn, double noise) { + if(tp / (double) (tp+fn) <= 1 - noise) { + return -1; + } + return Heuristics.getJaccardCoefficient(tp, tp + fn + fp); + } + +} diff --git a/components-core/src/main/java/org/dllearner/learningproblems/AccMethodNoWeakness.java b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodNoWeakness.java new file mode 100644 index 0000000000..e6096b06ae --- /dev/null +++ b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodNoWeakness.java @@ -0,0 +1,7 @@ +package org.dllearner.learningproblems; + +/** + * Accuracy Method should implement this interface if it cannot filter weak solutions and thus does not return -1. + */ +public interface AccMethodNoWeakness { +} diff --git a/components-core/src/main/java/org/dllearner/learningproblems/AccMethodPredAcc.java b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodPredAcc.java new file mode 100644 index 0000000000..3010275cdd --- /dev/null +++ b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodPredAcc.java @@ -0,0 +1,70 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.learningproblems; + +import org.dllearner.core.ComponentAnn; + +@ComponentAnn(name = "Predictive Accuracy", shortName = "pred_acc", version = 0) +public class AccMethodPredAcc implements AccMethodTwoValued, AccMethodWithBeta { + + private double beta = 0; + + public AccMethodPredAcc() { + } + + public AccMethodPredAcc(boolean init) { + if (init) init(); + } + + @Override + public void init() { + } + + @Override + public double getAccOrTooWeak2(int tp, int fn, int fp, int tn, double noise) { + int posExamples = tp + fn; + int negExamples = fp + tn; + int allExamples = posExamples + negExamples; + + if (beta == 0) { + int maxNotCovered = (int) Math.ceil(noise * posExamples); + + if (fn != 0 && fn >= maxNotCovered) { + return -1; + } + + return (tp + tn) / (double) allExamples; + + } else { + + if ((beta * tp + negExamples) / (beta * posExamples + negExamples) < 1 - noise) { + return -1; + } + + // correctly classified divided by all examples + return (beta * tp + tn) / (beta * posExamples + negExamples); + } + + } + + @Override + public void setBeta(double beta) { + this.beta = beta; + } +} diff --git a/components-core/src/main/java/org/dllearner/learningproblems/AccMethodPredAccApprox.java b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodPredAccApprox.java new file mode 100644 index 0000000000..e5d9508298 --- /dev/null +++ b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodPredAccApprox.java @@ -0,0 +1,121 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.learningproblems; + +import org.apache.log4j.Logger; +import org.dllearner.core.ComponentAnn; +import org.dllearner.core.Reasoner; +import org.dllearner.core.config.ConfigOption; +import org.semanticweb.owlapi.model.OWLClassExpression; +import org.semanticweb.owlapi.model.OWLIndividual; + +import java.util.Collection; +import java.util.Iterator; + +@ComponentAnn(name = "Predictive Accuracy Approximate", shortName = "approx.prec_acc", version = 0) +public class AccMethodPredAccApprox extends AccMethodPredAcc implements AccMethodTwoValuedApproximate { + final static Logger logger = Logger.getLogger(AccMethodPredAccApprox.class); + @Override + public void init() { + logger.warn("Approximating predictive accuracy is an experimental feature. USE IT AT YOUR OWN RISK. If you consider to use it for anything serious, please extend the unit tests at org.dllearner.test.junit.HeuristicTests first and verify that it works."); + } + // approximation and F-measure + // (taken from class learning => super class instances corresponds to negative examples + // and class instances to positive examples) + @ConfigOption(description = "The Approximate Delta", defaultValue = "0.05", required = false) + private double approxDelta = 0.05; + private Reasoner reasoner; + + + @Override + public double getAccApprox2(OWLClassExpression description, Collection positiveExamples, Collection negativeExamples, double noise) { + int maxNotCovered = (int) Math.ceil(noise*positiveExamples.size()); + + int notCoveredPos = 0; + // int notCoveredNeg = 0; + + int posClassifiedAsPos = 0; + int negClassifiedAsNeg = 0; + + int nrOfPosChecks = 0; + int nrOfNegChecks = 0; + + // special case: we test positive and negative examples in turn + Iterator itPos = positiveExamples.iterator(); + Iterator itNeg = negativeExamples.iterator(); + + do { + // in each loop we pick 0 or 1 positives and 0 or 1 negative + // and classify it + + if(itPos.hasNext()) { + OWLIndividual posExample = itPos.next(); + // System.out.println(posExample); + + if(reasoner.hasType(description, posExample)) { + posClassifiedAsPos++; + } else { + notCoveredPos++; + } + nrOfPosChecks++; + + // take noise into account + if(notCoveredPos > maxNotCovered) { + return -1; + } + } + + if(itNeg.hasNext()) { + OWLIndividual negExample = itNeg.next(); + if(!reasoner.hasType(description, negExample)) { + negClassifiedAsNeg++; + } + nrOfNegChecks++; + } + + // compute how accurate our current approximation is and return if it is sufficiently accurate + double approx[] = Heuristics.getPredAccApproximation(positiveExamples.size(), negativeExamples.size(), 1, nrOfPosChecks, posClassifiedAsPos, nrOfNegChecks, negClassifiedAsNeg); + if(approx[1]. + */ +package org.dllearner.learningproblems; + +import org.dllearner.core.ComponentAnn; +import org.dllearner.core.config.ConfigOption; + +@ComponentAnn(name = "Weighted Predictive Accuracy", shortName = "weighted.pred_acc", version = 0) +public class AccMethodPredAccWeighted implements AccMethodTwoValued { + + @ConfigOption(defaultValue = "false", description = "balance the weights to relative set size") + private boolean balanced = false; + @ConfigOption(defaultValue = "1", description = "weight on the positive examples") + private double posWeight = 1; + @ConfigOption(defaultValue = "1", description = "weight on the negative examples") + private double negWeight = 1; + + public AccMethodPredAccWeighted() { + } + + public AccMethodPredAccWeighted(boolean init) { + if (init) init(); + } + + @Override + public void init() { + } + + @Override + public double getAccOrTooWeak2(int tp, int fn, int fp, int tn, double noise) { + int posExamples = tp + fn; + int negExamples = fp + tn; + + int maxNotCovered = (int) Math.ceil(noise*posExamples); + + if(fn != 0 && fn >= maxNotCovered) { + return -1; + } + + if (balanced) { + posWeight = 1/(double)posExamples; + negWeight = 1/(double)negExamples; + } + return ((tp*posWeight) + (tn*negWeight)) / ((posExamples*posWeight)+(negExamples*negWeight)); + } + + public boolean isBalanced() { + return balanced; + } + + public void setBalanced(boolean balanced) { + this.balanced = balanced; + } + + public double getPosWeight() { + return posWeight; + } + + public void setPosWeight(double posWeight) { + this.posWeight = posWeight; + } + + public double getNegWeight() { + return negWeight; + } + + public void setNegWeight(double negWeight) { + this.negWeight = negWeight; + } + +} diff --git a/components-core/src/main/java/org/dllearner/learningproblems/AccMethodThreeValued.java b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodThreeValued.java new file mode 100644 index 0000000000..b6c80e63a5 --- /dev/null +++ b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodThreeValued.java @@ -0,0 +1,19 @@ +package org.dllearner.learningproblems; + +/** + * Implementations of generalised measures according to method in A Note on the Evaluation of Inductive Concept Classification Procedures + */ +public interface AccMethodThreeValued extends AccMethod { + /** + * Calculate generalised measures according to method in A Note on the Evaluation of Inductive Concept Classification Procedures + * @param pos1 nr of instances matching the concept and in I_C + * @param neg1 nr of instances matching the negated concept and in D_C + * @param icPos nr of total instances matching the concept + * @param icNeg nr of total instances matching the negated concept + * @param posEx nr of instances in the target concept + * @param negatedPosEx nr of instances in the negated target concept + * @param noise noise parameter + * @return + */ + double getAccOrTooWeak3(int pos1, int neg1, int icPos, int icNeg, int posEx, int negatedPosEx, double noise); +} diff --git a/components-core/src/main/java/org/dllearner/learningproblems/AccMethodThreeValuedApproximate.java b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodThreeValuedApproximate.java new file mode 100644 index 0000000000..cef24f4af0 --- /dev/null +++ b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodThreeValuedApproximate.java @@ -0,0 +1,7 @@ +package org.dllearner.learningproblems; + +/** + * Approximation support for @AccMethodThreeValued + */ +public interface AccMethodThreeValuedApproximate extends AccMethodApproximate, AccMethodThreeValued { +} diff --git a/components-core/src/main/java/org/dllearner/learningproblems/AccMethodTwoValued.java b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodTwoValued.java new file mode 100644 index 0000000000..27f8b32737 --- /dev/null +++ b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodTwoValued.java @@ -0,0 +1,35 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.learningproblems; + +/** + * Accuracy calculation with true/false positive/negative + */ +public interface AccMethodTwoValued extends AccMethod { + /** + * Compute accuracy according to this method + * @param tp True Positives (positive as positive) + * @param fn False Negative (positive as negative) + * @param fp False Positive (negative as positive) + * @param tn True Negative (negative as negative) + * @param noise Noise + * @return accuracy value or -1 if too weak + */ + double getAccOrTooWeak2(int tp, int fn, int fp, int tn, double noise); +} diff --git a/components-core/src/main/java/org/dllearner/learningproblems/AccMethodTwoValuedApproximate.java b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodTwoValuedApproximate.java new file mode 100644 index 0000000000..807d35c742 --- /dev/null +++ b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodTwoValuedApproximate.java @@ -0,0 +1,41 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.learningproblems; + +import org.semanticweb.owlapi.model.OWLClassExpression; +import org.semanticweb.owlapi.model.OWLIndividual; + +import java.util.Collection; + +/** + * Approximation support for @AccMethodTwoValued + */ +public interface AccMethodTwoValuedApproximate extends AccMethodApproximate, AccMethodTwoValued { + + /** + * calculate approximate accuracy for an expression, according to method + * @param description the expression to test + * @param positiveExamples set of positive examples + * @param negativeExamples set of negative examples + * @param noise noise + * @return approximate accuracy value or -1 if too weak + */ + double getAccApprox2(OWLClassExpression description, Collection positiveExamples, Collection negativeExamples, double noise); + +} diff --git a/components-core/src/main/java/org/dllearner/learningproblems/AccMethodWithBeta.java b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodWithBeta.java new file mode 100644 index 0000000000..2f73790d15 --- /dev/null +++ b/components-core/src/main/java/org/dllearner/learningproblems/AccMethodWithBeta.java @@ -0,0 +1,15 @@ +package org.dllearner.learningproblems; + +/** + * Accuracy Method with beta factor; + * the beta factor is used to indicate the coverage on class learning problems. + * beta also influences weak elimination + */ +public interface AccMethodWithBeta { + /** + * set the beta value; + * the consumer of the accuracy method should call this on all implementing accuracy methods + * @param beta + */ + void setBeta(double beta); +} diff --git a/components-core/src/main/java/org/dllearner/learningproblems/AxiomScore.java b/components-core/src/main/java/org/dllearner/learningproblems/AxiomScore.java index ad8f66955b..c4b832bdb2 100644 --- a/components-core/src/main/java/org/dllearner/learningproblems/AxiomScore.java +++ b/components-core/src/main/java/org/dllearner/learningproblems/AxiomScore.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.learningproblems; import org.dllearner.core.Score; @@ -67,34 +66,37 @@ public AxiomScore(double accuracy, double confidence, int nrOfPositiveExamples, public double getAccuracy() { return accuracy; } - + + /** + * @return the confidence value. + */ public double getConfidence(){ return confidence; } /** - * Whether the score was computed only based on a sample of the knowledge base. - * @return the sampleBased + * @return whether the score was computed only based on a sample of the knowledge base */ public boolean isSampleBased() { return sampleBased; } - + + /** + * @return the total number of examples used to compute the score. + */ public int getTotalNrOfExamples() { return nrOfPositiveExamples + nrOfNegativeExamples; } /** - * Returns the number of positive examples used to compute the score. - * @return + * @return the number of positive examples used to compute the score. */ public int getNrOfPositiveExamples() { return nrOfPositiveExamples; } /** - * Returns the number of negative examples used to compute the score. - * @return + * @return the number of negative examples used to compute the score. */ public int getNrOfNegativeExamples() { return nrOfNegativeExamples; diff --git a/components-core/src/main/java/org/dllearner/learningproblems/ClassAsInstanceLearningProblem.java b/components-core/src/main/java/org/dllearner/learningproblems/ClassAsInstanceLearningProblem.java index e4ce8317be..7b6c826bd4 100644 --- a/components-core/src/main/java/org/dllearner/learningproblems/ClassAsInstanceLearningProblem.java +++ b/components-core/src/main/java/org/dllearner/learningproblems/ClassAsInstanceLearningProblem.java @@ -1,18 +1,27 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.learningproblems; -import java.io.File; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; - +import com.google.common.collect.Sets; import org.dllearner.algorithms.celoe.CELOE; -import org.dllearner.core.AbstractClassExpressionLearningProblem; -import org.dllearner.core.AbstractKnowledgeSource; -import org.dllearner.core.ComponentInitException; -import org.dllearner.core.EvaluatedDescription; +import org.dllearner.core.*; +import org.dllearner.core.StringRenderer.Rendering; import org.dllearner.core.config.ConfigOption; import org.dllearner.kb.OWLAPIOntology; import org.dllearner.learningproblems.Heuristics.HeuristicType; @@ -21,18 +30,18 @@ import org.dllearner.reasoning.ReasonerImplementation; import org.dllearner.utilities.owl.OWLClassExpressionUtils; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.io.ToStringRenderer; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLClass; import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLOntology; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl; -import uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntaxObjectRenderer; -import com.google.common.collect.Sets; +import java.io.File; +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; /** * A learning problem in which positive and negative examples are classes, i.e. @@ -45,6 +54,7 @@ * @author Lorenz Buehmann * */ +@ComponentAnn(name = "Class as Instance LP", shortName = "classasinstance", version = 0.1) public class ClassAsInstanceLearningProblem extends AbstractClassExpressionLearningProblem> { private static final Logger logger = LoggerFactory.getLogger(ClassAsInstanceLearningProblem.class); @@ -54,8 +64,8 @@ public class ClassAsInstanceLearningProblem extends AbstractClassExpressionLearn private HeuristicType heuristic = HeuristicType.PRED_ACC; - protected Set positiveExamples = new TreeSet(); - protected Set negativeExamples = new TreeSet(); + protected Set positiveExamples = new TreeSet<>(); + protected Set negativeExamples = new TreeSet<>(); @@ -71,10 +81,10 @@ public void init() throws ComponentInitException { */ @Override public ScorePosNeg computeScore(OWLClassExpression description, double noise) { - SortedSet posAsPos = new TreeSet(); - SortedSet posAsNeg = new TreeSet(); - SortedSet negAsPos = new TreeSet(); - SortedSet negAsNeg = new TreeSet(); + SortedSet posAsPos = new TreeSet<>(); + SortedSet posAsNeg = new TreeSet<>(); + SortedSet negAsPos = new TreeSet<>(); + SortedSet negAsNeg = new TreeSet<>(); // for each positive example, we check whether it is a subclass of the given concept for (OWLClass example : positiveExamples) { @@ -94,9 +104,9 @@ public ScorePosNeg computeScore(OWLClassExpression description, double } // compute the accuracy - double accuracy = getAccuracy(description); + double accuracy = getAccuracyOrTooWeak(description); - return new ScoreTwoValued(OWLClassExpressionUtils.getLength(description), percentPerLengthUnit, posAsPos, posAsNeg, + return new ScoreTwoValued<>(OWLClassExpressionUtils.getLength(description), percentPerLengthUnit, posAsPos, posAsNeg, negAsPos, negAsNeg, accuracy); } @@ -109,14 +119,6 @@ public EvaluatedDescription evaluate(OWLClassExpression description) { return new EvaluatedDescriptionPosNeg(description, score); } - /* (non-Javadoc) - * @see org.dllearner.core.AbstractLearningProblem#getAccuracy(org.dllearner.core.owl.Description) - */ - @Override - public double getAccuracy(OWLClassExpression description, double noise) { - return getAccuracyOrTooWeak(description, noise); - } - /* (non-Javadoc) * @see org.dllearner.core.AbstractLearningProblem#getAccuracyOrTooWeak(org.dllearner.core.owl.Description, double) */ @@ -236,7 +238,7 @@ public void setPercentPerLengthUnit(double percentPerLengthUnit) { } public static void main(String[] args) throws Exception{ - ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); + StringRenderer.setRenderer(Rendering.DL_SYNTAX); File file = new File("../examples/father.owl"); OWLClass cls1 = new OWLClassImpl(IRI.create("http://example.com/father#male")); OWLClass cls2 = new OWLClassImpl(IRI.create("http://example.com/father#female")); diff --git a/components-core/src/main/java/org/dllearner/learningproblems/ClassLearningProblem.java b/components-core/src/main/java/org/dllearner/learningproblems/ClassLearningProblem.java index 67003b60bc..105906ce36 100644 --- a/components-core/src/main/java/org/dllearner/learningproblems/ClassLearningProblem.java +++ b/components-core/src/main/java/org/dllearner/learningproblems/ClassLearningProblem.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2012, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,892 +16,228 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.learningproblems; -import java.util.Collections; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Random; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; - -import org.dllearner.core.AbstractClassExpressionLearningProblem; -import org.dllearner.core.AbstractReasonerComponent; -import org.dllearner.core.AnnComponentManager; -import org.dllearner.core.ComponentAnn; -import org.dllearner.core.ComponentInitException; +import com.google.common.collect.Sets; +import org.dllearner.core.*; import org.dllearner.core.config.ConfigOption; -import org.dllearner.learningproblems.Heuristics.HeuristicType; -import org.dllearner.reasoning.SPARQLReasoner; -import org.dllearner.utilities.Helper; -import org.dllearner.utilities.owl.OWLClassExpressionToSPARQLConverter; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLObjectIntersectionOf; +import org.dllearner.utilities.ReasoningUtils; +import org.dllearner.utilities.ReasoningUtils.Coverage; +import org.dllearner.utilities.ReasoningUtilsCLP; +import org.semanticweb.owlapi.model.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - +import org.springframework.beans.factory.annotation.Autowired; import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; -import com.hp.hpl.jena.query.ParameterizedSparqlString; -import com.hp.hpl.jena.query.QueryExecution; +import java.util.*; /** * The problem of learning the OWL class expression of an existing class * in an OWL ontology. - * + * * @author Jens Lehmann * */ @ComponentAnn(name = "ClassLearningProblem", shortName = "clp", version = 0.6) public class ClassLearningProblem extends AbstractClassExpressionLearningProblem { - + private static Logger logger = LoggerFactory.getLogger(ClassLearningProblem.class); - private long nanoStartTime; - @ConfigOption(name="maxExecutionTimeInSeconds",defaultValue="10",description="Maximum execution time in seconds") + private long nanoStartTime; + @ConfigOption(name = "maxExecutionTimeInSeconds", + defaultValue = "10", + description = "Maximum execution time in seconds") private int maxExecutionTimeInSeconds = 10; - - @ConfigOption(name = "classToDescribe", description="class of which an OWL class expression should be learned", required=true) + + @ConfigOption(name = "classToDescribe", + description = "class of which an OWL class expression should be learned", + required = true) private OWLClass classToDescribe; - + private List classInstances; private TreeSet classInstancesSet; - @ConfigOption(name="equivalence",defaultValue="true",description="Whether this is an equivalence problem (or superclass learning problem)") + @ConfigOption(name = "equivalence", + defaultValue = "true", + description = "Whether this is an equivalence problem (or superclass learning problem)") private boolean equivalence = true; - @ConfigOption(name = "approxDelta", description = "The Approximate Delta", defaultValue = "0.05", required = false) - private double approxDelta = 0.05; - - @ConfigOption(name = "useApproximations", description = "Use Approximations", defaultValue = "false", required = false) - private boolean useApproximations; - - // factor for higher weight on recall (needed for subclass learning) - private double coverageFactor; - - @ConfigOption(name = "betaSC", description="beta index for F-measure in super class learning", required=false, defaultValue="3.0") + @ConfigOption(name = "betaSC", + description = "beta index for F-measure in super class learning", + required = false, + defaultValue = "3.0") private double betaSC = 3.0; - - @ConfigOption(name = "betaEq", description="beta index for F-measure in definition learning", required=false, defaultValue="1.0") + + @ConfigOption(name = "betaEq", + description = "beta index for F-measure in definition learning", + required = false, + defaultValue = "1.0") private double betaEq = 1.0; - + // instances of super classes excluding instances of the class itself private List superClassInstances; // instances of super classes including instances of the class itself private List classAndSuperClassInstances; // specific variables for generalised F-measure private TreeSet negatedClassInstances; - - @ConfigOption(name = "accuracyMethod", description = "Specifies, which method/function to use for computing accuracy. Available measues are \"pred_acc\" (predictive accuracy), \"fmeasure\" (F measure), \"generalised_fmeasure\" (generalised F-Measure according to Fanizzi and d'Amato).",defaultValue = "pred_acc") - private String accuracyMethod = "pred_acc"; - - private HeuristicType heuristic = HeuristicType.AMEASURE; - - @ConfigOption(name = "checkConsistency", description = "whether to check for consistency of suggestions (when added to ontology)", required=false, defaultValue="true") + + @ConfigOption(name = "accuracyMethod", + description = "Specifies, which method/function to use for computing accuracy. Available measues are \"pred_acc\" (predictive accuracy), \"fmeasure\" (F measure), \"generalised_fmeasure\" (generalised F-Measure according to Fanizzi and d'Amato).", + defaultValue = "PRED_ACC") + protected AccMethod accuracyMethod; + + @ConfigOption(name = "checkConsistency", + description = "whether to check for consistency of suggestions (when added to ontology)", + required = false, + defaultValue = "true") private boolean checkConsistency = true; - + private OWLDataFactory df = new OWLDataFactoryImpl(); - @ConfigOption(name="useInstanceChecks",defaultValue="true",description="Whether to use instance checks (or get individiuals query)") - private boolean useInstanceChecks = true; - - private OWLClassExpressionToSPARQLConverter converter = new OWLClassExpressionToSPARQLConverter(); + public ClassLearningProblem() { - + } - -// public ClassLearningProblemConfigurator getConfigurator(){ -// return configurator; -// } - + + protected ReasoningUtils newReasoningUtils(AbstractReasonerComponent reasoner) { + return new ReasoningUtilsCLP(this, reasoner); + } + public ClassLearningProblem(AbstractReasonerComponent reasoner) { super(reasoner); -// configurator = new ClassLearningProblemConfigurator(this); } - - /* - public static Collection> createConfigOptions() { - Collection> options = new LinkedList>(); - URLConfigOption classToDescribeOption = new URLConfigOption("classToDescribe", "class of which a OWLClassExpression should be learned", null, true, false); - classToDescribeOption.setRefersToOWLClass(true); - options.add(classToDescribeOption); - StringConfigOption type = new StringConfigOption("type", "whether to learn an equivalence class or super class axiom","equivalence"); // or domain/range of a property. - type.setAllowedValues(new String[] {"equivalence", "superClass"}); // , "domain", "range"}); - options.add(type); - BooleanConfigOption approx = new BooleanConfigOption("useApproximations", "whether to use stochastic approximations for computing accuracy", true); - options.add(approx); - DoubleConfigOption approxAccuracy = new DoubleConfigOption("approxAccuracy", "accuracy of the approximation (only for expert use)", 0.05); - options.add(approxAccuracy); - StringConfigOption accMethod = new StringConfigOption("accuracyMethod", "Specifies, which method/function to use for computing accuracy.","standard"); // or domain/range of a property. - accMethod.setAllowedValues(new String[] {"standard", "fmeasure", "pred_acc", "generalised_fmeasure", "jaccard"}); - options.add(accMethod); - BooleanConfigOption consistency = new BooleanConfigOption("checkConsistency", "Specify whether to check consistency for solution candidates. This is convenient for user interfaces, but can be performance intensive.", true); - options.add(consistency); - options.add(CommonConfigOptions.maxExecutionTimeInSeconds(10)); - DoubleConfigOption betaSC = new DoubleConfigOption("betaSC", "Higher values of beta rate recall higher than precision or in other words, covering the instances of the class to describe is more important even at the cost of covering additional instances. The actual implementation depends on the selected heuristic. This values is used only for super class learning.", 3.0); - options.add(betaSC); - DoubleConfigOption betaEq = new DoubleConfigOption("betaEq", "Higher values of beta rate recall higher than precision or in other words, covering the instances of the class to describe is more important even at the cost of covering additional instances. The actual implementation depends on the selected heuristic. This values is used only for equivalence class learning.", 1.0); - options.add(betaEq); - return options; - } - */ public static String getName() { return "class learning problem"; } - + @Override public void init() throws ComponentInitException { -// classToDescribe = df.getOWLClass(IRI.create(configurator.getClassToDescribe().toString()); -// useApproximations = configurator.getUseApproximations(); - - String accM = accuracyMethod; - if(accM.equals("standard")) { - heuristic = HeuristicType.AMEASURE; - } else if(accM.equals("fmeasure")) { - heuristic = HeuristicType.FMEASURE; - } else if(accM.equals("generalised_fmeasure")) { - heuristic = HeuristicType.GEN_FMEASURE; - } else if(accM.equals("jaccard")) { - heuristic = HeuristicType.JACCARD; - } else if(accM.equals("pred_acc")) { - heuristic = HeuristicType.PRED_ACC; - } - - if(useApproximations && heuristic.equals(HeuristicType.PRED_ACC)) { - System.err.println("Approximating predictive accuracy is an experimental feature. USE IT AT YOUR OWN RISK. If you consider to use it for anything serious, please extend the unit tests at org.dllearner.test.junit.HeuristicTests first to verify that it works."); - } - - if(useApproximations && !(heuristic.equals(HeuristicType.PRED_ACC) || heuristic.equals(HeuristicType.AMEASURE) || heuristic.equals(HeuristicType.FMEASURE))) { - throw new ComponentInitException("Approximations only supported for F-Measure or Standard-Measure. It is unsupported for \"" + heuristic + ".\""); + + if (accuracyMethod != null && accuracyMethod instanceof AccMethodPredAccApprox) { + logger.warn("Approximating predictive accuracy is an experimental feature. USE IT AT YOUR OWN RISK. If you consider to use it for anything serious, please extend the unit tests at org.dllearner.test.junit.HeuristicTests first to verify that it works."); } - -// useFMeasure = configurator.getAccuracyMethod().equals("fmeasure"); -// approxDelta = configurator.getApproxAccuracy(); - - if(!getReasoner().getClasses().contains(classToDescribe)) { + + if (!getReasoner().getClasses().contains(classToDescribe)) { throw new ComponentInitException("The class \"" + classToDescribe + "\" does not exist. Make sure you spelled it correctly."); } - - classInstances = new LinkedList(getReasoner().getIndividuals(classToDescribe)); + + classInstances = new LinkedList<>(getReasoner().getIndividuals(classToDescribe)); // sanity check - if(classInstances.size() == 0) { + if (classInstances.size() == 0) { throw new ComponentInitException("Class " + classToDescribe + " has 0 instances according to \"" + AnnComponentManager.getName(getReasoner().getClass()) + "\". Cannot perform class learning with 0 instances."); } - - classInstancesSet = new TreeSet(classInstances); -// equivalence = (configurator.getType().equals("equivalence")); -// maxExecutionTimeInSeconds = configurator.getMaxExecutionTimeInSeconds(); - - if(equivalence) { + + classInstancesSet = new TreeSet<>(classInstances); + + double coverageFactor; + if (equivalence) { coverageFactor = betaEq; } else { coverageFactor = betaSC; } - + // we compute the instances of the super class to perform // optimisations later on Set superClasses = getReasoner().getSuperClasses(classToDescribe); - TreeSet superClassInstancesTmp = new TreeSet(getReasoner().getIndividuals()); - for(OWLClassExpression superClass : superClasses) { + TreeSet superClassInstancesTmp = new TreeSet<>(getReasoner().getIndividuals()); + for (OWLClassExpression superClass : superClasses) { superClassInstancesTmp.retainAll(getReasoner().getIndividuals(superClass)); } // we create one list, which includes instances of the class (an instance of the class is also instance of all super classes) ... - classAndSuperClassInstances = new LinkedList(superClassInstancesTmp); + classAndSuperClassInstances = new LinkedList<>(superClassInstancesTmp); // ... and a second list not including them superClassInstancesTmp.removeAll(classInstances); // since we use the instance list for approximations, we want to avoid // any bias through URI names, so we shuffle the list once pseudo-randomly - superClassInstances = new LinkedList(superClassInstancesTmp); + superClassInstances = new LinkedList<>(superClassInstancesTmp); Random rand = new Random(1); Collections.shuffle(classInstances, rand); Collections.shuffle(superClassInstances, rand); - - if(heuristic.equals(HeuristicType.GEN_FMEASURE)) { - OWLClassExpression classToDescribeNeg = df.getOWLObjectComplementOf(classToDescribe); - negatedClassInstances = new TreeSet(); - for(OWLIndividual ind : superClassInstances) { - if(getReasoner().hasType(classToDescribeNeg, ind)) { - negatedClassInstances.add(ind); - } - } + + if (accuracyMethod == null) { + accuracyMethod = new AccMethodPredAcc(true); + } + if (accuracyMethod instanceof AccMethodApproximate) { + ((AccMethodApproximate) accuracyMethod).setReasoner(getReasoner()); + } + if (accuracyMethod instanceof AccMethodThreeValued) { + Coverage[] cc = reasoningUtil.getCoverage(df.getOWLObjectComplementOf(classToDescribe), Sets.newTreeSet(superClassInstances)); + negatedClassInstances = Sets.newTreeSet(cc[0].trueSet); // System.out.println("negated class instances: " + negatedClassInstances); } - + if (accuracyMethod instanceof AccMethodWithBeta) { + ((AccMethodWithBeta)accuracyMethod).setBeta(coverageFactor); + } + // System.out.println(classInstances.size() + " " + superClassInstances.size()); } - + @Override public ClassScore computeScore(OWLClassExpression description, double noise) { - + // TODO: reuse code to ensure that we never return inconsistent results // between getAccuracy, getAccuracyOrTooWeak and computeScore - Set additionalInstances = new TreeSet(); - Set coveredInstances = new TreeSet(); - - int additionalInstancesCnt = 0; - int coveredInstancesCnt = 0; - - if(reasoner.getClass().isAssignableFrom(SPARQLReasoner.class)) { - // R(C) - String query = "SELECT (COUNT(DISTINCT(?s)) AS ?cnt) WHERE {" - + "?s a ?sup . ?classToDescribe ?sup . " - + converter.convert("?s", description) - + "FILTER NOT EXISTS {?s a ?classToDescribe}}"; - ParameterizedSparqlString template = new ParameterizedSparqlString(query); - //System.err.println(converter.convert("?s", description)); - //template.setIri("cls", description.asOWLClass().toStringID()); - template.setIri("classToDescribe", classToDescribe.toStringID()); - - QueryExecution qe = ((SPARQLReasoner) reasoner).getQueryExecutionFactory().createQueryExecution(template.toString()); - additionalInstancesCnt = qe.execSelect().next().getLiteral("cnt").getInt(); - - // R(A) - OWLObjectIntersectionOf ce = df.getOWLObjectIntersectionOf(classToDescribe, description); - coveredInstancesCnt = ((SPARQLReasoner) reasoner).getPopularityOf(ce); - } else { - // overhang - for(OWLIndividual ind : superClassInstances) { - if(getReasoner().hasType(description, ind)) { - additionalInstances.add(ind); - } - } - - // coverage - for(OWLIndividual ind : classInstances) { - if(getReasoner().hasType(description, ind)) { - coveredInstances.add(ind); - } - } - additionalInstancesCnt = additionalInstances.size(); - coveredInstancesCnt = coveredInstances.size(); - } - - double recall = coveredInstancesCnt/(double)classInstances.size(); - double precision = (additionalInstancesCnt + coveredInstancesCnt == 0) ? 0 : coveredInstancesCnt/(double)(coveredInstancesCnt+additionalInstancesCnt); + Coverage[] cc = ((ReasoningUtilsCLP)reasoningUtil).getCoverageCLP(description, Sets.newTreeSet(classInstances), Sets.newTreeSet(superClassInstances)); + + double recall = Heuristics.divideOrZero(cc[0].trueCount, classInstances.size()); // tp / (tp+fn) + double precision = Heuristics.divideOrZero(cc[0].trueCount, cc[0].trueCount + cc[1].trueCount); // tp / (tp+fp) // for each OWLClassExpression with less than 100% coverage, we check whether it is // leads to an inconsistent knowledge base - - double acc = 0; - if(heuristic.equals(HeuristicType.FMEASURE)) { - acc = Heuristics.getFScore(recall, precision, coverageFactor); - } else if(heuristic.equals(HeuristicType.AMEASURE)) { - acc = Heuristics.getAScore(recall, precision, coverageFactor); + + double acc; + if (accuracyMethod instanceof AccMethodTwoValued) { + acc = reasoningUtil.getAccuracyOrTooWeakExact2((AccMethodTwoValued) accuracyMethod, cc, noise); + } else if (accuracyMethod instanceof AccMethodThreeValued) { + acc = ((ReasoningUtilsCLP)reasoningUtil).getAccuracyOrTooWeakExact3((AccMethodThreeValued) accuracyMethod, description, Sets.newTreeSet(classInstances), Sets.newTreeSet(superClassInstances), negatedClassInstances, noise); } else { - // TODO: some superfluous instance checks are required to compute accuracy => - // move accuracy computation here if possible - acc = getAccuracyOrTooWeakExact(description, noise); + throw new RuntimeException(); } - - if(checkConsistency) { - + + if (checkConsistency) { + // we check whether the axiom already follows from the knowledge base -// boolean followsFromKB = reasoner.isSuperClassOf(description, classToDescribe); - -// boolean followsFromKB = equivalence ? reasoner.isEquivalentClass(description, classToDescribe) : reasoner.isSuperClassOf(description, classToDescribe); boolean followsFromKB = followsFromKB(description); - + // workaround due to a bug (see http://sourceforge.net/tracker/?func=detail&aid=2866610&group_id=203619&atid=986319) -// boolean isConsistent = coverage >= 0.999999 || isConsistent(description); // (if the axiom follows, then the knowledge base remains consistent) boolean isConsistent = followsFromKB || isConsistent(description); - -// double acc = useFMeasure ? getFMeasure(coverage, protusion) : getAccuracy(coverage, protusion); - return new ClassScore(coveredInstances, Helper.difference(classInstancesSet, coveredInstances), recall, additionalInstances, precision, acc, isConsistent, followsFromKB); - + + return new ClassScore(cc[0].trueSet, cc[0].falseSet, recall, cc[1].trueSet, precision, acc, isConsistent, followsFromKB); + } else { - return new ClassScore(coveredInstances, Helper.difference(classInstancesSet, coveredInstances), recall, additionalInstances, precision, acc); + return new ClassScore(cc[0].trueSet, cc[0].falseSet, recall, cc[1].trueSet, precision, acc); } } - + public boolean isEquivalenceProblem() { return equivalence; } - /* (non-Javadoc) - * @see org.dllearner.core.LearningProblem#getAccuracy(org.dllearner.core.owl.Description) - */ - @Override - public double getAccuracy(OWLClassExpression description, double noise) { - // a noise value of 1.0 means that we never return too weak (-1.0) - return getAccuracyOrTooWeak(description, noise); - } - @Override public double getAccuracyOrTooWeak(OWLClassExpression description, double noise) { - // delegates to the appropriate methods - return useApproximations ? getAccuracyOrTooWeakApprox(description, noise) : getAccuracyOrTooWeakExact(description, noise); - } - - // instead of using the standard operation, we use optimisation - // and approximation here - public double getAccuracyOrTooWeakApprox(OWLClassExpression description, double noise) { - if(heuristic.equals(HeuristicType.FMEASURE)) { - // we abort when there are too many uncovered positives - int maxNotCovered = (int) Math.ceil(noise*classInstances.size()); - int instancesCovered = 0; - int instancesNotCovered = 0; - - for(OWLIndividual ind : classInstances) { - if(getReasoner().hasType(description, ind)) { - instancesCovered++; - } else { - instancesNotCovered ++; - if(instancesNotCovered > maxNotCovered) { - return -1; - } - } - } - - double recall = instancesCovered/(double)classInstances.size(); - - int testsPerformed = 0; - int instancesDescription = 0; - - for(OWLIndividual ind : superClassInstances) { - - if(getReasoner().hasType(description, ind)) { - instancesDescription++; - } - testsPerformed++; - - // check whether approximation is sufficiently accurate - double[] approx = Heuristics.getFScoreApproximation(instancesCovered, recall, coverageFactor, superClassInstances.size(), testsPerformed, instancesDescription); - if(approx[1] maxNotCovered) { - return -1; - } - } - - // approximation step (starting after 10 tests) - total = instancesCovered + instancesNotCovered; - if(total > 10) { - // compute confidence interval - double p1 = p1(instancesCovered, total); - double p2 = p3(p1, total); - lowerBorderA = Math.max(0, p1 - p2); - upperBorderA = Math.min(1, p1 + p2); - double size = upperBorderA - lowerBorderA; - // if the interval has a size smaller than 10%, we can be confident - if(size < 2 * approxDelta) { - // we have to distinguish the cases that the accuracy limit is - // below, within, or above the limit and that the mean is below - // or above the limit - double mean = instancesCovered/(double)total; - - // we can estimate the best possible concept to reach with downward refinement - // by setting precision to 1 and recall = mean stays as it is - double optimumEstimate = heuristic.equals(HeuristicType.FMEASURE) ? ((1+Math.sqrt(coverageFactor))*mean)/(Math.sqrt(coverageFactor)+1) : (coverageFactor*mean+1)/(coverageFactor+1); - - // if the mean is greater than the required minimum, we can accept; - // we also accept if the interval is small and close to the minimum - // (worst case is to accept a few inaccurate descriptions) - if(optimumEstimate > 1-noise-0.03) { -// || (upperBorderA > mean && size < 0.03)) { - instancesCovered = (int) (instancesCovered/(double)total * classInstances.size()); - upperEstimateA = (int) (upperBorderA * classInstances.size()); - lowerEstimateA = (int) (lowerBorderA * classInstances.size()); - estimatedA = true; - break; - } - - // reject only if the upper border is far away (we are very - // certain not to lose a potential solution) -// if(upperBorderA + 0.1 < 1-noise) { - double optimumEstimateUpperBorder = heuristic.equals(HeuristicType.FMEASURE) ? ((1+Math.sqrt(coverageFactor))*(upperBorderA+0.1))/(Math.sqrt(coverageFactor)+1) : (coverageFactor*(upperBorderA+0.1)+1)/(coverageFactor+1); - if(optimumEstimateUpperBorder < 1 - noise) { - return -1; - } - } - } - } - - double recall = instancesCovered/(double)classInstances.size(); - -// MonitorFactory.add("estimatedA","count", estimatedA ? 1 : 0); -// MonitorFactory.add("aInstances","count", total); - - // we know that a definition candidate is always subclass of the - // intersection of all super classes, so we test only the relevant instances - // (leads to undesired effects for descriptions not following this rule, - // but improves performance a lot); - // for learning a superclass of a defined class, similar observations apply; - - - int testsPerformed = 0; - int instancesDescription = 0; -// boolean estimatedB = false; - - for(OWLIndividual ind : superClassInstances) { - - if(getReasoner().hasType(description, ind)) { - instancesDescription++; - } - - testsPerformed++; - - if(testsPerformed > 10) { - - // compute confidence interval - double p1 = p1(instancesDescription, testsPerformed); - double p2 = p3(p1, testsPerformed); - double lowerBorder = Math.max(0, p1 - p2); - double upperBorder = Math.min(1, p1 + p2); - int lowerEstimate = (int) (lowerBorder * superClassInstances.size()); - int upperEstimate = (int) (upperBorder * superClassInstances.size()); - - double size; - if(estimatedA) { -// size = 1/(coverageFactor+1) * (coverageFactor * (upperBorderA-lowerBorderA) + Math.sqrt(upperEstimateA/(upperEstimateA+lowerEstimate)) + Math.sqrt(lowerEstimateA/(lowerEstimateA+upperEstimate))); - size = heuristic.equals(HeuristicType.FMEASURE) ? getFMeasure(upperBorderA, upperEstimateA/(double)(upperEstimateA+lowerEstimate)) - getFMeasure(lowerBorderA, lowerEstimateA/(double)(lowerEstimateA+upperEstimate)) : Heuristics.getAScore(upperBorderA, upperEstimateA/(double)(upperEstimateA+lowerEstimate), coverageFactor) - Heuristics.getAScore(lowerBorderA, lowerEstimateA/(double)(lowerEstimateA+upperEstimate),coverageFactor); - } else { -// size = 1/(coverageFactor+1) * (coverageFactor * coverage + Math.sqrt(instancesCovered/(instancesCovered+lowerEstimate)) + Math.sqrt(instancesCovered/(instancesCovered+upperEstimate))); - size = heuristic.equals(HeuristicType.FMEASURE) ? getFMeasure(recall, instancesCovered/(double)(instancesCovered+lowerEstimate)) - getFMeasure(recall, instancesCovered/(double)(instancesCovered+upperEstimate)) : Heuristics.getAScore(recall, instancesCovered/(double)(instancesCovered+lowerEstimate),coverageFactor) - Heuristics.getAScore(recall, instancesCovered/(double)(instancesCovered+upperEstimate),coverageFactor); - } - - if(size < 0.1) { -// System.out.println(instancesDescription + " of " + testsPerformed); -// System.out.println("interval from " + lowerEstimate + " to " + upperEstimate); -// System.out.println("size: " + size); - -// estimatedB = true; - // calculate total number of instances - instancesDescription = (int) (instancesDescription/(double)testsPerformed * superClassInstances.size()); - break; - } - } - } - - // since we measured/estimated accuracy only on instances outside A (superClassInstances - // does not include instances of A), we need to add it in the denominator - double precision = instancesCovered/(double)(instancesDescription+instancesCovered); - if(instancesCovered + instancesDescription == 0) { - precision = 0; - } - - return heuristic.equals(HeuristicType.FMEASURE) ? getFMeasure(recall, precision) : Heuristics.getAScore(recall, precision, coverageFactor); - - } else if(heuristic.equals(HeuristicType.FMEASURE)) { - int maxNotCovered = (int) Math.ceil(noise*classInstances.size()); - - int notCoveredPos = 0; -// int notCoveredNeg = 0; - - int posClassifiedAsPos = 0; - int negClassifiedAsNeg = 0; - - int nrOfPosChecks = 0; - int nrOfNegChecks = 0; - - // special case: we test positive and negative examples in turn - Iterator itPos = classInstances.iterator(); - Iterator itNeg = superClassInstances.iterator(); - - do { - // in each loop we pick 0 or 1 positives and 0 or 1 negative - // and classify it - - if(itPos.hasNext()) { - OWLIndividual posExample = itPos.next(); -// System.out.println(posExample); - - if(getReasoner().hasType(description, posExample)) { - posClassifiedAsPos++; - } else { - notCoveredPos++; - } - nrOfPosChecks++; - - // take noise into account - if(notCoveredPos > maxNotCovered) { - return -1; - } - } - - if(itNeg.hasNext()) { - OWLIndividual negExample = itNeg.next(); - if(!getReasoner().hasType(description, negExample)) { - negClassifiedAsNeg++; - } - nrOfNegChecks++; - } - - // compute how accurate our current approximation is and return if it is sufficiently accurate - double approx[] = Heuristics.getPredAccApproximation(classInstances.size(), superClassInstances.size(), 1, nrOfPosChecks, posClassifiedAsPos, nrOfNegChecks, negClassifiedAsNeg); - if(approx[1] coveredInstancesSet = new TreeSet(); - for(OWLIndividual ind : classInstances) { - if(getReasoner().hasType(description, ind)) { - coveredInstancesSet.add(ind); - } - if(terminationTimeExpired()){ - return 0; - } - } - - // if even the optimal case (no additional instances covered) is not sufficient, - // the concept is too weak - if(coveredInstancesSet.size() / (double) classInstances.size() <= 1 - noise) { - return -1; - } - - // computing R(C) restricted to relevant instances - TreeSet additionalInstancesSet = new TreeSet(); - for(OWLIndividual ind : superClassInstances) { - if(getReasoner().hasType(description, ind)) { - additionalInstancesSet.add(ind); - } - if(terminationTimeExpired()){ - return 0; - } - } - - Set union = Helper.union(classInstancesSet, additionalInstancesSet); - return Heuristics.getJaccardCoefficient(coveredInstancesSet.size(), union.size()); - - } else if (heuristic.equals(HeuristicType.AMEASURE) || heuristic.equals(HeuristicType.FMEASURE) || heuristic.equals(HeuristicType.PRED_ACC)) { - - int additionalInstances = 0; - int coveredInstances = 0; - - if(reasoner.getClass().isAssignableFrom(SPARQLReasoner.class)) { - // R(C) - String query = "SELECT (COUNT(DISTINCT(?s)) AS ?cnt) WHERE {" - + "?s a ?sup . ?classToDescribe ?sup . " - + converter.convert("?s", description) - + "FILTER NOT EXISTS {?s a ?classToDescribe}}"; - ParameterizedSparqlString template = new ParameterizedSparqlString(query); - //System.err.println(converter.convert("?s", description)); - //template.setIri("cls", description.asOWLClass().toStringID()); - template.setIri("classToDescribe", classToDescribe.toStringID()); - - QueryExecution qe = ((SPARQLReasoner) reasoner).getQueryExecutionFactory().createQueryExecution(template.toString()); - additionalInstances = qe.execSelect().next().getLiteral("cnt").getInt(); - - // R(A) - OWLObjectIntersectionOf ce = df.getOWLObjectIntersectionOf(classToDescribe, description); - coveredInstances = ((SPARQLReasoner) reasoner).getPopularityOf(ce); - - //System.out.println(coveredInstances); - //System.out.println(additionalInstances); - } else { - // computing R(C) restricted to relevant instances - if(useInstanceChecks) { - for(OWLIndividual ind : superClassInstances) { - if(getReasoner().hasType(description, ind)) { - additionalInstances++; - } - if(terminationTimeExpired()){ - return 0; - } - } - } else { - SortedSet individuals = getReasoner().getIndividuals(description); - individuals.retainAll(superClassInstances); - additionalInstances = individuals.size(); - } - - - // computing R(A) - if(useInstanceChecks) { - for(OWLIndividual ind : classInstances) { - if(getReasoner().hasType(description, ind)) { - coveredInstances++; - } - if(terminationTimeExpired()){ - return 0; - } - } - } else { - SortedSet individuals = getReasoner().getIndividuals(description); - individuals.retainAll(classInstances); - coveredInstances = individuals.size(); - } - } - -// System.out.println(description + ":" + coveredInstances + "/" + classInstances.size()); - double recall = coveredInstances/(double)classInstances.size(); - - // noise computation is incorrect -// if(recall < 1 - noise) { -// return -1; -// } - - double precision = (additionalInstances + coveredInstances == 0) ? 0 : coveredInstances / (double) (coveredInstances + additionalInstances); - - if(heuristic.equals(HeuristicType.AMEASURE)) { - // best reachable concept has same recall and precision 1: - // 1/t+1 * (t*r + 1) - if((coverageFactor*recall+1)/(coverageFactor+1) <(1-noise)) { - return -1; - } else { - return Heuristics.getAScore(recall, precision, coverageFactor); - } - } else if(heuristic.equals(HeuristicType.FMEASURE)) { - // best reachable concept has same recall and precision 1: - if(((1+Math.sqrt(coverageFactor))*recall)/(Math.sqrt(coverageFactor)+1)<1-noise) { - return -1; - } else { - return Heuristics.getFScore(recall, precision, coverageFactor); - } - } else if(heuristic.equals(HeuristicType.PRED_ACC)) { - if((coverageFactor * coveredInstances + superClassInstances.size()) / (coverageFactor * classInstances.size() + superClassInstances.size()) < 1 -noise) { - return -1; - } else { - // correctly classified divided by all examples - return (coverageFactor * coveredInstances + superClassInstances.size() - additionalInstances) / (coverageFactor * classInstances.size() + superClassInstances.size()); - } - } - -// return heuristic.equals(HeuristicType.FMEASURE) ? getFMeasure(recall, precision) : getAccuracy(recall, precision); - } else if (heuristic.equals(HeuristicType.GEN_FMEASURE)) { - - // implementation is based on: - // http://sunsite.informatik.rwth-aachen.de/Publications/CEUR-WS/Vol-426/swap2008_submission_14.pdf - // default negation should be turned off when using fast instance checker - - // compute I_C (negated and non-negated concepts separately) - TreeSet icPos = new TreeSet(); - TreeSet icNeg = new TreeSet(); - OWLClassExpression descriptionNeg = df.getOWLObjectComplementOf(description); - // loop through all relevant instances - for(OWLIndividual ind : classAndSuperClassInstances) { - if(getReasoner().hasType(description, ind)) { - icPos.add(ind); - } else if(getReasoner().hasType(descriptionNeg, ind)) { - icNeg.add(ind); - } - if(terminationTimeExpired()){ - return 0; - } - } - - // semantic precision - // first compute I_C \cap Cn(DC) - // it seems that in our setting, we can ignore Cn, because the examples (class instances) - // are already part of the background knowledge - Set tmp1Pos = Helper.intersection(icPos, classInstancesSet); - Set tmp1Neg = Helper.intersection(icNeg, negatedClassInstances); - int tmp1Size = tmp1Pos.size() + tmp1Neg.size(); - - // Cn(I_C) \cap D_C is the same set if we ignore Cn ... - - int icSize = icPos.size() + icNeg.size(); - double prec = (icSize == 0) ? 0 : tmp1Size / (double) icSize; - double rec = tmp1Size / (double) (classInstances.size() + negatedClassInstances.size()); - -// System.out.println(description); - -// System.out.println("I_C pos: " + icPos); -// System.out.println("I_C neg: " + icNeg); -// System.out.println("class instances: " + classInstances); -// System.out.println("negated class instances: " + negatedClassInstances); - -// System.out.println(prec); -// System.out.println(rec); -// System.out.println(coverageFactor); - - // too weak: see F-measure above - // => does not work for generalised F-measure, because even very general - // concepts do not have a recall of 1 -// if(((1+Math.sqrt(coverageFactor))*rec)/(Math.sqrt(coverageFactor)+1)<1-noise) { -// return -1; -// } - // we only return too weak if there is no recall - if(rec <= 0.0000001) { - return -1; - } - - return getFMeasure(rec,prec); - } - - throw new Error("ClassLearningProblem error: not implemented"); - } - - private boolean terminationTimeExpired(){ - boolean val = ((System.nanoTime() - nanoStartTime) >= (maxExecutionTimeInSeconds*1000000000l)); - if(val) { + public boolean terminationTimeExpired() { + boolean val = ((System.nanoTime() - nanoStartTime) >= (maxExecutionTimeInSeconds * 1000000000L)); + if (val) { logger.warn("Description test aborted, because it took longer than " + maxExecutionTimeInSeconds + " seconds."); } return val; } - -// @Deprecated -// public double getAccuracyOrTooWeakStandard(OWLClassExpression description, double minAccuracy) { -// // since we have to perform a retrieval operation anyway, we cannot easily -// // get a benefit from the accuracy limit -// double accuracy = getAccuracy(description); -// if(accuracy >= minAccuracy) { -// return accuracy; -// } else { -// return -1; -// } -// } - - // please note that getting recall and precision wastes some computational - // resource, because both methods need to compute the covered instances - public double getRecall(OWLClassExpression description) { - int coveredInstances = 0; - for(OWLIndividual ind : classInstances) { - if(getReasoner().hasType(description, ind)) { - coveredInstances++; - } - } - return coveredInstances/(double)classInstances.size(); - } - - public double getPrecision(OWLClassExpression description) { - - int additionalInstances = 0; - for(OWLIndividual ind : superClassInstances) { - if(getReasoner().hasType(description, ind)) { - additionalInstances++; - } - } - - int coveredInstances = 0; - for(OWLIndividual ind : classInstances) { - if(getReasoner().hasType(description, ind)) { - coveredInstances++; - } - } - return (additionalInstances + coveredInstances == 0) ? 0 : coveredInstances / (double) (coveredInstances + additionalInstances); - } - - public double getPredictiveAccuracy() { - return 0; - } - + // see http://sunsite.informatik.rwth-aachen.de/Publications/CEUR-WS/Vol-426/swap2008_submission_14.pdf // for all methods below (currently dummies) - public double getMatchRate() { - return 0; - } - - public double getOmissionError() { - return 0; - } - - public double getInductionRate() { - return 0; - } - - public double getComissionError() { - return 0; - } - - public double getGeneralisedRecall() { - return 0; - } - - public double getGeneralisedPrecision() { - return 0; - } - - @SuppressWarnings("unused") - private double getInverseJaccardDistance(TreeSet set1, TreeSet set2) { - Set intersection = Helper.intersection(set1, set2); - Set union = Helper.union(set1, set2); - return 1 - (union.size() - intersection.size()) / (double) union.size(); - } - - // computes accuracy from coverage and protusion (changing this function may - // make it necessary to change the appoximation too) => not the case anymore -// private double getAccuracy(double recall, double precision) { -// return (coverageFactor * coverage + Math.sqrt(protusion)) / (coverageFactor + 1); - // log: changed from precision^^0.5 (root) to precision^^0.8 as the root is too optimistic in some cases -// return (coverageFactor * recall + Math.pow(precision, 0.8)) / (coverageFactor + 1); -// } - - private double getFMeasure(double recall, double precision) { - // balanced F measure -// return (precision + recall == 0) ? 0 : 2 * precision * recall / (precision + recall); - // see e.g. http://en.wikipedia.org/wiki/F-measure - return (precision + recall == 0) ? 0 : - ( (1+Math.sqrt(coverageFactor)) * (precision * recall) - / (Math.sqrt(coverageFactor) * precision + recall) ); - } - - // see paper: expression used in confidence interval estimation - public static double p3(double p1, int total) { - return 1.96 * Math.sqrt(p1*(1-p1)/(total+4)); - } - - // see paper: expression used in confidence interval estimation -// private static double p2(int success, int total) { -// double p1 = p1(success, total); -// return 1.96 * Math.sqrt(p1*(1-p1)/(total+4)); -// } - - // see paper: p' - public static double p1(int success, int total) { - return (success+2)/(double)(total+4); - } - + + /** * @return the classToDescribe */ @@ -912,7 +248,7 @@ public OWLClass getClassToDescribe() { public void setClassToDescribe(OWLClass classToDescribe) { this.classToDescribe = classToDescribe; } - + public void setClassToDescribe(IRI classIRI) { this.classToDescribe = df.getOWLClass(classIRI); } @@ -931,14 +267,14 @@ public EvaluatedDescriptionClass evaluate(OWLClassExpression description, double */ public boolean isConsistent(OWLClassExpression description) { OWLAxiom axiom; - if(equivalence) { + if (equivalence) { axiom = df.getOWLEquivalentClassesAxiom(classToDescribe, description); } else { axiom = df.getOWLSubClassOfAxiom(classToDescribe, description); } return getReasoner().remainsSatisfiable(axiom); } - + public boolean followsFromKB(OWLClassExpression description) { return equivalence ? getReasoner().isEquivalentClass(description, classToDescribe) : getReasoner().isSuperClassOf(description, classToDescribe); } @@ -959,42 +295,6 @@ public void setEquivalence(boolean equivalence) { this.equivalence = equivalence; } - public boolean isUseApproximations() { - return useApproximations; - } - - public void setUseApproximations(boolean useApproximations) { - this.useApproximations = useApproximations; - } - - public HeuristicType getHeuristic() { - return heuristic; - } - - public void setHeuristic(HeuristicType heuristic) { - this.heuristic = heuristic; - - if(heuristic == HeuristicType.AMEASURE) { - accuracyMethod = "standard"; - } else if(heuristic == HeuristicType.FMEASURE) { - accuracyMethod = "fmeasure"; - } else if(heuristic == HeuristicType.GEN_FMEASURE) { - accuracyMethod = "generalised_fmeasure"; - } else if(heuristic == HeuristicType.JACCARD) { - accuracyMethod = "jaccard"; - } else if(heuristic == HeuristicType.PRED_ACC) { - accuracyMethod = "pred_acc"; - } - } - - public double getApproxDelta() { - return approxDelta; - } - - public void setApproxDelta(double approxDelta) { - this.approxDelta = approxDelta; - } - public double getBetaSC() { return betaSC; } @@ -1019,25 +319,50 @@ public void setCheckConsistency(boolean checkConsistency) { this.checkConsistency = checkConsistency; } - public String getAccuracyMethod() { + public AccMethod getAccuracyMethod() { return accuracyMethod; } - public void setAccuracyMethod(String accuracyMethod) { + @Autowired(required = false) + public void setAccuracyMethod(AccMethod accuracyMethod) { this.accuracyMethod = accuracyMethod; } - - /** - * @param useInstanceChecks the useInstanceChecks to set - */ - public void setUseInstanceChecks(boolean useInstanceChecks) { - this.useInstanceChecks = useInstanceChecks; + + public double getRecall(OWLClassExpression description) { + ReasoningUtils.CoverageCount[] cc = reasoningUtil.getCoverageCount(description, classInstancesSet); + if (cc == null) { + return 0; + } + return cc[0].trueCount/(double)cc[0].total; } - - /** - * @return the useInstanceChecks - */ - public boolean isUseInstanceChecks() { - return useInstanceChecks; + + public double getAccuracyOrTooWeakApprox(OWLClassExpression description, double noise) { + AccMethodTwoValuedApproximate acc; + if (accuracyMethod instanceof AccMethodPredAcc) { + acc = new AccMethodPredAccApprox(); + } else if (accuracyMethod instanceof AccMethodFMeasure) { + acc = new AccMethodFMeasureApprox(); + } else if (accuracyMethod instanceof AccMethodAMeasure) { + acc = new AccMethodAMeasureApprox(); + } else { + throw new RuntimeException(); + } + acc.setReasoner(reasoner); + return reasoningUtil.getAccuracyOrTooWeak2(acc, description, Sets.newTreeSet(classInstances), Sets.newTreeSet(superClassInstances), noise); + } + + public double getAccuracyOrTooWeakExact(OWLClassExpression description, double noise) { + AccMethodTwoValued acc; + if (accuracyMethod instanceof AccMethodPredAcc) { + acc = new AccMethodPredAcc(); + } else if (accuracyMethod instanceof AccMethodFMeasure) { + acc = new AccMethodFMeasure(); + } else if (accuracyMethod instanceof AccMethodAMeasure) { + acc = new AccMethodAMeasure(); + } else { + throw new RuntimeException(); + } + return reasoningUtil.getAccuracyOrTooWeak2(acc, description, Sets.newTreeSet(classInstances), Sets.newTreeSet(superClassInstances), noise); } } + diff --git a/components-core/src/main/java/org/dllearner/learningproblems/ClassScore.java b/components-core/src/main/java/org/dllearner/learningproblems/ClassScore.java index 5715261d25..87fc75eff4 100644 --- a/components-core/src/main/java/org/dllearner/learningproblems/ClassScore.java +++ b/components-core/src/main/java/org/dllearner/learningproblems/ClassScore.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.learningproblems; import java.io.Serializable; diff --git a/components-core/src/main/java/org/dllearner/learningproblems/EvaluatedDescriptionClass.java b/components-core/src/main/java/org/dllearner/learningproblems/EvaluatedDescriptionClass.java index 89ffc3eb73..1efed02fd7 100644 --- a/components-core/src/main/java/org/dllearner/learningproblems/EvaluatedDescriptionClass.java +++ b/components-core/src/main/java/org/dllearner/learningproblems/EvaluatedDescriptionClass.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.learningproblems; import java.io.Serializable; diff --git a/components-core/src/main/java/org/dllearner/learningproblems/EvaluatedDescriptionPosNeg.java b/components-core/src/main/java/org/dllearner/learningproblems/EvaluatedDescriptionPosNeg.java index 5ca5c59b21..b3060d8c5a 100644 --- a/components-core/src/main/java/org/dllearner/learningproblems/EvaluatedDescriptionPosNeg.java +++ b/components-core/src/main/java/org/dllearner/learningproblems/EvaluatedDescriptionPosNeg.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.learningproblems; import java.util.Set; @@ -34,7 +33,7 @@ * evaluated by the learning algorithm, i.e. it has been checked * which examples it covers. It can be used as return value for * learning algorithms to make it easier for applications to - * assess how good an offered class OWLClassExpression is and how it + * assess how good an offered class description is and how it * classifies particular examples. * * @author Jens Lehmann @@ -46,8 +45,8 @@ public class EvaluatedDescriptionPosNeg extends EvaluatedDescription posAsPos, Set posAsNeg, Set negAsPos, Set negAsNeg) { - // usually core methods should not depend on methods outside of the core package (except utilities) - // in this case, this is just a convenience constructor - super(description, new ScoreTwoValued(posAsPos, posAsNeg, negAsPos, negAsNeg)); - score2 = (ScorePosNeg) score; - } /** * @see org.dllearner.learningproblems.ScorePosNeg#getAccuracy() @@ -85,6 +70,7 @@ public double getAccuracy() { * @see org.dllearner.learningproblems.ScorePosNeg * @return The score object associated with this evaluated description. */ + @Override public ScorePosNeg getScore() { return score2; } @@ -136,7 +122,7 @@ public String asJSON() { object.put("coveredPositives", getJSONArray(score2.getCoveredPositives())); object.put("coveredNegatives", getJSONArray(score2.getCoveredNegatives())); object.put("notCoveredPositives", getJSONArray(score2.getNotCoveredPositives())); - object.put("notCoveredNegatives", getJSONArray(score2.getNotCoveredNegatives())); + object.put("notCoveredNegatives", getJSONArray(score2.getNotCoveredNegatives())); return object.toString(3); } catch (JSONException e) { e.printStackTrace(); diff --git a/components-core/src/main/java/org/dllearner/learningproblems/EvaluatedDescriptionPosOnly.java b/components-core/src/main/java/org/dllearner/learningproblems/EvaluatedDescriptionPosOnly.java index 35d1abd6ed..c8808ecdf6 100644 --- a/components-core/src/main/java/org/dllearner/learningproblems/EvaluatedDescriptionPosOnly.java +++ b/components-core/src/main/java/org/dllearner/learningproblems/EvaluatedDescriptionPosOnly.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.learningproblems; import java.util.Set; diff --git a/components-core/src/main/java/org/dllearner/learningproblems/EvaluationCache.java b/components-core/src/main/java/org/dllearner/learningproblems/EvaluationCache.java index 14779e170d..8004746233 100644 --- a/components-core/src/main/java/org/dllearner/learningproblems/EvaluationCache.java +++ b/components-core/src/main/java/org/dllearner/learningproblems/EvaluationCache.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.learningproblems; import java.util.List; @@ -25,13 +24,14 @@ import java.util.SortedSet; import java.util.TreeMap; -import org.dllearner.utilities.Helper; import org.dllearner.utilities.datastructures.SortedSetTuple; import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLIndividual; import org.semanticweb.owlapi.model.OWLObjectIntersectionOf; import org.semanticweb.owlapi.model.OWLObjectUnionOf; +import com.google.common.collect.Sets; + /** * Caches results of previous concept evaluation to speed up * further evaluations. Implements a fast evaluation approach, @@ -52,7 +52,7 @@ public class EvaluationCache { public EvaluationCache(SortedSet examples) { this.examples = examples; - cache = new TreeMap>(); + cache = new TreeMap<>(); } public void put(OWLClassExpression concept, SortedSet individuals) { @@ -61,7 +61,7 @@ public void put(OWLClassExpression concept, SortedSet individuals /** * Determines which examples are instances of a concept. - * @param concept + * @param concept the concept * @return A tuple of two sets, where the first element is the * set of individuals belonging to the class and the second element * is the set of individuals not belonging to the class. For all @@ -70,30 +70,30 @@ public void put(OWLClassExpression concept, SortedSet individuals */ public SortedSetTuple infer(OWLClassExpression concept) { if(checkForEqualConcepts) { - SortedSet pos = cache.get(concept); - SortedSet neg = Helper.difference(examples, pos); - return new SortedSetTuple(pos,neg); + Set pos = cache.get(concept); + Set neg = Sets.difference(examples, pos); + return new SortedSetTuple<>(pos, neg); } else { // for a negation NOT C we can only say which concepts are not in it // (those in C), but we cannot say which ones are in NOT C // for a conjunction we know that the intersection of instances - // of all children belongs to the concept + // of all children belongs to the concept if(concept instanceof OWLObjectIntersectionOf) { handleMultiConjunction((OWLObjectIntersectionOf)concept); // disjunctions are similar to conjunctions but we use union here; // note that there can be instances which are neither in a concept - // C nor in a concept D, but in (C OR D) + // C nor in a concept D, but in (C OR D) } else if(concept instanceof OWLObjectUnionOf) { List operands = ((OWLObjectUnionOf) concept).getOperandsAsList(); Set pos = cache.get(operands.get(0)); for (int i = 1; i < operands.size(); i++) { - pos = Helper.union(pos, cache.get(operands.get(i))); + pos = Sets.union(pos, cache.get(operands.get(i))); } // in all other cases we cannot infer anything, so we return an // empty tuple } else { - return new SortedSetTuple(); + return new SortedSetTuple<>(); } } @@ -104,7 +104,7 @@ private SortedSetTuple handleMultiConjunction(OWLObjectIntersecti List operands = mc.getOperandsAsList(); Set pos = cache.get(operands.get(0)); for (int i = 1; i < operands.size(); i++) { - pos = Helper.intersection(pos, cache.get(operands.get(i))); + pos = Sets.intersection(pos, cache.get(operands.get(i))); } // TODO: handle the case that some children may not be in cache return null; diff --git a/components-core/src/main/java/org/dllearner/learningproblems/FuzzyPosNegLP.java b/components-core/src/main/java/org/dllearner/learningproblems/FuzzyPosNegLP.java new file mode 100644 index 0000000000..c670492a7b --- /dev/null +++ b/components-core/src/main/java/org/dllearner/learningproblems/FuzzyPosNegLP.java @@ -0,0 +1,106 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.learningproblems; + +import java.util.Map; +import java.util.SortedSet; +import java.util.TreeSet; + +import org.dllearner.core.AbstractClassExpressionLearningProblem; +import org.dllearner.core.AbstractReasonerComponent; +import org.dllearner.core.owl.fuzzydll.FuzzyIndividual; +import org.semanticweb.owlapi.model.OWLIndividual; +import org.semanticweb.owlapi.model.OWLNamedIndividual; + +/** + * @author Jens Lehmann + * + */ +public abstract class FuzzyPosNegLP extends AbstractClassExpressionLearningProblem> { + + protected SortedSet positiveExamples; + protected SortedSet negativeExamples; + protected SortedSet allExamples; + + protected SortedSet fuzzyExamples; + + protected Map fuzzyEx; + + public Map getFuzzyEx() { + return fuzzyEx; + } + + public void setFuzzyEx(Map fuzzyEx) { + fuzzyExamples = new TreeSet<>(); + + for (OWLIndividual i : fuzzyEx.keySet()) { + this.fuzzyExamples.add(new FuzzyIndividual(i.toStringID(), fuzzyEx.get(i).doubleValue())); + } + } + + protected boolean useRetrievalForClassification = false; + protected double percentPerLengthUnit = 0.05; + protected double totalTruth = 0; + + public FuzzyPosNegLP() {} + + public FuzzyPosNegLP(AbstractReasonerComponent reasoningService) { + super(reasoningService); + } + + /* + * (non-Javadoc) + * + * @see org.dllearner.core.Component#init() + */ + @Override + public void init() { + // commented by Josue as now there's no need of + and - examples (more code need to be deleted in this sense) + // allExamples = Helper.union(positiveExamples, negativeExamples); + } + + public SortedSet getNegativeExamples() { + return negativeExamples; + } + + public SortedSet getPositiveExamples() { + return positiveExamples; + } + + public void setNegativeExamples(SortedSet set) { + this.negativeExamples=set; + } + + public void setPositiveExamples(SortedSet set) { + this.positiveExamples=set; + } + + public double getPercentPerLengthUnit() { + return percentPerLengthUnit; + } + + public SortedSet getFuzzyExamples() { + return fuzzyExamples; + } + + public void setFuzzyExamples(SortedSet fuzzyExamples) { + this.fuzzyExamples = fuzzyExamples; + } + +} \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/learningproblems/FuzzyPosNegLPStandard.java b/components-core/src/main/java/org/dllearner/learningproblems/FuzzyPosNegLPStandard.java new file mode 100644 index 0000000000..7df726c9f6 --- /dev/null +++ b/components-core/src/main/java/org/dllearner/learningproblems/FuzzyPosNegLPStandard.java @@ -0,0 +1,627 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.learningproblems; + +import java.util.Iterator; +import java.util.SortedSet; +import java.util.TreeSet; + +import org.dllearner.core.AbstractReasonerComponent; +import org.dllearner.core.ComponentAnn; +import org.dllearner.core.EvaluatedDescription; +import org.dllearner.core.config.ConfigOption; +import org.dllearner.core.owl.fuzzydll.FuzzyIndividual; +import org.dllearner.learningproblems.Heuristics.HeuristicType; +import org.dllearner.utilities.owl.OWLClassExpressionUtils; +import org.semanticweb.owlapi.model.OWLClassExpression; +import org.semanticweb.owlapi.model.OWLIndividual; +import org.semanticweb.owlapi.model.OWLNamedIndividual; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The aim of this learning problem is to learn a concept definition such that + * the positive examples and the negative examples do not follow. It is + * 2-valued, because we only distinguish between covered and non-covered + * examples. (A 3-valued problem distinguishes between covered examples, + * examples covered by the negation of the concept, and all other examples.) The + * 2-valued learning problem is often more useful for Description Logics due to + * (the Open World Assumption and) the fact that negative knowledge, e.g. that a + * person does not have a child, is or cannot be expressed. + * + * @author Jens Lehmann + * + */ +@ComponentAnn(name = "FuzzyPosNegLPStandard", shortName = "fuzzyPosNeg", version = 0.2) +public class FuzzyPosNegLPStandard extends FuzzyPosNegLP { + + private static final Logger logger = LoggerFactory.getLogger(FuzzyPosNegLPStandard.class); + + // approximation and F-measure + // (taken from class learning => super class instances corresponds to negative examples + // and class instances to positive examples) + private double approxDelta = 0.05; + private boolean useApproximations; +// private boolean useFMeasure; + private boolean useOldDIGOptions = false; + + private HeuristicType heuristic = HeuristicType.PRED_ACC; + + private int errorIndex = 0; + + @ConfigOption(name = "accuracyMethod", description = "Specifies, which method/function to use for computing accuracy. Available measues are \"PRED_ACC\" (predictive accuracy), \"FMEASURE\" (F measure), \"GEN_FMEASURE\" (generalised F-Measure according to Fanizzi and d'Amato).",defaultValue = "PRED_ACC") + private HeuristicType accuracyMethod = HeuristicType.PRED_ACC; + + public FuzzyPosNegLPStandard() {} + + public FuzzyPosNegLPStandard(AbstractReasonerComponent reasoningService) { + super(reasoningService); + } + + public FuzzyPosNegLPStandard(AbstractReasonerComponent reasoningService, SortedSet positiveExamples, SortedSet negativeExamples) { + super(reasoningService); + this.positiveExamples = positiveExamples; + this.negativeExamples = negativeExamples; + } + + @Override + public void init() { + super.init(); + + if(useApproximations && accuracyMethod.equals(HeuristicType.PRED_ACC)) { + logger.warn("Approximating predictive accuracy is an experimental feature. USE IT AT YOUR OWN RISK. If you consider to use it for anything serious, please extend the unit tests at org.dllearner.test.junit.HeuristicTests first and verify that it works."); + } + + } + + /* + * (non-Javadoc) + * + * @see org.dllearner.core.Component#getName() + */ + public static String getName() { + return "fuzzy pos neg learning problem"; + } + + @Override + public double getAccuracyOrTooWeak(OWLClassExpression description, double noise) { + // delegates to the appropriate methods + return useApproximations ? getAccuracyOrTooWeakApprox(description, noise) : getAccuracyOrTooWeakExact(description, noise); + } + + public double getAccuracyOrTooWeakApprox(OWLClassExpression description, double noise) { + if(heuristic.equals(HeuristicType.PRED_ACC)) { + int maxNotCovered = (int) Math.ceil(noise*positiveExamples.size()); + + int notCoveredPos = 0; +// int notCoveredNeg = 0; + + int posClassifiedAsPos = 0; + int negClassifiedAsNeg = 0; + + int nrOfPosChecks = 0; + int nrOfNegChecks = 0; + + // special case: we test positive and negative examples in turn + Iterator itPos = positiveExamples.iterator(); + Iterator itNeg = negativeExamples.iterator(); + + do { + // in each loop we pick 0 or 1 positives and 0 or 1 negative + // and classify it + + if(itPos.hasNext()) { + OWLIndividual posExample = itPos.next(); +// System.out.println(posExample); + + if(getReasoner().hasType(description, posExample)) { + posClassifiedAsPos++; + } else { + notCoveredPos++; + } + nrOfPosChecks++; + + // take noise into account + if(notCoveredPos > maxNotCovered) { + return -1; + } + } + + if(itNeg.hasNext()) { + OWLIndividual negExample = itNeg.next(); + if(!getReasoner().hasType(description, negExample)) { + negClassifiedAsNeg++; + } + nrOfNegChecks++; + } + + // compute how accurate our current approximation is and return if it is sufficiently accurate + double approx[] = Heuristics.getPredAccApproximation(positiveExamples.size(), negativeExamples.size(), 1, nrOfPosChecks, posClassifiedAsPos, nrOfNegChecks, negClassifiedAsNeg); + if(approx[1] maxNotCovered) { + return -1; + } + } + } + + double recall = instancesCovered/(double)positiveExamples.size(); + + int testsPerformed = 0; + int instancesDescription = 0; + + for(OWLIndividual ind : negativeExamples) { + + if(getReasoner().hasType(description, ind)) { + instancesDescription++; + } + testsPerformed++; + + // check whether approximation is sufficiently accurate + double[] approx = Heuristics.getFScoreApproximation(instancesCovered, recall, 1, negativeExamples.size(), testsPerformed, instancesDescription); + if(approx[1]= maxNotCovered) { + return -1; + } + } + } + for (OWLIndividual example : negativeExamples) { + if (!getReasoner().hasType(description, example)) { + notCoveredNeg++; + } + } + return (positiveExamples.size() - notCoveredPos + notCoveredNeg) / (double) allExamples.size(); + } + + // added by Josue + private double crispfMeasure(OWLClassExpression description, double noise) { + // crisp F-measure + int additionalInstances = 0; + for(OWLIndividual ind : negativeExamples) { + if(getReasoner().hasType(description, ind)) { + additionalInstances++; + } + } + + int coveredInstances = 0; + for(OWLIndividual ind : positiveExamples) { + if(getReasoner().hasType(description, ind)) { + coveredInstances++; + } + } + + double recall = coveredInstances/(double)positiveExamples.size(); + + if(recall < 1 - noise) { + return -1; + } + + double precision = (additionalInstances + coveredInstances == 0) ? 0 : coveredInstances / (double) (coveredInstances + additionalInstances); + + return Heuristics.getFScore(recall, precision); + } + + public double getFMeasureOrTooWeakExact(OWLClassExpression description, double noise) { + + // added by Josue + // fuzzy F-measure + double coveredMembershipDegree = 0; + double totalMembershipDegree = 0; + double invertedCoveredMembershipDegree = 0; + double lastMembershipDegree = 0; + + for (FuzzyIndividual ind: fuzzyExamples) { + lastMembershipDegree = (1 - Math.abs(ind.getTruthDegree() - getReasoner().hasTypeFuzzyMembership(description, ind))); + coveredMembershipDegree += lastMembershipDegree * ind.getTruthDegree(); + totalMembershipDegree += ind.getTruthDegree(); + invertedCoveredMembershipDegree += (1 - ind.getTruthDegree()) * (1 - lastMembershipDegree); + } + double fuzzyRecall = totalMembershipDegree == 0 ? 0 :coveredMembershipDegree/totalMembershipDegree; + + if(fuzzyRecall < 1 - noise) { + return -1; + } + double fuzzyPrecision = (coveredMembershipDegree + invertedCoveredMembershipDegree) == 0 ? 0: coveredMembershipDegree / (coveredMembershipDegree + invertedCoveredMembershipDegree); + double fuzzyFmeasure = Heuristics.getFScore(fuzzyRecall, fuzzyPrecision); + + // double crispFmeasure = crispfMeasure(description, noise); + + // crispFmeasure = fuzzyFmeasure; + +// if (crispFmeasure != fuzzyFmeasure) { +// System.err.println("************************"); +// System.err.println("* crispFmeasuer = " + crispFmeasure); +// System.err.println("* fuzzyFmeasuer = " + fuzzyFmeasure); +// System.err.println("************************"); +// Scanner sc = new Scanner(System.in); +// sc.nextLine(); +// } + + return fuzzyFmeasure; + } + + // instead of using the standard operation, we use optimisation + // and approximation here; + // now deprecated because the Heuristics helper class is used + @Deprecated + public double getFMeasureOrTooWeakApprox(OWLClassExpression description, double noise) { + // we abort when there are too many uncovered positives + int maxNotCovered = (int) Math.ceil(noise*positiveExamples.size()); + int instancesCovered = 0; + int instancesNotCovered = 0; + int total = 0; + boolean estimatedA = false; + + double lowerBorderA = 0; + int lowerEstimateA = 0; + double upperBorderA = 1; + int upperEstimateA = positiveExamples.size(); + + for(OWLIndividual ind : positiveExamples) { + if(getReasoner().hasType(description, ind)) { + instancesCovered++; + } else { + instancesNotCovered ++; + if(instancesNotCovered > maxNotCovered) { + return -1; + } + } + + // approximation step (starting after 10 tests) + total = instancesCovered + instancesNotCovered; + if(total > 10) { + // compute confidence interval + double p1 = Heuristics.p1(instancesCovered, total); + double p2 = Heuristics.p3(p1, total); + lowerBorderA = Math.max(0, p1 - p2); + upperBorderA = Math.min(1, p1 + p2); + double size = upperBorderA - lowerBorderA; + // if the interval has a size smaller than 10%, we can be confident + if(size < 2 * approxDelta) { + // we have to distinguish the cases that the accuracy limit is + // below, within, or above the limit and that the mean is below + // or above the limit + double mean = instancesCovered/(double)total; + + // if the mean is greater than the required minimum, we can accept; + // we also accept if the interval is small and close to the minimum + // (worst case is to accept a few inaccurate descriptions) + if(mean > 1-noise || (upperBorderA > mean && size < 0.03)) { + instancesCovered = (int) (instancesCovered/(double)total * positiveExamples.size()); + upperEstimateA = (int) (upperBorderA * positiveExamples.size()); + lowerEstimateA = (int) (lowerBorderA * positiveExamples.size()); + estimatedA = true; + break; + } + + // reject only if the upper border is far away (we are very + // certain not to lose a potential solution) + if(upperBorderA + 0.1 < 1-noise) { + return -1; + } + } + } + } + + double recall = instancesCovered/(double)positiveExamples.size(); + +// MonitorFactory.add("estimatedA","count", estimatedA ? 1 : 0); +// MonitorFactory.add("aInstances","count", total); + + // we know that a definition candidate is always subclass of the + // intersection of all super classes, so we test only the relevant instances + // (leads to undesired effects for descriptions not following this rule, + // but improves performance a lot); + // for learning a superclass of a defined class, similar observations apply; + + + int testsPerformed = 0; + int instancesDescription = 0; +// boolean estimatedB = false; + + for(OWLIndividual ind : negativeExamples) { + + if(getReasoner().hasType(description, ind)) { + instancesDescription++; + } + + testsPerformed++; + + if(testsPerformed > 10) { + + // compute confidence interval + double p1 = Heuristics.p1(instancesDescription, testsPerformed); + double p2 = Heuristics.p3(p1, testsPerformed); + double lowerBorder = Math.max(0, p1 - p2); + double upperBorder = Math.min(1, p1 + p2); + int lowerEstimate = (int) (lowerBorder * negativeExamples.size()); + int upperEstimate = (int) (upperBorder * negativeExamples.size()); + + double size; + if(estimatedA) { + size = getFMeasure(upperBorderA, upperEstimateA/(double)(upperEstimateA+lowerEstimate)) - getFMeasure(lowerBorderA, lowerEstimateA/(double)(lowerEstimateA+upperEstimate)); + } else { + size = getFMeasure(recall, instancesCovered/(double)(instancesCovered+lowerEstimate)) - getFMeasure(recall, instancesCovered/(double)(instancesCovered+upperEstimate)); + } + + if(size < 0.1) { + instancesDescription = (int) (instancesDescription/(double)testsPerformed * negativeExamples.size()); + break; + } + } + } + + double precision = instancesCovered/(double)(instancesDescription+instancesCovered); + if(instancesCovered + instancesDescription == 0) { + precision = 0; + } + +// System.out.println("description: " + description); +// System.out.println("recall: " + recall); +// System.out.println("precision: " + precision); +// System.out.println("F-measure: " + getFMeasure(recall, precision)); +// System.out.println("exact: " + getAccuracyOrTooWeakExact(description, noise)); + + return getFMeasure(recall, precision); + } + + + /* (non-Javadoc) + * @see org.dllearner.core.LearningProblem#evaluate(org.dllearner.core.owl.Description) + */ + @Override + public EvaluatedDescription evaluate(OWLClassExpression description) { + ScorePosNeg score = computeScore(description); + return new EvaluatedDescriptionPosNeg(description, score); + } + + private double getFMeasure(double recall, double precision) { + return 2 * precision * recall / (precision + recall); + } + + public double getApproxDelta() { + return approxDelta; + } + + public void setApproxDelta(double approxDelta) { + this.approxDelta = approxDelta; + } + + public boolean isUseApproximations() { + return useApproximations; + } + + public void setUseApproximations(boolean useApproximations) { + this.useApproximations = useApproximations; + } + + public HeuristicType getHeuristic() { + return heuristic; + } + + public void setHeuristic(HeuristicType heuristic) { + this.heuristic = heuristic; + } + + /** + * @param accuracyMethod the accuracy method to set + */ + public void setAccuracyMethod(HeuristicType accuracyMethod) { + this.accuracyMethod = accuracyMethod; + } + + + public double getAccuracy(int posAsPos, int posAsNeg, int negAsPos, int negAsNeg, double noise) { + int maxNotCovered = (int) Math.ceil(noise * positiveExamples.size()); + + if(posAsNeg > maxNotCovered) { + return -1; + } + + switch (accuracyMethod) { + case PRED_ACC: + return (posAsPos + negAsNeg) / (double) allExamples.size(); + case FMEASURE: + double recall = posAsPos / (double)positiveExamples.size(); + + if(recall < 1 - noise) { + return -1; + } + + double precision = (negAsPos + posAsPos == 0) ? 0 : posAsPos / (double) (posAsPos + negAsPos); + + return Heuristics.getFScore(recall, precision); + default: + throw new Error("Heuristic " + accuracyMethod + " not implemented."); + } + + } + + /* (non-Javadoc) + * @see org.dllearner.core.AbstractLearningProblem#computeScore(org.semanticweb.owlapi.model.OWLObject, double) + */ + @Override + public ScorePosNeg computeScore(OWLClassExpression concept, double noise) { + SortedSet posAsPos = new TreeSet<>(); + SortedSet posAsNeg = new TreeSet<>(); + SortedSet negAsPos = new TreeSet<>(); + SortedSet negAsNeg = new TreeSet<>(); + + for (OWLIndividual example : positiveExamples) { + if (getReasoner().hasType(concept, example)) { + posAsPos.add(example); + } else { + posAsNeg.add(example); + } + } + for (OWLIndividual example : negativeExamples) { + if (getReasoner().hasType(concept, example)) + negAsPos.add(example); + else + negAsNeg.add(example); + } + + // TODO: this computes accuracy twice - more elegant method should be implemented + double accuracy = getAccuracy(posAsPos.size(), posAsNeg.size(), negAsPos.size(), negAsNeg.size(), noise); + + return new ScoreTwoValued( + OWLClassExpressionUtils.getLength(concept), + getPercentPerLengthUnit(), + posAsPos, posAsNeg, negAsPos, negAsNeg, + accuracy); + } + +} \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/learningproblems/Heuristics.java b/components-core/src/main/java/org/dllearner/learningproblems/Heuristics.java index d3d84d4da2..16e7b43287 100644 --- a/components-core/src/main/java/org/dllearner/learningproblems/Heuristics.java +++ b/components-core/src/main/java/org/dllearner/learningproblems/Heuristics.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.learningproblems; /** @@ -29,8 +28,8 @@ */ public class Heuristics { - public static enum HeuristicType { PRED_ACC, AMEASURE, JACCARD, FMEASURE, GEN_FMEASURE, ENTROPY, MATTHEWS_CORRELATION, YOUDEN_INDEX }; - + public enum HeuristicType { PRED_ACC, AMEASURE, JACCARD, FMEASURE, GEN_FMEASURE, ENTROPY, MATTHEWS_CORRELATION, YOUDEN_INDEX } + /** * Computes F1-Score. * @param recall Recall. @@ -39,7 +38,7 @@ public static enum HeuristicType { PRED_ACC, AMEASURE, JACCARD, FMEASURE, GEN_FM */ public static double getFScore(double recall, double precision) { return (precision + recall == 0) ? 0 : - ( 2 * (precision * recall) / (double) (precision + recall) ); + ( 2 * (precision * recall) / (precision + recall) ); } /** @@ -53,23 +52,29 @@ public static double getFScore(double recall, double precision) { public static double getFScore(double recall, double precision, double beta) { return (precision + recall == 0) ? 0 : ( (1+ beta * beta) * (precision * recall) - / (beta * beta * precision + recall) ); - } - + / (beta * beta * precision + recall) ); + } + + public static double getFScoreBalanced(double recall, double precision, double beta) { + // balanced F measure + return (precision + recall == 0) ? 0 : + ( (1+Math.sqrt(beta)) * (precision * recall) + / (Math.sqrt(beta) * precision + recall) ); + } /** * Computes arithmetic mean of precision and recall, which is called "A-Score" - * here (A=arithmetic), but is not an established notion in machine learning. + * here (A=arithmetic), but is not an established notion in machine learning. * @param recall Recall. * @param precision Precison. * @return Arithmetic mean of precision and recall. */ public static double getAScore(double recall, double precision) { - return (recall + precision) / (double) 2; + return (recall + precision) / 2; } /** * Computes arithmetic mean of precision and recall, which is called "A-Score" - * here (A=arithmetic), but is not an established notion in machine learning. + * here (A=arithmetic), but is not an established notion in machine learning. * @param recall Recall. * @param precision Precison. * @param beta Weights precision and recall. If beta is >1, then recall is more important @@ -95,18 +100,18 @@ public static double getJaccardCoefficient(int elementsIntersection, int element public static double getPredictiveAccuracy(int nrOfExamples, int nrOfPosClassifiedPositives, int nrOfNegClassifiedNegatives) { return (nrOfPosClassifiedPositives + nrOfNegClassifiedNegatives) / (double) nrOfExamples; - } + } public static double getPredictiveAccuracy(int nrOfPosExamples, int nrOfNegExamples, int nrOfPosClassifiedPositives, int nrOfNegClassifiedNegatives, double beta) { - return (nrOfPosClassifiedPositives + beta * nrOfNegClassifiedNegatives) / (double) (nrOfPosExamples + beta * nrOfNegExamples); - } + return (nrOfPosClassifiedPositives + beta * nrOfNegClassifiedNegatives) / (nrOfPosExamples + beta * nrOfNegExamples); + } public static double getPredictiveAccuracy2(int nrOfExamples, int nrOfPosClassifiedPositives, int nrOfPosClassifiedNegatives) { return (nrOfPosClassifiedPositives + nrOfExamples - nrOfPosClassifiedNegatives) / (double) nrOfExamples; } public static double getPredictiveAccuracy2(int nrOfPosExamples, int nrOfNegExamples, int nrOfPosClassifiedPositives, int nrOfNegClassifiedNegatives, double beta) { - return (nrOfPosClassifiedPositives + beta * nrOfNegClassifiedNegatives) / (double) (nrOfPosExamples + beta * nrOfNegExamples); + return (nrOfPosClassifiedPositives + beta * nrOfNegClassifiedNegatives) / (nrOfPosExamples + beta * nrOfNegExamples); } public static double getMatthewsCorrelationCoefficient(int tp, int fp, int tn, int fn) { @@ -116,7 +121,7 @@ public static double getMatthewsCorrelationCoefficient(int tp, int fp, int tn, i /** * Computes the 95% confidence interval of an experiment with boolean outcomes, * e.g. heads or tails coin throws. It uses the very efficient, but still accurate - * Wald method. + * Wald method. * @param success Number of successes, e.g. number of times the coin shows head. * @param total Total number of tries, e.g. total number of times the coin was thrown. * @return A two element double array, where element 0 is the lower border and element @@ -137,7 +142,7 @@ public static double[] getConfidenceInterval95Wald(int total, int success) { /** * Computes the 95% confidence interval average of an experiment with boolean outcomes, * e.g. heads or tails coin throws. It uses the very efficient, but still accurate - * Wald method. + * Wald method. * @param success Number of successes, e.g. number of times the coin shows head. * @param total Total number of tries, e.g. total number of times the coin was thrown. * @return The average of the lower border and upper border of the 95% confidence interval. @@ -157,7 +162,7 @@ public static double getConfidenceInterval95WaldAverage(int total, int success) * @param nrOfPosClassifiedPositives The number of positive examples, which were indeed classified as positive by the hypothesis. * @param noise The noise parameter is a value between 0 and 1, which indicates how noisy the example data is (0 = no noise, 1 = completely random). * If a hypothesis contains more errors on the positive examples than the noise value multiplied by the - * number of all examples, then the hypothesis is too weak. + * number of all examples, then the hypothesis is too weak. * @return True if the hypothesis is too weak and false otherwise. */ public boolean isTooWeak(int nrOfPositiveExamples, int nrOfPosClassifiedPositives, double noise) { @@ -174,18 +179,28 @@ public boolean isTooWeak(int nrOfPositiveExamples, int nrOfPosClassifiedPositive * @param nrOfNegClassifiedPositives The number of positive examples, which were indeed classified as negative by the hypothesis. * @param noise The noise parameter is a value between 0 and 1, which indicates how noisy the example data is (0 = no noise, 1 = completely random). * If a hypothesis contains more errors on the positive examples than the noise value multiplied by the - * number of all examples, then the hypothesis is too weak. + * number of all examples, then the hypothesis is too weak. * @return True if the hypothesis is too weak and false otherwise. */ public boolean isTooWeak2(int nrOfPositiveExamples, int nrOfNegClassifiedPositives, double noise) { if(noise < 0 || noise > 1 || nrOfNegClassifiedPositives <= nrOfPositiveExamples || nrOfPositiveExamples < 1) { throw new IllegalArgumentException(); - } + } return (noise * nrOfPositiveExamples) < nrOfNegClassifiedPositives; } + // see paper: p' + public static double p1(int success, int total) { + return (success+2)/(double)(total+4); + } + + // see paper: expression used in confidence interval estimation + public static double p3(double p1, int total) { + return 1.96 * Math.sqrt(p1*(1-p1)/(total+4)); + } + /** - * This method can be used to approximate F-Measure and thereby saving a lot of + * This method can be used to approximate F-Measure and thereby saving a lot of * instance checks. It assumes that all positive examples (or instances of a class) * have already been tested via instance checks, i.e. recall is already known and * precision is approximated. @@ -211,7 +226,7 @@ public static double[] getFScoreApproximation(int nrOfPosClassifiedPositives, do double fMeasureLow = (1 + Math.sqrt(beta)) * (nrOfPosClassifiedPositives/(nrOfPosClassifiedPositives+upperBorder)*recall) / (Math.sqrt(beta)*nrOfPosClassifiedPositives/(nrOfPosClassifiedPositives+upperBorder)+recall); double diff = fMeasureHigh - fMeasureLow; // compute F-score for proportion ? - // double proportionInstanceChecks = successfulInstanceChecks / (double) nrOfInstanceChecks * nrOfRelevantInstances; // + // double proportionInstanceChecks = successfulInstanceChecks / (double) nrOfInstanceChecks * nrOfRelevantInstances; // // => don't do it for now, because the difference between proportion and center of interval is usually quite small // for sufficiently small diffs // return interval length and center @@ -247,7 +262,7 @@ public static double[] getAScoreApproximationStep1(double beta, int nrOfPosExamp * In step 2 of the A-Score approximation, the precision and overall A-Score is estimated based on * the estimated recall. * @param nrOfPosClassifiedPositives Positive examples (instance of a class), which are classified as positives. - * @param recallInterval The estimated recall, which needs to be given as a two element array with the first element being the mean value and the second element being the length of the interval (to be compatible with the step1 method). + * @param recallInterval The estimated recall, which needs to be given as a two element array with the first element being the mean value and the second element being the length of the interval (to be compatible with the step1 method). * @param beta Weights precision and recall. If beta is >1, then recall is more important * than precision. * @param nrOfRelevantInstances Number of relevant instances, i.e. number of instances, which @@ -291,8 +306,8 @@ public static double[] getPredAccApproximation(int nrOfPositiveExamples, int nrO // multiply by number of instances from which the random samples are drawn double lowerBorder = intervalPos[0] * nrOfPositiveExamples + beta * intervalNeg[0] * nrOfNegativeExamples; double upperBorder = intervalNeg[1] * nrOfPositiveExamples + beta * intervalNeg[1] * nrOfNegativeExamples; - double predAccLow = lowerBorder / (double) (nrOfPositiveExamples + beta * nrOfNegativeExamples); - double predAccHigh = upperBorder / (double) (nrOfPositiveExamples + beta * nrOfNegativeExamples); + double predAccLow = lowerBorder / (nrOfPositiveExamples + beta * nrOfNegativeExamples); + double predAccHigh = upperBorder / (nrOfPositiveExamples + beta * nrOfNegativeExamples); double diff = predAccHigh - predAccLow; // return interval length and center double[] ret = new double[2]; @@ -300,5 +315,9 @@ public static double[] getPredAccApproximation(int nrOfPositiveExamples, int nrO ret[1] = diff; return ret; } + + public static double divideOrZero(int numerator, int denominator) { + return denominator == 0 ? 0 : numerator / (double)denominator; + } } diff --git a/components-core/src/main/java/org/dllearner/learningproblems/PosNegLP.java b/components-core/src/main/java/org/dllearner/learningproblems/PosNegLP.java index cb5ed07588..27a50cf83a 100644 --- a/components-core/src/main/java/org/dllearner/learningproblems/PosNegLP.java +++ b/components-core/src/main/java/org/dllearner/learningproblems/PosNegLP.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,76 +16,46 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.learningproblems; -import java.util.Collection; -import java.util.LinkedList; -import java.util.Set; -import java.util.TreeSet; - +import com.google.common.collect.Sets; +import com.google.common.collect.Sets.SetView; import org.apache.log4j.Logger; import org.dllearner.core.AbstractClassExpressionLearningProblem; import org.dllearner.core.AbstractReasonerComponent; import org.dllearner.core.ComponentInitException; -import org.dllearner.core.options.BooleanConfigOption; -import org.dllearner.core.options.CommonConfigOptions; -import org.dllearner.core.options.StringConfigOption; -import org.dllearner.core.options.StringSetConfigOption; +import org.dllearner.core.config.ConfigOption; import org.dllearner.reasoning.SPARQLReasoner; -import org.dllearner.utilities.Helper; -import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLIndividual; import org.semanticweb.owlapi.model.OWLNamedIndividual; +import org.springframework.beans.factory.annotation.Autowired; -import com.google.common.collect.Sets; -import com.google.common.collect.Sets.SetView; +import java.util.Set; +import java.util.TreeSet; /** * @author Jens Lehmann * */ public abstract class PosNegLP extends AbstractClassExpressionLearningProblem> { - private static Logger logger = Logger.getLogger(PosNegLP.class); + protected static Logger logger = Logger.getLogger(PosNegLP.class); - protected Set positiveExamples = new TreeSet(); - protected Set negativeExamples = new TreeSet(); - protected Set allExamples = new TreeSet(); + @ConfigOption(description = "list of positive examples", required = true) + protected Set positiveExamples = new TreeSet<>(); + @ConfigOption(description = "list of negative examples", required = true) + protected Set negativeExamples = new TreeSet<>(); + protected Set allExamples = new TreeSet<>(); @org.dllearner.core.config.ConfigOption(name = "useRetrievalForClassification", description = "\"Specifies whether to use retrieval or instance checks for testing a concept. - NO LONGER FULLY SUPPORTED.",defaultValue = "false") private boolean useRetrievalForClassification = false; - @org.dllearner.core.config.ConfigOption(name = "useMultiInstanceChecks", description = "Use The Multi Instance Checks", defaultValue = "UseMultiInstanceChecks.TWOCHECKS", required = false) - private UseMultiInstanceChecks useMultiInstanceChecks = UseMultiInstanceChecks.TWOCHECKS; @org.dllearner.core.config.ConfigOption(name = "percentPerLengthUnit", description = "Percent Per Length Unit", defaultValue = "0.05", required = false) private double percentPerLengthUnit = 0.05; - - /** - * If instance checks are used for testing concepts (e.g. no retrieval), then - * there are several options to do this. The enumeration lists the supported - * options. These options are only important if the reasoning mechanism - * supports sending several reasoning requests at once as it is the case for - * DIG reasoners. - * - * @author Jens Lehmann - * - */ - public enum UseMultiInstanceChecks { - /** - * Perform a separate instance check for each example. - */ - NEVER, - /** - * Perform one instance check for all positive and one instance check - * for all negative examples. - */ - TWOCHECKS, - /** - * Perform all instance checks at once. - */ - ONECHECK - }; - + @ConfigOption( + name = "accuracyMethod", + description = "Specifies, which method/function to use for computing accuracy. Available measues are \"PRED_ACC\" (predictive accuracy), \"FMEASURE\" (F measure), \"GEN_FMEASURE\" (generalised F-Measure according to Fanizzi and d'Amato).", + defaultValue = "PRED_ACC") + protected AccMethodTwoValued accuracyMethod; public PosNegLP(){ @@ -94,21 +64,6 @@ public PosNegLP(){ public PosNegLP(AbstractReasonerComponent reasoningService) { super(reasoningService); } - - public static Collection> createConfigOptions() { - Collection> options = new LinkedList>(); - options.add(new StringSetConfigOption("positiveExamples", - "positive examples",null, true, false)); - options.add(new StringSetConfigOption("negativeExamples", - "negative examples",null, true, false)); - options.add(new BooleanConfigOption("useRetrievalForClassficiation", - "Specifies whether to use retrieval or instance checks for testing a concept. - NO LONGER FULLY SUPPORTED.", false)); - options.add(CommonConfigOptions.getPercentPerLenghtUnitOption(0.05)); - StringConfigOption multiInstanceChecks = new StringConfigOption("useMultiInstanceChecks", "See UseMultiInstanceChecks enum. - NO LONGER FULLY SUPPORTED.","twoChecks"); - multiInstanceChecks.setAllowedValues(new String[] {"never", "twoChecks", "oneCheck"}); - options.add(multiInstanceChecks); - return options; - } /* * (non-Javadoc) @@ -135,12 +90,19 @@ public void init() throws ComponentInitException { logger.warn("You declared some individuals as both positive and negative examples."); } - allExamples = Helper.union(positiveExamples, negativeExamples); + allExamples = Sets.union(positiveExamples, negativeExamples); + + if (accuracyMethod == null) { + accuracyMethod = new AccMethodPredAcc(true); + } + if (accuracyMethod instanceof AccMethodApproximate) { + ((AccMethodApproximate)accuracyMethod).setReasoner(reasoner); + } // sanity check whether examples are contained in KB if(reasoner != null && !reasoner.getIndividuals().containsAll(allExamples) && !reasoner.getClass().isAssignableFrom(SPARQLReasoner.class)) { - Set missing = Helper.difference(allExamples, reasoner.getIndividuals()); - double percentage = (double) (missing.size()/allExamples.size()); + Set missing = Sets.difference(allExamples, reasoner.getIndividuals()); + double percentage = missing.size()/allExamples.size(); percentage = Math.round(percentage * 1000) / 1000; String str = "The examples (" + (percentage * 100) + " % of total) below are not contained in the knowledge base (check spelling and prefixes)\n"; str += missing.toString(); @@ -169,8 +131,6 @@ public void setNegativeExamples(Set set) { public void setPositiveExamples(Set set) { this.positiveExamples=set; } - - public abstract int coveredNegativeExamplesOrTooWeak(OWLClassExpression concept); public double getPercentPerLengthUnit() { return percentPerLengthUnit; @@ -188,11 +148,12 @@ public void setUseRetrievalForClassification(boolean useRetrievalForClassificati this.useRetrievalForClassification = useRetrievalForClassification; } - public UseMultiInstanceChecks getUseMultiInstanceChecks() { - return useMultiInstanceChecks; - } + public AccMethodTwoValued getAccuracyMethod() { + return accuracyMethod; + } - public void setUseMultiInstanceChecks(UseMultiInstanceChecks useMultiInstanceChecks) { - this.useMultiInstanceChecks = useMultiInstanceChecks; - } + @Autowired(required=false) + public void setAccuracyMethod(AccMethodTwoValued accuracyMethod) { + this.accuracyMethod = accuracyMethod; + } } \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/learningproblems/PosNegLPStandard.java b/components-core/src/main/java/org/dllearner/learningproblems/PosNegLPStandard.java index 2083e9f3ef..5752745f8b 100644 --- a/components-core/src/main/java/org/dllearner/learningproblems/PosNegLPStandard.java +++ b/components-core/src/main/java/org/dllearner/learningproblems/PosNegLPStandard.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,32 +16,18 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.learningproblems; -import java.util.Collection; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; - import org.dllearner.core.AbstractReasonerComponent; import org.dllearner.core.ComponentAnn; import org.dllearner.core.ComponentInitException; import org.dllearner.core.EvaluatedDescription; -import org.dllearner.core.config.ConfigOption; -import org.dllearner.core.options.BooleanConfigOption; -import org.dllearner.core.options.DoubleConfigOption; -import org.dllearner.core.options.StringConfigOption; -import org.dllearner.learningproblems.Heuristics.HeuristicType; -import org.dllearner.reasoning.SPARQLReasoner; -import org.dllearner.utilities.Helper; +import org.dllearner.utilities.ReasoningUtils.Coverage; import org.dllearner.utilities.owl.OWLClassExpressionUtils; import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLIndividual; -import com.google.common.collect.Sets; +import java.util.SortedSet; /** * The aim of this learning problem is to learn a concept definition such that @@ -49,7 +35,7 @@ * 2-valued, because we only distinguish between covered and non-covered * examples. (A 3-valued problem distinguishes between covered examples, * examples covered by the negation of the concept, and all other examples.) The - * 2-valued learning problem is often more useful for OWLClassExpression Logics due to + * 2-valued learning problem is often more useful for Description Logics due to * (the Open World Assumption and) the fact that negative knowledge, e.g. that a * person does not have a child, is or cannot be expressed. * @@ -60,25 +46,7 @@ public class PosNegLPStandard extends PosNegLP implements Cloneable{ - // approximation and F-measure - // (taken from class learning => super class instances corresponds to negative examples - // and class instances to positive examples) - @ConfigOption(name = "approxDelta", description = "The Approximate Delta", defaultValue = "0.05", required = false) - private double approxDelta = 0.05; - - @ConfigOption(name = "useApproximations", description = "Use Approximations", defaultValue = "false", required = false) - private boolean useApproximations; - - @ConfigOption(name = "accuracyMethod", description = "Specifies, which method/function to use for computing accuracy. Available measues are \"pred_acc\" (predictive accuracy), \"fmeasure\" (F measure), \"generalised_fmeasure\" (generalised F-Measure according to Fanizzi and d'Amato).",defaultValue = "predacc") - private String accuracyMethod = "pred_acc"; - -// private boolean useFMeasure; - private boolean useOldDIGOptions = false; - - private HeuristicType heuristic = HeuristicType.PRED_ACC; - - - public PosNegLPStandard() { + public PosNegLPStandard() { } public PosNegLPStandard(AbstractReasonerComponent reasoningService){ @@ -87,17 +55,14 @@ public PosNegLPStandard(AbstractReasonerComponent reasoningService){ /** * Copy constructor - * @param lp + * @param lp the learning problem */ - public PosNegLPStandard(PosNegLPStandard lp) { + public PosNegLPStandard(PosNegLP lp) { this.positiveExamples = lp.getPositiveExamples(); this.negativeExamples = lp.getNegativeExamples(); this.reasoner = lp.getReasoner(); - this.approxDelta = lp.getApproxDelta(); - this.useApproximations = lp.isUseApproximations(); this.accuracyMethod = lp.getAccuracyMethod(); - setUseMultiInstanceChecks(lp.getUseMultiInstanceChecks()); setUseRetrievalForClassification(lp.isUseRetrievalForClassification()); } @@ -107,46 +72,9 @@ public PosNegLPStandard(AbstractReasonerComponent reasoningService, SortedSet> createConfigOptions() { - Collection> options = new LinkedList>(PosNegLP.createConfigOptions()); - BooleanConfigOption approx = new BooleanConfigOption("useApproximations", "whether to use stochastic approximations for computing accuracy", false); - options.add(approx); - DoubleConfigOption approxAccuracy = new DoubleConfigOption("approxAccuracy", "accuracy of the approximation (only for expert use)", 0.05); - options.add(approxAccuracy); - StringConfigOption accMethod = new StringConfigOption("accuracyMethod", "Specifies, which method/function to use for computing accuracy.","predacc"); // or domain/range of a property. - accMethod.setAllowedValues(new String[] {"fmeasure", "predacc"}); - options.add(accMethod); - return options; - } - @Override public void init() throws ComponentInitException { super.init(); - - String accM = getAccuracyMethod(); - if(accM.equals("standard")) { - heuristic = HeuristicType.AMEASURE; - } else if(accM.equals("fmeasure")) { - heuristic = HeuristicType.FMEASURE; - } else if(accM.equals("generalised_fmeasure")) { - heuristic = HeuristicType.GEN_FMEASURE; - } else if(accM.equals("jaccard")) { - heuristic = HeuristicType.JACCARD; - } else if(accM.equals("pred_acc")) { - heuristic = HeuristicType.PRED_ACC; - } - -// useFMeasure = configurator.getAccuracyMethod().equals("fmeasure"); - -// if((!useApproximations && useFMeasure) || (useApproximations && !useFMeasure)) { -// System.err.println("Currently F measure can only be used in combination with approximated reasoning."); -// System.exit(0); -// } - - if(useApproximations && heuristic.equals(HeuristicType.PRED_ACC)) { - System.err.println("Approximating predictive accuracy is an experimental feature. USE IT AT YOUR OWN RISK. If you consider to use it for anything serious, please extend the unit tests at org.dllearner.test.junit.HeuristicTests first and verify that it works."); - } - } /* @@ -159,591 +87,33 @@ public static String getName() { } /** - * This method computes (using the reasoner) whether a concept is too weak. - * If it is not weak, it returns the number of covered negative examples. It - * can use retrieval or instance checks for classification. - * - * @see org.dllearner.learningproblems.PosNegLP.UseMultiInstanceChecks - * TODO: Performance could be slightly improved by counting the number of - * covers instead of using sets and counting their size. - * @param concept - * The concept to test. - * @return -1 if concept is too weak and the number of covered negative - * examples otherwise. - */ - @Override - public int coveredNegativeExamplesOrTooWeak(OWLClassExpression concept) { - - if (isUseRetrievalForClassification()) { - SortedSet posClassified = getReasoner().getIndividuals(concept); - Set negAsPos = Helper.intersection(negativeExamples, posClassified); - SortedSet posAsNeg = new TreeSet(); - - // the set is constructed piecewise to avoid expensive set - // operations - // on a large number of individuals - for (OWLIndividual posExample : positiveExamples) { - if (!posClassified.contains(posExample)) - posAsNeg.add(posExample); - } - - // too weak - if (posAsNeg.size() > 0) - return -1; - // number of covered negatives - else - return negAsPos.size(); - } else { - if (getUseMultiInstanceChecks() != UseMultiInstanceChecks.NEVER) { - // two checks - if (getUseMultiInstanceChecks() == UseMultiInstanceChecks.TWOCHECKS) { - Set s = getReasoner().hasType(concept, positiveExamples); - // if the concept is too weak, then do not query negative - // examples - if (s.size() != positiveExamples.size()) - return -1; - else { - s = getReasoner().hasType(concept, negativeExamples); - return s.size(); - } - // one check - } else { - Set s = getReasoner().hasType(concept, allExamples); - // test whether all positive examples are covered - if (s.containsAll(positiveExamples)) - return s.size() - positiveExamples.size(); - else - return -1; - } - } else { - // SortedSet posAsNeg = new TreeSet(); - SortedSet negAsPos = new TreeSet(); - - for (OWLIndividual example : positiveExamples) { - if (!getReasoner().hasType(concept, example)) - return -1; - // posAsNeg.add(example); - } - for (OWLIndividual example : negativeExamples) { - if (getReasoner().hasType(concept, example)) - negAsPos.add(example); - } - - return negAsPos.size(); - } - } - } - - /** - * Computes score of a given concept using the reasoner. Either retrieval or - * instance check are used. For the latter, this method treats - * UseMultiInstanceChecks.TWO_CHECKS as if it were - * UseMultiInstanceChecks.ONE_CHECKS (it does not make much sense - * to implement TWO_CHECKS in this function, because we have to test all - * examples to create a score object anyway). + * Computes score of a given concept using the reasoner. * - * NOTE: The options above are no longer supported, because of interface changes (the options - * are more or less only relevant in combination with DIG). - * - * @see org.dllearner.learningproblems.PosNegLP.UseMultiInstanceChecks * @param concept * The concept to test. * @return Corresponding Score object. */ @Override public ScorePosNeg computeScore(OWLClassExpression concept, double noise) { - if(useOldDIGOptions) { - if (isUseRetrievalForClassification()) { - SortedSet posClassified = getReasoner().getIndividuals(concept); - Set posAsPos = Helper.intersection(positiveExamples, posClassified); - Set negAsPos = Helper.intersection(negativeExamples, posClassified); - SortedSet posAsNeg = new TreeSet(); - // piecewise set construction - for (OWLIndividual posExample : positiveExamples) { - if (!posClassified.contains(posExample)) - posAsNeg.add(posExample); - } - SortedSet negAsNeg = new TreeSet(); - for (OWLIndividual negExample : negativeExamples) { - if (!posClassified.contains(negExample)) - negAsNeg.add(negExample); - } - return new ScoreTwoValued( - OWLClassExpressionUtils.getLength(concept), - getPercentPerLengthUnit(), - posAsPos, posAsNeg, negAsPos, negAsNeg); - // instance checks for classification - } else { - Set posAsPos = new TreeSet(); - Set posAsNeg = new TreeSet(); - Set negAsPos = new TreeSet(); - Set negAsNeg = new TreeSet(); - - if (getUseMultiInstanceChecks() != UseMultiInstanceChecks.NEVER) { - SortedSet posClassified = getReasoner().hasType(concept, - allExamples); - Set negClassified = Helper.difference(allExamples, - posClassified); - posAsPos = Helper.intersection(positiveExamples, posClassified); - posAsNeg = Helper.intersection(positiveExamples, negClassified); - negAsPos = Helper.intersection(negativeExamples, posClassified); - negAsNeg = Helper.intersection(negativeExamples, negClassified); - - // System.out.println("pos classified: " + posClassified); - - return new ScoreTwoValued( - OWLClassExpressionUtils.getLength(concept), - getPercentPerLengthUnit(), - posAsPos, posAsNeg, - negAsPos, negAsNeg); - } else { - for (OWLIndividual example : positiveExamples) { - if (getReasoner().hasType(concept, example)) { - posAsPos.add(example); - } else { - posAsNeg.add(example); - } - } - for (OWLIndividual example : negativeExamples) { - if (getReasoner().hasType(concept, example)) { - negAsPos.add(example); - } else { - negAsNeg.add(example); - } - } - return new ScoreTwoValued(OWLClassExpressionUtils.getLength(concept), getPercentPerLengthUnit(), posAsPos, posAsNeg, negAsPos, - negAsNeg); - } - } - } else { - - SortedSet posAsPos = new TreeSet(); - SortedSet posAsNeg = new TreeSet(); - SortedSet negAsPos = new TreeSet(); - SortedSet negAsNeg = new TreeSet(); - - if(reasoner.getClass().isAssignableFrom(SPARQLReasoner.class)) { - SortedSet individuals = reasoner.getIndividuals(concept); - - posAsPos.addAll(Sets.intersection(positiveExamples, individuals)); - posAsNeg.addAll(Sets.difference(positiveExamples, individuals)); - negAsNeg.addAll(Sets.difference(negativeExamples, individuals)); - negAsPos.addAll(Sets.intersection(negativeExamples, individuals)); - } else { - for (OWLIndividual example : positiveExamples) { - if (getReasoner().hasType(concept, example)) { - posAsPos.add(example); - } else { - posAsNeg.add(example); - } - } - for (OWLIndividual example : negativeExamples) { - if (getReasoner().hasType(concept, example)) - negAsPos.add(example); - else - negAsNeg.add(example); - } - } - - // TODO: this computes accuracy twice - more elegant method should be implemented - double accuracy = getAccuracy(posAsPos.size(), posAsNeg.size(), negAsPos.size(), negAsNeg.size(), noise); - - return new ScoreTwoValued( - OWLClassExpressionUtils.getLength(concept), - getPercentPerLengthUnit(), - posAsPos, posAsNeg, negAsPos, negAsNeg, - accuracy); - } + Coverage[] cc = reasoningUtil.getCoverage(concept, positiveExamples, negativeExamples); - } + // TODO: this computes accuracy twice - more elegant method should be implemented + double accuracy = reasoningUtil.getAccuracyOrTooWeakExact2(accuracyMethod, cc, noise); - /* (non-Javadoc) - * @see org.dllearner.core.LearningProblem#getAccuracy(org.dllearner.core.owl.Description) - */ - @Override - public double getAccuracy(OWLClassExpression description, double noise) { - // a noise value of 1.0 means that we never return too weak (-1.0) - return getAccuracyOrTooWeak(description, noise); - } - - /* (non-Javadoc) - * @see org.dllearner.core.LearningProblem#getAccuracy(org.dllearner.core.owl.Description) - */ - @Override - public double getAccuracy(OWLClassExpression description) { - // a noise value of 1.0 means that we never return too weak (-1.0) - return getAccuracyOrTooWeak(description, 1.0); - } - - @Override - public double getAccuracyOrTooWeak(OWLClassExpression description, double noise) { - // delegates to the appropriate methods - return useApproximations ? getAccuracyOrTooWeakApprox(description, noise) : getAccuracyOrTooWeakExact(description, noise); - } - - public double getAccuracyOrTooWeakApprox(OWLClassExpression description, double noise) { - if(heuristic.equals(HeuristicType.PRED_ACC)) { - int maxNotCovered = (int) Math.ceil(noise*positiveExamples.size()); - - int notCoveredPos = 0; -// int notCoveredNeg = 0; - - int posClassifiedAsPos = 0; - int negClassifiedAsNeg = 0; - - int nrOfPosChecks = 0; - int nrOfNegChecks = 0; - - // special case: we test positive and negative examples in turn - Iterator itPos = positiveExamples.iterator(); - Iterator itNeg = negativeExamples.iterator(); - - do { - // in each loop we pick 0 or 1 positives and 0 or 1 negative - // and classify it - - if(itPos.hasNext()) { - OWLIndividual posExample = itPos.next(); -// System.out.println(posExample); - - if(getReasoner().hasType(description, posExample)) { - posClassifiedAsPos++; - } else { - notCoveredPos++; - } - nrOfPosChecks++; - - // take noise into account - if(notCoveredPos > maxNotCovered) { - return -1; - } - } - - if(itNeg.hasNext()) { - OWLIndividual negExample = itNeg.next(); - if(!getReasoner().hasType(description, negExample)) { - negClassifiedAsNeg++; - } - nrOfNegChecks++; - } - - // compute how accurate our current approximation is and return if it is sufficiently accurate - double approx[] = Heuristics.getPredAccApproximation(positiveExamples.size(), negativeExamples.size(), 1, nrOfPosChecks, posClassifiedAsPos, nrOfNegChecks, negClassifiedAsNeg); - if(approx[1] maxNotCovered) { - return -1; - } - } - } - - double recall = instancesCovered/(double)positiveExamples.size(); - - int testsPerformed = 0; - int instancesDescription = 0; - - for(OWLIndividual ind : negativeExamples) { - - if(getReasoner().hasType(description, ind)) { - instancesDescription++; - } - testsPerformed++; - - // check whether approximation is sufficiently accurate - double[] approx = Heuristics.getFScoreApproximation(instancesCovered, recall, 1, negativeExamples.size(), testsPerformed, instancesDescription); - if(approx[1] individuals = reasoner.getIndividuals(description); - - // compute diff with positive examples - notCoveredPos = Sets.difference(positiveExamples, individuals).size(); - - // compute diff with negative examples - notCoveredNeg = Sets.difference(negativeExamples, individuals).size(); - - // return 'too weak' if too many pos examples are not covered - if(notCoveredPos != 0 && notCoveredPos >= maxNotCovered) { - return -1; - } - } else { - for (OWLIndividual example : positiveExamples) { - if (!getReasoner().hasType(description, example)) { - notCoveredPos++; - - // we can stop if too many pos examples are not covered - if(notCoveredPos >= maxNotCovered) { - return -1; - } - } - } - for (OWLIndividual example : negativeExamples) { - if (!getReasoner().hasType(description, example)) { - notCoveredNeg++; - } - } - } - -// System.out.println("not covered pos: " + notCoveredPos); -// System.out.println("not covered neg: " + notCoveredNeg); - -// if(useFMeasure) { -// double recall = (positiveExamples.size() - notCoveredPos) / (double) positiveExamples.size(); -// double precision = (positiveExamples.size() - notCoveredPos) / (double) (allExamples.size() - notCoveredPos - notCoveredNeg); -// return getFMeasure(recall, precision); -// } else { - return (positiveExamples.size() - notCoveredPos + notCoveredNeg) / (double) allExamples.size(); -// } - } - - public double getAccuracy(int posAsPos, int posAsNeg, int negAsPos, int negAsNeg, double noise) { - int maxNotCovered = (int) Math.ceil(noise * positiveExamples.size()); - - if(posAsNeg > maxNotCovered) { - return -1; - } - - switch (heuristic) { - case PRED_ACC: - return (posAsPos + negAsNeg) / (double) allExamples.size(); - case FMEASURE: - double recall = posAsPos / (double)positiveExamples.size(); - - if(recall < 1 - noise) { - return -1; - } - - double precision = (negAsPos + posAsPos == 0) ? 0 : posAsPos / (double) (posAsPos + negAsPos); - - return Heuristics.getFScore(recall, precision); - default: - throw new Error("Heuristic " + heuristic + " not implemented."); - } + return new ScoreTwoValued( + OWLClassExpressionUtils.getLength(concept), + getPercentPerLengthUnit(), + cc[0].trueSet, cc[0].falseSet, cc[1].trueSet, cc[1].falseSet, + accuracy); } - public double getFMeasureOrTooWeakExact(OWLClassExpression description, double noise) { - int additionalInstances = 0; - for(OWLIndividual ind : negativeExamples) { - if(getReasoner().hasType(description, ind)) { - additionalInstances++; - } - } - - int coveredInstances = 0; - for(OWLIndividual ind : positiveExamples) { - if(getReasoner().hasType(description, ind)) { - coveredInstances++; - } - } - - double recall = coveredInstances/(double)positiveExamples.size(); - - if(recall < 1 - noise) { - return -1; - } - - double precision = (additionalInstances + coveredInstances == 0) ? 0 : coveredInstances / (double) (coveredInstances + additionalInstances); - - double fscore = Heuristics.getFScore(recall, precision); -// return getFMeasure(recall, precision); - return fscore; + @Override + public double getAccuracyOrTooWeak(OWLClassExpression description, double noise) { + return reasoningUtil.getAccuracyOrTooWeak2(accuracyMethod, description, positiveExamples, negativeExamples, noise); } - - // instead of using the standard operation, we use optimisation - // and approximation here; - // now deprecated because the Heuristics helper class is used - @Deprecated - public double getFMeasureOrTooWeakApprox(OWLClassExpression description, double noise) { - // we abort when there are too many uncovered positives - int maxNotCovered = (int) Math.ceil(noise*positiveExamples.size()); - int instancesCovered = 0; - int instancesNotCovered = 0; - int total = 0; - boolean estimatedA = false; - - double lowerBorderA = 0; - int lowerEstimateA = 0; - double upperBorderA = 1; - int upperEstimateA = positiveExamples.size(); - - for(OWLIndividual ind : positiveExamples) { - if(getReasoner().hasType(description, ind)) { - instancesCovered++; - } else { - instancesNotCovered ++; - if(instancesNotCovered > maxNotCovered) { - return -1; - } - } - - // approximation step (starting after 10 tests) - total = instancesCovered + instancesNotCovered; - if(total > 10) { - // compute confidence interval - double p1 = ClassLearningProblem.p1(instancesCovered, total); - double p2 = ClassLearningProblem.p3(p1, total); - lowerBorderA = Math.max(0, p1 - p2); - upperBorderA = Math.min(1, p1 + p2); - double size = upperBorderA - lowerBorderA; - // if the interval has a size smaller than 10%, we can be confident - if(size < 2 * approxDelta) { - // we have to distinguish the cases that the accuracy limit is - // below, within, or above the limit and that the mean is below - // or above the limit - double mean = instancesCovered/(double)total; - - // if the mean is greater than the required minimum, we can accept; - // we also accept if the interval is small and close to the minimum - // (worst case is to accept a few inaccurate descriptions) - if(mean > 1-noise || (upperBorderA > mean && size < 0.03)) { - instancesCovered = (int) (instancesCovered/(double)total * positiveExamples.size()); - upperEstimateA = (int) (upperBorderA * positiveExamples.size()); - lowerEstimateA = (int) (lowerBorderA * positiveExamples.size()); - estimatedA = true; - break; - } - - // reject only if the upper border is far away (we are very - // certain not to lose a potential solution) - if(upperBorderA + 0.1 < 1-noise) { - return -1; - } - } - } - } - - double recall = instancesCovered/(double)positiveExamples.size(); - -// MonitorFactory.add("estimatedA","count", estimatedA ? 1 : 0); -// MonitorFactory.add("aInstances","count", total); - - // we know that a definition candidate is always subclass of the - // intersection of all super classes, so we test only the relevant instances - // (leads to undesired effects for descriptions not following this rule, - // but improves performance a lot); - // for learning a superclass of a defined class, similar observations apply; - - - int testsPerformed = 0; - int instancesDescription = 0; -// boolean estimatedB = false; - - for(OWLIndividual ind : negativeExamples) { - if(getReasoner().hasType(description, ind)) { - instancesDescription++; - } - - testsPerformed++; - - if(testsPerformed > 10) { - - // compute confidence interval - double p1 = ClassLearningProblem.p1(instancesDescription, testsPerformed); - double p2 = ClassLearningProblem.p3(p1, testsPerformed); - double lowerBorder = Math.max(0, p1 - p2); - double upperBorder = Math.min(1, p1 + p2); - int lowerEstimate = (int) (lowerBorder * negativeExamples.size()); - int upperEstimate = (int) (upperBorder * negativeExamples.size()); - - double size; - if(estimatedA) { - size = getFMeasure(upperBorderA, upperEstimateA/(double)(upperEstimateA+lowerEstimate)) - getFMeasure(lowerBorderA, lowerEstimateA/(double)(lowerEstimateA+upperEstimate)); - } else { - size = getFMeasure(recall, instancesCovered/(double)(instancesCovered+lowerEstimate)) - getFMeasure(recall, instancesCovered/(double)(instancesCovered+upperEstimate)); - } - - if(size < 0.1) { - instancesDescription = (int) (instancesDescription/(double)testsPerformed * negativeExamples.size()); - break; - } - } - } - - double precision = instancesCovered/(double)(instancesDescription+instancesCovered); - if(instancesCovered + instancesDescription == 0) { - precision = 0; - } - -// System.out.println("description: " + description); -// System.out.println("recall: " + recall); -// System.out.println("precision: " + precision); -// System.out.println("F-measure: " + getFMeasure(recall, precision)); -// System.out.println("exact: " + getAccuracyOrTooWeakExact(description, noise)); - - return getFMeasure(recall, precision); - } - /* (non-Javadoc) * @see org.dllearner.core.LearningProblem#evaluate(org.dllearner.core.owl.Description) */ @@ -753,34 +123,6 @@ public EvaluatedDescription evaluate(OWLClassExpression description, double nois return new EvaluatedDescriptionPosNeg(description, score); } - private double getFMeasure(double recall, double precision) { - return 2 * precision * recall / (precision + recall); - } - - public double getApproxDelta() { - return approxDelta; - } - - public void setApproxDelta(double approxDelta) { - this.approxDelta = approxDelta; - } - - public boolean isUseApproximations() { - return useApproximations; - } - - public void setUseApproximations(boolean useApproximations) { - this.useApproximations = useApproximations; - } - - public String getAccuracyMethod() { - return accuracyMethod; - } - - public void setAccuracyMethod(String accuracyMethod) { - this.accuracyMethod = accuracyMethod; - } - /* (non-Javadoc) * @see java.lang.Object#clone() */ diff --git a/components-core/src/main/java/org/dllearner/learningproblems/PosNegLPStrict.java b/components-core/src/main/java/org/dllearner/learningproblems/PosNegLPStrict.java new file mode 100644 index 0000000000..5b0c29ca4f --- /dev/null +++ b/components-core/src/main/java/org/dllearner/learningproblems/PosNegLPStrict.java @@ -0,0 +1,206 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.learningproblems; + +import com.google.common.collect.Sets; +import org.apache.commons.lang3.NotImplementedException; +import org.dllearner.core.AbstractReasonerComponent; +import org.dllearner.core.ComponentAnn; +import org.dllearner.core.ComponentInitException; +import org.dllearner.core.EvaluatedDescription; +import org.dllearner.core.config.ConfigOption; +import org.dllearner.utilities.owl.OWLClassExpressionUtils; +import org.semanticweb.owlapi.model.OWLClassExpression; +import org.semanticweb.owlapi.model.OWLIndividual; +import org.semanticweb.owlapi.model.OWLNamedIndividual; + +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; + +/** + * @author Jens Lehmann + * + */ +@ComponentAnn(name = "PosNegLPStrict", shortName = "posNegStrict", version = 0.1, description = "three valued definition learning problem") +public class PosNegLPStrict extends PosNegLP { + + private Set neutralExamples; + + @ConfigOption(name="penaliseNeutralExamples", description = "if set to true neutral examples are penalised") + private boolean penaliseNeutralExamples = false; + + private static final double defaultAccuracyPenalty = 1; + + @ConfigOption(name="accuracyPenalty", description = "penalty for pos/neg examples which are classified as neutral", defaultValue = "1.0") + private double accuracyPenalty = defaultAccuracyPenalty; + + private static final double defaultErrorPenalty = 3; + @ConfigOption(name="errorPenalty", description = "penalty for pos. examples classified as negative or vice versa", defaultValue = "3.0") + private double errorPenalty = defaultErrorPenalty; + + + public void setAccuracyPenalty(double accuracyPenalty) { + this.accuracyPenalty = accuracyPenalty; + } + + public void setErrorPenalty(double errorPenalty) { + this.errorPenalty = errorPenalty; + } + + public void setPenaliseNeutralExamples(boolean penaliseNeutralExamples) { + this.penaliseNeutralExamples = penaliseNeutralExamples; + } + + public PosNegLPStrict(AbstractReasonerComponent reasoningService) { + super(reasoningService); + } + + public PosNegLPStrict() { super(); } + + /* (non-Javadoc) + * @see org.dllearner.core.Component#getName() + */ + public static String getName() { + return "three valued definition learning problem"; + } + + + /* (non-Javadoc) + * @see org.dllearner.core.Component#init() + */ + @Override + public void init() throws ComponentInitException { + super.init(); + // compute neutral examples, i.e. those which are neither positive + // nor negative (we have to take care to copy sets instead of + // modifying them) + neutralExamples = Sets.intersection(getReasoner().getIndividuals(), positiveExamples); + neutralExamples.retainAll(negativeExamples); + } + + /* (non-Javadoc) + * @see org.dllearner.learningproblems.DefinitionLP#computeScore(org.dllearner.core.dl.Concept) + */ + @Override + public ScorePosNeg computeScore(OWLClassExpression concept) { + if(isUseRetrievalForClassification()) { + SortedSet posClassified = getReasoner().getIndividuals(concept); + SortedSet negClassified = getReasoner().getIndividuals(dataFactory.getOWLObjectComplementOf(concept)); + Set neutClassified = new TreeSet<>(Sets.intersection(getReasoner().getIndividuals(),posClassified)); + neutClassified.retainAll(negClassified); + return new ScoreThreeValued( + OWLClassExpressionUtils.getLength(concept), accuracyPenalty, errorPenalty, + penaliseNeutralExamples, getPercentPerLengthUnit(), posClassified, neutClassified, + negClassified, positiveExamples, neutralExamples, negativeExamples); + } else { + if(getReasoner().getReasonerType().isOWLAPIReasoner()) { + if(penaliseNeutralExamples) + throw new Error("It does not make sense to use single instance checks when" + + "neutral examples are penalized. Use Retrievals instead."); + + // TODO: umschreiben in instance checks + SortedSet posClassified = new TreeSet<>(); + SortedSet negClassified = new TreeSet<>(); + // Beispiele durchgehen + // TODO: Implementierung ist ineffizient, da man hier schon in Klassen wie + // posAsNeut, posAsNeg etc. einteilen koennte; so wird das extra in der Score-Klasse + // gemacht; bei wichtigen Benchmarks des 3-wertigen Lernproblems muesste man das + // umstellen + // pos => pos + for(OWLIndividual example : positiveExamples) { + if(getReasoner().hasType(concept, example)) + posClassified.add(example); + } + // neg => pos + for(OWLIndividual example: negativeExamples) { + if(getReasoner().hasType(concept, example)) + posClassified.add(example); + } + + OWLClassExpression negatedConcept = dataFactory.getOWLObjectComplementOf(concept); + + // pos => neg + for(OWLIndividual example : positiveExamples) { + if(getReasoner().hasType(negatedConcept, example)) + negClassified.add(example); + } + // neg => neg + for(OWLIndividual example : negativeExamples) { + if(getReasoner().hasType(negatedConcept, example)) + negClassified.add(example); + } + + Set neutClassified = new TreeSet<>(Sets.intersection(getReasoner().getIndividuals(),posClassified)); + neutClassified.retainAll(negClassified); + return new ScoreThreeValued(OWLClassExpressionUtils.getLength(concept), accuracyPenalty, errorPenalty, + penaliseNeutralExamples, getPercentPerLengthUnit(), posClassified, + neutClassified, negClassified, positiveExamples, neutralExamples, + negativeExamples); + } else + throw new Error("score cannot be computed in this configuration"); + } + } + + @Override + public ScorePosNeg computeScore(OWLClassExpression ce, double noise) { + return null; + } + + public Set getNeutralExamples() { + return neutralExamples; + } + + /** + * @return the accuracyPenalty + */ + public double getAccuracyPenalty() { + return accuracyPenalty; + } + + /** + * @return the errorPenalty + */ + public double getErrorPenalty() { + return errorPenalty; + } + + /** + * @return the penaliseNeutralExamples + */ + public boolean isPenaliseNeutralExamples() { + return penaliseNeutralExamples; + } + + /* (non-Javadoc) + * @see org.dllearner.core.LearningProblem#getAccuracyOrTooWeak(org.dllearner.core.owl.Description, double) + */ + @Override + public double getAccuracyOrTooWeak(OWLClassExpression description, double minAccuracy) { + throw new NotImplementedException("Not implemented yet!"); + } + + /* (non-Javadoc) + * @see org.dllearner.core.LearningProblem#evaluate(org.dllearner.core.owl.Description) + */ + @Override + public EvaluatedDescription evaluate(OWLClassExpression description) { + throw new NotImplementedException("Not implemented yet!"); + } +} \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/learningproblems/PosNegUndLP.java b/components-core/src/main/java/org/dllearner/learningproblems/PosNegUndLP.java index aa7f93ce93..777b37106f 100644 --- a/components-core/src/main/java/org/dllearner/learningproblems/PosNegUndLP.java +++ b/components-core/src/main/java/org/dllearner/learningproblems/PosNegUndLP.java @@ -1,31 +1,45 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.learningproblems; -import java.util.HashSet; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; - -import org.dllearner.core.AbstractClassExpressionLearningProblem; import org.dllearner.core.AbstractReasonerComponent; import org.dllearner.core.ComponentAnn; import org.dllearner.core.ComponentInitException; import org.dllearner.core.EvaluatedDescription; -import org.dllearner.core.Score; -import org.dllearner.learningproblems.PosNegLP; -import org.dllearner.learningproblems.PosNegLPStandard; -import org.dllearner.learningproblems.ScorePosNeg; +import org.dllearner.core.config.ConfigOption; import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLIndividual; + +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; /** - * A ternary learning problem (positive, negative and uncertain instances) to manage the problem of the Open World Assumption - * typically employed for ontologies + * A ternary learning problem (positive, negative and uncertain instances) to manage the problem of the Open World Assumption + * typically employed for ontologies * @author Utente * */ -@ComponentAnn(name="PosNegUndLP", shortName="posNegUndLP", version=1.0, description="A learning problem with uncertain-memebrship instances") +@ComponentAnn(name="PosNegUndLP", shortName="posNegUndLP", version=1.0, description="A learning problem with uncertain-membership instances") public class PosNegUndLP extends PosNegLPStandard implements Cloneable{ //private SortedSet positiveExample; //private SortedSet negativeExample; + @ConfigOption(description = "the uncertain examples", required = true) private Set uncertainExamples; /* @@ -40,7 +54,7 @@ public static String getName() { // getter and setters public Set getPositiveExample() { - return new TreeSet(super.getPositiveExamples()); + return new TreeSet<>(super.getPositiveExamples()); } public void setPositiveExample(SortedSet positiveExample) { @@ -48,7 +62,7 @@ public void setPositiveExample(SortedSet positiveExample) { } public Set getNegativeExample() { - return new TreeSet(super.getNegativeExamples());//negativeExamples; + return new TreeSet<>(super.getNegativeExamples());//negativeExamples; } public void setNegativeExample(Set negativeExample) { @@ -56,7 +70,7 @@ public void setNegativeExample(Set negativeExample) { } public Set getUncertainExample() { - return new TreeSet(uncertainExamples); + return new TreeSet<>(uncertainExamples); } public void setUncertainExample(Set uncertainExample) { @@ -105,35 +119,24 @@ public EvaluatedDescription evaluate(OWLClassExpression description) { return super.evaluate(description); } - @Override - public double getAccuracy(OWLClassExpression description) { - // TODO Auto-generated method stub - return super.getAccuracy(description); - } - - @Override - public double getAccuracyOrTooWeak(OWLClassExpression description, double noise) { - // TODO Auto-generated method stub - return super.getAccuracyOrTooWeak(description, noise); - } //TODO add two methods: the first one performs classification by inducing the derived concept definition (see the source code of PosNegstandard ) // the second one performs classification with the induced algorithms.tree.models in order to deal with specific settings such as binary classification or missing values for TDTs and ETDTs /** - * A method for binarizing a ternary learning problem. This is important to work if you want to run a method - * such as CELOE starting from randomly generated queries - * @return + * A method for binarizing a ternary learning problem. This is important to work if you want to run a method + * such as CELOE starting from randomly generated queries + * @return the pos/neg learning problem */ public PosNegLP getPosNegLP(){ PosNegLPStandard binaryProblem= new PosNegLPStandard(getReasoner()); binaryProblem.setPositiveExamples(getPositiveExample()); - SortedSet therestOfWorld= new TreeSet(); + SortedSet therestOfWorld= new TreeSet<>(); //positive vs. the rest of world therestOfWorld.addAll(getNegativeExample()); therestOfWorld.addAll(uncertainExamples); binaryProblem.setNegativeExamples(therestOfWorld); // System.out.println(getPositiveExamples().size()+" "+therestOfWorld.size()); - return binaryProblem; + return binaryProblem; } } diff --git a/components-core/src/main/java/org/dllearner/learningproblems/PosOnlyLP.java b/components-core/src/main/java/org/dllearner/learningproblems/PosOnlyLP.java index 86dc986c12..01ec5fde56 100644 --- a/components-core/src/main/java/org/dllearner/learningproblems/PosOnlyLP.java +++ b/components-core/src/main/java/org/dllearner/learningproblems/PosOnlyLP.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,33 +16,20 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.learningproblems; -import java.util.Collection; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; -import java.util.Random; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; - +import com.google.common.collect.Sets; import org.apache.log4j.Logger; import org.dllearner.core.AbstractClassExpressionLearningProblem; import org.dllearner.core.AbstractReasonerComponent; import org.dllearner.core.ComponentAnn; import org.dllearner.core.EvaluatedDescription; -import org.dllearner.core.options.CommonConfigMappings; -import org.dllearner.core.options.ConfigEntry; -import org.dllearner.core.options.ConfigOption; -import org.dllearner.core.options.InvalidConfigOptionValueException; -import org.dllearner.core.options.StringSetConfigOption; +import org.dllearner.core.config.ConfigOption; import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLIndividual; import org.semanticweb.owlapi.model.OWLNamedIndividual; -import com.google.common.collect.Sets; +import java.util.*; /** * A learning problem, where we learn from positive examples only. @@ -56,6 +43,7 @@ public class PosOnlyLP extends AbstractClassExpressionLearningProblem positiveExamples; private List positiveExamplesShuffled; @@ -81,27 +69,6 @@ public PosOnlyLP(AbstractReasonerComponent reasoningService, SortedSet void applyConfigEntry(ConfigEntry entry) throws InvalidConfigOptionValueException { - String name = entry.getOptionName(); - if (name.equals("positiveExamples")) - positiveExamples = CommonConfigMappings - .getIndividualSet((Set) entry.getValue()); - } - - public static Collection> createConfigOptions() { - Collection> options = new LinkedList>(); - options.add(new StringSetConfigOption("positiveExamples", - "positive examples", null, true, false)); - return options; - } - public static String getName() { return "pos only learning problem"; } @@ -109,30 +76,14 @@ public static String getName() { @Override public void init() { - // old init code, where pos only was realised through pseudo negatives - - // by default we test all other instances of the knowledge base -// pseudoNegatives = Helper.difference(reasoner.getIndividuals(), positiveExamples); - - // create an instance of a standard definition learning problem - // instanciated with pseudo-negatives -// definitionLP = ComponentFactory.getPosNegLPStandard( -// reasoner, -// SetManipulation.indToString(positiveExamples), -// SetManipulation.indToString(pseudoNegatives)); - //definitionLP = new PosNegDefinitionLP(reasoningService, positiveExamples, pseudoNegatives); - // TODO: we must make sure that the problem also gets the same - // reasoning options (i.e. options are the same up to reversed example sets) -// definitionLP.init(); - Random rand = new Random(1); if(getReasoner()!=null) { - individuals = new LinkedList(getReasoner().getIndividuals()); + individuals = new LinkedList<>(getReasoner().getIndividuals()); Collections.shuffle(individuals, rand); } - positiveExamplesShuffled = new LinkedList(positiveExamples); + positiveExamplesShuffled = new LinkedList<>(positiveExamples); Collections.shuffle(positiveExamplesShuffled, rand); } @@ -147,18 +98,6 @@ public void setUseApproximations(boolean useApproximations) { this.useApproximations = useApproximations; } - /** - * @return the pseudoNegatives - */ -// public SortedSet getPseudoNegatives() { -// return pseudoNegatives; -// } - - -// public int coveredPseudoNegativeExamplesOrTooWeak(OWLClassExpression concept) { -// return definitionLP.coveredNegativeExamplesOrTooWeak(concept); -// }posOnlyLPLearningTests - /* (non-Javadoc) * @see org.dllearner.core.LearningProblem#computeScore(org.dllearner.core.owl.Description) */ @@ -166,8 +105,8 @@ public void setUseApproximations(boolean useApproximations) { public ScorePosOnly computeScore(OWLClassExpression description, double noise) { Set retrieval = getReasoner().getIndividuals(description); - Set instancesCovered = new TreeSet(); - Set instancesNotCovered = new TreeSet(); + Set instancesCovered = new TreeSet<>(); + Set instancesNotCovered = new TreeSet<>(); for(OWLIndividual ind : positiveExamples) { if(retrieval.contains(ind)) { instancesCovered.add(ind); @@ -184,28 +123,6 @@ public ScorePosOnly computeScore(OWLClassExpression description, double noise) { return new ScorePosOnly(instancesCovered, instancesNotCovered, coverage, retrieval, protusion, getAccuracy(coverage, protusion)); } - - /* (non-Javadoc) - * @see org.dllearner.core.LearningProblem#getAccuracy(org.dllearner.core.owl.Description) - */ - @Override - public double getAccuracy(OWLClassExpression description, double noise) { - Set retrieval = getReasoner().getIndividuals(description); - - int instancesCovered = 0; - for(OWLIndividual ind : positiveExamples) { - if(retrieval.contains(ind)) { - instancesCovered++; - } - } - - double coverage = instancesCovered/(double)positiveExamples.size(); - double protusion = retrieval.size() == 0 ? 0 : instancesCovered/(double)retrieval.size(); - - return getAccuracy(coverage, protusion); - } - - public double getAccuracyOrTooWeakApprox(OWLClassExpression description, double noise) { // instead of using the standard operation, we use optimisation @@ -389,4 +306,8 @@ private double getAccuracy(double coverage, double protusion) { public void setPositiveExamples(SortedSet positiveExamples) { this.positiveExamples = positiveExamples; } + + public void setPositiveExamples(Set positiveExamples) { + this.positiveExamples = new TreeSet<>(positiveExamples); + } } diff --git a/components-core/src/main/java/org/dllearner/learningproblems/PropertyAxiomLearningProblem.java b/components-core/src/main/java/org/dllearner/learningproblems/PropertyAxiomLearningProblem.java index 9b7b7272a2..e37a43a7cb 100644 --- a/components-core/src/main/java/org/dllearner/learningproblems/PropertyAxiomLearningProblem.java +++ b/components-core/src/main/java/org/dllearner/learningproblems/PropertyAxiomLearningProblem.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.learningproblems; import org.dllearner.core.AbstractLearningProblem; @@ -15,6 +33,7 @@ public class PropertyAxiomLearningProblem extends Ab @Override public void init() throws ComponentInitException { // TODO Auto-generated method stub + throw new ComponentInitException("not implemented"); } @Override @@ -23,12 +42,6 @@ public AxiomScore computeScore(T hypothesis, double noise) { return null; } - @Override - public double getAccuracy(T hypothesis, double noise) { - // TODO Auto-generated method stub - return 0; - } - @Override public double getAccuracyOrTooWeak(T hypothesis, double noise) { // TODO Auto-generated method stub diff --git a/components-core/src/main/java/org/dllearner/learningproblems/QueryTreeScore.java b/components-core/src/main/java/org/dllearner/learningproblems/QueryTreeScore.java index fd8d82afd9..8c467c44f6 100644 --- a/components-core/src/main/java/org/dllearner/learningproblems/QueryTreeScore.java +++ b/components-core/src/main/java/org/dllearner/learningproblems/QueryTreeScore.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.learningproblems; diff --git a/components-core/src/main/java/org/dllearner/learningproblems/ScorePosNeg.java b/components-core/src/main/java/org/dllearner/learningproblems/ScorePosNeg.java index 6e51ad3d3a..e777a5d32f 100644 --- a/components-core/src/main/java/org/dllearner/learningproblems/ScorePosNeg.java +++ b/components-core/src/main/java/org/dllearner/learningproblems/ScorePosNeg.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.learningproblems; import java.util.Set; diff --git a/components-core/src/main/java/org/dllearner/learningproblems/ScorePosOnly.java b/components-core/src/main/java/org/dllearner/learningproblems/ScorePosOnly.java index 97cef3e739..1ef6278d95 100644 --- a/components-core/src/main/java/org/dllearner/learningproblems/ScorePosOnly.java +++ b/components-core/src/main/java/org/dllearner/learningproblems/ScorePosOnly.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.learningproblems; import java.util.Set; diff --git a/components-core/src/main/java/org/dllearner/learningproblems/ScoreSimple.java b/components-core/src/main/java/org/dllearner/learningproblems/ScoreSimple.java new file mode 100644 index 0000000000..0f7a1d8a1d --- /dev/null +++ b/components-core/src/main/java/org/dllearner/learningproblems/ScoreSimple.java @@ -0,0 +1,56 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.learningproblems; + +import org.dllearner.core.Score; + +/** + * A simple score implementation which only contains the accuracy. + * + * @author Lorenz Buehmann + * + */ +public class ScoreSimple extends Score { + + /** + * A minimum simple score object. + */ + public static ScoreSimple MIN = new ScoreSimple(Double.NEGATIVE_INFINITY); + /** + * A maximum simple score object. + */ + public static ScoreSimple MAX = new ScoreSimple(Double.POSITIVE_INFINITY); + + private double accuracy; + + public ScoreSimple(double accuracy) { + this.accuracy = accuracy; + } + + /* (non-Javadoc) + * @see org.dllearner.core.Score#getAccuracy() + */ + @Override + public double getAccuracy() { + return accuracy; + } + + + +} diff --git a/components-core/src/main/java/org/dllearner/learningproblems/ScoreThreeValued.java b/components-core/src/main/java/org/dllearner/learningproblems/ScoreThreeValued.java index 28f0a88cdf..cba4ca0ddc 100644 --- a/components-core/src/main/java/org/dllearner/learningproblems/ScoreThreeValued.java +++ b/components-core/src/main/java/org/dllearner/learningproblems/ScoreThreeValued.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,20 +16,20 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.learningproblems; import java.text.DecimalFormat; import java.util.Set; -import org.dllearner.utilities.Helper; import org.semanticweb.owlapi.model.OWLEntity; +import com.google.common.collect.Sets; + /** * Computes the score (a negative value) by comparing the classification results * with ideal results. * - * TODO: The implementation is not very efficient, because some things are + * TODO: The implementation is not very efficient, because some things are * only computed to be able to present the score results. This means that * it would be better to compute only the necessary computations and do * the other ones only when they are needed to calculate statistical values. @@ -41,9 +41,9 @@ public class ScoreThreeValued extends ScorePosNeg { private static final long serialVersionUID = -1780084688122949685L; - public enum ScoreMethod {POSITIVE, FULL}; - - // configuration options + public enum ScoreMethod {POSITIVE, FULL} + + // configuration options private double accuracyPenalty; private double errorPenalty; private boolean penaliseNeutralExamples; @@ -69,7 +69,7 @@ public enum ScoreMethod {POSITIVE, FULL}; private Set negAsNeut; private Set posAsPos; private Set negAsNeg; - private Set neutAsNeut; + private Set neutAsNeut; private double score; private double accuracy; @@ -108,20 +108,20 @@ public ScoreThreeValued(int conceptLength, } private void computeClassificationMatrix() { - posAsNeg = Helper.intersection(posExamples,negClassified); - negAsPos = Helper.intersection(negExamples,posClassified); - posAsNeut = Helper.intersection(posExamples,neutClassified); - neutAsPos = Helper.intersection(neutExamples,posClassified); - neutAsNeg = Helper.intersection(neutExamples,negClassified); - negAsNeut = Helper.intersection(negExamples,neutClassified); + posAsNeg = Sets.intersection(posExamples,negClassified); + negAsPos = Sets.intersection(negExamples,posClassified); + posAsNeut = Sets.intersection(posExamples,neutClassified); + neutAsPos = Sets.intersection(neutExamples,posClassified); + neutAsNeg = Sets.intersection(neutExamples,negClassified); + negAsNeut = Sets.intersection(negExamples,neutClassified); // die 3 Berechnungen sind nicht so wichtig f�r die Punktzahl, d.h. falls // es Performance bringt, dann kann man sie auch ausgliedern - posAsPos = Helper.intersection(posExamples,posClassified); - negAsNeg = Helper.intersection(negExamples,negClassified); - neutAsNeut = Helper.intersection(neutExamples,neutClassified); + posAsPos = Sets.intersection(posExamples,posClassified); + negAsNeg = Sets.intersection(negExamples,negClassified); + neutAsNeut = Sets.intersection(neutExamples,neutClassified); } - private void computeStatistics() { + private void computeStatistics() { score = - posAsNeg.size()*errorPenalty - negAsPos.size()*errorPenalty - posAsNeut.size()*accuracyPenalty; @@ -130,7 +130,7 @@ private void computeStatistics() { score -= negAsNeut.size()*accuracyPenalty; if(penaliseNeutralExamples) - score -= (neutAsPos.size()*accuracyPenalty + score -= (neutAsPos.size()*accuracyPenalty + neutAsNeg.size()*accuracyPenalty); // TODO: man könnte hier statt error penalty auch accuracy penalty @@ -144,7 +144,7 @@ private void computeStatistics() { // ausgegliedert werden // int domainSize = abox.domain.size(); int numberOfExamples = posExamples.size()+negExamples.size(); - int domainSize = numberOfExamples + neutExamples.size(); + int domainSize = numberOfExamples + neutExamples.size(); int correctlyClassified = posAsPos.size() + negAsNeg.size() + neutAsNeut.size(); int correctOnExamples = posAsPos.size() + negAsNeg.size(); int errors = posAsNeg.size() + negAsPos.size(); @@ -156,10 +156,10 @@ private void computeStatistics() { // und neg. Beispiele accuracyOnExamples = (double) correctOnExamples/numberOfExamples; - accuracyOnPositiveExamples = (double) posAsPos.size()/posExamples.size(); + accuracyOnPositiveExamples = (double) posAsPos.size()/posExamples.size(); // Error = Quotient von komplett falsch klassifizierten durch Anzahl pos. - // und neg. Beispiele + // und neg. Beispiele errorRate = (double) errors/numberOfExamples; } @@ -226,11 +226,11 @@ public String toString() { str += "Inaccurately classified (penalty of " + df.format(accuracyPenalty) + " per instance):\n"; str += " positive --> neutral: " + posAsNeut + "\n"; if(penaliseNeutralExamples) { - str += " neutral --> positive: " + neutAsPos + "\n"; + str += " neutral --> positive: " + neutAsPos + "\n"; str += " neutral --> negative: " + neutAsNeg + "\n"; } if(scoreMethod == ScoreMethod.FULL) - str += " negative --> neutral: " + negAsNeut + "\n"; + str += " negative --> neutral: " + negAsNeut + "\n"; str += "Classification errors (penalty of " + df.format(errorPenalty) + " per instance):\n"; str += " positive --> negative: " + posAsNeg + "\n"; str += " negative --> positive: " + negAsPos + "\n"; @@ -238,7 +238,7 @@ public String toString() { str += " Score: " + df.format(score) + "\n"; str += " Accuracy: " + df.format(accuracy*100) + "%\n"; str += " Accuracy on examples: " + df.format(accuracyOnExamples*100) + "%\n"; - str += " Accuracy on positive examples: " + df.format(accuracyOnPositiveExamples*100) + "%\n"; + str += " Accuracy on positive examples: " + df.format(accuracyOnPositiveExamples*100) + "%\n"; str += " Error rate: " + df.format(errorRate*100) + "%\n"; return str; } @@ -272,11 +272,11 @@ public Set getNotCoveredPositives() { @Override public Set getNotCoveredNegatives() { return negAsNeg; - } + } @Override public ScorePosNeg getModifiedLengthScore(int newLength) { - return new ScoreThreeValued(newLength, accuracyPenalty, errorPenalty, penaliseNeutralExamples, percentPerLengthUnit, posClassified, neutClassified, negClassified, posExamples, neutExamples, negExamples); + return new ScoreThreeValued<>(newLength, accuracyPenalty, errorPenalty, penaliseNeutralExamples, percentPerLengthUnit, posClassified, neutClassified, negClassified, posExamples, neutExamples, negExamples); } /* (non-Javadoc) diff --git a/components-core/src/main/java/org/dllearner/learningproblems/ScoreTwoValued.java b/components-core/src/main/java/org/dllearner/learningproblems/ScoreTwoValued.java index 04a2348be7..bb536d2a58 100644 --- a/components-core/src/main/java/org/dllearner/learningproblems/ScoreTwoValued.java +++ b/components-core/src/main/java/org/dllearner/learningproblems/ScoreTwoValued.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.learningproblems; import java.util.Set; @@ -25,7 +24,7 @@ /** * Calculates accuracy and score (with respect to some length penalty) of - * a class description. + * a class description. * * TODO: In fact, a score value influencing a learning algorithm * should not be calculated here, but rather in a separate heuristic @@ -39,31 +38,16 @@ public class ScoreTwoValued extends ScorePosNeg { private static final long serialVersionUID = 6264873890324824550L; - private Set posAsPos; + private Set posAsPos; private Set posAsNeg; private Set negAsPos; - private Set negAsNeg; + private Set negAsNeg; private double score; private double accuracy; private int nrOfExamples; private int conceptLength; private double percentPerLengthUnit; - - public ScoreTwoValued(Set posAsPos, Set posAsNeg, Set negAsPos, Set negAsNeg) { - this(0,0,posAsPos,posAsNeg,negAsPos,negAsNeg); - } - - @Deprecated - public ScoreTwoValued(int conceptLength, double percentPerLengthUnit, Set posAsPos, Set posAsNeg, Set negAsPos, Set negAsNeg) { - this.conceptLength = conceptLength; - this.percentPerLengthUnit = percentPerLengthUnit; - this.posAsPos = posAsPos; - this.posAsNeg = posAsNeg; - this.negAsPos = negAsPos; - this.negAsNeg = negAsNeg; - nrOfExamples = posAsPos.size()+posAsNeg.size()+negAsPos.size()+negAsNeg.size(); - computeScore(); - } + public ScoreTwoValued(int conceptLength, double percentPerLengthUnit, Set posAsPos, Set posAsNeg, Set negAsPos, Set negAsNeg, double accuracy) { this.conceptLength = conceptLength; @@ -77,21 +61,10 @@ public ScoreTwoValued(int conceptLength, double percentPerLengthUnit, Set pos score = accuracy - 1 - percentPerLengthUnit * conceptLength; } - // score should not be computed within this class anymore, but directly within learning problem (to support - // different functions like predictive accuracy, F-measure etc.) - @Deprecated - private void computeScore() { - // compute accuracy - accuracy = posAsPos.size() + negAsNeg.size(); - accuracy = accuracy / (double) nrOfExamples; - // compute score - score = accuracy - 1 - percentPerLengthUnit * conceptLength; - } - @Override public double getAccuracy() { return accuracy; - } + } /** * score = accuracy - 1 - length * length penalty @@ -130,11 +103,11 @@ public Set getNotCoveredPositives() { @Override public Set getNotCoveredNegatives() { return negAsNeg; - } + } @Override public ScorePosNeg getModifiedLengthScore(int newLength) { - return new ScoreTwoValued(newLength, percentPerLengthUnit, posAsPos, posAsNeg, negAsPos, negAsNeg); + return new ScoreTwoValued<>(newLength, percentPerLengthUnit, posAsPos, posAsNeg, negAsPos, negAsNeg, accuracy); } /** diff --git a/components-core/src/main/java/org/dllearner/parser/KBParser.java b/components-core/src/main/java/org/dllearner/parser/KBParser.java index aee0da8e37..b87e3293a0 100644 --- a/components-core/src/main/java/org/dllearner/parser/KBParser.java +++ b/components-core/src/main/java/org/dllearner/parser/KBParser.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - /* Generated By:JavaCC: Do not edit this line. KBParser.java */ package org.dllearner.parser; diff --git a/components-core/src/main/java/org/dllearner/parser/KBParserConstants.java b/components-core/src/main/java/org/dllearner/parser/KBParserConstants.java index 2a849555eb..ebabcd9c4f 100644 --- a/components-core/src/main/java/org/dllearner/parser/KBParserConstants.java +++ b/components-core/src/main/java/org/dllearner/parser/KBParserConstants.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - /* Generated By:JavaCC: Do not edit this line. KBParserConstants.java */ package org.dllearner.parser; diff --git a/components-core/src/main/java/org/dllearner/parser/KBParserTokenManager.java b/components-core/src/main/java/org/dllearner/parser/KBParserTokenManager.java index 9571dcceab..594287ba1b 100644 --- a/components-core/src/main/java/org/dllearner/parser/KBParserTokenManager.java +++ b/components-core/src/main/java/org/dllearner/parser/KBParserTokenManager.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - /* Generated By:JavaCC: Do not edit this line. KBParserTokenManager.java */ package org.dllearner.parser; diff --git a/components-core/src/main/java/org/dllearner/parser/ParseException.java b/components-core/src/main/java/org/dllearner/parser/ParseException.java index 2cbe190916..d2b8ee6c9a 100644 --- a/components-core/src/main/java/org/dllearner/parser/ParseException.java +++ b/components-core/src/main/java/org/dllearner/parser/ParseException.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 4.1 */ /* JavaCCOptions:KEEP_LINE_COL=null */ package org.dllearner.parser; diff --git a/components-core/src/main/java/org/dllearner/parser/PrologParser.java b/components-core/src/main/java/org/dllearner/parser/PrologParser.java index f8d7d518b8..d3d94de660 100644 --- a/components-core/src/main/java/org/dllearner/parser/PrologParser.java +++ b/components-core/src/main/java/org/dllearner/parser/PrologParser.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - /* Generated By:JavaCC: Do not edit this line. PrologParser.java */ package org.dllearner.parser; diff --git a/components-core/src/main/java/org/dllearner/parser/PrologParserConstants.java b/components-core/src/main/java/org/dllearner/parser/PrologParserConstants.java index 81e07017ae..520717508c 100644 --- a/components-core/src/main/java/org/dllearner/parser/PrologParserConstants.java +++ b/components-core/src/main/java/org/dllearner/parser/PrologParserConstants.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - /* Generated By:JavaCC: Do not edit this line. PrologParserConstants.java */ package org.dllearner.parser; diff --git a/components-core/src/main/java/org/dllearner/parser/PrologParserTokenManager.java b/components-core/src/main/java/org/dllearner/parser/PrologParserTokenManager.java index f4722e7b86..f3366b753c 100644 --- a/components-core/src/main/java/org/dllearner/parser/PrologParserTokenManager.java +++ b/components-core/src/main/java/org/dllearner/parser/PrologParserTokenManager.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - /* Generated By:JavaCC: Do not edit this line. PrologParserTokenManager.java */ package org.dllearner.parser; diff --git a/components-core/src/main/java/org/dllearner/parser/SimpleCharStream.java b/components-core/src/main/java/org/dllearner/parser/SimpleCharStream.java index 3f2f774f88..252fa978bf 100644 --- a/components-core/src/main/java/org/dllearner/parser/SimpleCharStream.java +++ b/components-core/src/main/java/org/dllearner/parser/SimpleCharStream.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.1 */ /* JavaCCOptions:STATIC=false */ package org.dllearner.parser; diff --git a/components-core/src/main/java/org/dllearner/parser/Token.java b/components-core/src/main/java/org/dllearner/parser/Token.java index 73763925d0..bafe2125af 100644 --- a/components-core/src/main/java/org/dllearner/parser/Token.java +++ b/components-core/src/main/java/org/dllearner/parser/Token.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - /* Generated By:JavaCC: Do not edit this line. Token.java Version 4.1 */ /* JavaCCOptions:TOKEN_EXTENDS=,KEEP_LINE_COL=null */ package org.dllearner.parser; diff --git a/components-core/src/main/java/org/dllearner/parser/TokenMgrError.java b/components-core/src/main/java/org/dllearner/parser/TokenMgrError.java index 4686496be4..d9e5f581e3 100644 --- a/components-core/src/main/java/org/dllearner/parser/TokenMgrError.java +++ b/components-core/src/main/java/org/dllearner/parser/TokenMgrError.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - /* Generated By:JavaCC: Do not edit this line. TokenMgrError.java Version 4.1 */ /* JavaCCOptions: */ package org.dllearner.parser; diff --git a/components-core/src/main/java/org/dllearner/prolog/ArrayListSet.java b/components-core/src/main/java/org/dllearner/prolog/ArrayListSet.java deleted file mode 100644 index 1dfa8a839f..0000000000 --- a/components-core/src/main/java/org/dllearner/prolog/ArrayListSet.java +++ /dev/null @@ -1,69 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.prolog; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.Set; - -/** - * - * @author Sebastian Bader - * - * @param - */ -public class ArrayListSet extends ArrayList implements Set { - - private static final long serialVersionUID = 1530739499015312204L; - - public ArrayListSet() { - this(10); - } - - public ArrayListSet(int initialCapacity) { - super(initialCapacity); - } - - public ArrayListSet(Collection c) { - this(c.size()); - addAll(c); - } - - @Override - public boolean add(T o) { - if (contains(o)) - return false; - return super.add(o); - } - - @Override - public boolean addAll(Collection c) { - Iterator iter = c.iterator(); - boolean ret = false; - while (iter.hasNext()) { - if (add(iter.next())) { - ret = true; - } - } - return ret; - } - -} diff --git a/components-core/src/main/java/org/dllearner/prolog/Atom.java b/components-core/src/main/java/org/dllearner/prolog/Atom.java index 572d280d8e..a2498a45d9 100644 --- a/components-core/src/main/java/org/dllearner/prolog/Atom.java +++ b/components-core/src/main/java/org/dllearner/prolog/Atom.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.prolog; import java.util.ArrayList; @@ -70,9 +69,9 @@ public int getArity() { * replaced by the term. */ public Atom getInstance(Variable variable, Term term) { - ArrayList newArgs = new ArrayList(arguments.size()); + ArrayList newArgs = new ArrayList<>(arguments.size()); for (int i = 0; i < arguments.size(); i++) { - Term argument = (Term) arguments.get(i); + Term argument = arguments.get(i); newArgs.add(argument.getInstance(variable, term)); } return new Atom(name, newArgs); @@ -93,7 +92,7 @@ public String toString() { public String toPLString() { StringBuffer ret = new StringBuffer(name + "("); for (int i = 0; i < arguments.size(); i++) { - ret.append(((Term) arguments.get(i)).toPLString()); + ret.append(arguments.get(i).toPLString()); if (i + 1 < arguments.size()) ret.append(", "); } diff --git a/components-core/src/main/java/org/dllearner/prolog/Body.java b/components-core/src/main/java/org/dllearner/prolog/Body.java index a16aebfa9d..707a67aba7 100644 --- a/components-core/src/main/java/org/dllearner/prolog/Body.java +++ b/components-core/src/main/java/org/dllearner/prolog/Body.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.prolog; import java.util.ArrayList; @@ -30,7 +29,7 @@ public class Body { private ArrayList literals; public Body() { - literals = new ArrayList(); + literals = new ArrayList<>(); } public void addLiteral(Literal literal) { @@ -47,7 +46,7 @@ public boolean isEmpty() { public boolean isGround() { for (int i = 0; i < literals.size(); i++) { - if (!((Literal) literals.get(i)).isGround()) + if (!literals.get(i).isGround()) return false; } @@ -58,7 +57,7 @@ public Body getInstance(Variable variable, Term term) { Body newbody = new Body(); for (int i = 0; i < literals.size(); i++) { - Literal literal = (Literal) literals.get(i); + Literal literal = literals.get(i); newbody.addLiteral(literal.getInstance(variable, term)); } @@ -82,7 +81,7 @@ public String toPLString() { StringBuffer ret = new StringBuffer(); for (int i = 0; i < literals.size(); i++) { - ret.append(((Literal) literals.get(i)).toPLString()); + ret.append(literals.get(i).toPLString()); if (i + 1 < literals.size()) ret.append(", "); } diff --git a/components-core/src/main/java/org/dllearner/prolog/Clause.java b/components-core/src/main/java/org/dllearner/prolog/Clause.java index 566406491c..830d3aab64 100644 --- a/components-core/src/main/java/org/dllearner/prolog/Clause.java +++ b/components-core/src/main/java/org/dllearner/prolog/Clause.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.prolog; diff --git a/components-core/src/main/java/org/dllearner/prolog/Constant.java b/components-core/src/main/java/org/dllearner/prolog/Constant.java index 490a8c1ea2..2621f5529b 100644 --- a/components-core/src/main/java/org/dllearner/prolog/Constant.java +++ b/components-core/src/main/java/org/dllearner/prolog/Constant.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.prolog; /** diff --git a/components-core/src/main/java/org/dllearner/prolog/Function.java b/components-core/src/main/java/org/dllearner/prolog/Function.java index e2b8eb880c..44cde81c44 100644 --- a/components-core/src/main/java/org/dllearner/prolog/Function.java +++ b/components-core/src/main/java/org/dllearner/prolog/Function.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.prolog; import java.util.ArrayList; @@ -45,7 +44,7 @@ private Function(String name, int type, ArrayList arguments) { public Function(Function source) { this(source.name, source.type); - arguments = new ArrayList(); + arguments = new ArrayList<>(); for (int i = 0; i < source.getArity(); i++) arguments.add((Term) (source.getArgument(i)).clone()); } @@ -57,19 +56,19 @@ public Function(String name, ArrayList arguments) { public Function(String name, Term term2) { this(name, FunctionDefinition.TYPE_PREFIX); - this.arguments = new ArrayList(1); + this.arguments = new ArrayList<>(1); arguments.add(term2); } public Function(Term term1, String name) { this(name, FunctionDefinition.TYPE_POSTFIX); - this.arguments = new ArrayList(1); + this.arguments = new ArrayList<>(1); arguments.add(term1); } public Function(Term term1, String name, Term term2) { this(name, FunctionDefinition.TYPE_INFIX); - this.arguments = new ArrayList(2); + this.arguments = new ArrayList<>(2); arguments.add(term1); arguments.add(term2); } @@ -96,7 +95,7 @@ public int getType() { } public Term getArgument(int index) { - return (Term) arguments.get(index); + return arguments.get(index); } public void setArgument(int index, Term term) { @@ -128,16 +127,16 @@ public String toString() { @Override public String toPLString() { if ((type == FunctionDefinition.TYPE_PREFIX) && (getArity() == 1)) { - return name + ((Term) arguments.get(0)).toPLString(); + return name + arguments.get(0).toPLString(); } else if ((type == FunctionDefinition.TYPE_POSTFIX) && (getArity() == 1)) { - return ((Term) arguments.get(0)).toPLString() + name; + return arguments.get(0).toPLString() + name; } else if ((type == FunctionDefinition.TYPE_POSTFIX) && (getArity() == 2)) { - return ((Term) arguments.get(0)).toPLString() + name - + ((Term) arguments.get(1)).toPLString(); + return arguments.get(0).toPLString() + name + + arguments.get(1).toPLString(); } else { StringBuffer ret = new StringBuffer(name + "("); for (int i = 0; i < arguments.size(); i++) { - ret.append(((Term) arguments.get(i)).toPLString()); + ret.append(arguments.get(i).toPLString()); if (i + 1 < arguments.size()) ret.append(", "); } @@ -148,9 +147,9 @@ public String toPLString() { @Override public Term getInstance(Variable variable, Term term) { - ArrayList newArgs = new ArrayList(arguments.size()); + ArrayList newArgs = new ArrayList<>(arguments.size()); for (int i = 0; i < arguments.size(); i++) { - Term argument = (Term) arguments.get(i); + Term argument = arguments.get(i); newArgs.add(argument.getInstance(variable, term)); } return new Function(name, this.type, newArgs); diff --git a/components-core/src/main/java/org/dllearner/prolog/FunctionDefinition.java b/components-core/src/main/java/org/dllearner/prolog/FunctionDefinition.java index 4f5674b4f7..369ea4e076 100644 --- a/components-core/src/main/java/org/dllearner/prolog/FunctionDefinition.java +++ b/components-core/src/main/java/org/dllearner/prolog/FunctionDefinition.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.prolog; /** diff --git a/components-core/src/main/java/org/dllearner/prolog/List.java b/components-core/src/main/java/org/dllearner/prolog/List.java index c5ffaa8df5..4ec039e02c 100644 --- a/components-core/src/main/java/org/dllearner/prolog/List.java +++ b/components-core/src/main/java/org/dllearner/prolog/List.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.prolog; import java.util.ArrayList; @@ -47,7 +46,7 @@ public static List compose(ArrayList content) { if (content.isEmpty()) { return new List(); } else { - Term head = (Term) content.remove(0); + Term head = content.remove(0); List body = compose(content); return new List(head, body); } diff --git a/components-core/src/main/java/org/dllearner/prolog/Literal.java b/components-core/src/main/java/org/dllearner/prolog/Literal.java index 7debd6b70e..5651c5e4b8 100644 --- a/components-core/src/main/java/org/dllearner/prolog/Literal.java +++ b/components-core/src/main/java/org/dllearner/prolog/Literal.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.prolog; diff --git a/components-core/src/main/java/org/dllearner/prolog/Number.java b/components-core/src/main/java/org/dllearner/prolog/Number.java index 398b189c21..071f2946cc 100644 --- a/components-core/src/main/java/org/dllearner/prolog/Number.java +++ b/components-core/src/main/java/org/dllearner/prolog/Number.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.prolog; diff --git a/components-core/src/main/java/org/dllearner/prolog/PredicateDefinition.java b/components-core/src/main/java/org/dllearner/prolog/PredicateDefinition.java index 3218ca69fc..01dbc4ff3f 100644 --- a/components-core/src/main/java/org/dllearner/prolog/PredicateDefinition.java +++ b/components-core/src/main/java/org/dllearner/prolog/PredicateDefinition.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.prolog; /** diff --git a/components-core/src/main/java/org/dllearner/prolog/Program.java b/components-core/src/main/java/org/dllearner/prolog/Program.java index 85d9acb128..3454f513e5 100644 --- a/components-core/src/main/java/org/dllearner/prolog/Program.java +++ b/components-core/src/main/java/org/dllearner/prolog/Program.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.prolog; import java.util.ArrayList; @@ -30,7 +29,7 @@ public class Program { private ArrayList clauses; public Program() { - clauses = new ArrayList(); + clauses = new ArrayList<>(); } public void addClause(Clause clause) { @@ -43,7 +42,7 @@ public ArrayList getClauses() { public boolean isGround() { for (int c = 0; c < clauses.size(); c++) { - Clause clause = (Clause) clauses.get(c); + Clause clause = clauses.get(c); if (!clause.isGround()) return false; } @@ -68,7 +67,7 @@ public String toPLString() { StringBuffer ret = new StringBuffer(); for (int i = 0; i < clauses.size(); i++) { - ret.append(((Clause) clauses.get(i)).toPLString()); + ret.append(clauses.get(i).toPLString()); if (i + 1 < clauses.size()) ret.append("\n"); } diff --git a/components-core/src/main/java/org/dllearner/prolog/PrologConstant.java b/components-core/src/main/java/org/dllearner/prolog/PrologConstant.java index 7257758f84..e135959bab 100644 --- a/components-core/src/main/java/org/dllearner/prolog/PrologConstant.java +++ b/components-core/src/main/java/org/dllearner/prolog/PrologConstant.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.prolog; diff --git a/components-core/src/main/java/org/dllearner/prolog/StringConstant.java b/components-core/src/main/java/org/dllearner/prolog/StringConstant.java index 5ec4c0f7b4..cab10374fd 100644 --- a/components-core/src/main/java/org/dllearner/prolog/StringConstant.java +++ b/components-core/src/main/java/org/dllearner/prolog/StringConstant.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.prolog; /** diff --git a/components-core/src/main/java/org/dllearner/prolog/Term.java b/components-core/src/main/java/org/dllearner/prolog/Term.java index 9f9a67dedb..21b937013b 100644 --- a/components-core/src/main/java/org/dllearner/prolog/Term.java +++ b/components-core/src/main/java/org/dllearner/prolog/Term.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.prolog; /** diff --git a/components-core/src/main/java/org/dllearner/prolog/Variable.java b/components-core/src/main/java/org/dllearner/prolog/Variable.java index 41fc9db3d0..a0e95e01a2 100644 --- a/components-core/src/main/java/org/dllearner/prolog/Variable.java +++ b/components-core/src/main/java/org/dllearner/prolog/Variable.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.prolog; /** diff --git a/components-core/src/main/java/org/dllearner/reasoning/ClosedWorldReasoner.java b/components-core/src/main/java/org/dllearner/reasoning/ClosedWorldReasoner.java index b7e9a0cf6f..fb6b36cffc 100644 --- a/components-core/src/main/java/org/dllearner/reasoning/ClosedWorldReasoner.java +++ b/components-core/src/main/java/org/dllearner/reasoning/ClosedWorldReasoner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,77 +16,28 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.reasoning; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeMap; -import java.util.TreeSet; - -import org.dllearner.core.AbstractReasonerComponent; -import org.dllearner.core.ComponentAnn; -import org.dllearner.core.ComponentInitException; -import org.dllearner.core.KnowledgeSource; -import org.dllearner.core.ReasoningMethodUnsupportedException; +import com.google.common.collect.*; +import com.google.common.collect.Sets.SetView; +import com.google.common.hash.HashFunction; +import com.google.common.hash.Hasher; +import com.google.common.hash.Hashing; +import org.dllearner.core.*; import org.dllearner.core.config.ConfigOption; -import org.dllearner.utilities.Helper; +import org.dllearner.utilities.MapUtils; import org.dllearner.utilities.OWLAPIUtils; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormatter; -import org.semanticweb.owlapi.model.DataRangeType; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLDataHasValue; -import org.semanticweb.owlapi.model.OWLDataOneOf; -import org.semanticweb.owlapi.model.OWLDataProperty; -import org.semanticweb.owlapi.model.OWLDataPropertyExpression; -import org.semanticweb.owlapi.model.OWLDataRange; -import org.semanticweb.owlapi.model.OWLDataSomeValuesFrom; -import org.semanticweb.owlapi.model.OWLDatatype; -import org.semanticweb.owlapi.model.OWLDatatypeRestriction; -import org.semanticweb.owlapi.model.OWLEntity; -import org.semanticweb.owlapi.model.OWLFacetRestriction; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLLiteral; -import org.semanticweb.owlapi.model.OWLObjectAllValuesFrom; -import org.semanticweb.owlapi.model.OWLObjectComplementOf; -import org.semanticweb.owlapi.model.OWLObjectHasValue; -import org.semanticweb.owlapi.model.OWLObjectIntersectionOf; -import org.semanticweb.owlapi.model.OWLObjectMaxCardinality; -import org.semanticweb.owlapi.model.OWLObjectMinCardinality; -import org.semanticweb.owlapi.model.OWLObjectOneOf; -import org.semanticweb.owlapi.model.OWLObjectProperty; -import org.semanticweb.owlapi.model.OWLObjectPropertyExpression; -import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom; -import org.semanticweb.owlapi.model.OWLObjectUnionOf; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.vocab.OWL2Datatype; +import org.semanticweb.owlapi.model.*; import org.semanticweb.owlapi.vocab.OWLFacet; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import com.google.common.collect.Sets; -import com.google.common.hash.HashFunction; -import com.google.common.hash.Hasher; -import com.google.common.hash.Hashing; -import com.hp.hpl.jena.datatypes.xsd.XSDDatatype; +import java.io.*; +import java.util.*; +import java.util.Map.Entry; /** * Reasoner for fast instance checks. It works by completely dematerialising the @@ -101,7 +52,7 @@ * Meanwhile, the algorithm has been extended to also perform fast retrieval * operations. However, those need write access to memory and potentially have * to deal with all individuals in a knowledge base. For many knowledge bases, - * they should still be reasonably fast. + * they should still be reasonably fast. * * @author Jens Lehmann * @@ -121,26 +72,27 @@ public class ClosedWorldReasoner extends AbstractReasonerComponent { private TreeSet individuals; // instances of classes - private Map> classInstancesPos = new TreeMap>(); - private Map> classInstancesNeg = new TreeMap>(); + private Map> classInstancesPos = new TreeMap<>(); + private Map> classInstancesNeg = new TreeMap<>(); // object property mappings - private Map>> opPos = new TreeMap>>(); + private Map>> opPos = new TreeMap<>(); +// private Map> opPos = new TreeMap<>(); // data property mappings - private Map>> dpPos = new TreeMap>>(); + private Map>> dpPos = new TreeMap<>(); // datatype property mappings // we have one mapping for true and false for efficiency reasons - private Map> bdPos = new TreeMap>(); - private Map> bdNeg = new TreeMap>(); + private Map> bdPos = new TreeMap<>(); + private Map> bdNeg = new TreeMap<>(); // for int and double we assume that a property can have several values, // althoug this should be rare, // e.g. hasValue(object,2) and hasValue(object,3) - private Map>> dd = new TreeMap>>(); - private Map>> id = new TreeMap>>(); - private Map>> sd = new TreeMap>>(); + private Map>> dd = new TreeMap<>(); + private Map>> id = new TreeMap<>(); + private Map>> sd = new TreeMap<>(); - private Map>> numericValueMappings = new TreeMap>>(); + private Map>> numericValueMappings = new TreeMap<>(); @@ -148,7 +100,7 @@ public class ClosedWorldReasoner extends AbstractReasonerComponent { private boolean defaultNegation = true; @ConfigOption( - name = "forAllRetrievalSemantics", + name = "forAllRetrievalSemantics", description = "This option controls how to interpret the all quantifier in forall r.C. " + "The standard option is to return all those which do not have an r-filler not in C. " + "The domain semantics is to use those which are in the domain of r and do not have an r-filler not in C. " + @@ -156,14 +108,14 @@ public class ClosedWorldReasoner extends AbstractReasonerComponent { defaultValue = "standard") private ForallSemantics forallSemantics = ForallSemantics.SomeOnly; - public enum ForallSemantics { + public enum ForallSemantics { Standard, // standard all quantor - NonEmpty, // p only C for instance a returns false if there is no fact p(a,x) for any x - SomeOnly // p only C for instance a returns false if there is no fact p(a,x) with x \in C + NonEmpty, // p only C for instance a returns false if there is no fact p(a,x) for any x + SomeOnly // p only C for instance a returns false if there is no fact p(a,x) with x \in C } /** - * There are different ways on how disjointness between classes can be + * There are different ways on how disjointness between classes can be * assumed. * @author Lorenz Buehmann * @@ -178,12 +130,9 @@ public enum DisjointnessSemantics { private boolean materializeExistentialRestrictions = false; private boolean useCaching = true; private boolean handlePunning = false; + private boolean precomputeNegations = true; - /** - * Creates an instance of the fast instance checker. - */ - public ClosedWorldReasoner() { - } + public ClosedWorldReasoner() {} public ClosedWorldReasoner(TreeSet individuals, Map> classInstancesPos, @@ -192,7 +141,7 @@ public ClosedWorldReasoner(TreeSet individuals, Map> bdPos, Map> bdNeg, KnowledgeSource... sources) { - super(new HashSet(Arrays.asList(sources))); + super(new HashSet<>(Arrays.asList(sources))); this.individuals = individuals; this.classInstancesPos = classInstancesPos; this.opPos = opPos; @@ -201,18 +150,18 @@ public ClosedWorldReasoner(TreeSet individuals, this.bdNeg = bdNeg; if(baseReasoner == null){ - baseReasoner = new OWLAPIReasoner(new HashSet(Arrays.asList(sources))); + baseReasoner = new OWLAPIReasoner(new HashSet<>(Arrays.asList(sources))); try { baseReasoner.init(); } catch (ComponentInitException e) { - e.printStackTrace(); + throw new RuntimeException("Intialization of base reasoner failed.", e); } } for (OWLClass atomicConcept : baseReasoner.getClasses()) { TreeSet pos = classInstancesPos.get(atomicConcept); if(pos != null){ - classInstancesNeg.put(atomicConcept, (TreeSet) Helper.difference(individuals, pos)); + classInstancesNeg.put(atomicConcept, new TreeSet<>(Sets.difference(individuals, pos))); } else { classInstancesPos.put(atomicConcept, new TreeSet()); classInstancesNeg.put(atomicConcept, individuals); @@ -241,14 +190,19 @@ public ClosedWorldReasoner(Set sources) { } public ClosedWorldReasoner(KnowledgeSource... sources) { - super(new HashSet(Arrays.asList(sources))); + super(new HashSet<>(Arrays.asList(sources))); + } + + public ClosedWorldReasoner(OWLAPIReasoner baseReasoner) { + super(baseReasoner.getSources()); + this.baseReasoner = baseReasoner; } /** * @return The name of this component. */ public static String getName() { - return "fast instance checker"; + return "closed world reasoner"; } /* @@ -263,10 +217,6 @@ public void init() throws ComponentInitException { baseReasoner.init(); } - objectProperties = baseReasoner.getObjectProperties(); - - individuals = (TreeSet) baseReasoner.getIndividuals(); - // loadOrDematerialize(); materialize(); } @@ -301,7 +251,7 @@ private void loadOrDematerialize(){ sd = mat.sd; } catch (ClassNotFoundException | IOException e) { e.printStackTrace(); - } + } logger.debug("done."); } else { materialize(); @@ -319,7 +269,7 @@ private void loadOrDematerialize(){ oos.writeObject(mat); } catch (IOException e) { e.printStackTrace(); - } + } } } else { materialize(); @@ -330,6 +280,10 @@ private void materialize(){ logger.info("Materializing TBox..."); long dematStartTime = System.currentTimeMillis(); + objectProperties = baseReasoner.getObjectProperties(); + + individuals = (TreeSet) baseReasoner.getIndividuals(); + logger.debug("materialising concepts"); for (OWLClass cls : baseReasoner.getClasses()) { if(!cls.getIRI().isReservedVocabulary()){ @@ -341,12 +295,14 @@ private void materialize(){ * we should avoid this operation because it returns a new * set and thus could lead to memory issues * Instead, we could later answer '\neg A(x)' by just check - * for A(x) and return the inverse. + * for A(x) and return the inverse. */ -// classInstancesNeg.put(atomicConcept, (TreeSet) Helper.difference(individuals, pos)); + if(precomputeNegations) { + classInstancesNeg.put(cls, new TreeSet<>(Sets.difference(individuals, pos))); + } } else { - OWLObjectComplementOf negatedAtomicConcept = df.getOWLObjectComplementOf(cls); - classInstancesNeg.put(cls, (TreeSet) baseReasoner.getIndividuals(negatedAtomicConcept)); + OWLObjectComplementOf negatedClass = df.getOWLObjectComplementOf(cls); + classInstancesNeg.put(cls, (TreeSet) baseReasoner.getIndividuals(negatedClass)); } } else { System.err.println(cls); @@ -381,7 +337,7 @@ private void materialize(){ for (OWLDataProperty dp : baseReasoner.getStringDatatypeProperties()) { sd.put(dp, baseReasoner.getStringDatatypeMembers(dp)); - } + } logger.debug("finished materialising data properties."); if(materializeExistentialRestrictions){ @@ -401,9 +357,9 @@ private void materialize(){ OWLOntology ontology = baseReasoner.getReasoner().getRootOntology(); OWLIndividual genericIndividual = df.getOWLNamedIndividual(IRI.create("http://dl-learner.org/punning#genInd")); - Map> map = new HashMap>(); + Map> map = new HashMap<>(); for (OWLIndividual individual : individuals) { - SortedSet objects = new TreeSet(); + SortedSet objects = new TreeSet<>(); objects.add(genericIndividual); map.put(individual, objects); } @@ -416,7 +372,7 @@ private void materialize(){ for (OWLIndividual individual : individuals) { SortedSet objects = map.get(individual); if(objects == null){ - objects = new TreeSet(); + objects = new TreeSet<>(); map.put(individual, objects); } objects.add(clsAsInd); @@ -425,7 +381,7 @@ private void materialize(){ } } opPos.put(OWLPunningDetector.punningProperty, map); - objectProperties = new TreeSet(objectProperties); + objectProperties = new TreeSet<>(objectProperties); objectProperties.add(OWLPunningDetector.punningProperty); objectProperties = Collections.unmodifiableSet(objectProperties); // individuals.add(genericIndividual); @@ -437,7 +393,7 @@ private void materialize(){ private void fill(SortedSet individuals, OWLClassExpression d){ if(!d.isAnonymous()){ - classInstancesPos.get(d).addAll(individuals); + classInstancesPos.get(d.asOWLClass()).addAll(individuals); } else if(d instanceof OWLObjectIntersectionOf){ Set operands = ((OWLObjectIntersectionOf) d).getOperands(); for (OWLClassExpression operand : operands) { @@ -448,14 +404,14 @@ private void fill(SortedSet individuals, OWLClassExpression d){ OWLClassExpression filler = ((OWLObjectSomeValuesFrom) d).getFiller(); Map> map = opPos.get(role); //create new individual as object value for each individual - SortedSet newIndividuals = new TreeSet(); + SortedSet newIndividuals = new TreeSet<>(); int i = 0; for (OWLIndividual individual : individuals) { OWLIndividual newIndividual = df.getOWLNamedIndividual(IRI.create("http://dllearner.org#genInd_" + i++)); newIndividuals.add(newIndividual); SortedSet values = map.get(individual); if(values == null){ - values = new TreeSet(); + values = new TreeSet<>(); map.put(individual, values); } values.add(newIndividual); @@ -476,7 +432,7 @@ public boolean hasTypeImpl(OWLClassExpression description, OWLIndividual individ } else if (description.isOWLNothing()) { return false; } else if (!description.isAnonymous()) { - return classInstancesPos.get(description).contains(individual); + return classInstancesPos.get(description.asOWLClass()).contains(individual); } else if (description instanceof OWLObjectComplementOf) { OWLClassExpression operand = ((OWLObjectComplementOf) description).getOperand(); if(!operand.isAnonymous()) { @@ -490,7 +446,7 @@ public boolean hasTypeImpl(OWLClassExpression description, OWLIndividual individ return !hasTypeImpl(operand, individual); } else { logger.debug("Converting class expression to negation normal form in fast instance check (should be avoided if possible)."); - return hasTypeImpl(description.getNNF(), individual); + return hasTypeImpl(description.getNNF(), individual); } } } else if (description instanceof OWLObjectUnionOf) { @@ -519,7 +475,7 @@ else if (description instanceof OWLObjectSomeValuesFrom) { OWLIndividual subject = entry.getKey(); SortedSet objects = entry.getValue(); - // check if the individual is contained in the objects and + // check if the individual is contained in the objects and // subject is of type C if(objects.contains(individual)) { if(hasTypeImpl(fillerConcept, subject)) { @@ -532,7 +488,7 @@ else if (description instanceof OWLObjectSomeValuesFrom) { return true; } - SortedSet values = opPos.get(property).get(individual); + SortedSet values = opPos.get(property).get(individual); if(values == null){ return false; @@ -559,7 +515,7 @@ else if (description instanceof OWLObjectSomeValuesFrom) { if (property.isAnonymous()) {// \forall r^{-1}.C Map> mapping = opPos.get(property.getNamedProperty()); - Set values = new HashSet(); + Set values = new HashSet<>(); for (Entry> entry : mapping.entrySet()) { OWLIndividual subject = entry.getKey(); @@ -581,7 +537,7 @@ else if (description instanceof OWLObjectSomeValuesFrom) { hasCorrectFiller = true; } else { return false; - } + } } if(forallSemantics == ForallSemantics.SomeOnly) { @@ -605,7 +561,7 @@ else if (description instanceof OWLObjectSomeValuesFrom) { hasCorrectFiller = true; } else { return false; - } + } } if(forallSemantics == ForallSemantics.SomeOnly) { @@ -637,7 +593,7 @@ else if (description instanceof OWLObjectSomeValuesFrom) { OWLIndividual subject = entry.getKey(); SortedSet objects = entry.getValue(); - // count the number of subjects which are related to the individual such that + // count the number of subjects which are related to the individual such that // subject is of type C if(objects.contains(individual)) { if(hasTypeImpl(fillerConcept, subject)) { @@ -778,7 +734,7 @@ else if (description instanceof OWLObjectSomeValuesFrom) { return values != null && values.contains(value); } - } + } // else if (OWLClassExpression instanceof BooleanValueRestriction) { // DatatypeProperty dp = ((BooleanValueRestriction) description) // .getRestrictedPropertyExpression(); @@ -792,7 +748,7 @@ else if (description instanceof OWLObjectSomeValuesFrom) { // } else { // return bdNeg.get(dp).contains(individual); // } -// } +// } else if (description instanceof OWLDataSomeValuesFrom) { OWLDataPropertyExpression property = ((OWLDataSomeValuesFrom) description).getProperty(); OWLDataRange filler = ((OWLDataSomeValuesFrom) description).getFiller(); @@ -823,7 +779,7 @@ else if (description instanceof OWLDataSomeValuesFrom) { min = Double.parseDouble(facet.getFacetValue().getLiteral()); } else if(facet.getFacet() == OWLFacet.MAX_INCLUSIVE){ max = Double.parseDouble(facet.getFacetValue().getLiteral()); - } + } } //we can return false if largest number is below minimum or lowest number is above maximum @@ -852,7 +808,7 @@ else if (description instanceof OWLDataSomeValuesFrom) { min = facet.getFacetValue().parseInteger(); } else if(facet.getFacet() == OWLFacet.MAX_INCLUSIVE){ max = facet.getFacetValue().parseInteger(); - } + } } //we can return false if largest number is below minimum or lowest number is above maximum @@ -884,7 +840,7 @@ else if (description instanceof OWLDataSomeValuesFrom) { min = facet.getFacetValue(); } else if(facet.getFacet() == OWLFacet.MAX_INCLUSIVE){ max = facet.getFacetValue(); - } + } } // we can return false if largest number is below minimum or lowest number is above maximum @@ -900,7 +856,7 @@ else if (description instanceof OWLDataSomeValuesFrom) { if( (minDateTime != null && parser.parseDateTime(values.last().getLiteral()).isBefore(minDateTime)) - || + || (maxDateTime != null && parser.parseDateTime(values.first().getLiteral()).isAfter(maxDateTime)) ){ return false; @@ -918,7 +874,7 @@ else if (description instanceof OWLDataSomeValuesFrom) { OWLDataOneOf dataOneOf = (OWLDataOneOf) filler; Set values = dataOneOf.getValues(); - // given \exists r.{v_1,...,v_n} we can check for each value v_i + // given \exists r.{v_1,...,v_n} we can check for each value v_i // if (\exists r.{v_i})(ind) holds for (OWLLiteral value : values) { @@ -942,7 +898,7 @@ else if (description instanceof OWLDataSomeValuesFrom) { Map> mapping = dpPos.get(property); SortedSet values = mapping.get(individual); - + return values != null && values.contains(value); } else if (description instanceof OWLObjectOneOf) { return ((OWLObjectOneOf) description).getIndividuals().contains(individual); @@ -957,10 +913,9 @@ public SortedSet getIndividualsImpl(OWLClassExpression concept) t return getIndividualsImplFast(concept); } - public SortedSet getIndividualsImplStandard(OWLClassExpression concept) - throws ReasoningMethodUnsupportedException { + public SortedSet getIndividualsImplStandard(OWLClassExpression concept) { if (!concept.isAnonymous()) { - return classInstancesPos.get((OWLClass) concept); + return classInstancesPos.get(concept); } else if (concept instanceof OWLObjectComplementOf){ OWLClassExpression operand = ((OWLObjectComplementOf) concept).getOperand(); if(!operand.isAnonymous()) { @@ -969,7 +924,7 @@ public SortedSet getIndividualsImplStandard(OWLClassExpression co } // return rs.retrieval(concept); - SortedSet inds = new TreeSet(); + SortedSet inds = new TreeSet<>(); for (OWLIndividual i : individuals) { if (hasType(concept, i)) { inds.add(i); @@ -986,26 +941,30 @@ public SortedSet getIndividualsImplFast(OWLClassExpression descri if (description.isOWLThing()) { return (TreeSet) individuals.clone(); } else if (description.isOWLNothing()) { - return new TreeSet(); + return new TreeSet<>(); } else if (!description.isAnonymous()) { if(classInstancesPos.containsKey(description.asOWLClass())){ return (TreeSet) classInstancesPos.get(description).clone(); } else { - return new TreeSet(); + return new TreeSet<>(); } } else if (description instanceof OWLObjectComplementOf) { OWLClassExpression operand = ((OWLObjectComplementOf) description).getOperand(); if(!operand.isAnonymous()) { if(isDefaultNegation()){ - return new TreeSet(Sets.difference(individuals, classInstancesPos.get(operand))); + if(precomputeNegations) { + return (TreeSet) classInstancesNeg.get(operand).clone(); + } + SetView diff = Sets.difference(individuals, classInstancesPos.get(operand)); + return new TreeSet<>(diff); } else { return (TreeSet) classInstancesNeg.get(operand).clone(); } } // implement retrieval as default negation - return Helper.difference((TreeSet) individuals.clone(), getIndividualsImpl(operand)); + return new TreeSet<>(Sets.difference(individuals, getIndividualsImpl(operand))); } else if (description instanceof OWLObjectUnionOf) { - SortedSet ret = new TreeSet(); + SortedSet ret = new TreeSet<>(); for (OWLClassExpression operand : ((OWLObjectUnionOf) description).getOperands()) { ret.addAll(getIndividualsImpl(operand)); } @@ -1019,24 +978,31 @@ public SortedSet getIndividualsImplFast(OWLClassExpression descri } return ret; } else if (description instanceof OWLObjectSomeValuesFrom) { - SortedSet returnSet = new TreeSet(); + SortedSet returnSet = new TreeSet<>(); OWLObjectPropertyExpression property = ((OWLObjectSomeValuesFrom) description).getProperty(); OWLClassExpression filler = ((OWLObjectSomeValuesFrom) description).getFiller(); //get instances of filler concept SortedSet targetSet = getIndividualsImpl(filler); + + // the mapping of instances related by r + Map> mapping = opPos.get(property.getNamedProperty()); - if (property.isAnonymous()) { - throw new ReasoningMethodUnsupportedException("Retrieval for OWLClassExpression " - + description + " unsupported. Inverse object properties not supported."); + if (property.isAnonymous()) { // \exists r^{-1}.C + // invert the mapping + // get all objects that are related by r to (at least) one subject which is of type C + Multimap mappingInv = Multimaps.invertFrom( + MapUtils.createSortedMultiMap(opPos.get(property.getNamedProperty())), + TreeMultimap.create()); + + mapping = mappingInv.asMap(); } - Map> mapping = opPos.get(property.asOWLObjectProperty()); - + // each individual is connected to a set of individuals via the property; // we loop through the complete mapping - for(Entry> entry : mapping.entrySet()) { - SortedSet inds = entry.getValue(); + for(Entry> entry : mapping.entrySet()) { + Collection inds = entry.getValue(); for(OWLIndividual ind : inds) { if(targetSet.contains(ind)) { returnSet.add(entry.getKey()); @@ -1045,6 +1011,7 @@ public SortedSet getIndividualsImplFast(OWLClassExpression descri } } } + return returnSet; } else if (description instanceof OWLObjectAllValuesFrom) { // \forall restrictions are difficult to handle; assume we want to check @@ -1060,23 +1027,30 @@ public SortedSet getIndividualsImplFast(OWLClassExpression descri OWLObjectPropertyExpression property = ((OWLObjectAllValuesFrom) description).getProperty(); OWLClassExpression filler = ((OWLObjectAllValuesFrom) description).getFiller(); - - if (property.isAnonymous()) { - throw new ReasoningMethodUnsupportedException("Retrieval for OWLClassExpression " - + description + " unsupported. Inverse object properties not supported."); - } - - //get instances of filler concept + + // get instances of filler concept SortedSet targetSet = getIndividualsImpl(filler); - - Map> mapping = opPos.get(property.asOWLObjectProperty()); + + // the mapping of instances related by r + Map> mapping = opPos.get(property.getNamedProperty()); + + if (property.isAnonymous()) { // \forall r^{-1}.C + // invert the mapping + // get all objects that are related by r to (at least) one subject which is of type C + Multimap mappingInv = Multimaps.invertFrom( + MapUtils.createSortedMultiMap(opPos.get(property.getNamedProperty())), + TreeMultimap.create()); + + mapping = mappingInv.asMap(); + } + // SortedSet returnSet = new TreeSet(mapping.keySet()); SortedSet returnSet = (SortedSet) individuals.clone(); // each individual is connected to a set of individuals via the property; // we loop through the complete mapping - for(Entry> entry : mapping.entrySet()) { - SortedSet inds = entry.getValue(); + for(Entry> entry : mapping.entrySet()) { + Collection inds = entry.getValue(); for(OWLIndividual ind : inds) { if(!targetSet.contains(ind)) { returnSet.remove(entry.getKey()); @@ -1088,25 +1062,32 @@ public SortedSet getIndividualsImplFast(OWLClassExpression descri } else if (description instanceof OWLObjectMinCardinality) { OWLObjectPropertyExpression property = ((OWLObjectMinCardinality) description).getProperty(); OWLClassExpression filler = ((OWLObjectMinCardinality) description).getFiller(); - - if (property.isAnonymous()) { - throw new ReasoningMethodUnsupportedException("Retrieval for OWLClassExpression " - + description + " unsupported. Inverse object properties not supported."); - } - + //get instances of filler concept SortedSet targetSet = getIndividualsImpl(filler); + + // the mapping of instances related by r + Map> mapping = opPos.get(property.getNamedProperty()); + + if (property.isAnonymous()) { // \forall r^{-1}.C + // invert the mapping + // get all objects that are related by r to (at least) one subject which is of type C + Multimap mappingInv = Multimaps.invertFrom( + MapUtils.createSortedMultiMap(opPos.get(property.getNamedProperty())), + TreeMultimap.create()); + + mapping = mappingInv.asMap(); + } - Map> mapping = opPos.get(property.asOWLObjectProperty()); - - SortedSet returnSet = new TreeSet(); + + SortedSet returnSet = new TreeSet<>(); int number = ((OWLObjectMinCardinality) description).getCardinality(); - for(Entry> entry : mapping.entrySet()) { + for(Entry> entry : mapping.entrySet()) { int nrOfFillers = 0; int index = 0; - SortedSet inds = entry.getValue(); + Collection inds = entry.getValue(); // we do not need to run tests if there are not sufficiently many fillers if(inds.size() < number) { @@ -1118,17 +1099,17 @@ public SortedSet getIndividualsImplFast(OWLClassExpression descri if(nrOfFillers >= number) { returnSet.add(entry.getKey()); break; - } + } // early abort when too many instance checks failed if (inds.size() - index < number) { break; - } + } if(targetSet.contains(ind)) { nrOfFillers++; } index++; } - } + } return returnSet; } else if (description instanceof OWLObjectMaxCardinality) { @@ -1136,24 +1117,30 @@ public SortedSet getIndividualsImplFast(OWLClassExpression descri OWLClassExpression filler = ((OWLObjectMaxCardinality) description).getFiller(); int number = ((OWLObjectMaxCardinality) description).getCardinality(); - if (property.isAnonymous()) { - throw new ReasoningMethodUnsupportedException("Retrieval for OWLClassExpression " - + description + " unsupported. Inverse object properties not supported."); - } - //get instances of filler concept SortedSet targetSet = getIndividualsImpl(filler); - - Map> mapping = opPos.get(property.asOWLObjectProperty()); + + // the mapping of instances related by r + Map> mapping = opPos.get(property.getNamedProperty()); + + if (property.isAnonymous()) { // \forall r^{-1}.C + // invert the mapping + // get all objects that are related by r to (at least) one subject which is of type C + Multimap mappingInv = Multimaps.invertFrom( + MapUtils.createSortedMultiMap(opPos.get(property.getNamedProperty())), + TreeMultimap.create()); + + mapping = mappingInv.asMap(); + } // initially all individuals are in the return set and we then remove those - // with too many fillers + // with too many fillers SortedSet returnSet = (SortedSet) individuals.clone(); - for(Entry> entry : mapping.entrySet()) { + for(Entry> entry : mapping.entrySet()) { int nrOfFillers = 0; int index = 0; - SortedSet inds = entry.getValue(); + Collection inds = entry.getValue(); // we do not need to run tests if there are not sufficiently many fillers if(number < inds.size()) { @@ -1165,63 +1152,52 @@ public SortedSet getIndividualsImplFast(OWLClassExpression descri // stop inner loop when nr of fillers is reached if(nrOfFillers >= number) { break; - } + } // early abort when too many instance are true already if (inds.size() - index < number) { returnSet.add(entry.getKey()); break; - } + } if(targetSet.contains(ind)) { nrOfFillers++; } index++; } - } + } return returnSet; } else if (description instanceof OWLObjectHasValue) { OWLObjectPropertyExpression property = ((OWLObjectHasValue) description).getProperty(); - OWLIndividual value = ((OWLObjectHasValue)description).getValue(); - - if (property.isAnonymous()) { - throw new ReasoningMethodUnsupportedException("Retrieval for OWLClassExpression " - + description + " unsupported. Inverse object properties not supported."); + OWLIndividual value = ((OWLObjectHasValue)description).getFiller(); + + // the mapping of instances related by r + Map> mapping = opPos.get(property.getNamedProperty()); + + if (property.isAnonymous()) { // \exists r^{-1}.{a} + // invert the mapping + // get all objects that are related by r to (at least) one subject which is of type C + Multimap mappingInv = Multimaps.invertFrom( + MapUtils.createSortedMultiMap(opPos.get(property.getNamedProperty())), + TreeMultimap.create()); + + mapping = mappingInv.asMap(); } + + SortedSet returnSet = new TreeSet<>(); - Map> mapping = opPos.get(property.asOWLObjectProperty()); - - SortedSet returnSet = new TreeSet(); - - for(Entry> entry : mapping.entrySet()) { + for(Entry> entry : mapping.entrySet()) { if(entry.getValue().contains(value)) { returnSet.add(entry.getKey()); } } return returnSet; - } -// else if (description instanceof BooleanValueRestriction) { -// DatatypeProperty dp = ((BooleanValueRestriction) description) -// .getRestrictedPropertyExpression(); -// boolean value = ((BooleanValueRestriction) description).getBooleanValue(); -// -// if (value) { -// return (TreeSet) bdPos.get(dp).clone(); -// } else { -// return (TreeSet) bdNeg.get(dp).clone(); -// } -// } - else if (description instanceof OWLDataSomeValuesFrom) { + } else if (description instanceof OWLDataSomeValuesFrom) { OWLDataPropertyExpression property = ((OWLDataSomeValuesFrom) description).getProperty(); OWLDataRange filler = ((OWLDataSomeValuesFrom) description).getFiller(); - if (property.isAnonymous()) { - throw new ReasoningMethodUnsupportedException("Retrieval for OWLClassExpression " - + description + " unsupported. Inverse object properties not supported."); - } - if(filler.isDatatype()){ //we assume that the values are of the given datatype - return new TreeSet(dpPos.get(property).keySet()); + return new TreeSet<>(dpPos.get(property).keySet()); // OWLDatatype dt = filler.asOWLDatatype(); // if(dt.isDouble()){ // return new TreeSet(dd.get(property).keySet()); @@ -1234,26 +1210,26 @@ else if (description instanceof OWLDataSomeValuesFrom) { OWLDatatype datatype = ((OWLDatatypeRestriction) filler).getDatatype(); Set facetRestrictions = ((OWLDatatypeRestriction) filler).getFacetRestrictions(); - if(datatype.isDouble()){ + if(OWLAPIUtils.floatDatatypes.contains(datatype)) { double min = -Double.MAX_VALUE; double max = Double.MAX_VALUE; for (OWLFacetRestriction facet : facetRestrictions) { if(facet.getFacet() == OWLFacet.MIN_INCLUSIVE){ - min = facet.getFacetValue().parseDouble(); + min = Double.parseDouble(facet.getFacetValue().getLiteral()); } else if(facet.getFacet() == OWLFacet.MAX_INCLUSIVE){ - max = facet.getFacetValue().parseDouble(); - } + max = Double.parseDouble(facet.getFacetValue().getLiteral()); + } } - Map> mapping = dd.get(property); - SortedSet returnSet = new TreeSet(); - + Map> mapping = dd.get(property); + SortedSet returnSet = new TreeSet<>(); + for(Entry> entry : mapping.entrySet()) { //we can skip of largest number is below minimum or lowest number is above maximum if(entry.getValue().last() < min || entry.getValue().first() > max) { continue; } - + //search a value which is in the interval for (Double value : entry.getValue()) { if(value >= min && value <= max){ @@ -1263,18 +1239,90 @@ else if (description instanceof OWLDataSomeValuesFrom) { } } return returnSet; + } else if(OWLAPIUtils.intDatatypes.contains(datatype)) { + int min = Integer.MIN_VALUE; + int max = Integer.MAX_VALUE; + for (OWLFacetRestriction facet : facetRestrictions) { + if(facet.getFacet() == OWLFacet.MIN_INCLUSIVE){ + min = facet.getFacetValue().parseInteger(); + } else if(facet.getFacet() == OWLFacet.MAX_INCLUSIVE){ + max = facet.getFacetValue().parseInteger(); + } + } + Map> mapping = id.get(property); + SortedSet returnSet = new TreeSet<>(); + for(Entry> entry : mapping.entrySet()) { + //we can skip of largest number is below minimum or lowest number is above maximum + if(entry.getValue().last() < min || + entry.getValue().first() > max) { + continue; + } + + //search a value which is in the interval + for (Integer value : entry.getValue()) { + if(value >= min && value <= max){ + returnSet.add(entry.getKey()); + break; + } + } + } + return returnSet; + } else if(OWLAPIUtils.dtDatatypes.contains(datatype)) { + // TODO we cannot ensure the sorting, because OWL API does only String comparison + // on the lexical String value + OWLLiteral min = null; + OWLLiteral max = null; + for (OWLFacetRestriction facet : facetRestrictions) { + if (facet.getFacet() == OWLFacet.MIN_INCLUSIVE) { + min = facet.getFacetValue(); + } else if (facet.getFacet() == OWLFacet.MAX_INCLUSIVE) { + max = facet.getFacetValue(); + } + } + Map> mapping = dpPos.get(property); + // we can return false if largest number is below minimum or lowest number is above maximum + DateTimeFormatter parser = OWLAPIUtils.dateTimeParsers.get(datatype); + DateTime minDateTime = null; + if (min != null) { + minDateTime = parser.parseDateTime(min.getLiteral()); + } + DateTime maxDateTime = null; + if (max != null) { + maxDateTime = parser.parseDateTime(max.getLiteral()); + } + SortedSet returnSet = new TreeSet<>(); + for (Entry> entry : mapping.entrySet()) { + //search a value which is in the interval + for (OWLLiteral value : entry.getValue()) { + if (OWLAPIUtils.inRange(value, min, max)) { + returnSet.add(entry.getKey()); + } + } + } + return returnSet; + } + } else if(filler.getDataRangeType() == DataRangeType.DATA_ONE_OF) { + OWLDataOneOf dataOneOf = (OWLDataOneOf) filler; + Set values = dataOneOf.getValues(); + + Map> mapping = dpPos.get(property); + SortedSet returnSet = new TreeSet<>(); + + for(Entry> entry : mapping.entrySet()) { + OWLIndividual ind = entry.getKey(); + SortedSet indValues = entry.getValue(); + + if(!Sets.intersection(values, indValues).isEmpty()) { + returnSet.add(ind); + } } + return returnSet; } } else if (description instanceof OWLDataHasValue){ OWLDataPropertyExpression property = ((OWLDataHasValue) description).getProperty(); - OWLLiteral value = ((OWLDataHasValue) description).getValue(); - - if (property.isAnonymous()) { - throw new ReasoningMethodUnsupportedException("Retrieval for OWLClassExpression " - + description + " unsupported. Inverse object properties not supported."); - } + OWLLiteral value = ((OWLDataHasValue) description).getFiller(); - SortedSet returnSet = new TreeSet(); + SortedSet returnSet = new TreeSet<>(); Map> mapping = dpPos.get(property); @@ -1285,10 +1333,12 @@ else if (description instanceof OWLDataSomeValuesFrom) { } return returnSet; + } else if(description instanceof OWLObjectOneOf) { + return new TreeSet(((OWLObjectOneOf) description).getIndividuals()); } - throw new ReasoningMethodUnsupportedException("Retrieval for OWLClassExpression " - + description + " unsupported."); + throw new ReasoningMethodUnsupportedException("Retrieval for class expression " + + description + " unsupported."); } @@ -1335,7 +1385,7 @@ public Set getIntDatatypePropertiesImpl() { @Override public Set getStringDatatypePropertiesImpl() { return baseReasoner.getStringDatatypeProperties(); - } + } @Override protected SortedSet getSuperClassesImpl(OWLClassExpression concept) throws ReasoningMethodUnsupportedException { @@ -1350,7 +1400,7 @@ protected SortedSet getSubClassesImpl(OWLClassExpression con @Override protected SortedSet getSuperPropertiesImpl(OWLObjectProperty role) throws ReasoningMethodUnsupportedException { return baseReasoner.getSuperPropertiesImpl(role); - } + } @Override protected SortedSet getSubPropertiesImpl(OWLObjectProperty role) throws ReasoningMethodUnsupportedException { @@ -1360,12 +1410,12 @@ protected SortedSet getSubPropertiesImpl(OWLObjectProperty ro @Override protected SortedSet getSuperPropertiesImpl(OWLDataProperty role) throws ReasoningMethodUnsupportedException { return baseReasoner.getSuperPropertiesImpl(role); - } + } @Override protected SortedSet getSubPropertiesImpl(OWLDataProperty role) throws ReasoningMethodUnsupportedException { return baseReasoner.getSubPropertiesImpl(role); - } + } /* * (non-Javadoc) @@ -1398,6 +1448,7 @@ public boolean isSuperClassOfImpl(OWLClassExpression superConcept, OWLClassExpre /* (non-Javadoc) * @see org.dllearner.core.Reasoner#isDisjoint(OWLClass class1, OWLClass class2) */ + @Override public boolean isDisjointImpl(OWLClass clsA, OWLClass clsB) { if (disjointnessSemantics == DisjointnessSemantics.INSTANCE_BASED) { TreeSet instancesA = classInstancesPos.get(clsA); @@ -1483,18 +1534,18 @@ public final SortedSet getFalseDatatypeMembersImpl(OWLDataPropert @Override public Map> getIntDatatypeMembersImpl(OWLDataProperty datatypeProperty) { return id.get(datatypeProperty); - } + } @Override public Map> getDoubleDatatypeMembersImpl(OWLDataProperty datatypeProperty) { return dd.get(datatypeProperty); - } + } @Override public Map> getDatatypeMembersImpl(OWLDataProperty datatypeProperty) { return dpPos.get(datatypeProperty); // return rc.getDatatypeMembersImpl(OWLDataProperty); - } + } @Override public Set getRelatedIndividualsImpl(OWLIndividual individual, OWLObjectProperty objectProperty) throws ReasoningMethodUnsupportedException { @@ -1504,22 +1555,22 @@ public Set getRelatedIndividualsImpl(OWLIndividual individual, OW @Override protected Map> getObjectPropertyRelationshipsImpl(OWLIndividual individual) { return baseReasoner.getObjectPropertyRelationships(individual); - } + } @Override public Set getRelatedValuesImpl(OWLIndividual individual, OWLDataProperty datatypeProperty) throws ReasoningMethodUnsupportedException { return baseReasoner.getRelatedValues(individual, datatypeProperty); - } + } @Override public boolean isSatisfiableImpl() { return baseReasoner.isSatisfiable(); - } + } @Override public Set getLabelImpl(OWLEntity entity) throws ReasoningMethodUnsupportedException { return baseReasoner.getLabel(entity); - } + } /* * (non-Javadoc) @@ -1556,6 +1607,14 @@ public boolean remainsSatisfiableImpl(OWLAxiom axiom) { protected Set getAssertedDefinitionsImpl(OWLClass nc) { return baseReasoner.getAssertedDefinitionsImpl(nc); } + + /* (non-Javadoc) + * @see org.dllearner.core.AbstractReasonerComponent#getInconsistentClassesImpl() + */ + @Override + protected Set getInconsistentClassesImpl() throws ReasoningMethodUnsupportedException { + return baseReasoner.getInconsistentClasses(); + } public OWLAPIReasoner getReasonerComponent() { return baseReasoner; @@ -1611,4 +1670,12 @@ public OWLDatatype getDatatype(OWLDataProperty dp) { return baseReasoner.getDatatype(dp); } + /* (non-Javadoc) + * @see org.dllearner.core.AbstractReasonerComponent#setSynchronized() + */ + @Override + public void setSynchronized() { + baseReasoner.setSynchronized(); + } + } diff --git a/components-core/src/main/java/org/dllearner/reasoning/ExistentialRestrictionMaterialization.java b/components-core/src/main/java/org/dllearner/reasoning/ExistentialRestrictionMaterialization.java index ee6f6a11f5..45f564041e 100644 --- a/components-core/src/main/java/org/dllearner/reasoning/ExistentialRestrictionMaterialization.java +++ b/components-core/src/main/java/org/dllearner/reasoning/ExistentialRestrictionMaterialization.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.reasoning; @@ -13,7 +28,8 @@ import java.util.Stack; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.io.ToStringRenderer; +import org.dllearner.core.StringRenderer; +import org.dllearner.core.StringRenderer.Rendering; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLClass; import org.semanticweb.owlapi.model.OWLClassExpression; @@ -39,9 +55,9 @@ import org.semanticweb.owlapi.model.OWLOntologyManager; import org.semanticweb.owlapi.reasoner.OWLReasoner; import org.semanticweb.owlapi.reasoner.OWLReasonerFactory; +import org.semanticweb.owlapi.search.EntitySearcher; import org.semanticweb.owlapi.util.OWLClassExpressionVisitorAdapter; -import uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntaxObjectRenderer; import com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory; @@ -81,7 +97,7 @@ public Set materialize(OWLClass cls){ class SuperClassFinder extends OWLClassExpressionVisitorAdapter{ private Map> map = new HashMap<>(); - Stack> stack = new Stack>(); + Stack> stack = new Stack<>(); OWLDataFactory df; boolean onlyIfExistentialOnPath = true; @@ -117,11 +133,11 @@ private void computeSuperClasses(OWLClass cls){ } // System.out.println(s + cls); indent++; - Set superClasses = new HashSet(); + Set superClasses = new HashSet<>(); superClasses.add(cls); //get the directly asserted super classes - Collection superClassExpressions = cls.getSuperClasses(ontology); + Collection superClassExpressions = EntitySearcher.getSuperClasses(cls, ontology); //omit trivial super class superClassExpressions.remove(cls); @@ -149,12 +165,12 @@ public void visit(OWLClass ce) { */ @Override public void visit(OWLObjectIntersectionOf ce) { - Set newIntersections = new HashSet(); + Set newIntersections = new HashSet<>(); Set operands = ce.getOperands(); for (OWLClassExpression op : operands) { op.accept(this); Set operandSuperClassExpressions = stack.pop(); - Set newOperands = new HashSet(operands); + Set newOperands = new HashSet<>(operands); newOperands.remove(op); for (OWLClassExpression opSup : operandSuperClassExpressions) { newOperands.add(opSup); @@ -188,7 +204,7 @@ public void visit(OWLObjectComplementOf ce) { */ @Override public void visit(OWLObjectSomeValuesFrom ce) { - Set newRestrictions = new HashSet(); + Set newRestrictions = new HashSet<>(); newRestrictions.add(ce); OWLClassExpression filler = ce.getFiller(); filler.accept(this); @@ -293,7 +309,7 @@ public void visit(OWLDataMaxCardinality ce) { public static void main(String[] args) throws Exception{ - ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); + StringRenderer.setRenderer(Rendering.DL_SYNTAX); String s = "@prefix : ." + "@prefix rdf: ." + "@prefix owl: ." diff --git a/components-core/src/main/java/org/dllearner/reasoning/Materialization.java b/components-core/src/main/java/org/dllearner/reasoning/Materialization.java index be618eea12..dd655f5df2 100644 --- a/components-core/src/main/java/org/dllearner/reasoning/Materialization.java +++ b/components-core/src/main/java/org/dllearner/reasoning/Materialization.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.reasoning; import java.io.Serializable; @@ -16,22 +34,22 @@ class Materialization implements Serializable{ // we use sorted sets (map indices) here, because they have only log(n) // complexity for checking whether an element is contained in them // instances of classes - public Map> classInstancesPos = new TreeMap>(); - public Map> classInstancesNeg = new TreeMap>(); + public Map> classInstancesPos = new TreeMap<>(); + public Map> classInstancesNeg = new TreeMap<>(); // object property mappings - public Map>> opPos = new TreeMap>>(); + public Map>> opPos = new TreeMap<>(); // data property mappings - public Map>> dpPos = new TreeMap>>(); + public Map>> dpPos = new TreeMap<>(); // datatype property mappings // for boolean data properties we have one mapping for true and false for efficiency reasons - public Map> bdPos = new TreeMap>(); - public Map> bdNeg = new TreeMap>(); + public Map> bdPos = new TreeMap<>(); + public Map> bdNeg = new TreeMap<>(); //double datatype property mappings - public Map>> dd = new TreeMap>>(); + public Map>> dd = new TreeMap<>(); //int datatype property - public Map>> id = new TreeMap>>(); + public Map>> id = new TreeMap<>(); //string datatype property - public Map>> sd = new TreeMap>>(); + public Map>> sd = new TreeMap<>(); } \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/reasoning/OWLAPIDIGConverter.java b/components-core/src/main/java/org/dllearner/reasoning/OWLAPIDIGConverter.java deleted file mode 100644 index decd1ecde7..0000000000 --- a/components-core/src/main/java/org/dllearner/reasoning/OWLAPIDIGConverter.java +++ /dev/null @@ -1,99 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.reasoning; - -import java.io.ByteArrayOutputStream; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; - -import javax.xml.transform.OutputKeys; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerConfigurationException; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.TransformerFactoryConfigurationError; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -import org.dllearner.core.OntologyFormat; -import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLOntologyCreationException; -import org.semanticweb.owlapi.model.OWLOntologyManager; -import org.w3c.dom.Document; - - -public class OWLAPIDIGConverter { - - public static String getTellsString(URL file, OntologyFormat format, URI kbURI) { - - String ret = ""; - try { - - // Load an ontology from a physical URI - OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); - // the next function could return an ontology - manager.loadOntologyFromOntologyDocument(IRI.create(file)); -//TODO: OWLAPI3 conversion -// DIGTranslatorImpl dig = new DIGTranslatorImpl(manager); -// Document doc = dig.createTellsDocument(kbURI.toString()); -// dig.translateToDIG(manager.getOntologies(), doc, doc.getDocumentElement()); -// ret = xml2string(doc); - - }catch (OWLOntologyCreationException e) { - e.printStackTrace(); - } catch (URISyntaxException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - return ret; - } - - public static String xml2string(Document d) { - // XML als String erzeugen (ziemlich umst�ndlich) - String tellString = ""; - try { - // transformer erzeugen mit identity transformation - Transformer transformer = TransformerFactory.newInstance().newTransformer(); - transformer.setOutputProperty(OutputKeys.INDENT, "yes"); - transformer.setOutputProperty(OutputKeys.METHOD, "xml"); - // Quelle ist das DOM-Objekt - DOMSource source = new DOMSource(d); - // Resultat ist ein OutputStream, leider kann man das Resultat nicht - // direkt als String bekommen - ByteArrayOutputStream os = new ByteArrayOutputStream(); - StreamResult result = new StreamResult(os); - // Transformation ausf�hren - transformer.transform(source, result); - // String aus OutputStream generieren - tellString = os.toString(); - - } catch (TransformerConfigurationException e) { - e.printStackTrace(); - } catch (TransformerFactoryConfigurationError e) { - e.printStackTrace(); - } catch (TransformerException e) { - e.printStackTrace(); - } - return tellString; - } -} diff --git a/components-core/src/main/java/org/dllearner/reasoning/OWLAPIReasoner.java b/components-core/src/main/java/org/dllearner/reasoning/OWLAPIReasoner.java index 6dd2337af9..8f3a109664 100644 --- a/components-core/src/main/java/org/dllearner/reasoning/OWLAPIReasoner.java +++ b/components-core/src/main/java/org/dllearner/reasoning/OWLAPIReasoner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,25 +16,11 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.reasoning; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeMap; -import java.util.TreeSet; - +import com.clarkparsia.owlapi.explanation.PelletExplanation; +import com.clarkparsia.owlapiv3.XSD; +import com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory; import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.dllearner.core.AbstractReasonerComponent; @@ -46,76 +32,39 @@ import org.dllearner.kb.OWLOntologyKnowledgeSource; import org.dllearner.utilities.OWLAPIUtils; import org.dllearner.utilities.owl.OWLClassExpressionMinimizer; -import org.semanticweb.HermiT.Reasoner.ReasonerFactory; +import org.semanticweb.HermiT.Configuration; import org.semanticweb.elk.owlapi.ElkReasonerFactory; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.model.AddAxiom; -import org.semanticweb.owlapi.model.AddImport; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAnnotation; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLClassAssertionAxiom; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLDataProperty; -import org.semanticweb.owlapi.model.OWLDataPropertyRangeAxiom; -import org.semanticweb.owlapi.model.OWLDataRange; -import org.semanticweb.owlapi.model.OWLDatatype; -import org.semanticweb.owlapi.model.OWLEntity; -import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom; -import org.semanticweb.owlapi.model.OWLImportsDeclaration; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLLiteral; -import org.semanticweb.owlapi.model.OWLNamedIndividual; -import org.semanticweb.owlapi.model.OWLObject; -import org.semanticweb.owlapi.model.OWLObjectProperty; -import org.semanticweb.owlapi.model.OWLObjectPropertyExpression; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyChange; -import org.semanticweb.owlapi.model.OWLOntologyChangeException; -import org.semanticweb.owlapi.model.OWLOntologyCreationException; -import org.semanticweb.owlapi.model.OWLOntologyFormat; -import org.semanticweb.owlapi.model.OWLOntologyManager; -import org.semanticweb.owlapi.model.RemoveAxiom; +import org.semanticweb.owlapi.model.*; +import org.semanticweb.owlapi.model.parameters.Imports; import org.semanticweb.owlapi.owllink.OWLlinkHTTPXMLReasonerFactory; import org.semanticweb.owlapi.owllink.OWLlinkReasonerConfiguration; -import org.semanticweb.owlapi.reasoner.BufferingMode; -import org.semanticweb.owlapi.reasoner.FreshEntityPolicy; -import org.semanticweb.owlapi.reasoner.IndividualNodeSetPolicy; -import org.semanticweb.owlapi.reasoner.InferenceType; -import org.semanticweb.owlapi.reasoner.Node; -import org.semanticweb.owlapi.reasoner.NodeSet; -import org.semanticweb.owlapi.reasoner.NullReasonerProgressMonitor; -import org.semanticweb.owlapi.reasoner.OWLReasoner; -import org.semanticweb.owlapi.reasoner.OWLReasonerConfiguration; -import org.semanticweb.owlapi.reasoner.OWLReasonerFactory; -import org.semanticweb.owlapi.reasoner.ReasonerProgressMonitor; -import org.semanticweb.owlapi.reasoner.SimpleConfiguration; +import org.semanticweb.owlapi.reasoner.*; +import org.semanticweb.owlapi.reasoner.structural.StructuralReasonerFactory; +import org.semanticweb.owlapi.search.EntitySearcher; import org.semanticweb.owlapi.vocab.OWL2Datatype; -import org.semanticweb.owlapi.vocab.PrefixOWLOntologyFormat; -import org.semanticweb.owlapi.vocab.XSDVocabulary; - import uk.ac.manchester.cs.factplusplus.owlapiv3.FaCTPlusPlusReasonerFactory; import uk.ac.manchester.cs.jfact.JFactFactory; -import uk.ac.manchester.cs.owl.owlapi.OWL2DatatypeImpl; import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; -import uk.ac.manchester.cs.owl.owlapi.OWLDatatypeImpl; +import uk.ac.manchester.cs.owl.owlapi.alternateimpls.ThreadSafeOWLReasoner; -import com.clarkparsia.owlapi.explanation.PelletExplanation; -import com.clarkparsia.owlapiv3.XSD; -import com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory; +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.*; -import de.tudresden.inf.lat.cel.owlapi.CelReasoner; -import eu.trowl.owlapi3.rel.reasoner.dl.RELReasonerFactory; +//import de.tudresden.inf.lat.cel.owlapi.CelReasoner; +//import eu.trowl.owlapi3.rel.reasoner.dl.RELReasonerFactory; +//import org.semanticweb.elk.owlapi.ElkReasonerFactory; /** * Mapping to OWL API reasoner interface. The OWL API currently - * supports three reasoners: FaCT++, HermiT, Pellet, ELK, CEL and TrOWL. FaCT++ is connected + * supports the following OWL reasoners({@link ReasonerType}): FaCT++, HermiT, Pellet, ELK, CEL and TrOWL. FaCT++ is connected * using JNI and native libraries, while the others are pure Java * libraries. * * @author Jens Lehmann + * @author Lorenz Buehmann */ @ComponentAnn(name = "OWL API Reasoner", shortName = "oar", version = 0.8) public class OWLAPIReasoner extends AbstractReasonerComponent { @@ -128,17 +77,17 @@ public class OWLAPIReasoner extends AbstractReasonerComponent { private OWLDataFactory df; // primitives - Set atomicConcepts = new TreeSet(); - Set atomicRoles = new TreeSet(); - SortedSet datatypeProperties = new TreeSet(); - SortedSet individuals = new TreeSet(); + Set atomicConcepts = new TreeSet<>(); + Set atomicRoles = new TreeSet<>(); + SortedSet datatypeProperties = new TreeSet<>(); + SortedSet individuals = new TreeSet<>(); // namespaces - private Map prefixes = new TreeMap(); + private Map prefixes = new TreeMap<>(); private String baseURI; // references to OWL API ontologies - private Set owlAPIOntologies = new HashSet(); + private Set owlAPIOntologies = new HashSet<>(); private OWLClassExpressionMinimizer minimizer; @@ -161,7 +110,7 @@ public OWLAPIReasoner() { } public OWLAPIReasoner(KnowledgeSource... sources) { - super(new HashSet(Arrays.asList(sources))); + super(new HashSet<>(Arrays.asList(sources))); } public OWLAPIReasoner(Set sources) { @@ -182,16 +131,16 @@ public static String getName() { public void init() throws ComponentInitException { // reset variables (otherwise subsequent initialisation with // different knowledge sources will merge both) - atomicConcepts = new TreeSet(); - atomicRoles = new TreeSet(); - datatypeProperties = new TreeSet(); - individuals = new TreeSet(); + atomicConcepts = new TreeSet<>(); + atomicRoles = new TreeSet<>(); + datatypeProperties = new TreeSet<>(); + individuals = new TreeSet<>(); // create OWL API ontology manager - make sure we use a new data factory so that we don't default to the static one which can cause problems in a multi threaded environment. df = new OWLDataFactoryImpl(); manager = OWLManager.createOWLOntologyManager(); - prefixes = new TreeMap(); + prefixes = new TreeMap<>(); for (KnowledgeSource source : sources) { if (source instanceof OWLOntologyKnowledgeSource) { @@ -202,19 +151,19 @@ public void init() throws ComponentInitException { throw new ComponentInitException("OWL API Reasoner requires an OWLKnowledgeSource. Received a KS of type: " + source.getClass().getName()); } - atomicConcepts.addAll(ontology.getClassesInSignature(true)); - atomicRoles.addAll(ontology.getObjectPropertiesInSignature(true)); - datatypeProperties.addAll(ontology.getDataPropertiesInSignature(true)); - individuals.addAll(ontology.getIndividualsInSignature(true)); + atomicConcepts.addAll(ontology.getClassesInSignature(Imports.INCLUDED)); + atomicRoles.addAll(ontology.getObjectPropertiesInSignature(Imports.INCLUDED)); + datatypeProperties.addAll(ontology.getDataPropertiesInSignature(Imports.INCLUDED)); + individuals.addAll(ontology.getIndividualsInSignature(Imports.INCLUDED)); // if several knowledge sources are included, then we can only // guarantee that the base URI is from one of those sources (there // can't be more than one); but we will take care that all prefixes are // correctly imported - OWLOntologyFormat format = manager.getOntologyFormat(ontology); - if (format instanceof PrefixOWLOntologyFormat) { - prefixes.putAll(((PrefixOWLOntologyFormat) format).getPrefixName2PrefixMap()); - baseURI = ((PrefixOWLOntologyFormat) format).getDefaultPrefix(); + OWLDocumentFormat format = manager.getOntologyFormat(ontology); + if (format != null && format.isPrefixOWLOntologyFormat()) { + prefixes.putAll(format.asPrefixOWLOntologyFormat().getPrefixName2PrefixMap()); + baseURI = format.asPrefixOWLOntologyFormat().getDefaultPrefix(); prefixes.remove(""); } } @@ -223,9 +172,9 @@ public void init() throws ComponentInitException { try { //The following line illustrates a problem with using different OWLOntologyManagers. This can manifest itself if we have multiple sources who were created with different manager instances. //ontology = OWLManager.createOWLOntologyManager().createOntology(IRI.create("http://dl-learner/all"), new HashSet(owlAPIOntologies)); - ontology = manager.createOntology(IRI.create("http://dl-learner/all"), new HashSet(owlAPIOntologies)); + ontology = manager.createOntology(IRI.create("http://dl-learner/all"), new HashSet<>(owlAPIOntologies)); //we have to add all import declarations manually here, because these are not OWL axioms - List addImports = new ArrayList(); + List addImports = new ArrayList<>(); for (OWLOntology ont : owlAPIOntologies) { for (OWLImportsDeclaration importDeclaration : ont.getImportsDeclarations()) { addImports.add(new AddImport(ontology, importDeclaration)); @@ -283,36 +232,34 @@ public void init() throws ComponentInitException { } private void initDatatypes() { - Set numericDataProperties = new HashSet(); - for (OWLDataProperty dataProperty : datatypeProperties) { - Collection ranges = dataProperty.getRanges(owlAPIOntologies); - Iterator it = ranges.iterator(); - if (it.hasNext()) { - OWLDataRange range = it.next(); - if (range.isDatatype()) { - OWLDatatype datatype = range.asOWLDatatype(); - - if(datatype.isBuiltIn()) { // OWL 2 DL compliant datatypes - datatype2Properties.put(range.asOWLDatatype(), dataProperty); - - dataproperty2datatype.put(dataProperty, range.asOWLDatatype()); - - if(OWLAPIUtils.isNumericDatatype(range.asOWLDatatype())) { - numericDataProperties.add(dataProperty); - } - } else if(OWLAPIUtils.dtDatatypes.contains(datatype)) { // support for other XSD datatypes, e.g. xsd:date - datatype2Properties.put(range.asOWLDatatype(), dataProperty); - - dataproperty2datatype.put(dataProperty, range.asOWLDatatype()); - } else { - datatype2Properties.put(XSD.STRING, dataProperty); - dataproperty2datatype.put(dataProperty, XSD.STRING); - } - } else { // TODO handle complex data property ranges - - } - } - } + Set numericDataProperties = new HashSet<>(); + for (OWLDataProperty dataProperty : datatypeProperties) { + Collection ranges = EntitySearcher.getRanges(dataProperty, owlAPIOntologies); + Iterator it = ranges.iterator(); + if (it.hasNext()) { + OWLDataRange range = it.next(); + if (range.isDatatype()) { + OWLDatatype datatype = range.asOWLDatatype(); + + if (datatype.isBuiltIn()) { // OWL 2 DL compliant datatypes + datatype2Properties.put(range.asOWLDatatype(), dataProperty); + + dataproperty2datatype.put(dataProperty, range.asOWLDatatype()); + + if (OWLAPIUtils.isNumericDatatype(range.asOWLDatatype())) { + numericDataProperties.add(dataProperty); + } + } else if (OWLAPIUtils.dtDatatypes.contains(datatype)) { // support for other XSD datatypes, e.g. xsd:date + datatype2Properties.put(range.asOWLDatatype(), dataProperty); + + dataproperty2datatype.put(dataProperty, range.asOWLDatatype()); + } else { // TODO handle non-built-in data types + } + } else { // TODO handle complex data property ranges + } + } else { // TODO handle data properties without range assertion + } + } } private void initBaseReasoner() { @@ -342,23 +289,31 @@ private void initBaseReasoner() { reasonerFactory = new ElkReasonerFactory(); break; case HERMIT: - reasonerFactory = new ReasonerFactory(); - break; - case TROWL: - reasonerFactory = new RELReasonerFactory(); - break; - case CEL: - reasoner = new CelReasoner(ontology, conf); + reasonerFactory = new org.semanticweb.HermiT.ReasonerFactory(); + Configuration c = new Configuration(); + c.ignoreUnsupportedDatatypes = true; +// c.throwInconsistentOntologyException = false; + conf = c; break; +// case TROWL: +// reasonerFactory = new RELReasonerFactory(); +// break; +// case CEL: +// reasoner = new CelReasoner(ontology, conf); +// break; case OWLLINK: reasonerFactory = new OWLlinkHTTPXMLReasonerFactory(); - URL url = null; + URL url; try { url = new URL(getOwlLinkURL());//Configure the server end-point conf = new OWLlinkReasonerConfiguration(url); } catch (MalformedURLException e) { - logger.error("Illegal URL <" + url + "> for OWL Link HTTP reasoner", e); + logger.error("Illegal URL <" + getOwlLinkURL() + "> for OWL Link HTTP reasoner", e); } + break; + case STRUCTURAL : + reasonerFactory = new StructuralReasonerFactory(); + break; default: reasonerFactory = PelletReasonerFactory.getInstance(); } @@ -435,6 +390,27 @@ else if (reasoner instanceof uk.ac.manchester.cs.factplusplus.owlapiv3.FaCTPlusP return ReasonerType.OWLAPI_FUZZY; // TODO } + /** + * A convenience method that determines if the specified axiom is entailed by the set of reasoner axioms. + * @see OWLReasoner#isEntailed(OWLAxiom) + * @param axiom the axiom + * @return {@code true} if {@code axiom} is entailed by the reasoner axioms + * or {@code false} if {@code axiom} is not entailed by the reasoner + * axioms. {@code true} if the set of reasoner axioms is + * inconsistent. + */ + public boolean isEntailed(OWLAxiom axiom) { + try { + return reasoner.isEntailed(axiom); + } catch (UnsupportedOperationException e) { + if (useFallbackReasoner) { + return fallbackReasoner.isEntailed(axiom); + } else { + throw e; + } + } + } + @Override public boolean isSuperClassOfImpl(OWLClassExpression superConcept, OWLClassExpression subConcept) { @@ -469,36 +445,15 @@ public boolean isDisjointImpl(OWLClass clsA, OWLClass clsB) { // 2. check for entailment of DisjointClass(A, B) resp. // SubClassOf(OWLIntersectionOf(A, B), owl:Nothing) // OWLAxiom axiom = df.getOWLDisjointClassesAxiom(clsA, clsB); - OWLAxiom axiom = df.getOWLSubClassOfAxiom( - df.getOWLObjectIntersectionOf(clsA, clsB), df.getOWLNothing()); - boolean res; - try { - res = reasoner.isEntailed(axiom); - } catch (UnsupportedOperationException e) { - if (useFallbackReasoner) { - res = fallbackReasoner.isEntailed(axiom); - } else { - throw e; - } - } - return res; + return isEntailed(df.getOWLSubClassOfAxiom( + df.getOWLObjectIntersectionOf(clsA, clsB), + df.getOWLNothing())); } @Override protected boolean isEquivalentClassImpl(OWLClassExpression class1, OWLClassExpression class2) { - OWLEquivalentClassesAxiom axiom = df.getOWLEquivalentClassesAxiom(class1, class2); - boolean res; - try { - res = reasoner.isEntailed(axiom); - } catch (UnsupportedOperationException e) { - if (useFallbackReasoner) { - res = fallbackReasoner.isEntailed(axiom); - } else { - throw e; - } - } - return res; + return isEntailed(df.getOWLEquivalentClassesAxiom(class1, class2)); } @Override @@ -547,7 +502,7 @@ protected TreeSet getSubClassesImpl(OWLClassExpression conce } private SortedSet getRepresentativeEntities(NodeSet nodeSet){ - SortedSet representatives = new TreeSet(); + SortedSet representatives = new TreeSet<>(); for (Node node : nodeSet) { if(!node.isBottomNode() && !node.isTopNode()){ representatives.add(node.getRepresentativeElement()); @@ -557,7 +512,7 @@ private SortedSet getRepresentativeEntities(NodeSet } protected SortedSet getEquivalentClassesImpl(OWLClassExpression concept) { - SortedSet equivalentclasses = new TreeSet<>(); + SortedSet equivalentClasses = new TreeSet<>(); Node classNodes; try { classNodes = reasoner.getEquivalentClasses(concept); @@ -570,10 +525,10 @@ protected SortedSet getEquivalentClassesImpl(OWLClassExpress } for (OWLClass eqCls : classNodes.getEntitiesMinusTop()) { - equivalentclasses.add(eqCls); + equivalentClasses.add(eqCls); } - equivalentclasses.remove(concept); - return equivalentclasses; + equivalentClasses.remove(concept); + return equivalentClasses; } @Override @@ -681,7 +636,7 @@ public SortedSet getIndividualsImpl(OWLClassExpression ce) { } } - SortedSet inds = new TreeSet(); + SortedSet inds = new TreeSet<>(); for (OWLNamedIndividual ind : individuals) { inds.add(ind); } @@ -724,26 +679,15 @@ public OWLClassExpression getDomainImpl(OWLObjectProperty objectProperty) { // atomic classes, but it might be the case that in the ontology complex // domain definitions are contained - Set domains = new HashSet(); + Set domains = new HashSet<>(); // get all asserted domains - domains.addAll(objectProperty.getDomains(ontology)); + domains.addAll(EntitySearcher.getDomains(objectProperty, ontology)); // do the same for all super properties - NodeSet superProperties; - try { - superProperties = reasoner.getSuperObjectProperties(objectProperty, - false); - } catch (UnsupportedOperationException e) { - if (useFallbackReasoner) { - superProperties = fallbackReasoner.getSuperObjectProperties( - objectProperty, false); - } else { - throw e; - } - } - for (OWLObjectPropertyExpression supProp : superProperties.getFlattened()) { - domains.addAll(supProp.getDomains(ontology)); + SortedSet superProperties = getSuperProperties(objectProperty); + for (OWLObjectProperty supProp : superProperties) { + domains.addAll(EntitySearcher.getDomains(supProp, ontology)); } // last but not least, call a reasoner @@ -762,21 +706,8 @@ public OWLClassExpression getDomainImpl(OWLObjectProperty objectProperty) { domains.remove(df.getOWLThing()); - OWLClassExpression domain; - // several domains have to be treated as intersection - if (domains.size() > 1) { - domain = df.getOWLObjectIntersectionOf(domains); - - // simplify expression, e.g. keep the most specific class in - // expressions - // like A AND B - domain = minimizer.minimize(domain); - } else if (domains.size() == 1) { - domain = domains.iterator().next(); - } else { - domain = df.getOWLThing(); - } + OWLClassExpression domain = asIntersection(domains); logger.trace("Domain({},{})", objectProperty, domain); return domain; @@ -793,25 +724,15 @@ public OWLClassExpression getDomainImpl(OWLDataProperty dataProperty) { // atomic classes, but it might be the case that in the ontology complex // domain definitions are contained - Set domains = new HashSet(); + Set domains = new HashSet<>(); // get all asserted domains - domains.addAll(dataProperty.getDomains(ontology)); + domains.addAll(EntitySearcher.getDomains(dataProperty, ontology)); // do the same for all super properties - NodeSet superProperties; - try { - superProperties = reasoner.getSuperDataProperties(dataProperty, false); - } catch (UnsupportedOperationException e) { - if (useFallbackReasoner) { - superProperties = fallbackReasoner.getSuperDataProperties(dataProperty, false); - } else { - throw e; - } - } - - for (OWLDataProperty supProp : superProperties.getFlattened()) { - domains.addAll(supProp.getDomains(ontology)); + SortedSet superProperties = getSuperProperties(dataProperty); + for (OWLDataProperty supProp : superProperties) { + domains.addAll(EntitySearcher.getDomains(supProp, ontology)); } // last but not least, call a reasoner @@ -830,21 +751,8 @@ public OWLClassExpression getDomainImpl(OWLDataProperty dataProperty) { domains.remove(df.getOWLThing()); - OWLClassExpression domain; - // several domains have to be treated as intersection - if (domains.size() > 1) { - domain = df.getOWLObjectIntersectionOf(domains); - - // simplify expression, e.g. keep the most specific class in - // expressions - // like A AND B - domain = minimizer.minimize(domain); - } else if (domains.size() == 1) { - domain = domains.iterator().next(); - } else { - domain = df.getOWLThing(); - } + OWLClassExpression domain = asIntersection(domains); logger.trace("Domain({},{})", dataProperty, domain); return domain; @@ -862,27 +770,15 @@ public OWLClassExpression getRangeImpl(OWLObjectProperty objectProperty) { // atomic classes, but it might be the case that in the ontology complex // range definitions are contained - Set ranges = new HashSet(); + Set ranges = new HashSet<>(); // get all asserted ranges - ranges.addAll(objectProperty.getRanges(ontology)); + ranges.addAll(EntitySearcher.getRanges(objectProperty, ontology)); // do the same for all super properties - NodeSet superProperties; - try { - superProperties = reasoner.getSuperObjectProperties(objectProperty, false); - } catch (UnsupportedOperationException e) { - if (useFallbackReasoner) { - superProperties = fallbackReasoner.getSuperObjectProperties( - objectProperty, false); - } else { - throw e; - } - } - - for (OWLObjectPropertyExpression supProp : superProperties - .getFlattened()) { - ranges.addAll(supProp.getRanges(ontology)); + SortedSet superProperties = getSuperProperties(objectProperty); + for (OWLObjectPropertyExpression supProp : superProperties) { + ranges.addAll(EntitySearcher.getRanges(supProp, ontology)); } // last but not least, call a reasoner @@ -898,21 +794,8 @@ public OWLClassExpression getRangeImpl(OWLObjectProperty objectProperty) { } ranges.addAll(nodeSet.getFlattened()); - OWLClassExpression range; - - // several domains have to be treated as intersection - if (ranges.size() > 1) { - range = df.getOWLObjectIntersectionOf(ranges); - - // simplify expression, e.g. keep the most specific class in - // expressions - // like A AND B - range = minimizer.minimize(range); - } else if (!ranges.isEmpty()) { - range = ranges.iterator().next(); - } else { - range = df.getOWLThing(); - } + // several ranges have to be treated as intersection + OWLClassExpression range = asIntersection(ranges); logger.trace("Range({},{})", objectProperty, range); return range; @@ -930,27 +813,13 @@ public OWLDataRange getRangeImpl(OWLDataProperty datatypeProperty) { } } - private OWLClassExpression asIntersection(NodeSet nodeSet){ - if(nodeSet.isEmpty() || nodeSet.isTopSingleton()){ + private OWLClassExpression asIntersection(Set classExpressions){ + if(classExpressions.isEmpty()){ return df.getOWLThing(); - } else if(nodeSet.isSingleton()){ - return nodeSet.iterator().next().getRepresentativeElement(); + } else if(classExpressions.size() == 1){ + return classExpressions.iterator().next(); } else { - Set operands = new HashSet(nodeSet.getNodes().size()); - for (Node node : nodeSet) { - if(node.getSize() != 0) { - if(!node.isTopNode() && !node.isBottomNode()){ - operands.add(node.getRepresentativeElement()); - } - } else { - logger.warn("Reasoner returned empty node. Seems to be a bug."); - } - - } - if(operands.size() == 1) { - return operands.iterator().next(); - } - return df.getOWLObjectIntersectionOf(operands); + return df.getOWLObjectIntersectionOf(classExpressions); } } @@ -959,8 +828,8 @@ private OWLClassExpression getDescriptionFromReturnedDomain(NodeSet no return df.getOWLThing(); } - Set union = new HashSet(); - Set domains = new HashSet(); + Set union = new HashSet<>(); + Set domains = new HashSet<>(); for (Node node : nodeSet) { union.add(node.getRepresentativeElement()); @@ -988,19 +857,19 @@ private OWLClassExpression getDescriptionFromReturnedDomain(NodeSet no @Override public Map> getPropertyMembersImpl(OWLObjectProperty objectProperty) { - Map> map = new TreeMap>(); + Map> map = new TreeMap<>(); for (OWLIndividual ind : individuals) { Set inds = getRelatedIndividuals(ind, objectProperty); - map.put(ind, new TreeSet(inds)); + map.put(ind, new TreeSet<>(inds)); } return map; } @Override protected Map> getObjectPropertyRelationshipsImpl(OWLIndividual individual) { - Map> map = new HashMap>(); + Map> map = new HashMap<>(); - for (OWLObjectProperty prop : ontology.getObjectPropertiesInSignature(true)) { + for (OWLObjectProperty prop : ontology.getObjectPropertiesInSignature(Imports.INCLUDED)) { map.put(prop, getRelatedIndividualsImpl(individual, prop)); } @@ -1024,7 +893,7 @@ public Set getRelatedIndividualsImpl( } } - Set values = new HashSet(namedIndividuals.size()); + Set values = new HashSet<>(namedIndividuals.size()); for (OWLNamedIndividual namedIndividual : namedIndividuals) { values.add(namedIndividual); @@ -1059,25 +928,13 @@ public Set getRelatedValuesImpl(OWLIndividual individual, // } public Map> getDoubleValues(OWLDataProperty dataProperty) { - Map> map = new TreeMap>(); + Map> map = new TreeMap<>(); for (OWLIndividual ind : individuals) { - Set literals; - try { - literals = reasoner.getDataPropertyValues( - ind.asOWLNamedIndividual(), dataProperty); - - } catch (UnsupportedOperationException e) { - if (useFallbackReasoner) { - literals = fallbackReasoner.getDataPropertyValues( - ind.asOWLNamedIndividual(), dataProperty); - } else { - throw e; - } - } + Set literals = getRelatedValuesImpl(ind, dataProperty); if (!literals.isEmpty()) { - SortedSet values = new TreeSet(); + SortedSet values = new TreeSet<>(); for (OWLLiteral lit : literals) { if (lit.isDouble()) { values.add(lit.parseDouble()); @@ -1092,25 +949,13 @@ public Map> getDoubleValues(OWLDataProperty dat @Override public Map> getDatatypeMembersImpl(OWLDataProperty dataProperty) { - Map> map = - new TreeMap>(); + Map> map = new TreeMap<>(); for (OWLIndividual ind : individuals) { - Set literals; - - try { - literals = reasoner.getDataPropertyValues(ind.asOWLNamedIndividual(), dataProperty); - } catch (UnsupportedOperationException e) { - if (useFallbackReasoner) { - literals = fallbackReasoner.getDataPropertyValues( - ind.asOWLNamedIndividual(), dataProperty); - } else { - throw e; - } - } + Set literals = getRelatedValuesImpl(ind, dataProperty); if (!literals.isEmpty()) { - map.put(ind, new TreeSet(literals)); + map.put(ind, new TreeSet<>(literals)); } } return map; @@ -1121,7 +966,7 @@ public Map> getDatatypeMembersImpl(OWLDataP // consists of equivalent classes; this method picks one class // from each node to flatten the set of nodes private TreeSet getFirstClasses(NodeSet nodeSet) { - TreeSet concepts = new TreeSet(); + TreeSet concepts = new TreeSet<>(); for (Node node : nodeSet) { // take one element from the set and ignore the rest // (TODO: we need to make sure we always ignore the same concepts) @@ -1136,7 +981,7 @@ private TreeSet getFirstClasses(NodeSet nodeSet) { } private Set getFirstClassesNoTopBottom(NodeSet nodeSet) { - Set concepts = new HashSet(); + Set concepts = new HashSet<>(); for (Node node : nodeSet) { if(!node.isBottomNode() && !node.isTopNode()){ concepts.add(node.getRepresentativeElement()); @@ -1146,7 +991,7 @@ private Set getFirstClassesNoTopBottom(NodeSet nodeSet) { } private TreeSet getFirstObjectProperties(NodeSet nodeSet) { - TreeSet roles = new TreeSet(); + TreeSet roles = new TreeSet<>(); for (Node node : nodeSet) { if (node.isBottomNode() || node.isTopNode()) { continue; @@ -1159,16 +1004,17 @@ private TreeSet getFirstObjectProperties(NodeSet getFirstDatatypeProperties(NodeSet nodeSet) { - TreeSet roles = new TreeSet(); + TreeSet roles = new TreeSet<>(); for (Node node : nodeSet) { if (node.isBottomNode() || node.isTopNode()) { continue; @@ -1178,10 +1024,11 @@ private TreeSet getFirstDatatypeProperties(NodeSet getBooleanDatatypePropertiesImpl() { @Override public Set getDoubleDatatypePropertiesImpl() { - Set properties = new TreeSet(); + Set properties = new TreeSet<>(); for (OWLDatatype dt:OWLAPIUtils.floatDatatypes) { properties.addAll(datatype2Properties.get(dt)); @@ -1203,7 +1050,7 @@ public Set getDoubleDatatypePropertiesImpl() { @Override public Set getIntDatatypePropertiesImpl() { - Set properties = new TreeSet(); + Set properties = new TreeSet<>(); for (OWLDatatype dt:OWLAPIUtils.intDatatypes) { properties.addAll(datatype2Properties.get(dt)); @@ -1256,24 +1103,19 @@ public Set getOWLAPIOntologies() { @Override public Set getInconsistentClassesImpl() { - Set concepts = new HashSet(); + Set unsatisfiableClasses; - Node unsatClsNodes; try { - unsatClsNodes = reasoner.getUnsatisfiableClasses(); + unsatisfiableClasses = reasoner.getUnsatisfiableClasses().getEntitiesMinusBottom(); } catch (UnsupportedOperationException e) { if (useFallbackReasoner) { - unsatClsNodes = fallbackReasoner.getUnsatisfiableClasses(); + unsatisfiableClasses = fallbackReasoner.getUnsatisfiableClasses().getEntitiesMinusBottom(); } else { throw e; } } - for (OWLClass concept : unsatClsNodes.getEntities()) { - concepts.add(df.getOWLClass(IRI.create(concept.toStringID()))); - } - - return concepts; + return unsatisfiableClasses; } public Set getInconsistentOWLClasses() { @@ -1292,8 +1134,8 @@ public Set getInconsistentOWLClasses() { @Override public Set getLabelImpl(OWLEntity entity) { - Collection labelAnnotations = entity.getAnnotations(ontology, df.getRDFSLabel()); - Set annotations = new HashSet(); + Collection labelAnnotations = EntitySearcher.getAnnotations(entity, ontology, df.getRDFSLabel()); + Set annotations = new HashSet<>(); for (OWLAnnotation label : labelAnnotations) { annotations.add((OWLLiteral) label.getValue()); } @@ -1337,12 +1179,12 @@ public boolean remainsSatisfiableImpl(OWLAxiom axiom) { /** * Returns asserted class definitions of given class * - * @param nc the class + * @param cls the class * @return the asserted class definitions */ @Override protected Set getAssertedDefinitionsImpl(OWLClass cls) { - Collection definitions = cls.getEquivalentClasses(ontology); + Collection definitions = EntitySearcher.getEquivalentClasses(cls, ontology); return new HashSet<>(definitions); } @@ -1405,4 +1247,21 @@ public void setUseFallbackReasoner(boolean useFallbackReasoner) { public OWLDatatype getDatatype(OWLDataProperty dp) { return dataproperty2datatype.get(dp); } + + /* (non-Javadoc) + * @see org.dllearner.core.AbstractReasonerComponent#setSynchronized() + */ + @Override + public void setSynchronized() { + if(!(reasoner instanceof ThreadSafeOWLReasoner)) { + reasoner = new ThreadSafeOWLReasoner(reasoner); + } + } + + public static void main(String[] args) throws Exception{ + OWLOntology o = OWLManager.createOWLOntologyManager().loadOntologyFromOntologyDocument(new File("/tmp/test2.rdf")); + System.out.println(o.getClassesInSignature()); + System.out.println(o.getDataPropertiesInSignature()); + System.out.println(o.getIndividualsInSignature().size()); + } } diff --git a/components-core/src/main/java/org/dllearner/reasoning/OWLPunningDetector.java b/components-core/src/main/java/org/dllearner/reasoning/OWLPunningDetector.java index 2d7b75ac89..4ba96b62a4 100644 --- a/components-core/src/main/java/org/dllearner/reasoning/OWLPunningDetector.java +++ b/components-core/src/main/java/org/dllearner/reasoning/OWLPunningDetector.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.reasoning; @@ -14,6 +29,7 @@ import com.google.common.collect.Sets; +import org.semanticweb.owlapi.model.parameters.Imports; import uk.ac.manchester.cs.owl.owlapi.OWLObjectPropertyImpl; /** @@ -41,9 +57,9 @@ enum PunningType { /** * Checks whether the same IRI denotes both a class and an individual in the ontology. - * @param ontology - * @param iri - * @return + * @param ontology the OWL ontology + * @param cls the OWL class + * @return TRUE if the class IRI is used also as individual, otherwise FALSE */ public static boolean hasPunning(OWLOntology ontology, OWLClass cls){ return hasPunning(ontology, cls.getIRI()); @@ -51,20 +67,19 @@ public static boolean hasPunning(OWLOntology ontology, OWLClass cls){ /** * Checks whether the ontology contains punning, i.e. entities declared as both, class and individual. - * @param ontology - * @param iri - * @return + * @param ontology the OWL ontology + * @return TRUE if there is at least one entity that is both, class and individual, otherwise FALSE */ public static boolean hasPunning(OWLOntology ontology){ - Set classes = ontology.getClassesInSignature(true); - Set individuals = ontology.getIndividualsInSignature(true); + Set classes = ontology.getClassesInSignature(Imports.INCLUDED); + Set individuals = ontology.getIndividualsInSignature(Imports.INCLUDED); - Set classIRIs = new HashSet(classes.size()); + Set classIRIs = new HashSet<>(classes.size()); for (OWLClass cls : classes) { classIRIs.add(cls.getIRI()); } - Set individualIRIs = new HashSet(classes.size()); + Set individualIRIs = new HashSet<>(classes.size()); for (OWLNamedIndividual ind : individuals) { individualIRIs.add(ind.getIRI()); } @@ -74,24 +89,13 @@ public static boolean hasPunning(OWLOntology ontology){ /** * Checks whether the same IRI denotes both a class and an individual in the ontology. - * @param ontology - * @param iri - * @return + * @param ontology the OWL ontology + * @param iri the entity IRI + * @return TRUE if the IRI is used as both, class and individual, otherwise FALSE */ public static boolean hasPunning(OWLOntology ontology, IRI iri){ boolean isClass = ontology.getClassesInSignature().contains(ontology.getOWLOntologyManager().getOWLDataFactory().getOWLClass(iri)); boolean isIndividual = ontology.getIndividualsInSignature().contains(ontology.getOWLOntologyManager().getOWLDataFactory().getOWLNamedIndividual(iri)); return isClass && isIndividual; } - - /** - * Checks whether the same IRI denotes both a class and an individual in the ontology. - * @param ontology - * @param iri - * @return - */ - public static boolean hasPunning(OWLOntology ontology, String iri){ - return hasPunning(ontology, IRI.create(iri)); - } - } diff --git a/components-core/src/main/java/org/dllearner/reasoning/ReasonerImplementation.java b/components-core/src/main/java/org/dllearner/reasoning/ReasonerImplementation.java index 31cb392f07..91fc01ea13 100644 --- a/components-core/src/main/java/org/dllearner/reasoning/ReasonerImplementation.java +++ b/components-core/src/main/java/org/dllearner/reasoning/ReasonerImplementation.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.reasoning; @@ -14,8 +29,8 @@ public enum ReasonerImplementation { HERMIT, JFACT, ELK, - CEL, - TROWL, +// TROWL, OWLLINK, - FACT + FACT, + STRUCTURAL } diff --git a/components-core/src/main/java/org/dllearner/reasoning/ReasonerType.java b/components-core/src/main/java/org/dllearner/reasoning/ReasonerType.java index 0bc16f5b9a..c94a4a55eb 100755 --- a/components-core/src/main/java/org/dllearner/reasoning/ReasonerType.java +++ b/components-core/src/main/java/org/dllearner/reasoning/ReasonerType.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.reasoning; /** @@ -26,5 +25,12 @@ * */ public enum ReasonerType { - KAON2, DIG, FAST_RETRIEVAL, OWLAPI_FACT, OWLAPI_PELLET, OWLAPI_HERMIT, OWLAPI_FUZZY, OWLAPI_JFACT, CLOSED_WORLD_REASONER, SPARQL_NATIVE + DIG, OWLAPI_FACT, OWLAPI_PELLET, OWLAPI_HERMIT, OWLAPI_FUZZY, OWLAPI_JFACT, CLOSED_WORLD_REASONER, SPARQL_NATIVE; + + /** + * @return true if reasoner type is OWL API, otherwise false + */ + public boolean isOWLAPIReasoner() { + return this.name().toUpperCase().startsWith("OWLAPI_"); + } } \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/reasoning/SKOSReasoner.java b/components-core/src/main/java/org/dllearner/reasoning/SKOSReasoner.java index d866e31a43..9bb70346e8 100644 --- a/components-core/src/main/java/org/dllearner/reasoning/SKOSReasoner.java +++ b/components-core/src/main/java/org/dllearner/reasoning/SKOSReasoner.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.reasoning; @@ -43,8 +58,7 @@ public class SKOSReasoner extends GenericRuleReasoner { protected static Map ruleFiles; /** - * @param factory - * @param configuration + * @param factory the Jena reasoner factory */ public SKOSReasoner(ReasonerFactory factory) { super(loadRulesLevel(DEFAULT_RULES), factory); diff --git a/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java b/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java index 9d8ae35edb..cde46fdd98 100644 --- a/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java +++ b/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,33 +16,32 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.reasoning; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeMap; -import java.util.TreeSet; -import java.util.concurrent.TimeUnit; - +import com.clarkparsia.owlapiv3.XSD; +import com.google.common.base.Function; +import com.google.common.base.Joiner; +import com.google.common.collect.HashMultimap; +import com.google.common.collect.Iterables; +import com.google.common.collect.Multimap; +import com.google.common.collect.Sets; +import com.hp.hpl.jena.query.ParameterizedSparqlString; +import com.hp.hpl.jena.query.QueryExecution; +import com.hp.hpl.jena.query.QuerySolution; +import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.rdf.model.*; +import com.hp.hpl.jena.sparql.engine.http.QueryExceptionHTTP; +import com.hp.hpl.jena.vocabulary.OWL; +import com.hp.hpl.jena.vocabulary.OWL2; +import com.hp.hpl.jena.vocabulary.RDF; +import com.hp.hpl.jena.vocabulary.RDFS; import org.aksw.jena_sparql_api.core.QueryExecutionFactory; import org.aksw.jena_sparql_api.delay.core.QueryExecutionFactoryDelay; import org.aksw.jena_sparql_api.model.QueryExecutionFactoryModel; import org.aksw.jena_sparql_api.pagination.core.QueryExecutionFactoryPaginated; +import org.apache.commons.lang3.NotImplementedException; import org.apache.jena.riot.RDFDataMgr; -import org.dllearner.core.AbstractReasonerComponent; -import org.dllearner.core.ComponentAnn; -import org.dllearner.core.ComponentInitException; -import org.dllearner.core.IndividualReasoner; -import org.dllearner.core.KnowledgeSource; -import org.dllearner.core.ReasoningMethodUnsupportedException; -import org.dllearner.core.SchemaReasoner; +import org.dllearner.core.*; import org.dllearner.core.config.ConfigOption; import org.dllearner.core.owl.ClassHierarchy; import org.dllearner.core.owl.DatatypePropertyHierarchy; @@ -54,61 +53,34 @@ import org.dllearner.kb.sparql.QueryExecutionFactoryHttp; import org.dllearner.kb.sparql.SPARQLQueryUtils; import org.dllearner.kb.sparql.SparqlEndpoint; +import org.dllearner.utilities.OWLAPIUtils; import org.dllearner.utilities.OwlApiJenaUtils; import org.dllearner.utilities.datastructures.SortedSetTuple; import org.dllearner.utilities.owl.OWLClassExpressionToSPARQLConverter; -import org.semanticweb.owlapi.model.EntityType; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLDataProperty; -import org.semanticweb.owlapi.model.OWLDataRange; -import org.semanticweb.owlapi.model.OWLDatatype; -import org.semanticweb.owlapi.model.OWLEntity; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLLiteral; -import org.semanticweb.owlapi.model.OWLObjectProperty; -import org.semanticweb.owlapi.model.OWLProperty; +import org.semanticweb.owlapi.model.*; import org.semanticweb.owlapi.util.OWLObjectDuplicator; import org.semanticweb.owlapi.vocab.XSDVocabulary; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.Marker; import org.slf4j.helpers.BasicMarkerFactory; - import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; -import com.clarkparsia.owlapiv3.XSD; -import com.google.common.collect.HashMultimap; -import com.google.common.collect.ImmutableSortedSet; -import com.google.common.collect.Multimap; -import com.hp.hpl.jena.query.ParameterizedSparqlString; -import com.hp.hpl.jena.query.QueryExecution; -import com.hp.hpl.jena.query.QuerySolution; -import com.hp.hpl.jena.query.ResultSet; -import com.hp.hpl.jena.rdf.model.Literal; -import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.ModelFactory; -import com.hp.hpl.jena.rdf.model.RDFNode; -import com.hp.hpl.jena.rdf.model.Resource; -import com.hp.hpl.jena.sparql.engine.http.QueryExceptionHTTP; -import com.hp.hpl.jena.vocabulary.OWL; -import com.hp.hpl.jena.vocabulary.OWL2; -import com.hp.hpl.jena.vocabulary.RDF; -import com.hp.hpl.jena.vocabulary.RDFS; +import java.util.*; +import java.util.Map.Entry; +import java.util.concurrent.TimeUnit; /** * A reasoner implementation that provides inference services by the execution * of SPARQL queries on *
    - *
  • local files (usually in forms of JENA API models) - *
  • remote SPARQL endpoints + *
  • local files (usually in forms of JENA API models)
  • + *
  • remote SPARQL endpoints
  • *
+ *

* Compared to other reasoner implementations, it doesn't do any pre-computation * by default because it might be too expensive on very large knowledge bases. - * + *

* @author Lorenz Buehmann * */ @@ -120,7 +92,7 @@ public class SPARQLReasoner extends AbstractReasonerComponent implements SchemaR public enum PopularityType { - CLASS, OBJECT_PROPERTY, DATA_PROPERTY; + CLASS, OBJECT_PROPERTY, DATA_PROPERTY } private static final ParameterizedSparqlString CLASS_POPULARITY_QUERY = new ParameterizedSparqlString( @@ -142,26 +114,26 @@ public enum PopularityType { @ConfigOption(name = "useGenericSplitsCode", description = "Whether to use the generic facet generation code, which requires downloading all instances and is thus not recommended", defaultValue = "false") private boolean useGenericSplitsCode = false; + @ConfigOption(description = "Whether to use SPARQL1.1 Value Lists", defaultValue = "false") + private boolean useValueLists = false; + private QueryExecutionFactory qef; private SparqlEndpointKS ks; private ClassHierarchy hierarchy; - private Map entityPopularityMap = new HashMap(); - private Map classPopularityMap = new HashMap(); - private Map objectPropertyPopularityMap = new HashMap(); - private Map dataPropertyPopularityMap = new HashMap(); - private Map individualPopularityMap = new HashMap(); + private Map entityPopularityMap = new HashMap<>(); + private Map classPopularityMap = new HashMap<>(); private boolean batchedMode = true; - private Set precomputedPopularityTypes = new HashSet(); + private Set precomputedPopularityTypes = new HashSet<>(); private boolean prepared = false; private OWLClassExpressionToSPARQLConverter converter = new OWLClassExpressionToSPARQLConverter(); - private OWLDataFactory df = new OWLDataFactoryImpl(false, false); + private OWLDataFactory df = new OWLDataFactoryImpl(); private OWLObjectDuplicator duplicator = new OWLObjectDuplicator(df); - + /** * Default constructor for usage of config files + Spring API. */ @@ -194,11 +166,8 @@ public SPARQLReasoner(QueryExecutionFactory qef) { */ @Override public void init() throws ComponentInitException { - classPopularityMap = new HashMap(); - objectPropertyPopularityMap = new HashMap(); - dataPropertyPopularityMap = new HashMap(); - individualPopularityMap = new HashMap(); - + classPopularityMap = new HashMap<>(); + // this is only done if the reasoner is setup via config file if(qef == null) { if(ks == null) { @@ -212,6 +181,9 @@ public void init() throws ComponentInitException { : owl_file.getReasoning().getReasonerFactory().getURI())); ks = new LocalModelBasedSparqlEndpointKS(model, owl_file.getReasoning()); } + if (sources.size() > 1) { + throw new ComponentInitException("SPARQLReasoner only supports a single knowledge source"); + } } if(ks.isRemote()){ qef = ks.getQueryExecutionFactory(); @@ -301,7 +273,7 @@ public void precomputeObjectPropertyPopularity(){ if (qs.get("p").isURIResource()) { OWLObjectProperty property = df.getOWLObjectProperty(IRI.create(qs.getResource("p").getURI())); int cnt = qs.getLiteral("cnt").getInt(); - objectPropertyPopularityMap.put(property, cnt); + entityPopularityMap.put(property, cnt); } } } else { @@ -310,7 +282,7 @@ public void precomputeObjectPropertyPopularity(){ for (OWLObjectProperty property : properties) { ResultSet rs = executeSelectQuery(String.format(queryTemplate, property.toStringID())); int cnt = rs.next().getLiteral("cnt").getInt(); - objectPropertyPopularityMap.put(property, cnt); + entityPopularityMap.put(property, cnt); } } precomputedPopularityTypes.add(PopularityType.OBJECT_PROPERTY); @@ -336,7 +308,7 @@ public void precomputeDataPropertyPopularity(){ if (qs.get("p").isURIResource()) { OWLDataProperty property = df.getOWLDataProperty(IRI.create(qs.getResource("p").getURI())); int cnt = qs.getLiteral("cnt").getInt(); - dataPropertyPopularityMap.put(property, cnt); + entityPopularityMap.put(property, cnt); } } } else { @@ -345,7 +317,7 @@ public void precomputeDataPropertyPopularity(){ for (OWLDataProperty property : properties) { ResultSet rs = executeSelectQuery(String.format(queryTemplate, property.toStringID())); int cnt = rs.next().getLiteral("cnt").getInt(); - dataPropertyPopularityMap.put(property, cnt); + entityPopularityMap.put(property, cnt); } } precomputedPopularityTypes.add(PopularityType.DATA_PROPERTY); @@ -406,14 +378,18 @@ public int getObjectCountForProperty(OWLObjectProperty p){ return cnt; } - + + /** + * Computes the popularity of the given entity. + * @param entity the entity + * @param the OWL entity type + * @return the popularity + */ public int getPopularity(T entity){ - Integer popularity = null; - - if(useCache){ - popularity = entityPopularityMap.get(entity); - } - + // check if we have the value cached + Integer popularity = entityPopularityMap.get(entity); + + // compute the value if not cached if(popularity == null){ ParameterizedSparqlString queryTemplate; if(entity.isOWLClass()){ @@ -430,10 +406,11 @@ public int getPopularity(T entity){ ResultSet rs = executeSelectQuery(queryTemplate.toString()); popularity = rs.next().getLiteral("cnt").getInt(); - + + // put to cache entityPopularityMap.put(entity, popularity); } - return popularity.intValue(); + return popularity; } @@ -454,56 +431,7 @@ public ClassHierarchy prepareSubsumptionHierarchy() { if(precomputeClassHierarchy) { if(!prepared){ hierarchy = prepareSubsumptionHierarchyFast(); -// logger.info("Preparing class subsumption hierarchy ..."); -// long startTime = System.currentTimeMillis(); -// TreeMap> subsumptionHierarchyUp = new TreeMap>( -// ); -// TreeMap> subsumptionHierarchyDown = new TreeMap>( -// ); -// -// // parents/children of top ... -// SortedSet topClasses = getSubClassesImpl(df.getOWLThing()); -// subsumptionHierarchyUp.put(df.getOWLThing(), new TreeSet()); -// subsumptionHierarchyDown.put(df.getOWLThing(), topClasses); -// -// // ... bottom ... -// SortedSet leafClasses = getSuperClassesImpl(df.getOWLNothing()); -// subsumptionHierarchyUp.put(df.getOWLNothing(), leafClasses); -// subsumptionHierarchyDown.put(df.getOWLNothing(), new TreeSet()); -// -// // ... and named classes -// Set atomicConcepts; -// if(ks.isRemote()){ -// atomicConcepts = new SPARQLTasks(ks.getEndpoint()).getAllClasses(); -// } else { -// atomicConcepts = new TreeSet(); -// for(OntClass cls : ((LocalModelBasedSparqlEndpointKS)ks).getModel().listClasses().toList()){ -// if(!cls.isAnon()){ -// atomicConcepts.add(df.getOWLClass(IRI.create(cls.getURI()))); -// } -// } -// } -// -// SortedSet tmp; -// for (OWLClass cls : atomicConcepts) { -// tmp = getSubClassesImpl(cls); -// subsumptionHierarchyDown.put(cls, tmp); -// -// // put to super classes of owl:Nothing if class has no children -// if(tmp.isEmpty()) { -// leafClasses.add(cls); -// } -// -// tmp = getSuperClassesImpl(cls); -// subsumptionHierarchyUp.put(cls, tmp); -// -// // put to sub classes of owl:Thing if class has no parents -// if(tmp.isEmpty()) { -// topClasses.add(cls); -// } -// } -// logger.info("... done in {}ms", (System.currentTimeMillis()-startTime)); -// hierarchy = new ClassHierarchy(subsumptionHierarchyUp, subsumptionHierarchyDown); + prepared = true; } } else { @@ -555,15 +483,15 @@ public boolean isFunctional(OWLDataProperty property){ /** * Pre-computes the class hierarchy. Instead of executing queries for each class, * we query by the predicate rdfs:subClassOf. - * @return + * @return the class hierarchy */ public final ClassHierarchy prepareSubsumptionHierarchyFast() { logger.info("Preparing class subsumption hierarchy ..."); long startTime = System.currentTimeMillis(); - TreeMap> subsumptionHierarchyUp = new TreeMap>( - ); - TreeMap> subsumptionHierarchyDown = new TreeMap>( - ); + TreeMap> subsumptionHierarchyUp = new TreeMap<>( + ); + TreeMap> subsumptionHierarchyDown = new TreeMap<>( + ); String query = "SELECT * WHERE {" + "?sub a . " @@ -582,7 +510,7 @@ public final ClassHierarchy prepareSubsumptionHierarchyFast() { //add subclasses SortedSet subClasses = subsumptionHierarchyDown.get(sup); if (subClasses == null) { - subClasses = new TreeSet(); + subClasses = new TreeSet<>(); subsumptionHierarchyDown.put(sup, subClasses); } subClasses.add(sub); @@ -590,7 +518,7 @@ public final ClassHierarchy prepareSubsumptionHierarchyFast() { //add superclasses SortedSet superClasses = subsumptionHierarchyUp.get(sub); if (superClasses == null) { - superClasses = new TreeSet(); + superClasses = new TreeSet<>(); subsumptionHierarchyUp.put(sub, superClasses); } superClasses.add(sup); @@ -611,10 +539,10 @@ public ObjectPropertyHierarchy prepareObjectPropertyHierarchy() throws Reasoning // if(precomputeObjectPropertyHierarchy) { logger.info("Preparing object property subsumption hierarchy ..."); long startTime = System.currentTimeMillis(); - TreeMap> subsumptionHierarchyUp = new TreeMap>( - ); - TreeMap> subsumptionHierarchyDown = new TreeMap>( - ); + TreeMap> subsumptionHierarchyUp = new TreeMap<>( + ); + TreeMap> subsumptionHierarchyDown = new TreeMap<>( + ); String query = "SELECT * WHERE {" + "?sub a . " @@ -627,15 +555,15 @@ public ObjectPropertyHierarchy prepareObjectPropertyHierarchy() throws Reasoning + "}" + "}"; ResultSet rs = executeSelectQuery(query); - SortedSet properties = new TreeSet(); + while (rs.hasNext()) { QuerySolution qs = rs.next(); if (qs.get("sub").isURIResource()) { IRI iri = IRI.create(qs.get("sub").asResource().getURI()); + if(!iri.isReservedVocabulary()) { OWLObjectProperty sub = df.getOWLObjectProperty(iri); - properties.add(sub); - + // add sub properties entry if (!subsumptionHierarchyDown.containsKey(sub)) { subsumptionHierarchyDown.put(sub, new TreeSet()); @@ -649,8 +577,7 @@ public ObjectPropertyHierarchy prepareObjectPropertyHierarchy() throws Reasoning // if there is a super property if(qs.get("sup") != null && qs.get("sup").isURIResource()){ OWLObjectProperty sup = df.getOWLObjectProperty(IRI.create(qs.get("sup").asResource().getURI())); - properties.add(sup); - + // add sub properties entry if (!subsumptionHierarchyDown.containsKey(sup)) { subsumptionHierarchyDown.put(sup, new TreeSet()); @@ -664,7 +591,7 @@ public ObjectPropertyHierarchy prepareObjectPropertyHierarchy() throws Reasoning // add super properties entry SortedSet superClasses = subsumptionHierarchyUp.get(sub); if (superClasses == null) { - superClasses = new TreeSet(); + superClasses = new TreeSet<>(); subsumptionHierarchyUp.put(sub, superClasses); } superClasses.add(sup); @@ -672,7 +599,7 @@ public ObjectPropertyHierarchy prepareObjectPropertyHierarchy() throws Reasoning // add sub properties entry SortedSet subProperties = subsumptionHierarchyDown.get(sup); if (subProperties == null) { - subProperties = new TreeSet(); + subProperties = new TreeSet<>(); subsumptionHierarchyDown.put(sup, subProperties); } subProperties.add(sub); @@ -693,10 +620,10 @@ public ObjectPropertyHierarchy prepareObjectPropertyHierarchy() throws Reasoning public DatatypePropertyHierarchy prepareDatatypePropertyHierarchy() throws ReasoningMethodUnsupportedException { logger.info("Preparing data property subsumption hierarchy ..."); long startTime = System.currentTimeMillis(); - TreeMap> subsumptionHierarchyUp = new TreeMap>( - ); - TreeMap> subsumptionHierarchyDown = new TreeMap>( - ); + TreeMap> subsumptionHierarchyUp = new TreeMap<>( + ); + TreeMap> subsumptionHierarchyDown = new TreeMap<>( + ); String query = "SELECT * WHERE {" + "?sub a . " @@ -708,13 +635,12 @@ public DatatypePropertyHierarchy prepareDatatypePropertyHierarchy() throws Reaso + "FILTER(?sub != && ?sub != )" + "}"; ResultSet rs = executeSelectQuery(query); - SortedSet properties = new TreeSet(); + while (rs.hasNext()) { QuerySolution qs = rs.next(); if (qs.get("sub").isURIResource()) { OWLDataProperty sub = df.getOWLDataProperty(IRI.create(qs.get("sub").asResource().getURI())); - properties.add(sub); - + // add sub properties entry if (!subsumptionHierarchyDown.containsKey(sub)) { subsumptionHierarchyDown.put(sub, new TreeSet()); @@ -728,8 +654,7 @@ public DatatypePropertyHierarchy prepareDatatypePropertyHierarchy() throws Reaso // if there is a super property if(qs.get("sup") != null && qs.get("sup").isURIResource()){ OWLDataProperty sup = df.getOWLDataProperty(IRI.create(qs.get("sup").asResource().getURI())); - properties.add(sup); - + // add sub properties entry if (!subsumptionHierarchyDown.containsKey(sup)) { subsumptionHierarchyDown.put(sup, new TreeSet()); @@ -743,7 +668,7 @@ public DatatypePropertyHierarchy prepareDatatypePropertyHierarchy() throws Reaso // add super properties entry SortedSet superClasses = subsumptionHierarchyUp.get(sub); if (superClasses == null) { - superClasses = new TreeSet(); + superClasses = new TreeSet<>(); subsumptionHierarchyUp.put(sub, superClasses); } superClasses.add(sup); @@ -751,7 +676,7 @@ public DatatypePropertyHierarchy prepareDatatypePropertyHierarchy() throws Reaso // add sub properties entry SortedSet subProperties = subsumptionHierarchyDown.get(sup); if (subProperties == null) { - subProperties = new TreeSet(); + subProperties = new TreeSet<>(); subsumptionHierarchyDown.put(sup, subProperties); } subProperties.add(sub); @@ -772,8 +697,8 @@ public Model loadSchema(String namespace){ Model model = ModelFactory.createDefaultModel(); //load class hierarchy - String query = String.format("CONSTRUCT {?s ?o} WHERE " + - "{?s ?o." + (namespace != null ? "FILTER(REGEX(STR(?s), '^" + namespace + "'))}" : "")); + String query = "CONSTRUCT {?s ?o} WHERE " + + "{?s ?o." + (namespace != null ? "FILTER(REGEX(STR(?s), '^" + namespace + "'))}" : ""); model.add(loadIncrementally(query)); query = "CONSTRUCT {?s ?o} WHERE {?s ?o}"; model.add(loadIncrementally(query)); @@ -816,7 +741,7 @@ public Model loadSchema(String namespace){ query = "CONSTRUCT {?s ?o} WHERE {?s ?o}"; model.add(loadIncrementally(query)); //load property characteristics - Set propertyCharacteristics = new HashSet(); + Set propertyCharacteristics = new HashSet<>(); // propertyCharacteristics.add(OWL.FunctionalProperty); propertyCharacteristics.add(OWL.InverseFunctionalProperty); propertyCharacteristics.add(OWL.SymmetricProperty); @@ -844,7 +769,7 @@ public Model loadSchema(String namespace){ /** * Gets all logical axioms according to entities of type owl:Class, owl:ObjectProperty and owl:DatatypeProperty. - * @return + * @return the OWL schema as Jena model */ public Model loadOWLSchema(){ Model schema = ModelFactory.createDefaultModel(); @@ -923,16 +848,21 @@ public Set getTypes(OWLIndividual individual, String namespace) { } public Set getMostSpecificTypes(OWLIndividual individual) { - Set types = new HashSet(); + Set types = new HashSet<>(); String query = String.format( - "SELECT ?type WHERE {<%s> a ?type . " + "SELECT ?type WHERE {<%s> a ?type ." + "FILTER NOT EXISTS{<%s> a ?moreSpecificType ." - + "?moreSpecificType ?type.}}", individual.toStringID(), individual.toStringID()); + + "?moreSpecificType ?type ." + + "FILTER((?type != ?moreSpecificType) && (?moreSpecificType!= ))}}", + individual.toStringID(), individual.toStringID()); ResultSet rs = executeSelectQuery(query); - QuerySolution qs; while(rs.hasNext()){ - qs = rs.next(); - types.add(df.getOWLClass(IRI.create(qs.getResource("type").getURI()))); + QuerySolution qs = rs.next(); + IRI iri = IRI.create(qs.getResource("type").getURI()); + if(!iri.isReservedVocabulary()) { + types.add(df.getOWLClass(iri)); + } + } return types; } @@ -941,14 +871,14 @@ public Set getMostSpecificTypes(OWLIndividual individual) { /** * Returns the entity type of the given resource IRI, i.e. whether the resource * is a owl:Class, owl:ObjectProperty,owl:DatatypeProperty or owl:NamedIndividual. - * @param iri - * @return + * @param iri the IRI of the entity + * @return the entity type */ public EntityType getOWLEntityType(String iri) { ParameterizedSparqlString query = new ParameterizedSparqlString("SELECT ?type WHERE {?s a ?type .}"); query.setIri("s", iri); ResultSet rs = executeSelectQuery(query.toString()); - Set> entityTypes = new HashSet>(); + Set> entityTypes = new HashSet<>(); while(rs.hasNext()){ QuerySolution qs = rs.next(); String uri = qs.getResource("type").getURI(); @@ -966,13 +896,19 @@ public EntityType getOWLEntityType(String iri) { } public Set getTypes(String namespace) { - Set types = new TreeSet(); - String query = String.format("SELECT DISTINCT ?class WHERE {[] a ?class." + (namespace != null ? ("FILTER(REGEX(?class,'^" + namespace + "'))") : "") + "}"); + return getTypes(namespace, false); + } + + public Set getTypes(String namespace, boolean omitEmptyTypes) { + Set types = new TreeSet<>(); + String query = "SELECT DISTINCT ?cls WHERE {[] a ?cls ." + + (omitEmptyTypes ? "[] a ?cls ." : "" ) + + (namespace != null ? ("FILTER(REGEX(?cls,'^" + namespace + "'))") : "") + "}"; ResultSet rs = executeSelectQuery(query); QuerySolution qs; while(rs.hasNext()){ qs = rs.next(); - types.add(df.getOWLClass(IRI.create(qs.getResource("class").getURI()))); + types.add(df.getOWLClass(IRI.create(qs.getResource("cls").getURI()))); } return types; } @@ -1023,7 +959,7 @@ public SortedSet getOWLIndividuals() { } else { rs = executeSelectQuery(SPARQLQueryUtils.SELECT_INDIVIDUALS_QUERY_ALT); } - SortedSet individuals = new TreeSet(); + SortedSet individuals = new TreeSet<>(); individuals.addAll(asOWLEntities(EntityType.NAMED_INDIVIDUAL, rs, "var1")); return individuals; } @@ -1067,20 +1003,29 @@ public SortedSet getOWLDataProperties(String namespace) { } public Set getDataPropertiesByRange(XSDVocabulary xsdType) { - String query = String.format(SPARQLQueryUtils.SELECT_DATA_PROPERTIES_BY_RANGE_QUERY, xsdType.getIRI().toString()); + return getDataPropertiesByRange(xsdType.getIRI()); + } + public Set getDataPropertiesByRange(Set dts) { + Set r = new TreeSet<>(); + for (OWLDatatype dt:dts) { + r.addAll(getDataPropertiesByRange(dt.getIRI())); + } + return r; + } + public Set getDataPropertiesByRange(IRI iri) { + String query = String.format(SPARQLQueryUtils.SELECT_DATA_PROPERTIES_BY_RANGE_QUERY, iri.toString()); logger.debug(sparql_debug, "get properties by range query: " + query); ResultSet rs = executeSelectQuery(query); SortedSet properties = asOWLEntities(EntityType.DATA_PROPERTY, rs, "var1"); return properties; } - /* (non-Javadoc) * @see org.dllearner.core.AbstractReasonerComponent#getIntDatatypePropertiesImpl() */ @Override protected Set getIntDatatypePropertiesImpl() throws ReasoningMethodUnsupportedException { - return getDataPropertiesByRange(XSDVocabulary.INT); + return getDataPropertiesByRange(OWLAPIUtils.intDatatypes); } /* (non-Javadoc) @@ -1088,7 +1033,7 @@ protected Set getIntDatatypePropertiesImpl() throws ReasoningMe */ @Override protected Set getDoubleDatatypePropertiesImpl() throws ReasoningMethodUnsupportedException { - return getDataPropertiesByRange(XSDVocabulary.DOUBLE); + return getDataPropertiesByRange(OWLAPIUtils.floatDatatypes); } /* (non-Javadoc) @@ -1096,7 +1041,7 @@ protected Set getDoubleDatatypePropertiesImpl() throws Reasonin */ @Override protected Set getBooleanDatatypePropertiesImpl() throws ReasoningMethodUnsupportedException { - return getDataPropertiesByRange(XSDVocabulary.BOOLEAN); + return getDataPropertiesByRange(OWLAPIUtils.fixedDatatypes); } /* (non-Javadoc) @@ -1108,7 +1053,7 @@ protected Set getStringDatatypePropertiesImpl() throws Reasonin } public Set getProperties(boolean inferType, String namespace) { - Set properties = new HashSet(); + Set properties = new HashSet<>(); String query = "SELECT DISTINCT ?p ?type WHERE {?s ?p ?o." + (namespace != null ? ("FILTER(REGEX(?p,'^" + namespace + "'))") : "") + "OPTIONAL{?p a ?type.}}"; @@ -1131,7 +1076,7 @@ public Set getProperties(boolean inferType, String namespace) { properties.add(df.getOWLObjectProperty(IRI.create(uri))); } else if(!types.contains(OWL.ObjectProperty.getURI()) && types.contains(OWL.DatatypeProperty.getURI())){ properties.add(df.getOWLDataProperty(IRI.create(uri))); - } else { + } else if(inferType){ //infer the type by values query = "SELECT ?o WHERE {?s <" + uri + "> ?o. } LIMIT 100"; rs = executeSelectQuery(query); @@ -1156,7 +1101,7 @@ public Set getProperties(boolean inferType, String namespace) { } public Set getProperties(boolean inferType) { - Set properties = new TreeSet(); + Set properties = new TreeSet<>(); String query = "SELECT DISTINCT ?p ?type WHERE {?s ?p ?o. OPTIONAL{?p a ?type.}}"; ResultSet rs = executeSelectQuery(query); Multimap uri2Types = HashMultimap.create(); @@ -1177,7 +1122,7 @@ public Set getProperties(boolean inferType) { properties.add(df.getOWLObjectProperty(IRI.create(uri))); } else if(!types.contains(OWL.ObjectProperty.getURI()) && types.contains(OWL.DatatypeProperty.getURI())){ properties.add(df.getOWLDataProperty(IRI.create(uri))); - } else { + } else if(inferType) { //infer the type by values query = "SELECT ?o WHERE {?s <" + uri + "> ?o. } LIMIT 100"; rs = executeSelectQuery(query); @@ -1204,9 +1149,8 @@ public Set getProperties(boolean inferType) { /** * Returns a set of sibling classes, i.e. classes that are on the same level * in the class hierarchy. - * @param cls - * @param limit - * @return + * @param cls the OWL class + * @return the sibling classes */ public Set getSiblingClasses(OWLClass cls) { String query = SPARQLQueryUtils.SELECT_SIBLING_CLASSES_QUERY.replace("%s", cls.toStringID()); @@ -1227,21 +1171,19 @@ public boolean hasTypeImpl(OWLClassExpression description, OWLIndividual individ return result; } else { // complex class expressions //TODO use ASK queries - SortedSet individuals = getIndividuals(description); + SortedSet individuals = getIndividuals(description, Collections.singleton(individual)); return individuals.contains(individual); // String queryBody = converter.convert("?ind", description); // queryBody = queryBody.replace("?ind", "<" + individual.toStringID() + ">"); // String query = "ASK {" + queryBody + "}"; // // FIXME universal and cardinality restrictions do not work with ASK queries -// boolean result = executeAskQuery(query); -// return result; } } @Override public SortedSet hasTypeImpl(OWLClassExpression description, Set individuals) { - SortedSet allIndividuals = getIndividuals(description); - allIndividuals.retainAll(individuals); + SortedSet allIndividuals = getIndividuals(description, individuals); + //allIndividuals.retainAll(individuals); return allIndividuals; } @@ -1250,17 +1192,33 @@ public SortedSet getIndividualsImpl(OWLClassExpression descriptio return getIndividuals(description, 0); } - public SortedSet getIndividuals(OWLClassExpression description, int limit) { + public SortedSet getIndividuals(OWLClassExpression description, int limit, Set indValues) { // we need to copy it to get something like A AND B from A AND A AND B description = duplicator.duplicateObject(description); - SortedSet individuals = new TreeSet(); - String query = converter.asQuery("?ind", description, false).toString();//System.out.println(query); + SortedSet individuals = new TreeSet<>(); + String query; + + if (indValues != null) { + String tp = converter.convert("?ind", description); + query = "SELECT DISTINCT ?ind WHERE { \n" + + "VALUES ?ind { \n"; + for (OWLIndividual x:indValues) { + query += "<" + x.toStringID() + "> "; + } + query += "}. \n " + tp + "\n}"; + //query = converter.asQuery("?ind", description).toString(); + //System.exit(1); // XXX + } else { + query = converter.asQuery("?ind", description, false).toString();//System.out.println(query); + } if(limit != 0) { query += " LIMIT " + limit; } // query = String.format(SPARQLQueryUtils.PREFIXES + " SELECT ?ind WHERE {?ind rdf:type/rdfs:subClassOf* <%s> .}", description.asOWLClass().toStringID()); + if(logger.isDebugEnabled()){Thread.dumpStack(); logger.debug(sparql_debug, "get individuals query: " + query); + } ResultSet rs = executeSelectQuery(query); while(rs.hasNext()){ QuerySolution qs = rs.next(); @@ -1271,86 +1229,76 @@ public SortedSet getIndividuals(OWLClassExpression description, i logger.debug(sparql_debug, "get individuals result: " + individuals); return individuals; } + + public SortedSet getIndividuals(OWLClassExpression description, int limit) { + return getIndividuals(description, limit, null); + } + + public SortedSet getIndividuals(OWLClassExpression description, Set indValues) { + return getIndividuals(description, 0, indValues); + } - /** - * @param wantedClass - * @param excludeClass - * @param limit - * @return get individual of class wantedClass excluding all individual of type excludeClass - * @author sherif - */ - public SortedSet getIndividualsExcluding(OWLClassExpression wantedClass, OWLClassExpression excludeClass, int limit) { - if(wantedClass.isAnonymous()){ - throw new UnsupportedOperationException("Only named classes are supported."); - } - SortedSet individuals = new TreeSet(); - String query = - "SELECT DISTINCT ?ind WHERE {" + - "?ind a <"+((OWLClass)wantedClass).toStringID() + "> . " + - "FILTER NOT EXISTS { ?ind a <" + ((OWLClass)excludeClass).toStringID() + "> } }"; - if(limit != 0) { - query += " LIMIT " + limit; - } - ResultSet rs = executeSelectQuery(query); - QuerySolution qs; - while(rs.hasNext()){ - qs = rs.next(); - if(qs.get("ind").isURIResource()){ - individuals.add(df.getOWLNamedIndividual(IRI.create(qs.getResource("ind").getURI()))); + public int getIndividualsCount(OWLClassExpression description, int limit, Set indValues) { + description = duplicator.duplicateObject(description); + + String query; + + if (indValues != null) { + String tp = converter.convert("?ind", description); + query = "SELECT (COUNT(DISTINCT ?ind) as ?cnt) WHERE { \n" + + "VALUES ?ind { \n"; + for (OWLIndividual x:indValues) { + query += "<" + x.toStringID() + "> "; } + query += "}. \n " + tp + "\n}"; + } else { + query = converter.asQuery("?ind", description, true).toString(); + System.err.println(query); + System.exit(1); } - return individuals; - } - - /** - * @param cls - * @param limit - * @return Random Individuals not including any of the input class individuals - * @author sherif - */ - public SortedSet getRandomIndividuals(OWLClass cls, int limit) { - SortedSet individuals = new TreeSet(); - String query = - " SELECT DISTINCT ?ind WHERE {"+ - "?ind ?p ?o ."+ - "FILTER(NOT EXISTS { ?ind a <" + cls.toStringID() + "> } ) }"; if(limit != 0) { query += " LIMIT " + limit; } + if(logger.isDebugEnabled()){ + logger.debug(sparql_debug, "get individuals query: " + query); + } ResultSet rs = executeSelectQuery(query); - QuerySolution qs; while(rs.hasNext()){ - qs = rs.next(); - if(qs.get("ind").isURIResource()){ - individuals.add(df.getOWLNamedIndividual(IRI.create(qs.getResource("ind").getURI()))); + QuerySolution qs = rs.next(); + if(qs.get("cnt").isLiteral()){ + int ret = qs.get("cnt").asLiteral().getInt();logger.debug(sparql_debug, "result: "+ret); + return ret; } } - return individuals; + throw new Error("no result"); + } + + public int getIndividualsCount(OWLClassExpression description, int limit) { + return getIndividualsCount(description, limit, null); } + public int getIndividualsCount(OWLClassExpression description, Set indValues) { + return getIndividualsCount(description, 0, indValues); + } + /** - * @param cls - * @param limit - * @return Random Individuals not including any of the input classes individuals - * @author sherif + * @param wantedClass the class to which the individuals must belong to + * @param excludeClass the class to which the individuals must not belong to + * @param limit the max. number of individuals + * @return get individuals of class wantedClass excluding all individuals of type excludeClass */ - public SortedSet getRandomIndividuals(Set cls, int limit) { - SortedSet individuals = new TreeSet(); - - String filterStr=""; - for(OWLClass nc : cls){ - filterStr = filterStr.concat("FILTER(NOT EXISTS { ?ind a <").concat(nc.toStringID()).concat("> } ) "); + public SortedSet getIndividualsExcluding(OWLClassExpression wantedClass, OWLClassExpression excludeClass, int limit) { + if(wantedClass.isAnonymous()){ + throw new UnsupportedOperationException("Only named classes are supported."); } - + SortedSet individuals = new TreeSet<>(); String query = - " SELECT DISTINCT ?ind WHERE {"+ - "?ind a ?o .?o "+ - filterStr+ " }"; + "SELECT DISTINCT ?ind WHERE {" + + "?ind a <"+((OWLClass)wantedClass).toStringID() + "> . " + + "FILTER NOT EXISTS { ?ind a <" + ((OWLClass)excludeClass).toStringID() + "> } }"; if(limit != 0) { query += " LIMIT " + limit; } - - System.out.println("!!!!!!!!!!!!!!!!!!!! "+query); ResultSet rs = executeSelectQuery(query); QuerySolution qs; while(rs.hasNext()){ @@ -1361,43 +1309,103 @@ public SortedSet getRandomIndividuals(Set cls, int limi } return individuals; } - - /** - * @param cls - * @param limit - * @return Super class of the input class Individuals not including any of the input class individuals - * @author sherif - */ - public SortedSet getSuperClassIndividuals(OWLClass cls, int limit) { - SortedSet individuals = new TreeSet(); - Set superClasses = getSuperClasses(cls); - - for(OWLClassExpression sup : superClasses){ - if(!sup.isAnonymous()) { - String query = "SELECT DISTINCT ?ind WHERE { " - + "?ind a <" + sup.asOWLClass().toStringID() + "> . " - + "FILTER NOT EXISTS { ?ind a <" + cls.toStringID() + "> } }"; - if(limit != 0) { - query += " LIMIT " + limit/superClasses.size(); - } - - System.out.println("---------------------------------------------- "+query); - - ResultSet rs = executeSelectQuery(query); - QuerySolution qs; - while(rs.hasNext()){ - qs = rs.next(); - if(qs.get("ind").isURIResource()){ - individuals.add(df.getOWLNamedIndividual(IRI.create(qs.getResource("ind").getURI()))); - } - } - System.out.println(individuals.size()); - System.out.println(individuals); - } - } - - return individuals; - } + +// /** +// * @param cls +// * @param limit +// * @return Random Individuals not including any of the input class individuals +// * @author sherif +// */ +// public SortedSet getRandomIndividuals(OWLClass cls, int limit) { +// SortedSet individuals = new TreeSet(); +// String query = +// " SELECT DISTINCT ?ind WHERE {"+ +// "?ind ?p ?o ."+ +// "FILTER(NOT EXISTS { ?ind a <" + cls.toStringID() + "> } ) }"; +// if(limit != 0) { +// query += " LIMIT " + limit; +// } +// ResultSet rs = executeSelectQuery(query); +// QuerySolution qs; +// while(rs.hasNext()){ +// qs = rs.next(); +// if(qs.get("ind").isURIResource()){ +// individuals.add(df.getOWLNamedIndividual(IRI.create(qs.getResource("ind").getURI()))); +// } +// } +// return individuals; +// } + +// /** +// * @param cls +// * @param limit +// * @return Random Individuals not including any of the input classes individuals +// * @author sherif +// */ +// public SortedSet getRandomIndividuals(Set cls, int limit) { +// SortedSet individuals = new TreeSet(); +// +// String filterStr=""; +// for(OWLClass nc : cls){ +// filterStr = filterStr.concat("FILTER(NOT EXISTS { ?ind a <").concat(nc.toStringID()).concat("> } ) "); +// } +// +// String query = +// " SELECT DISTINCT ?ind WHERE {"+ +// "?ind a ?o .?o "+ +// filterStr+ " }"; +// if(limit != 0) { +// query += " LIMIT " + limit; +// } +// +// System.out.println("!!!!!!!!!!!!!!!!!!!! "+query); +// ResultSet rs = executeSelectQuery(query); +// QuerySolution qs; +// while(rs.hasNext()){ +// qs = rs.next(); +// if(qs.get("ind").isURIResource()){ +// individuals.add(df.getOWLNamedIndividual(IRI.create(qs.getResource("ind").getURI()))); +// } +// } +// return individuals; +// } + +// /** +// * @param cls +// * @param limit +// * @return Super class of the input class Individuals not including any of the input class individuals +// * @author sherif +// */ +// public SortedSet getSuperClassIndividuals(OWLClass cls, int limit) { +// SortedSet individuals = new TreeSet(); +// Set superClasses = getSuperClasses(cls); +// +// for(OWLClassExpression sup : superClasses){ +// if(!sup.isAnonymous()) { +// String query = "SELECT DISTINCT ?ind WHERE { " +// + "?ind a <" + sup.asOWLClass().toStringID() + "> . " +// + "FILTER NOT EXISTS { ?ind a <" + cls.toStringID() + "> } }"; +// if(limit != 0) { +// query += " LIMIT " + limit/superClasses.size(); +// } +// +// System.out.println("---------------------------------------------- "+query); +// +// ResultSet rs = executeSelectQuery(query); +// QuerySolution qs; +// while(rs.hasNext()){ +// qs = rs.next(); +// if(qs.get("ind").isURIResource()){ +// individuals.add(df.getOWLNamedIndividual(IRI.create(qs.getResource("ind").getURI()))); +// } +// } +// System.out.println(individuals.size()); +// System.out.println(individuals); +// } +// } +// +// return individuals; +// } @Override public SortedSetTuple doubleRetrievalImpl(OWLClassExpression description) { @@ -1406,7 +1414,7 @@ public SortedSetTuple doubleRetrievalImpl(OWLClassExpression desc @Override public Set getRelatedIndividualsImpl(OWLIndividual individual, OWLObjectProperty objectProperty) { - Set individuals = new HashSet(); + Set individuals = new HashSet<>(); String query = String.format("SELECT ?ind WHERE {<%s> <%s> ?ind, FILTER(isIRI(?ind))}", individual.toStringID(), objectProperty.toStringID()); ResultSet rs = executeSelectQuery(query); @@ -1426,7 +1434,7 @@ public Set getRelatedValuesImpl(OWLIndividual individual, OWLDataPro @Override public Map> getObjectPropertyRelationshipsImpl(OWLIndividual individual) { - Map> prop2individuals = new HashMap>(); + Map> prop2individuals = new HashMap<>(); String query = String.format("SELECT ?prop ?ind WHERE {" + "<%s> ?prop ?ind." + " FILTER(isIRI(?ind) && ?prop != <%s> && ?prop != <%s>)}", @@ -1443,7 +1451,7 @@ public Map> getObjectPropertyRelationships property = df.getOWLObjectProperty(IRI.create(qs.getResource("prop").getURI())); individuals = prop2individuals.get(property); if(individuals == null){ - individuals = new HashSet(); + individuals = new HashSet<>(); prop2individuals.put(property, individuals); } individuals.add(ind); @@ -1454,7 +1462,7 @@ public Map> getObjectPropertyRelationships @Override public Map> getPropertyMembersImpl(OWLObjectProperty objectProperty) { - Map> subject2objects = new HashMap>(); + Map> subject2objects = new HashMap<>(); String query = String.format("SELECT ?s ?o WHERE {" + "?s <%s> ?o." + " FILTER(isIRI(?o))}", @@ -1471,7 +1479,7 @@ public Map> getPropertyMembersImpl(OWLOb obj = df.getOWLNamedIndividual(IRI.create(qs.getResource("o").getURI())); objects = subject2objects.get(sub); if(objects == null){ - objects = new TreeSet(); + objects = new TreeSet<>(); subject2objects.put(sub, objects); } objects.add(obj); @@ -1482,7 +1490,7 @@ public Map> getPropertyMembersImpl(OWLOb @Override public Map> getDatatypeMembersImpl(OWLDataProperty dataProperty) { - Map> subject2objects = new HashMap>(); + Map> subject2objects = new HashMap<>(); String query = String.format(SPARQLQueryUtils.SELECT_PROPERTY_RELATIONSHIPS_QUERY, dataProperty.toStringID()); @@ -1494,21 +1502,31 @@ public Map> getDatatypeMembersImpl(OWLDataP OWLLiteral obj = OwlApiJenaUtils.getOWLLiteral(qs.getLiteral("var2")); SortedSet objects = subject2objects.get(sub); if(objects == null){ - objects = new TreeSet(); + objects = new TreeSet<>(); subject2objects.put(sub, objects); } objects.add(obj); } return subject2objects; } + + private String datatypeSparqlFilter(Iterable dts) { + return Joiner.on(" || ").join( + Iterables.transform(dts, new Function(){ + @Override + public String apply(OWLDatatype input) { + return "DATATYPE(?o) = <" + input.toStringID() + ">"; + }} + ) + ); + } @Override public Map> getDoubleDatatypeMembersImpl(OWLDataProperty datatypeProperty) { - Map> subject2objects = new HashMap>(); - String query = String.format("SELECT ?s ?o WHERE {" + - "?s <%s> ?o." + - " FILTER(DATATYPE(?o) = <%s>)}", - datatypeProperty.toStringID(), XSD.DOUBLE.toStringID()); + Map> subject2objects = new HashMap<>(); + String query = "SELECT ?s ?o WHERE {" + + String.format("?s <%s> ?o.", datatypeProperty.toStringID()) + + " FILTER(" + datatypeSparqlFilter(OWLAPIUtils.floatDatatypes) + ")}"; ResultSet rs = executeSelectQuery(query); QuerySolution qs; @@ -1528,7 +1546,7 @@ public Map> getDoubleDatatypeMembersImpl(OWLDat //obj = qs.getLiteral("o").getDouble(); objects = subject2objects.get(sub); if(objects == null){ - objects = new TreeSet(); + objects = new TreeSet<>(); subject2objects.put(sub, objects); } objects.add(obj); @@ -1539,11 +1557,10 @@ public Map> getDoubleDatatypeMembersImpl(OWLDat @Override public Map> getIntDatatypeMembersImpl(OWLDataProperty datatypeProperty) { - Map> subject2objects = new HashMap>(); - String query = String.format("SELECT ?s ?o WHERE {" + - "?s <%s> ?o." + - " FILTER(DATATYPE(?o) = <%s>)}", - datatypeProperty.toStringID(), XSD.INT.toStringID()); + Map> subject2objects = new HashMap<>(); + String query = "SELECT ?s ?o WHERE {" + + String.format("?s <%s> ?o.", datatypeProperty.toStringID()) + + " FILTER(" + datatypeSparqlFilter(OWLAPIUtils.intDatatypes) + ")}"; ResultSet rs = executeSelectQuery(query); QuerySolution qs; @@ -1556,7 +1573,7 @@ public Map> getIntDatatypeMembersImpl(OWLDataP obj = qs.getLiteral("o").getInt(); objects = subject2objects.get(sub); if(objects == null){ - objects = new TreeSet(); + objects = new TreeSet<>(); subject2objects.put(sub, objects); } objects.add(obj); @@ -1567,11 +1584,10 @@ public Map> getIntDatatypeMembersImpl(OWLDataP @Override public Map> getBooleanDatatypeMembersImpl(OWLDataProperty datatypeProperty) { - Map> subject2objects = new HashMap>(); - String query = String.format("SELECT ?s ?o WHERE {" + - "?s <%s> ?o." + - " FILTER(DATATYPE(?o) = <%s>)}", - datatypeProperty.toStringID(), XSD.BOOLEAN.toStringID()); + Map> subject2objects = new HashMap<>(); + String query = "SELECT ?s ?o WHERE {" + + String.format("?s <%s> ?o.", datatypeProperty.toStringID()) + + " FILTER(" + datatypeSparqlFilter(OWLAPIUtils.fixedDatatypes) + ")}"; ResultSet rs = executeSelectQuery(query); QuerySolution qs; @@ -1584,7 +1600,7 @@ public Map> getBooleanDatatypeMembersImpl(OWLD obj = qs.getLiteral("o").getBoolean(); objects = subject2objects.get(sub); if(objects == null){ - objects = new TreeSet(); + objects = new TreeSet<>(); subject2objects.put(sub, objects); } objects.add(obj); @@ -1595,7 +1611,7 @@ public Map> getBooleanDatatypeMembersImpl(OWLD @Override public SortedSet getTrueDatatypeMembersImpl(OWLDataProperty datatypeProperty) { - SortedSet members = new TreeSet(); + SortedSet members = new TreeSet<>(); String query = String.format("SELECT ?ind WHERE {" + "?ind <%s> ?o." + " FILTER(isLiteral(?o) && DATATYPE(?o) = <%s> && ?o = %s)}", @@ -1614,7 +1630,7 @@ public SortedSet getTrueDatatypeMembersImpl(OWLDataProperty datat @Override public SortedSet getFalseDatatypeMembersImpl(OWLDataProperty datatypeProperty) { - SortedSet members = new TreeSet(); + SortedSet members = new TreeSet<>(); String query = String.format("SELECT ?ind WHERE {" + "?ind <%s> ?o." + " FILTER(isLiteral(?o) && DATATYPE(?o) = <%s> && ?o = %s)}", @@ -1651,7 +1667,7 @@ public OWLClassExpression getDomainImpl(OWLObjectProperty objectProperty) { ResultSet rs = executeSelectQuery(query); QuerySolution qs; - SortedSet domains = new TreeSet(); + SortedSet domains = new TreeSet<>(); while(rs.hasNext()){ qs = rs.next(); domains.add(df.getOWLClass(IRI.create(qs.getResource("domain").getURI()))); @@ -1703,7 +1719,7 @@ public SortedSet getDomains(OWLObjectProperty objectProperty) { ResultSet rs = executeSelectQuery(query); QuerySolution qs; - SortedSet domains = new TreeSet(); + SortedSet domains = new TreeSet<>(); while(rs.hasNext()){ qs = rs.next(); domains.add(df.getOWLClass(IRI.create(qs.getResource("domain").getURI()))); @@ -1721,7 +1737,7 @@ public OWLClassExpression getDomainImpl(OWLDataProperty datatypeProperty) { ResultSet rs = executeSelectQuery(query); QuerySolution qs; - SortedSet domains = new TreeSet(); + SortedSet domains = new TreeSet<>(); while(rs.hasNext()){ qs = rs.next(); domains.add(df.getOWLClass(IRI.create(qs.getResource("domain").getURI()))); @@ -1744,7 +1760,7 @@ public OWLClassExpression getRangeImpl(OWLObjectProperty objectProperty) { ResultSet rs = executeSelectQuery(query); QuerySolution qs; - SortedSet domains = new TreeSet(); + SortedSet domains = new TreeSet<>(); while(rs.hasNext()){ qs = rs.next(); domains.add(df.getOWLClass(IRI.create(qs.getResource("range").getURI()))); @@ -1766,7 +1782,7 @@ public SortedSet getRanges(OWLObjectProperty objectProperty) { ResultSet rs = executeSelectQuery(query); QuerySolution qs; - SortedSet ranges = new TreeSet(); + SortedSet ranges = new TreeSet<>(); while(rs.hasNext()){ qs = rs.next(); ranges.add(df.getOWLClass(IRI.create(qs.getResource("range").getURI()))); @@ -1825,7 +1841,7 @@ public int getPropertyCount(OWLObjectProperty property){ } public SortedSet getInverseObjectProperties(OWLObjectProperty property){ - SortedSet inverseObjectProperties = new TreeSet(); + SortedSet inverseObjectProperties = new TreeSet<>(); String query = "SELECT ?p WHERE {" + "{<%p> <%ax> ?p.} UNION {?p <%ax> <%p>}}".replace("%p", property.toStringID()).replace("%ax", OWL.inverseOf.getURI()); ResultSet rs = executeSelectQuery(query); @@ -1930,12 +1946,9 @@ public SortedSet getSuperClassesImpl(OWLClassExpression desc if(description.isAnonymous()){ throw new IllegalArgumentException("Only named classes are supported."); } else if(description.isOWLThing()) { - return ImmutableSortedSet.of(); + return Sets.newTreeSet(); } else if(description.isOWLNothing()) { - query = String.format( - SPARQLQueryUtils.SELECT_LEAF_CLASSES_OWL, - description.asOWLClass().toStringID() - ); + query = SPARQLQueryUtils.SELECT_LEAF_CLASSES_OWL; } else { query = String.format( SPARQLQueryUtils.SELECT_DIRECT_SUPERCLASS_OF_QUERY, @@ -1955,20 +1968,20 @@ public SortedSet getSuperClasses(OWLClassExpression descript if(description.isAnonymous()){ throw new IllegalArgumentException("Only named classes are supported."); } - SortedSet superClasses = new TreeSet(); + SortedSet superClasses = new TreeSet<>(); String query; if(direct){ query = String.format("SELECT ?sup {<%s> ?sup. FILTER(isIRI(?sup))}", description.asOWLClass().toStringID()); } else { - query = String.format("SELECT ?sub {<%s> * ?sup. FILTER(isIRI(?sup))}", + query = String.format("SELECT ?sup {<%s> * ?sup. FILTER(isIRI(?sup))}", description.asOWLClass().toStringID()); } ResultSet rs = executeSelectQuery(query); QuerySolution qs; while(rs.hasNext()){ qs = rs.next(); - superClasses.add(df.getOWLClass(IRI.create(qs.getResource("sub").getURI()))); + superClasses.add(df.getOWLClass(IRI.create(qs.getResource("sup").getURI()))); } superClasses.remove(description); System.out.println("Sup(" + description + "):" + superClasses); @@ -1984,12 +1997,11 @@ public SortedSet getSubClasses(OWLClassExpression descriptio if(description.isAnonymous()){ throw new IllegalArgumentException("Only named classes are supported."); } - SortedSet subClasses = new TreeSet(); -// if(ks.isRemote()){ + SortedSet subClasses = new TreeSet<>(); String query; if(description.isOWLThing()) { - query = String.format(SPARQLQueryUtils.SELECT_TOP_LEVEL_OWL_CLASSES); + query = SPARQLQueryUtils.SELECT_TOP_LEVEL_OWL_CLASSES; } else { query = String.format(SPARQLQueryUtils.SELECT_SUBCLASS_OF_QUERY, description.asOWLClass().toStringID()); if(direct){ @@ -2001,17 +2013,11 @@ public SortedSet getSubClasses(OWLClassExpression descriptio ResultSet rs = executeSelectQuery(query); subClasses.addAll(asOWLEntities(EntityType.CLASS, rs, "var1")); -// } else { -// OntClass ontCls = ((LocalModelBasedSparqlEndpointKS)ks).getModel().getOntClass(description.asOWLClass().toStringID()); -// ExtendedIterator iterator = ontCls.listSubClasses(direct); -// while(iterator.hasNext()) { -// subClasses.add(new OWLClassImpl(IRI.create(iterator.next().getURI()))); -// } -// } + subClasses.remove(description); subClasses.remove(df.getOWLNothing()); // System.out.println("Sub(" + description + "):" + subClasses); - return new TreeSet(subClasses); + return new TreeSet<>(subClasses); } public boolean isSuperClassOf(OWLClass sup, OWLClass sub, boolean direct) { @@ -2024,7 +2030,7 @@ public boolean isSuperClassOf(OWLClass sup, OWLClass sub, boolean direct) { @Override public SortedSet getSuperPropertiesImpl(OWLObjectProperty objectProperty) { - SortedSet properties = new TreeSet(); + SortedSet properties = new TreeSet<>(); String query = String.format( SPARQLQueryUtils.SELECT_SUPERPROPERTY_OF_QUERY, objectProperty.toStringID() @@ -2042,7 +2048,7 @@ public SortedSet getSuperPropertiesImpl(OWLObjectProperty obj @Override public SortedSet getSubPropertiesImpl(OWLObjectProperty objectProperty) { - SortedSet properties = new TreeSet(); + SortedSet properties = new TreeSet<>(); String query = String.format( SPARQLQueryUtils.SELECT_SUBPROPERTY_OF_QUERY, objectProperty.toStringID() @@ -2059,10 +2065,9 @@ public SortedSet getSubPropertiesImpl(OWLObjectProperty objec } public SortedSet getEquivalentProperties(OWLObjectProperty objectProperty) { - SortedSet properties = new TreeSet(); + SortedSet properties = new TreeSet<>(); String query = String.format( - SPARQLQueryUtils.SELECT_EQUIVALENT_PROPERTIES_QUERY, - objectProperty.toStringID() + SPARQLQueryUtils.SELECT_EQUIVALENT_PROPERTIES_QUERY, objectProperty.toStringID(), objectProperty.toStringID() ); ResultSet rs = executeSelectQuery(query); QuerySolution qs; @@ -2074,10 +2079,10 @@ public SortedSet getEquivalentProperties(OWLObjectProperty ob } public SortedSet getDisjointProperties(OWLObjectProperty objectProperty) { - SortedSet properties = new TreeSet(); + SortedSet properties = new TreeSet<>(); String query = String.format( SPARQLQueryUtils.SELECT_DISJOINT_PROPERTIES_QUERY, - objectProperty.toStringID() + objectProperty.toStringID(), objectProperty.toStringID() ); ResultSet rs = executeSelectQuery(query); QuerySolution qs; @@ -2089,10 +2094,10 @@ public SortedSet getDisjointProperties(OWLObjectProperty obje } public SortedSet getEquivalentProperties(OWLDataProperty objectProperty) { - SortedSet superProperties = new TreeSet(); + SortedSet superProperties = new TreeSet<>(); String query = String.format( SPARQLQueryUtils.SELECT_EQUIVALENT_PROPERTIES_QUERY, - objectProperty.toStringID() + objectProperty.toStringID(), objectProperty.toStringID() ); ResultSet rs = executeSelectQuery(query); QuerySolution qs; @@ -2105,7 +2110,7 @@ public SortedSet getEquivalentProperties(OWLDataProperty object @Override public SortedSet getSuperPropertiesImpl(OWLDataProperty dataProperty) { - SortedSet properties = new TreeSet(); + SortedSet properties = new TreeSet<>(); String query = String.format( SPARQLQueryUtils.SELECT_SUPERPROPERTY_OF_QUERY, dataProperty.toStringID() @@ -2123,7 +2128,7 @@ public SortedSet getSuperPropertiesImpl(OWLDataProperty dataPro @Override public SortedSet getSubPropertiesImpl(OWLDataProperty dataProperty) { - SortedSet properties = new TreeSet(); + SortedSet properties = new TreeSet<>(); String query = String.format( SPARQLQueryUtils.SELECT_SUPERPROPERTY_OF_QUERY, dataProperty.toStringID() @@ -2140,10 +2145,10 @@ public SortedSet getSubPropertiesImpl(OWLDataProperty dataPrope } public SortedSet getDisjointProperties(OWLDataProperty dataProperty) { - SortedSet properties = new TreeSet(); + SortedSet properties = new TreeSet<>(); String query = String.format( SPARQLQueryUtils.SELECT_DISJOINT_PROPERTIES_QUERY, - dataProperty.toStringID() + dataProperty.toStringID(), dataProperty.toStringID() ); ResultSet rs = executeSelectQuery(query); QuerySolution qs; @@ -2242,12 +2247,13 @@ public Map getDataPropertyDomains() { /** * Convert a SPARQL resultset into OWL entities based on the given entity type. - * @param entityType - * @param entityIRIs - * @return + * @param entityType the entity type + * @param rs the Jena resultset + * @param var the var name in the resultset + * @return a set of entities of the given entity type */ private SortedSet asOWLEntities(EntityType entityType, ResultSet rs, String var) { - Collection entityIRIs = new HashSet(); + Collection entityIRIs = new HashSet<>(); while(rs.hasNext()) { QuerySolution qs = rs.next(); Resource resource = qs.getResource(var); @@ -2260,12 +2266,12 @@ private SortedSet asOWLEntities(EntityType entityTyp /** * Convert a collection of IRIs into OWL entities based on the given entity type. - * @param entityType - * @param entityIRIs - * @return + * @param entityType the entity type + * @param entityIRIs the entity IRIs + * @return a set of entities of the given entity type */ private SortedSet asOWLEntities(EntityType entityType, Collection entityIRIs) { - SortedSet entities = new TreeSet(); + SortedSet entities = new TreeSet<>(); for (IRI iri : entityIRIs) { if(!iri.isReservedVocabulary()) { entities.add(df.getOWLEntity(entityType, iri)); @@ -2286,14 +2292,14 @@ private SortedSet asOWLEntities(EntityType entityTyp protected ResultSet executeSelectQuery(String queryString, long timeout, TimeUnit timeoutUnits){ logger.trace("Sending query \n {}", queryString);//System.out.println(queryString); QueryExecution qe = qef.createQueryExecution(queryString); - qe.setTimeout(timeout); + qe.setTimeout(timeout, timeoutUnits); try { ResultSet rs = qe.execSelect(); return rs; } catch (QueryExceptionHTTP e) { throw new QueryExceptionHTTP("Error sending query \"" + queryString + "\" to endpoint " + qef.getId(), e); } catch (Exception e) { - throw new RuntimeException("Query execution failed." ,e); + throw new RuntimeException("Error sending query \"" + queryString + "\" to endpoint " + qef.getId(), e); } } @@ -2309,8 +2315,7 @@ protected boolean executeAskQuery(String queryString){ } /** - * Returns TRUE if the class hierarchy was computed before. - * @return + * @return TRUE if the class hierarchy was computed before, otherwise FALSE */ public boolean isPrepared(){ return hierarchy != null; @@ -2394,4 +2399,21 @@ public OWLDatatype getDatatype(OWLDataProperty dp) { return XSD.STRING; } + /* (non-Javadoc) + * @see org.dllearner.core.AbstractReasonerComponent#setSynchronized() + */ + @Override + public void setSynchronized() { + throw new NotImplementedException("Method setSynchronized() not implemented yet!"); + } + + public boolean isUseValueLists() { + return useValueLists; + } + + public void setUseValueLists(boolean useValueLists) { + this.useValueLists = useValueLists; + } + + } diff --git a/components-core/src/main/java/org/dllearner/core/options/InvalidConfigOptionValueException.java b/components-core/src/main/java/org/dllearner/reasoning/SPARQLSKOSReasoner.java similarity index 53% rename from components-core/src/main/java/org/dllearner/core/options/InvalidConfigOptionValueException.java rename to components-core/src/main/java/org/dllearner/reasoning/SPARQLSKOSReasoner.java index 56bada708c..3b3a4d7bab 100644 --- a/components-core/src/main/java/org/dllearner/core/options/InvalidConfigOptionValueException.java +++ b/components-core/src/main/java/org/dllearner/reasoning/SPARQLSKOSReasoner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,24 +16,21 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +package org.dllearner.reasoning; -package org.dllearner.core.options; - +import org.aksw.jena_sparql_api.core.QueryExecutionFactory; /** - * @author Jens Lehmann + * @author Lorenz Buehmann * */ -public class InvalidConfigOptionValueException extends Exception { - - private static final long serialVersionUID = 3286110428258072698L; - - public InvalidConfigOptionValueException(ConfigOption option, Object value) { - super("The value " + value + " is not valid for the configuration option " + option + "."); - } +public class SPARQLSKOSReasoner { - public InvalidConfigOptionValueException(ConfigOption option, Object value, String reason) { - super("The value " + value + " is not valid for the configuration option " + option + ". Reason: " + reason + "."); + private QueryExecutionFactory qef; + + public SPARQLSKOSReasoner(QueryExecutionFactory qef) { + this.qef = qef; } + } diff --git a/components-core/src/main/java/org/dllearner/reasoning/StructuralReasonerExtended.java b/components-core/src/main/java/org/dllearner/reasoning/StructuralReasonerExtended.java index 5871bf5008..510d246c46 100644 --- a/components-core/src/main/java/org/dllearner/reasoning/StructuralReasonerExtended.java +++ b/components-core/src/main/java/org/dllearner/reasoning/StructuralReasonerExtended.java @@ -1,42 +1,21 @@ -/* - * This file is part of the OWL API. - * - * The contents of this file are subject to the LGPL License, Version 3.0. +/** + * Copyright (C) 2007 - 2016, Jens Lehmann * - * Copyright (C) 2011, The University of Manchester + * This file is part of DL-Learner. * - * This program is free software: you can redistribute it and/or modify + * DL-Learner is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or + * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * - * This program is distributed in the hope that it will be useful, + * DL-Learner is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - * - * - * Alternatively, the contents of this file may be used under the terms of the Apache License, Version 2.0 - * in which case, the provisions of the Apache License Version 2.0 are applicable instead of those above. - * - * Copyright 2011, University of Manchester - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * along with this program. If not, see . */ - package org.dllearner.reasoning; import java.util.ArrayList; @@ -80,6 +59,7 @@ import org.semanticweb.owlapi.model.OWLOntology; import org.semanticweb.owlapi.model.OWLSameIndividualAxiom; import org.semanticweb.owlapi.model.OWLSubClassOfAxiom; +import org.semanticweb.owlapi.model.parameters.AxiomAnnotations; import org.semanticweb.owlapi.reasoner.AxiomNotInProfileException; import org.semanticweb.owlapi.reasoner.BufferingMode; import org.semanticweb.owlapi.reasoner.ClassExpressionNotInProfileException; @@ -107,10 +87,13 @@ import org.semanticweb.owlapi.reasoner.impl.OWLObjectPropertyNode; import org.semanticweb.owlapi.reasoner.impl.OWLObjectPropertyNodeSet; import org.semanticweb.owlapi.reasoner.impl.OWLReasonerBase; +import org.semanticweb.owlapi.search.EntitySearcher; import org.semanticweb.owlapi.util.CollectionFactory; import org.semanticweb.owlapi.util.OWLObjectPropertyManager; import org.semanticweb.owlapi.util.Version; +import static org.semanticweb.owlapi.model.parameters.Imports.INCLUDED; + /** * Author: Matthew Horridge
* The University of Manchester
@@ -252,7 +235,7 @@ public Node getUnsatisfiableClasses() throws ReasonerInterruptedExcept @Override public boolean isEntailed(OWLAxiom axiom) throws ReasonerInterruptedException, UnsupportedEntailmentTypeException, TimeOutException, AxiomNotInProfileException, FreshEntitiesException, InconsistentOntologyException { - boolean containsAxiom = getRootOntology().containsAxiom(axiom, true); + boolean containsAxiom = getRootOntology().containsAxiom(axiom, INCLUDED, AxiomAnnotations.IGNORE_AXIOM_ANNOTATIONS); if(containsAxiom){ return true; @@ -270,12 +253,12 @@ public boolean isEntailed(OWLAxiom axiom) throws ReasonerInterruptedException, U return false; } else if (!ce.isAnonymous()) { OWLClass cls = ce.asOWLClass(); - Set clses = new HashSet(); + Set clses = new HashSet<>(); clses.add(cls); clses.addAll(getSubClasses(cls, false).getFlattened()); for (OWLClass curCls : clses) { - boolean contained = getRootOntology().containsAxiom(df.getOWLClassAssertionAxiom(curCls, individual), true); + boolean contained = getRootOntology().containsAxiom(df.getOWLClassAssertionAxiom(curCls, individual), INCLUDED, AxiomAnnotations.IGNORE_AXIOM_ANNOTATIONS); if (contained) { return true; } @@ -303,7 +286,7 @@ public boolean isEntailed(OWLAxiom axiom) throws ReasonerInterruptedException, U OWLClassExpression filler = ((OWLObjectSomeValuesFrom) ce).getFiller(); - Set axioms = getRootOntology().getAxioms(individual); + Set axioms = getRootOntology().getAxioms(individual, INCLUDED); for (OWLIndividualAxiom curAx : axioms) { if(curAx instanceof OWLObjectPropertyAssertionAxiom && @@ -324,7 +307,8 @@ public boolean isEntailed(OWLAxiom axiom) throws ReasonerInterruptedException, U @Override public boolean isEntailed(Set axioms) throws ReasonerInterruptedException, UnsupportedEntailmentTypeException, TimeOutException, AxiomNotInProfileException, FreshEntitiesException, InconsistentOntologyException { for (OWLAxiom ax : axioms) { - if (!getRootOntology().containsAxiomIgnoreAnnotations(ax, true)) { + assert ax != null; + if (!getRootOntology().containsAxiom(ax, INCLUDED, AxiomAnnotations.IGNORE_AXIOM_ANNOTATIONS)) { return false; } } @@ -439,7 +423,7 @@ public NodeSet getDisjointObjectProperties(OWLObjec @Override public Node getInverseObjectProperties(OWLObjectPropertyExpression pe) throws InconsistentOntologyException, FreshEntitiesException, ReasonerInterruptedException, TimeOutException { ensurePrepared(); - OWLObjectPropertyExpression inv = pe.getInverseProperty().getSimplified(); + OWLObjectPropertyExpression inv = pe.getInverseProperty(); return getEquivalentObjectProperties(inv); } @@ -579,7 +563,7 @@ public NodeSet getInstances(OWLClassExpression ce, boolean d DefaultNodeSet result = new OWLNamedIndividualNodeSet(); if (!ce.isAnonymous()) { OWLClass cls = ce.asOWLClass(); - Set clses = new HashSet(); + Set clses = new HashSet<>(); clses.add(cls); if (!direct) { clses.addAll(getSubClasses(cls, false).getFlattened()); @@ -603,13 +587,14 @@ public NodeSet getInstances(OWLClassExpression ce, boolean d if(ce instanceof OWLObjectIntersectionOf){ List operands = ((OWLObjectIntersectionOf) ce).getOperandsAsList(); - Set> tmp = new HashSet>(); + Set> tmp = new HashSet<>(); tmp.addAll(getInstances(operands.get(0), direct).getNodes()); for (int i = 1; i < operands.size(); i++) { tmp.retainAll(getInstances(operands.get(i), direct).getNodes()); } + result.addAllNodes(tmp); } else if(ce instanceof OWLObjectUnionOf){ Set operands = ((OWLObjectUnionOf) ce).getOperands(); @@ -620,7 +605,7 @@ public NodeSet getInstances(OWLClassExpression ce, boolean d System.out.println(ce); OWLObjectPropertyExpression ope = ((OWLObjectSomeValuesFrom) ce).getProperty(); OWLClassExpression filler = ((OWLObjectSomeValuesFrom) ce).getFiller(); - Set properties = new HashSet(); + Set properties = new HashSet<>(); properties.add(ope.asOWLObjectProperty()); if (!direct) { properties.addAll(getSubObjectProperties(ope, false).getFlattened()); @@ -628,8 +613,8 @@ public NodeSet getInstances(OWLClassExpression ce, boolean d for (OWLOntology ontology : getRootOntology().getImportsClosure()) { for (OWLObjectPropertyExpression curProp : properties) { Set referencingAxioms = ontology.getReferencingAxioms(curProp.asOWLObjectProperty()); - int i = 0; - for (OWLAxiom axiom : referencingAxioms) { + + for (OWLAxiom axiom : referencingAxioms) { if(axiom instanceof OWLObjectPropertyAssertionAxiom){ // check if object is instance of filler OWLIndividual object = ((OWLObjectPropertyAssertionAxiom) axiom).getObject(); @@ -665,7 +650,7 @@ public NodeSet getObjectPropertyValues(OWLNamedIndividual in for (OWLOntology ontology : getRootOntology().getImportsClosure()) { for (OWLObjectPropertyAssertionAxiom axiom : ontology.getObjectPropertyAssertionAxioms(ind)) { if (!axiom.getObject().isAnonymous()) { - if (axiom.getProperty().getSimplified().equals(pe.getSimplified())) { + if (axiom.getProperty().equals(pe)) { if (getIndividualNodeSetPolicy().equals(IndividualNodeSetPolicy.BY_SAME_AS)) { result.addNode(getSameIndividuals(axiom.getObject().asOWLNamedIndividual())); } @@ -676,7 +661,7 @@ public NodeSet getObjectPropertyValues(OWLNamedIndividual in } // Inverse of pe if (axiom.getObject().equals(ind) && !axiom.getSubject().isAnonymous()) { - OWLObjectPropertyExpression invPe = axiom.getProperty().getInverseProperty().getSimplified(); + OWLObjectPropertyExpression invPe = axiom.getProperty().getInverseProperty(); if (!invPe.isAnonymous() && inverses.contains(invPe.asOWLObjectProperty())) { if (getIndividualNodeSetPolicy().equals(IndividualNodeSetPolicy.BY_SAME_AS)) { result.addNode(getSameIndividuals(axiom.getObject().asOWLNamedIndividual())); @@ -696,7 +681,7 @@ public NodeSet getObjectPropertyValues(OWLNamedIndividual in @Override public Set getDataPropertyValues(OWLNamedIndividual ind, OWLDataProperty pe) throws InconsistentOntologyException, FreshEntitiesException, ReasonerInterruptedException, TimeOutException { ensurePrepared(); - Set literals = new HashSet(); + Set literals = new HashSet<>(); Set superProperties = getSuperDataProperties(pe, false).getFlattened(); superProperties.addAll(getEquivalentDataProperties(pe).getEntities()); for (OWLOntology ontology : getRootOntology().getImportsClosure()) { @@ -712,9 +697,9 @@ public Set getDataPropertyValues(OWLNamedIndividual ind, OWLDataProp @Override public Node getSameIndividuals(OWLNamedIndividual ind) throws InconsistentOntologyException, FreshEntitiesException, ReasonerInterruptedException, TimeOutException { ensurePrepared(); - Set inds = new HashSet(); - Set processed = new HashSet(); - List stack = new ArrayList(); + Set inds = new HashSet<>(); + Set processed = new HashSet<>(); + List stack = new ArrayList<>(); stack.add(ind); while (!stack.isEmpty()) { OWLNamedIndividual currentInd = stack.remove(0); @@ -837,9 +822,9 @@ private abstract class HierarchyInfo { */ T bottomEntity; - private Set directChildrenOfTopNode = new HashSet(); + private Set directChildrenOfTopNode = new HashSet<>(); - private Set directParentsOfBottomNode = new HashSet(); + private Set directParentsOfBottomNode = new HashSet<>(); private NodeCache nodeCache; @@ -851,7 +836,7 @@ private abstract class HierarchyInfo { public HierarchyInfo(String name, T topEntity, T bottomEntity, RawHierarchyProvider rawParentChildProvider) { this.topEntity = topEntity; this.bottomEntity = bottomEntity; - this.nodeCache = new NodeCache(this); + this.nodeCache = new NodeCache<>(this); this.rawParentChildProvider = rawParentChildProvider; this.name = name; } @@ -885,7 +870,7 @@ public RawHierarchyProvider getRawParentChildProvider() { Set getEntitiesInSignature(Set axioms) { - Set result = new HashSet(); + Set result = new HashSet<>(); for (OWLAxiom ax : axioms) { result.addAll(getEntitiesInSignature(ax)); } @@ -896,8 +881,8 @@ public void computeHierarchy() { pm.reasonerTaskStarted("Computing " + name + " hierarchy"); pm.reasonerTaskBusy(); nodeCache.clear(); - Map> cache = new HashMap>(); - Set entities = new HashSet(); + Map> cache = new HashMap<>(); + Set entities = new HashSet<>(); for (OWLOntology ont : getRootOntology().getImportsClosure()) { entities.addAll(getEntities(ont)); } @@ -909,16 +894,16 @@ public void computeHierarchy() { private void updateForSignature(Set signature, Map> cache) { - HashSet> cyclesResult = new HashSet>(); - Set processed = new HashSet(); + HashSet> cyclesResult = new HashSet<>(); + Set processed = new HashSet<>(); nodeCache.clearTopNode(); nodeCache.clearBottomNode(); nodeCache.clearNodes(signature); directChildrenOfTopNode.removeAll(signature); - Set equivTopOrChildrenOfTop = new HashSet(); - Set equivBottomOrParentsOfBottom = new HashSet(); + Set equivTopOrChildrenOfTop = new HashSet<>(); + Set equivBottomOrParentsOfBottom = new HashSet<>(); for (T entity : signature) { if (!processed.contains(entity)) { pm.reasonerTaskProgressChanged(processed.size(), signature.size()); @@ -1054,7 +1039,7 @@ else if (stackEntities.contains(superEntity)) { } } if (lowlinkMap.get(entity).equals(indexMap.get(entity))) { - Set scc = new HashSet(); + Set scc = new HashSet<>(); while (true) { T clsPrime = stack.pop(); stackEntities.remove(clsPrime); @@ -1082,7 +1067,7 @@ public NodeSet getNodeHierarchyChildren(T parent, boolean direct, DefaultNode } - Set directChildren = new HashSet(); + Set directChildren = new HashSet<>(); for (T equiv : node) { directChildren.addAll(rawParentChildProvider.getChildren(equiv)); if(directParentsOfBottomNode.contains(equiv)) { @@ -1117,7 +1102,7 @@ public NodeSet getNodeHierarchyParents(T child, boolean direct, DefaultNodeSe } - Set directParents = new HashSet(); + Set directParents = new HashSet<>(); for (T equiv : node) { directParents.addAll(rawParentChildProvider.getParents(equiv)); if(directChildrenOfTopNode.contains(equiv)) { @@ -1157,7 +1142,7 @@ private static class NodeCache { private Node bottomNode; - private Map> map = new HashMap>(); + private Map> map = new HashMap<>(); protected NodeCache(HierarchyInfo hierarchyInfo) { this.hierarchyInfo = hierarchyInfo; @@ -1178,7 +1163,7 @@ else if (element.isBottomEntity()) { } public Set> getNodes(Set elements) { - Set> result = new HashSet>(); + Set> result = new HashSet<>(); for (T element : elements) { result.add(getNode(element)); } @@ -1279,7 +1264,7 @@ public ObjectPropertyHierarchyInfo() { @Override protected Set getEntitiesInSignature(OWLAxiom ax) { - Set result = new HashSet(); + Set result = new HashSet<>(); for (OWLObjectProperty property : ax.getObjectPropertiesInSignature()) { result.add(property); result.add(property.getInverseProperty()); @@ -1289,7 +1274,7 @@ protected Set getEntitiesInSignature(OWLAxiom ax) { @Override protected Set getEntities(OWLOntology ont) { - Set result = new HashSet(); + Set result = new HashSet<>(); for (OWLObjectPropertyExpression property : ont.getObjectPropertiesInSignature()) { result.add(property); result.add(property.getInverseProperty()); @@ -1392,7 +1377,7 @@ public RawClassHierarchyProvider() {} @Override public Collection getParents(OWLClass child) { - Collection result = new HashSet(); + Collection result = new HashSet<>(); for (OWLOntology ont : getRootOntology().getImportsClosure()) { for (OWLSubClassOfAxiom ax : ont.getSubClassAxiomsForSubClass(child)) { OWLClassExpression superCls = ax.getSuperClass(); @@ -1429,7 +1414,7 @@ else if (ce instanceof OWLObjectIntersectionOf) { @Override public Collection getChildren(OWLClass parent) { - Collection result = new HashSet(); + Collection result = new HashSet<>(); for (OWLOntology ont : getRootOntology().getImportsClosure()) { for (OWLAxiom ax : ont.getReferencingAxioms(parent)) { if (ax instanceof OWLSubClassOfAxiom) { @@ -1475,12 +1460,12 @@ public RawObjectPropertyHierarchyProvider() { public void rebuild() { propertyManager = new OWLObjectPropertyManager(getRootOntology().getOWLOntologyManager(), getRootOntology()); sub2Super = propertyManager.getPropertyHierarchy(); - super2Sub = new HashMap>(); + super2Sub = new HashMap<>(); for(OWLObjectPropertyExpression sub : sub2Super.keySet()) { for(OWLObjectPropertyExpression superProp : sub2Super.get(sub)) { Set subs = super2Sub.get(superProp); if(subs == null) { - subs = new HashSet(); + subs = new HashSet<>(); super2Sub.put(superProp, subs); } subs.add(sub); @@ -1498,7 +1483,7 @@ public Collection getParents(OWLObjectPropertyExpre return Collections.emptySet(); } else { - return new HashSet(propertyExpressions); + return new HashSet<>(propertyExpressions); } } @@ -1513,7 +1498,7 @@ public Collection getChildren(OWLObjectPropertyExpr return Collections.emptySet(); } else { - return new HashSet(propertyExpressions); + return new HashSet<>(propertyExpressions); } } @@ -1523,8 +1508,8 @@ private class RawDataPropertyHierarchyProvider implements RawHierarchyProvider getParents(OWLDataProperty child) { - Set properties = new HashSet(); - for (OWLDataPropertyExpression prop : child.getSuperProperties(getRootOntology().getImportsClosure())) { + Set properties = new HashSet<>(); + for (OWLDataPropertyExpression prop : EntitySearcher.getSuperProperties(child, getRootOntology().getImportsClosure())) { properties.add(prop.asOWLDataProperty()); } return properties; @@ -1532,8 +1517,8 @@ public Collection getParents(OWLDataProperty child) { @Override public Collection getChildren(OWLDataProperty parent) { - Set properties = new HashSet(); - for (OWLDataPropertyExpression prop : parent.getSubProperties(getRootOntology().getImportsClosure())) { + Set properties = new HashSet<>(); + for (OWLDataPropertyExpression prop : EntitySearcher.getSubProperties(parent, getRootOntology().getImportsClosure())) { properties.add(prop.asOWLDataProperty()); } return properties; diff --git a/components-core/src/main/java/org/dllearner/refinementoperators/CustomHierarchyRefinementOperator.java b/components-core/src/main/java/org/dllearner/refinementoperators/CustomHierarchyRefinementOperator.java index 94ff418de2..f54f921b3f 100644 --- a/components-core/src/main/java/org/dllearner/refinementoperators/CustomHierarchyRefinementOperator.java +++ b/components-core/src/main/java/org/dllearner/refinementoperators/CustomHierarchyRefinementOperator.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2012, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -34,10 +34,10 @@ */ public interface CustomHierarchyRefinementOperator extends RefinementOperator { - public void setClassHierarchy(ClassHierarchy classHierarchy); + void setClassHierarchy(ClassHierarchy classHierarchy); - public void setObjectPropertyHierarchy(ObjectPropertyHierarchy objectPropertyHierarchy); + void setObjectPropertyHierarchy(ObjectPropertyHierarchy objectPropertyHierarchy); - public void setDataPropertyHierarchy(DatatypePropertyHierarchy dataPropertyHierarchy); + void setDataPropertyHierarchy(DatatypePropertyHierarchy dataPropertyHierarchy); } diff --git a/components-core/src/main/java/org/dllearner/refinementoperators/CustomStartRefinementOperator.java b/components-core/src/main/java/org/dllearner/refinementoperators/CustomStartRefinementOperator.java index 374fd29611..09407e7126 100644 --- a/components-core/src/main/java/org/dllearner/refinementoperators/CustomStartRefinementOperator.java +++ b/components-core/src/main/java/org/dllearner/refinementoperators/CustomStartRefinementOperator.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2012, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -28,6 +28,6 @@ */ public interface CustomStartRefinementOperator extends RefinementOperator { - public void setStartClass(OWLClassExpression description); + void setStartClass(OWLClassExpression description); } diff --git a/components-core/src/main/java/org/dllearner/refinementoperators/ELDown3.java b/components-core/src/main/java/org/dllearner/refinementoperators/ELDown.java similarity index 86% rename from components-core/src/main/java/org/dllearner/refinementoperators/ELDown3.java rename to components-core/src/main/java/org/dllearner/refinementoperators/ELDown.java index 561b4d26a9..e787b43b5b 100644 --- a/components-core/src/main/java/org/dllearner/refinementoperators/ELDown3.java +++ b/components-core/src/main/java/org/dllearner/refinementoperators/ELDown.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,44 +16,21 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.refinementoperators; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeMap; -import java.util.TreeSet; - import org.apache.log4j.Logger; -import org.dllearner.algorithms.el.ELDescriptionEdge; -import org.dllearner.algorithms.el.ELDescriptionEdgeComparator; -import org.dllearner.algorithms.el.ELDescriptionNode; -import org.dllearner.algorithms.el.ELDescriptionTree; -import org.dllearner.algorithms.el.ELDescriptionTreeComparator; -import org.dllearner.algorithms.el.TreeAndRoleSet; -import org.dllearner.algorithms.el.TreeAndRoleSetComparator; +import org.dllearner.algorithms.el.*; import org.dllearner.core.AbstractReasonerComponent; import org.dllearner.core.ComponentInitException; +import org.dllearner.core.config.ConfigOption; +import org.dllearner.core.owl.ClassHierarchy; import org.dllearner.core.owl.DatatypePropertyHierarchy; -import org.dllearner.core.owl.Hierarchy; import org.dllearner.core.owl.ObjectPropertyHierarchy; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLDataProperty; -import org.semanticweb.owlapi.model.OWLDataRange; -import org.semanticweb.owlapi.model.OWLObjectProperty; -import org.semanticweb.owlapi.model.OWLProperty; - +import org.semanticweb.owlapi.model.*; import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; +import java.util.*; + //import com.jamonapi.Monitor; //import com.jamonapi.MonitorFactory; @@ -76,31 +53,31 @@ * @author Jens Lehmann * */ -@SuppressWarnings("unused") -public class ELDown3 extends RefinementOperatorAdapter { +//@ComponentAnn(name = "EL Downward refinement operator", shortName = "eldown", version = 0.1) +public class ELDown extends RefinementOperatorAdapter { - private static Logger logger = Logger.getLogger(ELDown3.class); + private static Logger logger = Logger.getLogger(ELDown.class); private AbstractReasonerComponent rs; // hierarchies - private Hierarchy subsumptionHierarchy; + private ClassHierarchy classHierarchy; private ObjectPropertyHierarchy opHierarchy; private DatatypePropertyHierarchy dpHierarchy; // domains and ranges - private Map opDomains = new TreeMap(); - private Map opRanges = new TreeMap(); - private Map dpDomains = new TreeMap(); - private Map dpRanges = new TreeMap(); + private Map opDomains = new TreeMap<>(); + private Map opRanges = new TreeMap<>(); + private Map dpDomains = new TreeMap<>(); + private Map dpRanges = new TreeMap<>(); // app_A set of applicable properties for a given class - private Map> appOP = new TreeMap>(); - private Map> appDP = new TreeMap>(); + private Map> appOP = new TreeMap<>(); + private Map> appDP = new TreeMap<>(); // most general applicable properties - private Map> mgrOP = new TreeMap>(); - private Map> mgrDP = new TreeMap>(); + private Map> mgrOP = new TreeMap<>(); + private Map> mgrDP = new TreeMap<>(); // utility class private Utility utility; @@ -109,20 +86,46 @@ public class ELDown3 extends RefinementOperatorAdapter { private ELDescriptionTreeComparator treeComp = new ELDescriptionTreeComparator(); private ELDescriptionEdgeComparator edgeComp = new ELDescriptionEdgeComparator(); private TreeAndRoleSetComparator mComp = new TreeAndRoleSetComparator(); - + + @ConfigOption(description = "maximum depth", defaultValue = "2") private int maxClassExpressionDepth = 2; private OWLDataFactory df = new OWLDataFactoryImpl(); - public ELDown3(AbstractReasonerComponent rs) { + private boolean instanceBasedDisjoints; + + public ELDown(AbstractReasonerComponent rs) { this(rs, true); } - public ELDown3(AbstractReasonerComponent rs, boolean instanceBasedDisjoints) { + public ELDown(AbstractReasonerComponent rs, boolean instanceBasedDisjoints) { this.rs = rs; - subsumptionHierarchy = rs.getClassHierarchy(); - opHierarchy = rs.getObjectPropertyHierarchy(); - dpHierarchy = rs.getDatatypePropertyHierarchy(); + this.instanceBasedDisjoints = instanceBasedDisjoints; + } + + public ELDown(AbstractReasonerComponent rs, boolean instanceBasedDisjoints, ClassHierarchy classHierarchy, + ObjectPropertyHierarchy opHierarchy, DatatypePropertyHierarchy dpHierarchy) { + this.rs = rs; + this.instanceBasedDisjoints = instanceBasedDisjoints; + this.classHierarchy = classHierarchy; + this.opHierarchy = opHierarchy; + this.dpHierarchy = dpHierarchy; + } + + @Override + public void init() throws ComponentInitException { + if(classHierarchy == null) { + classHierarchy = rs.getClassHierarchy(); + } + + if(opHierarchy == null) { + opHierarchy = rs.getObjectPropertyHierarchy(); + } + + if(dpHierarchy == null) { + dpHierarchy = rs.getDatatypePropertyHierarchy(); + } + // query reasoner for domains and ranges // (because they are used often in the operator) @@ -146,7 +149,7 @@ public Set refine(OWLClassExpression concept) { logger.trace("refining " + concept); ELDescriptionTree tree = new ELDescriptionTree(rs, concept); List refinementTrees = refine(tree); - Set refinements = new HashSet(); + Set refinements = new HashSet<>(); for(ELDescriptionTree refinementTree : refinementTrees) { refinements.add(refinementTree.transformToClassExpression()); } @@ -163,11 +166,11 @@ public Set refine(OWLClassExpression concept) { */ public List refine(ELDescriptionTree tree) { logger.trace("applying \\rho on " + tree.toDescriptionString()); - List refinements = new LinkedList(); + List refinements = new LinkedList<>(); // loop over all nodes of the tree and perform one of the // transformations on it (we make a copy of all nodes, because // the transformations can, of course, add new nodes) - List nodes = new LinkedList(tree.getNodes()); + List nodes = new LinkedList<>(tree.getNodes()); for(ELDescriptionNode v : nodes) { logger.trace("picked node v: " + v); @@ -195,7 +198,7 @@ public List refine(ELDescriptionTree tree) { // operation 1: label extension private List extendLabel(ELDescriptionTree tree, ELDescriptionNode v, int[] position) { // Monitor mon = MonitorFactory.start("extend label"); - List refinements = new LinkedList(); + List refinements = new LinkedList<>(); // the index is the range of role in the edge pointing to the parent of this node OWLClassExpression index; @@ -229,7 +232,7 @@ private List extendLabel(ELDescriptionTree tree, ELDescriptio // operation 2: label refinement private List refineLabel(ELDescriptionTree tree, ELDescriptionNode v, int[] position) { // Monitor mon = MonitorFactory.start("refine label"); - List refinements = new LinkedList(); + List refinements = new LinkedList<>(); // loop through all classes in label for(OWLClass nc : v.getLabel()) { @@ -256,7 +259,7 @@ private List refineLabel(ELDescriptionTree tree, ELDescriptio // operation 3: refine edge private List refineEdge(ELDescriptionTree tree, ELDescriptionNode v, int[] position) { // Monitor mon = MonitorFactory.start("refine edge"); - List refinements = new LinkedList(); + List refinements = new LinkedList<>(); for(int edgeNumber = 0; edgeNumber < v.getEdges().size(); edgeNumber++) { ELDescriptionEdge edge = v.getEdges().get(edgeNumber); @@ -308,10 +311,10 @@ private List refineEdge(ELDescriptionTree tree, ELDescription // new version of as private Collection attachSubtree2(ELDescriptionTree tree, ELDescriptionNode v, int[] position) { // Monitor mon = MonitorFactory.start("attach tree"); - Set refinements = new TreeSet(treeComp); + Set refinements = new TreeSet<>(treeComp); // create and initialise M - TreeSet m = new TreeSet(mComp); + TreeSet m = new TreeSet<>(mComp); ELDescriptionTree topTree = new ELDescriptionTree(rs, df.getOWLThing()); OWLClassExpression index = getIndex(v); SortedSet appOPs = utility.computeApplicableObjectProperties(index); @@ -340,7 +343,7 @@ private Collection attachSubtree2(ELDescriptionTree tree, ELD // } // logger.trace("R': " + rpSet); - Set rppSet = new TreeSet(); + Set rppSet = new TreeSet<>(); while(!rpSet.isEmpty()) { // pick an element r from R' @@ -405,9 +408,9 @@ private Collection attachSubtree2(ELDescriptionTree tree, ELD // new version of as private Collection attachSubtreeDatatypeProperties(ELDescriptionTree tree, ELDescriptionNode v, int[] position) { // Monitor mon = MonitorFactory.start("attach tree"); - Set refinements = new TreeSet(treeComp); + Set refinements = new TreeSet<>(treeComp); // create and initialise M - TreeSet m = new TreeSet(mComp); + TreeSet m = new TreeSet<>(mComp); ELDescriptionTree topTree = new ELDescriptionTree(rs, df.getOWLThing()); OWLClassExpression index = getIndex(v); SortedSet appOPs = utility.computeApplicableDatatypeProperties(index); @@ -436,7 +439,7 @@ private Collection attachSubtreeDatatypeProperties(ELDescript // } // logger.trace("R': " + rpSet); - Set rppSet = new TreeSet(); + Set rppSet = new TreeSet<>(); while(!rpSet.isEmpty()) { // pick an element r from R' @@ -515,11 +518,11 @@ private ELDescriptionTree mergeTrees(ELDescriptionTree tree, ELDescriptionNode n // logger.trace("merge start: " + mergedTree); // create a list of nodes we still need to process - LinkedList toProcess = new LinkedList(); + LinkedList toProcess = new LinkedList<>(); toProcess.add(newTree.getRootNode()); // map from nodes to cloned nodes - Map cloneMap = new HashMap(); + Map cloneMap = new HashMap<>(); // Monitor mon2 = MonitorFactory.start("as.tmp"); @@ -569,7 +572,7 @@ public boolean asCheck(ELDescriptionNode v) { // System.out.println("asCheck: " + v.getTree().toSimulationString()); // find all edges up to the root node - List piVEdges = new LinkedList(); + List piVEdges = new LinkedList<>(); ELDescriptionNode tmp = v; while(!tmp.isRoot()) { piVEdges.add(tmp.getParentEdge()); @@ -614,7 +617,7 @@ public boolean asCheck(ELDescriptionNode v) { // of hasChild) // TODO: used in both EL operators => move to utility class private OWLClassExpression getFlattenedConcept(ELDescriptionNode node) { - Set operands = new HashSet(); + Set operands = new HashSet<>(); // add all named classes to intersection for(OWLClass nc : node.getLabel()) { @@ -664,11 +667,4 @@ private boolean containsSuperProperty(OWLObjectProperty prop, Set refine(OWLClassExpression description, int maxLength); + Set refine(OWLClassExpression description, int maxLength); /** * Optional refinement operation, where the learning algorithm can * specify an additional bound on the length of descriptions and * a list of known refinements, which do not need to be returned. * - * @param OWLClassExpression The description, which will be refined. - * @param maxLength The maximum length of returned description, where length is defined by {@link OWLClassExpressionUtils#getLength()}. + * @param description The description, which will be refined. + * @param maxLength The maximum length of returned description, where length is defined by {@link OWLClassExpressionUtils#getLength(OWLClassExpression)}. * @param knownRefinements A collection of known refinements, which do not need to be returned. * @return A set of refinements obeying the above restrictions. */ - public Set refine(OWLClassExpression description, int maxLength, List knownRefinements); - + Set refine(OWLClassExpression description, int maxLength, List knownRefinements); + + void setLengthMetric(OWLClassExpressionLengthMetric lengthMetric); + + OWLClassExpressionLengthMetric getLengthMetric(); } diff --git a/components-core/src/main/java/org/dllearner/refinementoperators/MathOperations.java b/components-core/src/main/java/org/dllearner/refinementoperators/MathOperations.java index b88fd286aa..99eb499bb0 100644 --- a/components-core/src/main/java/org/dllearner/refinementoperators/MathOperations.java +++ b/components-core/src/main/java/org/dllearner/refinementoperators/MathOperations.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.refinementoperators; import java.util.Collections; @@ -120,7 +119,7 @@ else if(number - i == 0){ */ public static List> getCombos(int number) { // on Notebook: length 70 in 17 seconds, length 50 in 800ms, length 30 in 15ms - List> combosTmp = new LinkedList>(); + List> combosTmp = new LinkedList<>(); decompose(number, number, new LinkedList(), combosTmp); return combosTmp; } @@ -135,7 +134,7 @@ public static List> getCombos(int number) { * @return A two dimensional list constructed in {@link #getCombos(int)}. */ public static List> getCombos(int length, int maxValue) { - List> combosTmp = new LinkedList>(); + List> combosTmp = new LinkedList<>(); decompose(length, maxValue, new LinkedList(), combosTmp); return combosTmp; } @@ -146,8 +145,8 @@ private static LinkedList cloneList(LinkedList list) { } /** - * Implements a cross product in the sense that each union OWLClassExpression in the - * base set is extended by each OWLClassExpression in the new set. + * Implements a cross product in the sense that each union in the + * base set is extended by each class expression in the new set. * * Example: * baseSet = {A1 OR A2, A1 or A3} @@ -164,7 +163,7 @@ private static LinkedList cloneList(LinkedList list) { * @return The "cross product" of baseSet and newSet. */ public static SortedSet incCrossProduct(Set baseSet, Set newSet) { - SortedSet retSet = new TreeSet(); + SortedSet retSet = new TreeSet<>(); if(baseSet.isEmpty()) { for(OWLClassExpression c : newSet) { @@ -199,7 +198,7 @@ public static SortedSet incCrossProduct(Set * @return See description. */ public static boolean containsDoubleObjectSomeRestriction(OWLClassExpression d) { - Set roles = new HashSet(); + Set roles = new HashSet<>(); for(OWLClassExpression c : d.getNestedClassExpressions()) { if(c instanceof OWLObjectSomeValuesFrom) { OWLObjectPropertyExpression role = ((OWLObjectSomeValuesFrom)c).getProperty(); diff --git a/components-core/src/main/java/org/dllearner/refinementoperators/OperatorInverter.java b/components-core/src/main/java/org/dllearner/refinementoperators/OperatorInverter.java index 5e708dcf86..660e3e6028 100644 --- a/components-core/src/main/java/org/dllearner/refinementoperators/OperatorInverter.java +++ b/components-core/src/main/java/org/dllearner/refinementoperators/OperatorInverter.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,19 +16,20 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.refinementoperators; -import java.util.List; -import java.util.Set; -import java.util.TreeSet; - +import org.dllearner.core.ComponentAnn; import org.dllearner.core.ComponentInitException; +import org.dllearner.core.config.ConfigOption; +import org.dllearner.utilities.owl.OWLClassExpressionLengthMetric; import org.dllearner.utilities.owl.OWLClassExpressionUtils; import org.semanticweb.owlapi.model.OWLClassExpression; - import uk.ac.manchester.cs.owl.owlapi.OWLObjectComplementOfImpl; +import java.util.List; +import java.util.Set; +import java.util.TreeSet; + /** * The class uses an existing refinement operator and inverts it, i.e. a * downward refinement operator is turned into an upward refinement operator @@ -37,20 +38,58 @@ * @author Jens Lehmann * */ +@ComponentAnn(name = "OperatorInverter", shortName = "inv_op", version = 0.1) public class OperatorInverter implements LengthLimitedRefinementOperator { + private OWLClassExpressionLengthMetric lengthMetric; + + public LengthLimitedRefinementOperator getOperator() { + return operator; + } + + public void setOperator(LengthLimitedRefinementOperator operator) { + this.operator = operator; + this.lengthMetric = operator.getLengthMetric(); + if (this.lengthMetric == null) { + this.lengthMetric = OWLClassExpressionLengthMetric.getDefaultMetric(); + } + } + + public boolean isUseNegationNormalForm() { + return useNegationNormalForm; + } + + public void setUseNegationNormalForm(boolean useNegationNormalForm) { + this.useNegationNormalForm = useNegationNormalForm; + } + + public boolean isGuaranteeLength() { + return guaranteeLength; + } + + public void setGuaranteeLength(boolean guaranteeLength) { + this.guaranteeLength = guaranteeLength; + } + + @ConfigOption(description = "operator to invert", required = true) private LengthLimitedRefinementOperator operator; + @ConfigOption(description = "whether to apply NNF", defaultValue = "true") private boolean useNegationNormalForm = true; + @ConfigOption(description = "Whether inverse solutions must respect the desired max length", defaultValue = "true") private boolean guaranteeLength = true; public OperatorInverter(LengthLimitedRefinementOperator operator) { this.operator = operator; + this.lengthMetric = operator.getLengthMetric(); + } + + public OperatorInverter() { } @Override public Set refine(OWLClassExpression description) { Set refinements = operator.refine(getNegation(description)); - TreeSet results = new TreeSet(); + TreeSet results = new TreeSet<>(); for(OWLClassExpression d : refinements) { results.add(getNegation(d)); } @@ -63,16 +102,16 @@ public Set refine(OWLClassExpression description, int maxLen // System.out.println("negated description: " + negatedDescription); // concept length can change because of the conversion process; as a heuristic // we increase maxLength by the length difference of negated and original concept - int lengthDiff = Math.max(0, OWLClassExpressionUtils.getLength(negatedDescription) - OWLClassExpressionUtils.getLength(description)); + int lengthDiff = Math.max(0, OWLClassExpressionUtils.getLength(negatedDescription, lengthMetric) - OWLClassExpressionUtils.getLength(description, lengthMetric)); Set refinements = operator.refine(negatedDescription, maxLength+lengthDiff+1); // System.out.println("refinv: " + refinements); - TreeSet results = new TreeSet(); + TreeSet results = new TreeSet<>(); for(OWLClassExpression d : refinements) { OWLClassExpression dNeg = getNegation(d); // System.out.println("dNeg: " + dNeg); // to satisfy the guarantee that the method does not return longer // concepts, we perform an additional check - if(!guaranteeLength || OWLClassExpressionUtils.getLength(dNeg) <= maxLength) { + if(!guaranteeLength || OWLClassExpressionUtils.getLength(dNeg, lengthMetric) <= maxLength) { results.add(dNeg); } } @@ -85,6 +124,16 @@ public Set refine(OWLClassExpression description, int maxLen throw new Error("Method not implemented."); } + @Override + public void setLengthMetric(OWLClassExpressionLengthMetric lengthMetric) { + this.lengthMetric = lengthMetric; + operator.setLengthMetric(lengthMetric); + } + + public OWLClassExpressionLengthMetric getLengthMetric() { + return this.lengthMetric; + } + private OWLClassExpression getNegation(OWLClassExpression description) { OWLClassExpression negatedDescription = new OWLObjectComplementOfImpl(description); if(useNegationNormalForm) { diff --git a/components-core/src/main/java/org/dllearner/refinementoperators/PsiDown.java b/components-core/src/main/java/org/dllearner/refinementoperators/PsiDown.java index adaefd1e2c..0ab115920b 100644 --- a/components-core/src/main/java/org/dllearner/refinementoperators/PsiDown.java +++ b/components-core/src/main/java/org/dllearner/refinementoperators/PsiDown.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,30 +16,17 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.refinementoperators; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; -import java.util.TreeSet; - +import com.google.common.collect.Lists; import org.dllearner.core.AbstractReasonerComponent; import org.dllearner.core.ComponentInitException; import org.dllearner.core.owl.OWLObjectIntersectionOfImplExt; import org.dllearner.core.owl.OWLObjectUnionOfImplExt; import org.dllearner.learningproblems.PosNegLP; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLObjectAllValuesFrom; -import org.semanticweb.owlapi.model.OWLObjectComplementOf; -import org.semanticweb.owlapi.model.OWLObjectIntersectionOf; -import org.semanticweb.owlapi.model.OWLObjectProperty; -import org.semanticweb.owlapi.model.OWLObjectPropertyExpression; -import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom; -import org.semanticweb.owlapi.model.OWLObjectUnionOf; +import org.semanticweb.owlapi.model.*; -import com.google.common.collect.Lists; +import java.util.*; /** * Operatoren Psi-Down und Psi-Up müssen noch so umgeschrieben werden, dass sie @@ -69,7 +56,7 @@ public PsiDown(PosNegLP learningProblem, AbstractReasonerComponent reasoningServ } private void createTopSet() { - topSet = new TreeSet(); + topSet = new TreeSet<>(); // TOP OR TOP => Was soll mit Refinements passieren, die immer improper sind? List operands = Lists.newArrayList(df.getOWLThing(), df.getOWLThing()); @@ -95,13 +82,13 @@ private void createTopSet() { @SuppressWarnings("unchecked") public Set refine(OWLClassExpression concept) { - Set refinements = new HashSet(); - Set tmp = new HashSet(); + Set refinements = new HashSet<>(); + Set tmp; if (concept.isOWLThing()) { return (Set) topSet.clone(); } else if (concept.isOWLNothing()) { - return new HashSet(); + return new HashSet<>(); } else if (!concept.isAnonymous()) { // beachte: die Funktion gibt bereits nur nicht-äquivalente Konzepte zurück // beachte weiter: die zurückgegebenen Instanzen dürfen nicht verändert werden, @@ -134,7 +121,7 @@ public Set refine(OWLClassExpression concept) { // hier wird nur eine neue Liste erstellt // => eigentlich muss nicht geklont werden (d.h. deep copy) da // die Konzepte nicht verändert werden während des Algorithmus - List newChildren = new LinkedList(operands); + List newChildren = new LinkedList<>(operands); // es muss genau die vorherige Reihenfolge erhalten bleiben // (zumindest bis die Normalform definiert ist) int index = newChildren.indexOf(child); @@ -154,7 +141,7 @@ public Set refine(OWLClassExpression concept) { tmp = refine(child); // neue MultiConjunction konstruieren for(OWLClassExpression c : tmp) { - List newChildren = new LinkedList(operands); + List newChildren = new LinkedList<>(operands); // es muss genau die vorherige Reihenfolge erhalten bleiben // (zumindest bis die Normalform definiert ist) int index = newChildren.indexOf(child); @@ -167,7 +154,7 @@ public Set refine(OWLClassExpression concept) { // ein Element der Disjunktion kann weggelassen werden for(OWLClassExpression child : ((OWLObjectUnionOf) concept).getOperandsAsList()) { - List newChildren = new LinkedList(operands); + List newChildren = new LinkedList<>(operands); newChildren.remove(child); // wenn nur ein Kind da ist, dann wird Disjunktion gleich weggelassen if(newChildren.size()==1) @@ -192,8 +179,8 @@ public Set refine(OWLClassExpression concept) { refinements.add(df.getOWLNothing()); } else if (concept instanceof OWLObjectAllValuesFrom) { - OWLObjectPropertyExpression role = ((OWLObjectSomeValuesFrom) concept).getProperty(); - OWLClassExpression filler = ((OWLObjectSomeValuesFrom) concept).getFiller(); + OWLObjectPropertyExpression role = ((OWLObjectAllValuesFrom) concept).getProperty(); + OWLClassExpression filler = ((OWLObjectAllValuesFrom) concept).getFiller(); tmp = refine(filler); for(OWLClassExpression c : tmp) { diff --git a/components-core/src/main/java/org/dllearner/refinementoperators/PsiUp.java b/components-core/src/main/java/org/dllearner/refinementoperators/PsiUp.java index 1b5f8ca1cc..c8587c5e56 100644 --- a/components-core/src/main/java/org/dllearner/refinementoperators/PsiUp.java +++ b/components-core/src/main/java/org/dllearner/refinementoperators/PsiUp.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.refinementoperators; import java.util.HashSet; @@ -58,7 +57,7 @@ public PsiUp(PosNegLP learningProblem, AbstractReasonerComponent reasoningServic } private void createBottomSet() { - bottomSet = new TreeSet(); + bottomSet = new TreeSet<>(); // BOTTOM AND BOTTOM List operands = Lists.newArrayList(df.getOWLNothing(), df.getOWLNothing()); @@ -84,11 +83,11 @@ private void createBottomSet() { @SuppressWarnings("unchecked") public Set refine(OWLClassExpression concept) { - Set refinements = new HashSet(); - Set tmp = new HashSet(); + Set refinements = new HashSet<>(); + Set tmp = new HashSet<>(); if (concept.isOWLThing()) { - return new TreeSet(); + return new TreeSet<>(); } else if (concept.isOWLNothing()) { return (Set) bottomSet.clone(); } else if (!concept.isAnonymous()) { @@ -134,7 +133,7 @@ public Set refine(OWLClassExpression concept) { // hier wird nur eine neue Liste erstellt // => eigentlich muss nicht geklont werden (d.h. deep copy) da // die Konzepte nicht verändert werden während des Algorithmus - List newChildren = new LinkedList(operands); + List newChildren = new LinkedList<>(operands); // es muss genau die vorherige Reihenfolge erhalten bleiben // (zumindest bis die Normalform definiert ist) int index = newChildren.indexOf(child); @@ -147,7 +146,7 @@ public Set refine(OWLClassExpression concept) { // ein Element der Konjunktion kann weggelassen werden for(OWLClassExpression child : operands) { - List newChildren = new LinkedList(operands); + List newChildren = new LinkedList<>(operands); newChildren.remove(child); if(newChildren.size()==1) refinements.add(newChildren.get(0)); @@ -166,7 +165,7 @@ public Set refine(OWLClassExpression concept) { tmp = refine(child); // neue MultiConjunction konstruieren for(OWLClassExpression c : tmp) { - List newChildren = new LinkedList(operands); + List newChildren = new LinkedList<>(operands); // es muss genau die vorherige Reihenfolge erhalten bleiben // (zumindest bis die Normalform definiert ist) int index = newChildren.indexOf(child); @@ -188,8 +187,8 @@ public Set refine(OWLClassExpression concept) { if(filler.isOWLThing()) refinements.add(df.getOWLThing()); } else if (concept instanceof OWLObjectAllValuesFrom) { - OWLObjectPropertyExpression role = ((OWLObjectSomeValuesFrom) concept).getProperty(); - OWLClassExpression filler = ((OWLObjectSomeValuesFrom) concept).getFiller(); + OWLObjectPropertyExpression role = ((OWLObjectAllValuesFrom) concept).getProperty(); + OWLClassExpression filler = ((OWLObjectAllValuesFrom) concept).getFiller(); tmp = refine(filler); for(OWLClassExpression c : tmp) { diff --git a/components-core/src/main/java/org/dllearner/refinementoperators/ReasoningBasedRefinementOperator.java b/components-core/src/main/java/org/dllearner/refinementoperators/ReasoningBasedRefinementOperator.java index ca7f9e4406..c7d9cb33d1 100644 --- a/components-core/src/main/java/org/dllearner/refinementoperators/ReasoningBasedRefinementOperator.java +++ b/components-core/src/main/java/org/dllearner/refinementoperators/ReasoningBasedRefinementOperator.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2012, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -29,6 +29,6 @@ */ public interface ReasoningBasedRefinementOperator extends RefinementOperator { - public void setReasoner(Reasoner reasoner); + void setReasoner(Reasoner reasoner); } diff --git a/components-core/src/main/java/org/dllearner/refinementoperators/RefinementOperator.java b/components-core/src/main/java/org/dllearner/refinementoperators/RefinementOperator.java index 2d108e4789..5f6c70f80d 100755 --- a/components-core/src/main/java/org/dllearner/refinementoperators/RefinementOperator.java +++ b/components-core/src/main/java/org/dllearner/refinementoperators/RefinementOperator.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.refinementoperators; import java.util.Set; @@ -27,7 +26,7 @@ /** * Interface for all refinement operators based on OWL/Description Logics. * A refinement operator - * maps a OWLClassExpression to a set of descriptions. For downward refinement + * maps a description to a set of descriptions. For downward refinement * operators those descriptions are more special. For upward refinement * operators, those descriptions are more general. * @@ -38,9 +37,9 @@ public interface RefinementOperator extends Component { /** * Standard refinement operation. - * @param OWLClassExpression The description, which will be refined. + * @param description The description, which will be refined. * @return A set of refinements. */ - public Set refine(OWLClassExpression description); + Set refine(OWLClassExpression description); } diff --git a/components-core/src/main/java/org/dllearner/refinementoperators/RefinementOperatorAdapter.java b/components-core/src/main/java/org/dllearner/refinementoperators/RefinementOperatorAdapter.java index 96e1b3662a..787d9ad046 100644 --- a/components-core/src/main/java/org/dllearner/refinementoperators/RefinementOperatorAdapter.java +++ b/components-core/src/main/java/org/dllearner/refinementoperators/RefinementOperatorAdapter.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,17 +16,16 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.refinementoperators; -import java.util.List; -import java.util.Set; - +import org.dllearner.utilities.owl.OWLClassExpressionLengthMetric; import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLDataFactory; - import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; +import java.util.List; +import java.util.Set; + /** * Adapter for {@link RefinementOperator} interface. * @@ -60,4 +59,10 @@ public Set refine(OWLClassExpression description, int maxLen throw new UnsupportedOperationException(); } + + public void setLengthMetric(OWLClassExpressionLengthMetric lengthMetric) { + throw new UnsupportedOperationException(); + } + + public OWLClassExpressionLengthMetric getLengthMetric() { return null; } } diff --git a/components-core/src/main/java/org/dllearner/refinementoperators/RhoDRDown.java b/components-core/src/main/java/org/dllearner/refinementoperators/RhoDRDown.java index 4d96130e00..8eaa59d981 100644 --- a/components-core/src/main/java/org/dllearner/refinementoperators/RhoDRDown.java +++ b/components-core/src/main/java/org/dllearner/refinementoperators/RhoDRDown.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,75 +16,31 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.refinementoperators; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeMap; -import java.util.TreeSet; - -import org.dllearner.core.AbstractReasonerComponent; -import org.dllearner.core.AnnComponentManager; -import org.dllearner.core.Component; -import org.dllearner.core.ComponentAnn; -import org.dllearner.core.ComponentInitException; -import org.dllearner.core.Reasoner; +import com.google.common.base.Functions; +import com.google.common.base.Joiner; +import com.google.common.collect.*; +import com.hp.hpl.jena.query.QueryExecution; +import com.hp.hpl.jena.query.QuerySolution; +import com.hp.hpl.jena.query.ResultSet; +import org.dllearner.core.*; import org.dllearner.core.config.ConfigOption; import org.dllearner.core.options.CommonConfigOptions; -import org.dllearner.core.owl.ClassHierarchy; -import org.dllearner.core.owl.DatatypePropertyHierarchy; -import org.dllearner.core.owl.OWLObjectIntersectionOfImplExt; -import org.dllearner.core.owl.OWLObjectUnionOfImplExt; -import org.dllearner.core.owl.ObjectPropertyHierarchy; +import org.dllearner.core.owl.*; import org.dllearner.reasoning.SPARQLReasoner; -import org.dllearner.utilities.Helper; import org.dllearner.utilities.OWLAPIUtils; import org.dllearner.utilities.OWLCLassExpressionToOWLClassTransformer; import org.dllearner.utilities.ToIRIFunction; import org.dllearner.utilities.owl.ConceptTransformation; +import org.dllearner.utilities.owl.OWLClassExpressionLengthMetric; import org.dllearner.utilities.owl.OWLClassExpressionToSPARQLConverter; import org.dllearner.utilities.owl.OWLClassExpressionUtils; import org.dllearner.utilities.split.DefaultDateTimeValuesSplitter; import org.dllearner.utilities.split.DefaultNumericValuesSplitter; import org.dllearner.utilities.split.ValuesSplitter; -import org.semanticweb.owlapi.expression.ParserException; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLDataHasValue; -import org.semanticweb.owlapi.model.OWLDataProperty; -import org.semanticweb.owlapi.model.OWLDataPropertyExpression; -import org.semanticweb.owlapi.model.OWLDataRange; -import org.semanticweb.owlapi.model.OWLDataSomeValuesFrom; -import org.semanticweb.owlapi.model.OWLDatatype; -import org.semanticweb.owlapi.model.OWLDatatypeRestriction; -import org.semanticweb.owlapi.model.OWLFacetRestriction; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLLiteral; -import org.semanticweb.owlapi.model.OWLNaryBooleanClassExpression; -import org.semanticweb.owlapi.model.OWLObjectAllValuesFrom; -import org.semanticweb.owlapi.model.OWLObjectCardinalityRestriction; -import org.semanticweb.owlapi.model.OWLObjectComplementOf; -import org.semanticweb.owlapi.model.OWLObjectHasValue; -import org.semanticweb.owlapi.model.OWLObjectIntersectionOf; -import org.semanticweb.owlapi.model.OWLObjectMaxCardinality; -import org.semanticweb.owlapi.model.OWLObjectMinCardinality; -import org.semanticweb.owlapi.model.OWLObjectProperty; -import org.semanticweb.owlapi.model.OWLObjectPropertyExpression; -import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom; -import org.semanticweb.owlapi.model.OWLObjectUnionOf; +import org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxParserException; +import org.semanticweb.owlapi.model.*; import org.semanticweb.owlapi.vocab.OWLFacet; import org.semanticweb.owlapi.vocab.OWLRDFVocabulary; import org.slf4j.Logger; @@ -92,20 +48,13 @@ import org.slf4j.Marker; import org.slf4j.helpers.BasicMarkerFactory; import org.springframework.beans.factory.annotation.Autowired; - import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl; import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; -import com.google.common.base.Functions; -import com.google.common.base.Joiner; -import com.google.common.collect.FluentIterable; -import com.google.common.collect.HashMultimap; -import com.google.common.collect.Lists; -import com.google.common.collect.Multimap; -import com.google.common.collect.Sets; -import com.hp.hpl.jena.query.QueryExecution; -import com.hp.hpl.jena.query.QuerySolution; -import com.hp.hpl.jena.query.ResultSet; +import java.util.*; +import java.util.Map.Entry; + +import static com.google.common.primitives.Ints.max; /** * A downward refinement operator, which makes use of domains @@ -144,15 +93,16 @@ public class RhoDRDown extends RefinementOperatorAdapter implements Component, C private DatatypePropertyHierarchy dataPropertyHierarchy; // domains and ranges - private Map opDomains = new TreeMap(); - private Map dpDomains = new TreeMap(); - private Map opRanges = new TreeMap(); + private Map opDomains = new TreeMap<>(); + private Map dpDomains = new TreeMap<>(); + private Map opRanges = new TreeMap<>(); // maximum number of fillers for each role - private Map maxNrOfFillers = new TreeMap(); + private Map maxNrOfFillers = new TreeMap<>(); // limit for cardinality restrictions (this makes sense if we e.g. have compounds with up to // more than 200 atoms but we are only interested in atoms with certain characteristics and do // not want something like e.g. >= 204 hasAtom.NOT Carbon-87; which blows up the search space + @ConfigOption(defaultValue = "5", description = "limit for cardinality restrictions (this makes sense if we e.g. have compounds with too many atoms)") private int cardinalityLimit = 5; // start concept (can be used to start from an arbitrary concept, needs @@ -165,52 +115,54 @@ public class RhoDRDown extends RefinementOperatorAdapter implements Component, C // the length of concepts of top refinements, the first values is // for refinements of \rho_\top(\top), the second one for \rho_A(\top) private int topRefinementsLength = 0; - private Map topARefinementsLength = new TreeMap(); + private Map topARefinementsLength = new TreeMap<>(); // M is finite and this value is the maximum length of any value in M - private static int mMaxLength = 4; + private int mMaxLength = 4; // the sets M_\top and M_A - private Map> m = new TreeMap>(); - private Map>> mA = new TreeMap>>(); + private Map> m = new TreeMap<>(); + private Map>> mA = new TreeMap<>(); // @see MathOperations.getCombos - private Map>> combos = new HashMap>>(); + private Map>> combos = new HashMap<>(); // refinements of the top concept ordered by length - private Map> topRefinements = new TreeMap>(); - private Map>> topARefinements = new TreeMap>>(); + private Map> topRefinements = new TreeMap<>(); + private Map>> topARefinements = new TreeMap<>(); // cumulated refinements of top (all from length one to the specified length) - private Map> topRefinementsCumulative = new HashMap>(); - private Map>> topARefinementsCumulative = new TreeMap>>(); + private Map> topRefinementsCumulative = new HashMap<>(); + private Map>> topARefinementsCumulative = new TreeMap<>(); // app_A set of applicable properties for a given class (separate for // object properties, boolean datatypes, and double datatypes) - private Map> appOP = new TreeMap>(); - private Map> appBD = new TreeMap>(); - private Map> appNumeric = new TreeMap>(); - private Map> appSD = new TreeMap>(); + private Map> appOP = new TreeMap<>(); + private Map> appBD = new TreeMap<>(); + private Map> appNumeric = new TreeMap<>(); + private Map> appSD = new TreeMap<>(); // most general applicable properties - private Map> mgr = new TreeMap>(); - private Map> mgbd = new TreeMap>(); - private Map> mgNumeric = new TreeMap>(); - private Map> mgDT = new TreeMap>(); - private Map> mgsd = new TreeMap>(); + private Map> mgr = new TreeMap<>(); + private Map> mgbd = new TreeMap<>(); + private Map> mgNumeric = new TreeMap<>(); + private Map> mgDT = new TreeMap<>(); + private Map> mgsd = new TreeMap<>(); // splits for double datatype properties in ascending order private Map> splits = new TreeMap<>(); + @ConfigOption(description = "the number of generated split intervals for numeric types", defaultValue = "12") private int maxNrOfSplits = 12; // data structure for a simple frequent pattern matching preprocessing phase private int frequencyThreshold = CommonConfigOptions.valueFrequencyThresholdDefault; - private Map> valueFrequency = new HashMap>(); + private Map> valueFrequency = new HashMap<>(); // data structure with identified frequent values - private Map> frequentValues = new HashMap>(); + private Map> frequentValues = new HashMap<>(); // frequent data values - private Map> frequentDataValues = new HashMap>(); - private Map> dataValueFrequency = new HashMap>(); + private Map> frequentDataValues = new HashMap<>(); + private Map> dataValueFrequency = new HashMap<>(); + @ConfigOption(description = "whether to use hasValue on frequently occuring strings", defaultValue = "false") private boolean useDataHasValueConstructor = false; // statistics @@ -268,7 +220,7 @@ public class RhoDRDown extends RefinementOperatorAdapter implements Component, C private boolean useSomeOnly = true; // caches for reasoner queries - private Map> cachedDisjoints = new TreeMap>(); + private Map> cachedDisjoints = new TreeMap<>(); // private Map> abDisjoint = new TreeMap>(); // private Map> notABDisjoint = new TreeMap>(); @@ -276,8 +228,10 @@ public class RhoDRDown extends RefinementOperatorAdapter implements Component, C private boolean isInitialised = false; + @ConfigOption(description = "whether to generate object complement while refining", defaultValue = "false") private boolean useObjectValueNegation = false; + private OWLClassExpressionLengthMetric lengthMetric = OWLClassExpressionLengthMetric.getDefaultMetric(); private OWLDataFactory df = new OWLDataFactoryImpl(); public RhoDRDown() {} @@ -324,7 +278,7 @@ public void init() throws ComponentInitException { if (useHasValueConstructor) { for (OWLObjectProperty op : reasoner.getObjectProperties()) { // init - Map opMap = new TreeMap(); + Map opMap = new TreeMap<>(); valueFrequency.put(op, opMap); // sets ordered by corresponding individual (which we ignore) @@ -344,7 +298,7 @@ public void init() throws ComponentInitException { } // keep only frequent patterns - Set frequentInds = new TreeSet(); + Set frequentInds = new TreeSet<>(); for (OWLIndividual i : opMap.keySet()) { if (opMap.get(i) >= frequencyThreshold) { frequentInds.add(i); @@ -354,10 +308,10 @@ public void init() throws ComponentInitException { frequentValues.put(op, frequentInds); if(useInverse) { - opMap = new TreeMap(); + opMap = new TreeMap<>(); valueFrequency.put(op.getInverseProperty(), opMap); - frequentInds = new TreeSet(); + frequentInds = new TreeSet<>(); for (Entry> entry : propertyMembers .entrySet()) { @@ -377,7 +331,7 @@ public void init() throws ComponentInitException { if(useDataHasValueConstructor) { for(OWLDataProperty dp : reasoner.getDatatypeProperties()) { - Map dpMap = new TreeMap(); + Map dpMap = new TreeMap<>(); dataValueFrequency.put(dp, dpMap); // sets ordered by corresponding individual (which we ignore) @@ -395,7 +349,7 @@ public void init() throws ComponentInitException { } // keep only frequent patterns - Set frequentInds = new TreeSet(); + Set frequentInds = new TreeSet<>(); for(OWLLiteral i : dpMap.keySet()) { if(dpMap.get(i) >= frequencyThreshold) { logger.trace("adding value "+i+", because "+dpMap.get(i) +">="+frequencyThreshold); @@ -406,10 +360,6 @@ public void init() throws ComponentInitException { } } - if(useInverse) { - - } - // we do not need the temporary set anymore and let the // garbage collector take care of it valueFrequency = null; @@ -418,22 +368,25 @@ public void init() throws ComponentInitException { // compute splits for numeric data properties if(useNumericDatatypes) { - if(reasoner.getClass().isAssignableFrom(SPARQLReasoner.class) + if(reasoner instanceof SPARQLReasoner && !((SPARQLReasoner)reasoner).isUseGenericSplitsCode()) { // TODO SPARQL support for splits - logger.warn("Numeric Facet restrictions are not (yet) implemented for " + AnnComponentManager.getName(reasoner.getClass()) + ", option ignored"); + logger.warn("Numeric Facet restrictions are not (yet) implemented for " + AnnComponentManager.getName(reasoner) + ", option ignored"); } else { ValuesSplitter splitter = new DefaultNumericValuesSplitter(reasoner, df, maxNrOfSplits); splits.putAll(splitter.computeSplits()); + if (logger.isDebugEnabled()) { + logger.debug( sparql_debug, "Numeric Splits: {}", splits); + } } } // compute splits for time data properties if (useTimeDatatypes) { - if(reasoner.getClass().isAssignableFrom(SPARQLReasoner.class) + if(reasoner instanceof SPARQLReasoner && !((SPARQLReasoner)reasoner).isUseGenericSplitsCode()) { // TODO SPARQL support for splits - logger.warn("Time based Facet restrictions are not (yet) implemented for " + AnnComponentManager.getName(reasoner.getClass()) + ", option ignored"); + logger.warn("Time based Facet restrictions are not (yet) implemented for " + AnnComponentManager.getName(reasoner) + ", option ignored"); } else { ValuesSplitter splitter = new DefaultDateTimeValuesSplitter(reasoner, df, maxNrOfSplits); splits.putAll(splitter.computeSplits()); @@ -443,11 +396,11 @@ public void init() throws ComponentInitException { // determine the maximum number of fillers for each role // (up to a specified cardinality maximum) if(useCardinalityRestrictions) { - if(reasoner.getClass().isAssignableFrom(SPARQLReasoner.class)) { + if(reasoner instanceof SPARQLReasoner) { logger.warn("Cardinality restrictions in Sparql not fully implemented, defaulting to 10."); } for(OWLObjectProperty op : reasoner.getObjectProperties()) { - if(reasoner.getClass().isAssignableFrom(SPARQLReasoner.class)) { + if(reasoner instanceof SPARQLReasoner) { // TODO SPARQL support for cardinalities maxNrOfFillers.put(op, 10); } else { @@ -498,11 +451,21 @@ public void init() throws ComponentInitException { } else { try { this.startClass = OWLAPIUtils.classExpressionPropertyExpander(startClass, reasoner, df); - } catch (ParserException e) { + } catch (ManchesterOWLSyntaxParserException e) { logger.info("Error parsing startClass: " + e.getMessage()); this.startClass = df.getOWLThing(); } } + + if(subHierarchy == null) { + subHierarchy = reasoner.getClassHierarchy(); + } + if(objectPropertyHierarchy == null) { + objectPropertyHierarchy = reasoner.getObjectPropertyHierarchy(); + } + if(dataPropertyHierarchy == null) { + dataPropertyHierarchy = reasoner.getDatatypePropertyHierarchy(); + } isInitialised = true; } @@ -518,8 +481,8 @@ public Set refine(OWLClassExpression concept) { @Override public Set refine(OWLClassExpression description, int maxLength) { // check that maxLength is valid - if(maxLength < OWLClassExpressionUtils.getLength(description)) { - throw new Error("length has to be at least class expression length (class expression: " + description + " with length " + OWLClassExpressionUtils.getLength(description) +", and max length: " + maxLength + ")"); + if(maxLength < OWLClassExpressionUtils.getLength(description, lengthMetric)) { + throw new Error("length has to be at least class expression length (class expression: " + description + " with length " + OWLClassExpressionUtils.getLength(description, lengthMetric) +", and max length: " + maxLength + ")"); } return refine(description, maxLength, null, startClass); } @@ -548,10 +511,10 @@ public Set refine(OWLClassExpression description, int maxLen // check whether using list or set makes more sense // here; and whether HashSet or TreeSet should be used // => TreeSet because duplicates are possible - Set refinements = new TreeSet(); + Set refinements = new TreeSet<>(); // used as temporary variable - Set tmp = new HashSet(); + Set tmp; if(description.isOWLThing()) { // extends top refinements if necessary @@ -589,14 +552,14 @@ public Set refine(OWLClassExpression description, int maxLen // refine the child; the new max length is the current max length minus // the currently considered concept plus the length of the child // TODO: add better explanation - int length = OWLClassExpressionUtils.getLength(description); - int childLength = OWLClassExpressionUtils.getLength(child); + int length = OWLClassExpressionUtils.getLength(description, lengthMetric); + int childLength = OWLClassExpressionUtils.getLength(child, lengthMetric); tmp = refine(child, maxLength - length + childLength, null, currDomain); // create new intersection for(OWLClassExpression c : tmp) { if(!useSomeOnly || isCombinable(description, c)) { - List newChildren = new ArrayList(operands); + List newChildren = new ArrayList<>(operands); newChildren.add(c); newChildren.remove(child); Collections.sort(newChildren); @@ -605,7 +568,7 @@ public Set refine(OWLClassExpression description, int maxLen // clean concept and transform it to ordered negation normal form // (non-recursive variant because only depth 1 was modified) mc = ConceptTransformation.cleanConceptNonRecursive(mc); - ConceptTransformation.transformToOrderedNegationNormalFormNonRecursive(mc); + mc = ConceptTransformation.nnf(mc); // check whether the intersection is OK (sanity checks), then add it if(checkIntersection((OWLObjectIntersectionOf) mc)) @@ -622,20 +585,20 @@ public Set refine(OWLClassExpression description, int maxLen // System.out.println("union child: " + child + " " + maxLength + " " + description.getLength() + " " + child.getLength()); // refine child - int length = OWLClassExpressionUtils.getLength(description); - int childLength = OWLClassExpressionUtils.getLength(child); + int length = OWLClassExpressionUtils.getLength(description, lengthMetric); + int childLength = OWLClassExpressionUtils.getLength(child, lengthMetric); tmp = refine(child, maxLength - length + childLength, null, currDomain); // construct union (see above) for(OWLClassExpression c : tmp) { - List newChildren = new ArrayList(operands); + List newChildren = new ArrayList<>(operands); newChildren.remove(child); newChildren.add(c); Collections.sort(newChildren); - OWLObjectUnionOf md = new OWLObjectUnionOfImplExt(newChildren); + OWLClassExpression md = new OWLObjectUnionOfImplExt(newChildren); // transform to ordered negation normal form - ConceptTransformation.transformToOrderedNegationNormalFormNonRecursive(md); + md = ConceptTransformation.nnf(md); // note that we do not have to call clean here because a disjunction will // never be nested in another disjunction in this operator @@ -652,7 +615,7 @@ public Set refine(OWLClassExpression description, int maxLen } else { // copy children list and remove a different element in each turn for(int i=0; i newChildren = new LinkedList(operands); + List newChildren = new LinkedList<>(operands); newChildren.remove(i); OWLObjectUnionOf md = new OWLObjectUnionOfImplExt(newChildren); refinements.add(md); @@ -667,7 +630,7 @@ public Set refine(OWLClassExpression description, int maxLen OWLClassExpression domain = role.isAnonymous() ? opDomains.get(role.getNamedProperty()) : opRanges.get(role); // rule 1: EXISTS r.D => EXISTS r.E - tmp = refine(filler, maxLength-2, null, domain); + tmp = refine(filler, maxLength-lengthMetric.objectSomeValuesLength-lengthMetric.objectProperyLength, null, domain); for(OWLClassExpression c : tmp){ refinements.add(df.getOWLObjectSomeValuesFrom(role, c)); @@ -683,7 +646,7 @@ public Set refine(OWLClassExpression description, int maxLen // rule 3: EXISTS r.D => >= 2 r.D // (length increases by 1 so we have to check whether max length is sufficient) if(useCardinalityRestrictions) {// && !role.isAnonymous()) { - if(maxLength > OWLClassExpressionUtils.getLength(description) && maxNrOfFillers.get(role) > 1) { + if(maxLength > OWLClassExpressionUtils.getLength(description, lengthMetric) && maxNrOfFillers.get(role) > 1) { OWLObjectMinCardinality min = df.getOWLObjectMinCardinality(2,role,filler); refinements.add(min); } @@ -715,7 +678,7 @@ public Set refine(OWLClassExpression description, int maxLen if(description instanceof OWLObjectMaxCardinality) { // rule 1: <= x r.C => <= x r.D if(useNegation || cardinality > 0){ - tmp = refine(filler, maxLength-3, null, range); + tmp = refine(filler, maxLength-lengthMetric.objectCardinalityLength-lengthMetric.objectProperyLength, null, range); for(OWLClassExpression d : tmp) { refinements.add(df.getOWLObjectMaxCardinality(cardinality,role,d)); @@ -729,7 +692,7 @@ public Set refine(OWLClassExpression description, int maxLen } } else if(description instanceof OWLObjectMinCardinality) { - tmp = refine(filler, maxLength-3, null, range); + tmp = refine(filler, maxLength-lengthMetric.objectCardinalityLength-lengthMetric.objectProperyLength, null, range); for(OWLClassExpression d : tmp) { refinements.add(df.getOWLObjectMinCardinality(cardinality,role,d)); @@ -786,7 +749,7 @@ public Set refine(OWLClassExpression description, int maxLen } else if (description instanceof OWLDataHasValue) { OWLDataPropertyExpression dp = ((OWLDataHasValue) description).getProperty(); - OWLLiteral value = ((OWLDataHasValue) description).getValue(); + OWLLiteral value = ((OWLDataHasValue) description).getFiller(); if(!dp.isAnonymous()){ Set subDPs = dataPropertyHierarchy.getMoreSpecialRoles(dp.asOWLDataProperty()); @@ -800,7 +763,7 @@ public Set refine(OWLClassExpression description, int maxLen if(!description.isOWLThing() && !description.isOWLNothing() && !(description instanceof OWLObjectAllValuesFrom && ((OWLObjectAllValuesFrom)description).getFiller().isOWLNothing())) { // -1 because of the AND symbol which is appended - int topRefLength = maxLength - OWLClassExpressionUtils.getLength(description) - 1; + int topRefLength = maxLength - OWLClassExpressionUtils.getLength(description, lengthMetric) - 1; // maybe we have to compute new top refinements here if(currDomain.isOWLThing()) { @@ -874,7 +837,7 @@ public Set refine(OWLClassExpression description, int maxLen // clean and transform to ordered negation normal form mc = (OWLObjectIntersectionOf) ConceptTransformation.cleanConceptNonRecursive(mc); - ConceptTransformation.transformToOrderedNegationNormalFormNonRecursive(mc); + mc = (OWLObjectIntersectionOf) ConceptTransformation.nnf(mc); // last check before intersection is added if(checkIntersection(mc)) @@ -923,14 +886,14 @@ private boolean isCombinable(OWLClassExpression ce, OWLClassExpression child) { } private Set refine(OWLObjectAllValuesFrom ce, int maxLength) { - Set refinements = new HashSet(); + Set refinements = new HashSet<>(); OWLObjectPropertyExpression role = ce.getProperty(); OWLClassExpression filler = ce.getFiller(); OWLClassExpression range = role.isAnonymous() ? opDomains.get(role.getNamedProperty()) : opRanges.get(role); // rule 1: ALL r.D => ALL r.E - Set tmp = refine(filler, maxLength-2, null, range); + Set tmp = refine(filler, maxLength-lengthMetric.objectAllValuesLength-lengthMetric.objectProperyLength, null, range); for(OWLClassExpression c : tmp) { refinements.add(df.getOWLObjectAllValuesFrom(role, c)); @@ -973,9 +936,9 @@ public static boolean checkIntersection(OWLObjectIntersectionOf intersection) { // rule 2: min restrictions at most once boolean minDoubleOccurence = false; // rule 3: no double occurences of boolean datatypes - TreeSet occuredDP = new TreeSet(); + TreeSet occuredDP = new TreeSet<>(); // rule 4: no double occurences of hasValue restrictions - TreeSet occuredVR = new TreeSet(); + TreeSet occuredVR = new TreeSet<>(); // rule 5: max. restrictions at most once boolean maxIntOccurence = false; // rule 6: min restrictions at most once @@ -1110,7 +1073,7 @@ private void computeTopRefinements(int maxLength, OWLClassExpression domain) { if(validCombo) { - SortedSet baseSet = new TreeSet(); + SortedSet baseSet = new TreeSet<>(); for(Integer j : combo) { if(domain == null) baseSet = MathOperations.incCrossProduct(baseSet, m.get(j)); @@ -1119,9 +1082,11 @@ private void computeTopRefinements(int maxLength, OWLClassExpression domain) { } // convert all concepts in ordered negation normal form + Set tmp = new HashSet<>(); for(OWLClassExpression concept : baseSet) { - ConceptTransformation.transformToOrderedForm(concept); + tmp.add((OWLObjectUnionOf) ConceptTransformation.nnf(concept)); } + baseSet = new TreeSet<>(tmp); // apply the exists filter (throwing out all refinements with // double \exists r for any r) @@ -1147,7 +1112,7 @@ private void computeTopRefinements(int maxLength, OWLClassExpression domain) { // create cumulative versions of refinements such that they can // be accessed easily - TreeSet cumulativeRefinements = new TreeSet(); + TreeSet cumulativeRefinements = new TreeSet<>(); for(int j=1; j<=i; j++) { if(domain == null) { cumulativeRefinements.addAll(topRefinements.get(j)); @@ -1184,42 +1149,32 @@ private void computeTopRefinements(int maxLength, OWLClassExpression domain) { private void computeM() { long mComputationTimeStartNs = System.nanoTime(); logger.debug(sparql_debug, "computeM"); - // initialise all possible lengths (1 to 3) + // initialise all possible lengths (1 to mMaxLength) for(int i=1; i<=mMaxLength; i++) { m.put(i, new TreeSet()); } SortedSet m1 = subHierarchy.getSubClasses(df.getOWLThing(), true); - m.put(1,m1); + m.get(lengthMetric.classLength).addAll(m1); - SortedSet m2 = new TreeSet(); if(useNegation) { + int lc = lengthMetric.objectComplementLength + lengthMetric.classLength; Set m2tmp = subHierarchy.getSuperClasses(df.getOWLNothing(), true); for(OWLClassExpression c : m2tmp) { if(!c.isOWLThing()) { - m2.add(df.getOWLObjectComplementOf(c)); + m.get(lc).add(df.getOWLObjectComplementOf(c)); } } } - // boolean datatypes, e.g. testPositive = true - if(useBooleanDatatypes) { - Set booleanDPs = reasoner.getBooleanDatatypeProperties(); - logger.debug(sparql_debug, "BOOL DPs:"+booleanDPs); - for(OWLDataProperty dp : booleanDPs) { - m2.add(df.getOWLDataHasValue(dp, df.getOWLLiteral(true))); - m2.add(df.getOWLDataHasValue(dp, df.getOWLLiteral(false))); - } - } - m.put(2,m2); - - SortedSet m3 = new TreeSet(); if(useExistsConstructor) { + int lc = lengthMetric.objectSomeValuesLength + lengthMetric.objectProperyLength + lengthMetric.classLength; + int lc_i = lengthMetric.objectSomeValuesLength + lengthMetric.objectInverseLength + lengthMetric.classLength; for(OWLObjectProperty r : objectPropertyHierarchy.getMostGeneralRoles()) { - m3.add(df.getOWLObjectSomeValuesFrom(r, df.getOWLThing())); + m.get(lc).add(df.getOWLObjectSomeValuesFrom(r, df.getOWLThing())); if(useInverse) { - m3.add(df.getOWLObjectSomeValuesFrom(r.getInverseProperty(), df.getOWLThing())); + m.get(lc_i).add(df.getOWLObjectSomeValuesFrom(r.getInverseProperty(), df.getOWLThing())); } } } @@ -1228,50 +1183,65 @@ private void computeM() { // we allow \forall r.\top here because otherwise the operator // becomes too difficult to manage due to dependencies between // M_A and M_A' where A'=ran(r) + int lc = lengthMetric.objectAllValuesLength + lengthMetric.objectProperyLength + lengthMetric.classLength; + int lc_i = lengthMetric.objectAllValuesLength + lengthMetric.objectInverseLength + lengthMetric.classLength; for(OWLObjectProperty r : objectPropertyHierarchy.getMostGeneralRoles()) { - m3.add(df.getOWLObjectAllValuesFrom(r, df.getOWLThing())); + m.get(lc).add(df.getOWLObjectAllValuesFrom(r, df.getOWLThing())); if(useInverse) { - m3.add(df.getOWLObjectAllValuesFrom(r.getInverseProperty(), df.getOWLThing())); + m.get(lc_i).add(df.getOWLObjectAllValuesFrom(r.getInverseProperty(), df.getOWLThing())); } } } + // boolean datatypes, e.g. testPositive = true + if(useBooleanDatatypes) { + Set booleanDPs = reasoner.getBooleanDatatypeProperties(); + logger.debug(sparql_debug, "BOOL DPs:"+booleanDPs); + int lc = lengthMetric.dataHasValueLength + lengthMetric.dataProperyLength; + for(OWLDataProperty dp : booleanDPs) { + m.get(lc).add(df.getOWLDataHasValue(dp, df.getOWLLiteral(true))); + m.get(lc).add(df.getOWLDataHasValue(dp, df.getOWLLiteral(false))); + } + } + if(useNumericDatatypes) { Set numericDPs = reasoner.getNumericDataProperties(); logger.debug(sparql_debug, "Numeric DPs:"+numericDPs); + int lc = lengthMetric.dataSomeValuesLength + lengthMetric.dataProperyLength + 1; for(OWLDataProperty dp : numericDPs) { if(splits.get(dp) != null && splits.get(dp).size() > 0) { OWLLiteral min = splits.get(dp).get(0); OWLLiteral max = splits.get(dp).get(splits.get(dp).size()-1); OWLDatatypeRestriction restriction = asDatatypeRestriction(dp, min, OWLFacet.MIN_INCLUSIVE); - m3.add(df.getOWLDataSomeValuesFrom(dp, restriction)); + m.get(lc).add(df.getOWLDataSomeValuesFrom(dp, restriction)); restriction = asDatatypeRestriction(dp, max, OWLFacet.MAX_INCLUSIVE); - m3.add(df.getOWLDataSomeValuesFrom(dp, restriction)); + m.get(lc).add(df.getOWLDataSomeValuesFrom(dp, restriction)); } } } if(useTimeDatatypes) { - Set dataProperties = new HashSet(); + Set dataProperties = new HashSet<>(); for (OWLDataProperty dp : reasoner.getDatatypeProperties()) { OWLDatatype datatype = reasoner.getDatatype(dp); if(datatype != null && OWLAPIUtils.dtDatatypes.contains(datatype)) { dataProperties.add(dp); } } + int lc = lengthMetric.dataSomeValuesLength + lengthMetric.dataProperyLength + 1; for(OWLDataProperty dp : dataProperties) { if(splits.get(dp).size() > 0) { OWLLiteral min = splits.get(dp).get(0); OWLLiteral max = splits.get(dp).get(splits.get(dp).size()-1); OWLDatatypeRestriction restriction = asDatatypeRestriction(dp, min, OWLFacet.MIN_INCLUSIVE); - m3.add(df.getOWLDataSomeValuesFrom(dp, restriction)); + m.get(lc).add(df.getOWLDataSomeValuesFrom(dp, restriction)); restriction = asDatatypeRestriction(dp, max, OWLFacet.MAX_INCLUSIVE); - m3.add(df.getOWLDataSomeValuesFrom(dp, restriction)); + m.get(lc).add(df.getOWLDataSomeValuesFrom(dp, restriction)); } } } @@ -1279,20 +1249,19 @@ private void computeM() { if(useDataHasValueConstructor) { Set stringDPs = reasoner.getStringDatatypeProperties(); logger.debug(sparql_debug, "STRING DPs:"+stringDPs); + int lc = lengthMetric.dataHasValueLength + lengthMetric.dataProperyLength; for(OWLDataProperty dp : stringDPs) { // loop over frequent values Set freqValues = frequentDataValues.get(dp); for(OWLLiteral lit : freqValues) { - m3.add(df.getOWLDataHasValue(dp, lit)); + m.get(lc).add(df.getOWLDataHasValue(dp, lit)); } } } - m.put(3,m3); - - SortedSet m4 = new TreeSet(); if(useCardinalityRestrictions) { logger.debug(sparql_debug, "most general properties:"); + int lc = lengthMetric.objectCardinalityLength + lengthMetric.objectProperyLength + lengthMetric.classLength; for(OWLObjectProperty r : objectPropertyHierarchy.getMostGeneralRoles()) { int maxFillers = maxNrOfFillers.get(r); logger.debug(sparql_debug, "`"+r+"="+maxFillers); @@ -1300,10 +1269,9 @@ private void computeM() { // one filler: <= 0 r.C is equivalent to NOT EXISTS r.C, // but we still keep it, because ALL r.NOT C may be difficult to reach if((useNegation && maxFillers > 0) || (!useNegation && maxFillers > 1)) - m4.add(df.getOWLObjectMaxCardinality(maxFillers-1, r, df.getOWLThing())); + m.get(lc).add(df.getOWLObjectMaxCardinality(maxFillers-1, r, df.getOWLThing())); } } - m.put(4,m4); logger.debug(sparql_debug, "m: " + m); @@ -1327,76 +1295,40 @@ private OWLDatatypeRestriction asDatatypeRestriction(OWLDataProperty dp, OWLLite private void computeM(OWLClassExpression nc) { long mComputationTimeStartNs = System.nanoTime(); -// System.out.println(nc); - mA.put(nc, new TreeMap>()); - // initialise all possible lengths (1 to 3) + // initialise all possible lengths (1 to mMaxLength) for(int i=1; i<=mMaxLength; i++) { mA.get(nc).put(i, new TreeSet()); } - // incomplete, prior implementation -// SortedSet m1 = subHierarchy.getSubClasses(nc); -// mA.get(nc).put(1,m1); - // most general classes, which are not disjoint with nc and provide real refinement SortedSet m1 = getClassCandidates(nc); - mA.get(nc).put(1,m1); + mA.get(nc).get(lengthMetric.classLength).addAll(m1); // most specific negated classes, which are not disjoint with nc - SortedSet m2 = new TreeSet(); if(useNegation) { + SortedSet m2; m2 = getNegClassCandidates(nc); - mA.get(nc).put(2,m2); - } - -// System.out.println("m1 " + "(" + nc + "): " + m1); -// System.out.println("m2 " + "(" + nc + "): " + m2); - - /* - SortedSet m2 = new TreeSet(conceptComparator); - if(useNegation) { - // the definition in the paper is more complex, but actually - // we only have to insert the most specific concepts satisfying - // the mentioned restrictions; there is no need to implement a - // recursive method because for A subClassOf A' we have not A' - // subClassOf A and thus: if A and B are disjoint then also A' - // and B; if not A AND B = B then also not A' AND B = B - // 2010/03: the latter is not correct => a recursive method is needed - SortedSet m2tmp = subHierarchy.getSuperClasses(new Nothing()); - - for(OWLClassExpression c : m2tmp) { -// if(c instanceof Thing) -// m2.add(c); -// else { - // we obviously do not add \top (\top refines \top does not make sense) - if(!(c instanceof Thing)) { - NamedClass a = (OWLClass) c; - if(!isNotADisjoint(a, nc) && isNotAMeaningful(a, nc)) - m2.add(df.getOWLObjectComplementOf(a)); - } - } + mA.get(nc).get(lengthMetric.classLength + lengthMetric.objectComplementLength).addAll(m2); } - */ // compute applicable properties computeMg(nc); // boolean datatypes, e.g. testPositive = true if(useBooleanDatatypes) { + int lc = lengthMetric.dataHasValueLength + lengthMetric.dataProperyLength; Set booleanDPs = mgbd.get(nc); - for(OWLDataProperty dp : booleanDPs) { - m2.add(df.getOWLDataHasValue(dp, df.getOWLLiteral(true))); - m2.add(df.getOWLDataHasValue(dp, df.getOWLLiteral(false))); + for (OWLDataProperty dp : booleanDPs) { + mA.get(nc).get(lc).add(df.getOWLDataHasValue(dp, df.getOWLLiteral(true))); + mA.get(nc).get(lc).add(df.getOWLDataHasValue(dp, df.getOWLLiteral(false))); } } - mA.get(nc).put(2,m2); - - SortedSet m3 = new TreeSet(); if(useExistsConstructor) { + int lc = lengthMetric.objectSomeValuesLength + lengthMetric.objectProperyLength + lengthMetric.classLength; for(OWLObjectProperty r : mgr.get(nc)) { - m3.add(df.getOWLObjectSomeValuesFrom(r, df.getOWLThing())); + mA.get(nc).get(lc).add(df.getOWLObjectSomeValuesFrom(r, df.getOWLThing())); } } @@ -1404,63 +1336,64 @@ private void computeM(OWLClassExpression nc) { // we allow \forall r.\top here because otherwise the operator // becomes too difficult to manage due to dependencies between // M_A and M_A' where A'=ran(r) + int lc = lengthMetric.objectAllValuesLength + lengthMetric.objectProperyLength + lengthMetric.classLength; for(OWLObjectProperty r : mgr.get(nc)) { - m3.add(df.getOWLObjectAllValuesFrom(r, df.getOWLThing())); + mA.get(nc).get(lc).add(df.getOWLObjectAllValuesFrom(r, df.getOWLThing())); } } if(useNumericDatatypes) { Set numericDPs = mgNumeric.get(nc); + int lc = lengthMetric.dataSomeValuesLength + lengthMetric.dataProperyLength + 1; for(OWLDataProperty dp : numericDPs) { List splitLiterals = splits.get(dp); if(splitLiterals != null && splitLiterals.size() > 0) { OWLLiteral min = splits.get(dp).get(0); OWLLiteral max = splits.get(dp).get(splits.get(dp).size()-1); - m3.add(df.getOWLDataSomeValuesFrom(dp, asDatatypeRestriction(dp, min, OWLFacet.MIN_INCLUSIVE))); - m3.add(df.getOWLDataSomeValuesFrom(dp, asDatatypeRestriction(dp, max, OWLFacet.MAX_INCLUSIVE))); + mA.get(nc).get(lc).add(df.getOWLDataSomeValuesFrom(dp, asDatatypeRestriction(dp, min, OWLFacet.MIN_INCLUSIVE))); + mA.get(nc).get(lc).add(df.getOWLDataSomeValuesFrom(dp, asDatatypeRestriction(dp, max, OWLFacet.MAX_INCLUSIVE))); } } } if(useTimeDatatypes) { Set dtDPs = mgDT.get(nc); + int lc = lengthMetric.dataSomeValuesLength + lengthMetric.dataProperyLength + 1; -// for(OWLDataProperty dp : dtDPs) { -// if(splits.get(dp).size() > 0) { -// OWLLiteral min = splits.get(dp).get(0); -// OWLLiteral max = splits.get(dp).get(splits.get(dp).size()-1); -// m3.add(df.getOWLDataSomeValuesFrom(dp, asDatatypeRestriction(dp, min, OWLFacet.MIN_INCLUSIVE))); -// m3.add(df.getOWLDataSomeValuesFrom(dp, asDatatypeRestriction(dp, max, OWLFacet.MAX_INCLUSIVE))); -// } -// } + for(OWLDataProperty dp : dtDPs) { + if(splits.get(dp).size() > 0) { + OWLLiteral min = splits.get(dp).get(0); + OWLLiteral max = splits.get(dp).get(splits.get(dp).size()-1); + mA.get(nc).get(lc).add(df.getOWLDataSomeValuesFrom(dp, asDatatypeRestriction(dp, min, OWLFacet.MIN_INCLUSIVE))); + mA.get(nc).get(lc).add(df.getOWLDataSomeValuesFrom(dp, asDatatypeRestriction(dp, max, OWLFacet.MAX_INCLUSIVE))); + } + } } if(useDataHasValueConstructor) { Set stringDPs = mgsd.get(nc); + int lc = lengthMetric.dataHasValueLength + lengthMetric.dataProperyLength; for(OWLDataProperty dp : stringDPs) { // loop over frequent values Set freqValues = frequentDataValues.get(dp); for(OWLLiteral lit : freqValues) { - m3.add(df.getOWLDataHasValue(dp, lit)); + mA.get(nc).get(lc).add(df.getOWLDataHasValue(dp, lit)); } } } - mA.get(nc).put(3,m3); - - SortedSet m4 = new TreeSet(); if(useCardinalityRestrictions) { + int lc = lengthMetric.objectCardinalityLength + lengthMetric.objectProperyLength + lengthMetric.classLength; for(OWLObjectProperty r : mgr.get(nc)) { int maxFillers = maxNrOfFillers.get(r); // zero fillers: <= -1 r.C does not make sense // one filler: <= 0 r.C is equivalent to NOT EXISTS r.C, // but we still keep it, because ALL r.NOT C may be difficult to reach if((useNegation && maxFillers > 0) || (!useNegation && maxFillers > 1)) - m4.add(df.getOWLObjectMaxCardinality(maxFillers-1, r, df.getOWLThing())); + mA.get(nc).get(lc).add(df.getOWLObjectMaxCardinality(maxFillers-1, r, df.getOWLThing())); } } - mA.get(nc).put(4,m4); // System.out.println("m for " + nc + ": " + mA.get(nc)); @@ -1473,11 +1406,11 @@ public SortedSet getClassCandidates(OWLClassExpression index } private SortedSet getClassCandidatesRecursive(OWLClassExpression index, OWLClassExpression upperClass) { - SortedSet candidates = new TreeSet(); + SortedSet candidates = new TreeSet<>(); SortedSet subClasses = subHierarchy.getSubClasses(upperClass, true); - if(reasoner.getClass().isAssignableFrom(SPARQLReasoner.class)) { + if(reasoner instanceof SPARQLReasoner) { OWLClassExpressionToSPARQLConverter conv = new OWLClassExpressionToSPARQLConverter(); String query = "SELECT DISTINCT ?concept WHERE {"; query += conv.convert("?ind", index); @@ -1490,7 +1423,7 @@ private SortedSet getClassCandidatesRecursive(OWLClassExpres query += "}"; // System.out.println(query); - SortedSet meaningfulClasses = new TreeSet(); + SortedSet meaningfulClasses = new TreeSet<>(); QueryExecution qe = ((SPARQLReasoner)reasoner).getQueryExecutionFactory().createQueryExecution(query); ResultSet rs = qe.execSelect(); while(rs.hasNext()) { @@ -1551,16 +1484,17 @@ private SortedSet getClassCandidatesRecursive(OWLClassExpres // get candidates for a refinement of \top restricted to a class B public SortedSet getNegClassCandidates(OWLClassExpression index) { - return getNegClassCandidatesRecursive(index, df.getOWLNothing()); + return getNegClassCandidatesRecursive(index, df.getOWLNothing(), null); } - private SortedSet getNegClassCandidatesRecursive(OWLClassExpression index, OWLClassExpression lowerClass) { - SortedSet candidates = new TreeSet(); + private SortedSet getNegClassCandidatesRecursive(OWLClassExpression index, OWLClassExpression lowerClass, Set seenClasses) { + if (seenClasses == null) { seenClasses = new TreeSet<>(); } + SortedSet candidates = new TreeSet<>(); // System.out.println("index " + index + " lower class " + lowerClass); SortedSet superClasses = subHierarchy.getSuperClasses(lowerClass); - if(reasoner.getClass().isAssignableFrom(SPARQLReasoner.class)) { + if(reasoner instanceof SPARQLReasoner) { OWLClassExpressionToSPARQLConverter conv = new OWLClassExpressionToSPARQLConverter(); String query = "SELECT DISTINCT ?concept WHERE {"; query += conv.convert("?ind", index); @@ -1572,7 +1506,7 @@ private SortedSet getNegClassCandidatesRecursive(OWLClassExp + "}"; query += "}"; // System.out.println(query); - SortedSet meaningfulClasses = new TreeSet(); + SortedSet meaningfulClasses = new TreeSet<>(); QueryExecution qe = ((SPARQLReasoner)reasoner).getQueryExecutionFactory().createQueryExecution(query); ResultSet rs = qe.execSelect(); while(rs.hasNext()) { @@ -1604,8 +1538,9 @@ private SortedSet getNegClassCandidatesRecursive(OWLClassExp if(meaningful) { candidates.add(negatedCandidate); - } else { - candidates.addAll(getNegClassCandidatesRecursive(index, candidate)); + } else if (!seenClasses.contains(candidate)) { + seenClasses.add(candidate); + candidates.addAll(getNegClassCandidatesRecursive(index, candidate, seenClasses)); } } } @@ -1625,15 +1560,16 @@ private void computeMg(OWLClassExpression domain) { mgbd.put(domain, new TreeSet()); mgNumeric.put(domain, new TreeSet()); mgsd.put(domain, new TreeSet()); + mgDT.put(domain, new TreeSet()); SortedSet mostGeneral = objectPropertyHierarchy.getMostGeneralRoles(); computeMgrRecursive(domain, mostGeneral, mgr.get(domain)); SortedSet mostGeneralDP = dataPropertyHierarchy.getMostGeneralRoles(); // we make the (reasonable) assumption here that all sub and super // datatype properties have the same type (e.g. boolean, integer, double) - Set mostGeneralBDP = Helper.intersection(mostGeneralDP, reasoner.getBooleanDatatypeProperties()); - Set mostGeneralNumericDPs = Helper.intersection(mostGeneralDP, reasoner.getNumericDataProperties()); - Set mostGeneralStringDPs = Helper.intersection(mostGeneralDP, reasoner.getStringDatatypeProperties()); + Set mostGeneralBDP = Sets.intersection(mostGeneralDP, reasoner.getBooleanDatatypeProperties()); + Set mostGeneralNumericDPs = Sets.intersection(mostGeneralDP, reasoner.getNumericDataProperties()); + Set mostGeneralStringDPs = Sets.intersection(mostGeneralDP, reasoner.getStringDatatypeProperties()); computeMgbdRecursive(domain, mostGeneralBDP, mgbd.get(domain)); computeMostGeneralNumericDPRecursive(domain, mostGeneralNumericDPs, mgNumeric.get(domain)); computeMostGeneralStringDPRecursive(domain, mostGeneralStringDPs, mgsd.get(domain)); @@ -1680,13 +1616,13 @@ private void computeApp(OWLClassExpression domain) { SortedSet individuals1 = reasoner.getIndividuals(domain); // object properties Set mostGeneral = objectPropertyHierarchy.getMostGeneralRoles(); - Set applicableRoles = new TreeSet(); + Set applicableRoles = new TreeSet<>(); for(OWLObjectProperty role : mostGeneral) { // TODO: currently we just rely on named classes as roles, // instead of computing dom(r) and ran(r) OWLClassExpression d = opDomains.get(role); - Set individuals2 = new HashSet(); + Set individuals2 = new HashSet<>(); for (Entry> entry : reasoner.getPropertyMembers(role).entrySet()) { OWLIndividual ind = entry.getKey(); if(!entry.getValue().isEmpty()){ @@ -1706,7 +1642,7 @@ private void computeApp(OWLClassExpression domain) { // boolean datatype properties Set mostGeneralBDPs = reasoner.getBooleanDatatypeProperties(); - Set applicableBDPs = new TreeSet(); + Set applicableBDPs = new TreeSet<>(); for(OWLDataProperty role : mostGeneralBDPs) { // Description d = (OWLClass) rs.getDomain(role); OWLClassExpression d = dpDomains.get(role); @@ -1717,7 +1653,7 @@ private void computeApp(OWLClassExpression domain) { // numeric data properties Set mostGeneralNumericDPs = reasoner.getNumericDataProperties(); - Set applicableNumericDPs = new TreeSet(); + Set applicableNumericDPs = new TreeSet<>(); for(OWLDataProperty role : mostGeneralNumericDPs) { // get domain of property OWLClassExpression d = dpDomains.get(role); @@ -1729,7 +1665,7 @@ private void computeApp(OWLClassExpression domain) { // string datatype properties Set mostGeneralSDPs = reasoner.getStringDatatypeProperties(); - Set applicableSDPs = new TreeSet(); + Set applicableSDPs = new TreeSet<>(); for(OWLDataProperty role : mostGeneralSDPs) { // Description d = (OWLClass) rs.getDomain(role); OWLClassExpression d = dpDomains.get(role); @@ -1790,12 +1726,10 @@ private boolean isDisjoint(OWLClassExpression d1, OWLClassExpression d2) { // the order of access does not matter) // create new entries if necessary - Map map1 = new TreeMap(); - Map map2 = new TreeMap(); if (tmp == null) - cachedDisjoints.put(d1, map1); + cachedDisjoints.put(d1, new TreeMap()); if (!cachedDisjoints.containsKey(d2)) - cachedDisjoints.put(d2, map2); + cachedDisjoints.put(d2, new TreeMap()); // add result symmetrically in the OWLClassExpression matrix cachedDisjoints.get(d1).put(d2, result); @@ -1805,7 +1739,7 @@ private boolean isDisjoint(OWLClassExpression d1, OWLClassExpression d2) { } private boolean isDisjointInstanceBased(OWLClassExpression d1, OWLClassExpression d2) { - if(reasoner.getClass().isAssignableFrom(SPARQLReasoner.class)) { + if(reasoner instanceof SPARQLReasoner) { SortedSet individuals = reasoner.getIndividuals(df.getOWLObjectIntersectionOf(d1, d2)); return individuals.isEmpty(); } else { @@ -2053,4 +1987,25 @@ public void setMaxNrOfSplits(int maxNrOfSplits) { this.maxNrOfSplits = maxNrOfSplits; } -} \ No newline at end of file + public OWLClassExpressionLengthMetric getLengthMetric() { + return lengthMetric; + } + + public void setLengthMetric(OWLClassExpressionLengthMetric lengthMetric) { + this.lengthMetric = lengthMetric; + + mMaxLength = max ( + lengthMetric.classLength, + lengthMetric.objectComplementLength + lengthMetric.classLength, + lengthMetric.objectSomeValuesLength + lengthMetric.objectProperyLength + lengthMetric.classLength, + lengthMetric.objectSomeValuesLength + lengthMetric.objectProperyLength + lengthMetric.classLength + lengthMetric.objectInverseLength, + lengthMetric.objectAllValuesLength + lengthMetric.objectProperyLength + lengthMetric.classLength, + lengthMetric.objectAllValuesLength + lengthMetric.objectProperyLength + lengthMetric.classLength + lengthMetric.objectInverseLength, + lengthMetric.dataHasValueLength + lengthMetric.dataProperyLength, + lengthMetric.dataSomeValuesLength + lengthMetric.dataProperyLength + 1, + lengthMetric.objectCardinalityLength + lengthMetric.objectProperyLength + lengthMetric.classLength); + + logger.debug("mMaxLength = " + mMaxLength); + } + +} diff --git a/components-core/src/main/java/org/dllearner/refinementoperators/SynchronizedRefinementOperator.java b/components-core/src/main/java/org/dllearner/refinementoperators/SynchronizedRefinementOperator.java new file mode 100644 index 0000000000..8617a15977 --- /dev/null +++ b/components-core/src/main/java/org/dllearner/refinementoperators/SynchronizedRefinementOperator.java @@ -0,0 +1,107 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.refinementoperators; + +import org.dllearner.core.ComponentInitException; +import org.dllearner.utilities.owl.OWLClassExpressionLengthMetric; +import org.semanticweb.owlapi.model.OWLClassExpression; + +import java.util.List; +import java.util.Set; + +/** + * A wrapper class that makes the call of the refinement methods synchronized, i.e. + * it's supposed to provide a thread-safe implementation. + * + * TODO This only works if the used datastructures do not need additional + * synchronization. + * @author Lorenz Buehmann + * + */ +// not for conf +public class SynchronizedRefinementOperator implements LengthLimitedRefinementOperator{ + + private final LengthLimitedRefinementOperator delegate; + + public SynchronizedRefinementOperator(LengthLimitedRefinementOperator delegate) { + this.delegate = delegate; + } + + /* (non-Javadoc) + * @see org.dllearner.core.Component#init() + */ + @Override + public void init() throws ComponentInitException { + synchronized (delegate) { + delegate.init(); + } + } + + /* (non-Javadoc) + * @see org.dllearner.refinementoperators.RefinementOperator#refine(org.semanticweb.owlapi.model.OWLClassExpression) + */ + @Override + public Set refine(OWLClassExpression description) { + synchronized (delegate) { + return delegate.refine(description); + } + } + + /* (non-Javadoc) + * @see org.dllearner.refinementoperators.LengthLimitedRefinementOperator#refine(org.semanticweb.owlapi.model.OWLClassExpression, int) + */ + @Override + public Set refine(OWLClassExpression description, int maxLength) { + synchronized (delegate) { + return delegate.refine(description, maxLength); + } + } + + /* (non-Javadoc) + * @see org.dllearner.refinementoperators.LengthLimitedRefinementOperator#refine(org.semanticweb.owlapi.model.OWLClassExpression, int, java.util.List) + */ + @Override + public Set refine(OWLClassExpression description, int maxLength, + List knownRefinements) { + synchronized (delegate) { + return delegate.refine(description, maxLength, knownRefinements); + } + } + + @Override + public void setLengthMetric(OWLClassExpressionLengthMetric lengthMetric) { + synchronized (delegate) { + delegate.setLengthMetric(lengthMetric); + } + } + + public OWLClassExpressionLengthMetric getLengthMetric() { + synchronized (delegate) { + return delegate.getLengthMetric(); + } + } + + /** + * @return the wrapped refinement operator + */ + public LengthLimitedRefinementOperator getDelegate() { + return delegate; + } + +} diff --git a/components-core/src/main/java/org/dllearner/refinementoperators/Utility.java b/components-core/src/main/java/org/dllearner/refinementoperators/Utility.java index c078e7f81d..db3f556c66 100644 --- a/components-core/src/main/java/org/dllearner/refinementoperators/Utility.java +++ b/components-core/src/main/java/org/dllearner/refinementoperators/Utility.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,9 +16,9 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.refinementoperators; +import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.SortedSet; @@ -27,7 +27,6 @@ import org.dllearner.core.AbstractReasonerComponent; import org.dllearner.core.owl.ClassHierarchy; -import org.dllearner.utilities.Helper; import org.semanticweb.owlapi.model.OWLClass; import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLDataFactory; @@ -37,6 +36,7 @@ import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; +import com.google.common.collect.Sets; import com.jamonapi.Monitor; import com.jamonapi.MonitorFactory; @@ -49,18 +49,18 @@ public final class Utility { private AbstractReasonerComponent reasoner; - ClassHierarchy sh; + ClassHierarchy sh; // specifies whether to do real disjoint tests or check that // two named classes do not have common instances - private boolean instanceBasedDisjoints = true; + private boolean instanceBasedDisjoints = true; // cache for reasoner queries - private Map> cachedDisjoints = new TreeMap>(); + private Map> cachedDisjoints = new TreeMap<>(); // cache for applicaple object properties - private Map> appOPCache = new TreeMap>(); - private Map> appDPCache = new TreeMap>(); + private Map> appOPCache = new TreeMap<>(); + private Map> appDPCache = new TreeMap<>(); private Map opDomains; private Map dpDomains; @@ -88,12 +88,12 @@ public Utility(AbstractReasonerComponent rs, Map computeApplicableObjectProperties(OWLClassEx SortedSet applicableObjectProperties = appOPCache.get(index); if(applicableObjectProperties == null) { Set objectProperties = reasoner.getObjectProperties(); - applicableObjectProperties = new TreeSet(); + applicableObjectProperties = new TreeSet<>(); for(OWLObjectProperty op : objectProperties) { OWLClassExpression domain = opDomains.get(op); if(!isDisjoint(index,domain)) { @@ -111,16 +111,16 @@ public SortedSet computeApplicableObjectProperties(OWLClassEx } appOPCache.put(index, applicableObjectProperties); } - return applicableObjectProperties; + return applicableObjectProperties; } /** - * Compute the set of applicable data properties for a - * given description. + * Compute the set of applicable data properties for a + * given description. * * @param index The index is a OWLClassExpression which determines * which of the properties are applicable. Exactly those which - * where the index and property domain are not disjoint are + * where the index and property domain are not disjoint are * applicable, where disjoint is defined by {@link #isDisjoint(OWLClassExpression, OWLClassExpression)}. * */ @@ -129,7 +129,7 @@ public SortedSet computeApplicableDatatypeProperties(OWLClassEx SortedSet applicableDatatypeProperties = appDPCache.get(index); if(applicableDatatypeProperties == null) { Set datatypeProperties = reasoner.getDatatypeProperties(); - applicableDatatypeProperties = new TreeSet(); + applicableDatatypeProperties = new TreeSet<>(); for(OWLDataProperty op : datatypeProperties) { OWLClassExpression domain = dpDomains.get(op); if(!isDisjoint(index,domain)) { @@ -138,7 +138,7 @@ public SortedSet computeApplicableDatatypeProperties(OWLClassEx } appDPCache.put(index, applicableDatatypeProperties); } - return applicableDatatypeProperties; + return applicableDatatypeProperties; } /** @@ -154,7 +154,7 @@ public SortedSet computeApplicableDatatypeProperties(OWLClassEx * @return The most general applicable properties. */ public Set computeMgr(Set applicableObjectProperties) { - return Helper.intersection(reasoner.getMostGeneralProperties(), applicableObjectProperties); + return new HashSet<>(Sets.intersection(reasoner.getMostGeneralProperties(), applicableObjectProperties)); } /** @@ -170,7 +170,7 @@ public Set computeMgr(Set applicableObject * @return The most general applicable properties. */ public Set computeMgrDP(Set applicableDatatypeProperties) { - return Helper.intersection(reasoner.getMostGeneralDatatypeProperties(), applicableDatatypeProperties); + return new HashSet<>(Sets.intersection(reasoner.getMostGeneralDatatypeProperties(), applicableDatatypeProperties)); } public Set getClassCandidates(OWLClassExpression index, Set existingClasses) { @@ -178,7 +178,7 @@ public Set getClassCandidates(OWLClassExpression index, Set } private Set getClassCandidatesRecursive(OWLClassExpression index, Set existingClasses, OWLClassExpression upperClass) { - Set candidates = new TreeSet(); + Set candidates = new TreeSet<>(); // we descend the subsumption hierarchy to ensure that we get // the most general concepts satisfying the criteria @@ -234,7 +234,7 @@ private boolean checkSuperClasses(Set existingClasses, OWLClass candid return false; } return true; - } + } // returns false if any of the classes is disjoint with the new one; true otherwise private boolean checkDisjoints(Set existingClasses, OWLClass candidate) { @@ -243,7 +243,7 @@ private boolean checkDisjoints(Set existingClasses, OWLClass candidate return false; } return true; - } + } public boolean isDisjoint(OWLClassExpression d1, OWLClassExpression d2) { @@ -264,19 +264,17 @@ public boolean isDisjoint(OWLClassExpression d1, OWLClassExpression d2) { } else { OWLClassExpression d = df.getOWLObjectIntersectionOf(d1, d2); Monitor mon = MonitorFactory.start("disjointness reasoning"); - result = reasoner.isSuperClassOf(df.getOWLNothing(), d); + result = reasoner.isSuperClassOf(df.getOWLNothing(), d); mon.stop(); } // add the result to the cache (we add it twice such that // the order of access does not matter) // create new entries if necessary - Map map1 = new TreeMap(); - Map map2 = new TreeMap(); if(tmp == null) - cachedDisjoints.put(d1, map1); + cachedDisjoints.put(d1, new TreeMap()); if(!cachedDisjoints.containsKey(d2)) - cachedDisjoints.put(d2, map2); + cachedDisjoints.put(d2, new TreeMap()); // add result symmetrically in the OWLClassExpression matrix cachedDisjoints.get(d1).put(d2, result); @@ -285,7 +283,7 @@ public boolean isDisjoint(OWLClassExpression d1, OWLClassExpression d2) { } else { return tmp2; } - } + } private boolean isDisjointInstanceBased(OWLClassExpression d1, OWLClassExpression d2) { SortedSet d1Instances = reasoner.getIndividuals(d1); diff --git a/components-core/src/main/java/org/dllearner/utilities/CSVFileToArray.java b/components-core/src/main/java/org/dllearner/utilities/CSVFileToArray.java deleted file mode 100644 index 542be6b899..0000000000 --- a/components-core/src/main/java/org/dllearner/utilities/CSVFileToArray.java +++ /dev/null @@ -1,73 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.utilities; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.util.ArrayList; -import java.util.StringTokenizer; - - -/** - * @author Jens Lehmann - * - */ -public class CSVFileToArray { - public String filename; - BufferedReader br; - String separator = " "; - - public CSVFileToArray(String filename, String separator)throws FileNotFoundException, IOException{ - this(filename); - this.separator = separator; - - - } - - public CSVFileToArray(String filename)throws FileNotFoundException, IOException{ - File file = new File(filename); - this.br = new BufferedReader(new FileReader(file)); - - } - - - public ArrayList next()throws IOException{ - String line = this.br.readLine(); - if(line == null) { - br.close(); - return null;} - ArrayList a = new ArrayList(); - StringTokenizer st = new StringTokenizer(line,this.separator); - while(st.hasMoreElements()){ - a.add(st.nextToken()); - - } - return a; - - } - - - - - -} diff --git a/components-core/src/main/java/org/dllearner/utilities/CoverageAdapter.java b/components-core/src/main/java/org/dllearner/utilities/CoverageAdapter.java new file mode 100644 index 0000000000..c48b7cf52e --- /dev/null +++ b/components-core/src/main/java/org/dllearner/utilities/CoverageAdapter.java @@ -0,0 +1,46 @@ +package org.dllearner.utilities; + +/** + * Created by Simon Bin on 16-1-26. + */ +public class CoverageAdapter { + public static class CoverageAdapter2 { + private final ReasoningUtils.CoverageCount[] cc; + + public CoverageAdapter2(ReasoningUtils.CoverageCount[] cc) { + this.cc = cc; + } + + public int tp() { + return cc[0].trueCount; + } + + public void setTp(int tp) { + cc[0].trueCount = tp; + } + + public int fn() { + return cc[0].falseCount; + } + + public void setFn(int fn) { + cc[0].falseCount = fn; + } + + public int fp() { + return cc[1].trueCount; + } + + public void setFp(int fp) { + cc[1].trueCount = fp; + } + + public int tn() { + return cc[1].falseCount; + } + + public void setTn(int tn) { + cc[1].falseCount = tn; + } + } +} diff --git a/components-core/src/main/java/org/dllearner/utilities/EnrichmentVocabulary.java b/components-core/src/main/java/org/dllearner/utilities/EnrichmentVocabulary.java index 0843f832da..48216d2bc5 100644 --- a/components-core/src/main/java/org/dllearner/utilities/EnrichmentVocabulary.java +++ b/components-core/src/main/java/org/dllearner/utilities/EnrichmentVocabulary.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities; import org.semanticweb.owlapi.model.IRI; diff --git a/components-core/src/main/java/org/dllearner/utilities/Files.java b/components-core/src/main/java/org/dllearner/utilities/Files.java index 2f4dbe04cf..0fb8a6a61b 100644 --- a/components-core/src/main/java/org/dllearner/utilities/Files.java +++ b/components-core/src/main/java/org/dllearner/utilities/Files.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities; import java.io.BufferedInputStream; @@ -31,7 +30,6 @@ import java.io.InputStreamReader; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; -import java.io.OutputStream; import java.net.URL; import java.util.ArrayList; import java.util.List; @@ -62,11 +60,11 @@ public class Files { public static String readFile(URL file) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(file.openStream())); - StringBuffer input = new StringBuffer(); + StringBuilder input = new StringBuilder(); String inputLine; while ((inputLine = in.readLine()) != null) { - input.append(inputLine + "\n"); - } + input.append(inputLine).append("\n"); + } in.close(); return input.toString(); @@ -79,18 +77,17 @@ public static String readFile(URL file) throws IOException { * The file to read. * @return Content of the file. */ - public static String readFile(File file) throws FileNotFoundException, IOException { - - BufferedReader br = new BufferedReader(new FileReader(file)); - StringBuffer content = new StringBuffer(); - try{ - String line; - - while ((line = br.readLine()) != null) { - content.append(line); - content.append(System.getProperty("line.separator")); + public static String readFile(File file) throws IOException { + + StringBuilder content = new StringBuilder(); + try (BufferedReader br = new BufferedReader(new FileReader(file))) { + String line; + + while ((line = br.readLine()) != null) { + content.append(line); + content.append(System.getProperty("line.separator")); + } } - }finally{br.close();} return content.toString(); } @@ -102,12 +99,12 @@ public static String readFile(File file) throws FileNotFoundException, IOExcepti * The file to read. * @return StringArray with lines */ - public static String[] readFileAsArray(File file) throws FileNotFoundException, IOException { + public static String[] readFileAsArray(File file) throws IOException { String content = readFile(file); StringTokenizer st = new StringTokenizer(content, System.getProperty("line.separator")); - List l = new ArrayList(); + List l = new ArrayList<>(); while (st.hasMoreTokens()) { - l.add((String) st.nextToken()); + l.add(st.nextToken()); } @@ -117,8 +114,8 @@ public static String[] readFileAsArray(File file) throws FileNotFoundException, /** * writes a serializable Object to a File. - * @param obj - * @param file + * @param obj the object + * @param file the file */ public static void writeObjectToFile(Object obj, File file){ @@ -230,83 +227,9 @@ public static void mkdir(String dir){ e.printStackTrace(); if(debug){System.exit(0);} // this should not be a show stopper - } - } - } - - /** - * deletes all Files in the dir, does not delete the dir itself - * no warning is issued, use with care, cannot undelete files - * - * @param dir without a separator e.g. tmp/dirtodelete - */ - public static void deleteDir(String dir) { - - File f = new File(dir); - - if(debug){ - System.out.println(dir); - System.exit(0); - } - - String[] files = f.list(); - - for (int i = 0; i < files.length; i++) { - - Files.deleteFile(new File(dir+File.separator+files[i])); - } - } - - /** - * lists all files in a directory - * - * - * @param dir without a separator e.g. tmp/dir - * @return a string array with filenames - */ - public static String[] listDir(String dir) { - - File f = new File(dir); - - if(debug){ - System.out.println(dir); - System.exit(0); } - - return f.list(); - - - } - - /** - * copies all files in dir to "tmp/"+System.currentTimeMillis() - * @param dir the dir to be backupped - */ - public static void backupDirectory(String dir){ - File f = new File(dir); - String backupDir = "../tmp/"+System.currentTimeMillis(); - mkdir("../tmp"); - mkdir(backupDir); - - if(debug){ - System.out.println(dir); - System.exit(0); } - - String[] files = f.list(); - try{ - for (int i = 0; i < files.length; i++) { - File target = new File(dir+File.separator+files[i]); - if(!target.isDirectory()){ - String s = readFile(target); - createFile(new File(backupDir+File.separator+files[i]), s); - } - } - }catch (Exception e) { - e.printStackTrace(); } - } - } diff --git a/components-core/src/main/java/org/dllearner/utilities/FuzzyOwl2.java b/components-core/src/main/java/org/dllearner/utilities/FuzzyOwl2.java index b0aab97beb..8c44c27a04 100644 --- a/components-core/src/main/java/org/dllearner/utilities/FuzzyOwl2.java +++ b/components-core/src/main/java/org/dllearner/utilities/FuzzyOwl2.java @@ -1,126 +1,35 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.utilities; -import java.io.File; -import java.io.FileOutputStream; -import java.io.PrintStream; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; -import java.util.Hashtable; -import java.util.Iterator; -import java.util.List; -import java.util.Set; - +import fuzzyowl2.*; +import fuzzyowl2.parser.Parser; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.model.AxiomType; -import org.semanticweb.owlapi.model.DataRangeType; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAnnotation; -import org.semanticweb.owlapi.model.OWLAnnotationProperty; -import org.semanticweb.owlapi.model.OWLAnnotationValue; -import org.semanticweb.owlapi.model.OWLAsymmetricObjectPropertyAxiom; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLClassAssertionAxiom; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLDataAllValuesFrom; -import org.semanticweb.owlapi.model.OWLDataCardinalityRestriction; -import org.semanticweb.owlapi.model.OWLDataExactCardinality; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLDataHasValue; -import org.semanticweb.owlapi.model.OWLDataIntersectionOf; -import org.semanticweb.owlapi.model.OWLDataMaxCardinality; -import org.semanticweb.owlapi.model.OWLDataMinCardinality; -import org.semanticweb.owlapi.model.OWLDataProperty; -import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom; -import org.semanticweb.owlapi.model.OWLDataPropertyDomainAxiom; -import org.semanticweb.owlapi.model.OWLDataPropertyExpression; -import org.semanticweb.owlapi.model.OWLDataPropertyRangeAxiom; -import org.semanticweb.owlapi.model.OWLDataRange; -import org.semanticweb.owlapi.model.OWLDataSomeValuesFrom; -import org.semanticweb.owlapi.model.OWLDatatype; -import org.semanticweb.owlapi.model.OWLDatatypeDefinitionAxiom; -import org.semanticweb.owlapi.model.OWLDatatypeRestriction; -import org.semanticweb.owlapi.model.OWLDeclarationAxiom; -import org.semanticweb.owlapi.model.OWLDifferentIndividualsAxiom; -import org.semanticweb.owlapi.model.OWLDisjointClassesAxiom; -import org.semanticweb.owlapi.model.OWLDisjointDataPropertiesAxiom; -import org.semanticweb.owlapi.model.OWLDisjointObjectPropertiesAxiom; -import org.semanticweb.owlapi.model.OWLDisjointUnionAxiom; -import org.semanticweb.owlapi.model.OWLEntity; -import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom; -import org.semanticweb.owlapi.model.OWLEquivalentDataPropertiesAxiom; -import org.semanticweb.owlapi.model.OWLEquivalentObjectPropertiesAxiom; -import org.semanticweb.owlapi.model.OWLFacetRestriction; -import org.semanticweb.owlapi.model.OWLFunctionalDataPropertyAxiom; -import org.semanticweb.owlapi.model.OWLFunctionalObjectPropertyAxiom; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLInverseFunctionalObjectPropertyAxiom; -import org.semanticweb.owlapi.model.OWLInverseObjectPropertiesAxiom; -import org.semanticweb.owlapi.model.OWLIrreflexiveObjectPropertyAxiom; -import org.semanticweb.owlapi.model.OWLLiteral; -import org.semanticweb.owlapi.model.OWLNegativeDataPropertyAssertionAxiom; -import org.semanticweb.owlapi.model.OWLNegativeObjectPropertyAssertionAxiom; -import org.semanticweb.owlapi.model.OWLObjectAllValuesFrom; -import org.semanticweb.owlapi.model.OWLObjectCardinalityRestriction; -import org.semanticweb.owlapi.model.OWLObjectComplementOf; -import org.semanticweb.owlapi.model.OWLObjectExactCardinality; -import org.semanticweb.owlapi.model.OWLObjectHasSelf; -import org.semanticweb.owlapi.model.OWLObjectHasValue; -import org.semanticweb.owlapi.model.OWLObjectIntersectionOf; -import org.semanticweb.owlapi.model.OWLObjectMaxCardinality; -import org.semanticweb.owlapi.model.OWLObjectMinCardinality; -import org.semanticweb.owlapi.model.OWLObjectOneOf; -import org.semanticweb.owlapi.model.OWLObjectProperty; -import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom; -import org.semanticweb.owlapi.model.OWLObjectPropertyDomainAxiom; -import org.semanticweb.owlapi.model.OWLObjectPropertyExpression; -import org.semanticweb.owlapi.model.OWLObjectPropertyRangeAxiom; -import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom; -import org.semanticweb.owlapi.model.OWLObjectUnionOf; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyCreationException; -import org.semanticweb.owlapi.model.OWLOntologyManager; -import org.semanticweb.owlapi.model.OWLProperty; -import org.semanticweb.owlapi.model.OWLReflexiveObjectPropertyAxiom; -import org.semanticweb.owlapi.model.OWLSameIndividualAxiom; -import org.semanticweb.owlapi.model.OWLSubClassOfAxiom; -import org.semanticweb.owlapi.model.OWLSubDataPropertyOfAxiom; -import org.semanticweb.owlapi.model.OWLSubObjectPropertyOfAxiom; -import org.semanticweb.owlapi.model.OWLSubPropertyChainOfAxiom; -import org.semanticweb.owlapi.model.OWLSymmetricObjectPropertyAxiom; -import org.semanticweb.owlapi.model.OWLTransitiveObjectPropertyAxiom; +import org.semanticweb.owlapi.model.*; +import org.semanticweb.owlapi.search.EntitySearcher; import org.semanticweb.owlapi.util.SimpleShortFormProvider; import org.semanticweb.owlapi.vocab.OWLFacet; -import fuzzyowl2.ChoquetConcept; -import fuzzyowl2.ConceptDefinition; -import fuzzyowl2.FuzzyConcept; -import fuzzyowl2.FuzzyDatatype; -import fuzzyowl2.FuzzyLogic; -import fuzzyowl2.FuzzyModifier; -import fuzzyowl2.FuzzyNominalConcept; -import fuzzyowl2.FuzzyProperty; -import fuzzyowl2.LeftShoulderFunction; -import fuzzyowl2.LinearFunction; -import fuzzyowl2.LinearModifier; -import fuzzyowl2.ModifiedConcept; -import fuzzyowl2.ModifiedFunction; -import fuzzyowl2.ModifiedProperty; -import fuzzyowl2.OwaConcept; -import fuzzyowl2.PropertyDefinition; -import fuzzyowl2.QowaConcept; -import fuzzyowl2.QuasiSugenoConcept; -import fuzzyowl2.RightShoulderFunction; -import fuzzyowl2.SugenoConcept; -import fuzzyowl2.TrapezoidalFunction; -import fuzzyowl2.TriangularFunction; -import fuzzyowl2.TriangularModifier; -import fuzzyowl2.WeightedConcept; -import fuzzyowl2.WeightedMaxConcept; -import fuzzyowl2.WeightedMinConcept; -import fuzzyowl2.WeightedSumConcept; -import fuzzyowl2.parser.Parser; +import java.io.File; +import java.io.FileOutputStream; +import java.io.PrintStream; +import java.util.*; /** @@ -254,15 +163,15 @@ public class FuzzyOwl2 */ public FuzzyOwl2(String input, String output) { - definedConcepts = new Hashtable (); - definedProperties = new Hashtable (); - fuzzyDatatypes = new Hashtable (); - fuzzyModifiers = new Hashtable (); + definedConcepts = new Hashtable<>(); + definedProperties = new Hashtable<>(); + fuzzyDatatypes = new Hashtable<>(); + fuzzyModifiers = new Hashtable<>(); manager = OWLManager.createOWLOntologyManager(); ontologyPath = input; loadOntology(ontologyPath); - ontologies = new HashSet(); + ontologies = new HashSet<>(); ontologies.add(ontology); // Imported ontologies @@ -504,7 +413,7 @@ protected void processDatatypeAnnotations() if (ent.isOWLDatatype()) { OWLDatatype dt = ent.asOWLDatatype(); - Collection annotations = dt.getAnnotations(o, label); + Collection annotations = EntitySearcher.getAnnotations(dt, o, label); if (annotations != null) { if (annotations.size() > 1) @@ -527,7 +436,7 @@ else if (annotations.size() == 1) if (ent.isOWLDatatype()) { OWLDatatype dt = ent.asOWLDatatype(); - Collection annotations = dt.getAnnotations(o, label); + Collection annotations = EntitySearcher.getAnnotations(dt, o, label); if (annotations != null) { if (annotations.size() == 1) @@ -548,7 +457,7 @@ else if (annotations.size() == 1) if (ent.isOWLDatatype()) { OWLDatatype dt = ent.asOWLDatatype(); - Collection annotations = dt.getAnnotations(o, label); + Collection annotations = EntitySearcher.getAnnotations(dt, o, label); if (annotations != null) { if (annotations.size() == 1) @@ -579,7 +488,7 @@ protected void processConceptAnnotations() if (ent.isOWLClass()) { OWLClass cls = ent.asOWLClass(); - Collection annotations = cls.getAnnotations(o, label); + Collection annotations = EntitySearcher.getAnnotations(cls, o, label); if (annotations.size() > 1) exit("Error: There are " + annotations.size() + " class annotations for " + cls + "."); @@ -618,7 +527,7 @@ else if (annotations.size() == 1) case WEIGHTED_MAX: List sourceList = c.getWeightedConcepts(); - ArrayList list = new ArrayList(); + ArrayList list = new ArrayList<>(); for(ConceptDefinition def : sourceList) list.add(new WeightedConcept(def.getNumber(), def.getFuzzyConcept() ) ); WeightedMaxConcept wmax = new WeightedMaxConcept(list); @@ -628,7 +537,7 @@ else if (annotations.size() == 1) case WEIGHTED_MIN: sourceList = c.getWeightedConcepts(); - list = new ArrayList(); + list = new ArrayList<>(); for(ConceptDefinition def : sourceList) list.add(new WeightedConcept(def.getNumber(), def.getFuzzyConcept() ) ); WeightedMinConcept wmin = new WeightedMinConcept(list); @@ -638,7 +547,7 @@ else if (annotations.size() == 1) case WEIGHTED_SUM: sourceList = c.getWeightedConcepts(); - list = new ArrayList(); + list = new ArrayList<>(); for(ConceptDefinition def : sourceList) list.add(new WeightedConcept(def.getNumber(), def.getFuzzyConcept() ) ); WeightedSumConcept wsum = new WeightedSumConcept(list); @@ -743,7 +652,7 @@ protected void processPropertyAnnotations() else // if (ent.isOWLDataProperty() ) prop = ent.asOWLDataProperty(); - Collection annotations = prop.getAnnotations(o, label); + Collection annotations = EntitySearcher.getAnnotations(prop, o, label); if (annotations.size() > 1) exit("Error: There are " + annotations.size() + " property annotations for " + prop + "."); @@ -1103,14 +1012,14 @@ else if (d.isOWLNothing()) case OBJECT_HAS_VALUE: OWLObjectHasValue hasValue = (OWLObjectHasValue) c; - OWLIndividual i = hasValue.getValue(); + OWLIndividual i = hasValue.getFiller(); p = hasValue.getProperty(); return getObjectHasValueName(p, i); case DATA_HAS_VALUE: OWLDataHasValue dataHasValue = (OWLDataHasValue) c; - OWLLiteral lit = dataHasValue.getValue(); + OWLLiteral lit = dataHasValue.getFiller(); dp = dataHasValue.getProperty(); return getDataHasValueName(dp, lit); diff --git a/components-core/src/main/java/org/dllearner/utilities/FuzzyOwl2toFuzzyDL.java b/components-core/src/main/java/org/dllearner/utilities/FuzzyOwl2toFuzzyDL.java index c42cc34b3d..9471272126 100644 --- a/components-core/src/main/java/org/dllearner/utilities/FuzzyOwl2toFuzzyDL.java +++ b/components-core/src/main/java/org/dllearner/utilities/FuzzyOwl2toFuzzyDL.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.utilities; import java.util.HashSet; @@ -68,8 +86,8 @@ public class FuzzyOwl2toFuzzyDL extends FuzzyOwl2 public FuzzyOwl2toFuzzyDL(String input, String output) { super(input, output); - numericalDatatypes = new HashSet(); - stringDatatypes = new HashSet(); + numericalDatatypes = new HashSet<>(); + stringDatatypes = new HashSet<>(); } @@ -516,7 +534,7 @@ protected void writeWeightedMaxConceptDefinition(String name, WeightedMaxConcept String s = "(define-concept " + name + " (w-max "; List list = c.getWeightedConcepts(); WeightedConcept wc = list.get(0); - s += "(" + wc.getWeight() + " " + wc.getFuzzyConcept().toString() + ")"; + s += "(" + wc.getWeight() + " " + wc.getFuzzyConcept() + ")"; for(int i=1; i list = c.getWeightedConcepts(); WeightedConcept wc = list.get(0); - s += "(" + wc.getWeight() + " " + wc.getFuzzyConcept().toString() + ")"; + s += "(" + wc.getWeight() + " " + wc.getFuzzyConcept() + ")"; for(int i=1; i list = c.getWeightedConcepts(); WeightedConcept wc = list.get(0); - s += "(" + wc.getWeight() + " " + wc.getFuzzyConcept().toString() + ")"; + s += "(" + wc.getWeight() + " " + wc.getFuzzyConcept() + ")"; for(int i=1; i list = c.getConcepts(); s += list.get(0); for(int i=1; i list = c.getConcepts(); s += list.get(0); for(int i=1; i list = c.getConcepts(); s += list.get(0); for(int i=1; i list = c.getConcepts(); s += list.get(0); for(int i=1; i list = c.getConcepts(); s += list.get(0); for(int i=1; i. */ - package org.dllearner.utilities; -import java.util.ArrayList; -import java.util.Collection; -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.TreeMap; import java.util.TreeSet; import org.dllearner.core.AbstractReasonerComponent; -import org.dllearner.core.ReasoningMethodUnsupportedException; -import org.dllearner.core.owl.FlatABox; import org.dllearner.utilities.datastructures.SortedSetTuple; import org.semanticweb.owlapi.model.EntityType; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLDataFactory; import org.semanticweb.owlapi.model.OWLEntity; import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLObjectComplementOf; import org.semanticweb.owlapi.model.OWLObjectProperty; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; +import com.google.common.collect.Sets; + /** * TODO: JavaDoc * @@ -56,88 +48,9 @@ */ public class Helper { - private static Logger logger = LoggerFactory.getLogger(Helper.class); + private static Logger logger = LoggerFactory.getLogger(Helper.class); private static final OWLDataFactory df = new OWLDataFactoryImpl(); - // findet alle atomaren Konzepte in einem Konzept - public static List getAtomicConcepts(OWLClassExpression concept) { - return new ArrayList(concept.getClassesInSignature()); - } - - // findet alle atomaren Rollen in einem Konzept - public static List getAtomicRoles(OWLClassExpression concept) { - return new ArrayList(concept.getObjectPropertiesInSignature()); - } - - // sucht, ob der übergebene String mit einem Prefix beginnt der - // versteckt werden soll und gibt diesen zurück, ansonsten wird - // null zurück gegeben - // public static String findPrefixToHide(String name) { - // for(String prefix : Config.hidePrefixes) { - // if(name.startsWith(prefix)) - // return prefix; - // } - // return null; - // } - - /** - * - * Transforms an URI to an abbreviated version, e.g. if the base URI is - * "http://example.com/" and the uri is "http://example.com/test", then - * "test" is returned. If the the uri is "http://anotherexample.com/test2" - * and a prefix "ns1" is given for "http://anotherexample.com", then - * "ns1:test2" is returned. If there is no match, uri is returned. - * - * @param uri - * The full uri, which should be transformed to an abbreviated - * version. - * @param baseURI - * The base uri (ignored if null). - * @param prefixes - * A prefix map (ignored if null), where each entry contains a - * short string e.g. ns1 as key and the corresponding uri as - * value. - * @return Abbreviated version of the parameter uri. - */ - public static String getAbbreviatedString(String uri, String baseURI, - Map prefixes) { - if (baseURI != null && uri.startsWith(baseURI)) { - return uri.substring(baseURI.length()); - } else { - if (prefixes != null) { - for (Entry prefix : prefixes.entrySet()) { - if (uri.startsWith(prefix.getValue())) - return prefix.getKey() + ":" + uri.substring(prefix.getValue().length()); - } - } - return uri; - } - } - - /** - * Transforms a list of URIs into their abbreviated version. - * @see #getAbbreviatedString(String, String, Map) - * @param list List of URIs. - * @param baseURI The base uri (ignored if null). - * @param prefixes A prefix map (ignored if null), where each entry contains a - * short string e.g. ns1 as key and the corresponding uri as - * value. - * @return A list with shortened URIs. - */ - public static String getAbbreviatedCollection(Collection list, String baseURI, - Map prefixes) { - StringBuffer str = new StringBuffer("["); - Iterator it = list.iterator(); // easier to implement using an iterator than foreach - while(it.hasNext()) { - str.append(getAbbreviatedString(it.next(),baseURI,prefixes)); - if(it.hasNext()) { - str.append(", "); - } - } - str.append("]"); - return str.toString(); - } - public static String prettyPrintNanoSeconds(long nanoSeconds) { return prettyPrintNanoSeconds(nanoSeconds, false, false); } @@ -189,104 +102,15 @@ public static String prettyPrintMilliSeconds(long milliSeconds) { return str; } - public static void addMapEntry(Map> map, T1 keyEntry, T2 setEntry) { - if (map.containsKey(keyEntry)) { - map.get(keyEntry).add(setEntry); - } else { - SortedSet newSet = new TreeSet(); - newSet.add(setEntry); - map.put(keyEntry, newSet); - } - } - - /** - * Das ist eine "generic method", d.h. die Methode hat einen bestimmten Typ. - * Ich habe das benutzt um allen beteiligten Mengen den gleichen Typ zu - * geben, denn ansonsten ist es nicht möglich der neu zu erzeugenden Menge - * (union) den gleichen Typ wie den Argumenten zu geben. - * - * Die Methode hat gegenüber addAll den Vorteil, dass sie ein neues Objekt - * erzeugt. - * - * @param - * @param set1 - * @param set2 - */ - public static Set union(Set set1, Set set2) { - // TODO: effizientere Implementierung (längere Liste klonen und Elemente - // anhängen) - Set union = new TreeSet(); - union.addAll(set1); - union.addAll(set2); - return union; - /* - * Set union; if(set1.size()>set2.size()) { union = set1.clone(); } else { - * } return union; - */ - } - - public static SortedSet union(SortedSet set1, SortedSet set2) { - // Set union = set1.clone(); - // ((Cloneable) set1).clone(); - - // TODO: effizientere Implementierung (längere Liste klonen und Elemente - // anhängen) - - // f�r TreeSet gibt es einen Konstruktor, der eine Collection - // entgegennimmt - // und einen weiteren, der ein SortedSet entgegennimmt; vermutlich ist - // letzterer schneller - - SortedSet union; - if (set1.size() > set2.size()) { - union = new TreeSet(set1); - union.addAll(set2); - } else { - union = new TreeSet(set2); - union.addAll(set1); - } - // SortedSet union = new TreeSet(set1); - // union.addAll(set1); - // union.addAll(set2); - return union; - - } - - public static Set intersection(Set set1, Set set2) { - // TreeSet intersection = (TreeSet) set1.clone(); - // TODO: effizienter implementieren d.h. lange Liste klonen und dann - // retainAll - SortedSet intersection = new TreeSet(set1); - // intersection.addAll(set1); - intersection.retainAll(set2); - return intersection; - } - public static Set intersectionTuple(Set set, SortedSetTuple tuple) { - Set ret = intersection(set, tuple.getPosSet()); + Set ret = Sets.intersection(set, tuple.getPosSet()); ret.retainAll(tuple.getNegSet()); return ret; } - - public static SortedSet difference(SortedSet set1, SortedSet set2) { - // TODO: effizienter implementieren - SortedSet difference = new TreeSet(set1); - // difference.addAll(set1); - difference.removeAll(set2); - return difference; - } - - public static Set difference(Set set1, Set set2) { - // TODO: effizienter implementieren - SortedSet difference = new TreeSet(set1); - // difference.addAll(set1); - difference.removeAll(set2); - return difference; - } // Umwandlung von Menge von Individuals auf Menge von Strings public static SortedSet getIndividualSet(Set individuals) { - SortedSet ret = new TreeSet(); + SortedSet ret = new TreeSet<>(); for (String s : individuals) { ret.add(df.getOWLNamedIndividual(IRI.create(s))); } @@ -294,18 +118,18 @@ public static SortedSet getIndividualSet(Set individuals) } public static SortedSetTuple getIndividualTuple(SortedSetTuple tuple) { - return new SortedSetTuple(getIndividualSet(tuple.getPosSet()), + return new SortedSetTuple<>(getIndividualSet(tuple.getPosSet()), getIndividualSet(tuple.getNegSet())); } public static SortedSetTuple getStringTuple(SortedSetTuple tuple) { - return new SortedSetTuple(getStringSet(tuple.getPosSet()), getStringSet(tuple + return new SortedSetTuple<>(getStringSet(tuple.getPosSet()), getStringSet(tuple .getNegSet())); } // Umwandlung von Menge von Individuals auf Menge von Strings public static SortedSet getStringSet(Set individuals) { - SortedSet ret = new TreeSet(); + SortedSet ret = new TreeSet<>(); for (OWLIndividual i : individuals) { ret.add(i.toStringID()); } @@ -314,176 +138,13 @@ public static SortedSet getStringSet(Set individuals) { public static Map> getStringMap( Map> roleMembers) { - Map> ret = new TreeMap>(); + Map> ret = new TreeMap<>(); for (OWLIndividual i : roleMembers.keySet()) { ret.put(i.toStringID(), getStringSet(roleMembers.get(i))); } return ret; } - /** - * TODO: split in two methods (one for concepts, one for roles), document - * what exactly the method is doing, remove dependencies from old Config - * class, incorporate the new methods in the learning algorithms when - * appropriate (common conf options for allowed concepts/roles and forbidden - * concepts/roles need to be created) - * - * Computes the set of allowed concepts based on configuration settings - * (also ignores anonymous and standard RDF, RDFS, OWL concept produces by - * Jena). - * - * DEPRECATED METHOD (RELIED ON OLD CONFIG). - * - */ -// public static void autoDetectConceptsAndRoles(ReasonerComponent rs) { -// // einige Sachen, die momentan nur vom Refinement-Algorithmus -// // unterstützt werden (später ev. auch von anderen Algorithmen) -// // if (Config.algorithm == Algorithm.REFINEMENT) { -// -// // berechnen der verwendbaren Konzepte -// if (Config.Refinement.allowedConceptsAutoDetect) { -// // TODO: Code aus DIG-Reasoner-Klasse einfügen -// -// Set allowedConceptsTmp = new TreeSet( -// new ConceptComparator()); -// allowedConceptsTmp.addAll(rs.getAtomicConcepts()); -// Iterator it = allowedConceptsTmp.iterator(); -// while (it.hasNext()) { -// String conceptName = it.next().toStringID(); -// // System.out.println(conceptName); -// // seltsame anon-Konzepte, die von Jena erzeugt werden -// // löschen -// if (conceptName.startsWith("anon")) { -// System.out -// .println(" Ignoring concept " -// + conceptName -// + " (probably an anonymous concept produced by Jena when reading in OWL file)."); -// it.remove(); -// } else if (conceptName.startsWith("http://www.w3.org/1999/02/22-rdf-syntax-ns#")) { -// System.out.println(" Ignoring concept " + conceptName -// + " (RDF construct produced by Jena when reading in OWL file)."); -// it.remove(); -// } else if (conceptName.startsWith("http://www.w3.org/2000/01/rdf-schema#")) { -// System.out.println(" Ignoring concept " + conceptName -// + " (RDF Schema construct produced by Jena when reading in OWL file)."); -// it.remove(); -// } else if (conceptName.startsWith("http://www.w3.org/2002/07/owl#")) { -// System.out.println(" Ignoring concept " + conceptName -// + " (OWL construct produced by Jena when reading in OWL file)."); -// it.remove(); -// } -// } -// -// // hier werden jetzt noch die zu ignorierenden Konzepte entfernt -// if (Config.Refinement.ignoredConcepts != null) { -// -// for (AtomicConcept ac : Config.Refinement.ignoredConcepts) { -// boolean success = allowedConceptsTmp.remove(ac); -// if (!success) { -// System.out.println("Ignored concept " + ac -// + " does not exist in knowledge base."); -// System.exit(0); -// } -// -// } -// } -// -// Config.Refinement.allowedConcepts = allowedConceptsTmp; -// } else { -// // prüfen, ob nur verfügbare Konzepte vom Nutzer gewählt worden -// Set allowed = new HashSet(); -// allowed.addAll(Config.Refinement.allowedConcepts); -// allowed.removeAll(rs.getAtomicConcepts()); -// if (allowed.size() > 0) { -// System.out -// .println("Some of the concepts you told the learner to use in the definition, " -// + "do not exist in the background knowledge: " + allowed); -// System.out.println("Please correct this problem and restart."); -// System.exit(0); -// } -// } -// -// if (Config.Refinement.allowedRolesAutoDetect) { -// Set allowedRolesTmp = rs.getAtomicRoles(); -// -// // hier werden jetzt noch die zu ignorierenden Rollen entfernt -// if (Config.Refinement.ignoredRoles != null) { -// -// for (AtomicRole ar : Config.Refinement.ignoredRoles) { -// boolean success = allowedRolesTmp.remove(ar); -// if (!success) { -// System.out.println("Ignored role " + ar -// + " does not exist in knowledge base."); -// System.exit(0); -// } -// -// } -// } -// -// Config.Refinement.allowedRoles = allowedRolesTmp; -// -// } else { -// Set allowedR = new HashSet(); -// allowedR.addAll(Config.Refinement.allowedRoles); -// -// Set existingR = new TreeSet(new RoleComparator()); -// existingR.addAll(rs.getAtomicRoles()); -// -// // allowedR.removeAll(rs.getAtomicRoles()); -// allowedR.removeAll(existingR); -// -// if (allowedR.size() > 0) { -// System.out -// .println("Some of the roles you told the learner to use in the definition, " -// + "do not exist in the background knowledge: " + allowedR); -// System.out.println("Please correct this problem and restart."); -// System.out.println(rs.getAtomicRoles()); -// System.out.println(Config.Refinement.allowedRoles); -// System.exit(0); -// } -// -// } -// } - - /** - * Removes concepts, which should be ignored by the learning algorithm. - * (The main reason to use this method is because Jena introduces such - * concepts when ontologies are converted to DIG.) Currently ignored - * concepts are those having prefix "anon" and concepts belonging to - * the RDF, RDFS, OWL standards. - * - * @param concepts The set from which concepts will be removed. - * @deprecated Deprecated method, because it is not needed anymore. - */ - @Deprecated - public static void removeUninterestingConcepts(Set concepts) { - Iterator it = concepts.iterator(); - while (it.hasNext()) { - String conceptName = it.next().toStringID(); - - // ignore some concepts (possibly produced by Jena) - if (conceptName.startsWith("anon")) { - logger.debug(" Ignoring concept " - + conceptName - + " (probably an anonymous concept produced by Jena when reading in OWL file)."); - it.remove(); - } else if (conceptName.startsWith("http://www.w3.org/1999/02/22-rdf-syntax-ns#")) { - logger.debug(" Ignoring concept " + conceptName - + " (RDF construct produced by Jena when reading in OWL file)."); - it.remove(); - } else if (conceptName.startsWith("http://www.w3.org/2000/01/rdf-schema#")) { - logger.debug(" Ignoring concept " + conceptName - + " (RDF Schema construct produced by Jena when reading in OWL file)."); - it.remove(); - } else if (conceptName.startsWith("http://www.w3.org/2002/07/owl#")) { - logger.debug(" Ignoring concept " + conceptName - + " (OWL construct produced by Jena when reading in OWL file)."); - it.remove(); - } - - } - } - // concepts case 1: no ignore or allowed list public static Set computeEntities(AbstractReasonerComponent rs, EntityType entityType) { // if there is no ignore or allowed list, we just ignore the concepts @@ -533,7 +194,7 @@ public static Set computeEntitiesUsingIgnoreList(Abstra // concepts case 2: ignore list public static Set computeConceptsUsingIgnoreList(AbstractReasonerComponent rs, Set ignoredConcepts) { - Set concepts = new TreeSet(rs.getClasses()); + Set concepts = new TreeSet<>(rs.getClasses()); // Helper.removeUninterestingConcepts(concepts); for (OWLClass ac : ignoredConcepts) { boolean success = concepts.remove(ac); @@ -543,33 +204,17 @@ public static Set computeConceptsUsingIgnoreList(AbstractReasonerCompo return concepts; } - // concepts case 3: allowed list - // superseeded by checkConcepts() -// public static void checkAllowedList(ReasonerComponent rs, Set allowedConcepts) { -// // check whether allowed concepts exist in knowledgebase(s) -// Set allowed = new HashSet(); -// allowed.addAll(allowedConcepts); -// allowed.removeAll(rs.getAtomicConcepts()); -// if (allowed.size() > 0) { -// System.out -// .println("Some of the concepts you told the learner to use in the definition, " -// + "do not exist in the background knowledge: " + allowed); -// System.out.println("Please correct this problem and restart."); -// System.exit(0); -// } -// } - /** * Checks whether the roles exist in background knowledge * @param roles The roles to check. * @return The first non-existing role or null if they are all in the * background knowledge. */ - // + // public static OWLObjectProperty checkRoles(AbstractReasonerComponent rs, Set roles) { Set existingRoles = rs.getObjectProperties(); for (OWLObjectProperty ar : roles) { - if(!existingRoles.contains(ar)) + if(!existingRoles.contains(ar)) return ar; } return null; @@ -577,14 +222,14 @@ public static OWLObjectProperty checkRoles(AbstractReasonerComponent rs, Set T checkEntities(AbstractReasonerComponent rs, Set entities) { Set existingEntities = (Set) computeEntities(rs, entities.iterator().next().getEntityType()); for (T entity : entities) { - if(!existingEntities.contains(entity)) + if(!existingEntities.contains(entity)) return entity; } return null; @@ -596,55 +241,14 @@ public static T checkEntities(AbstractReasonerComponent rs * @return The first non-existing role or null if they are all in the * background knowledge. */ - // + // public static OWLClass checkConcepts(AbstractReasonerComponent rs, Set concepts) { Set existingConcepts = rs.getClasses(); for (OWLClass ar : concepts) { - if(!existingConcepts.contains(ar)) + if(!existingConcepts.contains(ar)) return ar; } return null; } - // creates a flat ABox by querying a reasoner - public static FlatABox createFlatABox(AbstractReasonerComponent rs) - throws ReasoningMethodUnsupportedException { - long dematStartTime = System.currentTimeMillis(); - - FlatABox aBox = new FlatABox(); // FlatABox.getInstance(); - if(!rs.getClasses().isEmpty()) { - for (OWLClass atomicConcept : rs.getClasses()) { - aBox.atomicConceptsPos.put(atomicConcept.toStringID(), getStringSet(rs - .getIndividuals(atomicConcept))); - OWLObjectComplementOf negatedAtomicConcept = df.getOWLObjectComplementOf(atomicConcept); - aBox.atomicConceptsNeg.put(atomicConcept.toStringID(), getStringSet(rs - .getIndividuals(negatedAtomicConcept))); - aBox.concepts.add(atomicConcept.toStringID()); - } - } - - if(!rs.getObjectProperties().isEmpty()) { - for (OWLObjectProperty atomicRole : rs.getObjectProperties()) { - aBox.rolesPos.put(atomicRole.toStringID(), getStringMap(rs.getPropertyMembers(atomicRole))); - aBox.roles.add(atomicRole.toStringID()); - } - } - - aBox.domain = getStringSet(rs.getIndividuals()); - aBox.top = aBox.domain; - - // System.out.println(aBox); - - long dematDuration = System.currentTimeMillis() - dematStartTime; - System.out.println("OK (" + dematDuration + " ms)"); - return aBox; - } - - public static String arrayContent(int[] ar) { - String str = ""; - for(int i=0; i. */ package org.dllearner.utilities; @@ -14,7 +29,7 @@ */ public class IndividualMapping { - private TObjectIntMap mapping = new TObjectIntCustomHashMap(); + private TObjectIntMap mapping = new TObjectIntCustomHashMap<>(); private volatile int value = 0; diff --git a/components-core/src/main/java/org/dllearner/utilities/JamonMonitorLogger.java b/components-core/src/main/java/org/dllearner/utilities/JamonMonitorLogger.java index 79229b2176..e533bbbcb5 100644 --- a/components-core/src/main/java/org/dllearner/utilities/JamonMonitorLogger.java +++ b/components-core/src/main/java/org/dllearner/utilities/JamonMonitorLogger.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities; import java.io.File; @@ -51,13 +50,13 @@ public class JamonMonitorLogger { public static List getMonitors(){ //MonitorFactory mf=(MonitorFactory)MonitorFactory.getFactory(); - LinkedList l=new LinkedList(); + LinkedList l= new LinkedList<>(); @SuppressWarnings("unchecked") Iterator it = MonitorFactory.getFactory().iterator(); //mf.iterator(); while (it.hasNext()) { - Monitor monitor = (Monitor) it.next(); + Monitor monitor = it.next(); l.add(monitor); } @@ -75,15 +74,15 @@ public static void printAllSortedByLabel() { public static String getStringForAllSortedByLabel() { List l= getMonitors(); - SortedSet sset = new TreeSet(); - StringBuffer sbuf = new StringBuffer(); + SortedSet sset = new TreeSet<>(); + StringBuilder sbuf = new StringBuilder(); for (int i = 0; i < l.size(); i++) { Monitor monitor = l.get(i); sset.add(monitor.toString()); } for (String onemon : sset) { - sbuf.append(onemon+"\n"); + sbuf.append(onemon).append("\n"); } return sbuf.toString(); } diff --git a/components-core/src/main/java/org/dllearner/utilities/LabelShortFormProvider.java b/components-core/src/main/java/org/dllearner/utilities/LabelShortFormProvider.java index edb9ea51bb..43cb4fd70a 100644 --- a/components-core/src/main/java/org/dllearner/utilities/LabelShortFormProvider.java +++ b/components-core/src/main/java/org/dllearner/utilities/LabelShortFormProvider.java @@ -1,20 +1,40 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.utilities; -import org.dllearner.kb.sparql.ExtractionDBCache; -import org.dllearner.kb.sparql.SparqlEndpoint; -import org.dllearner.kb.sparql.SparqlQuery; -import org.semanticweb.owlapi.model.OWLEntity; -import org.semanticweb.owlapi.util.ShortFormProvider; -import org.semanticweb.owlapi.util.SimpleIRIShortFormProvider; - import com.hp.hpl.jena.query.ParameterizedSparqlString; import com.hp.hpl.jena.query.Query; -import com.hp.hpl.jena.query.QueryExecutionFactory; +import com.hp.hpl.jena.query.QueryExecution; import com.hp.hpl.jena.query.ResultSet; -import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP; import com.hp.hpl.jena.vocabulary.RDFS; +import org.aksw.jena_sparql_api.core.QueryExecutionFactory; +import org.semanticweb.owlapi.model.OWLEntity; +import org.semanticweb.owlapi.util.ShortFormProvider; +import org.semanticweb.owlapi.util.SimpleIRIShortFormProvider; + +import javax.annotation.Nonnull; +/** + * A short form provider for OWL entities which uses the English rdfs:label if exist. + * + * @author Lorenz Buehmann + */ public class LabelShortFormProvider implements ShortFormProvider{ private final ParameterizedSparqlString queryTemplate = new ParameterizedSparqlString( @@ -23,21 +43,10 @@ public class LabelShortFormProvider implements ShortFormProvider{ private final SimpleIRIShortFormProvider fallback = new SimpleIRIShortFormProvider(); - private ExtractionDBCache cache; - private SparqlEndpoint endpoint; - private Model model; - - public LabelShortFormProvider(Model model) { - this.model = model; - } - - public LabelShortFormProvider(SparqlEndpoint endpoint) { - this.endpoint = endpoint; - } - - public LabelShortFormProvider(SparqlEndpoint endpoint, ExtractionDBCache cache) { - this.endpoint = endpoint; - this.cache = cache; + private final QueryExecutionFactory qef; + + public LabelShortFormProvider(QueryExecutionFactory qef) { + this.qef = qef; } public void setLabelProperty(String labelProperty) { @@ -46,43 +55,25 @@ public void setLabelProperty(String labelProperty) { @Override public void dispose() { + qef.close(); } + @Nonnull @Override - public String getShortForm(OWLEntity entity) { + public String getShortForm(@Nonnull OWLEntity entity) { queryTemplate.clearParams(); queryTemplate.setIri("entity", entity.toStringID()); queryTemplate.setIri("labelProperty", labelProperty); Query query = queryTemplate.asQuery(); - ResultSet rs = executeSelect(query); - String label = null; - if(rs.hasNext()){ - label = rs.next().getLiteral("label").asLiteral().getLexicalForm(); - } else { - label = fallback.getShortForm(entity.getIRI()); - } - return label; - } - - protected ResultSet executeSelect(Query query){ - ResultSet rs = null; - if(endpoint != null){ - if(cache != null){ - rs = SparqlQuery.convertJSONtoResultSet(cache.executeSelectQuery(endpoint, query.toString())); + try(QueryExecution qe = qef.createQueryExecution(query)) { + ResultSet rs = qe.execSelect(); + String label = null; + if(rs.hasNext()){ + label = rs.next().getLiteral("label").asLiteral().getLexicalForm(); } else { - QueryEngineHTTP qe = new QueryEngineHTTP(endpoint.getURL().toString(), query); - for(String uri : endpoint.getDefaultGraphURIs()){ - qe.addDefaultGraph(uri); - } - rs = qe.execSelect(); + label = fallback.getShortForm(entity.getIRI()); } - } else { - rs = QueryExecutionFactory.create(query, model).execSelect(); + return label; } - - return rs; } - - - } diff --git a/components-core/src/main/java/org/dllearner/utilities/Logging.java b/components-core/src/main/java/org/dllearner/utilities/Logging.java index 43b5aded16..b293d8057e 100644 --- a/components-core/src/main/java/org/dllearner/utilities/Logging.java +++ b/components-core/src/main/java/org/dllearner/utilities/Logging.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities; import java.util.Enumeration; diff --git a/components-core/src/main/java/org/dllearner/utilities/MapUtils.java b/components-core/src/main/java/org/dllearner/utilities/MapUtils.java index b80a7ad294..521f9a1e3c 100644 --- a/components-core/src/main/java/org/dllearner/utilities/MapUtils.java +++ b/components-core/src/main/java/org/dllearner/utilities/MapUtils.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.utilities; import java.util.ArrayList; @@ -9,25 +27,35 @@ import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Multimap; +import com.google.common.collect.Multimaps; +import com.google.common.collect.TreeMultimap; +import org.aksw.commons.collections.multimaps.MultimapUtils; +/** + * Utility class for operations on maps. + * + * @author Lorenz Buehmann + */ public class MapUtils { /** - * Returns a list of entries sorted by the values descending. - * @param map - * @return + * Returns a list of entries sorted by the map values descending. + * + * @param map the map + * @return a list of entries sorted by the map values descending. */ - public static > List> sortByValues(Map map){ + public static > List> sortByValues(Map map) { return sortByValues(map, false); } - + /** - * Returns a list of entries sorted by the values either ascending or descending. - * @param map - * @return + * Returns a list of entries sorted by the map values either ascending or descending. + * + * @param map the map + * @return a list of entries sorted by the map values either ascending or descending. */ public static > List> sortByValues(Map map, final boolean ascending){ - List> entries = new ArrayList>(map.entrySet()); + List> entries = new ArrayList<>(map.entrySet()); Collections.sort(entries, new Comparator>() { @Override @@ -41,12 +69,32 @@ public int compare(Entry o1, Entry o2) { }); return entries; } - + + /** + * Constructs a multimap with the same mappings as the specified map. + * + * @param input the input map + * @return the multimap + */ public static Multimap createMultiMap(Map> input) { - Multimap multimap = ArrayListMultimap.create(); - for (Map.Entry> entry : input.entrySet()) { - multimap.putAll(entry.getKey(), entry.getValue()); - } - return multimap; + Multimap multimap = ArrayListMultimap.create(); + for (Map.Entry> entry : input.entrySet()) { + multimap.putAll(entry.getKey(), entry.getValue()); + } + return multimap; + } + + /** + * Creates a Guava sorted multimap using the input map. + * + * @param input the input map + * @return the multimap + */ + public static Multimap createSortedMultiMap(Map> input) { + Multimap multimap = TreeMultimap.create(); + for (Map.Entry> entry : input.entrySet()) { + multimap.putAll(entry.getKey(), entry.getValue()); } + return multimap; + } } diff --git a/components-core/src/main/java/org/dllearner/utilities/NumberComparator.java b/components-core/src/main/java/org/dllearner/utilities/NumberComparator.java index 651aad036b..1ee4452c39 100644 --- a/components-core/src/main/java/org/dllearner/utilities/NumberComparator.java +++ b/components-core/src/main/java/org/dllearner/utilities/NumberComparator.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.utilities; import java.util.Comparator; diff --git a/components-core/src/main/java/org/dllearner/utilities/OWLAPIUtils.java b/components-core/src/main/java/org/dllearner/utilities/OWLAPIUtils.java index 63aee867d7..9441a4c0a3 100644 --- a/components-core/src/main/java/org/dllearner/utilities/OWLAPIUtils.java +++ b/components-core/src/main/java/org/dllearner/utilities/OWLAPIUtils.java @@ -1,79 +1,82 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.utilities; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; -import java.util.TreeMap; -import java.util.TreeSet; - -import org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntaxEditorParser; +import com.clarkparsia.owlapiv3.XSD; +import com.google.common.collect.Iterables; +import com.google.common.collect.Sets; import org.dllearner.core.AbstractReasonerComponent; import org.dllearner.utilities.owl.SimpleOWLEntityChecker; +import org.jetbrains.annotations.NotNull; import org.joda.time.DateTime; -import org.joda.time.format.DateTimeFormat; -import org.joda.time.format.DateTimeFormatter; -import org.joda.time.format.DateTimeFormatterBuilder; -import org.joda.time.format.ISODateTimeFormat; -import org.joda.time.format.ISOPeriodFormat; -import org.joda.time.format.PeriodFormatter; -import org.joda.time.format.PeriodFormatterBuilder; -import org.semanticweb.owlapi.expression.ParserException; -import org.semanticweb.owlapi.model.EntityType; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLDatatype; -import org.semanticweb.owlapi.model.OWLLiteral; -import org.semanticweb.owlapi.vocab.OWL2Datatype; - -import uk.ac.manchester.cs.owl.owlapi.OWL2DatatypeImpl; +import org.joda.time.format.*; +import org.semanticweb.owlapi.apibinding.OWLManager; +import org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxClassExpressionParser; +import org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxParserException; +import org.semanticweb.owlapi.model.*; +import org.semanticweb.owlapi.vocab.XSDVocabulary; import uk.ac.manchester.cs.owl.owlapi.OWLDatatypeImpl; -import com.clarkparsia.owlapiv3.XSD; -import com.google.common.collect.Iterables; -import com.google.common.collect.Sets; +import java.util.*; /** + * A collection of utility methods for the OWL API. + * * @author Lorenz Buehmann * */ public class OWLAPIUtils { private static final OWLCLassExpressionToOWLClassTransformer OWL_CLASS_TRANSFORM_FUNCTION = new OWLCLassExpressionToOWLClassTransformer(); + + public static final OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); + public static final OWLDataFactory factory = manager.getOWLDataFactory(); - public final static Set intDatatypes = new TreeSet(Arrays.asList( - XSD.INT, - XSD.INTEGER, - XSD.POSITIVE_INTEGER, - XSD.NEGATIVE_INTEGER, - XSD.NON_POSITIVE_INTEGER, - XSD.NON_NEGATIVE_INTEGER, - XSD.SHORT, - XSD.BYTE, - XSD.UNSIGNED_INT, - XSD.UNSIGNED_LONG - )); - public final static Set floatDatatypes = new TreeSet(Arrays.asList( - XSD.FLOAT, - XSD.DOUBLE, - OWL2DatatypeImpl.getDatatype(OWL2Datatype.XSD_DECIMAL) - )); - public final static Set fixedDatatypes = new TreeSet(Arrays.asList( - XSD.BOOLEAN - )); + public final static Set intDatatypes = new TreeSet<>(Arrays.asList( + XSD.INT, + XSD.INTEGER, + XSD.POSITIVE_INTEGER, + XSD.NEGATIVE_INTEGER, + XSD.NON_POSITIVE_INTEGER, + XSD.NON_NEGATIVE_INTEGER, + XSD.SHORT, + XSD.BYTE, + XSD.UNSIGNED_INT, + XSD.UNSIGNED_LONG + )); + public final static Set floatDatatypes = new TreeSet<>(Arrays.asList( + XSD.FLOAT, + XSD.DOUBLE, + factory.getOWLDatatype(XSDVocabulary.DECIMAL.getIRI()) + + )); + public final static Set fixedDatatypes = new TreeSet<>(Collections.singletonList( + XSD.BOOLEAN + )); /** * The OWL 2 datatypes for the representation of time instants with and * without time zone offsets. */ public final static Set owl2TimeDatatypes = Sets.newTreeSet(Arrays.asList( - OWL2DatatypeImpl.getDatatype(OWL2Datatype.XSD_DATE_TIME), - OWL2DatatypeImpl.getDatatype(OWL2Datatype.XSD_DATE_TIME_STAMP) + factory.getOWLDatatype(XSDVocabulary.DATE_TIME.getIRI()), + factory.getOWLDatatype(XSDVocabulary.DATE_TIME_STAMP.getIRI()) )); public final static Set dtDatatypes = Sets.newTreeSet(Arrays.asList( @@ -94,10 +97,10 @@ public class OWLAPIUtils { private static final Map> javaTypeMap; static { - javaTypeMap = new TreeMap>(); + javaTypeMap = new TreeMap<>(); javaTypeMap.put(XSD.BYTE, Byte.class); javaTypeMap.put(XSD.SHORT, Short.class); - javaTypeMap.put(OWL2DatatypeImpl.getDatatype(OWL2Datatype.XSD_DECIMAL), Double.class); + javaTypeMap.put(factory.getOWLDatatype(XSDVocabulary.DECIMAL.getIRI()), Double.class); javaTypeMap.put(XSD.INT, Integer.class); javaTypeMap.put(XSD.INTEGER, Integer.class); javaTypeMap.put(XSD.POSITIVE_INTEGER, Integer.class); @@ -122,7 +125,7 @@ public class OWLAPIUtils { dateTimeFormatters.put(XSD.TIME, DateTimeFormat.forPattern("hh:mm:ss.sss").withOffsetParsed()); dateTimeFormatters.put(XSD.DATE, ISODateTimeFormat.date()); dateTimeFormatters.put(XSD.DATE_TIME, ISODateTimeFormat.dateHourMinuteSecond()); // .dateTimeNoMillis()); - dateTimeFormatters.put(OWL2DatatypeImpl.getDatatype(OWL2Datatype.XSD_DATE_TIME_STAMP), ISODateTimeFormat.dateTimeNoMillis().withOffsetParsed()); + dateTimeFormatters.put(factory.getOWLDatatype(XSDVocabulary.DATE_TIME_STAMP.getIRI()), ISODateTimeFormat.dateTimeNoMillis().withOffsetParsed()); } public static final Map dateTimeParsers = new HashMap<>(dateTimeFormatters); @@ -173,11 +176,19 @@ public class OWLAPIUtils { .toFormatter()); } - + + /** + * @param entityType the OWL entity type + * @return the name of the OWL entity type + */ public static String getPrintName(EntityType entityType) { return entityType.getPrintName().toLowerCase(); } - + + /** + * @param lit the OWL literal + * @return whether the OWL literal is an integer, i.e. whether the datatype is some integer + */ public static boolean isIntegerDatatype(OWLLiteral lit) { return intDatatypes.contains(lit.getDatatype()); } @@ -189,21 +200,26 @@ public static boolean isIntegerDatatype(OWLDatatype datatype) { public static boolean isNumericDatatype(OWLDatatype datatype){ return numericDatatypes.contains(datatype); } - + + /** + * Convenience method that converts a set of OWL class expressions to a set of OWL classes. + * @param classExpressions a set of OWL class expressions + * @return a set of OWL classes + */ public static Set asOWLClasses(Set classExpressions) { return Sets.newHashSet(Iterables.transform(classExpressions, OWL_CLASS_TRANSFORM_FUNCTION)); } public static final String UNPARSED_OCE = "dllearner+unparsed:"; + + public static OWLClassExpression classExpressionPropertyExpander (OWLClassExpression startClass, AbstractReasonerComponent reasoner, OWLDataFactory dataFactory) { if(!startClass.isAnonymous() && startClass.asOWLClass().getIRI().toString().startsWith(UNPARSED_OCE)) { try { String s = startClass.asOWLClass().getIRI().toString().substring(UNPARSED_OCE.length()); - ManchesterOWLSyntaxEditorParser parser = new ManchesterOWLSyntaxEditorParser(dataFactory, s); - parser.setOWLEntityChecker(new SimpleOWLEntityChecker(reasoner)); - return parser.parseClassExpression(); - } catch (ParserException e) { + return fromManchester(s, reasoner, dataFactory); + } catch (ManchesterOWLSyntaxParserException e) { throw new RuntimeException("Parsing of class expression in OWL Manchester Syntax failed. Please check the syntax and " + "remember to use either full IRIs or prefixed IRIs.", e); } @@ -212,14 +228,28 @@ public static OWLClassExpression classExpressionPropertyExpander (OWLClassExpres } } - + + @NotNull + public static OWLClassExpression fromManchester(String expr, AbstractReasonerComponent reasoner, OWLDataFactory dataFactory) { + ManchesterOWLSyntaxClassExpressionParser parser = new ManchesterOWLSyntaxClassExpressionParser(dataFactory, new SimpleOWLEntityChecker(reasoner)); + return parser.parse(expr); + } + + @NotNull + public static OWLClassExpression fromManchester(String expr, AbstractReasonerComponent reasoner, OWLDataFactory dataFactory, boolean shortForm) { + SimpleOWLEntityChecker oec = new SimpleOWLEntityChecker(reasoner); + oec.setAllowShortForm(shortForm); + ManchesterOWLSyntaxClassExpressionParser parser = new ManchesterOWLSyntaxClassExpressionParser(dataFactory, oec); + return parser.parse(expr); + } + /** * Checks whether the given value is in the closed interval [min,max], i.e. * the value is greater than min and lower than max. * @param value the value * @param min the lower interval endpoint * @param max the upper interval endpoint - * @return + * @return whether the given value is in the closed interval [min,max] */ public static boolean inRange(OWLLiteral value, OWLLiteral min, OWLLiteral max) { OWLDatatype datatype = value.getDatatype(); diff --git a/components-core/src/main/java/org/dllearner/utilities/OWLCLassExpressionToOWLClassTransformer.java b/components-core/src/main/java/org/dllearner/utilities/OWLCLassExpressionToOWLClassTransformer.java index 506bd8e9fa..c2c5693c2d 100644 --- a/components-core/src/main/java/org/dllearner/utilities/OWLCLassExpressionToOWLClassTransformer.java +++ b/components-core/src/main/java/org/dllearner/utilities/OWLCLassExpressionToOWLClassTransformer.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.utilities; diff --git a/components-core/src/main/java/org/dllearner/utilities/OWLClassExpression2FuzzyDLConverter.java b/components-core/src/main/java/org/dllearner/utilities/OWLClassExpression2FuzzyDLConverter.java index fbd96a0ebd..5d9ac193d8 100644 --- a/components-core/src/main/java/org/dllearner/utilities/OWLClassExpression2FuzzyDLConverter.java +++ b/components-core/src/main/java/org/dllearner/utilities/OWLClassExpression2FuzzyDLConverter.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.utilities; diff --git a/components-core/src/main/java/org/dllearner/utilities/OwlApiJenaUtils.java b/components-core/src/main/java/org/dllearner/utilities/OwlApiJenaUtils.java index 7474046b2f..03cbe82f2e 100644 --- a/components-core/src/main/java/org/dllearner/utilities/OwlApiJenaUtils.java +++ b/components-core/src/main/java/org/dllearner/utilities/OwlApiJenaUtils.java @@ -1,35 +1,46 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.utilities; -import java.io.IOException; -import java.io.PipedInputStream; -import java.io.PipedOutputStream; -import java.util.List; -import java.util.Set; - -import org.coode.owlapi.turtle.TurtleOntologyFormat; -import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLDatatype; -import org.semanticweb.owlapi.model.OWLLiteral; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyCreationException; -import org.semanticweb.owlapi.model.OWLOntologyManager; -import org.semanticweb.owlapi.model.OWLOntologyStorageException; - -import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; - +import com.hp.hpl.jena.datatypes.BaseDatatype; +import com.hp.hpl.jena.datatypes.RDFDatatype; +import com.hp.hpl.jena.datatypes.xsd.XSDDatatype; +import com.hp.hpl.jena.graph.Node; import com.hp.hpl.jena.graph.NodeFactory; import com.hp.hpl.jena.graph.impl.LiteralLabel; +import com.hp.hpl.jena.graph.impl.LiteralLabelFactory; import com.hp.hpl.jena.rdf.model.Literal; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.rdf.model.Statement; import com.hp.hpl.jena.rdf.model.impl.LiteralImpl; +import org.semanticweb.owlapi.apibinding.OWLManager; +import org.semanticweb.owlapi.formats.TurtleDocumentFormat; +import org.semanticweb.owlapi.model.*; +import org.semanticweb.owlapi.vocab.Namespaces; +import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; + +import java.io.IOException; +import java.io.PipedInputStream; +import java.io.PipedOutputStream; +import java.util.List; +import java.util.Set; /** * @author Lorenz Buehmann @@ -37,12 +48,17 @@ */ public class OwlApiJenaUtils { - private static OWLDataFactory dataFactory = new OWLDataFactoryImpl(false, false); + private static OWLDataFactory dataFactory = new OWLDataFactoryImpl(); + /** + * Converts a JENA API model into an OWL API ontology. + * @param model the JENA API model + * @return the OWL API ontology + */ public static OWLOntology getOWLOntology(final Model model) { OWLOntology ontology; - try (PipedInputStream is = new PipedInputStream(); PipedOutputStream os = new PipedOutputStream(is);) { + try (PipedInputStream is = new PipedInputStream(); PipedOutputStream os = new PipedOutputStream(is)) { OWLOntologyManager man = OWLManager.createOWLOntologyManager(); new Thread(new Runnable() { public void run() { @@ -60,31 +76,22 @@ public void run() { throw new RuntimeException("Could not convert JENA API model to OWL API ontology.", e); } } - + /** - * Convert JENA API OWL statements into OWL API axioms. - * @param axioms the JENA API statements - * @return + * Converts an OWL API ontology into a JENA API model. + * @param ontology the OWL API ontology + * @return the JENA API model */ - public static Set asOWLAxioms(List statements) { - Model model = ModelFactory.createDefaultModel(); - model.add(statements); - OWLOntology ontology = getOWLOntology(model); - return ontology.getAxioms(); - } - public static Model getModel(final OWLOntology ontology) { Model model = ModelFactory.createDefaultModel(); - try (PipedInputStream is = new PipedInputStream(); PipedOutputStream os = new PipedOutputStream(is);) { + try (PipedInputStream is = new PipedInputStream(); PipedOutputStream os = new PipedOutputStream(is)) { new Thread(new Runnable() { public void run() { try { - ontology.getOWLOntologyManager().saveOntology(ontology, new TurtleOntologyFormat(), os); + ontology.getOWLOntologyManager().saveOntology(ontology, new TurtleDocumentFormat(), os); os.close(); - } catch (OWLOntologyStorageException e) { - e.printStackTrace(); - } catch (IOException e) { + } catch (OWLOntologyStorageException | IOException e) { e.printStackTrace(); } } @@ -95,7 +102,24 @@ public void run() { throw new RuntimeException("Could not convert OWL API ontology to JENA API model.", e); } } - + + /** + * Convert statements from JENA API into OWL API axioms. + * @param statements the JENA API statements + * @return the set of axioms + */ + public static Set asOWLAxioms(List statements) { + Model model = ModelFactory.createDefaultModel(); + model.add(statements); + OWLOntology ontology = getOWLOntology(model); + return ontology.getAxioms(); + } + + /** + * Converts a JENA API literal into an OWL API literal. + * @param lit the JENA API literal + * @return the OWL API literal + */ public static OWLLiteral getOWLLiteral(Literal lit){ OWLLiteral literal = null; if(lit.getDatatypeURI() != null){ @@ -110,15 +134,37 @@ public static OWLLiteral getOWLLiteral(Literal lit){ } return literal; } - + + /** + * Converts a JENA API literal into an OWL API literal. + * @param lit the JENA API literal + * @return the OWL API literal + */ public static OWLLiteral getOWLLiteral(LiteralLabel lit){ return getOWLLiteral(new LiteralImpl(NodeFactory.createLiteral(lit), null)); } + + /** + * Converts an OWL API literal into a JENA API literal. + * @param lit the OWL API literal + * @return the JENA API literal + */ + public static LiteralLabel getLiteral(OWLLiteral lit){ + OWLDatatype owlDatatype = lit.getDatatype(); + + RDFDatatype datatype; + if(Namespaces.XSD.inNamespace(owlDatatype.getIRI())){ + datatype = new XSDDatatype(owlDatatype.getIRI().getRemainder().get()); + } else { + datatype = new BaseDatatype(lit.getDatatype().toStringID()); + } + return LiteralLabelFactory.create(lit.getLiteral(), lit.getLang(), datatype); + } /** - * Convert OWL API OWL axioms into JENA API statements. + * Convert OWL axioms from OWL API into JENA API statements. * @param axioms the OWL API axioms - * @return + * @return the JENA statements */ public static Set asStatements(Set axioms) { try { @@ -129,5 +175,23 @@ public static Set asStatements(Set axioms) { throw new RuntimeException("Conversion of axioms failed.", e); } } - + + /** + * Convert an OWL API entity into a JENA API node. + * @param entity the OWL API entity + * @return the JENA API node + */ + public static Node asNode(OWLEntity entity) { + return NodeFactory.createURI(entity.toStringID()); + } + + /** + * Convert a JENA Node into an OWL entity of the given type. + * @param node the JENA node + * @param entityType the type of the OWL entity, e.g. class, property, etc. + * @return the OWL entity + */ + public static T asOWLEntity(Node node, EntityType entityType) { + return dataFactory.getOWLEntity(entityType, IRI.create(node.getURI())); + } } diff --git a/components-core/src/main/java/org/dllearner/utilities/PrefixCCMap.java b/components-core/src/main/java/org/dllearner/utilities/PrefixCCMap.java index b106ec78b7..65a4937cbf 100644 --- a/components-core/src/main/java/org/dllearner/utilities/PrefixCCMap.java +++ b/components-core/src/main/java/org/dllearner/utilities/PrefixCCMap.java @@ -1,8 +1,25 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.utilities; import java.io.BufferedReader; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; @@ -17,6 +34,13 @@ import java.util.HashSet; import java.util.Set; +/** + * A map of prefixes based on http://prefix.cc/ service. The prefixes are cached locally in + * src/main/resources/prefixes.csv. An update of this file has to be forced manually by running the main method + * of this class. + * + * @author Lorenz Buehmann + */ public class PrefixCCMap extends HashMap{ private static final String LOCAL_FILE = "prefixes.csv"; @@ -44,8 +68,6 @@ private void fillMap() { put(key, value); } } - } catch (FileNotFoundException e) { - e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { @@ -68,8 +90,8 @@ public static PrefixCCMap getInstance(){ /** * This main methods updates the local prefix file by loading latest prefix list from prefix.cc. - * @param args - * @throws IOException + * @param args the arguments + * @throws IOException */ public static void main(String[] args) throws IOException { //load latest file from prefix.cc @@ -90,7 +112,7 @@ public static void main(String[] args) throws IOException { PrintWriter pw = new PrintWriter(new FileWriter(tmpFile)); BufferedReader br = new BufferedReader(new FileReader(inFile)); String line = null; - Set values = new HashSet(); + Set values = new HashSet<>(); while ((line = br.readLine()) != null) { String[] entry = line.split(","); if(entry.length == 2){ diff --git a/components-core/src/main/java/org/dllearner/utilities/QueryUtils.java b/components-core/src/main/java/org/dllearner/utilities/QueryUtils.java index 862909e328..c1a1110a36 100644 --- a/components-core/src/main/java/org/dllearner/utilities/QueryUtils.java +++ b/components-core/src/main/java/org/dllearner/utilities/QueryUtils.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.utilities; import java.util.ArrayList; @@ -58,9 +76,9 @@ public class QueryUtils extends ElementVisitorBase { private int optionalCount = 0; private int filterCount = 0; - private Map triple2Parent = new HashMap(); + private Map triple2Parent = new HashMap<>(); - Stack parents = new Stack(); + Stack parents = new Stack<>(); public static String addPrefix(String queryString, Map prefix2Namespace){ Query query = QueryFactory.create(queryString); @@ -80,11 +98,11 @@ public static String addPrefixes(String queryString, String prefix, String names /** * Returns all variables that occur in a triple pattern of the SPARQL query. - * @param query + * @param query the query * @return */ public Set getVariables(Query query){ - Set vars = new HashSet(); + Set vars = new HashSet<>(); Set triplePatterns = extractTriplePattern(query, false); @@ -103,11 +121,11 @@ public Set getVariables(Query query){ /** * Returns all variables that occur as subject in a triple pattern of the SPARQL query. - * @param query + * @param query the query * @return */ public Set getSubjectVariables(Query query){ - Set vars = new HashSet(); + Set vars = new HashSet<>(); Set triplePatterns = extractTriplePattern(query, false); @@ -122,11 +140,11 @@ public Set getSubjectVariables(Query query){ /** * Returns all variables that occur as subject in a triple pattern of the SPARQL query. - * @param query + * @param query the query * @return */ public static Set getSubjectVars(Query query){ - final Set vars = new HashSet(); + final Set vars = new HashSet<>(); ElementWalker.walk(query.getQueryPattern(), new ElementVisitorBase(){ @Override @@ -186,7 +204,8 @@ public void visit(ElementPathBlock el) { /** * Given a SPARQL query and a start node, return the outgoing * triple patterns. - * @param query + * @param query the query + * @param source the start node * @return */ public static Set getOutgoingTriplePatterns(Query query, final Node source){ @@ -227,7 +246,8 @@ public void visit(ElementPathBlock el) { /** * Given a SPARQL query and a start node, return the maximum subject-object * join depth. - * @param query + * @param query the query + * @param source the start node * @return */ public static int getSubjectObjectJoinDepth(Query query, final Node source){ @@ -272,11 +292,11 @@ public void visit(ElementPathBlock el) { /** * Returns all variables that occur as object in a triple pattern of the SPARQL query. - * @param query + * @param query the query * @return */ public static Set getObjectVars(Query query){ - final Set vars = new HashSet(); + final Set vars = new HashSet<>(); ElementWalker.walk(query.getQueryPattern(), new ElementVisitorBase(){ @Override @@ -307,11 +327,11 @@ public void visit(ElementPathBlock el) { /** * Returns all variables that occur as subject in a triple pattern of the SPARQL query. - * @param query + * @param query the query * @return */ public Set getObjectVariables(Query query){ - Set vars = new HashSet(); + Set vars = new HashSet<>(); Set triplePatterns = extractTriplePattern(query, false); @@ -328,7 +348,7 @@ public Set getObjectVariables(Query query){ * Returns all triple patterns in given SPARQL query that have the given node in subject position, i.e. the outgoing * triple patterns. * @param query The SPARQL query. - * @param node + * @param node the node * @return */ public Set extractOutgoingTriplePatterns(Query query, Node node){ @@ -347,7 +367,7 @@ public Set extractOutgoingTriplePatterns(Query query, Node node){ * Returns all triple patterns in given SPARQL query that have the given node in object position, i.e. the incoming * triple patterns. * @param query The SPARQL query. - * @param node + * @param node the node * @return */ public Set extractIncomingTriplePatterns(Query query, Node node){ @@ -366,7 +386,7 @@ public Set extractIncomingTriplePatterns(Query query, Node node){ * Returns all triple patterns in given SPARQL query that have the given node in object position, i.e. the ingoing * triple patterns. * @param query The SPARQL query. - * @param node + * @param node the node * @return */ public Set extractIngoingTriplePatterns(Query query, Node node){ @@ -385,11 +405,11 @@ public Set extractIngoingTriplePatterns(Query query, Node node){ * Returns all triple patterns in given SPARQL query that have the given node either in subject or in object position, i.e. * the ingoing and outgoing triple patterns. * @param query The SPARQL query. - * @param node + * @param node the node * @return */ public Set extractTriplePatterns(Query query, Node node){ - Set triplePatterns = new HashSet(); + Set triplePatterns = new HashSet<>(); triplePatterns.addAll(extractIngoingTriplePatterns(query, node)); triplePatterns.addAll(extractOutgoingTriplePatterns(query, node)); return triplePatterns; @@ -409,7 +429,7 @@ public Set extractTriplePatternsWithPredicate(Query query, Node predicat // filter by predicate Iterator iterator = triplePatterns.iterator(); while (iterator.hasNext()) { - Triple tp = (Triple) iterator.next(); + Triple tp = iterator.next(); if(!tp.predicateMatches(predicate)) { iterator.remove(); } @@ -422,11 +442,11 @@ public Set extractTriplePatternsWithPredicate(Query query, Node predicat * Returns all triple patterns in given SPARQL query that have the given node either in subject or in object position, i.e. * the incoming and outgoing triple patterns. * @param query The SPARQL query. - * @param node + * @param node the node * @return */ public Set extractNonOptionalTriplePatterns(Query query, Node node){ - Set triplePatterns = new HashSet(); + Set triplePatterns = new HashSet<>(); triplePatterns.addAll(extractIngoingTriplePatterns(query, node)); triplePatterns.addAll(extractOutgoingTriplePatterns(query, node)); triplePatterns.removeAll(optionalTriplePattern); @@ -436,13 +456,12 @@ public Set extractNonOptionalTriplePatterns(Query query, Node node){ /** * Returns triple patterns for each projection variable v such that v is either in subject or object position. * @param query The SPARQL query. - * @param node * @return */ public Map> extractTriplePatternsForProjectionVars(Query query){ - Map> var2TriplePatterns = new HashMap>(); + Map> var2TriplePatterns = new HashMap<>(); for (Var var : query.getProjectVars()) { - Set triplePatterns = new HashSet(); + Set triplePatterns = new HashSet<>(); triplePatterns.addAll(extractIngoingTriplePatterns(query, var)); triplePatterns.addAll(extractOutgoingTriplePatterns(query, var)); var2TriplePatterns.put(var, triplePatterns); @@ -453,13 +472,12 @@ public Map> extractTriplePatternsForProjectionVars(Query query){ /** * Returns triple patterns for each projection variable v such that v is in subject position. * @param query The SPARQL query. - * @param node * @return */ public Map> extractOutgoingTriplePatternsForProjectionVars(Query query){ - Map> var2TriplePatterns = new HashMap>(); + Map> var2TriplePatterns = new HashMap<>(); for (Var var : query.getProjectVars()) { - Set triplePatterns = new HashSet(); + Set triplePatterns = new HashSet<>(); triplePatterns.addAll(extractOutgoingTriplePatterns(query, var)); var2TriplePatterns.put(var, triplePatterns); } @@ -476,13 +494,12 @@ public Set getOptionalTriplePatterns() { /** * Returns triple patterns for each projection variable v such that v is in subject position. * @param query The SPARQL query. - * @param node * @return */ public Map> extractIncomingTriplePatternsForProjectionVars(Query query){ - Map> var2TriplePatterns = new HashMap>(); + Map> var2TriplePatterns = new HashMap<>(); for (Var var : query.getProjectVars()) { - Set triplePatterns = new HashSet(); + Set triplePatterns = new HashSet<>(); triplePatterns.addAll(extractIncomingTriplePatterns(query, var)); var2TriplePatterns.put(var, triplePatterns); } @@ -492,13 +509,12 @@ public Map> extractIncomingTriplePatternsForProjectionVars(Query /** * Returns triple patterns for each projection variable v such that v is in object position. * @param query The SPARQL query. - * @param node * @return */ public Map> extractIngoingTriplePatternsForProjectionVars(Query query){ - Map> var2TriplePatterns = new HashMap>(); + Map> var2TriplePatterns = new HashMap<>(); for (Var var : query.getProjectVars()) { - Set triplePatterns = new HashSet(); + Set triplePatterns = new HashSet<>(); triplePatterns.addAll(extractIngoingTriplePatterns(query, var)); var2TriplePatterns.put(var, triplePatterns); } @@ -510,8 +526,8 @@ public Set extractTriplePattern(Query query){ } public Set extractTriplePattern(Query query, boolean ignoreOptionals){ - triplePattern = new HashSet(); - optionalTriplePattern = new HashSet(); + triplePattern = new HashSet<>(); + optionalTriplePattern = new HashSet<>(); query.getQueryPattern().visit(this); @@ -519,7 +535,7 @@ public Set extractTriplePattern(Query query, boolean ignoreOptionals){ if(!ignoreOptionals){ if(query.isSelectType()){ for(Triple t : optionalTriplePattern){ - if(!ListUtils.intersection(new ArrayList(VarUtils.getVars(t)), query.getProjectVars()).isEmpty()){ + if(!ListUtils.intersection(new ArrayList<>(VarUtils.getVars(t)), query.getProjectVars()).isEmpty()){ triplePattern.add(t); } } @@ -537,8 +553,8 @@ public Set extractTriplePattern(ElementGroup group){ } public Set extractTriplePattern(ElementGroup group, boolean ignoreOptionals){ - triplePattern = new HashSet(); - optionalTriplePattern = new HashSet(); + triplePattern = new HashSet<>(); + optionalTriplePattern = new HashSet<>(); group.visit(this); @@ -587,8 +603,8 @@ public Query removeUnboundObjectVarTriples(Query query) { * Removes triple patterns of form (s rdf:type A) if there exists a * triple pattern (s rdf:type B) such that the underlying * knowledge base entails (B rdfs:subClassOf A). - * @param qef - * @param query + * @param qef the query execution factory + * @param query the query */ public void filterOutGeneralTypes(QueryExecutionFactory qef, Query query){ // extract all rdf:type triple patterns @@ -603,7 +619,7 @@ public void filterOutGeneralTypes(QueryExecutionFactory qef, Query query){ // keep the most specific types for each subject for (Node subject : subject2TriplePatterns.keySet()) { Collection triplePatterns = subject2TriplePatterns.get(subject); - Collection triplesPatterns2Remove = new HashSet(); + Collection triplesPatterns2Remove = new HashSet<>(); for (Triple tp : triplePatterns) { if(!triplesPatterns2Remove.contains(tp)) { @@ -625,7 +641,7 @@ public void filterOutGeneralTypes(QueryExecutionFactory qef, Query query){ } private Set getSuperClasses(QueryExecutionFactory qef, Node cls){ - Set superClasses = new HashSet(); + Set superClasses = new HashSet<>(); superClassesQueryTemplate.setIri("sub", cls.getURI()); @@ -649,8 +665,7 @@ private Set getSuperClasses(QueryExecutionFactory qef, Node cls){ @Override public void visit(ElementGroup el) { parents.push(el); - for (Iterator iterator = el.getElements().iterator(); iterator.hasNext();) { - Element e = iterator.next(); + for (Element e : el.getElements()) { e.visit(this); } parents.pop(); @@ -705,8 +720,7 @@ public void visit(ElementPathBlock el) { @Override public void visit(ElementUnion el) { unionCount++; - for (Iterator iterator = el.getElements().iterator(); iterator.hasNext();) { - Element e = iterator.next(); + for (Element e : el.getElements()) { e.visit(this); } } @@ -730,7 +744,7 @@ public int getFilterCount() { /** * Returns the ElementGroup object containing the triple pattern. - * @param triple + * @param triple the triple patterm * @return */ public ElementGroup getElementGroup(Triple triple){ diff --git a/components-core/src/main/java/org/dllearner/utilities/ReasoningUtils.java b/components-core/src/main/java/org/dllearner/utilities/ReasoningUtils.java new file mode 100644 index 0000000000..f0065d12de --- /dev/null +++ b/components-core/src/main/java/org/dllearner/utilities/ReasoningUtils.java @@ -0,0 +1,306 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.utilities; + +import com.google.common.collect.Sets; +import org.dllearner.core.AbstractReasonerComponent; +import org.dllearner.core.Component; +import org.dllearner.learningproblems.AccMethodApproximate; +import org.dllearner.learningproblems.AccMethodTwoValued; +import org.dllearner.learningproblems.AccMethodTwoValuedApproximate; +import org.dllearner.reasoning.SPARQLReasoner; +import org.semanticweb.owlapi.model.OWLClassExpression; +import org.semanticweb.owlapi.model.OWLIndividual; + +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; + +/** + * Common utilities for using a reasoner in learning problems + */ +public class ReasoningUtils implements Component { + + /** + * binary counter to divide a set in 2 partitions + */ + public class CoverageCount { + public int trueCount; + public int falseCount; + public int total; + } + + public class Coverage3Count extends CoverageCount { + public int unknownCount; + } + + /** + * binary set to divide a set in 2 partitions + */ + public class Coverage extends CoverageCount { + public SortedSet trueSet = new TreeSet<>(); + public SortedSet falseSet = new TreeSet<>(); + } + + public class Coverage3 extends Coverage3Count { + public SortedSet trueSet = new TreeSet<>(); + public SortedSet falseSet = new TreeSet<>(); + public SortedSet unknownSet = new TreeSet<>(); + } + + protected AbstractReasonerComponent reasoner; + + /** + * create new reasoning utils + * @param reasoner reasoner to use + */ + public ReasoningUtils(AbstractReasonerComponent reasoner) { + this.reasoner = reasoner; + } + + /** + * callback to interrupt individual instance check + * @return true when instance check loop should be aborted + */ + protected boolean interrupted() { return false; } + + + /** + * binary partition a list of sets into true and false, depending on whether they satisfy concept + * @param concept the OWL concept used for partition + * @param sets list of sets to partition + * @return an array of Coverage data, one entry for each input set + */ + @SafeVarargs + public final Coverage[] getCoverage(OWLClassExpression concept, Set... sets) { + Coverage[] rv = new Coverage [ sets.length ]; + + if(!reasoner.isUseInstanceChecks()) { + if (reasoner instanceof SPARQLReasoner && + ((SPARQLReasoner)reasoner).isUseValueLists()) { + for (int i = 0; i < sets.length; ++i) { + SortedSet trueSet = reasoner.hasType(concept, sets[i]); + + rv[i] = new Coverage(); + rv[i].total = sets[i].size(); + + rv[i].trueSet.addAll(trueSet); + rv[i].falseSet.addAll(Sets.difference(sets[i], trueSet)); + + rv[i].trueCount = rv[i].trueSet.size(); + rv[i].falseCount = rv[i].falseSet.size(); + } + } else { + SortedSet individuals = reasoner.getIndividuals(concept); + for (int i = 0; i < sets.length; ++i) { + rv[i] = new Coverage(); + rv[i].total = sets[i].size(); + + rv[i].trueSet.addAll(Sets.intersection(sets[i], individuals)); + rv[i].falseSet.addAll(Sets.difference(sets[i], individuals)); + + rv[i].trueCount = rv[i].trueSet.size(); + rv[i].falseCount = rv[i].falseSet.size(); + } + } + } else { + for (int i = 0; i < sets.length; ++i) { + rv[i] = new Coverage(); + rv[i].total = sets[i].size(); + + for (OWLIndividual example : sets[i]) { + if (getReasoner().hasType(concept, example)) { + rv[i].trueSet.add(example); + } else { + rv[i].falseSet.add(example); + } + if (interrupted()) { + return null; + } + } + + rv[i].trueCount = rv[i].trueSet.size(); + rv[i].falseCount = rv[i].falseSet.size(); + } + } + return rv; + } + + /** + * count the numbers of individuals satisfying a concept + * @param concept the OWL concept used for counting + * @param sets list of sets of individuals to count on + * @return an array of Coverage counts, one entry for each input set + */ + @SafeVarargs + public final CoverageCount[] getCoverageCount(OWLClassExpression concept, + Set... sets) { + CoverageCount[] rv = new CoverageCount [ sets.length ]; + + if(!reasoner.isUseInstanceChecks()) { + if (reasoner instanceof SPARQLReasoner && + ((SPARQLReasoner)reasoner).isUseValueLists()) { + + for (int i = 0; i < sets.length; ++i) { + int trueCount = ((SPARQLReasoner) reasoner).getIndividualsCount(concept, sets[i]); + + rv[i] = new CoverageCount(); + rv[i].total = sets[i].size(); + + rv[i].trueCount = trueCount; + rv[i].falseCount = sets[i].size()- trueCount; + } + } else { + SortedSet individuals = reasoner.getIndividuals(concept); + for (int i = 0; i < sets.length; ++i) { + rv[i] = new CoverageCount(); + rv[i].total = sets[i].size(); + + rv[i].trueCount = Sets.intersection(sets[i], individuals).size(); + rv[i].falseCount = Sets.difference(sets[i], individuals).size(); + } + } + } else { + for (int i = 0; i < sets.length; ++i) { + rv[i] = new CoverageCount(); + rv[i].total = sets[i].size(); + + for (OWLIndividual example : sets[i]) { + if (getReasoner().hasType(concept, example)) { + ++rv[i].trueCount; + } else { + ++rv[i].falseCount; + } + if (interrupted()) { + return null; + } + } + } + } + return rv; + } + + /** + * partition a list of sets into true, false and unknown, depending on whether they satisfy concept A or B + * @param trueConcept the OWL concept used for true partition + * @param falseConcept the OWL concept used for false partition + * @param sets list of sets to partition + * @return an array of Coverage data, one entry for each input set + */ + @SafeVarargs + public final Coverage3[] getCoverage3(OWLClassExpression trueConcept, OWLClassExpression falseConcept, Set... sets) { + Coverage3[] rv = new Coverage3 [ sets.length ]; + + if(!reasoner.isUseInstanceChecks()) { + if (reasoner instanceof SPARQLReasoner && + ((SPARQLReasoner)reasoner).isUseValueLists()) { + for (int i = 0; i < sets.length; ++i) { + rv[i] = new Coverage3(); + rv[i].total = sets[i].size(); + + SortedSet trueSet = reasoner.hasType(trueConcept, sets[i]); + SortedSet falseSet = reasoner.hasType(falseConcept, sets[i]); + rv[i].trueSet.addAll(trueSet); + rv[i].falseSet.addAll(falseSet); + rv[i].unknownSet.addAll(Sets.difference(sets[i], Sets.union(trueSet, falseSet))); + + rv[i].trueCount = rv[i].trueSet.size(); + rv[i].falseCount = rv[i].falseSet.size(); + rv[i].unknownCount = rv[i].unknownSet.size(); + } + } else { + SortedSet trueIndividuals = reasoner.getIndividuals(trueConcept); + SortedSet falseIndividuals = reasoner.getIndividuals(falseConcept); + for (int i = 0; i < sets.length; ++i) { + rv[i] = new Coverage3(); + rv[i].total = sets[i].size(); + + rv[i].trueSet.addAll(Sets.intersection(sets[i], trueIndividuals)); + rv[i].falseSet.addAll(Sets.intersection(sets[i], falseIndividuals)); + rv[i].unknownSet.addAll(Sets.difference(sets[i], Sets.union(rv[i].trueSet, rv[i].falseSet))); + + rv[i].trueCount = rv[i].trueSet.size(); + rv[i].falseCount = rv[i].falseSet.size(); + rv[i].unknownCount = rv[i].unknownSet.size(); + } + } + } else { + for (int i = 0; i < sets.length; ++i) { + rv[i] = new Coverage3(); + rv[i].total = sets[i].size(); + + for (OWLIndividual example : sets[i]) { + if (getReasoner().hasType(trueConcept, example)) { + rv[i].trueSet.add(example); + } else if (getReasoner().hasType(falseConcept, example)) { + rv[i].falseSet.add(example); + } else { + rv[i].unknownSet.add(example); + } + if (interrupted()) { + return null; + } + } + + rv[i].trueCount = rv[i].trueSet.size(); + rv[i].falseCount = rv[i].falseSet.size(); + rv[i].unknownCount = rv[i].unknownSet.size(); + } + } + return rv; + } + + /** + * calculate accuracy of a concept, using the supplied accuracy method + * @param accuracyMethod accuracy method to use + * @param description concept to test + * @param positiveExamples set of positive examples to use for calculating the accuracy + * @param negativeExamples set of negative examples to use for calculating the accuracy + * @param noise noise level of the data + * @return -1 when the concept is too weak or the accuracy value as calculated by the accuracy method + */ + public double getAccuracyOrTooWeak2(AccMethodTwoValued accuracyMethod, OWLClassExpression description, Set positiveExamples, + Set negativeExamples, double noise) { + if (accuracyMethod instanceof AccMethodApproximate) { + return ((AccMethodTwoValuedApproximate) accuracyMethod).getAccApprox2(description, positiveExamples, negativeExamples, noise); + } else { + CoverageCount[] cc = this.getCoverageCount(description, positiveExamples, negativeExamples); + return getAccuracyOrTooWeakExact2(accuracyMethod, cc, noise); + } + } + + public double getAccuracyOrTooWeakExact2(AccMethodTwoValued accuracyMethod, CoverageCount[] cc, double noise) { +// return accuracyMethod.getAccOrTooWeak2(cc[0].trueCount, cc[0].falseCount, cc[1].trueCount, cc[1].falseCount, noise); + CoverageAdapter.CoverageAdapter2 c2 = new CoverageAdapter.CoverageAdapter2(cc); + return accuracyMethod.getAccOrTooWeak2(c2.tp(), c2.fn(), c2.fp(), c2.tn(), noise); + } + + @Override + public void init() { + } + + public AbstractReasonerComponent getReasoner() { + return reasoner; + } + + public void setReasoner(AbstractReasonerComponent reasoner) { + this.reasoner = reasoner; + } + +} diff --git a/components-core/src/main/java/org/dllearner/utilities/ReasoningUtilsCLP.java b/components-core/src/main/java/org/dllearner/utilities/ReasoningUtilsCLP.java new file mode 100644 index 0000000000..d7399481d6 --- /dev/null +++ b/components-core/src/main/java/org/dllearner/utilities/ReasoningUtilsCLP.java @@ -0,0 +1,147 @@ +package org.dllearner.utilities; + +import com.google.common.collect.Sets; +import com.hp.hpl.jena.query.ParameterizedSparqlString; +import com.hp.hpl.jena.query.QueryExecution; +import org.dllearner.core.AbstractReasonerComponent; +import org.dllearner.learningproblems.AccMethodApproximate; +import org.dllearner.learningproblems.AccMethodThreeValued; +import org.dllearner.learningproblems.AccMethodTwoValued; +import org.dllearner.learningproblems.ClassLearningProblem; +import org.dllearner.reasoning.SPARQLReasoner; +import org.dllearner.utilities.owl.OWLClassExpressionToSPARQLConverter; +import org.semanticweb.owlapi.model.OWLClassExpression; +import org.semanticweb.owlapi.model.OWLDataFactory; +import org.semanticweb.owlapi.model.OWLIndividual; +import org.semanticweb.owlapi.model.OWLObjectIntersectionOf; +import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; + +import java.util.Set; + +/** + * Created by Simon Bin on 16-1-25. + */ +public class ReasoningUtilsCLP extends ReasoningUtils { + final private ClassLearningProblem problem; + private OWLDataFactory df = new OWLDataFactoryImpl(); + private OWLClassExpressionToSPARQLConverter converter = new OWLClassExpressionToSPARQLConverter(); + public ReasoningUtilsCLP(ClassLearningProblem problem, AbstractReasonerComponent reasoner) { + super(reasoner); + this.problem = problem; + } + + protected boolean interrupted() { + return problem.terminationTimeExpired(); + } + + public Coverage[] getCoverageCLP(OWLClassExpression description, Set classInstances, + Set superClassInstances) { + if (reasoner instanceof SPARQLReasoner) { + SPARQLReasoner reasoner2 = (SPARQLReasoner)reasoner; + Coverage[] ret = new Coverage[2]; + ret[0] = new Coverage(); + ret[1] = new Coverage(); + + // R(C) + String query = "SELECT (COUNT(DISTINCT ?s) AS ?cnt) WHERE {" + + "?s a ?sup . ?classToDescribe ?sup . " + + converter.convert("?s", description) + + "FILTER NOT EXISTS {?s a ?classToDescribe}}"; + ParameterizedSparqlString template = new ParameterizedSparqlString(query); + //System.err.println(converter.convert("?s", description)); + //template.setIri("cls", description.asOWLClass().toStringID()); + template.setIri("classToDescribe", problem.getClassToDescribe().toStringID()); + + QueryExecution qe = reasoner2.getQueryExecutionFactory().createQueryExecution(template.toString()); + ret[1].trueCount = qe.execSelect().next().getLiteral("cnt").getInt(); + ret[1].falseCount = superClassInstances.size() - ret[1].trueCount; + + // R(A) + OWLObjectIntersectionOf ce = df.getOWLObjectIntersectionOf(problem.getClassToDescribe(), description); + ret[0].trueCount = reasoner2.getPopularityOf(ce); + ret[0].falseCount = classInstances.size() - ret[0].trueCount; + + return ret; + } else { + return getCoverage(description, classInstances, superClassInstances); + } + + } + + + public double getAccuracyCLP(AccMethodTwoValued accuracyMethod, + OWLClassExpression description, Set classInstances, + Set superClassInstances, + double noise + ) { + /* + // computing R(A) + CoverageCount[] coveredInstanceCount = getCoverageCount(description, classInstances); + if (coveredInstanceCount == null) { + return 0; + } + + // if even the optimal case (no additional instances covered) is not sufficient, + // the concept is too weak + if(coveredInstanceCount[0].trueCount / (double) classInstances.size() <= 1 - noise) { + return -1; + } + + CoverageCount[] additionalInstanceCount = getCoverageCount(description, superClassInstances); + // computing R(C) restricted to relevant instances + if (additionalInstanceCount == null) { + return 0; + } + + return accuracyMethod.getAccOrTooWeak2(coveredInstanceCount[0].trueCount, + coveredInstanceCount[0].falseCount, + additionalInstanceCount[0].trueCount, + additionalInstanceCount[0].falseCount, + noise); + + */ + + // computing R(A) and R(C) + CoverageCount[] cc = getCoverageCount(description, classInstances, superClassInstances); + if (cc == null) { + return 0; + } + return accuracyMethod.getAccOrTooWeak2(cc[0].trueCount, cc[0].falseCount, cc[1].trueCount, cc[1].falseCount, noise); + } + + public double getAccuracyOrTooWeak3(AccMethodThreeValued accuracyMethod, OWLClassExpression description, Set classInstances, Set superClassInstances, Set negatedClassInstances, double noise) { + if (accuracyMethod instanceof AccMethodApproximate) { + throw new RuntimeException(); + } else { + return getAccuracyOrTooWeakExact3(accuracyMethod, description, classInstances, superClassInstances, negatedClassInstances, noise); + } + } + + public double getAccuracyOrTooWeakExact3(AccMethodThreeValued accuracyMethod, OWLClassExpression description, Set classInstances, Set superClassInstances, Set negatedClassInstances, double noise) { + // implementation is based on: + // http://sunsite.informatik.rwth-aachen.de/Publications/CEUR-WS/Vol-426/swap2008_submission_14.pdf + // default negation should be turned off when using fast instance checker + // compute I_C (negated and non-negated concepts separately) + ReasoningUtils.Coverage3[] cc = getCoverage3(description, df.getOWLObjectComplementOf(description), Sets.union(classInstances, superClassInstances)); + // trueSet = icPos, falseSet = icNeg + if (cc == null) { // timeout + return 0; + } + // semantic precision + // first compute I_C \cap Cn(DC) + // it seems that in our setting, we can ignore Cn, because the examples (class instances) + // are already part of the background knowledge + Set tmp1Pos = Sets.intersection(cc[0].trueSet, classInstances); + Set tmp1Neg = Sets.intersection(cc[0].falseSet, negatedClassInstances); + // icPos + icNeg <===> all returned results + // --> precision = tmp1size / (icpos + icneg) + // classInstances + negatedClassInstances <==> all results that should be returned + // -> recall = tmp1size / (cI + ncI) + + // F_beta = true positives / (true positives + false negatives + false positives) + + // Cn(I_C) \cap D_C is the same set if we ignore Cn ... + // ---> @@@@ AccMethodGenFMeasure + return accuracyMethod.getAccOrTooWeak3(tmp1Pos.size(), tmp1Neg.size(), cc[0].trueCount, cc[0].falseCount, classInstances.size(), negatedClassInstances.size(), noise); + } +} diff --git a/components-core/src/main/java/org/dllearner/utilities/RootClassFinder.java b/components-core/src/main/java/org/dllearner/utilities/RootClassFinder.java deleted file mode 100644 index 60c3e93090..0000000000 --- a/components-core/src/main/java/org/dllearner/utilities/RootClassFinder.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.dllearner.utilities; - -import java.util.Set; - -import org.semanticweb.owlapi.model.OWLClass; - -public interface RootClassFinder { - Set getRootUnsatisfiableClasses(); - Set getDerivedUnsatisfiableClasses(); - Set getDependentChildClasses(OWLClass unsatClass); - -} diff --git a/components-core/src/main/java/org/dllearner/utilities/SPARULTranslator.java b/components-core/src/main/java/org/dllearner/utilities/SPARULTranslator.java deleted file mode 100644 index c426cf0781..0000000000 --- a/components-core/src/main/java/org/dllearner/utilities/SPARULTranslator.java +++ /dev/null @@ -1,83 +0,0 @@ -package org.dllearner.utilities; - -import java.util.List; - -import org.coode.owlapi.rdf.model.AbstractTranslator; -import org.coode.owlapi.rdf.model.RDFLiteralNode; -import org.coode.owlapi.rdf.model.RDFNode; -import org.coode.owlapi.rdf.model.RDFResourceNode; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLLiteral; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyChange; -import org.semanticweb.owlapi.model.OWLOntologyManager; -import org.semanticweb.owlapi.model.RemoveAxiom; - -public class SPARULTranslator extends AbstractTranslator { - - private StringBuilder sb; - - public SPARULTranslator(OWLOntologyManager manager, OWLOntology ontology, - boolean useStrongTyping) { - super(manager, ontology, useStrongTyping); - // TODO Auto-generated constructor stub - } - - public String translate(OWLOntologyChange change){ - sb = new StringBuilder(); - sb.append(change instanceof RemoveAxiom ? "DELETE DATA" : "INSERT DATA"); - sb.append("{"); - change.getAxiom().accept(this); - sb.append("}"); - sb.append("\n"); - - return sb.toString(); - } - - public String translate(List changes){ - sb = new StringBuilder(); - for(OWLOntologyChange change : changes){ - sb.append(change instanceof RemoveAxiom ? "DELETE DATA" : "INSERT DATA"); - sb.append("{"); - change.getAxiom().accept(this); - sb.append("}"); - sb.append("\n"); - } - - return sb.toString(); - } - - @Override - protected void addTriple(RDFResourceNode subject, RDFResourceNode pred, - RDFNode object) { - sb.append(subject).append(" ").append(pred).append(" ").append(object).append("\n"); - - } - - @Override - protected RDFResourceNode getAnonymousNode(Object key) { - return new RDFResourceNode(System.identityHashCode(key)); - } - - @Override - protected RDFResourceNode getPredicateNode(IRI iri) { - return new RDFResourceNode(iri); - } - - @Override - protected RDFResourceNode getResourceNode(IRI iri) { - return new RDFResourceNode(iri); - } - - @Override - protected RDFLiteralNode getLiteralNode(OWLLiteral literal) { - if(literal.getDatatype() != null){ - return new RDFLiteralNode(literal.toString(), literal.getDatatype().getIRI()); - } else { - return new RDFLiteralNode(literal.toString(), literal.getLang()); - } - - } - - -} \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/utilities/StringFormatter.java b/components-core/src/main/java/org/dllearner/utilities/StringFormatter.java index 1ae414bd61..ce1b7d476b 100644 --- a/components-core/src/main/java/org/dllearner/utilities/StringFormatter.java +++ b/components-core/src/main/java/org/dllearner/utilities/StringFormatter.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities; import java.io.File; @@ -31,10 +30,10 @@ public class StringFormatter { /** * formats a double value between 0 and 100 to a percentage * ex: 0.7854684 will be return 78.5% - * @param d + * @param d the double value */ public static String doubleToPercent(double d){ - return doubleToPercent( d, 1, true); + return doubleToPercent( d, 1, true); } public static String doubleToPercent(double d, int decimals){ @@ -54,11 +53,11 @@ public static String doubleToPercent(double d, int decimals){ public static String myReplaceAll(String s, char search, String replacement ){ String ret =""; char[] arr = s.toCharArray(); - for (int i = 0; i < arr.length; i++) { - if(arr[i]==search){ - ret+=replacement; - }else{ - ret+=arr[i]; + for (char anArr : arr) { + if (anArr == search) { + ret += replacement; + } else { + ret += anArr; } } return ret; @@ -74,8 +73,8 @@ public static String doubleToPercent(double d, int decimals, boolean addPercentS format += "0"; } format+="%"; - DecimalFormat df = new DecimalFormat( format ); - String ret = df.format(d); + DecimalFormat df = new DecimalFormat( format ); + String ret = df.format(d); ret = (addPercentSign)?ret:ret.replaceAll("%", ""); return ret; @@ -92,8 +91,8 @@ public static String doubleRound(double d, int decimals, String before, String a format += "0"; } - DecimalFormat df = new DecimalFormat( format ); - ret = df.format(d); + DecimalFormat df = new DecimalFormat( format ); + ret = df.format(d); ret = ret.replaceAll("%", ""); } ret = before + ret+ after; @@ -101,9 +100,9 @@ public static String doubleRound(double d, int decimals, String before, String a } - public static String convertStatPercentageToLatex(Stat s, + public static String convertStatPercentageToLatex(Stat s, int decimals, - boolean addPercentSign, + boolean addPercentSign, boolean includeSTDDeviation){ String ret =""; @@ -115,14 +114,14 @@ public static String convertStatPercentageToLatex(Stat s, return ret; } - public static String convertStatDoubleToLatex(Stat s, + public static String convertStatDoubleToLatex(Stat s, int decimals, boolean includeSTDDeviation){ return convertStatDoubleToLatex(s, decimals,"","",includeSTDDeviation); } - public static String convertStatDoubleToLatex(Stat s, + public static String convertStatDoubleToLatex(Stat s, int decimals, String before, String after, diff --git a/components-core/src/main/java/org/dllearner/utilities/ToIRIFunction.java b/components-core/src/main/java/org/dllearner/utilities/ToIRIFunction.java index 9f8634d57e..303c0d76f4 100644 --- a/components-core/src/main/java/org/dllearner/utilities/ToIRIFunction.java +++ b/components-core/src/main/java/org/dllearner/utilities/ToIRIFunction.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.utilities; diff --git a/components-core/src/main/java/org/dllearner/utilities/TreeUtils.java b/components-core/src/main/java/org/dllearner/utilities/TreeUtils.java new file mode 100644 index 0000000000..2002a8497e --- /dev/null +++ b/components-core/src/main/java/org/dllearner/utilities/TreeUtils.java @@ -0,0 +1,51 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.utilities; + +import java.util.Map; + +import org.dllearner.algorithms.celoe.OENode; +import org.dllearner.utilities.datastructures.AbstractSearchTree; + +public class TreeUtils { + + public static String toTreeString(AbstractSearchTree tree, + String baseURI, Map prefixes) { + return TreeUtils.toTreeString(tree.getRoot(), baseURI, prefixes); + } + public static String toTreeString(OENode node, + String baseURI, Map prefixes) { + return TreeUtils.toTreeString(node, 0, baseURI, prefixes).toString(); + } + + private static StringBuilder toTreeString(OENode node, + int depth, String baseURI, Map prefixes) { + StringBuilder treeString = new StringBuilder(); + for(int i=0; i "); + treeString.append(node.getShortDescription(baseURI, prefixes)).append("\n"); + for(OENode child : node.getChildren()) { + treeString.append(TreeUtils.toTreeString(child, depth+1, baseURI, prefixes)); + } + return treeString; + } + +} diff --git a/components-core/src/main/java/org/dllearner/utilities/URLencodeUTF8.java b/components-core/src/main/java/org/dllearner/utilities/URLencodeUTF8.java index 3878f11211..377bcab748 100644 --- a/components-core/src/main/java/org/dllearner/utilities/URLencodeUTF8.java +++ b/components-core/src/main/java/org/dllearner/utilities/URLencodeUTF8.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities; import java.io.UnsupportedEncodingException; diff --git a/components-core/src/main/java/org/dllearner/utilities/analyse/TypeOntology.java b/components-core/src/main/java/org/dllearner/utilities/analyse/TypeOntology.java index 3eabe53992..7170829caa 100644 --- a/components-core/src/main/java/org/dllearner/utilities/analyse/TypeOntology.java +++ b/components-core/src/main/java/org/dllearner/utilities/analyse/TypeOntology.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.utilities.analyse; import java.util.ArrayList; @@ -27,10 +45,10 @@ public class TypeOntology { private int addTypes(OntModel model) { int changes=0; - Set dataProperties = new HashSet(); - Set objectProperties = new HashSet(); - Set classes = new HashSet(); - Set individuals = new HashSet(); + Set dataProperties = new HashSet<>(); + Set objectProperties = new HashSet<>(); + Set classes = new HashSet<>(); + Set individuals = new HashSet<>(); Set triples = model.getGraph().find(Triple.ANY).toSet(); @@ -187,7 +205,7 @@ public static void main(String... args) { + " ! regex(str(?o), '^http://dbpedia.org/resource/Category') &&" + " ! regex(str(?o), '^http://dbpedia.org/resource/Template') ) . }"; OntModel model = ModelFactory.createOntologyModel(); - List individuals = new ArrayList(7); + List individuals = new ArrayList<>(7); individuals.add("http://dbpedia.org/resource/Philolaus"); individuals.add("http://dbpedia.org/resource/Zeno_of_Elea"); individuals.add("http://dbpedia.org/resource/Socrates"); diff --git a/components-core/src/main/java/org/dllearner/utilities/components/ComponentCombo.java b/components-core/src/main/java/org/dllearner/utilities/components/ComponentCombo.java deleted file mode 100644 index dc487a1e51..0000000000 --- a/components-core/src/main/java/org/dllearner/utilities/components/ComponentCombo.java +++ /dev/null @@ -1,148 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.utilities.components; - -import java.net.URL; -import java.util.HashSet; -import java.util.Set; - -import org.dllearner.algorithms.ocel.OCEL; -import org.dllearner.core.AbstractCELA; -import org.dllearner.core.AbstractClassExpressionLearningProblem; -import org.dllearner.core.AbstractKnowledgeSource; -import org.dllearner.core.AbstractReasonerComponent; -import org.dllearner.core.ComponentInitException; -import org.dllearner.core.LearningProblemUnsupportedException; -import org.dllearner.kb.OWLFile; -import org.dllearner.learningproblems.PosNegLP; -import org.dllearner.learningproblems.PosNegLPStandard; -import org.dllearner.reasoning.ClosedWorldReasoner; -import org.semanticweb.owlapi.model.OWLIndividual; - -/** - * A mix of components, which are typically combined to create a full - * learning task. - * - * Add more constructors if you like (they should be useful in general, - * not just for a very specific scenario). - * - * @author Jens Lehmann - * - */ -public class ComponentCombo { - - private Set sources; - private AbstractReasonerComponent reasoner; - private PosNegLP problem; - private AbstractCELA algorithm; - - /** - * Builds a component combination object from the specified components. - * @param source A knowledge source. - * @param reasoner A reasoner. - * @param problem A learning problem. - * @param algorithm A learning algorithm. - */ - public ComponentCombo(AbstractKnowledgeSource source, AbstractReasonerComponent reasoner, PosNegLP problem, AbstractCELA algorithm) { - this(getSourceSet(source), reasoner, problem, algorithm); - } - - /** - * Builds a component combination object from the specified components. - * @param sources A set of knowledge sources. - * @param reasoner A reasoner. - * @param problem A learning problem. - * @param algorithm A learning algorithm. - */ - public ComponentCombo(Set sources, AbstractReasonerComponent reasoner, PosNegLP problem, AbstractCELA algorithm) { - this.sources = sources; - this.reasoner = reasoner; - this.problem = problem; - this.algorithm = algorithm; - } - - private static Set getSourceSet(AbstractKnowledgeSource source) { - Set sources = new HashSet(); - sources.add(source); - return sources; - } - - /** - * Builds a standard combination of components. Currently, this is an OWL - * File, the FastInstanceChecker reasoning algorithm, a definition learning - * problem with positive and negative examples, and the example based - * refinement algorithm. - * @param owlFile URL of an OWL file (background knowledge). - * @param posExamples Set of positive examples. - * @param negExamples Set of negative examples. - */ - public ComponentCombo(URL owlFile, Set posExamples, Set negExamples) { - AbstractKnowledgeSource source = new OWLFile(); - sources = getSourceSet(source); - reasoner = new ClosedWorldReasoner(source); - problem = new PosNegLPStandard(reasoner); - problem.setPositiveExamples(posExamples); - problem.setNegativeExamples(negExamples); - algorithm = new OCEL(problem, reasoner); - } - - /** - * Initialise all components. - * @throws ComponentInitException Thrown if a component could not be initialised properly. - */ - public void initAll() throws ComponentInitException { - for(AbstractKnowledgeSource source : sources) { - source.init(); - } - reasoner.init(); - problem.init(); - algorithm.init(); - } - - /** - * @return the sources - */ - public Set getSources() { - return sources; - } - - /** - * @return the reasoner - */ - public AbstractReasonerComponent getReasoner() { - return reasoner; - } - - /** - * @return the problem - */ - public AbstractClassExpressionLearningProblem getProblem() { - return problem; - } - - /** - * @return the algorithm - */ - public AbstractCELA getAlgorithm() { - return algorithm; - } - - -} diff --git a/components-core/src/main/java/org/dllearner/utilities/components/ReasonerComponentFactory.java b/components-core/src/main/java/org/dllearner/utilities/components/ReasonerComponentFactory.java deleted file mode 100644 index 9af197e11d..0000000000 --- a/components-core/src/main/java/org/dllearner/utilities/components/ReasonerComponentFactory.java +++ /dev/null @@ -1,94 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.utilities.components; - -import java.io.File; -import java.net.MalformedURLException; -import java.net.URL; - -import org.dllearner.core.AbstractReasonerComponent; -import org.dllearner.core.ComponentInitException; -import org.dllearner.kb.OWLFile; -import org.dllearner.reasoning.ClosedWorldReasoner; -import org.dllearner.reasoning.OWLAPIReasoner; -import org.dllearner.reasoning.ReasonerImplementation; -import org.dllearner.reasoning.ReasonerType; - -/** - * Factory class for reasoners. - * - * @author Sebastian Hellmann - * @author Jens Lehmann - */ -public class ReasonerComponentFactory { - - /** - * Simple method for creating a reasoner component. - * - * @param ontologyFile URI or path to OWL ontology file. - * @param type Reasoner type. - * @return A reasoner component. - */ - public static AbstractReasonerComponent getReasonerComponent(String ontologyFile, ReasonerType type) { - AbstractReasonerComponent rc = null; - - try { - // knowledge source - OWLFile ks = new OWLFile(); - URL fileURL = new File(ontologyFile).toURI().toURL(); - ks.setURL(fileURL); - ks.init(); - - // reasoner component - switch (type) { - case CLOSED_WORLD_REASONER: - rc = new ClosedWorldReasoner(ks); - break; - case OWLAPI_FACT: - rc = new OWLAPIReasoner(ks); - ((OWLAPIReasoner) rc).setReasonerImplementation(ReasonerImplementation.JFACT); - break; - case OWLAPI_PELLET: - rc = new OWLAPIReasoner(ks); - break; - //case DIG: - //case FAST_RETRIEVAL: - //case KAON2: - //case OWLAPI_FUZZY: - case OWLAPI_HERMIT: - rc = new OWLAPIReasoner(ks); - ((OWLAPIReasoner) rc).setReasonerImplementation(ReasonerImplementation.HERMIT); - break; - //case SPARQL_NATIVE: - default: - rc = new ClosedWorldReasoner(ks); - break; - } - rc.init(); - } catch (MalformedURLException e) { - e.printStackTrace(); - } catch (ComponentInitException e) { - e.printStackTrace(); - } - - return rc; - } - -} diff --git a/components-core/src/main/java/org/dllearner/utilities/datastructures/AbstractSearchTree.java b/components-core/src/main/java/org/dllearner/utilities/datastructures/AbstractSearchTree.java new file mode 100644 index 0000000000..5f0396b734 --- /dev/null +++ b/components-core/src/main/java/org/dllearner/utilities/datastructures/AbstractSearchTree.java @@ -0,0 +1,156 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.utilities.datastructures; + +import org.dllearner.core.AbstractSearchTreeNode; + +import java.util.*; + +public class AbstractSearchTree { + + // all nodes in the search tree (used for selecting most promising node) + protected NavigableSet nodes; + + // the sort order on the set + protected Comparator sortOrderComp; + + // root of search tree + protected T root; + + /** + * create a new search tree + * @param comparator the comparator to use for the nodes + */ + public AbstractSearchTree(Comparator comparator) { + sortOrderComp = comparator; + } + + /** + * add node to the search tree + * @param parentNode the parent node or null if root + * @param node the node to add + */ + public void addNode(T parentNode, T node) { + // link to parent (unless start node) + if(parentNode == null) { + this.setRoot(node); + } else { + parentNode.addChild(node); + } + } + + /** + * internally used by tree<->node contract to notify a tree about an added node + * @param node the node + */ + public final void notifyNode(T node) { + if (node.getParent() == null || nodes.contains(node.getParent())) { + if (allowedNode(node)) + nodes.add(node); + } + } + + /** + * filter certain nodes to be permitted in the node-set + * @param node node to test + * @return whether node is allowed in the node-set + */ + protected boolean allowedNode(T node) { + return true; + } + + /** + * set the tree root to a node + * @param node the node + */ + public void setRoot(T node) { + if (this.root != null || !this.nodes.isEmpty()) { + throw new Error("Tree Root already set"); + } + this.root = node; + node.notifyTree(this); + } + + /** + * must be called before modifying a node, to support immutable set element pattern + * @param node the node + */ + public final void updatePrepare(T node) { + for (T child : (Collection)node.getChildren()) { + if (allowedNode(child)) + updatePrepare(child); + } + nodes.remove(node); + } + + /** + * must be called after modifying a node, to support immutable set element pattern + */ + public final void updateDone(T node) { + if (allowedNode(node)) { + nodes.add(node); + for (T child : (Collection)node.getChildren()) { + updateDone(child); + } + } + } + + /** + * @return an iterator over the elements in this search tree in descending comparison order + */ + public Iterator descendingIterator() { + return nodes.descendingIterator(); + } + + /** + * @return a set of the nodes in the search tree ordered in descending comparison order + */ + public SortedSet descendingSet() { + return nodes.descendingSet(); + } + + /** + * @return best node according to comparator + */ + public T best() { + return nodes.last(); + } + + /** + * @return the underlying set of all tree nodes + */ + public Set getNodeSet() { + return nodes; + } + + /** + * @return the tree size + */ + public int size() { + return nodes.size(); + } + + /** + * @return the tree root node + */ + public T getRoot() { + return root; + } + +} diff --git a/components-core/src/main/java/org/dllearner/utilities/datastructures/Datastructures.java b/components-core/src/main/java/org/dllearner/utilities/datastructures/Datastructures.java deleted file mode 100644 index c92a3ddb26..0000000000 --- a/components-core/src/main/java/org/dllearner/utilities/datastructures/Datastructures.java +++ /dev/null @@ -1,119 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.utilities.datastructures; - -import java.util.Arrays; -import java.util.Iterator; -import java.util.List; -import java.util.Set; -import java.util.TreeSet; - -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLObjectProperty; - -/** - * Conversion between different data structures. - * - * @author Jens Lehmann - * @author Sebastian Hellmann - * - */ -public class Datastructures { - - public static boolean strToBool(String str) { - if (str.equals("true")) - return true; - else if (str.equals("false")) - return false; - else - throw new Error("Cannot convert to boolean."); - } - - /** - * easy conversion - * - * @param s - */ - public static String[] setToArray(Set s) { - if(s==null)return null; - String[] ret=new String[s.size()]; - int i=0; - for (Iterator iter = s.iterator(); iter.hasNext();) { - ret[i] = iter.next(); - i++; - - } - return ret; - - } - - public static String[] sortedSet2StringListIndividuals(Set individuals){ - - String[] ret=new String[individuals.size()]; - Iterator i=individuals.iterator(); - int a=0; - while (i.hasNext()){ - ret[a++]=i.next().toStringID(); - } - Arrays.sort(ret); - return ret; - } - - public static String[] sortedSet2StringListRoles(Set s){ - - String[] ret=new String[s.size()]; - Iterator i=s.iterator(); - int a=0; - while (i.hasNext()){ - ret[a++]=i.next().toStringID(); - } - Arrays.sort(ret); - return ret; - } - - public static String[] sortedSet2StringListConcepts(Set s){ - - String[] ret=new String[s.size()]; - Iterator i=s.iterator(); - int a=0; - while (i.hasNext()){ - ret[a++]=i.next().toStringID(); - } - Arrays.sort(ret); - return ret; - } - - public static Set individualSetToStringSet(Set individuals) { - Set ret = new TreeSet(); - for(OWLIndividual ind : individuals) { - ret.add(ind.toStringID()); - } - return ret; - } - - public static Set individualListToStringSet(List individuals) { - Set ret = new TreeSet(); - for(OWLIndividual ind : individuals) { - ret.add(ind.toStringID()); - } - return ret; - } -} diff --git a/components-core/src/main/java/org/dllearner/utilities/datastructures/DescriptionSubsumptionTree.java b/components-core/src/main/java/org/dllearner/utilities/datastructures/DescriptionSubsumptionTree.java index 73e561f563..4ac05fab80 100644 --- a/components-core/src/main/java/org/dllearner/utilities/datastructures/DescriptionSubsumptionTree.java +++ b/components-core/src/main/java/org/dllearner/utilities/datastructures/DescriptionSubsumptionTree.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,16 +16,8 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.datastructures; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Comparator; -import java.util.List; -import java.util.SortedSet; -import java.util.TreeSet; - import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.dllearner.core.AbstractReasonerComponent; @@ -35,6 +27,9 @@ import org.dllearner.utilities.owl.OWLClassExpressionUtils; import org.semanticweb.owlapi.model.OWLClassExpression; +import javax.annotation.Nonnull; +import java.util.*; + /** * This class takes Descritptions and a reasoner and orders the * descriptions by subsumption into a tree, represented by the internal class "Node" @@ -77,7 +72,7 @@ public int compare(EvaluatedDescription o1, EvaluatedDescriptio * holds the nodes that are subclasses of this node. * ordered by accuracy */ - public SortedSet subClasses = new TreeSet(); + public SortedSet subClasses = new TreeSet<>(); public Node(EvaluatedDescription ed, boolean root) { this.root = root; @@ -105,7 +100,7 @@ public void insert(Node node) { logger.warn("Adding " + node.getEvalDesc() + "\n\t as subclass of " + this.getEvalDesc()); subClasses.add(node); } else { - SortedSet subClassesTmp = new TreeSet(subClasses); + SortedSet subClassesTmp = new TreeSet<>(subClasses); for (Node sub : subClassesTmp) { logger.warn("Testing relation between: " + node.getEvalDesc() + "\n\t and " + sub.getEvalDesc()); @@ -172,7 +167,7 @@ public String toString() { * @return */ public String _toString(String tab) { - StringBuffer ret = new StringBuffer(); + StringBuilder ret = new StringBuilder(); ret.append((root) ? "Thing\n" : tab + getEvalDesc() + "\n"); tab += " "; for (Node sub : subClasses) { @@ -208,7 +203,7 @@ public double getAccuracy() { } @Override - public int compareTo(Node node) { + public int compareTo(@Nonnull Node node) { if (this.equals(node)) { return 0; } @@ -279,14 +274,14 @@ public void insert(Collection> e public void insertEdPosNeg(Collection evaluatedDescriptions, int limit, double accuracyThreshold) { - List newSet = new ArrayList(); + List newSet = new ArrayList<>(); int i = 0; for (EvaluatedDescriptionPosNeg evaluatedDescription : evaluatedDescriptions) { if (i >= evaluatedDescriptions.size() || newSet.size() >= limit) { break; } if (evaluatedDescription.getAccuracy() > accuracyThreshold) { - newSet.add((EvaluatedDescription) evaluatedDescription); + newSet.add(evaluatedDescription); logger.warn(evaluatedDescription); } i++; diff --git a/components-core/src/main/java/org/dllearner/utilities/datastructures/MapToStringTupleListConverter.java b/components-core/src/main/java/org/dllearner/utilities/datastructures/MapToStringTupleListConverter.java index d851296d02..6bc9d041f5 100644 --- a/components-core/src/main/java/org/dllearner/utilities/datastructures/MapToStringTupleListConverter.java +++ b/components-core/src/main/java/org/dllearner/utilities/datastructures/MapToStringTupleListConverter.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.utilities.datastructures; import java.util.ArrayList; @@ -19,7 +37,7 @@ public class MapToStringTupleListConverter implements Converter convert(Map source) { - List result = new ArrayList(); + List result = new ArrayList<>(); Set keys = source.keySet(); diff --git a/components-core/src/main/java/org/dllearner/utilities/datastructures/Maps.java b/components-core/src/main/java/org/dllearner/utilities/datastructures/Maps.java deleted file mode 100644 index 55d56506ab..0000000000 --- a/components-core/src/main/java/org/dllearner/utilities/datastructures/Maps.java +++ /dev/null @@ -1,78 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.utilities.datastructures; - -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; - -/** - * @author Jens Lehmann - * - */ -public class Maps { - - /** - * Reverts a map, i.e. if the map contains an entry x => y, then the - * returned map contains an entry y => x. (The minimal map with this - * property is returned.) - * - * @param - * Type of map keys. - * @param - * Type of map values - * @param map - * The map to invert. - * @return A reverted map. - */ - public static Map> revert(Map map) { - Map> result = new HashMap>(); - - for (Map.Entry entry : map.entrySet()) { - X x = entry.getKey(); - Y y = entry.getValue(); - Collection s = result.get(y); - if (s == null) { - result.put(y, s = new HashSet()); - } - s.add(x); - } - return result; - } - - public static Map> revertCollectionMap(Map> map) { - Map> result = new HashMap>(); - - for (Map.Entry> entry : map.entrySet()) { - X x = entry.getKey(); - Collection y = entry.getValue(); - for (Y value : y) { - Collection s = result.get(value); - if (s == null) { - result.put(value, s = new HashSet()); - } - s.add(x); - } - } - return result; - } - -} diff --git a/components-core/src/main/java/org/dllearner/utilities/datastructures/PrefixMap.java b/components-core/src/main/java/org/dllearner/utilities/datastructures/PrefixMap.java deleted file mode 100644 index 59a1321bf9..0000000000 --- a/components-core/src/main/java/org/dllearner/utilities/datastructures/PrefixMap.java +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright 2006 Google Inc. All Rights Reserved. - -package org.dllearner.utilities.datastructures; - -/** - * Maps string prefixes to values. For example, if you {@code put("foo", 1)}, - * {@code get("foobar")} returns {@code 1}. Prohibits null values. - * - *

Use instead of iterating over a series of string prefixes calling - * {@code String.startsWith(prefix)}. - * - * @author crazybob@google.com (Bob Lee) - */ -public interface PrefixMap { - /** - * Maps prefix to value. - * - * @return The previous value stored for this prefix, or null if none. - * @throws IllegalArgumentException if prefix is an empty string. - */ - T put(CharSequence prefix, T value); - - /** - * Finds a prefix that matches {@code s} and returns the mapped value. - * - * If multiple prefixes in the map match {@code s}, the longest match wins. - * - * @return value for prefix matching {@code s} or {@code null} if none match. - */ - T get(CharSequence s); -} \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/utilities/datastructures/PrefixTrie.java b/components-core/src/main/java/org/dllearner/utilities/datastructures/PrefixTrie.java deleted file mode 100644 index e50256d2b8..0000000000 --- a/components-core/src/main/java/org/dllearner/utilities/datastructures/PrefixTrie.java +++ /dev/null @@ -1,191 +0,0 @@ -// Copyright 2006 Google Inc. All Rights Reserved. - -package org.dllearner.utilities.datastructures; - -import java.util.Map; - -/** - * Trie implementation supporting CharSequences as prefixes. - * - * Prefixes are sequences of characters, and the set of allowed characters is - * specified as a range of sequential characters. By default, any seven-bit - * character may appear in a prefix, and so the trie is a 128-ary tree. - * - * @author crazybob@google.com (Bob Lee) - * @author mharris@google.com (Matthew Harris) - */ -public class PrefixTrie implements PrefixMap { - // The set of allowed characters in prefixes is given by a range of - // consecutive characters. rangeOffset denotes the beginning of the range, - // and rangeSize gives the number of characters in the range, which is used as - // the number of children of each node. - private final char rangeOffset; - private final int rangeSize; - - private final Node root; - - /** - * Constructs a trie for holding strings of seven-bit characters. - */ - public PrefixTrie() { - rangeOffset = '\0'; - rangeSize = 128; - root = new Node(rangeSize); - } - - /** - * Constructs a trie for holding strings of characters. - * - * The set of characters allowed in prefixes is given by the range - * [rangeOffset, lastCharInRange], inclusive. - * - * @param firstCharInRange - * @param lastCharInRange - */ - public PrefixTrie(char firstCharInRange, char lastCharInRange) { - this.rangeOffset = firstCharInRange; - this.rangeSize = lastCharInRange - firstCharInRange + 1; - - if (rangeSize <= 0) { - throw new IllegalArgumentException("Char range must include some chars"); - } - - root = new Node(rangeSize); - } - - /** - * {@inheritDoc} - * - * @throws IllegalArgumentException if prefix contains a character outside the - * range of legal prefix characters. - */ - public T put(CharSequence prefix, T value) { - if (value == null) { - throw new NullPointerException(); - } - - Node current = root; - for (int i = 0; i < prefix.length(); i++) { - int nodeIndex = prefix.charAt(i) - rangeOffset; - try { - Node next = current.next[nodeIndex]; - if (next == null) { - next = current.next[nodeIndex] = new Node(rangeSize); - } - current = next; - } catch (ArrayIndexOutOfBoundsException e) { - throw new IllegalArgumentException( - "'" + prefix.charAt(i) + "' is not a legal prefix character."); - } - } - T oldValue = current.value; - current.value = value; - return oldValue; - } - - public boolean contains(CharSequence s) { - Node current = root; - for (int i = 0; i < s.length(); i++) { - int nodeIndex = s.charAt(i) - rangeOffset; - if (nodeIndex < 0 || rangeSize <= nodeIndex) { - return false; - } - current = current.next[nodeIndex]; - if (current == null) { - return false; - } - } - return (current.value!=null); - } - - /** {@inheritDoc} */ - public T get(CharSequence s) { - Node deepestWithValue = root; - Node current = root; - for (int i = 0; i < s.length(); i++) { - int nodeIndex = s.charAt(i) - rangeOffset; - if (nodeIndex < 0 || rangeSize <= nodeIndex) { - return null; - } - current = current.next[nodeIndex]; - if (current == null) { - break; - } - if (current.value != null) { - deepestWithValue = current; - } - } - return deepestWithValue.value; - } - - public CharSequence getLongestMatch(CharSequence s) { - Node current = root; - int i; - int end = 0; - for (i = 0; i < s.length(); i++) { - int nodeIndex = s.charAt(i) - rangeOffset; - if (nodeIndex < 0 || rangeSize <= nodeIndex) - return null; - - current = current.next[nodeIndex]; - if (current == null) - break; - - if (current.value != null) - end = i; - } - if (end==0) - return null; - else - return s.subSequence(0, end+1); - } - - /** - * Returns a Map containing the same data as this structure. - * - * This implementation constructs and populates an entirely new map rather - * than providing a map view on the trie, so this is mostly useful for - * debugging. - * - * @return A Map mapping each prefix to its corresponding value. - */ - public Map toMap() { - Map map = com.google.common.collect.Maps.newTreeMap(); - addEntries(root, new StringBuilder(), map); - return map; - } - - /** - * Adds to the given map all entries at or below the given node. - * - * @param node - * @param builder A StringBuilder containing the prefix for the given node. - * @param map - */ - private void addEntries(Node node, - StringBuilder builder, - Map map) { - if (node.value != null) { - map.put(builder.toString(), node.value); - } - - for (int i = 0; i < node.next.length; i++) { - Node next = node.next[i]; - if (next != null) { - builder.append((char) (i + rangeOffset)); - addEntries(next, builder, map); - builder.deleteCharAt(builder.length() - 1); - } - } - } - - private static class Node { - T value; - final Node[] next; - - @SuppressWarnings("unchecked") - Node(int numChildren) { - next = new Node[numChildren]; - } - } -} \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/utilities/datastructures/RDFNodeTuple.java b/components-core/src/main/java/org/dllearner/utilities/datastructures/RDFNodeTuple.java index 09819df2e3..f62114078d 100644 --- a/components-core/src/main/java/org/dllearner/utilities/datastructures/RDFNodeTuple.java +++ b/components-core/src/main/java/org/dllearner/utilities/datastructures/RDFNodeTuple.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,12 +16,12 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.datastructures; -import org.dllearner.kb.extraction.LiteralNode; - +import com.google.common.collect.ComparisonChain; import com.hp.hpl.jena.rdf.model.RDFNode; +import com.hp.hpl.jena.sparql.util.NodeComparator; +import org.dllearner.kb.extraction.LiteralNode; /** * A container which can hold two Strings, mainly used as a helper. @@ -44,14 +44,15 @@ public String toString() { } public boolean equals(RDFNodeTuple t) { - return ((b.toString().equals(t.b.toString())) && (a.toString().equals(t.a))); + return b.equals(t.b) && a.equals(t.a); } - - public int compareTo(RDFNodeTuple t){ - int comp = a.toString().compareTo(t.a.toString()); - if( comp == 0 ){ - return b.toString().compareTo(t.b.toString()); - }else return comp; + + public int compareTo(RDFNodeTuple t) { + NodeComparator comparator = new NodeComparator(); + return ComparisonChain.start(). + compare(a.asNode(), t.a.asNode(), comparator). + compare(b.asNode(), t.b.asNode(), comparator). + result(); } public boolean aPartContains(String partOf) { diff --git a/components-core/src/main/java/org/dllearner/utilities/datastructures/ResultConceptSorter.java b/components-core/src/main/java/org/dllearner/utilities/datastructures/ResultConceptSorter.java deleted file mode 100644 index cb144bcfcf..0000000000 --- a/components-core/src/main/java/org/dllearner/utilities/datastructures/ResultConceptSorter.java +++ /dev/null @@ -1,107 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.utilities.datastructures; - -import java.util.SortedSet; - -// class is not used anywhere and is not documented - delete? -public class ResultConceptSorter implements Comparable { - String concept; - SortedSet instances; - double accuracy; - double accuracy2; - int nrOfInstances; - SortedSet coveredInRest; - SortedSet possibleNewCandidates; - SortedSet notCoveredInTotal; - - - public ResultConceptSorter(String concept, SortedSet instances, double accuracy, - double accuracy2, int nrOfInstances, SortedSet coveredInRest, - SortedSet possibleNewCandidates, SortedSet notCoveredInTotal) { - super(); - this.concept = concept; - this.instances = instances; - this.accuracy = accuracy; - this.accuracy2 = accuracy2; - this.nrOfInstances = nrOfInstances; - this.coveredInRest = coveredInRest; - this.possibleNewCandidates = possibleNewCandidates; - this.notCoveredInTotal = notCoveredInTotal; - } - - - - - public int compareTo(ResultConceptSorter in) { - ResultConceptSorter obj = in; - if(obj.accuracy > this.accuracy) return 1; - else if(obj.accuracy == this.accuracy){ - - if(obj.nrOfInstancesthis.nrOfInstances)return -1; - else return 1; - //if(obj.nrOfInstances==this.nrOfInstances)return 0; - } - else {//if(obj.accuracy < this.accuracy){ - return -1; - } - - } - - - - - public String toStringFull(){ - String ret=""; - ret+="concept\t"+concept+"\n"; - ret+="instances\t"+instances+"\n"; - ret+="accuracy\t"+accuracy+"\n"; - ret+="nrOfInstances\t"+nrOfInstances+"\n"; - ret+="accuracy2\t"+accuracy2+"\n"; - ret+="coveredInRest("+coveredInRest.size()+")\t"+coveredInRest+"\n"; - ret+="possibleNewCandidates("+possibleNewCandidates.size()+")\t"+possibleNewCandidates+"\n"; - ret+="notCoveredInTotal("+notCoveredInTotal.size()+")\t"+notCoveredInTotal+"\n"; - - return ret; - - } - - @Override - public String toString(){ - String ret=""; - ret+="concept\t"+concept+"\n"; - //ret+="instances\t"+instances+"\n"; - ret+="accuracy\t"+accuracy+"\n"; - ret+="nrOfInstances\t"+nrOfInstances+"\n"; - ret+="accuracy2\t"+accuracy2+"\n"; - //ret+="coveredInRest("+coveredInRest.size()+")\t"+coveredInRest+"\n"; - //ret+="possibleNewCandidates("+possibleNewCandidates.size()+")\t"+possibleNewCandidates+"\n"; - //ret+="notCoveredInTotal("+notCoveredInTotal.size()+")\t"+notCoveredInTotal+"\n"; - - return ret; - - } - - - - - -} diff --git a/components-core/src/main/java/org/dllearner/utilities/datastructures/SearchTree.java b/components-core/src/main/java/org/dllearner/utilities/datastructures/SearchTree.java new file mode 100644 index 0000000000..87da566755 --- /dev/null +++ b/components-core/src/main/java/org/dllearner/utilities/datastructures/SearchTree.java @@ -0,0 +1,33 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.utilities.datastructures; + +import java.util.Comparator; +import java.util.TreeSet; + +import org.dllearner.core.AbstractSearchTreeNode; + +public class SearchTree extends AbstractSearchTree { + + public SearchTree(Comparator comparator) { + super(comparator); + nodes = new TreeSet<>(sortOrderComp); + } + +} diff --git a/components-core/src/main/java/org/dllearner/algorithms/SearchTreeNode.java b/components-core/src/main/java/org/dllearner/utilities/datastructures/SearchTreeNode.java similarity index 86% rename from components-core/src/main/java/org/dllearner/algorithms/SearchTreeNode.java rename to components-core/src/main/java/org/dllearner/utilities/datastructures/SearchTreeNode.java index 159b55dcd8..1abfd31941 100644 --- a/components-core/src/main/java/org/dllearner/algorithms/SearchTreeNode.java +++ b/components-core/src/main/java/org/dllearner/utilities/datastructures/SearchTreeNode.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,8 +16,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - -package org.dllearner.algorithms; +package org.dllearner.utilities.datastructures; import java.util.Collection; @@ -35,11 +34,11 @@ public interface SearchTreeNode { * Gets the OWL 2 class expression at this search tree node. * @return The expression at this node. */ - public OWLClassExpression getExpression(); + OWLClassExpression getExpression(); /** * The children of this node. * @return The children of this node. */ - public Collection getChildren(); + Collection getChildren(); } diff --git a/components-core/src/main/java/org/dllearner/utilities/CommonPrefixMap.java b/components-core/src/main/java/org/dllearner/utilities/datastructures/SearchTreeNonWeak.java similarity index 52% rename from components-core/src/main/java/org/dllearner/utilities/CommonPrefixMap.java rename to components-core/src/main/java/org/dllearner/utilities/datastructures/SearchTreeNonWeak.java index 7b91f1e125..e5af7a7092 100644 --- a/components-core/src/main/java/org/dllearner/utilities/CommonPrefixMap.java +++ b/components-core/src/main/java/org/dllearner/utilities/datastructures/SearchTreeNonWeak.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,29 +16,26 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +package org.dllearner.utilities.datastructures; -package org.dllearner.utilities; +import org.dllearner.core.AbstractSearchTreeNode; -import java.util.HashMap; +import java.util.Comparator; /** - * Can be used as base for a prefix map. - * - * TODO: We might implement a version of this class, which is synchronised with prefix.cc. - * - * @author Jens Lehmann + * A Search Tree which does not maintain weak nodes in its set * + * @param */ -public class CommonPrefixMap extends HashMap { +public class SearchTreeNonWeak extends SearchTree { - private static final long serialVersionUID = 5434065917532534702L; + public SearchTreeNonWeak(Comparator comparator) { + super(comparator); + } - public CommonPrefixMap() { - put("dbpedia","http://dbpedia.org/resource/"); - put("dbp","http://dbpedia.org/property/"); - put("dbo","http://dbpedia.org/ontology/"); - put("yago","http://dbpedia.org/class/yago/"); - put("gml","http://www.opengis.net/gml/"); + @Override + protected boolean allowedNode(T node) { + return super.allowedNode(node) && !node.isTooWeak(); } - + } diff --git a/components-core/src/main/java/org/dllearner/utilities/datastructures/TrainTestList.java b/components-core/src/main/java/org/dllearner/utilities/datastructures/SearchTreeNonWeakPartialSet.java similarity index 50% rename from components-core/src/main/java/org/dllearner/utilities/datastructures/TrainTestList.java rename to components-core/src/main/java/org/dllearner/utilities/datastructures/SearchTreeNonWeakPartialSet.java index 8217456e55..60561c5279 100644 --- a/components-core/src/main/java/org/dllearner/utilities/datastructures/TrainTestList.java +++ b/components-core/src/main/java/org/dllearner/utilities/datastructures/SearchTreeNonWeakPartialSet.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,41 +16,22 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.datastructures; -import java.util.LinkedList; -import java.util.List; +import java.util.Collection; +import java.util.Comparator; -import org.semanticweb.owlapi.model.OWLIndividual; +import org.dllearner.core.AbstractSearchTreeNode; -/** - * A tuple of training set and test set. - * - * @author Jens Lehmann - * - */ -public class TrainTestList { +public class SearchTreeNonWeakPartialSet + extends SearchTreeNonWeak implements SearchTreePartialSet { - private List trainList; - private List testList; - - public TrainTestList() { - trainList = new LinkedList(); - testList = new LinkedList(); - } - - public TrainTestList(List trainList, List testList) { - this.trainList = trainList; - this.testList = testList; + public SearchTreeNonWeakPartialSet(Comparator comparator) { + super(comparator); } - public List getTrainList() { - return trainList; + public void retainAll(Collection promisingNodes) { + this.nodes.retainAll(promisingNodes); } - public List getTestList() { - return testList; - } - } diff --git a/components-core/src/main/java/org/dllearner/utilities/datastructures/SearchTreePartialSet.java b/components-core/src/main/java/org/dllearner/utilities/datastructures/SearchTreePartialSet.java new file mode 100644 index 0000000000..0c7ad37db1 --- /dev/null +++ b/components-core/src/main/java/org/dllearner/utilities/datastructures/SearchTreePartialSet.java @@ -0,0 +1,25 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.utilities.datastructures; + +import java.util.Collection; + +public interface SearchTreePartialSet { + void retainAll(Collection promisingNodes); +} diff --git a/components-core/src/main/java/org/dllearner/utilities/datastructures/SetManipulation.java b/components-core/src/main/java/org/dllearner/utilities/datastructures/SetManipulation.java index 31963bebe0..6ca4884c16 100644 --- a/components-core/src/main/java/org/dllearner/utilities/datastructures/SetManipulation.java +++ b/components-core/src/main/java/org/dllearner/utilities/datastructures/SetManipulation.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,18 +16,12 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.datastructures; -import java.util.Collection; -import java.util.List; import java.util.Random; import java.util.SortedSet; import java.util.TreeSet; -import org.apache.log4j.Level; -import org.apache.log4j.Logger; -import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLIndividual; import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; @@ -40,14 +34,14 @@ public class SetManipulation { * shrinks a set to the limit fuzzy here means the elements will be randomly * picked * - * @param set - * @param limit + * @param set the set + * @param limit the limit */ public static SortedSet fuzzyShrink(SortedSet set, int limit) { if (set.size() <= limit) { return set; } - SortedSet ret = new TreeSet(); + SortedSet ret = new TreeSet<>(); Random r = new Random(); double treshold = ((double) limit) / set.size(); // System.out.println("treshold"+howmany); @@ -69,15 +63,15 @@ public static SortedSet fuzzyShrink(SortedSet set, int limit) { /** * shrinks a set to the limit fuzzy here means the elements will be randomly * picked - * - * @param set - * @param limit + * + * @param set the set + * @param limit the limit */ public static SortedSet fuzzyShrinkInd(SortedSet set, int limit) { if (set.size() <= limit) { return set; } - SortedSet ret = new TreeSet(); + SortedSet ret = new TreeSet<>(); Random r = new Random(); double treshold = ((double) limit) / set.size(); // System.out.println("treshold"+howmany); @@ -98,16 +92,16 @@ public static SortedSet fuzzyShrinkInd(SortedSet s /** * shrinks a set to the limit takes the first elements up to limit - * - * @param set - * @param limit + * + * @param set the set + * @param limit the limit */ public static SortedSet stableShrink(SortedSet set, int limit) { if (set.size() <= limit) { return set; } - SortedSet ret = new TreeSet(); + SortedSet ret = new TreeSet<>(); for (T oneInd : set) { ret.add(oneInd); @@ -119,16 +113,16 @@ public static SortedSet stableShrink(SortedSet set, /** * shrinks a set to the limit takes the first elements up to limit - * - * @param set - * @param limit + * + * @param set the set + * @param limit the limit */ public static SortedSet stableShrinkInd(SortedSet set, int limit) { if (set.size() <= limit) { return set; } - SortedSet ret = new TreeSet(); + SortedSet ret = new TreeSet<>(); for (OWLIndividual oneInd : set) { ret.add(oneInd); @@ -140,57 +134,4 @@ public static SortedSet stableShrinkInd(SortedSet return ret; } - /** - * XXX - * getFirst n Elements from list. - * changes the list object!!! - * @param list - * @param nrElements - * @return returns the list shrunken to size. - */ - public static List getFirst(List list, int nrElements) { - int size; - while ((size = list.size()) > nrElements) { - list.remove(size - 1); - } - return list; - } - - public static SortedSet stringToInd(SortedSet set) { - SortedSet ret = new TreeSet(); - for (String ind : set) { - ret.add(df.getOWLNamedIndividual(IRI.create(ind))); - } - return ret; - } - - public static SortedSet stringToInd(Collection individualsAsString) { - SortedSet ret = new TreeSet(); - for (String ind : individualsAsString) { - ret.add(df.getOWLNamedIndividual(IRI.create(ind))); - } - return ret; - } - - public static SortedSetindToString(SortedSet set) { - SortedSet ret = new TreeSet(); - for (OWLIndividual ind : set) { - ret.add(ind.toString()); - } - return ret; - } - - public static void printSet(String s, SortedSet set, Logger logger) { - if(logger.getLevel().equals(Level.DEBUG)){ - logger.info(s +" ["+ set.size()+"]: "+set); - }else{ - logger.info(s +" ["+ set.size()+"]"); - } - - } - - public static void printSet(String s, SortedSet set) { - System.out.println(s +" ["+ set.size()+"]: "+set); - - } } diff --git a/components-core/src/main/java/org/dllearner/utilities/datastructures/SortedSetTuple.java b/components-core/src/main/java/org/dllearner/utilities/datastructures/SortedSetTuple.java index 978813f590..2bf0a1249c 100755 --- a/components-core/src/main/java/org/dllearner/utilities/datastructures/SortedSetTuple.java +++ b/components-core/src/main/java/org/dllearner/utilities/datastructures/SortedSetTuple.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,14 +16,13 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.datastructures; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; -import org.dllearner.utilities.Helper; +import com.google.common.collect.Sets; /** * @@ -39,13 +38,13 @@ public class SortedSetTuple { private SortedSet negSet; public SortedSetTuple() { - posSet = new TreeSet(); - negSet = new TreeSet(); + posSet = new TreeSet<>(); + negSet = new TreeSet<>(); } public SortedSetTuple(Set posSet, Set negSet) { - this.posSet = new TreeSet(posSet); - this.negSet = new TreeSet(negSet); + this.posSet = new TreeSet<>(posSet); + this.negSet = new TreeSet<>(negSet); } public SortedSet getPosSet() { @@ -56,8 +55,8 @@ public SortedSet getNegSet() { return negSet; } - public SortedSet getCompleteSet() { - return Helper.union(posSet, negSet); + public Set getCompleteSet() { + return Sets.union(posSet, negSet); } } diff --git a/components-core/src/main/java/org/dllearner/utilities/datastructures/StringTuple.java b/components-core/src/main/java/org/dllearner/utilities/datastructures/StringTuple.java index afd74a0373..d4141aa3fc 100644 --- a/components-core/src/main/java/org/dllearner/utilities/datastructures/StringTuple.java +++ b/components-core/src/main/java/org/dllearner/utilities/datastructures/StringTuple.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.datastructures; /** diff --git a/components-core/src/main/java/org/dllearner/utilities/datastructures/SynchronizedSearchTree.java b/components-core/src/main/java/org/dllearner/utilities/datastructures/SynchronizedSearchTree.java new file mode 100644 index 0000000000..fd9bc9bf5e --- /dev/null +++ b/components-core/src/main/java/org/dllearner/utilities/datastructures/SynchronizedSearchTree.java @@ -0,0 +1,35 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.utilities.datastructures; + +import java.util.Comparator; +import java.util.TreeSet; + +import org.dllearner.core.AbstractSearchTreeNode; + +import com.google.common.collect.Sets; + +public class SynchronizedSearchTree extends AbstractSearchTree { + + public SynchronizedSearchTree(Comparator comparator) { + super(comparator); + nodes = Sets.synchronizedNavigableSet(new TreeSet<>(sortOrderComp)); + } + +} diff --git a/components-core/src/main/java/org/dllearner/Info.java b/components-core/src/main/java/org/dllearner/utilities/datastructures/WeakSearchTreeNode.java similarity index 77% rename from components-core/src/main/java/org/dllearner/Info.java rename to components-core/src/main/java/org/dllearner/utilities/datastructures/WeakSearchTreeNode.java index 09abc86e9e..b658709871 100644 --- a/components-core/src/main/java/org/dllearner/Info.java +++ b/components-core/src/main/java/org/dllearner/utilities/datastructures/WeakSearchTreeNode.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,12 +16,8 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +package org.dllearner.utilities.datastructures; - -// File is updated automatically when a new version is created -package org.dllearner; - -public class Info { - public static final String build = "2010-08-07"; +public interface WeakSearchTreeNode extends SearchTreeNode { + boolean isTooWeak(); } - \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/utilities/examples/AutomaticNegativeExampleFinderOWL.java b/components-core/src/main/java/org/dllearner/utilities/examples/AutomaticNegativeExampleFinderOWL.java index 0046e86f6e..e3b62ccf6b 100644 --- a/components-core/src/main/java/org/dllearner/utilities/examples/AutomaticNegativeExampleFinderOWL.java +++ b/components-core/src/main/java/org/dllearner/utilities/examples/AutomaticNegativeExampleFinderOWL.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.examples; import java.util.Collection; @@ -40,12 +39,12 @@ public class AutomaticNegativeExampleFinderOWL { private SortedSet fullPositiveSet; - private SortedSet fromRelated = new TreeSet(); - private SortedSet fromSuperclasses = new TreeSet(); - private SortedSet fromParallelClasses = new TreeSet(); - private SortedSet fromAllOther = new TreeSet(); - private SortedSet fromDomain = new TreeSet(); - private SortedSet fromRange = new TreeSet(); + private SortedSet fromRelated = new TreeSet<>(); + private SortedSet fromSuperclasses = new TreeSet<>(); + private SortedSet fromParallelClasses = new TreeSet<>(); + private SortedSet fromAllOther = new TreeSet<>(); + private SortedSet fromDomain = new TreeSet<>(); + private SortedSet fromRange = new TreeSet<>(); static int poslimit = 10; static int neglimit = 20; @@ -62,7 +61,7 @@ public AutomaticNegativeExampleFinderOWL( SortedSet fullPositiveSet, AbstractReasonerComponent reasoningService) { super(); - this.fullPositiveSet = new TreeSet(); + this.fullPositiveSet = new TreeSet<>(); this.fullPositiveSet.addAll(fullPositiveSet); this.reasoningService = reasoningService; @@ -86,7 +85,7 @@ public SortedSet getNegativeExamples(int neglimit, boolean forceN * @param forceNegLimit forces that exactly neglimit instances are returned by adding more instances */ public SortedSet getNegativeExamples(int neglimit, boolean stable, boolean forceNegLimit ) { - SortedSet negatives = new TreeSet(); + SortedSet negatives = new TreeSet<>(); negatives.addAll(fromParallelClasses); negatives.addAll(fromRelated); negatives.addAll(fromSuperclasses); @@ -178,7 +177,7 @@ public void makeNegativeExamplesFromParallelClasses(SortedSet pos @SuppressWarnings("unused") private void makeNegativeExamplesFromClassesOfInstances(SortedSet positiveSet) { logger.debug("making neg Examples from parallel classes"); - SortedSet classes = new TreeSet(); + SortedSet classes = new TreeSet<>(); this.fromParallelClasses.clear(); for (OWLIndividual instance : positiveSet) { diff --git a/components-core/src/main/java/org/dllearner/utilities/examples/AutomaticNegativeExampleFinderSPARQL.java b/components-core/src/main/java/org/dllearner/utilities/examples/AutomaticNegativeExampleFinderSPARQL.java index 07ec9b0f66..df162d2459 100644 --- a/components-core/src/main/java/org/dllearner/utilities/examples/AutomaticNegativeExampleFinderSPARQL.java +++ b/components-core/src/main/java/org/dllearner/utilities/examples/AutomaticNegativeExampleFinderSPARQL.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.examples; import java.util.Iterator; @@ -39,14 +38,14 @@ public class AutomaticNegativeExampleFinderSPARQL { private SortedSet fullPositiveSet; - private SortedSet fromRelated = new TreeSet(); - private SortedSet fromNearbyClasses = new TreeSet(); - private SortedSet fromSuperclasses = new TreeSet();; - private SortedSet fromParallelClasses = new TreeSet();; - private SortedSet fromRandom = new TreeSet();; - private SortedSet fromDomain = new TreeSet();; - private SortedSet fromRange = new TreeSet();; - + private SortedSet fromRelated = new TreeSet<>(); + private SortedSet fromNearbyClasses = new TreeSet<>(); + private SortedSet fromSuperclasses = new TreeSet<>(); + private SortedSet fromParallelClasses = new TreeSet<>(); + private SortedSet fromRandom = new TreeSet<>(); + private SortedSet fromDomain = new TreeSet<>(); + private SortedSet fromRange = new TreeSet<>(); + static int poslimit = 10; static int neglimit = 20; @@ -61,7 +60,7 @@ public AutomaticNegativeExampleFinderSPARQL( SortedSet fullPositiveSet, SPARQLTasks st, SortedSet filterClasses) { super(); - this.fullPositiveSet = new TreeSet(); + this.fullPositiveSet = new TreeSet<>(); this.fullPositiveSet.addAll(fullPositiveSet); this.sparqltasks = st; this.filterClasses=filterClasses; @@ -86,7 +85,7 @@ public SortedSet getNegativeExamples(int neglimit ) { * @param stable decides whether neg Examples are randomly picked, default false, faster for developing, since the cache can be used */ public SortedSet getNegativeExamples(int neglimit, boolean stable ) { - SortedSet negatives = new TreeSet(); + SortedSet negatives = new TreeSet<>(); negatives.addAll(fromNearbyClasses); negatives.addAll(fromParallelClasses); negatives.addAll(fromRelated); @@ -162,7 +161,7 @@ private void makeNegativeExamplesFromRelatedInstances(String oneInstance, String }*/ public void makeNegativeExamplesFromNearbyClasses(SortedSet positiveSet, int sparqlResultLimit){ - SortedSet classes = new TreeSet(); + SortedSet classes = new TreeSet<>(); Iterator instanceIter = positiveSet.iterator(); while(classes.isEmpty() && instanceIter.hasNext()) { classes.addAll(sparqltasks.getClassesForInstance(instanceIter.next(), 100)); @@ -171,14 +170,12 @@ public void makeNegativeExamplesFromNearbyClasses(SortedSet positiveSet, if (filterClasses!=null&&filterClasses.size()>0){ boolean br=false; for (String oneClass : classes){ - Iterator iter=filterClasses.iterator(); - while (iter.hasNext()){ - if (oneClass.startsWith(iter.next())){ + for (String filterClass : filterClasses) { + if (oneClass.startsWith(filterClass)) { break; - } - else{ - concept=oneClass; - br=true; + } else { + concept = oneClass; + br = true; break; } } @@ -188,7 +185,7 @@ public void makeNegativeExamplesFromNearbyClasses(SortedSet positiveSet, concept = concept.replaceAll("\"", ""); SortedSet superClasses = sparqltasks.getSuperClasses(concept, 1); - classes = new TreeSet(); + classes = new TreeSet<>(); for (String oneSuperClass : superClasses) { classes.addAll(sparqltasks.getSubClasses(oneSuperClass, 1)); } @@ -197,7 +194,7 @@ public void makeNegativeExamplesFromNearbyClasses(SortedSet positiveSet, try{ fromNearbyClasses.addAll(sparqltasks.retrieveInstancesForClassDescription("\"" + oneClass + "\"", sparqlResultLimit)); - } catch (Exception e){} + } catch (Exception e){e.printStackTrace();} } this.fromNearbyClasses.removeAll(fullPositiveSet); @@ -216,7 +213,7 @@ public void makeNegativeExamplesFromParallelClasses(SortedSet positiveSe private void makeNegativeExamplesFromClassesOfInstances(SortedSet positiveSet, int sparqlResultLimit) { logger.debug("making neg Examples from parallel classes"); - SortedSet classes = new TreeSet(); + SortedSet classes = new TreeSet<>(); // superClasses.add(concept.replace("\"", "")); // logger.debug("before"+superClasses); // superClasses = dbpediaGetSuperClasses( superClasses, 4); @@ -260,7 +257,7 @@ private void makeNegativeExamplesFromClassesOfInstances(SortedSet positi */ public void makeNegativeExamplesFromSuperClassesOfInstances(SortedSet positiveSet, int sparqlResultSetLimit) { - SortedSet classes = new TreeSet(); + SortedSet classes = new TreeSet<>(); Iterator instanceIter = positiveSet.iterator(); while(classes.isEmpty() && instanceIter.hasNext()) { classes.addAll(sparqltasks.getClassesForInstance(instanceIter.next(), sparqlResultSetLimit)); diff --git a/components-core/src/main/java/org/dllearner/utilities/examples/AutomaticNegativeExampleFinderSPARQL2.java b/components-core/src/main/java/org/dllearner/utilities/examples/AutomaticNegativeExampleFinderSPARQL2.java index 90a6277006..45a1e5f27b 100644 --- a/components-core/src/main/java/org/dllearner/utilities/examples/AutomaticNegativeExampleFinderSPARQL2.java +++ b/components-core/src/main/java/org/dllearner/utilities/examples/AutomaticNegativeExampleFinderSPARQL2.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,14 +16,12 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.examples; import static org.dllearner.utilities.examples.AutomaticNegativeExampleFinderSPARQL2.Strategy.RANDOM; import static org.dllearner.utilities.examples.AutomaticNegativeExampleFinderSPARQL2.Strategy.SIBLING; import static org.dllearner.utilities.examples.AutomaticNegativeExampleFinderSPARQL2.Strategy.SUPERCLASS; -import java.sql.SQLException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -33,14 +31,7 @@ import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; -import java.util.concurrent.TimeUnit; -import org.aksw.jena_sparql_api.cache.core.QueryExecutionFactoryCacheEx; -import org.aksw.jena_sparql_api.cache.extra.CacheBackend; -import org.aksw.jena_sparql_api.cache.extra.CacheFrontend; -import org.aksw.jena_sparql_api.cache.extra.CacheFrontendImpl; -import org.aksw.jena_sparql_api.cache.h2.CacheCoreH2; -import org.aksw.jena_sparql_api.cache.h2.CacheUtilsH2; import org.aksw.jena_sparql_api.core.QueryExecutionFactory; import org.aksw.jena_sparql_api.http.QueryExecutionFactoryHttp; import org.dllearner.kb.SparqlEndpointKS; @@ -83,15 +74,13 @@ public class AutomaticNegativeExampleFinderSPARQL2 { private OWLDataFactory df = new OWLDataFactoryImpl(); public enum Strategy{ - SUPERCLASS, SIBLING, RANDOM; + SUPERCLASS, SIBLING, RANDOM } - // for re-using existing queries private SPARQLReasoner sr; private String namespace; - private String cacheDirectory = "cache"; private QueryExecutionFactory qef; public AutomaticNegativeExampleFinderSPARQL2(SparqlEndpoint se, SPARQLReasoner reasoner) { @@ -103,11 +92,6 @@ public AutomaticNegativeExampleFinderSPARQL2(SparqlEndpoint se, SPARQLReasoner r this.namespace = namespace; qef = new QueryExecutionFactoryHttp(se.getURL().toString(), se.getDefaultGraphURIs()); - if(cacheDirectory != null){ - long timeToLive = TimeUnit.DAYS.toMillis(30); - CacheFrontend cacheFrontend = CacheUtilsH2.createCacheFrontend(cacheDirectory, true, timeToLive); - qef = new QueryExecutionFactoryCacheEx(qef, cacheFrontend); - } } public AutomaticNegativeExampleFinderSPARQL2(SparqlEndpoint se) { @@ -159,7 +143,7 @@ public SortedSet getNegativeExamples(Set positiveE } public SortedSet getNegativeExamples(Set positiveExamples, Collection strategies, int limit) { - Map strategiesWithWeight = new HashMap(); + Map strategiesWithWeight = new HashMap<>(); double weight = 1d/strategies.size(); for (Strategy strategy : strategies) { strategiesWithWeight.put(strategy, weight); @@ -183,7 +167,7 @@ public SortedSet getNegativeExamples(Set positiveE } private SortedSet computeNegativeExamples(OWLClass classToDescribe, Multiset positiveExamplesTypes, Map strategiesWithWeight, int maxNrOfReturnedInstances) { - SortedSet negativeExamples = new TreeSet(); + SortedSet negativeExamples = new TreeSet<>(); for (Entry entry : strategiesWithWeight.entrySet()) { Strategy strategy = entry.getKey(); @@ -195,7 +179,7 @@ private SortedSet computeNegativeExamples(OWLClass classToDescrib if(strategy == SIBLING){//get sibling class based examples logger.info("Applying sibling classes strategy..."); - SortedSet siblingNegativeExamples = new TreeSet(); + SortedSet siblingNegativeExamples = new TreeSet<>(); //for each type of the positive examples for (OWLClass nc : positiveExamplesTypes.elementSet()) { int frequency = positiveExamplesTypes.count(nc); @@ -218,7 +202,7 @@ private SortedSet computeNegativeExamples(OWLClass classToDescrib negativeExamples.addAll(siblingNegativeExamples); } else if(strategy == SUPERCLASS){//get super class based examples logger.info("Applying super class strategy..."); - SortedSet superClassNegativeExamples = new TreeSet(); + SortedSet superClassNegativeExamples = new TreeSet<>(); //for each type of the positive examples for (OWLClass nc : positiveExamplesTypes.elementSet()) { int frequency = positiveExamplesTypes.count(nc); @@ -245,7 +229,7 @@ private SortedSet computeNegativeExamples(OWLClass classToDescrib negativeExamples.addAll(superClassNegativeExamples); } else if(strategy == RANDOM){//get some random examples logger.info("Applying random strategy..."); - SortedSet randomNegativeExamples = new TreeSet(); + SortedSet randomNegativeExamples = new TreeSet<>(); String query = "SELECT DISTINCT ?s WHERE {?s a ?type. ?type a owl:Class ."; if(classToDescribe != null){ query += "FILTER NOT EXISTS{?s a <" + classToDescribe.toStringID() + "> }"; @@ -256,14 +240,14 @@ private SortedSet computeNegativeExamples(OWLClass classToDescrib throw new UnsupportedOperationException("Currently it's not possible to get random examples for unknown class to describe."); } - query += "} ORDER BY RAND() LIMIT " + maxNrOfReturnedInstances; + query += "} LIMIT " + maxNrOfReturnedInstances; - QueryExecution qe = qef.createQueryExecution(query); - ResultSet rs = qe.execSelect(); - QuerySolution qs; - while(rs.hasNext()){ - qs = rs.next(); - randomNegativeExamples.add(df.getOWLNamedIndividual(IRI.create(qs.getResource("s").getURI()))); + try(QueryExecution qe = qef.createQueryExecution(query)) { + ResultSet rs = qe.execSelect(); + while (rs.hasNext()) { + QuerySolution qs = rs.next(); + randomNegativeExamples.add(df.getOWLNamedIndividual(IRI.create(qs.getResource("s").getURI()))); + } } randomNegativeExamples.removeAll(negativeExamples); negativeExamples.addAll(new ArrayList<>(randomNegativeExamples).subList(0, Math.min(randomNegativeExamples.size(), maxNrOfReturnedInstances - negativeExamples.size()))); diff --git a/components-core/src/main/java/org/dllearner/utilities/examples/AutomaticPositiveExampleFinderOWL.java b/components-core/src/main/java/org/dllearner/utilities/examples/AutomaticPositiveExampleFinderOWL.java index 0c7499a4b2..1503dfcb44 100644 --- a/components-core/src/main/java/org/dllearner/utilities/examples/AutomaticPositiveExampleFinderOWL.java +++ b/components-core/src/main/java/org/dllearner/utilities/examples/AutomaticPositiveExampleFinderOWL.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.examples; import java.util.SortedSet; @@ -40,7 +39,7 @@ public class AutomaticPositiveExampleFinderOWL { public AutomaticPositiveExampleFinderOWL(AbstractReasonerComponent reasoningService) { - this.posExamples = new TreeSet(); + this.posExamples = new TreeSet<>(); this.reasoningService = reasoningService; } diff --git a/components-core/src/main/java/org/dllearner/utilities/examples/AutomaticPositiveExampleFinderSPARQL.java b/components-core/src/main/java/org/dllearner/utilities/examples/AutomaticPositiveExampleFinderSPARQL.java index fbd62f30d8..d5cd61272b 100644 --- a/components-core/src/main/java/org/dllearner/utilities/examples/AutomaticPositiveExampleFinderSPARQL.java +++ b/components-core/src/main/java/org/dllearner/utilities/examples/AutomaticPositiveExampleFinderSPARQL.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.examples; import java.util.SortedSet; @@ -39,7 +38,7 @@ public class AutomaticPositiveExampleFinderSPARQL { public AutomaticPositiveExampleFinderSPARQL(SPARQLTasks st) { super(); - this.posExamples = new TreeSet(); + this.posExamples = new TreeSet<>(); this.sparqltasks = st; } diff --git a/components-core/src/main/java/org/dllearner/utilities/examples/ExMakerHelper.java b/components-core/src/main/java/org/dllearner/utilities/examples/ExMakerHelper.java deleted file mode 100644 index 6dd6c6381a..0000000000 --- a/components-core/src/main/java/org/dllearner/utilities/examples/ExMakerHelper.java +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.utilities.examples; - -import java.util.Collection; -import java.util.Random; - -public class ExMakerHelper { - - - /** - * bad performance don't use for large sets - * use collections.shuffle and remove last - * @param from - * @return - */ - public static String pickOneRandomly(Collection from){ -// Monitor m = JamonMonitorLogger.getTimeMonitor(ExMakerHelper.class, "bad_performance").start(); - - if(from.isEmpty()){ - return null; - } - Random r = new Random(); - String[] array = from.toArray(new String[from.size()]); - - int index = Math.round((float)(array.length*r.nextFloat())); -// m.stop(); - try{ - return array[index]; - }catch (Exception e) { - return pickOneRandomly(from); - } - } -} diff --git a/components-core/src/main/java/org/dllearner/utilities/examples/ExampleContainer.java b/components-core/src/main/java/org/dllearner/utilities/examples/ExampleContainer.java index eea054bbbd..d1cef7243c 100644 --- a/components-core/src/main/java/org/dllearner/utilities/examples/ExampleContainer.java +++ b/components-core/src/main/java/org/dllearner/utilities/examples/ExampleContainer.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,12 +16,12 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.examples; import java.util.SortedSet; import java.util.TreeSet; +import org.jetbrains.annotations.NotNull; import org.semanticweb.owlapi.model.OWLIndividual; @@ -38,10 +38,10 @@ public class ExampleContainer implements Comparable{ - private static SortedSet exampleSets = new TreeSet(); + private static SortedSet exampleSets = new TreeSet<>(); - private SortedSet positiveExamples = new TreeSet(); - private SortedSet negativeExamples = new TreeSet(); + private SortedSet positiveExamples = new TreeSet<>(); + private SortedSet negativeExamples = new TreeSet<>(); public ExampleContainer(SortedSet positiveExamples, SortedSet negativeExamples) { @@ -54,14 +54,15 @@ public ExampleContainer(SortedSet positiveExamples, SortedSet. */ - package org.dllearner.utilities.examples; import java.io.File; @@ -66,13 +65,13 @@ public static void main(String[] args) { String b = "http://nlp2rdf.org/ontology/s"; String baseDir = "examples/nlp2rdf/tiger/"; - SortedSet pos = new TreeSet(Arrays.asList(new String[]{b+"197",b+"2013",b+"2704"})); - SortedSet neg = new TreeSet(Arrays.asList(new String[]{b+"1",b+"2",b+"3"})); - List urls = new ArrayList(); + SortedSet pos = new TreeSet<>(Arrays.asList(new String[]{b + "197", b + "2013", b + "2704"})); + SortedSet neg = new TreeSet<>(Arrays.asList(new String[]{b + "1", b + "2", b + "3"})); + List urls = new ArrayList<>(); urls.addAll (convert(baseDir, pos)); urls.addAll (convert(baseDir, neg)); - Set tmp = new HashSet(); + Set tmp = new HashSet<>(); try { URL add = new File(baseDir+"tiger.rdf").toURI().toURL(); // add = new File(baseDir+"new.rdf").toURI().toURL(); @@ -109,7 +108,7 @@ public static void main(String[] args) { } public static List convert(String baseDir, SortedSet exampleURIs){ - List u = new ArrayList(); + List u = new ArrayList<>(); for (String exampleURI : exampleURIs) { try { u.add(new File(toFileName(baseDir, exampleURI)).toURI().toURL()); diff --git a/components-core/src/main/java/org/dllearner/utilities/learn/LearnConfiguration.java b/components-core/src/main/java/org/dllearner/utilities/learn/LearnConfiguration.java deleted file mode 100644 index 2653041c3a..0000000000 --- a/components-core/src/main/java/org/dllearner/utilities/learn/LearnConfiguration.java +++ /dev/null @@ -1,82 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.utilities.learn; - -import java.util.SortedSet; -import java.util.TreeSet; - -import org.dllearner.core.AbstractCELA; -import org.dllearner.core.AbstractKnowledgeSource; -import org.dllearner.core.AbstractClassExpressionLearningProblem; -import org.dllearner.core.AbstractReasonerComponent; -import org.dllearner.core.ComponentManager; - -public class LearnConfiguration { - - public double noisePercentage = 0; - public int maxExecutionTimeInSeconds = 0; - public int minExecutionTimeInSeconds = 0; - public int guaranteeXgoodDescriptions = 1; - - public SortedSet ignoredConcepts = new TreeSet(); - - public boolean useAllConstructor = false; - public boolean useExistsConstructor = true; - public boolean useCardinalityRestrictions = false; - public boolean useNegation = false; - - public boolean writeSearchTree = false; - public String searchTreeFile = "log/searchTree.txt"; - public boolean replaceSearchTree = true; - - - public void applyConfigEntries(ComponentManager cm, AbstractKnowledgeSource ks, AbstractClassExpressionLearningProblem lp, AbstractReasonerComponent rs, AbstractCELA la) { - try { - - // LEARNINGALGORITHM - cm.applyConfigEntry(la, "useAllConstructor", useAllConstructor); - cm.applyConfigEntry(la, "useExistsConstructor", useExistsConstructor); - cm.applyConfigEntry(la, "useCardinalityRestrictions", useCardinalityRestrictions); - cm.applyConfigEntry(la, "useNegation", useNegation); - - cm.applyConfigEntry(la, "minExecutionTimeInSeconds", minExecutionTimeInSeconds); - cm.applyConfigEntry(la, "maxExecutionTimeInSeconds", - maxExecutionTimeInSeconds); - cm.applyConfigEntry(la, "guaranteeXgoodDescriptions", - guaranteeXgoodDescriptions); - - cm.applyConfigEntry(la, "writeSearchTree", writeSearchTree); - cm.applyConfigEntry(la, "searchTreeFile", searchTreeFile); - cm.applyConfigEntry(la, "replaceSearchTree", replaceSearchTree); - - cm.applyConfigEntry(la, "noisePercentage", noisePercentage); - - if(ignoredConcepts.size()>0) { - cm.applyConfigEntry(la,"ignoredConcepts",ignoredConcepts); - } - - - } catch (Exception e) { - e.printStackTrace(); - } - // return null; - - } -} diff --git a/components-core/src/main/java/org/dllearner/utilities/learn/LearnOWLFileConfiguration.java b/components-core/src/main/java/org/dllearner/utilities/learn/LearnOWLFileConfiguration.java deleted file mode 100644 index 11fa24f753..0000000000 --- a/components-core/src/main/java/org/dllearner/utilities/learn/LearnOWLFileConfiguration.java +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.utilities.learn; - -import java.io.File; - -import org.dllearner.core.AbstractCELA; -import org.dllearner.core.AbstractKnowledgeSource; -import org.dllearner.core.AbstractClassExpressionLearningProblem; -import org.dllearner.core.AbstractReasonerComponent; -import org.dllearner.core.ComponentManager; -public class LearnOWLFileConfiguration extends LearnConfiguration { - - - - // SparqlKnowledgeSource - public String OWLFileURL = ""; - - - public void setOWLFileURL(String ontologyPath){ - OWLFileURL = new File(ontologyPath).toURI().toString(); - - } - - @Override - public void applyConfigEntries(ComponentManager cm, AbstractKnowledgeSource ks, AbstractClassExpressionLearningProblem lp, AbstractReasonerComponent rs, AbstractCELA la) { - try { - super.applyConfigEntries(cm, ks, lp, rs, la); - // KNOWLEDGESOURCE - cm.applyConfigEntry(ks, "url", OWLFileURL); - - - } catch (Exception e) { - e.printStackTrace(); - } - // return null; - - } -} diff --git a/components-core/src/main/java/org/dllearner/utilities/learn/LearnSPARQLConfiguration.java b/components-core/src/main/java/org/dllearner/utilities/learn/LearnSPARQLConfiguration.java deleted file mode 100644 index a8459a2073..0000000000 --- a/components-core/src/main/java/org/dllearner/utilities/learn/LearnSPARQLConfiguration.java +++ /dev/null @@ -1,81 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.utilities.learn; - -import org.dllearner.core.AbstractCELA; -import org.dllearner.core.AbstractKnowledgeSource; -import org.dllearner.core.AbstractClassExpressionLearningProblem; -import org.dllearner.core.AbstractReasonerComponent; -import org.dllearner.core.ComponentManager; -import org.dllearner.kb.sparql.Cache; -import org.dllearner.kb.sparql.SparqlEndpoint; - -public class LearnSPARQLConfiguration extends LearnConfiguration { - - - - // SparqlKnowledgeSource - public SparqlEndpoint sparqlEndpoint = SparqlEndpoint.getEndpointDBpedia(); - public String predefinedEndpoint = null; - public int recursiondepth = 1; - public boolean closeAfterRecursion = true; - public boolean getAllSuperClasses = true; - public boolean useLits = false; - public boolean randomizeCache = false; - public String predefinedFilter = null; - - - @Override - public void applyConfigEntries(ComponentManager cm, AbstractKnowledgeSource ks, AbstractClassExpressionLearningProblem lp, AbstractReasonerComponent rs, AbstractCELA la) { - try { - super.applyConfigEntries(cm, ks, lp, rs, la); - // KNOWLEDGESOURCE - if(predefinedEndpoint ==null){ - cm.applyConfigEntry(ks, "url", sparqlEndpoint.getURL().toString()); - }else { - cm.applyConfigEntry(ks, "predefinedEndpoint", predefinedEndpoint); - } - if(predefinedFilter==null){ - //todo manual - }else{ - cm.applyConfigEntry(ks, "predefinedFilter", predefinedFilter); - } - - cm.applyConfigEntry(ks, "useLits", useLits); - cm.applyConfigEntry(ks, "recursionDepth", recursiondepth); - cm.applyConfigEntry(ks, "closeAfterRecursion", closeAfterRecursion); - cm.applyConfigEntry(ks, "getAllSuperClasses", getAllSuperClasses); - - - if (randomizeCache) - cm.applyConfigEntry(ks, "cacheDir", "cache/" - + System.currentTimeMillis() + ""); - else { - cm.applyConfigEntry(ks, "cacheDir", Cache.getDefaultCacheDir()); - } - - - } catch (Exception e) { - e.printStackTrace(); - } - // return null; - - } -} diff --git a/components-core/src/main/java/org/dllearner/utilities/learn/UsedEntitiesDetection.java b/components-core/src/main/java/org/dllearner/utilities/learn/UsedEntitiesDetection.java index e16a4f4202..276994453b 100644 --- a/components-core/src/main/java/org/dllearner/utilities/learn/UsedEntitiesDetection.java +++ b/components-core/src/main/java/org/dllearner/utilities/learn/UsedEntitiesDetection.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.learn; import java.util.Comparator; @@ -83,24 +82,24 @@ public int compare(Set key1, Set key2) { * * @param reasoner A reasoner. * @param individuals A set of individuals to start from. - * @param depth The maximum depth for the search. + * @param maxDepth The maximum depth for the search. */ public UsedEntitiesDetection(AbstractReasonerComponent reasoner, Set individuals, int maxDepth) { this.reasoner = reasoner; this.maxDepth = maxDepth; - usedClasses = new TreeMap,Set>(keyComp); - usedObjectProperties = new TreeMap,Set>(keyComp); + usedClasses = new TreeMap<>(keyComp); + usedObjectProperties = new TreeMap<>(keyComp); - Set startKey = new TreeSet(); + Set startKey = new TreeSet<>(); computeUsedEntitiesRec(startKey, individuals); } private void computeUsedEntitiesRec(Set key, Set individuals) { - Set types = new TreeSet(); + Set types = new TreeSet<>(); // Set properties = new TreeSet(); // we must use the object property comparator to avoid double occurences of properties - Map> relations = new TreeMap>(); + Map> relations = new TreeMap<>(); for(OWLIndividual individual : individuals) { // add all types @@ -112,7 +111,7 @@ private void computeUsedEntitiesRec(Set key, Set inds = new TreeSet(entry.getValue()); + Set inds = new TreeSet<>(entry.getValue()); // if property exists, add the found individuals if(relations.containsKey(prop)) { @@ -132,7 +131,7 @@ private void computeUsedEntitiesRec(Set key, Set> entry : relations.entrySet()) { // construct new key (copy and add) - Set newKey = new TreeSet(key); + Set newKey = new TreeSet<>(key); newKey.add(entry.getKey()); // recursion diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/ConceptTransformation.java b/components-core/src/main/java/org/dllearner/utilities/owl/ConceptTransformation.java index f43018d909..435be2cf97 100644 --- a/components-core/src/main/java/org/dllearner/utilities/owl/ConceptTransformation.java +++ b/components-core/src/main/java/org/dllearner/utilities/owl/ConceptTransformation.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,18 +16,17 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.owl; import java.util.ArrayList; import java.util.HashSet; -import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; import org.dllearner.core.AbstractReasonerComponent; +import org.dllearner.core.owl.NNF; import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLDataFactory; import org.semanticweb.owlapi.model.OWLNaryBooleanClassExpression; @@ -53,45 +52,48 @@ public class ConceptTransformation { public static long cleaningTimeNs = 0; - private static long cleaningTimeNsStart = 0; public static long onnfTimeNs = 0; private static long onnfTimeNsStart = 0; public static long shorteningTimeNs = 0; - private static long shorteningTimeNsStart = 0; - + private static final OWLDataFactory df = new OWLDataFactoryImpl(); private static final OWLObjectDuplicator DUPLICATOR = new OWLObjectDuplicator(df); private static final OWLClassExpressionCleaner CLASS_EXPRESSION_CLEANER = new OWLClassExpressionCleaner(df); public static OWLClassExpression cleanConceptNonRecursive(OWLClassExpression concept) { - // cleaningTimeNsStart = System.nanoTime(); - return concept.accept(CLASS_EXPRESSION_CLEANER); - // cleaningTimeNs += System.nanoTime() - cleaningTimeNsStart; + return cleanConcept(concept); } public static OWLClassExpression cleanConcept(OWLClassExpression concept) { - // cleaningTimeNsStart = System.nanoTime(); - return concept.accept(CLASS_EXPRESSION_CLEANER); - // cleaningTimeNs += System.nanoTime() - cleaningTimeNsStart; + long cleaningTimeNsStart = System.nanoTime(); + OWLClassExpression cleanedConcept = concept.accept(CLASS_EXPRESSION_CLEANER); + cleaningTimeNs += System.nanoTime() - cleaningTimeNsStart; + return cleanedConcept; } - // wandelt ein Konzept in Negationsnormalform um - public static OWLClassExpression transformToNegationNormalForm(OWLClassExpression concept) { - return concept.getNNF(); + /** + * Returns the class expression in negation normal form. + * @param ce the class expression + * @return the class expression in negation normal form + */ + public static OWLClassExpression nnf(OWLClassExpression ce) { + NNF nnfGen = new NNF(new OWLDataFactoryImpl()); + OWLClassExpression nnf = ce.accept(nnfGen); + return nnf; } /** * Expand the class expression by adding \exists r.\top for all properties r * that are involved in some \forall r.C on the same modal depth. - * @param ce + * @param ce the class expression to expand * @return */ public static OWLClassExpression appendSomeValuesFrom(OWLClassExpression ce) { // if forall semantics is someonly if (ce instanceof OWLObjectIntersectionOf) { - Set newOperands = new HashSet(); - Set universallyQuantifiedProperties = new HashSet(); - Set existentiallyQuantifiedProperties = new HashSet(); + Set newOperands = new HashSet<>(); + Set universallyQuantifiedProperties = new HashSet<>(); + Set existentiallyQuantifiedProperties = new HashSet<>(); for (OWLClassExpression operand : ((OWLObjectIntersectionOf) ce).getOperands()) { newOperands.add(appendSomeValuesFrom(operand)); if(operand instanceof OWLObjectAllValuesFrom) { @@ -127,211 +129,76 @@ public static OWLClassExpression appendSomeValuesFrom(OWLClassExpression ce) { public static OWLClassExpression applyEquivalenceRules(OWLClassExpression concept) { OWLClassExpression conceptClone = DUPLICATOR.duplicateObject(concept); -// conceptClone.getChildren().clear(); -// -// for(OWLClassExpression c : OWLClassExpressionUtils.getChildren(concept.getChildren()) { -// conceptClone.addChild(applyEquivalenceRules(c)); -// } - - // return conceptClone; - // TOP, BOTTOM in Disjunktion entfernen + // remove \top and \bot from disjunction if(conceptClone instanceof OWLObjectUnionOf) { - List operands = ((OWLObjectUnionOf) conceptClone).getOperandsAsList(); - Iterator it = operands.iterator(); - while(it.hasNext()) { - OWLClassExpression c = it.next(); - // for(Concept c : concept.getChildren()) { - // TOP in Disjunktion => ganze Disjunktion äquivalent zu Top - if(c.isOWLThing()) + SortedSet newOperands = new TreeSet<>(); + for (OWLClassExpression op : ((OWLObjectUnionOf) conceptClone).getOperandsAsList()) { + OWLClassExpression c = applyEquivalenceRules(op); + + if(c.isOWLThing()) {// \top in C => C \equiv \top return df.getOWLThing(); - // BOTTOM in Disjunktion => entfernen - else if(c.isOWLNothing()) - it.remove(); + } + else if(c.isOWLNothing()) {// \bot in C => remove + } else { + newOperands.add(c); + } } - // falls nur noch ein Kind übrig bleibt, dann entfällt - // MultiDisjunction - if(operands.size()==1) - return operands.get(0); - - // falls keine Kinder übrig bleiben, dann war das letzte Kind - // BOTTOM - if(operands.isEmpty()) + // if there are no children the last child was \bot + if (newOperands.isEmpty()) { return df.getOWLNothing(); - - } else if(conceptClone instanceof OWLObjectIntersectionOf) { - List operands = ((OWLObjectUnionOf) conceptClone).getOperandsAsList(); - Iterator it = operands.iterator(); - while(it.hasNext()) { - OWLClassExpression c = it.next(); - // TOP in Konjunktion => entfernen - if(c.isOWLThing()) - it.remove(); - // BOTTOM in Konjunktion => alles äquivalent zu BOTTOM - else if(c.isOWLNothing()) - return df.getOWLNothing(); + }if (newOperands.size() == 1) { + return newOperands.first(); + } else { + return df.getOWLObjectUnionOf(newOperands); + } + } else if(conceptClone instanceof OWLObjectIntersectionOf) {// remove \top and \bot from intersection + SortedSet newOperands = new TreeSet<>(); + for (OWLClassExpression op : ((OWLObjectIntersectionOf) conceptClone).getOperandsAsList()) { + OWLClassExpression c = applyEquivalenceRules(op); + + if(c.isOWLThing()) {// \top in C => remove + + } else if(c.isOWLNothing()) {// \bot in C => C \equiv \bot + return df.getOWLNothing(); + } else { + newOperands.add(c); + } } - if(operands.size()==1) - return operands.get(0); - - // falls keine Kinder übrig bleiben, dann war das letzte Kind - // TOP - if(operands.isEmpty()) + // if there are no children the last child was \top + if (newOperands.isEmpty()) { return df.getOWLThing(); - - return df.getOWLObjectIntersectionOf(new TreeSet(operands)); + } else if (newOperands.size() == 1) { + return newOperands.first(); + } else { + return df.getOWLObjectIntersectionOf(newOperands); + } } return conceptClone; } - // TODO: aus Effizienzgründen könnte man noch eine nicht-rekursive Methode entwickeln, die - // nur die obere Ebene umwandelt - public static void transformToOrderedNegationNormalFormNonRecursive(OWLClassExpression concept) { - // onnfTimeNsStart = System.nanoTime(); - - // Liste der Kinder sortieren -// Collections.sort(concept.getChildren(), conceptComparator); - - // onnfTimeNs += System.nanoTime() - onnfTimeNsStart; - } - - // wandelt ein Konzept in geordnete Negationsnormalform um; - // es wird angenommen, dass das Eingabekonzept in Negationsnormalform und - // "sauber" ist - public static void transformToOrderedForm(OWLClassExpression concept) { - - /** - // alle Kinderkonzepte in geordnete Negationsnormalform bringen - for(OWLClassExpression child : concept.getChildren()) { - transformToOrderedForm(child, conceptComparator); - } - - onnfTimeNsStart = System.nanoTime(); - // Liste der Kinder sortieren - Collections.sort(concept.getChildren(), conceptComparator); - - // Konvertierung von Liste in Array => Array sortieren => Rekonvertierung in Liste - // List childList = concept.getChildren(); - // Concept[] childArray = (Concept[]) childList.toArray(); - // Arrays.sort(childArray, conceptComparator); - // childList = Arrays.asList(childArray); - onnfTimeNs += System.nanoTime() - onnfTimeNsStart; - **/ - } - /* - public static OWLClassExpression transformToMultiClean(OWLClassExpression concept) { - concept = transformToMulti(concept); - cleanConcept(concept); - return concept; - } - - // ersetzt einfache Disjunktionen/Konjunktionen durch Multi - public static OWLClassExpression transformToMulti(OWLClassExpression concept) { - // alle Kinderkonzepte in geordnete Negationsnormalform bringen - List multiChildren = new LinkedList(); - - // es müssen veränderte Kinder entfernt und neu hinzugefügt werden - // (einfache Zuweisung mit = funktioniert nicht, da die Pointer die gleichen - // bleiben) - Iterator it = concept.getChildren().iterator(); - while(it.hasNext()) { - Description child = it.next(); - multiChildren.add(transformToMulti(child)); - it.remove(); - } - - for(OWLClassExpression multiChild : multiChildren) - concept.addChild(multiChild); - - if(concept instanceof Disjunction) - return new MultiDisjunction(concept.getChildren()); - - if(concept instanceof Conjunction) - return new MultiConjunction(concept.getChildren()); - - return concept; - } - */ - // liefert ein ev. verkürztes Konzept, wenn in Disjunktionen bzw. - // Konjunktionen Elemente mehrfach vorkommen - // (erstmal nicht-rekursiv implementiert) - public static OWLClassExpression getShortConceptNonRecursive(OWLClassExpression concept) { - return concept; - } - /** * Tries to shorten a concept, e.g. male AND male is shortened to male. * @param concept The input concepts. - * @param conceptComparator A comparator for concepts. * @return A shortened version of the concept (equal to the input concept if it cannot be shortened). */ public static OWLClassExpression getShortConcept(OWLClassExpression concept) { - shorteningTimeNsStart = System.nanoTime(); - // deep copy des Konzepts, da es nicht verändert werden darf - // (Nachteil ist, dass auch Konzepte kopiert werden, bei denen sich gar - // nichts ändert) + long shorteningTimeNsStart = System.nanoTime(); OWLClassExpression clone = DUPLICATOR.duplicateObject(concept); - clone = getShortConcept(clone, 0); - // return getShortConcept(concept, conceptComparator, 0); shorteningTimeNs += System.nanoTime() - shorteningTimeNsStart; return clone; } - // das Eingabekonzept darf nicht modifiziert werden - private static OWLClassExpression getShortConcept(OWLClassExpression concept, int recDepth) { - //probably no longer necessary as OWL API uses sets for class expressions - return concept; - /** - //if(recDepth==0) - // System.out.println(concept); - - // Kinder schrittweise ersetzen - // TODO: effizienter wäre nur zu ersetzen, wenn sich etwas geändert hat - List tmp = new LinkedList(); - Iterator it = concept.getChildren().iterator(); - while(it.hasNext()) { - OWLClassExpression c = it.next(); - // concept.addChild(getShortConcept(c, conceptComparator)); - OWLClassExpression newChild = getShortConcept(c, recDepth+1); - // Vergleich, ob es sich genau um die gleichen Objekte handelt - // (es wird explizit == statt equals verwendet) - if(c != newChild) { - tmp.add(newChild); - it.remove(); - } - } - for(OWLClassExpression child : tmp) - concept.addChild(child); - - if(concept instanceof OWLNaryBooleanClassExpression) { - // Verkürzung geschieht einfach durch einfügen in eine geordnete Menge - SortedSet newChildren = new TreeSet(); - newChildren.addAll(((OWLNaryBooleanClassExpression) concept).getOperands()); - // falls sich Kinderliste auf ein Element reduziert hat, dann gebe nur - // dieses Element zurück (umschließende Konjunktion/Disjunktion entfällt) - if(newChildren.size()==1) - return newChildren.first(); - // ev. geht das noch effizienter, wenn man keine neue Liste erstellen - // muss(?) => Listen erstellen dürfte allerdings sehr schnell gehen - if(concept instanceof OWLObjectIntersectionOf) - return df.getOWLObjectIntersectionOf(newChildren); - else - return df.getOWLObjectUnionOf(newChildren); - } else - return concept; - **/ - } - /** * Method to determine, whether a class expression is minimal, * e.g. \forall r.\top (\equiv \top) or male \sqcup male are not * minimal. This method performs heuristic sanity checks (it will * not try to find semantically equivalent shorter descriptions). - * @param OWLClassExpression Input description. + * @param description Input description. * @return True if a superfluous construct has been found. */ public static boolean isDescriptionMinimal(OWLClassExpression description) { @@ -364,7 +231,7 @@ private static boolean findEquivalences(OWLClassExpression description) { public static OWLClassExpression replaceRange(OWLClassExpression description, AbstractReasonerComponent rs) { OWLClassExpression rewrittenClassExpression = description; if(description instanceof OWLNaryBooleanClassExpression){ - Set newOperands = new TreeSet(); + Set newOperands = new TreeSet<>(); for (OWLClassExpression operand : ((OWLNaryBooleanClassExpression) description).getOperands()) { newOperands.add(replaceRange(operand, rs)); } @@ -418,8 +285,8 @@ public static boolean isSubdescription(OWLClassExpression description, OWLClassE // return ((subDescription instanceof NamedClass) && (((NamedClass)description).toStringID().equals(((NamedClass)subDescription).toStringID()))); // } - List children = new ArrayList(OWLClassExpressionUtils.getChildren(description)); - List subChildren = new ArrayList(OWLClassExpressionUtils.getChildren(subDescription)); + List children = new ArrayList<>(OWLClassExpressionUtils.getChildren(description)); + List subChildren = new ArrayList<>(OWLClassExpressionUtils.getChildren(subDescription)); // no children: both have to be equal if(children.size()==0) { @@ -484,7 +351,7 @@ public static boolean isSubdescription(OWLClassExpression description, OWLClassE /** * Counts occurrences of \forall in description. - * @param OWLClassExpression A description. + * @param description A description. * @return Number of \forall occurrences. */ public static int getForallOccurences(OWLClassExpression description) { @@ -501,7 +368,7 @@ public static int getForallOccurences(OWLClassExpression description) { * Gets the "contexts" of all \forall occurrences in a description. A context * is a set of properties, i.e. in \exists hasChild.\exists hasBrother.\forall hasChild.male, * the context of the only \forall occurrence is [hasChild, hasBrother, hasChild]. - * @param OWLClassExpression A description. + * @param description A description. * @return Set of property contexts. */ public static SortedSet getForallContexts(OWLClassExpression description) { @@ -522,7 +389,7 @@ private static SortedSet getForallContexts(OWLClassExpression d OWLClassExpression filler = ((OWLObjectAllValuesFrom) description).getFiller(); currentContextCopy.add(op); // System.out.println("cc: " + currentContext); - TreeSet contexts = new TreeSet(); + TreeSet contexts = new TreeSet<>(); contexts.add(currentContextCopy); contexts.addAll(getForallContexts(filler, currentContextCopy)); return contexts; @@ -533,14 +400,14 @@ private static SortedSet getForallContexts(OWLClassExpression d return getForallContexts(filler, currentContextCopy); // restrictions without a child (has value) } else { - return new TreeSet(); + return new TreeSet<>(); } } else { - return new TreeSet(); + return new TreeSet<>(); } // for non-restrictions, we collect contexts over all children } else { - TreeSet contexts = new TreeSet(); + TreeSet contexts = new TreeSet<>(); if(description instanceof OWLNaryBooleanClassExpression){ for(OWLClassExpression child : ((OWLNaryBooleanClassExpression) description).getOperands()) { // System.out.println("testing child " + child + " " + currentContext); diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/DLSyntaxObjectRenderer.java b/components-core/src/main/java/org/dllearner/utilities/owl/DLSyntaxObjectRenderer.java index fdd570691d..4736a7d62f 100644 --- a/components-core/src/main/java/org/dllearner/utilities/owl/DLSyntaxObjectRenderer.java +++ b/components-core/src/main/java/org/dllearner/utilities/owl/DLSyntaxObjectRenderer.java @@ -1,80 +1,30 @@ -package org.dllearner.utilities.owl; - -/* - * This file is part of the OWL API. - * - * The contents of this file are subject to the LGPL License, Version 3.0. +/** + * Copyright (C) 2007 - 2016, Jens Lehmann * - * Copyright (C) 2011, The University of Manchester + * This file is part of DL-Learner. * - * This program is free software: you can redistribute it and/or modify + * DL-Learner is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or + * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * - * This program is distributed in the hope that it will be useful, + * DL-Learner is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - * - * - * Alternatively, the contents of this file may be used under the terms of the Apache License, Version 2.0 - * in which case, the provisions of the Apache License Version 2.0 are applicable instead of those above. - * - * Copyright 2011, The University of Manchester - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * along with this program. If not, see . */ - -import static uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntax.AND; -import static uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntax.BOTTOM; -import static uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntax.COMMA; -import static uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntax.COMP; -import static uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntax.DISJOINT_WITH; -import static uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntax.EQUAL; -import static uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntax.EQUIVALENT_TO; -import static uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntax.EXISTS; -import static uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntax.FORALL; -import static uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntax.IMPLIES; -import static uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntax.IN; -import static uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntax.INVERSE; -import static uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntax.MAX; -import static uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntax.MIN; -import static uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntax.NOT; -import static uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntax.NOT_EQUAL; -import static uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntax.OR; -import static uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntax.SELF; -import static uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntax.SUBCLASS; -import static uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntax.TOP; -import static uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntax.WEDGE; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.TreeSet; +package org.dllearner.utilities.owl; import org.semanticweb.owlapi.io.OWLObjectRenderer; import org.semanticweb.owlapi.model.*; -import org.semanticweb.owlapi.util.IRIShortFormProvider; -import org.semanticweb.owlapi.util.ShortFormProvider; -import org.semanticweb.owlapi.util.SimpleIRIShortFormProvider; -import org.semanticweb.owlapi.util.SimpleShortFormProvider; -import uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntax; +import javax.annotation.Nonnull; +import java.util.Iterator; + +import static org.semanticweb.owlapi.dlsyntax.renderer.DLSyntax.*; /** * Extended version of the DLSyntaxObjectRenderer class in OWL API. Extension is @@ -82,832 +32,62 @@ * * Renders objects in unicode DL syntax. * - * @author Matthew Horridge, The University Of Manchester, Bio-Health - * Informatics Group, Date: 10-Feb-2008 + * @author Lorenz Buehmann */ -public class DLSyntaxObjectRenderer implements OWLObjectRenderer, OWLObjectVisitor { - - private ShortFormProvider shortFormProvider; - private IRIShortFormProvider iriShortFormProvider; - private StringBuilder buffer; - private OWLObject focusedObject; - - /** default constructor */ - public DLSyntaxObjectRenderer() { - shortFormProvider = new SimpleShortFormProvider(); - iriShortFormProvider = new SimpleIRIShortFormProvider(); - buffer = new StringBuilder(); - } - - /** - * @param focusedObject - * focusedObject - */ - public void setFocusedObject(OWLObject focusedObject) { - this.focusedObject = focusedObject; - } - - /** - * @param obj - * obj - * @return true if focused - */ - public boolean isFocusedObject(OWLObject obj) { - if (focusedObject == null) { - return false; - } - return focusedObject.equals(obj); - } +public class DLSyntaxObjectRenderer extends org.semanticweb.owlapi.dlsyntax.renderer.DLSyntaxObjectRenderer +implements OWLObjectRenderer, OWLObjectVisitor { + /////////////////////////////////////////////////////////////////////////////////////////////// + // + // Data stuff + // + /////////////////////////////////////////////////////////////////////////////////////////////// @Override - public void setShortFormProvider(ShortFormProvider shortFormProvider) { - this.shortFormProvider = shortFormProvider; - } - - @Override - public String render(OWLObject object) { - buffer = new StringBuilder(); - object.accept(this); - return buffer.toString(); - } - - @Override - public void visit(OWLOntology ontology) { - for (OWLAxiom ax : new TreeSet(ontology.getLogicalAxioms())) { - ax.accept(this); - write("\n"); - } - } - - protected void write(String s) { - buffer.append(s); - } - - protected String renderEntity(OWLEntity entity) { - return shortFormProvider.getShortForm(entity); - } - - protected void writeEntity(OWLEntity entity) { - write(renderEntity(entity)); - } - - protected void write(DLSyntax keyword) { - write(keyword.toString()); - } - - protected void write(int i) { - write(Integer.toString(i)); - } - - protected void writeNested(OWLObject object) { - if (isBracketedIfNested(object)) { - write("("); - } - object.accept(this); - if (isBracketedIfNested(object)) { - write(")"); - } - } - - protected boolean isBracketedIfNested(OWLObject object) { - // if(object instanceof OWLObjectComplementOf) { - // if(!((OWLObjectComplementOf) object).getOperand().isAnonymous()) { - // return false; - // } - // } - // return object instanceof OWLClassExpression && !((OWLClassExpression) - // object).isClassExpressionLiteral(); - return !(object instanceof OWLEntity); - } - - private void writeObject(OWLObject object, boolean nest) { - if (nest) { - writeNested(object); - } else { - object.accept(this); - } - } - - protected void write(Collection objects, DLSyntax delim, boolean nest) { - if (objects.size() == 2) { - Iterator it = objects.iterator(); - OWLObject o1 = it.next(); - OWLObject o2 = it.next(); - if (isFocusedObject(o1) || !isFocusedObject(o2)) { - writeObject(o1, nest); - writeSpace(); - write(delim); - writeSpace(); - writeObject(o2, nest); - } else { - writeObject(o2, nest); - writeSpace(); - write(delim); - writeSpace(); - writeObject(o1, nest); - } - } else { - for (Iterator it = objects.iterator(); it.hasNext();) { - OWLObject o = it.next(); - writeObject(o, nest); - if (it.hasNext()) { - writeSpace(); - write(delim); - writeSpace(); - } - } - } - } - - // protected void write(Collection objects, DLSyntax - // keyword, boolean nest) { - // write(objects, keyword, nest); - // } - @Override - public void visit(OWLSubClassOfAxiom axiom) { - axiom.getSubClass().accept(this); - writeSpace(); - write(SUBCLASS); - writeSpace(); - axiom.getSuperClass().accept(this); - } - - private void writePropertyAssertion(OWLPropertyAssertionAxiom ax) { - if (ax instanceof OWLNegativeObjectPropertyAssertionAxiom - || ax instanceof OWLNegativeDataPropertyAssertionAxiom) { - write(NOT); - } - ax.getProperty().accept(this); + public void visit(@Nonnull OWLDataIntersectionOf node) { write("("); - ax.getSubject().accept(this); - write(", "); - ax.getObject().accept(this); + write(node.getOperands(), AND, false); write(")"); } @Override - public void visit(OWLNegativeObjectPropertyAssertionAxiom axiom) { - write(NOT); - writePropertyAssertion(axiom); - } - - @Override - public void visit(OWLAsymmetricObjectPropertyAxiom axiom) { - axiom.getProperty().accept(this); - writeSpace(); - write(DISJOINT_WITH); - writeSpace(); - axiom.getProperty().accept(this); - write(INVERSE); - } - - @Override - public void visit(OWLReflexiveObjectPropertyAxiom axiom) { - write(TOP); - writeSpace(); - write(SUBCLASS); - writeSpace(); - write(EXISTS); - writeSpace(); - axiom.getProperty().accept(this); - write(" ."); - write(SELF); - } - - @Override - public void visit(OWLDisjointClassesAxiom axiom) { - List descs = new ArrayList(axiom.getClassExpressions()); - for (int i = 0; i < descs.size(); i++) { - for (int j = i + 1; j < descs.size(); j++) { - descs.get(i).accept(this); - writeSpace(); - write(DISJOINT_WITH); - writeSpace(); - descs.get(j).accept(this); - if (j < descs.size() - 1) { - write(", "); - } - } - } - // write(axiom.getClassExpressions(), DISJOINT_WITH, true); - } - - private void writeDomainAxiom(OWLPropertyDomainAxiom axiom) { - write(EXISTS); - writeSpace(); - axiom.getProperty().accept(this); - writeRestrictionSeparator(); - write(TOP); - writeSpace(); - write(SUBCLASS); - writeSpace(); - writeNested(axiom.getDomain()); - } - - private void writeRestrictionSeparator() { - write("."); - } - - @Override - public void visit(OWLDataPropertyDomainAxiom axiom) { - writeDomainAxiom(axiom); - } - - @Override - public void visit(OWLObjectPropertyDomainAxiom axiom) { - writeDomainAxiom(axiom); - } - - @Override - public void visit(OWLEquivalentObjectPropertiesAxiom axiom) { - write(axiom.getProperties(), EQUIVALENT_TO, false); - } - - @Override - public void visit(OWLNegativeDataPropertyAssertionAxiom axiom) { - write(NOT); - writePropertyAssertion(axiom); - } - - @Override - public void visit(OWLDifferentIndividualsAxiom axiom) { - write(axiom.getIndividuals(), NOT_EQUAL, false); - } - - @Override - public void visit(OWLDisjointDataPropertiesAxiom axiom) { - write(axiom.getProperties(), DISJOINT_WITH, false); - } - - @Override - public void visit(OWLDisjointObjectPropertiesAxiom axiom) { - write(axiom.getProperties(), DISJOINT_WITH, false); - } - - private void writeRangeAxiom(OWLPropertyRangeAxiom axiom) { - write(TOP); - writeSpace(); - write(SUBCLASS); - writeSpace(); - write(FORALL); - writeSpace(); - axiom.getProperty().accept(this); - writeRestrictionSeparator(); - writeNested(axiom.getRange()); - } - - @Override - public void visit(OWLObjectPropertyRangeAxiom axiom) { - writeRangeAxiom(axiom); - } - - @Override - public void visit(OWLObjectPropertyAssertionAxiom axiom) { - writePropertyAssertion(axiom); - } - - private void writeFunctionalProperty(OWLPropertyExpression property) { - write(TOP); - writeSpace(); - write(SUBCLASS); - writeSpace(); - write(MAX); - writeSpace(); - write(1); - writeSpace(); - property.accept(this); - } - - @Override - public void visit(OWLFunctionalObjectPropertyAxiom axiom) { - writeFunctionalProperty(axiom.getProperty()); - } - - @Override - public void visit(OWLSubObjectPropertyOfAxiom axiom) { - axiom.getSubProperty().accept(this); - writeSpace(); - write(SUBCLASS); - writeSpace(); - axiom.getSuperProperty().accept(this); - } - - @Override - public void visit(OWLDisjointUnionAxiom axiom) { - axiom.getOWLClass().accept(this); - write(EQUAL); - write(axiom.getClassExpressions(), OR, false); - } - - @Override - public void visit(OWLDeclarationAxiom axiom) { - } - - @Override - public void visit(OWLAnnotationAssertionAxiom axiom) { - } - - @Override - public void visit(OWLSymmetricObjectPropertyAxiom axiom) { - axiom.getProperty().accept(this); - writeSpace(); - write(EQUIVALENT_TO); - writeSpace(); - axiom.getProperty().accept(this); - write(INVERSE); - } - - private void writeSpace() { - write(" "); - } - - @Override - public void visit(OWLDataPropertyRangeAxiom axiom) { - writeRangeAxiom(axiom); - } - - @Override - public void visit(OWLFunctionalDataPropertyAxiom axiom) { - writeFunctionalProperty(axiom.getProperty()); - } - - @Override - public void visit(OWLEquivalentDataPropertiesAxiom axiom) { - write(axiom.getProperties(), EQUIVALENT_TO, false); - } - - @Override - public void visit(OWLClassAssertionAxiom axiom) { - if (axiom.getClassExpression().isAnonymous()) { - write("("); - } - axiom.getClassExpression().accept(this); - if (axiom.getClassExpression().isAnonymous()) { - write(")"); - } + public void visit(@Nonnull OWLDataUnionOf node) { write("("); - axiom.getIndividual().accept(this); + write(node.getOperands(), OR, false); write(")"); } @Override - public void visit(OWLEquivalentClassesAxiom axiom) { - write(axiom.getClassExpressions(), EQUIVALENT_TO, false); - } + public void visit(@Nonnull OWLDatatypeRestriction node) { + node.getDatatype().accept(this); + write("["); + Iterator iterator = node.getFacetRestrictions().iterator(); - @Override - public void visit(OWLDataPropertyAssertionAxiom axiom) { - writePropertyAssertion(axiom); - } - - @Override - public void visit(OWLTransitiveObjectPropertyAxiom axiom) { - axiom.getProperty().accept(this); - writeSpace(); - write(IN); - writeSpace(); - write("R"); - write("\u207A"); - } - - @Override - public void visit(OWLIrreflexiveObjectPropertyAxiom axiom) { - write(TOP); - writeSpace(); - write(SUBCLASS); - writeSpace(); - write(NOT); - write(EXISTS); - writeSpace(); - axiom.getProperty().accept(this); - write(" ."); - write(SELF); - } - - @Override - public void visit(OWLSubDataPropertyOfAxiom axiom) { - axiom.getSubProperty().accept(this); - write(SUBCLASS); - axiom.getSuperProperty().accept(this); - } - - @Override - public void visit(OWLInverseFunctionalObjectPropertyAxiom axiom) { - write(TOP); - writeSpace(); - write(SUBCLASS); - writeSpace(); - write(MAX); - writeSpace(); - write(1); - writeSpace(); - axiom.getProperty().accept(this); - write(INVERSE); - } - - @Override - public void visit(OWLSameIndividualAxiom axiom) { - write(axiom.getIndividuals(), EQUAL, false); - } - - @Override - public void visit(OWLSubPropertyChainOfAxiom axiom) { - write(axiom.getPropertyChain(), COMP, false); - writeSpace(); - write(SUBCLASS); - writeSpace(); - axiom.getSuperProperty().accept(this); - } - - @Override - public void visit(OWLInverseObjectPropertiesAxiom axiom) { - OWLObject o1 = axiom.getFirstProperty(); - OWLObject o2 = axiom.getSecondProperty(); - OWLObject first, second; - if (isFocusedObject(o1) || !isFocusedObject(o2)) { - first = o1; - second = o2; - } else { - first = o2; - second = o1; - } - first.accept(this); - writeSpace(); - write(EQUIVALENT_TO); - writeSpace(); - second.accept(this); - write(INVERSE); - } - - @Override - public void visit(SWRLRule rule) { - write(rule.getHead(), WEDGE, false); - writeSpace(); - write(IMPLIES); - writeSpace(); - write(rule.getBody(), WEDGE, false); - } - - @Override - public void visit(OWLClass desc) { - if (desc.isOWLThing()) { - write(TOP); - } else if (desc.isOWLNothing()) { - write(BOTTOM); - } else { - writeEntity(desc); - } - } - - @Override - public void visit(OWLObjectIntersectionOf desc) { - write(desc.getOperands(), AND, true); - } - - @Override - public void visit(OWLObjectUnionOf desc) { - write(desc.getOperands(), OR, true); - } - - @Override - public void visit(OWLObjectComplementOf desc) { - write(NOT); - writeNested(desc.getOperand()); - } - - private void writeCardinalityRestriction(OWLDataCardinalityRestriction restriction, DLSyntax keyword) { - write(keyword); - writeSpace(); - write(restriction.getCardinality()); - writeSpace(); - restriction.getProperty().accept(this); - // if (restriction.isQualified()) { - writeRestrictionSeparator(); - writeNested(restriction.getFiller()); - // } - } - - private void writeCardinalityRestriction(OWLObjectCardinalityRestriction restriction, DLSyntax keyword) { - write(keyword); - writeSpace(); - write(restriction.getCardinality()); - writeSpace(); - restriction.getProperty().accept(this); - // if (restriction.isQualified()) { - writeRestrictionSeparator(); - writeNested(restriction.getFiller()); - // } - } - - private void writeQuantifiedRestriction(OWLQuantifiedDataRestriction restriction, DLSyntax keyword) { - write(keyword); - writeSpace(); - restriction.getProperty().accept(this); - writeRestrictionSeparator(); - writeNested(restriction.getFiller()); - } - - private void writeQuantifiedRestriction(OWLQuantifiedObjectRestriction restriction, DLSyntax keyword) { - write(keyword); - writeSpace(); - restriction.getProperty().accept(this); - writeRestrictionSeparator(); - writeNested(restriction.getFiller()); - } - - @Override - public void visit(OWLObjectSomeValuesFrom desc) { - writeQuantifiedRestriction(desc, EXISTS); - } - - @Override - public void visit(OWLObjectAllValuesFrom desc) { - writeQuantifiedRestriction(desc, FORALL); - } - - private , V extends OWLObject> void writeValueRestriction( - OWLHasValueRestriction restriction) { - write(EXISTS); - writeSpace(); - restriction.getProperty().accept(this); - writeRestrictionSeparator(); - write("{"); - restriction.getValue().accept(this); - write("}"); - } - - @Override - public void visit(OWLObjectHasValue desc) { - writeValueRestriction(desc); - } - - @Override - public void visit(OWLObjectMinCardinality desc) { - writeCardinalityRestriction(desc, MIN); - } - - @Override - public void visit(OWLObjectExactCardinality desc) { - writeCardinalityRestriction(desc, EQUAL); - } - - @Override - public void visit(OWLObjectMaxCardinality desc) { - writeCardinalityRestriction(desc, MAX); - } - - @Override - public void visit(OWLObjectHasSelf desc) { - write(EXISTS); - writeSpace(); - desc.getProperty().accept(this); - write(" ."); - write(SELF); - } - - @Override - public void visit(OWLObjectOneOf desc) { - for (Iterator it = desc.getIndividuals().iterator(); it.hasNext();) { - write("{"); - it.next().accept(this); - write("}"); - if (it.hasNext()) { - write(" "); - write(OR); - write(" "); + while(iterator.hasNext()) { + OWLFacetRestriction facetRestriction = iterator.next(); + facetRestriction.accept(this); + if(iterator.hasNext()) { + write(" " + COMMA + " "); } } + write("]"); } @Override - public void visit(OWLDataSomeValuesFrom desc) { - writeQuantifiedRestriction(desc, EXISTS); - } - - @Override - public void visit(OWLDataAllValuesFrom desc) { - writeQuantifiedRestriction(desc, FORALL); - } - - @Override - public void visit(OWLDataHasValue desc) { - writeValueRestriction(desc); - } - - @Override - public void visit(OWLDataMinCardinality desc) { - writeCardinalityRestriction(desc, MIN); - } - - @Override - public void visit(OWLDataExactCardinality desc) { - writeCardinalityRestriction(desc, EQUAL); - } - - @Override - public void visit(OWLDataMaxCardinality desc) { - writeCardinalityRestriction(desc, MAX); - } - - // ///////////////////////////////////////////////////////////////////////////////////////////// - // - // Data stuff - // - // ///////////////////////////////////////////////////////////////////////////////////////////// - - public void visit(OWLDataIntersectionOf node) { - write("("); - write(node.getOperands(), AND, false); - write(")"); - } - - public void visit(OWLDataUnionOf node) { - write("("); - write(node.getOperands(), OR, false); - write(")"); - } - - public void visit(OWLDatatypeRestriction node) { - node.getDatatype().accept(this); - write("["); - Iterator iterator = node.getFacetRestrictions().iterator(); - - while(iterator.hasNext()) { - OWLFacetRestriction facetRestriction = iterator.next(); - facetRestriction.accept(this); - if(iterator.hasNext()) { - write(" " + COMMA + " "); - } - } - write("]"); - } - - public void visit(OWLFacetRestriction node) { - write(node.getFacet().getSymbolicForm()); - writeSpace(); - node.getFacetValue().accept(this); - } - - @Override - public void visit(OWLDatatype node) { - write(shortFormProvider.getShortForm(node)); - } - - @Override - public void visit(OWLDataComplementOf node) { - write(NOT); - node.getDataRange().accept(this); - } - - @Override - public void visit(OWLDataOneOf node) { - for (Iterator it = node.getValues().iterator(); it.hasNext();) { - write("{"); - it.next().accept(this); - write("}"); - if (it.hasNext()) { - write(OR); - } + public void visit(@Nonnull OWLFacetRestriction node) { + switch (node.getFacet()) { + case MIN_INCLUSIVE: write("\u2265"); /* >= */ break; + case MIN_EXCLUSIVE: write("\u003e"); /* > */ break; + case MAX_INCLUSIVE: write("\u2264"); /* <= */ break; + case MAX_EXCLUSIVE: write("\u003c"); /* < */ break; + default: + write(node.getFacet().getSymbolicForm()); } + writeSpace(); + node.getFacetValue().accept(this); } - @Override - public void visit(OWLLiteral node) { - write(node.getLiteral()); - } - - @Override - public void visit(OWLObjectProperty property) { - writeEntity(property); - } - - @Override - public void visit(OWLObjectInverseOf property) { - property.getInverse().accept(this); - write(INVERSE); - } - - @Override - public void visit(OWLDataProperty property) { - writeEntity(property); - } - - @Override - public void visit(OWLNamedIndividual individual) { - writeEntity(individual); - } - - @Override - public void visit(OWLDatatypeDefinitionAxiom axiom) { - } - - @Override - public void visit(OWLHasKeyAxiom axiom) { - } - - @Override - public void visit(OWLAnnotationPropertyDomainAxiom axiom) { - } - - @Override - public void visit(OWLAnnotationPropertyRangeAxiom axiom) { - } - - @Override - public void visit(OWLSubAnnotationPropertyOfAxiom axiom) { - } - - @Override - public void visit(OWLAnnotationProperty property) { - } - - @Override - public void visit(OWLAnonymousIndividual individual) { - } - - @Override - public void visit(IRI iri) { - } - - @Override - public void visit(OWLAnnotation node) { - } - - @Override - public void visit(SWRLClassAtom node) { - node.getPredicate().accept(this); - write("("); - node.getArgument().accept(this); - write(")"); - } - - @Override - public void visit(SWRLDataRangeAtom node) { - node.getPredicate().accept(this); - write("("); - node.getArgument().accept(this); - write(")"); - } - - @Override - public void visit(SWRLObjectPropertyAtom node) { - node.getPredicate().accept(this); - write("("); - node.getFirstArgument().accept(this); - write(", "); - node.getSecondArgument().accept(this); - write(")"); - } - - @Override - public void visit(SWRLDataPropertyAtom node) { - node.getPredicate().accept(this); - write("("); - node.getFirstArgument().accept(this); - write(", "); - node.getSecondArgument().accept(this); - write(")"); - } - - @Override - public void visit(SWRLBuiltInAtom node) { - write(node.getPredicate().toString()); - write("("); - write(node.getArguments(), COMMA, true); - write(")"); - } - - @Override - public void visit(SWRLVariable node) { - write("?"); - write(iriShortFormProvider.getShortForm(node.getIRI())); - } - - @Override - public void visit(SWRLIndividualArgument node) { - node.getIndividual().accept(this); - } - - @Override - public void visit(SWRLLiteralArgument node) { - node.getLiteral().accept(this); - } - - @Override - public void visit(SWRLSameIndividualAtom node) { - write("sameAs("); - node.getFirstArgument().accept(this); - write(", "); - node.getSecondArgument().accept(this); - write(")"); - } - - @Override - public void visit(SWRLDifferentIndividualsAtom node) { - write("differentFrom("); - node.getFirstArgument().accept(this); - write(", "); - node.getSecondArgument().accept(this); - write(")"); + /* private :-( */ + protected void writeSpace() { + write(" "); } } diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/DescriptionSet.java b/components-core/src/main/java/org/dllearner/utilities/owl/DescriptionSet.java deleted file mode 100644 index 998d76fcc3..0000000000 --- a/components-core/src/main/java/org/dllearner/utilities/owl/DescriptionSet.java +++ /dev/null @@ -1,69 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.utilities.owl; - -import java.util.Collection; -import java.util.Iterator; -import java.util.SortedSet; -import java.util.TreeSet; - -import org.semanticweb.owlapi.model.OWLClassExpression; - -/** - * A set of descriptions, which is bound by a maximum - * size. Can be used by algorithms to store the most promising - * n class descriptions. - * - * @author Jens Lehmann - * - */ -public class DescriptionSet { - - private SortedSet set = new TreeSet(); - - private int maxSize; - - public DescriptionSet(int maxSize) { - this.maxSize = maxSize; - } - - public void add(OWLClassExpression ed) { - set.add(ed); - if(set.size()>maxSize) { - Iterator it = set.iterator(); - it.next(); - it.remove(); - } - } - - public void addAll(Collection eds) { - for(OWLClassExpression ed : eds) { - add(ed); - } - } - - /** - * @return the set - */ - public SortedSet getSet() { - return set; - } - -} diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/EvaluatedDescriptionComparator.java b/components-core/src/main/java/org/dllearner/utilities/owl/EvaluatedDescriptionComparator.java index 68afb7ce23..1753c7b41f 100644 --- a/components-core/src/main/java/org/dllearner/utilities/owl/EvaluatedDescriptionComparator.java +++ b/components-core/src/main/java/org/dllearner/utilities/owl/EvaluatedDescriptionComparator.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.owl; import java.util.Comparator; @@ -29,6 +28,8 @@ import org.semanticweb.owlapi.model.OWLNaryBooleanClassExpression; import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom; +import com.google.common.collect.ComparisonChain; + /** * Comparator for evaluated descriptions, which orders them by * accuracy as first criterion, length as second criterion, and @@ -44,26 +45,11 @@ public class EvaluatedDescriptionComparator implements Comparator ed1, EvaluatedDescription ed2) { - double acc1 = ed1.getAccuracy(); - double acc2 = ed2.getAccuracy(); - if(acc1 > acc2) - return 1; - else if(acc1 < acc2) - return -1; - else { - int length1 = - getLength(ed1); -// ed1.getDescriptionLength(); - int length2 = - getLength(ed2); -// ed2.getDescriptionLength(); - if(length1 < length2) - return 1; - else if(length1 > length2) - return -1; - else - return ed1.getDescription().compareTo(ed2.getDescription()); - } + return ComparisonChain.start() + .compare(ed1.getAccuracy(), ed2.getAccuracy()) // higher accuracy is better + .compare(getLength(ed2), getLength(ed1)) // shorter is better + .compare(ed1.getDescription(), ed2.getDescription()) // syntactic sorting + .result(); } private int getLength(EvaluatedDescription ed){ diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/EvaluatedDescriptionPosNegComparator.java b/components-core/src/main/java/org/dllearner/utilities/owl/EvaluatedDescriptionPosNegComparator.java index 9a4a8adf3f..a500d16f93 100644 --- a/components-core/src/main/java/org/dllearner/utilities/owl/EvaluatedDescriptionPosNegComparator.java +++ b/components-core/src/main/java/org/dllearner/utilities/owl/EvaluatedDescriptionPosNegComparator.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.owl; import java.util.Comparator; diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/EvaluatedDescriptionSet.java b/components-core/src/main/java/org/dllearner/utilities/owl/EvaluatedDescriptionSet.java index 784fa008da..b33ab8055e 100644 --- a/components-core/src/main/java/org/dllearner/utilities/owl/EvaluatedDescriptionSet.java +++ b/components-core/src/main/java/org/dllearner/utilities/owl/EvaluatedDescriptionSet.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,13 +16,12 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.owl; import java.util.Collection; -import java.util.Iterator; import java.util.LinkedList; import java.util.List; +import java.util.NavigableSet; import java.util.TreeSet; import org.dllearner.core.AbstractClassExpressionLearningProblem; @@ -32,9 +31,8 @@ import org.semanticweb.owlapi.model.OWLClassExpression; /** - * A set of evaluated descriptions, which is bound by a maximum - * size. Can be used by algorithms to store the most promising - * n class descriptions. + * A set of evaluated descriptions, which is bound by a maximum size. Can be + * used by algorithms to store the most promising n class descriptions. * * @author Jens Lehmann * @@ -42,88 +40,135 @@ public class EvaluatedDescriptionSet { private EvaluatedDescriptionComparator comp = new EvaluatedDescriptionComparator(); - - private TreeSet> set = new TreeSet<>(comp); + + private NavigableSet> set = new TreeSet<>(comp); private int maxSize; - + + /** + * @param maxSize the maximum number of elements contained in this set + */ public EvaluatedDescriptionSet(int maxSize) { this.maxSize = maxSize; } - - public void add(OWLClassExpression description, double accuracy, AbstractClassExpressionLearningProblem problem) { - // bug http://sourceforge.net/tracker/?func=detail&atid=986319&aid=3029181&group_id=203619 - // -> set should be filled up to max size before we compare acc. with the worst result - if(set.size() problem) { + // bug + // http://sourceforge.net/tracker/?func=detail&atid=986319&aid=3029181&group_id=203619 + // -> set should be filled up to max size before we compare acc. with + // the worst result + if (set.size() < maxSize || getWorst().getAccuracy() <= accuracy) { set.add(problem.evaluate(description)); - } - if(set.size()>maxSize) { - // delete the worst element - Iterator> it = set.iterator(); - it.next(); - it.remove(); - } + } + // delete the worst element if set is full + if (set.size() > maxSize) { + set.pollFirst(); + } } - + + /** + * Adds an evaluated description to this set and ensures that the size does not + * exceed the limit. + * @param ed the evaluated description to add + */ public void add(EvaluatedDescription ed) { set.add(ed); - if(set.size()>maxSize) { - Iterator> it = set.iterator(); - it.next(); - it.remove(); + // delete the worst element if set is full + if (set.size() > maxSize) { + set.pollFirst(); } } + /** + * Adds a collection of evaluated description to this set and ensures that the size does not + * exceed the limit. + * @param eds the evaluated descriptions to add + */ public void addAll(Collection eds) { for(EvaluatedDescriptionPosNeg ed : eds) { add(ed); } - } - + } + + /** + * @return true if this set with a maximum size of n contains n elements. + */ public boolean isFull() { return (set.size() >= maxSize); } - + + /** + * @return true if this set contains no elements. + */ + public boolean isEmpty() { + return (set.isEmpty()); + } + + /** + * @return the size of this set + */ public int size() { return set.size(); } - + + /** + * @return the best evaluated description or null if this set is empty. + */ public EvaluatedDescription getBest() { - return set.size()==0 ? null : set.last(); - } - - public double getBestAccuracy() { - return set.size()==0 ? Double.NEGATIVE_INFINITY : set.last().getAccuracy(); + return set.isEmpty() ? null : set.last(); } + /** + * @return the worst evaluated description or null if this set is empty. + */ public EvaluatedDescription getWorst() { - return set.size()==0 ? null : set.first(); + return set.isEmpty() ? null : set.first(); } - + /** - * @return the set + * @return the best accuracy so far or -Infinity if this set is empty. */ - public TreeSet> getSet() { + public double getBestAccuracy() { + return set.isEmpty() ? Double.NEGATIVE_INFINITY : set.last().getAccuracy(); + } + + /** + * @return the underlying set of evaluated descriptions. + */ + public NavigableSet> getSet() { return set; } - + + /** + * @return a list which contains only the class expressions of this set. + */ public List toDescriptionList() { - List list = new LinkedList(); + List list = new LinkedList<>(); for(EvaluatedDescription ed : set.descendingSet()) { list.add(ed.getDescription()); } return list; } - @Override - public String toString() { - return set.toString(); - } - /** - * @return the maxSize + * @return the maximum size of this set. */ public int getMaxSize() { return maxSize; } + + @Override + public String toString() { + return set.toString(); + } } diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/ManchesterOWLSyntaxOWLObjectRendererImplExt.java b/components-core/src/main/java/org/dllearner/utilities/owl/ManchesterOWLSyntaxOWLObjectRendererImplExt.java index 8457741b99..868056238e 100644 --- a/components-core/src/main/java/org/dllearner/utilities/owl/ManchesterOWLSyntaxOWLObjectRendererImplExt.java +++ b/components-core/src/main/java/org/dllearner/utilities/owl/ManchesterOWLSyntaxOWLObjectRendererImplExt.java @@ -1,52 +1,32 @@ -/* - * This file is part of the OWL API. - * - * The contents of this file are subject to the LGPL License, Version 3.0. +/** + * Copyright (C) 2007 - 2016, Jens Lehmann * - * Copyright (C) 2011, The University of Manchester + * This file is part of DL-Learner. * - * This program is free software: you can redistribute it and/or modify + * DL-Learner is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or + * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * - * This program is distributed in the hope that it will be useful, + * DL-Learner is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - * - * - * Alternatively, the contents of this file may be used under the terms of the Apache License, Version 2.0 - * in which case, the provisions of the Apache License Version 2.0 are applicable instead of those above. - * - * Copyright 2011, University of Manchester - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * along with this program. If not, see . */ package org.dllearner.utilities.owl; -import java.io.IOException; -import java.io.StringWriter; -import java.io.Writer; - import org.semanticweb.owlapi.io.OWLObjectRenderer; import org.semanticweb.owlapi.model.OWLObject; import org.semanticweb.owlapi.util.ShortFormProvider; import org.semanticweb.owlapi.util.SimpleShortFormProvider; +import java.io.IOException; +import java.io.StringWriter; +import java.io.Writer; + /** * An implementation of the OWLObjectRenderer interface. (Renders standalone * class class expressions and axioms in the manchester syntax). diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/ManchesterOWLSyntaxObjectRendererExt.java b/components-core/src/main/java/org/dllearner/utilities/owl/ManchesterOWLSyntaxObjectRendererExt.java index a6f9230346..6eb0e5aedf 100644 --- a/components-core/src/main/java/org/dllearner/utilities/owl/ManchesterOWLSyntaxObjectRendererExt.java +++ b/components-core/src/main/java/org/dllearner/utilities/owl/ManchesterOWLSyntaxObjectRendererExt.java @@ -1,108 +1,41 @@ -/* - * This file is part of the OWL API. - * - * The contents of this file are subject to the LGPL License, Version 3.0. +/** + * Copyright (C) 2007 - 2016, Jens Lehmann * - * Copyright (C) 2011, The University of Manchester + * This file is part of DL-Learner. * - * This program is free software: you can redistribute it and/or modify + * DL-Learner is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or + * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * - * This program is distributed in the hope that it will be useful, + * DL-Learner is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - * - * - * Alternatively, the contents of this file may be used under the terms of the Apache License, Version 2.0 - * in which case, the provisions of the Apache License Version 2.0 are applicable instead of those above. - * - * Copyright 2011, University of Manchester - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * along with this program. If not, see . */ package org.dllearner.utilities.owl; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.AND; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.ANNOTATIONS; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.ANNOTATION_PROPERTY; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.ASYMMETRIC; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.CLASS; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.COMMA; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.DATA_PROPERTY; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.DIFFERENT_FROM; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.DIFFERENT_INDIVIDUALS; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.DISJOINT_CLASSES; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.DISJOINT_PROPERTIES; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.DISJOINT_UNION_OF; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.DISJOINT_WITH; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.DOMAIN; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.EQUIVALENT_CLASSES; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.EQUIVALENT_PROPERTIES; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.EQUIVALENT_TO; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.EXACTLY; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.FACET_RESTRICTION_SEPARATOR; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.FUNCTIONAL; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.HAS_KEY; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.INDIVIDUAL; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.INVERSE; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.INVERSE_FUNCTIONAL; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.INVERSE_OF; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.IRREFLEXIVE; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.MAX; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.MIN; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.NOT; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.OBJECT_PROPERTY; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.ONE_OF_DELIMETER; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.ONLY; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.ONTOLOGY; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.OR; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.RANGE; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.REFLEXIVE; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.SAME_AS; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.SAME_INDIVIDUAL; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.SELF; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.SOME; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.SUBCLASS_OF; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.SUB_PROPERTY_OF; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.SYMMETRIC; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.TRANSITIVE; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.TYPE; -import static org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax.VALUE; - -import java.io.Writer; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.Set; -import java.util.TreeSet; -import java.util.logging.Level; -import org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax; +import org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax; +import org.semanticweb.owlapi.manchestersyntax.renderer.AbstractRenderer; import org.semanticweb.owlapi.model.*; import org.semanticweb.owlapi.util.CollectionFactory; import org.semanticweb.owlapi.util.ShortFormProvider; import org.semanticweb.owlapi.vocab.SWRLBuiltInsVocabulary; import org.semanticweb.owlapi.vocab.XSDVocabulary; -import uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.AbstractRenderer; +import javax.annotation.Nonnull; +import java.io.Writer; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.*; +import static org.semanticweb.owlapi.util.CollectionFactory.sortOptionally; /** * @author Matthew Horridge, The University Of Manchester, Bio-Health @@ -111,9 +44,6 @@ public class ManchesterOWLSyntaxObjectRendererExt extends AbstractRenderer implements OWLObjectVisitor { - /** line length */ - public static final int LINE_LENGTH = 70; - /** * @param writer * writer @@ -125,28 +55,34 @@ public ManchesterOWLSyntaxObjectRendererExt(Writer writer, super(writer, entityShortFormProvider); } - protected List sort( - Collection objects) { - List sortedObjects = new ArrayList( - objects); - sortOptionally(sortedObjects); - return sortedObjects; - } - - private void sortOptionally(List toReturn) { - try { - Collections.sort(toReturn); - } catch (IllegalArgumentException e) { - // catch possible sorting misbehaviour - if (!e.getMessage().contains( - "Comparison method violates its general contract!")) { - throw e; - } - } + @Override + public void setUseTabbing(boolean useTabbing) { + super.setUseTabbing(useTabbing); + } + + @Override + public boolean isUseTabbing() { + return super.isUseTabbing(); + } + + @Override + public void setUseWrapping(boolean useWrapping) { + super.setUseWrapping(useWrapping); } - protected void write(Set objects, - ManchesterOWLSyntax delimeter, boolean newline) { + @Override + public boolean isUseWrapping() { + return super.isUseWrapping(); + } + + @Nonnull + protected static List sort( + @Nonnull Collection objects) { + return CollectionFactory.sortOptionally(objects); + } + + protected void write(@Nonnull Set objects, + @Nonnull ManchesterOWLSyntax delimeter, boolean newline) { int tab = getIndent(); pushTab(tab); for (Iterator it = sort(objects).iterator(); it @@ -162,9 +98,9 @@ protected void write(Set objects, popTab(); } - protected void writeCommaSeparatedList(Set objects) { - for (Iterator it = new TreeSet(objects) - .iterator(); it.hasNext();) { + protected void writeCommaSeparatedList( + @Nonnull Set objects) { + for (Iterator it = sort(objects).iterator(); it.hasNext();) { it.next().accept(this); if (it.hasNext()) { write(", "); @@ -172,8 +108,8 @@ protected void writeCommaSeparatedList(Set objects) { } } - protected void write(Set objects, - boolean newline) { + protected void write(@Nonnull Set objects, + boolean newline) { boolean first = true; for (Iterator it = sort(objects).iterator(); it .hasNext();) { @@ -195,15 +131,17 @@ protected void write(Set objects, } } - private void writeRestriction(OWLQuantifiedDataRestriction restriction, - ManchesterOWLSyntax keyword) { + private void writeRestriction( + @Nonnull OWLQuantifiedDataRestriction restriction, + @Nonnull ManchesterOWLSyntax keyword) { restriction.getProperty().accept(this); write(keyword); restriction.getFiller().accept(this); } - private void writeRestriction(OWLQuantifiedObjectRestriction restriction, - ManchesterOWLSyntax keyword) { + private void writeRestriction( + @Nonnull OWLQuantifiedObjectRestriction restriction, + @Nonnull ManchesterOWLSyntax keyword) { restriction.getProperty().accept(this); write(keyword); boolean conjunctionOrDisjunction = false; @@ -212,8 +150,8 @@ private void writeRestriction(OWLQuantifiedObjectRestriction restriction, || restriction.getFiller() instanceof OWLObjectUnionOf) { conjunctionOrDisjunction = true; incrementTab(4); - if(isUseWrapping()) { - writeNewLine(); + if (isUseWrapping()) { + writeNewLine(); } } write("("); @@ -227,23 +165,21 @@ private void writeRestriction(OWLQuantifiedObjectRestriction restriction, } } - private - , V extends OWLObject> - void writeRestriction(OWLHasValueRestriction restriction) { - restriction.getProperty().accept(this); + private void writeRestriction( + @Nonnull OWLHasValueRestriction restriction, + @Nonnull OWLPropertyExpression p) { + p.accept(this); write(VALUE); - restriction.getValue().accept(this); + restriction.getFiller().accept(this); } - private - , F extends OWLPropertyRange> - void writeRestriction( - OWLCardinalityRestriction restriction, - ManchesterOWLSyntax keyword) { - restriction.getProperty().accept(this); + private void writeRestriction( + @Nonnull OWLCardinalityRestriction restriction, + @Nonnull ManchesterOWLSyntax keyword, + @Nonnull OWLPropertyExpression p) { + p.accept(this); write(keyword); write(Integer.toString(restriction.getCardinality())); - // if(restriction.isQualified()) { writeSpace(); if (restriction.getFiller() instanceof OWLAnonymousClassExpression) { write("("); @@ -252,34 +188,26 @@ void writeRestriction( if (restriction.getFiller() instanceof OWLAnonymousClassExpression) { write(")"); } - // } } - // ///////////////////////////////////////////////////////////////////////////////////////////// - // // Class expressions - // - // ///////////////////////////////////////////////////////////////////////////////////////////// @Override - public void visit(OWLClass desc) { - write(getShortFormProvider().getShortForm(desc)); + public void visit(OWLClass ce) { + write(getShortFormProvider().getShortForm(ce)); } @Override - public void visit(OWLObjectIntersectionOf desc) { - write(desc.getOperands(), true); + public void visit(@Nonnull OWLObjectIntersectionOf ce) { + write(ce.getOperands(), true); } @Override - public void visit(OWLObjectUnionOf desc) { + public void visit(@Nonnull OWLObjectUnionOf ce) { boolean first = true; - for (Iterator it = desc.getOperands() + for (Iterator it = sortOptionally(ce.getOperands()) .iterator(); it.hasNext();) { OWLClassExpression op = it.next(); if (!first) { - // if (isUseWrapping()) { - // writeNewLine(); - // } write(" ", OR, " "); } first = false; @@ -294,96 +222,92 @@ public void visit(OWLObjectUnionOf desc) { } @Override - public void visit(OWLObjectComplementOf desc) { - write("", NOT, desc.isAnonymous() ? " " : ""); - if (desc.isAnonymous()) { + public void visit(@Nonnull OWLObjectComplementOf ce) { + write("", NOT, ce.isAnonymous() ? " " : ""); + if (ce.isAnonymous()) { write("("); } - desc.getOperand().accept(this); - if (desc.isAnonymous()) { + ce.getOperand().accept(this); + if (ce.isAnonymous()) { write(")"); } } @Override - public void visit(OWLObjectSomeValuesFrom desc) { - writeRestriction(desc, SOME); + public void visit(OWLObjectSomeValuesFrom ce) { + writeRestriction(ce, SOME); } @Override - public void visit(OWLObjectAllValuesFrom desc) { - writeRestriction(desc, ONLY); + public void visit(OWLObjectAllValuesFrom ce) { + writeRestriction(ce, ONLY); } @Override - public void visit(OWLObjectHasValue desc) { - writeRestriction(desc); + public void visit(@Nonnull OWLObjectHasValue ce) { + writeRestriction(ce, ce.getProperty()); } @Override - public void visit(OWLObjectMinCardinality desc) { - writeRestriction(desc, MIN); + public void visit(@Nonnull OWLObjectMinCardinality ce) { + writeRestriction(ce, MIN, ce.getProperty()); } @Override - public void visit(OWLObjectExactCardinality desc) { - writeRestriction(desc, EXACTLY); + public void visit(@Nonnull OWLObjectExactCardinality ce) { + writeRestriction(ce, EXACTLY, ce.getProperty()); } @Override - public void visit(OWLObjectMaxCardinality desc) { - writeRestriction(desc, MAX); + public void visit(@Nonnull OWLObjectMaxCardinality ce) { + writeRestriction(ce, MAX, ce.getProperty()); } @Override - public void visit(OWLObjectHasSelf desc) { - desc.getProperty().accept(this); + public void visit(@Nonnull OWLObjectHasSelf ce) { + ce.getProperty().accept(this); write(SOME); write(SELF); } @Override - public void visit(OWLObjectOneOf desc) { + public void visit(@Nonnull OWLObjectOneOf ce) { write("{"); - write(desc.getIndividuals(), ONE_OF_DELIMETER, false); + write(ce.getIndividuals(), ONE_OF_DELIMETER, false); write("}"); } @Override - public void visit(OWLDataSomeValuesFrom desc) { - writeRestriction(desc, SOME); + public void visit(OWLDataSomeValuesFrom ce) { + writeRestriction(ce, SOME); } @Override - public void visit(OWLDataAllValuesFrom desc) { - writeRestriction(desc, ONLY); + public void visit(OWLDataAllValuesFrom ce) { + writeRestriction(ce, ONLY); } @Override - public void visit(OWLDataHasValue desc) { - writeRestriction(desc); + public void visit(@Nonnull OWLDataHasValue ce) { + writeRestriction(ce, ce.getProperty()); } @Override - public void visit(OWLDataMinCardinality desc) { - writeRestriction(desc, MIN); + public void visit(@Nonnull OWLDataMinCardinality ce) { + writeRestriction(ce, MIN, ce.getProperty()); } @Override - public void visit(OWLDataExactCardinality desc) { - writeRestriction(desc, EXACTLY); + public void visit(@Nonnull OWLDataExactCardinality ce) { + writeRestriction(ce, EXACTLY, ce.getProperty()); } @Override - public void visit(OWLDataMaxCardinality desc) { - writeRestriction(desc, MAX); + public void visit(@Nonnull OWLDataMaxCardinality ce) { + writeRestriction(ce, MAX, ce.getProperty()); } - // ///////////////////////////////////////////////////////////////////////////////////////////// - // // Entities stuff - // - // ///////////////////////////////////////////////////////////////////////////////////////////// @Override public void visit(OWLObjectProperty property) { write(getShortFormProvider().getShortForm(property)); @@ -405,51 +329,31 @@ public void visit(OWLAnnotationProperty property) { } @Override - public void visit(OWLDatatype datatype) { - write(getShortFormProvider().getShortForm(datatype)); + public void visit(OWLDatatype node) { + write(getShortFormProvider().getShortForm(node)); } @Override - public void visit(OWLAnonymousIndividual individual) { - write(individual.toString()); + public void visit(@Nonnull OWLAnonymousIndividual individual) { + write(individual.toStringID()); } @Override - public void visit(IRI iri) { + public void visit(@Nonnull IRI iri) { write(iri.toQuotedString()); } @Override - public void visit(OWLAnnotation node) { + public void visit(@Nonnull OWLAnnotation node) { writeAnnotations(node.getAnnotations()); node.getProperty().accept(this); writeSpace(); node.getValue().accept(this); } - // private String escape(String s) { - // for(int i = 0; i < s.length(); i++) { - // char ch = s.charAt(i); - // if(i == 0 && ch == '\'') { - // return s; - // } - // if(" [](){},^<>?@".indexOf(ch) != -1) { - // StringBuilder sb = new StringBuilder(); - // sb.append("'"); - // sb.append(s); - // sb.append("'"); - // return sb.toString(); - // } - // } - // return s; - // } - // ///////////////////////////////////////////////////////////////////////////////////////////// - // // Data stuff - // - // ///////////////////////////////////////////////////////////////////////////////////////////// @Override - public void visit(OWLDataComplementOf node) { + public void visit(@Nonnull OWLDataComplementOf node) { write(NOT); if (node.getDataRange().isDatatype()) { node.getDataRange().accept(this); @@ -461,28 +365,28 @@ public void visit(OWLDataComplementOf node) { } @Override - public void visit(OWLDataOneOf node) { + public void visit(@Nonnull OWLDataOneOf node) { write("{"); write(node.getValues(), ONE_OF_DELIMETER, false); write("}"); } @Override - public void visit(OWLDataIntersectionOf node) { + public void visit(@Nonnull OWLDataIntersectionOf node) { write("("); write(node.getOperands(), AND, false); write(")"); } @Override - public void visit(OWLDataUnionOf node) { + public void visit(@Nonnull OWLDataUnionOf node) { write("("); write(node.getOperands(), OR, false); write(")"); } @Override - public void visit(OWLDatatypeRestriction node) { + public void visit(@Nonnull OWLDatatypeRestriction node) { node.getDatatype().accept(this); write("["); write(node.getFacetRestrictions(), FACET_RESTRICTION_SEPARATOR, false); @@ -490,7 +394,7 @@ public void visit(OWLDatatypeRestriction node) { } @Override - public void visit(OWLLiteral node) { + public void visit(@Nonnull OWLLiteral node) { // xsd:decimal is the default datatype for literal forms like "33.3" // with no specified datatype if (XSDVocabulary.DECIMAL.getIRI().equals(node.getDatatype().getIRI())) { @@ -516,7 +420,7 @@ public void visit(OWLLiteral node) { } } - private void writeLiteral(String literal) { + private void writeLiteral(@Nonnull String literal) { write("\""); for (int i = 0; i < literal.length(); i++) { char ch = literal.charAt(i); @@ -531,37 +435,24 @@ private void writeLiteral(String literal) { } @Override - public void visit(OWLFacetRestriction node) { + public void visit(@Nonnull OWLFacetRestriction node) { write(node.getFacet().getSymbolicForm()); writeSpace(); node.getFacetValue().accept(this); } - // ///////////////////////////////////////////////////////////////////////////////////////////// - // // Property expression stuff - // - // ///////////////////////////////////////////////////////////////////////////////////////////// @Override - public void visit(OWLObjectInverseOf property) { + public void visit(@Nonnull OWLObjectInverseOf property) { write(INVERSE); write("("); property.getInverse().accept(this); write(")"); } - // ///////////////////////////////////////////////////////////////////////////////////////////// - // // Annotation stuff - // - // ///////////////////////////////////////////////////////////////////////////////////////////// - // ///////////////////////////////////////////////////////////////////////////////////////////// - // // Stand alone axiom representation - // // We render each axiom as a one line frame - // - // ///////////////////////////////////////////////////////////////////////////////////////////// private boolean wrapSave; private boolean tabSave; @@ -578,7 +469,7 @@ private void restore() { } @Override - public void visit(OWLSubClassOfAxiom axiom) { + public void visit(@Nonnull OWLSubClassOfAxiom axiom) { setAxiomWriting(); axiom.getSubClass().accept(this); write(SUBCLASS_OF); @@ -587,7 +478,7 @@ public void visit(OWLSubClassOfAxiom axiom) { } @Override - public void visit(OWLNegativeObjectPropertyAssertionAxiom axiom) { + public void visit(@Nonnull OWLNegativeObjectPropertyAssertionAxiom axiom) { setAxiomWriting(); write(NOT); write("("); @@ -600,15 +491,8 @@ public void visit(OWLNegativeObjectPropertyAssertionAxiom axiom) { restore(); } - // private void writePropertyCharacteristic(ManchesterOWLSyntax - // characteristic) { - // setAxiomWriting(); - // writeSectionKeyword(CHARACTERISTICS); - // write(characteristic); - // restore(); - // } @Override - public void visit(OWLAsymmetricObjectPropertyAxiom axiom) { + public void visit(@Nonnull OWLAsymmetricObjectPropertyAxiom axiom) { setAxiomWriting(); writeSectionKeyword(ASYMMETRIC); axiom.getProperty().accept(this); @@ -616,17 +500,19 @@ public void visit(OWLAsymmetricObjectPropertyAxiom axiom) { } @Override - public void visit(OWLReflexiveObjectPropertyAxiom axiom) { + public void visit(@Nonnull OWLReflexiveObjectPropertyAxiom axiom) { setAxiomWriting(); writeSectionKeyword(REFLEXIVE); axiom.getProperty().accept(this); restore(); } - private void writeBinaryOrNaryList(ManchesterOWLSyntax binaryKeyword, - Set objects, ManchesterOWLSyntax naryKeyword) { + private void writeBinaryOrNaryList( + @Nonnull ManchesterOWLSyntax binaryKeyword, + @Nonnull Set objects, + @Nonnull ManchesterOWLSyntax naryKeyword) { if (objects.size() == 2) { - Iterator it = objects.iterator(); + Iterator it = sort(objects).iterator(); it.next().accept(this); write(binaryKeyword); it.next().accept(this); @@ -637,7 +523,7 @@ private void writeBinaryOrNaryList(ManchesterOWLSyntax binaryKeyword, } @Override - public void visit(OWLDisjointClassesAxiom axiom) { + public void visit(@Nonnull OWLDisjointClassesAxiom axiom) { setAxiomWriting(); writeBinaryOrNaryList(DISJOINT_WITH, axiom.getClassExpressions(), DISJOINT_CLASSES); @@ -645,7 +531,7 @@ public void visit(OWLDisjointClassesAxiom axiom) { } @Override - public void visit(OWLDataPropertyDomainAxiom axiom) { + public void visit(@Nonnull OWLDataPropertyDomainAxiom axiom) { setAxiomWriting(); axiom.getProperty().accept(this); write(DOMAIN); @@ -654,7 +540,7 @@ public void visit(OWLDataPropertyDomainAxiom axiom) { } @Override - public void visit(OWLObjectPropertyDomainAxiom axiom) { + public void visit(@Nonnull OWLObjectPropertyDomainAxiom axiom) { setAxiomWriting(); axiom.getProperty().accept(this); write(DOMAIN); @@ -663,7 +549,7 @@ public void visit(OWLObjectPropertyDomainAxiom axiom) { } @Override - public void visit(OWLEquivalentObjectPropertiesAxiom axiom) { + public void visit(@Nonnull OWLEquivalentObjectPropertiesAxiom axiom) { setAxiomWriting(); writeBinaryOrNaryList(EQUIVALENT_TO, axiom.getProperties(), EQUIVALENT_PROPERTIES); @@ -671,7 +557,7 @@ public void visit(OWLEquivalentObjectPropertiesAxiom axiom) { } @Override - public void visit(OWLNegativeDataPropertyAssertionAxiom axiom) { + public void visit(@Nonnull OWLNegativeDataPropertyAssertionAxiom axiom) { setAxiomWriting(); write(NOT); write("("); @@ -685,7 +571,7 @@ public void visit(OWLNegativeDataPropertyAssertionAxiom axiom) { } @Override - public void visit(OWLDifferentIndividualsAxiom axiom) { + public void visit(@Nonnull OWLDifferentIndividualsAxiom axiom) { setAxiomWriting(); writeBinaryOrNaryList(DIFFERENT_FROM, axiom.getIndividuals(), DIFFERENT_INDIVIDUALS); @@ -693,7 +579,7 @@ public void visit(OWLDifferentIndividualsAxiom axiom) { } @Override - public void visit(OWLDisjointDataPropertiesAxiom axiom) { + public void visit(@Nonnull OWLDisjointDataPropertiesAxiom axiom) { setAxiomWriting(); writeBinaryOrNaryList(DISJOINT_WITH, axiom.getProperties(), DISJOINT_PROPERTIES); @@ -701,7 +587,7 @@ public void visit(OWLDisjointDataPropertiesAxiom axiom) { } @Override - public void visit(OWLDisjointObjectPropertiesAxiom axiom) { + public void visit(@Nonnull OWLDisjointObjectPropertiesAxiom axiom) { setAxiomWriting(); writeBinaryOrNaryList(DISJOINT_WITH, axiom.getProperties(), DISJOINT_PROPERTIES); @@ -709,7 +595,7 @@ public void visit(OWLDisjointObjectPropertiesAxiom axiom) { } @Override - public void visit(OWLObjectPropertyRangeAxiom axiom) { + public void visit(@Nonnull OWLObjectPropertyRangeAxiom axiom) { setAxiomWriting(); axiom.getProperty().accept(this); write(RANGE); @@ -718,7 +604,7 @@ public void visit(OWLObjectPropertyRangeAxiom axiom) { } @Override - public void visit(OWLObjectPropertyAssertionAxiom axiom) { + public void visit(@Nonnull OWLObjectPropertyAssertionAxiom axiom) { setAxiomWriting(); axiom.getSubject().accept(this); write(" "); @@ -729,7 +615,7 @@ public void visit(OWLObjectPropertyAssertionAxiom axiom) { } @Override - public void visit(OWLFunctionalObjectPropertyAxiom axiom) { + public void visit(@Nonnull OWLFunctionalObjectPropertyAxiom axiom) { setAxiomWriting(); writeSectionKeyword(FUNCTIONAL); axiom.getProperty().accept(this); @@ -737,7 +623,7 @@ public void visit(OWLFunctionalObjectPropertyAxiom axiom) { } @Override - public void visit(OWLSubObjectPropertyOfAxiom axiom) { + public void visit(@Nonnull OWLSubObjectPropertyOfAxiom axiom) { setAxiomWriting(); axiom.getSubProperty().accept(this); write(SUB_PROPERTY_OF); @@ -746,7 +632,7 @@ public void visit(OWLSubObjectPropertyOfAxiom axiom) { } @Override - public void visit(OWLDisjointUnionAxiom axiom) { + public void visit(@Nonnull OWLDisjointUnionAxiom axiom) { setAxiomWriting(); axiom.getOWLClass().accept(this); write(DISJOINT_UNION_OF); @@ -761,7 +647,7 @@ private void writeFrameType(OWLObject object) { OWLOntology ont = (OWLOntology) object; if (!ont.isAnonymous()) { write("<"); - write(ont.getOntologyID().getOntologyIRI().toString()); + write(ont.getOntologyID().getOntologyIRI().get().toString()); write(">"); } } else { @@ -781,14 +667,14 @@ private void writeFrameType(OWLObject object) { } @Override - public void visit(OWLDeclarationAxiom axiom) { + public void visit(@Nonnull OWLDeclarationAxiom axiom) { setAxiomWriting(); writeFrameType(axiom.getEntity()); restore(); } @Override - public void visit(OWLAnnotationAssertionAxiom axiom) { + public void visit(@Nonnull OWLAnnotationAssertionAxiom axiom) { setAxiomWriting(); axiom.getSubject().accept(this); write(" "); @@ -797,7 +683,7 @@ public void visit(OWLAnnotationAssertionAxiom axiom) { } @Override - public void visit(OWLAnnotationPropertyDomainAxiom axiom) { + public void visit(@Nonnull OWLAnnotationPropertyDomainAxiom axiom) { setAxiomWriting(); axiom.getProperty().accept(this); write(DOMAIN); @@ -805,7 +691,7 @@ public void visit(OWLAnnotationPropertyDomainAxiom axiom) { } @Override - public void visit(OWLAnnotationPropertyRangeAxiom axiom) { + public void visit(@Nonnull OWLAnnotationPropertyRangeAxiom axiom) { setAxiomWriting(); axiom.getProperty().accept(this); write(RANGE); @@ -813,7 +699,7 @@ public void visit(OWLAnnotationPropertyRangeAxiom axiom) { } @Override - public void visit(OWLSubAnnotationPropertyOfAxiom axiom) { + public void visit(@Nonnull OWLSubAnnotationPropertyOfAxiom axiom) { setAxiomWriting(); axiom.getSubProperty().accept(this); write(SUB_PROPERTY_OF); @@ -821,7 +707,7 @@ public void visit(OWLSubAnnotationPropertyOfAxiom axiom) { } @Override - public void visit(OWLSymmetricObjectPropertyAxiom axiom) { + public void visit(@Nonnull OWLSymmetricObjectPropertyAxiom axiom) { setAxiomWriting(); writeSectionKeyword(SYMMETRIC); axiom.getProperty().accept(this); @@ -829,7 +715,7 @@ public void visit(OWLSymmetricObjectPropertyAxiom axiom) { } @Override - public void visit(OWLDataPropertyRangeAxiom axiom) { + public void visit(@Nonnull OWLDataPropertyRangeAxiom axiom) { setAxiomWriting(); axiom.getProperty().accept(this); writeSectionKeyword(RANGE); @@ -838,7 +724,7 @@ public void visit(OWLDataPropertyRangeAxiom axiom) { } @Override - public void visit(OWLFunctionalDataPropertyAxiom axiom) { + public void visit(@Nonnull OWLFunctionalDataPropertyAxiom axiom) { setAxiomWriting(); writeSectionKeyword(FUNCTIONAL); axiom.getProperty().accept(this); @@ -846,7 +732,7 @@ public void visit(OWLFunctionalDataPropertyAxiom axiom) { } @Override - public void visit(OWLEquivalentDataPropertiesAxiom axiom) { + public void visit(@Nonnull OWLEquivalentDataPropertiesAxiom axiom) { setAxiomWriting(); writeFrameKeyword(EQUIVALENT_PROPERTIES); writeCommaSeparatedList(axiom.getProperties()); @@ -854,7 +740,7 @@ public void visit(OWLEquivalentDataPropertiesAxiom axiom) { } @Override - public void visit(OWLClassAssertionAxiom axiom) { + public void visit(@Nonnull OWLClassAssertionAxiom axiom) { setAxiomWriting(); axiom.getIndividual().accept(this); write(TYPE); @@ -863,7 +749,7 @@ public void visit(OWLClassAssertionAxiom axiom) { } @Override - public void visit(OWLEquivalentClassesAxiom axiom) { + public void visit(@Nonnull OWLEquivalentClassesAxiom axiom) { setAxiomWriting(); writeBinaryOrNaryList(EQUIVALENT_TO, axiom.getClassExpressions(), EQUIVALENT_CLASSES); @@ -871,7 +757,7 @@ public void visit(OWLEquivalentClassesAxiom axiom) { } @Override - public void visit(OWLDataPropertyAssertionAxiom axiom) { + public void visit(@Nonnull OWLDataPropertyAssertionAxiom axiom) { setAxiomWriting(); axiom.getSubject().accept(this); write(" "); @@ -882,7 +768,7 @@ public void visit(OWLDataPropertyAssertionAxiom axiom) { } @Override - public void visit(OWLTransitiveObjectPropertyAxiom axiom) { + public void visit(@Nonnull OWLTransitiveObjectPropertyAxiom axiom) { setAxiomWriting(); writeSectionKeyword(TRANSITIVE); axiom.getProperty().accept(this); @@ -890,7 +776,7 @@ public void visit(OWLTransitiveObjectPropertyAxiom axiom) { } @Override - public void visit(OWLIrreflexiveObjectPropertyAxiom axiom) { + public void visit(@Nonnull OWLIrreflexiveObjectPropertyAxiom axiom) { setAxiomWriting(); writeSectionKeyword(IRREFLEXIVE); axiom.getProperty().accept(this); @@ -898,7 +784,7 @@ public void visit(OWLIrreflexiveObjectPropertyAxiom axiom) { } @Override - public void visit(OWLSubDataPropertyOfAxiom axiom) { + public void visit(@Nonnull OWLSubDataPropertyOfAxiom axiom) { setAxiomWriting(); axiom.getSubProperty().accept(this); writeSectionKeyword(SUB_PROPERTY_OF); @@ -907,7 +793,7 @@ public void visit(OWLSubDataPropertyOfAxiom axiom) { } @Override - public void visit(OWLInverseFunctionalObjectPropertyAxiom axiom) { + public void visit(@Nonnull OWLInverseFunctionalObjectPropertyAxiom axiom) { setAxiomWriting(); writeSectionKeyword(INVERSE_FUNCTIONAL); axiom.getProperty().accept(this); @@ -915,14 +801,14 @@ public void visit(OWLInverseFunctionalObjectPropertyAxiom axiom) { } @Override - public void visit(OWLSameIndividualAxiom axiom) { + public void visit(@Nonnull OWLSameIndividualAxiom axiom) { setAxiomWriting(); writeBinaryOrNaryList(SAME_AS, axiom.getIndividuals(), SAME_INDIVIDUAL); restore(); } @Override - public void visit(OWLSubPropertyChainOfAxiom axiom) { + public void visit(@Nonnull OWLSubPropertyChainOfAxiom axiom) { setAxiomWriting(); for (Iterator it = axiom .getPropertyChain().iterator(); it.hasNext();) { @@ -937,7 +823,7 @@ public void visit(OWLSubPropertyChainOfAxiom axiom) { } @Override - public void visit(OWLInverseObjectPropertiesAxiom axiom) { + public void visit(@Nonnull OWLInverseObjectPropertiesAxiom axiom) { setAxiomWriting(); axiom.getFirstProperty().accept(this); write(INVERSE_OF); @@ -946,7 +832,7 @@ public void visit(OWLInverseObjectPropertiesAxiom axiom) { } @Override - public void visit(SWRLRule rule) { + public void visit(@Nonnull SWRLRule rule) { setAxiomWriting(); for (Iterator it = rule.getBody().iterator(); it.hasNext();) { it.next().accept(this); @@ -965,7 +851,7 @@ public void visit(SWRLRule rule) { } @Override - public void visit(OWLHasKeyAxiom axiom) { + public void visit(@Nonnull OWLHasKeyAxiom axiom) { setAxiomWriting(); axiom.getClassExpression().accept(this); write(HAS_KEY); @@ -973,13 +859,9 @@ public void visit(OWLHasKeyAxiom axiom) { write(axiom.getDataPropertyExpressions(), COMMA, false); } - // ///////////////////////////////////////////////////////////////////////////////////////////// - // // SWRL - // - // ///////////////////////////////////////////////////////////////////////////////////////////// @Override - public void visit(SWRLClassAtom node) { + public void visit(@Nonnull SWRLClassAtom node) { if (node.getPredicate().isAnonymous()) { write("("); } @@ -993,7 +875,7 @@ public void visit(SWRLClassAtom node) { } @Override - public void visit(SWRLDataRangeAtom node) { + public void visit(@Nonnull SWRLDataRangeAtom node) { node.getPredicate().accept(this); write("("); node.getArgument().accept(this); @@ -1001,7 +883,7 @@ public void visit(SWRLDataRangeAtom node) { } @Override - public void visit(SWRLObjectPropertyAtom node) { + public void visit(@Nonnull SWRLObjectPropertyAtom node) { node.getPredicate().accept(this); write("("); node.getFirstArgument().accept(this); @@ -1011,7 +893,7 @@ public void visit(SWRLObjectPropertyAtom node) { } @Override - public void visit(SWRLDataPropertyAtom node) { + public void visit(@Nonnull SWRLDataPropertyAtom node) { node.getPredicate().accept(this); write("("); node.getFirstArgument().accept(this); @@ -1021,7 +903,7 @@ public void visit(SWRLDataPropertyAtom node) { } @Override - public void visit(SWRLBuiltInAtom node) { + public void visit(@Nonnull SWRLBuiltInAtom node) { SWRLBuiltInsVocabulary voc = SWRLBuiltInsVocabulary.getBuiltIn(node .getPredicate()); if (voc != null) { @@ -1030,7 +912,7 @@ public void visit(SWRLBuiltInAtom node) { write(node.getPredicate().toQuotedString()); } write("("); - for (Iterator it = node.getArguments().iterator(); it + for (Iterator it = sort(node.getArguments()).iterator(); it .hasNext();) { it.next().accept(this); if (it.hasNext()) { @@ -1041,7 +923,7 @@ public void visit(SWRLBuiltInAtom node) { } @Override - public void visit(SWRLVariable node) { + public void visit(@Nonnull SWRLVariable node) { write("?"); // do not save the namespace if it's the conventional one if ("urn:swrl#".equals(node.getIRI().getNamespace())) { @@ -1052,17 +934,17 @@ public void visit(SWRLVariable node) { } @Override - public void visit(SWRLIndividualArgument node) { + public void visit(@Nonnull SWRLIndividualArgument node) { node.getIndividual().accept(this); } @Override - public void visit(SWRLLiteralArgument node) { + public void visit(@Nonnull SWRLLiteralArgument node) { node.getLiteral().accept(this); } @Override - public void visit(SWRLSameIndividualAtom node) { + public void visit(@Nonnull SWRLSameIndividualAtom node) { write(SAME_AS); write("("); node.getFirstArgument().accept(this); @@ -1072,7 +954,7 @@ public void visit(SWRLSameIndividualAtom node) { } @Override - public void visit(SWRLDifferentIndividualsAtom node) { + public void visit(@Nonnull SWRLDifferentIndividualsAtom node) { write(DIFFERENT_FROM); write("("); node.getFirstArgument().accept(this); @@ -1084,7 +966,7 @@ public void visit(SWRLDifferentIndividualsAtom node) { @Override public void visit(OWLDatatypeDefinitionAxiom axiom) {} - protected void writeAnnotations(Set annos) { + protected void writeAnnotations(@Nonnull Set annos) { if (annos.isEmpty()) { return; } @@ -1092,12 +974,9 @@ protected void writeAnnotations(Set annos) { write(ANNOTATIONS.toString()); write(": "); pushTab(getIndent()); - for (Iterator annoIt = annos.iterator(); annoIt + for (Iterator annoIt = sort(annos).iterator(); annoIt .hasNext();) { OWLAnnotation anno = annoIt.next(); - // if (!anno.getAnnotations().isEmpty()) { - // writeAnnotations(anno.getAnnotations()); - // } anno.accept(this); if (annoIt.hasNext()) { write(", "); @@ -1109,11 +988,7 @@ protected void writeAnnotations(Set annos) { popTab(); } - // ///////////////////////////////////////////////////////////////////////////////////////////// - // // Ontology - // - // ///////////////////////////////////////////////////////////////////////////////////////////// @Override public void visit(OWLOntology ontology) {} } diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/OWL2SPARULConverter.java b/components-core/src/main/java/org/dllearner/utilities/owl/OWL2SPARULConverter.java index d16af21a53..6de29d5281 100644 --- a/components-core/src/main/java/org/dllearner/utilities/owl/OWL2SPARULConverter.java +++ b/components-core/src/main/java/org/dllearner/utilities/owl/OWL2SPARULConverter.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,55 +16,62 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.owl; +import com.google.common.collect.ComparisonChain; +import org.semanticweb.owlapi.io.*; +import org.semanticweb.owlapi.model.*; +import org.semanticweb.owlapi.rdf.model.AbstractTranslator; +import org.semanticweb.owlapi.util.IndividualAppearance; +import org.semanticweb.owlapi.util.OWLAnonymousIndividualsWithMultipleOccurrences; + +import javax.annotation.Nonnull; import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; import java.util.List; -import org.coode.owlapi.rdf.model.AbstractTranslator; -import org.coode.owlapi.rdf.model.RDFLiteralNode; -import org.coode.owlapi.rdf.model.RDFNode; -import org.coode.owlapi.rdf.model.RDFResourceNode; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLLiteral; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyChange; -import org.semanticweb.owlapi.model.OWLOntologyManager; -import org.semanticweb.owlapi.model.RemoveAxiom; - +/** + * A converter of OWL axioms or OWL ontology changes into SPARQL 1.1 Update commands. + * + * @author Lorenz Buehmann + */ public class OWL2SPARULConverter extends - AbstractTranslator { + AbstractTranslator { private StringBuilder sb; public OWL2SPARULConverter(OWLOntologyManager manager, - OWLOntology ontology, boolean useStrongTyping) { - super(manager, ontology, useStrongTyping); + OWLOntology ontology, boolean useStrongTyping, IndividualAppearance individualAppearance) { + super(manager, ontology, useStrongTyping, individualAppearance); } public OWL2SPARULConverter(OWLOntology ontology, boolean useStrongTyping) { - super(ontology.getOWLOntologyManager(), ontology, useStrongTyping); + this(ontology.getOWLOntologyManager(), ontology, useStrongTyping, new OWLAnonymousIndividualsWithMultipleOccurrences()); } - - public String convert(OWLOntology ontology) { - return convert(ontology, true); - } - - public String convert(OWLOntology ontology, boolean add) { - return convert(ontology.getAxioms()); - } - - public String convert(Collection axioms) { - return convert(axioms, true); + + /** + * Converts an OWL axioms to a SPARQL 1.1 Update command. + * + * @param axiom the OWL axiom + * @param add whether the axiom has to be added('ADD') or removed('DELETE') + * @return the SPARQL 1.1 Update command + */ + public String convert(OWLAxiom axiom, boolean add) { + return convert(Collections.singleton(axiom), add); } - + + /** + * Converts a set of OWL axioms to a SPARQL 1.1 Update command. + * + * @param axioms the OWL axioms + * @param add whether those axioms have to be added('ADD') or removed('DELETE') + * @return the SPARQL 1.1 Update command + */ public String convert(Collection axioms, boolean add) { sb = new StringBuilder(); for (OWLAxiom ax : axioms) { - sb.append(add ? "INSERT DATA" - : "DELETE DATA"); + sb.append(add ? "INSERT DATA" : "DELETE DATA"); sb.append("{"); ax.accept(this); sb.append("}"); @@ -73,8 +80,37 @@ public String convert(Collection axioms, boolean add) { return sb.toString(); } - public String translate(List changes) { + /** + * Converts an OWL ontology change to a SPARQL 1.1 Update command. + * + * @param change the OWL ontology change + * @return the SPARQL 1.1 Update command + */ + public String convert(OWLOntologyChange change) { + return convert(Collections.singletonList(change)); + } + + /** + * Converts a list of OWL ontology changes to a SPARQL 1.1 Update command. + * + * @param changes the ontology changes + * @return the SPARQL 1.1 Update command + */ + public String convert(List changes) { sb = new StringBuilder(); + + // sort by type of change + Collections.sort(changes, new Comparator() { + @Override + public int compare(OWLOntologyChange o1, OWLOntologyChange o2) { + return ComparisonChain.start() + .compareTrueFirst(o1.isAddAxiom(), o2.isAddAxiom()) + .compare(o1.getAxiom(), o2.getAxiom()) + .result(); + } + }); + + // convert to SPARQL 1.1 Update for (OWLOntologyChange change : changes) { sb.append(change instanceof RemoveAxiom ? "DELETE DATA" : "INSERT DATA"); @@ -87,35 +123,39 @@ public String translate(List changes) { } @Override - protected void addTriple(RDFResourceNode subject, RDFResourceNode pred, - RDFNode object) { + protected void addTriple(@Nonnull RDFResource subject, @Nonnull RDFResourceIRI pred, + @Nonnull RDFNode object) { sb.append(subject).append(" ").append(pred).append(" ").append(object); } + @Nonnull @Override - protected RDFResourceNode getAnonymousNode(Object key) { - return new RDFResourceNode(System.identityHashCode(key)); + protected RDFResourceBlankNode getAnonymousNode(@Nonnull Object key) { + return new RDFResourceBlankNode(System.identityHashCode(key), false, false); } + @Nonnull @Override - protected RDFResourceNode getPredicateNode(IRI iri) { - return new RDFResourceNode(iri); + protected RDFResource getAnonymousNodeForExpressions(@Nonnull Object o) { + return new RDFResourceBlankNode(false, false); } + @Nonnull @Override - protected RDFResourceNode getResourceNode(IRI iri) { - return new RDFResourceNode(iri); + protected RDFResourceIRI getPredicateNode(@Nonnull IRI iri) { + return new RDFResourceIRI(iri); } + @Nonnull @Override - protected RDFLiteralNode getLiteralNode(OWLLiteral literal) { - if (literal.getDatatype() != null) { - return new RDFLiteralNode(literal.toString(), literal.getDatatype() - .getIRI()); - } else { - return new RDFLiteralNode(literal.toString(), literal.getLang()); - } + protected RDFResource getResourceNode(@Nonnull IRI iri) { + return new RDFResourceIRI(iri); + } + @Nonnull + @Override + protected RDFLiteral getLiteralNode(@Nonnull OWLLiteral literal) { + return new RDFLiteral(literal); } } \ No newline at end of file diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/OWLAPIRenderers.java b/components-core/src/main/java/org/dllearner/utilities/owl/OWLAPIRenderers.java index 8374413e23..6f716adf9b 100644 --- a/components-core/src/main/java/org/dllearner/utilities/owl/OWLAPIRenderers.java +++ b/components-core/src/main/java/org/dllearner/utilities/owl/OWLAPIRenderers.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,29 +16,20 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.owl; +import org.dllearner.utilities.StringFormatter; +import org.semanticweb.owlapi.apibinding.OWLManager; +import org.semanticweb.owlapi.formats.RDFXMLDocumentFormat; +import org.semanticweb.owlapi.formats.TurtleDocumentFormat; +import org.semanticweb.owlapi.model.*; +import org.semanticweb.owlapi.owlxml.renderer.OWLXMLObjectRenderer; +import org.semanticweb.owlapi.owlxml.renderer.OWLXMLWriter; + import java.io.ByteArrayOutputStream; import java.io.StringWriter; import java.io.UnsupportedEncodingException; -import org.coode.owlapi.owlxml.renderer.OWLXMLObjectRenderer; -import org.coode.owlapi.owlxml.renderer.OWLXMLWriter; -import org.coode.owlapi.turtle.TurtleOntologyFormat; -import org.dllearner.utilities.StringFormatter; -import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.io.RDFXMLOntologyFormat; -import org.semanticweb.owlapi.model.AddAxiom; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLObject; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyCreationException; -import org.semanticweb.owlapi.model.OWLOntologyManager; -import org.semanticweb.owlapi.model.OWLOntologyStorageException; - /** * A collection of various render methods provided by * OWL API. @@ -87,35 +78,23 @@ public static String toManchesterOWLSyntax(OWLClassExpression ce) { } /** - * Converts an OWL API OWLClassExpression to an OWL/XML syntax string. + * Converts an OWL API object to an OWL/XML syntax string. * - * @param OWLClassExpression Input OWLDescription. + * @param obj Input OWL object. * @return OWL/XML syntax string. */ - public static String toOWLXMLSyntax(OWLClassExpression description) { + public static String toOWLXMLSyntax(OWLObject obj) { StringWriter sw = new StringWriter(); try { OWLXMLWriter oxw = new OWLXMLWriter(sw, OWLManager.createOWLOntologyManager().createOntology(IRI.create("http://example.com/"))); OWLXMLObjectRenderer renderer = new OWLXMLObjectRenderer(oxw); - description.accept(renderer); + obj.accept(renderer); } catch (OWLOntologyCreationException e) { e.printStackTrace(); } return sw.toString(); } - - public static String toOWLXMLSyntax(OWLAxiom axiom) { - StringWriter sw = new StringWriter(); - try { - OWLXMLWriter oxw = new OWLXMLWriter(sw, OWLManager.createOWLOntologyManager().createOntology(IRI.create("http://example.com/"))); - OWLXMLObjectRenderer renderer = new OWLXMLObjectRenderer(oxw); - axiom.accept(renderer); - } catch (OWLOntologyCreationException e) { - e.printStackTrace(); - } - return sw.toString(); - } - + public static String toRDFXMLSyntax(OWLAxiom axiom) { ByteArrayOutputStream out = new ByteArrayOutputStream(); String str = ""; @@ -123,13 +102,9 @@ public static String toRDFXMLSyntax(OWLAxiom axiom) { OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); OWLOntology ontology = manager.createOntology(IRI.create("http://example.com/")); manager.applyChange(new AddAxiom(ontology, axiom)); - manager.saveOntology(ontology, new RDFXMLOntologyFormat(), out); + manager.saveOntology(ontology, new RDFXMLDocumentFormat(), out); str = new String(out.toByteArray(), "UTF-8"); - } catch (OWLOntologyCreationException e) { - e.printStackTrace(); - } catch (OWLOntologyStorageException e) { - e.printStackTrace(); - } catch (UnsupportedEncodingException e) { + } catch (OWLOntologyCreationException | OWLOntologyStorageException | UnsupportedEncodingException e) { e.printStackTrace(); } return str; @@ -142,13 +117,9 @@ public static String toTurtleSyntax(OWLAxiom axiom, boolean shortVersion) { OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); OWLOntology ontology = manager.createOntology(IRI.create("http://example.com/")); manager.applyChange(new AddAxiom(ontology, axiom)); - manager.saveOntology(ontology, new TurtleOntologyFormat(), out); + manager.saveOntology(ontology, new TurtleDocumentFormat(), out); str = new String(out.toByteArray(), "UTF-8"); - } catch (OWLOntologyCreationException e) { - e.printStackTrace(); - } catch (OWLOntologyStorageException e) { - e.printStackTrace(); - } catch (UnsupportedEncodingException e) { + } catch (OWLOntologyCreationException | UnsupportedEncodingException | OWLOntologyStorageException e) { e.printStackTrace(); } if(shortVersion) { diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/OWLAxiomCBDGenerator.java b/components-core/src/main/java/org/dllearner/utilities/owl/OWLAxiomCBDGenerator.java index 523bf22420..09f554f153 100644 --- a/components-core/src/main/java/org/dllearner/utilities/owl/OWLAxiomCBDGenerator.java +++ b/components-core/src/main/java/org/dllearner/utilities/owl/OWLAxiomCBDGenerator.java @@ -1,90 +1,34 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.utilities.owl; import java.util.HashSet; import java.util.Set; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom; -import org.semanticweb.owlapi.model.OWLAnnotationPropertyDomainAxiom; -import org.semanticweb.owlapi.model.OWLAnnotationPropertyRangeAxiom; -import org.semanticweb.owlapi.model.OWLAnonymousIndividual; -import org.semanticweb.owlapi.model.OWLAsymmetricObjectPropertyAxiom; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLAxiomVisitor; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLClassAssertionAxiom; -import org.semanticweb.owlapi.model.OWLClassAxiom; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLClassExpressionVisitor; -import org.semanticweb.owlapi.model.OWLDataAllValuesFrom; -import org.semanticweb.owlapi.model.OWLDataExactCardinality; -import org.semanticweb.owlapi.model.OWLDataHasValue; -import org.semanticweb.owlapi.model.OWLDataMaxCardinality; -import org.semanticweb.owlapi.model.OWLDataMinCardinality; -import org.semanticweb.owlapi.model.OWLDataProperty; -import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom; -import org.semanticweb.owlapi.model.OWLDataPropertyAxiom; -import org.semanticweb.owlapi.model.OWLDataPropertyDomainAxiom; -import org.semanticweb.owlapi.model.OWLDataPropertyExpression; -import org.semanticweb.owlapi.model.OWLDataPropertyRangeAxiom; -import org.semanticweb.owlapi.model.OWLDataSomeValuesFrom; -import org.semanticweb.owlapi.model.OWLDatatypeDefinitionAxiom; -import org.semanticweb.owlapi.model.OWLDeclarationAxiom; -import org.semanticweb.owlapi.model.OWLDifferentIndividualsAxiom; -import org.semanticweb.owlapi.model.OWLDisjointClassesAxiom; -import org.semanticweb.owlapi.model.OWLDisjointDataPropertiesAxiom; -import org.semanticweb.owlapi.model.OWLDisjointObjectPropertiesAxiom; -import org.semanticweb.owlapi.model.OWLDisjointUnionAxiom; -import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom; -import org.semanticweb.owlapi.model.OWLEquivalentDataPropertiesAxiom; -import org.semanticweb.owlapi.model.OWLEquivalentObjectPropertiesAxiom; -import org.semanticweb.owlapi.model.OWLFunctionalDataPropertyAxiom; -import org.semanticweb.owlapi.model.OWLFunctionalObjectPropertyAxiom; -import org.semanticweb.owlapi.model.OWLHasKeyAxiom; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLIndividualAxiom; -import org.semanticweb.owlapi.model.OWLIndividualVisitor; -import org.semanticweb.owlapi.model.OWLInverseFunctionalObjectPropertyAxiom; -import org.semanticweb.owlapi.model.OWLInverseObjectPropertiesAxiom; -import org.semanticweb.owlapi.model.OWLIrreflexiveObjectPropertyAxiom; -import org.semanticweb.owlapi.model.OWLNamedIndividual; -import org.semanticweb.owlapi.model.OWLNegativeDataPropertyAssertionAxiom; -import org.semanticweb.owlapi.model.OWLNegativeObjectPropertyAssertionAxiom; -import org.semanticweb.owlapi.model.OWLObjectAllValuesFrom; -import org.semanticweb.owlapi.model.OWLObjectComplementOf; -import org.semanticweb.owlapi.model.OWLObjectExactCardinality; -import org.semanticweb.owlapi.model.OWLObjectHasSelf; -import org.semanticweb.owlapi.model.OWLObjectHasValue; -import org.semanticweb.owlapi.model.OWLObjectIntersectionOf; -import org.semanticweb.owlapi.model.OWLObjectInverseOf; -import org.semanticweb.owlapi.model.OWLObjectMaxCardinality; -import org.semanticweb.owlapi.model.OWLObjectMinCardinality; -import org.semanticweb.owlapi.model.OWLObjectOneOf; -import org.semanticweb.owlapi.model.OWLObjectProperty; -import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom; -import org.semanticweb.owlapi.model.OWLObjectPropertyAxiom; -import org.semanticweb.owlapi.model.OWLObjectPropertyDomainAxiom; -import org.semanticweb.owlapi.model.OWLObjectPropertyExpression; -import org.semanticweb.owlapi.model.OWLObjectPropertyRangeAxiom; -import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom; -import org.semanticweb.owlapi.model.OWLObjectUnionOf; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLProperty; -import org.semanticweb.owlapi.model.OWLPropertyExpressionVisitor; -import org.semanticweb.owlapi.model.OWLReflexiveObjectPropertyAxiom; -import org.semanticweb.owlapi.model.OWLSameIndividualAxiom; -import org.semanticweb.owlapi.model.OWLSubAnnotationPropertyOfAxiom; -import org.semanticweb.owlapi.model.OWLSubClassOfAxiom; -import org.semanticweb.owlapi.model.OWLSubDataPropertyOfAxiom; -import org.semanticweb.owlapi.model.OWLSubObjectPropertyOfAxiom; -import org.semanticweb.owlapi.model.OWLSubPropertyChainOfAxiom; -import org.semanticweb.owlapi.model.OWLSymmetricObjectPropertyAxiom; -import org.semanticweb.owlapi.model.OWLTransitiveObjectPropertyAxiom; -import org.semanticweb.owlapi.model.SWRLRule; +import org.semanticweb.owlapi.model.*; +import org.semanticweb.owlapi.model.parameters.Imports; import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl; import uk.ac.manchester.cs.owl.owlapi.OWLNamedIndividualImpl; +import javax.annotation.Nonnull; + /** * Generates Concise Bounded Descriptions on the OWL axiom level. * @author Lorenz Buehmann @@ -124,10 +68,10 @@ public OWLAxiomCBDGenerator(OWLOntology ontology) { public Set getCBD(OWLIndividual ind, int maxDepth){ this.maxDepth = maxDepth; - cbdAxioms = new HashSet(); - visitedClasses = new HashSet(); - visitedProperties = new HashSet(); - visitedIndividuals = new HashSet(); + cbdAxioms = new HashSet<>(); + visitedClasses = new HashSet<>(); + visitedProperties = new HashSet<>(); + visitedIndividuals = new HashSet<>(); // we start with the directly related axioms currentDepth = 0; @@ -175,7 +119,7 @@ public void visit(OWLClass cls) { visitedClasses.add(cls); currentDepth++; - Set axioms = new HashSet();//.getAxioms(cls); + Set axioms = new HashSet<>();//.getAxioms(cls); if(subsumptionDown){ axioms.addAll(ontology.getSubClassAxiomsForSuperClass(cls)); } else { @@ -211,7 +155,7 @@ public void visit(OWLNamedIndividual individual) { visitedIndividuals.add(individual); currentDepth++; - Set axioms = ontology.getAxioms(individual); + Set axioms = ontology.getAxioms(individual, Imports.INCLUDED); for (OWLIndividualAxiom ax : axioms) { ax.accept(this); } @@ -235,7 +179,7 @@ public void visit(OWLObjectProperty property) { if(!visitedProperties.contains(property)){ visitedProperties.add(property); - Set axioms = ontology.getAxioms(property); + Set axioms = ontology.getAxioms(property, Imports.INCLUDED); for (OWLObjectPropertyAxiom ax : axioms) { ax.accept(this); } @@ -257,13 +201,18 @@ public void visit(OWLDataProperty property) { if(!visitedProperties.contains(property)){ visitedProperties.add(property); - Set axioms = ontology.getAxioms(property); + Set axioms = ontology.getAxioms(property, Imports.INCLUDED); for (OWLDataPropertyAxiom ax : axioms) { ax.accept(this); } } } - + + @Override + public void visit(@Nonnull OWLAnnotationProperty property) { + + } + /* (non-Javadoc) * @see org.semanticweb.owlapi.model.OWLAxiomVisitor#visit(org.semanticweb.owlapi.model.OWLClassAssertionAxiom) */ diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/OWLAxiomToSPARQLConverter.java b/components-core/src/main/java/org/dllearner/utilities/owl/OWLAxiomToSPARQLConverter.java index 5fd8b3b0b1..2d5a9336e5 100644 --- a/components-core/src/main/java/org/dllearner/utilities/owl/OWLAxiomToSPARQLConverter.java +++ b/components-core/src/main/java/org/dllearner/utilities/owl/OWLAxiomToSPARQLConverter.java @@ -1,8 +1,27 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.utilities.owl; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.io.ToStringRenderer; +import org.dllearner.core.StringRenderer; +import org.dllearner.core.StringRenderer.Rendering; import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom; import org.semanticweb.owlapi.model.OWLAnnotationPropertyDomainAxiom; import org.semanticweb.owlapi.model.OWLAnnotationPropertyRangeAxiom; @@ -56,8 +75,6 @@ import org.semanticweb.owlapi.model.SWRLRule; import org.semanticweb.owlapi.util.DefaultPrefixManager; -import uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntaxObjectRenderer; - import com.hp.hpl.jena.query.Query; import com.hp.hpl.jena.query.QueryFactory; import com.hp.hpl.jena.query.Syntax; @@ -248,12 +265,13 @@ public void visit(SWRLRule rule) { } public static void main(String[] args) throws Exception { - ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); + StringRenderer.setRenderer(Rendering.DL_SYNTAX); OWLAxiomToSPARQLConverter converter = new OWLAxiomToSPARQLConverter(); OWLOntologyManager man = OWLManager.createOWLOntologyManager(); OWLDataFactory df = man.getOWLDataFactory(); - PrefixManager pm = new DefaultPrefixManager("http://dbpedia.org/ontology/"); + PrefixManager pm = new DefaultPrefixManager(); + pm.setDefaultPrefix("http://dbpedia.org/ontology/"); OWLClass clsA = df.getOWLClass("A", pm); OWLClass clsB = df.getOWLClass("B", pm); diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionChildrenCollector.java b/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionChildrenCollector.java index 8b1bd5dcbc..d979ddd946 100644 --- a/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionChildrenCollector.java +++ b/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionChildrenCollector.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.utilities.owl; import java.util.Collections; @@ -61,7 +79,7 @@ public Set visit(OWLObjectUnionOf ce) { */ @Override public Set visit(OWLObjectComplementOf ce) { - return Collections. singleton(ce.getOperand()); + return Collections.singleton(ce.getOperand()); } /* (non-Javadoc) @@ -69,7 +87,7 @@ public Set visit(OWLObjectComplementOf ce) { */ @Override public Set visit(OWLObjectSomeValuesFrom ce) { - return Collections. singleton(ce.getFiller()); + return Collections.singleton(ce.getFiller()); } /* (non-Javadoc) @@ -77,7 +95,7 @@ public Set visit(OWLObjectSomeValuesFrom ce) { */ @Override public Set visit(OWLObjectAllValuesFrom ce) { - return Collections. singleton(ce.getFiller()); + return Collections.singleton(ce.getFiller()); } /* (non-Javadoc) @@ -93,7 +111,7 @@ public Set visit(OWLObjectHasValue ce) { */ @Override public Set visit(OWLObjectMinCardinality ce) { - return Collections. singleton(ce.getFiller()); + return Collections.singleton(ce.getFiller()); } /* (non-Javadoc) @@ -101,7 +119,7 @@ public Set visit(OWLObjectMinCardinality ce) { */ @Override public Set visit(OWLObjectExactCardinality ce) { - return Collections. singleton(ce.getFiller()); + return Collections.singleton(ce.getFiller()); } /* (non-Javadoc) @@ -109,7 +127,7 @@ public Set visit(OWLObjectExactCardinality ce) { */ @Override public Set visit(OWLObjectMaxCardinality ce) { - return Collections. singleton(ce.getFiller()); + return Collections.singleton(ce.getFiller()); } /* (non-Javadoc) diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionCleaner.java b/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionCleaner.java index 3272d46734..f569ac80b3 100644 --- a/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionCleaner.java +++ b/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionCleaner.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.utilities.owl; @@ -57,7 +72,7 @@ public OWLClassExpression visit(OWLClass ce) { */ @Override public OWLClassExpression visit(OWLObjectIntersectionOf ce) { - List operands = new ArrayList(); + List operands = new ArrayList<>(); for (OWLClassExpression operand : ce.getOperands()) { OWLClassExpression newOperand = operand.accept(this); if(newOperand instanceof OWLObjectIntersectionOf){ @@ -75,7 +90,7 @@ public OWLClassExpression visit(OWLObjectIntersectionOf ce) { */ @Override public OWLClassExpression visit(OWLObjectUnionOf ce) { - List operands = new ArrayList(); + List operands = new ArrayList<>(); for (OWLClassExpression operand : ce.getOperands()) { OWLClassExpression newOperand = operand.accept(this); if(newOperand instanceof OWLObjectUnionOf){ diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionLengthCalculator.java b/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionLengthCalculator.java new file mode 100644 index 0000000000..f1f0891623 --- /dev/null +++ b/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionLengthCalculator.java @@ -0,0 +1,424 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.utilities.owl; + +import org.semanticweb.owlapi.model.*; + +import javax.annotation.Nonnull; +import java.util.List; +import java.util.Set; + +/** + * Computes the length of a class expression. + * + * @author Lorenz Buehmann + * + */ +public class OWLClassExpressionLengthCalculator implements + OWLClassExpressionVisitor, OWLPropertyExpressionVisitor, + OWLDataRangeVisitor { + + private OWLClassExpressionLengthMetric metric; + private int length; + + public OWLClassExpressionLengthCalculator(OWLClassExpressionLengthMetric metric) { + this.metric = metric; + } + + public OWLClassExpressionLengthCalculator() { + this.metric = OWLClassExpressionLengthMetric.getDefaultMetric(); + } + + /** + * Computes the length of a class expression. + * + * @param ce the class expression + * @return the length of the class expression + */ + public int getLength(OWLClassExpression ce) { + ce.accept(this); + return length; + } + + /* + * (non-Javadoc) + * + * @see + * org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb + * .owlapi.model.OWLClass) + */ + @Override + public void visit(OWLClass ce) { + length += metric.classLength; + } + + /* + * (non-Javadoc) + * + * @see + * org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb + * .owlapi.model.OWLObjectIntersectionOf) + */ + @Override + public void visit(OWLObjectIntersectionOf ce) { + List operands = ce.getOperandsAsList(); + for (OWLClassExpression op : operands) { + op.accept(this); + } + length += (operands.size() - 1) * metric.objectIntersectionLength; + } + + /* + * (non-Javadoc) + * + * @see + * org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb + * .owlapi.model.OWLObjectUnionOf) + */ + @Override + public void visit(OWLObjectUnionOf ce) { + List operands = ce.getOperandsAsList(); + for (OWLClassExpression op : operands) { + op.accept(this); + } + length += (operands.size() - 1) * metric.objectUnionLength; + } + + /* + * (non-Javadoc) + * + * @see + * org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb + * .owlapi.model.OWLObjectComplementOf) + */ + @Override + public void visit(OWLObjectComplementOf ce) { + ce.getOperand().accept(this); + length += metric.objectComplementLength; + } + + /* + * (non-Javadoc) + * + * @see + * org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb + * .owlapi.model.OWLObjectSomeValuesFrom) + */ + @Override + public void visit(OWLObjectSomeValuesFrom ce) { + ce.getProperty().accept(this); + ce.getFiller().accept(this); + length += metric.objectSomeValuesLength; + } + + /* + * (non-Javadoc) + * + * @see + * org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb + * .owlapi.model.OWLObjectAllValuesFrom) + */ + @Override + public void visit(OWLObjectAllValuesFrom ce) { + ce.getProperty().accept(this); + ce.getFiller().accept(this); + length += metric.objectAllValuesLength; + } + + /* + * (non-Javadoc) + * + * @see + * org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb + * .owlapi.model.OWLObjectHasValue) + */ + @Override + public void visit(OWLObjectHasValue ce) { + ce.getProperty().accept(this); + length += metric.objectHasValueLength; + } + + /* + * (non-Javadoc) + * + * @see + * org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb + * .owlapi.model.OWLObjectMinCardinality) + */ + @Override + public void visit(OWLObjectMinCardinality ce) { + ce.getProperty().accept(this); + ce.getFiller().accept(this); + length += metric.objectCardinalityLength; + } + + /* + * (non-Javadoc) + * + * @see + * org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb + * .owlapi.model.OWLObjectExactCardinality) + */ + @Override + public void visit(OWLObjectExactCardinality ce) { + ce.getProperty().accept(this); + ce.getFiller().accept(this); + length += metric.objectCardinalityLength; + } + + /* + * (non-Javadoc) + * + * @see + * org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb + * .owlapi.model.OWLObjectMaxCardinality) + */ + @Override + public void visit(OWLObjectMaxCardinality ce) { + ce.getProperty().accept(this); + ce.getFiller().accept(this); + length += metric.objectCardinalityLength; + } + + /* + * (non-Javadoc) + * + * @see + * org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb + * .owlapi.model.OWLObjectHasSelf) + */ + @Override + public void visit(OWLObjectHasSelf ce) { + ce.getProperty().accept(this); + length += metric.objectHasValueLength; + } + + /* + * (non-Javadoc) + * + * @see + * org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb + * .owlapi.model.OWLObjectOneOf) + */ + @Override + public void visit(OWLObjectOneOf ce) { + length += metric.objectOneOfLength; + } + + /* + * (non-Javadoc) + * + * @see + * org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb + * .owlapi.model.OWLDataSomeValuesFrom) + */ + @Override + public void visit(OWLDataSomeValuesFrom ce) { + ce.getProperty().accept(this); + ce.getFiller().accept(this); + length += metric.dataSomeValuesLength; + } + + /* + * (non-Javadoc) + * + * @see + * org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb + * .owlapi.model.OWLDataAllValuesFrom) + */ + @Override + public void visit(OWLDataAllValuesFrom ce) { + ce.getProperty().accept(this); + ce.getFiller().accept(this); + length += metric.dataAllValuesLength; + } + + /* + * (non-Javadoc) + * + * @see + * org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb + * .owlapi.model.OWLDataHasValue) + */ + @Override + public void visit(OWLDataHasValue ce) { + ce.getProperty().accept(this); + length += metric.dataHasValueLength; + } + + /* + * (non-Javadoc) + * + * @see + * org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb + * .owlapi.model.OWLDataMinCardinality) + */ + @Override + public void visit(OWLDataMinCardinality ce) { + ce.getProperty().accept(this); + ce.getFiller().accept(this); + length += metric.dataCardinalityLength; + } + + /* + * (non-Javadoc) + * + * @see + * org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb + * .owlapi.model.OWLDataExactCardinality) + */ + @Override + public void visit(OWLDataExactCardinality ce) { + ce.getProperty().accept(this); + ce.getFiller().accept(this); + length += metric.dataCardinalityLength; + } + + /* + * (non-Javadoc) + * + * @see + * org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb + * .owlapi.model.OWLDataMaxCardinality) + */ + @Override + public void visit(OWLDataMaxCardinality ce) { + ce.getProperty().accept(this); + ce.getFiller().accept(this); + length += metric.dataCardinalityLength; + } + + /* + * (non-Javadoc) + * + * @see org.semanticweb.owlapi.model.OWLPropertyExpressionVisitor#visit(org. + * semanticweb.owlapi.model.OWLObjectProperty) + */ + @Override + public void visit(OWLObjectProperty property) { + length += metric.objectProperyLength; + } + + /* + * (non-Javadoc) + * + * @see org.semanticweb.owlapi.model.OWLPropertyExpressionVisitor#visit(org. + * semanticweb.owlapi.model.OWLObjectInverseOf) + */ + @Override + public void visit(OWLObjectInverseOf property) { + length += metric.objectInverseLength; + } + + /* + * (non-Javadoc) + * + * @see org.semanticweb.owlapi.model.OWLPropertyExpressionVisitor#visit(org. + * semanticweb.owlapi.model.OWLDataProperty) + */ + @Override + public void visit(OWLDataProperty property) { + length += metric.dataProperyLength; + } + + @Override + public void visit(@Nonnull OWLAnnotationProperty property) {} + + /* + * (non-Javadoc) + * + * @see + * org.semanticweb.owlapi.model.OWLDataRangeVisitor#visit(org.semanticweb + * .owlapi.model.OWLDatatype) + */ + @Override + public void visit(OWLDatatype node) { + length += metric.datatypeLength; + } + + /* + * (non-Javadoc) + * + * @see + * org.semanticweb.owlapi.model.OWLDataRangeVisitor#visit(org.semanticweb + * .owlapi.model.OWLDataOneOf) + */ + @Override + public void visit(OWLDataOneOf node) { + length += metric.dataOneOfLength; + } + + /* + * (non-Javadoc) + * + * @see + * org.semanticweb.owlapi.model.OWLDataRangeVisitor#visit(org.semanticweb + * .owlapi.model.OWLDataComplementOf) + */ + @Override + public void visit(OWLDataComplementOf node) { + length += metric.dataComplementLength; + } + + /* + * (non-Javadoc) + * + * @see + * org.semanticweb.owlapi.model.OWLDataRangeVisitor#visit(org.semanticweb + * .owlapi.model.OWLDataIntersectionOf) + */ + @Override + public void visit(OWLDataIntersectionOf node) { + Set operands = node.getOperands(); + for (OWLDataRange op : operands) { + op.accept(this); + } + length += (operands.size() - 1) * metric.dataIntersectionLength; + } + + /* + * (non-Javadoc) + * + * @see + * org.semanticweb.owlapi.model.OWLDataRangeVisitor#visit(org.semanticweb + * .owlapi.model.OWLDataUnionOf) + */ + @Override + public void visit(OWLDataUnionOf node) { + Set operands = node.getOperands(); + for (OWLDataRange op : operands) { + op.accept(this); + } + length += (operands.size() - 1) * metric.dataUnionLength; + } + + /* + * (non-Javadoc) + * + * @see + * org.semanticweb.owlapi.model.OWLDataRangeVisitor#visit(org.semanticweb + * .owlapi.model.OWLDatatypeRestriction) + */ + @Override + public void visit(OWLDatatypeRestriction node) { + Set facetRestrictions = node + .getFacetRestrictions(); + length += facetRestrictions.size(); + } +} diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionLengthMetric.java b/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionLengthMetric.java new file mode 100644 index 0000000000..1bbca940bd --- /dev/null +++ b/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionLengthMetric.java @@ -0,0 +1,218 @@ +package org.dllearner.utilities.owl; + +import org.dllearner.core.AbstractComponent; +import org.dllearner.core.ComponentAnn; + +/** + * Created by Simon Bin on 16-1-28. + */ +@ComponentAnn(name = "OWL Class Expression Length Metric", shortName = "cel_metric", version = 0.1) +public class OWLClassExpressionLengthMetric extends AbstractComponent { + public int classLength = 1; + public int objectIntersectionLength = 1; + public int objectUnionLength = 1; + public int objectComplementLength = 1; + public int objectSomeValuesLength = 1; + public int objectAllValuesLength = 1; + public int objectHasValueLength = 2; + public int objectCardinalityLength = 2; + public int objectOneOfLength = 1; + public int dataSomeValuesLength = 1; + public int dataAllValuesLength = 1; + public int dataHasValueLength = 2; + public int dataCardinalityLength = 2; + public int objectProperyLength = 1; + public int objectInverseLength = 2; + public int dataProperyLength = 1; + public int datatypeLength = 1; + public int dataOneOfLength = 1; + public int dataComplementLength = 1; + public int dataIntersectionLength = 1; + public int dataUnionLength = 1; + + public static OWLClassExpressionLengthMetric getDefaultMetric() { + OWLClassExpressionLengthMetric metric = new OWLClassExpressionLengthMetric(); + return metric; + } + + public static OWLClassExpressionLengthMetric getOCELMetric() { + OWLClassExpressionLengthMetric metric = new OWLClassExpressionLengthMetric(); + metric.dataHasValueLength = 1; + metric.objectHasValueLength = 1; + metric.dataSomeValuesLength = 0; + metric.objectSomeValuesLength = 0; + return metric; + } + + public int getClassLength() { + return classLength; + } + + public void setClassLength(int classLength) { + this.classLength = classLength; + } + + public int getObjectIntersectionLength() { + return objectIntersectionLength; + } + + public void setObjectIntersectionLength(int objectIntersectionLength) { + this.objectIntersectionLength = objectIntersectionLength; + } + + public int getObjectUnionLength() { + return objectUnionLength; + } + + public void setObjectUnionLength(int objectUnionLength) { + this.objectUnionLength = objectUnionLength; + } + + public int getObjectComplementLength() { + return objectComplementLength; + } + + public void setObjectComplementLength(int objectComplementLength) { + this.objectComplementLength = objectComplementLength; + } + + public int getObjectSomeValuesLength() { + return objectSomeValuesLength; + } + + public void setObjectSomeValuesLength(int objectSomeValuesLength) { + this.objectSomeValuesLength = objectSomeValuesLength; + } + + public int getObjectAllValuesLength() { + return objectAllValuesLength; + } + + public void setObjectAllValuesLength(int objectAllValuesLength) { + this.objectAllValuesLength = objectAllValuesLength; + } + + public int getObjectHasValueLength() { + return objectHasValueLength; + } + + public void setObjectHasValueLength(int objectHasValueLength) { + this.objectHasValueLength = objectHasValueLength; + } + + public int getObjectCardinalityLength() { + return objectCardinalityLength; + } + + public void setObjectCardinalityLength(int objectCardinalityLength) { + this.objectCardinalityLength = objectCardinalityLength; + } + + public int getObjectOneOfLength() { + return objectOneOfLength; + } + + public void setObjectOneOfLength(int objectOneOfLength) { + this.objectOneOfLength = objectOneOfLength; + } + + public int getDataSomeValuesLength() { + return dataSomeValuesLength; + } + + public void setDataSomeValuesLength(int dataSomeValuesLength) { + this.dataSomeValuesLength = dataSomeValuesLength; + } + + public int getDataAllValuesLength() { + return dataAllValuesLength; + } + + public void setDataAllValuesLength(int dataAllValuesLength) { + this.dataAllValuesLength = dataAllValuesLength; + } + + public int getDataHasValueLength() { + return dataHasValueLength; + } + + public void setDataHasValueLength(int dataHasValueLength) { + this.dataHasValueLength = dataHasValueLength; + } + + public int getDataCardinalityLength() { + return dataCardinalityLength; + } + + public void setDataCardinalityLength(int dataCardinalityLength) { + this.dataCardinalityLength = dataCardinalityLength; + } + + public int getObjectProperyLength() { + return objectProperyLength; + } + + public void setObjectProperyLength(int objectProperyLength) { + this.objectProperyLength = objectProperyLength; + } + + public int getObjectInverseLength() { + return objectInverseLength; + } + + public void setObjectInverseLength(int objectInverseLength) { + this.objectInverseLength = objectInverseLength; + } + + public int getDataProperyLength() { + return dataProperyLength; + } + + public void setDataProperyLength(int dataProperyLength) { + this.dataProperyLength = dataProperyLength; + } + + public int getDatatypeLength() { + return datatypeLength; + } + + public void setDatatypeLength(int datatypeLength) { + this.datatypeLength = datatypeLength; + } + + public int getDataOneOfLength() { + return dataOneOfLength; + } + + public void setDataOneOfLength(int dataOneOfLength) { + this.dataOneOfLength = dataOneOfLength; + } + + public int getDataComplementLength() { + return dataComplementLength; + } + + public void setDataComplementLength(int dataComplementLength) { + this.dataComplementLength = dataComplementLength; + } + + public int getDataIntersectionLength() { + return dataIntersectionLength; + } + + public void setDataIntersectionLength(int dataIntersectionLength) { + this.dataIntersectionLength = dataIntersectionLength; + } + + public int getDataUnionLength() { + return dataUnionLength; + } + + public void setDataUnionLength(int dataUnionLength) { + this.dataUnionLength = dataUnionLength; + } + + @Override + public void init() {} + +} diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionMinimizer.java b/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionMinimizer.java index 87be6deab3..25af4cad93 100644 --- a/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionMinimizer.java +++ b/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionMinimizer.java @@ -1,49 +1,31 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.utilities.owl; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.TreeMap; -import java.util.TreeSet; - +import com.google.common.collect.HashMultimap; +import com.google.common.collect.Multimap; import org.dllearner.core.AbstractReasonerComponent; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx; -import org.semanticweb.owlapi.model.OWLDataAllValuesFrom; -import org.semanticweb.owlapi.model.OWLDataExactCardinality; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLDataHasValue; -import org.semanticweb.owlapi.model.OWLDataMaxCardinality; -import org.semanticweb.owlapi.model.OWLDataMinCardinality; -import org.semanticweb.owlapi.model.OWLDataPropertyExpression; -import org.semanticweb.owlapi.model.OWLDataRange; -import org.semanticweb.owlapi.model.OWLDataSomeValuesFrom; -import org.semanticweb.owlapi.model.OWLDatatype; -import org.semanticweb.owlapi.model.OWLDatatypeRestriction; -import org.semanticweb.owlapi.model.OWLFacetRestriction; -import org.semanticweb.owlapi.model.OWLObjectAllValuesFrom; -import org.semanticweb.owlapi.model.OWLObjectComplementOf; -import org.semanticweb.owlapi.model.OWLObjectExactCardinality; -import org.semanticweb.owlapi.model.OWLObjectHasSelf; -import org.semanticweb.owlapi.model.OWLObjectHasValue; -import org.semanticweb.owlapi.model.OWLObjectIntersectionOf; -import org.semanticweb.owlapi.model.OWLObjectMaxCardinality; -import org.semanticweb.owlapi.model.OWLObjectMinCardinality; -import org.semanticweb.owlapi.model.OWLObjectOneOf; -import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom; -import org.semanticweb.owlapi.model.OWLObjectUnionOf; +import org.semanticweb.owlapi.model.*; import org.semanticweb.owlapi.util.OWLObjectDuplicator; -import com.google.common.collect.HashMultimap; -import com.google.common.collect.Multimap; +import java.util.*; +import java.util.Map.Entry; /** * @author Lorenz Buehmann @@ -57,7 +39,7 @@ public class OWLClassExpressionMinimizer implements OWLClassExpressionVisitorEx< private boolean beautify = true; - private Map> cachedSubclassOf = new TreeMap>(); + private Map> cachedSubclassOf = new TreeMap<>(); public OWLClassExpressionMinimizer(OWLDataFactory dataFactory, AbstractReasonerComponent reasoner) { this.df = dataFactory; @@ -71,6 +53,7 @@ public OWLClassExpression minimize(OWLClassExpression ce){ } public OWLClassExpression minimizeClone(OWLClassExpression ce){ + OWLObjectDuplicator objectDuplicator = new OWLObjectDuplicator(df); OWLClassExpression clone = objectDuplicator.duplicateObject(ce); return clone.accept(this); } @@ -94,16 +77,17 @@ public OWLClassExpression visit(OWLObjectIntersectionOf ce) { operands.set(i, operands.get(i).accept(this)); } + List oldOperands = new ArrayList<>(new TreeSet<>(operands)); List newOperands = new ArrayList<>(operands); if(newOperands.size() == 1){ return newOperands.iterator().next().accept(this); } - for (int i = 0; i < operands.size(); i++) { - OWLClassExpression op1 = operands.get(i); - for (int j = i + 1; j < operands.size(); j++) { - OWLClassExpression op2 = operands.get(j); + for (int i = 0; i < oldOperands.size(); i++) { + OWLClassExpression op1 = oldOperands.get(i); + for (int j = i + 1; j < oldOperands.size(); j++) { + OWLClassExpression op2 = oldOperands.get(j); //remove operand if it is a super class if(isSubClassOf(op1, op2)){ @@ -126,7 +110,7 @@ public OWLClassExpression visit(OWLObjectIntersectionOf ce) { Collection datapropertyRestrictions = entry.getValue(); if(datapropertyRestrictions.size() > 1) { - Set facetRestrictions = new TreeSet(); + Set facetRestrictions = new TreeSet<>(); for (OWLDataSomeValuesFrom restriction : datapropertyRestrictions) { OWLDataRange dataRange = restriction.getFiller(); if(dataRange instanceof OWLDatatypeRestriction) { @@ -150,11 +134,6 @@ public OWLClassExpression visit(OWLObjectIntersectionOf ce) { return df.getOWLObjectIntersectionOf(new HashSet<>(newOperands)); } - /** - * @param op1 - * @param op2 - * @return - */ private boolean isSubClassOf(OWLClassExpression subClass, OWLClassExpression superClass) { return superClass.isOWLThing() || reasoner.isSuperClassOf(superClass, subClass); } @@ -169,16 +148,17 @@ public OWLClassExpression visit(OWLObjectUnionOf ce) { for (int i = 0; i < operands.size(); i++) { operands.set(i, operands.get(i).accept(this)); } - List newOperands = new ArrayList(operands); + List oldOperands = new ArrayList<>(new TreeSet<>(operands)); + List newOperands = new ArrayList<>(operands); if(newOperands.size() == 1){ return newOperands.iterator().next().accept(this); } - for (int i = 0; i < operands.size(); i++) { - OWLClassExpression op1 = operands.get(i); - for (int j = i + 1; j < operands.size(); j++) { - OWLClassExpression op2 = operands.get(j); + for (int i = 0; i < oldOperands.size(); i++) { + OWLClassExpression op1 = oldOperands.get(i); + for (int j = i + 1; j < oldOperands.size(); j++) { + OWLClassExpression op2 = oldOperands.get(j); //remove operand if it is a subclass if(isSubClassOf(op2, op1)){ diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionToSPARQLConverter.java b/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionToSPARQLConverter.java index a6fc1f9a15..7b43657fcd 100644 --- a/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionToSPARQLConverter.java +++ b/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionToSPARQLConverter.java @@ -1,69 +1,23 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.utilities.owl; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.Stack; -import java.util.TreeSet; - -import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.io.ToStringRenderer; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLClassExpressionVisitor; -import org.semanticweb.owlapi.model.OWLDataAllValuesFrom; -import org.semanticweb.owlapi.model.OWLDataCardinalityRestriction; -import org.semanticweb.owlapi.model.OWLDataComplementOf; -import org.semanticweb.owlapi.model.OWLDataExactCardinality; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLDataHasValue; -import org.semanticweb.owlapi.model.OWLDataIntersectionOf; -import org.semanticweb.owlapi.model.OWLDataMaxCardinality; -import org.semanticweb.owlapi.model.OWLDataMinCardinality; -import org.semanticweb.owlapi.model.OWLDataOneOf; -import org.semanticweb.owlapi.model.OWLDataProperty; -import org.semanticweb.owlapi.model.OWLDataPropertyExpression; -import org.semanticweb.owlapi.model.OWLDataRange; -import org.semanticweb.owlapi.model.OWLDataRangeVisitor; -import org.semanticweb.owlapi.model.OWLDataSomeValuesFrom; -import org.semanticweb.owlapi.model.OWLDataUnionOf; -import org.semanticweb.owlapi.model.OWLDatatype; -import org.semanticweb.owlapi.model.OWLDatatypeRestriction; -import org.semanticweb.owlapi.model.OWLEntity; -import org.semanticweb.owlapi.model.OWLFacetRestriction; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLLiteral; -import org.semanticweb.owlapi.model.OWLNamedIndividual; -import org.semanticweb.owlapi.model.OWLObjectAllValuesFrom; -import org.semanticweb.owlapi.model.OWLObjectCardinalityRestriction; -import org.semanticweb.owlapi.model.OWLObjectComplementOf; -import org.semanticweb.owlapi.model.OWLObjectExactCardinality; -import org.semanticweb.owlapi.model.OWLObjectHasSelf; -import org.semanticweb.owlapi.model.OWLObjectHasValue; -import org.semanticweb.owlapi.model.OWLObjectIntersectionOf; -import org.semanticweb.owlapi.model.OWLObjectInverseOf; -import org.semanticweb.owlapi.model.OWLObjectMaxCardinality; -import org.semanticweb.owlapi.model.OWLObjectMinCardinality; -import org.semanticweb.owlapi.model.OWLObjectOneOf; -import org.semanticweb.owlapi.model.OWLObjectProperty; -import org.semanticweb.owlapi.model.OWLObjectPropertyExpression; -import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom; -import org.semanticweb.owlapi.model.OWLObjectUnionOf; -import org.semanticweb.owlapi.model.OWLOntologyManager; -import org.semanticweb.owlapi.model.OWLPropertyExpression; -import org.semanticweb.owlapi.model.OWLPropertyExpressionVisitor; -import org.semanticweb.owlapi.model.PrefixManager; -import org.semanticweb.owlapi.util.DefaultPrefixManager; -import org.semanticweb.owlapi.vocab.OWLFacet; - -import uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntaxObjectRenderer; - import com.google.common.base.Joiner; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; @@ -71,16 +25,25 @@ import com.hp.hpl.jena.query.Query; import com.hp.hpl.jena.query.QueryFactory; import com.hp.hpl.jena.query.Syntax; +import org.dllearner.core.StringRenderer; +import org.dllearner.core.StringRenderer.Rendering; +import org.semanticweb.owlapi.apibinding.OWLManager; +import org.semanticweb.owlapi.model.*; +import org.semanticweb.owlapi.util.DefaultPrefixManager; +import org.semanticweb.owlapi.vocab.OWLFacet; + +import javax.annotation.Nonnull; +import java.util.*; public class OWLClassExpressionToSPARQLConverter implements OWLClassExpressionVisitor, OWLPropertyExpressionVisitor, OWLDataRangeVisitor{ private String sparql = ""; - private Stack variables = new Stack(); + private Stack variables = new Stack<>(); private Multimap properties = HashMultimap.create(); private Map intersection; - private Set variableEntities = new HashSet(); + private Set variableEntities = new HashSet<>(); private VariablesMapping mapping; private boolean ignoreGenericTypeStatements = true; @@ -90,11 +53,11 @@ public class OWLClassExpressionToSPARQLConverter implements OWLClassExpressionVi private String outerGroupBy; - private Map> groupingVars = new HashMap>(); - private Map> havingConditions = new HashMap>(); + private Map> groupingVars = new HashMap<>(); + private Map> havingConditions = new HashMap<>(); - Stack parentVar = new Stack(); - Stack parent = new Stack(); + Stack parentVar = new Stack<>(); + Stack parent = new Stack<>(); public OWLClassExpressionToSPARQLConverter(VariablesMapping mapping) { this.mapping = mapping; @@ -208,7 +171,7 @@ private void reset(){ variables.clear(); properties.clear(); sparql = ""; - intersection = new HashMap(); + intersection = new HashMap<>(); mapping.reset(); cnt = 1; } @@ -293,16 +256,16 @@ private String newCountVar() { } @Override - public void visit(OWLObjectProperty property) { - } + public void visit(OWLObjectProperty property) {} @Override - public void visit(OWLObjectInverseOf property) { - } + public void visit(OWLObjectInverseOf property) {} @Override - public void visit(OWLDataProperty property) { - } + public void visit(OWLDataProperty property) {} + + @Override + public void visit(@Nonnull OWLAnnotationProperty property) {} @Override public void visit(OWLClass ce) { @@ -320,14 +283,14 @@ public void visit(OWLObjectIntersectionOf ce) { } Collection props = properties.get(modalDepth()); if(props.size() > 1){ - Collection vars = new TreeSet(); + Collection vars = new TreeSet<>(); for (OWLEntity p : props) { if(mapping.containsKey(p)){ vars.add(mapping.get(p)); } } if(vars.size() == 2){ - List varList = new ArrayList(vars); + List varList = new ArrayList<>(vars); sparql += "FILTER(" + varList.get(0) + "!=" + varList.get(1) + ")"; } } @@ -420,7 +383,7 @@ public void visit(OWLObjectAllValuesFrom ce) { @Override public void visit(OWLObjectHasValue ce) { OWLObjectPropertyExpression propertyExpression = ce.getProperty(); - OWLNamedIndividual value = ce.getValue().asOWLNamedIndividual(); + OWLNamedIndividual value = ce.getFiller().asOWLNamedIndividual(); if(propertyExpression.isAnonymous()){ //property expression is inverse of a property sparql += triple(value.toStringID(), propertyExpression.getNamedProperty(), variables.peek()); @@ -578,7 +541,7 @@ public void visit(OWLDataAllValuesFrom ce) { @Override public void visit(OWLDataHasValue ce) { OWLDataPropertyExpression propertyExpression = ce.getProperty(); - OWLLiteral value = ce.getValue(); + OWLLiteral value = ce.getFiller(); sparql += triple(variables.peek(), propertyExpression.asOWLDataProperty(), value); } @@ -672,12 +635,13 @@ public void visit(OWLDatatypeRestriction node) { } public static void main(String[] args) throws Exception { - ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); + StringRenderer.setRenderer(Rendering.DL_SYNTAX); OWLClassExpressionToSPARQLConverter converter = new OWLClassExpressionToSPARQLConverter(); OWLOntologyManager man = OWLManager.createOWLOntologyManager(); OWLDataFactory df = man.getOWLDataFactory(); - PrefixManager pm = new DefaultPrefixManager("http://dbpedia.org/ontology/"); + PrefixManager pm = new DefaultPrefixManager(); + pm.setDefaultPrefix("http://dbpedia.org/ontology/"); OWLClass clsA = df.getOWLClass("A", pm); OWLClass clsB = df.getOWLClass("B", pm); diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionUtils.java b/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionUtils.java index ee1fd75654..b0c99bbb39 100644 --- a/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionUtils.java +++ b/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionUtils.java @@ -1,61 +1,41 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.utilities.owl; -import java.util.List; -import java.util.Set; - -import org.semanticweb.owlapi.model.OWLClass; import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLClassExpressionVisitor; -import org.semanticweb.owlapi.model.OWLDataAllValuesFrom; -import org.semanticweb.owlapi.model.OWLDataComplementOf; -import org.semanticweb.owlapi.model.OWLDataExactCardinality; import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLDataHasValue; -import org.semanticweb.owlapi.model.OWLDataIntersectionOf; -import org.semanticweb.owlapi.model.OWLDataMaxCardinality; -import org.semanticweb.owlapi.model.OWLDataMinCardinality; -import org.semanticweb.owlapi.model.OWLDataOneOf; -import org.semanticweb.owlapi.model.OWLDataProperty; -import org.semanticweb.owlapi.model.OWLDataRange; -import org.semanticweb.owlapi.model.OWLDataRangeVisitor; -import org.semanticweb.owlapi.model.OWLDataSomeValuesFrom; -import org.semanticweb.owlapi.model.OWLDataUnionOf; -import org.semanticweb.owlapi.model.OWLDatatype; -import org.semanticweb.owlapi.model.OWLDatatypeRestriction; -import org.semanticweb.owlapi.model.OWLFacetRestriction; -import org.semanticweb.owlapi.model.OWLObjectAllValuesFrom; -import org.semanticweb.owlapi.model.OWLObjectComplementOf; -import org.semanticweb.owlapi.model.OWLObjectExactCardinality; -import org.semanticweb.owlapi.model.OWLObjectHasSelf; -import org.semanticweb.owlapi.model.OWLObjectHasValue; -import org.semanticweb.owlapi.model.OWLObjectIntersectionOf; -import org.semanticweb.owlapi.model.OWLObjectInverseOf; -import org.semanticweb.owlapi.model.OWLObjectMaxCardinality; -import org.semanticweb.owlapi.model.OWLObjectMinCardinality; -import org.semanticweb.owlapi.model.OWLObjectOneOf; -import org.semanticweb.owlapi.model.OWLObjectProperty; -import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom; -import org.semanticweb.owlapi.model.OWLObjectUnionOf; -import org.semanticweb.owlapi.model.OWLPropertyExpressionVisitor; import org.semanticweb.owlapi.util.MaximumModalDepthFinder; import org.semanticweb.owlapi.util.OWLObjectDuplicator; - import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; +import java.util.Set; + /** * A utility class for OWL class expressions. * * @author Lorenz Buehmann */ -public class OWLClassExpressionUtils implements OWLClassExpressionVisitor, OWLPropertyExpressionVisitor, OWLDataRangeVisitor{ +public class OWLClassExpressionUtils { - private static OWLDataFactory dataFactory = new OWLDataFactoryImpl(false, false); + private static OWLDataFactory dataFactory = new OWLDataFactoryImpl(); private static OWLObjectDuplicator duplicator = new OWLObjectDuplicator(dataFactory); - private static final OWLClassExpressionUtils visitor = new OWLClassExpressionUtils(); - private static volatile int length = 0; + private static final OWLClassExpressionLengthCalculator LENGTH_CALCULATOR= new OWLClassExpressionLengthCalculator(); private static final MaximumModalDepthFinder DEPTH_FINDER = new MaximumModalDepthFinder(); private static final OWLClassExpressionChildrenCollector CHILDREN_COLLECTOR = new OWLClassExpressionChildrenCollector(); @@ -64,14 +44,18 @@ public class OWLClassExpressionUtils implements OWLClassExpressionVisitor, OWLPr * @param ce the class expression * @return the length of the class expression */ - public static synchronized int getLength(OWLClassExpression ce){ - length = 0; - ce.accept(visitor); - return length; + public static int getLength(OWLClassExpression ce){ + OWLClassExpressionLengthCalculator calculator = new OWLClassExpressionLengthCalculator(); + return calculator.getLength(ce); } - + + public static int getLength(OWLClassExpression ce, OWLClassExpressionLengthMetric metric) { + OWLClassExpressionLengthCalculator calculator = new OWLClassExpressionLengthCalculator(metric); + return calculator.getLength(ce); + } + /** - * Returns the depth of a given class expression. + * Returns the depth of a class expression. * @param ce the class expression * @return the depth of the class expression */ @@ -81,18 +65,18 @@ public static synchronized int getDepth(OWLClassExpression ce){ } /** - * Returns the arity of a given class expression. + * Returns the arity of a class expression. * @param ce the class expression - * @return the depth of the class expression + * @return the arity of the class expression */ public static synchronized int getArity(OWLClassExpression ce){ return getChildren(ce).size(); } /** - * Returns all direct child expressions of the given class expression. + * Returns all direct child expressions of a class expression. * @param ce the class expression - * @return the direct child expression + * @return the direct child expressions */ public static Set getChildren(OWLClassExpression ce){ return ce.accept(CHILDREN_COLLECTOR); @@ -108,270 +92,14 @@ public static OWLClassExpression clone(OWLClassExpression ce) { } /** - * Determine whether a named class occurs on the outermost level, i.e. property depth 0 + * Determine whether a named class occurs on the outermost level of a class expression, i.e. property depth 0 * (it can still be at higher depth, e.g. if intersections are nested in unions) - * @param description - * @param cls - * @return + * @param description the class expression + * @param cls the named class + * @return whether the named class occurs on the outermost level of the class expression */ public static boolean occursOnFirstLevel(OWLClassExpression description, OWLClassExpression cls) { return description.containsConjunct(cls); } - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb.owlapi.model.OWLClass) - */ - @Override - public void visit(OWLClass ce) { - length++; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb.owlapi.model.OWLObjectIntersectionOf) - */ - @Override - public void visit(OWLObjectIntersectionOf ce) { - List operands = ce.getOperandsAsList(); - for (OWLClassExpression op : operands) { - op.accept(visitor); - } - length += operands.size() - 1; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb.owlapi.model.OWLObjectUnionOf) - */ - @Override - public void visit(OWLObjectUnionOf ce) { - List operands = ce.getOperandsAsList(); - for (OWLClassExpression op : operands) { - op.accept(visitor); - } - length += operands.size() - 1; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb.owlapi.model.OWLObjectComplementOf) - */ - @Override - public void visit(OWLObjectComplementOf ce) { - ce.getOperand().accept(visitor); - length++; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom) - */ - @Override - public void visit(OWLObjectSomeValuesFrom ce) { - ce.getProperty().accept(visitor); - ce.getFiller().accept(visitor); - length++; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb.owlapi.model.OWLObjectAllValuesFrom) - */ - @Override - public void visit(OWLObjectAllValuesFrom ce) { - ce.getProperty().accept(visitor); - ce.getFiller().accept(visitor); - length++; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb.owlapi.model.OWLObjectHasValue) - */ - @Override - public void visit(OWLObjectHasValue ce) { - ce.getProperty().accept(visitor); - length += 2; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb.owlapi.model.OWLObjectMinCardinality) - */ - @Override - public void visit(OWLObjectMinCardinality ce) { - ce.getProperty().accept(visitor); - ce.getFiller().accept(visitor); - length += 2; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb.owlapi.model.OWLObjectExactCardinality) - */ - @Override - public void visit(OWLObjectExactCardinality ce) { - ce.getProperty().accept(visitor); - ce.getFiller().accept(visitor); - length += 2; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb.owlapi.model.OWLObjectMaxCardinality) - */ - @Override - public void visit(OWLObjectMaxCardinality ce) { - ce.getProperty().accept(visitor); - ce.getFiller().accept(visitor); - length += 2; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb.owlapi.model.OWLObjectHasSelf) - */ - @Override - public void visit(OWLObjectHasSelf ce) { - ce.getProperty().accept(visitor); - length += 2; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb.owlapi.model.OWLObjectOneOf) - */ - @Override - public void visit(OWLObjectOneOf ce) { - length++; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb.owlapi.model.OWLDataSomeValuesFrom) - */ - @Override - public void visit(OWLDataSomeValuesFrom ce) { - ce.getProperty().accept(visitor); - ce.getFiller().accept(visitor); - length++; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb.owlapi.model.OWLDataAllValuesFrom) - */ - @Override - public void visit(OWLDataAllValuesFrom ce) { - ce.getProperty().accept(visitor); - ce.getFiller().accept(visitor); - length++; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb.owlapi.model.OWLDataHasValue) - */ - @Override - public void visit(OWLDataHasValue ce) { - ce.getProperty().accept(visitor); - length += 2; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb.owlapi.model.OWLDataMinCardinality) - */ - @Override - public void visit(OWLDataMinCardinality ce) { - ce.getProperty().accept(visitor); - ce.getFiller().accept(visitor); - length += 2; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb.owlapi.model.OWLDataExactCardinality) - */ - @Override - public void visit(OWLDataExactCardinality ce) { - ce.getProperty().accept(visitor); - ce.getFiller().accept(visitor); - length += 2; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitor#visit(org.semanticweb.owlapi.model.OWLDataMaxCardinality) - */ - @Override - public void visit(OWLDataMaxCardinality ce) { - ce.getProperty().accept(visitor); - ce.getFiller().accept(visitor); - length += 2; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLPropertyExpressionVisitor#visit(org.semanticweb.owlapi.model.OWLObjectProperty) - */ - @Override - public void visit(OWLObjectProperty property) { - length++; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLPropertyExpressionVisitor#visit(org.semanticweb.owlapi.model.OWLObjectInverseOf) - */ - @Override - public void visit(OWLObjectInverseOf property) { - length += 2; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLPropertyExpressionVisitor#visit(org.semanticweb.owlapi.model.OWLDataProperty) - */ - @Override - public void visit(OWLDataProperty property) { - length++; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLDataRangeVisitor#visit(org.semanticweb.owlapi.model.OWLDatatype) - */ - @Override - public void visit(OWLDatatype node) { - length++; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLDataRangeVisitor#visit(org.semanticweb.owlapi.model.OWLDataOneOf) - */ - @Override - public void visit(OWLDataOneOf node) { - length++; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLDataRangeVisitor#visit(org.semanticweb.owlapi.model.OWLDataComplementOf) - */ - @Override - public void visit(OWLDataComplementOf node) { - length += 2; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLDataRangeVisitor#visit(org.semanticweb.owlapi.model.OWLDataIntersectionOf) - */ - @Override - public void visit(OWLDataIntersectionOf node) { - Set operands = node.getOperands(); - for (OWLDataRange op : operands) { - op.accept(visitor); - } - length += operands.size() - 1; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLDataRangeVisitor#visit(org.semanticweb.owlapi.model.OWLDataUnionOf) - */ - @Override - public void visit(OWLDataUnionOf node) { - Set operands = node.getOperands(); - for (OWLDataRange op : operands) { - op.accept(visitor); - } - length += operands.size() - 1; - } - - /* (non-Javadoc) - * @see org.semanticweb.owlapi.model.OWLDataRangeVisitor#visit(org.semanticweb.owlapi.model.OWLDatatypeRestriction) - */ - @Override - public void visit(OWLDatatypeRestriction node) { - Set facetRestrictions = node.getFacetRestrictions(); - length += facetRestrictions.size(); - } } diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/OWLEntityTypeAdder.java b/components-core/src/main/java/org/dllearner/utilities/owl/OWLEntityTypeAdder.java index 37e434e74a..e06570cc2b 100644 --- a/components-core/src/main/java/org/dllearner/utilities/owl/OWLEntityTypeAdder.java +++ b/components-core/src/main/java/org/dllearner/utilities/owl/OWLEntityTypeAdder.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.utilities.owl; import java.util.HashSet; @@ -18,12 +36,12 @@ public class OWLEntityTypeAdder { * Infers the type of predicates p_i by analyzing the object of the triples using p_i and adds the * entity type assertion to the model, i.e. for a data property dp * will be added. - * @param model + * @param model the model */ public static void addEntityTypes(Model model){ StmtIterator iterator = model.listStatements(); - Set objectPropertyPredicates = new HashSet(); - Set dataPropertyPredicates = new HashSet(); + Set objectPropertyPredicates = new HashSet<>(); + Set dataPropertyPredicates = new HashSet<>(); while(iterator.hasNext()){ Statement st = iterator.next(); Property predicate = st.getPredicate(); diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/OWLPunningDetector.java b/components-core/src/main/java/org/dllearner/utilities/owl/OWLPunningDetector.java index e89f2eab96..69183150a7 100644 --- a/components-core/src/main/java/org/dllearner/utilities/owl/OWLPunningDetector.java +++ b/components-core/src/main/java/org/dllearner/utilities/owl/OWLPunningDetector.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.utilities.owl; import java.util.HashSet; @@ -25,10 +43,10 @@ public class OWLPunningDetector { new OWLObjectPropertyImpl(IRI.create("http://dl-learner.org/punning/relatedTo")); /** - * Checks whether the same IRI denotes both a class and an individual in the ontology. - * @param ontology - * @param iri - * @return + * Checks whether the class is also used as individual in the ontology. + * @param ontology the ontology + * @param cls the class + * @return whether the class is also used as individual in the ontology */ public static boolean hasPunning(OWLOntology ontology, OWLClass cls){ return hasPunning(ontology, cls.getIRI()); @@ -36,9 +54,9 @@ public static boolean hasPunning(OWLOntology ontology, OWLClass cls){ /** * Checks whether the same IRI denotes both a class and an individual in the ontology. - * @param ontology - * @param iri - * @return + * @param ontology ontology the ontology + * @param iri the IRI + * @return whether the IRI denotes both a class and an individual */ public static boolean hasPunning(OWLOntology ontology, IRI iri){ boolean isClass = ontology.getClassesInSignature().contains(ontology.getOWLOntologyManager().getOWLDataFactory().getOWLClass(iri)); @@ -48,12 +66,11 @@ public static boolean hasPunning(OWLOntology ontology, IRI iri){ /** * Returns the classes of the ontology that are also used as individuals, i.e. types of other classes. - * @param ontology - * @param iri - * @return + * @param ontology the ontology + * @return the classes */ public static Set getPunningClasses(OWLOntology ontology){ - Set classes = new HashSet(); + Set classes = new HashSet<>(); Set individualsInSignature = ontology.getIndividualsInSignature(); for (OWLClass cls : ontology.getClassesInSignature()) { if(individualsInSignature.contains(new OWLNamedIndividualImpl(cls.getIRI()))){ @@ -71,12 +88,11 @@ public static Set getPunningClasses(OWLOntology ontology){ /** * Returns the classes of the ontology that are also used as individuals, i.e. types of other classes. - * @param ontology - * @param iri - * @return + * @param ontology the ontology + * @return the classes */ public static Set getPunningIRIs(OWLOntology ontology){ - Set classes = new HashSet(); + Set classes = new HashSet<>(); Set individualsInSignature = ontology.getIndividualsInSignature(); for (OWLClass cls : ontology.getClassesInSignature()) { if(individualsInSignature.contains(new OWLNamedIndividualImpl(cls.getIRI()))){ @@ -94,9 +110,9 @@ public static Set getPunningIRIs(OWLOntology ontology){ /** * Checks whether the same IRI denotes both a class and an individual in the ontology. - * @param ontology - * @param iri - * @return + * @param ontology the ontology + * @param iri the IRI + * @return whether the same IRI denotes both a class and an individual */ public static boolean hasPunning(OWLOntology ontology, String iri){ return hasPunning(ontology, IRI.create(iri)); diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/OWLVocabulary.java b/components-core/src/main/java/org/dllearner/utilities/owl/OWLVocabulary.java index f99437d6d4..a1d71ef2ba 100644 --- a/components-core/src/main/java/org/dllearner/utilities/owl/OWLVocabulary.java +++ b/components-core/src/main/java/org/dllearner/utilities/owl/OWLVocabulary.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.owl; public class OWLVocabulary { @@ -95,10 +94,5 @@ public static boolean isStringSubClassVocab (String possSubClass){ || OWL_SUBCLASS_OF.equalsIgnoreCase(possSubClass)); } -// public static final String RDF_TYPE = ""; -// public static final String RDF_TYPE = ""; -// public static final String RDF_TYPE = ""; -// public static final String RDF_TYPE = ""; -// public static final String RDF_TYPE = ""; -// public static final String RDF_TYPE = ""; + } diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/OntologyCloserOWLAPI.java b/components-core/src/main/java/org/dllearner/utilities/owl/OntologyCloserOWLAPI.java index 58191920ae..b0bf23de23 100644 --- a/components-core/src/main/java/org/dllearner/utilities/owl/OntologyCloserOWLAPI.java +++ b/components-core/src/main/java/org/dllearner/utilities/owl/OntologyCloserOWLAPI.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,33 +16,15 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.owl; -import java.net.URI; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; -import java.util.SortedSet; - import org.dllearner.reasoning.OWLAPIReasoner; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.io.RDFXMLOntologyFormat; -import org.semanticweb.owlapi.model.AddAxiom; -import org.semanticweb.owlapi.model.AxiomType; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLObjectExactCardinality; -import org.semanticweb.owlapi.model.OWLObjectIntersectionOf; -import org.semanticweb.owlapi.model.OWLObjectProperty; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyManager; -import org.semanticweb.owlapi.model.OWLPropertyAxiom; -import org.semanticweb.owlapi.model.OWLTransitiveObjectPropertyAxiom; +import org.semanticweb.owlapi.formats.RDFXMLDocumentFormat; +import org.semanticweb.owlapi.model.*; + +import java.net.URI; +import java.util.*; public class OntologyCloserOWLAPI { @@ -56,7 +38,7 @@ public class OntologyCloserOWLAPI { public OntologyCloserOWLAPI(OWLAPIReasoner reasoner) { this.rs = reasoner; - this.indToRestr = new HashMap>(); + this.indToRestr = new HashMap<>(); // this.rs = new ReasonerComponent(reasoner); this.manager = OWLManager.createOWLOntologyManager(); this.factory = manager.getOWLDataFactory(); @@ -127,7 +109,7 @@ public void applyNumberRestrictionsConcise() { } }// end for - Set target = new HashSet(); + Set target = new HashSet<>(); Set s = null; for (OWLIndividual oneInd : indToRestr.keySet()) { @@ -138,7 +120,7 @@ public void applyNumberRestrictionsConcise() { // collect everything in an intersection OWLObjectIntersectionOf intersection = factory.getOWLObjectIntersectionOf(target); s = null; - target = new HashSet(); + target = new HashSet<>(); OWLAxiom axiom = factory.getOWLClassAssertionAxiom(intersection, factory .getOWLNamedIndividual(IRI.create(oneInd.toStringID()))); @@ -176,7 +158,7 @@ public boolean testForTransitiveProperties(boolean printflag) { public void writeOWLFile(URI filename) { try { - manager.saveOntology(this.onto, new RDFXMLOntologyFormat(), + manager.saveOntology(this.onto, new RDFXMLDocumentFormat(), IRI.create(filename)); } catch (Exception e) { e.printStackTrace(); diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/OntologyToByteConverter.java b/components-core/src/main/java/org/dllearner/utilities/owl/OntologyToByteConverter.java index 88965a2e4d..0d1d230f76 100644 --- a/components-core/src/main/java/org/dllearner/utilities/owl/OntologyToByteConverter.java +++ b/components-core/src/main/java/org/dllearner/utilities/owl/OntologyToByteConverter.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.utilities.owl; import org.semanticweb.owlapi.model.OWLOntology; diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/PropertyContext.java b/components-core/src/main/java/org/dllearner/utilities/owl/PropertyContext.java index fac0fef971..24016ba3fc 100644 --- a/components-core/src/main/java/org/dllearner/utilities/owl/PropertyContext.java +++ b/components-core/src/main/java/org/dllearner/utilities/owl/PropertyContext.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,11 +16,11 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.owl; import java.util.LinkedList; +import org.jetbrains.annotations.NotNull; import org.semanticweb.owlapi.model.OWLClass; import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLDataFactory; @@ -49,7 +49,7 @@ public class PropertyContext extends LinkedList implements Co * @see java.lang.Comparable#compareTo(java.lang.Object) */ @Override - public int compareTo(PropertyContext context) { + public int compareTo(@NotNull PropertyContext context) { // we first distinguish on size - simpler contexts come first int diff = context.size() - size(); if(diff != 0) { diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/SimpleOWLEntityChecker.java b/components-core/src/main/java/org/dllearner/utilities/owl/SimpleOWLEntityChecker.java index 15554e2a7e..fdd2aa726c 100644 --- a/components-core/src/main/java/org/dllearner/utilities/owl/SimpleOWLEntityChecker.java +++ b/components-core/src/main/java/org/dllearner/utilities/owl/SimpleOWLEntityChecker.java @@ -1,30 +1,33 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.utilities.owl; +import com.hp.hpl.jena.vocabulary.OWL; import org.dllearner.core.AbstractReasonerComponent; -import org.semanticweb.owlapi.apibinding.OWLManager; import org.semanticweb.owlapi.expression.OWLEntityChecker; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAnnotationProperty; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLDataProperty; -import org.semanticweb.owlapi.model.OWLDatatype; -import org.semanticweb.owlapi.model.OWLNamedIndividual; -import org.semanticweb.owlapi.model.OWLObjectProperty; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyCreationException; -import org.semanticweb.owlapi.model.OWLOntologyManager; -import org.semanticweb.owlapi.model.OWLOntologyManagerFactory; +import org.semanticweb.owlapi.model.*; import org.semanticweb.owlapi.util.IRIShortFormProvider; import org.semanticweb.owlapi.util.SimpleIRIShortFormProvider; - -import com.hp.hpl.jena.vocabulary.OWL; - import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; -import uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl; + +import java.util.Collection; +import java.util.Set; /** * @author Lorenz Buehmann @@ -43,6 +46,17 @@ public SimpleOWLEntityChecker(AbstractReasonerComponent rc) { this.rc = rc; } + private T find(String name, Collection c) { + for (T x : c) { + if(allowShortForm && sfp.getShortForm(x.getIRI()).equals(name) || + x.getIRI().toString().equals(name) || + x.getIRI().toQuotedString().equals(name)) { + return x; + } + } + return null; + } + /* (non-Javadoc) * @see org.semanticweb.owlapi.expression.OWLEntityChecker#getOWLClass(java.lang.String) */ @@ -51,14 +65,13 @@ public OWLClass getOWLClass(String name) { if ("owl:Thing".equals(name) || IRI.create(OWL.NS + "Thing").toQuotedString().equals(name)) { return df.getOWLThing(); } - for (OWLClass cls : rc.getClasses()) { - if(allowShortForm && sfp.getShortForm(cls.getIRI()).equals(name) || - cls.getIRI().toString().equals(name) || - cls.getIRI().toQuotedString().equals(name)) { - return cls; - } + OWLClass cls = find(name, rc.getClasses()); + if (cls != null) { + return cls; + } + if (allowShortForm && "Thing".equals(name)) { + return df.getOWLThing(); } - return null; } @@ -67,11 +80,7 @@ public OWLClass getOWLClass(String name) { */ @Override public OWLObjectProperty getOWLObjectProperty(String name) { - OWLObjectProperty p = df.getOWLObjectProperty(IRI.create(name)); - if(rc.getObjectProperties().contains(p)) { - return p; - } - return null; + return find(name, rc.getObjectProperties()); } /* (non-Javadoc) @@ -79,12 +88,7 @@ public OWLObjectProperty getOWLObjectProperty(String name) { */ @Override public OWLDataProperty getOWLDataProperty(String name) { - OWLDataProperty p = df.getOWLDataProperty(IRI.create(name)); - - if(rc.getDatatypeProperties().contains(p)) { - return p; - } - return null; + return find(name, rc.getDatatypeProperties()); } /* (non-Javadoc) @@ -92,12 +96,7 @@ public OWLDataProperty getOWLDataProperty(String name) { */ @Override public OWLNamedIndividual getOWLIndividual(String name) { - OWLNamedIndividual ind = df.getOWLNamedIndividual(IRI.create(name)); - - if(rc.getIndividuals().contains(ind)) { - return ind; - } - return null; + return find(name, (Set)(Set)rc.getIndividuals()); } /* (non-Javadoc) diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/SimpleOntologyToByteConverter.java b/components-core/src/main/java/org/dllearner/utilities/owl/SimpleOntologyToByteConverter.java index ed87fb889d..24ed94126f 100644 --- a/components-core/src/main/java/org/dllearner/utilities/owl/SimpleOntologyToByteConverter.java +++ b/components-core/src/main/java/org/dllearner/utilities/owl/SimpleOntologyToByteConverter.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.utilities.owl; import java.io.ByteArrayInputStream; @@ -28,9 +46,7 @@ public byte[] convert(OWLOntology ontology) { try { manager.saveOntology(ontology, baos); baos.close(); - } catch (OWLOntologyStorageException e) { - throw new RuntimeException(e); - } catch (IOException e) { + } catch (OWLOntologyStorageException | IOException e) { throw new RuntimeException(e); } @@ -46,9 +62,7 @@ public OWLOntology convert(byte[] bytes, OWLOntologyManager manager) { OWLOntology ontology = manager.loadOntologyFromOntologyDocument(bais); bais.close(); return ontology; - } catch (OWLOntologyCreationException e) { - throw new RuntimeException(e); - } catch (IOException e) { + } catch (OWLOntologyCreationException | IOException e) { throw new RuntimeException(e); } } diff --git a/components-core/src/main/java/org/dllearner/utilities/owl/VariablesMapping.java b/components-core/src/main/java/org/dllearner/utilities/owl/VariablesMapping.java index ce89a257d3..ee0fc4f24c 100644 --- a/components-core/src/main/java/org/dllearner/utilities/owl/VariablesMapping.java +++ b/components-core/src/main/java/org/dllearner/utilities/owl/VariablesMapping.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.utilities.owl; import java.util.HashMap; diff --git a/components-core/src/main/java/org/dllearner/utilities/package-info.java b/components-core/src/main/java/org/dllearner/utilities/package-info.java index e8a967774d..e391aea3b2 100644 --- a/components-core/src/main/java/org/dllearner/utilities/package-info.java +++ b/components-core/src/main/java/org/dllearner/utilities/package-info.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - /** * Utility classes. */ diff --git a/components-core/src/main/java/org/dllearner/utilities/semkernel/MPSemKernelWorkflow.java b/components-core/src/main/java/org/dllearner/utilities/semkernel/MPSemKernelWorkflow.java index b3833c356c..e2beb941b6 100644 --- a/components-core/src/main/java/org/dllearner/utilities/semkernel/MPSemKernelWorkflow.java +++ b/components-core/src/main/java/org/dllearner/utilities/semkernel/MPSemKernelWorkflow.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.utilities.semkernel; import java.io.BufferedReader; @@ -6,14 +24,7 @@ import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TreeSet; +import java.util.*; import org.apache.log4j.Logger; import org.dllearner.algorithms.semkernel.SemKernel; @@ -268,7 +279,7 @@ public void start() { private Map> readMGI2MPMapping(String mgi2mpFilePath) throws IOException { - Map> mgi2mp = new HashMap>(); + Map> mgi2mp = new HashMap<>(); BufferedReader bufferedReader = new BufferedReader(new FileReader( new File(mgi2mpFilePath))); @@ -314,7 +325,7 @@ public void prepareMPSampleTrainingData() throws IOException { OWLClass searchCls = new OWLClassImpl(IRI.create(searchClassUriStr)); - Set classifierFor = new TreeSet(); + Set classifierFor = new TreeSet<>(); classifierFor.add(searchClassUriStr); Set subClasses = @@ -325,8 +336,8 @@ public void prepareMPSampleTrainingData() throws IOException { classifierFor.add(uriStr); } - List negatives = new ArrayList(); - List positives = new ArrayList(); + List negatives = new ArrayList<>(); + List positives = new ArrayList<>(); // build lines to write to file (SVM light format) for (String mgiId : mgi2mp.keySet()) { @@ -395,7 +406,7 @@ private void prepareMPPredictionData() throws IOException { // } // buffWriter.newLine(); - Set classifierFor = new TreeSet(); + Set classifierFor = new TreeSet<>(); classifierFor.add(predClsUriStr); OWLClass predCls = new OWLClassImpl(IRI.create(predClsUriStr)); @@ -407,8 +418,8 @@ private void prepareMPPredictionData() throws IOException { classifierFor.add(uriStr); } - List negatives = new ArrayList(); - List positives = new ArrayList(); + List negatives = new ArrayList<>(); + List positives = new ArrayList<>(); // build lines to write to file (SVM light format) for (String mgiId : mgi2mp.keySet()) { @@ -455,7 +466,7 @@ private void prepareMPPredictionData() throws IOException { } private static Set readTrainURIs(String trainURIsFilePath) throws IOException { - Set uriStrs = new HashSet(); + Set uriStrs = new HashSet<>(); BufferedReader bufferedReader = new BufferedReader( new FileReader(new File(trainURIsFilePath))); @@ -475,7 +486,7 @@ private static Set readTrainURIs(String trainURIsFilePath) throws IOExce private Map> readMGI2GOMapping(String mgi2goFilePath) throws IOException { - Map> mgi2go = new HashMap>(); + Map> mgi2go = new HashMap<>(); BufferedReader bufferedReader = new BufferedReader(new FileReader( new File(mgi2goFilePath))); @@ -493,7 +504,7 @@ private Map> readMGI2GOMapping(String mgi2goFilePath) if (goId.trim().length() == 0) continue; // skip lines not containing a GO ID - if (evidenceCode != "ND" && !qualifier.contains("NOT")) { + if (!Objects.equals(evidenceCode, "ND") && !qualifier.contains("NOT")) { String goUriStr = oboPrefix + goId.replace(":", "_"); if (!mgi2go.containsKey(mgiId)) { diff --git a/components-core/src/main/java/org/dllearner/utilities/semkernel/SemKernelWorkflow.java b/components-core/src/main/java/org/dllearner/utilities/semkernel/SemKernelWorkflow.java index c43361f51b..1ad421cb6a 100644 --- a/components-core/src/main/java/org/dllearner/utilities/semkernel/SemKernelWorkflow.java +++ b/components-core/src/main/java/org/dllearner/utilities/semkernel/SemKernelWorkflow.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.utilities.semkernel; import org.dllearner.core.AbstractComponent; diff --git a/components-core/src/main/java/org/dllearner/utilities/sparql/RedundantTypeTriplePatternRemover.java b/components-core/src/main/java/org/dllearner/utilities/sparql/RedundantTypeTriplePatternRemover.java index 862b76e6b6..2d1900824d 100644 --- a/components-core/src/main/java/org/dllearner/utilities/sparql/RedundantTypeTriplePatternRemover.java +++ b/components-core/src/main/java/org/dllearner/utilities/sparql/RedundantTypeTriplePatternRemover.java @@ -1,12 +1,26 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.utilities.sparql; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; -import java.util.Map; import java.util.Map.Entry; import java.util.Set; @@ -52,8 +66,8 @@ public RedundantTypeTriplePatternRemover(QueryExecutionFactory qef) { /** * Returns a pruned copy of the given query. - * @param query - * @return + * @param query the query + * @return a pruned copy of the given query */ public Query pruneQuery(Query query) { Query copy = query.cloneQuery(); @@ -62,7 +76,7 @@ public Query pruneQuery(Query query) { } private Set getSuperClasses(Node cls){ - Set superClasses = new HashSet(); + Set superClasses = new HashSet<>(); superClassesQueryTemplate.setIri("sub", cls.getURI()); @@ -103,12 +117,12 @@ public void visit(ElementTriplesBlock el) { } // check for semantically redundant triple patterns - Set redundantTriples = new HashSet(); + Set redundantTriples = new HashSet<>(); for (Entry> entry : subject2TypeTriples.asMap().entrySet()) { Collection triples = entry.getValue(); // get all super classes - Set superClasses = new HashSet(); + Set superClasses = new HashSet<>(); for (Triple triple : triples) { Node cls = triple.getObject(); superClasses.addAll(getSuperClasses(cls)); @@ -143,12 +157,12 @@ public void visit(ElementPathBlock el) { } // check for semantically redundant triple patterns - Set redundantTriples = new HashSet(); + Set redundantTriples = new HashSet<>(); for (Entry> entry : subject2TypeTriples.asMap().entrySet()) { Collection triples = entry.getValue(); // get all super classes - Set superClasses = new HashSet(); + Set superClasses = new HashSet<>(); for (Triple triple : triples) { Node cls = triple.getObject(); superClasses.addAll(getSuperClasses(cls)); diff --git a/components-core/src/main/java/org/dllearner/utilities/split/AbstractDateTimeValuesSplitter.java b/components-core/src/main/java/org/dllearner/utilities/split/AbstractDateTimeValuesSplitter.java index fa2631164b..da1489bc50 100644 --- a/components-core/src/main/java/org/dllearner/utilities/split/AbstractDateTimeValuesSplitter.java +++ b/components-core/src/main/java/org/dllearner/utilities/split/AbstractDateTimeValuesSplitter.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.utilities.split; @@ -27,7 +42,7 @@ public AbstractDateTimeValuesSplitter(AbstractReasonerComponent reasoner, OWLDat */ @Override public Set getDataProperties() { - Set dataProperties = new HashSet(); + Set dataProperties = new HashSet<>(); for (OWLDataProperty dp : reasoner.getDatatypeProperties()) { OWLDatatype datatype = reasoner.getDatatype(dp); if(datatype != null && getDatatypes().contains(datatype)) { diff --git a/components-core/src/main/java/org/dllearner/utilities/split/AbstractNumericValuesSplitter.java b/components-core/src/main/java/org/dllearner/utilities/split/AbstractNumericValuesSplitter.java index 7fd6fa8299..bd2565e8e7 100644 --- a/components-core/src/main/java/org/dllearner/utilities/split/AbstractNumericValuesSplitter.java +++ b/components-core/src/main/java/org/dllearner/utilities/split/AbstractNumericValuesSplitter.java @@ -1,9 +1,25 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.utilities.split; import java.math.BigDecimal; +import java.math.RoundingMode; import java.util.Set; import org.dllearner.core.AbstractReasonerComponent; @@ -46,21 +62,21 @@ protected T mixTwoValues(T value1, T value2) { private T avg(T number1, T number2) { T avg = null; if (number1 instanceof Integer && number2 instanceof Integer){ - avg = (T) Integer.valueOf((((Integer)number1).intValue() + ((Integer)number2).intValue()) / 2); + avg = (T) Integer.valueOf(((Integer) number1 + (Integer) number2) / 2); } else if (number1 instanceof Short && number2 instanceof Short){ - avg = (T) Short.valueOf((short) ((((Short)number1).shortValue() + ((Short)number2).shortValue()) / 2)); + avg = (T) Short.valueOf((short) (((Short) number1 + (Short) number2) / 2)); } else if (number1 instanceof Byte && number2 instanceof Byte){ - avg = (T) Byte.valueOf((byte) ((((Byte)number1).byteValue() + ((Byte)number2).byteValue()) / 2)); + avg = (T) Byte.valueOf((byte) (((Byte) number1 + (Byte) number2) / 2)); } else if (number1 instanceof Long && number2 instanceof Long){ - avg = (T) Long.valueOf((((Long)number1).longValue() + ((Long)number2).longValue()) / 2); + avg = (T) Long.valueOf(((Long) number1 + (Long) number2) / 2); } else if (number1 instanceof Double && number2 instanceof Double) { avg = (T) Double.valueOf((BigDecimal.valueOf(((Double)number1).doubleValue()).add( - BigDecimal.valueOf(((Double)number2).doubleValue())).divide(BigDecimal.valueOf(2))).doubleValue()); + BigDecimal.valueOf(((Double)number2).doubleValue())).divide(BigDecimal.valueOf(2), RoundingMode.HALF_DOWN)).doubleValue()); } else if(number1 instanceof Float && number2 instanceof Float) { avg = (T) Float.valueOf( (BigDecimal.valueOf(((Float)number1).floatValue()). add(BigDecimal.valueOf(((Float)number2).floatValue())).divide( - BigDecimal.valueOf(2d))).floatValue()); + BigDecimal.valueOf(2d), RoundingMode.HALF_DOWN)).floatValue()); } return avg; } diff --git a/components-core/src/main/java/org/dllearner/utilities/split/AbstractValuesSplitter.java b/components-core/src/main/java/org/dllearner/utilities/split/AbstractValuesSplitter.java index bc173424b0..6643ff77ed 100644 --- a/components-core/src/main/java/org/dllearner/utilities/split/AbstractValuesSplitter.java +++ b/components-core/src/main/java/org/dllearner/utilities/split/AbstractValuesSplitter.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.utilities.split; @@ -73,14 +88,14 @@ public Map> computeSplits() { protected > List simpleListSplitter( Collection allValues, int maxNrOfSplits) { // convert set to a list where values are sorted - List values = new LinkedList(allValues); + List values = new LinkedList<>(allValues); Collections.sort(values); int nrOfValues = values.size(); int nrOfSplits = Math.min(maxNrOfSplits, nrOfValues + 1); // create split set - Set splitsDP = new TreeSet(); + Set splitsDP = new TreeSet<>(); // add the first element if (nrOfValues > 0) { diff --git a/components-core/src/main/java/org/dllearner/utilities/split/DefaultDateTimeValuesSplitter.java b/components-core/src/main/java/org/dllearner/utilities/split/DefaultDateTimeValuesSplitter.java index c1a906b911..ddf7f28f89 100644 --- a/components-core/src/main/java/org/dllearner/utilities/split/DefaultDateTimeValuesSplitter.java +++ b/components-core/src/main/java/org/dllearner/utilities/split/DefaultDateTimeValuesSplitter.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.utilities.split; @@ -57,7 +72,7 @@ public List computeSplits(OWLDataProperty dp) { DateTimeFormatter parser = OWLAPIUtils.dateTimeParsers.get(datatype); DateTimeFormatter formatter = OWLAPIUtils.dateTimeFormatters.get(datatype); - List values = new LinkedList(); + List values = new LinkedList<>(); for (Entry> entry : ind2Values.entrySet()) { for (OWLLiteral value : entry.getValue()) { diff --git a/components-core/src/main/java/org/dllearner/utilities/split/DefaultNumericValuesSplitter.java b/components-core/src/main/java/org/dllearner/utilities/split/DefaultNumericValuesSplitter.java index 7dd9e12366..09710fe8ea 100644 --- a/components-core/src/main/java/org/dllearner/utilities/split/DefaultNumericValuesSplitter.java +++ b/components-core/src/main/java/org/dllearner/utilities/split/DefaultNumericValuesSplitter.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.utilities.split; @@ -61,7 +76,6 @@ public List computeSplits(OWLDataProperty dp) { List splitLiterals = new ArrayList<>(); List splitValues = computeSplitValues(dp); - for (Number value : splitValues) { OWLLiteral literal = dataFactory.getOWLLiteral(value.toString(), reasoner.getDatatype(dp)); splitLiterals.add(literal); @@ -71,13 +85,17 @@ public List computeSplits(OWLDataProperty dp) { } private > List computeSplitValues(OWLDataProperty dp) { - Set valuesSet = new TreeSet(); + Set valuesSet = new TreeSet<>(); Map> ind2Values = reasoner.getNumericDatatypeMembers(dp); - // add all values to the set for(Entry> e : ind2Values.entrySet()){ - valuesSet.addAll(e.getValue()); + try { + valuesSet.addAll(e.getValue()); + } catch(ClassCastException ce) { + System.err.println("Mixed datatypes in "+dp.toStringID()); + throw ce; + } } return simpleListSplitter(valuesSet, maxNrOfSplits); diff --git a/components-core/src/main/java/org/dllearner/utilities/split/OptimizedNumericValuesSplitter.java b/components-core/src/main/java/org/dllearner/utilities/split/OptimizedNumericValuesSplitter.java index b345045ebc..fe062b9662 100644 --- a/components-core/src/main/java/org/dllearner/utilities/split/OptimizedNumericValuesSplitter.java +++ b/components-core/src/main/java/org/dllearner/utilities/split/OptimizedNumericValuesSplitter.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.utilities.split; @@ -31,9 +46,6 @@ public class OptimizedNumericValuesSplitter extends AbstractNumericValuesSplitte private PosNegLP lp; // private Class clazz; - /** - * @param reasoner - */ public OptimizedNumericValuesSplitter(AbstractReasonerComponent reasoner, OWLDataFactory dataFactory, PosNegLP lp) { super(reasoner, dataFactory); this.lp = lp; @@ -49,7 +61,7 @@ public List computeSplits(OWLDataProperty dp) { List splitLiterals = new ArrayList<>(); List splitValues = computeSplitValues(dp); - + for (Number value : splitValues) { OWLLiteral literal = dataFactory.getOWLLiteral(value.toString(), reasoner.getDatatype(dp)); splitLiterals.add(literal); @@ -59,10 +71,10 @@ public List computeSplits(OWLDataProperty dp) { } public > List computeSplitValues(OWLDataProperty dp) { - List splitsDP = new LinkedList(); + List splitsDP = new LinkedList<>(); NumberFormat numberFormat = NumberFormat.getInstance(); - SortedSet posRelatedValues = new TreeSet(); + SortedSet posRelatedValues = new TreeSet<>(); for (OWLIndividual ex : lp.getPositiveExamples()) { Set relatedValues = reasoner.getRelatedValues(ex, dp); @@ -84,7 +96,7 @@ public > List computeSplitValues(OWLDataProp } } - SortedSet negRelatedValues = new TreeSet(); + SortedSet negRelatedValues = new TreeSet<>(); for (OWLIndividual ex : lp.getNegativeExamples()) { Set relatedValues = reasoner.getRelatedValues(ex, dp); diff --git a/components-core/src/main/java/org/dllearner/utilities/split/ValuesSplitter.java b/components-core/src/main/java/org/dllearner/utilities/split/ValuesSplitter.java index 72c876cfe5..7c135d43b8 100644 --- a/components-core/src/main/java/org/dllearner/utilities/split/ValuesSplitter.java +++ b/components-core/src/main/java/org/dllearner/utilities/split/ValuesSplitter.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.utilities.split; diff --git a/components-core/src/main/java/org/dllearner/utilities/statistics/FleissKappa.java b/components-core/src/main/java/org/dllearner/utilities/statistics/FleissKappa.java index 141569e413..13d30aaccd 100644 --- a/components-core/src/main/java/org/dllearner/utilities/statistics/FleissKappa.java +++ b/components-core/src/main/java/org/dllearner/utilities/statistics/FleissKappa.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.statistics; import java.util.Arrays; @@ -59,7 +58,6 @@ public static void main(String[] args) /** * Computes the Kappa value - * @param n Number of rating per subjects (number of human raters) * @param mat Matrix[subjects][categories] * @return The Kappa value */ diff --git a/components-core/src/main/java/org/dllearner/utilities/statistics/RawAgreement.java b/components-core/src/main/java/org/dllearner/utilities/statistics/RawAgreement.java index 5928946584..ae57b12e80 100644 --- a/components-core/src/main/java/org/dllearner/utilities/statistics/RawAgreement.java +++ b/components-core/src/main/java/org/dllearner/utilities/statistics/RawAgreement.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.statistics; import org.apache.log4j.Logger; diff --git a/components-core/src/main/java/org/dllearner/utilities/statistics/SimpleClock.java b/components-core/src/main/java/org/dllearner/utilities/statistics/SimpleClock.java index 15cef40c5d..defb40fb52 100644 --- a/components-core/src/main/java/org/dllearner/utilities/statistics/SimpleClock.java +++ b/components-core/src/main/java/org/dllearner/utilities/statistics/SimpleClock.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.statistics; /** @@ -105,8 +104,7 @@ public void reset() { } /** - * returns the needed time up to now in ms - * @return + * @return the needed time up to now in ms */ public long getTime() { long now = System.currentTimeMillis(); diff --git a/components-core/src/main/java/org/dllearner/utilities/statistics/Stat.java b/components-core/src/main/java/org/dllearner/utilities/statistics/Stat.java index 7dbe3f4b94..ef39a3c9c7 100755 --- a/components-core/src/main/java/org/dllearner/utilities/statistics/Stat.java +++ b/components-core/src/main/java/org/dllearner/utilities/statistics/Stat.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.statistics; import java.text.DecimalFormat; @@ -62,8 +61,7 @@ public Stat(Stat stat1, Stat stat2) { * Creates a new stat object by merging several stat objects. The result is the same as if * the numbers, which have been added to each stat would have been added to this * stat object. - * @param stat1 Statistical object 1. - * @param stat2 Statistical object 2. + * @param stats the stats */ public Stat(Set stats) { for(Stat stat : stats){ diff --git a/components-core/src/main/java/org/dllearner/utilities/statistics/Statistics.java b/components-core/src/main/java/org/dllearner/utilities/statistics/Statistics.java deleted file mode 100644 index 23ba5b0cb4..0000000000 --- a/components-core/src/main/java/org/dllearner/utilities/statistics/Statistics.java +++ /dev/null @@ -1,253 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.utilities.statistics; - - -import java.util.HashMap; -import java.util.LinkedList; - - -public class Statistics { - - private static String currentLabel=""; - private static LinkedList order = new LinkedList(); - private static HashMap numberOfTriples = new HashMap(); - private static HashMap timeCollecting = new HashMap(); - private static HashMap timeLearning = new HashMap(); - private static HashMap timeTotal = new HashMap(); - - - private static HashMap numberOfSparqlQueries = new HashMap(); - private static HashMap numberOfCachedSparqlQueries = new HashMap(); - - - public static void addTriples(int value) { - - Integer current = numberOfTriples.get(currentLabel); - if(current==null) - numberOfTriples.put(currentLabel, new Integer(value)); - else { - numberOfTriples.put(currentLabel, new Integer(current.intValue()+value)); - } - - - - } - - public static void addTimeCollecting(long value) { - addTimeTotal(value); - Long current = timeCollecting.get(currentLabel); - if(current==null) - timeCollecting.put(currentLabel, new Long(value)); - else { - timeCollecting.put(currentLabel, new Long(current.longValue()+value)); - } - - - } - - public static void addTimeTotal(long value) { - Long current = timeTotal.get(currentLabel); - if(current==null) - timeTotal.put(currentLabel, new Long(value)); - else { - timeTotal.put(currentLabel, new Long(current.longValue()+value)); - } - - } - - public static void addTimeLearning(long value) { - addTimeTotal(value); - - Long current = timeLearning.get(currentLabel); - if(current==null) - timeLearning.put(currentLabel, new Long(value)); - else { - timeLearning.put(currentLabel, new Long(current.longValue()+value)); - } - } - - public static void increaseCachedQuery() { - Integer current = numberOfCachedSparqlQueries.get(currentLabel); - if(current==null) - numberOfCachedSparqlQueries.put(currentLabel, new Integer(1)); - else { - numberOfCachedSparqlQueries.put(currentLabel, new Integer(current.intValue()+1)); - } - } - - public static void increaseQuery() { - Integer current = numberOfSparqlQueries.get(currentLabel); - if(current==null) - numberOfSparqlQueries.put(currentLabel, new Integer(1)); - else { - numberOfSparqlQueries.put(currentLabel, new Integer(current.intValue()+1)); - } - } - - - public static void print(int number){ - - System.out.println("*****************TRIPLES"); - printInt(numberOfTriples,"triples\t"); - printIntAVG(numberOfTriples,number,"triples avg\t"); - - System.out.println("*****************TIME"); - - printLong(timeCollecting, "collecting\t"); - printLongAVG(timeCollecting,number,"collecting avg\t"); - printLong(timeLearning, "learning\t"); - printLongAVG(timeLearning,number,"learning avg\t"); - - System.out.println("*****************Queries"); - printInt(numberOfCachedSparqlQueries,"cached queries\t"); - printInt(numberOfSparqlQueries,"total queries\t"); - - - - //printIntAVG(numberOfTriples,number,"triples avg\t"); - - - - - - - - } - - - public static String getAVGTriplesForRecursionDepth(int number){ - - String ret="#Label, i.e. rec depth \t avg number of triples\n"; - for (int i = 0; i < order.size(); i++) { - String label=order.get(i); - try { - ret+=label+"\t"+ (numberOfTriples.get(label).intValue()/number)+"\n"; - } catch (Exception e) { } - } - return ret; - } - - public static String getAVGTimeLearning(int number){ - - String ret="#Label, i.e. rec depth \t avg time for learning including reasoning\n"; - for (int i = 0; i < order.size(); i++) { - String label=order.get(i); - try { - ret+=label+"\t"+ (timeLearning.get(label).longValue()/number)+"\n"; - } catch (Exception e) { } - } - return ret; -} - - public static String getAVGTimeCollecting(int number){ - - String ret="#Label, i.e. rec depth \t avg time for extraction\n"; - for (int i = 0; i < order.size(); i++) { - String label=order.get(i); - try { - ret+=label+"\t"+ (timeCollecting.get(label).longValue()/number)+"\n"; - } catch (Exception e) { } - } - return ret; - } - - public static String getAVGtotalTime(int number){ - - String ret="#Label, i.e. rec depth \t avg total time \n"; - for (int i = 0; i < order.size(); i++) { - String label=order.get(i); - try { - ret+=label+"\t"+ (timeTotal.get(label).longValue()/number)+"\n"; - } catch (Exception e) { } - } - return ret; - } - - public static void printIntAVG(HashMap hm, int number, String str){ - for (int i = 0; i < order.size(); i++) { - String label=order.get(i); - try { - System.out.println(str+""+label+"\t"+ (hm.get(label).intValue()/number)); - } catch (Exception e) { } - } - } - - public static void printInt(HashMap hm, String str){ - for (int i = 0; i < order.size(); i++) { - String label=order.get(i); - try { - System.out.println(str+""+label+"\t"+hm.get(label)); - } catch (Exception e) { } - } - } - - public static void printLongAVG(HashMap hm, int number, String str){ - for (int i = 0; i < order.size(); i++) { - String label=order.get(i); - try { - System.out.println(str+label+"\t"+ (hm.get(label).intValue()/number)); - } catch (Exception e) { } - } - } - - public static void printLong(HashMap hm,String str){ - for (int i = 0; i < order.size(); i++) { - String label=order.get(i); - try { - System.out.println(str+label+"\t"+hm.get(label)); - } catch (Exception e) { } - } - } - - - public static void setCurrentLabel(String label) { - currentLabel=label; - if (!order.contains(label))order.add(label); - } - - public static String getCurrentLabel() { - return currentLabel; - } - - public static void reset(){ - currentLabel=""; - order = new LinkedList(); - numberOfTriples = new HashMap(); - timeCollecting = new HashMap(); - timeLearning = new HashMap(); - timeTotal = new HashMap(); - - numberOfSparqlQueries = new HashMap(); - numberOfCachedSparqlQueries = new HashMap(); - System.out.println("xxxRESET"); - - } - - //stats - - - - - - - - -} diff --git a/components-core/src/main/java/org/dllearner/utilities/statistics/Table.java b/components-core/src/main/java/org/dllearner/utilities/statistics/Table.java index adeb3d0e14..81ecd073ee 100644 --- a/components-core/src/main/java/org/dllearner/utilities/statistics/Table.java +++ b/components-core/src/main/java/org/dllearner/utilities/statistics/Table.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.statistics; import java.io.File; @@ -38,11 +37,11 @@ */ public class Table implements Serializable{ - private static final long serialVersionUID = 0l; + private static final long serialVersionUID = 0L; //used to give a good percentage output //private DecimalFormat df = new DecimalFormat( ".00%" ); - private List columns = new ArrayList(); + private List columns = new ArrayList<>(); private String tableName = ""; private String caption = ""; @@ -126,7 +125,7 @@ public String getLatexString(){ } public String getRowInLatex(int index){ - List l = new ArrayList(); + List l = new ArrayList<>(); for(TableColumn c: columns){ l.add(c.getEntry(index)); } @@ -149,7 +148,7 @@ public void removeColumn(String header){ public List getColumnHeaders(){ - List entries = new ArrayList(); + List entries = new ArrayList<>(); for (TableColumn c : columns) { entries.add(c.getHeader()); } diff --git a/components-core/src/main/java/org/dllearner/utilities/statistics/TableColumn.java b/components-core/src/main/java/org/dllearner/utilities/statistics/TableColumn.java index bcebfc9538..4b7343b38f 100644 --- a/components-core/src/main/java/org/dllearner/utilities/statistics/TableColumn.java +++ b/components-core/src/main/java/org/dllearner/utilities/statistics/TableColumn.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.utilities.statistics; import java.io.File; @@ -30,9 +29,9 @@ public class TableColumn implements Serializable { - private static final long serialVersionUID = 1l; + private static final long serialVersionUID = 1L; private String header; - private List entries = new ArrayList(); + private List entries = new ArrayList<>(); public TableColumn() { super(); diff --git a/components-core/src/main/resources/components.ini b/components-core/src/main/resources/components.ini deleted file mode 100644 index 76db8a681f..0000000000 --- a/components-core/src/main/resources/components.ini +++ /dev/null @@ -1,31 +0,0 @@ -// list of all components DL-Learner should use -// (if you implement your own components add them here) -# knowledge sources -org.dllearner.kb.OWLFile -org.dllearner.kb.KBFile -org.dllearner.kb.sparql.SparqlKnowledgeSource -org.dllearner.kb.OWLAPIOntology -# reasoners -org.dllearner.reasoning.OWLAPIReasoner -org.dllearner.reasoning.fuzzydll.FuzzyOWLAPIReasoner -org.dllearner.reasoning.DIGReasoner -org.dllearner.reasoning.FastRetrievalReasoner -org.dllearner.reasoning.FastInstanceChecker -org.dllearner.reasoning.ProtegeReasoner -org.dllearner.reasoning.PelletReasoner -# learning problems -org.dllearner.learningproblems.PosNegLPStandard -org.dllearner.learningproblems.PosNegLPStrict -org.dllearner.learningproblems.PosOnlyLP -org.dllearner.learningproblems.ClassLearningProblem -# learning algorithms -org.dllearner.algorithms.RandomGuesser -org.dllearner.algorithms.BruteForceLearner -org.dllearner.algorithms.refinement.ROLearner -org.dllearner.algorithms.refinement2.ROLComponent2 -org.dllearner.algorithms.gp.GP -org.dllearner.algorithms.el.ELLearningAlgorithm -org.dllearner.algorithms.el.ELLearningAlgorithmDisjunctive -org.dllearner.algorithms.celoe.CELOE -org.dllearner.algorithms.fuzzydll.FuzzyCELOE -org.dllearner.algorithms.isle.ISLE diff --git a/components-core/src/site/apt/format.apt b/components-core/src/site/apt/format.apt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/components-core/src/site/apt/index.apt b/components-core/src/site/apt/index.apt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/components-core/src/site/site.xml b/components-core/src/site/site.xml new file mode 100644 index 0000000000..43f53f872c --- /dev/null +++ b/components-core/src/site/site.xml @@ -0,0 +1,31 @@ + + + + + + org.apache.maven.skins + maven-fluido-skin + 1.4 + + + + true + + + + + DL-Learner + DL-Learner + https://raw.githubusercontent.com/AKSW/DL-Learner/develop/images/logos/D_quadratic.png + http://dl-learner.org/ + 1cm + + + +

+ + + + diff --git a/components-core/src/test/java/org/dllearner/algorithms/pattern/OWLAxiomRenamerTest.java b/components-core/src/test/java/org/dllearner/algorithms/pattern/OWLAxiomRenamerTest.java index 999d5bf40c..68a042d959 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/pattern/OWLAxiomRenamerTest.java +++ b/components-core/src/test/java/org/dllearner/algorithms/pattern/OWLAxiomRenamerTest.java @@ -1,28 +1,35 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.pattern; -import static org.junit.Assert.assertEquals; - +import com.google.common.collect.HashMultiset; +import com.google.common.collect.Multiset; +import org.dllearner.core.StringRenderer; +import org.dllearner.core.StringRenderer.Rendering; import org.junit.Before; import org.junit.Test; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.io.ToStringRenderer; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLObjectProperty; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyCreationException; -import org.semanticweb.owlapi.model.OWLOntologyManager; -import org.semanticweb.owlapi.model.PrefixManager; +import org.semanticweb.owlapi.model.*; import org.semanticweb.owlapi.util.DefaultPrefixManager; - import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; -import uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntaxObjectRenderer; -import com.google.common.collect.HashMultiset; -import com.google.common.collect.Multiset; +import static org.junit.Assert.assertEquals; public class OWLAxiomRenamerTest { private OWLDataFactory df; @@ -40,7 +47,8 @@ public class OWLAxiomRenamerTest { @Before public void setUp() throws Exception { df = new OWLDataFactoryImpl(); - PrefixManager pm = new DefaultPrefixManager("http://examples.org/ontology#"); + PrefixManager pm = new DefaultPrefixManager(); + pm.setDefaultPrefix("http://examples.org/ontology#"); clsA = df.getOWLClass("A", pm); clsB = df.getOWLClass("B", pm); @@ -57,7 +65,7 @@ public void setUp() throws Exception { @Test public void testRename() { - ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); + StringRenderer.setRenderer(Rendering.DL_SYNTAX); OWLAxiom ax1 = df.getOWLSubClassOfAxiom(clsA, clsB); OWLAxiom ax2 = df.getOWLSubClassOfAxiom(clsB, clsC); diff --git a/components-core/src/test/java/org/dllearner/algorithms/pattern/OWLPatternDetectionTest.java b/components-core/src/test/java/org/dllearner/algorithms/pattern/OWLPatternDetectionTest.java index 4805487049..2294b40b89 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/pattern/OWLPatternDetectionTest.java +++ b/components-core/src/test/java/org/dllearner/algorithms/pattern/OWLPatternDetectionTest.java @@ -1,19 +1,36 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.pattern; +import org.dllearner.core.StringRenderer; +import org.dllearner.core.StringRenderer.Rendering; import org.dllearner.kb.repository.OntologyRepository; import org.dllearner.kb.repository.bioportal.BioPortalRepository; import org.dllearner.kb.repository.tones.TONESRepository; import org.junit.Test; -import org.semanticweb.owlapi.io.ToStringRenderer; - -import uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntaxObjectRenderer; public class OWLPatternDetectionTest { // @Before public void setUp() throws Exception { - ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); + StringRenderer.setRenderer(Rendering.DL_SYNTAX); } // @Test diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/QTLTest.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/QTLTest.java index 8974e60b4f..092efd367b 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/qtl/QTLTest.java +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/QTLTest.java @@ -1,5 +1,24 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl; +import java.io.File; import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit; @@ -15,6 +34,7 @@ import org.dllearner.algorithms.qtl.impl.QueryTreeFactory; import org.dllearner.algorithms.qtl.impl.QueryTreeFactoryBase; import org.dllearner.algorithms.qtl.util.Entailment; +import org.dllearner.algorithms.qtl.util.PrefixCCPrefixMapping; import org.dllearner.algorithms.qtl.util.StopURIsDBpedia; import org.dllearner.algorithms.qtl.util.StopURIsOWL; import org.dllearner.algorithms.qtl.util.StopURIsRDFS; @@ -175,6 +195,8 @@ public static void main(String[] args) throws Exception { PredicateExistenceFilterDBpedia filter = new PredicateExistenceFilterDBpedia(null); System.out.println(filter.filter(bestSolution).getStringRepresentation()); System.out.println(QueryTreeUtils.toSPARQLQueryString(filter.filter(bestSolution))); + + QueryTreeUtils.asGraph(bestSolution, null, PrefixCCPrefixMapping.Full, new File("/tmp/tree.graphml")); } } diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/TreeSubsumptionTest.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/TreeSubsumptionTest.java index 59f2cceffa..80764d8cd5 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/qtl/TreeSubsumptionTest.java +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/TreeSubsumptionTest.java @@ -1,8 +1,8 @@ /** - * Copyright (C) 2007-2010, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. - * + * * DL-Learner is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or @@ -15,7 +15,6 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * */ package org.dllearner.algorithms.qtl; diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/examples/DBpediaExample.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/examples/DBpediaExample.java index e4a624dc3a..0f7ab19e59 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/qtl/examples/DBpediaExample.java +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/examples/DBpediaExample.java @@ -1,8 +1,8 @@ /** - * Copyright (C) 2007-2010, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. - * + * * DL-Learner is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or @@ -15,7 +15,6 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * */ package org.dllearner.algorithms.qtl.examples; diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/examples/LinkedGeoDataExample.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/examples/LinkedGeoDataExample.java index a3562d4e3d..b5847cfa6a 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/qtl/examples/LinkedGeoDataExample.java +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/examples/LinkedGeoDataExample.java @@ -1,8 +1,8 @@ /** - * Copyright (C) 2007-2010, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. - * + * * DL-Learner is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or @@ -15,7 +15,6 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * */ package org.dllearner.algorithms.qtl.examples; diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/BenchmarkDescriptionGeneratorHTML.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/BenchmarkDescriptionGeneratorHTML.java new file mode 100644 index 0000000000..7d33db8ddb --- /dev/null +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/BenchmarkDescriptionGeneratorHTML.java @@ -0,0 +1,228 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.algorithms.qtl.experiments; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.aksw.jena_sparql_api.core.QueryExecutionFactory; +import org.aksw.jena_sparql_api.http.QueryExecutionFactoryHttp; +import org.aksw.jena_sparql_api.pagination.core.QueryExecutionFactoryPaginated; + +import com.google.common.base.Charsets; +import com.google.common.io.Files; +import com.hp.hpl.jena.query.Query; +import com.hp.hpl.jena.query.QueryExecution; +import com.hp.hpl.jena.query.QueryFactory; +import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.sparql.core.Var; + +/** + * @author Lorenz Buehmann + * + */ +public class BenchmarkDescriptionGeneratorHTML { + + String style = + "\n" + + "\n" + + "" + + "\n" + + ""; + + String style2 = + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n"; + + + private QueryExecutionFactory qef; + + public BenchmarkDescriptionGeneratorHTML(QueryExecutionFactory qef) { + this.qef = qef; + } + + private List loadQueries(File queriesFile) throws IOException { + List queries = new ArrayList<>(); + + for (String queryString : Files.readLines(queriesFile, Charsets.UTF_8)) { + Query q = QueryFactory.create(queryString); + queries.add(q); + } + return queries; + } + + public void generateBenchmarkDescription(File benchmarkQueriesFile, File htmlOutputFile) throws Exception{ + List queries = loadQueries(benchmarkQueriesFile); + + Var var = Var.alloc("s"); + String html = "\n"; + html += style2; + html += "\n"; + html += "\n"; + // table header + html += "" + + "" + + "" + + "" + + "" + + "\n"; + + html += "\n"; + int id = 1; + for (Query query : queries) { + html += "\n"; + + // 1. column: ID + html += "\n"; + + // 2. column: SPARQL query + html += "\n"; + + // 3. column: depth + int depth = org.dllearner.utilities.QueryUtils.getSubjectObjectJoinDepth(query, var) + 1; + html += "\n"; + + // 4. column: #instances + int nrOfInstances = 0; + QueryExecution qe = qef.createQueryExecution(query); + ResultSet rs = qe.execSelect(); + while(rs.hasNext()) { + rs.next(); + nrOfInstances++; + } + qe.close(); + html += "\n"; + + html += "\n"; + } + html += "\n"; + html += "
IDQueryDepth#Instances
" + id++ + "
" + query.toString().replace("<", "<").replace(">", ">") + "
" + depth + "" + nrOfInstances + "
\n"; + html += "\n"; + html += "\n"; + + try { + Files.write(html, htmlOutputFile, Charsets.UTF_8); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) throws Exception{ + if(args.length < 4) { + System.out.println("Usage: BenchmarkDescriptionGeneratorHTML "); + System.exit(0); + } + File source = new File(args[0]); + File target = new File(args[1]); + String endpointURL = args[2]; + String defaultGraph = args[3]; + + QueryExecutionFactory qef = new QueryExecutionFactoryHttp(endpointURL, defaultGraph); + qef = new QueryExecutionFactoryPaginated(qef); + BenchmarkDescriptionGeneratorHTML generator = new BenchmarkDescriptionGeneratorHTML(qef); + generator.generateBenchmarkDescription(source, target); + } + +} diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/DBpediaEvaluationDataset.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/DBpediaEvaluationDataset.java index 4594b668d3..8788922400 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/DBpediaEvaluationDataset.java +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/DBpediaEvaluationDataset.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.experiments; @@ -34,26 +49,12 @@ */ public class DBpediaEvaluationDataset extends EvaluationDataset { - static SparqlEndpoint endpoint; - - static { - try { - endpoint = SparqlEndpoint.getEndpointDBpedia(); - endpoint = new SparqlEndpoint( -// new URL("http://akswnc3.informatik.uni-leipzig.de:8860/sparql"), - new URL("http://sake.informatik.uni-leipzig.de:8890/sparql"), - "http://dbpedia.org"); - } catch (Exception e) { - e.printStackTrace(); - } - } - - - public DBpediaEvaluationDataset() { + public DBpediaEvaluationDataset(File benchmarkDirectory, SparqlEndpoint endpoint) { // set KS + File cacheDir = new File(benchmarkDirectory, "cache"); try { ks = new SparqlEndpointKS(endpoint); - ks.setCacheDir("./cache-qtl/qtl-qald-iswc2015-cache;mv_store=false"); + ks.setCacheDir(cacheDir.getAbsolutePath() + "/sparql/qtl-AAAI-cache;mv_store=false"); ks.init(); } catch (ComponentInitException e) { e.printStackTrace(); @@ -83,8 +84,9 @@ public DBpediaEvaluationDataset() { prefixMapping.setNsPrefix("schema", "http://schema.org/"); } + @SuppressWarnings("unchecked") public List> getQueryTreeFilters() { - return Lists.>newArrayList( + return Lists.newArrayList( new PredicateDropStatementFilter(StopURIsDBpedia.get()), new ObjectDropStatementFilter(StopURIsDBpedia.get()), new PredicateDropStatementFilter(StopURIsRDFS.get()), diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/DBpediaLearningProblemsGenerator.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/DBpediaLearningProblemsGenerator.java index c4388d5b63..38af6a5c22 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/DBpediaLearningProblemsGenerator.java +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/DBpediaLearningProblemsGenerator.java @@ -1,188 +1,55 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.experiments; -import java.io.File; -import java.io.FileInputStream; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.Random; -import java.util.Set; -import java.util.concurrent.ArrayBlockingQueue; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Future; -import java.util.concurrent.RunnableFuture; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - -import org.apache.commons.math3.random.JDKRandomGenerator; -import org.dllearner.kb.SparqlEndpointKS; -import org.dllearner.kb.sparql.ConciseBoundedDescriptionGenerator; -import org.dllearner.kb.sparql.ConciseBoundedDescriptionGeneratorImpl; +import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; +import org.apache.jena.riot.Lang; import org.dllearner.kb.sparql.SparqlEndpoint; -import org.dllearner.reasoning.SPARQLReasoner; -import org.semanticweb.owlapi.model.OWLClass; -import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.ModelFactory; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; /** * Generate learning problems based on the DBpedia knowledge base. * @author Lorenz Buehmann * */ -public class DBpediaLearningProblemsGenerator { - - SparqlEndpoint endpoint; - SparqlEndpointKS ks; - SPARQLReasoner reasoner; - ConciseBoundedDescriptionGenerator cbdGen; - - File dataDir; - private Model schema; - private File benchmarkDirectory; - private int threadCount; +public class DBpediaLearningProblemsGenerator extends SPARQLLearningProblemsGenerator { - public DBpediaLearningProblemsGenerator(File benchmarkDirectory, int threadCount) throws Exception { - this.benchmarkDirectory = benchmarkDirectory; - this.threadCount = threadCount; - - endpoint = SparqlEndpoint.create("http://sake.informatik.uni-leipzig.de:8890/sparql", "http://dbpedia.org"); - - ks = new SparqlEndpointKS(endpoint); - ks.setCacheDir(new File(benchmarkDirectory, "cache").getPath() + ";mv_store=false"); - ks.setPageSize(50000); - ks.setUseCache(true); - ks.setQueryDelay(100); - ks.init(); - - reasoner = new SPARQLReasoner(ks); - reasoner.init(); - - cbdGen = new ConciseBoundedDescriptionGeneratorImpl(ks.getQueryExecutionFactory()); - - dataDir = new File(benchmarkDirectory, "data/dbpedia/"); - dataDir.mkdirs(); - - schema = ModelFactory.createDefaultModel(); - schema.read(new FileInputStream(new File(benchmarkDirectory, "dbpedia_2014.owl")), null, "RDF/XML"); - } + private static final String DBPEDIA_ONTOLOGY_URL = "http://downloads.dbpedia.org/2014/dbpedia_2014.owl.bz2"; - private Set getClasses() { - return reasoner.getMostSpecificClasses(); + public DBpediaLearningProblemsGenerator(SparqlEndpoint endpoint, File benchmarkDirectory, int threadCount) throws Exception { + super(endpoint, benchmarkDirectory, threadCount); } - - - public void generateBenchmark(int nrOfSPARQLQueries, final int minDepth, final int maxDepth, int minNrOfExamples) { - Collection classes = getClasses(); - ArrayList classesList = new ArrayList<>(classes); - Collections.shuffle(classesList, new Random(123)); - classes = classesList; -// classes = Sets.newHashSet(new OWLClassImpl(IRI.create("http://dbpedia.org/ontology/AcademicJournal"))); - - Iterator iterator = classes.iterator(); - -// ExecutorService tp = Executors.newFixedThreadPool(threadCount); - List> futures = new ArrayList>(); - List paths = new ArrayList(); - - ThreadPoolExecutor tp = new CustomFutureReturningExecutor( - threadCount, threadCount, - 5000L, TimeUnit.MILLISECONDS, - new ArrayBlockingQueue(classes.size(), true)); - - JDKRandomGenerator rndGen = new JDKRandomGenerator(); - rndGen.setSeed(123); - - int nrOfQueriesPerDepth = nrOfSPARQLQueries / (maxDepth - minDepth + 1); - - for(int depth = minDepth; depth <= maxDepth; depth++) { - System.out.println("Generating " + nrOfQueriesPerDepth + " queries for depth " + depth); - List pathsForDepth = new ArrayList(); - // generate paths of depths <= maxDepth - while(pathsForDepth.size() < nrOfQueriesPerDepth && iterator.hasNext()) { - - // pick next class - OWLClass cls = iterator.next(); - -// int depth = rndGen.nextInt(maxDepth) + 1; - - Future future = tp.submit(new PathDetectionTask(dataDir, ks, schema, cls, depth, minNrOfExamples)); -// futures.add(future); - try { - Path path = future.get(); - if(path != null) { - pathsForDepth.add(path); - } - } catch (InterruptedException | ExecutionException e) { - e.printStackTrace(); - } - } - paths.addAll(pathsForDepth); - } - - -// for (Future future : futures) { -// try { -// Path path = future.get(); -// if(path != null) { -// paths.add(path); -// } -// if(paths.size() == nrOfSPARQLQueries) { -// System.err.println("Benchmark generation finished. Stopping all running threads."); -// tp.shutdownNow(); -// } -// } catch (InterruptedException | ExecutionException e) { -// e.printStackTrace(); -// } -// if(paths.size() == nrOfSPARQLQueries) { -// System.err.println("Benchmark generation finished. Stopping all running threads."); -// tp.shutdownNow(); -// } -// } - - tp.shutdownNow(); - try { - tp.awaitTermination(1, TimeUnit.HOURS); - } catch (InterruptedException e) { - // TODO Auto-generated catch block + + @Override + protected void loadSchema() { + try(InputStream is = new BZip2CompressorInputStream(new URL(DBPEDIA_ONTOLOGY_URL).openStream())){ + schema.read(is, null, Lang.RDFXML.getName()); + } catch (IOException e) { e.printStackTrace(); } -// try { -// tp.awaitTermination(1, TimeUnit.DAYS); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } - - for (Path path : paths) { - System.out.println(path); - } } - - - class CustomFutureReturningExecutor extends ThreadPoolExecutor { - - public CustomFutureReturningExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue) { - super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); - } - @Override - protected RunnableFuture newTaskFor(Callable callable) { - if (callable instanceof PathDetectionTask) { - return ((PathDetectionTask) callable).newTask(); - } else { - return super.newTaskFor(callable); // A regular Callable, delegate to parent - } - } - } - - public static void main(String[] args) throws Exception { File benchmarkBaseDirectory = new File(args[0]); int threadCount = Integer.parseInt(args[1]); @@ -190,8 +57,11 @@ public static void main(String[] args) throws Exception { int minDepth = Integer.parseInt(args[3]); int maxDepth = Integer.parseInt(args[4]); int minNrOfExamples = Integer.parseInt(args[5]); - - DBpediaLearningProblemsGenerator generator = new DBpediaLearningProblemsGenerator(benchmarkBaseDirectory, threadCount); + + SparqlEndpoint endpoint = SparqlEndpoint.create("http://dbpedia.org/sparql", "http://dbpedia.org"); +// SparqlEndpoint endpoint = SparqlEndpoint.create("http://sake.informatik.uni-leipzig.de:8890/sparql", "http://dbpedia.org"); + + DBpediaLearningProblemsGenerator generator = new DBpediaLearningProblemsGenerator(endpoint, benchmarkBaseDirectory, threadCount); generator.generateBenchmark(nrOfSPARQLQueries, minDepth, maxDepth, minNrOfExamples); } diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/Diagrams.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/Diagrams.java index cbfe37f603..9a240357f7 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/Diagrams.java +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/Diagrams.java @@ -1,21 +1,36 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.experiments; import java.io.File; -import java.math.BigDecimal; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.Arrays; -import java.util.List; import java.util.Map; +import java.util.Map.Entry; +import java.util.NavigableMap; import java.util.Properties; +import java.util.TreeMap; -import org.dllearner.algorithms.qtl.heuristics.QueryTreeHeuristic; -import org.dllearner.algorithms.qtl.heuristics.QueryTreeHeuristicSimple; +import org.apache.commons.lang3.ArrayUtils; import org.dllearner.learningproblems.Heuristics.HeuristicType; import com.google.common.base.Charsets; @@ -24,6 +39,11 @@ import com.google.common.io.Files; import com.google.common.primitives.Doubles; import com.google.common.primitives.Ints; +import com.panayotis.gnuplot.JavaPlot; +import com.panayotis.gnuplot.JavaPlot.Key; +import com.panayotis.gnuplot.plot.DataSetPlot; +import com.panayotis.gnuplot.style.PlotStyle; +import com.panayotis.gnuplot.style.Style; /** * @author Lorenz Buehmann @@ -50,9 +70,9 @@ public static void main(String[] args) throws Exception { int[] nrOfExamplesIntervals = { 5, 10, - 15, +// 15, 20, - 25, +// 25, 30 }; @@ -61,7 +81,7 @@ public static void main(String[] args) throws Exception { 0.1, 0.2, 0.3, - 0.4, +// 0.4, // 0.6 }; @@ -69,11 +89,17 @@ public static void main(String[] args) throws Exception { measure2ColumnName.put(HeuristicType.FMEASURE, "avg_fscore_best_returned"); measure2ColumnName.put(HeuristicType.PRED_ACC, "avg_predacc_best_returned"); measure2ColumnName.put(HeuristicType.MATTHEWS_CORRELATION, "avg_mathcorr_best_returned"); + HeuristicType[] measures = { HeuristicType.PRED_ACC, HeuristicType.FMEASURE, HeuristicType.MATTHEWS_CORRELATION}; + String[] labels = { + "A_1", + "F_1", + "MCC"}; + // get distinct noise intervals // |E| vs fscore @@ -97,32 +123,151 @@ public static void main(String[] args) throws Exception { } s += "\n"; } - Files.write(s, new File(dir, "examplesVsFscore-" + noise + ".tsv"), Charsets.UTF_8); + Files.write(s, new File(dir, "examplesVsScore-" + noise + ".tsv"), Charsets.UTF_8); } // noise vs fscore sql = "SELECT noise,%s from eval_overall WHERE heuristic_measure = ? && nrOfExamples = ?"; + + NavigableMap> input = new TreeMap<>(); for (int nrOfExamples : nrOfExamplesIntervals) { String s = ""; s += "\t"; s += Joiner.on("\t").join(Doubles.asList(noiseIntervals)); s += "\n"; - for (HeuristicType measure : measures) { - ps = conn.prepareStatement(String.format(sql, measure2ColumnName.get(measure))); - ps.setString(1, measure.toString()); - ps.setInt(2, nrOfExamples); - ResultSet rs = ps.executeQuery(); - s+= measure; - while(rs.next()) { - double noise = rs.getDouble(1); - double avgFscore = rs.getDouble(2); - s += "\t" + avgFscore; - } - s += "\n"; + + String gnuplot = ""; + + // F-score + ps = conn.prepareStatement( + "SELECT noise,avg_fscore_best_returned from eval_overall WHERE heuristic_measure = 'FMEASURE' && nrOfExamples = ?"); + ps.setInt(1, nrOfExamples); + ResultSet rs = ps.executeQuery(); + gnuplot += "\"F_1\"\n"; + while (rs.next()) { + double noise = rs.getDouble(1); + double avgFscore = rs.getDouble(2); + gnuplot += noise + "," + avgFscore + "\n"; + } + + // precision + gnuplot += "\n\n"; + ps = conn.prepareStatement( + "SELECT noise,avg_precision_best_returned from eval_overall WHERE heuristic_measure = 'FMEASURE' && nrOfExamples = ?"); + ps.setInt(1, nrOfExamples); + rs = ps.executeQuery(); + gnuplot += "\"precision\"\n"; + while (rs.next()) { + double noise = rs.getDouble(1); + double avgFscore = rs.getDouble(2); + gnuplot += noise + "," + avgFscore + "\n"; + } + + // recall + gnuplot += "\n\n"; + ps = conn.prepareStatement( + "SELECT noise,avg_recall_best_returned from eval_overall WHERE heuristic_measure = 'FMEASURE' && nrOfExamples = ?"); + ps.setInt(1, nrOfExamples); + rs = ps.executeQuery(); + gnuplot += "\"recall\"\n"; + while (rs.next()) { + double noise = rs.getDouble(1); + double avgFscore = rs.getDouble(2); + gnuplot += noise + "," + avgFscore + "\n"; + } + + // MCC + gnuplot += "\n\n"; + ps = conn.prepareStatement( + "SELECT noise,avg_mathcorr_best_returned from eval_overall WHERE heuristic_measure = 'MATTHEWS_CORRELATION' && nrOfExamples = ?"); + ps.setInt(1, nrOfExamples); + rs = ps.executeQuery(); + gnuplot += "\"MCC\"\n"; + while (rs.next()) { + double noise = rs.getDouble(1); + double avgFscore = rs.getDouble(2); + gnuplot += noise + "," + avgFscore + "\n"; + } + + + // baseline F-score + gnuplot += "\n\n"; + ps = conn.prepareStatement("SELECT noise,avg_fscore_baseline from eval_overall WHERE heuristic_measure = 'FMEASURE' && nrOfExamples = ?"); + ps.setInt(1, nrOfExamples); + rs = ps.executeQuery(); + gnuplot += "\"baseline F_1\"\n"; + while(rs.next()) { + double noise = rs.getDouble(1); + double avgFscore = rs.getDouble(2); + gnuplot += noise + "," + avgFscore + "\n"; } - Files.write(s, new File(dir, "noiseVsFscore-" + nrOfExamples + ".tsv"), Charsets.UTF_8); + + // baseline MCC + gnuplot += "\n\n"; + ps = conn.prepareStatement( + "SELECT noise,avg_mathcorr_baseline from eval_overall WHERE heuristic_measure = 'MATTHEWS_CORRELATION' && nrOfExamples = ?"); + ps.setInt(1, nrOfExamples); + rs = ps.executeQuery(); + gnuplot += "\"baseline MCC\"\n"; + while (rs.next()) { + double noise = rs.getDouble(1); + double avgFscore = rs.getDouble(2); + gnuplot += noise + "," + avgFscore + "\n"; + } + + Files.write(gnuplot.trim(), new File(dir, "noiseVsScore-" + nrOfExamples + ".dat"), Charsets.UTF_8); + } + if(!input.isEmpty()) { +// plotNoiseVsFscore(input); } } + + public static void plotNoiseVsFscore(NavigableMap> input) { + JavaPlot p = new JavaPlot(); + p.set("xlabel", "'Noise'"); + p.set("ylabel", "'Objective Function'"); + p.set("xtics", "0,.1,.4"); + p.set("ytics", "0,.2,1"); + p.set("xrange", "[0:.4]"); + + // last element + Entry> lastEntry = input.lastEntry(); + + + for (Entry> entry : input.entrySet()) { + Integer nrOfExamples = entry.getKey(); + + Map h2data = entry.getValue(); + int pointStyle = 5; + for (Entry entry2 : h2data.entrySet()) { + HeuristicType heuristic = entry2.getKey(); + double[][] data = entry2.getValue(); + + PlotStyle myPlotStyle = new PlotStyle(); + myPlotStyle.setStyle(Style.LINESPOINTS); + myPlotStyle.setLineWidth(1); + myPlotStyle.setPointType(pointStyle); + pointStyle += 2; + + DataSetPlot s = new DataSetPlot(data); + s.setPlotStyle(myPlotStyle); + s.setTitle(heuristic.name()); + p.addPlot(s); + + } + + if(entry.equals(lastEntry)) { + p.setKey(Key.OUTSIDE); + } else { + p.setKey(Key.OFF); + } + p.setTitle(nrOfExamples.toString()); + p.newGraph(); + } + p.newGraph(); + + p.plot(); + } } diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/EvaluationDataset.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/EvaluationDataset.java index d52338a69b..e4abd5c2f5 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/EvaluationDataset.java +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/EvaluationDataset.java @@ -1,12 +1,25 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.experiments; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Set; import org.dllearner.core.AbstractReasonerComponent; import org.dllearner.kb.SparqlEndpointKS; @@ -29,7 +42,7 @@ public class EvaluationDataset { AbstractReasonerComponent reasoner; List sparqlQueries; - List> queryTreeFilters = new ArrayList>(); + List> queryTreeFilters = new ArrayList<>(); public SparqlEndpointKS getKS() { return ks; diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/ExamplesWrapper.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/ExamplesWrapper.java new file mode 100644 index 0000000000..36388820e5 --- /dev/null +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/ExamplesWrapper.java @@ -0,0 +1,44 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.algorithms.qtl.experiments; + +import java.util.List; +import java.util.SortedMap; + +import org.dllearner.algorithms.qtl.datastructures.impl.RDFResourceTree; +import org.semanticweb.owlapi.model.OWLIndividual; + +public class ExamplesWrapper { + List correctPosExamples; + List falsePosExamples; + List correctNegExamples; + SortedMap posExamplesMapping; + SortedMap negExamplesMapping; + + public ExamplesWrapper(List correctPosExamples, + List falsePosExamples, List correctNegExamples, + SortedMap posExamplesMapping, + SortedMap negExamplesMapping) { + this.correctPosExamples = correctPosExamples; + this.falsePosExamples = falsePosExamples; + this.correctNegExamples = correctNegExamples; + this.posExamplesMapping = posExamplesMapping; + this.negExamplesMapping = negExamplesMapping; + } +} \ No newline at end of file diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/NegativeExampleSPARQLQueryGenerator.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/NegativeExampleSPARQLQueryGenerator.java new file mode 100644 index 0000000000..9d0d6fb0c9 --- /dev/null +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/NegativeExampleSPARQLQueryGenerator.java @@ -0,0 +1,253 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.algorithms.qtl.experiments; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; +import java.util.Stack; + +import org.aksw.jena_sparql_api.core.QueryExecutionFactory; +import org.dllearner.utilities.QueryUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.collect.Sets; +import com.google.common.collect.Sets.SetView; +import com.hp.hpl.jena.graph.Node; +import com.hp.hpl.jena.graph.Triple; +import com.hp.hpl.jena.query.Query; +import com.hp.hpl.jena.query.QueryExecution; +import com.hp.hpl.jena.query.QueryFactory; +import com.hp.hpl.jena.query.QuerySolution; +import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.sparql.core.TriplePath; +import com.hp.hpl.jena.sparql.expr.E_NotExists; +import com.hp.hpl.jena.sparql.syntax.Element; +import com.hp.hpl.jena.sparql.syntax.ElementFilter; +import com.hp.hpl.jena.sparql.syntax.ElementGroup; +import com.hp.hpl.jena.sparql.syntax.ElementOptional; +import com.hp.hpl.jena.sparql.syntax.ElementPathBlock; +import com.hp.hpl.jena.sparql.syntax.ElementTriplesBlock; +import com.hp.hpl.jena.sparql.syntax.ElementUnion; +import com.hp.hpl.jena.sparql.syntax.ElementVisitorBase; +import com.hp.hpl.jena.vocabulary.RDF; + +public class NegativeExampleSPARQLQueryGenerator extends ElementVisitorBase{ + + + private static final Logger logger = LoggerFactory + .getLogger(NegativeExampleSPARQLQueryGenerator.class); + + private boolean inOptionalClause; + private Stack parentGroup = new Stack<>(); + private QueryUtils triplePatternExtractor = new QueryUtils(); + private Triple triple; + Random randomGen = new Random(123); + + private QueryExecutionFactory qef; + + public NegativeExampleSPARQLQueryGenerator(QueryExecutionFactory qef) { + this.qef = qef; + } + + public List getNegativeExamples(String targetQuery, int size) { + logger.trace("Generating neg. examples..."); + Set negExamples = new HashSet<>(); + + // remove triple patterns as long as enough neg. examples have been found + Query query = QueryFactory.create(targetQuery); + + // generate queries that return neg. examples + List queries = generateQueries(query); + + // get a list of resources for each query + for (Query q : queries) { + q.setLimit(size); + logger.info("Trying query\n" + q); + QueryExecution qe = qef.createQueryExecution(q); + ResultSet rs = qe.execSelect(); + while(rs.hasNext()) { + QuerySolution qs = rs.next(); + String example = qs.getResource(query.getProjectVars().get(0).getName()).getURI(); + negExamples.add(example); + } + qe.close(); + } + logger.trace("...finished generating neg. examples."); + return new ArrayList<>(negExamples); + } + + private ElementFilter getNotExistsFilter(Element el){ + return new ElementFilter(new E_NotExists(el)); + } + + private List generateQueries(Query query) { + List queries = new ArrayList<>(); + + // extract paths + Node source = query.getProjectVars().get(0).asNode(); + Set> paths = getPaths(new ArrayList(), query, source); + + // for each path create query which excludes the path by FILTER NOT EXISTS + Set>> pathSubsets = Sets.powerSet(paths); + + for (Set> pathSubset : pathSubsets) { + if(!pathSubset.isEmpty() && pathSubset.size() < paths.size()) { + + ElementGroup eg = new ElementGroup(); + + // keep other paths + ElementTriplesBlock existsBlock = new ElementTriplesBlock(); + eg.addElement(existsBlock); + SetView> difference = Sets.difference(paths, pathSubset); + for(List otherPath : difference) { + for (Triple tp : otherPath) { + existsBlock.addTriple(tp); + } + } + + // not exists current path + ElementTriplesBlock notExistsBlock = new ElementTriplesBlock(); + for(List path : pathSubset) { + for (Triple tp : path) { + notExistsBlock.addTriple(tp); + } + } + ElementGroup notExistsGroup = new ElementGroup(); + notExistsGroup.addElement(notExistsBlock); + eg.addElementFilter(getNotExistsFilter(notExistsGroup)); + + Query newQuery = QueryFactory.create(); + newQuery.setQuerySelectType(); + newQuery.setQueryPattern(eg); + newQuery.addProjectVars(query.getProjectVars()); + newQuery.setDistinct(true); + queries.add(newQuery); + } + } + + return queries; + } + + private Set> getPaths(List path, Query query, Node source) { + Set> paths = new LinkedHashSet<>(); + Set outgoingTriplePatterns = QueryUtils.getOutgoingTriplePatterns(query, source); + for (Triple tp : outgoingTriplePatterns) { + List newPath = new ArrayList<>(path); + newPath.add(tp); + if(tp.getObject().isVariable()) { + paths.addAll(getPaths(newPath, query, tp.getObject())); + } else { + paths.add(newPath); + } + } + return paths; + } + + /** + * Returns a modified SPARQL query such that it is similar but different by choosing one of the triple patterns and use + * the negation of its existence. + * @param query the SPARQL query + */ + public Query generateSPARQLQuery(Query query){ + //choose a random triple for the modification + List triplePatterns = new ArrayList<>(triplePatternExtractor.extractTriplePattern(query)); + Collections.shuffle(triplePatterns, randomGen); + triple = triplePatterns.get(0); + + Query modifiedQuery = query.cloneQuery(); + modifiedQuery.getQueryPattern().visit(this); + logger.info("Negative examples query:\n" + modifiedQuery.toString()); + return modifiedQuery; + } + + @Override + public void visit(ElementGroup el) { + parentGroup.push(el); + for (Element e : new ArrayList<>(el.getElements())) { + e.visit(this); + } + parentGroup.pop(); + } + + @Override + public void visit(ElementOptional el) { + inOptionalClause = true; + el.getOptionalElement().visit(this); + inOptionalClause = false; + } + + @Override + public void visit(ElementTriplesBlock el) { + for (Iterator iterator = el.patternElts(); iterator.hasNext();) { + Triple t = iterator.next(); + if(inOptionalClause){ + + } else { + if(t.equals(triple)){ + ElementGroup parent = parentGroup.peek(); + ElementTriplesBlock elementTriplesBlock = new ElementTriplesBlock(); + elementTriplesBlock.addTriple(t); + ElementGroup eg = new ElementGroup(); + eg.addElement(elementTriplesBlock); + parent.addElement(new ElementFilter(new E_NotExists(eg))); + iterator.remove(); + } + } + } + } + + @Override + public void visit(ElementPathBlock el) { + for (Iterator iterator = el.patternElts(); iterator.hasNext();) { + TriplePath tp = iterator.next(); + if(inOptionalClause){ + + } else { + if(tp.asTriple().equals(triple)){ + ElementGroup parent = parentGroup.peek(); + ElementPathBlock elementTriplesBlock = new ElementPathBlock(); + elementTriplesBlock.addTriple(tp); + ElementGroup eg = new ElementGroup(); + eg.addElement(elementTriplesBlock); + parent.addElement(new ElementFilter(new E_NotExists(eg))); + iterator.remove(); + } + } + } + } + + @Override + public void visit(ElementUnion el) { + for (Element e : el.getElements()) { + e.visit(this); + } + } + + @Override + public void visit(ElementFilter el) { + } + + } \ No newline at end of file diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/Path.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/Path.java index f16d269675..46b4f52bad 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/Path.java +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/Path.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.experiments; import java.util.List; diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/PathDetectionTask.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/PathDetectionTask.java index 15774c2dd7..3d146c49ad 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/PathDetectionTask.java +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/PathDetectionTask.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.experiments; import java.io.File; @@ -79,12 +97,14 @@ public Path call() throws Exception { lines = Files.readLines(file, Charsets.UTF_8); ArrayList split = Lists.newArrayList(Splitter.on("\t").split(lines.get(0))); String object = split.remove(split.size() - 1); - List> propertyClusters = new ArrayList>(); - Set cluster = new TreeSet(); + List> propertyClusters = new ArrayList<>(); + for (String clusterString : split) { - for (String property : Splitter.on(",").split(clusterString)) { - cluster.add(property); + Set cluster = new TreeSet<>(); + for (String property : Splitter.on(",").trimResults().split(clusterString)) { + cluster.add(property.replace("[", "").replace("]", "")); } + propertyClusters.add(cluster); } return new Path(cls, propertyClusters, object); } catch (IOException e) { @@ -94,7 +114,7 @@ public Path call() throws Exception { // load data System.out.println(Thread.currentThread().getId() + ":" + "Loading data of depth " + depth + " for " + cls.toStringID() + "..."); long s = System.currentTimeMillis(); - Model data = loadDataFromCacheOrCompute(cls, depth, true); + Model data = ModelFactory.createDefaultModel();//loadDataFromCacheOrCompute(cls, depth, true); System.out.println(Thread.currentThread().getId() + ":" + "Got " + data.size() + " triples for " + cls.toStringID() + " in " + (System.currentTimeMillis() - s) + "ms"); // analyze @@ -182,7 +202,7 @@ private Model loadDataFromEndpointBatch(OWLClass cls, int maxDepth) { query += String.format("optional{?o%d ?p%d ?o%d .", i-1, i, i); } for(int i = 1; i < maxDepth; i++) { - query += String.format("}"); + query += "}"; } query += "}"; QueryExecutionFactory qef = new QueryExecutionFactoryPaginated(ks.getQueryExecutionFactory(), 500000); @@ -193,7 +213,7 @@ private Model loadDataFromEndpointBatch(OWLClass cls, int maxDepth) { } private List> getCooccuringPropertiesOnPath(Model model, OWLClass cls, List> propertiesOnPath, int clusterSize) { - List> properties = new ArrayList>(); + List> properties = new ArrayList<>(); String query = "SELECT DISTINCT "; for(int i = 0; i < clusterSize; i++) { @@ -216,7 +236,7 @@ private List> getCooccuringPropertiesOnPath(Model model, OWLClass cl if(clusterSize > 1) { String filter = "FILTER("; - List conditions = new ArrayList(); + List conditions = new ArrayList<>(); for(int i = 0; i < clusterSize; i++) { for(int j = i + 1; j < clusterSize; j++) { conditions.add("(?p" + i + "!=" + "?p" + j + ")"); @@ -235,7 +255,8 @@ private List> getCooccuringPropertiesOnPath(Model model, OWLClass cl System.out.println(query); // System.out.println(query); - QueryExecution qe = new QueryExecutionFactoryModel(model).createQueryExecution(query); +// QueryExecution qe = new QueryExecutionFactoryModel(model).createQueryExecution(query); + QueryExecution qe = ks.getQueryExecutionFactory().createQueryExecution(query); ResultSet rs = qe.execSelect(); while(rs.hasNext()) { @@ -249,22 +270,22 @@ private List> getCooccuringPropertiesOnPath(Model model, OWLClass cl properties.add(propertyCluster); } - return new ArrayList>(new HashSet>(properties)); + return new ArrayList<>(new HashSet<>(properties)); } private Path findPathOfDepthN(OWLClass cls, Model model, int depth) { // generate possible property paths of length n - List>> paths = new ArrayList>>(); + List>> paths = new ArrayList<>(); paths.add(Lists.>newArrayList()); for (int i = 0; i < depth; i++) { - List>> pathsNew = new ArrayList>>(); + List>> pathsNew = new ArrayList<>(); for (List> path : paths) { int clusterSize = rndGen.nextInt(3) + 1; - List> propertyClusters = getCooccuringPropertiesOnPath(model, cls, path, depth == 1 ? clusterSize : 1); + List> propertyClusters = getCooccuringPropertiesOnPath(model, cls, path, depth == 1 ? 2 : 1); for (Set propertyCluster : propertyClusters) { - List> newPath = new ArrayList>(path); + List> newPath = new ArrayList<>(path); newPath.add(propertyCluster); pathsNew.add(newPath); } diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/QTLEvaluation.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/QTLEvaluation.java index 6ca69844af..0b8fd97ffc 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/QTLEvaluation.java +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/QTLEvaluation.java @@ -1,40 +1,63 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.experiments; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.IOException; -import java.io.StringReader; -import java.math.BigDecimal; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.PreparedStatement; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.Random; -import java.util.Set; -import java.util.Stack; -import java.util.TreeSet; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; - +import com.google.common.base.Charsets; +import com.google.common.base.Function; +import com.google.common.base.Joiner; +import com.google.common.collect.*; +import com.google.common.hash.HashFunction; +import com.google.common.hash.Hashing; +import com.google.common.io.Files; +import com.hp.hpl.jena.datatypes.RDFDatatype; +import com.hp.hpl.jena.datatypes.xsd.XSDDatatype; +import com.hp.hpl.jena.graph.Node; +import com.hp.hpl.jena.graph.NodeFactory; +import com.hp.hpl.jena.graph.Triple; +import com.hp.hpl.jena.query.*; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; +import com.hp.hpl.jena.rdf.model.Resource; +import com.hp.hpl.jena.rdf.model.Statement; +import com.hp.hpl.jena.sparql.core.BasicPattern; +import com.hp.hpl.jena.sparql.core.Var; +import com.hp.hpl.jena.sparql.expr.*; +import com.hp.hpl.jena.sparql.expr.aggregate.AggCountVarDistinct; +import com.hp.hpl.jena.sparql.syntax.Element; +import com.hp.hpl.jena.sparql.syntax.ElementFilter; +import com.hp.hpl.jena.sparql.syntax.ElementGroup; +import com.hp.hpl.jena.sparql.syntax.ElementTriplesBlock; +import com.hp.hpl.jena.sparql.util.TripleComparator; +import com.hp.hpl.jena.util.iterator.Filter; +import com.hp.hpl.jena.vocabulary.RDF; +import com.jamonapi.MonitorFactory; +import joptsimple.OptionParser; +import joptsimple.OptionSet; +import joptsimple.OptionSpec; import org.aksw.jena_sparql_api.cache.core.QueryExecutionFactoryCacheEx; import org.aksw.jena_sparql_api.core.QueryExecutionFactory; +import org.apache.commons.collections15.ListUtils; +import org.apache.commons.lang3.time.DurationFormatUtils; +import org.apache.commons.mail.DefaultAuthenticator; +import org.apache.commons.mail.Email; +import org.apache.commons.mail.EmailException; +import org.apache.commons.mail.SimpleEmail; import org.apache.commons.math3.random.RandomDataGenerator; import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics; import org.apache.commons.math3.stat.descriptive.SynchronizedDescriptiveStatistics; @@ -50,83 +73,49 @@ import org.dllearner.algorithms.qtl.heuristics.QueryTreeHeuristic; import org.dllearner.algorithms.qtl.heuristics.QueryTreeHeuristicSimple; import org.dllearner.algorithms.qtl.impl.QueryTreeFactoryBase; +import org.dllearner.algorithms.qtl.operations.lgg.LGGGenerator; +import org.dllearner.algorithms.qtl.operations.lgg.LGGGeneratorSimple; import org.dllearner.algorithms.qtl.util.Entailment; import org.dllearner.algorithms.qtl.util.filters.PredicateExistenceFilter; import org.dllearner.algorithms.qtl.util.filters.PredicateExistenceFilterDBpedia; import org.dllearner.algorithms.qtl.util.statistics.TimeMonitors; import org.dllearner.core.ComponentAnn; import org.dllearner.core.ComponentInitException; +import org.dllearner.core.EvaluatedDescription; +import org.dllearner.core.StringRenderer; +import org.dllearner.core.StringRenderer.Rendering; import org.dllearner.kb.sparql.ConciseBoundedDescriptionGenerator; import org.dllearner.kb.sparql.ConciseBoundedDescriptionGeneratorImpl; +import org.dllearner.kb.sparql.SparqlEndpoint; import org.dllearner.learningproblems.Heuristics; import org.dllearner.learningproblems.Heuristics.HeuristicType; import org.dllearner.learningproblems.PosNegLPStandard; import org.dllearner.utilities.QueryUtils; -import org.semanticweb.owlapi.io.ToStringRenderer; +import org.semanticweb.owlapi.io.OWLObjectRenderer; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLIndividual; - import uk.ac.manchester.cs.owl.owlapi.OWLNamedIndividualImpl; -import uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntaxObjectRenderer; -import com.google.common.base.Charsets; -import com.google.common.collect.ComparisonChain; -import com.google.common.collect.HashMultimap; -import com.google.common.collect.Multimap; -import com.google.common.collect.Sets; -import com.google.common.hash.HashFunction; -import com.google.common.hash.Hashing; -import com.google.common.io.Files; -import com.hp.hpl.jena.datatypes.RDFDatatype; -import com.hp.hpl.jena.datatypes.xsd.XSDDatatype; -import com.hp.hpl.jena.datatypes.xsd.XSDDateTime; -import com.hp.hpl.jena.datatypes.xsd.impl.XSDAbstractDateTimeType; -import com.hp.hpl.jena.graph.Node; -import com.hp.hpl.jena.graph.NodeFactory; -import com.hp.hpl.jena.graph.Triple; -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.QueryFactory; -import com.hp.hpl.jena.query.QuerySolution; -import com.hp.hpl.jena.query.ResultSet; -import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.ModelFactory; -import com.hp.hpl.jena.rdf.model.Resource; -import com.hp.hpl.jena.rdf.model.Statement; -import com.hp.hpl.jena.sparql.core.BasicPattern; -import com.hp.hpl.jena.sparql.core.TriplePath; -import com.hp.hpl.jena.sparql.core.Var; -import com.hp.hpl.jena.sparql.expr.E_Equals; -import com.hp.hpl.jena.sparql.expr.E_LessThanOrEqual; -import com.hp.hpl.jena.sparql.expr.E_LogicalOr; -import com.hp.hpl.jena.sparql.expr.E_NotEquals; -import com.hp.hpl.jena.sparql.expr.E_NotExists; -import com.hp.hpl.jena.sparql.expr.E_NumAbs; -import com.hp.hpl.jena.sparql.expr.E_Str; -import com.hp.hpl.jena.sparql.expr.E_Subtract; -import com.hp.hpl.jena.sparql.expr.Expr; -import com.hp.hpl.jena.sparql.expr.ExprAggregator; -import com.hp.hpl.jena.sparql.expr.ExprVar; -import com.hp.hpl.jena.sparql.expr.NodeValue; -import com.hp.hpl.jena.sparql.expr.aggregate.AggCountVarDistinct; -import com.hp.hpl.jena.sparql.syntax.Element; -import com.hp.hpl.jena.sparql.syntax.ElementFilter; -import com.hp.hpl.jena.sparql.syntax.ElementGroup; -import com.hp.hpl.jena.sparql.syntax.ElementOptional; -import com.hp.hpl.jena.sparql.syntax.ElementPathBlock; -import com.hp.hpl.jena.sparql.syntax.ElementTriplesBlock; -import com.hp.hpl.jena.sparql.syntax.ElementUnion; -import com.hp.hpl.jena.sparql.syntax.ElementVisitorBase; -import com.hp.hpl.jena.sparql.syntax.ElementWalker; -import com.hp.hpl.jena.util.iterator.Filter; -import com.hp.hpl.jena.vocabulary.RDF; -import com.jamonapi.MonitorFactory; +import java.io.*; +import java.math.BigDecimal; +import java.net.URL; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.util.*; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; /** * @author Lorenz Buehmann * */ +@SuppressWarnings("unchecked") public class QTLEvaluation { private static final Logger logger = Logger.getLogger(QTLEvaluation.class.getName()); @@ -140,31 +129,18 @@ public class QTLEvaluation { "PREFIX rdfs: PREFIX owl: " + "SELECT ?sup WHERE {" + "?sub (rdfs:subClassOf|owl:equivalentClass)+ ?sup .}"); - + + private static final DecimalFormat dfPercent = new DecimalFormat("0.00%"); + enum NoiseMethod { RANDOM, SIMILAR, SIMILARITY_PARAMETERIZED } - NoiseMethod noiseMethod = NoiseMethod.RANDOM; - - static Map prefixes = new HashMap(); - static { - prefixes.put("sider", "http://www4.wiwiss.fu-berlin.de/sider/resource/sider/"); - prefixes.put("side_effects", "http://www4.wiwiss.fu-berlin.de/sider/resource/side_effects/"); - prefixes.put("drug", "http://www4.wiwiss.fu-berlin.de/drugbank/resource/drugbank/"); - prefixes.put("diseasome", "http://www4.wiwiss.fu-berlin.de/diseasome/resource/diseasome/"); - - prefixes.put("dbo", "http://dbpedia.org/ontology/"); - prefixes.put("dbpedia", "http://dbpedia.org/resource/"); + enum Baseline { + RANDOM, MOST_POPULAR_TYPE_IN_KB, MOST_FREQUENT_TYPE_IN_EXAMPLES, MOST_INFORMATIVE_EDGE_IN_EXAMPLES, LGG, MOST_FREQUENT_EDGE_IN_EXAMPLES } - - - List questionFiles; + QueryExecutionFactory qef; - String cacheDirectory = "./cache/qtl"; - - int minNrOfPositiveExamples = 9; - int maxDepth = 2; private org.dllearner.algorithms.qtl.impl.QueryTreeFactory queryTreeFactory; private ConciseBoundedDescriptionGenerator cbdGen; @@ -173,7 +149,7 @@ enum NoiseMethod { private EvaluationDataset dataset; - private Map> cache = new HashMap>(); + private Map> cache = new HashMap<>(); private int kbSize; @@ -181,114 +157,232 @@ enum NoiseMethod { PredicateExistenceFilter filter = new PredicateExistenceFilterDBpedia(null); -// List noiseExamples = new ArrayList(); -// private Map generatedExamples; -// private List correctExamples; - - - private Connection conn; - + // the directory where all files, results etc. are maintained private File benchmarkDirectory; + // whether to write eval results to a database private boolean write2DB; - + + // DB related objects + private Connection conn; private PreparedStatement psInsertOverallEval; - private PreparedStatement psInsertDetailEval; - private boolean failed; + // max. time for each QTL run + private int maxExecutionTimeInSeconds = 60; + + int minNrOfPositiveExamples = 9; + + int maxTreeDepth = 3; + + NoiseMethod noiseMethod = NoiseMethod.RANDOM; + // whether to override existing results + private boolean override = false; - public QTLEvaluation(EvaluationDataset dataset, File benchmarkDirectory, boolean write2DB) throws ComponentInitException { + // parameters + int[] nrOfExamplesIntervals = { +// 5, + 10, +// 15, + 20, +// 25, + 30 + }; + + double[] noiseIntervals = { + 0.0, + 0.1, + 0.2, + 0.3, +// 0.4, +// 0.6 + }; + + QueryTreeHeuristic[] heuristics = { + new QueryTreeHeuristicSimple(), +// new QueryTreeHeuristicComplex(qef) + }; + + HeuristicType[] measures = { + HeuristicType.PRED_ACC, + HeuristicType.FMEASURE, + HeuristicType.MATTHEWS_CORRELATION + }; + + private File cacheDirectory; + + private boolean useEmailNotification = false; + + private int nrOfThreads; + + OWLObjectRenderer owlRenderer = new org.dllearner.utilities.owl.DLSyntaxObjectRenderer(); + + + public QTLEvaluation(EvaluationDataset dataset, File benchmarkDirectory, boolean write2DB, boolean override, int maxQTLRuntime, boolean useEmailNotification, int nrOfThreads) throws ComponentInitException { this.dataset = dataset; this.benchmarkDirectory = benchmarkDirectory; this.write2DB = write2DB; + this.override = override; + this.maxExecutionTimeInSeconds = maxQTLRuntime; + this.useEmailNotification = useEmailNotification; + this.nrOfThreads = nrOfThreads; queryTreeFactory = new QueryTreeFactoryBase(); - queryTreeFactory.setMaxDepth(maxDepth); + queryTreeFactory.setMaxDepth(maxTreeDepth); // add some filters to avoid resources with namespaces like http://dbpedia.org/property/ - queryTreeFactory.addDropFilters((Filter[]) dataset.getQueryTreeFilters().toArray(new Filter[]{})); + List> var = dataset.getQueryTreeFilters(); + queryTreeFactory.addDropFilters((Filter[]) var.toArray(new Filter[var.size()])); qef = dataset.getKS().getQueryExecutionFactory(); cbdGen = new ConciseBoundedDescriptionGeneratorImpl(qef); - cbdGen.setRecursionDepth(maxDepth); + cbdGen.setRecursionDepth(maxTreeDepth); rnd.reSeed(123); kbSize = getKBSize(); - if(write2DB) { - try { - Properties config = new Properties(); - config.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("org/dllearner/algorithms/qtl/qtl-eval-config.properties")); - - String url = config.getProperty("url"); - String username = config.getProperty("username"); - String password = config.getProperty("password"); - Class.forName("com.mysql.jdbc.Driver").newInstance(); - conn = DriverManager.getConnection(url, username, password); - - String sql = "CREATE TABLE IF NOT EXISTS eval_overall (" + - "heuristic VARCHAR(100), " + - "heuristic_measure VARCHAR(100), " + - "nrOfExamples TINYINT, " + - "noise DOUBLE, " + - "avg_fscore_best_returned DOUBLE, " + - "avg_precision_best_returned DOUBLE, " + - "avg_recall_best_returned DOUBLE, " + - "avg_predacc_best_returned DOUBLE, " + - "avg_mathcorr_best_returned DOUBLE, " + - "avg_position_best DOUBLE, " + - "avg_fscore_best DOUBLE, " + - "avg_precision_best DOUBLE, " + - "avg_recall_best DOUBLE, " + - "PRIMARY KEY(heuristic, heuristic_measure, nrOfExamples, noise))"; - - java.sql.Statement stmt = conn.createStatement(); - stmt.execute(sql); - - sql = "CREATE TABLE IF NOT EXISTS eval_detailed (" + - "target_query VARCHAR(500)," + - "nrOfExamples TINYINT, " + - "noise DOUBLE, " + - "heuristic VARCHAR(100), " + - "heuristic_measure VARCHAR(100), " + - "query_top VARCHAR(5000), " + - "fscore_top DOUBLE, " + - "precision_top DOUBLE, " + - "recall_top DOUBLE, " + - "best_query TEXT," + - "best_rank TINYINT, " + - "best_fscore DOUBLE, " + - "best_precision DOUBLE, " + - "best_recall DOUBLE, " + - "PRIMARY KEY(target_query, nrOfExamples, noise, heuristic, heuristic_measure)) ENGINE=MyISAM"; - stmt = conn.createStatement(); - stmt.execute(sql); - - sql = "INSERT INTO eval_overall (heuristic, heuristic_measure, nrOfExamples, noise, " - + "avg_fscore_best_returned, avg_precision_best_returned, avg_recall_best_returned," - + "avg_predacc_best_returned, avg_mathcorr_best_returned, " - + "avg_position_best, avg_fscore_best, avg_precision_best, avg_recall_best)" + - "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)"; - psInsertOverallEval = conn.prepareStatement(sql); - - sql = "INSERT INTO eval_detailed (target_query, nrOfExamples, noise, heuristic, heuristic_measure, " - + "query_top, fscore_top, precision_top, recall_top," - + "best_query, best_rank, best_fscore, best_precision, best_recall)" + - "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; - psInsertDetailEval = conn.prepareStatement(sql); - } catch (Exception e) { - e.printStackTrace(); + setupDatabase(); + } + + cacheDirectory = new File(benchmarkDirectory, "cache"); + } + + private void setupDatabase() { + try { + Properties config = new Properties(); + config.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("org/dllearner/algorithms/qtl/qtl-eval-config.properties")); + + String url = config.getProperty("url"); + String username = config.getProperty("username"); + String password = config.getProperty("password"); + Class.forName("com.mysql.jdbc.Driver").newInstance(); + conn = DriverManager.getConnection(url, username, password); + + String sql; + java.sql.Statement stmt = conn.createStatement(); + +// // empty tables if override +// if(override) { +// sql = "DROP TABLE IF EXISTS eval_overall,eval_detailed;"; +// sql = "ALTER TABLE IF EXISTS eval_overall DROP PRIMARY KEY;"; +// stmt.execute(sql); +// } + + // create tables if not exist + sql = "CREATE TABLE IF NOT EXISTS eval_overall (" + + "heuristic VARCHAR(100), " + + "heuristic_measure VARCHAR(100), " + + "nrOfExamples TINYINT, " + + "noise DOUBLE, " + + "avg_fscore_best_returned DOUBLE, " + + "avg_precision_best_returned DOUBLE, " + + "avg_recall_best_returned DOUBLE, " + + "avg_predacc_best_returned DOUBLE, " + + "avg_mathcorr_best_returned DOUBLE, " + + "avg_position_best DOUBLE, " + + "avg_fscore_best DOUBLE, " + + "avg_precision_best DOUBLE, " + + "avg_recall_best DOUBLE, " + + "avg_predacc_best DOUBLE, " + + "avg_mathcorr_best DOUBLE, " + + "avg_fscore_baseline DOUBLE, " + + "avg_precision_baseline DOUBLE, " + + "avg_recall_baseline DOUBLE, " + + "avg_predacc_baseline DOUBLE, " + + "avg_mathcorr_baseline DOUBLE, " + + "avg_runtime_best_returned DOUBLE, " + + "PRIMARY KEY(heuristic, heuristic_measure, nrOfExamples, noise))"; + stmt.execute(sql); + + sql = "CREATE TABLE IF NOT EXISTS eval_detailed (" + + "target_query VARCHAR(500)," + + "nrOfExamples TINYINT, " + + "noise DOUBLE, " + + "heuristic VARCHAR(100), " + + "heuristic_measure VARCHAR(100), " + + "query_top LONGTEXT, " + + "fscore_top DOUBLE, " + + "precision_top DOUBLE, " + + "recall_top DOUBLE, " + + "best_query LONGTEXT," + + "best_rank SMALLINT, " + + "best_fscore DOUBLE, " + + "best_precision DOUBLE, " + + "best_recall DOUBLE, " + + "baseline_query TEXT," + + "baseline_fscore DOUBLE, " + + "baseline_precision DOUBLE, " + + "baseline_recall DOUBLE, " + + "runtime_top INT, " + + "PRIMARY KEY(target_query, nrOfExamples, noise, heuristic, heuristic_measure)) ENGINE=MyISAM"; + stmt.execute(sql); + + sql = "INSERT INTO eval_overall (" + + "heuristic, heuristic_measure, nrOfExamples, noise, " + + "avg_fscore_best_returned, avg_precision_best_returned, avg_recall_best_returned," + + "avg_predacc_best_returned, avg_mathcorr_best_returned, " + + "avg_position_best, avg_fscore_best, avg_precision_best, avg_recall_best, avg_predacc_best, avg_mathcorr_best," + + "avg_fscore_baseline, avg_precision_baseline, avg_recall_baseline, avg_predacc_baseline, avg_mathcorr_baseline," + + "avg_runtime_best_returned" + + ") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; + if(override) { + sql += " ON DUPLICATE KEY UPDATE "; + sql += "avg_fscore_best_returned = VALUES(avg_fscore_best_returned),"; + sql += "avg_precision_best_returned = VALUES(avg_precision_best_returned),"; + sql += "avg_recall_best_returned = VALUES(avg_recall_best_returned),"; + sql += "avg_predacc_best_returned = VALUES(avg_predacc_best_returned),"; + sql += "avg_mathcorr_best_returned = VALUES(avg_mathcorr_best_returned),"; + sql += "avg_position_best = VALUES(avg_position_best),"; + sql += "avg_fscore_best = VALUES(avg_fscore_best),"; + sql += "avg_precision_best = VALUES(avg_precision_best),"; + sql += "avg_recall_best = VALUES(avg_recall_best),"; + sql += "avg_predacc_best = VALUES(avg_predacc_best),"; + sql += "avg_mathcorr_best = VALUES(avg_mathcorr_best),"; + sql += "avg_fscore_baseline = VALUES(avg_fscore_baseline),"; + sql += "avg_precision_baseline = VALUES(avg_precision_baseline),"; + sql += "avg_recall_baseline = VALUES(avg_recall_baseline),"; + sql += "avg_predacc_baseline = VALUES(avg_predacc_baseline),"; + sql += "avg_mathcorr_baseline = VALUES(avg_mathcorr_baseline),"; + sql += "avg_runtime_best_returned = VALUES(avg_runtime_best_returned)"; } + psInsertOverallEval = conn.prepareStatement(sql); + + sql = "INSERT INTO eval_detailed (" + + "target_query, nrOfExamples, noise, heuristic, heuristic_measure, " + + "query_top, fscore_top, precision_top, recall_top," + + "best_query, best_rank, best_fscore, best_precision, best_recall, " + + "baseline_query,baseline_fscore, baseline_precision, baseline_recall," + + "runtime_top" + + ") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; + if(override) { + sql += " ON DUPLICATE KEY UPDATE "; + sql += "query_top = VALUES(query_top),"; + sql += "fscore_top = VALUES(fscore_top),"; + sql += "precision_top = VALUES(precision_top),"; + sql += "recall_top = VALUES(recall_top),"; + sql += "best_query = VALUES(best_query),"; + sql += "best_rank = VALUES(best_rank),"; + sql += "best_fscore = VALUES(best_fscore),"; + sql += "best_precision = VALUES(best_precision),"; + sql += "best_recall = VALUES(best_recall),"; + sql += "baseline_query = VALUES(baseline_query),"; + sql += "baseline_fscore = VALUES(baseline_fscore),"; + sql += "baseline_precision = VALUES(baseline_precision),"; + sql += "baseline_recall = VALUES(baseline_recall),"; + sql += "runtime_top = VALUES(runtime_top)"; + } + psInsertDetailEval = conn.prepareStatement(sql); + } catch (Exception e) { + e.printStackTrace(); } } private int getKBSize() { - String query = "SELECT (COUNT(*) AS ?cnt) WHERE {[] a ?type . ?type a .}"; + String query = "SELECT (COUNT(DISTINCT ?s) AS ?cnt) WHERE {?s a ?type . ?type a .}"; QueryExecution qe = qef.createQueryExecution(query); ResultSet rs = qe.execSelect(); @@ -300,59 +394,85 @@ private int getKBSize() { } private List getSparqlQueries(File queriesFile) throws IOException { - List sparqlQueries = new ArrayList(); + List sparqlQueries = new ArrayList<>(); for (String queryString : Files.readLines(queriesFile, Charsets.UTF_8)) { - Query q = QueryFactory.create(queryString); - int subjectObjectJoinDepth = QueryUtils.getSubjectObjectJoinDepth(q, q.getProjectVars().get(0)); - if(subjectObjectJoinDepth < maxDepth) { +// Query q = QueryFactory.create(queryString); +// int subjectObjectJoinDepth = QueryUtils.getSubjectObjectJoinDepth(q, q.getProjectVars().get(0)); +// if(subjectObjectJoinDepth < maxTreeDepth) { sparqlQueries.add(queryString); - } +// } } return sparqlQueries; } - public void run(File queriesFile) throws Exception{ - - List sparqlQueries = getSparqlQueries(queriesFile); -// sparqlQueries = sparqlQueries.subList(0, 20); - logger.info("Total number of queries: " + sparqlQueries.size()); - - // parameters - int[] nrOfExamplesIntervals = { - 5, - 10, -// 15, - 20, -// 25, - 30 - }; - - double[] noiseIntervals = { - 0.0, - 0.1, - 0.2, - 0.3, - 0.4, -// 0.6 - }; - - QueryTreeHeuristic[] heuristics = { - new QueryTreeHeuristicSimple(), -// new QueryTreeHeuristicComplex(qef) - }; - - HeuristicType[] measures = { - HeuristicType.PRED_ACC, - HeuristicType.FMEASURE, - HeuristicType.MATTHEWS_CORRELATION}; + private List filter(List queries, int nrOfQueriesPerDepth) { + List subset = new ArrayList<>(); + for(int depth = 1; depth <= maxTreeDepth; depth++) { + List tmp = new ArrayList<>(); + Iterator iterator = queries.iterator(); + while(iterator.hasNext() && tmp.size() < nrOfQueriesPerDepth) { + String queryString = iterator.next(); + Query q = QueryFactory.create(queryString); + int subjectObjectJoinDepth = QueryUtils.getSubjectObjectJoinDepth(q, q.getProjectVars().get(0)); + if(subjectObjectJoinDepth == (depth - 1)) { + tmp.add(queryString); + } + } + subset.addAll(tmp); + } + return subset; + } + + public void run(File queriesFile, int maxNrOfProcessedQueries, int maxTreeDepth, int[] exampleInterval, double[] noiseInterval, HeuristicType[] measures) throws Exception{ + this.maxTreeDepth = maxTreeDepth; + queryTreeFactory.setMaxDepth(maxTreeDepth); + + if(exampleInterval != null) { + nrOfExamplesIntervals = exampleInterval; + } + if(noiseInterval != null) { + this.noiseIntervals = noiseInterval; + } + if(measures != null) { + this.measures = measures; + } + + logger.info("Started QTL evaluation..."); + long t1 = System.currentTimeMillis(); + List queries = getSparqlQueries(queriesFile); + logger.info("#loaded queries: " + queries.size()); + + if(maxNrOfProcessedQueries == -1) { + maxNrOfProcessedQueries = queries.size(); + } + + queries = filter(queries, maxNrOfProcessedQueries / maxTreeDepth); +// queries = queries.subList(0, Math.min(queries.size(), maxNrOfProcessedQueries)); + logger.info("#queries to process: " + queries.size()); + + // generate examples for each query + logger.info("precomputing pos. and neg. examples..."); + final Map query2Examples = new HashMap<>(); + for (String query : queries) { + query2Examples.put(query, generateExamples(query)); + } + logger.info("precomputing pos. and neg. examples finished."); + + final int totalNrOfQTLRuns = heuristics.length * measures.length * nrOfExamplesIntervals.length * noiseIntervals.length * queries.size(); + logger.info("#QTL runs: " + totalNrOfQTLRuns); + + final AtomicInteger currentNrOfFinishedRuns = new AtomicInteger(0); + // loop over heuristics for(final QueryTreeHeuristic heuristic : heuristics) { final String heuristicName = heuristic.getClass().getAnnotation(ComponentAnn.class).shortName(); + // loop over heuristics measures for (HeuristicType measure : measures) { final String measureName = measure.toString(); + heuristic.setHeuristicType(measure); double[][] data = new double[nrOfExamplesIntervals.length][noiseIntervals.length]; @@ -368,7 +488,7 @@ public void run(File queriesFile) throws Exception{ File logFile = new File(benchmarkDirectory, "qtl2-" + nrOfExamples + "-" + noise + "-" + heuristicName + "-" + measureName + ".log"); File statsFile = new File(benchmarkDirectory, "qtl2-" + nrOfExamples + "-" + noise + "-" + heuristicName + "-" + measureName + ".stats"); - if(logFile.exists() && statsFile.exists()) { + if(!override && logFile.exists() && statsFile.exists()) { logger.info("Eval config already processed. For re-running please remove corresponding output files."); continue; } @@ -386,32 +506,42 @@ public void run(File queriesFile) throws Exception{ final DescriptiveStatistics nrOfReturnedSolutionsStats = new SynchronizedDescriptiveStatistics(); + final DescriptiveStatistics baselinePrecisionStats = new SynchronizedDescriptiveStatistics(); + final DescriptiveStatistics baselineRecallStats = new SynchronizedDescriptiveStatistics(); + final DescriptiveStatistics baselineFMeasureStats = new SynchronizedDescriptiveStatistics(); + final DescriptiveStatistics baselinePredAccStats = new SynchronizedDescriptiveStatistics(); + final DescriptiveStatistics baselineMathCorrStats = new SynchronizedDescriptiveStatistics(); + final DescriptiveStatistics bestReturnedSolutionPrecisionStats = new SynchronizedDescriptiveStatistics(); final DescriptiveStatistics bestReturnedSolutionRecallStats = new SynchronizedDescriptiveStatistics(); final DescriptiveStatistics bestReturnedSolutionFMeasureStats = new SynchronizedDescriptiveStatistics(); final DescriptiveStatistics bestReturnedSolutionPredAccStats = new SynchronizedDescriptiveStatistics(); final DescriptiveStatistics bestReturnedSolutionMathCorrStats = new SynchronizedDescriptiveStatistics(); + final DescriptiveStatistics bestReturnedSolutionRuntimeStats = new SynchronizedDescriptiveStatistics(); + final DescriptiveStatistics bestSolutionPrecisionStats = new SynchronizedDescriptiveStatistics(); final DescriptiveStatistics bestSolutionRecallStats = new SynchronizedDescriptiveStatistics(); final DescriptiveStatistics bestSolutionFMeasureStats = new SynchronizedDescriptiveStatistics(); - final DescriptiveStatistics bestSolutionPredAccStats = new SynchronizedDescriptiveStatistics(); final DescriptiveStatistics bestSolutionMathCorrStats = new SynchronizedDescriptiveStatistics(); final DescriptiveStatistics bestSolutionPositionStats = new SynchronizedDescriptiveStatistics(); + MonitorFactory.getTimeMonitor(TimeMonitors.CBD_RETRIEVAL.name()).reset(); MonitorFactory.getTimeMonitor(TimeMonitors.TREE_GENERATION.name()).reset(); - ExecutorService tp = Executors.newFixedThreadPool(4); - failed = false; - // if(nrOfExamples != 7) continue; + ExecutorService tp = Executors.newFixedThreadPool(nrOfThreads); + + // indicates if the execution for some of the queries failed + final AtomicBoolean failed = new AtomicBoolean(false); + // loop over SPARQL queries - for (final String sparqlQuery : sparqlQueries) { + for (final String sparqlQuery : queries) { +// if(!(sparqlQuery.contains("VideoGame")))continue; - // if(!sparqlQuery.contains("PrimeMinister"))continue; tp.submit(new Runnable(){ @Override @@ -419,22 +549,58 @@ public void run() { logger.info("##############################################################"); logger.info("Processing query\n" + sparqlQuery); - // some queries can return less examples - int possibleNrOfExamples = Math.min(getResultCount(sparqlQuery), nrOfExamples); try { - ExamplesWrapper examples = generateExamples(sparqlQuery, possibleNrOfExamples, noise); + ExamplesWrapper examples = query2Examples.get(sparqlQuery).get(nrOfExamples, nrOfExamples, noise); + logger.info("pos. examples:\n" + Joiner.on("\n").join(examples.correctPosExamples)); + logger.info("neg. examples:\n" + Joiner.on("\n").join(examples.correctNegExamples)); + + + // compute baseline + logger.info("Computing baseline..."); + RDFResourceTree baselineSolution = applyBaseLine(examples, Baseline.MOST_INFORMATIVE_EDGE_IN_EXAMPLES); + logger.info("done. \nBaseline solution:\n" + owlRenderer.render(QueryTreeUtils.toOWLClassExpression(baselineSolution))); + logger.info("Evaluating baseline..."); + Score baselineScore = computeScore(sparqlQuery, baselineSolution, noise); + logger.info("Baseline score:\n" + baselineScore); + String baseLineQuery = QueryTreeUtils.toSPARQLQueryString( + baselineSolution, dataset.getBaseIRI(), dataset.getPrefixMapping()); + baselinePrecisionStats.addValue(baselineScore.precision); + baselineRecallStats.addValue(baselineScore.recall); + baselineFMeasureStats.addValue(baselineScore.fmeasure); + baselinePredAccStats.addValue(baselineScore.predAcc); + baselineMathCorrStats.addValue(baselineScore.mathCorr); - // compute or load cached solutions - List solutions = generateSolutions(examples, noise, heuristic); + // run QTL + PosNegLPStandard lp = new PosNegLPStandard(); + lp.setPositiveExamples(examples.posExamplesMapping.keySet()); + lp.setNegativeExamples(examples.negExamplesMapping.keySet()); + QTL2Disjunctive la = new QTL2Disjunctive(lp, qef); + la.setRenderer(new org.dllearner.utilities.owl.DLSyntaxObjectRenderer()); + la.setReasoner(dataset.getReasoner()); + la.setEntailment(Entailment.RDFS); + la.setTreeFactory(queryTreeFactory); + la.setPositiveExampleTrees(examples.posExamplesMapping); + la.setNegativeExampleTrees(examples.negExamplesMapping); + la.setNoise(noise); + la.setHeuristic(heuristic); + la.setMaxExecutionTimeInSeconds(maxExecutionTimeInSeconds); + la.setMaxTreeComputationTimeInSeconds(maxExecutionTimeInSeconds); + la.init(); + la.start(); + List solutions = new ArrayList<>(la.getSolutions()); + +// List solutions = generateSolutions(examples, noise, heuristic); nrOfReturnedSolutionsStats.addValue(solutions.size()); // the best returned solution by QTL EvaluatedRDFResourceTree bestSolution = solutions.get(0); logger.info("Got " + solutions.size() + " query trees."); - logger.info("Best computed solution:\n" + bestSolution.asEvaluatedDescription()); + logger.info("Best computed solution:\n" + render(bestSolution.asEvaluatedDescription())); logger.info("QTL Score:\n" + bestSolution.getTreeScore()); - + long runtimeBestSolution = la.getTimeBestSolutionFound(); + bestReturnedSolutionRuntimeStats.addValue(runtimeBestSolution); + // convert to SPARQL query RDFResourceTree tree = bestSolution.getTree(); // filter.filter(tree); @@ -443,11 +609,11 @@ public void run() { // compute score Score score = computeScore(sparqlQuery, tree, noise); - bestReturnedSolutionPrecisionStats.addValue(score.getPrecision()); - bestReturnedSolutionRecallStats.addValue(score.getRecall()); - bestReturnedSolutionFMeasureStats.addValue(score.getFmeasure()); - bestReturnedSolutionPredAccStats.addValue(score.getPredAcc()); - bestReturnedSolutionMathCorrStats.addValue(score.getMathCorr()); + bestReturnedSolutionPrecisionStats.addValue(score.precision); + bestReturnedSolutionRecallStats.addValue(score.recall); + bestReturnedSolutionFMeasureStats.addValue(score.fmeasure); + bestReturnedSolutionPredAccStats.addValue(score.predAcc); + bestReturnedSolutionMathCorrStats.addValue(score.mathCorr); logger.info(score); // find the extensionally best matching tree in the list @@ -456,64 +622,95 @@ public void run() { Score bestMatchingScore = bestMatchingTreeWithScore.getSecond(); // position of best tree in list of solutions - int position = solutions.indexOf(bestMatchingTree); - bestSolutionPositionStats.addValue(position); + int positionBestScore = solutions.indexOf(bestMatchingTree); + bestSolutionPositionStats.addValue(positionBestScore); Score bestScore = score; - if (position > 0) { - logger.info("Position of best covering tree in list: " + position); - logger.info("Best covering solution:\n" + bestMatchingTree.asEvaluatedDescription()); + if (positionBestScore > 0) { + logger.info("Position of best covering tree in list: " + positionBestScore); + logger.info("Best covering solution:\n" + render(bestMatchingTree.asEvaluatedDescription())); logger.info("Tree score: " + bestMatchingTree.getTreeScore()); - String bestLearnedSPARQLQuery = QueryTreeUtils.toSPARQLQueryString( - filter.filter(bestMatchingTree.getTree()), - dataset.getBaseIRI(), dataset.getPrefixMapping()); bestScore = bestMatchingScore; logger.info(bestMatchingScore); } else { logger.info("Best returned solution was also the best covering solution."); } - bestSolutionRecallStats.addValue(bestScore.getRecall()); - bestSolutionPrecisionStats.addValue(bestScore.getPrecision()); - bestSolutionFMeasureStats.addValue(bestScore.getFmeasure()); - bestSolutionPredAccStats.addValue(bestScore.getPredAcc()); - bestSolutionMathCorrStats.addValue(bestScore.getMathCorr()); + bestSolutionRecallStats.addValue(bestScore.recall); + bestSolutionPrecisionStats.addValue(bestScore.precision); + bestSolutionFMeasureStats.addValue(bestScore.fmeasure); + bestSolutionPredAccStats.addValue(bestScore.predAcc); + bestSolutionMathCorrStats.addValue(bestScore.mathCorr); + for ( RDFResourceTree negTree : examples.negExamplesMapping.values()) { + if(QueryTreeUtils.isSubsumedBy(negTree, bestMatchingTree.getTree())) { + Files.append(sparqlQuery + "\n", new File("/tmp/negCovered.txt"), Charsets.UTF_8); + break; + } + } + String bestQuery = QueryFactory.create(QueryTreeUtils.toSPARQLQueryString( filter.filter(bestMatchingTree.getTree()), dataset.getBaseIRI(), dataset.getPrefixMapping())).toString(); - write2DB(sparqlQuery, nrOfExamples, examples, noise, heuristicName, measureName, - QueryFactory.create(learnedSPARQLQuery).toString(), score.getFmeasure(), score.getPrecision(), score.getRecall(), - bestQuery, position, bestScore.getFmeasure(), bestScore.getPrecision(), bestScore.getRecall()); + if(write2DB) { + write2DB(sparqlQuery, nrOfExamples, examples, noise, + baseLineQuery, baselineScore, + heuristicName, measureName, + QueryFactory.create(learnedSPARQLQuery).toString(), score, runtimeBestSolution, + bestQuery, positionBestScore, bestScore); + } } catch (Exception e) { - failed = true; - logger.error("Error occured.", e); -// System.exit(0); + failed.set(true); + logger.error("Error occured for query\n" + sparqlQuery, e); + try { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + e.printStackTrace(pw); + Files.append(sparqlQuery + "\n" + sw.toString(), new File(benchmarkDirectory, "failed-" + nrOfExamples + "-" + noise + "-" + heuristicName + "-" + measureName + ".txt"), Charsets.UTF_8); + } catch (IOException e1) { + e1.printStackTrace(); + } + } finally { + int cnt = currentNrOfFinishedRuns.incrementAndGet(); + logger.info("***********Evaluation Progress:" + + NumberFormat.getPercentInstance().format((double)cnt / totalNrOfQTLRuns) + + "(" + cnt + "/" + totalNrOfQTLRuns + ")" + + "***********"); } - }}); + } + }); + } tp.shutdown(); - tp.awaitTermination(1, TimeUnit.HOURS); - + tp.awaitTermination(12, TimeUnit.HOURS); + Logger.getRootLogger().removeAppender(appender); - if(!failed) { + if(!failed.get()) { String result = ""; + result += "\nBaseline Precision:\n" + baselinePrecisionStats; + result += "\nBaseline Recall:\n" + baselineRecallStats; + result += "\nBaseline F-measure:\n" + baselineFMeasureStats; + result += "\nBaseline PredAcc:\n" + baselinePredAccStats; + result += "\nBaseline MathCorr:\n" + baselineMathCorrStats; + result += "#Returned solutions:\n" + nrOfReturnedSolutionsStats; result += "\nOverall Precision:\n" + bestReturnedSolutionPrecisionStats; result += "\nOverall Recall:\n" + bestReturnedSolutionRecallStats; - result += "\nOverall FMeasure:\n" + bestReturnedSolutionFMeasureStats; + result += "\nOverall F-measure:\n" + bestReturnedSolutionFMeasureStats; result += "\nOverall PredAcc:\n" + bestReturnedSolutionPredAccStats; result += "\nOverall MathCorr:\n" + bestReturnedSolutionMathCorrStats; + + result += "\nTime until best returned solution found:\n" + bestReturnedSolutionRuntimeStats; + result += "\nPositions of best solution:\n" + Arrays.toString(bestSolutionPositionStats.getValues()); result += "\nPosition of best solution stats:\n" + bestSolutionPositionStats; - result += "\nOverall Precision of best solution:\n" + bestSolutionPrecisionStats; result += "\nOverall Recall of best solution:\n" + bestSolutionRecallStats; - result += "\nOverall FMeasure of best solution:\n" + bestSolutionFMeasureStats; + result += "\nOverall F-measure of best solution:\n" + bestSolutionFMeasureStats; result += "\nCBD generation time(total):\t" + MonitorFactory.getTimeMonitor(TimeMonitors.CBD_RETRIEVAL.name()).getTotal() + "\n"; result += "CBD generation time(avg):\t" + MonitorFactory.getTimeMonitor(TimeMonitors.CBD_RETRIEVAL.name()).getAvg() + "\n"; @@ -540,7 +737,15 @@ public void run() { bestSolutionPositionStats.getMean(), bestSolutionFMeasureStats.getMean(), bestSolutionPrecisionStats.getMean(), - bestSolutionRecallStats.getMean() + bestSolutionRecallStats.getMean(), + bestSolutionPredAccStats.getMean(), + bestSolutionMathCorrStats.getMean(), + baselineFMeasureStats.getMean(), + baselinePrecisionStats.getMean(), + baselineRecallStats.getMean(), + baselinePredAccStats.getMean(), + baselineMathCorrStats.getMean(), + bestReturnedSolutionRuntimeStats.getMean() ); } } @@ -562,14 +767,180 @@ public void run() { content += "\n"; } - File examplesVsNoise = new File(benchmarkDirectory, "examplesVsNoise-" + heuristicName + "-" + measureName + ".txt"); + File examplesVsNoise = new File(benchmarkDirectory, "examplesVsNoise-" + heuristicName + "-" + measureName + ".tsv"); try { Files.write(content, examplesVsNoise, Charsets.UTF_8); } catch (IOException e) { - e.printStackTrace(); + logger.error(e); } } } + + conn.close(); + + if(useEmailNotification) { + sendFinishedMail(); + } + long t2 = System.currentTimeMillis(); + long duration = t2 - t1; + logger.info("QTL evaluation finished in " + DurationFormatUtils.formatDurationHMS(duration) + "ms."); + } + + private String render(EvaluatedDescription ed) { + return owlRenderer.render(ed.getDescription()) + dfPercent.format(ed.getAccuracy()); + } + + private void sendFinishedMail() throws EmailException, IOException { + Properties config = new Properties(); + config.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("org/dllearner/algorithms/qtl/qtl-mail.properties")); + + Email email = new SimpleEmail(); + email.setHostName(config.getProperty("hostname")); + email.setSmtpPort(465); + email.setAuthenticator(new DefaultAuthenticator(config.getProperty("username"), config.getProperty("password"))); + email.setSSLOnConnect(true); + email.setFrom(config.getProperty("from")); + email.setSubject("QTL evaluation finished."); + email.setMsg("QTL evaluation finished."); + email.addTo(config.getProperty("to")); + email.send(); + } + + /* + * Compute a baseline solution. + * + * From simple to more complex: + * + * 1. random type + * 2. most popular type in KB + * 3. most frequent type in pos. examples + * 4. most informative edge, e.g. based on information gain + * 5. LGG of all pos. examples + * + */ + private RDFResourceTree applyBaseLine(ExamplesWrapper examples, Baseline baselineApproach) { + Collection posExamples = examples.posExamplesMapping.values(); + Collection negExamples = examples.negExamplesMapping.values(); + + switch (baselineApproach) { + case RANDOM:// 1. + String query = "SELECT ?cls WHERE {?cls a owl:Class .} ORDER BY RAND() LIMIT 1"; + QueryExecution qe = qef.createQueryExecution(query); + ResultSet rs = qe.execSelect(); + if(rs.hasNext()) { + QuerySolution qs = rs.next(); + Resource cls = qs.getResource("cls"); + RDFResourceTree solution = new RDFResourceTree(); + solution.addChild(new RDFResourceTree(cls.asNode()), RDF.type.asNode()); + return solution; + } + case MOST_POPULAR_TYPE_IN_KB:// 2. + query = "SELECT ?cls WHERE {?cls a owl:Class . ?s a ?cls .} ORDER BY DESC(COUNT(?s)) LIMIT 1"; + qe = qef.createQueryExecution(query); + rs = qe.execSelect(); + if(rs.hasNext()) { + QuerySolution qs = rs.next(); + Resource cls = qs.getResource("cls"); + RDFResourceTree solution = new RDFResourceTree(); + solution.addChild(new RDFResourceTree(cls.asNode()), RDF.type.asNode()); + return solution; + } + case MOST_FREQUENT_TYPE_IN_EXAMPLES:// 3. + Multiset types = HashMultiset.create(); + for (RDFResourceTree ex : posExamples) { + List children = ex.getChildren(RDF.type.asNode()); + for (RDFResourceTree child : children) { + types.add(child.getData()); + } + } + Node mostFrequentType = Ordering.natural().onResultOf(new Function, Integer>() { + public Integer apply(Multiset.Entry entry) { + return entry.getCount(); + } + }).max(types.entrySet()).getElement(); + RDFResourceTree solution = new RDFResourceTree(); + solution.addChild(new RDFResourceTree(mostFrequentType), RDF.type.asNode()); + return solution; + case MOST_FREQUENT_EDGE_IN_EXAMPLES:// 4. + {Multiset> pairs = HashMultiset.create(); + for (RDFResourceTree ex : posExamples) { + SortedSet edges = ex.getEdges(); + for (Node edge : edges) { + List children = ex.getChildren(edge); + for (RDFResourceTree child : children) { + pairs.add(new Pair<>(edge, child.getData())); + } + } + } + Pair mostFrequentPair = Ordering.natural().onResultOf(new Function>, Integer>() { + public Integer apply(Multiset.Entry> entry) { + return entry.getCount(); + } + }).max(pairs.entrySet()).getElement(); + solution = new RDFResourceTree(); + solution.addChild(new RDFResourceTree(mostFrequentPair.getValue()), mostFrequentPair.getKey()); + return solution;} + case MOST_INFORMATIVE_EDGE_IN_EXAMPLES: + // get all p-o in pos examples + Multiset> edgeObjectPairs = HashMultiset.create(); + for (RDFResourceTree ex : posExamples) { + SortedSet edges = ex.getEdges(); + for (Node edge : edges) { + List children = ex.getChildren(edge); + for (RDFResourceTree child : children) { + edgeObjectPairs.add(new Pair<>(edge, child.getData())); + } + } + } + + double bestAccuracy = -1; + solution = new RDFResourceTree(); + + for (Pair pair : edgeObjectPairs.elementSet()) { + Node edge = pair.getKey(); + Node childValue = pair.getValue(); + + // compute accuracy + int tp = edgeObjectPairs.count(pair); + int fn = posExamples.size() - tp; + int fp = 0; + for (RDFResourceTree ex : negExamples) { // compute false positives + List children = ex.getChildren(edge); + if(children != null) { + for (RDFResourceTree child : children) { + if(child.getData().equals(childValue)) { + fp++; + break; + } + } + } + } + int tn = negExamples.size() - fp; + + double accuracy = Heuristics.getPredictiveAccuracy( + posExamples.size(), + negExamples.size(), + tp, + tn, + 1.0); + // update best solution + if(accuracy >= bestAccuracy) { + solution = new RDFResourceTree(); + solution.addChild(new RDFResourceTree(childValue), edge); + bestAccuracy = accuracy; + } + } + return solution; + case LGG: + LGGGenerator lggGenerator = new LGGGeneratorSimple(); + RDFResourceTree lgg = lggGenerator.getLGG(Lists.newArrayList(posExamples)); + return lgg; + + default: + break; + + } + return null; } private List generateSolutions(ExamplesWrapper examples, double noise, QueryTreeHeuristic heuristic) throws ComponentInitException { @@ -580,6 +951,7 @@ private List generateSolutions(ExamplesWrapper example // lp.init(); QTL2Disjunctive la = new QTL2Disjunctive(lp, qef); + la.setRenderer(new org.dllearner.utilities.owl.DLSyntaxObjectRenderer()); la.setReasoner(dataset.getReasoner()); la.setEntailment(Entailment.RDFS); la.setTreeFactory(queryTreeFactory); @@ -587,10 +959,12 @@ private List generateSolutions(ExamplesWrapper example la.setNegativeExampleTrees(examples.negExamplesMapping); la.setNoise(noise); la.setHeuristic(heuristic); + la.setMaxExecutionTimeInSeconds(maxExecutionTimeInSeconds); + la.setMaxTreeComputationTimeInSeconds(maxExecutionTimeInSeconds); la.init(); la.start(); - List solutions = new ArrayList(la.getSolutions()); + List solutions = new ArrayList<>(la.getSolutions()); return solutions; } @@ -621,7 +995,7 @@ private Pair findBestMatchingTree(Collection findBestMatchingTree(Collection findBestMatchingTreeFast( Collection trees, String targetSPARQLQuery, double noise, ExamplesWrapper examples) throws Exception{ - logger.info("Finding best matching query tree..."); + logger.info("Searching for best matching query tree..."); - Set correctPositiveExampleTrees = new HashSet(); + Set correctPositiveExampleTrees = new HashSet<>(); for (String ex : examples.correctPosExamples) { correctPositiveExampleTrees.add(examples.posExamplesMapping.get(new OWLNamedIndividualImpl(IRI.create(ex)))); } - Set noisyPositiveExampleTrees = new HashSet(); - for (String ex : examples.noisePosExamples) { + Set noisyPositiveExampleTrees = new HashSet<>(); + for (String ex : examples.falsePosExamples) { noisyPositiveExampleTrees.add(examples.posExamplesMapping.get(new OWLNamedIndividualImpl(IRI.create(ex)))); } @@ -662,8 +1036,8 @@ private Pair findBestMatchingTreeFast( int coveredNoiseTreesBest = 0; int coveredCorrectTreesBest = 0; - for (EvaluatedRDFResourceTree evalutedTree : trees) { - RDFResourceTree tree = evalutedTree.getTree(); + for (EvaluatedRDFResourceTree evaluatedTree : trees) { + RDFResourceTree tree = evaluatedTree.getTree(); int coveredNoiseTrees = 0; for (RDFResourceTree noiseTree : noisyPositiveExampleTrees) { @@ -678,15 +1052,16 @@ private Pair findBestMatchingTreeFast( coveredCorrectTrees++; } } + // System.err.println("+" + coveredCorrectTrees + "|-" + coveredNoiseTrees); // this is obviously the most perfect solution according to the input if(coveredNoiseTrees == 0 && coveredCorrectTrees == correctPositiveExampleTrees.size()) { - bestTree = evalutedTree; + bestTree = evaluatedTree; break; } if(coveredCorrectTrees > coveredCorrectTreesBest || coveredNoiseTrees < coveredNoiseTreesBest) { - bestTree = evalutedTree; + bestTree = evaluatedTree; coveredCorrectTreesBest = coveredCorrectTrees; coveredNoiseTreesBest = coveredNoiseTrees; } @@ -700,51 +1075,54 @@ private Pair findBestMatchingTreeFast( return new Pair<>(bestTree, score); } - private ExamplesWrapper generateExamples(String sparqlQuery, int maxNrOfExamples, double noise) throws Exception{ - Random randomGen = new Random(123); - - // get all resources returned by the query - List resources = getResult(sparqlQuery, false); - - // pick some random positive examples from the list - Collections.shuffle(resources, randomGen); - List examples = resources.subList(0, Math.min(maxNrOfExamples, resources.size())); - logger.info("Pos. examples: " + examples); + private ExampleCandidates generateExamples(String sparqlQuery) throws Exception{ + logger.info("Generating examples for query ..." + sparqlQuery); + HashFunction hf = Hashing.md5(); + + File examplesDirectory = new File(cacheDirectory, "examples"); + examplesDirectory.mkdirs(); - // add noise if enabled - Pair, List> examplesSet; - if(noise > 0) { - examplesSet = generateNoise(examples, sparqlQuery, noise, randomGen); + // get all pos. examples, i.e. resources returned by the query + List posExamples; + String hash = hf.newHasher().putString(sparqlQuery, Charsets.UTF_8).hash().toString(); + File file = new File(examplesDirectory, hash + ".tp"); + if(file.exists()) { + posExamples = Files.readLines(file, Charsets.UTF_8); } else { - examplesSet = new Pair, List>(new ArrayList(examples), new ArrayList()); + posExamples = getResult(sparqlQuery, false); + Files.write(Joiner.on("\n").join(posExamples), file, Charsets.UTF_8); } + Collections.sort(posExamples); + logger.info("#Pos. examples: " + posExamples.size()); - // build query trees - Map posQueryTrees = new HashMap<>(); - for (String ex : examples) { - try { - RDFResourceTree queryTree = getQueryTree(ex); - posQueryTrees.put(new OWLNamedIndividualImpl(IRI.create(ex)), queryTree); - } catch (Exception e) { - throw e; - } + // get some neg. examples, i.e. resources not returned by the query + int maxNrOfNegExamples = 100; + List negExamples; + hash = hf.newHasher().putString(sparqlQuery, Charsets.UTF_8).putInt(maxNrOfNegExamples).toString(); + file = new File(examplesDirectory, hash + ".tn"); + if(file.exists()) { + negExamples = Files.readLines(file, Charsets.UTF_8); + } else { + negExamples = new NegativeExampleSPARQLQueryGenerator(qef).getNegativeExamples(sparqlQuery, maxNrOfNegExamples); + Files.write(Joiner.on("\n").join(negExamples), file, Charsets.UTF_8); } - - List negativeExamples = new NegativeExampleSPARQLQueryGenerator().getNegativeExamples(sparqlQuery, maxNrOfExamples); - Map negQueryTrees = new HashMap<>(); - for (String ex : negativeExamples) { - try { - RDFResourceTree queryTree = getQueryTree(ex); - negQueryTrees.put(new OWLNamedIndividualImpl(IRI.create(ex)), queryTree); - } catch (Exception e) { - throw e; - } + Collections.sort(negExamples); + logger.info("#Neg. examples: " + negExamples.size()); + + // get some noise candidates, i.e. resources used as false pos. examples + int maxNrOfNoiseCandidates = 100; + List noiseCandidates; + hash = hf.newHasher().putString(sparqlQuery, Charsets.UTF_8).putInt(maxNrOfNoiseCandidates).toString(); + file = new File(examplesDirectory, hash + ".fp"); + if(file.exists()) { + noiseCandidates = Files.readLines(file, Charsets.UTF_8); + } else { + noiseCandidates = generateNoiseCandidates(sparqlQuery, noiseMethod, ListUtils.union(posExamples, negExamples), maxNrOfNoiseCandidates); + Files.write(Joiner.on("\n").join(noiseCandidates), file, Charsets.UTF_8); } - - // add noise by modifying the query trees -// generateNoiseAttributeLevel(sparqlQuery, queryTrees, noise); - - return new ExamplesWrapper(examplesSet.getFirst(), examplesSet.getSecond(), negativeExamples, posQueryTrees, negQueryTrees); + logger.info("#False pos. example candidates: " + noiseCandidates.size()); + + return new ExampleCandidates(posExamples, negExamples, noiseCandidates); } private RDFResourceTree getSimilarTree(RDFResourceTree tree, String property, int maxTreeDepth){ @@ -761,13 +1139,38 @@ private RDFResourceTree getSimilarTree(RDFResourceTree tree, String property, in return null; } + /** + * Generates a list of candidates that are not contained in the given set of examples. + * @param sparqlQuery + * @param noiseMethod + * @param examples + * @param limit + * @return list of candidate resource + */ + private List generateNoiseCandidates(String sparqlQuery, NoiseMethod noiseMethod, List examples, int limit) { + List noiseCandidates = new ArrayList<>(); + + switch(noiseMethod) { + case RANDOM: noiseCandidates = generateNoiseCandidatesRandom(examples, limit); + break; + case SIMILAR:noiseCandidates = generateNoiseCandidatesSimilar(examples, sparqlQuery, limit); + break; + case SIMILARITY_PARAMETERIZED://TODO implement configurable noise method + break; + default:noiseCandidates = generateNoiseCandidatesRandom(examples, limit); + break; + } + Collections.sort(noiseCandidates); + return noiseCandidates; + } + private Pair, List> generateNoise(List examples, String sparqlQuery, double noise, Random randomGen) { // generate noise example candidates List noiseCandidateExamples = null; switch(noiseMethod) { case RANDOM: noiseCandidateExamples = generateNoiseCandidatesRandom(examples, 20); break; - case SIMILAR:noiseCandidateExamples = generateNoiseCandidatesSimilar(examples, sparqlQuery); + case SIMILAR:noiseCandidateExamples = generateNoiseCandidatesSimilar(examples, sparqlQuery, 20); break; case SIMILARITY_PARAMETERIZED://TODO implement configurable noise method break; @@ -784,7 +1187,7 @@ private Pair, List> generateNoise(List examples, St if (probabilityBased) { // 1. way - List newExamples = new ArrayList(); + List newExamples = new ArrayList<>(); for (Iterator iterator = examples.iterator(); iterator.hasNext();) { String posExample = iterator.next(); double rnd = randomGen.nextDouble(); @@ -810,8 +1213,8 @@ private Pair, List> generateNoise(List examples, St List posExamples2Replace = new ArrayList<>(examples.subList(0, nrOfPosExamples2Replace)); examples.removeAll(posExamples2Replace); List negExamples4Replacement = noiseCandidateExamples.subList(0, nrOfPosExamples2Replace); - List noiseExamples = new ArrayList(negExamples4Replacement); - List correctExamples = new ArrayList(examples); + List noiseExamples = new ArrayList<>(negExamples4Replacement); + List correctExamples = new ArrayList<>(examples); examples.addAll(negExamples4Replacement); logger.info("replaced " + posExamples2Replace + " by " + negExamples4Replacement); @@ -820,14 +1223,15 @@ private Pair, List> generateNoise(List examples, St } /** - * Randomly pick {@code n} instances from KB that do not belong to given examples {@code examples}. - * @param examples the examples that must not be contained in the returned list - * @param n the number of random examples + * Randomly pick {@code n} instances from KB that do not belong to given set of instances {@code examples}. + * @param examples the instances that must not be contained in the returned list + * @param n the number of random instances * @return */ private List generateNoiseCandidatesRandom(List examples, int n) { - List noiseExamples = new ArrayList<>(); + List noiseExampleCandidates = new ArrayList<>(); + rnd.reSeed(123); // get max number of instances in KB String query = "SELECT (COUNT(*) AS ?cnt) WHERE {[] a ?type . ?type a .}"; QueryExecution qe = qef.createQueryExecution(query); @@ -835,9 +1239,9 @@ private List generateNoiseCandidatesRandom(List examples, int n) int max = rs.next().get("cnt").asLiteral().getInt(); // generate random instances - while(noiseExamples.size() < n) { + while(noiseExampleCandidates.size() < n) { int offset = rnd.nextInt(0, max); - query = "SELECT ?s WHERE {?s a [] .} LIMIT 1 OFFSET " + offset; + query = "SELECT ?s WHERE {?s a ?type . ?type a .} LIMIT 1 OFFSET " + offset; qe = qef.createQueryExecution(query); rs = qe.execSelect(); @@ -845,22 +1249,24 @@ private List generateNoiseCandidatesRandom(List examples, int n) String resource = rs.next().getResource("s").getURI(); if(!examples.contains(resource) && !resource.contains("__")) { - noiseExamples.add(resource); + noiseExampleCandidates.add(resource); } qe.close(); } - return noiseExamples; + return noiseExampleCandidates; } - private List generateNoiseCandidatesSimilar(List examples, String queryString){ + private List generateNoiseCandidatesSimilar(List examples, String queryString, int limit){ + List negExamples = new ArrayList<>(); + Query query = QueryFactory.create(queryString); QueryUtils queryUtils = new QueryUtils(); Set triplePatterns = queryUtils.extractTriplePattern(query); - Set negExamples = new HashSet(); + Set negExamplesSet = new TreeSet<>(); if(triplePatterns.size() == 1){ Triple tp = triplePatterns.iterator().next(); @@ -885,7 +1291,7 @@ private List generateNoiseCandidatesSimilar(List examples, Strin // System.out.println(q); List result = getResult(q.toString()); - negExamples.addAll(result); + negExamplesSet.addAll(result); } else { // we modify each triple pattern by . ?var != o Set> powerSet = new TreeSet<>(new Comparator>() { @@ -901,7 +1307,7 @@ public int compare(Set o1, Set o2) { if(!set.isEmpty() && set.size() != triplePatterns.size()){ List existingTriplePatterns = new ArrayList<>(triplePatterns); List newTriplePatterns = new ArrayList<>(); - List filters = new ArrayList(); + List filters = new ArrayList<>(); int cnt = 0; for (Triple tp : set) { if(tp.getObject().isURI() || tp.getObject().isLiteral()){ @@ -925,7 +1331,7 @@ public int compare(Set o1, Set o2) { q.setQuerySelectType(); q.setDistinct(true); q.addProjectVars(query.getProjectVars()); - List allTriplePatterns = new ArrayList(existingTriplePatterns); + List allTriplePatterns = new ArrayList<>(existingTriplePatterns); allTriplePatterns.addAll(newTriplePatterns); ElementTriplesBlock tripleBlock = new ElementTriplesBlock(BasicPattern.wrap(allTriplePatterns)); ElementGroup eg = new ElementGroup(); @@ -960,21 +1366,22 @@ public int compare(Set o1, Set o2) { result = getResult(q.toString()); result.removeAll(examples); } - negExamples.addAll(result); + negExamplesSet.addAll(result); } } } - negExamples.removeAll(examples); + negExamplesSet.removeAll(examples); if(negExamples.isEmpty()){ logger.error("Found no negative example."); System.exit(0); } - return new ArrayList<>(negExamples); + negExamples.addAll(negExamplesSet); + return new ArrayList<>(negExamples).subList(0, Math.min(negExamples.size(), limit)); } private List getQueryTrees(List resources){ - List trees = new ArrayList(); + List trees = new ArrayList<>(); for (String resource : resources) { trees.add(getQueryTree(resource)); @@ -988,6 +1395,8 @@ private RDFResourceTree getQueryTree(String resource){ MonitorFactory.getTimeMonitor(TimeMonitors.CBD_RETRIEVAL.name()).start(); Model cbd = cbdGen.getConciseBoundedDescription(resource); MonitorFactory.getTimeMonitor(TimeMonitors.CBD_RETRIEVAL.name()).stop(); + + // rewrite NAN to NaN to avoid parse exception try(ByteArrayOutputStream baos = new ByteArrayOutputStream()){ cbd.write(baos, "N-TRIPLES", null); String modelAsString = new String(baos.toByteArray()); @@ -1003,6 +1412,7 @@ private RDFResourceTree getQueryTree(String resource){ MonitorFactory.getTimeMonitor(TimeMonitors.TREE_GENERATION.name()).start(); RDFResourceTree tree = queryTreeFactory.getQueryTree(resource, cbd); MonitorFactory.getTimeMonitor(TimeMonitors.TREE_GENERATION.name()).stop(); + return tree; } @@ -1014,7 +1424,7 @@ private List getResult(String sparqlQuery, boolean useCache){ logger.trace(sparqlQuery); List resources = cache.get(sparqlQuery); if(resources == null || !useCache) { - resources = new ArrayList(); + resources = new ArrayList<>(); // sparqlQuery = getPrefixedQuery(sparqlQuery); // we assume a single projection var @@ -1052,7 +1462,7 @@ private List getResultSplitted(String sparqlQuery){ // remove triple patterns with unbound object vars if(triplePatterns.size() > 10) { - query = removeUnboundObjectVarTriples(query); + query = queryUtils.removeUnboundObjectVarTriples(query); triplePatterns = queryUtils.extractTriplePattern(query); } @@ -1068,7 +1478,7 @@ private List getResultSplitted(String sparqlQuery){ Var targetVar = query.getProjectVars().get(0); // should be ?x0 - Multimap var2TriplePatterns = HashMultimap.create(); + final Multimap var2TriplePatterns = HashMultimap.create(); for (Triple tp : triplePatterns) { var2TriplePatterns.put(Var.alloc(tp.getSubject()), tp); } @@ -1078,8 +1488,8 @@ private List getResultSplitted(String sparqlQuery){ // 1. get the outgoing triple patterns of the target var that do not have // outgoing triple patterns - Set fixedTriplePatterns = new HashSet(); - Set> clusters = new HashSet>(); + Set fixedTriplePatterns = new HashSet<>(); + Set> clusters = new HashSet<>(); Collection targetVarTriplePatterns = var2TriplePatterns.get(targetVar); boolean useSplitting = false; for (Triple tp : targetVarTriplePatterns) { @@ -1117,7 +1527,7 @@ public int compare(Triple o1, Triple o2) { // again split clusters to have only a maximum number of triple patterns int maxNrOfTriplePatternsPerQuery = 20;// number of outgoing triple patterns form the target var in each executed query - Set> newClusters = new HashSet>(); + Set> newClusters = new HashSet<>(); for (Set cluster : clusters) { int cnt = 0; for (Triple triple : cluster) { @@ -1127,14 +1537,14 @@ public int compare(Triple o1, Triple o2) { } if(cnt > maxNrOfTriplePatternsPerQuery) { - Set newCluster = new HashSet(); + Set newCluster = new HashSet<>(); for (Triple triple : cluster) { if(triple.getSubject().matches(targetVar)) { newCluster.add(triple); } if(newCluster.size() == maxNrOfTriplePatternsPerQuery) { newClusters.add(newCluster); - newCluster = new HashSet(); + newCluster = new HashSet<>(); } } if(!newCluster.isEmpty()) { @@ -1142,18 +1552,18 @@ public int compare(Triple o1, Triple o2) { } } } + for (Set cluster : newClusters) { - - for(int i = 1; i < maxDepth; i++) { - Set additionalTriples = new HashSet(); + for(int i = 1; i < maxTreeDepth; i++) { + Set additionalTriples = new HashSet<>(); for (Triple triple : cluster) { if(triple.getObject().isVariable()){ - additionalTriples.addAll(var2TriplePatterns.get(Var.alloc(triple.getObject()))); + Collection triples = var2TriplePatterns.get(Var.alloc(triple.getObject())); + additionalTriples.addAll(triples); } } cluster.addAll(additionalTriples); } - } // clusters = newClusters; @@ -1162,6 +1572,25 @@ public int compare(Triple o1, Triple o2) { Set resources = null; // 3. run query for each cluster for (Set cluster : clusters) { + // remove redundant edges + SortedSet tmp = new TreeSet<>(new Comparator() { + + TripleComparator comp = new TripleComparator(); + + @Override + public int compare(Triple o1, Triple o2) { + boolean same = o1.subjectMatches(o2.getSubject()) + && o2.predicateMatches(o2.getPredicate()) + && o1.getObject().isVariable() && o2.getObject().isVariable(); +// && !var2TriplePatterns.containsKey(o1.getObject()); + if (same) return 0; + return comp.compare(o1, o2); + } + }); + tmp.addAll(cluster); + cluster = tmp; + + // build query Query q = new Query(); q.addProjectVars(Collections.singleton(targetVar)); ElementTriplesBlock el = new ElementTriplesBlock(); @@ -1172,13 +1601,13 @@ public int compare(Triple o1, Triple o2) { q.setDistinct(true); q.setQueryPattern(el); - q = rewriteForVirtuosoDateLiteralBug(q); + q = VirtuosoUtils.rewriteForVirtuosoDateLiteralBug(q); // q = rewriteForVirtuosoFloatingPointIssue(q); logger.trace(q); // sparqlQuery = getPrefixedQuery(sparqlQuery); System.out.println(q); List partialResult = getResult(q.toString()); - Set resourcesTmp = new HashSet(partialResult); + Set resourcesTmp = new HashSet<>(partialResult); if(resourcesTmp.isEmpty()) { System.err.println("Empty query result"); @@ -1194,45 +1623,14 @@ public int compare(Triple o1, Triple o2) { } } - return new ArrayList(resources); - } - - private Query removeUnboundObjectVarTriples(Query query) { - QueryUtils queryUtils = new QueryUtils(); - Set triplePatterns = queryUtils.extractTriplePattern(query); - - Multimap var2TriplePatterns = HashMultimap.create(); - for (Triple tp : triplePatterns) { - var2TriplePatterns.put(Var.alloc(tp.getSubject()), tp); - } - - Iterator iterator = triplePatterns.iterator(); - while (iterator.hasNext()) { - Triple triple = iterator.next(); - Node object = triple.getObject(); - if(object.isVariable() && !var2TriplePatterns.containsKey(Var.alloc(object))) { - iterator.remove(); - } - } - - Query newQuery = new Query(); - newQuery.addProjectVars(query.getProjectVars()); - ElementTriplesBlock el = new ElementTriplesBlock(); - for (Triple triple : triplePatterns) { - el.addTriple(triple); - } - newQuery.setQuerySelectType(); - newQuery.setDistinct(true); - newQuery.setQueryPattern(el); - - return newQuery; + return new ArrayList<>(resources); } private void filterOutGeneralTypes(Multimap var2Triples) { // keep the most specific types for each subject for (Var subject : var2Triples.keySet()) { Collection triplePatterns = var2Triples.get(subject); - Collection triplesPatterns2Remove = new HashSet(); + Collection triplesPatterns2Remove = new HashSet<>(); for (Triple tp : triplePatterns) { if (tp.getObject().isURI() && !triplesPatterns2Remove.contains(tp)) { @@ -1254,7 +1652,7 @@ private void filterOutGeneralTypes(Multimap var2Triples) { } private Set getSuperClasses(Node cls){ - Set superClasses = new HashSet(); + Set superClasses = new HashSet<>(); superClassesQueryTemplate.setIri("sub", cls.getURI()); @@ -1358,120 +1756,7 @@ public int compare(Triple o1, Triple o2) { return q; } - private Query rewriteForVirtuosoDateLiteralBug(Query query){ - final Query copy = QueryFactory.create(query); - final Element queryPattern = copy.getQueryPattern(); - final List filters = new ArrayList<>(); - ElementWalker.walk(queryPattern, new ElementVisitorBase() { - - int cnt = 0; - - @Override - public void visit(ElementGroup el) { - super.visit(el); - } - - @Override - public void visit(ElementTriplesBlock el) { - Set newTriplePatterns = new TreeSet<>(new Comparator() { - @Override - public int compare(Triple o1, Triple o2) { - return ComparisonChain.start().compare(o1.getSubject().toString(), o2.getSubject().toString()) - .compare(o1.getPredicate().toString(), o2.getPredicate().toString()) - .compare(o1.getObject().toString(), o2.getObject().toString()).result(); - } - }); - - Iterator iterator = el.patternElts(); - while (iterator.hasNext()) { - Triple tp = iterator.next(); - - if (tp.getObject().isLiteral()) { - RDFDatatype dt = tp.getObject().getLiteralDatatype(); - if (dt != null && dt instanceof XSDAbstractDateTimeType) { - iterator.remove(); - // new triple pattern - Node objectVar = NodeFactory.createVariable("date" + cnt++); - newTriplePatterns.add(Triple.create(tp.getSubject(), tp.getPredicate(), objectVar)); - - String lit = tp.getObject().getLiteralLexicalForm(); - Object literalValue = tp.getObject().getLiteralValue(); - Expr filterExpr = new E_Equals(new E_Str(new ExprVar(objectVar)), NodeValue.makeString(lit)); - if (literalValue instanceof XSDDateTime) { - Calendar calendar = ((XSDDateTime) literalValue).asCalendar(); - Date date = new Date(calendar.getTimeInMillis() + TimeUnit.HOURS.toMillis(2)); - SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd"); - String inActiveDate = format1.format(date); - filterExpr = new E_LogicalOr(filterExpr, new E_Equals( - new E_Str(new ExprVar(objectVar)), NodeValue.makeString(inActiveDate))); - } - ElementFilter filter = new ElementFilter(filterExpr); - filters.add(filter); - } - } - } - - for (Triple tp : newTriplePatterns) { - el.addTriple(tp); - } - - for (ElementFilter filter : filters) { - ((ElementGroup)queryPattern).addElementFilter(filter); - } - } - - @Override - public void visit(ElementPathBlock el) { - Set newTriplePatterns = new TreeSet<>(new Comparator() { - @Override - public int compare(Triple o1, Triple o2) { - return ComparisonChain.start().compare(o1.getSubject().toString(), o2.getSubject().toString()) - .compare(o1.getPredicate().toString(), o2.getPredicate().toString()) - .compare(o1.getObject().toString(), o2.getObject().toString()).result(); - } - }); - Iterator iterator = el.patternElts(); - while (iterator.hasNext()) { - Triple tp = iterator.next().asTriple(); - - if (tp.getObject().isLiteral()) { - RDFDatatype dt = tp.getObject().getLiteralDatatype(); - if (dt != null && dt instanceof XSDAbstractDateTimeType) { - iterator.remove(); - // new triple pattern - Node objectVar = NodeFactory.createVariable("date" + cnt++); - newTriplePatterns.add(Triple.create(tp.getSubject(), tp.getPredicate(), objectVar)); - - String lit = tp.getObject().getLiteralLexicalForm(); - Object literalValue = tp.getObject().getLiteralValue(); - Expr filterExpr = new E_Equals(new E_Str(new ExprVar(objectVar)), NodeValue.makeString(lit)); - if (literalValue instanceof XSDDateTime) { - Calendar calendar = ((XSDDateTime) literalValue).asCalendar(); - Date date = new Date(calendar.getTimeInMillis() + TimeUnit.HOURS.toMillis(2)); - SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd"); - String inActiveDate = format1.format(date); - filterExpr = new E_LogicalOr(filterExpr, new E_Equals( - new E_Str(new ExprVar(objectVar)), NodeValue.makeString(inActiveDate))); - } - ElementFilter filter = new ElementFilter(filterExpr); - filters.add(filter); - } - - } - } - - for (Triple tp : newTriplePatterns) { - el.addTriple(tp); - } - - } - }); - for (ElementFilter filter : filters) { - ((ElementGroup)queryPattern).addElementFilter(filter); - } - return copy; - } private Score computeScore(String referenceSparqlQuery, RDFResourceTree tree, double noise) throws Exception{ // apply some filters @@ -1493,33 +1778,13 @@ private Score computeScore(String referenceSparqlQuery, RDFResourceTree tree, do // if query is most general one P=|TARGET|/|KB| R=1 if (learnedSPARQLQuery.equals(QueryTreeUtils.EMPTY_QUERY_TREE_QUERY)) { - double precision = referenceResources.size() / (double) kbSize; - if(Double.isNaN(precision)){ - System.err.println("ERROR!!!" + learnedSPARQLQuery); - throw new Exception("Precision not a number."); - } - double recall = 1.0; - double fMeasure = Heuristics.getFScore(recall, precision); int tp = referenceResources.size(); int fp = kbSize - tp; int tn = 0; int fn = 0; - double predAcc = (tp + tn) / (double)((tp + fn) + (tn + fp)); - BigDecimal denominator = BigDecimal.valueOf(tp + fp). - multiply(BigDecimal.valueOf(tp + fn)). - multiply(BigDecimal.valueOf(tn + fp)). - multiply(BigDecimal.valueOf(tn + fn)); - double mathCorr = denominator.doubleValue() == 0 ? 0 : (tp * tn - fp * fn) / Math.sqrt(denominator.doubleValue()); - - if(Double.isNaN(predAcc) || Double.isNaN(mathCorr)){ - System.err.println("ERROR!!!\n" + learnedSPARQLQuery + "\n" + (Double.isNaN(predAcc) ? ("PredAcc") : ("MC") + "Not a number.")); - System.err.println(tp + "|" + fp + "|" + fn + "|" + tn); - throw new Exception(Double.isNaN(predAcc) ? ("PredAcc") : ("MC") + "Not a number."); - } - - return new Score(precision, recall, fMeasure, predAcc, mathCorr); + return score(tp, fp, tn, fn); } // get the learned resources @@ -1532,33 +1797,12 @@ private Score computeScore(String referenceSparqlQuery, RDFResourceTree tree, do // get the overlapping resources int overlap = Sets.intersection(Sets.newHashSet(referenceResources), Sets.newHashSet(learnedResources)).size(); - double precision = overlap / (double) learnedResources.size(); - if(Double.isNaN(precision)){ - System.err.println("ERROR!!!Precision not a number" + learnedSPARQLQuery); - throw new Exception("Precision not a number."); - } - double recall = overlap / (double) referenceResources.size(); - double fMeasure = Heuristics.getFScore(recall, precision); - int tp = overlap; int fp = Sets.difference(Sets.newHashSet(learnedResources), Sets.newHashSet(referenceResources)).size(); int fn = Sets.difference(Sets.newHashSet(referenceResources), Sets.newHashSet(learnedResources)).size(); int tn = kbSize - tp - fp - fn; - double predAcc = (tp + tn) / (double)((tp + fn) + (tn + fp)); - BigDecimal denominator = BigDecimal.valueOf(tp + fp). - multiply(BigDecimal.valueOf(tp + fn)). - multiply(BigDecimal.valueOf(tn + fp)). - multiply(BigDecimal.valueOf(tn + fn)); - double mathCorr = denominator.doubleValue() == 0 ? 0 : (tp * tn - fp * fn) / Math.sqrt(denominator.doubleValue()); - - if(Double.isNaN(predAcc) || Double.isNaN(mathCorr)){ - System.err.println("ERROR!!!Precision not a number\n" + learnedSPARQLQuery + "\n" + (Double.isNaN(predAcc) ? ("PredAcc") : ("MC") + "Not a number.")); - System.err.println(tp + "|" + fp + "|" + fn + "|" + tn); - throw new Exception(Double.isNaN(predAcc) ? ("PredAcc") : ("MC") + "Not a number."); - } - - return new Score(precision, recall, fMeasure, predAcc, mathCorr); + return score(tp, fp, tn, fn); } private Score computeScoreBySparqlCount(String referenceSparqlQuery, RDFResourceTree tree, double noise) throws Exception{ @@ -1582,33 +1826,13 @@ private Score computeScoreBySparqlCount(String referenceSparqlQuery, RDFResource // if query is most general one P=|TARGET|/|KB| R=1 if (learnedSPARQLQuery.equals(QueryTreeUtils.EMPTY_QUERY_TREE_QUERY)) { - double precision = referenceCnt / (double) kbSize; - if(Double.isNaN(precision)){ - System.err.println("ERROR!!!Precision not a number" + learnedSPARQLQuery); - throw new Exception("Precision not a number."); - } - double recall = 1.0; - double fMeasure = Heuristics.getFScore(recall, precision); int tp = referenceCnt; int fp = kbSize - tp; int tn = 0; int fn = 0; - double predAcc = (tp + tn) / (double)((tp + fn) + (tn + fp)); - BigDecimal denominator = BigDecimal.valueOf(tp + fp). - multiply(BigDecimal.valueOf(tp + fn)). - multiply(BigDecimal.valueOf(tn + fp)). - multiply(BigDecimal.valueOf(tn + fn)); - double mathCorr = denominator.doubleValue() == 0 ? 0 : (tp * tn - fp * fn) / Math.sqrt(denominator.doubleValue()); - - if(Double.isNaN(predAcc) || Double.isNaN(mathCorr)){ - System.err.println("ERROR!!!\n" + learnedSPARQLQuery + "\n" + (Double.isNaN(predAcc) ? ("PredAcc") : ("MC") + "Not a number.")); - System.err.println(tp + "|" + fp + "|" + fn + "|" + tn); - throw new Exception(Double.isNaN(predAcc) ? ("PredAcc") : ("MC") + "Not a number."); - } - - return new Score(precision, recall, fMeasure, predAcc, mathCorr); + return score(tp, fp, tn, fn); } // Q2 @@ -1618,7 +1842,7 @@ private Score computeScoreBySparqlCount(String referenceSparqlQuery, RDFResource q2Count.getProject().add(cntVar, new ExprAggregator(s.asVar(), new AggCountVarDistinct(s))); q2Count.setQueryPattern(q2.getQueryPattern()); logger.info("Learned query:\n" + q2Count); - q2Count = rewriteForVirtuosoDateLiteralBug(q2Count); + q2Count = VirtuosoUtils.rewriteForVirtuosoDateLiteralBug(q2Count); qe = qef.createQueryExecution(q2Count); rs = qe.execSelect(); qs = rs.next(); @@ -1628,7 +1852,7 @@ private Score computeScoreBySparqlCount(String referenceSparqlQuery, RDFResource // Q1 ∪ Q2 // if noise = 0 then Q1 ∪ Q2 = Q2 - int overlap = learnedCnt; + int overlap = Math.min(learnedCnt, referenceCnt); if(noise > 0) { Query q12 = QueryFactory.create(); q12.setQuerySelectType(); @@ -1642,7 +1866,7 @@ private Score computeScoreBySparqlCount(String referenceSparqlQuery, RDFResource } q12.setQueryPattern(whereClause); logger.info("Combined query:\n" + q12); - q12 = rewriteForVirtuosoDateLiteralBug(q12); + q12 = VirtuosoUtils.rewriteForVirtuosoDateLiteralBug(q12); qe = qef.createQueryExecution(q12); rs = qe.execSelect(); qs = rs.next(); @@ -1650,89 +1874,126 @@ private Score computeScoreBySparqlCount(String referenceSparqlQuery, RDFResource qe.close(); } - double precision = overlap / (double) learnedCnt; - if(Double.isNaN(precision)){ - System.err.println("ERRORPrecision not a number!!!" + learnedSPARQLQuery); - throw new Exception("Precision not a number."); - } - double recall = overlap / (double) referenceCnt; - double fMeasure = Heuristics.getFScore(recall, precision); - int tp = overlap; int fp = learnedCnt - overlap; int fn = referenceCnt - overlap; int tn = kbSize - tp - fp - fn; + return score(tp, fp, tn, fn); + } + + private Score score(int tp, int fp, int tn, int fn) throws Exception { + // P + double precision = (tp == 0 && fp == 0) ? 1.0 : (double) tp / (tp + fp); + + // R + double recall = (tp == 0 && fn == 0) ? 1.0 : (double) tp / (tp + fn); + + //F_1 + double fMeasure = Heuristics.getFScore(recall, precision); + + // pred. acc double predAcc = (tp + tn) / (double)((tp + fn) + (tn + fp)); + BigDecimal denominator = BigDecimal.valueOf(tp + fp). multiply(BigDecimal.valueOf(tp + fn)). multiply(BigDecimal.valueOf(tn + fp)). multiply(BigDecimal.valueOf(tn + fn)); + + // Mathews CC double mathCorr = denominator.doubleValue() == 0 ? 0 : (tp * tn - fp * fn) / Math.sqrt(denominator.doubleValue()); if(Double.isNaN(predAcc) || Double.isNaN(mathCorr)){ - System.err.println("ERROR!!!\n" + learnedSPARQLQuery + "\n" + (Double.isNaN(predAcc) ? ("PredAcc") : ("MC") + "Not a number.")); - System.err.println(tp + "|" + fp + "|" + fn + "|" + tn); - throw new Exception(Double.isNaN(predAcc) ? ("PredAcc") : ("MC") + "Not a number."); + throw new Exception(Double.isNaN(predAcc) ? ("PredAcc") : ("MC") + " not a number."); } return new Score(precision, recall, fMeasure, predAcc, mathCorr); } - private synchronized void write2DB(String targetQuery, int nrOfExamples, ExamplesWrapper examples, double noise, String heuristic, String heuristicMeasure, - String queryTop, double fmeasureTop, double precisionTop, double recallTop, - String bestQuery, int bestRank, double bestFmeasure, double bestPrecision, double bestRecall - ) { - try { + private void write2DB(String targetQuery, int nrOfExamples, ExamplesWrapper examples, + double noise, String baseLineQuery, Score baselineScore, String heuristicName, String heuristicMeasure, + String returnedQuery, Score returnedQueryScore, long returnedRuntime, String bestQuery, int bestQueryPosition, + Score bestQueryScore) { + logger.trace("Writing to DB..."); + try { psInsertDetailEval.setString(1, targetQuery); psInsertDetailEval.setInt(2, nrOfExamples); psInsertDetailEval.setDouble(3, noise); - psInsertDetailEval.setString(4, heuristic); + psInsertDetailEval.setString(4, heuristicName); psInsertDetailEval.setString(5, heuristicMeasure); - psInsertDetailEval.setString(6, queryTop); - psInsertDetailEval.setDouble(7, fmeasureTop); - psInsertDetailEval.setDouble(8, precisionTop); - psInsertDetailEval.setDouble(9, recallTop); + + psInsertDetailEval.setString(6, returnedQuery); + psInsertDetailEval.setDouble(7, returnedQueryScore.fmeasure); + psInsertDetailEval.setDouble(8, returnedQueryScore.precision); + psInsertDetailEval.setDouble(9, returnedQueryScore.recall); + psInsertDetailEval.setString(10, bestQuery); - psInsertDetailEval.setInt(11, bestRank); - psInsertDetailEval.setDouble(12, bestFmeasure); - psInsertDetailEval.setDouble(13, bestPrecision); - psInsertDetailEval.setDouble(14, bestRecall); - System.out.println(psInsertDetailEval); + psInsertDetailEval.setInt(11, bestQueryPosition); + psInsertDetailEval.setDouble(12, bestQueryScore.fmeasure); + psInsertDetailEval.setDouble(13, bestQueryScore.precision); + psInsertDetailEval.setDouble(14, bestQueryScore.recall); + + psInsertDetailEval.setString(15, baseLineQuery); + psInsertDetailEval.setDouble(16, baselineScore.fmeasure); + psInsertDetailEval.setDouble(17, baselineScore.precision); + psInsertDetailEval.setDouble(18, baselineScore.recall); + + psInsertDetailEval.setLong(19, returnedRuntime); + + logger.trace(psInsertDetailEval); psInsertDetailEval.executeUpdate(); + logger.trace("...finished writing to DB."); } catch (Exception e) { - e.printStackTrace(); + logger.error("Writing to DB failed with " + psInsertDetailEval, e); } + } - private synchronized void write2DB(String heuristic, String heuristicMeasure, int nrOfExamples, double noise, + private synchronized void write2DB( + String heuristic, String heuristicMeasure, int nrOfExamples, double noise, double fmeasure, double precision, double recall, double predAcc, double mathCorr, - double bestSolutionPosition, double bestSolutionFmeasure, double bestSolutionPrecision, double bestSolutionRecall + double bestSolutionPosition, double bestSolutionFmeasure, double bestSolutionPrecision, double bestSolutionRecall, double bestSolutionPredAcc, double bestSolutionMathCorr, + double baselineFmeasure, double baselinePrecision, double baselineRecall, double baselinePredAcc, double baselineMathCorr, + double bestReturnedSolutionRuntime ) { + logger.trace("Writing to DB..."); try { psInsertOverallEval.setString(1, heuristic); psInsertOverallEval.setString(2, heuristicMeasure); psInsertOverallEval.setInt(3, nrOfExamples); psInsertOverallEval.setDouble(4, noise); + psInsertOverallEval.setDouble(5, fmeasure); psInsertOverallEval.setDouble(6, precision); psInsertOverallEval.setDouble(7, recall); psInsertOverallEval.setDouble(8, predAcc); psInsertOverallEval.setDouble(9, mathCorr); + psInsertOverallEval.setDouble(10, bestSolutionPosition); psInsertOverallEval.setDouble(11, bestSolutionFmeasure); psInsertOverallEval.setDouble(12, bestSolutionPrecision); psInsertOverallEval.setDouble(13, bestSolutionRecall); + psInsertOverallEval.setDouble(14, bestSolutionPredAcc); + psInsertOverallEval.setDouble(15, bestSolutionMathCorr); - System.out.println(psInsertOverallEval); + psInsertOverallEval.setDouble(16, baselineFmeasure); + psInsertOverallEval.setDouble(17, baselinePrecision); + psInsertOverallEval.setDouble(18, baselineRecall); + psInsertOverallEval.setDouble(19, baselinePredAcc); + psInsertOverallEval.setDouble(20, baselineMathCorr); + + psInsertOverallEval.setDouble(21, bestReturnedSolutionRuntime); + + logger.trace(psInsertOverallEval); psInsertOverallEval.executeUpdate(); + logger.trace("...finished writing to DB."); } catch (Exception e) { - e.printStackTrace(); + logger.error("Writing to DB failed with " + psInsertOverallEval, e); } } public static void main(String[] args) throws Exception { - ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); + StringRenderer.setRenderer(Rendering.DL_SYNTAX); Logger.getLogger(QTLEvaluation.class).addAppender( new FileAppender(new SimpleLayout(), "log/qtl-qald.log", false)); Logger.getRootLogger().setLevel(Level.INFO); @@ -1740,21 +2001,83 @@ public static void main(String[] args) throws Exception { Logger.getLogger(QTLEvaluation.class).setLevel(Level.INFO); Logger.getLogger(QueryExecutionFactoryCacheEx.class).setLevel(Level.INFO); - if(args.length < 2) { - System.out.println("Usage: QTLEvaluation "); - System.exit(0); + OptionParser parser = new OptionParser(); + OptionSpec benchmarkDirectorySpec = parser.accepts("d", "base directory").withRequiredArg().ofType(File.class).required(); + OptionSpec queriesFileSpec = parser.accepts("q", "processed queries file").withRequiredArg().ofType(File.class).required(); + OptionSpec endpointURLSpec = parser.accepts("e", "endpoint URL").withRequiredArg().ofType(URL.class).required(); + OptionSpec defaultGraphSpec = parser.accepts("g", "default graph").withRequiredArg().ofType(String.class); + OptionSpec overrideSpec = parser.accepts("o", "override previous results").withOptionalArg().ofType(Boolean.class).defaultsTo(Boolean.FALSE); + OptionSpec write2DBSpec = parser.accepts("db", "write to database").withOptionalArg().ofType(Boolean.class).defaultsTo(Boolean.FALSE); + OptionSpec emailNotificationSpec = parser.accepts("mail", "enable email notification").withOptionalArg().ofType(Boolean.class).defaultsTo(Boolean.FALSE); + OptionSpec maxNrOfQueriesSpec = parser.accepts("max-queries", "max. nr. of processed queries").withRequiredArg().ofType(Integer.class).defaultsTo(-1); + OptionSpec maxTreeDepthSpec = parser.accepts("max-tree-depth", "max. depth of processed queries and generated trees").withRequiredArg().ofType(Integer.class).defaultsTo(3); + OptionSpec maxQTLRuntimeSpec = parser.accepts("max-qtl-runtime", "max. runtime of each QTL run").withRequiredArg().ofType(Integer.class).defaultsTo(10).required(); + OptionSpec nrOfThreadsSpec = parser.accepts("thread-count", "number of threads used for parallel evaluation").withRequiredArg().ofType(Integer.class).defaultsTo(1); + + OptionSpec exampleIntervalsSpec = parser.accepts("examples", "comma-separated list of number of examples used in evaluation").withRequiredArg().ofType(String.class); + OptionSpec noiseIntervalsSpec = parser.accepts("noise", "comma-separated list of noise values used in evaluation").withRequiredArg().ofType(String.class); + OptionSpec measuresSpec = parser.accepts("measures", "comma-separated list of measures used in evaluation").withRequiredArg().ofType(String.class); + + + + OptionSet options = parser.parse(args); + + File benchmarkDirectory = options.valueOf(benchmarkDirectorySpec); + File queriesFile = options.valueOf(queriesFileSpec); + boolean write2DB = options.valueOf(write2DBSpec); + boolean override = options.valueOf(overrideSpec); + boolean useEmailNotification = options.valueOf(emailNotificationSpec); + URL endpointURL = options.valueOf(endpointURLSpec); + String defaultGraph = options.has(defaultGraphSpec) ? options.valueOf(defaultGraphSpec) : null; + SparqlEndpoint endpoint = SparqlEndpoint.create(endpointURL.toString(), defaultGraph); + int maxNrOfQueries = options.valueOf(maxNrOfQueriesSpec); + int maxTreeDepth = options.valueOf(maxTreeDepthSpec); + int maxQTLRuntime = options.valueOf(maxQTLRuntimeSpec); + int nrOfThreads = options.valueOf(nrOfThreadsSpec); + + int[] exampleInterval = null; + if(options.has(exampleIntervalsSpec)) { + String s = options.valueOf(exampleIntervalsSpec); + String[] split = s.split(","); + exampleInterval = new int[split.length]; + for(int i = 0; i < split.length; i++) { + exampleInterval[i] = Integer.valueOf(split[i]); + } + } + + double[] noiseInterval = null; + if(options.has(noiseIntervalsSpec)) { + String s = options.valueOf(noiseIntervalsSpec); + String[] split = s.split(","); + noiseInterval = new double[split.length]; + for(int i = 0; i < split.length; i++) { + noiseInterval[i] = Double.valueOf(split[i]); + } + } + + HeuristicType[] measures = null; + if(options.has(measuresSpec)) { + String s = options.valueOf(measuresSpec); + String[] split = s.split(","); + measures = new HeuristicType[split.length]; + for(int i = 0; i < split.length; i++) { + if(split[i].equalsIgnoreCase("mcc")) { + measures[i] = HeuristicType.MATTHEWS_CORRELATION; + } else { + measures[i] = HeuristicType.valueOf(split[i].toUpperCase()); + } + } } - - File benchmarkDirectory = new File(args[0]); - File queries = new File(args[1]); - boolean write2DB = Boolean.valueOf(args[2]); - - new QTLEvaluation(new DBpediaEvaluationDataset(), benchmarkDirectory, write2DB).run(queries); + + EvaluationDataset dataset = new DBpediaEvaluationDataset(benchmarkDirectory, endpoint); + QTLEvaluation eval = new QTLEvaluation(dataset, benchmarkDirectory, write2DB, override, maxQTLRuntime, useEmailNotification, nrOfThreads); + eval.run(queriesFile, maxNrOfQueries, maxTreeDepth, exampleInterval, noiseInterval, measures); // new QALDExperiment(Dataset.BIOMEDICAL).run(); } class Score { + int tp, fp, tn, fn = 0; double precision, recall, fmeasure, predAcc, mathCorr = 0; public Score() {} @@ -1767,22 +2090,6 @@ public Score(double precision, double recall, double fmeasure, double predAcc, d this.mathCorr = mathCorr; } - public double getPrecision() { - return precision; - } - public double getRecall() { - return recall; - } - public double getFmeasure() { - return fmeasure; - } - public double getPredAcc() { - return predAcc; - } - public double getMathCorr() { - return mathCorr; - } - /* (non-Javadoc) * @see java.lang.Object#toString() */ @@ -1793,247 +2100,98 @@ public String toString() { } - class NegativeExampleSPARQLQueryGenerator extends ElementVisitorBase{ - - private boolean inOptionalClause; - private Stack parentGroup = new Stack<>(); - private QueryUtils triplePatternExtractor = new QueryUtils(); - private Triple triple; - Random randomGen = new Random(123); - - public List getNegativeExamples(String targetQuery, int size) { - List negExamples = new ArrayList(); - - // remove triple patterns as long as enough neg examples have been found - Query query = QueryFactory.create(targetQuery); - - List queries = generateQueries(query); - - while(negExamples.size() < size && !queries.isEmpty()) { - - Query q = queries.remove(0); - q.setLimit(size); -// System.err.println(q); - QueryExecution qe = qef.createQueryExecution(q); - ResultSet rs = qe.execSelect(); - while(rs.hasNext()) { - QuerySolution qs = rs.next(); - String example = qs.getResource(query.getProjectVars().get(0).getName()).getURI(); - negExamples.add(example); - } - qe.close(); - } - - return negExamples; - } - - private ElementFilter getNotExistsFilter(Element el){ - return new ElementFilter(new E_NotExists(el)); + + + class ExampleCandidates { + + List correctPosExampleCandidates; + List falsePosExampleCandidates; + List correctNegExampleCandidates; + + public ExampleCandidates(List correctPosExampleCandidates, + List correctNegExampleCandidates, + List falsePosExampleCandidates) { + this.correctPosExampleCandidates = correctPosExampleCandidates; + this.falsePosExampleCandidates = falsePosExampleCandidates; + this.correctNegExampleCandidates = correctNegExampleCandidates; } - private List generateQueries(Query query) { - List queries = new ArrayList(); + public ExamplesWrapper get(int nrOfPosExamples, int nrOfNegExamples, double noise) { + Random rnd = new Random(123); - // extract paths - Node source = query.getProjectVars().get(0).asNode(); - List> paths = getPaths(new ArrayList(), query, source); + // random sublist of the pos. examples + List correctPosExamples = new ArrayList<>(correctPosExampleCandidates); + Collections.sort(correctPosExamples); + Collections.shuffle(correctPosExamples, rnd); + correctPosExamples = new ArrayList<>(correctPosExamples.subList(0, Math.min(correctPosExamples.size(), nrOfPosExamples))); - int index = 0; - for (List path : paths) { - if(path.size() == 1 && path.get(0).getPredicate().equals(RDF.type.asNode())) { - index = paths.indexOf(path); - } - } - List path1 = paths.get(index == 0 ? 1 : 0); - List typePath = paths.get(index); + // random sublist of the neg. examples + List negExamples = new ArrayList<>(correctNegExampleCandidates); + Collections.sort(negExamples); + Collections.shuffle(negExamples, rnd); + negExamples = new ArrayList<>(negExamples.subList(0, Math.min(negExamples.size(), nrOfNegExamples))); - // get last tp first - if(path1.size() == 2) { - // remove last edge - ElementGroup eg = new ElementGroup(); - ElementTriplesBlock existsBlock = new ElementTriplesBlock(); - existsBlock.addTriple(path1.get(0)); - existsBlock.addTriple(typePath.get(0)); - eg.addElement(existsBlock); - - ElementTriplesBlock notExistsBlock = new ElementTriplesBlock(); - notExistsBlock.addTriple(path1.get(1)); - ElementGroup notExistsGroup = new ElementGroup(); - notExistsGroup.addElement(notExistsBlock); - eg.addElementFilter(getNotExistsFilter(notExistsGroup)); - - Query newQuery = QueryFactory.create(); - newQuery.setQuerySelectType(); - newQuery.setQueryPattern(eg); - newQuery.addProjectVars(query.getProjectVars()); - newQuery.setDistinct(true); - queries.add(newQuery); - - //remove both edges - eg = new ElementGroup(); - existsBlock = new ElementTriplesBlock(); - existsBlock.addTriple(typePath.get(0)); - eg.addElement(existsBlock); - - notExistsBlock = new ElementTriplesBlock(); - notExistsBlock.addTriple(path1.get(0)); - notExistsBlock.addTriple(path1.get(1)); - notExistsGroup = new ElementGroup(); - notExistsGroup.addElement(notExistsBlock); - eg.addElementFilter(getNotExistsFilter(notExistsGroup)); - - newQuery = QueryFactory.create(); - newQuery.setQuerySelectType(); - newQuery.setQueryPattern(eg); - newQuery.addProjectVars(query.getProjectVars()); - newQuery.setDistinct(true); - queries.add(newQuery); - - } else { - //remove both edges - ElementGroup eg = new ElementGroup(); - ElementTriplesBlock existsBlock = new ElementTriplesBlock(); - existsBlock.addTriple(typePath.get(0)); - eg.addElement(existsBlock); - - ElementTriplesBlock notExistsBlock = new ElementTriplesBlock(); - notExistsBlock.addTriple(path1.get(0)); - ElementGroup notExistsGroup = new ElementGroup(); - notExistsGroup.addElement(notExistsBlock); - eg.addElementFilter(getNotExistsFilter(notExistsGroup)); - - Query newQuery = QueryFactory.create(); - newQuery.setQuerySelectType(); - newQuery.setQueryPattern(eg); - newQuery.addProjectVars(query.getProjectVars()); - newQuery.setDistinct(true); - queries.add(newQuery); - } - - return queries; - } - - private List> getPaths(List path, Query query, Node source) { - List> paths = new ArrayList>(); - Set outgoingTriplePatterns = QueryUtils.getOutgoingTriplePatterns(query, source); - for (Triple tp : outgoingTriplePatterns) { - List newPath = new ArrayList(path); - newPath.add(tp); - if(tp.getObject().isVariable()) { - paths.addAll(getPaths(newPath, query, tp.getObject())); - } else { - paths.add(newPath); - } - } - return paths; - } - /** - * Returns a modified SPARQL query such that it is similar but different by choosing one of the triple patterns and use - * the negation of its existence. - * @param query - */ - public Query generateSPARQLQuery(Query query){ - //choose a random triple for the modification - List triplePatterns = new ArrayList(triplePatternExtractor.extractTriplePattern(query)); - Collections.shuffle(triplePatterns, randomGen); - triple = triplePatterns.get(0); - - Query modifiedQuery = query.cloneQuery(); - modifiedQuery.getQueryPattern().visit(this); - logger.info("Negative examples query:\n" + modifiedQuery.toString()); - return modifiedQuery; - } - - @Override - public void visit(ElementGroup el) { - parentGroup.push(el); - for (Iterator iterator = new ArrayList(el.getElements()).iterator(); iterator.hasNext();) { - Element e = iterator.next(); - e.visit(this); - } - parentGroup.pop(); - } + List falsePosExamples = new ArrayList<>(); + if(noise > 0) { + // randomly replace some of the pos. examples by false examples + // 2 options + // 1: iterate over pos. examples and if random number is below t_n, replace the example + // 2: replace the (#posExamples * t_n) randomly chosen pos. examples by randomly chosen negative examples + List falsePosExampleCandidates = new ArrayList<>(this.falsePosExampleCandidates); + Collections.sort(falsePosExampleCandidates); + Collections.shuffle(falsePosExampleCandidates, rnd); - @Override - public void visit(ElementOptional el) { - inOptionalClause = true; - el.getOptionalElement().visit(this); - inOptionalClause = false; - } - @Override - public void visit(ElementTriplesBlock el) { - for (Iterator iterator = el.patternElts(); iterator.hasNext();) { - Triple t = iterator.next(); - if(inOptionalClause){ - - } else { - if(t.equals(triple)){ - ElementGroup parent = parentGroup.peek(); - ElementTriplesBlock elementTriplesBlock = new ElementTriplesBlock(); - elementTriplesBlock.addTriple(t); - ElementGroup eg = new ElementGroup(); - eg.addElement(elementTriplesBlock); - parent.addElement(new ElementFilter(new E_NotExists(eg))); - iterator.remove(); - } - } - } - } - @Override - public void visit(ElementPathBlock el) { - for (Iterator iterator = el.patternElts(); iterator.hasNext();) { - TriplePath tp = iterator.next(); - if(inOptionalClause){ - - } else { - if(tp.asTriple().equals(triple)){ - ElementGroup parent = parentGroup.peek(); - ElementPathBlock elementTriplesBlock = new ElementPathBlock(); - elementTriplesBlock.addTriple(tp); - ElementGroup eg = new ElementGroup(); - eg.addElement(elementTriplesBlock); - parent.addElement(new ElementFilter(new E_NotExists(eg))); - iterator.remove(); + boolean probabilityBased = false; + + if (probabilityBased) { + // 1. way + for (Iterator iterator = correctPosExamples.iterator(); iterator.hasNext();) { + String posExample = iterator.next(); + double rndVal = rnd.nextDouble(); + if (rndVal <= noise) { + // remove the positive example + iterator.remove(); + + // add one of the negative examples + String falsePosExample = falsePosExampleCandidates.remove(0); + falsePosExamples.add(falsePosExample); + logger.info("Replacing " + posExample + " by " + falsePosExample); + } } + } else { + // 2. way + // replace at least 1 but not more than half of the examples + int upperBound = correctPosExamples.size() / 2; + int nrOfPosExamples2Replace = Math.min((int) Math.ceil(noise * correctPosExamples.size()), upperBound); + + logger.info("replacing " + nrOfPosExamples2Replace + "/" + correctPosExamples.size() + " examples to introduce noise"); + List posExamples2Replace = new ArrayList<>(correctPosExamples.subList(0, nrOfPosExamples2Replace)); + correctPosExamples.removeAll(posExamples2Replace); + + falsePosExamples = falsePosExampleCandidates.subList(0, nrOfPosExamples2Replace); + logger.info("replaced " + posExamples2Replace + "\nby\n" + falsePosExamples); } } - } - - @Override - public void visit(ElementUnion el) { - for (Iterator iterator = el.getElements().iterator(); iterator.hasNext();) { - Element e = iterator.next(); - e.visit(this); + + // ensure determinism + Collections.sort(correctPosExamples); + Collections.sort(negExamples); + Collections.sort(falsePosExamples); + + // generate trees + SortedMap posExamplesMapping = new TreeMap<>(); + for (String ex : ListUtils.union(correctPosExamples, falsePosExamples)) { + posExamplesMapping.put(new OWLNamedIndividualImpl(IRI.create(ex)), getQueryTree(ex)); } + SortedMap negExamplesMapping = new TreeMap<>(); + for (String ex : negExamples) { + negExamplesMapping.put(new OWLNamedIndividualImpl(IRI.create(ex)), getQueryTree(ex)); + } + + return new ExamplesWrapper(correctPosExamples, falsePosExamples, negExamples, posExamplesMapping, negExamplesMapping); } - - @Override - public void visit(ElementFilter el) { - } - - } - - class ExamplesWrapper { - List correctPosExamples; - List noisePosExamples; - List correctNegExamples; - Map posExamplesMapping; - Map negExamplesMapping; - - public ExamplesWrapper(List correctPosExamples, List noisePosExamples,List correctNegExamples, - Map posExamplesMapping, Map negExamplesMapping) { - super(); - this.correctPosExamples = correctPosExamples; - this.noisePosExamples = noisePosExamples; - this.correctNegExamples = correctNegExamples; - this.posExamplesMapping = posExamplesMapping; - this.negExamplesMapping = negExamplesMapping; - } - - } - } diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/SPARQLLearningProblemsGenerator.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/SPARQLLearningProblemsGenerator.java new file mode 100644 index 0000000000..1047bdb381 --- /dev/null +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/SPARQLLearningProblemsGenerator.java @@ -0,0 +1,196 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.algorithms.qtl.experiments; + +import com.google.common.base.Charsets; +import com.google.common.collect.Sets; +import com.google.common.io.Files; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; +import com.hp.hpl.jena.sparql.core.Var; +import org.apache.commons.math3.random.JDKRandomGenerator; +import org.dllearner.core.ComponentInitException; +import org.dllearner.kb.SparqlEndpointKS; +import org.dllearner.kb.sparql.SparqlEndpoint; +import org.dllearner.reasoning.SPARQLReasoner; +import org.semanticweb.owlapi.model.IRI; +import org.semanticweb.owlapi.model.OWLClass; +import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl; + +import java.io.File; +import java.io.IOException; +import java.util.*; +import java.util.concurrent.*; + +/** + * @author Lorenz Buehmann + * created on 12/27/15 + */ +public class SPARQLLearningProblemsGenerator { + protected Model schema; + protected File benchmarkDirectory; + protected int threadCount; + SparqlEndpointKS ks; + SPARQLReasoner reasoner; + File dataDir; + + public SPARQLLearningProblemsGenerator(SparqlEndpoint endpoint, File benchmarkDirectory, int threadCount) throws ComponentInitException { + this.benchmarkDirectory = benchmarkDirectory; + this.threadCount = threadCount; + + // setup the endpoint + ks = new SparqlEndpointKS(endpoint); + ks.setCacheDir(new File(benchmarkDirectory, "cache").getPath() + ";mv_store=false"); + ks.setPageSize(50000); + ks.setUseCache(true); + ks.setQueryDelay(100); + ks.init(); + + // the directory where instance data is stored + dataDir = new File(benchmarkDirectory, "data/dbpedia/"); + dataDir.mkdirs(); + + + schema = ModelFactory.createDefaultModel(); + + // initialize the reasoner + reasoner = new SPARQLReasoner(ks); + reasoner.init(); + } + + protected void loadSchema() {} + + protected Set getClasses() { + return reasoner.getOWLClasses(); +// return reasoner.getMostSpecificClasses(); + } + + public void generateBenchmark(int nrOfSPARQLQueries, final int minDepth, final int maxDepth, int minNrOfExamples) { + Collection classes = getClasses(); + ArrayList classesList = new ArrayList<>(classes); + Collections.shuffle(classesList, new Random(123)); + classes = classesList; + +// ExecutorService tp = Executors.newFixedThreadPool(threadCount); + List> futures = new ArrayList<>(); + List allPaths = new ArrayList<>(); + + ThreadPoolExecutor tp = new CustomFutureReturningExecutor( + threadCount, threadCount, + 5000L, TimeUnit.MILLISECONDS, + new ArrayBlockingQueue(classes.size(), true)); + + JDKRandomGenerator rndGen = new JDKRandomGenerator(); + rndGen.setSeed(123); + + int nrOfQueriesPerDepth = nrOfSPARQLQueries / (maxDepth - minDepth + 1); + + // for each depth <= maxDepth + for(int depth = minDepth; depth <= maxDepth; depth++) { + System.out.println("Generating " + nrOfQueriesPerDepth + " queries for depth " + depth); + + Iterator iterator = classes.iterator(); + + // generate paths of depths <= maxDepth + List paths = new ArrayList<>(); + + while(paths.size() < nrOfQueriesPerDepth && iterator.hasNext()) { + + // pick next class + OWLClass cls = iterator.next(); + +// int depth = rndGen.nextInt(maxDepth) + 1; + + Future future = tp.submit(new PathDetectionTask(dataDir, ks, schema, cls, depth, minNrOfExamples)); +// futures.add(future); + try { + Path path = future.get(); + if(path != null) { + paths.add(path); + } + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + } + allPaths.addAll(paths); + } + + +// for (Future future : futures) { +// try { +// Path path = future.get(); +// if(path != null) { +// paths.add(path); +// } +// if(paths.size() == nrOfSPARQLQueries) { +// System.err.println("Benchmark generation finished. Stopping all running threads."); +// tp.shutdownNow(); +// } +// } catch (InterruptedException | ExecutionException e) { +// e.printStackTrace(); +// } +// if(paths.size() == nrOfSPARQLQueries) { +// System.err.println("Benchmark generation finished. Stopping all running threads."); +// tp.shutdownNow(); +// } +// } + + tp.shutdownNow(); + try { + tp.awaitTermination(1, TimeUnit.HOURS); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } +// try { +// tp.awaitTermination(1, TimeUnit.DAYS); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } + + // write queries to disk + String queries = ""; + for (Path path : allPaths) { + System.out.println(path); + queries += path.asSPARQLQuery(Var.alloc("s")) + "\n"; + } + File file = new File(benchmarkDirectory, "queries_" + nrOfSPARQLQueries + "_" + minDepth + "-" + maxDepth + "_" + minNrOfExamples+ ".txt"); + try { + Files.write(queries, file, Charsets.UTF_8); + } catch (IOException e) { + e.printStackTrace(); + } + } + + class CustomFutureReturningExecutor extends ThreadPoolExecutor { + + public CustomFutureReturningExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue) { + super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); + } + + @Override + protected RunnableFuture newTaskFor(Callable callable) { + if (callable instanceof PathDetectionTask) { + return ((PathDetectionTask) callable).newTask(); + } else { + return super.newTaskFor(callable); // A regular Callable, delegate to parent + } + } + } +} diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/UOBMLearningProblemsGenerator.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/UOBMLearningProblemsGenerator.java new file mode 100644 index 0000000000..11e7d74863 --- /dev/null +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/UOBMLearningProblemsGenerator.java @@ -0,0 +1,68 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.algorithms.qtl.experiments; + +import org.apache.jena.riot.Lang; +import org.dllearner.kb.sparql.SparqlEndpoint; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; + +/** + * Generate learning problems based on the UOBM dataset. + * @author Lorenz Buehmann + * + */ +public class UOBMLearningProblemsGenerator extends SPARQLLearningProblemsGenerator{ + + private static final String ONTOLOGY_URL = "http://www.cs.ox.ac.uk/isg/tools/RDFox/2014/AAAI/input/UOBM/owl/UOBM.owl"; + + public UOBMLearningProblemsGenerator(SparqlEndpoint endpoint, File benchmarkDirectory, int threadCount) throws Exception { + super(endpoint, benchmarkDirectory, threadCount); + } + + @Override + protected void loadSchema() { + try(InputStream is = new URL(ONTOLOGY_URL).openStream()){ + schema.read(is, null, Lang.RDFXML.getName()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) throws Exception { + File benchmarkBaseDirectory = new File(args[0]); + int threadCount = Integer.parseInt(args[1]); + int nrOfSPARQLQueries = Integer.parseInt(args[2]); + int minDepth = Integer.parseInt(args[3]); + int maxDepth = Integer.parseInt(args[4]); + int minNrOfExamples = Integer.parseInt(args[5]); + +// SparqlEndpoint endpoint = SparqlEndpoint.create("http://dbpedia.org/sparql", "http://dbpedia.org"); + SparqlEndpoint endpoint = SparqlEndpoint.create("http://sake.informatik.uni-leipzig.de:8890/sparql", "http://uobm.org"); + + UOBMLearningProblemsGenerator generator = new UOBMLearningProblemsGenerator(endpoint, benchmarkBaseDirectory, threadCount); + generator.generateBenchmark(nrOfSPARQLQueries, minDepth, maxDepth, minNrOfExamples); + } + + +} diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/VirtuosoUtils.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/VirtuosoUtils.java new file mode 100644 index 0000000000..adc69845bb --- /dev/null +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/VirtuosoUtils.java @@ -0,0 +1,176 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.algorithms.qtl.experiments; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Comparator; +import java.util.Date; +import java.util.Iterator; +import java.util.List; +import java.util.Set; +import java.util.TreeSet; +import java.util.concurrent.TimeUnit; + +import com.google.common.collect.ComparisonChain; +import com.hp.hpl.jena.datatypes.RDFDatatype; +import com.hp.hpl.jena.datatypes.xsd.XSDDateTime; +import com.hp.hpl.jena.datatypes.xsd.impl.XSDAbstractDateTimeType; +import com.hp.hpl.jena.graph.Node; +import com.hp.hpl.jena.graph.NodeFactory; +import com.hp.hpl.jena.graph.Triple; +import com.hp.hpl.jena.query.Query; +import com.hp.hpl.jena.query.QueryFactory; +import com.hp.hpl.jena.sparql.core.TriplePath; +import com.hp.hpl.jena.sparql.expr.E_Equals; +import com.hp.hpl.jena.sparql.expr.E_LogicalOr; +import com.hp.hpl.jena.sparql.expr.E_Str; +import com.hp.hpl.jena.sparql.expr.Expr; +import com.hp.hpl.jena.sparql.expr.ExprVar; +import com.hp.hpl.jena.sparql.expr.NodeValue; +import com.hp.hpl.jena.sparql.syntax.Element; +import com.hp.hpl.jena.sparql.syntax.ElementFilter; +import com.hp.hpl.jena.sparql.syntax.ElementGroup; +import com.hp.hpl.jena.sparql.syntax.ElementPathBlock; +import com.hp.hpl.jena.sparql.syntax.ElementTriplesBlock; +import com.hp.hpl.jena.sparql.syntax.ElementVisitorBase; +import com.hp.hpl.jena.sparql.syntax.ElementWalker; + +public class VirtuosoUtils { + + /** + * Returns a rewritten query which tried to workaround issues with xsd:date + * literals and applies matching on the string value instead, i.e. a triple pattern + * ?s :p "2002-09-24"^xsd:string will be converted to + * ?s :p ?date1 . FILTER(STR(?date1) = "2002-09-24"). + * @param query the query to rewrite + * @return the rewritten query + */ + public static Query rewriteForVirtuosoDateLiteralBug(Query query){ + final Query copy = QueryFactory.create(query); + final Element queryPattern = copy.getQueryPattern(); + final List filters = new ArrayList<>(); + ElementWalker.walk(queryPattern, new ElementVisitorBase() { + + int cnt = 0; + + @Override + public void visit(ElementTriplesBlock el) { + Set newTriplePatterns = new TreeSet<>(new Comparator() { + @Override + public int compare(Triple o1, Triple o2) { + return ComparisonChain.start().compare(o1.getSubject().toString(), o2.getSubject().toString()) + .compare(o1.getPredicate().toString(), o2.getPredicate().toString()) + .compare(o1.getObject().toString(), o2.getObject().toString()).result(); + } + }); + + Iterator iterator = el.patternElts(); + while (iterator.hasNext()) { + Triple tp = iterator.next(); + + if (tp.getObject().isLiteral()) { + RDFDatatype dt = tp.getObject().getLiteralDatatype(); + if (dt != null && dt instanceof XSDAbstractDateTimeType) { + iterator.remove(); + // new triple pattern + Node objectVar = NodeFactory.createVariable("date" + cnt++); + newTriplePatterns.add(Triple.create(tp.getSubject(), tp.getPredicate(), objectVar)); + + String lit = tp.getObject().getLiteralLexicalForm(); + Object literalValue = tp.getObject().getLiteralValue(); + Expr filterExpr = new E_Equals(new E_Str(new ExprVar(objectVar)), NodeValue.makeString(lit)); + if (literalValue instanceof XSDDateTime) { + Calendar calendar = ((XSDDateTime) literalValue).asCalendar(); + Date date = new Date(calendar.getTimeInMillis() + TimeUnit.HOURS.toMillis(2)); + SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd"); + String inActiveDate = format1.format(date); + filterExpr = new E_LogicalOr(filterExpr, new E_Equals( + new E_Str(new ExprVar(objectVar)), NodeValue.makeString(inActiveDate))); + } + ElementFilter filter = new ElementFilter(filterExpr); + filters.add(filter); + } + } + } + + for (Triple tp : newTriplePatterns) { + el.addTriple(tp); + } + + for (ElementFilter filter : filters) { + ((ElementGroup)queryPattern).addElementFilter(filter); + } + } + + @Override + public void visit(ElementPathBlock el) { + Set newTriplePatterns = new TreeSet<>(new Comparator() { + @Override + public int compare(Triple o1, Triple o2) { + return ComparisonChain.start().compare(o1.getSubject().toString(), o2.getSubject().toString()) + .compare(o1.getPredicate().toString(), o2.getPredicate().toString()) + .compare(o1.getObject().toString(), o2.getObject().toString()).result(); + } + }); + + Iterator iterator = el.patternElts(); + while (iterator.hasNext()) { + Triple tp = iterator.next().asTriple(); + + if (tp.getObject().isLiteral()) { + RDFDatatype dt = tp.getObject().getLiteralDatatype(); + if (dt != null && dt instanceof XSDAbstractDateTimeType) { + iterator.remove(); + // new triple pattern + Node objectVar = NodeFactory.createVariable("date" + cnt++); + newTriplePatterns.add(Triple.create(tp.getSubject(), tp.getPredicate(), objectVar)); + + String lit = tp.getObject().getLiteralLexicalForm(); + Object literalValue = tp.getObject().getLiteralValue(); + Expr filterExpr = new E_Equals(new E_Str(new ExprVar(objectVar)), NodeValue.makeString(lit)); + if (literalValue instanceof XSDDateTime) { + Calendar calendar = ((XSDDateTime) literalValue).asCalendar(); + Date date = new Date(calendar.getTimeInMillis() + TimeUnit.HOURS.toMillis(2)); + SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd"); + String inActiveDate = format1.format(date); + filterExpr = new E_LogicalOr(filterExpr, new E_Equals( + new E_Str(new ExprVar(objectVar)), NodeValue.makeString(inActiveDate))); + } + ElementFilter filter = new ElementFilter(filterExpr); + filters.add(filter); + } + + } + } + + for (Triple tp : newTriplePatterns) { + el.addTriple(tp); + } + + } + }); + for (ElementFilter filter : filters) { + ((ElementGroup)queryPattern).addElementFilter(filter); + } + return copy; + } + +} diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/package-info.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/package-info.java index bc11166152..9d0c1d9532 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/package-info.java +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/experiments/package-info.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ /** * Experiments for QTL * @author Lorenz Buehmann diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/heuristics/QueryTreeEditDistanceTest.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/heuristics/QueryTreeEditDistanceTest.java index 3a1b89ac60..b6f7cbd155 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/qtl/heuristics/QueryTreeEditDistanceTest.java +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/heuristics/QueryTreeEditDistanceTest.java @@ -1,6 +1,24 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.heuristics; -import static junit.framework.Assert.assertEquals; +import static org.junit.Assert.assertEquals; import org.dllearner.algorithms.qtl.datastructures.impl.QueryTreeImpl; import org.dllearner.algorithms.qtl.datastructures.impl.RDFResourceTree; @@ -11,6 +29,8 @@ import com.hp.hpl.jena.rdf.model.ResourceFactory; public class QueryTreeEditDistanceTest { + + double delta = 0; /* * A A */ @@ -20,7 +40,7 @@ public void test01() { RDFResourceTree tree2 = new RDFResourceTree(NodeFactory.createURI("A")); double expectedDistance = 0; - assertEquals(expectedDistance, QueryTreeEditDistance.getDistance(tree1, tree2)); + assertEquals(expectedDistance, QueryTreeEditDistance.getDistance(tree1, tree2), delta); } /* @@ -32,7 +52,7 @@ public void test02() { RDFResourceTree tree2 = new RDFResourceTree(NodeFactory.createURI("A2")); double expectedDistance = 1; - assertEquals(expectedDistance, QueryTreeEditDistance.getDistance(tree1, tree2)); + assertEquals(expectedDistance, QueryTreeEditDistance.getDistance(tree1, tree2), delta); } /* @@ -54,7 +74,7 @@ public void test03() { RDFResourceTree tree2 = new RDFResourceTree(NodeFactory.createURI("A")); double expectedDistance = 1; - assertEquals(expectedDistance, QueryTreeEditDistance.getDistance(tree1, tree2)); + assertEquals(expectedDistance, QueryTreeEditDistance.getDistance(tree1, tree2), delta); } /* @@ -76,7 +96,7 @@ public void test04() { tree2.addChild(child2_1, NodeFactory.createURI("p")); double expectedDistance = 1; - assertEquals(expectedDistance, QueryTreeEditDistance.getDistance(tree1, tree2)); + assertEquals(expectedDistance, QueryTreeEditDistance.getDistance(tree1, tree2), delta); } /* @@ -111,7 +131,7 @@ public void test05() { tree2.addChild(child2_1, NodeFactory.createURI("q")); double expectedDistance = 2; - assertEquals(expectedDistance, QueryTreeEditDistance.getDistance(tree1, tree2)); + assertEquals(expectedDistance, QueryTreeEditDistance.getDistance(tree1, tree2), delta); } /* @@ -142,7 +162,7 @@ public void test06() { RDFResourceTree tree2 = new RDFResourceTree(NodeFactory.createURI("A")); double expectedDistance = 3; - assertEquals(expectedDistance, QueryTreeEditDistance.getDistance(tree1, tree2)); + assertEquals(expectedDistance, QueryTreeEditDistance.getDistance(tree1, tree2), delta); } /* @@ -197,7 +217,7 @@ public void test07() { tree2.addChild(child2_3, NodeFactory.createURI("r")); double expectedDistance = 2; - assertEquals(expectedDistance, QueryTreeEditDistance.getDistance(tree1, tree2)); + assertEquals(expectedDistance, QueryTreeEditDistance.getDistance(tree1, tree2), delta); } @Test diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/operations/LCSTest.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/operations/LCSTest.java index 4fb484e1e8..4129e1edd4 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/qtl/operations/LCSTest.java +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/operations/LCSTest.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.operations; diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/operations/LGGPerformance.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/operations/LGGPerformance.java new file mode 100644 index 0000000000..cebad954bb --- /dev/null +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/operations/LGGPerformance.java @@ -0,0 +1,141 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.algorithms.qtl.operations; + +import java.util.Random; + +import org.dllearner.algorithms.qtl.datastructures.impl.RDFResourceTree; +import org.dllearner.algorithms.qtl.operations.lgg.LGGGenerator; +import org.dllearner.algorithms.qtl.operations.lgg.LGGGeneratorSimple; +import org.junit.Test; + +import com.hp.hpl.jena.graph.Node; +import com.hp.hpl.jena.graph.NodeFactory; + +/** + * A test class to analyze the performance of the application of different LGG + * implementations in dependence on the size and complexity of the input trees. + * + * @author Lorenz Buehmann + * + */ +public class LGGPerformance { + +// @Test + public void testPerformance() { + LGGGenerator lggGen = new LGGGeneratorSimple(); + + RDFResourceTree child1Common = new RDFResourceTree(NodeFactory.createURI("ab")); + RDFResourceTree child2Common = new RDFResourceTree(NodeFactory.createURI("aabb")); + RDFResourceTree child3Common = new RDFResourceTree(NodeFactory.createURI("aaabbb")); + Node edge1Common = NodeFactory.createURI("p"); + Node edge2Common = NodeFactory.createURI("q"); + Node edge3Common = NodeFactory.createURI("r"); + + Random rnd = new Random(123); + + int level1 = 20; + int level2 = 30; + int level3 = 20; + + String var = "a"; + RDFResourceTree tree1 = new RDFResourceTree(NodeFactory.createURI(var)); + for(int i = 0; i < level1; i++) { + + RDFResourceTree child1; + if(rnd.nextBoolean()) { + child1 = new RDFResourceTree(child1Common); + } else { + child1 = new RDFResourceTree(NodeFactory.createURI(var + "_" + i)); + } + Node edge1 = edge1Common; + tree1.addChild(child1, edge1); + + for (int j = 0; j < level2; j++) { + + RDFResourceTree child2; + if(rnd.nextBoolean()) { + child2 = new RDFResourceTree(child2Common); + } else { + child2 = new RDFResourceTree(NodeFactory.createURI(var + var + "_" + j)); + } + Node edge2 = rnd.nextBoolean() ? edge2Common : NodeFactory.createURI("q" + j); + child1.addChild(child2, edge2); + + for (int k = 0; k < level3; k++) { + + RDFResourceTree child3; + if(rnd.nextBoolean()) { + child3 = new RDFResourceTree(child3Common); + } else { + child3 = new RDFResourceTree(NodeFactory.createURI(var + var + "_" + k)); + } + Node edge3 = rnd.nextBoolean() ? edge3Common : NodeFactory.createURI("r" + k); + child2.addChild(child3, edge3); + } + } + } + + var = "b"; + RDFResourceTree tree2 = new RDFResourceTree(NodeFactory.createURI("b")); + for(int i = 0; i < level1; i++) { + + RDFResourceTree child1; + if(rnd.nextBoolean()) { + child1 = new RDFResourceTree(child1Common); + } else { + child1 = new RDFResourceTree(NodeFactory.createURI(var + "_" + i)); + } + Node edge1 = edge1Common; + tree2.addChild(child1, edge1); + + for (int j = 0; j < level2; j++) { + + RDFResourceTree child2; + if(rnd.nextBoolean()) { + child2 = new RDFResourceTree(child2Common); + } else { + child2 = new RDFResourceTree(NodeFactory.createURI(var + var + "_" + j)); + } + Node edge2 = rnd.nextBoolean() ? edge2Common : NodeFactory.createURI("q" + j); + child1.addChild(child2, edge2); + + for (int k = 0; k < level3; k++) { + + RDFResourceTree child3; + if(rnd.nextBoolean()) { + child3 = new RDFResourceTree(child3Common); + } else { + child3 = new RDFResourceTree(NodeFactory.createURI(var + var + "_" + k)); + } + Node edge3 = rnd.nextBoolean() ? edge3Common : NodeFactory.createURI("r" + k); + child2.addChild(child3, edge3); + } + } + } + + long start = System.currentTimeMillis(); + RDFResourceTree lgg = lggGen.getLGG(tree1, tree2); + long end = System.currentTimeMillis(); + System.out.println("Operation took " + (end - start) + "ms"); + + System.out.println(lgg.getStringRepresentation()); + } + +} diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/operations/LGGTest.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/operations/LGGTest.java index d8a2f530cd..f69121b039 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/qtl/operations/LGGTest.java +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/operations/LGGTest.java @@ -1,9 +1,25 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.operations; import java.io.ByteArrayInputStream; +import java.io.File; import org.apache.jena.riot.Lang; import org.apache.jena.riot.RDFDataMgr; @@ -16,6 +32,8 @@ import org.dllearner.algorithms.qtl.operations.lgg.LGGGeneratorRDFS; import org.dllearner.algorithms.qtl.operations.lgg.LGGGeneratorSimple; import org.dllearner.core.AbstractReasonerComponent; +import org.dllearner.core.ComponentInitException; +import org.dllearner.kb.sparql.SparqlEndpoint; import org.dllearner.reasoning.SPARQLReasoner; import org.junit.BeforeClass; import org.junit.Test; @@ -50,7 +68,7 @@ public class LGGTest { private static LGGGenerator lggGenRDFS; @BeforeClass - public static void init() { + public static void init() throws ComponentInitException { String kb = "@prefix : . @prefix rdfs: ." + ":a1 :r :A . " + ":a2 :s :A . " @@ -58,7 +76,12 @@ public static void init() { + ":a4 :s :D . :D :p :F ." + "<_:lgg1_2> :r :A ." + "<_:lgg3_4> :r _:D . _:D :p _:F ." - + ":r rdfs:subPropertyOf :s ."; + + ":r rdfs:subPropertyOf :s ." + + ":a5 a :A ." + + ":a6 a :B ." + + ":A rdfs:subClassOf :B ." + + "<_:lgg5_6> a :B ." + ; model = ModelFactory.createDefaultModel(); model.read(new ByteArrayInputStream(kb.getBytes()), null, "TURTLE"); @@ -75,6 +98,7 @@ public static void init() { reasoner = new SPARQLReasoner(model); reasoner.setPrecomputeObjectPropertyHierarchy(false); reasoner.setPrecomputeDataPropertyHierarchy(false); + reasoner.init(); lggGenSimple = new LGGGeneratorSimple(); lggGenRDFS = new LGGGeneratorRDFS(reasoner); @@ -115,6 +139,28 @@ public void testPropertyEntailment() { assertTrue(QueryTreeUtils.sameTrees(lggRDFS, targetLGG)); } + + @Test + public void testClassEntailment() { + RDFResourceTree tree1 = treeFactory.getQueryTree("http://test.org/a5", model); + RDFResourceTree tree2 = treeFactory.getQueryTree("http://test.org/a6", model); + + System.out.println("Tree 1\n" + tree1.getStringRepresentation()); + System.out.println("Tree 2\n" + tree2.getStringRepresentation()); + + RDFResourceTree lggSimple = lggGenSimple.getLGG(tree1, tree2); + System.out.println("LGG_simple(T1,T2)\n" + lggSimple.getStringRepresentation()); + + assertTrue(lggSimple.isLeaf()); + + RDFResourceTree targetLGG = treeFactory.getQueryTree(new ResourceImpl(AnonId.create("lgg5_6")), model); + System.out.println("Target LGG\n" + targetLGG.getStringRepresentation()); + + RDFResourceTree lggRDFS = lggGenRDFS.getLGG(tree1, tree2); + System.out.println("LGG_RDFS(T1,T2)\n" + lggRDFS.getStringRepresentation()); + + assertTrue(QueryTreeUtils.sameTrees(lggRDFS, targetLGG)); + } @Test public void testPerformance() { @@ -146,7 +192,8 @@ public void testPerformance() { // @Test public void correctness() { treeFactory.setMaxDepth(2); - treeFactory.addDropFilters((Filter[]) new DBpediaEvaluationDataset().getQueryTreeFilters().toArray(new Filter[]{})); + java.util.List> var = new DBpediaEvaluationDataset(new File("/tmp/lggtest"), SparqlEndpoint.getEndpointDBpedia()).getQueryTreeFilters(); + treeFactory.addDropFilters((Filter[]) var.toArray(new Filter[var.size()])); // http://dbpedia.org/resource/Battle_Arena_Toshinden_3 Model model = ModelFactory.createDefaultModel(); RDFDataMgr.read( diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/operations/SubsumptionTest.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/operations/SubsumptionTest.java index 4f67e58ed6..1932d6d1b9 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/qtl/operations/SubsumptionTest.java +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/operations/SubsumptionTest.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.operations; @@ -48,9 +63,12 @@ public static void init() { String kb = "@prefix : . @prefix rdfs: ." + "<_:a1> :p1 _:b . " + "<_:a2> :p1 _:s . _:s :p2 :b ." - + ":a3 a :A . " - + ":a4 a _:cls . _:cls rdfs:subClassOf :A ." - + ":a5 a _:cls2 . _:cls2 rdfs:subClassOf _:cls3 . _:cls3 rdfs:subClassOf :A ."; + + ":t1 a :A . " + + ":t2 a _:cls1 . _:cls1 rdfs:subClassOf :A ." + + ":t3 a _:cls2 . _:cls2 rdfs:subClassOf :B ." + + ":t4 a _:cls3 . _:cls3 rdfs:subClassOf _:cls4 . _:cls4 rdfs:subClassOf :A ." + + ":A rdfs:subClassOf :B ." + ; model = ModelFactory.createDefaultModel(); model.read(new ByteArrayInputStream(kb.getBytes()), null, "TURTLE"); @@ -91,19 +109,26 @@ public void testSubsumptionSimple() { @Test public void testSubsumptionRDFS() { - RDFResourceTree tree1 = treeFactory.getQueryTree("http://test.org/a3", model); - assertTrue(QueryTreeUtils.isSubsumedBy(tree1, tree1)); + RDFResourceTree tree1 = treeFactory.getQueryTree("http://test.org/t1", model); print(tree1); - RDFResourceTree tree2 = treeFactory.getQueryTree("http://test.org/a4", model); + RDFResourceTree tree2 = treeFactory.getQueryTree("http://test.org/t2", model); print(tree2); + + RDFResourceTree tree3 = treeFactory.getQueryTree("http://test.org/t3", model); + print(tree3); + + RDFResourceTree tree4 = treeFactory.getQueryTree("http://test.org/t4", model); + print(tree4); + + assertTrue(QueryTreeUtils.isSubsumedBy(tree1, tree1)); + assertTrue(QueryTreeUtils.isSubsumedBy(tree1, tree2, Entailment.RDFS)); - assertFalse(QueryTreeUtils.isSubsumedBy(tree2, tree1, Entailment.RDFS)); + assertTrue(QueryTreeUtils.isSubsumedBy(tree2, tree1, Entailment.RDFS)); - RDFResourceTree tree3 = treeFactory.getQueryTree("http://test.org/a5", model); - print(tree3); + assertTrue(QueryTreeUtils.isSubsumedBy(tree2, tree3, Entailment.RDFS)); - assertTrue(QueryTreeUtils.isSubsumedBy(tree3, tree2, Entailment.RDFS)); + assertFalse(QueryTreeUtils.isSubsumedBy(tree3, tree2, Entailment.RDFS)); } public static void print(RDFResourceTree tree) { diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/operations/TreePruningTest.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/operations/TreePruningTest.java index 5ca3dcb0b0..8b6d2be914 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/qtl/operations/TreePruningTest.java +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/operations/TreePruningTest.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.operations; diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/qald/BioMedicalKB.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/qald/BioMedicalKB.java index 2c72af4d66..c5b0006ecf 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/qtl/qald/BioMedicalKB.java +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/qald/BioMedicalKB.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.qald; diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/qald/DBpediaKB.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/qald/DBpediaKB.java index 64b40c837f..08c56013f3 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/qtl/qald/DBpediaKB.java +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/qald/DBpediaKB.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.qald; diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/qald/KB.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/qald/KB.java index 960ff3cbfb..298415b7ad 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/qtl/qald/KB.java +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/qald/KB.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.algorithms.qtl.qald; import java.util.List; diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/qald/QALDExperiment.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/qald/QALDExperiment.java index a930c53d5a..97f3aa71a4 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/qtl/qald/QALDExperiment.java +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/qald/QALDExperiment.java @@ -1,35 +1,45 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.qtl.qald; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.IOException; -import java.io.StringReader; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Random; -import java.util.Set; -import java.util.Stack; -import java.util.TreeSet; -import java.util.concurrent.TimeUnit; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; - +import com.google.common.base.Charsets; +import com.google.common.collect.ComparisonChain; +import com.google.common.collect.HashMultimap; +import com.google.common.collect.Multimap; +import com.google.common.collect.Sets; +import com.google.common.io.Files; +import com.hp.hpl.jena.datatypes.RDFDatatype; +import com.hp.hpl.jena.datatypes.xsd.XSDDatatype; +import com.hp.hpl.jena.datatypes.xsd.XSDDateTime; +import com.hp.hpl.jena.datatypes.xsd.impl.XSDAbstractDateTimeType; +import com.hp.hpl.jena.graph.Node; +import com.hp.hpl.jena.graph.NodeFactory; +import com.hp.hpl.jena.graph.Triple; +import com.hp.hpl.jena.query.*; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; +import com.hp.hpl.jena.rdf.model.Resource; +import com.hp.hpl.jena.sparql.core.BasicPattern; +import com.hp.hpl.jena.sparql.core.TriplePath; +import com.hp.hpl.jena.sparql.core.Var; +import com.hp.hpl.jena.sparql.expr.*; +import com.hp.hpl.jena.sparql.syntax.*; import org.aksw.jena_sparql_api.cache.core.QueryExecutionFactoryCacheEx; import org.aksw.jena_sparql_api.core.QueryExecutionFactory; import org.apache.commons.math3.random.RandomDataGenerator; @@ -44,78 +54,34 @@ import org.dllearner.algorithms.qtl.datastructures.impl.EvaluatedRDFResourceTree; import org.dllearner.algorithms.qtl.datastructures.impl.RDFResourceTree; import org.dllearner.algorithms.qtl.impl.QueryTreeFactoryBase; -import org.dllearner.algorithms.qtl.util.Entailment; -import org.dllearner.algorithms.qtl.util.StopURIsDBpedia; -import org.dllearner.algorithms.qtl.util.StopURIsOWL; -import org.dllearner.algorithms.qtl.util.StopURIsRDFS; -import org.dllearner.algorithms.qtl.util.StopURIsSKOS; -import org.dllearner.algorithms.qtl.util.filters.NamespaceDropStatementFilter; -import org.dllearner.algorithms.qtl.util.filters.ObjectDropStatementFilter; -import org.dllearner.algorithms.qtl.util.filters.PredicateDropStatementFilter; -import org.dllearner.algorithms.qtl.util.filters.PredicateExistenceFilter; -import org.dllearner.algorithms.qtl.util.filters.PredicateExistenceFilterDBpedia; +import org.dllearner.algorithms.qtl.util.*; +import org.dllearner.algorithms.qtl.util.filters.*; import org.dllearner.core.ComponentInitException; +import org.dllearner.core.StringRenderer; +import org.dllearner.core.StringRenderer.Rendering; import org.dllearner.kb.sparql.ConciseBoundedDescriptionGenerator; import org.dllearner.kb.sparql.ConciseBoundedDescriptionGeneratorImpl; import org.dllearner.learningproblems.Heuristics; import org.dllearner.learningproblems.PosNegLPStandard; import org.dllearner.utilities.QueryUtils; -import org.semanticweb.owlapi.io.ToStringRenderer; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLIndividual; import org.w3c.dom.DOMException; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; - import uk.ac.manchester.cs.owl.owlapi.OWLNamedIndividualImpl; -import uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntaxObjectRenderer; -import com.google.common.base.Charsets; -import com.google.common.collect.ComparisonChain; -import com.google.common.collect.HashMultimap; -import com.google.common.collect.Multimap; -import com.google.common.collect.Sets; -import com.google.common.io.Files; -import com.hp.hpl.jena.datatypes.RDFDatatype; -import com.hp.hpl.jena.datatypes.xsd.XSDDatatype; -import com.hp.hpl.jena.datatypes.xsd.XSDDateTime; -import com.hp.hpl.jena.datatypes.xsd.impl.XSDAbstractDateTimeType; -import com.hp.hpl.jena.graph.Node; -import com.hp.hpl.jena.graph.NodeFactory; -import com.hp.hpl.jena.graph.Triple; -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.QueryFactory; -import com.hp.hpl.jena.query.QuerySolution; -import com.hp.hpl.jena.query.ResultSet; -import com.hp.hpl.jena.query.Syntax; -import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.ModelFactory; -import com.hp.hpl.jena.rdf.model.Resource; -import com.hp.hpl.jena.sparql.core.BasicPattern; -import com.hp.hpl.jena.sparql.core.TriplePath; -import com.hp.hpl.jena.sparql.core.Var; -import com.hp.hpl.jena.sparql.expr.E_Equals; -import com.hp.hpl.jena.sparql.expr.E_LessThanOrEqual; -import com.hp.hpl.jena.sparql.expr.E_LogicalOr; -import com.hp.hpl.jena.sparql.expr.E_NotEquals; -import com.hp.hpl.jena.sparql.expr.E_NotExists; -import com.hp.hpl.jena.sparql.expr.E_NumAbs; -import com.hp.hpl.jena.sparql.expr.E_Str; -import com.hp.hpl.jena.sparql.expr.E_Subtract; -import com.hp.hpl.jena.sparql.expr.Expr; -import com.hp.hpl.jena.sparql.expr.ExprVar; -import com.hp.hpl.jena.sparql.expr.NodeValue; -import com.hp.hpl.jena.sparql.syntax.Element; -import com.hp.hpl.jena.sparql.syntax.ElementFilter; -import com.hp.hpl.jena.sparql.syntax.ElementGroup; -import com.hp.hpl.jena.sparql.syntax.ElementOptional; -import com.hp.hpl.jena.sparql.syntax.ElementPathBlock; -import com.hp.hpl.jena.sparql.syntax.ElementTriplesBlock; -import com.hp.hpl.jena.sparql.syntax.ElementUnion; -import com.hp.hpl.jena.sparql.syntax.ElementVisitorBase; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.StringReader; +import java.text.SimpleDateFormat; +import java.util.*; +import java.util.concurrent.TimeUnit; /** * @author Lorenz Buehmann @@ -1263,7 +1229,7 @@ private Score computeScore(String referenceSparqlQuery, String learnedSPARQLQuer } public static void main(String[] args) throws Exception { - ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); + StringRenderer.setRenderer(Rendering.DL_SYNTAX); Logger.getLogger(QALDExperiment.class).addAppender( new FileAppender(new SimpleLayout(), "log/qtl-qald.log", false)); Logger.getRootLogger().setLevel(Level.INFO); diff --git a/components-core/src/test/java/org/dllearner/algorithms/qtl/qald/package-info.java b/components-core/src/test/java/org/dllearner/algorithms/qtl/qald/package-info.java index 9b2172449a..5dd3b1120d 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/qtl/qald/package-info.java +++ b/components-core/src/test/java/org/dllearner/algorithms/qtl/qald/package-info.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ /** * */ diff --git a/components-core/src/test/java/org/dllearner/algorithms/schema/SchemaGeneratorTest.java b/components-core/src/test/java/org/dllearner/algorithms/schema/SchemaGeneratorTest.java index 8addca27ce..e6f634aff8 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/schema/SchemaGeneratorTest.java +++ b/components-core/src/test/java/org/dllearner/algorithms/schema/SchemaGeneratorTest.java @@ -1,38 +1,47 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.schema; -import junit.framework.TestCase; - +import com.google.common.collect.Sets; +import com.hp.hpl.jena.rdf.model.Model; import org.apache.log4j.Level; import org.apache.log4j.Logger; +import org.dllearner.core.StringRenderer; +import org.dllearner.core.StringRenderer.Rendering; import org.dllearner.utilities.OwlApiJenaUtils; +import org.junit.BeforeClass; import org.junit.Test; -import org.semanticweb.owlapi.io.ToStringRenderer; import org.semanticweb.owlapi.model.AxiomType; import org.semanticweb.owlapi.model.OWLAxiom; import org.semanticweb.owlapi.model.OWLOntology; -import uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntaxObjectRenderer; - -import com.google.common.collect.Sets; -import com.hp.hpl.jena.rdf.model.Model; - /** * @author Lorenz Buehmann * @since Oct 25, 2014 */ -public class SchemaGeneratorTest extends TestCase{ +public class SchemaGeneratorTest { SyntheticDataGenerator dataGenerator = new SyntheticDataGenerator(); - /* (non-Javadoc) - * @see junit.framework.TestCase#setUp() - */ - @Override - protected void setUp() throws Exception { - ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); + @BeforeClass + public static void setUp() throws Exception { + StringRenderer.setRenderer(Rendering.DL_SYNTAX); Logger.getLogger(SimpleSchemaGenerator.class).setLevel(Level.TRACE); } diff --git a/components-core/src/test/java/org/dllearner/algorithms/schema/SyntheticDataGenerator.java b/components-core/src/test/java/org/dllearner/algorithms/schema/SyntheticDataGenerator.java index 2758c0c7d2..ec2b3415a2 100644 --- a/components-core/src/test/java/org/dllearner/algorithms/schema/SyntheticDataGenerator.java +++ b/components-core/src/test/java/org/dllearner/algorithms/schema/SyntheticDataGenerator.java @@ -1,28 +1,34 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.algorithms.schema; -import java.util.ArrayList; -import java.util.List; - import org.apache.commons.math3.random.RandomDataGenerator; +import org.dllearner.core.StringRenderer; +import org.dllearner.core.StringRenderer.Rendering; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.io.ToStringRenderer; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLDataProperty; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLObjectProperty; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyCreationException; -import org.semanticweb.owlapi.model.OWLOntologyManager; -import org.semanticweb.owlapi.model.PrefixManager; +import org.semanticweb.owlapi.model.*; import org.semanticweb.owlapi.util.DefaultPrefixManager; import org.semanticweb.owlapi.util.IRIShortFormProvider; import org.semanticweb.owlapi.util.SimpleIRIShortFormProvider; -import uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntaxObjectRenderer; +import java.util.ArrayList; +import java.util.List; /** * @author Lorenz Buehmann @@ -32,7 +38,8 @@ public class SyntheticDataGenerator { private static final String NS = "http://dl-learner.org/data/"; - PrefixManager pm = new DefaultPrefixManager(NS); + PrefixManager pm = new DefaultPrefixManager(); + IRIShortFormProvider sfp = new SimpleIRIShortFormProvider(); OWLOntologyManager man = OWLManager.createOWLOntologyManager(); OWLDataFactory df = man.getOWLDataFactory(); @@ -42,6 +49,10 @@ public class SyntheticDataGenerator { RandomDataGenerator rnd = new RandomDataGenerator(); OWLOntology ontology; + + public SyntheticDataGenerator() { + pm.setDefaultPrefix(NS); + } /** @@ -131,7 +142,7 @@ private OWLIndividual createIndividual(String name){ } public static void main(String[] args) throws Exception { - ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); + StringRenderer.setRenderer(Rendering.DL_SYNTAX); SyntheticDataGenerator dataGenerator = new SyntheticDataGenerator(); OWLOntology ontology = dataGenerator.createData(3, 10); diff --git a/components-core/src/test/java/org/dllearner/core/AnnComponentManagerTest.java b/components-core/src/test/java/org/dllearner/core/AnnComponentManagerTest.java index 3f763c6ca2..f7f163fedc 100644 --- a/components-core/src/test/java/org/dllearner/core/AnnComponentManagerTest.java +++ b/components-core/src/test/java/org/dllearner/core/AnnComponentManagerTest.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.core; import java.util.Collection; diff --git a/components-core/src/test/java/org/dllearner/core/FatherExample.java b/components-core/src/test/java/org/dllearner/core/FatherExample.java index 576aecd49f..322fbf01a3 100644 --- a/components-core/src/test/java/org/dllearner/core/FatherExample.java +++ b/components-core/src/test/java/org/dllearner/core/FatherExample.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.core; @@ -10,13 +25,14 @@ import org.dllearner.learningproblems.ClassLearningProblem; import org.dllearner.reasoning.ClosedWorldReasoner; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.io.ToStringRenderer; +import org.dllearner.core.StringRenderer; +import org.dllearner.core.StringRenderer.Rendering; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLClass; import org.semanticweb.owlapi.model.OWLOntology; import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl; -import uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.ManchesterOWLSyntaxOWLObjectRendererImpl; +import org.dllearner.utilities.owl.ManchesterOWLSyntaxOWLObjectRendererImplExt; /** * A basic example how to use DL-Learner. @@ -33,7 +49,7 @@ public class FatherExample { * @param args */ public static void main(String[] args) throws Exception{ - ToStringRenderer.getInstance().setRenderer(new ManchesterOWLSyntaxOWLObjectRendererImpl()); + StringRenderer.setRenderer(Rendering.MANCHESTER_SYNTAX); // setup the knowledge base File file = new File("../examples/father.owl"); diff --git a/components-core/src/test/java/org/dllearner/kb/OWLAPIOntologyTest.java b/components-core/src/test/java/org/dllearner/kb/OWLAPIOntologyTest.java index 380ade9b10..4a7744ca5b 100644 --- a/components-core/src/test/java/org/dllearner/kb/OWLAPIOntologyTest.java +++ b/components-core/src/test/java/org/dllearner/kb/OWLAPIOntologyTest.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb; import static org.junit.Assert.assertEquals; diff --git a/components-core/src/test/java/org/dllearner/kb/sparql/SparqlKnowledgeSourceTest.java b/components-core/src/test/java/org/dllearner/kb/sparql/SparqlKnowledgeSourceTest.java index ff3825ee85..b6bd8b7be6 100644 --- a/components-core/src/test/java/org/dllearner/kb/sparql/SparqlKnowledgeSourceTest.java +++ b/components-core/src/test/java/org/dllearner/kb/sparql/SparqlKnowledgeSourceTest.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.kb.sparql; import static org.junit.Assert.assertEquals; diff --git a/components-core/src/test/java/org/dllearner/learningproblems/ClassAsInstanceLearningProblemTest.java b/components-core/src/test/java/org/dllearner/learningproblems/ClassAsInstanceLearningProblemTest.java index 9f012955a4..acfb2829bb 100644 --- a/components-core/src/test/java/org/dllearner/learningproblems/ClassAsInstanceLearningProblemTest.java +++ b/components-core/src/test/java/org/dllearner/learningproblems/ClassAsInstanceLearningProblemTest.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.learningproblems; diff --git a/components-core/src/test/java/org/dllearner/learningproblems/OntologyEngineering.java b/components-core/src/test/java/org/dllearner/learningproblems/OntologyEngineering.java new file mode 100644 index 0000000000..813cd07f9a --- /dev/null +++ b/components-core/src/test/java/org/dllearner/learningproblems/OntologyEngineering.java @@ -0,0 +1,547 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.dllearner.learningproblems; +/** + * Copyright (C) 2007-2008, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.dllearner.algorithms.celoe.CELOE; +import org.dllearner.core.*; +import org.dllearner.kb.OWLAPIOntology; +import org.dllearner.reasoning.ClosedWorldReasoner; +import org.dllearner.reasoning.OWLAPIReasoner; +import org.dllearner.refinementoperators.RhoDRDown; +import org.dllearner.utilities.statistics.Stat; +import org.semanticweb.owlapi.apibinding.OWLManager; +import org.semanticweb.owlapi.model.OWLClass; +import org.semanticweb.owlapi.model.OWLClassExpression; +import org.semanticweb.owlapi.model.OWLIndividual; +import org.semanticweb.owlapi.model.OWLOntology; + +import java.io.*; +import java.text.DecimalFormat; +import java.util.*; + +//import org.dllearner.core.ComponentManager; +//import org.dllearner.core.owl.Description; +//import org.dllearner.core.owl.Individual; +//import org.dllearner.core.owl.NamedClass; +//import org.dllearner.core.owl.Thing; +//import org.dllearner.reasoning.FastInstanceChecker; + +/** + * The script loads an ontology, loops through all classes having at least a + * specified amount of instances, and evaluates a learning algorithm on those. + * It measures how many times the algorithm missed an appropriate solution of a + * problem, how often it found the correct solution (and the position of the + * solution in the suggestion list). It also tries to find consequences of + * adding an axiom (either it helped to detect an inconsistency or the class now + * has more inferred instances). + * + * @author Jens Lehmann + * + */ +public class OntologyEngineering { + + private static double minAccuracy = 0.85; + + private static double noisePercent = 5.0; + + private static int minInstanceCount = 3; + + private static int algorithmRuntimeInSeconds = 10; + + private static DecimalFormat df = new DecimalFormat(); + + // for performance measurements and development + private static boolean autoMode = true; + // if set to true, this overrides the two options below and tests all four combinations => doesn't seem to work + private static boolean testFCIApprox = true; + private static boolean useFastInstanceChecker = false; + private static boolean useApproximations = false; + private static boolean computeApproxDiff = true; + private static boolean useFMeasure = true; + public int i =1; + public static File f = new File("GS1.txt"); + public static void main(String[] args) throws ComponentInitException, + LearningProblemUnsupportedException, IOException, Exception { + + Logger.getRootLogger().setLevel(Level.WARN); + String fileName = "GeoSkills.owl"; + // OWL file is the first argument of the script + //File file = new File(filePath); + File file = new File("/home/hajira/Documents/AKSW/Celoe datasets/"+fileName); + OWLOntology ontology = OWLManager.createOWLOntologyManager().loadOntologyFromOntologyDocument(file); + AbstractKnowledgeSource ks = new OWLAPIOntology(ontology); + ks.init(); + + AbstractReasonerComponent rc ; + + // load OWL in reasoner + + + System.out.println("Loaded ontology " + fileName + "."); + + + // ??? we need to extract classes from LP not give a target class + //OWLClass classToDescribe = new OWLClassImpl(IRI.create("http://www.example.org/lymphography#Target2_Metastases")); + + //OWLClass classToDescribe = new OWLClassImpl(IRI.create("http://www.w3.org/2001/XMLSchema#string")); +// OWLClass classToDescribe = new OWLClassImpl(IRI.create("http://www.daml.org/2003/02/fips55/fips-55-ont#Place")); +// //OWLClass classToDescribe = new OWLClassImpl(IRI.create("http://www.example.org/lymphography#LND10_3")); +// ClassLearningProblem lp = new ClassLearningProblem(rc); +// lp.setClassToDescribe(classToDescribe); +// lp.init(); + + +// ?????????????????????????????????????? // How to give diff values of heuristics ?? + if(testFCIApprox) { + useFastInstanceChecker = false; + useApproximations = false; + rc=new OWLAPIReasoner(ks); + rc.init(); + run(rc); + + + useFastInstanceChecker = false; + useApproximations = true; + rc=new OWLAPIReasoner(ks); + rc.init(); + run(rc); + + + useFastInstanceChecker = true; + useApproximations = false; + rc = new ClosedWorldReasoner(ks); + rc.init(); + run(rc); + + + useFastInstanceChecker = true; + useApproximations = true; + rc = new ClosedWorldReasoner(ks); + rc.init(); + run(rc); + } else { + rc = new ClosedWorldReasoner(ks); + rc.init(); + run(rc); + } + } + + @SuppressWarnings("unchecked") + public static void run(AbstractReasonerComponent reasoner) throws ComponentInitException, IOException, LearningProblemUnsupportedException { + // ComponentManager cm = ComponentManager.getInstance(); + + String baseURI = reasoner.getBaseURI(); + Map prefixes = reasoner.getPrefixes(); + + // general statistical information + String userInputProtocol = ""; + int classCandidatesCount = 0; + Stat instanceCountStat = new Stat(); + Stat classExpressionTestsStat = new Stat(); + Stat approxDiffStat = new Stat(); + + // equivalence classes + int candidatesAboveThresholdCount = 0; + int missesCount = 0; + int foundDescriptionCount = 0; + int noSensibleDescriptionCount = 0; + int inconsistencyDetected = 0; + int moreInstancesCount = 0; + int nonPerfectCount = 0; + Stat moreInstancesCountStat = new Stat(); + Stat accStat = new Stat(); + Stat accSelectedStat = new Stat(); + Stat accAboveThresholdStat = new Stat(); + Stat positionStat = new Stat(); + + // super classes + int candidatesAboveThresholdCountSC = 0; + int missesCountSC = 0; + int foundDescriptionCountSC = 0; + int noSensibleDescriptionCountSC = 0; + int inconsistencyDetectedSC = 0; + int moreInstancesCountSC = 0; + int nonPerfectCountSC = 0; + Stat moreInstancesCountStatSC = new Stat(); + Stat accStatSC = new Stat(); + Stat accSelectedStatSC = new Stat(); + Stat accAboveThresholdStatSC = new Stat(); + Stat positionStatSC = new Stat(); + +// + + // loop through all classes + //Set classes = new TreeSet(reasoner.getNamedClasses()); + //classes.remove(new NamedClass("http://www.w3.org/2002/07/owl#Thing")); + // reduce number of classes for testing purposes +// shrinkSet(classes, 20); + + Set classes = new TreeSet(reasoner.getClasses()); + + for (OWLClass nc : classes) { + // check whether the class has sufficient instances + // ???? + //int instanceCount = reasoner.getRelatedIndividuals(nc, objectProperty).getIndividuals(nc).size(); + int instanceCount = reasoner.getIndividuals(nc).size(); + if (instanceCount < minInstanceCount) { + System.out.println("class " + nc.toString() + + + " has only " + instanceCount + " instances (minimum: " + minInstanceCount + + ") - skipping"); + } else { + System.out.println("\nlearning axioms for class " + + nc.toString() + " with " + instanceCount + + " instances"); + classCandidatesCount++; + instanceCountStat.addNumber(instanceCount); + + TreeSet suggestions; + // i=0 is equivalence and i=1 is super class + // what is the difference between the two ?? + + for (int i = 0; i <= 1; i++) { + // learn equivalence axiom + ClassLearningProblem lp = new ClassLearningProblem(reasoner); + lp.setClassToDescribe(nc); + if (i == 0) { + System.out + .println("generating suggestions for equivalent class (please wait " + + algorithmRuntimeInSeconds + " seconds)"); + lp.setEquivalence(true); + } else { + System.out.println("suggestions for super class (please wait " + + algorithmRuntimeInSeconds + " seconds)"); + lp.setEquivalence(false); + } + AccMethodTwoValued acc; + if (useApproximations) { + if (useFMeasure) { + acc = new AccMethodFMeasureApprox(); + } else { + acc = new AccMethodPredAccApprox(); + } + ((AccMethodTwoValuedApproximate)acc).setReasoner(reasoner); + } else { + if (useFMeasure) { + acc = new AccMethodFMeasure(); + } else { + acc = new AccMethodPredAcc(); + } + } + acc.init(); + lp.setAccuracyMethod(acc); + lp.init(); + CELOE celoe = new CELOE(lp, reasoner); + celoe.setMaxExecutionTimeInSeconds(algorithmRuntimeInSeconds); + celoe.setNoisePercentage(noisePercent); + celoe.setMaxNrOfResults(10); + celoe.init(); + + RhoDRDown op = (RhoDRDown) celoe.getOperator(); + op.setUseNegation(false); + op.setFrequencyThreshold(10); + op.setUseDataHasValueConstructor(true); + + celoe.start(); + classExpressionTestsStat.addNumber(celoe.getClassExpressionTests()); + + // test whether a solution above the threshold was found + EvaluatedDescription best = celoe.getCurrentlyBestEvaluatedDescription(); + double bestAcc = best.getAccuracy(); + if (i == 0) { + accStat.addNumber(bestAcc); + } else { + accStatSC.addNumber(bestAcc); + } + //if (bestAcc < minAccuracy || (best.getDescription() instanceof Thing)) { + if (bestAcc < minAccuracy ) { + System.out + .println("The algorithm did not find a suggestion with an accuracy above the threshold of " + + (100 * minAccuracy) + + "% or the best description is not appropriate. (The best one was \"" + + best.getDescription().toString() + + "\" with an accuracy of " + + df.format(bestAcc) + ".) - skipping"); + } else { + + if (i == 0) { + accAboveThresholdStat.addNumber(bestAcc); + candidatesAboveThresholdCount++; + } else { + accAboveThresholdStatSC.addNumber(bestAcc); + candidatesAboveThresholdCountSC++; + } + + suggestions = (TreeSet) celoe + .getCurrentlyBestEvaluatedDescriptions(); + List suggestionsList = new LinkedList( + suggestions.descendingSet()); + + if(computeApproxDiff) { + for(EvaluatedDescriptionClass ed : suggestionsList) { + OWLClassExpression d = ed.getDescription(); + // lp.setAccuracyMethod(accuracyMethod); + //lp.getReasoningUtil().getAccuracyOrTooWeak2() + double approx = lp.getAccuracyOrTooWeakApprox(d, noisePercent/(double)100); + double exact = lp.getAccuracyOrTooWeakExact(d, noisePercent/(double)100); + + double diff = Math.abs(approx-exact); + // do not count "too weak" + if(approx > -0.01 && exact > -0.01) { + approxDiffStat.addNumber(diff); + } + } + } + + int nr = 0; + for (EvaluatedDescription suggestion : suggestionsList) { + System.out.println(nr + + ": " + + suggestion.getDescription().toString() + " (accuracy: " + + df.format(suggestion.getAccuracy()) + ")"); + nr++; + } + + // knowledge engineer feedback: + // - number 0-9: one of axioms is appropriate + // - m ("missing"): none of the axioms is appropriate, + // but there is one which was not found + // - n ("none"): none of the axioms is appropriate and + // there is probably no other appropriate axiom + System.out.println("Type a number (\"0\"-\"" + + (suggestions.size() - 1) + + "\") if any of the suggestions is appropriate (if several are possible choose the lowest number). Type \"n\" if there is no appropriate suggestion for this class in your opinion. Type \"m\" if there is an appropriate suggestion in your opinion, but the algorithm did not suggest it."); + + String[] inputs = new String[suggestions.size() + 2]; + inputs[0] = "m"; + inputs[1] = "n"; + for (int j = 0; j < suggestions.size(); j++) { + inputs[j + 2] = "" + j; + } + + List allowedInputs = Arrays.asList(inputs); + String input; + if(autoMode) { + input = "n"; + } else { + do { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + input = br.readLine(); + } while (!allowedInputs.contains(input)); + } + + userInputProtocol += input; + + if (input.equals("m")) { + if (i == 0) { + missesCount++; + } else { + missesCountSC++; + } + System.out + .println("You said the algorithm missed a possible solution."); + } else if (input.equals("n")) { + if (i == 0) { + noSensibleDescriptionCount++; + } else { + noSensibleDescriptionCountSC++; + } + System.out + .println("You said that there is no reasonable class expression."); + } else { + int selectedNr = Integer.parseInt(input); + EvaluatedDescriptionClass selectedExpression = suggestionsList + .get(selectedNr); + System.out.println("You selected \"" + + selectedExpression.getDescription().toString() + "\"."); + boolean isConsistent = selectedExpression.isConsistent(); + if (!isConsistent) { + System.out + .println("Adding the expression leads to an inconsistency of the knowledge base (which is positive since it is a meaningful expression)."); + } + // selectedExpression.getAdditionalInstances(). + Set addInst = selectedExpression.getAdditionalInstances(); + int additionalInstances = addInst.size(); + if (additionalInstances > 0) { + System.out.println("Adding the expression leads to " + + additionalInstances + + " new instances, e.g. \"" + + addInst.iterator().next().toString( + ) + "\"."); + } + + if (i == 0) { + accSelectedStat.addNumber(bestAcc); + positionStat.addNumber(selectedNr); + foundDescriptionCount++; + if (!isConsistent) { + inconsistencyDetected++; + } + if (additionalInstances > 0) { + moreInstancesCount++; + moreInstancesCountStat.addNumber(additionalInstances); + } + if(bestAcc < 0.9999) { + nonPerfectCount++; + } + } else { + accSelectedStatSC.addNumber(bestAcc); + positionStatSC.addNumber(selectedNr); + foundDescriptionCountSC++; + if (!isConsistent) { + inconsistencyDetectedSC++; + } + if (additionalInstances > 0) { + moreInstancesCountSC++; + moreInstancesCountStatSC.addNumber(additionalInstances); + } + if(bestAcc < 0.9999) { + nonPerfectCountSC++; + } + } + } + } + + + } + } + } + + // print summary + + + + if(!f.exists()) + { + try { + f.createNewFile(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + + FileOutputStream fos = new FileOutputStream(f); + PrintStream ps = new PrintStream(fos,true); + System.setOut(ps); + + + + + + System.out.println(); + System.out + .println("knowledge engineering process finished successfully - summary shown below"); + System.out.println(); +// System.out.println("ontology: " + args[0]); + System.out.println("settings: " + minAccuracy + " min accuracy, " + minInstanceCount + " min instances, " + algorithmRuntimeInSeconds + "s algorithm runtime"); + System.out.println("user input protocol: " + userInputProtocol); + System.out.println("classes in ontology: " + classes.size()); + System.out.println("classes with at least " + minInstanceCount + " instances: " + + classCandidatesCount); + System.out.println("class expressions tested: " + classExpressionTestsStat.prettyPrint("")); + if(computeApproxDiff) { + System.out.println("approximation difference: " + approxDiffStat.prettyPrint()); + } + System.out.println(); + + System.out.println("statistics for equivalence axioms:"); + System.out.println("classes above " + (minAccuracy * 100) + "% threshold: " + + candidatesAboveThresholdCount); + System.out.println("axioms learned succesfully: " + foundDescriptionCount); + System.out.println("axioms missed: " + missesCount); + System.out.println("class with no sensible axioms: " + noSensibleDescriptionCount); + System.out.println("inconsistencies detected: " + inconsistencyDetected); + System.out.println("additional instances found: " + moreInstancesCountStat.prettyPrint("")); + System.out.println("average accuracy overall: " + accStat.prettyPrint("")); + System.out.println("average accuracy of selected expressions: " + + accSelectedStat.prettyPrint("")); + System.out.println("average accuracy of expressions above threshold: " + + accAboveThresholdStat.prettyPrint("")); + System.out.println("non-perfect (not 100% accuracy) axioms selected: " + nonPerfectCount); + System.out.println("average number typed by user: " + positionStat.prettyPrint("")); + System.out.println(); + + System.out.println("statistics for super class axioms:"); + System.out.println("classes above " + (minAccuracy * 100) + "% threshold: " + + candidatesAboveThresholdCountSC); + System.out.println("axioms learned succesfully: " + foundDescriptionCountSC); + System.out.println("axioms missed: " + missesCountSC); + System.out.println("class with no sensible axioms: " + noSensibleDescriptionCountSC); + System.out.println("inconsistencies detected: " + inconsistencyDetectedSC); + System.out.println("additional instances found: " + moreInstancesCountStatSC.prettyPrint("")); + System.out.println("average accuracy overall: " + accStatSC.prettyPrint("")); + System.out.println("average accuracy of selected expressions: " + + accSelectedStatSC.prettyPrint("")); + System.out.println("average accuracy of expressions above threshold: " + + accAboveThresholdStatSC.prettyPrint("")); + System.out.println("non-perfect (not 100% accuracy) axioms selected: " + nonPerfectCountSC); + System.out.println("average number typed by user: " + positionStatSC.prettyPrint("")); + System.out.println(); + + System.out.println("merged statistics for equivalence/superclass:"); + System.out.println("classes above " + (minAccuracy * 100) + "% threshold: " + + (candidatesAboveThresholdCount+candidatesAboveThresholdCountSC)); + System.out.println("axioms learned succesfully: " + (foundDescriptionCount+foundDescriptionCountSC)); + System.out.println("axioms missed: " + (missesCount+missesCountSC)); + System.out.println("class with no sensible axioms: " + (noSensibleDescriptionCount+noSensibleDescriptionCountSC)); + System.out.println("inconsistencies detected: " + (inconsistencyDetected+inconsistencyDetectedSC)); + System.out.println("additional instances found: " + new Stat(moreInstancesCountStat,moreInstancesCountStatSC).prettyPrint("")); + System.out.println("average accuracy overall: " + new Stat(accStat,accStatSC).prettyPrint("")); + System.out.println("average accuracy of selected expressions: " + + new Stat(accSelectedStat,accSelectedStatSC).prettyPrint("")); + System.out.println("average accuracy of expressions above threshold: " + + new Stat(accAboveThresholdStat,accAboveThresholdStatSC).prettyPrint("")); + System.out.println("non-perfect (not 100% accuracy) axioms selected: " + (nonPerfectCount+nonPerfectCountSC)); + System.out.println("average number typed by user: " + new Stat(positionStat,positionStatSC).prettyPrint("")); + System.out.println(); + //out.close(); + ps.close(); + } + + @SuppressWarnings("unused") + private static void shrinkSet(Set set, int nrOfElements) { + while (set.size() > nrOfElements) { + Iterator it = set.iterator(); + it.next(); + it.remove(); + } + } +} \ No newline at end of file diff --git a/components-core/src/test/java/org/dllearner/learningproblems/package-info.java b/components-core/src/test/java/org/dllearner/learningproblems/package-info.java index 57cae3314d..3938f83a15 100644 --- a/components-core/src/test/java/org/dllearner/learningproblems/package-info.java +++ b/components-core/src/test/java/org/dllearner/learningproblems/package-info.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ /** * @author Lorenz Buehmann * diff --git a/components-core/src/test/java/org/dllearner/test/ClassCastExceptionTest.java b/components-core/src/test/java/org/dllearner/test/ClassCastExceptionTest.java index 95d33972a4..2eb18b29f9 100644 --- a/components-core/src/test/java/org/dllearner/test/ClassCastExceptionTest.java +++ b/components-core/src/test/java/org/dllearner/test/ClassCastExceptionTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test; import java.io.File; diff --git a/components-core/src/test/java/org/dllearner/test/ClassLearningProblemExample.java b/components-core/src/test/java/org/dllearner/test/ClassLearningProblemExample.java index 59e98f66ef..418f34672a 100644 --- a/components-core/src/test/java/org/dllearner/test/ClassLearningProblemExample.java +++ b/components-core/src/test/java/org/dllearner/test/ClassLearningProblemExample.java @@ -1,30 +1,40 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.test; -import java.io.File; -import java.util.List; -import java.util.Timer; -import java.util.TimerTask; - import org.dllearner.algorithms.celoe.CELOE; -import org.dllearner.core.AbstractCELA; -import org.dllearner.core.AbstractKnowledgeSource; -import org.dllearner.core.AbstractReasonerComponent; -import org.dllearner.core.EvaluatedDescription; +import org.dllearner.core.*; +import org.dllearner.core.StringRenderer.Rendering; import org.dllearner.kb.OWLAPIOntology; import org.dllearner.learningproblems.ClassLearningProblem; import org.dllearner.learningproblems.EvaluatedDescriptionClass; import org.dllearner.reasoning.ClosedWorldReasoner; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.io.ToStringRenderer; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLOntology; import org.semanticweb.owlapi.model.OWLOntologyManager; - import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl; -import uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntaxObjectRenderer; + +import java.io.File; +import java.util.List; +import java.util.Timer; +import java.util.TimerTask; /** * Simple test class that learns a description of a given class. @@ -34,7 +44,7 @@ public class ClassLearningProblemExample { public static void main(String[] args) throws Exception { - ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); + StringRenderer.setRenderer(Rendering.DL_SYNTAX); // load a knowledge base String ontologyPath = "../examples/swore/swore.rdf"; diff --git a/components-core/src/test/java/org/dllearner/test/ComponentTest.java b/components-core/src/test/java/org/dllearner/test/ComponentTest.java index d775606274..a3e76f73c3 100644 --- a/components-core/src/test/java/org/dllearner/test/ComponentTest.java +++ b/components-core/src/test/java/org/dllearner/test/ComponentTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test; import java.net.MalformedURLException; @@ -63,7 +62,8 @@ public static void main(String[] args) throws ComponentInitException, MalformedU reasoner.init(); OWLDataFactory df = new OWLDataFactoryImpl(); - PrefixManager pm = new DefaultPrefixManager("http://localhost/foo#"); + PrefixManager pm = new DefaultPrefixManager(); + pm.setDefaultPrefix("http://localhost/foo#"); // create a learning problem and set positive and negative examples PosNegLPStandard lp = new PosNegLPStandard(reasoner); diff --git a/components-core/src/test/java/org/dllearner/test/FaCTBugDemo.java b/components-core/src/test/java/org/dllearner/test/FaCTBugDemo.java index f46e3355b1..a06ba1f396 100644 --- a/components-core/src/test/java/org/dllearner/test/FaCTBugDemo.java +++ b/components-core/src/test/java/org/dllearner/test/FaCTBugDemo.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test; diff --git a/components-core/src/test/java/org/dllearner/test/FilterTest.java b/components-core/src/test/java/org/dllearner/test/FilterTest.java index 44b4f5e5f7..ca569b4921 100644 --- a/components-core/src/test/java/org/dllearner/test/FilterTest.java +++ b/components-core/src/test/java/org/dllearner/test/FilterTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test; import java.util.SortedSet; @@ -24,7 +23,6 @@ import org.dllearner.kb.sparql.SPARQLTasks; import org.dllearner.kb.sparql.SparqlEndpoint; -import org.dllearner.utilities.datastructures.SetManipulation; import org.dllearner.utilities.datastructures.StringTuple; import org.dllearner.utilities.statistics.SimpleClock; @@ -104,7 +102,7 @@ static void testShort(){ tupleset = st.queryAsTuple(subject, true); } - SetManipulation.printSet("before", tupleset); + System.out.println("before ["+ tupleset.size()+"]: "+tupleset); sc.printAndSet("SHORT "); @@ -170,8 +168,8 @@ static void testShortWithFilter(){ //afterfilter = re.filterTuples(subject,tupleset); } sc.printAndSet("SHORT with filter"); - SetManipulation.printSet("before", tupleset); - SetManipulation.printSet("after", afterfilter); + System.out.println("before ["+ tupleset.size()+"]: "+tupleset); + System.out.println("before ["+ afterfilter.size()+"]: "+afterfilter); } } diff --git a/components-core/src/test/java/org/dllearner/test/HermitTest.java b/components-core/src/test/java/org/dllearner/test/HermitTest.java index 2e8d05a79c..ef0e3a250d 100644 --- a/components-core/src/test/java/org/dllearner/test/HermitTest.java +++ b/components-core/src/test/java/org/dllearner/test/HermitTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,24 +16,13 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test; -import org.semanticweb.HermiT.Reasoner.ReasonerFactory; -import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLObjectProperty; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyCreationException; -import org.semanticweb.owlapi.model.OWLOntologyManager; -import org.semanticweb.owlapi.reasoner.ConsoleProgressMonitor; -import org.semanticweb.owlapi.reasoner.InferenceType; -import org.semanticweb.owlapi.reasoner.OWLReasoner; -import org.semanticweb.owlapi.reasoner.OWLReasonerConfiguration; -import org.semanticweb.owlapi.reasoner.SimpleConfiguration; - import com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory; +import org.semanticweb.owlapi.apibinding.OWLManager; +import org.semanticweb.owlapi.model.*; +import org.semanticweb.owlapi.model.parameters.Imports; +import org.semanticweb.owlapi.reasoner.*; public class HermitTest { @@ -50,21 +39,21 @@ public static void main(String[] args) throws OWLOntologyCreationException{ OWLReasoner pellet = PelletReasonerFactory.getInstance().createNonBufferingReasoner(ontology, conf); pellet.precomputeInferences(InferenceType.CLASS_HIERARCHY, InferenceType.CLASS_ASSERTIONS); - for(OWLIndividual ind : ontology.getIndividualsInSignature(true)){ + for(OWLIndividual ind : ontology.getIndividualsInSignature(Imports.INCLUDED)){ System.out.println("Individual: " + ind); - for(OWLObjectProperty prop : ontology.getObjectPropertiesInSignature(true)){ + for(OWLObjectProperty prop : ontology.getObjectPropertiesInSignature(Imports.INCLUDED)){ System.out.println("Property: " + prop); pellet.getObjectPropertyValues(ind.asOWLNamedIndividual(), prop); } } System.out.println("Using HermiT reasoner"); - OWLReasoner hermit = new ReasonerFactory().createNonBufferingReasoner(ontology, conf); + OWLReasoner hermit = new org.semanticweb.HermiT.ReasonerFactory().createNonBufferingReasoner(ontology, conf); hermit.precomputeInferences(InferenceType.CLASS_HIERARCHY, InferenceType.CLASS_ASSERTIONS); - for(OWLIndividual ind : ontology.getIndividualsInSignature(true)){ + for(OWLIndividual ind : ontology.getIndividualsInSignature(Imports.INCLUDED)){ System.out.println("Individual: " + ind); - for(OWLObjectProperty prop : ontology.getObjectPropertiesInSignature(true)){ + for(OWLObjectProperty prop : ontology.getObjectPropertiesInSignature(Imports.INCLUDED)){ System.out.println("Property: " + prop); hermit.getObjectPropertyValues(ind.asOWLNamedIndividual(), prop); } diff --git a/components-core/src/test/java/org/dllearner/test/JenaConstruct.java b/components-core/src/test/java/org/dllearner/test/JenaConstruct.java index 3d4dab478f..8e2efa9b2d 100644 --- a/components-core/src/test/java/org/dllearner/test/JenaConstruct.java +++ b/components-core/src/test/java/org/dllearner/test/JenaConstruct.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test; import java.io.BufferedReader; diff --git a/components-core/src/test/java/org/dllearner/test/JenaHTTPTest.java b/components-core/src/test/java/org/dllearner/test/JenaHTTPTest.java index 94f7930803..ff15e9ac4b 100644 --- a/components-core/src/test/java/org/dllearner/test/JenaHTTPTest.java +++ b/components-core/src/test/java/org/dllearner/test/JenaHTTPTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test; import java.io.BufferedInputStream; diff --git a/components-core/src/test/java/org/dllearner/test/JenaLongQueryTest.java b/components-core/src/test/java/org/dllearner/test/JenaLongQueryTest.java index 8eb570e2b9..8e3043e73a 100644 --- a/components-core/src/test/java/org/dllearner/test/JenaLongQueryTest.java +++ b/components-core/src/test/java/org/dllearner/test/JenaLongQueryTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test; import com.hp.hpl.jena.query.ResultSet; diff --git a/components-core/src/test/java/org/dllearner/test/JenaQueryToResultSpeedTest.java b/components-core/src/test/java/org/dllearner/test/JenaQueryToResultSpeedTest.java index 42528e9b64..f42cc0bf68 100644 --- a/components-core/src/test/java/org/dllearner/test/JenaQueryToResultSpeedTest.java +++ b/components-core/src/test/java/org/dllearner/test/JenaQueryToResultSpeedTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test; import org.dllearner.kb.sparql.SparqlEndpoint; diff --git a/components-core/src/test/java/org/dllearner/test/OWLAPIBugDemo.java b/components-core/src/test/java/org/dllearner/test/OWLAPIBugDemo.java index 495a332e98..4364363db2 100644 --- a/components-core/src/test/java/org/dllearner/test/OWLAPIBugDemo.java +++ b/components-core/src/test/java/org/dllearner/test/OWLAPIBugDemo.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,31 +16,20 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test; -import java.io.File; -import java.util.HashSet; -import java.util.Set; - +import com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.model.AddAxiom; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLException; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLObjectProperty; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyManager; +import org.semanticweb.owlapi.model.*; import org.semanticweb.owlapi.reasoner.Node; import org.semanticweb.owlapi.reasoner.NodeSet; import org.semanticweb.owlapi.reasoner.OWLReasoner; import org.semanticweb.owlapi.util.SimpleIRIMapper; -import com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory; +import java.io.File; +import java.util.HashSet; +import java.util.Set; public class OWLAPIBugDemo { @@ -54,7 +43,7 @@ public static void main(String[] args) { File f = new File("src/dl-learner/org/dllearner/tools/ore/inconsistent.owl"); IRI physicalURI = IRI.create(f.toURI()); SimpleIRIMapper mapper = new SimpleIRIMapper(ontologyURI, physicalURI); - manager.addIRIMapper(mapper); + manager.getIRIMappers().add(mapper); OWLOntology ontology = manager.createOntology(ontologyURI); OWLDataFactory factory = manager.getOWLDataFactory(); @@ -89,9 +78,6 @@ public static void main(String[] args) { AddAxiom addAxiom3 = new AddAxiom(ontology, axiom3); manager.applyChange(addAxiom3); - Set ontologies = new HashSet(); - ontologies.add(ontology); - OWLReasoner reasoner = new PelletReasonerFactory().createReasoner(ontology); // class cast exception diff --git a/components-core/src/test/java/org/dllearner/test/OWLAPIConsistency.java b/components-core/src/test/java/org/dllearner/test/OWLAPIConsistency.java index 52b5aeccba..2d37f30db8 100644 --- a/components-core/src/test/java/org/dllearner/test/OWLAPIConsistency.java +++ b/components-core/src/test/java/org/dllearner/test/OWLAPIConsistency.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test; import java.io.File; diff --git a/components-core/src/test/java/org/dllearner/test/OWLLinkReasonerTest.java b/components-core/src/test/java/org/dllearner/test/OWLLinkReasonerTest.java index 1c0d426ddd..aca28b0d60 100644 --- a/components-core/src/test/java/org/dllearner/test/OWLLinkReasonerTest.java +++ b/components-core/src/test/java/org/dllearner/test/OWLLinkReasonerTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test; import java.io.File; diff --git a/components-core/src/test/java/org/dllearner/test/PelletPerformanceProblem.java b/components-core/src/test/java/org/dllearner/test/PelletPerformanceProblem.java index 956f2e4587..e98857cade 100644 --- a/components-core/src/test/java/org/dllearner/test/PelletPerformanceProblem.java +++ b/components-core/src/test/java/org/dllearner/test/PelletPerformanceProblem.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test; import java.io.File; diff --git a/components-core/src/test/java/org/dllearner/test/PunningTest.java b/components-core/src/test/java/org/dllearner/test/PunningTest.java index ac2a07d236..bb1956ba6c 100644 --- a/components-core/src/test/java/org/dllearner/test/PunningTest.java +++ b/components-core/src/test/java/org/dllearner/test/PunningTest.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.test; @@ -21,7 +36,8 @@ import org.junit.Assert; import org.junit.Test; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.io.ToStringRenderer; +import org.dllearner.core.StringRenderer; +import org.dllearner.core.StringRenderer.Rendering; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLClass; import org.semanticweb.owlapi.model.OWLClassExpression; @@ -73,7 +89,7 @@ private Set toIRI(Set entities){ @Test public void testPunning() throws OWLOntologyCreationException, ComponentInitException{ - ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); + StringRenderer.setRenderer(Rendering.DL_SYNTAX); OWLOntology ontology = loadExample(); OWLDataFactory df = new OWLDataFactoryImpl(); @@ -119,7 +135,8 @@ public void testPunning() throws OWLOntologyCreationException, ComponentInitExce System.out.println("Classes: " + ontology.getClassesInSignature()); System.out.println("Individuals: " + ontology.getIndividualsInSignature()); - PrefixManager pm = new DefaultPrefixManager("http://ex.org/"); + PrefixManager pm = new DefaultPrefixManager(); + pm.setDefaultPrefix("http://ex.org/"); OWLClass fahrzeug = df.getOWLClass("Fahrzeug", pm); OWLClassExpression d = fahrzeug; System.out.println(d); diff --git a/components-core/src/test/java/org/dllearner/test/ReasonerTests.java b/components-core/src/test/java/org/dllearner/test/ReasonerTest.java similarity index 73% rename from components-core/src/test/java/org/dllearner/test/ReasonerTests.java rename to components-core/src/test/java/org/dllearner/test/ReasonerTest.java index 8c543e5ec8..9c281aed22 100644 --- a/components-core/src/test/java/org/dllearner/test/ReasonerTests.java +++ b/components-core/src/test/java/org/dllearner/test/ReasonerTest.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.test; @@ -28,15 +43,18 @@ * @author Lorenz Buehmann * */ -public class ReasonerTests { +public class ReasonerTest { public static void main(String[] args) throws Exception { Logger.getRootLogger().setLevel(Level.OFF); Logger.getLogger(ElkReasoner.class).setLevel(Level.OFF); ReasonerImplementation[] reasonerImplementations = new ReasonerImplementation[]{ - ReasonerImplementation.ELK, ReasonerImplementation.TROWL, ReasonerImplementation.JFACT, - ReasonerImplementation.HERMIT, ReasonerImplementation.PELLET}; + ReasonerImplementation.ELK, +// ReasonerImplementation.TROWL, + ReasonerImplementation.JFACT, + ReasonerImplementation.HERMIT, + ReasonerImplementation.PELLET}; int maxExecutionTimeInSeconds = 10; diff --git a/components-core/src/test/java/org/dllearner/test/SPARQLBasedLearningTest.java b/components-core/src/test/java/org/dllearner/test/SPARQLBasedLearningTest.java index 69d9f7cb83..8e7ed17eca 100644 --- a/components-core/src/test/java/org/dllearner/test/SPARQLBasedLearningTest.java +++ b/components-core/src/test/java/org/dllearner/test/SPARQLBasedLearningTest.java @@ -1,27 +1,64 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.test; -import java.util.TreeSet; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.Timer; +import java.util.TimerTask; -import org.dllearner.algorithms.celoe.CELOE; +import org.dllearner.algorithms.el.ELLearningAlgorithm; +import org.dllearner.core.AbstractCELA; import org.dllearner.core.AbstractKnowledgeSource; import org.dllearner.core.EvaluatedDescription; +import org.dllearner.core.KnowledgeSource; import org.dllearner.kb.OWLAPIOntology; import org.dllearner.kb.SparqlEndpointKS; import org.dllearner.kb.sparql.ClassBasedSampleGenerator; import org.dllearner.kb.sparql.SparqlEndpoint; import org.dllearner.learningproblems.ClassLearningProblem; -import org.dllearner.reasoning.SPARQLReasoner; +import org.dllearner.learningproblems.PosNegLP; +import org.dllearner.learningproblems.PosNegLPStandard; +import org.dllearner.reasoning.ClosedWorldReasoner; +import org.dllearner.reasoning.OWLAPIReasoner; +import org.dllearner.reasoning.ReasonerImplementation; import org.dllearner.refinementoperators.RhoDRDown; +import org.dllearner.utilities.owl.DLSyntaxObjectRenderer; +import org.semanticweb.owlapi.apibinding.OWLManager; +import org.dllearner.core.StringRenderer; +import org.dllearner.core.StringRenderer.Rendering; +import org.semanticweb.owlapi.model.AxiomType; import org.semanticweb.owlapi.model.IRI; +import org.semanticweb.owlapi.model.OWLAxiom; import org.semanticweb.owlapi.model.OWLClass; +import org.semanticweb.owlapi.model.OWLDataFactory; +import org.semanticweb.owlapi.model.OWLDataPropertyRangeAxiom; +import org.semanticweb.owlapi.model.OWLDatatype; import org.semanticweb.owlapi.model.OWLOntology; - -import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl; +import org.semanticweb.owlapi.model.OWLOntologyManager; import com.google.common.collect.Sets; +import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP; + +import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl; /** * @author Lorenz Buehmann @@ -33,27 +70,73 @@ public class SPARQLBasedLearningTest { * @param args */ public static void main(String[] args) throws Exception{ + StringRenderer.setRenderer(Rendering.DL_SYNTAX); SparqlEndpoint endpoint = SparqlEndpoint.getEndpointDBpedia(); // endpoint = new SparqlEndpoint(new URL("http://sake.informatik.uni-leipzig.de:8890/sparql"), "http://dbpedia.org"); - OWLClass cls = new OWLClassImpl(IRI.create("http://dbpedia.org/ontology/SoccerClub")); - SparqlEndpointKS ks = new SparqlEndpointKS(endpoint); + Set ignoredProperties = Sets.newHashSet( + "http://dbpedia.org/ontology/wikiPageRevisionID", + "http://dbpedia.org/ontology/wikiPageID", + "http://dbpedia.org/ontology/abstract", + "http://dbpedia.org/ontology/alias" + ,"http://dbpedia.org/ontology/number" + ,"http://dbpedia.org/ontology/endowment" + ); + + // the class to describe + OWLClass cls = new OWLClassImpl(IRI.create("http://dbpedia.org/ontology/SoccerPlayer")); + + // (optionally) provide the schema + String ontologyURL = "http://mappings.dbpedia.org/server/ontology/dbpedia.owl"; + OWLOntologyManager man = OWLManager.createOWLOntologyManager(); + OWLDataFactory df = man.getOWLDataFactory(); + OWLOntology schemaOntology = man.loadOntology(IRI.create(ontologyURL)); + // OWL API does not support rdf:langString so far + Set rangeAxioms = schemaOntology.getAxioms(AxiomType.DATA_PROPERTY_RANGE); + Set toRemove = new HashSet(); + Set toAdd = new HashSet(); + for (OWLDataPropertyRangeAxiom ax : rangeAxioms) { + OWLDatatype datatype = ax.getRange().asOWLDatatype(); + if(datatype.equals(df.getOWLDatatype(IRI.create("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString")))) { + toRemove.add(ax); +// toAdd.add(df.getOWLDataPropertyRangeAxiom(ax.getProperty(), df.getOWLDatatype(IRI.create("http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral")))); + } + toRemove.add(ax); + } + man.removeAxioms(schemaOntology, toRemove); + man.addAxioms(schemaOntology, toAdd); + // remove functionality axioms because otherwise inconsistency can occur + man.removeAxioms(schemaOntology, schemaOntology.getAxioms(AxiomType.FUNCTIONAL_DATA_PROPERTY)); + man.removeAxioms(schemaOntology, schemaOntology.getAxioms(AxiomType.DISJOINT_CLASSES)); + + KnowledgeSource schemaKS = new OWLAPIOntology(schemaOntology); + schemaKS.init(); + + // setup the dataset + SparqlEndpointKS ks = new SparqlEndpointKS(endpoint, schemaKS); ks.init(); // extract sample of the knowledge base - ClassBasedSampleGenerator sampleGen = new ClassBasedSampleGenerator(ks.getQueryExecutionFactory()); + ClassBasedSampleGenerator sampleGen = new ClassBasedSampleGenerator(ks); sampleGen.addAllowedPropertyNamespaces(Sets.newHashSet("http://dbpedia.org/ontology/")); + sampleGen.addIgnoredProperties(ignoredProperties); sampleGen.addAllowedObjectNamespaces(Sets.newHashSet("http://dbpedia.org/ontology/", "http://dbpedia.org/resource/")); - OWLOntology sample = sampleGen.getSample(cls); + OWLOntology sampleOntology = sampleGen.getSample(cls); + + // add schema axioms to the sample + man.addAxioms(sampleOntology, schemaOntology.getLogicalAxioms()); // setup knowledge source - AbstractKnowledgeSource sampleKS = new OWLAPIOntology(sample); + AbstractKnowledgeSource sampleKS = new OWLAPIOntology(sampleOntology); ks.init(); // setup reasoner - SPARQLReasoner reasoner = new SPARQLReasoner(ks); -// ClosedWorldReasoner reasoner = new ClosedWorldReasoner(sampleKS); +// SPARQLReasoner reasoner = new SPARQLReasoner(ks); + OWLAPIReasoner baseReasoner = new OWLAPIReasoner(sampleKS); + baseReasoner.setReasonerImplementation(ReasonerImplementation.PELLET); + baseReasoner.init(); + ClosedWorldReasoner reasoner = new ClosedWorldReasoner(baseReasoner); reasoner.init(); // setup learning problem @@ -63,30 +146,57 @@ public static void main(String[] args) throws Exception{ // setup refinement operator RhoDRDown op = new RhoDRDown(); + op.setUseTimeDatatypes(false); op.setReasoner(reasoner); op.setUseNegation(false); op.init(); // setup learning algorithm - CELOE celoe = new CELOE(lp, reasoner); - celoe.setOperator(op); - celoe.setWriteSearchTree(true); - celoe.setSearchTreeFile("/tmp/searchtree-celoe.txt"); - celoe.setReplaceSearchTree(true); - celoe.setMaxExecutionTimeInSeconds(10); - celoe.setNoisePercentage(0); - celoe.init(); +// final AbstractCELA alg; +// +// CELOE celoe = new CELOE(lp, reasoner); +// celoe.setOperator(op); +// celoe.setWriteSearchTree(true); +// celoe.setSearchTreeFile("/tmp/searchtree-celoe.txt"); +// celoe.setReplaceSearchTree(true); +// celoe.setMaxExecutionTimeInSeconds(10); +// celoe.setNoisePercentage(60); +// celoe.init(); +// alg = celoe; + + PosNegLP lp2 = new PosNegLPStandard(reasoner); + lp2.setPositiveExamples(sampleGen.getPositiveExamples()); + lp2.setNegativeExamples(sampleGen.getNegativeExamples()); + lp2.init(); + ELLearningAlgorithm el = new ELLearningAlgorithm(lp2, reasoner); + el.setMaxExecutionTimeInSeconds(10); +// el.setStartClass(cls); + el.setClassToDescribe(cls); + el.setNoisePercentage(70); + el.init(); + final AbstractCELA alg = el; + + TimerTask t = new TimerTask() { + + @Override + public void run() { + System.out.println("T:" + alg.getCurrentlyBestEvaluatedDescriptions(10, 0.5, true)); + + } + }; + Timer timer = new Timer(); + timer.schedule(t, 1000, 1000); // run - celoe.start(); + alg.start(); // Set refinements = op.refine(new OWLClassImpl(IRI.create("http://dbpedia.org/ontology/Work")), 5); // for (OWLClassExpression ref : refinements) { // System.out.println(ref + ":" + lp.getAccuracy(ref, 1.0)); // } - - TreeSet solutions = celoe.getCurrentlyBestEvaluatedDescriptions(); + timer.cancel(); + List solutions = alg.getCurrentlyBestEvaluatedDescriptions(10, 0.5, true); System.out.println(solutions); } diff --git a/components-core/src/test/java/org/dllearner/test/SPARQLReasonerTest.java b/components-core/src/test/java/org/dllearner/test/SPARQLReasonerTest.java index 2e586c58ee..3f25349b60 100644 --- a/components-core/src/test/java/org/dllearner/test/SPARQLReasonerTest.java +++ b/components-core/src/test/java/org/dllearner/test/SPARQLReasonerTest.java @@ -1,29 +1,39 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.test; -import java.net.URL; -import java.util.SortedSet; - +import com.hp.hpl.jena.rdf.model.Model; import org.apache.jena.riot.RDFDataMgr; import org.dllearner.algorithms.celoe.CELOE; import org.dllearner.core.AbstractReasonerComponent; +import org.dllearner.core.StringRenderer; +import org.dllearner.core.StringRenderer.Rendering; import org.dllearner.kb.LocalModelBasedSparqlEndpointKS; -import org.dllearner.kb.SparqlEndpointKS; -import org.dllearner.kb.sparql.SparqlEndpoint; import org.dllearner.learningproblems.ClassLearningProblem; import org.dllearner.reasoning.SPARQLReasoner; import org.dllearner.refinementoperators.RhoDRDown; -import org.dllearner.utilities.owl.DLSyntaxObjectRenderer; -import org.semanticweb.owlapi.io.ToStringRenderer; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLClass; import org.semanticweb.owlapi.model.OWLIndividual; - import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl; -import com.hp.hpl.jena.rdf.model.Model; +import java.util.SortedSet; /** * @author Lorenz Buehmann @@ -33,7 +43,7 @@ public class SPARQLReasonerTest { public static void main(String[] args) throws Exception{ - ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); + StringRenderer.setRenderer(Rendering.DL_SYNTAX); Model model = RDFDataMgr.loadModel("../examples/swore/swore.rdf"); LocalModelBasedSparqlEndpointKS ks = new LocalModelBasedSparqlEndpointKS(model, true); @@ -43,6 +53,7 @@ public static void main(String[] args) throws Exception{ AbstractReasonerComponent rc = new SPARQLReasoner(ks); // AbstractReasonerComponent rc = new SPARQLReasoner(new SparqlEndpointKS(new SparqlEndpoint( // new URL("http://localhost:8890/sparql"), "http://family-benchmark.owl"))); + rc.setUseInstanceChecks(false); rc.init(); OWLClass classToDescribe = new OWLClassImpl(IRI.create("http://ns.softwiki.de/req/CustomerRequirement")); @@ -64,7 +75,6 @@ public static void main(String[] args) throws Exception{ // lp.init(); ClassLearningProblem lp = new ClassLearningProblem(rc); - lp.setUseInstanceChecks(false); lp.setClassToDescribe(classToDescribe); lp.init(); diff --git a/components-core/src/test/java/org/dllearner/test/SparqlEndpointTest.java b/components-core/src/test/java/org/dllearner/test/SparqlEndpointTest.java index 1ee447a2ae..f80a27436d 100644 --- a/components-core/src/test/java/org/dllearner/test/SparqlEndpointTest.java +++ b/components-core/src/test/java/org/dllearner/test/SparqlEndpointTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test; import java.util.HashSet; diff --git a/components-core/src/test/java/org/dllearner/test/SparqlExtractionTest.java b/components-core/src/test/java/org/dllearner/test/SparqlExtractionTest.java index 7f6c335c36..6c6f67f1e0 100644 --- a/components-core/src/test/java/org/dllearner/test/SparqlExtractionTest.java +++ b/components-core/src/test/java/org/dllearner/test/SparqlExtractionTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test; import java.net.URL; diff --git a/components-core/src/test/java/org/dllearner/test/SworeTest.java b/components-core/src/test/java/org/dllearner/test/SworeTest.java index 1659243d9e..281f4cca6c 100644 --- a/components-core/src/test/java/org/dllearner/test/SworeTest.java +++ b/components-core/src/test/java/org/dllearner/test/SworeTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test; import java.net.MalformedURLException; diff --git a/components-core/src/test/java/org/dllearner/test/TestGetExampleBug.java b/components-core/src/test/java/org/dllearner/test/TestGetExampleBug.java index 798a49ce86..750767efb6 100644 --- a/components-core/src/test/java/org/dllearner/test/TestGetExampleBug.java +++ b/components-core/src/test/java/org/dllearner/test/TestGetExampleBug.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test; import java.net.URL; diff --git a/components-core/src/test/java/org/dllearner/test/TestOneQueryForMusicRecommender.java b/components-core/src/test/java/org/dllearner/test/TestOneQueryForMusicRecommender.java index 3ed5f43847..bb5f421d5b 100644 --- a/components-core/src/test/java/org/dllearner/test/TestOneQueryForMusicRecommender.java +++ b/components-core/src/test/java/org/dllearner/test/TestOneQueryForMusicRecommender.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test; import java.net.URL; diff --git a/components-core/src/test/java/org/dllearner/test/TripleTypeTest.java b/components-core/src/test/java/org/dllearner/test/TripleTypeTest.java index bfdb826a6a..10d76a40ca 100644 --- a/components-core/src/test/java/org/dllearner/test/TripleTypeTest.java +++ b/components-core/src/test/java/org/dllearner/test/TripleTypeTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test; import java.util.List; diff --git a/components-core/src/test/java/org/dllearner/test/ValueKBParserTest.java b/components-core/src/test/java/org/dllearner/test/ValueKBParserTest.java index 5410955291..3d3da8eac7 100644 --- a/components-core/src/test/java/org/dllearner/test/ValueKBParserTest.java +++ b/components-core/src/test/java/org/dllearner/test/ValueKBParserTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test; import java.util.SortedSet; diff --git a/components-core/src/test/java/org/dllearner/test/junit/AllTestsRunner.java b/components-core/src/test/java/org/dllearner/test/junit/AllTestsRunner.java index 445510cac7..ced6939e70 100644 --- a/components-core/src/test/java/org/dllearner/test/junit/AllTestsRunner.java +++ b/components-core/src/test/java/org/dllearner/test/junit/AllTestsRunner.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,11 +16,8 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test.junit; -import junit.framework.TestSuite; - import org.apache.log4j.ConsoleAppender; import org.apache.log4j.Level; import org.apache.log4j.Logger; @@ -57,20 +54,25 @@ public static void main(String[] args) { // runs everything except example test Result result = JUnitCore.runClasses( - ClassExpressionTests.class, - ComponentTests.class, - ELDescriptionTreeTests.class, - ELDownTests.class, - HeuristicTests.class, - ParserTests.class, - RefinementOperatorTests.class, - SimulationTests.class, - UtilitiesTests.class); + ClassExpressionTest.class, + ComponentTest.class, + ELDescriptionTreeTest.class, + ELDownTest.class, + HeuristicTest.class, + ParserTest.class, + RefinementOperatorTest.class, + SimulationTest.class, + UtilitiesTest.class); + + if(result.wasSuccessful()) { + System.out.println("All tests succeeded!"); + } else { + System.err.println("Some tests failed:"); - for (Failure failure : result.getFailures()) { - System.err.println(failure.toString()); + for (Failure failure : result.getFailures()) { + System.err.println(failure.toString()); + } } - System.out.println(result.wasSuccessful()); } } diff --git a/components-core/src/test/java/org/dllearner/test/junit/ClassExpressionTests.java b/components-core/src/test/java/org/dllearner/test/junit/ClassExpressionTest.java similarity index 55% rename from components-core/src/test/java/org/dllearner/test/junit/ClassExpressionTests.java rename to components-core/src/test/java/org/dllearner/test/junit/ClassExpressionTest.java index 8961e580fd..f081ee2d6d 100644 --- a/components-core/src/test/java/org/dllearner/test/junit/ClassExpressionTests.java +++ b/components-core/src/test/java/org/dllearner/test/junit/ClassExpressionTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,30 +16,24 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test.junit; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - import org.dllearner.core.AbstractReasonerComponent; +import org.dllearner.core.StringRenderer; import org.dllearner.parser.KBParser; import org.dllearner.parser.ParseException; import org.dllearner.test.junit.TestOntologies.TestOntology; import org.dllearner.utilities.owl.ConceptTransformation; import org.dllearner.utilities.owl.OWLClassExpressionMinimizer; import org.junit.Test; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLObjectProperty; -import org.semanticweb.owlapi.model.PrefixManager; +import org.semanticweb.owlapi.io.OWLObjectRenderer; +import org.semanticweb.owlapi.model.*; import org.semanticweb.owlapi.util.DefaultPrefixManager; - import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + /** * Tests on class expressins, e.g. transformations or tests on them. @@ -47,10 +41,14 @@ * @author Jens Lehmann * */ -public class ClassExpressionTests { +public class ClassExpressionTest { OWLDataFactory df = new OWLDataFactoryImpl(); - PrefixManager pm = new DefaultPrefixManager(""); + PrefixManager pm = new DefaultPrefixManager(); + + public ClassExpressionTest() { + pm.setDefaultPrefix(""); + } /** @@ -108,45 +106,56 @@ public void subExpressionTest1() throws ParseException { */ @Test public void forAllContextTest() { - // create some basic ontology elements - OWLClass a1 = df.getOWLClass("a1", pm); - OWLClass a2 = df.getOWLClass("a2", pm); - OWLObjectProperty p1 = df.getOWLObjectProperty("p1", pm); - OWLObjectProperty p2 = df.getOWLObjectProperty("p2", pm); - OWLObjectProperty p3 = df.getOWLObjectProperty("p3", pm); - OWLIndividual i1 = df.getOWLNamedIndividual("i1", pm); - - // create some class expressions - OWLClassExpression d1 = df.getOWLObjectIntersectionOf(a1,a2); - OWLClassExpression d2 = df.getOWLObjectAllValuesFrom(p1,a1); - OWLClassExpression d3 = df.getOWLObjectUnionOf(d1,d2); - OWLClassExpression d4 = df.getOWLObjectHasValue(p2,i1); - OWLClassExpression d5 = df.getOWLObjectAllValuesFrom(p2,d2); - OWLClassExpression d6 = df.getOWLObjectAllValuesFrom(p1,d4); - OWLClassExpression d7 = df.getOWLObjectSomeValuesFrom(p3,d5); - OWLClassExpression d8 = df.getOWLObjectUnionOf(d2,d5); - OWLClassExpression d9 = df.getOWLObjectAllValuesFrom(p1,d8); - - // a1 AND a2 => should be empty result - assertTrue(ConceptTransformation.getForallContexts(d1).isEmpty()); - // note: the assertions below use toString() which should usually be avoided, but since - // the toString() method of SortedSet is unlikely to change we use it here for convenience - // ALL p1.a1 => context: [[p1]] - assertTrue(ConceptTransformation.getForallContexts(d2).toString().equals("[[]]")); - // (a1 AND a2) OR ALL p1.a1 => [[p1]] - assertTrue(ConceptTransformation.getForallContexts(d3).toString().equals("[[]]")); - // p2 hasValue i1 => [] - assertTrue(ConceptTransformation.getForallContexts(d4).isEmpty()); - // ALL p2.ALL p1.a1 => [[p2],[p1,p2]] - assertTrue(ConceptTransformation.getForallContexts(d5).toString().equals("[[, ], []]")); - // ALL p1.p2 hasValue i1 => [[p1]] - assertTrue(ConceptTransformation.getForallContexts(d6).toString().equals("[[]]")); - // EXISTS p3.ALL p2.ALL p1.a1 => [[p3,p2],[p3,p2,p1]] - assertTrue(ConceptTransformation.getForallContexts(d7).toString().equals("[[, , ], [, ]]")); - // (ALL p1.a1 OR ALL p2.ALL p1.a1) - assertTrue(ConceptTransformation.getForallContexts(d8).toString().equals("[[, ], [], []]")); - // ALL p1.(ALL p1.a1 OR ((a1 AND a2) OR ALL p1.a1)) => [[p1],[p1,p1],[p1,p2],[p1,p2,p1]] - assertTrue(ConceptTransformation.getForallContexts(d9).toString().equals("[[, , ], [, ], [, ], []]")); - + // if we rely on toString comparison below we better be a bit more explicit... + OWLObjectRenderer lastRenderer = StringRenderer.getRenderer(); + try { + StringRenderer.setRenderer(StringRenderer.Rendering.OWLAPI_SYNTAX); + // create some basic ontology elements + OWLClass a1 = df.getOWLClass("a1", pm); + OWLClass a2 = df.getOWLClass("a2", pm); + OWLObjectProperty p1 = df.getOWLObjectProperty("p1", pm); + OWLObjectProperty p2 = df.getOWLObjectProperty("p2", pm); + OWLObjectProperty p3 = df.getOWLObjectProperty("p3", pm); + OWLIndividual i1 = df.getOWLNamedIndividual("i1", pm); + + // create some class expressions + OWLClassExpression d1 = df.getOWLObjectIntersectionOf(a1, a2); + OWLClassExpression d2 = df.getOWLObjectAllValuesFrom(p1, a1); + OWLClassExpression d3 = df.getOWLObjectUnionOf(d1, d2); + OWLClassExpression d4 = df.getOWLObjectHasValue(p2, i1); + OWLClassExpression d5 = df.getOWLObjectAllValuesFrom(p2, d2); + OWLClassExpression d6 = df.getOWLObjectAllValuesFrom(p1, d4); + OWLClassExpression d7 = df.getOWLObjectSomeValuesFrom(p3, d5); + OWLClassExpression d8 = df.getOWLObjectUnionOf(d2, d5); + OWLClassExpression d9 = df.getOWLObjectAllValuesFrom(p1, d8); + + // a1 AND a2 => should be empty result + assertTrue(ConceptTransformation.getForallContexts(d1).isEmpty()); + // note: the assertions below use toString() which should usually be avoided, but since + // the toString() method of SortedSet is unlikely to change we use it here for convenience + // ALL p1.a1 => context: [[p1]] + System.out.println("[[]] ? " + ConceptTransformation.getForallContexts(d2).toString()); + assertTrue(ConceptTransformation.getForallContexts(d2).toString().equals("[[]]")); + // (a1 AND a2) OR ALL p1.a1 => [[p1]] + System.out.println("[[]] ? " + ConceptTransformation.getForallContexts(d3).toString()); + assertTrue(ConceptTransformation.getForallContexts(d3).toString().equals("[[]]")); + // p2 hasValue i1 => [] + assertTrue(ConceptTransformation.getForallContexts(d4).isEmpty()); + // ALL p2.ALL p1.a1 => [[p2],[p1,p2]] + System.out.println("[[, ], []] ? " + ConceptTransformation.getForallContexts(d5).toString()); + assertTrue(ConceptTransformation.getForallContexts(d5).toString().equals("[[, ], []]")); + // ALL p1.p2 hasValue i1 => [[p1]] + System.out.println("[[]] ? " + ConceptTransformation.getForallContexts(d6).toString()); + assertTrue(ConceptTransformation.getForallContexts(d6).toString().equals("[[]]")); + // EXISTS p3.ALL p2.ALL p1.a1 => [[p3,p2],[p3,p2,p1]] + System.out.println("[[, , ], [, ]] ? " + ConceptTransformation.getForallContexts(d7).toString()); + assertTrue(ConceptTransformation.getForallContexts(d7).toString().equals("[[, , ], [, ]]")); + // (ALL p1.a1 OR ALL p2.ALL p1.a1) + assertTrue(ConceptTransformation.getForallContexts(d8).toString().equals("[[, ], [], []]")); + // ALL p1.(ALL p1.a1 OR ((a1 AND a2) OR ALL p1.a1)) => [[p1],[p1,p1],[p1,p2],[p1,p2,p1]] + assertTrue(ConceptTransformation.getForallContexts(d9).toString().equals("[[, , ], [, ], [, ], []]")); + } finally { + StringRenderer.setRenderer(lastRenderer); + } } } diff --git a/components-core/src/test/java/org/dllearner/test/junit/ComponentTests.java b/components-core/src/test/java/org/dllearner/test/junit/ComponentTest.java similarity index 63% rename from components-core/src/test/java/org/dllearner/test/junit/ComponentTests.java rename to components-core/src/test/java/org/dllearner/test/junit/ComponentTest.java index bf3d3b580b..b68be1c4ff 100644 --- a/components-core/src/test/java/org/dllearner/test/junit/ComponentTests.java +++ b/components-core/src/test/java/org/dllearner/test/junit/ComponentTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,16 +16,10 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test.junit; -import static org.junit.Assert.assertFalse; - -import java.lang.reflect.InvocationTargetException; import java.util.Collection; -import java.util.List; -import org.dllearner.core.AbstractComponent; import org.dllearner.core.AnnComponentManager; import org.dllearner.core.Component; import org.junit.Test; @@ -36,27 +30,19 @@ * @author Jens Lehmann * */ -public class ComponentTests { +public class ComponentTest { /** * Checks whether all components implement the getName() method. While it * cannot be enforced to implement a static method, it should be done (e.g. * to be used as label for the component in GUIs). - * @throws SecurityException - * @throws NoSuchMethodException - * @throws InvocationTargetException - * @throws IllegalArgumentException - * @throws IllegalAccessException */ @Test - public void nameTest() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { - String defaultName = AbstractComponent.getName(); + public void nameTest() { AnnComponentManager cm = AnnComponentManager.getInstance(); Collection> components = cm.getComponents(); for (Class component : components) { - String componentName = (String) component.getMethod("getName").invoke(null); - assertFalse(component + " does not overwrite getName().", componentName - .equals(defaultName)); + AnnComponentManager.getName(component); } } diff --git a/components-core/src/test/java/org/dllearner/test/junit/ConfigOptionTest.java b/components-core/src/test/java/org/dllearner/test/junit/ConfigOptionTest.java index b2c57cd5f3..34268b31aa 100644 --- a/components-core/src/test/java/org/dllearner/test/junit/ConfigOptionTest.java +++ b/components-core/src/test/java/org/dllearner/test/junit/ConfigOptionTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -30,23 +30,6 @@ public class ConfigOptionTest { @Test public void testConfigOption() { // now outdated, because property editors do not need to be specified for each option - -// List configOptions = ConfigHelper.getConfigOptions(CELOE.class); -// assertFalse(configOptions.isEmpty()); -// -// CELOE celoe = new CELOE(); -// celoe.setMaxExecutionTimeInSeconds(10); -// Map optionValues = ConfigHelper.getConfigOptionValues(celoe); -// boolean found = false; -// for(Entry entry : optionValues.entrySet()) { -// System.out.println(entry.getKey() + " " + entry.getValue()); -// if(entry.getKey().name().equals("maxExecutionTimeInSeconds")) { -// found = true; -// assertTrue(Integer.valueOf(entry.getValue().toString())==10); -// } -// } -// assertTrue(found); - } } diff --git a/components-core/src/test/java/org/dllearner/test/junit/DisjointClassesLearningTest.java b/components-core/src/test/java/org/dllearner/test/junit/DisjointClassesLearningTest.java index b0a50a0f5a..55951dd511 100644 --- a/components-core/src/test/java/org/dllearner/test/junit/DisjointClassesLearningTest.java +++ b/components-core/src/test/java/org/dllearner/test/junit/DisjointClassesLearningTest.java @@ -1,7 +1,29 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.test.junit; +import java.net.MalformedURLException; + import org.dllearner.algorithms.DisjointClassesLearner; +import org.dllearner.core.ComponentInitException; import org.dllearner.kb.SparqlEndpointKS; +import org.dllearner.kb.sparql.SparqlEndpoint; import org.dllearner.reasoning.SPARQLReasoner; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLClassExpression; @@ -24,21 +46,32 @@ public class DisjointClassesLearningTest { //extends TestCase{ // reasoner.prepareSubsumptionHierarchy(); // } - public void testLearnSingleClass(){ +// @Test + public void testLearnSingleClass() throws MalformedURLException, ComponentInitException{ + ks = new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpediaLiveAKSW()); + ks.init(); + reasoner = new SPARQLReasoner(ks); + reasoner.init(); DisjointClassesLearner l = new DisjointClassesLearner(ks); l.setReasoner(reasoner); l.setMaxExecutionTimeInSeconds(maxExecutionTimeInSeconds); l.setEntityToDescribe(new OWLClassImpl(IRI.create("http://dbpedia.org/ontology/Book"))); - + l.init(); l.start(); System.out.println(l.getCurrentlyBestAxioms(5)); } - public void testLearnForMostGeneralClasses(){ +// @Test + public void testLearnForMostGeneralClasses() throws ComponentInitException, MalformedURLException{ + ks = new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpediaLiveAKSW()); + ks.init(); + reasoner = new SPARQLReasoner(ks); + reasoner.init(); DisjointClassesLearner l = new DisjointClassesLearner(ks); l.setReasoner(reasoner); l.setMaxExecutionTimeInSeconds(maxExecutionTimeInSeconds); + l.init(); for(OWLClassExpression cls : reasoner.getClassHierarchy().getMostGeneralClasses()){ l.setEntityToDescribe(cls.asOWLClass()); diff --git a/components-core/src/test/java/org/dllearner/test/junit/ELDescriptionTreeTests.java b/components-core/src/test/java/org/dllearner/test/junit/ELDescriptionTreeTest.java similarity index 97% rename from components-core/src/test/java/org/dllearner/test/junit/ELDescriptionTreeTests.java rename to components-core/src/test/java/org/dllearner/test/junit/ELDescriptionTreeTest.java index 0da6932b6e..18e1295fc0 100644 --- a/components-core/src/test/java/org/dllearner/test/junit/ELDescriptionTreeTests.java +++ b/components-core/src/test/java/org/dllearner/test/junit/ELDescriptionTreeTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test.junit; import static org.junit.Assert.assertFalse; @@ -51,7 +50,7 @@ * @author Jens Lehmann * */ -public final class ELDescriptionTreeTests { +public final class ELDescriptionTreeTest { @Test public void simulationTest() { diff --git a/components-core/src/test/java/org/dllearner/test/junit/ELDownTests.java b/components-core/src/test/java/org/dllearner/test/junit/ELDownTest.java similarity index 94% rename from components-core/src/test/java/org/dllearner/test/junit/ELDownTests.java rename to components-core/src/test/java/org/dllearner/test/junit/ELDownTest.java index 8a7e9678df..f9cde02828 100644 --- a/components-core/src/test/java/org/dllearner/test/junit/ELDownTests.java +++ b/components-core/src/test/java/org/dllearner/test/junit/ELDownTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test.junit; import static org.junit.Assert.assertTrue; @@ -42,7 +41,7 @@ import org.dllearner.parser.KBParser; import org.dllearner.parser.ParseException; import org.dllearner.reasoning.OWLAPIReasoner; -import org.dllearner.refinementoperators.ELDown3; +import org.dllearner.refinementoperators.ELDown; import org.dllearner.refinementoperators.RefinementOperator; import org.dllearner.test.junit.TestOntologies.TestOntology; import org.dllearner.utilities.Files; @@ -70,10 +69,10 @@ * @author Jens Lehmann * */ -public class ELDownTests { +public class ELDownTest { @SuppressWarnings("unused") - private static Logger logger = Logger.getLogger(ELDownTests.class); + private static Logger logger = Logger.getLogger(ELDownTest.class); OWLDataFactory df = new OWLDataFactoryImpl(); @@ -108,7 +107,8 @@ public void test1() throws ParseException, ComponentInitException, IOException { // For this test, we need to turn instance based disjoints // off! (We do not have any instances here.) - RefinementOperator operator = new ELDown3(rs, false); + RefinementOperator operator = new ELDown(rs, false); + operator.init(); // desired refinements as strings Set desiredString = new TreeSet(); @@ -127,7 +127,6 @@ public void test1() throws ParseException, ComponentInitException, IOException { OWLClassExpression tmp = KBParser.parseConcept(str); // eliminate conjunctions nested in other conjunctions tmp = ConceptTransformation.cleanConcept(tmp); - ConceptTransformation.transformToOrderedForm(tmp); desired.add(tmp); System.out.println("desired: " + tmp); } @@ -186,7 +185,7 @@ public void test1() throws ParseException, ComponentInitException, IOException { } @Test - public void test2() throws ParseException, IOException { + public void test2() throws ParseException, IOException, ComponentInitException { System.out.println("TEST 2"); AbstractReasonerComponent rs = TestOntologies.getTestOntology(TestOntology.SIMPLE_NO_DR); @@ -214,7 +213,6 @@ public void test2() throws ParseException, IOException { for(String str : desiredString) { OWLClassExpression tmp = KBParser.parseConcept(str); ConceptTransformation.cleanConcept(tmp); - ConceptTransformation.transformToOrderedForm(tmp); desired.add(tmp); System.out.println("desired: " + tmp); } @@ -226,14 +224,14 @@ public void test2() throws ParseException, IOException { logger.removeAllAppenders(); logger.addAppender(app); - RefinementOperator operator = new ELDown3(rs); + RefinementOperator operator = new ELDown(rs); + operator.init(); Set refinements = operator.refine(input); // assertTrue(refinements.size() == desired.size()); System.out.println("\nproduced refinements and their unit test status (true = assertion satisfied):"); for(OWLClassExpression refinement : refinements) { - ConceptTransformation.transformToOrderedForm(refinement); boolean ok = desired.contains(refinement); System.out.println(ok + ": " + refinement); // assertTrue(desired.contains(refinement)); @@ -241,7 +239,7 @@ public void test2() throws ParseException, IOException { } @Test - public void test3() throws ParseException, IOException { + public void test3() throws ParseException, IOException, ComponentInitException { System.out.println("TEST 3"); AbstractReasonerComponent rs = TestOntologies.getTestOntology(TestOntology.SIMPLE_NO_DISJOINT); @@ -264,7 +262,6 @@ public void test3() throws ParseException, IOException { for(String str : desiredString) { OWLClassExpression tmp = KBParser.parseConcept(str); tmp = ConceptTransformation.cleanConcept(tmp); - ConceptTransformation.transformToOrderedForm(tmp); desired.add(tmp); System.out.println("desired: " + tmp); } @@ -276,14 +273,14 @@ public void test3() throws ParseException, IOException { logger.removeAllAppenders(); logger.addAppender(app); - RefinementOperator operator = new ELDown3(rs); + RefinementOperator operator = new ELDown(rs); + operator.init(); Set refinements = operator.refine(input); // assertTrue(refinements.size() == desired.size()); System.out.println("\nproduced refinements and their unit test status (true = assertion satisfied):"); for(OWLClassExpression refinement : refinements) { - ConceptTransformation.transformToOrderedForm(refinement); boolean ok = desired.contains(refinement); System.out.println(ok + ": " + refinement); // assertTrue(desired.contains(refinement)); @@ -312,15 +309,19 @@ public void test4() throws ComponentInitException, ParseException, IOException { OWLClassExpression input = KBParser.parseConcept("(\"http://www.co-ode.org/ontologies/galen#1.0\" AND (\"http://www.co-ode.org/ontologies/galen#10.0\" AND (EXISTS \"http://www.co-ode.org/ontologies/galen#DomainAttribute\".(\"http://www.co-ode.org/ontologies/galen#1.0\" AND (\"http://www.co-ode.org/ontologies/galen#6.0\" AND \"http://www.co-ode.org/ontologies/galen#TopCategory\")) AND EXISTS \"http://www.co-ode.org/ontologies/galen#Attribute\".(\"http://www.co-ode.org/ontologies/galen#1.0\" AND (\"http://www.co-ode.org/ontologies/galen#TopCategory\" AND EXISTS \"http://www.co-ode.org/ontologies/galen#Attribute\".TOP)))))"); input = ConceptTransformation.cleanConcept(input); - ELDown3 operator = new ELDown3(reasoner); + ELDown operator = new ELDown(reasoner); + operator.init(); + operator.refine(input); } @Test - public void test5() { + public void test5() throws ComponentInitException { AbstractReasonerComponent rs = TestOntologies.getTestOntology(TestOntology.TRAINS_OWL); - RefinementOperator operator = new ELDown3(rs); + RefinementOperator operator = new ELDown(rs); + operator.init(); + Set refinements = operator.refine(new OWLClassImpl(OWLRDFVocabulary.OWL_THING.getIRI())); for(OWLClassExpression refinement : refinements) { System.out.println(refinement); @@ -357,7 +358,9 @@ public void asTest() throws ComponentInitException, MalformedURLException { ELDescriptionNode w = new ELDescriptionNode(v2, r1); - ELDown3 operator = new ELDown3(reasoner); + ELDown operator = new ELDown(reasoner); + operator.init(); + System.out.println(operator.asCheck(w)); } diff --git a/components-core/src/test/java/org/dllearner/test/junit/HeuristicTests.java b/components-core/src/test/java/org/dllearner/test/junit/HeuristicTest.java similarity index 81% rename from components-core/src/test/java/org/dllearner/test/junit/HeuristicTests.java rename to components-core/src/test/java/org/dllearner/test/junit/HeuristicTest.java index 4019bdc513..62a75662da 100644 --- a/components-core/src/test/java/org/dllearner/test/junit/HeuristicTests.java +++ b/components-core/src/test/java/org/dllearner/test/junit/HeuristicTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test.junit; import static org.junit.Assert.assertEquals; @@ -31,10 +30,7 @@ import org.dllearner.core.AbstractReasonerComponent; import org.dllearner.core.ComponentInitException; import org.dllearner.kb.OWLAPIOntology; -import org.dllearner.learningproblems.ClassLearningProblem; -import org.dllearner.learningproblems.Heuristics; -import org.dllearner.learningproblems.Heuristics.HeuristicType; -import org.dllearner.learningproblems.PosNegLPStandard; +import org.dllearner.learningproblems.*; import org.dllearner.reasoning.OWLAPIReasoner; import org.junit.Test; import org.semanticweb.owlapi.apibinding.OWLManager; @@ -54,7 +50,7 @@ * @author Jens Lehmann * */ -public class HeuristicTests { +public class HeuristicTest { // when comparing heuristic values, this is the maximum allowed difference between actual and returned value // (there can always be precision errors, so cannot assume that actual and returned values are exactly equal) @@ -67,7 +63,8 @@ public void classLearningTests() throws ComponentInitException, MalformedURLExce OWLDataFactory df = man.getOWLDataFactory(); OWLOntology kb = man.createOntology(); String ns = "http://dl-learner.org/junit/"; - PrefixManager pm = new DefaultPrefixManager(ns); + PrefixManager pm = new DefaultPrefixManager(); + pm.setDefaultPrefix(ns); OWLClass[] nc = new OWLClass[5]; for(int i=0; i<5; i++) { nc[i] = df.getOWLClass("A" + i, pm); @@ -82,7 +79,7 @@ public void classLearningTests() throws ComponentInitException, MalformedURLExce man.addAxiom(kb, df.getOWLClassAssertionAxiom(df.getOWLThing(),ind[i])); } - // A0 has 20 instances (i0 to i19) + // A0 has 20 instances (i0 to i19) for(int i=0; i<20; i++) { man.addAxiom(kb, df.getOWLClassAssertionAxiom(nc[0],ind[i])); } @@ -95,7 +92,7 @@ public void classLearningTests() throws ComponentInitException, MalformedURLExce // A2 has 40 instances (i10 to i49) for(int i=10; i<50; i++) { man.addAxiom(kb, df.getOWLClassAssertionAxiom(nc[2],ind[i])); - } + } // A3 has 5 instances (i8 to i12) for(int i=8; i<13; i++) { @@ -112,21 +109,21 @@ public void classLearningTests() throws ComponentInitException, MalformedURLExce //// equivalent classes, no noise, no approximations //// // evaluate A2 wrt. A1 using Jaccard - HeuristicTests.configureClassLP(problem, nc[0], HeuristicType.JACCARD); + HeuristicTest.configureClassLP(problem, nc[0], new AccMethodJaccard(true)); // the value should be 10 (i10-i19) divided by 30 (i0-i29) assertEqualsClassLP(problem, nc[1], 1/(double)3); assertEqualsClassLP(problem, nc[2], 1/(double)5); - HeuristicTests.configureClassLP(problem, nc[0], HeuristicType.PRED_ACC); + HeuristicTest.configureClassLP(problem, nc[0], new AccMethodPredAcc(true)); // the value should be the sum of 10 (correct positives) and 970 (correct negatives) divided by 1000 assertEqualsClassLP(problem, nc[1], (10+70)/(double)100); assertEqualsClassLP(problem, nc[2], (10+50)/(double)100); - HeuristicTests.configureClassLP(problem, nc[0], HeuristicType.AMEASURE); + HeuristicTest.configureClassLP(problem, nc[0], new AccMethodAMeasure(true)); assertEqualsClassLP(problem, nc[1], 0.5); assertEqualsClassLP(problem, nc[2], 0.375); - HeuristicTests.configureClassLP(problem, nc[0], HeuristicType.FMEASURE); + HeuristicTest.configureClassLP(problem, nc[0], new AccMethodFMeasure(true)); // recall = precision = F1-score = 0.5 assertEqualsClassLP(problem, nc[1], 0.5); // recall = 0.5, precision = 0.25, F1-score = 0.33... @@ -138,30 +135,30 @@ public void classLearningTests() throws ComponentInitException, MalformedURLExce // beta = 3.0 // Jaccard - HeuristicTests.configureClassLP(problem, nc[0], HeuristicType.JACCARD, false, false, 0.05); + HeuristicTest.configureClassLP(problem, nc[0], new AccMethodJaccard(true), false, 0.05); // the value should be 10 (i10-i19) divided by 30 (i0-i29) assertEqualsClassLP(problem, nc[1], 1/(double)3); - assertEqualsClassLP(problem, nc[2], 1/(double)5); + assertEqualsClassLP(problem, nc[2], 1/(double)5); - HeuristicTests.configureClassLP(problem, nc[0], HeuristicType.PRED_ACC, false, false, 0.05); + HeuristicTest.configureClassLP(problem, nc[0], new AccMethodPredAcc(true), false, 0.05); assertEqualsClassLP(problem, nc[1], 5/(double)7); assertEqualsClassLP(problem, nc[2], 4/(double)7); - HeuristicTests.configureClassLP(problem, nc[0], HeuristicType.AMEASURE, false, false, 0.05); + HeuristicTest.configureClassLP(problem, nc[0], new AccMethodAMeasure(true), false, 0.05); assertEqualsClassLP(problem, nc[1], 0.5); - assertEqualsClassLP(problem, nc[2], 0.4375); + assertEqualsClassLP(problem, nc[2], 0.4375); - HeuristicTests.configureClassLP(problem, nc[0], HeuristicType.FMEASURE, false, false, 0.05); + HeuristicTest.configureClassLP(problem, nc[0], new AccMethodFMeasure(true), false, 0.05); // recall = precision = F1-score = 0.5 assertEqualsClassLP(problem, nc[1], 0.5); // recall = 0.5, precision = 0.25, F_beta-score = 5/11... - assertEqualsClassLP(problem, nc[2], 5d/11); + assertEqualsClassLP(problem, nc[2], 5d/11); // TODO: generalised F-Measure //// noise tests //// - HeuristicTests.configureClassLP(problem, nc[0], HeuristicType.FMEASURE, false, true, 0.05); + HeuristicTest.configureClassLP(problem, nc[0], new AccMethodFMeasureApprox(true, reasoner), false, 0.05); assertEquals(problem.getAccuracyOrTooWeak(nc[3], 0.5),-1,delta); // TODO: test approximations @@ -177,7 +174,8 @@ public void posNegLPLearningTests() throws ComponentInitException, OWLOntologyCr OWLDataFactory df = man.getOWLDataFactory(); OWLOntology kb = man.createOntology(); String ns = "http://dl-learner.org/junit/"; - PrefixManager pm = new DefaultPrefixManager(ns); + PrefixManager pm = new DefaultPrefixManager(); + pm.setDefaultPrefix(ns); OWLClass[] nc = new OWLClass[5]; for(int i=0; i<5; i++) { nc[i] = df.getOWLClass("A" + i, pm); @@ -207,7 +205,7 @@ public void posNegLPLearningTests() throws ComponentInitException, OWLOntologyCr ks.init(); AbstractReasonerComponent reasoner = new OWLAPIReasoner(ks); - reasoner.init(); + reasoner.init(); PosNegLPStandard problem = new PosNegLPStandard(reasoner); @@ -216,7 +214,7 @@ public void posNegLPLearningTests() throws ComponentInitException, OWLOntologyCr OWLIndividual[] neg1 = new OWLIndividual[] {ind[5], ind[6], ind[7], ind[8], ind[9]}; // F-Measure and no approximations - HeuristicTests.configurePosNegStandardLP(problem, pos1, neg1, "fmeasure", false); + HeuristicTest.configurePosNegStandardLP(problem, pos1, neg1, new AccMethodFMeasure(true)); assertEqualsPosNegLPStandard(problem, nc[0], 0.5); // precision 2/3, recall 2/5 assertEqualsPosNegLPStandard(problem, nc[1], 2/3d); // precision 3/4, recall 3/5 @@ -224,7 +222,10 @@ public void posNegLPLearningTests() throws ComponentInitException, OWLOntologyCr // System.out.println(problem.getFMeasureOrTooWeakExact(nc[1], 1)); // F-Measure and approximations - HeuristicTests.configurePosNegStandardLP(problem, pos1, neg1, "fmeasure", true); + AccMethodFMeasureApprox accMethodFMeasureApprox = new AccMethodFMeasureApprox(); + accMethodFMeasureApprox.setReasoner(reasoner); + accMethodFMeasureApprox.init(); + HeuristicTest.configurePosNegStandardLP(problem, pos1, neg1, accMethodFMeasureApprox); assertEqualsPosNegLPStandard(problem, nc[0], 0.5); // precision 2/3, recall 2/5 assertEqualsPosNegLPStandard(problem, nc[1], 2/3d); // precision 3/4, recall 3/5 @@ -267,52 +268,49 @@ public void approximationTests() { // the class learning problem provides several ways to get the accuracy of a description, this method // tests all of those private static void assertEqualsClassLP(ClassLearningProblem problem, OWLClassExpression description, double accuracy) { - assertEquals(accuracy, problem.getAccuracy(description, 1.0), delta); assertEquals(accuracy, problem.getAccuracyOrTooWeak(description, 1.0), delta); assertEquals(accuracy, problem.computeScore(description, 1.0).getAccuracy(), delta); assertEquals(accuracy, problem.evaluate(description, 1.0).getAccuracy(), delta); } private static void assertEqualsPosNegLPStandard(PosNegLPStandard problem, OWLClassExpression description, double accuracy) { - assertEquals(accuracy, problem.getAccuracy(description,1.0), delta); assertEquals(accuracy, problem.getAccuracyOrTooWeak(description, 1.0), delta); assertEquals(accuracy, problem.computeScore(description, 1.0).getAccuracy(), delta); assertEquals(accuracy, problem.evaluate(description, 1.0).getAccuracy(), delta); } // convencience method to set the learning problem to a desired configuration (approximations disabled) - private static void configureClassLP(ClassLearningProblem problem, OWLClass classToDescribe, HeuristicType accuracyMethod) throws ComponentInitException { + private static void configureClassLP(ClassLearningProblem problem, OWLClass classToDescribe, AccMethodTwoValued accuracyMethod) throws ComponentInitException { problem.setClassToDescribe(classToDescribe); - problem.setHeuristic(accuracyMethod); - problem.setUseApproximations(false); - problem.init(); + problem.setAccuracyMethod(accuracyMethod); + problem.init(); } // convencience method to set the learning problem to a desired configuration - private static void configureClassLP(ClassLearningProblem problem, OWLClass classToDescribe, HeuristicType accuracyMethod, boolean equivalenceLearning, boolean useApproximations, double approxAccuracy) throws ComponentInitException { + private static void configureClassLP(ClassLearningProblem problem, OWLClass classToDescribe, AccMethodTwoValued accuracyMethod, boolean equivalenceLearning, double approxAccuracy) throws ComponentInitException { problem.setClassToDescribe(classToDescribe); // problem.getConfigurator().setType("superClass"); problem.setEquivalence(equivalenceLearning); - problem.setHeuristic(accuracyMethod); - problem.setUseApproximations(useApproximations); - problem.setApproxDelta(approxAccuracy); - problem.init(); + problem.setAccuracyMethod(accuracyMethod); + if (accuracyMethod instanceof AccMethodApproximate) { + ((AccMethodApproximate) accuracyMethod).setApproxDelta(approxAccuracy); + } + problem.init(); } // @SuppressWarnings("unchecked") - private static void configurePosNegStandardLP(PosNegLPStandard problem, OWLIndividual[] positiveExamples, OWLIndividual[] negativeExamples, String accuracyMethod, boolean useApproximations) throws ComponentInitException { + private static void configurePosNegStandardLP(PosNegLPStandard problem, OWLIndividual[] positiveExamples, OWLIndividual[] negativeExamples, AccMethodTwoValued accuracyMethod) throws ComponentInitException { Set s1 = new TreeSet(Arrays.asList(positiveExamples)); Set s2 = new TreeSet(Arrays.asList(negativeExamples)); - HeuristicTests.configurePosNegStandardLP(problem, s1, s2, accuracyMethod, useApproximations); + HeuristicTest.configurePosNegStandardLP(problem, s1, s2, accuracyMethod); } // convencience method to set the learning problem to a desired configuration (approximations disabled) - private static void configurePosNegStandardLP(PosNegLPStandard problem, Set positiveExamples, Set negativeExamples, String accuracyMethod, boolean useApproximations) throws ComponentInitException { + private static void configurePosNegStandardLP(PosNegLPStandard problem, Set positiveExamples, Set negativeExamples, AccMethodTwoValued accuracyMethod) throws ComponentInitException { problem.setPositiveExamples(positiveExamples); problem.setNegativeExamples(negativeExamples); problem.setAccuracyMethod(accuracyMethod); - problem.setUseApproximations(useApproximations); - problem.init(); - } + problem.init(); + } } diff --git a/components-core/src/test/java/org/dllearner/test/junit/LearningProblemTest.java b/components-core/src/test/java/org/dllearner/test/junit/LearningProblemTest.java index 413de909bf..69d1aee571 100644 --- a/components-core/src/test/java/org/dllearner/test/junit/LearningProblemTest.java +++ b/components-core/src/test/java/org/dllearner/test/junit/LearningProblemTest.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.test.junit; @@ -40,7 +55,8 @@ public void posOnlyLPLearningTests() throws ComponentInitException, OWLOntologyC OWLDataFactory df = man.getOWLDataFactory(); OWLOntology kb = man.createOntology(); String ns = "http://dl-learner.org/junit/"; - PrefixManager pm = new DefaultPrefixManager(ns); + PrefixManager pm = new DefaultPrefixManager(); + pm.setDefaultPrefix(ns); OWLClass[] nc = new OWLClass[5]; for(int i=0; i<5; i++) { nc[i] = df.getOWLClass("A" + i, pm); diff --git a/components-core/src/test/java/org/dllearner/test/junit/LiteralLearningTest.java b/components-core/src/test/java/org/dllearner/test/junit/LiteralLearningTest.java index 9efd8e233e..7d2e9042b9 100644 --- a/components-core/src/test/java/org/dllearner/test/junit/LiteralLearningTest.java +++ b/components-core/src/test/java/org/dllearner/test/junit/LiteralLearningTest.java @@ -1,12 +1,27 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.test.junit; -import static org.junit.Assert.assertTrue; - -import java.io.File; -import java.util.Set; -import java.util.TreeSet; - +import com.clarkparsia.owlapiv3.XSD; +import com.google.common.collect.Sets; import org.apache.log4j.Level; +import org.apache.log4j.Logger; import org.dllearner.algorithms.celoe.CELOE; import org.dllearner.core.AbstractKnowledgeSource; import org.dllearner.core.AbstractReasonerComponent; @@ -16,26 +31,20 @@ import org.dllearner.reasoning.ClosedWorldReasoner; import org.dllearner.reasoning.OWLAPIReasoner; import org.dllearner.refinementoperators.RhoDRDown; -import org.dllearner.utilities.owl.DLSyntaxObjectRenderer; import org.junit.Test; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.io.ToStringRenderer; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLDatatype; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyCreationException; -import org.semanticweb.owlapi.model.PrefixManager; +import org.semanticweb.owlapi.model.*; import org.semanticweb.owlapi.util.DefaultPrefixManager; +import org.semanticweb.owlapi.vocab.OWL2Datatype; import org.semanticweb.owlapi.vocab.OWLFacet; - import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; import uk.ac.manchester.cs.owl.owlapi.OWLLiteralImplInteger; -import com.clarkparsia.owlapiv3.XSD; -import com.google.common.collect.Sets; +import java.io.File; +import java.util.Set; +import java.util.TreeSet; + +import static org.junit.Assert.assertTrue; public final class LiteralLearningTest { static final String NUMBERS = "http://dl-learner.org/test/numbers#"; @@ -69,16 +78,14 @@ private class TestRunner { this.prefix = prefix; this.restrictionType = restrictionType; this.maxNrOfSplits = maxNrOfSplits; - org.apache.log4j.Logger.getLogger("org.dllearner").setLevel(Level.DEBUG); -// org.apache.log4j.Logger.getLogger(CELOE.class).setLevel(Level.DEBUG); - -// ToStringRenderer.getInstance().setRenderer(new ManchesterOWLSyntaxOWLObjectRendererImpl()); - ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); +// StringRenderer.setRenderer(Rendering.MANCHESTER_SYNTAX); +// StringRenderer.setRenderer(Rendering.DL_SYNTAX); File file = new File(owlfile); OWLOntology ontology = OWLManager.createOWLOntologyManager().loadOntologyFromOntologyDocument(file); df = new OWLDataFactoryImpl(); - pm = new DefaultPrefixManager(prefix); + pm = new DefaultPrefixManager(); + pm.setDefaultPrefix(prefix); ks = new OWLAPIOntology(ontology); ks.init(); } @@ -86,40 +93,51 @@ private class TestRunner { this(prefix, owlfile, restrictionType, 12); } public void run() throws ComponentInitException { - Set positiveExamples = new TreeSet(); - positiveExamples.add(df.getOWLNamedIndividual("N1", pm)); - positiveExamples.add(df.getOWLNamedIndividual("N2", pm)); - positiveExamples.add(df.getOWLNamedIndividual("N3", pm)); - - Set negativeExamples = new TreeSet(); - negativeExamples.add(df.getOWLNamedIndividual("N100", pm)); - negativeExamples.add(df.getOWLNamedIndividual("N102", pm)); - negativeExamples.add(df.getOWLNamedIndividual("N104", pm)); - - for(AbstractReasonerComponent rc : rcs) { - PosNegLPStandard lp = new PosNegLPStandard(rc); - lp.setPositiveExamples(positiveExamples); - lp.setNegativeExamples(negativeExamples); - lp.init(); - - RhoDRDown op = new RhoDRDown(); - op.setUseTimeDatatypes(true); - op.setUseNumericDatatypes(true); - op.setReasoner(rc); - op.setMaxNrOfSplits(maxNrOfSplits); - op.init(); - - CELOE alg = new CELOE(lp, rc); - alg.setMaxClassDescriptionTests(1000); - alg.setMaxExecutionTimeInSeconds(0); - alg.setOperator(op); - alg.init(); - - alg.start(); - OWLClassExpression soln = alg.getCurrentlyBestDescription(); - - assertTrue(soln.getNNF().equals(target)); - + Level oldLevel = Logger.getLogger("org.dllearner").getLevel(); + //Level oldLevelCELOE = Logger.getLogger(CELOE.class).getLevel(); + try { + Logger.getLogger("org.dllearner").setLevel(Level.DEBUG); + //Logger.getLogger(CELOE.class).setLevel(Level.DEBUG); + + + Set positiveExamples = new TreeSet(); + positiveExamples.add(df.getOWLNamedIndividual("N1", pm)); + positiveExamples.add(df.getOWLNamedIndividual("N2", pm)); + positiveExamples.add(df.getOWLNamedIndividual("N3", pm)); + + Set negativeExamples = new TreeSet(); + negativeExamples.add(df.getOWLNamedIndividual("N100", pm)); + negativeExamples.add(df.getOWLNamedIndividual("N102", pm)); + negativeExamples.add(df.getOWLNamedIndividual("N104", pm)); + + for (AbstractReasonerComponent rc : rcs) { + PosNegLPStandard lp = new PosNegLPStandard(rc); + lp.setPositiveExamples(positiveExamples); + lp.setNegativeExamples(negativeExamples); + lp.init(); + + RhoDRDown op = new RhoDRDown(); + op.setUseTimeDatatypes(true); + op.setUseNumericDatatypes(true); + op.setReasoner(rc); + op.setMaxNrOfSplits(maxNrOfSplits); + op.init(); + + CELOE alg = new CELOE(lp, rc); + alg.setMaxClassExpressionTests(1000); + alg.setMaxExecutionTimeInSeconds(0); + alg.setOperator(op); + alg.init(); + + alg.start(); + OWLClassExpression soln = alg.getCurrentlyBestDescription(); + + assertTrue(soln.getNNF().equals(target)); + + } + } finally { + Logger.getLogger("org.dllearner").setLevel(oldLevel); + //Logger.getLogger(CELOE.class).setLevel(oldLevelCELOE); } } public void setSingleRestrictionTarget(OWLFacet facetType, String solution) { @@ -234,6 +252,59 @@ public void gMonthTypeTest () throws OWLOntologyCreationException, ComponentInit runner.run(); } + + @Test + public void stringTypeTest () throws OWLOntologyCreationException, ComponentInitException { + OWLOntologyManager man = OWLManager.createOWLOntologyManager(); + OWLDataFactory df = man.getOWLDataFactory(); + OWLOntology ontology = man.createOntology(); + PrefixManager pm = new DefaultPrefixManager(); + pm.setDefaultPrefix("http://dl-learner.org/test/strings/"); + OWLDataProperty dp = df.getOWLDataProperty("stringValue", pm); + OWLClass cls = df.getOWLClass("A", pm); + man.addAxiom(ontology, df.getOWLDataPropertyDomainAxiom(dp, cls)); + man.addAxiom(ontology, df.getOWLDataPropertyRangeAxiom(dp, OWL2Datatype.XSD_STRING.getDatatype(df))); + + Set positiveExamples = new TreeSet<>(); + for (int i = 0; i < 10; i++) { + OWLNamedIndividual ind = df.getOWLNamedIndividual("p" + i, pm); + positiveExamples.add(ind); + man.addAxiom(ontology, df.getOWLDataPropertyAssertionAxiom(dp, ind, "X")); + man.addAxiom(ontology, df.getOWLClassAssertionAxiom(cls, ind)); + } + + Set negativeExamples = new TreeSet<>(); + for (int i = 0; i < 10; i++) { + OWLNamedIndividual ind = df.getOWLNamedIndividual("n" + i, pm); + negativeExamples.add(ind); + man.addAxiom(ontology, df.getOWLDataPropertyAssertionAxiom(dp, ind, "Y")); + man.addAxiom(ontology, df.getOWLClassAssertionAxiom(cls, ind)); + } + + OWLAPIOntology ks = new OWLAPIOntology(ontology); + ks.init(); + + ClosedWorldReasoner reasoner = new ClosedWorldReasoner(ks); + reasoner.init(); + + PosNegLPStandard lp = new PosNegLPStandard(reasoner); + lp.setPositiveExamples(positiveExamples); + lp.setNegativeExamples(negativeExamples); + lp.init(); + + RhoDRDown op = new RhoDRDown(); + op.setUseDataHasValueConstructor(true); + op.setReasoner(reasoner); + op.init(); + + CELOE alg = new CELOE(lp, reasoner); + alg.setMaxClassExpressionTests(1000); + alg.setMaxExecutionTimeInSeconds(0); + alg.setOperator(op); + alg.init(); + + alg.start(); + } @Test public void literalComparisonTest () { diff --git a/components-core/src/test/java/org/dllearner/test/junit/ParserTests.java b/components-core/src/test/java/org/dllearner/test/junit/ParserTest.java similarity index 95% rename from components-core/src/test/java/org/dllearner/test/junit/ParserTests.java rename to components-core/src/test/java/org/dllearner/test/junit/ParserTest.java index ad78f8c7f9..90c04fb30e 100644 --- a/components-core/src/test/java/org/dllearner/test/junit/ParserTests.java +++ b/components-core/src/test/java/org/dllearner/test/junit/ParserTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test.junit; import org.dllearner.parser.KBParser; @@ -31,7 +30,7 @@ * @author Jens Lehmann. * */ -public class ParserTests { +public class ParserTest { @Test public void KBParserTest() throws ParseException { diff --git a/components-core/src/test/java/org/dllearner/test/junit/PropertyAxiomLearningTest.java b/components-core/src/test/java/org/dllearner/test/junit/PropertyAxiomLearningTest.java index 877bd2449d..a894c18aa2 100644 --- a/components-core/src/test/java/org/dllearner/test/junit/PropertyAxiomLearningTest.java +++ b/components-core/src/test/java/org/dllearner/test/junit/PropertyAxiomLearningTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test.junit; import java.lang.reflect.Constructor; @@ -84,7 +83,10 @@ public class PropertyAxiomLearningTest { private static final String NS = "http://dllearner.org/test/"; - PrefixManager pm = new DefaultPrefixManager(NS); + static PrefixManager pm = new DefaultPrefixManager(); + static { + pm.setDefaultPrefix(NS); + } OWLDataFactory df = new OWLDataFactoryImpl(); private OWLObjectProperty op1 = df.getOWLObjectProperty("op1", pm); @@ -100,8 +102,7 @@ public class PropertyAxiomLearningTest { private OWLDataProperty disDataProperty = df.getOWLDataProperty(IRI.create( "http://dbpedia.org/ontology/height")); private OWLDataProperty equivDataProperty = df.getOWLDataProperty(IRI.create( "http://dbpedia.org/ontology/height")); - - + @BeforeClass public static void setUp() throws Exception { // ks = new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpedia()); diff --git a/components-core/src/test/java/org/dllearner/test/junit/ReasonerTest.java b/components-core/src/test/java/org/dllearner/test/junit/ReasonerTest.java index 418cbc33da..f84539788d 100644 --- a/components-core/src/test/java/org/dllearner/test/junit/ReasonerTest.java +++ b/components-core/src/test/java/org/dllearner/test/junit/ReasonerTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test.junit; import static org.junit.Assert.assertFalse; diff --git a/components-core/src/test/java/org/dllearner/test/junit/RefinementOperatorTests.java b/components-core/src/test/java/org/dllearner/test/junit/RefinementOperatorTest.java similarity index 70% rename from components-core/src/test/java/org/dllearner/test/junit/RefinementOperatorTests.java rename to components-core/src/test/java/org/dllearner/test/junit/RefinementOperatorTest.java index 3f98e9da1e..df2cd595a0 100644 --- a/components-core/src/test/java/org/dllearner/test/junit/RefinementOperatorTests.java +++ b/components-core/src/test/java/org/dllearner/test/junit/RefinementOperatorTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,24 +16,13 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test.junit; -import static org.junit.Assert.assertTrue; - -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; -import java.util.TreeSet; - import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.dllearner.algorithms.ocel.OCEL; -import org.dllearner.core.AbstractClassExpressionLearningProblem; -import org.dllearner.core.AbstractReasonerComponent; -import org.dllearner.core.ComponentInitException; -import org.dllearner.core.KnowledgeSource; -import org.dllearner.core.LearningProblemUnsupportedException; +import org.dllearner.core.*; +import org.dllearner.core.StringRenderer.Rendering; import org.dllearner.core.owl.ClassHierarchy; import org.dllearner.kb.OWLFile; import org.dllearner.learningproblems.PosNegLPStandard; @@ -45,14 +34,20 @@ import org.dllearner.refinementoperators.RhoDRDown; import org.dllearner.test.junit.TestOntologies.TestOntology; import org.dllearner.utilities.Helper; +import org.dllearner.utilities.owl.OWLClassExpressionLengthMetric; +import org.dllearner.utilities.owl.OWLClassExpressionUtils; import org.junit.Test; -import org.semanticweb.owlapi.io.ToStringRenderer; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLClass; import org.semanticweb.owlapi.model.OWLClassExpression; - import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl; -import uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntaxObjectRenderer; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +import java.util.TreeSet; + +import static org.junit.Assert.assertTrue; /** * A suite of JUnit tests related to refinement operators. @@ -60,7 +55,7 @@ * @author Jens Lehmann * */ -public class RefinementOperatorTests { +public class RefinementOperatorTest { private String baseURI; @@ -72,6 +67,7 @@ public class RefinementOperatorTests { @Test public void rhoDRDownTest() { try { + StringRenderer.setRenderer(Rendering.DL_SYNTAX); String file = "../examples/carcinogenesis/carcinogenesis.owl"; KnowledgeSource ks = new OWLFile(file); AbstractReasonerComponent reasoner = new OWLAPIReasoner(Collections.singleton(ks)); @@ -88,6 +84,9 @@ public void rhoDRDownTest() { op.setSubHierarchy(reasoner.getClassHierarchy()); op.setObjectPropertyHierarchy(reasoner.getObjectPropertyHierarchy()); op.setDataPropertyHierarchy(reasoner.getDatatypePropertyHierarchy()); + OWLClassExpressionLengthMetric metric = new OWLClassExpressionLengthMetric(); + metric.dataHasValueLength = 1; + op.setLengthMetric(metric); op.init(); OWLClassExpression concept = KBParser.parseConcept(uri("Compound")); Set results = op.refine(concept, 4, null); @@ -107,10 +106,10 @@ public void rhoDRDownTest() { e.printStackTrace(); } } - + @Test public void rhoDRDownTest2() throws ParseException, ComponentInitException { - ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); + StringRenderer.setRenderer(Rendering.DL_SYNTAX); AbstractReasonerComponent reasoner = TestOntologies.getTestOntology(TestOntology.EPC_OE); reasoner.init(); baseURI = reasoner.getBaseURI(); @@ -239,6 +238,86 @@ public void rhoDRDownTest5() throws ParseException, LearningProblemUnsupportedEx } } + + /** + * Applies the RhoDRDown operator to a concept and checks that the number of + * refinements does not exceed the requested length. + * + */ + @Test + public void rhoDRDownTest6() { + try { + String file = "../examples/carcinogenesis/carcinogenesis.owl"; + KnowledgeSource ks = new OWLFile(file); + AbstractReasonerComponent reasoner = new OWLAPIReasoner(Collections.singleton(ks)); + reasoner.init(); + baseURI = reasoner.getBaseURI(); + + RhoDRDown op = new RhoDRDown(); + op.setReasoner(reasoner); + op.setSubHierarchy(reasoner.getClassHierarchy()); + op.setObjectPropertyHierarchy(reasoner.getObjectPropertyHierarchy()); + op.setDataPropertyHierarchy(reasoner.getDatatypePropertyHierarchy()); + op.init(); + OWLClassExpression concept = KBParser.parseConcept(uri("Compound")); + int maxLength = 4; + Set results = op.refine(concept, maxLength, null); + + int tooLong = 0; + for(OWLClassExpression result : results) { + if (OWLClassExpressionUtils.getLength(result) > maxLength) { + tooLong++; + } + } + + if(tooLong!= 0) { + System.out.println(tooLong + " refinements were longer than " + maxLength); + } + assertTrue(tooLong==0); + } catch(ComponentInitException e) { + e.printStackTrace(); + } catch (ParseException e) { + e.printStackTrace(); + } + } + + @Test + public void rhoDRDownTest7() { + try { + StringRenderer.setRenderer(Rendering.DL_SYNTAX); + String file = "../examples/family/father_oe_inv.ttl"; + KnowledgeSource ks = new OWLFile(file); + AbstractReasonerComponent reasoner = new OWLAPIReasoner(Collections.singleton(ks)); + reasoner.init(); + baseURI = reasoner.getBaseURI(); + + RhoDRDown op = new RhoDRDown(); + op.setReasoner(reasoner); + op.setUseInverse(true); + op.setUseHasValueConstructor(true); + op.setSubHierarchy(reasoner.getClassHierarchy()); + op.setObjectPropertyHierarchy(reasoner.getObjectPropertyHierarchy()); + op.setDataPropertyHierarchy(reasoner.getDatatypePropertyHierarchy()); + op.init(); + OWLClassExpression concept = KBParser.parseConcept(uri("person")); + Set results = op.refine(concept, 5, null); + + for(OWLClassExpression result : results) { + System.out.println(result); + } + + int desiredResultSize = 11; + if(results.size() != desiredResultSize) { + System.out.println(results.size() + " results found, but should be " + desiredResultSize + "."); + } + assertTrue(results.size()==desiredResultSize); + } catch(ComponentInitException e) { + e.printStackTrace(); + } catch (ParseException e) { + e.printStackTrace(); + } + } + @Test public void invertedOperatorTest() throws ParseException, ComponentInitException { AbstractReasonerComponent reasoner = TestOntologies.getTestOntology(TestOntology.RHO1); @@ -265,38 +344,43 @@ public void invertedOperatorTest() throws ParseException, ComponentInitException @Test public void rhoDownTestPellet() throws ComponentInitException { - ToStringRenderer.getInstance().setRenderer(new org.dllearner.utilities.owl.DLSyntaxObjectRenderer()); - Logger.getRootLogger().setLevel(Level.TRACE); - AbstractReasonerComponent reasoner = TestOntologies.getTestOntology(TestOntology.FATHER); - reasoner.init(); - - RhoDRDown op = new RhoDRDown(); - op.setReasoner(reasoner); - op.setUseSomeOnly(false); - op.setSubHierarchy(reasoner.getClassHierarchy()); - op.setObjectPropertyHierarchy(reasoner.getObjectPropertyHierarchy()); - op.setDataPropertyHierarchy(reasoner.getDatatypePropertyHierarchy()); - op.setUseSomeOnly(false); - op.init(); - - OWLClass nc = new OWLClassImpl(IRI.create("http://example.com/father#male")); - Set refinements = op.refine(nc, 5); - for(OWLClassExpression refinement : refinements) { - System.out.println(refinement); - } - // refinements should be as follows: - // male ⊓ male - // male ⊓ (male ⊔ male) - // male ⊓ (female ⊔ male) - // male ⊓ (female ⊔ female) - // male ⊓ (¬male) - // male ⊓ (¬female) - // male ⊓ (∃ hasChild.⊤) - // male ⊓ (∀ hasChild.⊤) + StringRenderer.setRenderer(Rendering.DL_SYNTAX); + Level oldLevel = Logger.getRootLogger().getLevel(); + try { + Logger.getRootLogger().setLevel(Level.TRACE); + AbstractReasonerComponent reasoner = TestOntologies.getTestOntology(TestOntology.FATHER); + reasoner.init(); + + RhoDRDown op = new RhoDRDown(); + op.setReasoner(reasoner); + op.setUseSomeOnly(false); + op.setSubHierarchy(reasoner.getClassHierarchy()); + op.setObjectPropertyHierarchy(reasoner.getObjectPropertyHierarchy()); + op.setDataPropertyHierarchy(reasoner.getDatatypePropertyHierarchy()); + op.setUseSomeOnly(false); + op.init(); + + OWLClass nc = new OWLClassImpl(IRI.create("http://example.com/father#male")); + Set refinements = op.refine(nc, 5); + for (OWLClassExpression refinement : refinements) { + System.out.println(refinement); + } + // refinements should be as follows: + // male ⊓ male + // male ⊓ (male ⊔ male) + // male ⊓ (female ⊔ male) + // male ⊓ (female ⊔ female) + // male ⊓ (¬male) + // male ⊓ (¬female) + // male ⊓ (∃ hasChild.⊤) + // male ⊓ (∀ hasChild.⊤) // System.out.println(rs); // System.out.println("most general properties: " + rs.getMostGeneralProperties()); - System.out.println(reasoner.getObjectPropertyHierarchy()); - assertTrue(refinements.size() + " results found, but should be " + 8 + ".", refinements.size()==8); + System.out.println(reasoner.getObjectPropertyHierarchy()); + assertTrue(refinements.size() + " results found, but should be " + 8 + ".", refinements.size() == 8); + } finally { + Logger.getRootLogger().setLevel(oldLevel); + } } private String uri(String name) { diff --git a/components-core/src/test/java/org/dllearner/test/junit/SimulationTests.java b/components-core/src/test/java/org/dllearner/test/junit/SimulationTest.java similarity index 99% rename from components-core/src/test/java/org/dllearner/test/junit/SimulationTests.java rename to components-core/src/test/java/org/dllearner/test/junit/SimulationTest.java index d0b84c5c25..fc5163251e 100644 --- a/components-core/src/test/java/org/dllearner/test/junit/SimulationTests.java +++ b/components-core/src/test/java/org/dllearner/test/junit/SimulationTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test.junit; import static org.junit.Assert.assertTrue; @@ -47,7 +46,7 @@ * @author Jens Lehmann * */ -public class SimulationTests { +public class SimulationTest { /** * Empty tree - empty simulation. diff --git a/components-core/src/test/java/org/dllearner/test/junit/SomeOnlyReasonerTest.java b/components-core/src/test/java/org/dllearner/test/junit/SomeOnlyReasonerTest.java index b657b4f6e0..6940a0f2cd 100644 --- a/components-core/src/test/java/org/dllearner/test/junit/SomeOnlyReasonerTest.java +++ b/components-core/src/test/java/org/dllearner/test/junit/SomeOnlyReasonerTest.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.test.junit; @@ -12,10 +27,11 @@ import org.dllearner.core.ComponentInitException; import org.dllearner.core.LearningProblemUnsupportedException; import org.dllearner.kb.sparql.simple.SparqlSimpleExtractor; +import org.dllearner.learningproblems.AccMethodFMeasure; import org.dllearner.learningproblems.PosNegLPStandard; import org.dllearner.reasoning.ClosedWorldReasoner; import org.dllearner.refinementoperators.RhoDRDown; -import org.dllearner.utilities.datastructures.Datastructures; +import org.dllearner.utilities.Helper; import org.dllearner.utilities.datastructures.SortedSetTuple; import org.junit.Ignore; import org.semanticweb.owlapi.model.ClassExpressionType; @@ -49,7 +65,7 @@ public void someOnlyTest() throws ComponentInitException, LearningProblemUnsuppo negExamples); SparqlSimpleExtractor ks = new SparqlSimpleExtractor(); - ks.setInstances(new ArrayList(Datastructures.individualSetToStringSet(examples + ks.setInstances(new ArrayList(Helper.getStringSet(examples .getCompleteSet()))); // ks.getConfigurator().setPredefinedEndpoint("DBPEDIA"); // TODO: // probably the official endpoint is too slow? @@ -75,8 +91,7 @@ public void someOnlyTest() throws ComponentInitException, LearningProblemUnsuppo PosNegLPStandard lp = new PosNegLPStandard(rc); lp.setPositiveExamples(posExamples); lp.setNegativeExamples(negExamples); - lp.setAccuracyMethod("fmeasure"); - lp.setUseApproximations(false); + lp.setAccuracyMethod(new AccMethodFMeasure(true)); lp.init(); CELOE la = new CELOE(lp, rc); diff --git a/components-core/src/test/java/org/dllearner/test/junit/TestOntologies.java b/components-core/src/test/java/org/dllearner/test/junit/TestOntologies.java index 1c83bd2ea3..49c7dc9715 100644 --- a/components-core/src/test/java/org/dllearner/test/junit/TestOntologies.java +++ b/components-core/src/test/java/org/dllearner/test/junit/TestOntologies.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test.junit; import java.util.Collections; diff --git a/components-core/src/test/java/org/dllearner/test/junit/UtilitiesTests.java b/components-core/src/test/java/org/dllearner/test/junit/UtilitiesTest.java similarity index 95% rename from components-core/src/test/java/org/dllearner/test/junit/UtilitiesTests.java rename to components-core/src/test/java/org/dllearner/test/junit/UtilitiesTest.java index ba5bbb6fa8..cf7dbde9a9 100644 --- a/components-core/src/test/java/org/dllearner/test/junit/UtilitiesTests.java +++ b/components-core/src/test/java/org/dllearner/test/junit/UtilitiesTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2011, Jens Lehmann + * Copyright (C) 2007 - 2016, Jens Lehmann * * This file is part of DL-Learner. * @@ -16,7 +16,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package org.dllearner.test.junit; import java.util.Set; @@ -37,7 +36,7 @@ * @author Jens Lehmann * */ -public class UtilitiesTests { +public class UtilitiesTest { @Test public void entityDetection() { diff --git a/components-core/src/test/java/org/dllearner/utilities/owl/OWLClassExpressionTest.java b/components-core/src/test/java/org/dllearner/utilities/owl/OWLClassExpressionTest.java new file mode 100644 index 0000000000..ffa863d244 --- /dev/null +++ b/components-core/src/test/java/org/dllearner/utilities/owl/OWLClassExpressionTest.java @@ -0,0 +1,67 @@ +package org.dllearner.utilities.owl; + +import com.clarkparsia.owlapiv3.XSD; +import org.dllearner.core.StringRenderer; +import org.junit.Test; +import org.semanticweb.owlapi.model.*; +import org.semanticweb.owlapi.vocab.OWLFacet; +import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; +import uk.ac.manchester.cs.owl.owlapi.OWLDataPropertyImpl; + +import java.util.Collections; + +/** + * Created by Simon Bin on 16-1-27. + */ +public class OWLClassExpressionTest { + + @Test + public void OWLClassExpressionLength() { + StringRenderer.setRenderer(StringRenderer.Rendering.DL_SYNTAX); + OWLDataFactory df = new OWLDataFactoryImpl(); + OWLLiteral min = df.getOWLLiteral(10); + OWLLiteral max = df.getOWLLiteral(20); + + OWLDatatypeRestriction restriction1 = + df.getOWLDatatypeRestriction( + XSD.INTEGER, + Collections.singleton(df.getOWLFacetRestriction( + OWLFacet.MIN_INCLUSIVE, + min))); + OWLDataProperty dp = new OWLDataPropertyImpl(IRI.create("p1")); + OWLObjectProperty op = df.getOWLObjectProperty(IRI.create("op1")); + + OWLDatatypeRestriction restriction2 = + df.getOWLDatatypeRestriction( + XSD.INT, + Collections.singleton(df.getOWLFacetRestriction( + OWLFacet.MAX_INCLUSIVE, + max))); + OWLClass klass = df.getOWLClass(IRI.create("C")); + + OWLClassExpression[] ce = new OWLClassExpression[]{ + df.getOWLDataSomeValuesFrom(dp, restriction1), + df.getOWLDataSomeValuesFrom(dp, restriction2), + df.getOWLDataHasValue(dp, df.getOWLLiteral(true)), + df.getOWLObjectAllValuesFrom(op, df.getOWLThing()), + df.getOWLObjectSomeValuesFrom(op.getInverseProperty(), df.getOWLThing()), + df.getOWLObjectMaxCardinality(3, op, df.getOWLThing()), + df.getOWLObjectMinCardinality(3, op, df.getOWLThing()), + df.getOWLObjectAllValuesFrom(op, klass), + df.getOWLObjectAllValuesFrom(op, klass.getComplementNNF()), + }; + + //OWLClassExpressionLengthMetric metric = OWLClassExpressionLengthMetric.getDefaultMetric(); + OWLClassExpressionLengthMetric metric = OWLClassExpressionLengthMetric.getOCELMetric(); + //metric.objectCardinalityLength = 0; + metric.objectComplementLength = 0; + int[] lengths = new int[ce.length]; + for (int i = 0; i < ce.length; ++i) { + lengths[i] = new OWLClassExpressionLengthCalculator(metric).getLength(ce[i]); + } + + for (int i = 0; i < ce.length; ++i) { + System.err.println("length of " + ce[i] + " is " + lengths[i]); + } + } +} diff --git a/components-core/src/test/java/org/dllearner/utilities/owl/OntologyToByteConverterTest.java b/components-core/src/test/java/org/dllearner/utilities/owl/OntologyToByteConverterTest.java index 1f8f4f9d8e..2dfbdbcf8b 100644 --- a/components-core/src/test/java/org/dllearner/utilities/owl/OntologyToByteConverterTest.java +++ b/components-core/src/test/java/org/dllearner/utilities/owl/OntologyToByteConverterTest.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.utilities.owl; import static org.junit.Assert.assertEquals; diff --git a/components-core/src/test/java/org/dllearner/utilities/owl/SimpleOntologyToByteConverterTest.java b/components-core/src/test/java/org/dllearner/utilities/owl/SimpleOntologyToByteConverterTest.java index fa071e6a52..5772f23428 100644 --- a/components-core/src/test/java/org/dllearner/utilities/owl/SimpleOntologyToByteConverterTest.java +++ b/components-core/src/test/java/org/dllearner/utilities/owl/SimpleOntologyToByteConverterTest.java @@ -1,3 +1,21 @@ +/** + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.dllearner.utilities.owl; /** diff --git a/components-core/src/test/java/org/dllearner/utilities/split/DataValuesSplitterTest.java b/components-core/src/test/java/org/dllearner/utilities/split/DataValuesSplitterTest.java index 4693fad372..a6e4899dea 100644 --- a/components-core/src/test/java/org/dllearner/utilities/split/DataValuesSplitterTest.java +++ b/components-core/src/test/java/org/dllearner/utilities/split/DataValuesSplitterTest.java @@ -1,5 +1,20 @@ /** - * + * Copyright (C) 2007 - 2016, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ package org.dllearner.utilities.split; diff --git a/components-core/src/test/resources/log4j.properties b/components-core/src/test/resources/log4j.properties index b89219033b..39b940bdf1 100755 --- a/components-core/src/test/resources/log4j.properties +++ b/components-core/src/test/resources/log4j.properties @@ -1,20 +1,50 @@ -# Direct log messages to stdout -# Root logger option -log4j.rootLogger=INFO,stdout +# All loggers will default to the INFO level unless specified in a specific logger +# For more information, see the Log4J API manual at: http://bit.ly/KmvtWL . +log4j.rootLogger=INFO, stdout, file log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout -#log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %p [%c] %L - %m%n -log4j.appender.stdout.layout.ConversionPattern=%r [%C{1}] - %m%n -#File Appender -log4j.appender.FA=org.apache.log4j.FileAppender -log4j.appender.FA.File=REX.log -log4j.appender.FA.layout=org.apache.log4j.PatternLayout -log4j.appender.FA.layout.ConversionPattern=%d{ABSOLUTE} %p [%c] %L - %m%n +log4j.appender.file=org.apache.log4j.FileAppender +log4j.appender.file.File=log/interfaces.log +log4j.appender.file.layout=org.apache.log4j.PatternLayout +log4j.appender.file.layout.ConversionPattern= %-4r [%t] %-5p %c %x - %m%n +log4j.appender.file.Threshold=DEBUG + +# DL-Learner Logs +log4j.logger.org.dllearner=INFO +# Turn this to Debug if you wish to dump stack traces to the appenders (console, file) +# use INFO on release +log4j.logger.org.dllearner.cli=INFO +#log4j.logger.org.dllearner.cli=DEBUG + + +#log4j.logger.org.dllearner.server.nke.LogicalRelationStrategy=DEBUG +log4j.category.org.dllearner.kb.simple=DEBUG +#log4j.category.org.dllearner.reasoning.SPARQLReasoner=DEBUG +#log4j.category.org.dllearner.algorithms.celoe.CELOE=DEBUG +#log4j.category.org.dllearner.refinementoperators.RhoDRDown=DEBUG + +# Eliminate most of the Spring Context Loading noise +log4j.logger.org.springframework=WARN + +## log4j.logger.org.joseki.server.http.LocatorServletContext=ALL + +# Jena, including the Joseki client +#log4j.logger.com.hp.hpl.jena=WARN + + +# to disable spring stack trace (do this for release) +log4j.logger.org.springframework.context.support.ClassPathXmlApplicationContext=ERROR + +# hide useless owlapi warning "entityExpansionLimit not supported by parser" +log4j.logger.org.semanticweb.owlapi.util.SAXParsers=ERROR + + +## log4j.logger.com.hp.hpl.jena.util.FileManager=ALL +## log4j.logger.com.hp.hpl.jena.util.LocatorURL=ALL +## log4j.logger.com.hp.hpl.jena.util.LocatorClassLoader=ALL +## log4j.logger.com.hp.hpl.jena.util.LocatorFile=ALL +## log4j.logger.com.hp.hpl.jena.util.LocationMapper=ALL -log4j.category.org.dllearner.algorithms=INFO -log4j.category.org.dllearner.algorithms.isle=DEBUG -log4j.category.org.dllearner.algorithms.qtl=INFO -log4j.category.org.dllearner.algorithms.qtl.lgg=INFO diff --git a/components-core/src/test/resources/org/dllearner/algorithms/qtl/qtl-mail.properties.dist b/components-core/src/test/resources/org/dllearner/algorithms/qtl/qtl-mail.properties.dist new file mode 100644 index 0000000000..0e7d9c6f8b --- /dev/null +++ b/components-core/src/test/resources/org/dllearner/algorithms/qtl/qtl-mail.properties.dist @@ -0,0 +1,5 @@ +hostname = +from = +username = +password = +to = \ No newline at end of file diff --git a/components-core/src/test/resources/plot.sh b/components-core/src/test/resources/plot.sh new file mode 100644 index 0000000000..63380ad8e5 --- /dev/null +++ b/components-core/src/test/resources/plot.sh @@ -0,0 +1,41 @@ +#!/usr/bin/gnuplot +# +# Plotting the data of file data.dat with points and non-continuous lines +# +# AUTHOR: Hagen Wierstorf +# VERSION: gnuplot 4.6 patchlevel 0 + +reset + +# wxt +# set terminal wxt size 350,262 enhanced font 'Verdana,10' persist +# png +# set terminal pngcairo size 350,262 enhanced font 'Verdana,10' +#set output 'non-continuous_lines.png' + +# color definitions +set border linewidth 1.5 +# set style line 1 lc rgb '#0060ad' lt 1 lw 2 pt 7 pi -1 ps 1.5 +set style line 1 lc rgb '#0060ad' lt 1 lw 2 pt 5 ps 1.5 # --- blue +set style line 2 lc rgb '#ada400' lt 1 lw 2 pt 7 ps 1.5 # --- blue +set style line 3 lc rgb '#ad0009' lt 1 lw 2 pt 9 ps 1.5 # --- blue + +#set pointintervalbox 3 +set datafile separator "," +unset key + +set key autotitle columnheader + +set xrange [*:*] +set yrange [0:1] + +set xtics 0,.1,.4 +set ytics 0,.2,1 +#set tics scale 0.75 + +set xlabel "Noise" +set ylabel "Objective Function" + +#plot 'plot.dat' i 0 u 1:2 w linespoints title columnheader(1) +#plot 'plot.dat' with linespoints ls 1 #title columnheader(1) +plot for [IDX=0:2] 'plot.dat' i IDX u 1:2 w linespoints ls (IDX+1) # title columnheader(1) diff --git a/components-ext/pom.xml b/components-ext/pom.xml index 49007db5d1..9da2071c5f 100644 --- a/components-ext/pom.xml +++ b/components-ext/pom.xml @@ -12,7 +12,7 @@ org.dllearner dllearner-parent - 1.1 + 1.2 @@ -90,14 +90,13 @@ org.slf4j slf4j-api - + - nz.ac.waikato.cms.weka - weka-dev - 3.7.11 - + nz.ac.waikato.cms.weka + weka-dev + - + junit junit test diff --git a/components-ext/src/main/java/org/dllearner/algorithms/isle/index/syntactic/SolrSyntacticIndex.java b/components-ext/src/main/java/org/dllearner/algorithms/isle/index/syntactic/SolrSyntacticIndex.java index e7b818d3cf..70efb9c0b3 100644 --- a/components-ext/src/main/java/org/dllearner/algorithms/isle/index/syntactic/SolrSyntacticIndex.java +++ b/components-ext/src/main/java/org/dllearner/algorithms/isle/index/syntactic/SolrSyntacticIndex.java @@ -3,33 +3,13 @@ */ package org.dllearner.algorithms.isle.index.syntactic; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.TreeSet; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; - +import com.google.common.base.Joiner; +import com.google.common.collect.Sets; import org.apache.log4j.Logger; import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.SolrQuery; -import org.apache.solr.client.solrj.SolrServer; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.impl.HttpSolrClient; -import org.apache.solr.client.solrj.impl.HttpSolrServer; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; @@ -41,16 +21,15 @@ import org.dllearner.algorithms.isle.textretrieval.AnnotationEntityTextRetriever; import org.dllearner.algorithms.isle.textretrieval.RDFSLabelEntityTextRetriever; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLEntity; -import org.semanticweb.owlapi.model.OWLOntology; - +import org.semanticweb.owlapi.model.*; import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; -import com.google.common.base.Joiner; -import com.google.common.collect.Sets; +import java.io.*; +import java.util.*; +import java.util.Map.Entry; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; /** * @author Lorenz Buehmann diff --git a/components-ext/src/main/java/org/dllearner/algorithms/isle/metrics/RelevanceWeightedStableHeuristic.java b/components-ext/src/main/java/org/dllearner/algorithms/isle/metrics/RelevanceWeightedStableHeuristic.java index 7f18fc6ef8..e3fb570a45 100644 --- a/components-ext/src/main/java/org/dllearner/algorithms/isle/metrics/RelevanceWeightedStableHeuristic.java +++ b/components-ext/src/main/java/org/dllearner/algorithms/isle/metrics/RelevanceWeightedStableHeuristic.java @@ -101,8 +101,8 @@ public double getNodeScore(SearchTreeNode node){ public int compare(SearchTreeNode o1, SearchTreeNode o2) { // int diff = o2.getCoveredNegatives() - o1.getCoveredNegatives(); - double score1 = o1.getScore(); - double score2 = o2.getScore(); + double score1 = o1.getScore().getAccuracy(); + double score2 = o2.getScore().getAccuracy(); int diff = Double.compare(score1, score2); if(diff>0) { return 1; diff --git a/components-ext/src/main/java/org/dllearner/algorithms/isle/textretrieval/AnnotationEntityTextRetriever.java b/components-ext/src/main/java/org/dllearner/algorithms/isle/textretrieval/AnnotationEntityTextRetriever.java index 4cb8c86681..8611cdb717 100644 --- a/components-ext/src/main/java/org/dllearner/algorithms/isle/textretrieval/AnnotationEntityTextRetriever.java +++ b/components-ext/src/main/java/org/dllearner/algorithms/isle/textretrieval/AnnotationEntityTextRetriever.java @@ -3,31 +3,18 @@ */ package org.dllearner.algorithms.isle.textretrieval; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - +import com.google.common.base.Joiner; import org.dllearner.algorithms.isle.TextDocumentGenerator; import org.dllearner.algorithms.isle.index.LinguisticUtil; import org.dllearner.algorithms.isle.index.Token; import org.dllearner.kb.OWLAPIOntology; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAnnotation; -import org.semanticweb.owlapi.model.OWLAnnotationProperty; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLEntity; -import org.semanticweb.owlapi.model.OWLLiteral; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyManager; +import org.semanticweb.owlapi.model.*; +import org.semanticweb.owlapi.search.EntitySearcher; import org.semanticweb.owlapi.util.IRIShortFormProvider; import org.semanticweb.owlapi.util.SimpleIRIShortFormProvider; - import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; -import com.google.common.base.Joiner; +import java.util.*; /** @@ -82,7 +69,7 @@ public Map, Double> getRelevantText(OWLEntity entity) { Map, Double> textWithWeight = new HashMap, Double>(); for (OWLAnnotationProperty property : properties) { - Collection annotations = property.getAnnotations(ontology); + Collection annotations = EntitySearcher.getAnnotations(entity, ontology, property); for (OWLAnnotation annotation : annotations) { if (annotation.getValue() instanceof OWLLiteral) { OWLLiteral val = (OWLLiteral) annotation.getValue(); @@ -119,7 +106,7 @@ public Map getRelevantTextSimple(OWLEntity entity) { Map textWithWeight = new HashMap(); for (OWLAnnotationProperty property : properties) { - Collection annotations = property.getAnnotations(ontology); + Collection annotations = EntitySearcher.getAnnotations(entity, ontology, property); for (OWLAnnotation annotation : annotations) { if (annotation.getValue() instanceof OWLLiteral) { OWLLiteral val = (OWLLiteral) annotation.getValue(); diff --git a/dllearner-parent.iml b/dllearner-parent.iml deleted file mode 100644 index 02d0bed786..0000000000 --- a/dllearner-parent.iml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - diff --git a/examples/carcinogenesis/train-sparql.conf b/examples/carcinogenesis/train-sparql.conf index 485211389f..f5ab62f3b3 100644 --- a/examples/carcinogenesis/train-sparql.conf +++ b/examples/carcinogenesis/train-sparql.conf @@ -13,7 +13,6 @@ reasoner.sources = { ks } // learning problem lp.type = "posNegStandard" -lp.useMultiInstanceChecks = "ONECHECK" lp.positiveExamples = { "kb:d1", "kb:d10", diff --git a/examples/carcinogenesis/validate.conf b/examples/carcinogenesis/validate.conf index 730101af57..30c6567aa0 100644 --- a/examples/carcinogenesis/validate.conf +++ b/examples/carcinogenesis/validate.conf @@ -1,3 +1,7 @@ +// if increasing the run time to 200 seconds, a solution may be found +// possible solutions +// (2009) 1: (Compound and hasAtom only (not Nitrogen-35) and (amesTestPositive some {true} or hasStructure some Ar_halide)) (accuracy 69.915%, length 13, depth 4) + // contains 70% of all examples (see param.conf) for estimating the accuracy of DL-Learner prefixes = [ ("kb","http://dl-learner.org/carcinogenesis#") ] @@ -9,8 +13,11 @@ ks.fileName = "carcinogenesis.owl" reasoner.type = "closed world reasoner" reasoner.sources = { ks } +acc.type = "pred_acc.ocel" + // learning problem lp.type = "posNegStandard" +lp.accuracyMethod = acc lp.positiveExamples = { "kb:d25", "kb:d252", @@ -253,12 +260,13 @@ lp.negativeExamples = { } op.type = "rho" -op.cardinalityLimit = 5 +op.cardinalityLimit = 2 +op.useSomeOnly = false alg.type = "ocel" alg.searchTreeFile = "log/carcinogenesis/searchTree.log" alg.writeSearchTree = false -alg.noisePercentage = 30 +alg.noisePercentage = 31 alg.startClass = "kb:Compound" // store some settings to make the experiment reproducable in the future alg.negativeWeight = 0.8 diff --git a/examples/datatypes/stringuntyped.conf b/examples/datatypes/stringuntyped.conf index 3f10f57460..65eba308ae 100755 --- a/examples/datatypes/stringuntyped.conf +++ b/examples/datatypes/stringuntyped.conf @@ -1,7 +1,10 @@ /** * Simple test example for string datatypes - * solution - * notType hasValue "test" + * solution + * none + * since untyped data type properties cannot be learnt + * if we could make a good guess, then + * notType hasValue "test" */ prefixes = [ ("kb","http://my.example.com/string#") ] diff --git a/examples/family-benchmark/Female-sparql.conf b/examples/family-benchmark/Female-sparql.conf index c16a27f90b..161c7e3e65 100644 --- a/examples/family-benchmark/Female-sparql.conf +++ b/examples/family-benchmark/Female-sparql.conf @@ -13,6 +13,7 @@ ks.fileName = "family-benchmark.owl" reasoner.type = "SPARQL Reasoner" reasoner.laxMode = true reasoner.sources = { ks } +reasoner.useInstanceChecks = false // CELOE @@ -34,4 +35,3 @@ alg.searchTreeFile = "/tmp/tree.log" // learning problem lp.type = "ClassLearningProblem" lp.classToDescribe = "http://www.benchmark.org/family#Female" -lp.useInstanceChecks = false diff --git a/examples/family/father_oe.conf b/examples/family/father_oe.conf index a0fd9dd5dd..848c94c46b 100644 --- a/examples/family/father_oe.conf +++ b/examples/family/father_oe.conf @@ -12,7 +12,7 @@ reasoner.sources = { ks } // learning problem lp.type = "clp" lp.classToDescribe = "ex:father" -lp.accuracyMethod = "fmeasure" +accuracyMethod.type = "fmeasure" // create learning algorithm to run alg.type = "celoe" diff --git a/examples/family/father_oe_inv.conf b/examples/family/father_oe_inv.conf index bb4e1cc59b..317dfefa6a 100644 --- a/examples/family/father_oe_inv.conf +++ b/examples/family/father_oe_inv.conf @@ -12,7 +12,7 @@ reasoner.sources = { ks } // learning problem lp.type = "clp" lp.classToDescribe = "ex:fatherBornInLeipzig" -lp.accuracyMethod = "fmeasure" +accuracyMethod.type = "fmeasure" // refinement operator op.type = "rho" diff --git a/examples/family/father_someonly.conf b/examples/family/father_someonly.conf index 78e25b6ef2..b33ca5b1f9 100644 --- a/examples/family/father_someonly.conf +++ b/examples/family/father_someonly.conf @@ -11,11 +11,13 @@ ks.fileName = "father_someonly.owl" reasoner.type = "closed world reasoner" reasoner.sources = { ks } +fmeasure.type = "fmeasure" + // learning problem lp.type = "PosNegLPStandard" lp.positiveExamples = {"ex:bryan", "ex:bob", "ex:markus"} lp.negativeExamples = {"ex:stefan", "ex:martin", "ex:anna", "ex:michelle", "ex:alice"} -lp.accuracyMethod = "fmeasure" +lp.accuracyMethod = fmeasure // refinement operator op.type = "rho" diff --git a/examples/mutagenesis/train1.conf b/examples/mutagenesis/train1.conf index b7eba3ca50..9b040a7077 100644 --- a/examples/mutagenesis/train1.conf +++ b/examples/mutagenesis/train1.conf @@ -5,9 +5,8 @@ ks.type = "OWL File" ks.fileName = "mutagenesis.owl" // reasoner -reasoner.type = "owl api reasoner" +reasoner.type = "closed world reasoner" reasoner.sources = { ks } -reasoner.reasonerImplementation = "hermit" // learning problem lp.type = "posNegStandard" @@ -247,21 +246,11 @@ lp.negativeExamples = { "kb:f6" } -/* -// CELOE -alg1.type = "celoe" -alg1.noisePercentage = 30 -alg1.startClass = "kb:Compound" -alg1.maxExecutionTimeInSeconds = 60 -alg1.writeSearchTree = false -alg1.searchTreeFile = "log/mutagenesis/searchTree-celoe.log" -*/ // ELTL alg2.type = "eltl" alg2.noisePercentage = 30 alg2.startClass = "kb:Compound" -alg2.treeSearchTimeSeconds = 60 alg2.maxClassExpressionDepth = 5 -alg2.writeSearchTree = true +alg2.writeSearchTree = false alg2.searchTreeFile = "log/mutagenesis/searchTree-eltl.log" -alg2.replaceSearchTree = true \ No newline at end of file +alg2.replaceSearchTree = true diff --git a/interfaces/doc/configOptions.html b/interfaces/doc/configOptions.html index 9e2a23e9b5..906aa12910 100644 --- a/interfaces/doc/configOptions.html +++ b/interfaces/doc/configOptions.html @@ -23,15 +23,26 @@

DL-Learner Components

Click on the following items to filter the listing below by implemented interfaces (requires Javascript):

-show all

Click on a component to get an overview on its configuration options.

+

DisjunctiveHeuristic org.dllearner.algorithms.el.DisjunctiveHeuristic

+
short name
disjunctive_heuristic
version
0.1
implements
  • Heuristic
This component does not have configuration options.

ELTL org.dllearner.algorithms.el.ELLearningAlgorithm

short name
eltl
version
0.5
implements
  • LearningAlgorithm
  • ClassExpressionLearningAlgorithm
description
ELTL is an algorithm based on the refinement operator in http://jens-lehmann.org/files/2009/el_ilp.pdf.
@@ -160,6 +192,7 @@

DL-Learner Components

+ @@ -167,33 +200,57 @@

DL-Learner Components

-
option namedescriptiontypedefault valuerequired?
allowedConceptsList of classes that are allowed Set false
instanceBasedDisjointsSpecifies whether to use real disjointness checks or instance based ones (no common instances) in the refinement operator. booleantrue false
learningProblemThe Learning Problem variable to use in this algorithm AbstractClassExpressionLearningProblem false
maxClassExpressionDepthThe maximum depth for class expressions to test int2 false
maxExecutionTimeInSecondsmaximum execution of the algorithm in seconds int10 false
maxNrOfResultsSets the maximum number of results one is interested in int10 false
noisePercentagethe (approximated) percentage of noise within the examples double0.0 false
reasonerThe reasoner variable to use for this learning problem AbstractReasonerComponent false
searchTreeFilefile to use for the search tree Stringlog/searchTree.txt false
startClassYou can specify a start class for the algorithm. To do this, you have to use Manchester OWL syntax without using prefixes. OWLClassExpressionowl:Thing false
stopOnFirstDefinitionalgorithm will terminate immediately when a correct definition is found booleanfalse false
treeSearchTimeSecondsSpecifies how long the algorithm should search for a partial solution (a tree). double10.0 false
useMinimizerSpecifies whether returned expressions should be minimised by removing those parts, which are not needed. (Basically the minimiser tries to find the shortest expression which is equivalent to the learned expression). Turning this feature off may improve performance. booleantrue false
writeSearchTreespecifies whether to write a search tree booleanfalse false

ETDT org.dllearner.algorithms.decisiontrees.dsttdt.DSTTDTClassifier

short name
etdt
version
1.0
implements
  • LearningAlgorithm
  • ClassExpressionLearningAlgorithm
description
An Evidence-based Terminological Decision Tree
- - + + - + + + +
option namedescriptiontypedefault valuerequired?
BEAMvalue for limiting the number of generated concepts int4 false
PURITY_THRESHOLDPurity threshold for setting a leaf double0.05 false
allowedConceptsList of classes that are allowed Set false
allowedDataPropertiesList of data properties to allow Set false
allowedObjectPropertiesList of object properties to allow Set false
beamvalue for limiting the number of generated concepts int4 false
heuristicinstance of heuristic to use TreeInductionHeuristicsTreeInductionHeuristics false
ignoredConceptsList of classes to ignore Set false
ignoredDataPropertiesList of data properties to ignore Set false
ignoredObjectPropertiesList of object properties to ignore Set false
learningProblemThe Learning Problem variable to use in this algorithm AbstractClassExpressionLearningProblem false
nonspecificitya flag to decide if further control on the purity measure should be made booleanfalse false
maxExecutionTimeInSecondsmaximum execution of the algorithm in seconds int10 false
nonSpecifityControla flag to decide if further control on the purity measure should be made booleanfalse false
operatorrefinement operator instance to use RefinementOperatorDLTreesRefinementOperator false
puritythresholdPurity threshold for setting a leaf double0.05 false
reasonerThe reasoner variable to use for this learning problem AbstractReasonerComponent false
useMinimizerSpecifies whether returned expressions should be minimised by removing those parts, which are not needed. (Basically the minimiser tries to find the shortest expression which is equivalent to the learned expression). Turning this feature off may improve performance. booleantrue false
+

FMeasure org.dllearner.learningproblems.AccMethodFMeasure

+
short name
fmeasure
version
0.0
implements
  • AccMethod
This component does not have configuration options.
+

FMeasure Approximate org.dllearner.learningproblems.AccMethodFMeasureApprox

+
short name
approx.fmeasure
version
0.0
implements
  • AccMethod
+ +
option namedescriptiontypedefault valuerequired?
approxDeltaThe Approximate Delta double0.05 false
+
+

Flexible Heuristic org.dllearner.algorithms.ocel.FlexibleHeuristic

+
short name
flexheuristic
version
0.1
implements
  • Heuristic
+ + +
option namedescriptiontypedefault valuerequired?
nrOfNegativeExamplesthe number of negative examples int false
percentPerLengthUnitscore percent to deduct per expression length double true
+
+

FuzzyPosNegLPStandard org.dllearner.learningproblems.FuzzyPosNegLPStandard

+
short name
fuzzyPosNeg
version
0.2
implements
  • LearningProblem
+ +
option namedescriptiontypedefault valuerequired?
accuracyMethodSpecifies, which method/function to use for computing accuracy. Available measues are "PRED_ACC" (predictive accuracy), "FMEASURE" (F measure), "GEN_FMEASURE" (generalised F-Measure according to Fanizzi and d'Amato). HeuristicTypePRED_ACC false
+

GLOBAL OPTIONS org.dllearner.cli.DocumentationGeneratorMeta.GlobalDoc

option namedescriptiontypedefault valuerequired?
prefixesMapping of prefixes to replace inside other configuration file entries Example: [ ("ex","http://example.com/father#") ] Map false
renderingThe string renderer for any OWL expression output, can be "dlsyntax" or "manchester" Example: dlsyntax Stringmanchester false
+

Generalised FMeasure org.dllearner.learningproblems.AccMethodGenFMeasure

+
short name
gen_fmeasure
version
0.1
implements
  • AccMethod
This component does not have configuration options.
+

Jaccard Coefficient org.dllearner.learningproblems.AccMethodJaccard

+
short name
jaccard
version
0.1
implements
  • AccMethod
This component does not have configuration options.

KB File org.dllearner.kb.KBFile

short name
kbfile
version
0.8
implements
  • KnowledgeSource
@@ -201,6 +258,8 @@

DL-Learner Components

option namedescriptiontypedefault valuerequired?
baseDirchange the base directory (must be absolute) Stringdirectory of conf file false
urlURL pointer to the KB file String false
+

Lexicograpgic Heuristic org.dllearner.algorithms.ocel.LexicographicHeuristic

+
short name
lexheuristic
version
0.1
implements
  • Heuristic
This component does not have configuration options.

Local Endpoint org.dllearner.kb.LocalModelBasedSparqlEndpointKS

short name
local_sparql
version
0.9
implements
  • KnowledgeSource
@@ -224,20 +283,30 @@

DL-Learner Components

+ +
option namedescriptiontypedefault valuerequired?
cacheDirThe base directory of the SPARQL query cache. Stringtmp folder of the system false
ignoredDataPropertiesList of data properties to ignore Set false
ignoredObjectPropertiesList of object properties to ignore Set false
learningProblemThe Learning Problem variable to use in this algorithm AbstractClassExpressionLearningProblem false
maxExecutionTimeInSecondsmaximum execution of the algorithm in seconds int10 false
maxLengthmaximum length of class expression int4 false
reasonerThe reasoner variable to use for this learning problem AbstractReasonerComponent false
useMinimizerSpecifies whether returned expressions should be minimised by removing those parts, which are not needed. (Basically the minimiser tries to find the shortest expression which is equivalent to the learned expression). Turning this feature off may improve performance. booleantrue false

OEHeuristicRuntime org.dllearner.algorithms.celoe.OEHeuristicRuntime

short name
celoe_heuristic
version
0.5
implements
  • Heuristic
+ + +
option namedescriptiontypedefault valuerequired?
expansionPenaltyFactorpenalty for long descriptions (horizontal expansion) (strong by default) double0.1 false
gainBonusFactorbonus for being better than parent node double0.3 false
nodeRefinementPenaltypenalty if a node OWLClassExpression has very many refinements since exploring such a node is computationally very expensive double0.0001 false
startNodeBonusno description available double0.1 false

OWL API Reasoner org.dllearner.reasoning.OWLAPIReasoner

short name
oar
version
0.8
implements
  • ReasonerComponent
+ + + + +
option namedescriptiontypedefault valuerequired?
owlLinkURLspecifies the URL of the remote OWLLink server Stringnull false
precomputeClassHierarchyif class hierarchy should be precomputed booleantrue false
precomputeDataPropertyHierarchyno description available booleantrue false
precomputeObjectPropertyHierarchyno description available booleantrue false
reasonerImplementationspecifies the used OWL API reasoner implementation ReasonerImplementationpellet false
sourcesthe underlying knowledge sources Set true
useFallbackReasonerspecifies whether to use a fallback reasoner if a reasoner call fails because it's not supported or results in a bug. (the fallback works only on the assertional level booleanfalse false
useInstanceCheckswhether to use single instance checks booleanfalse false

OWL Class Expression Learner org.dllearner.algorithms.ocel.OCEL

@@ -249,6 +318,7 @@

DL-Learner Components

ignoredDataPropertiesList of data properties to ignore Set false ignoredObjectPropertiesList of object properties to ignore Set false learningProblemThe Learning Problem variable to use in this algorithm AbstractClassExpressionLearningProblem false +maxExecutionTimeInSecondsmaximum execution of the algorithm in seconds int10 false reasonerThe reasoner variable to use for this learning problem AbstractReasonerComponent false useMinimizerSpecifies whether returned expressions should be minimised by removing those parts, which are not needed. (Basically the minimiser tries to find the shortest expression which is equivalent to the learned expression). Turning this feature off may improve performance. booleantrue false
@@ -264,34 +334,107 @@

DL-Learner Components

urlURL pointer to the KB file or Endpoint URL false +

OperatorInverter org.dllearner.refinementoperators.OperatorInverter

+
short name
inv_op
version
0.1
implements
  • RefinementOperator
+ + + +
option namedescriptiontypedefault valuerequired?
guaranteeLengthWhether inverse solutions must respect the desired max length booleantrue false
operatoroperator to invert LengthLimitedRefinementOperator true
useNegationNormalFormwhether to apply NNF booleantrue false
+
+

PCELOE org.dllearner.algorithms.celoe.PCELOE

+
short name
pceloe
version
1.0
implements
  • LearningAlgorithm
  • ClassExpressionLearningAlgorithm
description
PCELOE is an experimental, parallel implementation of the CELOE algorithm.
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
option namedescriptiontypedefault valuerequired?
allowedConceptsList of classes that are allowed Set false
allowedDataPropertiesList of data properties to allow Set false
allowedObjectPropertiesList of object properties to allow Set false
expandAccuracy100Nodeswhether to try and refine solutions which already have accuracy value of 1 booleanfalse false
filterDescriptionsFollowingFromKBIf true, then the results will not contain suggestions, which already follow logically from the knowledge base. Be careful, since this requires a potentially expensive consistency check for candidate solutions. booleanfalse false
heuristicno description available AbstractHeuristicceloe_heuristic false
ignoredConceptsList of classes to ignore Set false
ignoredDataPropertiesList of data properties to ignore Set false
ignoredObjectPropertiesList of object properties to ignore Set false
learningProblemThe Learning Problem variable to use in this algorithm AbstractClassExpressionLearningProblem false
maxClassExpressionTestsThe maximum number of candidate hypothesis the algorithm is allowed to test (0 = no limit). The algorithm will stop afterwards. (The real number of tests can be slightly higher, because this criterion usually won't be checked after each single test.) int0 false
maxClassExpressionTestsAfterImprovementThe maximum number of candidate hypothesis the algorithm is allowed after an improvement in accuracy (0 = no limit). The algorithm will stop afterwards. (The real number of tests can be slightly higher, because this criterion usually won't be checked after each single test.) int0 false
maxDepthmaximum depth of description double7 false
maxExecutionTimeInSecondsmaximum execution of the algorithm in seconds int10 false
maxExecutionTimeInSecondsAfterImprovementmaximum execution of the algorithm in seconds int0 false
maxNrOfResultsSets the maximum number of results one is interested in. (Setting this to a lower value may increase performance as the learning algorithm has to store/evaluate/beautify less descriptions). int10 false
noisePercentagethe (approximated) percentage of noise within the examples double0.0 false
nrOfThreadsnumber of threads running in parallel int2 false
operatorthe refinement operator instance to use LengthLimitedRefinementOperator false
reasonerThe reasoner variable to use for this learning problem AbstractReasonerComponent false
replaceSearchTreespecifies whether to replace the search tree in the log file after each run or append the new search tree booleanfalse false
reuseExistingDescriptionIf true, the algorithm tries to find a good starting point close to an existing definition/super class of the given class in the knowledge base. booleanfalse false
searchTreeFilefile to use for the search tree Stringlog/searchTree.txt false
singleSuggestionModeUse this if you are interested in only one suggestion and your learning problem has many (more than 1000) examples. booleanfalse false
startClassYou can specify a start class for the algorithm. To do this, you have to use Manchester OWL syntax without using prefixes. OWLClassExpressionowl:Thing false
stopOnFirstDefinitionalgorithm will terminate immediately when a correct definition is found booleanfalse false
terminateOnNoiseReachedspecifies whether to terminate when noise criterion is met booleanfalse false
useMinimizerSpecifies whether returned expressions should be minimised by removing those parts, which are not needed. (Basically the minimiser tries to find the shortest expression which is equivalent to the learned expression). Turning this feature off may improve performance. booleantrue false
writeSearchTreespecifies whether to write a search tree booleanfalse false
+

PosNegLPStandard org.dllearner.learningproblems.PosNegLPStandard

short name
posNegStandard
version
0.8
implements
  • LearningProblem
- - + + + + + +
option namedescriptiontypedefault valuerequired?
accuracyMethodSpecifies, which method/function to use for computing accuracy. Available measues are "pred_acc" (predictive accuracy), "fmeasure" (F measure), "generalised_fmeasure" (generalised F-Measure according to Fanizzi and d'Amato). Stringpredacc false
approxDeltaThe Approximate Delta double0.05 false
accuracyMethodSpecifies, which method/function to use for computing accuracy. Available measues are "PRED_ACC" (predictive accuracy), "FMEASURE" (F measure), "GEN_FMEASURE" (generalised F-Measure according to Fanizzi and d'Amato). AccMethodTwoValuedPRED_ACC false
negativeExampleslist of negative examples Set true
percentPerLengthUnitPercent Per Length Unit double0.05 false
positiveExampleslist of positive examples Set true
useRetrievalForClassification"Specifies whether to use retrieval or instance checks for testing a concept. - NO LONGER FULLY SUPPORTED. booleanfalse false
+
+

PosNegLPStrict org.dllearner.learningproblems.PosNegLPStrict

+
short name
posNegStrict
version
0.1
implements
  • LearningProblem
description
three valued definition learning problem
+ + + + + - - +
option namedescriptiontypedefault valuerequired?
accuracyMethodSpecifies, which method/function to use for computing accuracy. Available measues are "PRED_ACC" (predictive accuracy), "FMEASURE" (F measure), "GEN_FMEASURE" (generalised F-Measure according to Fanizzi and d'Amato). AccMethodTwoValuedPRED_ACC false
accuracyPenaltypenalty for pos/neg examples which are classified as neutral double1.0 false
errorPenaltypenalty for pos. examples classified as negative or vice versa double3.0 false
negativeExampleslist of negative examples Set true
penaliseNeutralExamplesif set to true neutral examples are penalised boolean false
percentPerLengthUnitPercent Per Length Unit double0.05 false
useApproximationsUse Approximations booleanfalse false
useMultiInstanceChecksUse The Multi Instance Checks UseMultiInstanceChecksUseMultiInstanceChecks.TWOCHECKS false
positiveExampleslist of positive examples Set true
useRetrievalForClassification"Specifies whether to use retrieval or instance checks for testing a concept. - NO LONGER FULLY SUPPORTED. booleanfalse false

PosNegUndLP org.dllearner.learningproblems.PosNegUndLP

-
short name
posNegUndLP
version
1.0
implements
  • LearningProblem
description
A learning problem with uncertain-memebrship instances
- +
short name
posNegUndLP
version
1.0
implements
  • LearningProblem
description
A learning problem with uncertain-membership instances
option namedescriptiontypedefault valuerequired?
accuracyMethodSpecifies, which method/function to use for computing accuracy. Available measues are "pred_acc" (predictive accuracy), "fmeasure" (F measure), "generalised_fmeasure" (generalised F-Measure according to Fanizzi and d'Amato). Stringpredacc false
+ +
option namedescriptiontypedefault valuerequired?
uncertainExamplesthe uncertain examples Set true
+
+

Predictive Accuracy org.dllearner.learningproblems.AccMethodPredAcc

+
short name
pred_acc
version
0.0
implements
  • AccMethod
This component does not have configuration options.
+

Predictive Accuracy Approximate org.dllearner.learningproblems.AccMethodPredAccApprox

+
short name
approx.prec_acc
version
0.0
implements
  • AccMethod
-
option namedescriptiontypedefault valuerequired?
approxDeltaThe Approximate Delta double0.05 false
useApproximationsUse Approximations booleanfalse false
-

QueryTreeHeuristic org.dllearner.algorithms.qtl.heuristics.QueryTreeHeuristicSimple

-
short name
qtree_heuristic_simple
version
0.1
implements
  • Heuristic
This component does not have configuration options.
+

Predictive Accuracy without Weak elimination org.dllearner.learningproblems.AccMethodPredAccOCEL

+
short name
pred_acc.ocel
version
0.0
implements
  • AccMethod
This component does not have configuration options.
+

PropertyAxiomLearningProblem org.dllearner.learningproblems.PropertyAxiomLearningProblem

+
short name
palp
version
0.6
implements
  • LearningProblem
+ +
option namedescriptiontypedefault valuerequired?
reasonerThe reasoner component variable to use for this Learning Problem AbstractReasonerComponent false
+
+

QueryTreeHeuristic org.dllearner.algorithms.qtl.heuristics.QueryTreeHeuristicComplex

+
short name
qtree_heuristic_complex
version
0.1
implements
  • Heuristic
This component does not have configuration options.

Refinement Operator TDT org.dllearner.algorithms.decisiontrees.refinementoperators.DLTreesRefinementOperator

short name
tdtop
version
1.0
implements
  • RefinementOperator
- + + +
option namedescriptiontypedefault valuerequired?
beamno description available int5 false
kindOperatorno description available int1 false
lpthe learning problem instance to use PosNegLP false
reasonerthe reasoner instance to use Reasoner false
rono description available int1 false

SPARQL Reasoner org.dllearner.reasoning.SPARQLReasoner

short name
spr
version
0.1
implements
  • ReasonerComponent
+ + + + + + + +
option namedescriptiontypedefault valuerequired?
laxModeUse alternative relaxed Sparql-queries for Classes and Individuals booleanfalse false
precomputeClassHierarchyif class hierarchy should be precomputed booleantrue false
precomputeDataPropertyHierarchyno description available booleantrue false
precomputeObjectPropertyHierarchyno description available booleantrue false
sourcesthe underlying knowledge sources Set true
useCacheWhether to use a file-based cache booleantrue false
useGenericSplitsCodeWhether to use the generic facet generation code, which requires downloading all instances and is thus not recommended booleanfalse false
useInstanceCheckswhether to use single instance checks booleanfalse false
useValueListsWhether to use SPARQL1.1 Value Lists booleanfalse false

SPARQL endpoint org.dllearner.kb.SparqlEndpointKS

@@ -310,12 +453,33 @@

DL-Learner Components

short name
sparqlfrag
version
0.5
implements
  • KnowledgeSource
This component does not have configuration options.

SemKernel Workflow org.dllearner.utilities.semkernel.SemKernelWorkflow

short name
skw
version
0.1
implements
  • OtherComponent
This component does not have configuration options.
+

Stable Heuristic org.dllearner.algorithms.el.StableHeuristic

+
short name
el_heuristic
version
0.1
implements
  • Heuristic
This component does not have configuration options.
+

Weighted FMeasure org.dllearner.learningproblems.AccMethodFMeasureWeighted

+
short name
weighted.fmeasure
version
0.0
implements
  • AccMethod
+ + + +
option namedescriptiontypedefault valuerequired?
balancedbalance the weights to relative set size booleanfalse false
negWeightweight on the negative examples double1 false
posWeightweight on the positive examples double1 false
+
+

Weighted Predictive Accuracy org.dllearner.learningproblems.AccMethodPredAccWeighted

+
short name
weighted.pred_acc
version
0.0
implements
  • AccMethod
+ + + +
option namedescriptiontypedefault valuerequired?
balancedbalance the weights to relative set size booleanfalse false
negWeightweight on the negative examples double1 false
posWeightweight on the positive examples double1 false
+

asymmetric object property axiom learner org.dllearner.algorithms.properties.AsymmetricObjectPropertyAxiomLearner

short name
oplasymm
version
0.1
implements
  • LearningAlgorithm
  • AxiomLearningAlgorithm
description
A learning algorithm for asymmetric object property axioms.
This component does not have configuration options.

closed world reasoner org.dllearner.reasoning.ClosedWorldReasoner

short name
cwr
version
0.9
implements
  • ReasonerComponent
- + + + + + +
option namedescriptiontypedefault valuerequired?
defaultNegationWhether to use default negation, i.e. an instance not being in a class means that it is in the negation of the class. booleantrue false
forAllRetrievalSemanticsThis option controls how to interpret the all quantifier in forall r.C. The standard option is to return all those which do not have an r-filler not in C. The domain semantics is to use those which are in the domain of r and do not have an r-filler not in C. The forallExists semantics is to use those which have at least one r-filler and do not have an r-filler not in C. ForallSemanticsstandard false
forallSemanticsThis option controls how to interpret the all quantifier in forall r.C. The standard option is to return all those which do not have an r-filler not in C. The domain semantics is to use those which are in the domain of r and do not have an r-filler not in C. The forallExists semantics is to use those which have at least one r-filler and do not have an r-filler not in C. ForallSemanticsstandard false
precomputeClassHierarchyif class hierarchy should be precomputed booleantrue false
precomputeDataPropertyHierarchyno description available booleantrue false
precomputeObjectPropertyHierarchyno description available booleantrue false
sourcesthe underlying knowledge sources Set true
useInstanceCheckswhether to use single instance checks booleanfalse false

data property domain axiom learner org.dllearner.algorithms.properties.DataPropertyDomainAxiomLearner

@@ -326,9 +490,14 @@

DL-Learner Components

short name
dplsubprop
version
0.1
implements
  • LearningAlgorithm
  • AxiomLearningAlgorithm
description
A learning algorithm data subproperty axioms.
This component does not have configuration options.

disjoint classes learner org.dllearner.algorithms.DisjointClassesLearner

short name
cldisjoint
version
0.1
implements
  • LearningAlgorithm
  • AxiomLearningAlgorithm
  • ClassExpressionLearningAlgorithm
- + + + - + + + +
option namedescriptiontypedefault valuerequired?
maxExecutionTimeInSeconds int10 false
entityToDescribethe OWL entity to learn about OWLEntity false
ksthe sparql endpoint knowledge source SparqlEndpointKS false
maxExecutionTimeInSecondsmaximum execution of the algorithm in seconds (abstract) int10 false
maxFetchedRowsThe maximum number of rows fetched from the endpoint to approximate the result. int false
returnOnlyNewAxioms booleanfalse false
reasonerThe sparql reasoner instance to use SPARQLReasonerSPARQLReasoner false
returnOnlyNewAxiomsomit axioms already existing in the knowledge base booleanfalse false
suggestMostGeneralClassesonly keep most general classes in suggestions booleantrue false
useClassPopularityinclude instance count / popularity when computing scores booleantrue false

disjoint data properties axiom learner org.dllearner.algorithms.properties.DisjointDataPropertyAxiomLearner

@@ -378,7 +547,10 @@

DL-Learner Components

object subproperty axiom learner org.dllearner.algorithms.properties.SubObjectPropertyOfAxiomLearner

short name
oplsubprop
version
0.1
implements
  • LearningAlgorithm
  • AxiomLearningAlgorithm
description
A learning algorithm object subproperty axioms.
This component does not have configuration options.

positive only learning problem org.dllearner.learningproblems.PosOnlyLP

-
short name
posonlylp
version
0.6
implements
  • LearningProblem
This component does not have configuration options.
+
short name
posonlylp
version
0.6
implements
  • LearningProblem
+ +
option namedescriptiontypedefault valuerequired?
positiveExamplesthe positive examples SortedSet true
+

query tree learner with noise (disjunctive) org.dllearner.algorithms.qtl.QTL2Disjunctive

short name
qtl2dis
version
0.8
implements
  • LearningAlgorithm
  • ClassExpressionLearningAlgorithm
@@ -403,14 +575,17 @@

DL-Learner Components

+ + + @@ -418,19 +593,25 @@

DL-Learner Components

simple subclass learner org.dllearner.algorithms.SimpleSubclassLearner

short name
clsub
version
0.1
implements
  • LearningAlgorithm
  • AxiomLearningAlgorithm
  • ClassExpressionLearningAlgorithm
option namedescriptiontypedefault valuerequired?
allowedConceptsList of classes that are allowed Set false
disjointChecksno description available booleantrue false
dropDisjunctsno description available booleanfalse false
instanceBasedDisjointsno description available booleantrue false
maxNrOfSplitsthe number of generated split intervals for numeric types int12 false
useAllConstructorsupport of universal restrictions (owl:allValuesFrom), e.g. ∀ r.C booleantrue false
useBooleanDatatypessupport of boolean datatypes (xsd:boolean), e.g. ∃ r.{true} booleantrue false
useCardinalityRestrictionssupport of qualified cardinality restrictions (owl:minCardinality), e.g. ≥ 3 r.C booleantrue false
useDataHasValueConstructorwhether to use hasValue on frequently occuring strings booleanfalse false
useExistsConstructorsupport of existential restrictions (owl:someValuesFrom), e.g. ∃ r.C booleantrue false
useHasValueConstructorsupport of has value constructor (owl:hasValue), e.g. ∃ r.{a} booleanfalse false
useInversesupport of inverse object properties (owl:inverseOf), e.g. r⁻.C booleanfalse false
useNegationsupport of negation (owl:complementOf), e.g. ¬ C booleantrue false
useNumericDatatypessupport of numeric datatypes (xsd:int, xsd:double, ...), e.g. ∃ r.{true} booleantrue false
useObjectValueNegationwhether to generate object complement while refining booleanfalse false
useSomeOnlyuniversal restrictions on a property r are only used when there is already a cardinality and/or existential restriction on r booleantrue false
useStringDatatypessupport of string datatypes (xsd:string), e.g. ∃ r.{"SOME_STRING"} booleanfalse false
useTimeDatatypesno description available booleantrue false
- + + + - + +
option namedescriptiontypedefault valuerequired?
maxExecutionTimeInSeconds int10 false
entityToDescribethe OWL entity to learn about OWLEntity false
ksthe sparql endpoint knowledge source SparqlEndpointKS false
maxExecutionTimeInSecondsmaximum execution of the algorithm in seconds (abstract) int10 false
maxFetchedRowsThe maximum number of rows fetched from the endpoint to approximate the result. int false
returnOnlyNewAxioms booleanfalse false
reasonerThe sparql reasoner instance to use SPARQLReasonerSPARQLReasoner false
returnOnlyNewAxiomsomit axioms already existing in the knowledge base booleanfalse false

symmetric object property axiom learner org.dllearner.algorithms.properties.SymmetricObjectPropertyAxiomLearner

short name
oplsymm
version
0.1
implements
  • LearningAlgorithm
  • AxiomLearningAlgorithm
description
A learning algorithm for symmetric object property axioms.
This component does not have configuration options.

tdt org.dllearner.algorithms.decisiontrees.tdt.TDTClassifier

short name
tdt
version
1.0
implements
  • LearningAlgorithm
  • ClassExpressionLearningAlgorithm
description
A Terminological Decision Tree
- - + - + + + + +
option namedescriptiontypedefault valuerequired?
BINARYCLASSIFICATIONvalue for limiting the number of generated concepts booleanfalse false
PURITY_THRESHOLDPurity threshold for setting a leaf double0.05 false
binaryClassificationif it is a binary classification problem booleanfalse false
ccpvalue for limiting the number of generated concepts booleanfalse false
missingValueTreatmentfor overcoming the problem of missing values in tree algorithms.tree.models booleanfalse false
classToDescribeconcept for splitting undefined examples into positive and negative for binary classification problems OWLClassExpression false
heuristicthe heuristic instance to use TreeInductionHeuristicsTreeInductionHeuristics false
missingValueTreatmentForTDTfor overcoming the problem of missing values in tree algorithms.tree.models booleanfalse false
operatorthe refinement operator instance to use RefinementOperatorDLTreesRefinementOperator false
puritythresholdPurity threshold for setting a leaf double0.05 false

transitive object property axiom learner org.dllearner.algorithms.properties.TransitiveObjectPropertyAxiomLearner

diff --git a/interfaces/doc/configOptions.txt b/interfaces/doc/configOptions.txt index c0a7c2e5d7..f67e328a89 100644 --- a/interfaces/doc/configOptions.txt +++ b/interfaces/doc/configOptions.txt @@ -465,8 +465,22 @@ component: disjoint classes learner (org.dllearner.algorithms.DisjointClassesLea conf file usage: la.type = "cldisjoint" +option name: entityToDescribe +description: the OWL entity to learn about +type: OWLEntity +required: false +default value: +conf file usage: la.entityToDescribe = "" + +option name: ks +description: the sparql endpoint knowledge source +type: SparqlEndpointKS +required: false +default value: +conf file usage: la.ks = "" + option name: maxExecutionTimeInSeconds -description: +description: maximum execution of the algorithm in seconds (abstract) type: int required: false default value: 10 @@ -479,13 +493,34 @@ required: false default value: conf file usage: la.maxFetchedRows = +option name: reasoner +description: The sparql reasoner instance to use +type: SPARQLReasoner +required: false +default value: SPARQLReasoner +conf file usage: la.reasoner = "SPARQLReasoner" + option name: returnOnlyNewAxioms -description: +description: omit axioms already existing in the knowledge base type: boolean required: false default value: false conf file usage: la.returnOnlyNewAxioms = false +option name: suggestMostGeneralClasses +description: only keep most general classes in suggestions +type: boolean +required: false +default value: true +conf file usage: la.suggestMostGeneralClasses = true + +option name: useClassPopularity +description: include instance count / popularity when computing scores +type: boolean +required: false +default value: true +conf file usage: la.useClassPopularity = true + component: Naive AL Learner (org.dllearner.algorithms.NaiveALLearner) v0.1 ========================================================================== @@ -540,6 +575,20 @@ required: false default value: conf file usage: la.learningProblem = "" +option name: maxExecutionTimeInSeconds +description: maximum execution of the algorithm in seconds +type: int +required: false +default value: 10 +conf file usage: la.maxExecutionTimeInSeconds = 10 + +option name: maxLength +description: maximum length of class expression +type: int +required: false +default value: 4 +conf file usage: la.maxLength = 4 + option name: reasoner description: The reasoner variable to use for this learning problem type: AbstractReasonerComponent @@ -559,8 +608,22 @@ component: simple subclass learner (org.dllearner.algorithms.SimpleSubclassLearn conf file usage: la.type = "clsub" +option name: entityToDescribe +description: the OWL entity to learn about +type: OWLEntity +required: false +default value: +conf file usage: la.entityToDescribe = "" + +option name: ks +description: the sparql endpoint knowledge source +type: SparqlEndpointKS +required: false +default value: +conf file usage: la.ks = "" + option name: maxExecutionTimeInSeconds -description: +description: maximum execution of the algorithm in seconds (abstract) type: int required: false default value: 10 @@ -573,8 +636,15 @@ required: false default value: conf file usage: la.maxFetchedRows = +option name: reasoner +description: The sparql reasoner instance to use +type: SPARQLReasoner +required: false +default value: SPARQLReasoner +conf file usage: la.reasoner = "SPARQLReasoner" + option name: returnOnlyNewAxioms -description: +description: omit axioms already existing in the knowledge base type: boolean required: false default value: false @@ -607,6 +677,13 @@ required: false default value: conf file usage: la.allowedObjectProperties = {} +option name: expandAccuracy100Nodes +description: whether to try and refine solutions which already have accuracy value of 1 +type: boolean +required: false +default value: false +conf file usage: la.expandAccuracy100Nodes = false + option name: filterDescriptionsFollowingFromKB description: If true, then the results will not contain suggestions, which already follow logically from the knowledge base. Be careful, since this requires a potentially expensive consistency check for candidate solutions. type: boolean @@ -698,6 +775,13 @@ required: false default value: 0.0 conf file usage: la.noisePercentage = 0.0 +option name: operator +description: the refinement operator instance to use +type: LengthLimitedRefinementOperator +required: false +default value: +conf file usage: la.operator = "" + option name: reasoner description: The reasoner variable to use for this learning problem type: AbstractReasonerComponent @@ -768,25 +852,11 @@ required: false default value: false conf file usage: la.writeSearchTree = false -component: ETDT (org.dllearner.algorithms.decisiontrees.dsttdt.DSTTDTClassifier) v1.0 -===================================================================================== -An Evidence-based Terminological Decision Tree - -conf file usage: la.type = "etdt" - -option name: BEAM -description: value for limiting the number of generated concepts -type: int -required: false -default value: 4 -conf file usage: la.BEAM = 4 +component: PCELOE (org.dllearner.algorithms.celoe.PCELOE) v1.0 +============================================================== +PCELOE is an experimental, parallel implementation of the CELOE algorithm. -option name: PURITY_THRESHOLD -description: Purity threshold for setting a leaf -type: double -required: false -default value: 0.05 -conf file usage: la.PURITY_THRESHOLD = 0.05 +conf file usage: la.type = "pceloe" option name: allowedConcepts description: List of classes that are allowed @@ -809,6 +879,27 @@ required: false default value: conf file usage: la.allowedObjectProperties = {} +option name: expandAccuracy100Nodes +description: whether to try and refine solutions which already have accuracy value of 1 +type: boolean +required: false +default value: false +conf file usage: la.expandAccuracy100Nodes = false + +option name: filterDescriptionsFollowingFromKB +description: If true, then the results will not contain suggestions, which already follow logically from the knowledge base. Be careful, since this requires a potentially expensive consistency check for candidate solutions. +type: boolean +required: false +default value: false +conf file usage: la.filterDescriptionsFollowingFromKB = false + +option name: heuristic +description: no description available +type: AbstractHeuristic +required: false +default value: celoe_heuristic +conf file usage: la.heuristic = "celoe_heuristic" + option name: ignoredConcepts description: List of classes to ignore type: Set @@ -837,12 +928,68 @@ required: false default value: conf file usage: la.learningProblem = "" -option name: nonspecificity -description: a flag to decide if further control on the purity measure should be made -type: boolean +option name: maxClassExpressionTests +description: The maximum number of candidate hypothesis the algorithm is allowed to test (0 = no limit). The algorithm will stop afterwards. (The real number of tests can be slightly higher, because this criterion usually won't be checked after each single test.) +type: int required: false -default value: false -conf file usage: la.nonspecificity = false +default value: 0 +conf file usage: la.maxClassExpressionTests = 0 + +option name: maxClassExpressionTestsAfterImprovement +description: The maximum number of candidate hypothesis the algorithm is allowed after an improvement in accuracy (0 = no limit). The algorithm will stop afterwards. (The real number of tests can be slightly higher, because this criterion usually won't be checked after each single test.) +type: int +required: false +default value: 0 +conf file usage: la.maxClassExpressionTestsAfterImprovement = 0 + +option name: maxDepth +description: maximum depth of description +type: double +required: false +default value: 7 +conf file usage: la.maxDepth = 7 + +option name: maxExecutionTimeInSeconds +description: maximum execution of the algorithm in seconds +type: int +required: false +default value: 10 +conf file usage: la.maxExecutionTimeInSeconds = 10 + +option name: maxExecutionTimeInSecondsAfterImprovement +description: maximum execution of the algorithm in seconds +type: int +required: false +default value: 0 +conf file usage: la.maxExecutionTimeInSecondsAfterImprovement = 0 + +option name: maxNrOfResults +description: Sets the maximum number of results one is interested in. (Setting this to a lower value may increase performance as the learning algorithm has to store/evaluate/beautify less descriptions). +type: int +required: false +default value: 10 +conf file usage: la.maxNrOfResults = 10 + +option name: noisePercentage +description: the (approximated) percentage of noise within the examples +type: double +required: false +default value: 0.0 +conf file usage: la.noisePercentage = 0.0 + +option name: nrOfThreads +description: number of threads running in parallel +type: int +required: false +default value: 2 +conf file usage: la.nrOfThreads = 2 + +option name: operator +description: the refinement operator instance to use +type: LengthLimitedRefinementOperator +required: false +default value: +conf file usage: la.operator = "" option name: reasoner description: The reasoner variable to use for this learning problem @@ -851,52 +998,74 @@ required: false default value: conf file usage: la.reasoner = "" -option name: useMinimizer -description: Specifies whether returned expressions should be minimised by removing those parts, which are not needed. (Basically the minimiser tries to find the shortest expression which is equivalent to the learned expression). Turning this feature off may improve performance. +option name: replaceSearchTree +description: specifies whether to replace the search tree in the log file after each run or append the new search tree type: boolean required: false -default value: true -conf file usage: la.useMinimizer = true +default value: false +conf file usage: la.replaceSearchTree = false -component: tdt (org.dllearner.algorithms.decisiontrees.tdt.TDTClassifier) v1.0 -============================================================================== -A Terminological Decision Tree +option name: reuseExistingDescription +description: If true, the algorithm tries to find a good starting point close to an existing definition/super class of the given class in the knowledge base. +type: boolean +required: false +default value: false +conf file usage: la.reuseExistingDescription = false -conf file usage: la.type = "tdt" +option name: searchTreeFile +description: file to use for the search tree +type: String +required: false +default value: log/searchTree.txt +conf file usage: la.searchTreeFile = "log/searchTree.txt" -option name: BINARYCLASSIFICATION -description: value for limiting the number of generated concepts +option name: singleSuggestionMode +description: Use this if you are interested in only one suggestion and your learning problem has many (more than 1000) examples. type: boolean required: false default value: false -conf file usage: la.BINARYCLASSIFICATION = false +conf file usage: la.singleSuggestionMode = false -option name: PURITY_THRESHOLD -description: Purity threshold for setting a leaf -type: double +option name: startClass +description: You can specify a start class for the algorithm. To do this, you have to use Manchester OWL syntax without using prefixes. +type: OWLClassExpression required: false -default value: 0.05 -conf file usage: la.PURITY_THRESHOLD = 0.05 +default value: owl:Thing +conf file usage: la.startClass = "owl:Thing" -option name: ccp -description: value for limiting the number of generated concepts +option name: stopOnFirstDefinition +description: algorithm will terminate immediately when a correct definition is found type: boolean required: false default value: false -conf file usage: la.ccp = false +conf file usage: la.stopOnFirstDefinition = false -option name: missingValueTreatment -description: for overcoming the problem of missing values in tree algorithms.tree.models +option name: terminateOnNoiseReached +description: specifies whether to terminate when noise criterion is met type: boolean required: false default value: false -conf file usage: la.missingValueTreatment = false +conf file usage: la.terminateOnNoiseReached = false -component: ELTL (org.dllearner.algorithms.el.ELLearningAlgorithm) v0.5 -====================================================================== -ELTL is an algorithm based on the refinement operator in http://jens-lehmann.org/files/2009/el_ilp.pdf. +option name: useMinimizer +description: Specifies whether returned expressions should be minimised by removing those parts, which are not needed. (Basically the minimiser tries to find the shortest expression which is equivalent to the learned expression). Turning this feature off may improve performance. +type: boolean +required: false +default value: true +conf file usage: la.useMinimizer = true -conf file usage: la.type = "eltl" +option name: writeSearchTree +description: specifies whether to write a search tree +type: boolean +required: false +default value: false +conf file usage: la.writeSearchTree = false + +component: ETDT (org.dllearner.algorithms.decisiontrees.dsttdt.DSTTDTClassifier) v1.0 +===================================================================================== +An Evidence-based Terminological Decision Tree + +conf file usage: la.type = "etdt" option name: allowedConcepts description: List of classes that are allowed @@ -919,19 +1088,19 @@ required: false default value: conf file usage: la.allowedObjectProperties = {} -option name: classToDescribe -description: class of which an OWL class expression should be learned -type: IRI +option name: beam +description: value for limiting the number of generated concepts +type: int required: false -default value: -conf file usage: la.classToDescribe = "" +default value: 4 +conf file usage: la.beam = 4 option name: heuristic -description: The heuristic variable to use for ELTL -type: ELHeuristic +description: instance of heuristic to use +type: TreeInductionHeuristics required: false -default value: StableHeuristic -conf file usage: la.heuristic = "StableHeuristic" +default value: TreeInductionHeuristics +conf file usage: la.heuristic = "TreeInductionHeuristics" option name: ignoredConcepts description: List of classes to ignore @@ -954,13 +1123,6 @@ required: false default value: conf file usage: la.ignoredObjectProperties = {} -option name: instanceBasedDisjoints -description: Specifies whether to use real disjointness checks or instance based ones (no common instances) in the refinement operator. -type: boolean -required: false -default value: true -conf file usage: la.instanceBasedDisjoints = true - option name: learningProblem description: The Learning Problem variable to use in this algorithm type: AbstractClassExpressionLearningProblem @@ -968,26 +1130,33 @@ required: false default value: conf file usage: la.learningProblem = "" -option name: maxClassExpressionDepth -description: The maximum depth for class expressions to test +option name: maxExecutionTimeInSeconds +description: maximum execution of the algorithm in seconds type: int required: false -default value: 2 -conf file usage: la.maxClassExpressionDepth = 2 +default value: 10 +conf file usage: la.maxExecutionTimeInSeconds = 10 -option name: maxNrOfResults -description: Sets the maximum number of results one is interested in -type: int +option name: nonSpecifityControl +description: a flag to decide if further control on the purity measure should be made +type: boolean required: false -default value: 10 -conf file usage: la.maxNrOfResults = 10 +default value: false +conf file usage: la.nonSpecifityControl = false -option name: noisePercentage -description: the (approximated) percentage of noise within the examples +option name: operator +description: refinement operator instance to use +type: RefinementOperator +required: false +default value: DLTreesRefinementOperator +conf file usage: la.operator = "DLTreesRefinementOperator" + +option name: puritythreshold +description: Purity threshold for setting a leaf type: double required: false -default value: 0.0 -conf file usage: la.noisePercentage = 0.0 +default value: 0.05 +conf file usage: la.puritythreshold = 0.05 option name: reasoner description: The reasoner variable to use for this learning problem @@ -996,15 +1165,188 @@ required: false default value: conf file usage: la.reasoner = "" -option name: replaceSearchTree -description: specifies whether to replace the search tree in the log file after each run or append the new search tree +option name: useMinimizer +description: Specifies whether returned expressions should be minimised by removing those parts, which are not needed. (Basically the minimiser tries to find the shortest expression which is equivalent to the learned expression). Turning this feature off may improve performance. type: boolean required: false -default value: false -conf file usage: la.replaceSearchTree = false +default value: true +conf file usage: la.useMinimizer = true -option name: searchTreeFile -description: file to use for the search tree +component: tdt (org.dllearner.algorithms.decisiontrees.tdt.TDTClassifier) v1.0 +============================================================================== +A Terminological Decision Tree + +conf file usage: la.type = "tdt" + +option name: binaryClassification +description: if it is a binary classification problem +type: boolean +required: false +default value: false +conf file usage: la.binaryClassification = false + +option name: ccp +description: value for limiting the number of generated concepts +type: boolean +required: false +default value: false +conf file usage: la.ccp = false + +option name: classToDescribe +description: concept for splitting undefined examples into positive and negative for binary classification problems +type: OWLClassExpression +required: false +default value: +conf file usage: la.classToDescribe = "" + +option name: heuristic +description: the heuristic instance to use +type: TreeInductionHeuristics +required: false +default value: TreeInductionHeuristics +conf file usage: la.heuristic = "TreeInductionHeuristics" + +option name: missingValueTreatmentForTDT +description: for overcoming the problem of missing values in tree algorithms.tree.models +type: boolean +required: false +default value: false +conf file usage: la.missingValueTreatmentForTDT = false + +option name: operator +description: the refinement operator instance to use +type: RefinementOperator +required: false +default value: DLTreesRefinementOperator +conf file usage: la.operator = "DLTreesRefinementOperator" + +option name: puritythreshold +description: Purity threshold for setting a leaf +type: double +required: false +default value: 0.05 +conf file usage: la.puritythreshold = 0.05 + +component: ELTL (org.dllearner.algorithms.el.ELLearningAlgorithm) v0.5 +====================================================================== +ELTL is an algorithm based on the refinement operator in http://jens-lehmann.org/files/2009/el_ilp.pdf. + +conf file usage: la.type = "eltl" + +option name: allowedConcepts +description: List of classes that are allowed +type: Set +required: false +default value: +conf file usage: la.allowedConcepts = {} + +option name: allowedDataProperties +description: List of data properties to allow +type: Set +required: false +default value: +conf file usage: la.allowedDataProperties = {} + +option name: allowedObjectProperties +description: List of object properties to allow +type: Set +required: false +default value: +conf file usage: la.allowedObjectProperties = {} + +option name: classToDescribe +description: class of which an OWL class expression should be learned +type: IRI +required: false +default value: +conf file usage: la.classToDescribe = "" + +option name: heuristic +description: The heuristic variable to use for ELTL +type: ELHeuristic +required: false +default value: StableHeuristic +conf file usage: la.heuristic = "StableHeuristic" + +option name: ignoredConcepts +description: List of classes to ignore +type: Set +required: false +default value: +conf file usage: la.ignoredConcepts = {} + +option name: ignoredDataProperties +description: List of data properties to ignore +type: Set +required: false +default value: +conf file usage: la.ignoredDataProperties = {} + +option name: ignoredObjectProperties +description: List of object properties to ignore +type: Set +required: false +default value: +conf file usage: la.ignoredObjectProperties = {} + +option name: instanceBasedDisjoints +description: Specifies whether to use real disjointness checks or instance based ones (no common instances) in the refinement operator. +type: boolean +required: false +default value: true +conf file usage: la.instanceBasedDisjoints = true + +option name: learningProblem +description: The Learning Problem variable to use in this algorithm +type: AbstractClassExpressionLearningProblem +required: false +default value: +conf file usage: la.learningProblem = "" + +option name: maxClassExpressionDepth +description: The maximum depth for class expressions to test +type: int +required: false +default value: 2 +conf file usage: la.maxClassExpressionDepth = 2 + +option name: maxExecutionTimeInSeconds +description: maximum execution of the algorithm in seconds +type: int +required: false +default value: 10 +conf file usage: la.maxExecutionTimeInSeconds = 10 + +option name: maxNrOfResults +description: Sets the maximum number of results one is interested in +type: int +required: false +default value: 10 +conf file usage: la.maxNrOfResults = 10 + +option name: noisePercentage +description: the (approximated) percentage of noise within the examples +type: double +required: false +default value: 0.0 +conf file usage: la.noisePercentage = 0.0 + +option name: reasoner +description: The reasoner variable to use for this learning problem +type: AbstractReasonerComponent +required: false +default value: +conf file usage: la.reasoner = "" + +option name: replaceSearchTree +description: specifies whether to replace the search tree in the log file after each run or append the new search tree +type: boolean +required: false +default value: false +conf file usage: la.replaceSearchTree = false + +option name: searchTreeFile +description: file to use for the search tree type: String required: false default value: log/searchTree.txt @@ -1024,13 +1366,6 @@ required: false default value: false conf file usage: la.stopOnFirstDefinition = false -option name: treeSearchTimeSeconds -description: Specifies how long the algorithm should search for a partial solution (a tree). -type: double -required: false -default value: 10.0 -conf file usage: la.treeSearchTimeSeconds = 10.0 - option name: useMinimizer description: Specifies whether returned expressions should be minimised by removing those parts, which are not needed. (Basically the minimiser tries to find the shortest expression which is equivalent to the learned expression). Turning this feature off may improve performance. type: boolean @@ -1107,6 +1442,13 @@ required: false default value: conf file usage: la.learningProblem = "" +option name: maxExecutionTimeInSeconds +description: maximum execution of the algorithm in seconds +type: int +required: false +default value: 10 +conf file usage: la.maxExecutionTimeInSeconds = 10 + option name: noisePercentage description: the (approximated) percentage of noise within the examples type: double @@ -1210,6 +1552,13 @@ required: false default value: conf file usage: la.learningProblem = "" +option name: maxExecutionTimeInSeconds +description: maximum execution of the algorithm in seconds +type: int +required: false +default value: 10 +conf file usage: la.maxExecutionTimeInSeconds = 10 + option name: reasoner description: The reasoner variable to use for this learning problem type: AbstractReasonerComponent @@ -1312,6 +1661,18 @@ conf file usage: la.useMinimizer = true * LearningProblem * ******************* +component: Class as Instance LP (org.dllearner.learningproblems.ClassAsInstanceLearningProblem) v0.1 +==================================================================================================== + +conf file usage: lp.type = "classasinstance" + +option name: percentPerLengthUnit +description: Percent Per Length Unit +type: double +required: false +default value: 0.05 +conf file usage: lp.percentPerLengthUnit = 0.05 + component: ClassLearningProblem (org.dllearner.learningproblems.ClassLearningProblem) v0.6 ========================================================================================== @@ -1319,17 +1680,10 @@ conf file usage: lp.type = "clp" option name: accuracyMethod description: Specifies, which method/function to use for computing accuracy. Available measues are "pred_acc" (predictive accuracy), "fmeasure" (F measure), "generalised_fmeasure" (generalised F-Measure according to Fanizzi and d'Amato). -type: String -required: false -default value: pred_acc -conf file usage: lp.accuracyMethod = "pred_acc" - -option name: approxDelta -description: The Approximate Delta -type: double +type: AccMethod required: false -default value: 0.05 -conf file usage: lp.approxDelta = 0.05 +default value: PRED_ACC +conf file usage: lp.accuracyMethod = "PRED_ACC" option name: betaEq description: beta index for F-measure in definition learning @@ -1373,19 +1727,17 @@ required: false default value: 10 conf file usage: lp.maxExecutionTimeInSeconds = 10 -option name: useApproximations -description: Use Approximations -type: boolean -required: false -default value: false -conf file usage: lp.useApproximations = false +component: FuzzyPosNegLPStandard (org.dllearner.learningproblems.FuzzyPosNegLPStandard) v0.2 +============================================================================================ -option name: useInstanceChecks -description: Whether to use instance checks (or get individiuals query) -type: boolean +conf file usage: lp.type = "fuzzyPosNeg" + +option name: accuracyMethod +description: Specifies, which method/function to use for computing accuracy. Available measues are "PRED_ACC" (predictive accuracy), "FMEASURE" (F measure), "GEN_FMEASURE" (generalised F-Measure according to Fanizzi and d'Amato). +type: HeuristicType required: false -default value: true -conf file usage: lp.useInstanceChecks = true +default value: PRED_ACC +conf file usage: lp.accuracyMethod = "PRED_ACC" component: PosNegLPStandard (org.dllearner.learningproblems.PosNegLPStandard) v0.8 ================================================================================== @@ -1393,18 +1745,18 @@ component: PosNegLPStandard (org.dllearner.learningproblems.PosNegLPStandard) v0 conf file usage: lp.type = "posNegStandard" option name: accuracyMethod -description: Specifies, which method/function to use for computing accuracy. Available measues are "pred_acc" (predictive accuracy), "fmeasure" (F measure), "generalised_fmeasure" (generalised F-Measure according to Fanizzi and d'Amato). -type: String +description: Specifies, which method/function to use for computing accuracy. Available measues are "PRED_ACC" (predictive accuracy), "FMEASURE" (F measure), "GEN_FMEASURE" (generalised F-Measure according to Fanizzi and d'Amato). +type: AccMethodTwoValued required: false -default value: predacc -conf file usage: lp.accuracyMethod = "predacc" +default value: PRED_ACC +conf file usage: lp.accuracyMethod = "PRED_ACC" -option name: approxDelta -description: The Approximate Delta -type: double -required: false -default value: 0.05 -conf file usage: lp.approxDelta = 0.05 +option name: negativeExamples +description: list of negative examples +type: Set +required: true +default value: +conf file usage: lp.negativeExamples = {} option name: percentPerLengthUnit description: Percent Per Length Unit @@ -1413,19 +1765,12 @@ required: false default value: 0.05 conf file usage: lp.percentPerLengthUnit = 0.05 -option name: useApproximations -description: Use Approximations -type: boolean -required: false -default value: false -conf file usage: lp.useApproximations = false - -option name: useMultiInstanceChecks -description: Use The Multi Instance Checks -type: UseMultiInstanceChecks -required: false -default value: UseMultiInstanceChecks.TWOCHECKS -conf file usage: lp.useMultiInstanceChecks = "UseMultiInstanceChecks.TWOCHECKS" +option name: positiveExamples +description: list of positive examples +type: Set +required: true +default value: +conf file usage: lp.positiveExamples = {} option name: useRetrievalForClassification description: "Specifies whether to use retrieval or instance checks for testing a concept. - NO LONGER FULLY SUPPORTED. @@ -1434,38 +1779,105 @@ required: false default value: false conf file usage: lp.useRetrievalForClassification = false -component: PosNegUndLP (org.dllearner.learningproblems.PosNegUndLP) v1.0 -======================================================================== -A learning problem with uncertain-memebrship instances +component: PosNegLPStrict (org.dllearner.learningproblems.PosNegLPStrict) v0.1 +============================================================================== +three valued definition learning problem -conf file usage: lp.type = "posNegUndLP" +conf file usage: lp.type = "posNegStrict" option name: accuracyMethod -description: Specifies, which method/function to use for computing accuracy. Available measues are "pred_acc" (predictive accuracy), "fmeasure" (F measure), "generalised_fmeasure" (generalised F-Measure according to Fanizzi and d'Amato). -type: String +description: Specifies, which method/function to use for computing accuracy. Available measues are "PRED_ACC" (predictive accuracy), "FMEASURE" (F measure), "GEN_FMEASURE" (generalised F-Measure according to Fanizzi and d'Amato). +type: AccMethodTwoValued required: false -default value: predacc -conf file usage: lp.accuracyMethod = "predacc" +default value: PRED_ACC +conf file usage: lp.accuracyMethod = "PRED_ACC" -option name: approxDelta -description: The Approximate Delta +option name: accuracyPenalty +description: penalty for pos/neg examples which are classified as neutral +type: double +required: false +default value: 1.0 +conf file usage: lp.accuracyPenalty = 1.0 + +option name: errorPenalty +description: penalty for pos. examples classified as negative or vice versa +type: double +required: false +default value: 3.0 +conf file usage: lp.errorPenalty = 3.0 + +option name: negativeExamples +description: list of negative examples +type: Set +required: true +default value: +conf file usage: lp.negativeExamples = {} + +option name: penaliseNeutralExamples +description: if set to true neutral examples are penalised +type: boolean +required: false +default value: +conf file usage: lp.penaliseNeutralExamples = + +option name: percentPerLengthUnit +description: Percent Per Length Unit type: double required: false default value: 0.05 -conf file usage: lp.approxDelta = 0.05 +conf file usage: lp.percentPerLengthUnit = 0.05 + +option name: positiveExamples +description: list of positive examples +type: Set +required: true +default value: +conf file usage: lp.positiveExamples = {} -option name: useApproximations -description: Use Approximations +option name: useRetrievalForClassification +description: "Specifies whether to use retrieval or instance checks for testing a concept. - NO LONGER FULLY SUPPORTED. type: boolean required: false default value: false -conf file usage: lp.useApproximations = false +conf file usage: lp.useRetrievalForClassification = false + +component: PosNegUndLP (org.dllearner.learningproblems.PosNegUndLP) v1.0 +======================================================================== +A learning problem with uncertain-membership instances + +conf file usage: lp.type = "posNegUndLP" + +option name: uncertainExamples +description: the uncertain examples +type: Set +required: true +default value: +conf file usage: lp.uncertainExamples = {} component: positive only learning problem (org.dllearner.learningproblems.PosOnlyLP) v0.6 ========================================================================================= conf file usage: lp.type = "posonlylp" +option name: positiveExamples +description: the positive examples +type: SortedSet +required: true +default value: +conf file usage: lp.positiveExamples = "" + +component: PropertyAxiomLearningProblem (org.dllearner.learningproblems.PropertyAxiomLearningProblem) v0.6 +========================================================================================================== + +conf file usage: lp.type = "palp" + +option name: reasoner +description: The reasoner component variable to use for this Learning Problem +type: AbstractReasonerComponent +required: false +default value: +conf file usage: lp.reasoner = "" + ********************* @@ -1484,12 +1896,47 @@ required: false default value: true conf file usage: reasoner.defaultNegation = true -option name: forAllRetrievalSemantics +option name: forallSemantics description: This option controls how to interpret the all quantifier in forall r.C. The standard option is to return all those which do not have an r-filler not in C. The domain semantics is to use those which are in the domain of r and do not have an r-filler not in C. The forallExists semantics is to use those which have at least one r-filler and do not have an r-filler not in C. type: ForallSemantics required: false default value: standard -conf file usage: reasoner.forAllRetrievalSemantics = "standard" +conf file usage: reasoner.forallSemantics = "standard" + +option name: precomputeClassHierarchy +description: if class hierarchy should be precomputed +type: boolean +required: false +default value: true +conf file usage: reasoner.precomputeClassHierarchy = true + +option name: precomputeDataPropertyHierarchy +description: no description available +type: boolean +required: false +default value: true +conf file usage: reasoner.precomputeDataPropertyHierarchy = true + +option name: precomputeObjectPropertyHierarchy +description: no description available +type: boolean +required: false +default value: true +conf file usage: reasoner.precomputeObjectPropertyHierarchy = true + +option name: sources +description: the underlying knowledge sources +type: Set +required: true +default value: +conf file usage: reasoner.sources = {} + +option name: useInstanceChecks +description: whether to use single instance checks +type: boolean +required: false +default value: false +conf file usage: reasoner.useInstanceChecks = false component: OWL API Reasoner (org.dllearner.reasoning.OWLAPIReasoner) v0.8 ========================================================================= @@ -1503,6 +1950,27 @@ required: false default value: null conf file usage: reasoner.owlLinkURL = "null" +option name: precomputeClassHierarchy +description: if class hierarchy should be precomputed +type: boolean +required: false +default value: true +conf file usage: reasoner.precomputeClassHierarchy = true + +option name: precomputeDataPropertyHierarchy +description: no description available +type: boolean +required: false +default value: true +conf file usage: reasoner.precomputeDataPropertyHierarchy = true + +option name: precomputeObjectPropertyHierarchy +description: no description available +type: boolean +required: false +default value: true +conf file usage: reasoner.precomputeObjectPropertyHierarchy = true + option name: reasonerImplementation description: specifies the used OWL API reasoner implementation type: ReasonerImplementation @@ -1510,6 +1978,13 @@ required: false default value: pellet conf file usage: reasoner.reasonerImplementation = "pellet" +option name: sources +description: the underlying knowledge sources +type: Set +required: true +default value: +conf file usage: reasoner.sources = {} + option name: useFallbackReasoner description: specifies whether to use a fallback reasoner if a reasoner call fails because it's not supported or results in a bug. (the fallback works only on the assertional level type: boolean @@ -1517,11 +1992,53 @@ required: false default value: false conf file usage: reasoner.useFallbackReasoner = false +option name: useInstanceChecks +description: whether to use single instance checks +type: boolean +required: false +default value: false +conf file usage: reasoner.useInstanceChecks = false + component: SPARQL Reasoner (org.dllearner.reasoning.SPARQLReasoner) v0.1 ======================================================================== conf file usage: reasoner.type = "spr" +option name: laxMode +description: Use alternative relaxed Sparql-queries for Classes and Individuals +type: boolean +required: false +default value: false +conf file usage: reasoner.laxMode = false + +option name: precomputeClassHierarchy +description: if class hierarchy should be precomputed +type: boolean +required: false +default value: true +conf file usage: reasoner.precomputeClassHierarchy = true + +option name: precomputeDataPropertyHierarchy +description: no description available +type: boolean +required: false +default value: true +conf file usage: reasoner.precomputeDataPropertyHierarchy = true + +option name: precomputeObjectPropertyHierarchy +description: no description available +type: boolean +required: false +default value: true +conf file usage: reasoner.precomputeObjectPropertyHierarchy = true + +option name: sources +description: the underlying knowledge sources +type: Set +required: true +default value: +conf file usage: reasoner.sources = {} + option name: useCache description: Whether to use a file-based cache type: boolean @@ -1529,6 +2046,27 @@ required: false default value: true conf file usage: reasoner.useCache = true +option name: useGenericSplitsCode +description: Whether to use the generic facet generation code, which requires downloading all instances and is thus not recommended +type: boolean +required: false +default value: false +conf file usage: reasoner.useGenericSplitsCode = false + +option name: useInstanceChecks +description: whether to use single instance checks +type: boolean +required: false +default value: false +conf file usage: reasoner.useInstanceChecks = false + +option name: useValueLists +description: Whether to use SPARQL1.1 Value Lists +type: boolean +required: false +default value: false +conf file usage: reasoner.useValueLists = false + ********************** @@ -1547,12 +2085,52 @@ required: false default value: 5 conf file usage: op.beam = 5 -option name: kindOperator +option name: lp +description: the learning problem instance to use +type: PosNegLP +required: false +default value: +conf file usage: op.lp = "" + +option name: reasoner +description: the reasoner instance to use +type: Reasoner +required: false +default value: +conf file usage: op.reasoner = "" + +option name: ro description: no description available type: int required: false default value: 1 -conf file usage: op.kindOperator = 1 +conf file usage: op.ro = 1 + +component: OperatorInverter (org.dllearner.refinementoperators.OperatorInverter) v0.1 +===================================================================================== + +conf file usage: op.type = "inv_op" + +option name: guaranteeLength +description: Whether inverse solutions must respect the desired max length +type: boolean +required: false +default value: true +conf file usage: op.guaranteeLength = true + +option name: operator +description: operator to invert +type: LengthLimitedRefinementOperator +required: true +default value: +conf file usage: op.operator = "" + +option name: useNegationNormalForm +description: whether to apply NNF +type: boolean +required: false +default value: true +conf file usage: op.useNegationNormalForm = true component: rho refinement operator (org.dllearner.refinementoperators.RhoDRDown) v0.8 ===================================================================================== @@ -1594,6 +2172,13 @@ required: false default value: true conf file usage: op.instanceBasedDisjoints = true +option name: maxNrOfSplits +description: the number of generated split intervals for numeric types +type: int +required: false +default value: 12 +conf file usage: op.maxNrOfSplits = 12 + option name: useAllConstructor description: support of universal restrictions (owl:allValuesFrom), e.g. ∀ r.C type: boolean @@ -1615,6 +2200,13 @@ required: false default value: true conf file usage: op.useCardinalityRestrictions = true +option name: useDataHasValueConstructor +description: whether to use hasValue on frequently occuring strings +type: boolean +required: false +default value: false +conf file usage: op.useDataHasValueConstructor = false + option name: useExistsConstructor description: support of existential restrictions (owl:someValuesFrom), e.g. ∃ r.C type: boolean @@ -1650,6 +2242,13 @@ required: false default value: true conf file usage: op.useNumericDatatypes = true +option name: useObjectValueNegation +description: whether to generate object complement while refining +type: boolean +required: false +default value: false +conf file usage: op.useObjectValueNegation = false + option name: useSomeOnly description: universal restrictions on a property r are only used when there is already a cardinality and/or existential restriction on r type: boolean @@ -1682,6 +2281,27 @@ component: OEHeuristicRuntime (org.dllearner.algorithms.celoe.OEHeuristicRuntime conf file usage: h.type = "celoe_heuristic" +option name: expansionPenaltyFactor +description: penalty for long descriptions (horizontal expansion) (strong by default) +type: double +required: false +default value: 0.1 +conf file usage: h.expansionPenaltyFactor = 0.1 + +option name: gainBonusFactor +description: bonus for being better than parent node +type: double +required: false +default value: 0.3 +conf file usage: h.gainBonusFactor = 0.3 + +option name: nodeRefinementPenalty +description: penalty if a node OWLClassExpression has very many refinements since exploring such a node is computationally very expensive +type: double +required: false +default value: 0.0001 +conf file usage: h.nodeRefinementPenalty = 0.0001 + option name: startNodeBonus description: no description available type: double @@ -1689,6 +2309,40 @@ required: false default value: 0.1 conf file usage: h.startNodeBonus = 0.1 +component: DisjunctiveHeuristic (org.dllearner.algorithms.el.DisjunctiveHeuristic) v0.1 +======================================================================================= + +conf file usage: h.type = "disjunctive_heuristic" + +component: Stable Heuristic (org.dllearner.algorithms.el.StableHeuristic) v0.1 +============================================================================== + +conf file usage: h.type = "el_heuristic" + +component: Flexible Heuristic (org.dllearner.algorithms.ocel.FlexibleHeuristic) v0.1 +==================================================================================== + +conf file usage: h.type = "flexheuristic" + +option name: nrOfNegativeExamples +description: the number of negative examples +type: int +required: false +default value: +conf file usage: h.nrOfNegativeExamples = + +option name: percentPerLengthUnit +description: score percent to deduct per expression length +type: double +required: true +default value: +conf file usage: h.percentPerLengthUnit = + +component: Lexicograpgic Heuristic (org.dllearner.algorithms.ocel.LexicographicHeuristic) v0.1 +============================================================================================== + +conf file usage: h.type = "lexheuristic" + component: multiple criteria heuristic (org.dllearner.algorithms.ocel.MultiHeuristic) v0.7 ========================================================================================== @@ -1747,6 +2401,123 @@ component: QueryTreeHeuristic (org.dllearner.algorithms.qtl.heuristics.QueryTree conf file usage: h.type = "qtree_heuristic_simple" + +************* +* AccMethod * +************* + +component: AMeasure (org.dllearner.learningproblems.AccMethodAMeasure) v0.1 +=========================================================================== + +conf file usage: accMethod.type = "ameasure" + +component: AMeasure Approximate (org.dllearner.learningproblems.AccMethodAMeasureApprox) v0.1 +============================================================================================= + +conf file usage: accMethod.type = "approx.ameasure" + +component: FMeasure (org.dllearner.learningproblems.AccMethodFMeasure) v0.0 +=========================================================================== + +conf file usage: accMethod.type = "fmeasure" + +component: FMeasure Approximate (org.dllearner.learningproblems.AccMethodFMeasureApprox) v0.0 +============================================================================================= + +conf file usage: accMethod.type = "approx.fmeasure" + +option name: approxDelta +description: The Approximate Delta +type: double +required: false +default value: 0.05 +conf file usage: accMethod.approxDelta = 0.05 + +component: Weighted FMeasure (org.dllearner.learningproblems.AccMethodFMeasureWeighted) v0.0 +============================================================================================ + +conf file usage: accMethod.type = "weighted.fmeasure" + +option name: balanced +description: balance the weights to relative set size +type: boolean +required: false +default value: false +conf file usage: accMethod.balanced = false + +option name: negWeight +description: weight on the negative examples +type: double +required: false +default value: 1 +conf file usage: accMethod.negWeight = 1 + +option name: posWeight +description: weight on the positive examples +type: double +required: false +default value: 1 +conf file usage: accMethod.posWeight = 1 + +component: Generalised FMeasure (org.dllearner.learningproblems.AccMethodGenFMeasure) v0.1 +========================================================================================== + +conf file usage: accMethod.type = "gen_fmeasure" + +component: Jaccard Coefficient (org.dllearner.learningproblems.AccMethodJaccard) v0.1 +===================================================================================== + +conf file usage: accMethod.type = "jaccard" + +component: Predictive Accuracy (org.dllearner.learningproblems.AccMethodPredAcc) v0.0 +===================================================================================== + +conf file usage: accMethod.type = "pred_acc" + +component: Predictive Accuracy Approximate (org.dllearner.learningproblems.AccMethodPredAccApprox) v0.0 +======================================================================================================= + +conf file usage: accMethod.type = "approx.prec_acc" + +option name: approxDelta +description: The Approximate Delta +type: double +required: false +default value: 0.05 +conf file usage: accMethod.approxDelta = 0.05 + +component: Predictive Accuracy without Weak elimination (org.dllearner.learningproblems.AccMethodPredAccOCEL) v0.0 +================================================================================================================== + +conf file usage: accMethod.type = "pred_acc.ocel" + +component: Weighted Predictive Accuracy (org.dllearner.learningproblems.AccMethodPredAccWeighted) v0.0 +====================================================================================================== + +conf file usage: accMethod.type = "weighted.pred_acc" + +option name: balanced +description: balance the weights to relative set size +type: boolean +required: false +default value: false +conf file usage: accMethod.balanced = false + +option name: negWeight +description: weight on the negative examples +type: double +required: false +default value: 1 +conf file usage: accMethod.negWeight = 1 + +option name: posWeight +description: weight on the positive examples +type: double +required: false +default value: 1 +conf file usage: accMethod.posWeight = 1 + + ******************** * Other Components * ******************** diff --git a/interfaces/doc/manual/manual.tex b/interfaces/doc/manual/manual.tex index 13b11d6778..64f6b22be4 100644 --- a/interfaces/doc/manual/manual.tex +++ b/interfaces/doc/manual/manual.tex @@ -98,7 +98,7 @@ \section{Getting Started} \begin{lstlisting} DL-Learner command line interface Initializing Component "OWL File"... OK (1ms) -Initializing Component "fast instance checker"... OK (325ms) +Initializing Component "closed world reasoner"... OK (325ms) Initializing Component "PosNegLPStandard"... OK (0ms) Initializing Component "CELOE"... OK (9ms) Running algorithm instance "alg" (CELOE) @@ -182,7 +182,7 @@ \subsection{Knowledge Sources} \subsection{Reasoner Components} Several reasoner components are implemented, which can be interfaces to concrete reasoner implementations. -%To select a component in a conf file, use \verb|reasoner=$value;|, where \verb|$value| is one of \verb|digReasoner|, \verb|fastInstanceChecker|, or \verb|owlAPIReasoner|, which are explained below. +%To select a component in a conf file, use \verb|reasoner=$value;|, where \verb|$value| is one of \verb|digReasoner|, \verb|closedWorldReasoner|, or \verb|owlAPIReasoner|, which are explained below. Note that OWLlink reasoners can be attached via the OWL API interface. \begin{description} @@ -191,7 +191,7 @@ \subsection{Reasoner Components} \item reasonerType: Selects the desired reasoner. By default, Pellet is used. Usage: \verb|owlAPIReasoner.reasonerType = fact|. Pellet, FaCT++ and HermiT are already included in DL-Learner. Note that for FaCT++, you need to add -Djava.library.path=lib/fact/64bit (or 32bit) to the Java command. You can also use an external OWLlink reasoner by setting the reasoner type to \verb|owllink|. You can then use the option \verb|owlLinkURL| to specify the URL of the OWLlink reasoner (http://localhost:8080/ by default). \end{itemize} \item[DIG] DIG 1.1\footnote{\dig} is an interface to description logic reasoners and supported by a large variety of reasoners including Pellet, FaCT++, KAON2, and Racer Pro. The major drawback is that the current version DIG 1.1 is not aligned with the OWL specification and therefore lacks several features, which are crucial to the more recent learning algorithms in DL-Learner. If you still want to use the DIG interface, you have to download a DIG capable reasoner and start the DIG server there. DL-Learner communicates with the reasoner using the XML based protocol over HTTP. In the latest versions of DL-Learner, DIG support is considered unsupported. You can use it at your own risk, but we will not maintain this part of the code and may remove it in the future. - \item[Fast Instance Checker] Instance checks, i.e.~testing whether an individual is instance of a class, is the major reasoner task in many learning algorithms. This reasoner is a self-development of the DL-Learner project. It remedies some problems related to Machine Learning and the Open World Assumption in OWL and therefore is not correct w.r.t.~OWL semantics. (See \cite{cheng00} Section 4 for an explanation.) Furthermore, it provides an improved performance for instance checks by precomputing some inferences and keeping them in memory. The fast instance checker is build on top of Pellet and the default reasoner component in DL-Learner. + \item[Closed World Reasoner] Instance checks, i.e.~testing whether an individual is instance of a class, is the major reasoner task in many learning algorithms. This reasoner is a self-development of the DL-Learner project. It remedies some problems related to Machine Learning and the Open World Assumption in OWL and therefore is not correct w.r.t.~OWL semantics. (See \cite{cheng00} Section 4 for an explanation.) Furthermore, it provides an improved performance for instance checks by precomputing some inferences and keeping them in memory. The closed world reasoner is build on top of any other reasoner component in DL-Learner. \end{description} \subsection{Learning Problems} @@ -593,7 +593,7 @@ \section{General Information} \item Tracker (bugs, features): \url{https://github.com/AKSW/DL-Learner/issues} \item Mailing Lists: \url{http://sourceforge.net/p/dl-learner/mailman/} \item Contact: lehmann@informatik.uni-leipzig.de (please use the mailing list if possible) - \item Latest Release: \url{https://github.com/AKSW/DL-Learner/releases/tag/v1.0} + \item Latest Release: \url{https://github.com/AKSW/DL-Learner/releases/tag/v1.2} \end{itemize} \nocite{*} diff --git a/interfaces/pom.xml b/interfaces/pom.xml index 3aa2667409..66e4df0637 100644 --- a/interfaces/pom.xml +++ b/interfaces/pom.xml @@ -13,7 +13,7 @@ org.dllearner dllearner-parent - 1.1 + 1.2 @@ -131,7 +131,7 @@ true - -Xms256m -Xmx1024m + -Xms256m -Xmx1024m -Dlog4j.configuration=file:@BASEDIR@/etc/log4j.properties false @@ -217,7 +217,7 @@ - + @@ -253,8 +253,7 @@ ${project.build.directory}/${dist-dir} /usr/share/dl-learner/lib ${install-dir}/lib - -Xms256m -Xmx1024m - false + -Xms256m -Xmx1024m -Dlog4j.configuration=file:@BASEDIR@/etc/log4j.properties / unix @@ -500,6 +499,7 @@ org/dllearner/test/junit/GeizhalsTest.java org/dllearner/cli/MoosiqueCLITest.java + -Dlog4j.configuration=log4j.properties diff --git a/interfaces/run_geizhals.sh b/interfaces/run_geizhals.sh deleted file mode 100755 index 313a522de3..0000000000 --- a/interfaces/run_geizhals.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/sh -# Loop forever -while : -do -mvn -Djetty.port=8082 jetty:run -done # Start over diff --git a/interfaces/src/main/assemble/archive.xml b/interfaces/src/main/assemble/archive.xml index e3ca98c4d9..28f62da45c 100644 --- a/interfaces/src/main/assemble/archive.xml +++ b/interfaces/src/main/assemble/archive.xml @@ -3,7 +3,6 @@ zip tar.gz - zip @@ -53,7 +52,7 @@ log4j.properties - conf + etc ${project.build.directory}/site diff --git a/interfaces/src/main/java/org/dllearner/cli/CLI.java b/interfaces/src/main/java/org/dllearner/cli/CLI.java index ad32b4b7e6..a3b7262e50 100644 --- a/interfaces/src/main/java/org/dllearner/cli/CLI.java +++ b/interfaces/src/main/java/org/dllearner/cli/CLI.java @@ -19,37 +19,19 @@ */ package org.dllearner.cli; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.PrintStream; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - import org.apache.commons.lang.exception.ExceptionUtils; import org.apache.log4j.Level; import org.apache.xmlbeans.XmlObject; import org.dllearner.algorithms.decisiontrees.dsttdt.DSTTDTClassifier; import org.dllearner.algorithms.decisiontrees.refinementoperators.DLTreesRefinementOperator; import org.dllearner.algorithms.decisiontrees.tdt.TDTClassifier; -//import org.dllearner.algorithms.qtl.QTL2; import org.dllearner.configuration.IConfiguration; import org.dllearner.configuration.spring.ApplicationContextBuilder; import org.dllearner.configuration.spring.DefaultApplicationContextBuilder; import org.dllearner.configuration.util.SpringConfigurationXMLBeanConverter; import org.dllearner.confparser.ConfParserConfiguration; import org.dllearner.confparser.ParseException; -import org.dllearner.core.AbstractCELA; -import org.dllearner.core.AbstractClassExpressionLearningProblem; -import org.dllearner.core.AbstractLearningProblem; -import org.dllearner.core.AbstractReasonerComponent; -import org.dllearner.core.ComponentAnn; -import org.dllearner.core.ComponentInitException; -import org.dllearner.core.KnowledgeSource; -import org.dllearner.core.LearningAlgorithm; -import org.dllearner.core.ReasoningMethodUnsupportedException; +import org.dllearner.core.*; import org.dllearner.core.config.ConfigOption; import org.dllearner.learningproblems.PosNegLP; import org.dllearner.reasoning.ClosedWorldReasoner; @@ -63,6 +45,17 @@ import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +//import org.dllearner.algorithms.qtl.QTL2; + /** @@ -74,6 +67,10 @@ */ @ComponentAnn(name = "Command Line Interface", version = 0, shortName = "") public class CLI { + static { + if (System.getProperty("log4j.configuration") == null) + System.setProperty("log4j.configuration", "log4j.properties"); + } private static Logger logger = LoggerFactory.getLogger(CLI.class); @@ -113,14 +110,15 @@ public CLI(File confFile) { // separate init methods, because some scripts may want to just get the application // context from a conf file without actually running it - public void init() throws IOException { + public void init() throws IOException { if(context == null) { + Resource confFileR = new FileSystemResource(confFile); List springConfigResources = new ArrayList(); configuration = new ConfParserConfiguration(confFileR); ApplicationContextBuilder builder = new DefaultApplicationContextBuilder(); - context = builder.buildApplicationContext(configuration,springConfigResources); + context = builder.buildApplicationContext(configuration,springConfigResources); knowledgeSource = context.getBean(KnowledgeSource.class); rs = getMainReasonerComponent(); @@ -152,8 +150,8 @@ public void run() throws IOException { logger.warn("Cannot write Spring configuration, because " + springFilename + " already exists."); } else { Files.createFile(springFile, xml.toString()); - } - } + } + } rs = getMainReasonerComponent(); @@ -163,8 +161,8 @@ public void run() throws IOException { PosNegLP lp = context.getBean(PosNegLP.class); // if(la instanceof QTL2){ -// //new SPARQLCrossValidation((QTL2Disjunctive) la,lp,rs,nrOfFolds,false); -// } +// //new SPARQLCrossValidation((QTL2Disjunctive) la,lp,rs,nrOfFolds,false); +// } if((la instanceof TDTClassifier)||(la instanceof DSTTDTClassifier) ){ //TODO: verify if the quality of the code can be improved @@ -183,7 +181,7 @@ public void run() throws IOException { ((DSTTDTClassifier)la).setOperator(op); new CrossValidation2(la,lp,rs,nrOfFolds,false); }else { - new CrossValidation2(la,lp,rs,nrOfFolds,false); + new CrossValidation2(la,lp,rs,nrOfFolds,false); } } else { if(context.getBean(AbstractLearningProblem.class) instanceof AbstractClassExpressionLearningProblem) { @@ -230,7 +228,7 @@ public boolean isWriteSpringConfiguration() { public void setWriteSpringConfiguration(boolean writeSpringConfiguration) { this.writeSpringConfiguration = writeSpringConfiguration; - } + } /** * @return the lp @@ -255,9 +253,9 @@ public AbstractCELA getLearningAlgorithm() { /** * @param args - * @throws ParseException - * @throws IOException - * @throws ReasoningMethodUnsupportedException + * @throws ParseException + * @throws IOException + * @throws ReasoningMethodUnsupportedException */ public static void main(String[] args) throws ParseException, IOException, ReasoningMethodUnsupportedException { @@ -274,7 +272,7 @@ public static void main(String[] args) throws ParseException, IOException, Reaso File file = new File(args[args.length - 1]); if(!file.exists()) { System.out.println("File \"" + file + "\" does not exist."); - System.exit(0); + System.exit(0); } Resource confFile = new FileSystemResource(file); @@ -308,14 +306,32 @@ public static void main(String[] args) throws ParseException, IOException, Reaso // Get the Root Error Message logger.error("An Error Has Occurred During Processing."); -// logger.error(primaryCause.getMessage()); + if (primaryCause != null) { + logger.error(primaryCause.getMessage()); + } logger.debug("Stack Trace: ", e); logger.error("Terminating DL-Learner...and writing stacktrace to: " + stacktraceFileName); + createIfNotExists(new File(stacktraceFileName)); + FileOutputStream fos = new FileOutputStream(stacktraceFileName); PrintStream ps = new PrintStream(fos); e.printStackTrace(ps); } + } + private static boolean createIfNotExists(File f) { + if (f.exists()) return true; + + File p = f.getParentFile(); + if (p != null && !p.exists()) p.mkdirs(); + + try { + f.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + return false; + } + return true; } /** diff --git a/interfaces/src/main/java/org/dllearner/cli/CrossValidation.java b/interfaces/src/main/java/org/dllearner/cli/CrossValidation.java index c6ac113c81..1ceed64929 100644 --- a/interfaces/src/main/java/org/dllearner/cli/CrossValidation.java +++ b/interfaces/src/main/java/org/dllearner/cli/CrossValidation.java @@ -37,20 +37,20 @@ import org.dllearner.core.AbstractClassExpressionLearningProblem; import org.dllearner.core.AbstractReasonerComponent; import org.dllearner.core.ComponentInitException; +import org.dllearner.core.StringRenderer; import org.dllearner.learningproblems.Heuristics; import org.dllearner.learningproblems.PosNegLP; import org.dllearner.learningproblems.PosOnlyLP; import org.dllearner.utilities.Files; import org.dllearner.utilities.Helper; -import org.dllearner.utilities.datastructures.Datastructures; +import org.dllearner.utilities.owl.ManchesterOWLSyntaxOWLObjectRendererImplExt; import org.dllearner.utilities.owl.OWLClassExpressionUtils; import org.dllearner.utilities.statistics.Stat; -import org.semanticweb.owlapi.io.ToStringRenderer; import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLIndividual; import org.semanticweb.owlapi.util.SimpleShortFormProvider; -import uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.ManchesterOWLSyntaxOWLObjectRendererImpl; +import com.google.common.collect.Sets; /** @@ -68,7 +68,7 @@ public class CrossValidation { protected Stat length = new Stat(); protected Stat accuracyTraining = new Stat(); protected Stat fMeasure = new Stat(); - protected Stat fMeasureTraining = new Stat(); + protected Stat fMeasureTraining = new Stat(); public static boolean writeToFile = false; public static File outputFile; public static boolean multiThreaded = false; @@ -79,7 +79,7 @@ public class CrossValidation { protected Stat testingCompletenessStat = new Stat(); protected Stat testingCorrectnessStat = new Stat(); - DecimalFormat df = new DecimalFormat(); + DecimalFormat df = new DecimalFormat(); @@ -87,11 +87,11 @@ public CrossValidation() { } - public CrossValidation(AbstractCELA la, AbstractClassExpressionLearningProblem lp, final AbstractReasonerComponent rs, int folds, boolean leaveOneOut) { + public CrossValidation(AbstractCELA la, AbstractClassExpressionLearningProblem lp, final AbstractReasonerComponent rs, int folds, boolean leaveOneOut) { //console rendering of class expressions - ManchesterOWLSyntaxOWLObjectRendererImpl renderer = new ManchesterOWLSyntaxOWLObjectRendererImpl(); - ToStringRenderer.getInstance().setRenderer(renderer); - ToStringRenderer.getInstance().setShortFormProvider(new SimpleShortFormProvider()); + ManchesterOWLSyntaxOWLObjectRendererImplExt renderer = new ManchesterOWLSyntaxOWLObjectRendererImplExt(); + StringRenderer.setRenderer(renderer); + StringRenderer.setShortFormProvider(new SimpleShortFormProvider()); // the training and test sets used later on List> trainingSetsPos = new LinkedList>(); @@ -113,7 +113,7 @@ public CrossValidation(AbstractCELA la, AbstractClassExpressionLearningProblem l } List posExamplesList = new LinkedList(posExamples); List negExamplesList = new LinkedList(negExamples); - Collections.shuffle(posExamplesList, new Random(1)); + Collections.shuffle(posExamplesList, new Random(1)); Collections.shuffle(negExamplesList, new Random(2)); // sanity check whether nr. of folds makes sense for this benchmark @@ -136,7 +136,7 @@ public CrossValidation(AbstractCELA la, AbstractClassExpressionLearningProblem l System.exit(1); } else { // calculating where to split the sets, ; note that we split - // positive and negative examples separately such that the + // positive and negative examples separately such that the // distribution of positive and negative examples remains similar // (note that there are better but more complex ways to implement this, // which guarantee that the sum of the elements of a fold for pos @@ -155,8 +155,8 @@ public CrossValidation(AbstractCELA la, AbstractClassExpressionLearningProblem l testSetsPos.add(i, testPos); testSetsNeg.add(i, testNeg); trainingSetsPos.add(i, getTrainingSet(posExamples, testPos)); - trainingSetsNeg.add(i, getTrainingSet(negExamples, testNeg)); - } + trainingSetsNeg.add(i, getTrainingSet(negExamples, testNeg)); + } } @@ -229,21 +229,21 @@ public void run() { outputWriter("Finished " + folds + "-folds cross-validation."); outputWriter("runtime: " + statOutput(df, runtime, "s")); outputWriter("length: " + statOutput(df, length, "")); - outputWriter("F-Measure on training set: " + statOutput(df, fMeasureTraining, "%")); + outputWriter("F-Measure on training set: " + statOutput(df, fMeasureTraining, "%")); outputWriter("F-Measure: " + statOutput(df, fMeasure, "%")); - outputWriter("predictive accuracy on training set: " + statOutput(df, accuracyTraining, "%")); + outputWriter("predictive accuracy on training set: " + statOutput(df, accuracyTraining, "%")); outputWriter("predictive accuracy: " + statOutput(df, accuracy, "%")); } private void validate(AbstractCELA la, AbstractClassExpressionLearningProblem lp, AbstractReasonerComponent rs, int currFold, Set trainPos, Set trainNeg, Set testPos, Set testNeg){ - Set pos = Datastructures.individualSetToStringSet(trainPos); - Set neg = Datastructures.individualSetToStringSet(trainNeg); + Set pos = Helper.getStringSet(trainPos); + Set neg = Helper.getStringSet(trainNeg); String output = ""; output += "+" + new TreeSet(pos) + "\n"; output += "-" + new TreeSet(neg) + "\n"; - try { + try { lp.init(); la.setLearningProblem(lp); la.init(); @@ -260,15 +260,15 @@ private void validate(AbstractCELA la, AbstractClassExpressionLearningProblem lp OWLClassExpression concept = la.getCurrentlyBestDescription(); Set tmp = rs.hasType(concept, testPos); - Set tmp2 = Helper.difference(testPos, tmp); + Set tmp2 = Sets.difference(testPos, tmp); Set tmp3 = rs.hasType(concept, testNeg); - // calculate training accuracies + // calculate training accuracies int trainingCorrectPosClassified = getCorrectPosClassified(rs, concept, trainPos); int trainingCorrectNegClassified = getCorrectNegClassified(rs, concept, trainNeg); int trainingCorrectExamples = trainingCorrectPosClassified + trainingCorrectNegClassified; double trainingAccuracy = 100*((double)trainingCorrectExamples/(trainPos.size()+ - trainNeg.size())); + trainNeg.size())); accuracyTraining.addNumber(trainingAccuracy); // calculate test accuracies int correctPosClassified = getCorrectPosClassified(rs, concept, testPos); @@ -287,7 +287,7 @@ private void validate(AbstractCELA la, AbstractClassExpressionLearningProblem lp double precision = correctPosClassified + negAsPos == 0 ? 0 : correctPosClassified / (double) (correctPosClassified + negAsPos); double recall = correctPosClassified / (double) testPos.size(); // System.out.println(precision);System.out.println(recall); - fMeasure.addNumber(100*Heuristics.getFScore(recall, precision)); + fMeasure.addNumber(100*Heuristics.getFScore(recall, precision)); length.addNumber(OWLClassExpressionUtils.getLength(concept)); @@ -296,7 +296,7 @@ private void validate(AbstractCELA la, AbstractClassExpressionLearningProblem lp output += "test set errors neg: " + tmp3 + "\n"; output += "fold " + currFold + ":" + "\n"; output += " training: " + pos.size() + " positive and " + neg.size() + " negative examples"; - output += " testing: " + correctPosClassified + "/" + testPos.size() + " correct positives, " + output += " testing: " + correctPosClassified + "/" + testPos.size() + " correct positives, " + correctNegClassified + "/" + testNeg.size() + " correct negatives" + "\n"; output += " concept: " + concept.toString().replace("\n", " ") + "\n"; output += " accuracy: " + df.format(currAccuracy) + "% (" + df.format(trainingAccuracy) + "% on training set)" + "\n"; @@ -333,7 +333,7 @@ public static Set getTestingSet(List examples, int } public static Set getTrainingSet(Set examples, Set testingSet) { - return Helper.difference(examples, testingSet); + return Sets.difference(examples, testingSet); } // takes nr. of examples and the nr. of folds for this examples; @@ -352,7 +352,7 @@ public static String statOutput(DecimalFormat df, Stat stat, String unit) { String str = "av. " + df.format(stat.getMean()) + unit; str += " (deviation " + df.format(stat.getStandardDeviation()) + unit + "; "; str += "min " + df.format(stat.getMin()) + unit + "; "; - str += "max " + df.format(stat.getMax()) + unit + ")"; + str += "max " + df.format(stat.getMax()) + unit + ")"; return str; } diff --git a/interfaces/src/main/java/org/dllearner/cli/CrossValidation2.java b/interfaces/src/main/java/org/dllearner/cli/CrossValidation2.java index 5191611859..2d9c722df4 100644 --- a/interfaces/src/main/java/org/dllearner/cli/CrossValidation2.java +++ b/interfaces/src/main/java/org/dllearner/cli/CrossValidation2.java @@ -12,43 +12,41 @@ import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; -import org.dllearner.learningproblems.PosNegUndLP; import org.dllearner.algorithms.decisiontrees.dsttdt.DSTTDTClassifier; import org.dllearner.algorithms.decisiontrees.tdt.TDTClassifier; -import org.dllearner.cli.*; import org.dllearner.core.AbstractCELA; import org.dllearner.core.AbstractClassExpressionLearningProblem; import org.dllearner.core.AbstractReasonerComponent; import org.dllearner.core.ComponentInitException; +import org.dllearner.core.StringRenderer; import org.dllearner.learningproblems.Heuristics; import org.dllearner.learningproblems.PosNegLP; +import org.dllearner.learningproblems.PosNegUndLP; import org.dllearner.learningproblems.PosOnlyLP; import org.dllearner.utilities.Helper; -import org.dllearner.utilities.datastructures.Datastructures; +import org.dllearner.utilities.owl.ManchesterOWLSyntaxOWLObjectRendererImplExt; import org.dllearner.utilities.owl.OWLClassExpressionUtils; import org.dllearner.utilities.statistics.Stat; -import org.semanticweb.owlapi.io.ToStringRenderer; import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLDataFactory; import org.semanticweb.owlapi.model.OWLIndividual; import org.semanticweb.owlapi.model.OWLObjectComplementOf; import org.semanticweb.owlapi.util.SimpleShortFormProvider; -import com.google.common.collect.Sets; - import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; -import uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.ManchesterOWLSyntaxOWLObjectRendererImpl; + +import com.google.common.collect.Sets; public class CrossValidation2 extends org.dllearner.cli.CrossValidation{ DecimalFormat df = new DecimalFormat(); - protected Stat commissionTraining = new Stat(); // commission, omission and induction rate for + protected Stat commissionTraining = new Stat(); // commission, omission and induction rate for protected Stat commission = new Stat(); protected Stat omissionTraining = new Stat(); protected Stat omission = new Stat(); protected Stat inductionTraining = new Stat(); - protected Stat induction = new Stat(); + protected Stat induction = new Stat(); public CrossValidation2() { @@ -58,9 +56,9 @@ public CrossValidation2() { public CrossValidation2(AbstractCELA la, AbstractClassExpressionLearningProblem lp, final AbstractReasonerComponent rs, int folds, boolean leaveOneOut) { super(); - ManchesterOWLSyntaxOWLObjectRendererImpl renderer = new ManchesterOWLSyntaxOWLObjectRendererImpl(); - ToStringRenderer.getInstance().setRenderer(renderer); - ToStringRenderer.getInstance().setShortFormProvider(new SimpleShortFormProvider()); + ManchesterOWLSyntaxOWLObjectRendererImplExt renderer = new ManchesterOWLSyntaxOWLObjectRendererImplExt(); + StringRenderer.setRenderer(renderer); + StringRenderer.setShortFormProvider(new SimpleShortFormProvider()); // the training and test sets used later on List> trainingSetsPos = new LinkedList>(); @@ -93,9 +91,9 @@ public CrossValidation2(AbstractCELA la, AbstractClassExpressionLearningProblem List negExamplesList = new LinkedList(negExamples); List undExamplesList = new LinkedList(undExamples); //System.out.println("Undefined membership: "+undExamples); - Collections.shuffle(posExamplesList, new Random(1)); + Collections.shuffle(posExamplesList, new Random(1)); Collections.shuffle(negExamplesList, new Random(2)); - if(lp instanceof PosNegUndLP){ + if(lp instanceof PosNegUndLP){ Collections.shuffle(undExamplesList, new Random(3)); } @@ -121,7 +119,7 @@ public CrossValidation2(AbstractCELA la, AbstractClassExpressionLearningProblem System.exit(1); } else { // calculating where to split the sets, ; note that we split - // positive and negative examples separately such that the + // positive and negative examples separately such that the // distribution of positive and negative examples remains similar // (note that there are better but more complex ways to implement this, // which guarantee that the sum of the elements of a fold for pos @@ -147,9 +145,9 @@ public CrossValidation2(AbstractCELA la, AbstractClassExpressionLearningProblem testSetsNeg.add(i, testNeg); testSetsUnd.add(i,testUnd); trainingSetsPos.add(i, getTrainingSet(posExamples, testPos)); - trainingSetsNeg.add(i, getTrainingSet(negExamples, testNeg)); + trainingSetsNeg.add(i, getTrainingSet(negExamples, testNeg)); trainingSetsUnd.add(i, getTrainingSet(undExamples, testUnd)); - } + } //System.out.println("Test set size: "+testSetsPos.size()); } @@ -172,7 +170,7 @@ public CrossValidation2(AbstractCELA la, AbstractClassExpressionLearningProblem if(lp instanceof PosNegLP){ ((PosNegLP)lpClone).setPositiveExamples(trainPos); ((PosNegLP)lpClone).setNegativeExamples(trainNeg); - if (lp instanceof PosNegUndLP){ + if (lp instanceof PosNegUndLP){ ((PosNegUndLP)lpClone).setUncertainExample(trainUnd); } } else if(lp instanceof PosOnlyLP){ @@ -213,7 +211,7 @@ public void run() { } catch (InterruptedException e) { e.printStackTrace(); } - } + } else { for(int currFold=0; currFold trainPos = trainingSetsPos.get(currFold); @@ -245,9 +243,9 @@ public void run() { outputWriter("Finished " + folds + "-folds cross-validation."); outputWriter("runtime: " + statOutput(df, runtime, "s")); outputWriter("length: " + statOutput(df, length, "")); - outputWriter("F-Measure on training set: " + statOutput(df, fMeasureTraining, "%")); + outputWriter("F-Measure on training set: " + statOutput(df, fMeasureTraining, "%")); outputWriter("F-Measure: " + statOutput(df, fMeasure, "%")); - outputWriter("Match rate on training set: " + statOutput(df, accuracyTraining, "%")); + outputWriter("Match rate on training set: " + statOutput(df, accuracyTraining, "%")); outputWriter("Match rate: " + statOutput(df, accuracy, "%")); outputWriter("Commission rate: " + statOutput(df, commission, "%")); outputWriter("Omission rate: " + statOutput(df, omission, "%")); @@ -258,9 +256,9 @@ public void run() { private void validate(AbstractCELA la, AbstractClassExpressionLearningProblem lp, AbstractReasonerComponent rs, int currFold, Set trainPos, Set trainNeg,Set trainUnd, Set testPos, Set testNeg, Set testUnd){ //System.out.println("Validation starting"); - Set pos = Datastructures.individualSetToStringSet(trainPos); - Set neg = Datastructures.individualSetToStringSet(trainNeg); - Set und = Datastructures.individualSetToStringSet(trainUnd); + Set pos = Helper.getStringSet(trainPos); + Set neg = Helper.getStringSet(trainNeg); + Set und = Helper.getStringSet(trainUnd); String output = ""; TreeSet treeSetPos = new TreeSet(pos); output += "+" + treeSetPos + "\n"; @@ -269,7 +267,7 @@ private void validate(AbstractCELA la, AbstractClassExpressionLearningProblem lp TreeSet treeSetUnd = new TreeSet(und); output += "?" + treeSetUnd + "\n"; //System.out.printf("Learning algoritm preparation: %d %d %d \n", treeSetPos.size(),treeSetNeg.size(),treeSetUnd.size()); - try { + try { lp.init(); la.setLearningProblem(lp); la.init(); @@ -287,7 +285,7 @@ private void validate(AbstractCELA la, AbstractClassExpressionLearningProblem lp int trainingCorrectPosClassified,trainingCorrectNegClassified,trainingCorrectUndClassified,trainingCorrectExamples; int trainingSize; double trainingAccuracy; - int negAsPosTraining; + int negAsPosTraining; int posAsNegTraining; int undAsPosTraining; @@ -331,19 +329,19 @@ private void validate(AbstractCELA la, AbstractClassExpressionLearningProblem lp double currAccuracy; - OWLClassExpression concept = la.getCurrentlyBestDescription(); + OWLClassExpression concept = la.getCurrentlyBestDescription(); int testsize = testPos.size()+ testNeg.size()+testUnd.size(); if(!(la instanceof DSTTDTClassifier)&& !(la instanceof TDTClassifier)){ //System.out.println("Training completed"); - // extract the current concept description + // extract the current concept description //System.out.println("Training completed"+ concept); Set tmp = rs.hasType(concept, testPos); - Set tmp2 = Helper.difference(testPos, tmp); + Set tmp2 = Sets.difference(testPos, tmp); Set tmp3 = rs.hasType(concept, testNeg); - // calculate training accuracies + // calculate training accuracies trainingCorrectPosClassified = getCorrectPosClassified(rs, concept, trainPos); trainingCorrectNegClassified = getCorrectNegClassified(rs, concept, trainNeg); @@ -351,13 +349,13 @@ private void validate(AbstractCELA la, AbstractClassExpressionLearningProblem lp trainingCorrectExamples = trainingCorrectUndClassified+trainingCorrectPosClassified + trainingCorrectNegClassified; trainingSize = trainPos.size()+ trainNeg.size()+trainUnd.size(); - trainingAccuracy = 100*((double)trainingCorrectExamples/trainingSize); + trainingAccuracy = 100*((double)trainingCorrectExamples/trainingSize); //System.out.println("Training Correct Examples: "+ trainingCorrectExamples+ " Size: "+trainingSize); accuracyTraining.addNumber(trainingAccuracy); //in a ternary setting this is the match rate - //compute training match (accuracy), commission omission and induction + //compute training match (accuracy), commission omission and induction OWLDataFactory factory= new OWLDataFactoryImpl(); //get a data factory for derive the complement concept description negAsPosTraining = rs.hasType(concept, trainNeg).size(); posAsNegTraining= rs.hasType(factory.getOWLObjectComplementOf(concept), trainPos).size(); // commission cases @@ -421,7 +419,7 @@ private void validate(AbstractCELA la, AbstractClassExpressionLearningProblem lp double recallTraining = trainingCorrectPosClassified / (double) trainPos.size(); System.out.println(precisionTraining +"----"+recallTraining); // System.exit(1); - fMeasureTraining.addNumber(100*Heuristics.getFScore(recallTraining, precisionTraining)); + fMeasureTraining.addNumber(100*Heuristics.getFScore(recallTraining, precisionTraining)); // calculate test F-Score @@ -449,7 +447,7 @@ private void validate(AbstractCELA la, AbstractClassExpressionLearningProblem lp trainingCorrectExamples = trainingCorrectUndClassified+trainingCorrectPosClassified + trainingCorrectNegClassified; trainingSize = trainPos.size()+ trainNeg.size()+trainUnd.size(); - trainingAccuracy = 100*((double)trainingCorrectExamples/trainingSize); + trainingAccuracy = 100*((double)trainingCorrectExamples/trainingSize); accuracyTraining.addNumber(trainingAccuracy); negAsPosTraining = getWrongClassification(trainNeg, la); @@ -462,9 +460,9 @@ private void validate(AbstractCELA la, AbstractClassExpressionLearningProblem lp negAsUndTraining= getOmittedClassification(-1, trainNeg, la); //System.out.println(); - //System.out.println("Training "+posAsUndTraining+ "-----------"+ negAsUndTraining); - undAsPosTraining = getInductionClassification(trainUnd, la); //positive and negative induction - undAsNegTraining= 0; //getInductionClassified(-1,0,trainUnd, tdt); + //System.out.println("Training "+posAsUndTraining+ "-----------"+ negAsUndTraining); + undAsPosTraining = getInductionClassification(trainUnd, la); //positive and negative induction + undAsNegTraining= 0; //getInductionClassified(-1,0,trainUnd, tdt); //System.out.println("Training"); commissions = negAsPosTraining+posAsNegTraining; @@ -484,7 +482,7 @@ private void validate(AbstractCELA la, AbstractClassExpressionLearningProblem lp inductionTraining.addNumber(trainingInduction); omissionTraining.addNumber(trainingOmission); - correctPosClassified = getCorrectClassifications(1, testPos, la); //getCorrectPosClassified(rs, concept, testPos); + correctPosClassified = getCorrectClassifications(1, testPos, la); //getCorrectPosClassified(rs, concept, testPos); correctNegClassified = getCorrectClassifications(-1, testNeg, la); correctUndClassified = getCorrectClassifications(0, testUnd, la); correctExamples = correctUndClassified+correctPosClassified + correctNegClassified; @@ -529,7 +527,7 @@ private void validate(AbstractCELA la, AbstractClassExpressionLearningProblem lp double recallTraining = trainingCorrectPosClassified / (double) trainPos.size(); System.out.println(precisionTraining +"----"+recallTraining); - fMeasureTraining.addNumber(100*Heuristics.getFScore(recallTraining, precisionTraining)); + fMeasureTraining.addNumber(100*Heuristics.getFScore(recallTraining, precisionTraining)); // calculate test F-Score @@ -550,7 +548,7 @@ private void validate(AbstractCELA la, AbstractClassExpressionLearningProblem lp //System.exit(0); output += "fold " + currFold + ":" + "\n"; output += " training: " + pos.size() + " positive, " + neg.size() + " negative examples and "+ und.size() + " uncertain examples"; - output += " testing: " + correctPosClassified + "/" + testPos.size() + " correct positives, " + output += " testing: " + correctPosClassified + "/" + testPos.size() + " correct positives, " + correctNegClassified + "/" + testNeg.size() + " correct negatives " + correctUndClassified+"/"+ testUnd.size()+" correct uncertain \n"; output += " concept: " + concept.toString().replace("\n", " ") + "\n"; output += " match: " + df.format(currAccuracy) + "% (" + df.format(trainingAccuracy) + "% on training set)" + "\n"; @@ -570,7 +568,7 @@ private int getCorrectClassifications( int groundtruth, Set set, int trainingCorrectClassified=0; for (OWLIndividual indTestEx: set){ int label =0; - if (la instanceof DSTTDTClassifier) + if (la instanceof DSTTDTClassifier) label=((DSTTDTClassifier)la).classifyExamplesDST(indTestEx, ((DSTTDTClassifier)la).getCurrentmodel()); else if (la instanceof TDTClassifier){ label= ((TDTClassifier)la).classifyExample(indTestEx, ((TDTClassifier)la).getCurrentmodel());} @@ -579,7 +577,7 @@ else if (la instanceof TDTClassifier){ // // } // System.out.println(" GetCorrectClassified Label: "+label); - if (label == groundtruth) { + if (label == groundtruth) { //System.out.println("\t Ground truth "+label+" Predicted "+ groundtruth+ ": matched"); trainingCorrectClassified++; @@ -608,7 +606,7 @@ else if (la instanceof TDTClassifier){ // System.out.println(" GetWrongClassified neg aS POS Label"+label); //System.out.println("\t Ground truth +1 Predicted "+ label); //System.out.println(((label==+1))); - if ((label==+1)) { + if ((label==+1)) { trainingWrongClassified++; } @@ -637,7 +635,7 @@ else if (la instanceof TDTClassifier){ //tdt.classifyExamplesDST(indTestEx, tdt.getCurrentmodel()); //System.out.println("label: "+label +" groundtruth +1"); //System.out.println(((label!=-1) && (label!=0))); - if ((label==-1)) { + if ((label==-1)) { // System.out.println("POS s neg label: "+label); //System.out.println("\t Ground truth "+groundtruth+" Predicted "+ label+ ":committed"); trainingWrongClassified++; @@ -677,7 +675,7 @@ private int getInductionClassification(Set set, AbstractCELA la) int trainingWrongClassified=0; for (OWLIndividual indTestEx: set){ int label = 0; - if (la instanceof DSTTDTClassifier) + if (la instanceof DSTTDTClassifier) label=((DSTTDTClassifier)la).classifyExamplesDST(indTestEx, ((DSTTDTClassifier)la).getCurrentmodel()); else if (la instanceof TDTClassifier) label=((TDTClassifier)la).classifyExample(indTestEx, ((TDTClassifier)la).getCurrentmodel()); @@ -693,7 +691,7 @@ else if (la instanceof TDTClassifier) private int getCorrectUndClassified(AbstractReasonerComponent rs, OWLClassExpression concept, Set testUnd) { OWLDataFactory df = new OWLDataFactoryImpl(); - OWLObjectComplementOf complementOfConcept = df.getOWLObjectComplementOf(concept); + OWLObjectComplementOf complementOfConcept = df.getOWLObjectComplementOf(concept); int nOfUnc=0; for (OWLIndividual ind:testUnd){ if ((!rs.hasType(concept, ind)) && !(rs.hasType(complementOfConcept, ind))) @@ -705,7 +703,7 @@ private int getCorrectUndClassified(AbstractReasonerComponent rs, OWLClassExpres } - @Override + @Override public int getCorrectNegClassified(AbstractReasonerComponent rs, OWLClassExpression concept, Set testSetNeg){ // for dealing explictly with the Open World Assumption OWLDataFactory df = new OWLDataFactoryImpl(); diff --git a/interfaces/src/main/java/org/dllearner/cli/DocumentationGenerator.java b/interfaces/src/main/java/org/dllearner/cli/DocumentationGenerator.java index 26d293b4ad..5bfe30a7c6 100644 --- a/interfaces/src/main/java/org/dllearner/cli/DocumentationGenerator.java +++ b/interfaces/src/main/java/org/dllearner/cli/DocumentationGenerator.java @@ -19,36 +19,23 @@ */ package org.dllearner.cli; -import java.io.File; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; - +import com.google.common.base.CaseFormat; +import com.google.common.base.Strings; +import com.google.common.collect.Sets; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Level; import org.dllearner.cli.DocumentationGeneratorMeta.GlobalDoc; import org.dllearner.configuration.spring.editors.ConfigHelper; -import org.dllearner.core.AbstractReasonerComponent; -import org.dllearner.core.AnnComponentManager; -import org.dllearner.core.Component; -import org.dllearner.core.ComponentAnn; -import org.dllearner.core.KnowledgeSource; -import org.dllearner.core.LearningAlgorithm; -import org.dllearner.core.LearningProblem; +import org.dllearner.core.*; import org.dllearner.core.config.ConfigOption; import org.dllearner.utilities.Files; import org.reflections.Reflections; import org.semanticweb.owlapi.model.OWLClass; -import com.google.common.base.CaseFormat; -import com.google.common.base.Strings; -import com.google.common.collect.Sets; +import java.io.File; +import java.lang.reflect.Field; +import java.util.*; +import java.util.Map.Entry; /** * @@ -56,8 +43,9 @@ * */ public class DocumentationGenerator { - static { + if (System.getProperty("log4j.configuration") == null) + System.setProperty("log4j.configuration", "log4j.properties"); AnnComponentManager.setReflectionScanner(new Reflections()); org.apache.log4j.Logger.getLogger(AnnComponentManager.class).setLevel(Level.DEBUG); } @@ -207,9 +195,10 @@ private String getComponentConfigString( private void optionsTable(StringBuilder sb, Class component, String catString) { // generate table for configuration options - Map> options = ConfigHelper.getConfigOptionTypes(component); - for(Entry> entry : options.entrySet()) { - ConfigOption option = entry.getKey(); + Map> options = ConfigHelper.getConfigOptionTypes(component); + for(Entry> entry : options.entrySet()) { + String optionName = AnnComponentManager.getName(entry.getKey()); + ConfigOption option = entry.getKey().getAnnotation(ConfigOption.class); String type = entry.getValue().getSimpleName(); if(entry.getValue().equals(OWLClass.class)) { type = "IRI"; @@ -231,12 +220,12 @@ private void optionsTable(StringBuilder sb, Class component, String catString exampleValue = "[]"; } - sb.append("option name: " + option.name() + "\n" + sb.append("option name: " + optionName + "\n" + "description: " + option.description() + "\n" + "type: " + type + "\n" + "required: " + option.required() + "\n" + "default value: " + option.defaultValue() + "\n" - + "conf file usage: " + (catString.isEmpty()? "" : catString + ".") + option.name() + " = " + exampleValue +"\n"); + + "conf file usage: " + (catString.isEmpty()? "" : catString + ".") + optionName + " = " + exampleValue +"\n"); sb.append("\n"); } diff --git a/interfaces/src/main/java/org/dllearner/cli/DocumentationHTMLGenerator.java b/interfaces/src/main/java/org/dllearner/cli/DocumentationHTMLGenerator.java index 788b43da20..5c0463d488 100644 --- a/interfaces/src/main/java/org/dllearner/cli/DocumentationHTMLGenerator.java +++ b/interfaces/src/main/java/org/dllearner/cli/DocumentationHTMLGenerator.java @@ -1,13 +1,7 @@ package org.dllearner.cli; -import java.io.File; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.TreeMap; - import org.apache.commons.collections.bidimap.DualHashBidiMap; -import org.apache.commons.collections15.BidiMap; +import org.apache.commons.lang3.mutable.MutableInt; import org.dllearner.cli.DocumentationGeneratorMeta.GlobalDoc; import org.dllearner.configuration.spring.editors.ConfigHelper; import org.dllearner.core.AnnComponentManager; @@ -16,15 +10,24 @@ import org.dllearner.utilities.Files; import org.semanticweb.owlapi.model.OWLClass; +import java.io.File; +import java.lang.reflect.Field; +import java.util.*; +import java.util.Map.Entry; + /** * Script for generating documentation for all components, in particular - * their configuration options, in HTML format. The script is based on + * their configuration options, in HTML format. The script is based on * the new (as of 2011) annotation based component design. * * @author Jens Lehmann * */ public class DocumentationHTMLGenerator { + static { + if (System.getProperty("log4j.configuration") == null) + System.setProperty("log4j.configuration", "log4j.properties"); + } private AnnComponentManager cm; @@ -44,7 +47,7 @@ public void writeConfigDocumentation(File file) { for(Entry, String> entry : componentNames.entrySet()) { componentNamesInv.put(entry.getValue(), entry.getKey()); } - + StringBuffer sb = new StringBuffer(); sb.append(getHeader()); @@ -52,17 +55,36 @@ public void writeConfigDocumentation(File file) { sb.append("

DL-Learner Components

\n"); // filter interface + Map> compTree = new TreeMap<>(new Comparator() { + @Override + public int compare(Class o1, Class o2) { + return o1.getName().compareTo(o2.getName()); + } + }); + Map compSublevel = new HashMap<>(); + for (Class coreClazz : AnnComponentManager.coreComponentClasses) { + compSublevel.put(coreClazz, new MutableInt(0)); + } + for (Class coreClazz : AnnComponentManager.coreComponentClasses) { + compTree.put(coreClazz, new ArrayList()); + for (Class subClazz : AnnComponentManager.coreComponentClasses) { + if (subClazz.equals(coreClazz)) { + continue; + } + try { + subClazz.asSubclass(coreClazz); + compTree.get(coreClazz).add(subClazz); + compSublevel.get(subClazz).increment(); + } catch (ClassCastException e) { + // no subclass + } + } + } + sb.append("

Click on the following items to filter the listing below by implemented interfaces (requires Javascript):

\n"); sb.append("show all"); @@ -104,20 +126,37 @@ public void writeConfigDocumentation(File file) { Files.createFile(file, sb.toString()); } + private void printFilter(StringBuffer sb, Map> compTree, Map compSublevel, List filter, int level) { + for (Class e : filter) { + if (level != compSublevel.get(e).intValue()) { + continue; + } + sb.append("
  • " + e.getSimpleName() + ""); + List subClazzes = compTree.get(e); + if (!subClazzes.isEmpty()) { + sb.append("
      "); + printFilter(sb, compTree, compSublevel, compTree.get(e), level + 1); + sb.append("
    "); + } + sb.append("
  • "); + } + } + private void optionsTable(StringBuffer sb, Class comp) { // generate table for configuration options - Map> options = ConfigHelper.getConfigOptionTypes(comp); + Map> options = ConfigHelper.getConfigOptionTypes(comp); if(options.isEmpty()) { sb.append("This component does not have configuration options."); } else { sb.append("
    \n"); - for(Entry> entry : options.entrySet()) { - ConfigOption option = entry.getKey(); + for(Entry> entry : options.entrySet()) { + String optionName = AnnComponentManager.getName(entry.getKey()); + ConfigOption option = entry.getKey().getAnnotation(ConfigOption.class); String type = entry.getValue().getSimpleName(); if(entry.getValue().equals(OWLClass.class)) { type = "IRI"; } - sb.append("
    option namedescriptiontypedefault valuerequired?
    " + option.name() + "" + option.description() + sb.append("
    " + optionName + "" + option.description() + (option.exampleValue().length() > 0 ? (" Example: " + option.exampleValue()) : "") + " " + type + "" + option.defaultValue() + " " @@ -125,7 +164,7 @@ private void optionsTable(StringBuffer sb, Class comp) { } sb.append("
    \n"); } - } + } private String getHeader() { StringBuffer sb = new StringBuffer(); @@ -147,13 +186,13 @@ private String getHeader() { sb.append("\n"); sb.append(""); sb.append("\n"); + sb.append("\n"); sb.append("
    \n"); return sb.toString(); } @@ -162,12 +201,12 @@ private String getFooter() { return "
    "; } - // this is a hack, because we just assume that every PropertyEditor is named - // as TypeEditor (e.g. ObjectPropertyEditor); however that hack does not too much harm here + // this is a hack, because we just assume that every PropertyEditor is named + // as TypeEditor (e.g. OWLObjectPropertyEditor); however that hack does not too much harm here // private static String getOptionType(ConfigOption option) { // String name = option.propertyEditorClass().getSimpleName(); // return name.substring(0, name.length()-6); -// } +// } private static String getCoreTypes(Class comp) { if (Component.class.isAssignableFrom(comp)) { diff --git a/interfaces/src/main/java/org/dllearner/cli/Enrichment.java b/interfaces/src/main/java/org/dllearner/cli/Enrichment.java index 896667d2ab..b641cf16f2 100644 --- a/interfaces/src/main/java/org/dllearner/cli/Enrichment.java +++ b/interfaces/src/main/java/org/dllearner/cli/Enrichment.java @@ -19,91 +19,36 @@ */ package org.dllearner.cli; -import static java.util.Arrays.asList; - -import java.io.BufferedOutputStream; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintStream; -import java.io.UnsupportedEncodingException; -import java.lang.reflect.InvocationTargetException; -import java.math.BigInteger; -import java.net.Authenticator; -import java.net.MalformedURLException; -import java.net.PasswordAuthentication; -import java.net.SocketTimeoutException; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; -import java.net.URLEncoder; -import java.security.SecureRandom; -import java.text.DecimalFormat; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.SortedSet; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; - +import com.clarkparsia.owlapiv3.XSD; +import com.google.common.collect.Sets; +import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.rdf.model.*; +import com.hp.hpl.jena.sparql.engine.http.QueryExceptionHTTP; +import com.hp.hpl.jena.vocabulary.OWL; +import com.hp.hpl.jena.vocabulary.RDF; +import com.hp.hpl.jena.vocabulary.RDFS; import joptsimple.OptionException; import joptsimple.OptionParser; import joptsimple.OptionSet; import joptsimple.OptionSpec; - import org.apache.jena.riot.checker.CheckerLiterals; import org.apache.jena.riot.system.ErrorHandlerFactory; import org.apache.log4j.ConsoleAppender; import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.apache.log4j.SimpleLayout; -import org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntaxOntologyFormat; -import org.coode.owlapi.turtle.TurtleOntologyFormat; import org.dllearner.algorithms.celoe.CELOE; import org.dllearner.algorithms.properties.AxiomAlgorithms; import org.dllearner.algorithms.properties.MultiPropertyAxiomLearner; import org.dllearner.configuration.spring.editors.ConfigHelper; -import org.dllearner.core.AbstractAxiomLearningAlgorithm; -import org.dllearner.core.AbstractReasonerComponent; -import org.dllearner.core.AnnComponentManager; -import org.dllearner.core.AxiomLearningAlgorithm; -import org.dllearner.core.AxiomLearningProgressMonitor; -import org.dllearner.core.ComponentAnn; -import org.dllearner.core.ComponentInitException; -import org.dllearner.core.ConsoleAxiomLearningProgressMonitor; -import org.dllearner.core.EvaluatedAxiom; -import org.dllearner.core.EvaluatedDescription; -import org.dllearner.core.KnowledgeSource; -import org.dllearner.core.LearningAlgorithm; -import org.dllearner.core.LearningProblemUnsupportedException; -import org.dllearner.core.Score; -import org.dllearner.core.config.ConfigOption; +import org.dllearner.core.*; import org.dllearner.kb.LocalModelBasedSparqlEndpointKS; import org.dllearner.kb.OWLAPIOntology; import org.dllearner.kb.SparqlEndpointKS; -import org.dllearner.kb.sparql.ConciseBoundedDescriptionGenerator; -import org.dllearner.kb.sparql.ConciseBoundedDescriptionGeneratorImpl; -import org.dllearner.kb.sparql.SPARQLTasks; -import org.dllearner.kb.sparql.SparqlEndpoint; -import org.dllearner.kb.sparql.SparqlQuery; +import org.dllearner.kb.sparql.*; +import org.dllearner.learningproblems.AccMethodFMeasure; import org.dllearner.learningproblems.AxiomScore; import org.dllearner.learningproblems.ClassLearningProblem; -import org.dllearner.learningproblems.Heuristics.HeuristicType; import org.dllearner.reasoning.ClosedWorldReasoner; import org.dllearner.reasoning.SPARQLReasoner; import org.dllearner.utilities.EnrichmentVocabulary; @@ -114,45 +59,26 @@ import org.dllearner.utilities.owl.OWLAPIRenderers; import org.dllearner.utilities.owl.OWLEntityTypeAdder; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.io.RDFXMLOntologyFormat; +import org.semanticweb.owlapi.formats.ManchesterSyntaxDocumentFormat; +import org.semanticweb.owlapi.formats.RDFXMLDocumentFormat; +import org.semanticweb.owlapi.formats.TurtleDocumentFormat; import org.semanticweb.owlapi.io.SystemOutDocumentTarget; -import org.semanticweb.owlapi.model.AxiomType; -import org.semanticweb.owlapi.model.EntityType; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAnnotation; -import org.semanticweb.owlapi.model.OWLAnnotationProperty; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLDataProperty; -import org.semanticweb.owlapi.model.OWLEntity; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLNamedIndividual; -import org.semanticweb.owlapi.model.OWLObjectProperty; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyCreationException; -import org.semanticweb.owlapi.model.OWLOntologyManager; -import org.semanticweb.owlapi.model.OWLOntologyStorageException; -import org.semanticweb.owlapi.model.OWLProperty; +import org.semanticweb.owlapi.model.*; import org.semanticweb.owlapi.vocab.OWLRDFVocabulary; - import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; -import com.clarkparsia.owlapiv3.XSD; -import com.google.common.collect.Sets; -import com.hp.hpl.jena.query.ResultSet; -import com.hp.hpl.jena.rdf.model.Literal; -import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.ModelFactory; -import com.hp.hpl.jena.rdf.model.Property; -import com.hp.hpl.jena.rdf.model.RDFNode; -import com.hp.hpl.jena.rdf.model.Resource; -import com.hp.hpl.jena.rdf.model.Statement; -import com.hp.hpl.jena.rdf.model.StmtIterator; -import com.hp.hpl.jena.sparql.engine.http.QueryExceptionHTTP; -import com.hp.hpl.jena.vocabulary.OWL; -import com.hp.hpl.jena.vocabulary.RDF; -import com.hp.hpl.jena.vocabulary.RDFS; +import java.io.*; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.math.BigInteger; +import java.net.*; +import java.security.SecureRandom; +import java.text.DecimalFormat; +import java.util.*; +import java.util.Map.Entry; +import java.util.concurrent.*; + +import static java.util.Arrays.asList; /** * Command Line Interface for Enrichment. @@ -169,9 +95,9 @@ protected class AlgorithmRun { // since otherwise we run into memory problems for full enrichment private Class algorithm; private List> axioms; - private Map parameters; + private Map parameters; - public AlgorithmRun(Class algorithm, List> axioms, Map parameters) { + public AlgorithmRun(Class algorithm, List> axioms, Map parameters) { this.algorithm = algorithm; this.axioms = axioms; this.parameters = parameters; @@ -185,7 +111,7 @@ public List> getAxioms() { return axioms; } - public Map getParameters() { + public Map getParameters() { return parameters; } } @@ -246,7 +172,7 @@ public Map getParameters() { AxiomLearningProgressMonitor progressMonitor = new ConsoleAxiomLearningProgressMonitor(); - private OWLDataFactory dataFactory = new OWLDataFactoryImpl(false, false); + private OWLDataFactory dataFactory = new OWLDataFactoryImpl(); public Enrichment(SparqlEndpoint se, OWLEntity resource, double threshold, int nrOfAxiomsToLearn, boolean useInference, boolean verbose, int chunksize, int maxExecutionTimeInSeconds, @@ -525,7 +451,7 @@ private List> applyCELOE(SparqlEndpointKS ks, OWLClass } ksFragment = new OWLAPIOntology(ontology); try { - OWLManager.createOWLOntologyManager().saveOntology(ontology, new TurtleOntologyFormat(), new FileOutputStream("/tmp/test.ttl")); + OWLManager.createOWLOntologyManager().saveOntology(ontology, new TurtleDocumentFormat(), new FileOutputStream("/tmp/test.ttl")); } catch (OWLOntologyStorageException | FileNotFoundException e) { e.printStackTrace(); } @@ -545,8 +471,7 @@ private List> applyCELOE(SparqlEndpointKS ks, OWLClass ClassLearningProblem lp = new ClassLearningProblem(rc); lp.setClassToDescribe(nc); lp.setEquivalence(equivalence); - lp.setHeuristic(HeuristicType.FMEASURE); - lp.setUseApproximations(false); + lp.setAccuracyMethod(new AccMethodFMeasure(true)); lp.setMaxExecutionTimeInSeconds(10); lp.init(); @@ -626,6 +551,7 @@ private List> applyLearningAlgorithm(Class axiom) { /* * Generates list of OWL axioms. */ - List toRDF(List> evalAxioms, Class algorithm, Map parameters, SparqlEndpointKS ks){ + List toRDF(List> evalAxioms, Class algorithm, Map parameters, SparqlEndpointKS ks){ return toRDF(evalAxioms, algorithm, parameters, ks, null); } - private List toRDF(List> evalAxioms, Class algorithm, Map parameters, SparqlEndpointKS ks, String defaultNamespace){ + private List toRDF(List> evalAxioms, Class algorithm, Map parameters, SparqlEndpointKS ks, String defaultNamespace){ if(defaultNamespace == null || defaultNamespace.isEmpty()){ defaultNamespace = DEFAULT_NS; } @@ -714,7 +640,7 @@ private List toRDF(List> evalAxioms, Class toRDF(List> evalAxioms, Class entry : parameters.entrySet()){ + for(Entry entry : parameters.entrySet()){ paramInd = f.getOWLNamedIndividual(IRI.create(generateId())); ax = f.getOWLClassAssertionAxiom(EnrichmentVocabulary.Parameter, paramInd); axioms.add(ax); - ax = f.getOWLDataPropertyAssertionAxiom(EnrichmentVocabulary.parameterName, paramInd, entry.getKey().name()); + ax = f.getOWLDataPropertyAssertionAxiom(EnrichmentVocabulary.parameterName, paramInd, AnnComponentManager.getName(entry.getKey())); axioms.add(ax); ax = f.getOWLDataPropertyAssertionAxiom(EnrichmentVocabulary.parameterValue, paramInd, entry.getValue().toString()); axioms.add(ax); @@ -746,13 +672,7 @@ private List toRDF(List> evalAxioms, Class axioms, String defaultNames try { System.out.println("ENRICHMENT["); - ManchesterOWLSyntaxOntologyFormat manSyntaxFormat = new ManchesterOWLSyntaxOntologyFormat(); + ManchesterSyntaxDocumentFormat manSyntaxFormat = new ManchesterSyntaxDocumentFormat(); manSyntaxFormat.setDefaultPrefix(defaultNamespace); manSyntaxFormat.setPrefix("enrichment", "http://www.dl-learner.org/enrichment.owl#"); @@ -1187,7 +1107,7 @@ protected PasswordAuthentication getPasswordAuthentication() { // map resource to correct type OWLEntity resource = null; if(options.valueOf("resource") != null) { - resource = new SPARQLTasks(((SparqlEndpointKS)ks).getEndpoint()).guessResourceType(resourceURI.toString(), true); + resource = new SPARQLTasks(ks.getEndpoint()).guessResourceType(resourceURI.toString(), true); if(resource == null) { throw new IllegalArgumentException("Could not determine the type (class, object property or data property) of input resource " + options.valueOf("resource") + ". Enrichment only works for classes and properties."); @@ -1274,7 +1194,7 @@ protected PasswordAuthentication getPasswordAuthentication() { try { OWLOntology ontology = e.getGeneratedOntology(options.has("a")); OutputStream os = new BufferedOutputStream(new FileOutputStream(file)); - OWLManager.createOWLOntologyManager().saveOntology(ontology, new RDFXMLOntologyFormat(), os); + OWLManager.createOWLOntologyManager().saveOntology(ontology, new RDFXMLDocumentFormat(), os); } catch (OWLOntologyStorageException e1) { throw new Error("Could not save ontology."); } diff --git a/interfaces/src/main/java/org/dllearner/cli/SPARQLCrossValidation.java b/interfaces/src/main/java/org/dllearner/cli/SPARQLCrossValidation.java index 7f463ca593..379a5cad1c 100644 --- a/interfaces/src/main/java/org/dllearner/cli/SPARQLCrossValidation.java +++ b/interfaces/src/main/java/org/dllearner/cli/SPARQLCrossValidation.java @@ -30,7 +30,6 @@ import java.util.TreeSet; import org.dllearner.algorithms.qtl.QTL2Disjunctive; -import org.dllearner.algorithms.qtl.datastructures.QueryTree; import org.dllearner.algorithms.qtl.datastructures.impl.QueryTreeImpl.LiteralNodeSubsumptionStrategy; import org.dllearner.algorithms.qtl.datastructures.impl.RDFResourceTree; import org.dllearner.core.AbstractClassExpressionLearningProblem; @@ -42,12 +41,13 @@ import org.dllearner.reasoning.SPARQLReasoner; import org.dllearner.utilities.Files; import org.dllearner.utilities.Helper; -import org.dllearner.utilities.datastructures.Datastructures; import org.dllearner.utilities.owl.OWLClassExpressionUtils; import org.dllearner.utilities.statistics.Stat; import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLIndividual; +import com.google.common.collect.Sets; + /** * Performs cross validation for the given problem. Supports * k-fold cross-validation and leave-one-out cross-validation. @@ -63,7 +63,7 @@ public class SPARQLCrossValidation { protected Stat length = new Stat(); protected Stat accuracyTraining = new Stat(); protected Stat fMeasure = new Stat(); - protected Stat fMeasureTraining = new Stat(); + protected Stat fMeasureTraining = new Stat(); protected static boolean writeToFile = false; protected static File outputFile; @@ -80,9 +80,9 @@ public SPARQLCrossValidation() { } - public SPARQLCrossValidation(QTL2Disjunctive la, AbstractClassExpressionLearningProblem lp, IndividualReasoner rs, int folds, boolean leaveOneOut) { + public SPARQLCrossValidation(QTL2Disjunctive la, AbstractClassExpressionLearningProblem lp, IndividualReasoner rs, int folds, boolean leaveOneOut) { - DecimalFormat df = new DecimalFormat(); + DecimalFormat df = new DecimalFormat(); // the training and test sets used later on List> trainingSetsPos = new LinkedList>(); @@ -104,7 +104,7 @@ public SPARQLCrossValidation(QTL2Disjunctive la, AbstractClassExpressionLearning } List posExamplesList = new LinkedList(posExamples); List negExamplesList = new LinkedList(negExamples); - Collections.shuffle(posExamplesList, new Random(1)); + Collections.shuffle(posExamplesList, new Random(1)); Collections.shuffle(negExamplesList, new Random(2)); // sanity check whether nr. of folds makes sense for this benchmark @@ -127,7 +127,7 @@ public SPARQLCrossValidation(QTL2Disjunctive la, AbstractClassExpressionLearning System.exit(1); } else { // calculating where to split the sets, ; note that we split - // positive and negative examples separately such that the + // positive and negative examples separately such that the // distribution of positive and negative examples remains similar // (note that there are better but more complex ways to implement this, // which guarantee that the sum of the elements of a fold for pos @@ -146,16 +146,16 @@ public SPARQLCrossValidation(QTL2Disjunctive la, AbstractClassExpressionLearning testSetsPos.add(i, testPos); testSetsNeg.add(i, testNeg); trainingSetsPos.add(i, getTrainingSet(posExamples, testPos)); - trainingSetsNeg.add(i, getTrainingSet(negExamples, testNeg)); - } + trainingSetsNeg.add(i, getTrainingSet(negExamples, testNeg)); + } } // run the algorithm for(int currFold=0; currFold pos = Datastructures.individualSetToStringSet(trainingSetsPos.get(currFold)); - Set neg = Datastructures.individualSetToStringSet(trainingSetsNeg.get(currFold)); + Set pos = Helper.getStringSet(trainingSetsPos.get(currFold)); + Set neg = Helper.getStringSet(trainingSetsNeg.get(currFold)); if(lp instanceof PosNegLP){ ((PosNegLP)lp).setPositiveExamples(trainingSetsPos.get(currFold)); ((PosNegLP)lp).setNegativeExamples(trainingSetsNeg.get(currFold)); @@ -164,7 +164,7 @@ public SPARQLCrossValidation(QTL2Disjunctive la, AbstractClassExpressionLearning } - try { + try { lp.init(); la.init(); } catch (ComponentInitException e) { @@ -181,14 +181,14 @@ public SPARQLCrossValidation(QTL2Disjunctive la, AbstractClassExpressionLearning System.out.println(concept); // Set tmp = rs.hasType(concept, testSetsPos.get(currFold)); Set tmp = hasType(testSetsPos.get(currFold), la); - Set tmp2 = Helper.difference(testSetsPos.get(currFold), tmp); + Set tmp2 = Sets.difference(testSetsPos.get(currFold), tmp); // Set tmp3 = rs.hasType(concept, testSetsNeg.get(currFold)); Set tmp3 = hasType(testSetsNeg.get(currFold), la); outputWriter("test set errors pos: " + tmp2); outputWriter("test set errors neg: " + tmp3); - // calculate training accuracies + // calculate training accuracies System.out.println(getCorrectPosClassified(rs, concept, trainingSetsPos.get(currFold))); // int trainingCorrectPosClassified = getCorrectPosClassified(rs, concept, trainingSetsPos.get(currFold)); int trainingCorrectPosClassified = getCorrectPosClassified(trainingSetsPos.get(currFold), la); @@ -196,7 +196,7 @@ public SPARQLCrossValidation(QTL2Disjunctive la, AbstractClassExpressionLearning int trainingCorrectNegClassified = getCorrectNegClassified(trainingSetsNeg.get(currFold), la); int trainingCorrectExamples = trainingCorrectPosClassified + trainingCorrectNegClassified; double trainingAccuracy = 100*((double)trainingCorrectExamples/(trainingSetsPos.get(currFold).size()+ - trainingSetsNeg.get(currFold).size())); + trainingSetsNeg.get(currFold).size())); accuracyTraining.addNumber(trainingAccuracy); // calculate test accuracies // int correctPosClassified = getCorrectPosClassified(rs, concept, testSetsPos.get(currFold)); @@ -219,13 +219,13 @@ public SPARQLCrossValidation(QTL2Disjunctive la, AbstractClassExpressionLearning double precision = correctPosClassified + negAsPos == 0 ? 0 : correctPosClassified / (double) (correctPosClassified + negAsPos); double recall = correctPosClassified / (double) testSetsPos.get(currFold).size(); // System.out.println(precision);System.out.println(recall); - fMeasure.addNumber(100*Heuristics.getFScore(recall, precision)); + fMeasure.addNumber(100*Heuristics.getFScore(recall, precision)); length.addNumber(OWLClassExpressionUtils.getLength(concept)); outputWriter("fold " + currFold + ":"); outputWriter(" training: " + pos.size() + " positive and " + neg.size() + " negative examples"); - outputWriter(" testing: " + correctPosClassified + "/" + testSetsPos.get(currFold).size() + " correct positives, " + outputWriter(" testing: " + correctPosClassified + "/" + testSetsPos.get(currFold).size() + " correct positives, " + correctNegClassified + "/" + testSetsNeg.get(currFold).size() + " correct negatives"); outputWriter(" concept: " + concept); outputWriter(" accuracy: " + df.format(currAccuracy) + "% (" + df.format(trainingAccuracy) + "% on training set)"); @@ -238,9 +238,9 @@ public SPARQLCrossValidation(QTL2Disjunctive la, AbstractClassExpressionLearning outputWriter("Finished " + folds + "-folds cross-validation."); outputWriter("runtime: " + statOutput(df, runtime, "s")); outputWriter("length: " + statOutput(df, length, "")); - outputWriter("F-Measure on training set: " + statOutput(df, fMeasureTraining, "%")); + outputWriter("F-Measure on training set: " + statOutput(df, fMeasureTraining, "%")); outputWriter("F-Measure: " + statOutput(df, fMeasure, "%")); - outputWriter("predictive accuracy on training set: " + statOutput(df, accuracyTraining, "%")); + outputWriter("predictive accuracy on training set: " + statOutput(df, accuracyTraining, "%")); outputWriter("predictive accuracy: " + statOutput(df, accuracy, "%")); } @@ -290,7 +290,7 @@ public static Set getTestingSet(List examples, int } public static Set getTrainingSet(Set examples, Set testingSet) { - return Helper.difference(examples, testingSet); + return Sets.difference(examples, testingSet); } // takes nr. of examples and the nr. of folds for this examples; @@ -309,7 +309,7 @@ public static String statOutput(DecimalFormat df, Stat stat, String unit) { String str = "av. " + df.format(stat.getMean()) + unit; str += " (deviation " + df.format(stat.getStandardDeviation()) + unit + "; "; str += "min " + df.format(stat.getMin()) + unit + "; "; - str += "max " + df.format(stat.getMax()) + unit + ")"; + str += "max " + df.format(stat.getMax()) + unit + ")"; return str; } diff --git a/interfaces/src/main/java/org/dllearner/configuration/spring/ConfigurationBasedBeanDefinitionRegistryPostProcessor.java b/interfaces/src/main/java/org/dllearner/configuration/spring/ConfigurationBasedBeanDefinitionRegistryPostProcessor.java index a903286cf0..2ddfa85b2d 100644 --- a/interfaces/src/main/java/org/dllearner/configuration/spring/ConfigurationBasedBeanDefinitionRegistryPostProcessor.java +++ b/interfaces/src/main/java/org/dllearner/configuration/spring/ConfigurationBasedBeanDefinitionRegistryPostProcessor.java @@ -4,9 +4,6 @@ import org.dllearner.configuration.IConfiguration; import org.dllearner.configuration.IConfigurationProperty; -import org.dllearner.kb.KBFile; -import org.dllearner.kb.LocalModelBasedSparqlEndpointKS; -import org.dllearner.kb.OWLFile; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; @@ -106,10 +103,14 @@ private void addBaseDirectoryIfNeeded(BeanDefinition beanDefinition) { throw new RuntimeException("Can't find class " + beanDefinition.getBeanClassName()); } /** Add Base Directory */ - if (beanClass.isAssignableFrom(OWLFile.class) || - beanClass.isAssignableFrom(KBFile.class) || - beanClass == LocalModelBasedSparqlEndpointKS.class) { + try { + beanClass.getMethod("setBaseDir", String.class); beanDefinition.getPropertyValues().add("baseDir", configuration.getBaseDir()); + } catch (NoSuchMethodException e) { + // ignore + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); } } diff --git a/interfaces/src/main/java/org/dllearner/configuration/spring/CustomPropertyEditorRegistrar.java b/interfaces/src/main/java/org/dllearner/configuration/spring/CustomPropertyEditorRegistrar.java index 9edac9e3f9..286a022b72 100644 --- a/interfaces/src/main/java/org/dllearner/configuration/spring/CustomPropertyEditorRegistrar.java +++ b/interfaces/src/main/java/org/dllearner/configuration/spring/CustomPropertyEditorRegistrar.java @@ -1,13 +1,10 @@ package org.dllearner.configuration.spring; import org.dllearner.configuration.spring.editors.ClassExpressionPropertyEditor; -import org.dllearner.configuration.spring.editors.IndividualEditor; -import org.dllearner.configuration.spring.editors.ObjectPropertyEditor; +import org.dllearner.configuration.spring.editors.OWLEntityEditor; import org.dllearner.configuration.spring.editors.ReasonerImplementationEditor; import org.dllearner.reasoning.ReasonerImplementation; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLObjectProperty; +import org.semanticweb.owlapi.model.*; import org.springframework.beans.PropertyEditorRegistrar; import org.springframework.beans.PropertyEditorRegistry; @@ -26,8 +23,10 @@ public class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar { public void registerCustomEditors(PropertyEditorRegistry registry) { //Register any custom editors here. registry.registerCustomEditor(OWLClassExpression.class, new ClassExpressionPropertyEditor()); - registry.registerCustomEditor(OWLObjectProperty.class, new ObjectPropertyEditor()); - registry.registerCustomEditor(OWLIndividual.class, new IndividualEditor()); +// registry.registerCustomEditor(OWLClass.class, new OWLEntityEditor>()); + registry.registerCustomEditor(OWLObjectProperty.class, new OWLEntityEditor>(EntityType.OBJECT_PROPERTY)); + registry.registerCustomEditor(OWLDataProperty.class, new OWLEntityEditor>(EntityType.DATA_PROPERTY)); + registry.registerCustomEditor(OWLIndividual.class, new OWLEntityEditor>(EntityType.NAMED_INDIVIDUAL)); registry.registerCustomEditor(ReasonerImplementation.class, new ReasonerImplementationEditor()); } diff --git a/interfaces/src/main/java/org/dllearner/configuration/spring/editors/AbstractPropertyEditor.java b/interfaces/src/main/java/org/dllearner/configuration/spring/editors/AbstractPropertyEditor.java index 32fc7bde5e..4f373352d9 100644 --- a/interfaces/src/main/java/org/dllearner/configuration/spring/editors/AbstractPropertyEditor.java +++ b/interfaces/src/main/java/org/dllearner/configuration/spring/editors/AbstractPropertyEditor.java @@ -3,17 +3,13 @@ */ package org.dllearner.configuration.spring.editors; -import java.awt.Component; -import java.awt.Graphics; -import java.awt.Rectangle; -import java.beans.PropertyChangeListener; -import java.beans.PropertyEditor; +import java.beans.PropertyEditorSupport; /** * @author Lorenz Buehmann * */ -public abstract class AbstractPropertyEditor implements PropertyEditor{ +public abstract class AbstractPropertyEditor extends PropertyEditorSupport { protected T value; @@ -27,65 +23,5 @@ public Object getValue() { return value; } - /* (non-Javadoc) - * @see java.beans.PropertyEditor#addPropertyChangeListener(java.beans.PropertyChangeListener) - */ - @Override - public void addPropertyChangeListener(PropertyChangeListener arg0) { - } - - /* (non-Javadoc) - * @see java.beans.PropertyEditor#getCustomEditor() - */ - @Override - public Component getCustomEditor() { - return null; - } - - /* (non-Javadoc) - * @see java.beans.PropertyEditor#getJavaInitializationString() - */ - @Override - public String getJavaInitializationString() { - return null; - } - - /* (non-Javadoc) - * @see java.beans.PropertyEditor#getTags() - */ - @Override - public String[] getTags() { - return null; - } - - /* (non-Javadoc) - * @see java.beans.PropertyEditor#isPaintable() - */ - @Override - public boolean isPaintable() { - return false; - } - - /* (non-Javadoc) - * @see java.beans.PropertyEditor#paintValue(java.awt.Graphics, java.awt.Rectangle) - */ - @Override - public void paintValue(Graphics arg0, Rectangle arg1) { - } - - /* (non-Javadoc) - * @see java.beans.PropertyEditor#removePropertyChangeListener(java.beans.PropertyChangeListener) - */ - @Override - public void removePropertyChangeListener(PropertyChangeListener arg0) { - } - - /* (non-Javadoc) - * @see java.beans.PropertyEditor#supportsCustomEditor() - */ - @Override - public boolean supportsCustomEditor() { - return false; - } } diff --git a/interfaces/src/main/java/org/dllearner/configuration/spring/editors/ConfigHelper.java b/interfaces/src/main/java/org/dllearner/configuration/spring/editors/ConfigHelper.java index 79af8dac48..f55d811cee 100644 --- a/interfaces/src/main/java/org/dllearner/configuration/spring/editors/ConfigHelper.java +++ b/interfaces/src/main/java/org/dllearner/configuration/spring/editors/ConfigHelper.java @@ -31,6 +31,7 @@ import java.util.Map; import java.util.TreeMap; +import org.dllearner.core.AnnComponentManager; import org.dllearner.core.Component; import org.dllearner.core.config.ConfigOption; import org.springframework.beans.BeanUtils; @@ -63,9 +64,8 @@ public class ConfigHelper { public static void configure(Component component, String configName, T configValue){ List fields = getAllFields(component); for(Field f : fields){ - ConfigOption option = f.getAnnotation(ConfigOption.class); - if(option != null){ - if(option.name().equals(configName)){ + if(f.isAnnotationPresent(ConfigOption.class)){ + if(AnnComponentManager.getName(f).equals(configName)){ try { PropertyEditor editor = PropertyEditor.class.newInstance(); editor.setAsText(configValue.toString()); @@ -105,17 +105,16 @@ public static List getConfigOptions(Component component){ * @param component The component to analyse. * @return All config options of the component with their respective value. */ - public static Map getConfigOptionValues(Component component) { - Map optionValues = new HashMap(); + public static Map getConfigOptionValues(Component component) { + Map optionValues = new HashMap(); List fields = getAllFields(component); for(Field field : fields) { - ConfigOption option = field.getAnnotation(ConfigOption.class); - if(option != null) { + if(field.isAnnotationPresent(ConfigOption.class)) { try { // we invoke the public getter instead of accessing a private field (may cause problem with SecurityManagers) // use Spring BeanUtils TODO: might be unnecessarily slow because we already have the field? Object value = BeanUtils.getPropertyDescriptor(component.getClass(), field.getName()).getReadMethod().invoke(component); - optionValues.put(option, value); + optionValues.put(field, value); } catch (IllegalArgumentException e1) { e1.printStackTrace(); } catch (IllegalAccessException e1) { @@ -128,7 +127,7 @@ public static Map getConfigOptionValues(Component component } } return optionValues; - } + } /** * Returns all config options for the given component. @@ -154,7 +153,7 @@ public static List getConfigOptions(Class com * @param component * @return */ - public static Map> getConfigOptionTypes(Class component){ + public static Map> getConfigOptionTypes(Class component){ return getConfigOptionTypes(component, true); } @@ -163,12 +162,12 @@ public static Map> getConfigOptionTypes(Class component * @param component * @return */ - public static Map> getConfigOptionTypes(Class component, boolean useSuperTypes){ - Map> optionTypes = new TreeMap>(new Comparator() { + public static Map> getConfigOptionTypes(Class component, boolean useSuperTypes){ + Map> optionTypes = new TreeMap>(new Comparator() { @Override - public int compare(ConfigOption o1, ConfigOption o2) { - return o1.name().compareTo(o2.name()); + public int compare(Field o1, Field o2) { + return AnnComponentManager.getName(o1).compareTo(AnnComponentManager.getName(o2)); } }); Field[] fields = component.getDeclaredFields(); @@ -178,9 +177,8 @@ public int compare(ConfigOption o1, ConfigOption o2) { } } for(Field f : fields){ - ConfigOption option = f.getAnnotation(ConfigOption.class); - if(option != null){ - optionTypes.put(option, f.getType()); + if(f.isAnnotationPresent(ConfigOption.class)) { + optionTypes.put(f, f.getType()); } } return optionTypes; diff --git a/interfaces/src/main/java/org/dllearner/configuration/spring/editors/DataPropertyEditor.java b/interfaces/src/main/java/org/dllearner/configuration/spring/editors/DataPropertyEditor.java deleted file mode 100644 index 7ba0073e41..0000000000 --- a/interfaces/src/main/java/org/dllearner/configuration/spring/editors/DataPropertyEditor.java +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.configuration.spring.editors; - -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLDataProperty; - -import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; - -/** - * Created by IntelliJ IDEA. - * User: Chris - * Date: 7/26/11 - * Time: 9:42 PM - *

    - * 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 DataPropertyEditor extends AbstractPropertyEditor { - - private OWLDataFactory df = new OWLDataFactoryImpl(); - - @Override - public String getJavaInitializationString() { - /** This returns the value needed to reconstitute the object from a string */ - return value.toStringID(); - } - - @Override - public String getAsText() { - /** Get the text value of this object - for displaying in GUIS, etc */ - return value.toStringID(); - } - - @Override - public void setAsText(String text) throws IllegalArgumentException { - value = df.getOWLDataProperty(IRI.create(text)); - } - -} diff --git a/interfaces/src/main/java/org/dllearner/configuration/spring/editors/IndividualEditor.java b/interfaces/src/main/java/org/dllearner/configuration/spring/editors/IndividualEditor.java deleted file mode 100644 index 7527ed40aa..0000000000 --- a/interfaces/src/main/java/org/dllearner/configuration/spring/editors/IndividualEditor.java +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.configuration.spring.editors; - -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLIndividual; - -import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; - -/** - * Created by IntelliJ IDEA. - * User: Chris - * Date: 7/26/11 - * Time: 9:42 PM - *

    - * Basic Property Editor for OWL individuals. Doesn't have GUI support yet but we could add that later if we wanted. - */ -public class IndividualEditor extends AbstractPropertyEditor { - - private OWLDataFactory df = new OWLDataFactoryImpl(false, false); - - @Override - public String getJavaInitializationString() { - /** This returns the value needed to reconstitute the object from a string */ - return value.toStringID(); - } - - @Override - public String getAsText() { - /** Get the text value of this object - for displaying in GUIS, etc */ - return value.toStringID(); - } - - @Override - public void setAsText(String text) throws IllegalArgumentException { - value = df.getOWLNamedIndividual(IRI.create(text)); - } -} diff --git a/interfaces/src/main/java/org/dllearner/configuration/spring/editors/NamedClassEditor.java b/interfaces/src/main/java/org/dllearner/configuration/spring/editors/OWLEntityEditor.java similarity index 60% rename from interfaces/src/main/java/org/dllearner/configuration/spring/editors/NamedClassEditor.java rename to interfaces/src/main/java/org/dllearner/configuration/spring/editors/OWLEntityEditor.java index 592d871ab7..245828a1eb 100644 --- a/interfaces/src/main/java/org/dllearner/configuration/spring/editors/NamedClassEditor.java +++ b/interfaces/src/main/java/org/dllearner/configuration/spring/editors/OWLEntityEditor.java @@ -19,38 +19,35 @@ package org.dllearner.configuration.spring.editors; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLDataFactory; - +import org.semanticweb.owlapi.model.*; import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; /** - * Created by IntelliJ IDEA. - * User: Chris - * Date: 7/26/11 - * Time: 9:42 PM - *

    - * Basic Property Editor for OWL class. Doesn't have GUI support yet but we could add that later if we wanted. + * Basic Property Editor for OWL entity. + * @author Lorenz Buehmann */ -public class NamedClassEditor extends AbstractPropertyEditor { +public class OWLEntityEditor extends AbstractPropertyEditor { + + private final OWLDataFactory df = new OWLDataFactoryImpl(); - private OWLDataFactory df = new OWLDataFactoryImpl(); + private T entityType; + + public OWLEntityEditor(T entityType) { + this.entityType = entityType; + } @Override public String getJavaInitializationString() { - /** This returns the value needed to reconstitute the object from a string */ return value.toStringID(); } @Override public String getAsText() { - /** Get the text value of this object - for displaying in GUIS, etc */ return value.toStringID(); } @Override public void setAsText(String text) throws IllegalArgumentException { - value = df.getOWLClass(IRI.create(text)); + value = df.getOWLEntity(entityType, IRI.create(text)); } } diff --git a/interfaces/src/main/java/org/dllearner/configuration/spring/editors/ObjectPropertyEditor.java b/interfaces/src/main/java/org/dllearner/configuration/spring/editors/ObjectPropertyEditor.java deleted file mode 100644 index e35c6d86ae..0000000000 --- a/interfaces/src/main/java/org/dllearner/configuration/spring/editors/ObjectPropertyEditor.java +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Copyright (C) 2007-2011, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.dllearner.configuration.spring.editors; - -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLObjectProperty; - -import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; - -/** - * Created by IntelliJ IDEA. - * User: Chris - * Date: 7/26/11 - * Time: 9:42 PM - *

    - * Basic Property Editor for OWL object property. Doesn't have GUI support yet but we could add that later if we wanted. - */ -public class ObjectPropertyEditor extends AbstractPropertyEditor { - - private OWLDataFactory df = new OWLDataFactoryImpl(); - - @Override - public String getJavaInitializationString() { - /** This returns the value needed to reconstitute the object from a string */ - return value.toStringID(); - } - - @Override - public String getAsText() { - /** Get the text value of this object - for displaying in GUIS, etc */ - return value.toStringID(); - } - - @Override - public void setAsText(String text) throws IllegalArgumentException { - value = df.getOWLObjectProperty(IRI.create(text)); - } -} diff --git a/interfaces/src/main/java/org/dllearner/confparser/ConfParserConfiguration.java b/interfaces/src/main/java/org/dllearner/confparser/ConfParserConfiguration.java index f99d9a9b95..7a4ebf18af 100644 --- a/interfaces/src/main/java/org/dllearner/confparser/ConfParserConfiguration.java +++ b/interfaces/src/main/java/org/dllearner/confparser/ConfParserConfiguration.java @@ -1,160 +1,132 @@ -package org.dllearner.confparser; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; - -import org.apache.commons.collections15.BidiMap; -import org.dllearner.cli.ConfFileOption; -import org.dllearner.configuration.IConfiguration; -import org.dllearner.configuration.IConfigurationProperty; -import org.dllearner.core.AnnComponentManager; -import org.dllearner.core.Component; -import org.dllearner.utilities.owl.DLSyntaxObjectRenderer; -import org.dllearner.utilities.owl.ManchesterOWLSyntaxOWLObjectRendererImplExt; -import org.semanticweb.owlapi.io.OWLObjectRenderer; -import org.semanticweb.owlapi.io.ToStringRenderer; -import org.springframework.core.io.InputStreamResource; -import org.springframework.core.io.Resource; - -/** - * Created by IntelliJ IDEA. - * User: Chris - * Date: 8/27/11 - * Time: 7:21 AM - *

    - * Conf Parser Based implementation. - */ -public class ConfParserConfiguration implements IConfiguration { - - private final ConfParser parser; - private final String baseDir; - private final String typeProperty = "type"; - - enum Rendering { - DL_SYNTAX("dlsyntax", new DLSyntaxObjectRenderer()), - MANCHESTER_SYNTAX("manchester", new ManchesterOWLSyntaxOWLObjectRendererImplExt(true, false)); - - String name; - OWLObjectRenderer renderer; - - Rendering(String name, OWLObjectRenderer render) { - this.name = name; - this.renderer = render; - } - - public String getName() { - return name; - } - - public OWLObjectRenderer getRenderer() { - return renderer; - } - } - - public ConfParserConfiguration(Resource source) { - try { -// baseDir = source.getFile().getAbsoluteFile().getParent(); - if(!(source instanceof InputStreamResource)){ - baseDir = source.getFile().getAbsoluteFile().getParentFile().getAbsolutePath(); - } else { - baseDir = null; - } - parser = new ConfParser(source.getInputStream()); - parser.Start(); - - // setup rendering TODO put it into CLI - Rendering rendering = Rendering.MANCHESTER_SYNTAX; - ConfFileOption renderingOption = parser.getConfOptionsByProperty("rendering"); - if(renderingOption != null) { - String syntax = renderingOption.getPropertyValue(); - for (Rendering r : Rendering.values()) { - if(syntax.equals(r.getName())) { - rendering = r; - } - } - } - ToStringRenderer.getInstance().setRenderer(rendering.getRenderer()); - } catch (ParseException e) { - throw new RuntimeException(e); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - @Override - public Collection getBeanNames() { - Set result = new HashSet(); - Map> beans = parser.getConfOptionsByBean(); - result.addAll(beans.keySet()); - return result; - } - - @Override - public Class getClass(String beanName) { - - List confOptions = parser.getConfOptionsByBean(beanName); - - ConfFileOption option = null; - for (ConfFileOption confOption : confOptions) { - if(typeProperty.equalsIgnoreCase(confOption.getPropertyName())){ - option = confOption; - } - } - - if(option == null){ - throw new RuntimeException("No type property set for bean: " + beanName); - } - - Class result = null; - - String value = (String) option.getValue(); - // first option: use long name of @ComponentAnn annotation (case insensitive) - BidiMap, String> componentsNamed = AnnComponentManager.getInstance().getComponentsNamed(); - for(Entry, String> entry : componentsNamed.entrySet()) { - if(entry.getValue().equalsIgnoreCase(value)) { - return entry.getKey(); - } - } - // second option: use short name of @ComponentAnn annotation - // by convention, short names should always be lower case, but we still do it case insensitive - BidiMap, String> componentsNamedShort = AnnComponentManager.getInstance().getComponentsNamedShort(); - for(Entry, String> entry : componentsNamedShort.entrySet()) { - if(entry.getValue().equalsIgnoreCase(value)) { - return entry.getKey(); - } - } - // third option: use specified class name - try { - result = Class.forName(value); - } catch (ClassNotFoundException e) { - // if all methods fail, throw an exception - throw new RuntimeException("Problem getting class type for bean: " + beanName + " - trying to instantiate class: " + value,e); - } - return result; - } - - @Override - public String getBaseDir() { - return baseDir; - } - - @Override - public Collection getConfigurationProperties(String beanName) { - List confFileOptions = parser.getConfOptionsByBean(beanName); - Collection result = new ArrayList(); - - for (ConfFileOption confFileOption : confFileOptions) { - - if (!typeProperty.equalsIgnoreCase(confFileOption.getPropertyName())) { - result.add(confFileOption); - } - } - return result; - } -} +package org.dllearner.confparser; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.apache.commons.collections15.BidiMap; +import org.dllearner.cli.ConfFileOption; +import org.dllearner.configuration.IConfiguration; +import org.dllearner.configuration.IConfigurationProperty; +import org.dllearner.core.AnnComponentManager; +import org.dllearner.core.Component; +import org.dllearner.core.StringRenderer; +import org.dllearner.core.StringRenderer.Rendering; +import org.springframework.core.io.InputStreamResource; +import org.springframework.core.io.Resource; + +/** + * Created by IntelliJ IDEA. + * User: Chris + * Date: 8/27/11 + * Time: 7:21 AM + *

    + * Conf Parser Based implementation. + */ +public class ConfParserConfiguration implements IConfiguration { + + private final ConfParser parser; + private final String baseDir; + private final String typeProperty = "type"; + + public ConfParserConfiguration(Resource source) { + try { +// baseDir = source.getFile().getAbsoluteFile().getParent(); + if(!(source instanceof InputStreamResource)){ + baseDir = source.getFile().getAbsoluteFile().getParentFile().getAbsolutePath(); + } else { + baseDir = null; + } + parser = new ConfParser(source.getInputStream()); + parser.Start(); + + // setup rendering TODO put it into CLI + ConfFileOption renderingOption = parser.getConfOptionsByProperty("rendering"); + if(renderingOption != null) { + StringRenderer.setRenderer(renderingOption.getPropertyValue()); + } else { + StringRenderer.setRenderer(Rendering.MANCHESTER_SYNTAX); + } + } catch (ParseException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public Collection getBeanNames() { + Set result = new HashSet(); + Map> beans = parser.getConfOptionsByBean(); + result.addAll(beans.keySet()); + return result; + } + + @Override + public Class getClass(String beanName) { + + List confOptions = parser.getConfOptionsByBean(beanName); + + ConfFileOption option = null; + for (ConfFileOption confOption : confOptions) { + if(typeProperty.equalsIgnoreCase(confOption.getPropertyName())){ + option = confOption; + } + } + + if(option == null){ + throw new RuntimeException("No type property set for bean: " + beanName); + } + + Class result = null; + + String value = (String) option.getValue(); + // first option: use long name of @ComponentAnn annotation (case insensitive) + BidiMap, String> componentsNamed = AnnComponentManager.getInstance().getComponentsNamed(); + for(Entry, String> entry : componentsNamed.entrySet()) { + if(entry.getValue().equalsIgnoreCase(value)) { + return entry.getKey(); + } + } + // second option: use short name of @ComponentAnn annotation + // by convention, short names should always be lower case, but we still do it case insensitive + BidiMap, String> componentsNamedShort = AnnComponentManager.getInstance().getComponentsNamedShort(); + for(Entry, String> entry : componentsNamedShort.entrySet()) { + if(entry.getValue().equalsIgnoreCase(value)) { + return entry.getKey(); + } + } + // third option: use specified class name + try { + result = Class.forName(value); + } catch (ClassNotFoundException e) { + // if all methods fail, throw an exception + throw new RuntimeException("Problem getting class type for bean: " + beanName + " - trying to instantiate class: " + value,e); + } + return result; + } + + @Override + public String getBaseDir() { + return baseDir; + } + + @Override + public Collection getConfigurationProperties(String beanName) { + List confFileOptions = parser.getConfOptionsByBean(beanName); + Collection result = new ArrayList(); + + for (ConfFileOption confFileOption : confFileOptions) { + + if (!typeProperty.equalsIgnoreCase(confFileOption.getPropertyName())) { + result.add(confFileOption); + } + } + return result; + } +} diff --git a/interfaces/src/main/java/org/dllearner/server/DLLearnerWS.java b/interfaces/src/main/java/org/dllearner/server/DLLearnerWS.java index a822c20499..c0838f90ab 100644 --- a/interfaces/src/main/java/org/dllearner/server/DLLearnerWS.java +++ b/interfaces/src/main/java/org/dllearner/server/DLLearnerWS.java @@ -30,6 +30,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.NavigableSet; import java.util.Random; import java.util.Set; import java.util.SortedSet; @@ -41,7 +42,6 @@ import javax.jws.soap.SOAPBinding; import org.apache.log4j.Logger; -import org.dllearner.Info; import org.dllearner.core.AbstractCELA; import org.dllearner.core.AbstractClassExpressionLearningProblem; import org.dllearner.core.AbstractComponent; @@ -54,6 +54,7 @@ import org.dllearner.core.EvaluatedDescription; import org.dllearner.core.KnowledgeSource; import org.dllearner.core.LearningProblemUnsupportedException; +import org.dllearner.core.config.ConfigOption; import org.dllearner.kb.OWLFile; import org.dllearner.kb.sparql.Cache; import org.dllearner.kb.sparql.SPARQLTasks; @@ -64,7 +65,6 @@ import org.dllearner.learningproblems.PosOnlyLP; import org.dllearner.parser.KBParser; import org.dllearner.parser.ParseException; -import org.dllearner.utilities.datastructures.Datastructures; import org.dllearner.utilities.datastructures.StringTuple; import org.dllearner.utilities.examples.AutomaticNegativeExampleFinderSPARQL; import org.dllearner.utilities.owl.OWLAPIRenderers; @@ -100,13 +100,86 @@ public class DLLearnerWS { private static AnnComponentManager cm = AnnComponentManager.getInstance(); /** - * Returns the DL-Learner version this web service is based on. - * @return DL-Learner-Build. + * Conversion between different data structures. + * + * @author Jens Lehmann + * @author Sebastian Hellmann + * */ - @WebMethod - public String getBuild() { - return Info.build; + public static class Datastructures { + + public static boolean strToBool(String str) { + if (str.equals("true")) + return true; + else if (str.equals("false")) + return false; + else + throw new Error("Cannot convert to boolean."); + } + + /** + * easy conversion + * + * @param s + */ + public static String[] setToArray(Set s) { + if(s==null)return null; + String[] ret=new String[s.size()]; + int i=0; + for (Iterator iter = s.iterator(); iter.hasNext();) { + ret[i] = iter.next(); + i++; + + } + return ret; + + } + + public static String[] sortedSet2StringListIndividuals(Set individuals){ + + String[] ret=new String[individuals.size()]; + Iterator i=individuals.iterator(); + int a=0; + while (i.hasNext()){ + ret[a++]=i.next().toStringID(); + } + Arrays.sort(ret); + return ret; + } + + public static String[] sortedSet2StringListRoles(Set s){ + + String[] ret=new String[s.size()]; + Iterator i=s.iterator(); + int a=0; + while (i.hasNext()){ + ret[a++]=i.next().toStringID(); + } + Arrays.sort(ret); + return ret; + } + + public static String[] sortedSet2StringListConcepts(Set s){ + + String[] ret=new String[s.size()]; + Iterator i=s.iterator(); + int a=0; + while (i.hasNext()){ + ret[a++]=i.next().toStringID(); + } + Arrays.sort(ret); + return ret; + } + } +// /** +// * Returns the DL-Learner version this web service is based on. +// * @return DL-Learner-Build. +// */ +// @WebMethod +// public String getBuild() { +// return Info.build; +// } /** * Method to check whether web service is online and how fast it responses. @@ -201,11 +274,12 @@ public String[] getLearningAlgorithms() { @WebMethod public String[] getConfigOptions(String component, boolean allInfo) throws UnknownComponentException { Class componentClass = cm.getComponentClass(component); - Set options = AnnComponentManager.getConfigOptions(componentClass); + Set options = AnnComponentManager.getConfigOptions(componentClass); String[] optionsString = new String[options.size()]; int i = 0; - for(org.dllearner.core.config.ConfigOption option : options) { - optionsString[i] = option.name(); + for(Field f : options) { + ConfigOption option = f.getAnnotation(ConfigOption.class); + optionsString[i] = AnnComponentManager.getName(f); if(allInfo) { optionsString[i] += "#" + option.description(); optionsString[i] += "#" + option.required(); @@ -416,7 +490,7 @@ else if(format.equals("kb")) public String learnDescriptionsEvaluated(int id) throws ClientNotKnownException { ClientState state = getState(id); state.getLearningAlgorithm().start(); - TreeSet descriptions = state.getLearningAlgorithm() + NavigableSet descriptions = state.getLearningAlgorithm() .getCurrentlyBestEvaluatedDescriptions(); String json = "{"; int count = 1; @@ -704,7 +778,8 @@ public void applyConfigEntryStringArray(int sessionID, int componentID, String o * @param sessionID The session ID. * @param componentID The componentID. * @param optionName The name of the configuration option. - * @param value + * @param keys + * @param values * @throws ClientNotKnownException Thrown if client (session ID) is not known. * @throws UnknownComponentException */ @@ -759,7 +834,7 @@ private void applyConfigEntry(int sessionID, int componentID, String optionName, value = ignoredConcepts; } field.set(component, value); - } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException | URISyntaxException e) { + } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) { e.printStackTrace(); } // try { @@ -1004,7 +1079,7 @@ public String sparqlQuery(int sessionID, int componentID, String query) throws C * @param query The SPARQL query. * @param useCache Specify whether to use a cache for queries. * @return The result of the SPARQL query in JSON format or null if the endpoint does not exist. - * @see SPARQLEndpoint#getEndpointByName; + * @see SparqlEndpoint#getEndpointByName(String) */ @WebMethod public String sparqlQueryPredefinedEndpoint(String predefinedEndpoint, String query, boolean useCache) { diff --git a/interfaces/src/main/java/org/dllearner/server/EnrichmentServlet.java b/interfaces/src/main/java/org/dllearner/server/EnrichmentServlet.java index c17e2809ff..5f819a0a73 100644 --- a/interfaces/src/main/java/org/dllearner/server/EnrichmentServlet.java +++ b/interfaces/src/main/java/org/dllearner/server/EnrichmentServlet.java @@ -62,16 +62,16 @@ import org.dllearner.kb.sparql.SPARQLTasks; import org.dllearner.kb.sparql.SparqlEndpoint; import org.dllearner.kb.sparql.SparqlKnowledgeSource; +import org.dllearner.learningproblems.AccMethodFMeasure; import org.dllearner.learningproblems.AxiomScore; import org.dllearner.learningproblems.ClassLearningProblem; -import org.dllearner.learningproblems.Heuristics.HeuristicType; import org.dllearner.reasoning.ClosedWorldReasoner; import org.dllearner.reasoning.SPARQLReasoner; import org.dllearner.utilities.Helper; -import org.dllearner.utilities.SPARULTranslator; -import org.dllearner.utilities.datastructures.Datastructures; import org.dllearner.utilities.datastructures.SortedSetTuple; import org.dllearner.utilities.examples.AutomaticNegativeExampleFinderSPARQL2; +import org.dllearner.utilities.owl.ManchesterOWLSyntaxOWLObjectRendererImplExt; +import org.dllearner.utilities.owl.OWL2SPARULConverter; import org.json.JSONArray; import org.json.simple.JSONObject; import org.semanticweb.owlapi.apibinding.OWLManager; @@ -91,7 +91,6 @@ import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl; import uk.ac.manchester.cs.owl.owlapi.OWLDataPropertyImpl; import uk.ac.manchester.cs.owl.owlapi.OWLObjectPropertyImpl; -import uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.ManchesterOWLSyntaxOWLObjectRendererImpl; public class EnrichmentServlet extends HttpServlet { @@ -104,7 +103,7 @@ public class EnrichmentServlet extends HttpServlet { private static String validAxiomTypes = ""; - private SPARULTranslator sparul; + private OWL2SPARULConverter sparul; private OWLOntology ont; static { @@ -180,7 +179,7 @@ public EnrichmentServlet() { } catch (OWLOntologyCreationException e1) { e1.printStackTrace(); } - sparul = new SPARULTranslator(man, ont, false); + sparul = new OWL2SPARULConverter(ont, false); dataFactory = man.getOWLDataFactory(); } @@ -205,9 +204,9 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se final boolean useInference = req.getParameter("use_inference") == null ? false : Boolean.valueOf(req .getParameter("use_inference")); - final int maxNrOfReturnedAxioms = req.getParameter("max_returned_axioms") == null ? DEFAULT_MAX_NR_OF_RETURNED_AXIOMS : Integer.parseInt(req.getParameter("max_returned_axioms")); - final int maxExecutionTimeInSeconds = req.getParameter("max_execution_time") == null ? DEFAULT_MAX_EXECUTION_TIME_IN_SECONDS : Integer.parseInt(req.getParameter("max_execution_time")); - final double threshold = req.getParameter("threshold") == null ? DEFAULT_THRESHOLD : Double.parseDouble(req.getParameter("threshold")); + final int maxNrOfReturnedAxioms = req.getParameter("max_returned_axioms") == null ? DEFAULT_MAX_NR_OF_RETURNED_AXIOMS : Integer.parseInt(req.getParameter("max_returned_axioms")); + final int maxExecutionTimeInSeconds = req.getParameter("max_execution_time") == null ? DEFAULT_MAX_EXECUTION_TIME_IN_SECONDS : Integer.parseInt(req.getParameter("max_execution_time")); + final double threshold = req.getParameter("threshold") == null ? DEFAULT_THRESHOLD : Double.parseDouble(req.getParameter("threshold")); String resourceURI = req.getParameter("resource_uri"); if (resourceURI == null) { @@ -278,7 +277,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); List> list = new ArrayList>(); - final OWLObjectRenderer renderer = new ManchesterOWLSyntaxOWLObjectRendererImpl(); + final OWLObjectRenderer renderer = new ManchesterOWLSyntaxOWLObjectRendererImplExt(); // renderer.setShortFormProvider(new ManchesterOWLSyntaxPrefixNameShortFormProvider(new DefaultPrefixManager())); @@ -344,7 +343,7 @@ public JSONObject call() throws Exception { } private String getSPARUL(OWLAxiom axiom){ - return sparul.translate(new AddAxiom(ont, axiom)); + return sparul.convert(new AddAxiom(ont, axiom)); } private boolean oneOf(String value, String... possibleValues){ @@ -444,7 +443,7 @@ private List applyCELOE(SparqlEndpointKS ks, OWLClass nc, boolea SparqlKnowledgeSource ks2; AbstractReasonerComponent rc; ks2 = new SparqlKnowledgeSource(); - ks2.setInstances(Datastructures.individualSetToStringSet(examples.getCompleteSet())); + ks2.setInstances(Helper.getStringSet(examples.getCompleteSet())); ks2.setUrl(ks.getEndpoint().getURL()); ks2.setDefaultGraphURIs(new TreeSet(ks.getEndpoint().getDefaultGraphURIs())); ks2.setUseLits(false); @@ -465,8 +464,7 @@ private List applyCELOE(SparqlEndpointKS ks, OWLClass nc, boolea ClassLearningProblem lp = new ClassLearningProblem(rc); lp.setClassToDescribe(nc); lp.setEquivalence(equivalence); - lp.setHeuristic(HeuristicType.FMEASURE); - lp.setUseApproximations(false); + lp.setAccuracyMethod(new AccMethodFMeasure(true)); lp.setMaxExecutionTimeInSeconds(10); lp.init(); @@ -478,7 +476,7 @@ private List applyCELOE(SparqlEndpointKS ks, OWLClass nc, boolea System.out.print("running CELOE (for " + (equivalence ? "equivalent classes" : "sub classes") + ") ... "); la.start(); runTime = System.currentTimeMillis() - startTime; - System.out.println("done in " + runTime + " ms"); + System.out.println("done in " + runTime + " ms"); // convert the result to axioms (to make it compatible with the other algorithms) List> learnedDescriptions = la.getCurrentlyBestEvaluatedDescriptions(threshold); @@ -491,7 +489,7 @@ private List applyCELOE(SparqlEndpointKS ks, OWLClass nc, boolea axiom = dataFactory.getOWLSubClassOfAxiom(nc, learnedDescription.getDescription()); } Score score = lp.computeScore(learnedDescription.getDescription()); - learnedAxioms.add(new EvaluatedAxiom(axiom, new AxiomScore(score.getAccuracy()))); + learnedAxioms.add(new EvaluatedAxiom(axiom, new AxiomScore(score.getAccuracy()))); } return learnedAxioms; } diff --git a/interfaces/src/main/java/org/dllearner/test/UndocumentedOptionScanner.java b/interfaces/src/main/java/org/dllearner/test/UndocumentedOptionScanner.java new file mode 100644 index 0000000000..726574ef5e --- /dev/null +++ b/interfaces/src/main/java/org/dllearner/test/UndocumentedOptionScanner.java @@ -0,0 +1,94 @@ +package org.dllearner.test; + +import com.google.common.collect.Sets; +import org.apache.log4j.Logger; +import org.dllearner.core.AnnComponentManager; +import org.dllearner.core.Component; +import org.dllearner.core.annotations.OutVariable; +import org.dllearner.core.annotations.Unused; +import org.dllearner.core.config.ConfigOption; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.*; +import java.util.Map.Entry; + +public class UndocumentedOptionScanner { + public static void main(String[] args) { + Logger logger = Logger.getLogger(UndocumentedOptionScanner.class); + AnnComponentManager cm = AnnComponentManager.getInstance(); + for (Class c : cm.getComponents()) { + logger.info("\n@"+c.getCanonicalName()); + Map> fields = new TreeMap<>(); + Map> methods = new TreeMap<>(); + for (Method m : c.getMethods()) { + String name = m.getName(); + List set = methods.get(name); + if (set == null) { + set = new LinkedList(); + methods.put(name, set); + } + set.add(m); + } + for ( + Class cc = c; + cc != null; + cc = cc.getSuperclass() + ) { + for (Field f : cc.getDeclaredFields()) { + String name = f.getName(); + List set = fields.get(name); + if (set == null) { + set = new LinkedList<>(); + fields.put(name, set); + } + set.add(f); + } + } + + Set hasDoc = new TreeSet<>(); + Set noDoc = new TreeSet<>(); + for (Entry> f : fields.entrySet()) { + boolean isDocumented = false; + for (Field fs : f.getValue()) { + isDocumented = isDocumented || fs.isAnnotationPresent(ConfigOption.class) + || fs.isAnnotationPresent(Unused.class) + || fs.isAnnotationPresent(OutVariable.class); + } + if (isDocumented) { + hasDoc.add(AnnComponentManager.getName(f.getValue().get(0))); + } else { + noDoc.add(f.getKey()); + } + } + Set hasSetter = new TreeSet<>(); + Set noCO = new TreeSet<>(); + for (Entry> m : methods.entrySet()) { + String cn = m.getKey(); + if (cn.startsWith("set")) { + String optionName = cn.substring(3, 4).toLowerCase() + cn.substring(4); + if (cn.substring(4).equals(cn.substring(4).toUpperCase())) { optionName = optionName.toLowerCase(); } + if (hasDoc.contains(optionName)) { + hasSetter.add(optionName); + + } else if (noDoc.contains(optionName)) { + logger.warn("setter+var. but no @configOption: " + optionName); + noCO.add(optionName); + } else { + boolean deprecated = false; + for (Method ms : m.getValue()) { + deprecated = deprecated || ms.isAnnotationPresent(Deprecated.class); + } + if (!deprecated) { + logger.info("setter without var: "+optionName); + } + } + } + } + for (String noSetter : Sets.difference(hasDoc, hasSetter)) { + logger.warn("option without setter! " +noSetter); + } + //if (c.equals(CELOE.class)) { System.exit(0); } + } + } +} diff --git a/interfaces/src/main/resources/log4j.properties b/interfaces/src/main/resources/log4j.properties index 6e4e06aa0a..e448ecaa4e 100644 --- a/interfaces/src/main/resources/log4j.properties +++ b/interfaces/src/main/resources/log4j.properties @@ -15,7 +15,11 @@ log4j.appender.file.Threshold=DEBUG # DL-Learner Logs log4j.logger.org.dllearner=INFO # Turn this to Debug if you wish to dump stack traces to the appenders (console, file) +# use INFO on release log4j.logger.org.dllearner.cli=INFO +#log4j.logger.org.dllearner.cli=DEBUG + + log4j.logger.org.dllearner.server.nke.LogicalRelationStrategy=DEBUG log4j.category.org.dllearner.kb.simple=DEBUG #log4j.category.org.dllearner.reasoning.SPARQLReasoner=DEBUG @@ -30,6 +34,14 @@ log4j.logger.org.springframework=WARN # Jena, including the Joseki client #log4j.logger.com.hp.hpl.jena=WARN + +# to disable spring stack trace (do this for release) +log4j.logger.org.springframework.context.support.ClassPathXmlApplicationContext=ERROR + +# hide useless owlapi warning "entityExpansionLimit not supported by parser" +log4j.logger.org.semanticweb.owlapi.util.SAXParsers=ERROR + + ## log4j.logger.com.hp.hpl.jena.util.FileManager=ALL ## log4j.logger.com.hp.hpl.jena.util.LocatorURL=ALL ## log4j.logger.com.hp.hpl.jena.util.LocatorClassLoader=ALL diff --git a/interfaces/src/main/webapp/WEB-INF/web.xml b/interfaces/src/main/webapp/WEB-INF/web.xml index 6de018f99d..84c1f4f9ee 100644 --- a/interfaces/src/main/webapp/WEB-INF/web.xml +++ b/interfaces/src/main/webapp/WEB-INF/web.xml @@ -26,16 +26,6 @@ - - NKEGeizhals - org.dllearner.server.NKEGeizhals - - - - NKEGeizhals - /NKEGeizhals - - Enrichment org.dllearner.server.EnrichmentServlet diff --git a/interfaces/src/test/java/org/dllearner/test/junit/LearningProblemTest.java b/interfaces/src/test/java/org/dllearner/test/junit/LearningProblemTest.java new file mode 100644 index 0000000000..fe62a9bc41 --- /dev/null +++ b/interfaces/src/test/java/org/dllearner/test/junit/LearningProblemTest.java @@ -0,0 +1,54 @@ +package org.dllearner.test.junit; + +import org.dllearner.algorithms.ocel.OCEL; +import org.dllearner.cli.CLI; +import org.dllearner.core.AbstractCELA; +import org.dllearner.core.AbstractClassExpressionLearningProblem; +import org.dllearner.core.AbstractReasonerComponent; +import org.dllearner.core.StringRenderer; +import org.dllearner.utilities.OWLAPIUtils; +import org.dllearner.utilities.owl.OWLClassExpressionLengthMetric; +import org.dllearner.utilities.owl.OWLClassExpressionUtils; +import org.junit.Test; +import org.semanticweb.owlapi.model.OWLClassExpression; +import org.semanticweb.owlapi.model.OWLDataFactory; +import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; + +import java.io.File; +import java.io.IOException; + +import static org.junit.Assert.assertTrue; + +/** + * Created by ailin on 16-2-5. + */ +public class LearningProblemTest { + @Test + public void problemQualityTest() { + try { + StringRenderer.setRenderer(StringRenderer.Rendering.MANCHESTER_SYNTAX); + CLI cli = new CLI(new File("../examples/carcinogenesis/validate.conf")); + cli.init(); + AbstractReasonerComponent reasoner = cli.getReasonerComponent(); + OWLDataFactory df = new OWLDataFactoryImpl(); + AbstractCELA la = cli.getLearningAlgorithm(); + AbstractClassExpressionLearningProblem lp = cli.getLearningProblem(); + OWLClassExpressionLengthMetric metric; + if (la instanceof OCEL) { + metric = ((OCEL)la).getLengthMetric(); + } else { + metric = OWLClassExpressionLengthMetric.getDefaultMetric().getDefaultMetric(); + } + + OWLClassExpression concept = OWLAPIUtils.fromManchester("Compound and hasAtom only (not Nitrogen-35) and ((amesTestPositive some {true}) or hasStructure some Ar_halide)", reasoner, df, true); + + System.out.println("concept: " + concept + " {" + OWLClassExpressionUtils.getLength(concept, metric) + "}"); + + double acc = lp.getAccuracyOrTooWeak(concept); + + assertTrue("accuracy was only " + acc, acc > 0.69); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/interfaces/testNKEGeizhals.sh b/interfaces/testNKEGeizhals.sh deleted file mode 100755 index 0d980f8bb3..0000000000 --- a/interfaces/testNKEGeizhals.sh +++ /dev/null @@ -1,4 +0,0 @@ -curl http://localhost:8080/interfaces/NKEGeizhals -d "action=learn&data=%7B%22pos%22%3A%5B%5B%22http%3A%2F%2Fgeizhals.at%2Fdeutschland%2Fa621853.html%22%2C%22Acer%20Aspire%203750-2314G50MNkk%20(LX.RGR02.009%2FLX.RGR02.021)%22%2C%22Core%20i3-2310M%202x%202.10GHz%20%E2%80%A2%204096MB%20%E2%80%A2%20500GB%20%E2%80%A2%20DVD%2B%2F-RW%20DL%20%E2%80%A2%20Intel%20GMA%20HD%203000%20(IGP)%20shared%20memory%20%E2%80%A2%203x%20USB%20(2x%20USB%202.0%2C%201x%20USB%203.0)%2FGb%20LAN%2FWLAN%20802.11bgn%2FBLuetooth%203.0%20%E2%80%A2%20HDMI%20%E2%80%A2%205in1%20Card%20Reader%20%E2%80%A2%2013.3%5C%22%20WXGA%20glare%20LED%20TFT%20(1366x768)%20%E2%80%A2%20Windows%207%20Home%20Premium%20(64-bit)%20%E2%80%A2%20Li-Ionen-Akku%20(6%20Zellen)%20%E2%80%A2%202.10kg%20%E2%80%A2%2024%20Monate%20Herstellergarantie%20%E2%80%A2%20Besonderheiten%3A%20Nummernblock%22%5D%2C%5B%22http%3A%2F%2Fgeizhals.at%2Fdeutschland%2Fa621853.html%22%2C%22Acer%20Aspire%203750-2314G50MNkk%20(LX.RGR02.009%2FLX.RGR02.021)%22%2C%22Core%20i3-2310M%202x%202.10GHz%20%E2%80%A2%204096MB%20%E2%80%A2%20500GB%20%E2%80%A2%20DVD%2B%2F-RW%20DL%20%E2%80%A2%20Intel%20GMA%20HD%203000%20(IGP)%20shared%20memory%20%E2%80%A2%203x%20USB%20(2x%20USB%202.0%2C%201x%20USB%203.0)%2FGb%20LAN%2FWLAN%20802.11bgn%2FBLuetooth%203.0%20%E2%80%A2%20HDMI%20%E2%80%A2%205in1%20Card%20Reader%20%E2%80%A2%2013.3%5C%22%20WXGA%20glare%20LED%20TFT%20(1366x768)%20%E2%80%A2%20Windows%207%20Home%20Premium%20(64-bit)%20%E2%80%A2%20Li-Ionen-Akku%20(6%20Zellen)%20%E2%80%A2%202.10kg%20%E2%80%A2%2024%20Monate%20Herstellergarantie%20%E2%80%A2%20Besonderheiten%3A%20Nummernblock%22%5D%2C%5B%22http%3A%2F%2Fgeizhals.at%2Fdeutschland%2Fa621853.html%22%2C%22Acer%20Aspire%203750-2314G50MNkk%20(LX.RGR02.009%2FLX.RGR02.021)%22%2C%22Core%20i3-2310M%202x%202.10GHz%20%E2%80%A2%204096MB%20%E2%80%A2%20500GB%20%E2%80%A2%20DVD%2B%2F-RW%20DL%20%E2%80%A2%20Intel%20GMA%20HD%203000%20(IGP)%20shared%20memory%20%E2%80%A2%203x%20USB%20(2x%20USB%202.0%2C%201x%20USB%203.0)%2FGb%20LAN%2FWLAN%20802.11bgn%2FBLuetooth%203.0%20%E2%80%A2%20HDMI%20%E2%80%A2%205in1%20Card%20Reader%20%E2%80%A2%2013.3%5C%22%20WXGA%20glare%20LED%20TFT%20(1366x768)%20%E2%80%A2%20Windows%207%20Home%20Premium%20(64-bit)%20%E2%80%A2%20Li-Ionen-Akku%20(6%20Zellen)%20%E2%80%A2%202.10kg%20%E2%80%A2%2024%20Monate%20Herstellergarantie%20%E2%80%A2%20Besonderheiten%3A%20Nummernblock%22%5D%2C%5B%22http%3A%2F%2Fgeizhals.at%2Fdeutschland%2Fa621853.html%22%2C%22Acer%20Aspire%203750-2314G50MNkk%20(LX.RGR02.009%2FLX.RGR02.021)%22%2C%22Core%20i3-2310M%202x%202.10GHz%20%E2%80%A2%204096MB%20%E2%80%A2%20500GB%20%E2%80%A2%20DVD%2B%2F-RW%20DL%20%E2%80%A2%20Intel%20GMA%20HD%203000%20(IGP)%20shared%20memory%20%E2%80%A2%203x%20USB%20(2x%20USB%202.0%2C%201x%20USB%203.0)%2FGb%20LAN%2FWLAN%20802.11bgn%2FBLuetooth%203.0%20%E2%80%A2%20HDMI%20%E2%80%A2%205in1%20Card%20Reader%20%E2%80%A2%2013.3%5C%22%20WXGA%20glare%20LED%20TFT%20(1366x768)%20%E2%80%A2%20Windows%207%20Home%20Premium%20(64-bit)%20%E2%80%A2%20Li-Ionen-Akku%20(6%20Zellen)%20%E2%80%A2%202.10kg%20%E2%80%A2%2024%20Monate%20Herstellergarantie%20%E2%80%A2%20Besonderheiten%3A%20Nummernblock%22%5D%5D%2C%22neg%22%3A%5B%5B%22http%3A%2F%2Fgeizhals.at%2Fdeutschland%2Fa652244.html%22%2C%22Acer%20Aspire%205250-C52G32Mikk%2C%20schwarz%20(LX.RJY02.011)%22%2C%22AMD%20C-50%202x%201.00GHz%20%E2%80%A2%202048MB%20%E2%80%A2%20320GB%20%E2%80%A2%20DVD%2B%2F-RW%20DL%20%E2%80%A2%20AMD%20Radeon%20HD%206250%20(IGP)%20shared%20memory%20%E2%80%A2%203x%20USB%202.0%2FLAN%2FWLAN%20802.11bgn%20%E2%80%A2%20HDMI%20%E2%80%A2%202in1%20Card%20Reader%20%E2%80%A2%2015.6%5C%22%20WXGA%20glare%20LED%20TFT%201366x768)%20%E2%80%A2%20Windows%207%20Home%20Premium%20(64-bit)%20%E2%80%A2%20Li-Ionen-Akku%20%E2%80%A2%202.60kg%20%E2%80%A2%2024%20Monate%20Herstellergarantie%20%E2%80%A2%20Besonderheiten%3A%20Nummernblock%22%5D%5D%7D" -curl http://localhost:8080/interfaces/NKEGeizhals -d "action=feedback&data=gdgdfg&debug=treu" -#curl http://hanne.aksw.org:8082/interfaces/NKEGeizhals -d "action=learn&data=%7B%22pos%22%3A%5B%5B%22http%3A%2F%2Fgeizhals.at%2Fdeutschland%2Fa621853.html%22%2C%22Acer%20Aspire%203750-2314G50MNkk%20(LX.RGR02.009%2FLX.RGR02.021)%22%2C%22Core%20i3-2310M%202x%202.10GHz%20%E2%80%A2%204096MB%20%E2%80%A2%20500GB%20%E2%80%A2%20DVD%2B%2F-RW%20DL%20%E2%80%A2%20Intel%20GMA%20HD%203000%20(IGP)%20shared%20memory%20%E2%80%A2%203x%20USB%20(2x%20USB%202.0%2C%201x%20USB%203.0)%2FGb%20LAN%2FWLAN%20802.11bgn%2FBLuetooth%203.0%20%E2%80%A2%20HDMI%20%E2%80%A2%205in1%20Card%20Reader%20%E2%80%A2%2013.3%5C%22%20WXGA%20glare%20LED%20TFT%20(1366x768)%20%E2%80%A2%20Windows%207%20Home%20Premium%20(64-bit)%20%E2%80%A2%20Li-Ionen-Akku%20(6%20Zellen)%20%E2%80%A2%202.10kg%20%E2%80%A2%2024%20Monate%20Herstellergarantie%20%E2%80%A2%20Besonderheiten%3A%20Nummernblock%22%5D%2C%5B%22http%3A%2F%2Fgeizhals.at%2Fdeutschland%2Fa621853.html%22%2C%22Acer%20Aspire%203750-2314G50MNkk%20(LX.RGR02.009%2FLX.RGR02.021)%22%2C%22Core%20i3-2310M%202x%202.10GHz%20%E2%80%A2%204096MB%20%E2%80%A2%20500GB%20%E2%80%A2%20DVD%2B%2F-RW%20DL%20%E2%80%A2%20Intel%20GMA%20HD%203000%20(IGP)%20shared%20memory%20%E2%80%A2%203x%20USB%20(2x%20USB%202.0%2C%201x%20USB%203.0)%2FGb%20LAN%2FWLAN%20802.11bgn%2FBLuetooth%203.0%20%E2%80%A2%20HDMI%20%E2%80%A2%205in1%20Card%20Reader%20%E2%80%A2%2013.3%5C%22%20WXGA%20glare%20LED%20TFT%20(1366x768)%20%E2%80%A2%20Windows%207%20Home%20Premium%20(64-bit)%20%E2%80%A2%20Li-Ionen-Akku%20(6%20Zellen)%20%E2%80%A2%202.10kg%20%E2%80%A2%2024%20Monate%20Herstellergarantie%20%E2%80%A2%20Besonderheiten%3A%20Nummernblock%22%5D%2C%5B%22http%3A%2F%2Fgeizhals.at%2Fdeutschland%2Fa621853.html%22%2C%22Acer%20Aspire%203750-2314G50MNkk%20(LX.RGR02.009%2FLX.RGR02.021)%22%2C%22Core%20i3-2310M%202x%202.10GHz%20%E2%80%A2%204096MB%20%E2%80%A2%20500GB%20%E2%80%A2%20DVD%2B%2F-RW%20DL%20%E2%80%A2%20Intel%20GMA%20HD%203000%20(IGP)%20shared%20memory%20%E2%80%A2%203x%20USB%20(2x%20USB%202.0%2C%201x%20USB%203.0)%2FGb%20LAN%2FWLAN%20802.11bgn%2FBLuetooth%203.0%20%E2%80%A2%20HDMI%20%E2%80%A2%205in1%20Card%20Reader%20%E2%80%A2%2013.3%5C%22%20WXGA%20glare%20LED%20TFT%20(1366x768)%20%E2%80%A2%20Windows%207%20Home%20Premium%20(64-bit)%20%E2%80%A2%20Li-Ionen-Akku%20(6%20Zellen)%20%E2%80%A2%202.10kg%20%E2%80%A2%2024%20Monate%20Herstellergarantie%20%E2%80%A2%20Besonderheiten%3A%20Nummernblock%22%5D%2C%5B%22http%3A%2F%2Fgeizhals.at%2Fdeutschland%2Fa621853.html%22%2C%22Acer%20Aspire%203750-2314G50MNkk%20(LX.RGR02.009%2FLX.RGR02.021)%22%2C%22Core%20i3-2310M%202x%202.10GHz%20%E2%80%A2%204096MB%20%E2%80%A2%20500GB%20%E2%80%A2%20DVD%2B%2F-RW%20DL%20%E2%80%A2%20Intel%20GMA%20HD%203000%20(IGP)%20shared%20memory%20%E2%80%A2%203x%20USB%20(2x%20USB%202.0%2C%201x%20USB%203.0)%2FGb%20LAN%2FWLAN%20802.11bgn%2FBLuetooth%203.0%20%E2%80%A2%20HDMI%20%E2%80%A2%205in1%20Card%20Reader%20%E2%80%A2%2013.3%5C%22%20WXGA%20glare%20LED%20TFT%20(1366x768)%20%E2%80%A2%20Windows%207%20Home%20Premium%20(64-bit)%20%E2%80%A2%20Li-Ionen-Akku%20(6%20Zellen)%20%E2%80%A2%202.10kg%20%E2%80%A2%2024%20Monate%20Herstellergarantie%20%E2%80%A2%20Besonderheiten%3A%20Nummernblock%22%5D%5D%2C%22neg%22%3A%5B%5B%22http%3A%2F%2Fgeizhals.at%2Fdeutschland%2Fa652244.html%22%2C%22Acer%20Aspire%205250-C52G32Mikk%2C%20schwarz%20(LX.RJY02.011)%22%2C%22AMD%20C-50%202x%201.00GHz%20%E2%80%A2%202048MB%20%E2%80%A2%20320GB%20%E2%80%A2%20DVD%2B%2F-RW%20DL%20%E2%80%A2%20AMD%20Radeon%20HD%206250%20(IGP)%20shared%20memory%20%E2%80%A2%203x%20USB%202.0%2FLAN%2FWLAN%20802.11bgn%20%E2%80%A2%20HDMI%20%E2%80%A2%202in1%20Card%20Reader%20%E2%80%A2%2015.6%5C%22%20WXGA%20glare%20LED%20TFT%201366x768)%20%E2%80%A2%20Windows%207%20Home%20Premium%20(64-bit)%20%E2%80%A2%20Li-Ionen-Akku%20%E2%80%A2%202.60kg%20%E2%80%A2%2024%20Monate%20Herstellergarantie%20%E2%80%A2%20Besonderheiten%3A%20Nummernblock%22%5D%5D%7D" -curl http://localhost:8080/interfaces/NKEGeizhals -d "action=mostpopular" diff --git a/pom.xml b/pom.xml index 5048df6a22..fdc5e2a138 100644 --- a/pom.xml +++ b/pom.xml @@ -4,23 +4,22 @@ org.dllearner dllearner-parent - 1.1 + 1.2 pom DL Learner Parent Pom + A framework for supervised Machine Learning in OWL, RDF and Description Logics http://aksw.org/Projects/DLLearner 1.7 UTF-8 - - 2.3.0 - 1.6.4 1.2.16 - 5.2.1 - 3.5.1 - 3.2.13.RELEASE - 2.12.1-8 + 5.3.0 + 4.1.4 + 4.2.4.RELEASE + 2.13.0-5 + 2.4.0-dllearner @@ -28,7 +27,7 @@ components-ext interfaces scripts - + - - - - - org.apache.maven.plugins - maven-surefire-plugin - 2.18.1 - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.2 - - ${compiler.version} - ${compiler.version} - ${project.build.sourceEncoding} - - + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.19 + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + ${compiler.version} + ${compiler.version} + ${project.build.sourceEncoding} + + -Xlint:all,-options,-path + + + org.apache.maven.plugins @@ -101,7 +170,7 @@ org.apache.maven.plugins maven-javadoc-plugin - 2.10.1 + 2.10.3 false @@ -109,7 +178,7 @@ com.mycila license-maven-plugin - 2.8 + 2.11

    license_header.txt
    @@ -117,8 +186,8 @@ Jens Lehmann lehmann@informatik.uni-leipzig.de
    - src/**/*.java - + + src/**/*.java **/README @@ -132,13 +201,14 @@ src/**/*.html src/**/*.jj src/**/*.txt + package-info.java org.codehaus.mojo tomcat-maven-plugin - 1.0 + 2.2 org.apache.maven.plugins @@ -153,12 +223,12 @@ org.apache.maven.plugins maven-dependency-plugin - 2.9 + 2.10 org.apache.maven.plugins maven-release-plugin - 2.5.1 + 2.5.3 true @@ -174,25 +244,75 @@ false true - - master - develop - feature/ - release/ - hotfix/ - support/ - - - - - + + + + org.apache.maven.wagon + wagon-ssh + 2.0 + + + + + + - - - + + + stage-for-scm-publish + post-site + + stage + + + false<!– MSITE-652: configuration won't be necessary with m-site-p 3.2 –> + + + + + + org.apache.maven.plugins + maven-scm-publish-plugin + 1.1 + + + scm-publish + site-deploy + + publish-scm + + + + + --> + + + + + com.google.code.findbugs + jsr305 + 2.0.1 + + - - org.semanticweb.elk - elk-owlapi - 0.4.1 - + org.apache.commons commons-math3 3.5 - net.sourceforge.owlapi - owlapi-distribution - ${owlapi.version} - - - net.sourceforge.owlapi - owlapi-reasoner - ${owlapi.version} - - - net.sourceforge.owlapi - owlapi-debugging - ${owlapi.version} - - - net.sourceforge.owlapi - owlapi-util - ${owlapi.version} - - - net.sourceforge.owlapi - owlapi-api - ${owlapi.version} - - - net.sourceforge.owlapi - owlapi-apibinding - ${owlapi.version} - - - net.sourceforge.owlapi - owlapi-impl - ${owlapi.version} - + net.sourceforge.owlapi + owlapi-distribution + ${owlapi.version} + + + net.sourceforge.owlapi + owlapi-reasoner + ${owlapi.version} + + + net.sourceforge.owlapi + owlapi-debugging + ${owlapi.version} + + + net.sourceforge.owlapi + owlapi-util + ${owlapi.version} + + + net.sourceforge.owlapi + owlapi-api + ${owlapi.version} + + + net.sourceforge.owlapi + owlapi-apibinding + ${owlapi.version} + + + net.sourceforge.owlapi + owlapi-impl + ${owlapi.version} + + + net.sourceforge.owlapi + owlapitools-concurrentimpl + 4.1.0 + + + com.clarkparsia.pellet pellet-distribution - 2.3.3-dllearner + ${pellet.version} pom @@ -270,41 +393,59 @@ - + + + + + + + - eu.trowl - trowl-core - 1.5.0 + org.semanticweb.elk + elk-owlapi + 0.4.3-dllearner - + net.sourceforge.owlapi jfact - 1.2.2 + 4.0.3 - + - org.semanticweb.hermit - hermit - 1.3.8 + net.sourceforge.owlapi + org.semanticweb.hermit + 1.3.8.413 + + + + com.google.code.factplusplus + factpp-owlapi + 1.6.2 + + + + net.sourceforge.owlapi.owllink + owllink + 1.2.2 - - - org.apache.solr - solr-core - ${solr.version} - - - commons-logging - commons-logging - - - org.slf4j - slf4j-jdk14 - - - + + + org.apache.solr + solr-core + ${solr.version} + + + commons-logging + commons-logging + + + org.slf4j + slf4j-jdk14 + + + - + org.json json - 20140107 + 20151123 @@ -340,23 +481,22 @@ net.sf.jopt-simple jopt-simple 4.9 - org.apache.lucene lucene-core - 5.2.1 + ${solr.version} org.apache.lucene lucene-analyzers-common - 5.2.1 + ${solr.version} org.apache.lucene lucene-queryparser - 5.2.1 + ${solr.version} @@ -365,11 +505,11 @@ 0.5.4 - - org.apache.xmlbeans - xmlbeans - 2.4.0 - + + org.apache.xmlbeans + xmlbeans + 2.6.0 + com.jamonapi @@ -380,7 +520,13 @@ org.apache.commons commons-pool2 - 2.4.1 + 2.4.2 + + + + org.apache.commons + commons-compress + 1.10 @@ -402,18 +548,6 @@ test - - com.google.code.factplusplus - factpp-owlapi - 1.6.2 - - - - net.sourceforge.owlapi.owllink - owllink - 1.2.2 - - org.dllearner @@ -456,42 +590,47 @@ com.h2database h2 - 1.4.187 + 1.4.190 mysql mysql-connector-java - 5.1.36 + 5.1.38 - - postgresql + org.postgresql postgresql - 8.4-701.jdbc4 + 9.4-1206-jdbc42 + + + + nz.ac.waikato.cms.weka + weka-dev + 3.7.13 com.google.guava guava - 18.0 + 19.0 com.dumontierlab pdb2rdf-parser 0.0.8 - - - org.slf4j - slf4j-log4j12 - - - - com.hp.hpl.jena - jena - - + + + org.slf4j + slf4j-log4j12 + + + + com.hp.hpl.jena + jena + + com.dumontierlab @@ -520,113 +659,124 @@ - - org.springframework - spring-context - ${spring.version} - - - commons-logging - commons-logging - - - - org.springframework - spring-beans - ${spring.version} - + org.springframework + spring-context + ${spring.version} + + + commons-logging + commons-logging + + + + + org.springframework + spring-beans + ${spring.version} + - + - - - org.slf4j - slf4j-api - ${slf4j.version} - + + + org.slf4j + slf4j-api + ${slf4j.version} + - - - org.slf4j - slf4j-log4j12 - ${slf4j.version} - + + + org.slf4j + slf4j-log4j12 + ${slf4j.version} + - - - log4j - log4j - ${log4j.version} - + + + log4j + log4j + ${log4j.version} + - - - org.slf4j - jcl-over-slf4j - ${slf4j.version} - - + + org.slf4j + jcl-over-slf4j + ${slf4j.version} + + - - - xerces - xercesImpl - 2.6.0 - + + + xml-apis + xml-apis + 1.4.01 + + + xerces + xercesImpl + 2.11.0 + - - org.apache.commons - commons-lang3 - 3.4 - - - - gurobi - gurobi - 5.0.1 - - - - joda-time - joda-time - 2.8.1 - - - edu.berkeley.compbio - jlibsvm - 0.911 - - - org.aksw - semlibsvm - 3.20-SNAPSHOT - + + org.apache.commons + commons-lang3 + 3.4 + + + + gurobi + gurobi + 5.0.1 + + + + joda-time + joda-time + 2.9.1 + + + edu.berkeley.compbio + jlibsvm + 0.911 + + + google-collections + com.google.collections + + + + + org.aksw + semlibsvm + 3.20 + - - maven.aksw.internal - University Leipzig, AKSW Maven2 Repository - http://maven.aksw.org/repository/internal - - - - maven.aksw.snapshots - University Leipzig, AKSW Maven2 Repository - http://maven.aksw.org/repository/snapshots - - - - davidsoergel.releases - David Soergel's Maven Repository - http://dev.davidsoergel.com/nexus/content/repositories/releases - - + + maven.aksw.internal + University Leipzig, AKSW Maven2 Repository + http://maven.aksw.org/repository/internal + + + + maven.aksw.snapshots + University Leipzig, AKSW Maven2 Repository + http://maven.aksw.org/repository/snapshots + + + + davidsoergel.releases + David Soergel's Maven Repository + http://dev.davidsoergel.com/nexus/content/repositories/releases + + @@ -639,28 +789,82 @@ AKSW Snapshot Repository http://maven.aksw.org/archiva/repository/snapshots - + site.deployments Site deployments - scp://prod0.aksw.org/var/www/aksw.org/javadoc/ + scp://[2001:638:902:2010:0:168:35:165]/site - - - - org.apache.maven.plugins - maven-javadoc-plugin - - - http://java.sun.com/j2se/1.7.0/docs/api - - - - - + + + + org.apache.maven.plugins + maven-project-info-reports-plugin + 2.8.1 + + false + + + + + org.apache.maven.plugins + maven-javadoc-plugin + + public + + + http://java.sun.com/j2se/1.7.0/docs/api + + 2g + 128m + +
    + public + true + doc/javadoc + DL-Learner Javadoc + true + false + false + false + false + false + src/etc/overview.html + ${compiler.version} + src/main/java + true + src/etc/javadoc2.css + true + true + true + <img style='float:right' + src='https://raw.githubusercontent.com/github/media/master/octocats/blacktocat-16.png' + border='0' alt='Github.com Logo' /> DL-Learner is licenced + under the terms of the GNU General Public License.<br + />Copyright &#169; 2007-2015 Jens Lehmann + ISO-8859-1 + DL-Learner Javadoc + -Xdoclint:none +
    +
    +
    +
    + + + GitHub + https://github.com/AKSW/DL-Learner/issues + + + + + DL-Learner discussion + https://lists.sourceforge.net/lists/listinfo/dl-learner-discussion + http://sourceforge.net/p/dl-learner/mailman/dl-learner-discussion/ + + @@ -684,17 +888,56 @@ + + + release-sign-artifacts + + + performRelease + true + + + + + + + org.apache.maven.plugins + maven-gpg-plugin + + + sign-artifacts + verify + + sign + + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + http://java.sun.com/j2se/1.7.0/docs/api + + + + + attach-javadocs + + jar + + + 512m + 128m + + + + + + + - - - edu.berkeley.compbio - jlibsvm - - - google-collections - com.google.collections - - - - + diff --git a/scripts/pom.xml b/scripts/pom.xml index 798703555c..93b52ebf68 100644 --- a/scripts/pom.xml +++ b/scripts/pom.xml @@ -11,69 +11,30 @@ org.dllearner dllearner-parent - 1.1 + 1.2 - org.dllearner components-core - - - jena - com.hp.hpl.jena - - - owlapi - net.sourceforge.owlapi - - - owlapi-apibinding - net.sourceforge.owlapi - - - owlapi-api - net.sourceforge.owlapi - - - owlapi-impl - net.sourceforge.owlapi - - - - - org.dllearner - components-ext - - - jena - com.hp.hpl.jena - - + org.dllearner interfaces - - - owlapi - net.sourceforge.owlapi - - + net.sourceforge.secondstring secondstring - - postgresql - postgresql - + com.dumontierlab pdb2rdf-parser + com.dumontierlab pdb2rdf-cli @@ -84,55 +45,32 @@ - + mysql mysql-connector-java + org.apache.commons commons-compress - 1.2 + - weka - weka - 3.6.5 + org.apache.commons + commons-math3 + - net.sourceforge.owlapi - owlapi-distribution + nz.ac.waikato.cms.weka + weka-dev - + + + - - org.apache.commons - commons-math3 - 3.0 - + @@ -168,9 +102,8 @@ org.codehaus.mojo exec-maven-plugin - 1.2.1 + 1.4.0 - exec diff --git a/scripts/src/main/java/org/dllearner/examples/Carcinogenesis.java b/scripts/src/main/java/org/dllearner/examples/Carcinogenesis.java new file mode 100644 index 0000000000..0b604e2196 --- /dev/null +++ b/scripts/src/main/java/org/dllearner/examples/Carcinogenesis.java @@ -0,0 +1,756 @@ +/** + * Copyright (C) 2007-2008, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +package org.dllearner.examples; + +import org.dllearner.parser.KBParser; +import org.dllearner.parser.ParseException; +import org.dllearner.parser.PrologParser; +import org.dllearner.prolog.Atom; +import org.dllearner.prolog.Clause; +import org.dllearner.prolog.Program; +import org.dllearner.utilities.Files; +import org.dllearner.utilities.Helper; +import org.semanticweb.owlapi.apibinding.OWLManager; +import org.semanticweb.owlapi.formats.RDFXMLDocumentFormat; +import org.semanticweb.owlapi.model.*; +import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.*; + +/** + * This class maps the carcinogenesis Prolog files to an OWL file. In a first + * step, a Prolog parser is used to read all files. The main step involves + * applying mapping Prolog clauses to OWL axioms through domain specific mapping + * rules. + * + * The carcinogenesis Prolog files are available here: + * http://web.comlab.ox.ac.uk/oucl/research/areas/machlearn/cancer.html + * + * .f files contain positive and .n files contain negative examples. pte1.n and + * pte.f contain the PTE-1 challenge examples. train.n and train.f contain other + * examples which can be used to train for PTE-1. + * + * The PTE-2 directory contains PTE-2 files, i.e. all substances referred to in + * those files are only those of the PTE-2 challenge. + * + * @author Jens Lehmann + * + */ +public class Carcinogenesis { + + private static IRI ontologyIRI = IRI.create("http://dl-learner.org/carcinogenesis"); + + // directory of Prolog files + private static final String prologDirectory = "../examples/carcinogenesis/prolog/"; + + // mapping of symbols to names of chemical elements + private static Map chemElements; + + // structures in newgroups.pl + private static Set newGroups = new TreeSet(); + + // types of atoms, bonds, and structures + private static Set atomTypes = new TreeSet(); + private static Set bondTypes = new TreeSet(); + private static Set structureTypes = new TreeSet(); + + // we need a counter for bonds, because they are instances in OWL + // but not in Prolog + private static int bondNr = 0; + private static int structureNr = 0; + + // list of all individuals in the knowlege base +// private static Set individuals = new TreeSet(); + // list of all compounds + private static Set compounds = new TreeSet(); + // compounds with positive ames test + private static Set compoundsAmes = new TreeSet(); + // list of all bonds + private static Set bonds = new TreeSet(); + + // list of all "hasProperty" test + private static Set tests = new TreeSet(); + + // we ignore the ames test since its distribution in PTE-2 is so + // different from the training substances that a different testing + // strategy was probably in use + private static boolean ignoreAmes = false; + private static boolean ignoreSalmonella = false;; + private static boolean ignoreCytogenCa = false; + private static boolean includeMutagenesis = true; + // if true we learn carcinogenic, if false we learn non-carcinogenic + private static boolean learnCarcinogenic = true; + private static boolean useNewGroups = true; + + private static boolean createPTE1Conf = false; + private static boolean createPTE2Conf = false; + + static OWLOntologyManager man = OWLManager.createOWLOntologyManager(); + static OWLDataFactory df = new OWLDataFactoryImpl(); + + /** + * @param args + * No arguments supported. + */ + public static void main(String[] args) throws Exception { + + String[] files = new String[] { "newgroups.pl", "ames.pl", "atoms.pl", "bonds.pl", "gentoxprops.pl", + "ind_nos.pl", "ind_pos.pl"}; + // "pte2/canc_nos.pl", "pte2/pte2ames.pl", "pte2/pte2atoms.pl", + // "pte2/pte2bonds.pl", "pte2/pte2gentox.pl", "pte2/pte2ind_nos.pl", "pte2/pte2newgroups.pl" + // "train.b" => not a pure Prolog file but Progol/Aleph specific + // }; + File owlFile = new File("/tmp/carcinogenesis.owl"); + + Program program = null; + long startTime, duration; + String time; + + // reading files + System.out.print("Reading in carcinogenesis Prolog files ... "); + startTime = System.nanoTime(); + String content = ""; + for (String file : files) { + content += Files.readFile(new File(prologDirectory + file)); + } + duration = System.nanoTime() - startTime; + time = Helper.prettyPrintNanoSeconds(duration, false, false); + System.out.println("OK (" + time + ")."); + + // parsing files + System.out.print("Parsing Prolog files ... "); + startTime = System.nanoTime(); + PrologParser pp = new PrologParser(); + program = pp.parseProgram(content); + duration = System.nanoTime() - startTime; + time = Helper.prettyPrintNanoSeconds(duration, false, false); + System.out.println("OK (" + time + ")."); + + // prepare mapping + OWLOntology kb = man.createOntology(); + createChemElementsMapping(); + createNewGroups(); + // create subclasses of atom + OWLClass atomClass = getAtomicConcept("Atom"); + for (String element : chemElements.values()) { + OWLClass elClass = getAtomicConcept(element); + OWLSubClassOfAxiom sc = df.getOWLSubClassOfAxiom(elClass, atomClass); + man.addAxiom(kb, sc); + } + // define properties including domain and range + String kbString = "DPDOMAIN(" + getURI2("charge") + ") = " + getURI2("Atom") + ".\n"; + kbString += "DPRANGE(" + getURI2("charge") + ") = DOUBLE.\n"; + if(!ignoreAmes) { + kbString += "DPDOMAIN(" + getURI2("amesTestPositive") + ") = " + getURI2("Compound") + ".\n"; + kbString += "DPRANGE(" + getURI2("amesTestPositive") + ") = BOOLEAN.\n"; + } + if(includeMutagenesis) { + kbString += "DPDOMAIN(" + getURI2("isMutagenic") + ") = " + getURI2("Compound") + ".\n"; + kbString += "DPRANGE(" + getURI2("isMutagenic") + ") = BOOLEAN.\n"; + } + kbString += "OPDOMAIN(" + getURI2("hasAtom") + ") = " + getURI2("Compound") + ".\n"; + kbString += "OPRANGE(" + getURI2("hasAtom") + ") = " + getURI2("Atom") + ".\n"; + kbString += "OPDOMAIN(" + getURI2("hasBond") + ") = " + getURI2("Compound") + ".\n"; + kbString += "OPRANGE(" + getURI2("hasBond") + ") = " + getURI2("Bond") + ".\n"; + kbString += "OPDOMAIN(" + getURI2("inBond") + ") = " + getURI2("Bond") + ".\n"; + kbString += "OPRANGE(" + getURI2("inBond") + ") = " + getURI2("Atom") + ".\n"; + kbString += "OPDOMAIN(" + getURI2("hasStructure") + ") = " + getURI2("Compound") + ".\n"; + kbString += "OPRANGE(" + getURI2("hasStructure") + ") = " + getURI2("Structure") + ".\n"; + kbString += getURI2("Di") + " SUB " + getURI2("Structure") + ".\n"; + kbString += getURI2("Halide") + " SUB " + getURI2("Structure") + ".\n"; + kbString += getURI2("Ring") + " SUB " + getURI2("Structure") + ".\n"; + OWLOntology kb2 = KBParser.parseKBFile(kbString); + man.addAxioms(kb, kb2.getAxioms()); + + // mapping clauses to axioms + System.out.print("Mapping clauses to axioms ... "); + startTime = System.nanoTime(); + ArrayList clauses = program.getClauses(); + for (Clause clause : clauses) { + List axioms = mapClause(clause); + for (OWLAxiom axiom : axioms) + man.addAxiom(kb, axiom); + } + + if(includeMutagenesis) + addMutagenesis(kb); + + // special handling for ames test (we assume the ames test + // was performed on all compounds but only the positive ones + // are in ames.pl [the rest is negative in Prolog by CWA], so + // we add negative test results here) + for(String compound : compounds) { + if(!ignoreAmes && !compoundsAmes.contains(compound)) { + OWLAxiom ames = getBooleanDatatypePropertyAssertion(compound, "amesTestPositive", false); + man.addAxiom(kb, ames); + } + } + + // disjoint classes axioms + // OWL API is also buggy here, it adds a strange unused prefix + // and cannot parser its own generated file +// DisjointClassesAxiom disjointAtomTypes = getDisjointClassesAxiom(atomTypes); +// kb.addAxiom(disjointAtomTypes); + String[] mainClasses = new String[] {"Compound", "Atom", "Bond", "Structure"}; + Set mainClassesSet = new HashSet(Arrays.asList(mainClasses)); + OWLAxiom disjointAtomTypes = getDisjointClassesAxiom(mainClassesSet); + man.addAxiom(kb, disjointAtomTypes); + + // all different axiom (UNA) + // exporting differentIndividuals axioms is broken in OWL API +// individuals.addAll(compounds); +// individuals.addAll(bonds); +// DifferentIndividualsAxiom una = getDifferentIndividualsAxiom(individuals); +// kb.addAxiom(una); + + duration = System.nanoTime() - startTime; + time = Helper.prettyPrintNanoSeconds(duration, false, false); + System.out.println("OK (" + time + ")."); + + // writing generated knowledge base + System.out.print("Writing OWL file ... "); + startTime = System.nanoTime(); + man.saveOntology(kb, new RDFXMLDocumentFormat(), new FileOutputStream(owlFile)); + duration = System.nanoTime() - startTime; + time = Helper.prettyPrintNanoSeconds(duration, false, false); + System.out.println("OK (" + time + ")."); + + // generating conf files + File confTrainFile = new File("examples/carcinogenesis/train.conf"); + Files.clearFile(confTrainFile); + String confHeader = "import(\"carcinogenesis.owl\");\n\n"; + confHeader += "reasoner = fastInstanceChecker;\n"; + confHeader += "algorithm = refexamples;\n"; + confHeader += "refexamples.noisePercentage = 31;\n"; + confHeader += "refexamples.startClass = " + getURI2("Compound") + ";\n"; + confHeader += "refexamples.writeSearchTree = false;\n"; + confHeader += "refexamples.searchTreeFile = \"log/carcinogenesis/searchTree.log\";\n"; + confHeader += "\n"; + Files.appendToFile(confTrainFile, confHeader); + + // generating training examples + File trainingFilePositives = new File(prologDirectory + "train.f"); + File trainingFileNegatives = new File(prologDirectory + "train.n"); + + List posTrainExamples = getExamples(trainingFilePositives); + List negTrainExamples = getExamples(trainingFileNegatives); + appendPosExamples(confTrainFile, posTrainExamples); + appendNegExamples(confTrainFile, negTrainExamples); + + // generating test examples for PTE-1 + // => put all in one file, because they were used as training for PTE-2 + File confPTE1File = new File("examples/carcinogenesis/testpte1.conf"); + File testPTE1Positives = new File(prologDirectory + "pte1.f"); + File testPTE1Negatives = new File(prologDirectory + "pte1.n"); + + List posPTE1Examples = getExamples(testPTE1Positives); + List negPTE1Examples = getExamples(testPTE1Negatives); + appendPosExamples(confTrainFile, posPTE1Examples); + appendNegExamples(confTrainFile, negPTE1Examples); + if(createPTE1Conf) { + Files.clearFile(confPTE1File); + Files.appendToFile(confPTE1File, "import(\"pte.owl\");\nreasoner=fastInstanceChecker;\n\n"); + appendPosExamples(confPTE1File, posPTE1Examples); + appendNegExamples(confPTE1File, negPTE1Examples); + } + + // create a PTE-2 test file + if(createPTE2Conf) { + File confPTE2File = new File("examples/carcinogenesis/testpte2.conf"); + Files.clearFile(confPTE2File); + Files.appendToFile(confPTE2File, "import(\"pte.owl\");\nreasoner=fastInstanceChecker;\n\n"); + Files.appendToFile(confPTE2File, getPTE2Examples()); + } + + } + + private static List mapClause(Clause clause) throws IOException, ParseException, OWLOntologyCreationException { + List axioms = new LinkedList(); + Atom head = clause.getHead(); + String headName = head.getName(); + // Body body = clause.getBody(); + // ArrayList literals = body.getLiterals(); + // handle: atm(compound,atom,element,atomtype,charge) + + // Ames-Test: http://en.wikipedia.org/wiki/Ames_test + // problem: the file apparently mentions only positive + // tests (why is it different from the other tests e.g. in + // gentoxprops.pl?) => we need to add negative axioms for the + // remaining stuff or use closed world assumption in the + // TBox dematerialisation later on + if(headName.equals("ames")) { + if(!ignoreAmes) { + String compoundName = head.getArgument(0).toPLString(); + OWLAxiom ames = getBooleanDatatypePropertyAssertion(compoundName, "amesTestPositive", true); + axioms.add(ames); + compoundsAmes.add(compoundName); + } + } else if (headName.equals("atm")) { + String compoundName = head.getArgument(0).toPLString(); + String atomName = head.getArgument(1).toPLString(); + String elementName = head.getArgument(2).toPLString(); + String type = head.getArgument(3).toPLString(); + double charge = Double.parseDouble(head.getArgument(4).toPLString()); + // make the compound an instance of the Compound class + OWLAxiom cmpAxiom = getConceptAssertion("Compound", compoundName); + axioms.add(cmpAxiom); + compounds.add(compoundName); + // relate compound and atom + OWLAxiom ra = getRoleAssertion("hasAtom", compoundName, atomName); + axioms.add(ra); + // atom is made instance of the correct class + String atomClass = getAtomClass(elementName, type); + OWLAxiom ca = getConceptAssertion(atomClass, atomName); + axioms.add(ca); + // write subclass axiom if doesn't exist already + if (!atomTypes.contains(atomClass)) { + OWLClass subClass = getAtomicConcept(atomClass); + OWLClass superClass = getAtomicConcept(getFullElementName(elementName)); + OWLAxiom sc = df.getOWLSubClassOfAxiom(subClass, superClass); + axioms.add(sc); + atomTypes.add(atomClass); + } + // charge of atom + OWLAxiom dpa = getDoubleDatatypePropertyAssertion(atomName, "charge", + charge); + axioms.add(dpa); + } else if (headName.equals("bond")) { + String compoundName = head.getArgument(0).toPLString(); + String atom1Name = head.getArgument(1).toPLString(); + String atom2Name = head.getArgument(2).toPLString(); + String bondType = head.getArgument(3).toPLString(); + String bondClass = "Bond-" + bondType; + String bondInstance = "bond" + bondNr; + bonds.add(bondInstance); + OWLAxiom op = getRoleAssertion("hasBond", compoundName, "bond" + bondNr); + axioms.add(op); + // make Bond-X subclass of Bond if that hasn't been done already + if (!bondTypes.contains(bondClass)) { + OWLClass subClass = getAtomicConcept(bondClass); + OWLAxiom sc = df.getOWLSubClassOfAxiom(subClass, getAtomicConcept("Bond")); + axioms.add(sc); + bondTypes.add(bondClass); + } + // make e.g. bond382 instance of Bond-3 + OWLAxiom ca = getConceptAssertion(bondClass, bondInstance); + axioms.add(ca); + bondNr++; + // connect atoms with bond + OWLAxiom op1 = getRoleAssertion("inBond", bondInstance, atom1Name); + OWLAxiom op2 = getRoleAssertion("inBond", bondInstance, atom2Name); + axioms.add(op1); + axioms.add(op2); + } else if (headName.equals("has_property")) { + String compoundName = head.getArgument(0).toPLString(); + String testName = head.getArgument(1).toPLString(); + if(!(ignoreSalmonella && testName.equals("salmonella")) + && !(ignoreCytogenCa && testName.equals("cytogen_ca"))) { + String resultStr = head.getArgument(2).toPLString(); + boolean testResult = (resultStr.equals("p")) ? true : false; + + // create a new datatype property if it does not exist already + if(!tests.contains(testName)) { + String axiom1 = "DPDOMAIN(" + getURI2(testName) + ") = " + getURI2("Compound") + ".\n"; + String axiom2 = "DPRANGE(" + getURI2(testName) + ") = BOOLEAN.\n"; + OWLOntology kb = KBParser.parseKBFile(axiom1 + axiom2); + axioms.addAll(kb.getAxioms()); + } + // create an axiom with the test result + OWLAxiom dpa = getBooleanDatatypePropertyAssertion(compoundName, testName, + testResult); + axioms.add(dpa); + } + // either parse this or ashby_alert - not both - ashby_alert contains + // all information in ind already + } else if (headName.equals("ind") || headName.equals("ring_no")) { + // parse this only if the new groups are not parsed +// if(!useNewGroups) { + String compoundName = head.getArgument(0).toPLString(); + String structureName = head.getArgument(1).toPLString(); + int count = Integer.parseInt(head.getArgument(2).toPLString()); + // upper case first letter + String structureClass = structureName.substring(0,1).toUpperCase() + structureName.substring(1);; + String structureInstance = structureName + "-" + structureNr; + + addStructureSubclass(axioms, structureClass); + + for(int i=0; i axioms, String structureClass) { + // build in more fine-grained subclasses e.g. Di+number is subclass of Di + if (!structureTypes.contains(structureClass)) { + OWLClass nc = getAtomicConcept("Structure"); + if(structureClass.contains("Di")) + nc = getAtomicConcept("Di"); + else if(structureClass.contains("ring") || structureClass.contains("Ring")) + nc = getAtomicConcept("Ring"); + else if(structureClass.contains("halide") || structureClass.contains("Halide")) + nc = getAtomicConcept("Halide"); + OWLClass subClass = getAtomicConcept(structureClass); + OWLAxiom sc = df.getOWLSubClassOfAxiom(subClass, nc); + axioms.add(sc); + structureTypes.add(structureClass); + } + } + + // takes a *.f or *.n file as input and returns the + // contained examples + private static List getExamples(File file) throws FileNotFoundException, IOException, ParseException { + String content = Files.readFile(file); + PrologParser pp = new PrologParser(); + Program programPos = pp.parseProgram(content); + List ret = new LinkedList(); + for(Clause c : programPos.getClauses()) { + String example = c.getHead().getArgument(0).toPLString(); + ret.add(getIndividual(example)); + } + return ret; + } + + public static void appendPosExamples(File file, List examples) { + StringBuffer content = new StringBuffer(); + for(OWLIndividual example : examples) { + if(learnCarcinogenic) + content.append("+\""+example.toString()+"\"\n"); + else + content.append("-\""+example.toString()+"\"\n"); + } + Files.appendToFile(file, content.toString()); + } + + public static void appendNegExamples(File file, List examples) { + StringBuffer content = new StringBuffer(); + for(OWLIndividual example : examples) { + if(learnCarcinogenic) + content.append("-\""+example.toString()+"\"\n"); + else + content.append("+\""+example.toString()+"\"\n"); + } + Files.appendToFile(file, content.toString()); + } + + private static String getAtomClass(String element, String atomType) { + return getFullElementName(element) + "-" + atomType; + } + + private static OWLAxiom getConceptAssertion(String concept, String i) { + OWLIndividual ind = getIndividual(i); + OWLClass c = getAtomicConcept(concept); + return df.getOWLClassAssertionAxiom(c, ind); + } + + private static OWLAxiom getRoleAssertion(String role, String i1, String i2) { + OWLIndividual ind1 = getIndividual(i1); + OWLIndividual ind2 = getIndividual(i2); + OWLObjectProperty ar = getRole(role); + return df.getOWLObjectPropertyAssertionAxiom(ar, ind1, ind2); + } + + private static OWLAxiom getBooleanDatatypePropertyAssertion( + String individual, String datatypeProperty, boolean value) { + OWLIndividual ind = getIndividual(individual); + OWLDataProperty dp = getDatatypeProperty(datatypeProperty); + return df.getOWLDataPropertyAssertionAxiom(dp, ind, value); + } + + private static OWLAxiom getDoubleDatatypePropertyAssertion( + String individual, String datatypeProperty, double value) { + OWLIndividual ind = getIndividual(individual); + OWLDataProperty dp = getDatatypeProperty(datatypeProperty); + return df.getOWLDataPropertyAssertionAxiom(dp, ind, value); + } + + private static OWLAxiom getDisjointClassesAxiom(Set classes) { + Set descriptions = new HashSet(); + for(String namedClass : classes) + descriptions.add(df.getOWLClass(IRI.create(getURI(namedClass)))); + return df.getOWLDisjointClassesAxiom(descriptions); + } + + @SuppressWarnings({"unused"}) + private static OWLAxiom getDifferentIndividualsAxiom(Set individuals) { + Set inds = new HashSet(); + for(String i : individuals) + inds.add(getIndividual(i)); + return df.getOWLDifferentIndividualsAxiom(inds); + } + + private static OWLIndividual getIndividual(String name) { + return df.getOWLNamedIndividual(IRI.create(ontologyIRI + "#" + name)); + } + + private static OWLObjectProperty getRole(String name) { + return df.getOWLObjectProperty(IRI.create(ontologyIRI + "#" + name)); + } + + private static OWLDataProperty getDatatypeProperty(String name) { + return df.getOWLDataProperty(IRI.create(ontologyIRI + "#" + name)); + } + + private static OWLClass getAtomicConcept(String name) { + return df.getOWLClass(IRI.create(ontologyIRI + "#" + name)); + } + + private static String getURI(String name) { + return ontologyIRI + "#" + name; + } + + // returns URI including quotationsmark (need for KBparser) + private static String getURI2(String name) { + return "\"" + getURI(name) + "\""; + } + + private static String getFullElementName(String abbreviation) { + // return corresponding element or throw an error if it + // is not in the list + String result = chemElements.get(abbreviation); + if (result == null) + throw new Error("Unknown element " + abbreviation); + else + return result; + } + + // create chemical element list + private static void createChemElementsMapping() { + chemElements = new HashMap(); + chemElements.put("as", "Arsenic"); + chemElements.put("ba", "Barium"); + chemElements.put("br", "Bromine"); + chemElements.put("c", "Carbon"); + chemElements.put("ca", "Calcium"); + chemElements.put("cl", "Chlorine"); + chemElements.put("cu", "Copper"); + chemElements.put("f", "Fluorine"); + chemElements.put("ga", "Gallium"); + chemElements.put("h", "Hydrogen"); + chemElements.put("hg", "Mercury"); + chemElements.put("i", "Iodine"); + chemElements.put("k", "Krypton"); + chemElements.put("mn", "Manganese"); + chemElements.put("mo", "Molybdenum"); + chemElements.put("n", "Nitrogen"); + chemElements.put("na", "Sodium"); + chemElements.put("o", "Oxygen"); + chemElements.put("p", "Phosphorus"); + chemElements.put("pb", "Lead"); + chemElements.put("s", "Sulfur"); + chemElements.put("se", "Selenium"); + chemElements.put("sn", "Tin"); + chemElements.put("te", "Tellurium"); + chemElements.put("ti", "Titanium"); + chemElements.put("v", "Vanadium"); + chemElements.put("zn", "Zinc"); + } + + private static void createNewGroups() { + String[] groups = new String[] {"six_ring", "non_ar_6c_ring", + "ketone", "amine", "alcohol", "ether", "ar_halide", + "five_ring", "non_ar_5c_ring", "alkyl_halide", + "methyl", "non_ar_hetero_5_ring", "nitro", "sulfo", + "methoxy", "amine", "aldehyde", "sulfide", + "non_ar_hetero_6_ring", "phenol", "carboxylic_acid", + "ester", "imine", + }; + + List list = Arrays.asList(groups); + newGroups.addAll(list); + } + + /** + *

    To find out whether a substance is carinogenetic go to + * "http://ntp-server.niehs.nih.gov/" and click + * on "Testing Status of Agents at NTP".

    + * + * Levels: + *
      + *
    • CE = clear evidence
    • + *
    • SE = some evidence
    • + *
    • E = equivocal evidence
    • + *
    • NE = no evidence
    • + *
    + * Levels CE and SE are positive examples. E and NE negative examples. + * Experiments are performed on rats and mice of both genders, so we + * have four evidence values. An example is positive if at least one + * value is SE or CE. + * + *

    Some values are taken from the IJCAI-97 paper of Muggleton.

    + * + *

    Positives (19):
    + *

      + *
    • t3 (SE+3NE): http://ntp.niehs.nih.gov/index.cfm?objectid=BCACAFD4-123F-7908-7B521E4F665EFBD9
    • + *
    • t4 (3CE+NE) - contradicts IJCAI-97 paper and should probably be case 75-52-5 instead of 75-52-8: http://ntp.niehs.nih.gov/index.cfm?objectid=BCE49084-123F-7908-7BE127F7AF1FFBB5
    • + *
    • t5: paper
    • + *
    • t7: paper
    • + *
    • t8: paper
    • + *
    • t9 (3CE+SE): http://ntp.niehs.nih.gov/index.cfm?objectid=BD7C6869-123F-7908-7BDEA4CFAA55CEA8
    • + *
    • t10: paper
    • + *
    • t12 (2SE+E+NE): http://ntp.niehs.nih.gov/index.cfm?objectid=BCB0ADE0-123F-7908-7BEC101C7309C4DE
    • + *
    • t14 (2CE+2NE) probably 111-42-2 instead of 11-42-2: http://ntp.niehs.nih.gov/index.cfm?objectid=BCC60FF1-123F-7908-7B2D579AA48DE90C
    • + *
    • t15: paper
    • + *
    • t16 (2CE+SE+E): http://ntp.niehs.nih.gov/index.cfm?objectid=BCC5D9CE-123F-7908-7B959CCE5262468A
    • + *
    • t18 (2SE+E+NE): http://ntp.niehs.nih.gov/index.cfm?objectid=BCA087AA-123F-7908-7B79FDFDE3CDCF87
    • + *
    • t19 (2CE+E+NE): http://ntp.niehs.nih.gov/index.cfm?objectid=BCAE5690-123F-7908-7B02E35E2BB57694
    • + *
    • t20 (2SE+E+NE): http://ntp.niehs.nih.gov/index.cfm?objectid=BCF95607-123F-7908-7B0761D3C515CC12
    • + *
    • t21 (CE+3NE): http://ntp.niehs.nih.gov/index.cfm?objectid=BCFCB63C-123F-7908-7BF910C2783AE9FE
    • + *
    • t22 (SE+3NE): http://ntp.niehs.nih.gov/index.cfm?objectid=BD8345C2-123F-7908-7BC52FEF80F110E1
    • + *
    • t23 (4CE): http://ntp.niehs.nih.gov/index.cfm?objectid=BCADD2D9-123F-7908-7B5C8180FE80B22F
    • + *
    • t24 (CE+E): http://ntp.niehs.nih.gov/index.cfm?objectid=BCFB19FF-123F-7908-7B845E176F13E6E1
    • + *
    • t25 (3CE+SE): http://ntp.niehs.nih.gov/index.cfm?objectid=BD2D2A62-123F-7908-7B0DA824E782754C
    • + *
    • t30 (2CE+SE+E) : http://ntp.niehs.nih.gov/index.cfm?objectid=BCB13734-123F-7908-7BEBA533E35A48B7
    • + *
    + *

    + * + *

    Negatives (10): + *

      + *
    • t1 (4NE): http://ntp.niehs.nih.gov/index.cfm?objectid=BD9FF53C-123F-7908-7B123DAE0A25B122
    • + *
    • t2 (4NE): http://ntp.niehs.nih.gov/index.cfm?objectid=BCF8651E-123F-7908-7B21DD5ED83CD0FF
    • + *
    • t4: paper
    • + *
    • t6: paper
    • + *
    • t11: paper
    • + *
    • t13 (4NE): http://ntp.niehs.nih.gov/index.cfm?objectid=BD136ED6-123F-7908-7B619EE79F2FD062
    • + *
    • t17: paper
    • + *
    • t26 (2E+2NE): http://ntp.niehs.nih.gov/index.cfm?objectid=BD1E6209-123F-7908-7B95EB8BAE662CE7
    • + *
    • t27 (E+3NE): http://ntp.niehs.nih.gov/index.cfm?objectid=BCAC5D00-123F-7908-7BC46ECB72A6C91B
    • + *
    • t28 (E+3NE): http://ntp.niehs.nih.gov/index.cfm?objectid=BD34E02A-123F-7908-7BC6791917B591DF
    • + *
    + *

    + * + *

    Unclear (1): + *

      + *
    • t29: probably a negative (see http://ntp.niehs.nih.gov/index.cfm?objectid=BD855EA1-123F-7908-7B573FC3C08188DC) but + * no tests directly for this substance
    • + *
    + * + *

    The following examples are probably not part of the IJCAI PTE-2 challenge + * (reports younger than 1998): + *

      + *
    • pos: t21 (5/99), t25 (9/04), t30(10/01)
    • + *
    • neg: t26 (5/99), t27 (05/01), t28 (05/00), t29 (09/02)
    • + *
    + *

    + *

    + * @return A string for all examples as used in the conf file. + */ + public static String getPTE2Examples() { + String[] pos = new String[] {"t3","t4","t5","t7","t8", + "t9", + "t10","t12", + "t14","t15","t16","t18","t19","t20", + "t21", + "t22", + "t23", + "t24", + "t25", + "t30"}; + String[] neg = new String[] {"t1", "t2", + "t6", "t11", "t13", + "t17","t26","t27", + "t28","t29" + }; + + String ret = ""; + for(String posEx : pos) { + if(learnCarcinogenic) + ret += "+" + getURI2(posEx) + "\n"; + else + ret += "-" + getURI2(posEx) + "\n"; + } + for(String negEx : neg) { + if(learnCarcinogenic) + ret += "-" + getURI2(negEx) + "\n"; + else + ret += "+" + getURI2(negEx) + "\n"; + } + + return ret; + } + + private static void addMutagenesis(OWLOntology kb) { + String[] mutagenicCompounds = new String[] { + "d101", "d104", "d106", "d107", "d112", "d113", "d117", + "d121", "d123", "d126", "d128", "d13", "d135", "d137", + "d139", "d140", "d143", "d144", "d145", "d146", "d147", + "d152", "d153", "d154", "d155", "d156", "d159", "d160", + "d161", "d163", "d164", "d166", "d168", "d171", "d173", + "d174", "d177", "d179", "d18", "d180", "d182", "d183", + "d185", "d186", "d187", "d188", "d189", "d19", "d191", + "d192", "d193", "d195", "d197", "d2", "d201", "d202", + "d205", "d206", "d207", "d211", "d214", "d215", "d216", + "d224", "d225", "d227", "d228", "d229", "d231", "d235", + "d237", "d239", "d242", "d245", "d246", "d249", "d251", + "d254", "d257", "d258", "d261", "d264", "d266", "d269", + "d27", "d270", "d271", "d28", "d288", "d292", "d297", + "d300", "d308", "d309", "d311", "d313", "d314", "d322", + "d323", "d324", "d329", "d330", "d332", "d334", "d35", + "d36", "d37", "d38", "d41", "d42", "d48", "d50", "d51", + "d54", "d58", "d61", "d62", "d63", "d66", "d69", "d72", + "d76", "d77", "d78", "d84", "d86", "d89", "d92", "d96"}; + TreeSet mutagenic = new TreeSet(Arrays.asList(mutagenicCompounds)); + + for(String compound : compounds) { + if(mutagenic.contains(compound)) { + OWLAxiom muta = getBooleanDatatypePropertyAssertion(compound, "isMutagenic", true); + man.addAxiom(kb, muta); + } else { + OWLAxiom muta = getBooleanDatatypePropertyAssertion(compound, "isMutagenic", false); + man.addAxiom(kb, muta); + } + } + } +} \ No newline at end of file diff --git a/scripts/src/main/java/org/dllearner/examples/MouseDiabetes.java b/scripts/src/main/java/org/dllearner/examples/MouseDiabetes.java index a0d6dcaaed..63589a6471 100644 --- a/scripts/src/main/java/org/dllearner/examples/MouseDiabetes.java +++ b/scripts/src/main/java/org/dllearner/examples/MouseDiabetes.java @@ -28,10 +28,13 @@ import org.dllearner.reasoning.OWLAPIReasoner; import org.dllearner.refinementoperators.RhoDRDown; import org.dllearner.scripts.MouseDiabetesCBD; +import org.dllearner.utilities.OWLAPIUtils; +import org.dllearner.utilities.OwlApiJenaUtils; import org.dllearner.utilities.owl.DLSyntaxObjectRenderer; import org.semanticweb.elk.owlapi.ElkReasoner; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.io.ToStringRenderer; +import org.dllearner.core.StringRenderer; +import org.dllearner.core.StringRenderer.Rendering; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLClass; import org.semanticweb.owlapi.model.OWLClassExpression; @@ -190,7 +193,7 @@ public static Set readExamples(String filePath) throws IOExceptio private static void setUp() { logger.setLevel(Level.DEBUG); Logger.getLogger(AbstractReasonerComponent.class).setLevel(Level.OFF); - ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); + StringRenderer.setRenderer(Rendering.DL_SYNTAX); } public static OWLOntology readCBD() { @@ -234,7 +237,7 @@ public static OWLOntology readDumpFiles() throws OWLOntologyCreationException, I //convert JENA model to OWL API ontology logger.debug("converting to OWLApi ontology..."); - OWLOntology ontology = getOWLOntology(model); + OWLOntology ontology = OwlApiJenaUtils.getOWLOntology(model); logger.debug("finished conversion"); // sanity check @@ -252,27 +255,4 @@ private static boolean containsErrorNamedClasses(OWLOntology ontology){ } return false; } - - public static OWLOntology getOWLOntology(final Model model) { - OWLOntology ontology; - try (PipedInputStream is = new PipedInputStream(); - PipedOutputStream os = new PipedOutputStream(is);) { - OWLOntologyManager man = OWLManager.createOWLOntologyManager(); - new Thread(new Runnable() { - public void run() { - model.write(os, "TURTLE", null); - try { - os.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - }).start(); - ontology = man.loadOntologyFromOntologyDocument(is); - return ontology; - } catch (Exception e) { - throw new RuntimeException( - "Could not convert JENA API model to OWL API ontology.", e); - } - } } diff --git a/scripts/src/main/java/org/dllearner/examples/MouseDiabetes2.java b/scripts/src/main/java/org/dllearner/examples/MouseDiabetes2.java index a002a006ff..1fa7b9289d 100644 --- a/scripts/src/main/java/org/dllearner/examples/MouseDiabetes2.java +++ b/scripts/src/main/java/org/dllearner/examples/MouseDiabetes2.java @@ -29,10 +29,12 @@ import org.dllearner.reasoning.OWLAPIReasoner; import org.dllearner.refinementoperators.RhoDRDown; import org.dllearner.scripts.MouseDiabetesCBD; +import org.dllearner.utilities.OwlApiJenaUtils; import org.dllearner.utilities.owl.DLSyntaxObjectRenderer; import org.semanticweb.elk.owlapi.ElkReasoner; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.io.ToStringRenderer; +import org.dllearner.core.StringRenderer; +import org.dllearner.core.StringRenderer.Rendering; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLClass; import org.semanticweb.owlapi.model.OWLClassExpression; @@ -227,7 +229,7 @@ public static Set readExamples(String filePath) throws IOExceptio private static void setUp() { logger.setLevel(Level.DEBUG); Logger.getLogger(AbstractReasonerComponent.class).setLevel(Level.OFF); - ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); + StringRenderer.setRenderer(Rendering.DL_SYNTAX); } public static OWLOntology readCBD() { @@ -271,7 +273,7 @@ public static OWLOntology readDumpFiles() throws OWLOntologyCreationException, I //convert JENA model to OWL API ontology logger.debug("converting to OWLApi ontology..."); - OWLOntology ontology = getOWLOntology(model); + OWLOntology ontology = OwlApiJenaUtils.getOWLOntology(model); logger.debug("finished conversion"); // sanity check @@ -289,28 +291,4 @@ private static boolean containsErrorNamedClasses(OWLOntology ontology){ } return false; } - - public static OWLOntology getOWLOntology(final Model model) { - OWLOntology ontology; - try (PipedInputStream is = new PipedInputStream(); - PipedOutputStream os = new PipedOutputStream(is);) { - OWLOntologyManager man = OWLManager.createOWLOntologyManager(); - new Thread(new Runnable() { - @Override - public void run() { - model.write(os, "TURTLE", null); - try { - os.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - }).start(); - ontology = man.loadOntologyFromOntologyDocument(is); - return ontology; - } catch (Exception e) { - throw new RuntimeException( - "Could not convert JENA API model to OWL API ontology.", e); - } - } } diff --git a/scripts/src/main/java/org/dllearner/examples/MouseDiabetesExp3CaI.java b/scripts/src/main/java/org/dllearner/examples/MouseDiabetesExp3CaI.java index 18e1cc90c5..107454084c 100644 --- a/scripts/src/main/java/org/dllearner/examples/MouseDiabetesExp3CaI.java +++ b/scripts/src/main/java/org/dllearner/examples/MouseDiabetesExp3CaI.java @@ -20,7 +20,8 @@ import org.dllearner.refinementoperators.RhoDRDown; import org.dllearner.utilities.owl.DLSyntaxObjectRenderer; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.io.ToStringRenderer; +import org.dllearner.core.StringRenderer; +import org.dllearner.core.StringRenderer.Rendering; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLAxiom; import org.semanticweb.owlapi.model.OWLClass; @@ -167,7 +168,7 @@ public static Set readExamples(String filePath) throws IOException { private static void setUp() { logger.setLevel(Level.DEBUG); // Logger.getLogger(AbstractReasonerComponent.class).setLevel(Level.OFF); - ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); + StringRenderer.setRenderer(Rendering.DL_SYNTAX); } public static OWLOntology readDumpFiles() throws diff --git a/scripts/src/main/java/org/dllearner/examples/Reactome.java b/scripts/src/main/java/org/dllearner/examples/Reactome.java index fb6e4bfd81..afe3f42103 100644 --- a/scripts/src/main/java/org/dllearner/examples/Reactome.java +++ b/scripts/src/main/java/org/dllearner/examples/Reactome.java @@ -20,12 +20,13 @@ import org.dllearner.reasoning.ClosedWorldReasoner; import org.dllearner.reasoning.OWLAPIReasoner; import org.dllearner.reasoning.ReasonerImplementation; -import org.dllearner.refinementoperators.ELDown3; +import org.dllearner.refinementoperators.ELDown; import org.dllearner.refinementoperators.RhoDRDown; import org.dllearner.utilities.owl.DLSyntaxObjectRenderer; import org.semanticweb.elk.owlapi.ElkReasoner; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.io.ToStringRenderer; +import org.dllearner.core.StringRenderer; +import org.dllearner.core.StringRenderer.Rendering; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLIndividual; import org.semanticweb.owlapi.model.OWLOntology; @@ -3252,8 +3253,8 @@ public static void main(String[] args) throws Exception { private static void setUp() { logger.setLevel(Level.DEBUG); Logger.getLogger(AbstractReasonerComponent.class).setLevel(Level.OFF); - Logger.getLogger(ELDown3.class).setLevel(Level.TRACE); - ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); + Logger.getLogger(ELDown.class).setLevel(Level.TRACE); + StringRenderer.setRenderer(Rendering.DL_SYNTAX); } private static Set makeIndividuals(List uris) { diff --git a/scripts/src/main/java/org/dllearner/examples/ReactomeMinimal.java b/scripts/src/main/java/org/dllearner/examples/ReactomeMinimal.java index b932ff1414..382606fdeb 100644 --- a/scripts/src/main/java/org/dllearner/examples/ReactomeMinimal.java +++ b/scripts/src/main/java/org/dllearner/examples/ReactomeMinimal.java @@ -24,7 +24,8 @@ import org.dllearner.utilities.owl.DLSyntaxObjectRenderer; import org.semanticweb.elk.owlapi.ElkReasoner; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.io.ToStringRenderer; +import org.dllearner.core.StringRenderer; +import org.dllearner.core.StringRenderer.Rendering; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLIndividual; import org.semanticweb.owlapi.model.OWLOntology; @@ -152,7 +153,7 @@ private static void run() throws OWLOntologyCreationException, ComponentInitExce private static void setUp() { logger.setLevel(Level.DEBUG); Logger.getLogger(AbstractReasonerComponent.class).setLevel(Level.OFF); - ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); + StringRenderer.setRenderer(Rendering.DL_SYNTAX); } diff --git a/components-core/src/main/java/org/dllearner/utilities/experiments/ExMakerCrossFolds.java b/scripts/src/main/java/org/dllearner/experiments/ExMakerCrossFolds.java similarity index 98% rename from components-core/src/main/java/org/dllearner/utilities/experiments/ExMakerCrossFolds.java rename to scripts/src/main/java/org/dllearner/experiments/ExMakerCrossFolds.java index c2e992451d..732423fb88 100644 --- a/components-core/src/main/java/org/dllearner/utilities/experiments/ExMakerCrossFolds.java +++ b/scripts/src/main/java/org/dllearner/experiments/ExMakerCrossFolds.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package org.dllearner.utilities.experiments; +package org.dllearner.experiments; import java.util.ArrayList; import java.util.Collections; diff --git a/components-core/src/main/java/org/dllearner/utilities/experiments/ExMakerFixedSize.java b/scripts/src/main/java/org/dllearner/experiments/ExMakerFixedSize.java similarity index 98% rename from components-core/src/main/java/org/dllearner/utilities/experiments/ExMakerFixedSize.java rename to scripts/src/main/java/org/dllearner/experiments/ExMakerFixedSize.java index 5c91b50330..95ce115956 100644 --- a/components-core/src/main/java/org/dllearner/utilities/experiments/ExMakerFixedSize.java +++ b/scripts/src/main/java/org/dllearner/experiments/ExMakerFixedSize.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package org.dllearner.utilities.experiments; +package org.dllearner.experiments; import java.util.Random; import java.util.SortedSet; diff --git a/components-core/src/main/java/org/dllearner/utilities/experiments/ExMakerRandomizer.java b/scripts/src/main/java/org/dllearner/experiments/ExMakerRandomizer.java similarity index 99% rename from components-core/src/main/java/org/dllearner/utilities/experiments/ExMakerRandomizer.java rename to scripts/src/main/java/org/dllearner/experiments/ExMakerRandomizer.java index 4b39ce5c7b..387d818ed8 100644 --- a/components-core/src/main/java/org/dllearner/utilities/experiments/ExMakerRandomizer.java +++ b/scripts/src/main/java/org/dllearner/experiments/ExMakerRandomizer.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package org.dllearner.utilities.experiments; +package org.dllearner.experiments; import java.util.ArrayList; import java.util.List; diff --git a/components-core/src/main/java/org/dllearner/utilities/experiments/Examples.java b/scripts/src/main/java/org/dllearner/experiments/Examples.java similarity index 96% rename from components-core/src/main/java/org/dllearner/utilities/experiments/Examples.java rename to scripts/src/main/java/org/dllearner/experiments/Examples.java index 4640970726..1f5153aeaf 100644 --- a/components-core/src/main/java/org/dllearner/utilities/experiments/Examples.java +++ b/scripts/src/main/java/org/dllearner/experiments/Examples.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package org.dllearner.utilities.experiments; +package org.dllearner.experiments; import java.io.FileWriter; import java.text.DecimalFormat; @@ -27,9 +27,10 @@ import java.util.TreeSet; import org.apache.log4j.Logger; -import org.dllearner.utilities.Helper; import org.dllearner.utilities.URLencodeUTF8; +import com.google.common.collect.Sets; + /** * a container for examples used for operations like randomization * @@ -96,7 +97,7 @@ public double precision(SortedSet retrieved) { retrievedClean.removeAll(posTrain); retrievedClean.removeAll(negTrain); - int posAsPos = Helper.intersection(retrievedClean, getPosTest()).size(); + int posAsPos = Sets.intersection(retrievedClean, getPosTest()).size(); return ((double) posAsPos) / ((double) retrievedClean.size()); } @@ -111,7 +112,7 @@ public double recall(SortedSet retrieved) { if (sizeTotalOfPositives() == 0) { return 0.0d; } - int posAsPos = Helper.intersection(getPosTest(), retrieved).size(); + int posAsPos = Sets.intersection(getPosTest(), retrieved).size(); return ((double) posAsPos) / ((double) posTest.size()); } @@ -235,7 +236,7 @@ public void writeExamples(String filename) { } /** - * sum of training and test data + * sum of training and test data * @return */ public int size() { diff --git a/components-core/src/main/java/org/dllearner/utilities/experiments/ExperimentConfiguration.java b/scripts/src/main/java/org/dllearner/experiments/ExperimentConfiguration.java similarity index 99% rename from components-core/src/main/java/org/dllearner/utilities/experiments/ExperimentConfiguration.java rename to scripts/src/main/java/org/dllearner/experiments/ExperimentConfiguration.java index 7a26628478..1e44ee7261 100644 --- a/components-core/src/main/java/org/dllearner/utilities/experiments/ExperimentConfiguration.java +++ b/scripts/src/main/java/org/dllearner/experiments/ExperimentConfiguration.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package org.dllearner.utilities.experiments; +package org.dllearner.experiments; import java.util.ArrayList; import java.util.HashMap; diff --git a/components-core/src/main/java/org/dllearner/utilities/experiments/FinalizedMonitor.java b/scripts/src/main/java/org/dllearner/experiments/FinalizedMonitor.java similarity index 97% rename from components-core/src/main/java/org/dllearner/utilities/experiments/FinalizedMonitor.java rename to scripts/src/main/java/org/dllearner/experiments/FinalizedMonitor.java index 63fb94422d..f051033824 100644 --- a/components-core/src/main/java/org/dllearner/utilities/experiments/FinalizedMonitor.java +++ b/scripts/src/main/java/org/dllearner/experiments/FinalizedMonitor.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package org.dllearner.utilities.experiments; +package org.dllearner.experiments; import java.io.Serializable; diff --git a/components-core/src/main/java/org/dllearner/utilities/experiments/Jamon.java b/scripts/src/main/java/org/dllearner/experiments/Jamon.java similarity index 96% rename from components-core/src/main/java/org/dllearner/utilities/experiments/Jamon.java rename to scripts/src/main/java/org/dllearner/experiments/Jamon.java index 3d4e9f95dc..3b62b10d9a 100644 --- a/components-core/src/main/java/org/dllearner/utilities/experiments/Jamon.java +++ b/scripts/src/main/java/org/dllearner/experiments/Jamon.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package org.dllearner.utilities.experiments; +package org.dllearner.experiments; public class Jamon { diff --git a/components-core/src/main/java/org/dllearner/utilities/experiments/MyMonKey.java b/scripts/src/main/java/org/dllearner/experiments/MyMonKey.java similarity index 99% rename from components-core/src/main/java/org/dllearner/utilities/experiments/MyMonKey.java rename to scripts/src/main/java/org/dllearner/experiments/MyMonKey.java index c9b982a7e5..c0c4802021 100644 --- a/components-core/src/main/java/org/dllearner/utilities/experiments/MyMonKey.java +++ b/scripts/src/main/java/org/dllearner/experiments/MyMonKey.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package org.dllearner.utilities.experiments; +package org.dllearner.experiments; import java.util.List; diff --git a/components-core/src/main/java/org/dllearner/utilities/experiments/Table.java b/scripts/src/main/java/org/dllearner/experiments/Table.java similarity index 99% rename from components-core/src/main/java/org/dllearner/utilities/experiments/Table.java rename to scripts/src/main/java/org/dllearner/experiments/Table.java index dccfe29089..a5fe3a8b2f 100644 --- a/components-core/src/main/java/org/dllearner/utilities/experiments/Table.java +++ b/scripts/src/main/java/org/dllearner/experiments/Table.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package org.dllearner.utilities.experiments; +package org.dllearner.experiments; import java.io.File; import java.io.Serializable; diff --git a/components-core/src/main/java/org/dllearner/utilities/experiments/TableRowColumn.java b/scripts/src/main/java/org/dllearner/experiments/TableRowColumn.java similarity index 98% rename from components-core/src/main/java/org/dllearner/utilities/experiments/TableRowColumn.java rename to scripts/src/main/java/org/dllearner/experiments/TableRowColumn.java index a703e33af8..055f493d72 100644 --- a/components-core/src/main/java/org/dllearner/utilities/experiments/TableRowColumn.java +++ b/scripts/src/main/java/org/dllearner/experiments/TableRowColumn.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package org.dllearner.utilities.experiments; +package org.dllearner.experiments; import java.io.Serializable; import java.text.DecimalFormat; diff --git a/components-core/src/main/java/org/dllearner/utilities/experiments/Units.java b/scripts/src/main/java/org/dllearner/experiments/Units.java similarity index 94% rename from components-core/src/main/java/org/dllearner/utilities/experiments/Units.java rename to scripts/src/main/java/org/dllearner/experiments/Units.java index 21628dcdfd..17edc2e0d8 100644 --- a/components-core/src/main/java/org/dllearner/utilities/experiments/Units.java +++ b/scripts/src/main/java/org/dllearner/experiments/Units.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package org.dllearner.utilities.experiments; +package org.dllearner.experiments; public class Units { diff --git a/scripts/src/main/java/org/dllearner/scripts/Infgen.java b/scripts/src/main/java/org/dllearner/scripts/Infgen.java index 1568807c17..eea2939d79 100644 --- a/scripts/src/main/java/org/dllearner/scripts/Infgen.java +++ b/scripts/src/main/java/org/dllearner/scripts/Infgen.java @@ -1,62 +1,21 @@ package org.dllearner.scripts; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.io.PipedInputStream; -import java.io.PipedOutputStream; -import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Set; - +import com.hp.hpl.jena.rdf.model.InfModel; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; import org.apache.jena.riot.RDFDataMgr; -import org.coode.owlapi.turtle.TurtleOntologyFormat; import org.dllearner.utilities.OwlApiJenaUtils; import org.mindswap.pellet.jena.PelletReasonerFactory; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLDisjointClassesAxiom; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyCreationException; -import org.semanticweb.owlapi.model.OWLOntologyManager; -import org.semanticweb.owlapi.model.OWLOntologyStorageException; -import org.semanticweb.owlapi.reasoner.OWLReasoner; -import org.semanticweb.owlapi.util.InferredAxiomGenerator; -import org.semanticweb.owlapi.util.InferredClassAssertionAxiomGenerator; -import org.semanticweb.owlapi.util.InferredClassAxiomGenerator; -import org.semanticweb.owlapi.util.InferredDataPropertyAxiomGenerator; -import org.semanticweb.owlapi.util.InferredDataPropertyCharacteristicAxiomGenerator; -import org.semanticweb.owlapi.util.InferredDisjointClassesAxiomGenerator; -import org.semanticweb.owlapi.util.InferredEntityAxiomGenerator; -import org.semanticweb.owlapi.util.InferredEquivalentClassAxiomGenerator; -import org.semanticweb.owlapi.util.InferredEquivalentDataPropertiesAxiomGenerator; -import org.semanticweb.owlapi.util.InferredEquivalentObjectPropertyAxiomGenerator; -import org.semanticweb.owlapi.util.InferredIndividualAxiomGenerator; -import org.semanticweb.owlapi.util.InferredInverseObjectPropertiesAxiomGenerator; -import org.semanticweb.owlapi.util.InferredObjectPropertyAxiomGenerator; -import org.semanticweb.owlapi.util.InferredObjectPropertyCharacteristicAxiomGenerator; -import org.semanticweb.owlapi.util.InferredOntologyGenerator; -import org.semanticweb.owlapi.util.InferredPropertyAssertionGenerator; -import org.semanticweb.owlapi.util.InferredSubClassAxiomGenerator; -import org.semanticweb.owlapi.util.InferredSubDataPropertyAxiomGenerator; -import org.semanticweb.owlapi.util.InferredSubObjectPropertyAxiomGenerator; import org.semanticweb.owlapi.apibinding.OWLManager; +import org.semanticweb.owlapi.formats.TurtleDocumentFormat; +import org.semanticweb.owlapi.model.*; +import org.semanticweb.owlapi.reasoner.OWLReasoner; +import org.semanticweb.owlapi.util.*; -import com.hp.hpl.jena.ontology.OntModel; -import com.hp.hpl.jena.ontology.OntModelSpec; -import com.hp.hpl.jena.rdf.model.InfModel; -import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.ModelFactory; -import com.hp.hpl.jena.rdf.model.StmtIterator; -//import com.hp.hpl.jena.reasoner.Reasoner; -import com.hp.hpl.jena.reasoner.ReasonerRegistry; -import com.hp.hpl.jena.util.FileManager; +import java.io.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; public class Infgen { @@ -65,9 +24,10 @@ public static Model getModel(final OWLOntology ontology) { try (PipedInputStream is = new PipedInputStream(); PipedOutputStream os = new PipedOutputStream(is);) { new Thread(new Runnable() { + @Override public void run() { try { - ontology.getOWLOntologyManager().saveOntology(ontology, new TurtleOntologyFormat(), os); + ontology.getOWLOntologyManager().saveOntology(ontology, new TurtleDocumentFormat(), os); os.close(); } catch (OWLOntologyStorageException e) { e.printStackTrace(); @@ -100,13 +60,13 @@ private static void reasonWithPellet(String in, String out) throws FileNotFoundE inf.write(new FileOutputStream(out)); } - private static void reasonWithHermit(String in, String out) throws FileNotFoundException, IOException, OWLOntologyStorageException, OWLOntologyCreationException { + private static void reasonWithHermit(String in, String out, boolean copy) throws FileNotFoundException, IOException, OWLOntologyStorageException, OWLOntologyCreationException { OWLOntologyManager manager=OWLManager.createOWLOntologyManager(); File inputOntologyFile = new File(in); OWLOntology ontology=manager.loadOntologyFromOntologyDocument(inputOntologyFile); org.semanticweb.HermiT.Reasoner.ReasonerFactory factory = new org.semanticweb.HermiT.Reasoner.ReasonerFactory(); - // The factory can now be used to obtain an instance of HermiT as an OWLReasoner. + // The factory can now be used to obtain an instance of HermiT as an OWLReasoner. org.semanticweb.HermiT.Configuration c = new org.semanticweb.HermiT.Configuration(); OWLReasoner reasoner=factory.createReasoner(ontology, c); @@ -115,7 +75,8 @@ private static void reasonWithHermit(String in, String out) throws FileNotFoundE generators.add(new InferredClassAssertionAxiomGenerator()); generators.add(new InferredDisjointClassesAxiomGenerator() { boolean precomputed=false; - protected void addAxioms(OWLClass entity, OWLReasoner reasoner, OWLDataFactory dataFactory, Set result) { + @Override + protected void addAxioms(OWLClass entity, OWLReasoner reasoner, OWLDataFactory dataFactory, Set result) { if (!precomputed) { reasoner.precomputeInferences(org.semanticweb.owlapi.reasoner.InferenceType.DISJOINT_CLASSES); precomputed=true; @@ -137,19 +98,27 @@ protected void addAxioms(OWLClass entity, OWLReasoner reasoner, OWLDataFactory d InferredOntologyGenerator iog=new InferredOntologyGenerator(reasoner,generators); OWLOntology inferredAxiomsOntology=manager.createOntology(); - iog.fillOntology(manager, inferredAxiomsOntology); + iog.fillOntology(manager.getOWLDataFactory(), inferredAxiomsOntology); + if (copy) { + manager.addAxioms(inferredAxiomsOntology, ontology.getAxioms()); + Model m1 = OwlApiJenaUtils.getModel(inferredAxiomsOntology); + Model m0 = RDFDataMgr.loadModel("file://"+in); + m0.add(m1.listStatements()); + m0.write(new FileOutputStream(out)); + } else { File inferredOntologyFile=new File(out); if (!inferredOntologyFile.exists()) inferredOntologyFile.createNewFile(); inferredOntologyFile=inferredOntologyFile.getAbsoluteFile(); OutputStream outputStream=new FileOutputStream(inferredOntologyFile); manager.saveOntology(inferredAxiomsOntology, manager.getOntologyFormat(ontology), outputStream); + } System.out.println("The ontology in "+out+" should now contain all inferred axioms "); } private static void loadThroughJena(String in, String out) throws OWLOntologyCreationException, FileNotFoundException { OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); - OWLOntology ontology = manager.loadOntologyFromOntologyDocument(IRI.create("file:///"+in)); + OWLOntology ontology = manager.loadOntologyFromOntologyDocument(new File(in)); Model model = OwlApiJenaUtils.getModel(ontology); System.out.println("ltj; size:"+model.size()); model.write(new FileOutputStream(out)); @@ -163,14 +132,10 @@ private static void loadThroughJena2(String in, String out) throws OWLOntologyCr } public static void main(String[] args) throws Exception { - // TODO Auto-generated method stub - System.out.println("Hallo welt!"); - - //String in = "DL-Learner/examples/family-benchmark/family-benchmark.owl"; - String in = args.length > 0 ? args[0] : "DL-Learner/examples/carcinogenesis/carcinogenesis.owl"; + String in = args.length > 0 ? args[0] : "../examples/carcinogenesis/carcinogenesis.owl"; if (args.length <= 1) { loadThroughJena(in, in+".jena1"); - reasonWithHermit(in, in+".her0"); + reasonWithHermit(in, in+".her0", false); } else { for (int i = 0; i < args[1].length(); ++i) { @@ -178,15 +143,19 @@ public static void main(String[] args) throws Exception { String next = null; switch (args[1].charAt(i)) { case 'h': - next = in + "." + i + "." + "her"; - reasonWithHermit(step, next); + next = in + "." + i + "." + "her"; + reasonWithHermit(step, next, false); + break; + case 'H': + next = in + "." + i + "." + "her2"; + reasonWithHermit(step, next, true); break; case 'j': next = in + "." + i + "." + "jena"; loadThroughJena(step, next); break; case 'J': - next = in + "." + i + "." + "jena"; + next = in + "." + i + "." + "jena2"; loadThroughJena2(step, next); break; case 'p': @@ -202,14 +171,7 @@ public static void main(String[] args) throws Exception { } } } - //reasonWithPellet(in+".jena1", in+".her"); - //reasonWithPellet2(in, in+".wp2"); - - //loadThroughJena2(in, in+".jena2"); - //loadThroughJena(in+".her0", in+".jena"); - //reasonWithHermit(in+".jena", in+".her"); - //reasonWithPellet(in, in+".wp"); - //loadThroughJena(in+".wp", in+".wp.jena"); + } } diff --git a/scripts/src/main/java/org/dllearner/scripts/MouseDiabetesCBD.java b/scripts/src/main/java/org/dllearner/scripts/MouseDiabetesCBD.java index c4c4a13483..99802a2464 100644 --- a/scripts/src/main/java/org/dllearner/scripts/MouseDiabetesCBD.java +++ b/scripts/src/main/java/org/dllearner/scripts/MouseDiabetesCBD.java @@ -1,24 +1,19 @@ package org.dllearner.scripts; -import java.io.File; -import java.io.FileOutputStream; -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - import org.dllearner.examples.MouseDiabetes; import org.dllearner.utilities.owl.OWLAxiomCBDGenerator; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.io.RDFXMLOntologyFormat; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyManager; +import org.semanticweb.owlapi.formats.RDFXMLDocumentFormat; +import org.semanticweb.owlapi.model.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.File; +import java.io.FileOutputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + public class MouseDiabetesCBD { private static final Logger logger = LoggerFactory.getLogger(MouseDiabetesCBD.class); public static String cbdFilePath = "/tmp/smallis/kb_cbd_05.owl"; @@ -54,6 +49,6 @@ public static void main (String[] args) throws Exception { logger.info(" Also done"); } - man.saveOntology(cbdOnt, new RDFXMLOntologyFormat(), new FileOutputStream(new File(cbdFilePath))); + man.saveOntology(cbdOnt, new RDFXMLDocumentFormat(), new FileOutputStream(new File(cbdFilePath))); } } diff --git a/scripts/src/main/java/org/dllearner/scripts/ReactomeCBD.java b/scripts/src/main/java/org/dllearner/scripts/ReactomeCBD.java index 3e021dee11..1a2108ecca 100644 --- a/scripts/src/main/java/org/dllearner/scripts/ReactomeCBD.java +++ b/scripts/src/main/java/org/dllearner/scripts/ReactomeCBD.java @@ -1,5 +1,12 @@ package org.dllearner.scripts; +import org.dllearner.utilities.owl.OWLAxiomCBDGenerator; +import org.semanticweb.owlapi.apibinding.OWLManager; +import org.semanticweb.owlapi.formats.RDFXMLDocumentFormat; +import org.semanticweb.owlapi.model.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; @@ -7,17 +14,6 @@ import java.util.List; import java.util.Set; -import org.dllearner.utilities.owl.OWLAxiomCBDGenerator; -import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.io.RDFXMLOntologyFormat; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyManager; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - public class ReactomeCBD { private static final Logger logger = LoggerFactory.getLogger(ReactomeCBD.class); private static String dumpFilePath = "/tmp/tr.owl"; @@ -57,6 +53,6 @@ public static void main (String[] args) throws Exception { logger.info(" Also done"); } - man.saveOntology(cbdOnt, new RDFXMLOntologyFormat(), new FileOutputStream(new File(cbdFilePath))); + man.saveOntology(cbdOnt, new RDFXMLDocumentFormat(), new FileOutputStream(new File(cbdFilePath))); } } diff --git a/scripts/src/main/java/org/dllearner/scripts/SPARQLSampleDebugging.java b/scripts/src/main/java/org/dllearner/scripts/SPARQLSampleDebugging.java index d7425cca2e..e47a46a7c5 100644 --- a/scripts/src/main/java/org/dllearner/scripts/SPARQLSampleDebugging.java +++ b/scripts/src/main/java/org/dllearner/scripts/SPARQLSampleDebugging.java @@ -1,97 +1,38 @@ package org.dllearner.scripts; -import java.io.BufferedInputStream; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.PrintWriter; -import java.io.StringReader; -import java.io.UnsupportedEncodingException; -import java.net.MalformedURLException; -import java.net.URL; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.sql.Types; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Random; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; -import java.util.prefs.Preferences; - -import org.apache.commons.compress.compressors.CompressorException; -import org.apache.commons.compress.compressors.CompressorStreamFactory; -import org.apache.log4j.ConsoleAppender; -import org.apache.log4j.FileAppender; -import org.apache.log4j.Level; -import org.apache.log4j.Logger; -import org.apache.log4j.SimpleLayout; -//import org.dllearner.common.index.ModelGenerator; -//import org.dllearner.common.index.ModelGenerator.Strategy; -import org.dllearner.kb.sparql.ConciseBoundedDescriptionGenerator; -import org.dllearner.kb.sparql.ConciseBoundedDescriptionGeneratorImpl; -import org.dllearner.kb.sparql.ExtractionDBCache; -import org.dllearner.kb.sparql.SparqlEndpoint; -import org.dllearner.kb.sparql.SparqlQuery; -import org.ini4j.IniPreferences; -import org.ini4j.InvalidFileFormatException; -import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.io.RDFXMLOntologyFormat; -import org.semanticweb.owlapi.model.AxiomType; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAsymmetricObjectPropertyAxiom; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLDataProperty; -import org.semanticweb.owlapi.model.OWLException; -import org.semanticweb.owlapi.model.OWLFunctionalDataPropertyAxiom; -import org.semanticweb.owlapi.model.OWLFunctionalObjectPropertyAxiom; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLIrreflexiveObjectPropertyAxiom; -import org.semanticweb.owlapi.model.OWLObjectProperty; -import org.semanticweb.owlapi.model.OWLObjectPropertyCharacteristicAxiom; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyCreationException; -import org.semanticweb.owlapi.model.OWLOntologyManager; -import org.semanticweb.owlapi.model.OWLOntologyStorageException; -import org.semanticweb.owlapi.reasoner.ClassExpressionNotInProfileException; -import org.semanticweb.owlapi.reasoner.FreshEntitiesException; -import org.semanticweb.owlapi.reasoner.InconsistentOntologyException; -import org.semanticweb.owlapi.reasoner.InferenceType; -import org.semanticweb.owlapi.reasoner.OWLReasoner; -import org.semanticweb.owlapi.reasoner.ReasonerInterruptedException; -import org.semanticweb.owlapi.reasoner.TimeOutException; - -import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; - import com.clarkparsia.owlapi.explanation.PelletExplanation; import com.clarkparsia.owlapi.explanation.io.manchester.ManchesterSyntaxExplanationRenderer; import com.clarkparsia.pellet.owlapiv3.PelletReasoner; import com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory; -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.QueryFactory; -import com.hp.hpl.jena.query.QuerySolution; +import com.hp.hpl.jena.query.*; import com.hp.hpl.jena.query.ResultSet; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.vocabulary.OWL; import com.hp.hpl.jena.vocabulary.RDFS; +import org.apache.commons.compress.compressors.CompressorException; +import org.apache.commons.compress.compressors.CompressorStreamFactory; +import org.apache.log4j.*; +import org.dllearner.kb.sparql.*; +import org.ini4j.IniPreferences; +import org.ini4j.InvalidFileFormatException; +import org.semanticweb.owlapi.apibinding.OWLManager; +import org.semanticweb.owlapi.formats.RDFXMLDocumentFormat; +import org.semanticweb.owlapi.model.*; +import org.semanticweb.owlapi.model.parameters.Imports; +import org.semanticweb.owlapi.reasoner.*; +import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; + +import java.io.*; +import java.net.MalformedURLException; +import java.net.URL; +import java.sql.*; +import java.util.*; +import java.util.Map.Entry; +import java.util.prefs.Preferences; + +//import org.dllearner.common.index.ModelGenerator; +//import org.dllearner.common.index.ModelGenerator.Strategy; /** * FIXME: I just commented out all lines that caused errors. So, this class @@ -359,7 +300,7 @@ private Model convert(OWLOntology ontology) { try { OWLOntologyManager man = OWLManager.createOWLOntologyManager(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); - man.saveOntology(ontology, new RDFXMLOntologyFormat(), baos); + man.saveOntology(ontology, new RDFXMLDocumentFormat(), baos); bais = new ByteArrayInputStream(baos.toByteArray()); model.read(bais, null); } catch (OWLOntologyStorageException e) { @@ -458,7 +399,7 @@ public void computeSampleExplanations(OWLOntology reference, int nrOfExplanation logger.info("###################################################################"); logger.info("Resource " + resource);//resource = "http://dbpedia.org/resource/The_Man_Who_Wouldn%27t_Die"; module = extractSampleModule(resource);module.getOWLOntologyManager().removeAxioms(module, module.getAxioms(AxiomType.DATA_PROPERTY_ASSERTION)); - manager.addAxioms(reference, module.getABoxAxioms(true)); + manager.addAxioms(reference, module.getABoxAxioms(Imports.INCLUDED)); manager.removeAxioms(reference, reference.getAxioms(AxiomType.DATA_PROPERTY_ASSERTION)); boolean isConsistent = reasoner.isConsistent(); logger.info("Consistent: " + isConsistent); @@ -517,7 +458,7 @@ public void computeSampleExplanations(OWLOntology reference, int nrOfExplanation logger.info(entry.getKey() + "\t: " + entry.getValue()); } } - man.removeAxioms(reference, module.getABoxAxioms(true)); + man.removeAxioms(reference, module.getABoxAxioms(Imports.INCLUDED)); // writeToDB(resource, module.getLogicalAxiomCount(), isConsistent, explanations); } renderer.endRendering(); @@ -565,7 +506,7 @@ public void runOptimized(OWLOntology reference) throws OWLOntologyCreationExcept logger.info("###################################################################"); logger.info("Resource " + resource);//resource = "http://dbpedia.org/resource/The_Man_Who_Wouldn%27t_Die"; module = extractSampleModule(resource); - man.addAxioms(reference, module.getABoxAxioms(true)); + man.addAxioms(reference, module.getABoxAxioms(Imports.INCLUDED)); man.removeAxioms(reference, reference.getAxioms(AxiomType.DATA_PROPERTY_ASSERTION)); boolean isConsistent = reasoner.isConsistent(); logger.info("Consistent: " + isConsistent); @@ -626,7 +567,7 @@ public void runOptimized(OWLOntology reference) throws OWLOntologyCreationExcept logger.info(entry.getKey() + "\t: " + entry.getValue()); } } - man.removeAxioms(reference, module.getABoxAxioms(true)); + man.removeAxioms(reference, module.getABoxAxioms(Imports.INCLUDED)); // writeToDB(resource, module.getLogicalAxiomCount(), isConsistent, explanations); break; } @@ -864,7 +805,7 @@ private boolean containsUnsatisfiableObjectProperty(Set justification) OWLReasoner reasoner = null; try { OWLOntology ontology = manager.createOntology(justification); - manager.removeAxioms(ontology, ontology.getABoxAxioms(true)); + manager.removeAxioms(ontology, ontology.getABoxAxioms(Imports.INCLUDED)); reasoner = PelletReasonerFactory.getInstance().createNonBufferingReasoner(ontology); for(OWLObjectProperty p : ontology.getObjectPropertiesInSignature()){ boolean satisfiable = reasoner.isSatisfiable(factory.getOWLObjectExactCardinality(1, p)); diff --git a/components-core/src/main/java/org/dllearner/utilities/analyse/CountInstances.java b/scripts/src/main/java/org/dllearner/scripts/analyse/CountInstances.java similarity index 98% rename from components-core/src/main/java/org/dllearner/utilities/analyse/CountInstances.java rename to scripts/src/main/java/org/dllearner/scripts/analyse/CountInstances.java index 920b462868..b1439ec4cb 100644 --- a/components-core/src/main/java/org/dllearner/utilities/analyse/CountInstances.java +++ b/scripts/src/main/java/org/dllearner/scripts/analyse/CountInstances.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package org.dllearner.utilities.analyse; +package org.dllearner.scripts.analyse; import java.net.URL; import java.util.ArrayList; diff --git a/components-core/src/main/java/org/dllearner/utilities/analyse/Hierarchy.java b/scripts/src/main/java/org/dllearner/scripts/analyse/Hierarchy.java similarity index 99% rename from components-core/src/main/java/org/dllearner/utilities/analyse/Hierarchy.java rename to scripts/src/main/java/org/dllearner/scripts/analyse/Hierarchy.java index 3b61115e76..aecbdeddd9 100644 --- a/components-core/src/main/java/org/dllearner/utilities/analyse/Hierarchy.java +++ b/scripts/src/main/java/org/dllearner/scripts/analyse/Hierarchy.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package org.dllearner.utilities.analyse; +package org.dllearner.scripts.analyse; import java.io.File; import java.util.HashMap; diff --git a/components-core/src/main/java/org/dllearner/utilities/analyse/ScriptDoAll.java b/scripts/src/main/java/org/dllearner/scripts/analyse/ScriptDoAll.java similarity index 97% rename from components-core/src/main/java/org/dllearner/utilities/analyse/ScriptDoAll.java rename to scripts/src/main/java/org/dllearner/scripts/analyse/ScriptDoAll.java index 8d43c58b73..e23b5a7405 100644 --- a/components-core/src/main/java/org/dllearner/utilities/analyse/ScriptDoAll.java +++ b/scripts/src/main/java/org/dllearner/scripts/analyse/ScriptDoAll.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package org.dllearner.utilities.analyse; +package org.dllearner.scripts.analyse; import java.io.File; import java.util.ArrayList; @@ -28,8 +28,8 @@ import java.util.SortedSet; import java.util.TreeSet; +import org.dllearner.scripts.analyse.CountInstances.Count; import org.dllearner.utilities.Files; -import org.dllearner.utilities.analyse.CountInstances.Count; public class ScriptDoAll { diff --git a/examples/carcinogenesis/param-sparql-celoe-remote.conf b/test/carcinogenesis/param-sparql-celoe-remote.conf similarity index 100% rename from examples/carcinogenesis/param-sparql-celoe-remote.conf rename to test/carcinogenesis/param-sparql-celoe-remote.conf diff --git a/examples/family-benchmark/Female-sparql-remote.conf b/test/family-benchmark/Female-sparql-remote.conf similarity index 100% rename from examples/family-benchmark/Female-sparql-remote.conf rename to test/family-benchmark/Female-sparql-remote.conf diff --git a/examples/sparql/property_axioms.conf b/test/sparql/property_axioms.conf similarity index 100% rename from examples/sparql/property_axioms.conf rename to test/sparql/property_axioms.conf diff --git a/examples/sparql/scrobble.conf b/test/sparql/scrobble.conf similarity index 100% rename from examples/sparql/scrobble.conf rename to test/sparql/scrobble.conf diff --git a/examples/sparql/scrobble.kb b/test/sparql/scrobble.kb similarity index 100% rename from examples/sparql/scrobble.kb rename to test/sparql/scrobble.kb