Search in sources :

Example 6 with ToscaNode

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")));
}
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 7 with ToscaNode

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;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ToscaNode(org.eclipse.winery.topologygraph.model.ToscaNode) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 8 with ToscaNode

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;
}
Also used : Logger(org.slf4j.Logger) DefaultRefinementChooser(org.eclipse.winery.model.adaptation.substitution.refinement.DefaultRefinementChooser) LoggerFactory(org.slf4j.LoggerFactory) AbstractRefinement(org.eclipse.winery.model.adaptation.substitution.refinement.AbstractRefinement) RefinementChooser(org.eclipse.winery.model.adaptation.substitution.refinement.RefinementChooser) IToscaMatcher(org.eclipse.winery.topologygraph.matching.IToscaMatcher) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) Objects(java.util.Objects) TestRefinementModelId(org.eclipse.winery.model.ids.extensions.TestRefinementModelId) ToscaTypeMatcher(org.eclipse.winery.topologygraph.matching.ToscaTypeMatcher) ToscaNode(org.eclipse.winery.topologygraph.model.ToscaNode) BackendUtils(org.eclipse.winery.repository.backend.BackendUtils) Map(java.util.Map) OTRefinementModel(org.eclipse.winery.model.tosca.extensions.OTRefinementModel) OTRelationDirection(org.eclipse.winery.model.tosca.extensions.OTRelationDirection) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) RefinementCandidate(org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) ToscaNode(org.eclipse.winery.topologygraph.model.ToscaNode)

Example 9 with ToscaNode

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());
}
Also used : ToscaNode(org.eclipse.winery.topologygraph.model.ToscaNode) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) ArrayList(java.util.ArrayList) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) AbstractRefinementTest(org.eclipse.winery.model.adaptation.substitution.refinement.AbstractRefinementTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 10 with ToscaNode

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
}
Also used : ToscaGraph(org.eclipse.winery.topologygraph.model.ToscaGraph) ToscaIsomorphismMatcher(org.eclipse.winery.topologygraph.matching.ToscaIsomorphismMatcher) RefinementCandidate(org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) OTStayMapping(org.eclipse.winery.model.tosca.extensions.OTStayMapping) ToscaNode(org.eclipse.winery.topologygraph.model.ToscaNode) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) ToscaEdge(org.eclipse.winery.topologygraph.model.ToscaEdge) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel) ToscaTypeMatcher(org.eclipse.winery.topologygraph.matching.ToscaTypeMatcher) AbstractRefinementTest(org.eclipse.winery.model.adaptation.substitution.refinement.AbstractRefinementTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ToscaNode (org.eclipse.winery.topologygraph.model.ToscaNode)23 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)15 Test (org.junit.jupiter.api.Test)11 ArrayList (java.util.ArrayList)10 TTopologyTemplate (org.eclipse.winery.model.tosca.TTopologyTemplate)9 ToscaEdge (org.eclipse.winery.topologygraph.model.ToscaEdge)8 GraphMapping (org.jgrapht.GraphMapping)8 OTPatternRefinementModel (org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel)7 ToscaGraph (org.eclipse.winery.topologygraph.model.ToscaGraph)7 TRelationshipTemplate (org.eclipse.winery.model.tosca.TRelationshipTemplate)6 ToscaIsomorphismMatcher (org.eclipse.winery.topologygraph.matching.ToscaIsomorphismMatcher)6 QName (javax.xml.namespace.QName)5 RefinementCandidate (org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate)5 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 TPolicy (org.eclipse.winery.model.tosca.TPolicy)4 OTStayMapping (org.eclipse.winery.model.tosca.extensions.OTStayMapping)4 NamespaceManager (org.eclipse.winery.repository.backend.NamespaceManager)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 HashMap (java.util.HashMap)3