Skip to content

Commit

Permalink
Add method that compares two triples and determines action to take
Browse files Browse the repository at this point in the history
  • Loading branch information
Sophietje committed Dec 18, 2024
1 parent b2e54de commit f4129a3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

import java.io.IOException;
import java.time.Instant;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -729,6 +725,56 @@ public Set<KnowledgeGap> getKnowledgeGaps(RuleNode plan) {
return existingOrGaps;
}

private static TripleMatchType getTripleMatchType(TriplePattern existingTriple, TriplePattern newTriple) {
Map<TripleNode, TripleNode> matches = existingTriple.findMatches(newTriple);
if (matches == null) {
return TripleMatchType.ADD_TRIPLE;
} else {
ArrayList<TripleMatchType> matchType = new ArrayList<>();
for (Entry<TripleNode, TripleNode> match : matches.entrySet()) {
if (match.getKey().node.isVariable() && !match.getValue().node.isVariable()) {
matchType.add(TripleMatchType.IGNORE_TRIPLE);
} else if (!match.getKey().node.isVariable() && match.getValue().node.isVariable()) {
matchType.add(TripleMatchType.REPLACE_TRIPLE);
}
}

boolean equalMatchTypes = matchType.stream().allMatch(m -> m.equals(matchType.get(0)));
return equalMatchTypes ? matchType.get(0) : TripleMatchType.ADD_GAP;
}
}

private static KnowledgeGap applyMatchAction(TripleMatchType action, KnowledgeGap existingTriple, TriplePattern tripleToAdd, TriplePattern tripleToRemove) {
KnowledgeGap result = existingTriple;
switch(action) {
case ADD_GAP -> result.add(tripleToAdd);
case REPLACE_TRIPLE -> {
result.remove(tripleToRemove);
result.add(tripleToAdd);
}
}
return result;
}

private static KnowledgeGap mergeGap(KnowledgeGap gap, KnowledgeGap gapToAdd) {
KnowledgeGap result = null;
for (TriplePattern tripleToAdd : gapToAdd) {
TripleMatchType actionToTake = null;
for (TriplePattern existingTriple : gap) {
TripleMatchType newActionToTake = getTripleMatchType(existingTriple, tripleToAdd);
if (actionToTake == null) {
actionToTake = newActionToTake;
} else if (newActionToTake != actionToTake) {
// What to do if the actions to take are contradictory

}
}
// Compare actionToTake to previous actionToTakes?
//result = applyMatchAction(actionToTake, gap, tripleToAdd, tripleToRemove);
}
return result;
}

public static Set<KnowledgeGap> mergeGaps(Set<KnowledgeGap> listOfGaps, Set<KnowledgeGap> gapsToAdd) {
if (listOfGaps.isEmpty()) {
return gapsToAdd;
Expand All @@ -737,42 +783,13 @@ public static Set<KnowledgeGap> mergeGaps(Set<KnowledgeGap> listOfGaps, Set<Know
}

Set<KnowledgeGap> knowledgeGaps = new HashSet<>();
for (KnowledgeGap existingOrGap : listOfGaps) {
for (KnowledgeGap collectedOrGap : gapsToAdd) {
Set<KnowledgeGap> currentGaps = new HashSet<>();
for (TriplePattern triple1 : collectedOrGap) {
boolean added = false;
for (TriplePattern triple2 : existingOrGap) {
Map<TripleNode, TripleNode> matches = triple2.findMatches(triple1);
if (matches == null) continue;

for (Entry<TripleNode, TripleNode> match : matches.entrySet()) {
if (match.getKey().node.isVariable() && !match.getValue().node.isVariable()) {
// Triple from existing OrGap is more general and needs to be replaced
KnowledgeGap gap = new KnowledgeGap(Set.copyOf(existingOrGap));
gap.remove(triple2);
gap.add(triple1);
currentGaps.add(gap);
} else if (match.getValue().node.isVariable() && !match.getKey().node.isVariable()) {
// Triple in existingOrGap is more specific, no need to add collectedOrGap to newGap
added = true;
currentGaps.add(existingOrGap);
}
}
}
if (!added) {
if (currentGaps.isEmpty()) {
KnowledgeGap g = new KnowledgeGap();
g.add(triple1);
currentGaps.add(g);
} else {
currentGaps.forEach(g -> g.add(triple1));
}
}
}
knowledgeGaps.addAll(currentGaps);
for (KnowledgeGap existingGap : listOfGaps) {
for (KnowledgeGap gapToAdd : gapsToAdd) {
KnowledgeGap g = mergeGap(existingGap, gapToAdd);
knowledgeGaps.add(g);
}
}

return knowledgeGaps;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package eu.knowledge.engine.smartconnector.impl;

public enum TripleMatchType {
ADD_TRIPLE, IGNORE_TRIPLE, REPLACE_TRIPLE, ADD_GAP
}

0 comments on commit f4129a3

Please sign in to comment.