use of org.eclipse.winery.model.tosca.extensions.OTPermutationMapping in project winery by eclipse.
the class PermutationHelper method generatePrmWithStayMapping.
static OTPatternRefinementModel generatePrmWithStayMapping() {
/*
######## ########
# (1) #----stay-------> # (11) #
######## ########
| (2) |
| (2) \/
\/ ########
########---------------> # (13) #
# (2) #--------+ ########
######## | | (2)
| | \/
| (2 +------> ########
\/ # (15) #
######## ########
# (3) #--------+ | (2)
######## | \/
| ########
+------> # (16) #
########
*/
TTopologyTemplate detector = generateDetectorWithThreePatterns();
// region refinement structure
TNodeTemplate refinementNode_11 = new TNodeTemplate();
refinementNode_11.setType("{http://ex.org}nodeType_11");
refinementNode_11.setId("11");
TNodeTemplate refinementNode_13 = new TNodeTemplate();
refinementNode_13.setType("{http://ex.org}nodeType_13");
refinementNode_13.setId("13");
TNodeTemplate refinementNode_15 = new TNodeTemplate();
refinementNode_15.setType("{http://ex.org}nodeType_15");
refinementNode_15.setId("15");
TNodeTemplate refinementNode_16 = new TNodeTemplate();
refinementNode_16.setType("{http://ex.org}nodeType_16");
refinementNode_16.setId("16");
TRelationshipTemplate node11_hostedOn_node13 = ModelUtilities.createRelationshipTemplate(refinementNode_11, refinementNode_13, QName.valueOf("{http://ex.org}relType_hostedOn"));
TRelationshipTemplate node13_hostedOn_node15 = ModelUtilities.createRelationshipTemplate(refinementNode_13, refinementNode_15, QName.valueOf("{http://ex.org}relType_hostedOn"));
TRelationshipTemplate node15_hostedOn_node16 = ModelUtilities.createRelationshipTemplate(refinementNode_15, refinementNode_16, QName.valueOf("{http://ex.org}relType_hostedOn"));
TTopologyTemplate refinementStructure = new TTopologyTemplate.Builder().addNodeTemplate(refinementNode_11).addNodeTemplate(refinementNode_13).addNodeTemplate(refinementNode_15).addNodeTemplate(refinementNode_16).addRelationshipTemplate(node11_hostedOn_node13).addRelationshipTemplate(node13_hostedOn_node15).addRelationshipTemplate(node15_hostedOn_node16).build();
// endregion
// region mappings
OTStayMapping pattern1_to_node11 = new OTStayMapping.Builder("sm-1--11").setDetectorElement(detector.getNodeTemplate("1")).setRefinementElement(refinementNode_11).build();
OTRelationMapping pattern2_to_node13 = new OTRelationMapping.Builder("pm-2--13").setRelationType("{http://ex.org}relType_hostedOn").setDirection(OTRelationDirection.INGOING).setDetectorElement(detector.getNodeTemplate("2")).setRefinementElement(refinementNode_13).build();
OTPermutationMapping pattern2_to_node15 = new OTPermutationMapping.Builder("pm-2--15").setDetectorElement(detector.getNodeTemplate("2")).setRefinementElement(refinementNode_15).build();
OTRelationMapping pattern3_to_node16 = new OTRelationMapping.Builder("pm-3--16").setRelationType(QName.valueOf("{http://ex.org}relType_connectsTo")).setDirection(OTRelationDirection.INGOING).setDetectorElement(detector.getNodeTemplate("3")).setRefinementElement(refinementNode_16).build();
// endregion
// region prm
ArrayList<OTPermutationMapping> mappings = new ArrayList<>();
mappings.add(pattern2_to_node15);
return new OTPatternRefinementModel.Builder().setName("PrmWithStaying").setTargetNamespace("http://ex.org").setDetector(detector).setRefinementStructure(refinementStructure).setRelationMappings(Arrays.asList(pattern2_to_node13, pattern3_to_node16)).setStayMappings(Collections.singletonList(pattern1_to_node11)).setPermutationMappings(mappings).build();
}
use of org.eclipse.winery.model.tosca.extensions.OTPermutationMapping in project winery by eclipse.
the class TopologyFragmentRefinementModelResource method savePrmMappingTopology.
@PUT
@Path("graphicPrmTopology")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Override
public TopologyTemplateResource savePrmMappingTopology(TTopologyTemplate topologyTemplate) {
this.getTRefinementModel().resetAllMappings();
RestUtils.persist(this);
for (TRelationshipTemplate relTemplate : topologyTemplate.getRelationshipTemplates()) {
String mappingId;
// necessary for topologymodeler to create the IDs
if (relTemplate.getId().startsWith("con")) {
mappingId = relTemplate.getId().substring(relTemplate.getId().indexOf("_") + 1);
} else {
mappingId = relTemplate.getId();
}
if (relTemplate.getType().getLocalPart().startsWith("PermutationMapping")) {
this.getPermutationMappings().addMapping(new OTPermutationMapping(new OTPermutationMapping.Builder(mappingId).setDetectorElement(relTemplate.getSourceElement().getRef()).setRefinementElement(relTemplate.getTargetElement().getRef())));
}
if (relTemplate.getType().getLocalPart().startsWith("RelationshipMapping")) {
Map<String, String> propertiesMap = ((TEntityTemplate.WineryKVProperties) relTemplate.getProperties()).getKVProperties();
this.getRelationMappings().addMapping(new OTRelationMapping(new OTRelationMapping.Builder(mappingId).setDetectorElement(relTemplate.getSourceElement().getRef()).setRefinementElement(relTemplate.getTargetElement().getRef()).setDirection(OTRelationDirection.fromValue(propertiesMap.get("direction"))).setRelationType(QName.valueOf(propertiesMap.get("applicableRelationshipType"))).setValidSourceOrTarget(QName.valueOf(propertiesMap.get("validEndpointType")))));
}
if (relTemplate.getType().getLocalPart().startsWith("StayMapping")) {
this.getStayMappings().addMapping(new OTStayMapping(new OTStayMapping.Builder(mappingId).setDetectorElement(relTemplate.getSourceElement().getRef()).setRefinementElement(relTemplate.getTargetElement().getRef())));
}
if (relTemplate.getType().getLocalPart().startsWith("AttributeMapping")) {
Map<String, String> propertiesMap = ((TEntityTemplate.WineryKVProperties) relTemplate.getProperties()).getKVProperties();
this.getAttributeMappings().addMapping(new OTAttributeMapping(new OTAttributeMapping.Builder(mappingId).setDetectorElement(relTemplate.getSourceElement().getRef()).setRefinementElement(relTemplate.getTargetElement().getRef()).setType(OTAttributeMappingType.fromValue(propertiesMap.get("type"))).setDetectorProperty(propertiesMap.get("detectorProperty")).setRefinementProperty(propertiesMap.get("refinementProperty"))));
}
if (relTemplate.getType().getLocalPart().startsWith("DeploymentArtifactMapping")) {
Map<String, String> propertiesMap = ((TEntityTemplate.WineryKVProperties) relTemplate.getProperties()).getKVProperties();
this.getDeploymentArtifactMappings().addMapping(new OTDeploymentArtifactMapping(new OTDeploymentArtifactMapping.Builder(mappingId).setDetectorElement(relTemplate.getSourceElement().getRef()).setRefinementElement(relTemplate.getTargetElement().getRef()).setArtifactType(QName.valueOf(propertiesMap.get("requiredDeploymentArtifactType")))));
}
if (relTemplate.getType().getLocalPart().startsWith("BehaviorPatternMapping")) {
Map<String, String> propertiesMap = ((TEntityTemplate.WineryKVProperties) relTemplate.getProperties()).getKVProperties();
((PatternRefinementModelResource) this).getBehaviorPatternMappings().addMapping(new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder(mappingId).setDetectorElement(relTemplate.getSourceElement().getRef()).setRefinementElement(relTemplate.getTargetElement().getRef()).setProperty(new OTPropertyKV(propertiesMap.get("refinementProperty"), propertiesMap.get("refinementPropertyValue"))).setBehaviorPattern(propertiesMap.get("behaviorPattern"))));
}
}
return new RefinementTopologyTemplateResource(this, this.getTRefinementModel(), GRAFIC_PRM_MODEL);
}
use of org.eclipse.winery.model.tosca.extensions.OTPermutationMapping in project winery by eclipse.
the class ToscaComponentPatternMatcherTest method isCompatible.
@Test
public void isCompatible() {
// same type
TNodeTemplate refinementTemplate = new TNodeTemplate(new TNodeTemplate.Builder("d", new QName("type1")));
ToscaNode refinement = new ToscaNode();
refinement.addTEntityType(new TNodeType(new TNodeType.Builder("type1")));
refinement.setNodeTemplate(refinementTemplate);
TNodeTemplate candidateTemplate = new TNodeTemplate(new TNodeTemplate.Builder("c", new QName("type1")));
ToscaNode candidate = new ToscaNode();
candidate.addTEntityType(new TNodeType(new TNodeType.Builder("type1")));
candidate.setNodeTemplate(candidateTemplate);
OTPatternRefinementModel prm = new OTPatternRefinementModel(new OTPatternRefinementModel.Builder().setRefinementStructure(new TTopologyTemplate(new TTopologyTemplate.Builder().addNodeTemplate(refinementTemplate))));
// needs to be swapped manually as only prms retrieved from repo are swapped automatically
PatternDetectionUtils.swapDetectorWithRefinement(prm);
NamespaceManager namespaceManager = new MockNamespaceManager() {
@Override
public boolean isPatternNamespace(String namespace) {
return namespace.equals("patternNs");
}
};
ToscaComponentPatternMatcher matcher = new ToscaComponentPatternMatcher(prm, namespaceManager, new ArrayList<>(), new HashMap<>());
assertTrue(matcher.isCompatible(refinement, candidate));
// different type
refinementTemplate.setType(new QName("type1"));
refinement.getTypes().clear();
refinement.addTEntityType(new TNodeType(new TNodeType.Builder("type1")));
candidateTemplate.setType(new QName("type2"));
candidate.getTypes().clear();
candidate.addTEntityType(new TNodeType(new TNodeType.Builder("type2")));
assertFalse(matcher.isCompatible(refinement, candidate));
// share supertype
refinement.addTEntityType(new TNodeType(new TNodeType.Builder("super")));
candidate.addTEntityType(new TNodeType(new TNodeType.Builder("super")));
assertTrue(matcher.isCompatible(refinement, candidate));
// stay mapping exists
OTStayMapping stayMapping = new OTStayMapping(new OTStayMapping.Builder().setDetectorElement(null).setRefinementElement(refinementTemplate));
List<OTStayMapping> stayMappings = new ArrayList<>();
stayMappings.add((OTStayMapping) PatternDetectionUtils.swapDetectorWithRefinement(stayMapping));
prm.setStayMappings(stayMappings);
assertFalse(matcher.isCompatible(refinement, candidate));
assertNotNull(prm.getStayMappings());
prm.getStayMappings().clear();
assertTrue(matcher.isCompatible(refinement, candidate));
// component pattern mapping exists
List<OTPermutationMapping> componentPatternMappings = new ArrayList<>();
OTPermutationMapping componentPatternMapping = new OTPermutationMapping(new OTPermutationMapping.Builder().setDetectorElement(new TNodeTemplate.Builder("detector", QName.valueOf("{ns}type")).build()).setRefinementElement(refinementTemplate));
componentPatternMappings.add((OTPermutationMapping) PatternDetectionUtils.swapDetectorWithRefinement(componentPatternMapping));
prm.setPermutationMappings(componentPatternMappings);
assertFalse(matcher.isCompatible(refinement, candidate));
prm.getPermutationMappings().clear();
assertTrue(matcher.isCompatible(refinement, candidate));
// different behavior patterns
List<TPolicy> refinementPolicies = new ArrayList<>();
refinementPolicies.add(new TPolicy.Builder(QName.valueOf("{patternNs}type1")).build());
refinementTemplate.setPolicies(refinementPolicies);
List<TPolicy> candidatePolicies = new ArrayList<>();
candidateTemplate.setPolicies(candidatePolicies);
// detector has behavior pattern, candidate doesn't
assertFalse(matcher.isCompatible(refinement, candidate));
candidatePolicies.add(new TPolicy.Builder(QName.valueOf("{patternNs}type1")).build());
// detector and candidate have same behavior pattern
assertTrue(matcher.isCompatible(refinement, candidate));
refinementPolicies.clear();
// candidate has behavior pattern, detector doesn't
assertFalse(matcher.isCompatible(refinement, candidate));
candidatePolicies.clear();
assertTrue(matcher.isCompatible(refinement, candidate));
// detector supertype of candidate
refinementTemplate.setType(new QName("super"));
refinement.getTypes().clear();
refinement.addTEntityType(new TNodeType(new TNodeType.Builder("super")));
candidateTemplate.setType(new QName("type"));
candidate.getTypes().clear();
candidate.addTEntityType(new TNodeType(new TNodeType.Builder("type")));
candidate.addTEntityType(new TNodeType(new TNodeType.Builder("super")));
assertTrue(matcher.isCompatible(refinement, candidate));
// candidate supertype of detector
refinementTemplate.setType(new QName("type"));
refinement.getTypes().clear();
refinement.addTEntityType(new TNodeType(new TNodeType.Builder("type")));
refinement.addTEntityType(new TNodeType(new TNodeType.Builder("super")));
candidateTemplate.setType(new QName("super"));
candidate.getTypes().clear();
candidate.addTEntityType(new TNodeType(new TNodeType.Builder("super")));
assertTrue(matcher.isCompatible(refinement, candidate));
// different supertypes
refinementTemplate.setType(new QName("type1"));
refinement.getTypes().clear();
refinement.addTEntityType(new TNodeType(new TNodeType.Builder("type1")));
refinement.addTEntityType(new TNodeType(new TNodeType.Builder("super1")));
candidateTemplate.setType(new QName("type2"));
candidate.getTypes().clear();
candidate.addTEntityType(new TNodeType(new TNodeType.Builder("type2")));
candidate.addTEntityType(new TNodeType(new TNodeType.Builder("super2")));
assertFalse(matcher.isCompatible(refinement, candidate));
}
use of org.eclipse.winery.model.tosca.extensions.OTPermutationMapping in project winery by eclipse.
the class RefinementTopologyTemplateResource method createRelationshipsForMappings.
/**
* convert mappings into Relationship for use in grafic prm modeling
*/
private void createRelationshipsForMappings(OTRefinementModel refinementModel) {
for (OTPrmMapping mapping : getAllMappings(refinementModel)) {
TRelationshipTemplate.Builder builder = new TRelationshipTemplate.Builder("con_" + mapping.getId(), QName.valueOf("{http://opentosca.org/prmMappingTypes}" + mapping.getId().substring(0, mapping.getId().indexOf("_"))), new TNodeTemplate(mapping.getDetectorElement().getId()), new TNodeTemplate(mapping.getRefinementElement().getId()));
if (mapping instanceof OTPermutationMapping) {
builder.setName("PermutationMapping");
}
if (mapping instanceof OTRelationMapping) {
builder.setName("RelationshipMapping");
LinkedHashMap<String, String> kvproperties = new LinkedHashMap<>();
kvproperties.put("direction", ((OTRelationMapping) mapping).getDirection().value());
kvproperties.put("applicableRelationshipType", ((OTRelationMapping) mapping).getRelationType().toString());
kvproperties.put("validEndpointType", ((OTRelationMapping) mapping).getValidSourceOrTarget().toString());
TEntityTemplate.WineryKVProperties properties = new TEntityTemplate.WineryKVProperties();
properties.setKVProperties(kvproperties);
builder.setProperties(properties);
}
if (mapping instanceof OTStayMapping) {
builder.setName("StayMapping");
}
if (mapping instanceof OTAttributeMapping) {
builder.setName("AttributeMapping");
LinkedHashMap<String, String> kvproperties = new LinkedHashMap<>();
kvproperties.put("type", ((OTAttributeMapping) mapping).getType().value());
kvproperties.put("detectorProperty", ((OTAttributeMapping) mapping).getDetectorProperty());
kvproperties.put("refinementProperty", ((OTAttributeMapping) mapping).getRefinementProperty());
TEntityTemplate.WineryKVProperties properties = new TEntityTemplate.WineryKVProperties();
properties.setKVProperties(kvproperties);
builder.setProperties(properties);
}
if (mapping instanceof OTDeploymentArtifactMapping) {
builder.setName("DeploymentArtifactMapping");
LinkedHashMap<String, String> kvproperties = new LinkedHashMap<>();
kvproperties.put("requiredDeploymentArtifactType", ((OTDeploymentArtifactMapping) mapping).getArtifactType().toString());
TEntityTemplate.WineryKVProperties properties = new TEntityTemplate.WineryKVProperties();
properties.setKVProperties(kvproperties);
builder.setProperties(properties);
}
if (mapping instanceof OTBehaviorPatternMapping) {
builder.setName("BehaviorPatternMapping");
LinkedHashMap<String, String> kvproperties = new LinkedHashMap<>();
kvproperties.put("refinementProperty", ((OTBehaviorPatternMapping) mapping).getProperty().getKey());
kvproperties.put("refinementPropertyValue", ((OTBehaviorPatternMapping) mapping).getProperty().getValue());
kvproperties.put("behaviorPattern", ((OTBehaviorPatternMapping) mapping).getBehaviorPattern());
TEntityTemplate.WineryKVProperties properties = new TEntityTemplate.WineryKVProperties();
properties.setKVProperties(kvproperties);
builder.setProperties(properties);
}
TRelationshipTemplate templateForMapping = new TRelationshipTemplate(builder);
prmModellingTopologyTemplate.addRelationshipTemplate(templateForMapping);
}
}
use of org.eclipse.winery.model.tosca.extensions.OTPermutationMapping 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));
}
Aggregations