Advertisement
kimo12

Untitled

Apr 13th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.53 KB | None | 0 0
  1. package signalFlowGraph;
  2.  
  3. import java.awt.Point;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.Scanner;
  7.  
  8. public class AnalyzeSFG {
  9.  
  10.     private int[][] adjMatrix;
  11.     private int numberOfNodes;
  12.     private String path = new String();
  13.     private int forwardPathGain = 1;
  14.     private ArrayList<String> loopsPath = new ArrayList<>();
  15.     private ArrayList<String> forwardPath = new ArrayList<>();
  16.     private ArrayList<Integer> forwardGains = new ArrayList<>();
  17.     private ArrayList<Integer> loopsGains = new ArrayList<>();
  18.     private ArrayList<Point> searchLoops = new ArrayList<>();
  19.  
  20.     public static void main(String[] args) {
  21.         AnalyzeSFG g = new AnalyzeSFG();
  22.         g.inputs();
  23.         g.printMatrix();
  24.         g.traverseForwardPath(0);
  25.         g.loops();
  26.         g.print();
  27.     }
  28.  
  29.     private void makeAllZeros(int[][] matrix) {
  30.         for (int i = 0; i < matrix.length; i++)
  31.             Arrays.fill(matrix[i], 0);
  32.     }
  33.  
  34.     private void inputs() {
  35.         Scanner scan = new Scanner(System.in);
  36.         System.out.print("Enter The Number Of Nodes : ");
  37.         numberOfNodes = scan.nextInt();
  38.         adjMatrix = new int[numberOfNodes][numberOfNodes];
  39.         makeAllZeros(adjMatrix);
  40.         System.out.println("To Get Out From Entering Signal Flow Graph Representation");
  41.         System.out.println("Put From = 0 ");
  42.         while (true) {
  43.             int from, to, gain;
  44.             System.out.print("From : ");
  45.             from = scan.nextInt() - 1;
  46.             if (from < 0) {
  47.                 break;
  48.             }
  49.             System.out.print("To : ");
  50.             to = scan.nextInt() - 1;
  51.             System.out.print("gain : ");
  52.             gain = scan.nextInt();
  53.             adjMatrix[to][from] = gain;
  54.         }
  55.         addFinalNode();
  56.     }
  57.  
  58.     private void printMatrix() {
  59.         for (int i = 0; i < numberOfNodes; i++) {
  60.             for (int j = 0; j < numberOfNodes; j++) {
  61.                 System.out.print(adjMatrix[i][j] + " ");
  62.             }
  63.             System.out.println();
  64.         }
  65.  
  66.     }
  67.  
  68.     private void print() {
  69.         System.out.println("Forward Pathes");
  70.         for (int i = 0; i < this.forwardGains.size(); i++) {
  71.             System.out.println(this.forwardGains.get(i));
  72.             System.out.println(this.forwardPath.get(i));
  73.         }
  74.         System.out.println("Loops");
  75.         for (int i = 0; i < this.loopsPath.size(); i++) {
  76.             System.out.println(this.loopsPath.get(i));
  77.             System.out.println(this.loopsGains.get(i));
  78.         }
  79.     }
  80.  
  81.     private int[][] copyMatrix(int[][] copyTo, int[][] copyFrom) {
  82.         for (int i = 0; i < copyFrom.length; i++)
  83.             for (int j = 0; j < copyFrom.length; j++)
  84.                 copyTo[i][j] = copyFrom[i][j];
  85.         return copyTo;
  86.     }
  87.  
  88.     private int[] getLoopGainsArray(String gainString) {
  89.         gainString = gainString.replace("x", " ");
  90.         String[] arrayString = gainString.split(" ");
  91.         int[] array = new int[arrayString.length];
  92.         for (int i = 0; i < arrayString.length; i++)
  93.             array[i] = Integer.parseInt(arrayString[i]);
  94.         return array;
  95.     }
  96.  
  97.     private int getSimpleLoopGain(String gainString) {
  98.         int[] array = getLoopGainsArray(gainString);
  99.         int column, row;
  100.         int gain = 1;
  101.         for (int i = 1; i < array.length; i++) {
  102.             column = array[i - 1];
  103.             row = array[i];
  104.             gain *= adjMatrix[row][column];
  105.         }
  106.         return gain;
  107.         // x1x2x3x4x1
  108.     }
  109.  
  110.     private void addFinalNode() {
  111.         boolean addColumn = false;
  112.         for (int i = 0; i < this.adjMatrix.length; i++)
  113.             if (this.adjMatrix[i][this.numberOfNodes - 1] != 0) {
  114.                 addColumn = true;
  115.                 break;
  116.             }
  117.  
  118.         if (addColumn == true) {
  119.             int newDimension = this.numberOfNodes + 1;
  120.             int[][] copy = new int[newDimension][newDimension];
  121.             makeAllZeros(copy);
  122.             copy = copyMatrix(copy, adjMatrix);
  123.             copy[newDimension - 1][newDimension - 2] = 1;
  124.             adjMatrix = copy;
  125.             this.numberOfNodes = newDimension;
  126.         }
  127.     }
  128.  
  129.     private void loops() {
  130.         // searching for self loops
  131.         for (int i = 0; i < numberOfNodes; i++) {
  132.             if (adjMatrix[i][i] != 0) {
  133.                 String selfLoop = "x" + i + "x" + i;
  134.                 loopsPath.add(selfLoop);
  135.                 loopsGains.add(adjMatrix[i][i]);
  136.             }
  137.         }
  138.         // finiding possible loops path
  139.         for (int i = 1; i < numberOfNodes; i++) {
  140.             for (int j = i - 1; j >= 0; j--) {
  141.                 if (adjMatrix[j][i] != 0) {
  142.                     // from i to j (reverse path) j <==== i
  143.                     Point x = new Point(j, i);
  144.                     searchLoops.add(x);
  145.                 }
  146.             }
  147.         }
  148.         // search in the forwarc pathes for path from j to i
  149.         // if found j ===> i then the is a loop j => i => j
  150.         for (int i = 0; i < this.forwardPath.size(); i++) {
  151.             String current = this.forwardPath.get(i);
  152.             for (int j = 0; j < this.searchLoops.size(); j++) {
  153.                 int x = this.searchLoops.get(j).x;
  154.                 String from = "x" + x;
  155.                 if (current.contains(from)) {
  156.                     int y = this.searchLoops.get(j).y;
  157.                     String to = "x" + y;
  158.                     if (current.contains(to)) {
  159.                         // this is a loop we must calculate the gain and print
  160.                         // the loop path
  161.                         int start = current.lastIndexOf(from);
  162.                         int end = current.lastIndexOf(to) + 1;
  163.                         String loop = current.substring(start, end + 1) + current.substring(start, start + 2);
  164.                         // Chech that this loop isn't already taken
  165.                         boolean exist = false;
  166.                         for (int k = 0; k < this.loopsPath.size(); k++) {
  167.                             if (loop.equals(this.loopsPath.get(k))) {
  168.                                 exist = true;
  169.                             }
  170.                         }
  171.                         if (!exist) {
  172.                             loopsPath.add(loop);
  173.                             loopsGains.add(getSimpleLoopGain(loop));
  174.                         }
  175.                     }
  176.                 }
  177.             }
  178.         }
  179.  
  180.     }
  181.  
  182.     private void traverseForwardPath(int columnIndex) {
  183.  
  184.         if (isLastNode(columnIndex)) {
  185.             addToForwardPath(columnIndex, 1);
  186.             forwardPath.add(path);
  187.             forwardGains.add(forwardPathGain);
  188.             path = path.replace("x" + columnIndex, "");
  189.             return;
  190.         }
  191.  
  192.         int rowIndex = -1;
  193.         rowIndex = getNextNode(rowIndex + 1, columnIndex);
  194.         while (rowIndex != -1) {
  195.             if (rowIndex <= columnIndex) {
  196.                 rowIndex = getNextNode(rowIndex + 1, columnIndex);
  197.                 continue;
  198.             }
  199.             addToForwardPath(columnIndex, adjMatrix[rowIndex][columnIndex]);
  200.             traverseForwardPath(rowIndex);
  201.             removeFromForwardPath(columnIndex, adjMatrix[rowIndex][columnIndex]);
  202.             rowIndex = getNextNode(rowIndex + 1, columnIndex);
  203.         }
  204.     }
  205.  
  206.     private void removeFromForwardPath(int currentNode, int gain) {
  207.         String toBeRemoved = "x" + currentNode;
  208.         if (path.contains(toBeRemoved))
  209.             path = path.replace(toBeRemoved, "");
  210.         forwardPathGain /= gain;
  211.  
  212.     }
  213.  
  214.     private void addToForwardPath(int currentNode, int gain) {
  215.         path += "x" + currentNode;
  216.         forwardPathGain *= gain;
  217.     }
  218.  
  219.     private int getNextNode(int rowIndex, int columnIndex) {
  220.         for (; rowIndex < this.numberOfNodes; rowIndex++)
  221.             if (adjMatrix[rowIndex][columnIndex] != 0)
  222.                 return rowIndex;
  223.         return -1;
  224.     }
  225.  
  226.     private boolean isLastNode(int columnIndex) {
  227.         for (int i = 0; i < this.numberOfNodes; i++)
  228.             if (adjMatrix[i][columnIndex] != 0)
  229.                 return false;
  230.         return true;
  231.     }
  232.  
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement