Skip to content

Commit

Permalink
[graph-operations] - IdentifyingGraphSynchronizer - itShouldNotFixEdg…
Browse files Browse the repository at this point in the history
…eWhenTryingToSynchronizeNodeWithSameAttributeButDifferentType (#80)
  • Loading branch information
clickout authored Jul 18, 2023
1 parent 0498073 commit 23e0c39
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ public InMemoryGraphRepository mergeDuplicateNodesByIdentificators(
return resultingGraph.getGraph().traversable();
}

private LinkedMap<UniqueIdentifier, UniqueIdentifier> synchronizeNodes(
private LinkedMap<String, LinkedMap<UniqueIdentifier, UniqueIdentifier>> synchronizeNodes(
InMemoryGraphRepository newGraph
) {
var nodeIdChangesMap = new LinkedMap<UniqueIdentifier, UniqueIdentifier>();
var nodeIdChangesMap = new LinkedMap<String, LinkedMap<UniqueIdentifier, UniqueIdentifier>>();
for (var node : newGraph.getGraph().getAllNodes()) {
this.synchronizeNode(
newGraph,
Expand All @@ -162,7 +162,7 @@ private LinkedMap<UniqueIdentifier, UniqueIdentifier> synchronizeNodes(
private void synchronizeNode(
InMemoryGraphRepository newGraph,
Node inMemoryNode,
LinkedMap<UniqueIdentifier, UniqueIdentifier> nodeIdChangesMap
LinkedMap<String, LinkedMap<UniqueIdentifier, UniqueIdentifier>> nodeIdChangesMap
) {
var identifyingQuery = this.nodeIdentifyingFiltersResolver.resolve(
inMemoryNode,
Expand Down Expand Up @@ -194,25 +194,32 @@ private void synchronizeNode(
foundNode.getVersionedAttributes()
)
);
nodeIdChangesMap.put(
var nodeTypeMap = nodeIdChangesMap.computeIfAbsent(inMemoryNode.getType(), (key) -> new LinkedMap<>());
nodeTypeMap.put(
inMemoryNode.getId(),
foundNode.getId()
);
}

private void synchronizeEdges(
InMemoryGraphRepository newGraph,
LinkedMap<UniqueIdentifier, UniqueIdentifier> nodeIdChangeMap
LinkedMap<String, LinkedMap<UniqueIdentifier, UniqueIdentifier>> nodeIdChangeMap
) {
newGraph.getGraph().getAllEdges().forEach(
potentiallyRottenEdge ->
{
var fixedNodeFromId = nodeIdChangeMap.getOrDefault(
potentiallyRottenEdge.getNodeFromType(),
new LinkedMap<>()
).getOrDefault(
potentiallyRottenEdge.getNodeFromId(),
potentiallyRottenEdge.getNodeFromId()
);
var fixedNodeFrom = new Node(fixedNodeFromId, potentiallyRottenEdge.getNodeFromType());
var fixedNodeToId = nodeIdChangeMap.getOrDefault(
potentiallyRottenEdge.getNodeToType(),
new LinkedMap<>()
).getOrDefault(
potentiallyRottenEdge.getNodeToId(),
potentiallyRottenEdge.getNodeToId()
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_type: DifferentType
node_hash: D2A5E
node_edges:
attributes:
string_type -> test value
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import ai.stapi.graph.graphelements.Node;
import ai.stapi.graph.repositorypruner.RepositoryPruner;
import ai.stapi.graphoperations.fixtures.testsystem.TestSystemModelDefinitionsLoader;
import ai.stapi.identity.UniversallyUniqueIdentifier;
import ai.stapi.graphoperations.synchronization.nodeIdentificator.NodeIdentificator;
import ai.stapi.graphoperations.synchronization.nodeIdentificator.testImplementation.TestNodeIdentificatorsProvider;
import ai.stapi.identity.UniversallyUniqueIdentifier;
import ai.stapi.test.schemaintegration.SchemaIntegrationTestCase;
import ai.stapi.test.schemaintegration.StructureDefinitionScope;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -91,6 +91,96 @@ void itShouldSynchronizeNodeById() throws GraphException {
this.thenNodeApproved(actualNode);
}

@Test
void itShouldNotSynchronizeNodeByIdAndDifferentType() throws GraphException {
var existingNode = new Node("OriginalType");
this.nodeRepository.save(existingNode);

var expectedNode = new Node(
existingNode.getId(),
"DifferentType"
);
expectedNode = expectedNode.add(
new LeafAttribute<>(
"string_type",
new StringAttributeValue("test value")
)
);

this.identifyingGraphSynchronizer.synchronize(new Graph(expectedNode));

var actualNode = this.nodeRepository.loadNode(
expectedNode.getId(),
expectedNode.getType()
);

Assertions.assertTrue(this.nodeRepository.nodeExists(existingNode.getId(), existingNode.getType()));
this.thenNodeApproved(actualNode);
}


@Test
void itShouldNotFixEdgeWhenTryingToSynchronizeNodeWithSameAttributeButDifferentType() throws GraphException {
var existingNode = new Node("SameType");
var otherNode = new Node("OtherType");
otherNode = otherNode.add(
new LeafAttribute<>(
"identifying_attribute",
new StringAttributeValue("matching_value")
)
).add(
new LeafAttribute<>(
"original_attribute",
new StringAttributeValue("original_value")
)
);
var anotherNode = new Node("AnotherType");
var existingEdge = new Edge(existingNode, "originalEdge", otherNode);
this.nodeRepository.save(existingNode);
this.nodeRepository.save(otherNode);
this.nodeRepository.save(anotherNode);
this.edgeRepository.save(existingEdge);

var mergingNode1 = new Node(
existingNode.getId(),
"SameType"
);
mergingNode1 = mergingNode1.add(
new LeafAttribute<>(
"string_type",
new StringAttributeValue("test value")
)
);

this.testNodeIdentificatorsProvider.add(
otherNode.getType(),
new NodeIdentificator("identifying_attribute")
);

var mergingNode2 = new Node(anotherNode.getId(), "OtherType");
mergingNode2 = mergingNode2.add(
new LeafAttribute<>(
"identifying_attribute",
new StringAttributeValue("matching_value")
)
).add(
new LeafAttribute<>(
"new_attribute",
new StringAttributeValue("irrelevant_value")
)
);
var newEdge = new Edge(mergingNode1, "originalEdge", mergingNode2);
var someEdge = new Edge(mergingNode2, "originalEdge", anotherNode);
var graph = new Graph(mergingNode1, mergingNode2, anotherNode, newEdge, someEdge);

this.identifyingGraphSynchronizer.synchronize(graph);
Assertions.assertTrue(this.nodeRepository.nodeExists(existingNode.getId(), existingNode.getType()));
Assertions.assertTrue(this.nodeRepository.nodeExists(otherNode.getId(), otherNode.getType()));
Assertions.assertTrue(this.nodeRepository.nodeExists(anotherNode.getId(), anotherNode.getType()));
Assertions.assertTrue(this.edgeRepository.edgeExists(existingEdge.getId(), existingEdge.getType()));
Assertions.assertFalse(this.nodeRepository.nodeExists(mergingNode2.getId(), mergingNode2.getType()));
}

@Test
void itShouldSynchronizeNodeByIdAndMergeAttributes() throws GraphException {
var existingNode = new Node("Test_node_type");
Expand Down Expand Up @@ -454,7 +544,6 @@ void itShouldMergeDuplicateNodesByIdentificators_AndItWillFixEdges() {
var actualGraph = whenMergeDuplicates(graph);

this.thenGraphApproved(actualGraph);

}

@Test
Expand Down Expand Up @@ -507,7 +596,6 @@ void itShouldMergeDuplicateNodesByIdentificators_AndItWillMergeEdges() {
var actualGraph = whenMergeDuplicates(graph);

this.thenGraphApproved(actualGraph);

}

@NotNull
Expand Down

0 comments on commit 23e0c39

Please sign in to comment.