use of org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate 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
}
use of org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate in project winery by eclipse.
the class BehaviorPatternDetectionTest method removeIncompatibleBehaviorPatterns.
@Test
public void removeIncompatibleBehaviorPatterns() {
List<TPolicy> behaviorPatterns = new ArrayList<>();
behaviorPatterns.add(new TPolicy(new TPolicy.Builder(QName.valueOf("noProp")).setName("noProp")));
behaviorPatterns.add(new TPolicy(new TPolicy.Builder(QName.valueOf("oneProp")).setName("oneProp")));
behaviorPatterns.add(new TPolicy(new TPolicy.Builder(QName.valueOf("multiProps")).setName("multiProps")));
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");
refinementProps.put("multiPropsProp1", "true");
refinementProps.put("multiPropsProp2", "true");
refinementProps.put("multiPropsProp3", "*");
refinementProps.put("multiPropsProp4", "");
refinementProps.put("multiPropsProp5", null);
ModelUtilities.setPropertiesKV(refinementElement, refinementProps);
TNodeTemplate candidateElement = new TNodeTemplate(new TNodeTemplate.Builder("candidateElement", QName.valueOf("{ns}concreteType")).setX("1").setY("1"));
LinkedHashMap<String, String> candidateProps = new LinkedHashMap<>();
candidateProps.put("oneProp", "false");
candidateProps.put("multiPropsProp1", "false");
candidateProps.put("multiPropsProp2", "false");
candidateProps.put("multiPropsProp3", "");
candidateProps.put("multiPropsProp4", null);
candidateProps.put("multiPropsProp5", "");
ModelUtilities.setPropertiesKV(candidateElement, candidateProps);
List<OTBehaviorPatternMapping> behaviorPatternMappings = new ArrayList<>();
behaviorPatternMappings.add(new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder("behaviorPatternMap0").setDetectorElement(detectorElement).setRefinementElement(refinementElement).setBehaviorPattern("oneProp").setProperty(new OTPropertyKV("oneProp", "true"))));
behaviorPatternMappings.add(new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder("behaviorPatternMap1").setDetectorElement(detectorElement).setRefinementElement(refinementElement).setBehaviorPattern("multiProps").setProperty(new OTPropertyKV("multiPropsProp1", "true"))));
behaviorPatternMappings.add(new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder("behaviorPatternMap2").setDetectorElement(detectorElement).setRefinementElement(refinementElement).setBehaviorPattern("multiProps").setProperty(new OTPropertyKV("multiPropsProp2", "true"))));
behaviorPatternMappings.add(new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder("behaviorPatternMap3").setDetectorElement(detectorElement).setRefinementElement(refinementElement).setBehaviorPattern("multiProps").setProperty(new OTPropertyKV("multiPropsProp3", "*"))));
behaviorPatternMappings.add(new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder("behaviorPatternMap4").setDetectorElement(detectorElement).setRefinementElement(refinementElement).setBehaviorPattern("multiProps").setProperty(new OTPropertyKV("multiPropsProp4", ""))));
behaviorPatternMappings.add(new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder("behaviorPatternMap5").setDetectorElement(detectorElement).setRefinementElement(refinementElement).setBehaviorPattern("multiProps").setProperty(new OTPropertyKV("multiPropsProp5", null))));
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).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(candidateElement);
ToscaGraph topologyGraph = ToscaTransformer.createTOSCAGraph(topology);
ToscaGraph detectorGraph = ToscaTransformer.createTOSCAGraph(prm.getDetector());
IToscaMatcher matcher = new ToscaBehaviorPatternMatcher(prm, null);
ToscaIsomorphismMatcher isomorphismMatcher = new ToscaIsomorphismMatcher();
Iterator<GraphMapping<ToscaNode, ToscaEdge>> matches = isomorphismMatcher.findMatches(detectorGraph, topologyGraph, matcher);
RefinementCandidate refinementCandidate = new RefinementCandidate(prm, matches.next(), detectorGraph, 1);
// 'noProp' is applicable independent of properties as no behavior pattern mappings are specified
BehaviorPatternDetection behaviorPatternDetection = new BehaviorPatternDetection(new DefaultRefinementChooser());
behaviorPatternDetection.applyRefinement(refinementCandidate, topology);
assertEquals(topology.getNodeTemplateOrRelationshipTemplate().size(), 1);
List<TPolicy> policies = topology.getNodeTemplates().get(0).getPolicies();
assertEquals(policies.size(), 1);
assertEquals(policies.get(0).getName(), "noProp");
// 'noProp' still applicable, 'oneProp' now applicable as well because property is set
candidateProps.put("oneProp", "true");
topology.getNodeTemplateOrRelationshipTemplate().clear();
topology.addNodeTemplate(candidateElement);
behaviorPatternDetection.applyRefinement(refinementCandidate, topology);
assertEquals(topology.getNodeTemplateOrRelationshipTemplate().size(), 1);
policies = topology.getNodeTemplates().get(0).getPolicies();
assertEquals(policies.size(), 2);
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("noProp")));
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("oneProp")));
// 'noProp' & 'oneProp' still applicable, 'multiProps' not applicable because not all properties are set
candidateProps.put("multiPropsProp1", "true");
topology.getNodeTemplateOrRelationshipTemplate().clear();
topology.addNodeTemplate(candidateElement);
behaviorPatternDetection.applyRefinement(refinementCandidate, topology);
assertEquals(topology.getNodeTemplateOrRelationshipTemplate().size(), 1);
policies = topology.getNodeTemplates().get(0).getPolicies();
assertEquals(policies.size(), 2);
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("noProp")));
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("oneProp")));
// 'noProp' & 'oneProp' still applicable, 'multiProps' not applicable because not all properties are set
candidateProps.put("multiPropsProp2", "true");
topology.getNodeTemplateOrRelationshipTemplate().clear();
topology.addNodeTemplate(candidateElement);
behaviorPatternDetection.applyRefinement(refinementCandidate, topology);
assertEquals(topology.getNodeTemplateOrRelationshipTemplate().size(), 1);
policies = topology.getNodeTemplates().get(0).getPolicies();
assertEquals(policies.size(), 2);
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("noProp")));
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("oneProp")));
// 'noProp' & 'oneProp' & 'multiProps' applicable
candidateProps.put("multiPropsProp3", "non-null and non-empty");
topology.getNodeTemplateOrRelationshipTemplate().clear();
topology.addNodeTemplate(candidateElement);
behaviorPatternDetection.applyRefinement(refinementCandidate, topology);
assertEquals(topology.getNodeTemplateOrRelationshipTemplate().size(), 1);
policies = topology.getNodeTemplates().get(0).getPolicies();
assertEquals(policies.size(), 3);
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("noProp")));
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("oneProp")));
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("multiProps")));
}
use of org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate in project winery by eclipse.
the class ComponentPatternDetection method isApplicable.
@Override
public boolean isApplicable(RefinementCandidate candidate, TTopologyTemplate topology) {
RefinementCandidate adaptedPrm = adaptPrm(candidate);
adaptedPrms.put(candidate, adaptedPrm);
return super.isApplicable(adaptedPrm, topology);
}
use of org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate in project winery by eclipse.
the class ComponentPatternDetection method applyRefinement.
@Override
public Map<String, String> applyRefinement(RefinementCandidate refinement, TTopologyTemplate topology) {
RefinementCandidate adaptedPrm = adaptedPrms.get(refinement);
componentPatternCandidates.clear();
adaptedPrms.clear();
return super.applyRefinement(adaptedPrm, topology);
}
use of org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate in project winery by eclipse.
the class TopologyFragmentRefinement method applyRefinement.
@Override
public Map<String, String> applyRefinement(RefinementCandidate refinement, TTopologyTemplate topology) {
if (!(refinement.getRefinementModel() instanceof OTTopologyFragmentRefinementModel)) {
throw new UnsupportedOperationException("The refinement candidate is not a PRM!");
}
// determine the elements that are staying
OTTopologyFragmentRefinementModel prm = (OTTopologyFragmentRefinementModel) refinement.getRefinementModel();
List<String> stayingRefinementElementIds = RefinementUtils.getStayingRefinementElements(prm).stream().map(HasId::getId).collect(Collectors.toList());
// import the refinement structure
Map<String, String> idMapping = BackendUtils.mergeTopologyTemplateAinTopologyTemplateB(refinement.getRefinementModel().getRefinementTopology(), topology, stayingRefinementElementIds);
// only for UI: position the imported nodes next to the nodes to be refined
Map<String, Map<String, Integer>> coordinates = calculateNewPositions(refinement.getDetectorGraph(), refinement.getGraphMapping(), refinement.getRefinementModel().getRefinementTopology());
refinement.getRefinementModel().getRefinementTopology().getNodeTemplates().stream().filter(element -> !stayingRefinementElementIds.contains(element.getId())).forEach(node -> {
Map<String, Integer> newCoordinates = coordinates.get(node.getId());
TNodeTemplate nodeTemplate = topology.getNodeTemplate(idMapping.get(node.getId()));
if (nodeTemplate != null) {
nodeTemplate.setX(newCoordinates.get("x").toString());
nodeTemplate.setY(newCoordinates.get("y").toString());
}
});
// iterate over the detector nodes
refinement.getDetectorGraph().vertexSet().forEach(vertex -> {
// get the matching node in the topology
TNodeTemplate matchingNode = refinement.getGraphMapping().getVertexCorrespondence(vertex, false).getTemplate();
this.redirectInternalRelations(prm, vertex.getTemplate(), matchingNode, topology);
this.redirectExternalRelations(refinement, vertex.getTemplate(), matchingNode, topology, idMapping);
this.applyPropertyMappings(refinement, vertex.getId(), matchingNode, topology, idMapping);
this.applyDeploymentArtifactMapping(refinement, vertex.getTemplate(), matchingNode, topology, idMapping);
if (!getStayMappingsOfCurrentElement(prm, vertex.getTemplate()).findFirst().isPresent()) {
topology.getNodeTemplateOrRelationshipTemplate().remove(matchingNode);
} else if (shouldRemoveBehaviorPatterns(vertex.getTemplate(), matchingNode)) {
if (vertex.getTemplate().getPolicies() != null && matchingNode.getPolicies() != null)
vertex.getTemplate().getPolicies().forEach(detectorPolicy -> matchingNode.getPolicies().removeIf(matchingPolicy -> matchingPolicy.getPolicyType().equals(detectorPolicy.getPolicyType())));
}
});
refinement.getDetectorGraph().edgeSet().forEach(edge -> {
TRelationshipTemplate relationshipTemplate = refinement.getGraphMapping().getEdgeCorrespondence(edge, false).getTemplate();
this.applyPropertyMappings(refinement, edge.getId(), relationshipTemplate, topology, idMapping);
if (!getStayMappingsOfCurrentElement(prm, edge.getTemplate()).findFirst().isPresent()) {
topology.getNodeTemplateOrRelationshipTemplate().remove(relationshipTemplate);
}
});
return idMapping;
}
Aggregations