Skip to content

Commit

Permalink
make ocel work again
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonBin committed Feb 15, 2016
1 parent 27977d1 commit 0f12a2f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 76 deletions.
Expand Up @@ -21,7 +21,6 @@
import com.google.common.collect.Sets;
import com.jamonapi.Monitor;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.dllearner.core.*;
import org.dllearner.core.config.ConfigOption;
import org.dllearner.core.options.CommonConfigOptions;
Expand All @@ -40,10 +39,10 @@
import org.dllearner.utilities.owl.EvaluatedDescriptionPosNegComparator;
import org.dllearner.utilities.owl.OWLClassExpressionLengthMetric;
import org.dllearner.utilities.owl.OWLClassExpressionUtils;
import org.semanticweb.owlapi.io.OWLObjectRenderer;
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 java.io.File;
import java.text.DecimalFormat;
Expand Down Expand Up @@ -77,16 +76,11 @@
public class OCEL extends AbstractCELA {

// actual algorithm
private static Logger logger = Logger.getLogger(OCEL.class);
private static Logger logger = LoggerFactory.getLogger(OCEL.class);
private String logLevel = CommonConfigOptions.logLevelDefault;

private OWLObjectRenderer renderer;

// basic setup: learning problem and reasoning service
private AbstractReasonerComponent rs;
// often the learning problems needn't be accessed directly; instead
// use the example sets below and the posonly variable
private PosNegLP learningProblem;
private OWLClassExpression startDescription;
private int nrOfExamples;
private int nrOfPositiveExamples;
Expand All @@ -110,8 +104,6 @@ public class OCEL extends AbstractCELA {
private boolean useShortConceptConstruction = true;

// extended Options
@ConfigOption(defaultValue = "10", description = "maximum execution of the algorithm in seconds")
private int maxExecutionTimeInSeconds = CommonConfigOptions.maxExecutionTimeInSecondsDefault;
private boolean maxExecutionTimeAlreadyReached = false;
private int minExecutionTimeInSeconds = CommonConfigOptions.minExecutionTimeInSecondsDefault;
private boolean minExecutionTimeAlreadyReached = false;
Expand Down Expand Up @@ -147,10 +139,6 @@ public class OCEL extends AbstractCELA {
private boolean useCandidateReduction = true;
private int candidatePostReductionSize = 30;

// setting to true gracefully stops the algorithm
private boolean stop = false;
private boolean isRunning = false;

// solution protocol
private List<ExampleBasedNode> solutions = new LinkedList<>();

Expand Down Expand Up @@ -205,10 +193,6 @@ public class OCEL extends AbstractCELA {
private long evaluateSetCreationTimeNs = 0;
private long improperConceptsRemovalTimeNs = 0;

// prefixes
private String baseURI;
private Map<String, String> prefixes;

private boolean terminateOnNoiseReached = true;

private double negativeWeight = 1.0;
Expand All @@ -220,7 +204,6 @@ public class OCEL extends AbstractCELA {

private int negationPenalty = 0;

private OWLDataFactory dataFactory = new OWLDataFactoryImpl();
@ConfigOption(description = "adjust the weights of class expression length in refinement", defaultValue = "OCEL default metric")
private OWLClassExpressionLengthMetric lengthMetric;

Expand Down Expand Up @@ -268,7 +251,7 @@ public void init() throws ComponentInitException {

// set log level if the option has been set
if(!logLevel.equals(CommonConfigOptions.logLevelDefault))
logger.setLevel(Level.toLevel(logLevel,Level.toLevel(CommonConfigOptions.logLevelDefault)));
org.apache.log4j.Logger.getLogger(OCEL.class).setLevel(Level.toLevel(logLevel,Level.toLevel(CommonConfigOptions.logLevelDefault)));

if(searchTreeFile == null)
searchTreeFile = new File(defaultSearchTreeFile);
Expand Down Expand Up @@ -354,14 +337,14 @@ public void init() throws ComponentInitException {
// create an algorithm object and pass all configuration
// options to it

positiveExamples = learningProblem.getPositiveExamples();
negativeExamples = learningProblem.getNegativeExamples();
positiveExamples = ((PosNegLP)learningProblem).getPositiveExamples();
negativeExamples = ((PosNegLP)learningProblem).getNegativeExamples();
nrOfPositiveExamples = positiveExamples.size();
nrOfNegativeExamples = negativeExamples.size();

nrOfExamples = nrOfPositiveExamples + nrOfNegativeExamples;
baseURI = rs.getBaseURI();
prefixes = rs.getPrefixes();
baseURI = reasoner.getBaseURI();
prefixes = reasoner.getPrefixes();
// note: used concepts and roles do not need to be passed
// as argument, because it is sufficient to prepare the
// concept and role hierarchy accordingly
Expand Down Expand Up @@ -411,13 +394,13 @@ public void start() {
if (startDescription == null) {
startNode = new ExampleBasedNode(dataFactory.getOWLThing(), negativeWeight, startNodeBonus, expansionPenaltyFactor, negationPenalty);
startNode.setCoveredExamples(positiveExamples, negativeExamples);
startNode.setAccuracyMethod(learningProblem.getAccuracyMethod());
startNode.setAccuracyMethod(((PosNegLP)learningProblem).getAccuracyMethod());
} else {
startNode = new ExampleBasedNode(startDescription, negativeWeight, startNodeBonus, expansionPenaltyFactor, negationPenalty);
Set<OWLIndividual> coveredNegatives = rs.hasType(startDescription, negativeExamples);
Set<OWLIndividual> coveredPositives = rs.hasType(startDescription, positiveExamples);
Set<OWLIndividual> coveredNegatives = reasoner.hasType(startDescription, negativeExamples);
Set<OWLIndividual> coveredPositives = reasoner.hasType(startDescription, positiveExamples);
startNode.setCoveredExamples(coveredPositives, coveredNegatives);
startNode.setAccuracyMethod(learningProblem.getAccuracyMethod());
startNode.setAccuracyMethod(((PosNegLP)learningProblem).getAccuracyMethod());
}

searchTree.addNode(null, startNode);
Expand Down Expand Up @@ -469,8 +452,8 @@ public void start() {
// no handling needed, it will just look ugly in the output
logger.info("more accurate ("+acc+") class expression found: " + renderer.render(searchTreeStable.best().getConcept()));
if(logger.isTraceEnabled()){
logger.trace(Sets.difference(positiveExamples,bestNodeStable.getCoveredNegatives()));
logger.trace(Sets.difference(negativeExamples,bestNodeStable.getCoveredNegatives()));
logger.trace(Sets.difference(positiveExamples,bestNodeStable.getCoveredNegatives()).toString());
logger.trace(Sets.difference(negativeExamples,bestNodeStable.getCoveredNegatives()).toString());
}
printBestSolutions(5);
printStatistics(false);
Expand Down Expand Up @@ -539,7 +522,7 @@ public void start() {
logger.info("Algorithm stopped ("+conceptTests+" descriptions tested).\n");
} else {
logger.info("Algorithm terminated successfully ("+conceptTests+" descriptions tested).\n");
logger.info(rs.toString());
logger.info(reasoner.toString());
}

totalLearningTime.stop();
Expand Down Expand Up @@ -660,7 +643,7 @@ private void extendNodeProper(ExampleBasedNode node, OWLClassExpression concept,
// Test aller Konzepte auf properness (mit DIG in nur einer Anfrage)
if (usePropernessChecks) {
long propCalcReasoningStart = System.nanoTime();
improperConcepts = rs.isSuperClassOf(toEvaluateConcepts, concept);
improperConcepts = reasoner.isSuperClassOf(toEvaluateConcepts, concept);

propernessTestsReasoner += toEvaluateConcepts.size();
propernessCalcReasoningTimeNs += System.nanoTime() - propCalcReasoningStart;
Expand Down Expand Up @@ -706,7 +689,7 @@ private void extendNodeProper(ExampleBasedNode node, OWLClassExpression concept,
qualityKnown = true;
newNode.setQualityEvaluationMethod(ExampleBasedNode.QualityEvaluationMethod.OVERLY_GENERAL_LIST);
newNode.setCoveredExamples(positiveExamples, negativeExamples);
newNode.setAccuracyMethod(learningProblem.getAccuracyMethod());
newNode.setAccuracyMethod(((PosNegLP)learningProblem).getAccuracyMethod());
}

}
Expand All @@ -733,7 +716,7 @@ private void extendNodeProper(ExampleBasedNode node, OWLClassExpression concept,
for (OWLIndividual i : coveredPositives) {
// TODO: move code to a separate function
if (quality != -1) {
boolean covered = rs.hasType(refinement, i);
boolean covered = reasoner.hasType(refinement, i);
if (!covered)
misclassifiedPositives++;
else
Expand All @@ -751,16 +734,16 @@ private void extendNodeProper(ExampleBasedNode node, OWLClassExpression concept,
newlyCoveredNegatives = new HashSet<>();

for (OWLIndividual i : coveredNegatives) {
boolean covered = rs.hasType(refinement, i);
boolean covered = reasoner.hasType(refinement, i);
if (covered)
newlyCoveredNegatives.add(i);
}
}

propernessCalcReasoningTimeNs += System.nanoTime() - propCalcReasoningStart2;
newNode.setQualityEvaluationMethod(ExampleBasedNode.QualityEvaluationMethod.REASONER);
if (quality != -1 && !(learningProblem.getAccuracyMethod() instanceof AccMethodNoWeakness) &&
learningProblem.getAccuracyMethod().getAccOrTooWeak2(
if (quality != -1 && !(((PosNegLP)learningProblem).getAccuracyMethod() instanceof AccMethodNoWeakness) &&
((PosNegLP)learningProblem).getAccuracyMethod().getAccOrTooWeak2(
newlyCoveredPositives.size(), nrOfPositiveExamples - newlyCoveredPositives.size(),
newlyCoveredNegatives.size(), nrOfNegativeExamples - newlyCoveredNegatives.size(),
1) == -1)
Expand All @@ -772,7 +755,7 @@ private void extendNodeProper(ExampleBasedNode node, OWLClassExpression concept,
quality = (nrOfPositiveExamples - newlyCoveredPositives.size())
+ newlyCoveredNegatives.size();
newNode.setCoveredExamples(newlyCoveredPositives, newlyCoveredNegatives);
newNode.setAccuracyMethod(learningProblem.getAccuracyMethod());
newNode.setAccuracyMethod(((PosNegLP)learningProblem).getAccuracyMethod());
}

}
Expand Down Expand Up @@ -861,19 +844,19 @@ private void printStatistics(boolean finalStats) {
logger.debug(expandedNodeString);
logger.debug("algorithm runtime " + Helper.prettyPrintNanoSeconds(algorithmRuntime));
logger.debug("size of candidate set: " + searchTree.size());
logger.debug("subsumption time: " + Helper.prettyPrintNanoSeconds(rs.getSubsumptionReasoningTimeNs()));
logger.debug("instance check time: " + Helper.prettyPrintNanoSeconds(rs.getInstanceCheckReasoningTimeNs()));
logger.debug("retrieval time: " + Helper.prettyPrintNanoSeconds(rs.getRetrievalReasoningTimeNs()));
logger.debug("subsumption time: " + Helper.prettyPrintNanoSeconds(reasoner.getSubsumptionReasoningTimeNs()));
logger.debug("instance check time: " + Helper.prettyPrintNanoSeconds(reasoner.getInstanceCheckReasoningTimeNs()));
logger.debug("retrieval time: " + Helper.prettyPrintNanoSeconds(reasoner.getRetrievalReasoningTimeNs()));
}

if (computeBenchmarkInformation) {

long reasoningTime = rs.getOverallReasoningTimeNs();
long reasoningTime = reasoner.getOverallReasoningTimeNs();
double reasoningPercentage = 100 * reasoningTime / (double) algorithmRuntime;
long propWithoutReasoning = propernessCalcTimeNs - propernessCalcReasoningTimeNs;
double propPercentage = 100 * propWithoutReasoning / (double) algorithmRuntime;
double deletionPercentage = 100 * childConceptsDeletionTimeNs / (double) algorithmRuntime;
long subTime = rs.getSubsumptionReasoningTimeNs();
long subTime = reasoner.getSubsumptionReasoningTimeNs();
double subPercentage = 100 * subTime / (double) algorithmRuntime;
double refinementPercentage = 100 * refinementCalcTimeNs / (double) algorithmRuntime;
double redundancyCheckPercentage = 100 * redundancyCheckTimeNs / (double) algorithmRuntime;
Expand Down Expand Up @@ -1056,10 +1039,6 @@ private void reduceCandidates() {
}
}

public void stop() {
stop = true;
}

public OWLClassExpression getBestSolution() {
return searchTreeStable.best().getConcept();
}
Expand Down Expand Up @@ -1115,11 +1094,11 @@ public void printBestSolutions(int nrOfSolutions) {
}

public ScorePosNeg getSolutionScore() {
return learningProblem.computeScore(getBestSolution());
return ((PosNegLP)learningProblem).computeScore(getBestSolution());
}

private ScorePosNeg getScore(OWLClassExpression d) {
return learningProblem.computeScore(d);
return ((PosNegLP)learningProblem).computeScore(d);
}

public ExampleBasedNode getStartNode() {
Expand Down Expand Up @@ -1209,10 +1188,6 @@ else if(conceptTests >= maxClassDescriptionTests){

}

public boolean isRunning() {
return isRunning;
}

public boolean isUseTreeTraversal() {
return useTreeTraversal;
}
Expand Down Expand Up @@ -1364,16 +1339,6 @@ public void setForceRefinementLengthIncrease(boolean forceRefinementLengthIncrea
this.forceRefinementLengthIncrease = forceRefinementLengthIncrease;
}

@Override
public int getMaxExecutionTimeInSeconds() {
return maxExecutionTimeInSeconds;
}

@Override
public void setMaxExecutionTimeInSeconds(int maxExecutionTimeInSeconds) {
this.maxExecutionTimeInSeconds = maxExecutionTimeInSeconds;
}

public int getMinExecutionTimeInSeconds() {
return minExecutionTimeInSeconds;
}
Expand Down
Expand Up @@ -181,16 +181,6 @@ public void changeReasonerComponent(AbstractReasonerComponent reasoningService)
* choose to store results.)
*/
public static final int MAX_NR_OF_RESULTS = 100;

/**
* Every algorithm must be able to return the score of the
* best solution found.
*
* @return Best score.
*/
// @Deprecated
// public abstract Score getSolutionScore();


/**
* @see #getCurrentlyBestEvaluatedDescription()
Expand Down Expand Up @@ -358,7 +348,7 @@ protected String descriptionToString(OWLClassExpression description) {
protected String getSolutionString() {
int current = 1;
String str = "";
for(EvaluatedDescription<? extends Score> ed : bestEvaluatedDescriptions.getSet().descendingSet()) {
for(EvaluatedDescription<? extends Score> ed : getCurrentlyBestEvaluatedDescriptions().descendingSet()) {
// temporary code
OWLClassExpression description = ed.getDescription();
String descriptionString = descriptionToString(description);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -13,7 +13,7 @@
<compiler.version>1.8</compiler.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--Define the AKSW dependency version -->
<slf4j.version>1.6.4</slf4j.version>
<slf4j.version>1.7.16</slf4j.version>
<log4j.version>1.2.16</log4j.version>
<solr.version>5.3.0</solr.version>
<owlapi.version>4.1.4</owlapi.version>
Expand Down

0 comments on commit 0f12a2f

Please sign in to comment.