Skip to content

Commit

Permalink
Minor.
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzBuehmann committed Sep 27, 2016
1 parent 59692db commit 4368c46
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 90 deletions.
Expand Up @@ -29,6 +29,7 @@
import java.util.concurrent.TimeUnit;

import org.dllearner.core.AbstractAxiomLearningAlgorithm;
import org.dllearner.core.ComponentAnn;
import org.dllearner.core.Score;
import org.dllearner.kb.SparqlEndpointKS;
import org.dllearner.kb.sparql.SparqlEndpoint;
Expand Down Expand Up @@ -69,6 +70,7 @@
* @author Lorenz Buehmann
*
*/
@ComponentAnn(name = "patla", shortName = "patla", version = 0.1, description = "Pattern-based algorithm uses OWL axioms as pattern.")
public class PatternBasedAxiomLearningAlgorithm extends AbstractAxiomLearningAlgorithm<OWLAxiom, OWLObject, OWLEntity>{

private static final Logger logger = LoggerFactory.getLogger(PatternBasedAxiomLearningAlgorithm.class);
Expand Down
Expand Up @@ -32,6 +32,9 @@
import java.util.*;

/**
* Materialize the existential restrictions, i.e. for all instances x_i that belong to a concept \exists.r.C,
* we add facts r(x_i, _:newID), C(_:newID), and this recursively.
*
* @author Lorenz Buehmann
*
*/
Expand Down
Expand Up @@ -19,12 +19,11 @@
package org.dllearner.utilities.examples;

import com.google.common.collect.*;
import org.aksw.jena_sparql_api.core.QueryExecutionFactory;
import org.aksw.jena_sparql_api.http.QueryExecutionFactoryHttp;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import org.aksw.jena_sparql_api.core.QueryExecutionFactory;
import org.aksw.jena_sparql_api.http.QueryExecutionFactoryHttp;
import org.dllearner.kb.SparqlEndpointKS;
import org.dllearner.kb.sparql.SparqlEndpoint;
import org.dllearner.reasoning.SPARQLReasoner;
import org.dllearner.utilities.datastructures.SetManipulation;
Expand Down Expand Up @@ -63,40 +62,19 @@ public enum Strategy{
private String namespace;
private QueryExecutionFactory qef;

public AutomaticNegativeExampleFinderSPARQL2(SparqlEndpoint se, SPARQLReasoner reasoner) {
this(se, reasoner, null);
}

public AutomaticNegativeExampleFinderSPARQL2(SparqlEndpoint se, SPARQLReasoner reasoner, String namespace) {
this.sr = reasoner;
this.namespace = namespace;

qef = new QueryExecutionFactoryHttp(se.getURL().toString(), se.getDefaultGraphURIs());
}

public AutomaticNegativeExampleFinderSPARQL2(SparqlEndpoint se) {
this(se, (String) null);
}

public AutomaticNegativeExampleFinderSPARQL2(SparqlEndpoint se, String namespace) {
this(se, new SPARQLReasoner(new SparqlEndpointKS(se)), namespace);
this(new QueryExecutionFactoryHttp(se.getURL().toString(), se.getDefaultGraphURIs()));
}

public AutomaticNegativeExampleFinderSPARQL2(SPARQLReasoner reasoner, String namespace) {
this.sr = reasoner;
this.namespace = namespace;

public AutomaticNegativeExampleFinderSPARQL2(QueryExecutionFactory qef) {
this(new SPARQLReasoner(qef));
}

public AutomaticNegativeExampleFinderSPARQL2(SPARQLReasoner reasoner) {
this.sr = reasoner;
this.qef = sr.getQueryExecutionFactory();
this.qef = reasoner.getQueryExecutionFactory();
}

public AutomaticNegativeExampleFinderSPARQL2(QueryExecutionFactory qef) {
this.qef = qef;
this.sr = new SPARQLReasoner(qef);
}


public SortedSet<OWLIndividual> getNegativeExamples(OWLClass classToDescribe, Set<OWLIndividual> positiveExamples, int limit) {
return getNegativeExamples(classToDescribe, positiveExamples, Arrays.asList(SUPERCLASS, SIBLING, RANDOM), limit);
}
Expand Down Expand Up @@ -145,68 +123,27 @@ public SortedSet<OWLIndividual> getNegativeExamples(Set<OWLIndividual> positiveE
keepMostSpecificClasses(types);
return computeNegativeExamples(null, types, strategiesWithWeight, maxNrOfReturnedInstances);
}

private SortedSet<OWLIndividual> computeNegativeExamples(OWLClass classToDescribe, Multiset<OWLClass> positiveExamplesTypes, Map<Strategy, Double> strategiesWithWeight, int maxNrOfReturnedInstances) {

private SortedSet<OWLIndividual> computeNegativeExamples(OWLClass classToDescribe,
Multiset<OWLClass> positiveExamplesTypes,
Map<Strategy, Double> strategiesWithWeight,
int maxNrOfReturnedInstances) {
SortedSet<OWLIndividual> negativeExamples = new TreeSet<>();

for (Entry<Strategy, Double> entry : strategiesWithWeight.entrySet()) {
Strategy strategy = entry.getKey();
Double weight = entry.getValue();
//the max number of instances returned by the current strategy

// the max number of instances returned by the current strategy
int strategyLimit = (int)(weight * maxNrOfReturnedInstances);
//the highest frequency value

// the highest frequency value
int maxFrequency = positiveExamplesTypes.entrySet().iterator().next().getCount();

if(strategy == SIBLING){//get sibling class based examples
logger.info("Applying sibling classes strategy...");
SortedSet<OWLIndividual> siblingNegativeExamples = new TreeSet<>();
//for each type of the positive examples
for (OWLClass nc : positiveExamplesTypes.elementSet()) {
int frequency = positiveExamplesTypes.count(nc);
//get sibling classes
Set<OWLClass> siblingClasses = sr.getSiblingClasses(nc);
siblingClasses = filterByNamespace(siblingClasses);
logger.info("Sibling classes: " + siblingClasses);

int limit = (int)Math.ceil(((double)frequency / positiveExamplesTypes.size()) / siblingClasses.size() * strategyLimit);
//get instances for each sibling class
for (OWLClass siblingClass : siblingClasses) {
SortedSet<OWLIndividual> individuals = sr.getIndividualsExcluding(siblingClass, nc, maxNrOfReturnedInstances);
individuals.removeAll(siblingNegativeExamples);
SetManipulation.stableShrink(individuals, limit);
siblingNegativeExamples.addAll(individuals);
}
}
siblingNegativeExamples = SetManipulation.stableShrink(siblingNegativeExamples, strategyLimit);
logger.info("Negative examples(" + siblingNegativeExamples.size() + "): " + siblingNegativeExamples);
negativeExamples.addAll(siblingNegativeExamples);
negativeExamples.addAll(negativeExamplesBySiblingClasses(positiveExamplesTypes, strategyLimit, maxNrOfReturnedInstances));
} else if(strategy == SUPERCLASS){//get super class based examples
logger.info("Applying super class strategy...");
SortedSet<OWLIndividual> superClassNegativeExamples = new TreeSet<>();
//for each type of the positive examples
for (OWLClass nc : positiveExamplesTypes.elementSet()) {
int frequency = positiveExamplesTypes.count(nc);
//get super classes
Set<OWLClassExpression> superClasses = sr.getSuperClasses(nc);
superClasses.remove(df.getOWLThing());
// superClasses.remove(Thing.instance);
superClasses.remove(df.getOWLClass(OWLRDFVocabulary.RDFS_RESOURCE.getIRI()));
superClasses = filterByNamespace(superClasses);
logger.info("Super classes: " + superClasses);

int limit = (int)Math.ceil(((double)frequency / positiveExamplesTypes.size()) / superClasses.size() * strategyLimit);
//get instances for each super class
for (OWLClassExpression superClass : superClasses) {
SortedSet<OWLIndividual> individuals = sr.getIndividualsExcluding(superClass, nc, maxNrOfReturnedInstances);
individuals.removeAll(negativeExamples);
individuals.removeAll(superClassNegativeExamples);
SetManipulation.stableShrink(individuals, limit);
superClassNegativeExamples.addAll(individuals);
}
}
superClassNegativeExamples = SetManipulation.stableShrink(superClassNegativeExamples, strategyLimit);
logger.info("Negative examples(" + superClassNegativeExamples.size() + "): " + superClassNegativeExamples);
negativeExamples.addAll(superClassNegativeExamples);
negativeExamples.addAll(negativeExamplesBySuperClasses(positiveExamplesTypes, negativeExamples, strategyLimit, maxNrOfReturnedInstances));
} else if(strategy == RANDOM){//get some random examples
logger.info("Applying random strategy...");
SortedSet<OWLIndividual> randomNegativeExamples = new TreeSet<>();
Expand Down Expand Up @@ -236,6 +173,65 @@ private SortedSet<OWLIndividual> computeNegativeExamples(OWLClass classToDescrib
}
return negativeExamples;
}

private SortedSet<OWLIndividual> negativeExamplesBySuperClasses(Multiset<OWLClass> positiveExamplesTypes,
Set<OWLIndividual> negativeExamples,
int cnt, int totalCnt) {
logger.info("Applying super class strategy...");
SortedSet<OWLIndividual> negExamples = new TreeSet<>();
//for each type of the positive examples
for (OWLClass nc : positiveExamplesTypes.elementSet()) {
int frequency = positiveExamplesTypes.count(nc);
//get super classes
Set<OWLClassExpression> superClasses = sr.getSuperClasses(nc);
superClasses.remove(df.getOWLThing());
// superClasses.remove(Thing.instance);
superClasses.remove(df.getOWLClass(OWLRDFVocabulary.RDFS_RESOURCE.getIRI()));
superClasses = filterByNamespace(superClasses);
logger.info("Super classes: " + superClasses);

int limit = (int)Math.ceil(((double)frequency / positiveExamplesTypes.size()) / superClasses.size() * cnt);
//get instances for each super class
for (OWLClassExpression superClass : superClasses) {
SortedSet<OWLIndividual> individuals = sr.getIndividualsExcluding(superClass, nc, totalCnt);
individuals.removeAll(negativeExamples);
individuals.removeAll(negExamples);
SetManipulation.stableShrink(individuals, limit);
negExamples.addAll(individuals);
}
}
negExamples = SetManipulation.stableShrink(negExamples, cnt);
logger.info("Negative examples(" + negExamples.size() + "): " + negExamples);
return negExamples;
}

private SortedSet<OWLIndividual> negativeExamplesBySiblingClasses(Multiset<OWLClass> positiveExamplesTypes, int cnt, int totalCnt) {
logger.info("Applying sibling classes strategy...");
SortedSet<OWLIndividual> negExamples = new TreeSet<>();

// for each type of the positive examples
for (OWLClass nc : positiveExamplesTypes.elementSet()) {
int frequency = positiveExamplesTypes.count(nc);

// get sibling classes
Set<OWLClass> siblingClasses = sr.getSiblingClasses(nc);
siblingClasses = filterByNamespace(siblingClasses);
logger.info("Sibling classes: " + siblingClasses);

int limit = (int)Math.ceil(((double)frequency / positiveExamplesTypes.size()) / siblingClasses.size() * cnt);

// get instances for each sibling class
for (OWLClass siblingClass : siblingClasses) {
SortedSet<OWLIndividual> individuals = sr.getIndividualsExcluding(siblingClass, nc, totalCnt);
individuals.removeAll(negExamples);
SetManipulation.stableShrink(individuals, limit);
negExamples.addAll(individuals);
}
}
negExamples = SetManipulation.stableShrink(negExamples, cnt);
logger.info("Negative examples(" + negExamples.size() + "): " + negExamples);
return negExamples;
}

private <T extends OWLClassExpression> Set<T> filterByNamespace(Set<T> classes){
if(namespace != null){
Expand Down Expand Up @@ -270,7 +266,5 @@ private void keepMostSpecificClasses(Multiset<OWLClass> classes){
}
}
}


}
}
Expand Up @@ -185,13 +185,13 @@ public OWLClassExpression visit(OWLObjectUnionOf ce) {
@Override
public OWLClassExpression visit(OWLObjectComplementOf ce) {
OWLClassExpression operand = ce.getOperand();
OWLClassExpression shortendedOperand = operand.accept(this);
if(shortendedOperand.isOWLThing()){// \neg \top \equiv \bot
OWLClassExpression shortenedOperand = operand.accept(this);
if(shortenedOperand.isOWLThing()){// \neg \top \equiv \bot
return df.getOWLNothing();
} else if(shortendedOperand.isOWLNothing()){// \neg \bot \equiv \top
} else if(shortenedOperand.isOWLNothing()){// \neg \bot \equiv \top
return df.getOWLThing();
} else if(operand != shortendedOperand){
return df.getOWLObjectComplementOf(shortendedOperand);
} else if(operand != shortenedOperand){
return df.getOWLObjectComplementOf(shortenedOperand);
}
return ce;
}
Expand Down

0 comments on commit 4368c46

Please sign in to comment.