use of org.jgrapht.GraphMapping in project winery by eclipse.
the class BehaviorPatternDetectionTest method removeIncompatibleBehaviorPatterns.
@Test
public void removeIncompatibleBehaviorPatterns() {
List<TPolicy> behaviorPatterns = new ArrayList<>();
behaviorPatterns.add(new TPolicy(new TPolicy.Builder(QName.valueOf("noProp")).setName("noProp")));
behaviorPatterns.add(new TPolicy(new TPolicy.Builder(QName.valueOf("oneProp")).setName("oneProp")));
behaviorPatterns.add(new TPolicy(new TPolicy.Builder(QName.valueOf("multiProps")).setName("multiProps")));
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");
refinementProps.put("multiPropsProp1", "true");
refinementProps.put("multiPropsProp2", "true");
refinementProps.put("multiPropsProp3", "*");
refinementProps.put("multiPropsProp4", "");
refinementProps.put("multiPropsProp5", null);
ModelUtilities.setPropertiesKV(refinementElement, refinementProps);
TNodeTemplate candidateElement = new TNodeTemplate(new TNodeTemplate.Builder("candidateElement", QName.valueOf("{ns}concreteType")).setX("1").setY("1"));
LinkedHashMap<String, String> candidateProps = new LinkedHashMap<>();
candidateProps.put("oneProp", "false");
candidateProps.put("multiPropsProp1", "false");
candidateProps.put("multiPropsProp2", "false");
candidateProps.put("multiPropsProp3", "");
candidateProps.put("multiPropsProp4", null);
candidateProps.put("multiPropsProp5", "");
ModelUtilities.setPropertiesKV(candidateElement, candidateProps);
List<OTBehaviorPatternMapping> behaviorPatternMappings = new ArrayList<>();
behaviorPatternMappings.add(new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder("behaviorPatternMap0").setDetectorElement(detectorElement).setRefinementElement(refinementElement).setBehaviorPattern("oneProp").setProperty(new OTPropertyKV("oneProp", "true"))));
behaviorPatternMappings.add(new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder("behaviorPatternMap1").setDetectorElement(detectorElement).setRefinementElement(refinementElement).setBehaviorPattern("multiProps").setProperty(new OTPropertyKV("multiPropsProp1", "true"))));
behaviorPatternMappings.add(new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder("behaviorPatternMap2").setDetectorElement(detectorElement).setRefinementElement(refinementElement).setBehaviorPattern("multiProps").setProperty(new OTPropertyKV("multiPropsProp2", "true"))));
behaviorPatternMappings.add(new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder("behaviorPatternMap3").setDetectorElement(detectorElement).setRefinementElement(refinementElement).setBehaviorPattern("multiProps").setProperty(new OTPropertyKV("multiPropsProp3", "*"))));
behaviorPatternMappings.add(new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder("behaviorPatternMap4").setDetectorElement(detectorElement).setRefinementElement(refinementElement).setBehaviorPattern("multiProps").setProperty(new OTPropertyKV("multiPropsProp4", ""))));
behaviorPatternMappings.add(new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder("behaviorPatternMap5").setDetectorElement(detectorElement).setRefinementElement(refinementElement).setBehaviorPattern("multiProps").setProperty(new OTPropertyKV("multiPropsProp5", null))));
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).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(candidateElement);
ToscaGraph topologyGraph = ToscaTransformer.createTOSCAGraph(topology);
ToscaGraph detectorGraph = ToscaTransformer.createTOSCAGraph(prm.getDetector());
IToscaMatcher matcher = new ToscaBehaviorPatternMatcher(prm, null);
ToscaIsomorphismMatcher isomorphismMatcher = new ToscaIsomorphismMatcher();
Iterator<GraphMapping<ToscaNode, ToscaEdge>> matches = isomorphismMatcher.findMatches(detectorGraph, topologyGraph, matcher);
RefinementCandidate refinementCandidate = new RefinementCandidate(prm, matches.next(), detectorGraph, 1);
// 'noProp' is applicable independent of properties as no behavior pattern mappings are specified
BehaviorPatternDetection behaviorPatternDetection = new BehaviorPatternDetection(new DefaultRefinementChooser());
behaviorPatternDetection.applyRefinement(refinementCandidate, topology);
assertEquals(topology.getNodeTemplateOrRelationshipTemplate().size(), 1);
List<TPolicy> policies = topology.getNodeTemplates().get(0).getPolicies();
assertEquals(policies.size(), 1);
assertEquals(policies.get(0).getName(), "noProp");
// 'noProp' still applicable, 'oneProp' now applicable as well because property is set
candidateProps.put("oneProp", "true");
topology.getNodeTemplateOrRelationshipTemplate().clear();
topology.addNodeTemplate(candidateElement);
behaviorPatternDetection.applyRefinement(refinementCandidate, topology);
assertEquals(topology.getNodeTemplateOrRelationshipTemplate().size(), 1);
policies = topology.getNodeTemplates().get(0).getPolicies();
assertEquals(policies.size(), 2);
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("noProp")));
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("oneProp")));
// 'noProp' & 'oneProp' still applicable, 'multiProps' not applicable because not all properties are set
candidateProps.put("multiPropsProp1", "true");
topology.getNodeTemplateOrRelationshipTemplate().clear();
topology.addNodeTemplate(candidateElement);
behaviorPatternDetection.applyRefinement(refinementCandidate, topology);
assertEquals(topology.getNodeTemplateOrRelationshipTemplate().size(), 1);
policies = topology.getNodeTemplates().get(0).getPolicies();
assertEquals(policies.size(), 2);
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("noProp")));
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("oneProp")));
// 'noProp' & 'oneProp' still applicable, 'multiProps' not applicable because not all properties are set
candidateProps.put("multiPropsProp2", "true");
topology.getNodeTemplateOrRelationshipTemplate().clear();
topology.addNodeTemplate(candidateElement);
behaviorPatternDetection.applyRefinement(refinementCandidate, topology);
assertEquals(topology.getNodeTemplateOrRelationshipTemplate().size(), 1);
policies = topology.getNodeTemplates().get(0).getPolicies();
assertEquals(policies.size(), 2);
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("noProp")));
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("oneProp")));
// 'noProp' & 'oneProp' & 'multiProps' applicable
candidateProps.put("multiPropsProp3", "non-null and non-empty");
topology.getNodeTemplateOrRelationshipTemplate().clear();
topology.addNodeTemplate(candidateElement);
behaviorPatternDetection.applyRefinement(refinementCandidate, topology);
assertEquals(topology.getNodeTemplateOrRelationshipTemplate().size(), 1);
policies = topology.getNodeTemplates().get(0).getPolicies();
assertEquals(policies.size(), 3);
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("noProp")));
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("oneProp")));
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("multiProps")));
}
use of org.jgrapht.GraphMapping in project winery by eclipse.
the class AbstractRefinement method refineTopology.
public void refineTopology(TTopologyTemplate topology) {
ToscaIsomorphismMatcher isomorphismMatcher = new ToscaIsomorphismMatcher();
int[] id = new int[1];
while (getLoopCondition(topology)) {
ToscaGraph topologyGraph = ToscaTransformer.createTOSCAGraph(topology);
List<RefinementCandidate> candidates = new ArrayList<>();
this.refinementModels.forEach(prm -> {
ToscaGraph detectorGraph = ToscaTransformer.createTOSCAGraph(prm.getDetector());
IToscaMatcher matcher = this.getMatcher(prm);
Iterator<GraphMapping<ToscaNode, ToscaEdge>> matches = isomorphismMatcher.findMatches(detectorGraph, topologyGraph, matcher);
matches.forEachRemaining(mapping -> {
RefinementCandidate candidate = new RefinementCandidate(prm, mapping, detectorGraph, id[0]++);
if (isApplicable(candidate, topology)) {
candidates.add(candidate);
}
});
});
if (candidates.size() == 0) {
break;
}
RefinementCandidate refinement = this.refinementChooser.chooseRefinement(candidates, this.refinementServiceTemplateId, topology);
if (Objects.isNull(refinement)) {
break;
}
applyRefinement(refinement, topology);
}
}
use of org.jgrapht.GraphMapping in project winery by eclipse.
the class TestRefinementTest method applyRefinementTest.
@Test
public void applyRefinementTest() {
// region *** topology ***
TNodeTemplate tomcat = new TNodeTemplate();
tomcat.setId("tomcat");
tomcat.setType("{ns}tomcat");
TNodeTemplate webShop = new TNodeTemplate();
webShop.setId("webShop");
webShop.setType("{ns}webShop");
TNodeTemplate database = new TNodeTemplate();
database.setId("database");
database.setType("{ns}database");
TRelationshipTemplate webShopOnTomcat = new TRelationshipTemplate();
webShopOnTomcat.setSourceNodeTemplate(webShop);
webShopOnTomcat.setTargetNodeTemplate(tomcat);
webShopOnTomcat.setType("{ns}hostedOn");
TRelationshipTemplate webShopToDatabase = new TRelationshipTemplate();
webShopToDatabase.setTargetNodeTemplate(database);
webShopToDatabase.setSourceNodeTemplate(webShop);
webShopToDatabase.setType("{ns}connectsTo");
TTopologyTemplate topologyTemplate = new TTopologyTemplate.Builder().addNodeTemplate(tomcat).addNodeTemplate(webShop).addNodeTemplate(database).addRelationshipTemplate(webShopOnTomcat).addRelationshipTemplate(webShopToDatabase).build();
// endregion
// region *** refinement model ***
TNodeTemplate mySqlConnectorTest = new TNodeTemplate();
mySqlConnectorTest.setId("sqlConnectorTest");
mySqlConnectorTest.setType("{ns}sqlConnectorTest");
TTopologyTemplate refinementTopology = new TTopologyTemplate.Builder().addNodeTemplate(mySqlConnectorTest).build();
OTRelationMapping testHostedOn = new OTRelationMapping(new OTRelationMapping.Builder().setDirection(OTRelationDirection.OUTGOING).setRelationType(QName.valueOf("{ns}hostedOn")).setRefinementElement(mySqlConnectorTest).setDetectorElement(tomcat));
OTRelationMapping testConnectsTo = new OTRelationMapping(new OTRelationMapping.Builder().setDirection(OTRelationDirection.OUTGOING).setRelationType(QName.valueOf("{ns}connectsTo")).setRefinementElement(mySqlConnectorTest).setDetectorElement(database));
OTRelationMapping testIngoingRelationTest = new OTRelationMapping(new OTRelationMapping.Builder().setDirection(OTRelationDirection.INGOING).setRelationType(QName.valueOf("{ns}ingoingTest")).setRefinementElement(mySqlConnectorTest).setDetectorElement(webShop));
List<OTRelationMapping> relationMappings = new ArrayList<>();
relationMappings.add(testHostedOn);
relationMappings.add(testConnectsTo);
relationMappings.add(testIngoingRelationTest);
OTTestRefinementModel testRefinementModel = new OTTestRefinementModel(new OTTestRefinementModel.Builder().setTestFragment(refinementTopology).setDetector(topologyTemplate).setRelationMappings(relationMappings));
// endregion
ToscaGraph topologyGraph = ToscaTransformer.createTOSCAGraph(topologyTemplate);
ToscaGraph detectorGraph = ToscaTransformer.createTOSCAGraph(testRefinementModel.getDetector());
ToscaIsomorphismMatcher matcher = new ToscaIsomorphismMatcher();
Iterator<GraphMapping<ToscaNode, ToscaEdge>> mappings = matcher.findMatches(detectorGraph, topologyGraph, new ToscaTypeMatcher());
GraphMapping<ToscaNode, ToscaEdge> mapping = mappings.next();
RefinementCandidate refinementCandidate = new RefinementCandidate(testRefinementModel, mapping, detectorGraph, 1);
TestRefinement testRefinement = new TestRefinement();
testRefinement.applyRefinement(refinementCandidate, topologyTemplate);
// region *** assertions ***
assertEquals(4, topologyTemplate.getNodeTemplates().size());
assertEquals(5, topologyTemplate.getRelationshipTemplates().size());
TRelationshipTemplate mimicTestHostedOnTomcat = topologyTemplate.getRelationshipTemplate("mimicTest-hostedOn");
assertNotNull(mimicTestHostedOnTomcat);
assertEquals("tomcat", mimicTestHostedOnTomcat.getTargetElement().getRef().getId());
assertEquals("sqlConnectorTest", mimicTestHostedOnTomcat.getSourceElement().getRef().getId());
TRelationshipTemplate mimicTestConnectsToDatabase = topologyTemplate.getRelationshipTemplate("mimicTest-connectsTo");
assertNotNull(mimicTestConnectsToDatabase);
assertEquals("database", mimicTestConnectsToDatabase.getTargetElement().getRef().getId());
assertEquals("sqlConnectorTest", mimicTestConnectsToDatabase.getSourceElement().getRef().getId());
TRelationshipTemplate mimicTestTestIngoingConnection = topologyTemplate.getRelationshipTemplate("mimicTest-ingoingTest");
assertNotNull(mimicTestTestIngoingConnection);
assertEquals("webShop", mimicTestTestIngoingConnection.getSourceElement().getRef().getId());
assertEquals("sqlConnectorTest", mimicTestTestIngoingConnection.getTargetElement().getRef().getId());
// endregion
}
use of org.jgrapht.GraphMapping in project winery by eclipse.
the class ComplianceRuleChecker method findMatches.
public List<GraphMapping> findMatches(ToscaGraph queryGraph, ToscaGraph searchInGraph) {
ToscaIsomorphismMatcher matcher = new ToscaIsomorphismMatcher();
Iterator<GraphMapping<ToscaNode, ToscaEdge>> iterator = matcher.findMatches(queryGraph, searchInGraph, new ToscaComplianceRuleMatcher());
return convertToList(iterator);
}
use of org.jgrapht.GraphMapping in project winery by eclipse.
the class ServiceTemplateComplianceRuleRuleChecker method checkComplianceRules.
public ServiceTemplateCheckingResult checkComplianceRules() {
StringBuilder checkingResult = new StringBuilder("Rulechecking result for servicetemplate " + serviceTemplate.getIdFromIdOrNameField() + System.lineSeparator());
ServiceTemplateCheckingResult result = new ServiceTemplateCheckingResult();
List<ComplianceRuleId> ruleIds = getRuleIds(serviceTemplate);
if (ruleIds.isEmpty()) {
checkingResult.append("No rules defined");
} else {
IRepository repository = RepositoryFactory.getRepository();
for (ComplianceRuleId ruleId : ruleIds) {
OTComplianceRule tComplianceRule = repository.getElement(ruleId);
ComplianceRuleChecker checker = new ComplianceRuleChecker(tComplianceRule, serviceTemplate.getTopologyTemplate());
List<GraphMapping> graphMappings;
try {
graphMappings = checker.checkComplianceRule();
if (graphMappings.size() > 0) {
result.getUnsatisfied().add(ruleId.getQName());
checkingResult.append(ruleId.getQName().toString() + " violated:");
checkingResult.append(System.lineSeparator());
for (GraphMapping mapping : graphMappings) {
Map<ToscaNode, ToscaNode> resultMap = checker.getSubGraphMappingAsMap(mapping, checker.getIdentifierGraph());
checkingResult.append(System.lineSeparator());
checkingResult.append(resultMap.values().stream().map(node -> node.getTemplate().getIdFromIdOrNameField()).collect(Collectors.joining(";", "NodeTemplateIds: ", "")));
}
} else {
result.getSatisfied().add(ruleId.getQName());
checkingResult.append(ruleId.getQName().toString() + " satisfied");
}
} catch (ComplianceCheckingException e) {
result.getException().add(ruleId.getQName());
LOGGER.debug("Could not check compliance at rule " + ruleId.getQName().toString(), e);
}
}
}
LOGGER.debug(checkingResult.toString());
return result;
}
Aggregations