Skip to content

Commit

Permalink
Merge pull request #538 from TNO/535-fix-includemetakis-call-in-reaso…
Browse files Browse the repository at this point in the history
…nerprocessor

535 fix includemetakis call in reasonerprocessor
  • Loading branch information
bnouwt authored Sep 11, 2024
2 parents 273b8b0 + fe3282f commit 74da78a
Show file tree
Hide file tree
Showing 18 changed files with 339 additions and 349 deletions.
54 changes: 7 additions & 47 deletions reasoner/src/main/java/eu/knowledge/engine/reasoner/BaseRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ public static enum MatchFlag {
public static final EnumSet<MatchFlag> ALL_OPTS = EnumSet.allOf(MatchFlag.class);
}

public static enum MatchStrategy {
SUPREME_LEVEL, ULTRA_LEVEL, ADVANCED_LEVEL, NORMAL_LEVEL, ENTRY_LEVEL
}

public static class TrivialBindingSetHandler implements TransformBindingSetHandler {

private Set<TriplePattern> consequent;
Expand Down Expand Up @@ -169,7 +165,7 @@ public Set<TriplePattern> getConsequent() {
return this.consequent;
}

public Set<Match> consequentMatches(Set<TriplePattern> anAntecedent, MatchStrategy aMatchStrategy) {
public Set<Match> consequentMatches(Set<TriplePattern> anAntecedent, EnumSet<MatchFlag> aMatchConfig) {
if (!this.consequent.isEmpty()) {
Rule r = new Rule(anAntecedent, new SinkBindingSetHandler() {

Expand All @@ -179,19 +175,18 @@ public CompletableFuture<Void> handle(BindingSet aBindingSet) {
return null;
}
});
Map<BaseRule, Set<Match>> matches = getMatches(this, new HashSet<>(Arrays.asList(r)), false,
aMatchStrategy);
Map<BaseRule, Set<Match>> matches = getMatches(this, new HashSet<>(Arrays.asList(r)), false, aMatchConfig);

if (matches.containsKey(r))
return matches.get(r);
}
return new HashSet<>();
}

public Set<Match> antecedentMatches(Set<TriplePattern> aConsequent, MatchStrategy aMatchStrategy) {
public Set<Match> antecedentMatches(Set<TriplePattern> aConsequent, EnumSet<MatchFlag> aMatchConfig) {
if (!this.antecedent.isEmpty()) {
Rule r = new Rule(new HashSet<>(), aConsequent);
Map<BaseRule, Set<Match>> matches = getMatches(this, new HashSet<>(Arrays.asList(r)), true, aMatchStrategy);
Map<BaseRule, Set<Match>> matches = getMatches(this, new HashSet<>(Arrays.asList(r)), true, aMatchConfig);
if (matches.containsKey(r))
return matches.get(r);
}
Expand Down Expand Up @@ -382,9 +377,7 @@ public static Map<TriplePattern, Set<CombiMatch>> getMatchesPerTriplePerRule(Set
* @return A set of matches that all contribute to some full matche.
*/
public static Map<BaseRule, Set<Match>> getMatches(BaseRule aTargetRule, Set<BaseRule> someCandidateRules,
boolean antecedentOfTarget, MatchStrategy aStrategy) {

EnumSet<MatchFlag> config = fromStrategyToConfig(aStrategy, antecedentOfTarget);
boolean antecedentOfTarget, EnumSet<MatchFlag> config) {

Set<TriplePattern> targetGP = antecedentOfTarget ? aTargetRule.getAntecedent() : aTargetRule.getConsequent();

Expand Down Expand Up @@ -417,8 +410,8 @@ public static Map<BaseRule, Set<Match>> getMatches(BaseRule aTargetRule, Set<Bas

if (triplePatternMatchesIter.hasNext()) {
Set<CombiMatch> value = triplePatternMatchesIter.next().getValue();
LOG.trace("{}/{} ({}): biggest: {}, smaller: {}", 1, combiMatchesPerTriple.size(), value.size(),
biggestMatches.size(), smallerMatches.size());
LOG.trace("{}/{} ({}): biggest: {}, smaller: {} ({})", 1, combiMatchesPerTriple.size(), value.size(),
biggestMatches.size(), smallerMatches.size(), config);
biggestMatches.addAll(value);
}

Expand Down Expand Up @@ -545,39 +538,6 @@ private static boolean isSubCombiMatch(CombiMatch aSmallerMatch, List<CombiMatch
return false;
}

private static EnumSet<MatchFlag> fromStrategyToConfig(MatchStrategy aStrategy, boolean antecedentOfTarget) {
EnumSet<MatchFlag> config;
switch (aStrategy) {
case ENTRY_LEVEL:
config = EnumSet.of(MatchFlag.FULLY_COVERED, MatchFlag.ONE_TO_ONE, MatchFlag.ONLY_BIGGEST,
MatchFlag.SINGLE_RULE);
break;
case NORMAL_LEVEL:
config = EnumSet.of(MatchFlag.ONE_TO_ONE, MatchFlag.ONLY_BIGGEST);

// disable fully covered when matching consequents.
if (antecedentOfTarget)
config.add(MatchFlag.FULLY_COVERED);
break;
case ADVANCED_LEVEL:
config = EnumSet.of(MatchFlag.ONLY_BIGGEST);

// disable fully covered when matching consequents.
if (antecedentOfTarget)
config.add(MatchFlag.FULLY_COVERED);
break;
case ULTRA_LEVEL:
config = EnumSet.of(MatchFlag.ONLY_BIGGEST);
break;
case SUPREME_LEVEL:
config = EnumSet.noneOf(MatchFlag.class);
break;
default:
config = EnumSet.noneOf(MatchFlag.class);
}
return config;
}

private static void printCombiMatchesPerTriple(Map<TriplePattern, Set<CombiMatch>> combiMatchesPerTriple) {
StringBuilder sb = new StringBuilder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand All @@ -11,7 +12,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import eu.knowledge.engine.reasoner.BaseRule.MatchStrategy;
import eu.knowledge.engine.reasoner.BaseRule.MatchFlag;
import eu.knowledge.engine.reasoner.api.Binding;
import eu.knowledge.engine.reasoner.api.BindingSet;
import eu.knowledge.engine.reasoner.api.TriplePattern;
Expand All @@ -38,7 +39,7 @@ public class ReasonerPlan {
private final ProactiveRule start;
private final Map<BaseRule, RuleNode> ruleToRuleNode;
private boolean done;
private MatchStrategy strategy = MatchStrategy.ULTRA_LEVEL;
private EnumSet<MatchFlag> matchConfig = EnumSet.noneOf(MatchFlag.class);
private boolean useTaskBoard = true;

public ReasonerPlan(RuleStore aStore, ProactiveRule aStartRule) {
Expand All @@ -48,11 +49,11 @@ public ReasonerPlan(RuleStore aStore, ProactiveRule aStartRule) {
createOrGetReasonerNode(this.start, null);
}

public ReasonerPlan(RuleStore aStore, ProactiveRule aStartRule, MatchStrategy aStrategy) {
public ReasonerPlan(RuleStore aStore, ProactiveRule aStartRule, EnumSet<MatchFlag> aConfig) {
this.store = aStore;
this.start = aStartRule;
this.ruleToRuleNode = new HashMap<>();
this.strategy = aStrategy;
this.matchConfig = aConfig;
createOrGetReasonerNode(this.start, null);
}

Expand Down Expand Up @@ -231,7 +232,7 @@ private RuleNode createOrGetReasonerNode(BaseRule aRule, BaseRule aParent) {
// for now we only are interested in antecedent neighbors.
// TODO for looping we DO want to consider consequent neighbors as well.

this.store.getAntecedentNeighbors(aRule, this.strategy).forEach((rule, matches) -> {
this.store.getAntecedentNeighbors(aRule, this.matchConfig).forEach((rule, matches) -> {
if (!(rule instanceof ProactiveRule)) {
assert reasonerNode instanceof AntSide;
var newNode = createOrGetReasonerNode(rule, aRule);
Expand All @@ -247,7 +248,7 @@ private RuleNode createOrGetReasonerNode(BaseRule aRule, BaseRule aParent) {
});
} else {
// interested in both consequent and antecedent neighbors
this.store.getConsequentNeighbors(aRule, this.strategy).forEach((rule, matches) -> {
this.store.getConsequentNeighbors(aRule, this.matchConfig).forEach((rule, matches) -> {
if (!(rule instanceof ProactiveRule)) {
assert reasonerNode instanceof ConsSide;
var newNode = createOrGetReasonerNode(rule, aRule);
Expand All @@ -266,13 +267,13 @@ private RuleNode createOrGetReasonerNode(BaseRule aRule, BaseRule aParent) {
// determine whether our parent matches us partially
boolean ourAntecedentFullyMatchesParentConsequent = false;

if (aParent != null && this.store.getAntecedentNeighbors(aRule, this.strategy).containsKey(aParent)) {
if (aParent != null && this.store.getAntecedentNeighbors(aRule, this.matchConfig).containsKey(aParent)) {
ourAntecedentFullyMatchesParentConsequent = antecedentFullyMatchesConsequent(aRule, aParent,
this.store.getAntecedentNeighbors(aRule, this.strategy).get(aParent), this.strategy);
this.store.getAntecedentNeighbors(aRule, this.matchConfig).get(aParent));
}

if (!ourAntecedentFullyMatchesParentConsequent) {
this.store.getAntecedentNeighbors(aRule, this.strategy).forEach((rule, matches) -> {
this.store.getAntecedentNeighbors(aRule, this.matchConfig).forEach((rule, matches) -> {
if (!(rule instanceof ProactiveRule)) {
assert reasonerNode instanceof AntSide;
var newNode = createOrGetReasonerNode(rule, aRule);
Expand Down Expand Up @@ -317,7 +318,7 @@ private void scheduleOrDoTask(RuleNode current, TaskBoard taskBoard) {
* @return
*/
private boolean antecedentFullyMatchesConsequent(BaseRule antecedentRule, BaseRule consequentRule,
Set<Match> someMatches, MatchStrategy aMatchStrategy) {
Set<Match> someMatches) {

var antecedent = antecedentRule.getAntecedent();
var consequent = consequentRule.getConsequent();
Expand Down Expand Up @@ -348,8 +349,8 @@ private boolean antecedentFullyMatchesConsequent(BaseRule antecedentRule, BaseRu
return false;
}

public MatchStrategy getMatchStrategy() {
return this.strategy;
public EnumSet<MatchFlag> getMatchConfig() {
return this.matchConfig;
}

public RuleStore getStore() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package eu.knowledge.engine.reasoner.rulestore;

import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import eu.knowledge.engine.reasoner.BaseRule;
import eu.knowledge.engine.reasoner.BaseRule.MatchStrategy;
import eu.knowledge.engine.reasoner.BaseRule.MatchFlag;
import eu.knowledge.engine.reasoner.Match;

/**
Expand All @@ -31,128 +32,47 @@ public class MatchNode {
* All other rules in the {@link MatchNode#store} whose consequents match this
* rule's antecedent according to the {@link MatchStrategy#NORMAL_LEVEL}
*/
private Map<BaseRule, Set<Match>> antecedentNeighborsNormal;

/**
* All other rules in the {@link MatchNode#store} whose consequents match this
* rule's antecedent according to the {@link MatchStrategy#ADVANCED_LEVEL}
*/
private Map<BaseRule, Set<Match>> antecedentNeighborsAdvanced;

/**
* All other rules in the {@link MatchNode#store} whose consequents match this
* rule's antecedent according to the {@link MatchStrategy#ULTRA_LEVEL}
*/
private Map<BaseRule, Set<Match>> antecedentNeighborsUltra;
private Map<BaseRule, Set<Match>> antecedentNeighbors;

/**
* All other rules in the {@link MatchNode#store} whose antecedents match this
* rule's consequent according to the {@link MatchStrategy#NORMAL_LEVEL}
*/
private Map<BaseRule, Set<Match>> consequentNeighborsNormal;

/**
* All other rules in the {@link MatchNode#store} whose antecedents match this
* rule's consequent according to the {@link MatchStrategy#ADVANCED_LEVEL}
*/
private Map<BaseRule, Set<Match>> consequentNeighborsAdvanced;

/**
* All other rules in the {@link MatchNode#store} whose antecedents match this
* rule's consequent according to the {@link MatchStrategy#ULTRA_LEVEL}
*/
private Map<BaseRule, Set<Match>> consequentNeighborsUltra;
private Map<BaseRule, Set<Match>> consequentNeighbors;

public MatchNode(BaseRule aRule) {
this.rule = aRule;
this.antecedentNeighborsNormal = new HashMap<>();
this.antecedentNeighborsAdvanced = new HashMap<>();
this.antecedentNeighborsUltra = new HashMap<>();
this.consequentNeighborsNormal = new HashMap<>();
this.consequentNeighborsAdvanced = new HashMap<>();
this.consequentNeighborsUltra = new HashMap<>();
this.antecedentNeighbors = new HashMap<>();
this.consequentNeighbors = new HashMap<>();

}

public BaseRule getRule() {
return this.rule;
}

public void setConsequentNeighbor(BaseRule aRule, Set<Match> someMatches) {
this.setConsequentNeighbor(aRule, someMatches, MatchStrategy.ULTRA_LEVEL);
}

public void setAntecedentNeighbor(BaseRule aRule, Set<Match> someMatches) {
this.setAntecedentNeighbor(aRule, someMatches, MatchStrategy.ULTRA_LEVEL);
}

public void setConsequentNeighbor(BaseRule aRule, Set<Match> someMatches, MatchStrategy aStrategy) {
Map<BaseRule, Set<Match>> neighbors;
if (aStrategy.equals(MatchStrategy.NORMAL_LEVEL) || aStrategy.equals(MatchStrategy.ENTRY_LEVEL)) {
neighbors = this.consequentNeighborsNormal;
} else if (aStrategy.equals(MatchStrategy.ADVANCED_LEVEL)) {
neighbors = this.consequentNeighborsAdvanced;
} else if (aStrategy.equals(MatchStrategy.ULTRA_LEVEL)) {
neighbors = this.consequentNeighborsUltra;
} else {
assert false;
neighbors = new HashMap<>();
}
Map<BaseRule, Set<Match>> neighbors = this.consequentNeighbors;

if (!neighbors.containsKey(aRule)) {
neighbors.put(aRule, someMatches);
}
}

public void setAntecedentNeighbor(BaseRule aRule, Set<Match> someMatches, MatchStrategy aStrategy) {
Map<BaseRule, Set<Match>> neighbors;
if (aStrategy.equals(MatchStrategy.NORMAL_LEVEL) || aStrategy.equals(MatchStrategy.ENTRY_LEVEL)) {
neighbors = this.antecedentNeighborsNormal;
} else if (aStrategy.equals(MatchStrategy.ADVANCED_LEVEL)) {
neighbors = this.antecedentNeighborsAdvanced;
} else if (aStrategy.equals(MatchStrategy.ULTRA_LEVEL)) {
neighbors = this.antecedentNeighborsUltra;
} else {
assert false;
neighbors = new HashMap<>();
}
public void setAntecedentNeighbor(BaseRule aRule, Set<Match> someMatches) {
Map<BaseRule, Set<Match>> neighbors = this.antecedentNeighbors;

if (!neighbors.containsKey(aRule)) {
neighbors.put(aRule, someMatches);
}
}

public Map<BaseRule, Set<Match>> getConsequentNeighbors() {
return this.getConsequentNeighbors(MatchStrategy.ULTRA_LEVEL);
return this.consequentNeighbors;
}

public Map<BaseRule, Set<Match>> getAntecedentNeighbors() {
return this.getAntecedentNeighbors(MatchStrategy.ULTRA_LEVEL);
}

public Map<BaseRule, Set<Match>> getConsequentNeighbors(MatchStrategy aStrategy) {
if (aStrategy.equals(MatchStrategy.NORMAL_LEVEL)) {
return this.consequentNeighborsNormal;
} else if (aStrategy.equals(MatchStrategy.ADVANCED_LEVEL)) {
return this.consequentNeighborsAdvanced;
} else if (aStrategy.equals(MatchStrategy.ULTRA_LEVEL)) {
return this.consequentNeighborsUltra;
} else {
assert false;
return null;
}
}

public Map<BaseRule, Set<Match>> getAntecedentNeighbors(MatchStrategy aStrategy) {
if (aStrategy.equals(MatchStrategy.NORMAL_LEVEL)) {
return this.antecedentNeighborsNormal;
} else if (aStrategy.equals(MatchStrategy.ADVANCED_LEVEL)) {
return this.antecedentNeighborsAdvanced;
} else if (aStrategy.equals(MatchStrategy.ULTRA_LEVEL)) {
return this.antecedentNeighborsUltra;
} else {
assert false;
return null;
}
return this.antecedentNeighbors;
}

/**
Expand All @@ -163,12 +83,8 @@ public RuleStore getStore() {
}

public void reset() {
this.antecedentNeighborsNormal.clear();
this.antecedentNeighborsAdvanced.clear();
this.antecedentNeighborsUltra.clear();
this.consequentNeighborsNormal.clear();
this.consequentNeighborsAdvanced.clear();
this.consequentNeighborsUltra.clear();
this.antecedentNeighbors.clear();
this.consequentNeighbors.clear();

}
}
Loading

0 comments on commit 74da78a

Please sign in to comment.