PermutationGenerator { 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) .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; } PermutationGenerator(); boolean checkMutability(OTTopologyFragmentRefinementModel refinementModel); Map<String, OTTopologyFragmentRefinementModel> generatePermutations(OTTopologyFragmentRefinementModel refinementModel); String getMutabilityErrorReason();  }
@Test void checkSimpleMutabilityTest() { setUp(); PermutationGenerator permutationGenerator = new PermutationGenerator(); OTTopologyFragmentRefinementModel refinementModel = (OTTopologyFragmentRefinementModel) candidateForTopology3WithDa.getRefinementModel(); assertTrue(permutationGenerator.checkMutability(refinementModel)); List<OTStringList> options = refinementModel.getPermutationOptions(); assertNotNull(options); assertEquals(2, options.size()); options.forEach(permutationOption -> assertEquals(1, permutationOption.getValues().size())); }
@Test void checkSimplePrmMutability() { OTPatternRefinementModel refinementModel = generatePrmWithoutPermutationMaps(); PermutationGenerator permutationGenerator = new PermutationGenerator(); assertTrue(permutationGenerator.checkMutability(refinementModel)); assertEquals(6, refinementModel.getPermutationOptions().size()); assertEquals(7, refinementModel.getPermutationMappings().size()); }
@Test void checkPrmWithStayingMutability() { OTPatternRefinementModel refinementModel = generatePrmWithStayMapping(); PermutationGenerator permutationGenerator = new PermutationGenerator(); assertTrue(permutationGenerator.checkMutability(refinementModel)); assertEquals(2, refinementModel.getPermutationOptions().size()); assertEquals(5, refinementModel.getPermutationMappings().size()); }
@Test void checkMutabilityOfPrmWithAutomaticallyDeterminableComplexRelationMapping() { OTPatternRefinementModel refinementModel = generatePrmWithComplexRelationMaps(); PermutationGenerator permutationGenerator = new PermutationGenerator(); assertTrue(permutationGenerator.checkMutability(refinementModel)); assertEquals(2, refinementModel.getPermutationOptions().size()); List<OTPermutationMapping> mappings = refinementModel.getPermutationMappings(); assertEquals(5, mappings.size()); assertTrue(refinementModel.getPermutationMappings().removeIf(permutationMap -> permutationMap.getDetectorElement().getId().equals("1") && permutationMap.getRefinementElement().getId().equals("11") )); assertTrue(refinementModel.getPermutationMappings().removeIf(permutationMap -> permutationMap.getDetectorElement().getId().equals("1") && permutationMap.getRefinementElement().getId().equals("12") )); assertTrue(refinementModel.getPermutationMappings().removeIf(permutationMap -> permutationMap.getDetectorElement().getId().equals("2") && permutationMap.getRefinementElement().getId().equals("13") )); assertTrue(refinementModel.getPermutationMappings().removeIf(permutationMap -> permutationMap.getDetectorElement().getId().equals("2") && permutationMap.getRefinementElement().getId().equals("14") )); assertTrue(refinementModel.getPermutationMappings().removeIf(permutationMap -> permutationMap.getDetectorElement().getId().equals("1--2") && permutationMap.getRefinementElement().getId().equals("13") )); }
@Test void checkMutabilityOfPrmWithAutomaticallyDeterminableComplexRelationMapping2() { OTPatternRefinementModel refinementModel = generatePrmWithComplexRelationMaps2(); PermutationGenerator permutationGenerator = new PermutationGenerator(); assertTrue(permutationGenerator.checkMutability(refinementModel)); List<OTPermutationMapping> mappings = refinementModel.getPermutationMappings(); assertEquals(6, mappings.size()); assertTrue(refinementModel.getPermutationMappings().removeIf(permutationMap -> permutationMap.getDetectorElement().getId().equals("1") && permutationMap.getRefinementElement().getId().equals("11") )); assertTrue(refinementModel.getPermutationMappings().removeIf(permutationMap -> permutationMap.getDetectorElement().getId().equals("1") && permutationMap.getRefinementElement().getId().equals("12") )); assertTrue(refinementModel.getPermutationMappings().removeIf(permutationMap -> permutationMap.getDetectorElement().getId().equals("1") && permutationMap.getRefinementElement().getId().equals("13") )); assertTrue(refinementModel.getPermutationMappings().removeIf(permutationMap -> permutationMap.getDetectorElement().getId().equals("2") && permutationMap.getRefinementElement().getId().equals("14") )); assertTrue(refinementModel.getPermutationMappings().removeIf(permutationMap -> permutationMap.getDetectorElement().getId().equals("2") && permutationMap.getRefinementElement().getId().equals("15") )); assertTrue(refinementModel.getPermutationMappings().removeIf(permutationMap -> permutationMap.getDetectorElement().getId().equals("1--2") && permutationMap.getRefinementElement().getId().equals("14") )); }
@Test void checkMutabilityOfPrmWithTwoPatternsHostedOnOne() { OTPatternRefinementModel refinementModel = generatePrmWithTwoPatternsHostedOnAThird(); PermutationGenerator permutationGenerator = new PermutationGenerator(); assertTrue(permutationGenerator.checkMutability(refinementModel)); List<OTPermutationMapping> permutationMappings = refinementModel.getPermutationMappings(); assertEquals(7, permutationMappings.size()); assertTrue(permutationMappings.removeIf(mapping -> mapping.getDetectorElement().getId().equals("1") && mapping.getRefinementElement().getId().equals("11"))); assertTrue(permutationMappings.removeIf(mapping -> mapping.getDetectorElement().getId().equals("3") && mapping.getRefinementElement().getId().equals("12"))); assertTrue(permutationMappings.removeIf(mapping -> mapping.getDetectorElement().getId().equals("2") && mapping.getRefinementElement().getId().equals("13"))); assertTrue(permutationMappings.removeIf(mapping -> mapping.getDetectorElement().getId().equals("2") && mapping.getRefinementElement().getId().equals("14"))); assertTrue(permutationMappings.removeIf(mapping -> mapping.getDetectorElement().getId().equals("1--3") && mapping.getRefinementElement().getId().equals("12"))); assertTrue(permutationMappings.removeIf(mapping -> mapping.getDetectorElement().getId().equals("1--2") && mapping.getRefinementElement().getId().equals("13"))); assertTrue(permutationMappings.removeIf(mapping -> mapping.getDetectorElement().getId().equals("3--2") && mapping.getRefinementElement().getId().equals("13"))); }