use of org.eclipse.winery.topologygraph.matching.patterndetection.ToscaBehaviorPatternMatcher 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.matching.patterndetection.ToscaBehaviorPatternMatcher 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")));
}
Aggregations