use of org.eclipse.winery.topologygraph.model.ToscaNode in project winery by eclipse.
the class BehaviorPatternDetectionTest method addCompatibleBehaviorPatterns.
@Test
public void addCompatibleBehaviorPatterns() {
List<TPolicy> behaviorPatterns = new ArrayList<>();
behaviorPatterns.add(new TPolicy(new TPolicy.Builder(QName.valueOf("{patternNs}oneProp")).setName("oneProp")));
TNodeTemplate detectorElement = new TNodeTemplate(new TNodeTemplate.Builder("detectorElement", QName.valueOf("{ns}patternType")).setPolicies(behaviorPatterns).setX("1").setY("1"));
TNodeTemplate refinementElement = new TNodeTemplate(new TNodeTemplate.Builder("refinementElement", QName.valueOf("{ns}concreteType")).setX("1").setY("1"));
LinkedHashMap<String, String> refinementProps = new LinkedHashMap<>();
refinementProps.put("oneProp", "true");
ModelUtilities.setPropertiesKV(refinementElement, refinementProps);
List<TPolicy> stayingPolicies = new ArrayList<>();
stayingPolicies.add(new TPolicy(new TPolicy.Builder(QName.valueOf("{ns}normalPolicy")).setName("normalPolicy")));
TNodeTemplate stayingElement = new TNodeTemplate(new TNodeTemplate.Builder("stayingElement", QName.valueOf("{ns}concreteType")).setPolicies(stayingPolicies).setX("1").setY("1"));
LinkedHashMap<String, String> stayingProps = new LinkedHashMap<>();
stayingProps.put("oneProp", "false");
ModelUtilities.setPropertiesKV(stayingElement, stayingProps);
List<OTStayMapping> stayMappings = Collections.singletonList(new OTStayMapping(new OTStayMapping.Builder().setDetectorElement(detectorElement).setRefinementElement(refinementElement)));
List<OTBehaviorPatternMapping> behaviorPatternMappings = Collections.singletonList(new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder("behaviorPatternMap0").setDetectorElement(detectorElement).setRefinementElement(refinementElement).setBehaviorPattern("oneProp").setProperty(new OTPropertyKV("oneProp", "true"))));
TTopologyTemplate detector = new TTopologyTemplate();
detector.addNodeTemplate(detectorElement);
TTopologyTemplate refinementStructure = new TTopologyTemplate();
refinementStructure.addNodeTemplate(refinementElement);
OTPatternRefinementModel prm = new OTPatternRefinementModel(new OTPatternRefinementModel.Builder().setDetector(detector).setRefinementStructure(refinementStructure).setStayMappings(stayMappings).setBehaviorPatternMappings(behaviorPatternMappings));
// needs to be swapped manually as only PRMs retrieved from repo are swapped automatically
PatternDetectionUtils.swapDetectorWithRefinement(prm);
TTopologyTemplate topology = new TTopologyTemplate();
topology.addNodeTemplate(stayingElement);
ToscaGraph topologyGraph = ToscaTransformer.createTOSCAGraph(topology);
ToscaGraph detectorGraph = ToscaTransformer.createTOSCAGraph(prm.getDetector());
IToscaMatcher matcher = new ToscaBehaviorPatternMatcher(prm, namespaceManager());
ToscaIsomorphismMatcher isomorphismMatcher = new ToscaIsomorphismMatcher();
Iterator<GraphMapping<ToscaNode, ToscaEdge>> matches = isomorphismMatcher.findMatches(detectorGraph, topologyGraph, matcher);
RefinementCandidate refinementCandidate = new RefinementCandidate(prm, matches.next(), detectorGraph, 1);
// 'normalPolicy' is not removed because not behavior pattern, 'oneProp' not present behavior pattern mapping doesn't match
BehaviorPatternDetection behaviorPatternDetection = new BehaviorPatternDetection(new DefaultRefinementChooser());
behaviorPatternDetection.applyRefinement(refinementCandidate, topology);
assertEquals(topology.getNodeTemplateOrRelationshipTemplate().size(), 1);
assertEquals(topology.getNodeTemplates().get(0).getId(), "stayingElement");
List<TPolicy> policies = topology.getNodeTemplates().get(0).getPolicies();
assertEquals(policies.size(), 1);
assertEquals(policies.get(0).getName(), "normalPolicy");
// 'normalPolicy' is not removed because not behavior pattern, 'oneProp' present behavior pattern mapping matches
stayingProps.put("oneProp", "true");
topology.getNodeTemplateOrRelationshipTemplate().clear();
topology.addNodeTemplate(stayingElement);
behaviorPatternDetection.applyRefinement(refinementCandidate, topology);
assertEquals(topology.getNodeTemplateOrRelationshipTemplate().size(), 1);
assertEquals(topology.getNodeTemplates().get(0).getId(), "stayingElement");
policies = topology.getNodeTemplates().get(0).getPolicies();
assertEquals(policies.size(), 2);
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("normalPolicy")));
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("oneProp")));
}
use of org.eclipse.winery.topologygraph.model.ToscaNode in project winery by eclipse.
the class TopologyFragmentRefinement method calculateNewPositions.
private Map<String, Map<String, Integer>> calculateNewPositions(ToscaGraph detectorGraph, GraphMapping<ToscaNode, ToscaEdge> mapping, TTopologyTemplate refinementStructure) {
HashMap<String, Map<String, Integer>> coordinates = new HashMap<>();
int[] topLeftOriginal = { -1, -1 };
int[] topLeftReplacement = { -1, -1 };
detectorGraph.vertexSet().forEach(toscaNode -> {
ToscaNode node = mapping.getVertexCorrespondence(toscaNode, false);
getTopLeft(node.getTemplate().getX(), node.getTemplate().getY(), topLeftOriginal);
});
refinementStructure.getNodeTemplates().forEach(tNodeTemplate -> getTopLeft(tNodeTemplate.getX(), tNodeTemplate.getY(), topLeftReplacement));
refinementStructure.getNodeTemplates().forEach(nodeTemplate -> {
if (nodeTemplate.getX() != null && nodeTemplate.getY() != null) {
int x = Integer.parseInt(nodeTemplate.getX());
int y = Integer.parseInt(nodeTemplate.getY());
HashMap<String, Integer> newCoordinates = new HashMap<>();
newCoordinates.put("x", (x - topLeftReplacement[0]) + topLeftOriginal[0]);
newCoordinates.put("y", (y - topLeftReplacement[1]) + topLeftOriginal[1]);
coordinates.put(nodeTemplate.getId(), newCoordinates);
}
});
return coordinates;
}
use of org.eclipse.winery.topologygraph.model.ToscaNode in project winery by eclipse.
the class TestRefinement method applyRefinement.
@Override
public Map<String, String> applyRefinement(RefinementCandidate refinement, TTopologyTemplate topology) {
// import the refinement structure
Map<String, String> idMapping = BackendUtils.mergeTopologyTemplateAinTopologyTemplateB(refinement.getRefinementModel().getRefinementTopology(), topology);
// iterate over the refinement nodes and add the configured relations
refinement.getRefinementModel().getRefinementTopology().getNodeTemplates().forEach(nodeTemplate -> refinement.getRefinementModel().getRelationMappings().stream().filter(relationMapping -> relationMapping.getRefinementElement().getId().equals(nodeTemplate.getId())).forEach(relationMapping -> {
String relId = this.versionAppendix + "-" + relationMapping.getRelationType().getLocalPart();
int counter = 0;
while (Objects.nonNull(idMapping.get(relId))) {
relId += counter++;
}
TRelationshipTemplate relationshipTemplate = new TRelationshipTemplate();
relationshipTemplate.setType(relationMapping.getRelationType());
relationshipTemplate.setId(relId);
relationshipTemplate.setName(relationMapping.getRelationType().getLocalPart());
// Retrieve the id from the correspondence node of the graph mapping.
ToscaNode node = refinement.getDetectorGraph().getNode(relationMapping.getDetectorElement().getId());
String topologyNodeId = refinement.getGraphMapping().getVertexCorrespondence(node, false).getId();
if (relationMapping.getDirection() == OTRelationDirection.INGOING) {
String targetId = idMapping.get(relationMapping.getRefinementElement().getId());
relationshipTemplate.setSourceNodeTemplate(topology.getNodeTemplate(topologyNodeId));
relationshipTemplate.setTargetNodeTemplate(topology.getNodeTemplate(targetId));
} else {
String sourceId = idMapping.get(relationMapping.getRefinementElement().getId());
relationshipTemplate.setSourceNodeTemplate(topology.getNodeTemplate(sourceId));
relationshipTemplate.setTargetNodeTemplate(topology.getNodeTemplate(topologyNodeId));
}
topology.addRelationshipTemplate(relationshipTemplate);
}));
return idMapping;
}
use of org.eclipse.winery.topologygraph.model.ToscaNode in project winery by eclipse.
the class PatternRefinementTest method getExternalRelations.
// endregion
// region ********** getExternalRelations **********
@Test
public void getExternalRelations() {
setUp();
/*
input:
####### (1) ####### (1) #######
# (1) # <===== # (2) # <===== # (3) #
####### ####### #######
| (2)
\/
#######
# (4) #
#######
expect the algorithm to find the bold (==) relations
*/
PatternRefinement patternRefinement = new PatternRefinement();
ArrayList<ToscaNode> matchingNodes = new ArrayList<>();
Iterators.addAll(matchingNodes, candidateForTopology.getDetectorGraph().vertexSet().iterator());
assertEquals(2, matchingNodes.size());
TNodeTemplate nt2 = candidateForTopology.getGraphMapping().getVertexCorrespondence(matchingNodes.get(0), false).getTemplate();
List<TRelationshipTemplate> externalRelationsOf2 = patternRefinement.getExternalRelations(nt2, candidateForTopology, topology).collect(Collectors.toList());
assertEquals(2, externalRelationsOf2.size());
TNodeTemplate nt4 = matchingNodes.get(1).getTemplate();
List<TRelationshipTemplate> externalRelationsOf4 = patternRefinement.getExternalRelations(nt4, candidateForTopology, topology).collect(Collectors.toList());
assertEquals(0, externalRelationsOf4.size());
}
use of org.eclipse.winery.topologygraph.model.ToscaNode in project winery by eclipse.
the class PatternRefinementTest method redirectStayMappings.
// endregion
// region ********** redirectStayMappings **********
@Test
void redirectStayMappings() {
setUp();
/*
input:
####### (1) #######
# (1) # <----- # (2) #
####### #######
| (2)
\/
#######
# (4) #
#######
expected output:
####### (1) ######## ########
# (1) # <----- # (10) # # (11) #
####### ######## ########
| (2) | (2)
+-------|------+
\/
#######
# (4) #
#######
*/
// region *** add stay mapping to PRM ***
TTopologyTemplate refinementTopology = candidateForTopology.getRefinementModel().getRefinementTopology();
TTopologyTemplate detector = candidateForTopology.getRefinementModel().getDetector();
refinementTopology.getNodeTemplateOrRelationshipTemplate().removeIf(template -> template.getId().equals("13") || template.getId().equals("1213"));
TNodeTemplate nt12 = refinementTopology.getNodeTemplate("12");
assertNotNull(nt12);
nt12.setType("{http://ex.org}nodeType_4");
TNodeTemplate nt4 = detector.getNodeTemplate("8");
OTStayMapping nt4staysAsNt12 = new OTStayMapping.Builder("stay1").setDetectorElement(nt4).setRefinementElement(nt12).build();
((OTPatternRefinementModel) candidateForTopology.getRefinementModel()).setStayMappings(Collections.singletonList(nt4staysAsNt12));
// endregion
// recreate the candidate
ToscaGraph topologyGraph = ToscaTransformer.createTOSCAGraph(topology2);
ToscaGraph detectorGraph = ToscaTransformer.createTOSCAGraph(detector);
GraphMapping<ToscaNode, ToscaEdge> mapping = new ToscaIsomorphismMatcher().findMatches(detectorGraph, topologyGraph, new ToscaTypeMatcher()).next();
candidateForTopology = new RefinementCandidate(candidateForTopology.getRefinementModel(), mapping, detectorGraph, 1);
PatternRefinement patternRefinement = new PatternRefinement();
patternRefinement.applyRefinement(candidateForTopology, topology2);
// region *** assertions ***
assertNotNull(topology2.getNodeTemplate("4"));
TRelationshipTemplate relation1012 = topology2.getRelationshipTemplate("1012");
assertNotNull(relation1012);
assertEquals("4", relation1012.getTargetElement().getRef().getId());
assertEquals("10", relation1012.getSourceElement().getRef().getId());
TRelationshipTemplate relation1112 = topology2.getRelationshipTemplate("1112");
assertNotNull(relation1112);
assertEquals("4", relation1112.getTargetElement().getRef().getId());
assertEquals("11", relation1112.getSourceElement().getRef().getId());
TRelationshipTemplate relation21 = topology2.getRelationshipTemplate("21");
assertNotNull(relation21);
assertEquals("1", relation21.getTargetElement().getRef().getId());
assertEquals("10", relation21.getSourceElement().getRef().getId());
// endregion
}
Aggregations