Search in sources :

Example 11 with ToscaNode

use of org.eclipse.winery.topologygraph.model.ToscaNode in project winery by eclipse.

the class ToscaTransformer method createAndInitializeTOSCANode.

protected static ToscaNode createAndInitializeTOSCANode(TNodeTemplate nodeTemplate) {
    ToscaNode node = new ToscaNode();
    node.setNodeTemplate(nodeTemplate);
    addTEntityTypes(nodeTemplate.getType(), node, TNodeType.class);
    return node;
}
Also used : ToscaNode(org.eclipse.winery.topologygraph.model.ToscaNode)

Example 12 with ToscaNode

use of org.eclipse.winery.topologygraph.model.ToscaNode 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;
}
Also used : ToscaGraph(org.eclipse.winery.topologygraph.model.ToscaGraph) HashMap(java.util.HashMap) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) ToscaNode(org.eclipse.winery.topologygraph.model.ToscaNode) ToscaEdge(org.eclipse.winery.topologygraph.model.ToscaEdge) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate)

Example 13 with ToscaNode

use of org.eclipse.winery.topologygraph.model.ToscaNode in project winery by eclipse.

the class ToscaBehaviorPatternMatcherTest method propertiesCompatible.

@Test
public void propertiesCompatible() {
    LinkedHashMap<String, String> refinementProps = new LinkedHashMap<>();
    refinementProps.put("null", null);
    refinementProps.put("empty", "");
    refinementProps.put("star", "*");
    refinementProps.put("match", "this has to match");
    refinementProps.put("ignoreCase", "THIS HAS TO MATCH INDEPENDENT OF CASE");
    LinkedHashMap<String, String> candidateProps = new LinkedHashMap<>();
    candidateProps.put("null", "does not have to be null");
    candidateProps.put("empty", "does not have to be empty");
    candidateProps.put("star", "this has to be non-null and non-empty");
    candidateProps.put("match", "this has to match");
    candidateProps.put("ignoreCase", "this has to match independent of case");
    TNodeTemplate refinement = new TNodeTemplate();
    refinement.setId("refinement");
    ModelUtilities.setPropertiesKV(refinement, refinementProps);
    ToscaNode refinementNode = new ToscaNode();
    refinementNode.setNodeTemplate(refinement);
    TNodeTemplate candidate = new TNodeTemplate();
    candidate.setId("candidate");
    ModelUtilities.setPropertiesKV(candidate, candidateProps);
    ToscaNode candidateNode = new ToscaNode();
    candidateNode.setNodeTemplate(candidate);
    OTPatternRefinementModel prm = new OTPatternRefinementModel();
    prm.setRefinementTopology(new TTopologyTemplate(new TTopologyTemplate.Builder().addNodeTemplate(refinement)));
    // needs to be swapped manually as only prms retrieved from repo are swapped automatically
    PatternDetectionUtils.swapDetectorWithRefinement(prm);
    ToscaBehaviorPatternMatcher matcher = new ToscaBehaviorPatternMatcher(prm, null);
    assertTrue(matcher.propertiesCompatible(refinementNode, candidateNode));
    refinementProps.put("doesNotMatch", "something");
    candidateProps.put("doesNotMatch", "something else");
    assertFalse(matcher.propertiesCompatible(refinementNode, candidateNode));
    // props with behavior pattern mappings can be ignored
    List<OTBehaviorPatternMapping> behaviorPatternMappings = new ArrayList<>();
    OTBehaviorPatternMapping behaviorPatternMapping = new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder("behaviorPatternMap0").setRefinementElement(refinement).setProperty(new OTPropertyKV("doesNotMatch", "")));
    behaviorPatternMappings.add((OTBehaviorPatternMapping) PatternDetectionUtils.swapDetectorWithRefinement(behaviorPatternMapping));
    prm.setBehaviorPatternMappings(behaviorPatternMappings);
    assertTrue(matcher.propertiesCompatible(refinementNode, candidateNode));
    candidateProps.put("empty", "");
    assertTrue(matcher.propertiesCompatible(refinementNode, candidateNode));
    candidateProps.put("star", null);
    assertFalse(matcher.propertiesCompatible(refinementNode, candidateNode));
    candidateProps.put("star", "");
    assertFalse(matcher.propertiesCompatible(refinementNode, candidateNode));
}
Also used : ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ToscaNode(org.eclipse.winery.topologygraph.model.ToscaNode) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) OTPropertyKV(org.eclipse.winery.model.tosca.extensions.kvproperties.OTPropertyKV) OTBehaviorPatternMapping(org.eclipse.winery.model.tosca.extensions.OTBehaviorPatternMapping) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel) Test(org.junit.jupiter.api.Test)

Example 14 with ToscaNode

use of org.eclipse.winery.topologygraph.model.ToscaNode in project winery by eclipse.

the class ToscaPatternMatcherTest method behaviorPatternsCompatible.

@Test
public void behaviorPatternsCompatible() {
    List<TPolicy> detectorPolicies = new ArrayList<>();
    detectorPolicies.add(new TPolicy.Builder(QName.valueOf("{ns}type1")).build());
    List<TPolicy> candidatePolicies = new ArrayList<>();
    TNodeTemplate detector = new TNodeTemplate.Builder("detector", QName.valueOf("{ns}type")).setPolicies(detectorPolicies).build();
    ToscaNode detectorNode = new ToscaNode();
    detectorNode.setNodeTemplate(detector);
    TNodeTemplate candidate = new TNodeTemplate.Builder("candidate", QName.valueOf("{ns}type")).setPolicies(candidatePolicies).build();
    ToscaNode candidateNode = new ToscaNode();
    candidateNode.setNodeTemplate(candidate);
    NamespaceManager namespaceManager = new MockNamespaceManager() {

        @Override
        public boolean isPatternNamespace(String namespace) {
            return namespace.equals("patternNs");
        }
    };
    ToscaPatternMatcher matcher = new ToscaPatternMatcher(null, namespaceManager) {
    };
    // detector has policy, candidate doesn't
    assertTrue(matcher.behaviorPatternsCompatible(detectorNode, candidateNode));
    detectorPolicies.clear();
    candidatePolicies.add(new TPolicy(new TPolicy.Builder(QName.valueOf("{ns}type1"))));
    // candidate has policy, detector doesn't
    assertTrue(matcher.behaviorPatternsCompatible(detectorNode, candidateNode));
    detectorPolicies.add(new TPolicy(new TPolicy.Builder(QName.valueOf("{patternNs}type2"))));
    // detector has behavior pattern, candidate doesn't
    assertFalse(matcher.behaviorPatternsCompatible(detectorNode, candidateNode));
    candidatePolicies.add(new TPolicy(new TPolicy.Builder(QName.valueOf("{patternNs}type2"))));
    // detector and candidate have same behavior pattern
    assertTrue(matcher.behaviorPatternsCompatible(detectorNode, candidateNode));
    candidatePolicies.add(new TPolicy(new TPolicy.Builder(QName.valueOf("{patternNs}type3"))));
    // candidate has different behavior pattern than detector
    assertFalse(matcher.behaviorPatternsCompatible(detectorNode, candidateNode));
}
Also used : MockNamespaceManager(org.eclipse.winery.topologygraph.matching.MockNamespaceManager) NamespaceManager(org.eclipse.winery.repository.backend.NamespaceManager) MockNamespaceManager(org.eclipse.winery.topologygraph.matching.MockNamespaceManager) ToscaNode(org.eclipse.winery.topologygraph.model.ToscaNode) TPolicy(org.eclipse.winery.model.tosca.TPolicy) ArrayList(java.util.ArrayList) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) Test(org.junit.jupiter.api.Test)

Example 15 with ToscaNode

use of org.eclipse.winery.topologygraph.model.ToscaNode in project winery by eclipse.

the class ToscaComponentPatternMatcherTest method componentPatternsCompatible.

@Test
public void componentPatternsCompatible() {
    // prm1 doesn't match the candidate but implements the same Component Pattern 'patternType1'
    TNodeTemplate detector1 = new TNodeTemplate(new TNodeTemplate.Builder("detector1", new QName("patternType1")));
    TNodeTemplate refinement1 = new TNodeTemplate(new TNodeTemplate.Builder("refinement1", new QName("concreteType1")));
    ToscaNode refinement1Node = new ToscaNode();
    refinement1Node.setNodeTemplate(refinement1);
    List<OTPermutationMapping> permutationMappings1 = new ArrayList<>();
    permutationMappings1.add(new OTPermutationMapping(new OTPermutationMapping.Builder().setDetectorElement(detector1).setRefinementElement(refinement1)));
    OTPatternRefinementModel prm1 = new OTPatternRefinementModel(new OTPatternRefinementModel.Builder().setDetector(new TTopologyTemplate(new TTopologyTemplate.Builder().addNodeTemplate(detector1))).setRefinementStructure(new TTopologyTemplate(new TTopologyTemplate.Builder().addNodeTemplate(refinement1))).setPermutationMappings(permutationMappings1));
    // needs to be swapped manually as only prms retrieved from repo are swapped automatically
    PatternDetectionUtils.swapDetectorWithRefinement(prm1);
    // prm2 matches the candidate and also implements the Component Pattern 'patternType1'
    TNodeTemplate detector2 = new TNodeTemplate(new TNodeTemplate.Builder("detector2", new QName("patternType1")));
    TNodeTemplate refinement2 = new TNodeTemplate(new TNodeTemplate.Builder("refinement2", new QName("concreteType2")));
    ToscaNode refinement2Node = new ToscaNode();
    refinement2Node.setNodeTemplate(refinement2);
    List<OTPermutationMapping> permutationMappings2 = new ArrayList<>();
    permutationMappings2.add(new OTPermutationMapping(new OTPermutationMapping.Builder().setDetectorElement(detector2).setRefinementElement(refinement2)));
    OTPatternRefinementModel prm2 = new OTPatternRefinementModel(new OTPatternRefinementModel.Builder().setDetector(new TTopologyTemplate(new TTopologyTemplate.Builder().addNodeTemplate(detector2))).setRefinementStructure(new TTopologyTemplate(new TTopologyTemplate.Builder().addNodeTemplate(refinement2))).setPermutationMappings(permutationMappings2));
    // needs to be swapped manually as only prms retrieved from repo are swapped automatically
    PatternDetectionUtils.swapDetectorWithRefinement(prm2);
    List<OTRefinementModel> prms = new ArrayList<>();
    prms.add(prm1);
    ToscaComponentPatternMatcher matcher = new ToscaComponentPatternMatcher(prm1, null, prms, new HashMap<>());
    ToscaNode candidate = new ToscaNode();
    candidate.setNodeTemplate(new TNodeTemplate(new TNodeTemplate.Builder("nt1", new QName("concreteType2"))));
    // only prm1 available -> doesn't match
    assertFalse(matcher.isCompatible(refinement1Node, candidate));
    prms.add(prm2);
    // now prm2 can also be used -> matches
    assertTrue(matcher.isCompatible(refinement1Node, candidate));
    prms.clear();
    prms.add(prm2);
    matcher = new ToscaComponentPatternMatcher(prm2, null, prms, new HashMap<>());
    // prm2 used directly -> also matches
    assertTrue(matcher.isCompatible(refinement2Node, candidate));
    prms.add(prm1);
    // still matches
    assertTrue(matcher.isCompatible(refinement2Node, candidate));
    matcher = new ToscaComponentPatternMatcher(prm1, null, prms, new HashMap<>());
    OTPermutationMapping oneTooMany = new OTPermutationMapping(new OTPermutationMapping.Builder().setDetectorElement(new TNodeTemplate(new TNodeTemplate.Builder("tmp1", new QName("tmp1")))).setRefinementElement(refinement1));
    permutationMappings1.add((OTPermutationMapping) PatternDetectionUtils.swapDetectorWithRefinement(oneTooMany));
    // detector is associated with multiple Component patterns -> not one-to-one
    assertFalse(matcher.isCompatible(refinement1Node, candidate));
    oneTooMany.setDetectorElement(detector1);
    oneTooMany.setRefinementElement(new TNodeTemplate(new TNodeTemplate.Builder("tmp2", new QName("tmp2"))));
    PatternDetectionUtils.swapDetectorWithRefinement(oneTooMany);
    // refinement element is associated with multiple Component patterns -> not one-to-one
    assertFalse(matcher.isCompatible(refinement1Node, candidate));
    permutationMappings1.remove(oneTooMany);
    // after removal, it's one-to-one again
    assertTrue(matcher.isCompatible(refinement1Node, candidate));
    // empty or null
    prm2.getPermutationMappings().clear();
    assertFalse(matcher.isCompatible(refinement1Node, candidate));
    prm1.getPermutationMappings().clear();
    assertFalse(matcher.isCompatible(refinement1Node, candidate));
    prm2.setPermutationMappings(null);
    assertFalse(matcher.isCompatible(refinement1Node, candidate));
    prm1.setPermutationMappings(null);
    assertFalse(matcher.isCompatible(refinement1Node, candidate));
}
Also used : HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) OTRefinementModel(org.eclipse.winery.model.tosca.extensions.OTRefinementModel) ToscaNode(org.eclipse.winery.topologygraph.model.ToscaNode) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) OTPermutationMapping(org.eclipse.winery.model.tosca.extensions.OTPermutationMapping) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel) Test(org.junit.jupiter.api.Test)

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