Skip to content

Commit

Permalink
Move all removals in taxonomy states to pruning methods
Browse files Browse the repository at this point in the history
Computations that consume collections mainatined by the taxonomy states request the collections after the processing is done. The collections should be completely pruned at this point and thus empty.
  • Loading branch information
Peter Skocovsky committed Jun 9, 2016
1 parent 38cff66 commit 11227a3
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@
package org.semanticweb.elk.reasoner.stages;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.semanticweb.elk.owl.interfaces.ElkClass;
Expand All @@ -38,6 +35,7 @@
import org.semanticweb.elk.reasoner.indexing.model.IndexedClass;
import org.semanticweb.elk.reasoner.indexing.model.IndexedContextRoot;
import org.semanticweb.elk.reasoner.indexing.model.ModifiableIndexedClassExpression;
import org.semanticweb.elk.reasoner.indexing.model.OntologyIndex;
import org.semanticweb.elk.reasoner.saturation.SaturationState;
import org.semanticweb.elk.reasoner.saturation.SaturationStateDummyChangeListener;
import org.semanticweb.elk.reasoner.saturation.context.Context;
Expand All @@ -48,6 +46,7 @@
import org.semanticweb.elk.reasoner.taxonomy.model.Taxonomy;
import org.semanticweb.elk.reasoner.taxonomy.model.TaxonomyNode;
import org.semanticweb.elk.reasoner.taxonomy.model.UpdateableTaxonomy;
import org.semanticweb.elk.util.collections.Operations;

/**
* Stores information about the state of the class taxonomy
Expand All @@ -65,22 +64,24 @@ public class ClassTaxonomyState {
* Contains at least all classes that are in ontology, but either are
* removed from taxonomy or their super-nodes in taxonomy were removed.
*/
private final Set<IndexedClass> modified_ = Collections
.newSetFromMap(new ConcurrentHashMap<IndexedClass, Boolean>());
private final Queue<IndexedClass> modified_ = new ConcurrentLinkedQueue<IndexedClass>();

/**
* Contains at least all classes that are in taxonomy, but either are
* removed from ontology or their context became not saturated.
*/
private final Queue<IndexedClass> removed_ = new ConcurrentLinkedQueue<IndexedClass>();

private final OntologyIndex ontologyIndex_;

private final ElkPolarityExpressionConverter converter_;

public <C extends Context> ClassTaxonomyState(
final SaturationState<C> saturationState,
final DifferentialIndex ontologyIndex,
final ElkObject.Factory elkFactory) {

this.ontologyIndex_ = ontologyIndex;
this.converter_ = new ElkPolarityExpressionConverterImpl(elkFactory,
ontologyIndex);

Expand Down Expand Up @@ -114,7 +115,7 @@ public void contextMarkNonSaturated(final C context) {

}

final NodeStore.Listener<ElkClass> nodeStoreListener_ = new DummyNodeStoreListener<ElkClass>() {
private final NodeStore.Listener<ElkClass> nodeStoreListener_ = new DummyNodeStoreListener<ElkClass>() {

@Override
public void memberForNodeDisappeared(final ElkClass member,
Expand All @@ -124,7 +125,7 @@ public void memberForNodeDisappeared(final ElkClass member,

};

final Taxonomy.Listener<ElkClass> taxonomyListener_ = new DummyTaxonomyListener<ElkClass>() {
private final Taxonomy.Listener<ElkClass> taxonomyListener_ = new DummyTaxonomyListener<ElkClass>() {

@Override
public void directSupernodeRemoval(final TaxonomyNode<ElkClass> subNode,
Expand All @@ -141,22 +142,60 @@ public UpdateableTaxonomy<ElkClass> getTaxonomy() {
}

private void addModified(final ElkClass elkClass) {
ModifiableIndexedClassExpression converted = elkClass
final ModifiableIndexedClassExpression converted = elkClass
.accept(converter_);
if (converted != null && (converted instanceof IndexedClass)) {
final IndexedClass cls = (IndexedClass) converted;
modified_.add(cls);
}
}

private void pruneModified() {
private int pruneModified() {
final Iterator<IndexedClass> iter = modified_.iterator();
int size = 0;
while (iter.hasNext()) {
final IndexedClass cls = iter.next();
/* @formatter:off
*
* Should be pruned when:
* it is not in ontology, or
* it is in the top node, or
* it has super-nodes in taxonomy.
*
* @formatter:on
*/
if (!cls.occurs()) {
iter.remove();
continue;
}
// else
if (taxonomy_ == null) {
// it is not in taxonomy
size++;
continue;
}
// else
final TaxonomyNode<ElkClass> node = taxonomy_
.getNode(cls.getElkEntity());
if (node == null) {
// it is not in taxonomy
size++;
continue;
}
// else
if (node.equals(taxonomy_.getTopNode())) {
iter.remove();
continue;
}
// else
if (!node.getDirectSuperNodes().isEmpty()) {
iter.remove();
continue;
}
// else
size++;
}
return size;
}

/**
Expand All @@ -168,23 +207,57 @@ private void pruneModified() {
* ontology, but either are removed from taxonomy or their
* super-nodes in taxonomy were removed.
*/
Set<IndexedClass> getModified() {
pruneModified();
return modified_;
Collection<IndexedClass> getModified() {
final int size = pruneModified();
/*
* since getting the size of the queue is a linear operation, use the
* computed size
*/
return Operations.getCollection(modified_, size);
}

private void pruneRemoved() {
private int pruneRemoved() {
final Iterator<IndexedClass> iter = removed_.iterator();
int size = 0;
while (iter.hasNext()) {
final IndexedClass cls = iter.next();
if (taxonomy_ == null// TODO: Never set taxonomy_ to null !!!
|| taxonomy_.getNode(cls.getElkEntity()) == null) {
/* @formatter:off
*
* Should be pruned when
* it is not in taxonomy, or
* it is in the bottom class.
*
* @formatter:on
*/
if (taxonomy_ == null) {// TODO: Never set taxonomy_ to null !!!
// it is not in taxonomy
iter.remove();
if (cls.occurs()) {
modified_.add(cls);
}
continue;
}
// else
final TaxonomyNode<ElkClass> node = taxonomy_
.getNode(cls.getElkEntity());
if (node == null) {
iter.remove();
if (cls.occurs()) {
modified_.add(cls);
}
continue;
}
// else
if (cls == ontologyIndex_.getOwlNothing()) {
iter.remove();
if (cls.occurs()) {
modified_.add(cls);
}
continue;
}
size++;
}
return size;
}

/**
Expand All @@ -197,8 +270,12 @@ private void pruneRemoved() {
* became not saturated.
*/
Collection<IndexedClass> getRemoved() {
pruneRemoved();
return removed_;
final int size = pruneRemoved();
/*
* since getting the size of the queue is a linear operation, use the
* computed size
*/
return Operations.getCollection(removed_, size);
}

public Writer getWriter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.util.Collection;

import org.semanticweb.elk.exceptions.ElkRuntimeException;
import org.semanticweb.elk.reasoner.incremental.IncrementalStages;
import org.semanticweb.elk.reasoner.indexing.model.IndexedClass;
import org.semanticweb.elk.reasoner.taxonomy.ClassTaxonomyComputation;
Expand Down Expand Up @@ -82,6 +83,13 @@ public boolean postExecute() {
if (!super.postExecute()) {
return false;
}
final Collection<IndexedClass> modified = reasoner.classTaxonomyState
.getModified();
if (!modified.isEmpty()) {
throw new ElkRuntimeException(
ClassTaxonomyComputation.class.getSimpleName()
+ " did not consume all modified classes!");
}
reasoner.ontologyIndex.initClassChanges();
reasoner.ruleAndConclusionStats.add(computation_
.getRuleAndConclusionStatistics());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
*/
package org.semanticweb.elk.reasoner.stages;

import java.util.Set;
import java.util.Collection;

import org.semanticweb.elk.exceptions.ElkRuntimeException;
import org.semanticweb.elk.reasoner.indexing.model.IndexedIndividual;
import org.semanticweb.elk.reasoner.taxonomy.InstanceTaxonomyComputation;

Expand Down Expand Up @@ -62,7 +63,7 @@ public boolean preExecute() {
if (!super.preExecute())
return false;

final Set<IndexedIndividual> modified = reasoner.instanceTaxonomyState
final Collection<IndexedIndividual> modified = reasoner.instanceTaxonomyState
.getModified();

this.computation_ = new InstanceTaxonomyComputation(modified,
Expand All @@ -83,6 +84,13 @@ public boolean postExecute() {
return false;
}

final Collection<IndexedIndividual> modified = reasoner.instanceTaxonomyState
.getModified();
if (!modified.isEmpty()) {
throw new ElkRuntimeException(
InstanceTaxonomyComputation.class.getSimpleName()
+ " did not consume all modified individuals!");
}
reasoner.ontologyIndex.initIndividualChanges();
// reasoner.ruleAndConclusionStats.add(computation_.getRuleAndConclusionStatistics());
this.computation_ = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Collection;

import org.semanticweb.elk.exceptions.ElkException;
import org.semanticweb.elk.exceptions.ElkRuntimeException;
import org.semanticweb.elk.reasoner.incremental.IncrementalStages;
import org.semanticweb.elk.reasoner.indexing.model.IndexedClass;
import org.semanticweb.elk.reasoner.indexing.model.IndexedClassEntity;
Expand Down Expand Up @@ -98,6 +99,18 @@ public boolean postExecute() {
if (!super.postExecute()) {
return false;
}
final Collection<IndexedClass> removedClasses = reasoner.classTaxonomyState
.getRemoved();
if (!removedClasses.isEmpty()) {
throw new ElkRuntimeException(TaxonomyCleaning.class.getSimpleName()
+ " did not consume all removed classes!");
}
final Collection<IndexedIndividual> removedIndividuals = reasoner.instanceTaxonomyState
.getRemoved();
if (!removedIndividuals.isEmpty()) {
throw new ElkRuntimeException(TaxonomyCleaning.class.getSimpleName()
+ " did not consume all removed individuals!");
}
// at this point we're done with unsaturated contexts
markAllContextsAsSaturated();
this.cleaning_ = null;
Expand Down
Loading

0 comments on commit 11227a3

Please sign in to comment.