Storyboard TTC2011HelloWorldCountNumberOfNodes


Simple Graph application classes:


create example graph:


Count nodes in graph direct:

   public void countNodesInJava(Graph graph)
   {
      int noOfNodes = graph.getNodes().size();

      systemout = "Number of nodes: " + noOfNodes;
   }

systemout: Number of nodes: 8

Count nodes in graph per pattern:

   public void countNodesPerPattern(Graph graph)
   {
      graphPO = new GraphPO(graph);

      NodePO nodePO = graphPO.hasNodes();

      int noOfNodes = graphPO.getPattern().allMatches();

      systemout = "Number of nodes: " + noOfNodes;
   }

systemout: Number of nodes: 8

Retrieve set of matching nodes per pattern and count its elements:

   public void countNodesPerNodeSet(Graph graph)
   {
      graphPO = new GraphPO(graph);

      NodeSet allMatches = graphPO
            .hasNodes()
            .allMatches();

      systemout = "Nodes: " + allMatches.getName().concat(", ") + " Number of nodes: " + allMatches.size();      
   }

systemout: Nodes: n1, n2, n3, n4, n5, n6, n7, n8 Number of nodes: 8


Retrieve set of looping edges per pattern and count its elements:

   public void countLoopingEdgesPerPattern(Graph graph)
   {
      edgesPO = new GraphPO(graph)
      .hasEdges();

      NodePO srcPO = edgesPO.hasSrc();

      edgesPO.hasTgt(srcPO);

      EdgeSet loopingEdges = edgesPO.allMatches();

      systemout = "Looping Edges: " + loopingEdges.getName().concat(", ") + " Number of looping edges: " + loopingEdges.size();
   }

systemout: Looping Edges: e16, e17 Number of looping edges: 2

For comparison a Java solution:

   public void countLoopingEdgesInJava(Graph graph)
   {
      String sysout = "Looping Edges: ";

      int noOfLoopingEdges = 0;

      for (Edge edge : graph.getEdges())
      {
         if (edge.getSrc() != null && edge.getSrc() == edge.getTgt())
         {
            noOfLoopingEdges++;

            sysout += edge.getName() + ", ";
         }
      }

      systemout = sysout.substring(0, sysout.length() - 2) + " Number of looping edges: " + noOfLoopingEdges;
   }

systemout: Looping Edges: e16, e17 Number of looping edges: 2


Retrieve isolated nodes and count them:

   public void countIsolatedNodesPerPattern(Graph graph)
   {
      graphPO = new GraphPO(graph);

      NodePO nodePO = graphPO.hasNodes();

      nodePO.startNAC().hasOutEdges().endNAC();

      nodePO.startNAC().hasInEdges().endNAC();

      NodeSet isolatedNodes = nodePO.allMatches();

      systemout = "Isolated nodes: " + isolatedNodes.getName().concat(", ") + " Number of isolated nodes: " + isolatedNodes.size();
   }

systemout: Isolated nodes: n6, n7 Number of isolated nodes: 2

For comparison a Java solution:

   public void countIsolatedNodesInJava(Graph graph)
   {
      systemout = "Isolated nodes: ";

      int noOfIsolatedNodes = 0;

      for (Node isoNode : graph.getNodes())
      {
         if (isoNode.getOutEdges().size() == 0 
               && isoNode.getInEdges().size() == 0)
         {
            noOfIsolatedNodes++;

            systemout += isoNode.getName() + ", ";
         }
      }

      systemout = systemout.substring(0, systemout.length() - 2) + " Number of isolated nodes: " + noOfIsolatedNodes;
   }

systemout: Isolated nodes: n6, n7 Number of isolated nodes: 2


Count circles of three nodes:

   public void countCirclesOfThreeNodesPerPattern(Graph graph)
   {
      graphPO = new GraphPO(graph);

      NodePO firstCircleNodePO = graphPO.hasNodes();

      NodePO secondCircleNodePO = firstCircleNodePO.hasOutEdges().hasTgt();

      NodePO thirdCircleNodePO = secondCircleNodePO.hasOutEdges().hasTgt();

      thirdCircleNodePO.hasOutEdges().hasTgt(firstCircleNodePO);

      graphPO.getPattern().matchIsomorphic();

      int noOfCircles = graphPO.getPattern().allMatches();

      systemout = "Circles found: " + noOfCircles;
   }

systemout: Circles found: 6

If you want to print the matched nodes for each circle, you need to iterate through the matches:

   public void countCirclesOfThreeNodesPerPatternReportMatches(Graph graph)
   {
      graphPO = new GraphPO(graph);

      NodePO firstCircleNodePO = graphPO.hasNodes();

      NodePO secondCircleNodePO = firstCircleNodePO.hasOutEdges().hasTgt();

      NodePO thirdCircleNodePO = secondCircleNodePO.hasOutEdges().hasTgt();

      thirdCircleNodePO.hasOutEdges().hasTgt(firstCircleNodePO);

      graphPO.getPattern().matchIsomorphic();

      systemout = "Circles found: \n";
      int noOfCircles = 0;

      while (graphPO.getPattern().getHasMatch())
      {
         systemout += firstCircleNodePO.getName() + " --> "
               + secondCircleNodePO.getName() + " --> " 
               + thirdCircleNodePO.getName() + " --> \n";

         noOfCircles++; 

         graphPO.getPattern().findNextMatch();
      }

      systemout += "" + noOfCircles + " circles found";
   }

systemout: Circles found: n1 --> n2 --> n3 --> n2 --> n3 --> n1 --> n3 --> n1 --> n2 --> n3 --> n4 --> n5 --> n4 --> n5 --> n3 --> n5 --> n3 --> n4 --> 6 circles found

Just for comparison, a plain java implementation:

   public void countCirclesOfThreeNodesInJava(Graph graph)
   {
      int noOfCircles = 0;

      systemout = "Circles found: \n";

      for (Node firstNode : graph.getNodes())
      {
         for (Edge firstEdge : firstNode.getOutEdges())
         {
            Node secondNode = firstEdge.getTgt();

            if (secondNode != null && secondNode != firstNode)
            {
               for (Edge secondEdge : secondNode.getOutEdges())
               {
                  Node thirdNode = secondEdge.getTgt();

                  if (thirdNode != null && thirdNode != secondNode && thirdNode != firstNode)
                  {
                     for (Edge thirdEdge : thirdNode.getOutEdges())
                     {
                        if (thirdEdge.getTgt() == firstNode)
                        {
                           // found match
                           systemout += firstNode.getName() + " --> "
                                 + secondNode.getName() + " --> " 
                                 + thirdNode.getName() + " --> \n";

                           noOfCircles++;
                        }
                     }
                  }
               }
            }
         }
      }

      systemout += "" + noOfCircles + " circles found";
   }

systemout: Circles found: n1 --> n2 --> n3 --> n2 --> n3 --> n1 --> n3 --> n1 --> n2 --> n3 --> n4 --> n5 --> n4 --> n5 --> n3 --> n5 --> n3 --> n4 --> 6 circles found


Count dangling edges per pattern:

   public void countDanglingEdgesPerPattern(Graph graph)
   {
      edgesPO = new GraphPO(graph).hasEdges();

      edgesPO.startNAC().hasTgt();

      edgesPO.hasSrc().endNAC();

      EdgeSet allMatches = edgesPO.allMatches();

      systemout = "Dangling edges " + allMatches.getName().concat(", ") + " number: " + allMatches.size();
   }

systemout: Dangling edges e18, e19 number: 2

Count dangling edges in Java:

   public void countDanglingEdgesInJava(Graph graph)
   {
      systemout = "Dangling edges "; 

      int noOfMatches = 0;

      for (Edge edge : graph.getEdges())
      {
         if (edge.getSrc() == null
               || edge.getTgt() == null)
         {
            systemout += edge.getName() + ", ";

            noOfMatches++;
         }
      }

      systemout = systemout.substring(0, systemout.length() - 2) + " number: " + noOfMatches;
   }

systemout: Dangling edges e18, e19 number: 2