use of org.jgrapht.GraphMapping in project winery by eclipse.
the class ComponentPatternDetectionTest method isCompatible.
private boolean isCompatible(OTPatternRefinementModel prm, List<OTRefinementModel> refinementModels) {
ToscaComponentPatternMatcher matcher = new ToscaComponentPatternMatcher(prm, null, refinementModels, new HashMap<>());
ToscaIsomorphismMatcher isomorphismMatcher = new ToscaIsomorphismMatcher();
ToscaGraph detectorGraph = ToscaTransformer.createTOSCAGraph(prm.getDetector());
ToscaGraph topologyGraph = ToscaTransformer.createTOSCAGraph(topology);
Iterator<GraphMapping<ToscaNode, ToscaEdge>> matches = isomorphismMatcher.findMatches(detectorGraph, topologyGraph, matcher);
return matches.hasNext();
}
use of org.jgrapht.GraphMapping in project winery by eclipse.
the class AbstractRefinementTest method setUp.
protected static void setUp() {
// region *** topology ***
TNodeTemplate nt1 = new TNodeTemplate.Builder("1", QName.valueOf("{http://ex.org}nodeType_1")).setX("10").setY("10").build();
TNodeTemplate nt2 = new TNodeTemplate.Builder("2", QName.valueOf("{http://ex.org}nodeType_2")).setX("110").setY("10").build();
TNodeTemplate nt3 = new TNodeTemplate.Builder("3", QName.valueOf("{http://ex.org}nodeType_3")).setX("210").setY("10").build();
TNodeTemplate nt4 = new TNodeTemplate.Builder("4", QName.valueOf("{http://ex.org}nodeType_4")).setX("110").setY("110").build();
TRelationshipTemplate rt21 = new TRelationshipTemplate.Builder("21", QName.valueOf("{http://ex.org}relType_1"), nt2, nt1).build();
TRelationshipTemplate rt32 = new TRelationshipTemplate.Builder("32", QName.valueOf("{http://ex.org}relType_1"), nt3, nt2).build();
TRelationshipTemplate rt24 = new TRelationshipTemplate.Builder("24", QName.valueOf("{http://ex.org}relType_2"), nt2, nt4).build();
/*
####### (1) ####### (1) #######
# (1) # <----- # (2) # <----- # (3) #
####### ####### #######
| (2)
\/
#######
# (4) #
#######
*/
topology = new TTopologyTemplate.Builder().addNodeTemplate(nt1).addNodeTemplate(nt2).addNodeTemplate(nt3).addNodeTemplate(nt4).addRelationshipTemplate(rt21).addRelationshipTemplate(rt32).addRelationshipTemplate(rt24).build();
// endregion
// region *** topology2 ***
/*
####### (1) #######
# (1) # <----- # (2) #
####### #######
| (2)
\/
#######
# (4) #
#######
*/
topology2 = new TTopologyTemplate.Builder().addNodeTemplate(nt1).addNodeTemplate(nt2).addNodeTemplate(nt4).addRelationshipTemplate(rt21).addRelationshipTemplate(rt24).build();
// endregion
// region *** topology 3 ***
/*
####### (1) #######
# (1) # <----- # (2) #
####### #######
| (2)
\/
#######----|
# (4) # DA |
#######----|
*/
QName artifactTypeFile = new QName("file", "http://example.org/tosca/at");
QName test_da = new QName("test_da", "http://example.org/tosca/atemp/das");
TNodeTemplate nt30 = new TNodeTemplate.Builder("30", QName.valueOf("{http://ex.org}nodeType_4")).setX("110").setY("110").build();
TRelationshipTemplate rt230 = new TRelationshipTemplate.Builder("302", QName.valueOf("{http://ex.org}relType_2"), nt2, nt30).build();
TDeploymentArtifact da = new TDeploymentArtifact.Builder("test_da", artifactTypeFile).setArtifactRef(test_da).build();
nt30.setDeploymentArtifacts(new ArrayList<>(Collections.singletonList(da)));
topology3 = new TTopologyTemplate.Builder().addNodeTemplate(nt1).addNodeTemplate(nt2).addNodeTemplate(nt30).addRelationshipTemplate(rt21).addRelationshipTemplate(rt230).build();
// endregion
// region *** topology4 ***
TRelationshipTemplate rt14 = new TRelationshipTemplate.Builder("14", QName.valueOf("{http://ex.org}relType_1"), nt1, nt4).build();
/*
####### (1) #######
# (3) # -----># (2) #
####### #######
| (2)
\/
####### (1) #######
# (1) # -----># (4) #
####### #######
*/
topology4 = new TTopologyTemplate.Builder().addNodeTemplate(nt1).addNodeTemplate(nt2).addNodeTemplate(nt3).addNodeTemplate(nt4).addRelationshipTemplate(rt32).addRelationshipTemplate(rt14).addRelationshipTemplate(rt24).build();
// endregion
// region *** matching PRM ***
// region *** detector ***
TNodeTemplate nt7 = new TNodeTemplate.Builder("7", QName.valueOf("{http://ex.org}nodeType_2")).build();
TNodeTemplate nt8 = new TNodeTemplate.Builder("8", QName.valueOf("{http://ex.org}nodeType_4")).build();
TRelationshipTemplate rt78 = new TRelationshipTemplate.Builder("78", QName.valueOf("{http://ex.org}relType_2"), nt7, nt8).build();
/*
#######
# (2) #
#######
| (2)
\/
#######
# (4) #
#######
*/
TTopologyTemplate detector = new TTopologyTemplate.Builder().addNodeTemplate(nt7).addNodeTemplate(nt8).addRelationshipTemplate(rt78).build();
// endregion
// region *** refinement structure
TNodeTemplate nt10 = new TNodeTemplate.Builder("10", QName.valueOf("{http://ex.org}nodeType_10")).setX("5").setY("10").build();
TNodeTemplate nt11 = new TNodeTemplate.Builder("11", QName.valueOf("{http://ex.org}nodeType_11")).setX("105").setY("5").build();
TNodeTemplate nt12 = new TNodeTemplate.Builder("12", QName.valueOf("{http://ex.org}nodeType_12")).setX("55").setY("105").build();
TNodeTemplate nt13 = new TNodeTemplate.Builder("13", QName.valueOf("{http://ex.org}nodeType_13")).setX("55").setY("205").build();
TRelationshipTemplate rt1012 = new TRelationshipTemplate.Builder("1012", QName.valueOf("{http://ex.org}relType_2"), nt10, nt12).build();
TRelationshipTemplate rt1112 = new TRelationshipTemplate.Builder("1112", QName.valueOf("{http://ex.org}relType_2"), nt11, nt12).build();
TRelationshipTemplate rt1213 = new TRelationshipTemplate.Builder("1213", QName.valueOf("{http://ex.org}relType_2"), nt12, nt13).build();
/*
######## ########
# (10) # # (11) #
######## ########
| (2) | (2)
+-------|------+
\/
########
# (12) #
########
| (2)
\/
########
# (13) #
########
*/
TTopologyTemplate refinementStructure = new TTopologyTemplate.Builder().addNodeTemplate(nt10).addNodeTemplate(nt11).addNodeTemplate(nt12).addNodeTemplate(nt13).addRelationshipTemplate(rt1012).addRelationshipTemplate(rt1112).addRelationshipTemplate(rt1213).build();
// endregion
// region *** relation mapping ***
OTRelationMapping rm1 = new OTRelationMapping(new OTRelationMapping.Builder());
rm1.setDetectorElement(nt7);
rm1.setRelationType(QName.valueOf("{http://ex.org}relType_1"));
rm1.setDirection(OTRelationDirection.INGOING);
rm1.setValidSourceOrTarget(QName.valueOf("{http://ex.org}nodeType_3"));
rm1.setRefinementElement(nt11);
OTRelationMapping rm2 = new OTRelationMapping(new OTRelationMapping.Builder());
rm2.setDetectorElement(nt7);
rm2.setRelationType(QName.valueOf("{http://ex.org}relType_1"));
rm2.setDirection(OTRelationDirection.OUTGOING);
rm2.setValidSourceOrTarget(QName.valueOf("{http://ex.org}nodeType_1"));
rm2.setRefinementElement(nt10);
// endregion
OTPatternRefinementModel prmWithNt2HostedOnNt4AndIngoingRt1AtNt2AndOutgoingRt1AtNt2 = new OTPatternRefinementModel(new OTPatternRefinementModel.Builder());
prmWithNt2HostedOnNt4AndIngoingRt1AtNt2AndOutgoingRt1AtNt2.setDetector(detector);
prmWithNt2HostedOnNt4AndIngoingRt1AtNt2AndOutgoingRt1AtNt2.setRefinementTopology(refinementStructure);
ToscaGraph topologyGraph = ToscaTransformer.createTOSCAGraph(topology);
ToscaGraph detectorGraph = ToscaTransformer.createTOSCAGraph(detector);
ToscaIsomorphismMatcher matcher = new ToscaIsomorphismMatcher();
Iterator<GraphMapping<ToscaNode, ToscaEdge>> mappings = matcher.findMatches(detectorGraph, topologyGraph, new ToscaTypeMatcher());
GraphMapping<ToscaNode, ToscaEdge> detectorWithTopologyMapping = mappings.next();
List<OTRelationMapping> relationMappings = new ArrayList<>();
relationMappings.add(rm1);
relationMappings.add(rm2);
prmWithNt2HostedOnNt4AndIngoingRt1AtNt2AndOutgoingRt1AtNt2.setRelationMappings(relationMappings);
candidateForTopology = new RefinementCandidate(prmWithNt2HostedOnNt4AndIngoingRt1AtNt2AndOutgoingRt1AtNt2, detectorWithTopologyMapping, detectorGraph, 1);
// endregion
// region *** invalidCandidateForTopology **
OTPatternRefinementModel prmWithNt2HostedOnNt4AndOutgoingRt1AtNt2 = new OTPatternRefinementModel(new OTPatternRefinementModel.Builder());
prmWithNt2HostedOnNt4AndOutgoingRt1AtNt2.setDetector(detector);
List<OTRelationMapping> relationMappingsNonMatchingPrm = new ArrayList<>();
relationMappingsNonMatchingPrm.add(rm2);
prmWithNt2HostedOnNt4AndOutgoingRt1AtNt2.setRelationMappings(relationMappingsNonMatchingPrm);
invalidCandidateForTopology = new RefinementCandidate(prmWithNt2HostedOnNt4AndOutgoingRt1AtNt2, detectorWithTopologyMapping, detectorGraph, 2);
// endregion
// region *** Candidates for Topology 2 ***
ToscaGraph topology2Graph = ToscaTransformer.createTOSCAGraph(topology2);
GraphMapping<ToscaNode, ToscaEdge> detectorWithTopology2Mapping = matcher.findMatches(detectorGraph, topology2Graph, new ToscaTypeMatcher()).next();
candidateForTopology2 = new RefinementCandidate(prmWithNt2HostedOnNt4AndIngoingRt1AtNt2AndOutgoingRt1AtNt2, detectorWithTopology2Mapping, detectorGraph, 3);
secondValidCandidateForTopology2 = new RefinementCandidate(prmWithNt2HostedOnNt4AndOutgoingRt1AtNt2, detectorWithTopology2Mapping, detectorGraph, 4);
// endregion
// region *** Candidates for Topology 3 ***
ToscaGraph topologyGraph3 = ToscaTransformer.createTOSCAGraph(topology3);
GraphMapping<ToscaNode, ToscaEdge> detectorToTopology3Mapping = new ToscaIsomorphismMatcher().findMatches(detectorGraph, topologyGraph3, new ToscaTypeMatcher()).next();
OTDeploymentArtifactMapping deploymentArtifactMapping1 = new OTDeploymentArtifactMapping(new OTDeploymentArtifactMapping.Builder());
deploymentArtifactMapping1.setId("daMap-1");
deploymentArtifactMapping1.setArtifactType(artifactTypeFile);
deploymentArtifactMapping1.setDetectorElement(nt8);
deploymentArtifactMapping1.setRefinementElement(nt13);
OTPatternRefinementModel matchingPrmWithDa = new OTPatternRefinementModel(new OTPatternRefinementModel.Builder());
matchingPrmWithDa.setDetector(detector);
matchingPrmWithDa.setRefinementTopology(refinementStructure);
matchingPrmWithDa.setDeploymentArtifactMappings(Collections.singletonList(deploymentArtifactMapping1));
matchingPrmWithDa.setRelationMappings(relationMappings);
candidateForTopology3WithDa = new RefinementCandidate(matchingPrmWithDa, detectorToTopology3Mapping, detectorGraph, 5);
QName artifactTypeZip = new QName("zip", "http://example.org/tosca/at");
OTDeploymentArtifactMapping deploymentArtifactMapping2 = new OTDeploymentArtifactMapping(new OTDeploymentArtifactMapping.Builder());
deploymentArtifactMapping2.setId("daMap-1");
deploymentArtifactMapping2.setArtifactType(artifactTypeZip);
deploymentArtifactMapping2.setDetectorElement(nt7);
deploymentArtifactMapping2.setRefinementElement(nt11);
OTPatternRefinementModel nonMatchingPrmWithDa = new OTPatternRefinementModel(new OTPatternRefinementModel.Builder());
nonMatchingPrmWithDa.setDetector(detector);
nonMatchingPrmWithDa.setRefinementTopology(refinementStructure);
nonMatchingPrmWithDa.setDeploymentArtifactMappings(Collections.singletonList(deploymentArtifactMapping2));
nonMatchingPrmWithDa.setRelationMappings(relationMappings);
candidateForTopology3WithNotMatchingDa = new RefinementCandidate(nonMatchingPrmWithDa, detectorToTopology3Mapping, detectorGraph, 6);
// endregion
// region *** Candidates for Topology 4 ***
ToscaGraph topology4Graph = ToscaTransformer.createTOSCAGraph(topology4);
GraphMapping<ToscaNode, ToscaEdge> detectorWithTopology4Mapping = matcher.findMatches(detectorGraph, topology4Graph, new ToscaTypeMatcher()).next();
OTRelationMapping rm3 = new OTRelationMapping(new OTRelationMapping.Builder());
rm3.setRelationType(QName.valueOf("{http://ex.org}relType_1"));
rm3.setDirection(OTRelationDirection.INGOING);
rm3.setDetectorElement(nt7);
rm3.setRefinementElement(nt11);
OTPatternRefinementModel prmWithNT2HostedOnNT4AndIngoingRT1AtNT2 = new OTPatternRefinementModel(new OTPatternRefinementModel.Builder());
prmWithNT2HostedOnNT4AndIngoingRT1AtNT2.setDetector(detector);
prmWithNT2HostedOnNT4AndIngoingRT1AtNT2.setRefinementTopology(refinementStructure);
prmWithNT2HostedOnNT4AndIngoingRT1AtNT2.setRelationMappings(Collections.singletonList(rm3));
invalidCandidateForTopology4 = new RefinementCandidate(prmWithNT2HostedOnNT4AndIngoingRT1AtNT2, detectorWithTopology4Mapping, detectorGraph, 7);
OTRelationMapping rm4 = new OTRelationMapping(new OTRelationMapping.Builder());
rm4.setRelationType(QName.valueOf("{http://ex.org}relType_1"));
rm4.setDirection(OTRelationDirection.INGOING);
rm4.setDetectorElement(nt8);
rm4.setRefinementElement(nt13);
OTPatternRefinementModel prmWithNT2HostedOnNT4AndIngoingRT1AtNT2AndIngoingRT1AtNT4 = new OTPatternRefinementModel(new OTPatternRefinementModel.Builder());
prmWithNT2HostedOnNT4AndIngoingRT1AtNT2AndIngoingRT1AtNT4.setDetector(detector);
prmWithNT2HostedOnNT4AndIngoingRT1AtNT2AndIngoingRT1AtNT4.setRefinementTopology(refinementStructure);
prmWithNT2HostedOnNT4AndIngoingRT1AtNT2AndIngoingRT1AtNT4.setRelationMappings(Arrays.asList(rm3, rm4));
validCandidateForTopology4 = new RefinementCandidate(prmWithNT2HostedOnNT4AndIngoingRT1AtNT2AndIngoingRT1AtNT4, detectorWithTopology4Mapping, detectorGraph, 8);
// endregion
}
use of org.jgrapht.GraphMapping 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.jgrapht.GraphMapping in project winery by eclipse.
the class TOSCAGraphIsomorphismTest method testTOSCAComplianceRuleChecker.
@Test
public void testTOSCAComplianceRuleChecker() throws IOException, ComplianceCheckingException {
HashMap<DefinitionsChildId, TExtensibleElements> allEntities = new HashMap<>();
TOSCAModelPropertiesBuilder bldr = new TOSCAModelPropertiesBuilder(TEST_TARGET_NAMESPACE, "MyProperties");
bldr.addProperty("key1", "value1");
bldr.addProperty("key2", "value2");
// create NodeTypes A B
String idNodeTypeA = "idA";
TNodeType nodeTypeA = TOSCAModelHelper.createTNodeType(idNodeTypeA, TEST_TARGET_NAMESPACE);
String idNodeTypeB = "idB";
TNodeType nodeTypeB = TOSCAModelHelper.createTNodeType(idNodeTypeB, TEST_TARGET_NAMESPACE);
String idNodeTypeC = "idC";
TNodeType nodeTypeC = TOSCAModelHelper.createTNodeType(idNodeTypeC, TEST_TARGET_NAMESPACE);
NodeTypeId idA = TOSCAModelHelper.createNodeTypeId(idNodeTypeA);
NodeTypeId idB = TOSCAModelHelper.createNodeTypeId(idNodeTypeB);
NodeTypeId idC = TOSCAModelHelper.createNodeTypeId(idNodeTypeC);
allEntities.put(idA, nodeTypeA);
allEntities.put(idB, nodeTypeB);
allEntities.put(idC, nodeTypeC);
// createRelationshipTypes
String relTypeIdAString = "adRelA";
RelationshipTypeId relTypeIdA = new RelationshipTypeId(new QName(TEST_TARGET_NAMESPACE, relTypeIdAString));
TRelationshipType relTypeA = TOSCAModelHelper.createTRelationshipType(relTypeIdAString, TEST_TARGET_NAMESPACE);
allEntities.put(relTypeIdA, relTypeA);
// createNodeTemplates
TNodeTemplate nodeTemplate1 = TOSCAModelHelper.createTNodeTemplate("01");
nodeTemplate1.setType(idA.getQName());
TNodeTemplate nodeTemplate2 = TOSCAModelHelper.createTNodeTemplate("02");
nodeTemplate2.setType(idB.getQName());
TNodeTemplate nodeTemplate3 = TOSCAModelHelper.createTNodeTemplate("03");
nodeTemplate3.setType(idA.getQName());
nodeTemplate3.setProperties(bldr.build());
TNodeTemplate nodeTemplate4 = TOSCAModelHelper.createTNodeTemplate("04");
nodeTemplate4.setType(idB.getQName());
TNodeTemplate nodeTemplate5 = TOSCAModelHelper.createTNodeTemplate("05");
nodeTemplate5.setType(idA.getQName());
TNodeTemplate nodeTemplate6 = TOSCAModelHelper.createTNodeTemplate("06");
nodeTemplate6.setType(idB.getQName());
TNodeTemplate nodeTemplate7 = TOSCAModelHelper.createTNodeTemplate("07");
nodeTemplate7.setType(idA.getQName());
nodeTemplate7.setProperties(bldr.build());
TNodeTemplate nodeTemplate8 = TOSCAModelHelper.createTNodeTemplate("08");
nodeTemplate8.setType(idB.getQName());
TNodeTemplate nodeTemplate9 = TOSCAModelHelper.createTNodeTemplate("CompletelyUnrelated");
nodeTemplate9.setType(idC.getQName());
// create RelationshipTemplates
TRelationshipTemplate relTemplate1 = TOSCAModelHelper.createTRelationshipTemplate("1");
relTemplate1.setSourceNodeTemplate(nodeTemplate1);
relTemplate1.setTargetNodeTemplate(nodeTemplate2);
relTemplate1.setType(relTypeIdA.getQName());
TRelationshipTemplate relTemplate2 = TOSCAModelHelper.createTRelationshipTemplate("2");
relTemplate2.setSourceNodeTemplate(nodeTemplate3);
relTemplate2.setTargetNodeTemplate(nodeTemplate4);
relTemplate2.setType(relTypeIdA.getQName());
TRelationshipTemplate relTemplate3 = TOSCAModelHelper.createTRelationshipTemplate("3");
relTemplate3.setSourceNodeTemplate(nodeTemplate5);
relTemplate3.setTargetNodeTemplate(nodeTemplate6);
relTemplate3.setType(relTypeIdA.getQName());
TRelationshipTemplate relTemplate4 = TOSCAModelHelper.createTRelationshipTemplate("4");
relTemplate4.setSourceNodeTemplate(nodeTemplate7);
relTemplate4.setTargetNodeTemplate(nodeTemplate8);
relTemplate4.setType(relTypeIdA.getQName());
// create TopologyTemplates
List<TNodeTemplate> nodeTemplates = new ArrayList<>();
List<TRelationshipTemplate> relationshipTemplates = new ArrayList<>();
// create identifier
nodeTemplates.add(nodeTemplate1);
nodeTemplates.add(nodeTemplate2);
relationshipTemplates.add(relTemplate1);
TTopologyTemplate identifier = TOSCAModelHelper.createTTopologyTemplate(nodeTemplates, relationshipTemplates);
identifier.setNodeTemplates(nodeTemplates);
identifier.setRelationshipTemplates(relationshipTemplates);
nodeTemplates.clear();
relationshipTemplates.clear();
// create required structure
nodeTemplates.add(nodeTemplate3);
nodeTemplates.add(nodeTemplate4);
relationshipTemplates.add(relTemplate2);
TTopologyTemplate requiredStructure = TOSCAModelHelper.createTTopologyTemplate(nodeTemplates, relationshipTemplates);
requiredStructure.setNodeTemplates(nodeTemplates);
requiredStructure.setRelationshipTemplates(relationshipTemplates);
nodeTemplates.clear();
relationshipTemplates.clear();
// create topologyToSearchIn
nodeTemplates.add(nodeTemplate5);
nodeTemplates.add(nodeTemplate6);
nodeTemplates.add(nodeTemplate7);
nodeTemplates.add(nodeTemplate8);
relationshipTemplates.add(relTemplate3);
relationshipTemplates.add(relTemplate4);
TTopologyTemplate topologyTemplateToSearchIn = TOSCAModelHelper.createTTopologyTemplate(nodeTemplates, relationshipTemplates);
topologyTemplateToSearchIn.setNodeTemplates(nodeTemplates);
topologyTemplateToSearchIn.setRelationshipTemplates(relationshipTemplates);
nodeTemplates.clear();
relationshipTemplates.clear();
// create unrelated topology
nodeTemplates.add(nodeTemplate9);
TTopologyTemplate unrelatedTopology = TOSCAModelHelper.createTTopologyTemplate(nodeTemplates, relationshipTemplates);
unrelatedTopology.setNodeTemplates(nodeTemplates);
unrelatedTopology.setRelationshipTemplates(relationshipTemplates);
nodeTemplates.clear();
relationshipTemplates.clear();
persist(allEntities);
// **************** //
// Test starts here //
// **************** //
ComplianceRuleChecker checker;
// test null topologyTemplateToCheck
checker = new ComplianceRuleChecker(null, null, null);
ComplianceCheckingException expected = null;
try {
checker.checkComplianceRule();
} catch (ComplianceCheckingException e) {
expected = e;
}
assertNotNull(expected);
assertTrue(StringUtils.equals(ComplianceCheckingException.NO_TEMPLATE_TO_CHECK, expected.getMessage()));
checker.setToCheckTemplate(topologyTemplateToSearchIn);
// test empty rule
expected = null;
try {
checker.checkComplianceRule();
} catch (ComplianceCheckingException e) {
expected = e;
}
assertNotNull(expected);
assertTrue(StringUtils.equals(ComplianceCheckingException.EMPTY_COMPLIANCE_RULE, expected.getMessage()));
// test Whitelist
checker.setRequiredStructureTemplate(requiredStructure);
expected = null;
try {
checker.checkComplianceRule();
} catch (ComplianceCheckingException e) {
expected = e;
}
assertNotNull(expected);
assertTrue(StringUtils.equals(ComplianceCheckingException.WHITELISTING_NOT_YET_IMPLEMENTED, expected.getMessage()));
// test blacklist
checker.setRequiredStructureTemplate(null);
checker.setIdentifierTemplate(identifier);
List<GraphMapping> blacklistResult = checker.checkComplianceRule();
assertEquals(2, blacklistResult.size());
// test completeRule
// invalid Rule: identifier and required structure have no mapping
checker.setIdentifierTemplate(unrelatedTopology);
checker.setRequiredStructureTemplate(requiredStructure);
expected = null;
try {
checker.checkComplianceRule();
} catch (ComplianceCheckingException e) {
expected = e;
}
assertNotNull(expected);
assertTrue(StringUtils.equals(ComplianceCheckingException.IDENTIFIER_NOT_IN_REQUIREDSTRUCTURE, expected.getMessage()));
// valid Rule: identifier in required structure
// finds one violation
checker.setIdentifierTemplate(identifier);
checker.setRequiredStructureTemplate(requiredStructure);
List<GraphMapping> violatingMappings = checker.checkComplianceRule();
assertEquals(1, violatingMappings.size());
// check the mapping.
// must contain nodesTemplates 5 & 6 and relTemplate3
TOSCANode identifierNode1 = checker.getIdentifierGraph().getNode("01");
TOSCANode identifierNode2 = checker.getIdentifierGraph().getNode("02");
TOSCAEdge identifierEdge = checker.getIdentifierGraph().getEdge(identifierNode1, identifierNode2);
GraphMapping violatingMapping = violatingMappings.stream().findFirst().get();
assertNotNull(violatingMapping);
Map<TOSCANode, TOSCANode> resultMap = checker.getSubGraphMappingAsMap(violatingMapping, checker.getIdentifierGraph());
assertTrue(resultMap.get(identifierNode1).getId().matches("05"));
assertTrue(resultMap.get(identifierNode2).getId().matches("06"));
assertTrue(((TOSCAEdge) violatingMapping.getEdgeCorrespondence(identifierEdge, false)).getId().matches("3"));
// finds no violation
checker.setRequiredStructureTemplate(identifier);
assertEquals(0, checker.checkComplianceRule().size());
TComplianceRule rule = new TComplianceRule();
rule.setName("test");
rule.setTargetNamespace(TEST_TARGET_NAMESPACE);
rule.setIdentifier(identifier);
rule.setRequiredStructure(requiredStructure);
}
use of org.jgrapht.GraphMapping in project winery by eclipse.
the class ToscaGraphIsomorphismTest method testTOSCADefaultMatcher.
@Test
public void testTOSCADefaultMatcher() {
ToscaGraph queryGraph = new ToscaGraph();
ToscaGraph searchInGraph = new ToscaGraph();
ToscaNode node1 = createTOSCANode("node_01", "A");
ToscaNode node2 = createTOSCANode("node_02", "B");
queryGraph.addVertex(node1);
queryGraph.addVertex(node2);
ToscaEdge edge1 = addEdge(queryGraph, node1, node2, "edge_1", "a");
ToscaNode node3 = createTOSCANode("node_03", "A");
ToscaNode node4 = createTOSCANode("node_04", "A");
ToscaNode node5 = createTOSCANode("node_05", "B");
ToscaNode node6 = createTOSCANode("node_06", "B");
searchInGraph.addVertex(node3);
searchInGraph.addVertex(node4);
searchInGraph.addVertex(node5);
searchInGraph.addVertex(node6);
addEdge(searchInGraph, node3, node4, "edge_2", "b");
addEdge(searchInGraph, node3, node5, "edge_3", "a");
addEdge(searchInGraph, node4, node6, "edge_4", "a");
ToscaIsomorphismMatcher matcher = new ToscaIsomorphismMatcher();
Iterator<GraphMapping<ToscaNode, ToscaEdge>> iterator = matcher.findMatches(queryGraph, searchInGraph, null);
int isomorphismCount = 0;
while (iterator.hasNext()) {
isomorphismCount++;
GraphMapping<ToscaNode, ToscaEdge> mapping = iterator.next();
mapping.getEdgeCorrespondence(edge1, false);
mapping.getVertexCorrespondence(node1, false);
mapping.getVertexCorrespondence(node2, false);
}
assertEquals(2, isomorphismCount);
}
Aggregations