Search in sources :

Example 1 with RefinementCandidate

use of org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate 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 2 with RefinementCandidate

use of org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate in project winery by eclipse.

the class ComponentPatternDetection method adaptPrm.

private RefinementCandidate adaptPrm(RefinementCandidate candidate) {
    OTPatternRefinementModel prm = (OTPatternRefinementModel) candidate.getRefinementModel();
    OTPatternRefinementModel adaptedPrm = PatternDetectionUtils.clone(prm);
    componentPatternCandidates.get(prm).forEach(componentPatternCandidate -> addRelationMappings(componentPatternCandidate, adaptedPrm));
    removeNotApplicable(adaptedPrm);
    return new RefinementCandidate(adaptedPrm, candidate.getGraphMapping(), candidate.getDetectorGraph(), candidate.getId());
}
Also used : RefinementCandidate(org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel)

Example 3 with RefinementCandidate

use of org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate in project winery by eclipse.

the class BehaviorPatternDetection method addCompatibleBehaviorPatterns.

private void addCompatibleBehaviorPatterns(TEntityTemplate refinementElement, RefinementCandidate refinement) {
    OTPatternRefinementModel prm = (OTPatternRefinementModel) refinement.getRefinementModel();
    TEntityTemplate detectorElement = prm.getStayMappings().stream().filter(stayMapping -> stayMapping.getRefinementElement().getId().equals(refinementElement.getId())).findFirst().get().getDetectorElement();
    ToscaEntity detectorEntity = refinement.getDetectorGraph().getVertexOrEdge(detectorElement.getId()).get();
    TEntityTemplate stayingElement = getEntityCorrespondence(detectorEntity, refinement.getGraphMapping());
    List<TPolicy> refinementPolicies = ((HasPolicies) refinementElement).getPolicies();
    List<TPolicy> stayingPolicies = ((HasPolicies) stayingElement).getPolicies();
    if (refinementPolicies != null) {
        if (stayingPolicies != null) {
            // avoid duplicates
            refinementPolicies.forEach(refinementPolicy -> {
                boolean policyExists = stayingPolicies.stream().anyMatch(stayingPolicy -> stayingPolicy.getPolicyType().equals(refinementPolicy.getPolicyType()));
                if (!policyExists) {
                    stayingPolicies.add(refinementPolicy);
                }
            });
        } else {
            ((HasPolicies) stayingElement).setPolicies(new ArrayList<>(refinementPolicies));
        }
        removeIncompatibleBehaviorPatterns(refinementElement, stayingElement, refinement);
    }
}
Also used : TPolicy(org.eclipse.winery.model.tosca.TPolicy) HasPolicies(org.eclipse.winery.model.tosca.HasPolicies) PatternDetectionUtils(org.eclipse.winery.topologygraph.matching.patterndetection.PatternDetectionUtils) TopologyFragmentRefinement(org.eclipse.winery.model.adaptation.substitution.refinement.topologyrefinement.TopologyFragmentRefinement) RefinementChooser(org.eclipse.winery.model.adaptation.substitution.refinement.RefinementChooser) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) IToscaMatcher(org.eclipse.winery.topologygraph.matching.IToscaMatcher) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) ArrayList(java.util.ArrayList) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel) List(java.util.List) PatternRefinementModelId(org.eclipse.winery.model.ids.extensions.PatternRefinementModelId) ToscaEntity(org.eclipse.winery.topologygraph.model.ToscaEntity) Map(java.util.Map) OTRefinementModel(org.eclipse.winery.model.tosca.extensions.OTRefinementModel) ToscaBehaviorPatternMatcher(org.eclipse.winery.topologygraph.matching.patterndetection.ToscaBehaviorPatternMatcher) ModelUtilities(org.eclipse.winery.model.tosca.utils.ModelUtilities) PatternDetectionUtils.getEntityCorrespondence(org.eclipse.winery.topologygraph.matching.patterndetection.PatternDetectionUtils.getEntityCorrespondence) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) RefinementCandidate(org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) TPolicy(org.eclipse.winery.model.tosca.TPolicy) ToscaEntity(org.eclipse.winery.topologygraph.model.ToscaEntity) HasPolicies(org.eclipse.winery.model.tosca.HasPolicies) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel)

Example 4 with RefinementCandidate

use of org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate 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 5 with RefinementCandidate

use of org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate in project winery by eclipse.

the class PatternRefinementTestWithGitBackedRepository method refineTopologyWithMultipleSameSubGraphs.

@Test
public void refineTopologyWithMultipleSameSubGraphs() throws Exception {
    this.setRevisionTo("origin/plain");
    List<RefinementCandidate> myCandidates = new ArrayList<>();
    PatternRefinement refinement = new PatternRefinement((candidates, refinementServiceTemplate, currentTopology) -> {
        myCandidates.addAll(candidates);
        return null;
    });
    ServiceTemplateId serviceTemplateId = refinement.refineServiceTemplate(new ServiceTemplateId("http://winery.opentosca.org/test/concrete/servicetemplates", "Pattern-basedDeplyomentModelWithTwoSameSubgraphs_w1-wip1", false));
    assertEquals(2, myCandidates.size());
    myCandidates.forEach(prmc -> assertEquals("IaaS_connectedTo_ThirdPattern_w1-wip1", prmc.getRefinementModel().getName()));
    ArrayList<String> nodeIdsToBeReplacedInFirstPmc = myCandidates.get(0).getNodeIdsToBeReplaced();
    assertEquals("Infrastructure-As-A-Service_w1", nodeIdsToBeReplacedInFirstPmc.get(0));
    assertEquals("ThirdPattern_w1", nodeIdsToBeReplacedInFirstPmc.get(1));
    ArrayList<String> nodeIdsToBeReplacedInSecondPmc = myCandidates.get(1).getNodeIdsToBeReplaced();
    assertEquals("Infrastructure-As-A-Service_w1_2", nodeIdsToBeReplacedInSecondPmc.get(0));
    assertEquals("ThirdPattern_w1", nodeIdsToBeReplacedInSecondPmc.get(1));
    assertEquals("Pattern-basedDeplyomentModelWithTwoSameSubgraphs_w1-wip1-refined-w1-wip1", serviceTemplateId.getQName().getLocalPart());
    assertEquals("http://winery.opentosca.org/test/concrete/servicetemplates", serviceTemplateId.getQName().getNamespaceURI());
}
Also used : RefinementCandidate(org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate) ArrayList(java.util.ArrayList) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) Test(org.junit.jupiter.api.Test)

Aggregations

RefinementCandidate (org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate)15 ArrayList (java.util.ArrayList)10 TTopologyTemplate (org.eclipse.winery.model.tosca.TTopologyTemplate)9 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)8 List (java.util.List)7 Map (java.util.Map)7 IToscaMatcher (org.eclipse.winery.topologygraph.matching.IToscaMatcher)7 ToscaNode (org.eclipse.winery.topologygraph.model.ToscaNode)7 Test (org.junit.jupiter.api.Test)7 ModelUtilities (org.eclipse.winery.model.tosca.utils.ModelUtilities)6 ToscaEdge (org.eclipse.winery.topologygraph.model.ToscaEdge)6 ToscaGraph (org.eclipse.winery.topologygraph.model.ToscaGraph)6 DefaultRefinementChooser (org.eclipse.winery.model.adaptation.substitution.refinement.DefaultRefinementChooser)5 RefinementChooser (org.eclipse.winery.model.adaptation.substitution.refinement.RefinementChooser)5 TRelationshipTemplate (org.eclipse.winery.model.tosca.TRelationshipTemplate)5 OTRefinementModel (org.eclipse.winery.model.tosca.extensions.OTRefinementModel)5 OTStayMapping (org.eclipse.winery.model.tosca.extensions.OTStayMapping)5 LinkedHashMap (java.util.LinkedHashMap)4 QName (javax.xml.namespace.QName)4 TEntityTemplate (org.eclipse.winery.model.tosca.TEntityTemplate)4