Search in sources :

Example 1 with VF2SubgraphIsomorphismInspector

use of org.jgrapht.alg.isomorphism.VF2SubgraphIsomorphismInspector in project winery by eclipse.

the class Detection method detectPattern.

/**
 * 2. step: Create all subgraphs of the topology graph and test for isomorphism with pattern graphs
 *
 * @param tTopologyTemplate: the TOSCA topology will be labeled
 */
private void detectPattern(TTopologyTemplate tTopologyTemplate) {
    abstractTopology = new AbstractTopology(tTopologyTemplate, labeledNodeTemplates);
    List<TNodeTemplate> tNodeTemplateList = ModelUtilities.getAllNodeTemplates(tTopologyTemplate);
    List<TRelationshipTemplate> tRelationshipTemplateList = ModelUtilities.getAllRelationshipTemplates(tTopologyTemplate);
    getLowestNode(tNodeTemplateList.get(0), tRelationshipTemplateList);
    Set<TNodeTemplateExtended> allNodes = abstractTopology.getGraph().vertexSet();
    TNodeTemplateExtended baseNodeExtended = new TNodeTemplateExtended();
    Iterator iterator = allNodes.iterator();
    // search for the lowest node in the abstract topology graph, this is used to copy the lowest node from the original Topology to the AbstractTopology
    while (iterator.hasNext()) {
        TNodeTemplateExtended temp = (TNodeTemplateExtended) iterator.next();
        if (temp.getNodeTemplate().getId().equals(basisNodeTemplate.getId())) {
            baseNodeExtended = temp;
            break;
        }
    }
    // map the topology outgoing from the given base node
    abstractTopology.map(baseNodeExtended);
    // in the patternList all graphs of the pattern objects are added
    List<DirectedGraph<PatternComponent, RelationshipEdge>> patternList = new ArrayList<>();
    HashMap<Integer, String> patternNames = new HashMap<>();
    // create objects of all known patterns
    ExecutionEnvironmentPattern executionEnvironmentPattern = new ExecutionEnvironmentPattern();
    NodeBasedAvailabilityPattern nodeBasedAvailabilityPattern = new NodeBasedAvailabilityPattern();
    ElasticityManagerPattern elasticityManagerPattern = new ElasticityManagerPattern();
    ElasticLoadBalancerPattern elasticLoadBalancerPattern = new ElasticLoadBalancerPattern();
    ElasticQueuePattern elasticQueuePattern = new ElasticQueuePattern();
    EnvironmentBasedAvailabilityPattern environmentBasedAvailabilityPattern = new EnvironmentBasedAvailabilityPattern();
    MessageOrientedMiddlewarePattern messageOrientedMiddlewarePattern = new MessageOrientedMiddlewarePattern();
    RelationalDatabasePattern relationalDatabasePattern = new RelationalDatabasePattern();
    KeyValueStoragePattern keyValueStoragePattern = new KeyValueStoragePattern();
    ExecutionEnvironmentPattern2 executionEnvironmentPattern2 = new ExecutionEnvironmentPattern2();
    // to receive the right pattern name for the current counter, pattern names are associated with numbers
    patternNames.put(0, patternExecEnv);
    patternNames.put(1, patternNodeBasedAvail);
    patternNames.put(2, patternElasticityManager);
    patternNames.put(3, patternElasticLoadBalancer);
    patternNames.put(4, patternElasticQueue);
    patternNames.put(5, patternEnvBasedAvail);
    patternNames.put(6, patternMessageMiddleware);
    patternNames.put(7, patternRelationalDatabase);
    patternNames.put(8, patternKeyValueStorage);
    patternNames.put(9, patternExecEnv);
    // pattern are added in order
    patternList.add(executionEnvironmentPattern.getPatternGraph());
    patternList.add(nodeBasedAvailabilityPattern.getPatternGraph());
    patternList.add(elasticityManagerPattern.getPatternGraph());
    patternList.add(elasticLoadBalancerPattern.getPatternGraph());
    patternList.add(elasticQueuePattern.getPatternGraph());
    patternList.add(environmentBasedAvailabilityPattern.getPatternGraph());
    patternList.add(messageOrientedMiddlewarePattern.getPatternGraph());
    patternList.add(relationalDatabasePattern.getPatternGraph());
    patternList.add(keyValueStoragePattern.getPatternGraph());
    patternList.add(executionEnvironmentPattern2.getPatternGraph());
    int countIndex = 0;
    // abstractTopology represents the base graph, for each pattern graph search for a subgraph isomorphism between base graph & pattern graph
    for (DirectedGraph<PatternComponent, RelationshipEdge> pattern : patternList) {
        VF2SubgraphIsomorphismInspector<TNodeTemplateExtended, RelationshipEdge> inspector = new VF2SubgraphIsomorphismInspector(abstractTopology.getGraph(), pattern);
        if (inspector.isomorphismExists()) {
            Iterator it = inspector.getMappings();
            while (it.hasNext()) {
                IsomorphicGraphMapping mapping = (IsomorphicGraphMapping) it.next();
                // list for counting all matches between pattern nodes and base graph nodes, must be true for all
                List<Boolean> matched = new ArrayList<>();
                // this graph holds the nodes of the base graph in which the pattern occurs
                DirectedGraph<TNodeTemplateExtended, RelationshipEdge> originGraph = new SimpleDirectedGraph<>(RelationshipEdge.class);
                // each node of the pattern graph is compared to the according node in the GraphMapping
                for (PatternComponent p : pattern.vertexSet()) {
                    // check if matched subgraph and topology have the same components, get the correspondent vertex in the mapping for a node
                    TNodeTemplateExtended v = (TNodeTemplateExtended) mapping.getVertexCorrespondence(p, false);
                    // if the names equal, the node is added to the originGraph and a boolean with value true is added to the matched list
                    if (p.getName().equals(v.getLabel())) {
                        matched.add(true);
                        originGraph.addVertex(v);
                    } else {
                        matched.add(false);
                    }
                }
                // correspondent to the countIndex, the pattern name is retrieved
                if (!matched.contains(false) && !impossiblePattern.contains(patternNames.get(countIndex))) {
                    // add a new pattern position: the pattern name & the subgraph in which it occurs, this graph was built up in the previous step
                    PatternPosition temp = new PatternPosition(patternNames.get(countIndex), originGraph);
                    patternPositions.add(temp);
                    detectedPattern.add(patternNames.get(countIndex));
                    // sett some additional probabilities
                    if (patternNames.get(countIndex).equals(patternEnvBasedAvail)) {
                        patternProbabilityHigh.add(patternPublicCloud);
                    } else if (patternNames.get(countIndex).equals(patternEnvBasedAvail)) {
                        impossiblePattern.add(patternNodeBasedAvail);
                    } else if (patternNames.get(countIndex).equals(patternNodeBasedAvail)) {
                        impossiblePattern.add(patternEnvBasedAvail);
                    }
                }
            }
        }
        countIndex++;
    }
}
Also used : IsomorphicGraphMapping(org.jgrapht.alg.isomorphism.IsomorphicGraphMapping) DirectedGraph(org.jgrapht.DirectedGraph) SimpleDirectedGraph(org.jgrapht.graph.SimpleDirectedGraph) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) VF2SubgraphIsomorphismInspector(org.jgrapht.alg.isomorphism.VF2SubgraphIsomorphismInspector) SimpleDirectedGraph(org.jgrapht.graph.SimpleDirectedGraph)

Aggregations

TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)1 TRelationshipTemplate (org.eclipse.winery.model.tosca.TRelationshipTemplate)1 DirectedGraph (org.jgrapht.DirectedGraph)1 IsomorphicGraphMapping (org.jgrapht.alg.isomorphism.IsomorphicGraphMapping)1 VF2SubgraphIsomorphismInspector (org.jgrapht.alg.isomorphism.VF2SubgraphIsomorphismInspector)1 SimpleDirectedGraph (org.jgrapht.graph.SimpleDirectedGraph)1