use of org.eclipse.winery.model.tosca.extensions.OTPermutationMapping 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.OTPermutationMapping in project winery by eclipse.
the class ToscaComponentPatternMatcher method componentPatternsCompatible.
protected boolean componentPatternsCompatible(ToscaEntity left, ToscaEntity right) {
// By convention, the left node is always the element to search in right.
TEntityTemplate detectorElement = left.getTemplate();
TEntityTemplate candidateElement = right.getTemplate();
if (!(detectorElement instanceof TNodeTemplate) || !(candidateElement instanceof TNodeTemplate)) {
return false;
}
if (prm.getPermutationMappings() == null || prm.getPermutationMappings().isEmpty()) {
return false;
}
List<OTPermutationMapping> detectorMappings = prm.getPermutationMappings().stream().filter(mapping -> isOneToOne(mapping, prm) && mapping.getDetectorElement().getId().equals(detectorElement.getId())).collect(Collectors.toList());
if (detectorMappings.isEmpty()) {
return false;
}
TEntityTemplate refinementElement = detectorMappings.get(0).getRefinementElement();
return refinementModels.stream().map(OTPatternRefinementModel.class::cast).filter(otherPrm -> otherPrm.getPermutationMappings() != null && !otherPrm.getPermutationMappings().isEmpty()).anyMatch(otherPrm -> {
List<OTPermutationMapping> matchingDetectorMappings = otherPrm.getPermutationMappings().stream().filter(mapping -> isOneToOne(mapping, otherPrm) && super.isCompatible(mapping.getDetectorElement(), candidateElement) && mapping.getRefinementElement().getType().equals(refinementElement.getType())).collect(Collectors.toList());
if (!matchingDetectorMappings.isEmpty()) {
componentPatternCandidates.get(prm).add(new ComponentPatternCandidate((TNodeTemplate) detectorElement, (TNodeTemplate) refinementElement, otherPrm, (TNodeTemplate) matchingDetectorMappings.get(0).getDetectorElement()));
return true;
}
return false;
});
}
use of org.eclipse.winery.model.tosca.extensions.OTPermutationMapping in project winery by eclipse.
the class PermutationGenerator method generatePermutations.
public Map<String, OTTopologyFragmentRefinementModel> generatePermutations(OTTopologyFragmentRefinementModel refinementModel) {
Map<String, OTTopologyFragmentRefinementModel> permutations = new HashMap<>();
IRepository repository = RepositoryFactory.getRepository();
if (!checkMutability(refinementModel)) {
throw new RuntimeException("The refinement model cannot be permuted!");
}
QName refinementModelQName = new QName(refinementModel.getTargetNamespace(), refinementModel.getName());
DefinitionsChildId refinementModelId = new TopologyFragmentRefinementModelId(refinementModelQName);
if (refinementModel instanceof OTPatternRefinementModel) {
refinementModelId = new PatternRefinementModelId(refinementModelQName);
}
for (OTStringList options : refinementModel.getPermutationOptions()) {
String permutationName = VersionSupport.getNewComponentVersionId(refinementModelId, "permutation-" + String.join("-", options.getValues()).replaceAll("_", "-"));
QName permutationQName = new QName(refinementModel.getTargetNamespace(), permutationName);
DefinitionsChildId permutationModelId = new TopologyFragmentRefinementModelId(permutationQName);
if (refinementModel instanceof OTPatternRefinementModel) {
permutationModelId = new PatternRefinementModelId(permutationQName);
}
try {
// To ensure that the permutationMaps are duplicated correctly, save the permutation first
repository.setElement(refinementModelId, refinementModel);
repository.setElement(permutationModelId, refinementModel);
} catch (IOException e) {
logger.error("Error while creating permutation!", e);
break;
}
OTTopologyFragmentRefinementModel permutation = repository.getElement(permutationModelId);
permutation.setName(permutationName);
permutations.put(permutationName, permutation);
Map<String, String> alreadyAdded = new HashMap<>();
for (String option : options.getValues()) {
permutation.getPermutationMappings().stream().filter(permutationMap -> permutationMap.getDetectorElement().getId().equals(option)).map(OTPrmMapping::getRefinementElement).filter(refinementElement -> refinementElement instanceof TNodeTemplate).map(refinementElement -> (TNodeTemplate) refinementElement).forEach(refinementElement -> {
TNodeTemplate addedDetectorElement = alreadyAdded.containsKey(refinementElement.getId()) ? permutation.getDetector().getNodeTemplate(alreadyAdded.get(refinementElement.getId())) : addNodeFromRefinementStructureToDetector(refinementElement, permutation, alreadyAdded);
// region outgoing relations of the currently permuted refinementElement
ModelUtilities.getOutgoingRelationshipTemplates(permutation.getRefinementStructure(), refinementElement).forEach(relation -> {
// Using the permutation maps defined in the original model as we remove them in the permutation
refinementModel.getPermutationMappings().stream().filter(permutationMap -> permutationMap.getRefinementElement().getId().equals(relation.getTargetElement().getRef().getId())).filter(permutationMap -> permutationMap.getDetectorElement() instanceof TNodeTemplate).forEach(permutationMap -> {
// to be mutated, add it the component and create the relation.
if (permutationMap.getDetectorElement().getId().equals(option) || options.getValues().contains(permutationMap.getDetectorElement().getId())) {
String alreadyAddedElement = alreadyAdded.get(relation.getTargetElement().getRef().getId());
TNodeTemplate target = alreadyAddedElement == null ? addNodeFromRefinementStructureToDetector((TNodeTemplate) relation.getTargetElement().getRef(), permutation, alreadyAdded) : permutation.getDetector().getNodeTemplate(alreadyAddedElement);
ModelUtilities.createRelationshipTemplateAndAddToTopology(addedDetectorElement, target, relation.getType(), permutation.getDetector());
} else if (!options.getValues().contains(permutationMap.getDetectorElement().getId())) {
// Else if the target is part of the detector, add the relation between the
// added element and the detector element.
// No need to check instance of again, as we filter them in line 383.
TNodeTemplate target = (TNodeTemplate) permutationMap.getDetectorElement();
ModelUtilities.createRelationshipTemplateAndAddToTopology(addedDetectorElement, target, relation.getType(), permutation.getDetector());
}
});
});
// region handle ingoing relations in the detector
for (TRelationshipTemplate relation : permutation.getDetector().getRelationshipTemplates()) {
if (relation.getTargetElement().getRef().getId().equals(option)) {
Optional<OTPermutationMapping> relationTarget = permutation.getPermutationMappings().stream().filter(permutationMap -> permutationMap.getDetectorElement().getId().equals(relation.getId())).filter(permutationMap -> permutationMap.getRefinementElement().getId().equals(refinementElement.getId())).findFirst();
long refinementEquivalents = permutation.getPermutationMappings().stream().filter(permutationMap -> permutationMap.getDetectorElement().getId().equals(option)).map(OTPrmMapping::getRefinementElement).distinct().count();
if (relationTarget.isPresent() || refinementEquivalents == 1) {
ModelUtilities.createRelationshipTemplateAndAddToTopology((TNodeTemplate) relation.getSourceElement().getRef(), addedDetectorElement, relation.getType(), permutation.getDetector());
}
}
}
// endregion
});
// region remove permuted
if (permutation.getAttributeMappings() != null) {
permutation.getRelationMappings().removeIf(map -> map.getDetectorElement().getId().equals(option));
}
if (permutation.getAttributeMappings() != null) {
permutation.getAttributeMappings().removeIf(map -> map.getDetectorElement().getId().equals(option));
}
if (permutation.getDeploymentArtifactMappings() != null) {
permutation.getDeploymentArtifactMappings().removeIf(map -> map.getDetectorElement().getId().equals(option));
}
permutation.getPermutationMappings().removeIf(permMap -> permMap.getDetectorElement().getId().equals(option) || permMap.getDetectorElement() instanceof TRelationshipTemplate && (((TRelationshipTemplate) permMap.getDetectorElement()).getSourceElement().getRef().getId().equals(option) || ((TRelationshipTemplate) permMap.getDetectorElement()).getTargetElement().getRef().getId().equals(option)));
permutation.getDetector().getNodeTemplateOrRelationshipTemplate().removeIf(template -> template instanceof TRelationshipTemplate && (((TRelationshipTemplate) template).getSourceElement().getRef().getId().equals(option) || ((TRelationshipTemplate) template).getTargetElement().getRef().getId().equals(option)) || template.getId().equals(option));
// endregion
}
try {
RepositoryFactory.getRepository().setElement(permutationModelId, permutation);
} catch (IOException e) {
logger.error("Error while saving permutation!", e);
break;
}
}
return permutations;
}
use of org.eclipse.winery.model.tosca.extensions.OTPermutationMapping in project winery by eclipse.
the class PermutationHelper method addSomePermutationMappings.
static void addSomePermutationMappings(OTPatternRefinementModel refinementModel) {
OTPermutationMapping pattern2_to_node14 = new OTPermutationMapping.Builder("pm-2--14").setDetectorElement(refinementModel.getDetector().getNodeTemplate("2")).setRefinementElement(refinementModel.getRefinementStructure().getNodeTemplate("14")).build();
OTPermutationMapping pattern3_to_node15 = new OTPermutationMapping.Builder("pm-3--15").setDetectorElement(refinementModel.getDetector().getNodeTemplate("3")).setRefinementElement(refinementModel.getRefinementStructure().getNodeTemplate("15")).build();
OTPermutationMapping pattern3_to_node16 = new OTPermutationMapping.Builder("pm-3--16").setDetectorElement(refinementModel.getDetector().getNodeTemplate("3")).setRefinementElement(refinementModel.getRefinementStructure().getNodeTemplate("16")).build();
ArrayList<OTPermutationMapping> permutationMaps = new ArrayList<>(Arrays.asList(pattern2_to_node14, pattern3_to_node15, pattern3_to_node16));
refinementModel.setPermutationMappings(permutationMaps);
}
use of org.eclipse.winery.model.tosca.extensions.OTPermutationMapping in project winery by eclipse.
the class PermutationHelper method generatePrmWithComplexRelationMaps2.
static OTPatternRefinementModel generatePrmWithComplexRelationMaps2() {
/*
######## ######## ########
# (1) #---------------> # (11) # -----> # (12) #
######## ######## ########
| (2) |______________| (2)
| (2) \/
| ########
| # (13) #
| ########
| | (2)
\/ \/
########---------------> ########
# (2) # # (14) #
######## ########
| (2)
\/
########
# (15) #
########
*/
TTopologyTemplate detector = generateDetectorWithTwoPatterns();
// region refinement structure
TNodeTemplate refinementNode_11 = new TNodeTemplate();
refinementNode_11.setType("{http://ex.org}nodeType_11");
refinementNode_11.setId("11");
TNodeTemplate refinementNode_12 = new TNodeTemplate();
refinementNode_12.setType("{http://ex.org}nodeType_12");
refinementNode_12.setId("12");
TNodeTemplate refinementNode_13 = new TNodeTemplate();
refinementNode_13.setType("{http://ex.org}nodeType_13");
refinementNode_13.setId("13");
TNodeTemplate refinementNode_14 = new TNodeTemplate();
refinementNode_14.setType("{http://ex.org}nodeType_14");
refinementNode_14.setId("14");
TNodeTemplate refinementNode_15 = new TNodeTemplate();
refinementNode_15.setType("{http://ex.org}nodeType_15");
refinementNode_15.setId("15");
TRelationshipTemplate node11_hostedOn_node13 = ModelUtilities.createRelationshipTemplate(refinementNode_11, refinementNode_13, QName.valueOf("{http://ex.org}relType_hostedOn"));
TRelationshipTemplate node12_hostedOn_node13 = ModelUtilities.createRelationshipTemplate(refinementNode_12, refinementNode_13, QName.valueOf("{http://ex.org}relType_hostedOn"));
TRelationshipTemplate node13_hostedOn_node14 = ModelUtilities.createRelationshipTemplate(refinementNode_13, refinementNode_14, QName.valueOf("{http://ex.org}relType_hostedOn"));
TRelationshipTemplate node14_hostedOn_node15 = ModelUtilities.createRelationshipTemplate(refinementNode_14, refinementNode_15, QName.valueOf("{http://ex.org}relType_hostedOn"));
TRelationshipTemplate node11_connectsTo_node12 = ModelUtilities.createRelationshipTemplate(refinementNode_11, refinementNode_12, QName.valueOf("{http://ex.org}relType_connectsTo"));
TTopologyTemplate refinementStructure = new TTopologyTemplate.Builder().addNodeTemplate(refinementNode_11).addNodeTemplate(refinementNode_12).addNodeTemplate(refinementNode_13).addNodeTemplate(refinementNode_14).addNodeTemplate(refinementNode_15).addRelationshipTemplate(node11_hostedOn_node13).addRelationshipTemplate(node12_hostedOn_node13).addRelationshipTemplate(node13_hostedOn_node14).addRelationshipTemplate(node14_hostedOn_node15).addRelationshipTemplate(node11_connectsTo_node12).build();
// endregion
// region mappings
OTPermutationMapping pattern1_to_node11 = new OTPermutationMapping.Builder("pm-1--11").setRefinementElement(refinementNode_11).setDetectorElement(detector.getNodeTemplate("1")).build();
OTPermutationMapping pattern2_to_node14 = new OTPermutationMapping.Builder("pm-2--14").setDetectorElement(detector.getNodeTemplate("2")).setRefinementElement(refinementNode_14).build();
// endregion
// region permutation mapping
List<OTPermutationMapping> list = new ArrayList<>();
list.add(pattern1_to_node11);
list.add(pattern2_to_node14);
return new OTPatternRefinementModel.Builder().setName("PrmWithComplexRelationMappings2").setTargetNamespace("http://ex.org").setDetector(detector).setRefinementStructure(refinementStructure).setPermutationMappings(list).build();
}
Aggregations