Skip to content
This repository has been archived by the owner on Mar 14, 2019. It is now read-only.

Commit

Permalink
prevent ConcurrentModificationException in case new partial bindings are
Browse files Browse the repository at this point in the history
added while rules are being processed
  • Loading branch information
Tobias Hammerschmidt committed Nov 4, 2015
1 parent 744f507 commit 8ee8a32
Showing 1 changed file with 43 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
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.Queue;
import java.util.Set;
import java.util.logging.Level;

Expand Down Expand Up @@ -48,7 +50,7 @@ public class ContinuousRulesStrategy extends SROIQStrategy {
private Interpreter interpreter;
private boolean merging;
private Set<PartialBinding> unsafeRules;
private Set<PartialBinding> partialBindings;
private Queue<PartialBinding> partialBindings;
private Map<Pair<Rule, VariableBinding>, Integer> rulesApplied;
private RulesToATermTranslator atermTranslator;
private RuleAtomAsserter ruleAtomAsserter;
Expand All @@ -57,21 +59,22 @@ public class ContinuousRulesStrategy extends SROIQStrategy {
public ContinuousRulesStrategy(ABox abox) {
super( abox );
bindingStrategy = new BindingGeneratorStrategyImpl( abox );
partialBindings = new HashSet<PartialBinding>();
partialBindings = new LinkedList<PartialBinding>();
unsafeRules = new HashSet<PartialBinding>();
rulesApplied = new HashMap<Pair<Rule, VariableBinding>, Integer>();
atermTranslator = new RulesToATermTranslator();
ruleAtomAsserter = new RuleAtomAsserter();
atomTester = new TrivialSatisfactionHelpers( abox );
}

public void addUnsafeRule(Rule rule, Set<ATermAppl> explain) {
unsafeRules.add(new PartialBinding(rule, new VariableBinding(abox), new DependencySet(explain)));
}

public void addPartialBinding(PartialBinding binding) {
partialBindings.add(binding);
}
if( !partialBindings.contains(binding) )
partialBindings.add(binding);
}

@Override
public Edge addEdge(Individual subj, Role pred, Node obj, DependencySet ds) {
Expand All @@ -82,7 +85,7 @@ public Edge addEdge(Individual subj, Role pred, Node obj, DependencySet ds) {
interpreter.alphaNet.activateEdge(edge);
}
}

return edge;
}

Expand All @@ -95,29 +98,29 @@ public void addType(Node node, ATermAppl c, DependencySet ds) {
interpreter.alphaNet.activateType(ind, c, ds);
}
}

@Override
protected boolean mergeIndividuals(Individual y, Individual x, DependencySet ds) {
if (super.mergeIndividuals(y, x, ds)) {
if (interpreter != null) {
interpreter.alphaNet.activateDifferents(y);
}
return true;
}
return false;
if (super.mergeIndividuals(y, x, ds)) {
if (interpreter != null) {
interpreter.alphaNet.activateDifferents(y);
}
return true;
}
return false;
}

@Override
public boolean setDifferent(Node y, Node z, DependencySet ds) {
if (super.setDifferent(y, z, ds)) {
if (super.setDifferent(y, z, ds)) {
if( interpreter != null && !merging && !abox.isClosed() && y.isRootNominal() && y.isIndividual() && z.isRootNominal() && z.isIndividual()) {
interpreter.alphaNet.activateDifferent((Individual) y, (Individual) z, ds);
}
return true;
}
return false;

return true;
}

return false;
}

public Collection<PartialBinding> applyRete() {
Expand All @@ -134,17 +137,15 @@ public Collection<PartialBinding> applyRete() {
t = timers.startTimer( "rule-reteRun" );
interpreter.run();
t.stop();

return interpreter.getBindings();
}



public void applyRuleBindings() {

int total = 0;

for( PartialBinding ruleBinding : partialBindings ) {
for( PartialBinding ruleBinding = partialBindings.poll(); ruleBinding != null; ruleBinding = partialBindings.poll() ) {
Rule rule = ruleBinding.getRule();
VariableBinding initial = ruleBinding.getBinding();

Expand All @@ -160,14 +161,14 @@ public void applyRuleBindings() {
log.fine( "Binding: " + binding );
log.fine( "total:" + total );
}

int branch = createDisjunctionsFromBinding( binding, rule, ruleBinding
.getDependencySet() );

if( branch >= 0 ) {
rulesApplied.put( ruleKey, branch );
}

if( abox.isClosed() ) {
return;
}
Expand Down Expand Up @@ -195,17 +196,17 @@ public void complete(Expressivity expr) {
continue;

Set<ATermAppl> explain = abox.doExplanation() ? rule.getExplanation(atermTranslator) : Collections
.<ATermAppl> emptySet();
.<ATermAppl> emptySet();

try {
compiler.compile(normalizedRule, explain);
}
catch (UnsupportedOperationException uoe) {
throw new RuntimeException("Unsupported rule " + normalizedRule, uoe);
}
compiler.compile(normalizedRule, explain);
}
catch(UnsupportedOperationException uoe) {
throw new RuntimeException("Unsupported rule " + normalizedRule, uoe);
}
}
t.stop();

AlphaNetwork alphaNet = compiler.getAlphaNet();
if (abox.doExplanation()) {
alphaNet.setDoExplanation(true);
Expand Down Expand Up @@ -245,7 +246,7 @@ public void complete(Expressivity expr) {
if( abox.isClosed() )
break;
}

if( abox.isClosed() )
break;

Expand Down Expand Up @@ -310,13 +311,13 @@ private int createDisjunctionsFromBinding(VariableBinding binding, Rule rule, De
atoms.add( atom );
}
}

// all the atoms in the body are true
if( atoms.isEmpty() ) {
if( rule.getHead().isEmpty() ) {
if( log.isLoggable( Level.FINE ) )
log.fine( "Empty head for rule " + rule );
abox.setClash( Clash.unexplained( null, ds ) );
abox.setClash( Clash.unexplained( null, ds ) );
}
else {
for( RuleAtom atom : rule.getHead() ) {
Expand All @@ -325,7 +326,7 @@ private int createDisjunctionsFromBinding(VariableBinding binding, Rule rule, De
}
return -1;
}

int bodyAtomCount = atoms.size();

for( RuleAtom atom : rule.getHead() ) {
Expand All @@ -334,7 +335,7 @@ private int createDisjunctionsFromBinding(VariableBinding binding, Rule rule, De
atoms.add( atom );
}
}

// all no head atoms are added to the list they are all true (unless
// there were no head atoms to begin with) which means there is nothing
// to be done
Expand All @@ -343,13 +344,13 @@ private int createDisjunctionsFromBinding(VariableBinding binding, Rule rule, De
}
// if there is only one atom in the list that should be a body atom
// (otherwise it would mean that all body atoms are true which would
// have been caught with the if condition at the beginning) and we
// have been caught with the if condition at the beginning) and we
// can directly assert it without creating a disjunction
else if( atoms.size() == 1 ) {
ruleAtomAsserter.assertAtom( atoms.get( 0 ), binding, ds, true, abox, this );
return -1;
}
else {
else {
RuleBranch r = new RuleBranch( abox, this, ruleAtomAsserter, atoms, binding, bodyAtomCount, ds );
addBranch( r );
r.tryNext();
Expand Down

0 comments on commit 8ee8a32

Please sign in to comment.