Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions reasoner/src/main/java/eu/knowledge/engine/reasoner/Match.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,16 @@ public Match merge(Match otherMatch) {

Match m = null;

// if both sides of the matching patterns do not overlap
boolean doMapsIntersect = doIntersect(this.getMatchingPatterns(), otherMatch.getMatchingPatterns());

if (!doMapsIntersect) {
var mergedMatchingPatterns = mergeMatchingPatterns(this.getMatchingPatterns(),
otherMatch.getMatchingPatterns());

// if both sides of the matching patterns do not overlap
if (mergedMatchingPatterns != null) {
// and if the mappings do not conflict
Map<TripleNode, TripleNode> mergedMapping = mergeContexts(this.getMappings(), otherMatch.getMappings());
if (mergedMapping != null) {
// if both patterns and mappings do not conflict.
Map<TriplePattern, TriplePattern> newMatchingPatterns = new HashMap<>(this.getMatchingPatterns());
newMatchingPatterns.putAll(otherMatch.getMatchingPatterns());

Map<TripleNode, TripleNode> newMapping = new HashMap<>(this.getMappings());
newMapping.putAll(otherMatch.getMappings());
m = new Match(newMatchingPatterns, newMapping);
m = new Match(mergedMatchingPatterns, mergedMapping);
}
}
return m;
Expand All @@ -101,19 +96,29 @@ public Match merge(Match otherMatch) {
*
* @param aFirstMap
* @param aSecondMap
* @return
* @return {@code null} if the matching patterns overlap on either keys or
* values, otherwise the merged mapping.
*/
private boolean doIntersect(Map<TriplePattern, TriplePattern> aFirstMap,
private HashMap<TriplePattern, TriplePattern> mergeMatchingPatterns(Map<TriplePattern, TriplePattern> aFirstMap,
Map<TriplePattern, TriplePattern> aSecondMap) {

var mergedMatchingPatterns = new HashMap<TriplePattern, TriplePattern>(aFirstMap.size() + aSecondMap.size());

boolean firstTime = true;

for (Entry<TriplePattern, TriplePattern> entry1 : aFirstMap.entrySet()) {
for (Entry<TriplePattern, TriplePattern> entry2 : aSecondMap.entrySet()) {
if (entry1.getKey().equals(entry2.getKey()) || entry1.getValue().equals(entry2.getValue()))
return true;
return null;

if (firstTime)
mergedMatchingPatterns.put(entry2.getKey(), entry2.getValue());
}
mergedMatchingPatterns.put(entry1.getKey(), entry1.getValue());
firstTime = false;
}

return false;
return mergedMatchingPatterns;
}

public Map<TripleNode, TripleNode> getMappings() {
Expand All @@ -135,7 +140,10 @@ private Map<TripleNode, TripleNode> mergeContexts(Map<TripleNode, TripleNode> ex
Map<TripleNode, TripleNode> newContext) {

Collection<TripleNode> existingContextValues = existingContext.values();
Map<TripleNode, TripleNode> mergedContext = new HashMap<TripleNode, TripleNode>(existingContext);
Map<TripleNode, TripleNode> mergedContext = new HashMap<TripleNode, TripleNode>(
existingContext.size() + newContext.size());
mergedContext.putAll(existingContext);

for (Map.Entry<TripleNode, TripleNode> newEntry : newContext.entrySet()) {
Node node;
if ((node = getOtherNode(existingContext, newEntry.getKey().node)) != null) {
Expand Down