use of org.eclipse.winery.model.adaptation.substitution.refinement.DefaultRefinementChooser in project winery by eclipse.
the class ComponentPatternDetectionTest method refineTopologyWithBehaviorPatterns.
@Test
public void refineTopologyWithBehaviorPatterns() {
TPolicy behaviorPattern1 = new TPolicy(new TPolicy.Builder(QName.valueOf("behaviorPattern1")).setName("behaviorPattern1"));
TPolicy behaviorPattern2 = new TPolicy(new TPolicy.Builder(QName.valueOf("behaviorPattern2")).setName("behaviorPattern2"));
List<TPolicy> topologyPolicies = new ArrayList<>();
topologyPolicies.add(behaviorPattern1);
((HasPolicies) topology.getNodeTemplateOrRelationshipTemplate("database")).setPolicies(topologyPolicies);
List<TPolicy> detectorPolicies = new ArrayList<>();
detectorPolicies.add(behaviorPattern1);
((HasPolicies) prm2.getDetector().getNodeTemplateOrRelationshipTemplate("database-rs2")).setPolicies(detectorPolicies);
List<TPolicy> refinementPolicies = new ArrayList<>();
refinementPolicies.add(behaviorPattern1);
refinementPolicies.add(behaviorPattern2);
((HasPolicies) prm2.getRefinementStructure().getNodeTemplateOrRelationshipTemplate("relationalDb-d2")).setPolicies(refinementPolicies);
ComponentPatternDetection detection = new ComponentPatternDetection(new DefaultRefinementChooser(), Arrays.asList(prm2, prm1));
detection.refineTopology(topology);
// behaviorPattern2 must have been removed as it cannot be guaranteed to be implemented
HasPolicies db = ((HasPolicies) topology.getNodeTemplateOrRelationshipTemplate("relationalDb-d2"));
assertEquals(db.getPolicies().size(), 1);
assertTrue(db.getPolicies().contains(behaviorPattern1));
}
use of org.eclipse.winery.model.adaptation.substitution.refinement.DefaultRefinementChooser in project winery by eclipse.
the class ComponentPatternDetectionTest method testRemoveBehaviorPatterns.
@Test
public void testRemoveBehaviorPatterns() {
// region: detector
TNodeTemplate detectorElement = new TNodeTemplate(new TNodeTemplate.Builder("detectorElement", QName.valueOf("{ns}type1")).setX("1").setY("1"));
TNodeTemplate detectorElement2 = new TNodeTemplate(new TNodeTemplate.Builder("detectorElement2", QName.valueOf("{ns}pattern1")).setX("1").setY("1"));
List<TPolicy> detectorPolicies = new ArrayList<>();
detectorPolicies.add(new TPolicy(new TPolicy.Builder(QName.valueOf("{patternNs}one")).setName("one")));
detectorPolicies.add(new TPolicy(new TPolicy.Builder(QName.valueOf("{patternNs}two")).setName("two")));
detectorElement.setPolicies(detectorPolicies);
TTopologyTemplate detector = new TTopologyTemplate(new TTopologyTemplate.Builder().addNodeTemplates(Arrays.asList(detectorElement, detectorElement2)));
// endregion
// region: refinement
TNodeTemplate refinementElement = new TNodeTemplate(new TNodeTemplate.Builder("refinementElement", QName.valueOf("{ns}type1")).setX("1").setY("1"));
TNodeTemplate refinementElement2 = new TNodeTemplate(new TNodeTemplate.Builder("refinementElement2", QName.valueOf("{ns}type2")).setX("1").setY("1"));
List<TPolicy> refinementPolicies = new ArrayList<>();
refinementPolicies.add(new TPolicy(new TPolicy.Builder(QName.valueOf("{patternNs}one")).setName("one")));
refinementElement.setPolicies(refinementPolicies);
TTopologyTemplate refinement = new TTopologyTemplate(new TTopologyTemplate.Builder().addNodeTemplates(Arrays.asList(refinementElement, refinementElement2)));
// endregion
OTStayMapping stayMapping = new OTStayMapping(new OTStayMapping.Builder().setDetectorElement(detectorElement).setRefinementElement(refinementElement));
OTPatternRefinementModel prm = new OTPatternRefinementModel(new OTPatternRefinementModel.Builder().setDetector(detector).setRefinementStructure(refinement).setStayMappings(Arrays.asList(stayMapping)));
PatternDetectionUtils.swapDetectorWithRefinement(prm);
TNodeTemplate nodeTemplate = new TNodeTemplate(new TNodeTemplate.Builder("nodeTemplate", QName.valueOf("{ns}type1")).setX("1").setY("1"));
TNodeTemplate nodeTemplate2 = new TNodeTemplate(new TNodeTemplate.Builder("nodeTemplate2", QName.valueOf("{ns}type2")).setX("1").setY("1"));
nodeTemplate.setPolicies(new ArrayList<>(refinementPolicies));
TTopologyTemplate topology = new TTopologyTemplate(new TTopologyTemplate.Builder().addNodeTemplates(Arrays.asList(nodeTemplate, nodeTemplate2)));
ComponentPatternDetection detection = new ComponentPatternDetection(new DefaultRefinementChooser(), Arrays.asList(prm));
detection.refineTopology(topology);
assertEquals(topology.getNodeTemplates().size(), 2);
Set<QName> types = topology.getNodeTemplates().stream().map(TNodeTemplate::getType).collect(Collectors.toSet());
assertTrue(types.contains(QName.valueOf("{ns}type1")));
assertTrue(types.contains(QName.valueOf("{ns}pattern1")));
TNodeTemplate nodeTemplate1 = topology.getNodeTemplate("nodeTemplate");
assertNotNull(nodeTemplate1);
assertNotNull(nodeTemplate1.getPolicies());
List<TPolicy> policies = nodeTemplate1.getPolicies();
assertEquals(policies.size(), 1);
assertTrue(policies.stream().anyMatch(policy -> policy.getPolicyType().equals(QName.valueOf("{patternNs}one"))));
}
use of org.eclipse.winery.model.adaptation.substitution.refinement.DefaultRefinementChooser in project winery by eclipse.
the class ComponentPatternDetectionTest method refineTopology.
@Test
public void refineTopology() {
ComponentPatternDetection detection = new ComponentPatternDetection(new DefaultRefinementChooser(), Arrays.asList(prm2, prm1));
detection.refineTopology(topology);
// check inserted elements
assertNotNull(topology.getNodeTemplate("relationalDb-d2"));
assertNotNull(topology.getRelationshipTemplate("relationalDb-privateCloud-d2"));
assertNotNull(topology.getNodeTemplate("privateCloud-d2"));
assertNotNull(topology.getRelationshipTemplate("processor-db"));
assertNotNull(topology.getNodeTemplate("processor"));
assertNotNull(topology.getRelationshipTemplate("processor-webserver"));
assertNotNull(topology.getNodeTemplate("webserver"));
assertNotNull(topology.getRelationshipTemplate("webserver-vm"));
// check removed elements
assertNull(topology.getNodeTemplate("database"));
assertNull(topology.getRelationshipTemplate("db-dbms"));
assertNull(topology.getNodeTemplate("dbms"));
assertNull(topology.getRelationshipTemplate("dbms-vm"));
assertNull(topology.getNodeTemplate("vm"));
assertNull(topology.getRelationshipTemplate("vm-openstack"));
assertNull(topology.getNodeTemplate("openstack"));
// redirected relations
TRelationshipTemplate processorToDb = topology.getRelationshipTemplate("processor-db");
assertNotNull(processorToDb);
assertEquals(processorToDb.getSourceElement().getRef().getId(), "processor");
assertEquals(processorToDb.getTargetElement().getRef().getId(), "relationalDb-d2");
TRelationshipTemplate webserverToVm = topology.getRelationshipTemplate("webserver-vm");
assertNotNull(webserverToVm);
assertEquals(webserverToVm.getSourceElement().getRef().getId(), "webserver");
assertEquals(webserverToVm.getTargetElement().getRef().getId(), "privateCloud-d2");
// attributes should not have been mapped
TNodeTemplate relationalDb = topology.getNodeTemplate("relationalDb-d2");
assertNotNull(relationalDb);
LinkedHashMap<String, String> relationDbProps = ModelUtilities.getPropertiesKV(relationalDb);
assertNotNull(relationDbProps);
assertTrue(relationDbProps.isEmpty());
TNodeTemplate privateCloud = topology.getNodeTemplate("privateCloud-d2");
assertNotNull(privateCloud);
LinkedHashMap<String, String> privateCloudProps = ModelUtilities.getPropertiesKV(privateCloud);
assertNotNull(privateCloudProps);
assertTrue(privateCloudProps.isEmpty());
}
use of org.eclipse.winery.model.adaptation.substitution.refinement.DefaultRefinementChooser 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.model.adaptation.substitution.refinement.DefaultRefinementChooser 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