use of org.eclipse.winery.model.tosca.extensions.OTTopologyFragmentRefinementModel in project winery by eclipse.
the class PermutationGenerator method checkMutability.
public boolean checkMutability(OTTopologyFragmentRefinementModel refinementModel) {
logger.info("Starting mutability check of {}", refinementModel.getIdFromIdOrNameField());
this.mutabilityErrorReason = "";
List<TNodeTemplate> detectorNodeTemplates = refinementModel.getDetector().getNodeTemplates();
Set<TNodeTemplate> mutableNodes = detectorNodeTemplates.stream().filter(nodeTemplate -> !isStayPlaceholder(nodeTemplate, refinementModel)).collect(Collectors.toSet());
List<OTStringList> permutationOptions = new ArrayList<>();
refinementModel.setPermutationOptions(permutationOptions);
Sets.powerSet(mutableNodes).stream().filter(set -> !(set.size() == 0 || set.size() == mutableNodes.size())).forEach(permutation -> permutationOptions.add(new OTStringList(permutation.stream().map(HasId::getId).collect(Collectors.toList()))));
refinementModel.setComponentSets(new ArrayList<>());
for (TNodeTemplate detectorNode : detectorNodeTemplates) {
getAllMappingsForDetectorNode(detectorNode, refinementModel).stream().filter(mapping -> mapping.getRefinementElement() instanceof TNodeTemplate).map(mapping -> (TNodeTemplate) mapping.getRefinementElement()).forEach(refinementNode -> this.checkComponentMutability(refinementNode, detectorNode, refinementModel));
ModelUtilities.getIncomingRelationshipTemplates(refinementModel.getDetector(), detectorNode).stream().filter(relation -> refinementModel.getComponentSets().stream().noneMatch(setList -> setList.getValues().contains(relation.getTargetElement().getRef().getId()) && setList.getValues().contains(relation.getSourceElement().getRef().getId()))).forEach(relation -> {
TNodeTemplate dependantNode = (TNodeTemplate) relation.getSourceElement().getRef();
if (refinementModel.getRelationMappings() != null) {
refinementModel.getRelationMappings().stream().filter(relMap -> relMap.getDetectorElement().equals(dependantNode)).filter(relMap -> RefinementUtils.canRedirectRelation(relMap, relation, this.relationshipTypes, this.nodeTypes)).findFirst().ifPresent(relMap -> addMutabilityMapping(relMap.getDetectorElement(), relMap.getRefinementElement(), refinementModel));
}
});
}
if (refinementModel.getPermutationMappings() == null) {
this.mutabilityErrorReason = "No permutation mappings could be identified";
logger.info(this.errorMessage, this.mutabilityErrorReason);
return false;
}
List<String> unmappedDetectorNodes = refinementModel.getDetector().getNodeTemplates().stream().filter(detectorNode -> !isStayPlaceholder(detectorNode, refinementModel)).filter(detectorNode -> !permutabilityMappingExistsForDetectorElement(detectorNode, refinementModel)).map(HasId::getId).collect(Collectors.toList());
if (unmappedDetectorNodes.size() > 0) {
this.mutabilityErrorReason = "There are detector nodes which could not be mapped to a refinement node: " + String.join(", ", unmappedDetectorNodes);
logger.info(this.errorMessage, this.mutabilityErrorReason);
return false;
}
List<String> unmappedRefinementNodes = refinementModel.getRefinementStructure().getNodeTemplates().stream().filter(refinementNode -> !isStayingRefinementElement(refinementNode, refinementModel)).filter(refinementNode -> !permutabilityMappingExistsForRefinementNode(refinementNode, refinementModel)).map(HasId::getId).collect(Collectors.toList());
if (unmappedRefinementNodes.size() > 0) {
this.mutabilityErrorReason = "There are refinement nodes which could not be mapped to a detector node: " + String.join(", ", unmappedRefinementNodes);
logger.info(this.errorMessage, this.mutabilityErrorReason);
return false;
}
List<String> unmappableRelationIds = chekMutabilityOfDetectorRelations(refinementModel);
if (unmappableRelationIds.size() > 0) {
this.mutabilityErrorReason = "There are relations that cannot be redirected during the generation: " + String.join(", ", unmappableRelationIds);
logger.info(this.errorMessage, this.mutabilityErrorReason);
return false;
}
return true;
}
use of org.eclipse.winery.model.tosca.extensions.OTTopologyFragmentRefinementModel 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.OTTopologyFragmentRefinementModel 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;
}
use of org.eclipse.winery.model.tosca.extensions.OTTopologyFragmentRefinementModel in project winery by eclipse.
the class TopologyFragmentRefinement method applyDeploymentArtifactMapping.
private boolean applyDeploymentArtifactMapping(RefinementCandidate refinement, TNodeTemplate detectorNode, TNodeTemplate matchingNode, TTopologyTemplate topology, Map<String, String> idMapping) {
List<OTDeploymentArtifactMapping> deploymentArtifactMappings = ((OTTopologyFragmentRefinementModel) refinement.getRefinementModel()).getDeploymentArtifactMappings();
return matchingNode.getDeploymentArtifacts() == null || matchingNode.getDeploymentArtifacts().isEmpty() || (deploymentArtifactMappings != null && matchingNode.getDeploymentArtifacts().stream().filter(deploymentArtifact -> deploymentArtifact.getArtifactType() != null).allMatch(deploymentArtifact -> deploymentArtifactMappings.stream().filter(mapping -> mapping.getDetectorElement().getId().equals(detectorNode.getId())).anyMatch(mapping -> {
if (ModelUtilities.isOfType(mapping.getArtifactType(), deploymentArtifact.getArtifactType(), this.artifactTypes)) {
if (idMapping != null) {
TNodeTemplate addedNode = topology.getNodeTemplate(idMapping.get(mapping.getRefinementElement().getId()));
if (addedNode != null) {
List<TDeploymentArtifact> existingDAs = addedNode.getDeploymentArtifacts();
if (existingDAs == null) {
existingDAs = new ArrayList<>();
addedNode.setDeploymentArtifacts(existingDAs);
} else if (existingDAs.stream().anyMatch(da -> da.getName().equals(deploymentArtifact.getName()))) {
deploymentArtifact.setName(deploymentArtifact.getName() + UUID.randomUUID());
}
existingDAs.add(deploymentArtifact);
} else {
LOGGER.error("Error while adding Deployment Artifacts! Node was not added to the topology!");
}
}
return true;
}
return false;
})));
}
use of org.eclipse.winery.model.tosca.extensions.OTTopologyFragmentRefinementModel in project winery by eclipse.
the class PermutationGeneratorTestWithGitBackedRepository method prmWithTwoHostedOnOneTest.
@Test
void prmWithTwoHostedOnOneTest() throws Exception {
this.setRevisionTo("origin/plain");
OTPatternRefinementModel refinementModel = generatePrmWithTwoPatternsHostedOnAThird();
PatternRefinementModelId id = new PatternRefinementModelId(refinementModel.getTargetNamespace(), refinementModel.getIdFromIdOrNameField(), false);
RepositoryFactory.getRepository().setElement(id, refinementModel);
PermutationGenerator generator = new PermutationGenerator();
Map<String, OTTopologyFragmentRefinementModel> permutations = generator.generatePermutations(refinementModel);
assertEquals(6, permutations.size());
OTTopologyFragmentRefinementModel permutation_1 = permutations.get("PrmWithComplexRelationMappings_permutation-1-w1-wip1");
assertNotNull(permutation_1);
assertEquals(3, permutation_1.getDetector().getNodeTemplates().size());
assertEquals(3, permutation_1.getDetector().getRelationshipTemplates().size());
assertTrue(permutation_1.getDetector().getRelationshipTemplates().removeIf(relation -> relation.getSourceElement().getRef().getId().equals("11") && relation.getTargetElement().getRef().getId().equals("2")));
assertTrue(permutation_1.getDetector().getRelationshipTemplates().removeIf(relation -> relation.getSourceElement().getRef().getId().equals("11") && relation.getTargetElement().getRef().getId().equals("3")));
OTTopologyFragmentRefinementModel permutation_2 = permutations.get("PrmWithComplexRelationMappings_permutation-2-w1-wip1");
assertNotNull(permutation_2);
assertEquals(4, permutation_2.getDetector().getNodeTemplates().size());
assertEquals(4, permutation_2.getDetector().getRelationshipTemplates().size());
assertTrue(permutation_2.getDetector().getRelationshipTemplates().removeIf(relation -> relation.getSourceElement().getRef().getId().equals("1") && relation.getTargetElement().getRef().getId().equals("13")));
assertTrue(permutation_2.getDetector().getRelationshipTemplates().removeIf(relation -> relation.getSourceElement().getRef().getId().equals("3") && relation.getTargetElement().getRef().getId().equals("13")));
OTTopologyFragmentRefinementModel permutation_3 = permutations.get("PrmWithComplexRelationMappings_permutation-3-w1-wip1");
assertNotNull(permutation_3);
assertEquals(3, permutation_3.getDetector().getNodeTemplates().size());
assertEquals(3, permutation_3.getDetector().getRelationshipTemplates().size());
assertTrue(permutation_3.getDetector().getRelationshipTemplates().removeIf(relation -> relation.getSourceElement().getRef().getId().equals("12") && relation.getTargetElement().getRef().getId().equals("2")));
assertTrue(permutation_3.getDetector().getRelationshipTemplates().removeIf(relation -> relation.getSourceElement().getRef().getId().equals("1") && relation.getTargetElement().getRef().getId().equals("12")));
OTTopologyFragmentRefinementModel permutation_1_2 = permutations.get("PrmWithComplexRelationMappings_permutation-1-2-w1-wip1");
assertNotNull(permutation_1_2);
assertEquals(4, permutation_1_2.getDetector().getNodeTemplates().size());
assertEquals(4, permutation_1_2.getDetector().getRelationshipTemplates().size());
assertTrue(permutation_1_2.getDetector().getRelationshipTemplates().removeIf(relation -> relation.getSourceElement().getRef().getId().equals("11") && relation.getTargetElement().getRef().getId().equals("13")));
assertTrue(permutation_1_2.getDetector().getRelationshipTemplates().removeIf(relation -> relation.getSourceElement().getRef().getId().equals("11") && relation.getTargetElement().getRef().getId().equals("3")));
assertTrue(permutation_1_2.getDetector().getRelationshipTemplates().removeIf(relation -> relation.getSourceElement().getRef().getId().equals("3") && relation.getTargetElement().getRef().getId().equals("13")));
OTTopologyFragmentRefinementModel permutation_1_3 = permutations.get("PrmWithComplexRelationMappings_permutation-1-3-w1-wip1");
assertNotNull(permutation_1_3);
assertEquals(3, permutation_1_3.getDetector().getNodeTemplates().size());
assertEquals(3, permutation_1_3.getDetector().getRelationshipTemplates().size());
assertTrue(permutation_1_3.getDetector().getRelationshipTemplates().removeIf(relation -> relation.getSourceElement().getRef().getId().equals("11") && relation.getTargetElement().getRef().getId().equals("2")));
assertTrue(permutation_1_3.getDetector().getRelationshipTemplates().removeIf(relation -> relation.getSourceElement().getRef().getId().equals("11") && relation.getTargetElement().getRef().getId().equals("12")));
assertTrue(permutation_1_3.getDetector().getRelationshipTemplates().removeIf(relation -> relation.getSourceElement().getRef().getId().equals("12") && relation.getTargetElement().getRef().getId().equals("2")));
OTTopologyFragmentRefinementModel permutation_2_3 = permutations.get("PrmWithComplexRelationMappings_permutation-2-3-w1-wip1");
assertNotNull(permutation_2_3);
assertEquals(4, permutation_2_3.getDetector().getNodeTemplates().size());
assertEquals(4, permutation_2_3.getDetector().getRelationshipTemplates().size());
assertTrue(permutation_2_3.getDetector().getRelationshipTemplates().removeIf(relation -> relation.getSourceElement().getRef().getId().equals("1") && relation.getTargetElement().getRef().getId().equals("12")));
assertTrue(permutation_2_3.getDetector().getRelationshipTemplates().removeIf(relation -> relation.getSourceElement().getRef().getId().equals("1") && relation.getTargetElement().getRef().getId().equals("13")));
assertTrue(permutation_2_3.getDetector().getRelationshipTemplates().removeIf(relation -> relation.getSourceElement().getRef().getId().equals("12") && relation.getTargetElement().getRef().getId().equals("13")));
}
Aggregations