use of org.eclipse.winery.model.tosca.extensions.OTBehaviorPatternMapping 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.tosca.extensions.OTBehaviorPatternMapping 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.OTBehaviorPatternMapping in project winery by eclipse.
the class ToscaBehaviorPatternMatcherTest method propertiesCompatible.
@Test
public void propertiesCompatible() {
LinkedHashMap<String, String> refinementProps = new LinkedHashMap<>();
refinementProps.put("null", null);
refinementProps.put("empty", "");
refinementProps.put("star", "*");
refinementProps.put("match", "this has to match");
refinementProps.put("ignoreCase", "THIS HAS TO MATCH INDEPENDENT OF CASE");
LinkedHashMap<String, String> candidateProps = new LinkedHashMap<>();
candidateProps.put("null", "does not have to be null");
candidateProps.put("empty", "does not have to be empty");
candidateProps.put("star", "this has to be non-null and non-empty");
candidateProps.put("match", "this has to match");
candidateProps.put("ignoreCase", "this has to match independent of case");
TNodeTemplate refinement = new TNodeTemplate();
refinement.setId("refinement");
ModelUtilities.setPropertiesKV(refinement, refinementProps);
ToscaNode refinementNode = new ToscaNode();
refinementNode.setNodeTemplate(refinement);
TNodeTemplate candidate = new TNodeTemplate();
candidate.setId("candidate");
ModelUtilities.setPropertiesKV(candidate, candidateProps);
ToscaNode candidateNode = new ToscaNode();
candidateNode.setNodeTemplate(candidate);
OTPatternRefinementModel prm = new OTPatternRefinementModel();
prm.setRefinementTopology(new TTopologyTemplate(new TTopologyTemplate.Builder().addNodeTemplate(refinement)));
// needs to be swapped manually as only prms retrieved from repo are swapped automatically
PatternDetectionUtils.swapDetectorWithRefinement(prm);
ToscaBehaviorPatternMatcher matcher = new ToscaBehaviorPatternMatcher(prm, null);
assertTrue(matcher.propertiesCompatible(refinementNode, candidateNode));
refinementProps.put("doesNotMatch", "something");
candidateProps.put("doesNotMatch", "something else");
assertFalse(matcher.propertiesCompatible(refinementNode, candidateNode));
// props with behavior pattern mappings can be ignored
List<OTBehaviorPatternMapping> behaviorPatternMappings = new ArrayList<>();
OTBehaviorPatternMapping behaviorPatternMapping = new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder("behaviorPatternMap0").setRefinementElement(refinement).setProperty(new OTPropertyKV("doesNotMatch", "")));
behaviorPatternMappings.add((OTBehaviorPatternMapping) PatternDetectionUtils.swapDetectorWithRefinement(behaviorPatternMapping));
prm.setBehaviorPatternMappings(behaviorPatternMappings);
assertTrue(matcher.propertiesCompatible(refinementNode, candidateNode));
candidateProps.put("empty", "");
assertTrue(matcher.propertiesCompatible(refinementNode, candidateNode));
candidateProps.put("star", null);
assertFalse(matcher.propertiesCompatible(refinementNode, candidateNode));
candidateProps.put("star", "");
assertFalse(matcher.propertiesCompatible(refinementNode, candidateNode));
}
use of org.eclipse.winery.model.tosca.extensions.OTBehaviorPatternMapping 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"));
}
use of org.eclipse.winery.model.tosca.extensions.OTBehaviorPatternMapping 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);
}
}
Aggregations