use of org.eclipse.winery.topologygraph.model.ToscaGraph 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
}
use of org.eclipse.winery.topologygraph.model.ToscaGraph in project winery by eclipse.
the class ToscaTransformer method createTOSCAGraph.
public static ToscaGraph createTOSCAGraph(TTopologyTemplate topologyTemplate) {
ToscaGraph graph = new ToscaGraph();
List<TRelationshipTemplate> relationshipTemplates = topologyTemplate.getRelationshipTemplates();
List<TNodeTemplate> nodeTemplates = topologyTemplate.getNodeTemplates();
Map<String, ToscaNode> nodes = new HashMap<>();
for (TNodeTemplate nodeTemplate : nodeTemplates) {
ToscaNode node = createAndInitializeTOSCANode(nodeTemplate);
nodes.put(nodeTemplate.getId(), node);
graph.addVertex(node);
}
for (TRelationshipTemplate tRelationshipTemplate : relationshipTemplates) {
ToscaNode source = nodes.get(tRelationshipTemplate.getSourceElement().getRef().getId());
ToscaNode target = nodes.get(tRelationshipTemplate.getTargetElement().getRef().getId());
ToscaEdge edge = new ToscaEdge(source, target);
graph.addEdge(source, target, edge);
initializeTOSCAEdge(tRelationshipTemplate, edge);
}
return graph;
}
use of org.eclipse.winery.topologygraph.model.ToscaGraph in project winery by eclipse.
the class ToscaGraphIsomorphismTest method testTOSCADefaultMatcher.
@Test
public void testTOSCADefaultMatcher() {
ToscaGraph queryGraph = new ToscaGraph();
ToscaGraph searchInGraph = new ToscaGraph();
ToscaNode node1 = createTOSCANode("node_01", "A");
ToscaNode node2 = createTOSCANode("node_02", "B");
queryGraph.addVertex(node1);
queryGraph.addVertex(node2);
ToscaEdge edge1 = addEdge(queryGraph, node1, node2, "edge_1", "a");
ToscaNode node3 = createTOSCANode("node_03", "A");
ToscaNode node4 = createTOSCANode("node_04", "A");
ToscaNode node5 = createTOSCANode("node_05", "B");
ToscaNode node6 = createTOSCANode("node_06", "B");
searchInGraph.addVertex(node3);
searchInGraph.addVertex(node4);
searchInGraph.addVertex(node5);
searchInGraph.addVertex(node6);
addEdge(searchInGraph, node3, node4, "edge_2", "b");
addEdge(searchInGraph, node3, node5, "edge_3", "a");
addEdge(searchInGraph, node4, node6, "edge_4", "a");
ToscaIsomorphismMatcher matcher = new ToscaIsomorphismMatcher();
Iterator<GraphMapping<ToscaNode, ToscaEdge>> iterator = matcher.findMatches(queryGraph, searchInGraph, null);
int isomorphismCount = 0;
while (iterator.hasNext()) {
isomorphismCount++;
GraphMapping<ToscaNode, ToscaEdge> mapping = iterator.next();
mapping.getEdgeCorrespondence(edge1, false);
mapping.getVertexCorrespondence(node1, false);
mapping.getVertexCorrespondence(node2, false);
}
assertEquals(2, isomorphismCount);
}
use of org.eclipse.winery.topologygraph.model.ToscaGraph in project winery by eclipse.
the class BehaviorPatternDetectionTest method removeIncompatibleBehaviorPatterns.
@Test
public void removeIncompatibleBehaviorPatterns() {
List<TPolicy> behaviorPatterns = new ArrayList<>();
behaviorPatterns.add(new TPolicy(new TPolicy.Builder(QName.valueOf("noProp")).setName("noProp")));
behaviorPatterns.add(new TPolicy(new TPolicy.Builder(QName.valueOf("oneProp")).setName("oneProp")));
behaviorPatterns.add(new TPolicy(new TPolicy.Builder(QName.valueOf("multiProps")).setName("multiProps")));
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");
refinementProps.put("multiPropsProp1", "true");
refinementProps.put("multiPropsProp2", "true");
refinementProps.put("multiPropsProp3", "*");
refinementProps.put("multiPropsProp4", "");
refinementProps.put("multiPropsProp5", null);
ModelUtilities.setPropertiesKV(refinementElement, refinementProps);
TNodeTemplate candidateElement = new TNodeTemplate(new TNodeTemplate.Builder("candidateElement", QName.valueOf("{ns}concreteType")).setX("1").setY("1"));
LinkedHashMap<String, String> candidateProps = new LinkedHashMap<>();
candidateProps.put("oneProp", "false");
candidateProps.put("multiPropsProp1", "false");
candidateProps.put("multiPropsProp2", "false");
candidateProps.put("multiPropsProp3", "");
candidateProps.put("multiPropsProp4", null);
candidateProps.put("multiPropsProp5", "");
ModelUtilities.setPropertiesKV(candidateElement, candidateProps);
List<OTBehaviorPatternMapping> behaviorPatternMappings = new ArrayList<>();
behaviorPatternMappings.add(new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder("behaviorPatternMap0").setDetectorElement(detectorElement).setRefinementElement(refinementElement).setBehaviorPattern("oneProp").setProperty(new OTPropertyKV("oneProp", "true"))));
behaviorPatternMappings.add(new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder("behaviorPatternMap1").setDetectorElement(detectorElement).setRefinementElement(refinementElement).setBehaviorPattern("multiProps").setProperty(new OTPropertyKV("multiPropsProp1", "true"))));
behaviorPatternMappings.add(new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder("behaviorPatternMap2").setDetectorElement(detectorElement).setRefinementElement(refinementElement).setBehaviorPattern("multiProps").setProperty(new OTPropertyKV("multiPropsProp2", "true"))));
behaviorPatternMappings.add(new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder("behaviorPatternMap3").setDetectorElement(detectorElement).setRefinementElement(refinementElement).setBehaviorPattern("multiProps").setProperty(new OTPropertyKV("multiPropsProp3", "*"))));
behaviorPatternMappings.add(new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder("behaviorPatternMap4").setDetectorElement(detectorElement).setRefinementElement(refinementElement).setBehaviorPattern("multiProps").setProperty(new OTPropertyKV("multiPropsProp4", ""))));
behaviorPatternMappings.add(new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder("behaviorPatternMap5").setDetectorElement(detectorElement).setRefinementElement(refinementElement).setBehaviorPattern("multiProps").setProperty(new OTPropertyKV("multiPropsProp5", null))));
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).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(candidateElement);
ToscaGraph topologyGraph = ToscaTransformer.createTOSCAGraph(topology);
ToscaGraph detectorGraph = ToscaTransformer.createTOSCAGraph(prm.getDetector());
IToscaMatcher matcher = new ToscaBehaviorPatternMatcher(prm, null);
ToscaIsomorphismMatcher isomorphismMatcher = new ToscaIsomorphismMatcher();
Iterator<GraphMapping<ToscaNode, ToscaEdge>> matches = isomorphismMatcher.findMatches(detectorGraph, topologyGraph, matcher);
RefinementCandidate refinementCandidate = new RefinementCandidate(prm, matches.next(), detectorGraph, 1);
// 'noProp' is applicable independent of properties as no behavior pattern mappings are specified
BehaviorPatternDetection behaviorPatternDetection = new BehaviorPatternDetection(new DefaultRefinementChooser());
behaviorPatternDetection.applyRefinement(refinementCandidate, topology);
assertEquals(topology.getNodeTemplateOrRelationshipTemplate().size(), 1);
List<TPolicy> policies = topology.getNodeTemplates().get(0).getPolicies();
assertEquals(policies.size(), 1);
assertEquals(policies.get(0).getName(), "noProp");
// 'noProp' still applicable, 'oneProp' now applicable as well because property is set
candidateProps.put("oneProp", "true");
topology.getNodeTemplateOrRelationshipTemplate().clear();
topology.addNodeTemplate(candidateElement);
behaviorPatternDetection.applyRefinement(refinementCandidate, topology);
assertEquals(topology.getNodeTemplateOrRelationshipTemplate().size(), 1);
policies = topology.getNodeTemplates().get(0).getPolicies();
assertEquals(policies.size(), 2);
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("noProp")));
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("oneProp")));
// 'noProp' & 'oneProp' still applicable, 'multiProps' not applicable because not all properties are set
candidateProps.put("multiPropsProp1", "true");
topology.getNodeTemplateOrRelationshipTemplate().clear();
topology.addNodeTemplate(candidateElement);
behaviorPatternDetection.applyRefinement(refinementCandidate, topology);
assertEquals(topology.getNodeTemplateOrRelationshipTemplate().size(), 1);
policies = topology.getNodeTemplates().get(0).getPolicies();
assertEquals(policies.size(), 2);
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("noProp")));
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("oneProp")));
// 'noProp' & 'oneProp' still applicable, 'multiProps' not applicable because not all properties are set
candidateProps.put("multiPropsProp2", "true");
topology.getNodeTemplateOrRelationshipTemplate().clear();
topology.addNodeTemplate(candidateElement);
behaviorPatternDetection.applyRefinement(refinementCandidate, topology);
assertEquals(topology.getNodeTemplateOrRelationshipTemplate().size(), 1);
policies = topology.getNodeTemplates().get(0).getPolicies();
assertEquals(policies.size(), 2);
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("noProp")));
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("oneProp")));
// 'noProp' & 'oneProp' & 'multiProps' applicable
candidateProps.put("multiPropsProp3", "non-null and non-empty");
topology.getNodeTemplateOrRelationshipTemplate().clear();
topology.addNodeTemplate(candidateElement);
behaviorPatternDetection.applyRefinement(refinementCandidate, topology);
assertEquals(topology.getNodeTemplateOrRelationshipTemplate().size(), 1);
policies = topology.getNodeTemplates().get(0).getPolicies();
assertEquals(policies.size(), 3);
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("noProp")));
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("oneProp")));
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("multiProps")));
}
use of org.eclipse.winery.topologygraph.model.ToscaGraph in project winery by eclipse.
the class AbstractRefinement method refineTopology.
public void refineTopology(TTopologyTemplate topology) {
ToscaIsomorphismMatcher isomorphismMatcher = new ToscaIsomorphismMatcher();
int[] id = new int[1];
while (getLoopCondition(topology)) {
ToscaGraph topologyGraph = ToscaTransformer.createTOSCAGraph(topology);
List<RefinementCandidate> candidates = new ArrayList<>();
this.refinementModels.forEach(prm -> {
ToscaGraph detectorGraph = ToscaTransformer.createTOSCAGraph(prm.getDetector());
IToscaMatcher matcher = this.getMatcher(prm);
Iterator<GraphMapping<ToscaNode, ToscaEdge>> matches = isomorphismMatcher.findMatches(detectorGraph, topologyGraph, matcher);
matches.forEachRemaining(mapping -> {
RefinementCandidate candidate = new RefinementCandidate(prm, mapping, detectorGraph, id[0]++);
if (isApplicable(candidate, topology)) {
candidates.add(candidate);
}
});
});
if (candidates.size() == 0) {
break;
}
RefinementCandidate refinement = this.refinementChooser.chooseRefinement(candidates, this.refinementServiceTemplateId, topology);
if (Objects.isNull(refinement)) {
break;
}
applyRefinement(refinement, topology);
}
}
Aggregations