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")));
}
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());
}
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);
}
}
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;
}
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());
}
Aggregations