Search in sources :

Example 96 with TTopologyTemplate

use of org.eclipse.winery.model.tosca.TTopologyTemplate 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)

Example 97 with TTopologyTemplate

use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.

the class PatternDetectionUtilsTest method swapDetectorWithRefinement.

@Test
public void swapDetectorWithRefinement() {
    TTopologyTemplate detector = new TTopologyTemplate();
    TTopologyTemplate refinementStructure = new TTopologyTemplate();
    TEntityTemplate detectorElement = new TNodeTemplate();
    TEntityTemplate refinementElement = new TNodeTemplate();
    OTRelationMapping relationMapping = new OTRelationMapping(new OTRelationMapping.Builder().setDetectorElement(detectorElement).setRefinementElement(refinementElement).setDirection(OTRelationDirection.INGOING).setRelationType("relationType").setValidSourceOrTarget(QName.valueOf("validSourceOrTarget")));
    OTPermutationMapping permutationMapping = new OTPermutationMapping(new OTPermutationMapping.Builder().setDetectorElement(detectorElement).setRefinementElement(refinementElement));
    OTAttributeMapping attributeMapping = new OTAttributeMapping(new OTAttributeMapping.Builder().setDetectorElement(detectorElement).setRefinementElement(refinementElement).setDetectorProperty("detectorProperty").setRefinementProperty("refinementProperty"));
    OTStayMapping stayMapping = new OTStayMapping(new OTStayMapping.Builder().setDetectorElement(detectorElement).setRefinementElement(refinementElement));
    OTDeploymentArtifactMapping deploymentArtifactMapping = new OTDeploymentArtifactMapping(new OTDeploymentArtifactMapping.Builder().setDetectorElement(detectorElement).setRefinementElement(refinementElement).setArtifactType(QName.valueOf("artifactType")));
    OTBehaviorPatternMapping behaviorPatternMapping = new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder("behaviorPatternMap0").setDetectorElement(detectorElement).setRefinementElement(refinementElement).setBehaviorPattern("behaviorPattern").setProperty(new OTPropertyKV("key", "value")));
    OTPatternRefinementModel prm = new OTPatternRefinementModel(new OTPatternRefinementModel.Builder().setDetector(detector).setRefinementStructure(refinementStructure).setRelationMappings(Collections.singletonList(relationMapping)).setPermutationMappings(Collections.singletonList(permutationMapping)).setAttributeMappings(Collections.singletonList(attributeMapping)).setStayMappings(Collections.singletonList(stayMapping)).setDeploymentArtifactMappings(Collections.singletonList(deploymentArtifactMapping)).setBehaviorPatternMappings(Collections.singletonList(behaviorPatternMapping)));
    PatternDetectionUtils.swapDetectorWithRefinement(prm);
    assertEquals(prm.getDetector(), refinementStructure);
    assertEquals(prm.getRefinementStructure(), detector);
    assertEquals(relationMapping.getDetectorElement(), refinementElement);
    assertEquals(relationMapping.getRefinementElement(), detectorElement);
    assertEquals(relationMapping.getDirection(), OTRelationDirection.INGOING);
    assertEquals(relationMapping.getRelationType(), QName.valueOf("relationType"));
    assertEquals(relationMapping.getValidSourceOrTarget(), QName.valueOf("validSourceOrTarget"));
    assertEquals(permutationMapping.getDetectorElement(), refinementElement);
    assertEquals(permutationMapping.getRefinementElement(), detectorElement);
    assertEquals(attributeMapping.getDetectorElement(), refinementElement);
    assertEquals(attributeMapping.getRefinementElement(), detectorElement);
    assertEquals(attributeMapping.getDetectorProperty(), "refinementProperty");
    assertEquals(attributeMapping.getRefinementProperty(), "detectorProperty");
    assertEquals(stayMapping.getDetectorElement(), refinementElement);
    assertEquals(stayMapping.getRefinementElement(), detectorElement);
    assertEquals(deploymentArtifactMapping.getDetectorElement(), refinementElement);
    assertEquals(deploymentArtifactMapping.getRefinementElement(), detectorElement);
    assertEquals(deploymentArtifactMapping.getArtifactType(), QName.valueOf("artifactType"));
    assertEquals(behaviorPatternMapping.getDetectorElement(), refinementElement);
    assertEquals(behaviorPatternMapping.getRefinementElement(), detectorElement);
    assertEquals(behaviorPatternMapping.getBehaviorPattern(), "behaviorPattern");
    assertEquals(behaviorPatternMapping.getProperty(), new OTPropertyKV("key", "value"));
}
Also used : OTDeploymentArtifactMapping(org.eclipse.winery.model.tosca.extensions.OTDeploymentArtifactMapping) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) OTAttributeMapping(org.eclipse.winery.model.tosca.extensions.OTAttributeMapping) OTRelationMapping(org.eclipse.winery.model.tosca.extensions.OTRelationMapping) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) OTStayMapping(org.eclipse.winery.model.tosca.extensions.OTStayMapping) OTPropertyKV(org.eclipse.winery.model.tosca.extensions.kvproperties.OTPropertyKV) OTBehaviorPatternMapping(org.eclipse.winery.model.tosca.extensions.OTBehaviorPatternMapping) 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)

Example 98 with TTopologyTemplate

use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.

the class BackendUtils method clone.

/**
 * @param topologyTemplate which should be cloned
 * @return Copy od topologyTemplate
 */
public static TTopologyTemplate clone(TTopologyTemplate topologyTemplate) {
    @SuppressWarnings("deprecated") TTopologyTemplate topologyTemplateClone = new TTopologyTemplate();
    List<TEntityTemplate> entityTemplate = topologyTemplate.getNodeTemplateOrRelationshipTemplate();
    topologyTemplateClone.getNodeTemplateOrRelationshipTemplate().addAll(entityTemplate);
    return topologyTemplateClone;
}
Also used : TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate)

Example 99 with TTopologyTemplate

use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.

the class BackendUtils method getAllNestedNodeTemplates.

/**
 * Returns a list of the topology template nested in the given service template
 */
public static List<TNodeTemplate> getAllNestedNodeTemplates(TServiceTemplate serviceTemplate) {
    List<TNodeTemplate> l = new ArrayList<>();
    TTopologyTemplate topologyTemplate = serviceTemplate.getTopologyTemplate();
    if (topologyTemplate == null) {
        return Collections.emptyList();
    }
    for (TEntityTemplate t : topologyTemplate.getNodeTemplateOrRelationshipTemplate()) {
        if (t instanceof TNodeTemplate) {
            l.add((TNodeTemplate) t);
        }
    }
    return l;
}
Also used : TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) ArrayList(java.util.ArrayList) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate)

Example 100 with TTopologyTemplate

use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.

the class IRepository method getReferenceCount.

default int getReferenceCount(ArtifactTemplateId id) {
    // We do not use a database, therefore, we have to go through all possibilities pointing to the artifact template
    // DAs and IAs point to an artifact template
    // DAs are contained in Node Type Implementations and Node Templates
    // IAs are contained in Node Type Implementations and Relationship Type Implementations
    int count = 0;
    Collection<TDeploymentArtifact> allDAs = new HashSet<>();
    Collection<TImplementationArtifact> allIAs = new HashSet<>();
    // handle Node Type Implementation, which contains DAs and IAs
    SortedSet<NodeTypeImplementationId> nodeTypeImplementations = this.getAllDefinitionsChildIds(NodeTypeImplementationId.class);
    for (NodeTypeImplementationId ntiId : nodeTypeImplementations) {
        final TNodeTypeImplementation nodeTypeImplementation = this.getElement(ntiId);
        List<TDeploymentArtifact> deploymentArtifacts = nodeTypeImplementation.getDeploymentArtifacts();
        if (deploymentArtifacts != null) {
            allDAs.addAll(deploymentArtifacts);
        }
        List<TImplementationArtifact> implementationArtifacts = nodeTypeImplementation.getImplementationArtifacts();
        if (implementationArtifacts != null) {
            allIAs.addAll(implementationArtifacts);
        }
    }
    // check all RelationshipTypeImplementations for IAs
    SortedSet<RelationshipTypeImplementationId> relationshipTypeImplementations = this.getAllDefinitionsChildIds(RelationshipTypeImplementationId.class);
    for (RelationshipTypeImplementationId rtiId : relationshipTypeImplementations) {
        List<TImplementationArtifact> implementationArtifacts = this.getElement(rtiId).getImplementationArtifacts();
        if (implementationArtifacts != null) {
            allIAs.addAll(implementationArtifacts);
        }
    }
    // check all node templates for DAs
    SortedSet<ServiceTemplateId> serviceTemplates = this.getAllDefinitionsChildIds(ServiceTemplateId.class);
    for (ServiceTemplateId sid : serviceTemplates) {
        TTopologyTemplate topologyTemplate = this.getElement(sid).getTopologyTemplate();
        if (topologyTemplate != null) {
            List<TEntityTemplate> nodeTemplateOrRelationshipTemplate = topologyTemplate.getNodeTemplateOrRelationshipTemplate();
            for (TEntityTemplate template : nodeTemplateOrRelationshipTemplate) {
                if (template instanceof TNodeTemplate) {
                    TNodeTemplate nodeTemplate = (TNodeTemplate) template;
                    List<TDeploymentArtifact> deploymentArtifacts = nodeTemplate.getDeploymentArtifacts();
                    if (deploymentArtifacts != null) {
                        allDAs.addAll(deploymentArtifacts);
                    }
                }
            }
        }
    }
    // now we have all DAs and IAs
    QName ourQName = id.getQName();
    // check DAs for artifact templates
    for (TDeploymentArtifact da : allDAs) {
        QName artifactRef = da.getArtifactRef();
        if (ourQName.equals(artifactRef)) {
            count++;
        }
    }
    // check IAs for artifact templates
    for (TImplementationArtifact ia : allIAs) {
        QName artifactRef = ia.getArtifactRef();
        if (ourQName.equals(artifactRef)) {
            count++;
        }
    }
    return count;
}
Also used : NodeTypeImplementationId(org.eclipse.winery.model.ids.definitions.NodeTypeImplementationId) TImplementationArtifact(org.eclipse.winery.model.tosca.TImplementationArtifact) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) QName(javax.xml.namespace.QName) TNodeTypeImplementation(org.eclipse.winery.model.tosca.TNodeTypeImplementation) RelationshipTypeImplementationId(org.eclipse.winery.model.ids.definitions.RelationshipTypeImplementationId) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) HashSet(java.util.HashSet)

Aggregations

TTopologyTemplate (org.eclipse.winery.model.tosca.TTopologyTemplate)158 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)112 ArrayList (java.util.ArrayList)85 TRelationshipTemplate (org.eclipse.winery.model.tosca.TRelationshipTemplate)62 List (java.util.List)59 QName (javax.xml.namespace.QName)55 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)55 Test (org.junit.jupiter.api.Test)51 Map (java.util.Map)48 HashMap (java.util.HashMap)47 LinkedHashMap (java.util.LinkedHashMap)42 TNodeType (org.eclipse.winery.model.tosca.TNodeType)42 ModelUtilities (org.eclipse.winery.model.tosca.utils.ModelUtilities)38 IRepository (org.eclipse.winery.repository.backend.IRepository)38 NodeTypeId (org.eclipse.winery.model.ids.definitions.NodeTypeId)36 TServiceTemplate (org.eclipse.winery.model.tosca.TServiceTemplate)35 Objects (java.util.Objects)34 IOException (java.io.IOException)32 HashSet (java.util.HashSet)32 Collectors (java.util.stream.Collectors)32