Advertisement
kimo12

Untitled

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