Search in sources :

Example 1 with DefaultRefinementChooser

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));
}
Also used : DefaultRefinementChooser(org.eclipse.winery.model.adaptation.substitution.refinement.DefaultRefinementChooser) TPolicy(org.eclipse.winery.model.tosca.TPolicy) ArrayList(java.util.ArrayList) HasPolicies(org.eclipse.winery.model.tosca.HasPolicies) Test(org.junit.jupiter.api.Test)

Example 2 with DefaultRefinementChooser

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"))));
}
Also used : TPolicy(org.eclipse.winery.model.tosca.TPolicy) BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) OTRelationMapping(org.eclipse.winery.model.tosca.extensions.OTRelationMapping) HasPolicies(org.eclipse.winery.model.tosca.HasPolicies) OTAttributeMapping(org.eclipse.winery.model.tosca.extensions.OTAttributeMapping) PatternDetectionUtils(org.eclipse.winery.topologygraph.matching.patterndetection.PatternDetectionUtils) GraphMapping(org.jgrapht.GraphMapping) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) HashMap(java.util.HashMap) ToscaEdge(org.eclipse.winery.topologygraph.model.ToscaEdge) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel) ToscaComponentPatternMatcher(org.eclipse.winery.topologygraph.matching.patterndetection.ToscaComponentPatternMatcher) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ToscaIsomorphismMatcher(org.eclipse.winery.topologygraph.matching.ToscaIsomorphismMatcher) OTPermutationMapping(org.eclipse.winery.model.tosca.extensions.OTPermutationMapping) OTRefinementModel(org.eclipse.winery.model.tosca.extensions.OTRefinementModel) OTRelationDirection(org.eclipse.winery.model.tosca.extensions.OTRelationDirection) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) OTAttributeMappingType(org.eclipse.winery.model.tosca.extensions.OTAttributeMappingType) OTStayMapping(org.eclipse.winery.model.tosca.extensions.OTStayMapping) Iterator(java.util.Iterator) DefaultRefinementChooser(org.eclipse.winery.model.adaptation.substitution.refinement.DefaultRefinementChooser) Set(java.util.Set) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) ToscaNode(org.eclipse.winery.topologygraph.model.ToscaNode) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) ModelUtilities(org.eclipse.winery.model.tosca.utils.ModelUtilities) ToscaGraph(org.eclipse.winery.topologygraph.model.ToscaGraph) QName(javax.xml.namespace.QName) Collections(java.util.Collections) ToscaTransformer(org.eclipse.winery.topologygraph.transformation.ToscaTransformer) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) DefaultRefinementChooser(org.eclipse.winery.model.adaptation.substitution.refinement.DefaultRefinementChooser) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) OTStayMapping(org.eclipse.winery.model.tosca.extensions.OTStayMapping) TPolicy(org.eclipse.winery.model.tosca.TPolicy) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel) Test(org.junit.jupiter.api.Test)

Example 3 with DefaultRefinementChooser

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());
}
Also used : DefaultRefinementChooser(org.eclipse.winery.model.adaptation.substitution.refinement.DefaultRefinementChooser) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) Test(org.junit.jupiter.api.Test)

Example 4 with DefaultRefinementChooser

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")));
}
Also used : TPolicy(org.eclipse.winery.model.tosca.TPolicy) PatternDetectionUtils(org.eclipse.winery.topologygraph.matching.patterndetection.PatternDetectionUtils) GraphMapping(org.jgrapht.GraphMapping) IToscaMatcher(org.eclipse.winery.topologygraph.matching.IToscaMatcher) ToscaEdge(org.eclipse.winery.topologygraph.model.ToscaEdge) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) NamespaceProperties(org.eclipse.winery.repository.backend.filebased.NamespaceProperties) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel) OTBehaviorPatternMapping(org.eclipse.winery.model.tosca.extensions.OTBehaviorPatternMapping) ToscaIsomorphismMatcher(org.eclipse.winery.topologygraph.matching.ToscaIsomorphismMatcher) Nullable(org.eclipse.jdt.annotation.Nullable) Map(java.util.Map) ToscaBehaviorPatternMatcher(org.eclipse.winery.topologygraph.matching.patterndetection.ToscaBehaviorPatternMatcher) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) OTStayMapping(org.eclipse.winery.model.tosca.extensions.OTStayMapping) Iterator(java.util.Iterator) DefaultRefinementChooser(org.eclipse.winery.model.adaptation.substitution.refinement.DefaultRefinementChooser) Collection(java.util.Collection) Test(org.junit.jupiter.api.Test) List(java.util.List) ToscaNode(org.eclipse.winery.topologygraph.model.ToscaNode) NamespaceManager(org.eclipse.winery.repository.backend.NamespaceManager) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ModelUtilities(org.eclipse.winery.model.tosca.utils.ModelUtilities) ToscaGraph(org.eclipse.winery.topologygraph.model.ToscaGraph) QName(javax.xml.namespace.QName) OTPropertyKV(org.eclipse.winery.model.tosca.extensions.kvproperties.OTPropertyKV) Collections(java.util.Collections) RefinementCandidate(org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate) ToscaTransformer(org.eclipse.winery.topologygraph.transformation.ToscaTransformer) NonNull(org.eclipse.jdt.annotation.NonNull) ToscaGraph(org.eclipse.winery.topologygraph.model.ToscaGraph) RefinementCandidate(org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) ToscaIsomorphismMatcher(org.eclipse.winery.topologygraph.matching.ToscaIsomorphismMatcher) ToscaBehaviorPatternMatcher(org.eclipse.winery.topologygraph.matching.patterndetection.ToscaBehaviorPatternMatcher) IToscaMatcher(org.eclipse.winery.topologygraph.matching.IToscaMatcher) DefaultRefinementChooser(org.eclipse.winery.model.adaptation.substitution.refinement.DefaultRefinementChooser) OTStayMapping(org.eclipse.winery.model.tosca.extensions.OTStayMapping) TPolicy(org.eclipse.winery.model.tosca.TPolicy) OTPropertyKV(org.eclipse.winery.model.tosca.extensions.kvproperties.OTPropertyKV) OTBehaviorPatternMapping(org.eclipse.winery.model.tosca.extensions.OTBehaviorPatternMapping) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel) GraphMapping(org.jgrapht.GraphMapping) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultRefinementChooser

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")));
}
Also used : TPolicy(org.eclipse.winery.model.tosca.TPolicy) PatternDetectionUtils(org.eclipse.winery.topologygraph.matching.patterndetection.PatternDetectionUtils) GraphMapping(org.jgrapht.GraphMapping) IToscaMatcher(org.eclipse.winery.topologygraph.matching.IToscaMatcher) ToscaEdge(org.eclipse.winery.topologygraph.model.ToscaEdge) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) NamespaceProperties(org.eclipse.winery.repository.backend.filebased.NamespaceProperties) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel) OTBehaviorPatternMapping(org.eclipse.winery.model.tosca.extensions.OTBehaviorPatternMapping) ToscaIsomorphismMatcher(org.eclipse.winery.topologygraph.matching.ToscaIsomorphismMatcher) Nullable(org.eclipse.jdt.annotation.Nullable) Map(java.util.Map) ToscaBehaviorPatternMatcher(org.eclipse.winery.topologygraph.matching.patterndetection.ToscaBehaviorPatternMatcher) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) OTStayMapping(org.eclipse.winery.model.tosca.extensions.OTStayMapping) Iterator(java.util.Iterator) DefaultRefinementChooser(org.eclipse.winery.model.adaptation.substitution.refinement.DefaultRefinementChooser) Collection(java.util.Collection) Test(org.junit.jupiter.api.Test) List(java.util.List) ToscaNode(org.eclipse.winery.topologygraph.model.ToscaNode) NamespaceManager(org.eclipse.winery.repository.backend.NamespaceManager) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ModelUtilities(org.eclipse.winery.model.tosca.utils.ModelUtilities) ToscaGraph(org.eclipse.winery.topologygraph.model.ToscaGraph) QName(javax.xml.namespace.QName) OTPropertyKV(org.eclipse.winery.model.tosca.extensions.kvproperties.OTPropertyKV) Collections(java.util.Collections) RefinementCandidate(org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate) ToscaTransformer(org.eclipse.winery.topologygraph.transformation.ToscaTransformer) NonNull(org.eclipse.jdt.annotation.NonNull) ToscaGraph(org.eclipse.winery.topologygraph.model.ToscaGraph) RefinementCandidate(org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) ToscaIsomorphismMatcher(org.eclipse.winery.topologygraph.matching.ToscaIsomorphismMatcher) ToscaBehaviorPatternMatcher(org.eclipse.winery.topologygraph.matching.patterndetection.ToscaBehaviorPatternMatcher) IToscaMatcher(org.eclipse.winery.topologygraph.matching.IToscaMatcher) DefaultRefinementChooser(org.eclipse.winery.model.adaptation.substitution.refinement.DefaultRefinementChooser) TPolicy(org.eclipse.winery.model.tosca.TPolicy) OTPropertyKV(org.eclipse.winery.model.tosca.extensions.kvproperties.OTPropertyKV) OTBehaviorPatternMapping(org.eclipse.winery.model.tosca.extensions.OTBehaviorPatternMapping) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel) GraphMapping(org.jgrapht.GraphMapping) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultRefinementChooser (org.eclipse.winery.model.adaptation.substitution.refinement.DefaultRefinementChooser)7 Test (org.junit.jupiter.api.Test)6 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 QName (javax.xml.namespace.QName)4 TPolicy (org.eclipse.winery.model.tosca.TPolicy)4 TTopologyTemplate (org.eclipse.winery.model.tosca.TTopologyTemplate)4 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)4 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)4 Collections (java.util.Collections)3 Iterator (java.util.Iterator)3 LinkedHashMap (java.util.LinkedHashMap)3 TRelationshipTemplate (org.eclipse.winery.model.tosca.TRelationshipTemplate)3 OTPatternRefinementModel (org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel)3 OTStayMapping (org.eclipse.winery.model.tosca.extensions.OTStayMapping)3 ModelUtilities (org.eclipse.winery.model.tosca.utils.ModelUtilities)3 ToscaIsomorphismMatcher (org.eclipse.winery.topologygraph.matching.ToscaIsomorphismMatcher)3 PatternDetectionUtils (org.eclipse.winery.topologygraph.matching.patterndetection.PatternDetectionUtils)3 ToscaEdge (org.eclipse.winery.topologygraph.model.ToscaEdge)3