Advertisement
kimo12

Untitled

Apr 13th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.55 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 inputs() {
  31.         Scanner scan = new Scanner(System.in);
  32.         System.out.print("Enter The Number Of Nodes : ");
  33.         numberOfNodes = scan.nextInt();
  34.         adjMatrix = new int[numberOfNodes][numberOfNodes];
  35.         for (int i = 0; i < this.numberOfNodes; i++)
  36.             Arrays.fill(adjMatrix[i], 0);
  37.         System.out.println("To Get Out From Entering Signal Flow Graph Representation");
  38.         System.out.println("Put From = 0 ");
  39.         while (true) {
  40.             int from, to, gain;
  41.             System.out.print("From : ");
  42.             from = scan.nextInt() - 1;
  43.             if (from < 0) {
  44.                 break;
  45.             }
  46.             System.out.print("To : ");
  47.             to = scan.nextInt() - 1;
  48.             System.out.print("gain : ");
  49.             gain = scan.nextInt();
  50.             adjMatrix[to][from] = gain;
  51.         }
  52.     }
  53.  
  54.     private void printMatrix() {
  55.         for (int i = 0; i < numberOfNodes; i++) {
  56.             for (int j = 0; j < numberOfNodes; j++) {
  57.                 System.out.print(adjMatrix[i][j] + " ");
  58.             }
  59.             System.out.println();
  60.         }
  61.  
  62.     }
  63.  
  64.     private void print() {
  65.         for (int i = 0; i < this.forwardGains.size(); i++) {
  66.             System.out.println(this.forwardGains.get(i));
  67.             System.out.println(this.forwardPath.get(i));
  68.         }
  69.         for (int i = 0; i < this.loopsPath.size(); i++) {
  70.             System.out.println(this.loopsPath.get(i));
  71.         }
  72.     }
  73.  
  74.     private void loops() {
  75.         // searching for self loops
  76.         for (int i = 0; i < numberOfNodes; i++) {
  77.             if (adjMatrix[i][i] != 0) {
  78.                 String selfLoop = "x" + i + "x" + i;
  79.                 loopsPath.add(selfLoop);
  80.                 loopsGains.add(adjMatrix[i][i]);
  81.             }
  82.         }
  83.         // finiding possible loops path
  84.         for (int i = 1; i < numberOfNodes; i++) {
  85.             for (int j = i - 1; j >= 0; j--) {
  86.                 if (adjMatrix[j][i] != 0) {
  87.                     // from i to j (reverse path) j <==== i
  88.                     Point x = new Point(j, i);
  89.                     searchLoops.add(x);
  90.                 }
  91.             }
  92.         }
  93.         // search in the forwarc pathes for path from j to i
  94.         // if found j ===> i then the is a loop j => i => j
  95.         for (int i = 0; i < this.forwardPath.size(); i++) {
  96.             String current = this.forwardPath.get(i);
  97.             for (int j = 0; j < this.searchLoops.size(); j++) {
  98.                 int x = this.searchLoops.get(j).x;
  99.                 String from = "x" + x;
  100.                 if (current.contains(from)) {
  101.                     int y = this.searchLoops.get(j).y;
  102.                     String to = "x" + y;
  103.                     if (current.contains(to)) {
  104.                         // this is a loop we must calculate the gain and print
  105.                         // the loop path
  106.                         int start = current.lastIndexOf(from) - 1;
  107.                         int end = current.lastIndexOf(to);
  108.                         String loop = current.substring(start, end) + current.substring(start, start + 1);
  109.                         loopsPath.add(loop);
  110.                     }
  111.                 }
  112.             }
  113.         }
  114.  
  115.     }
  116.  
  117.     private void traverseForwardPath(int columnIndex) {
  118.  
  119.         if (isLastNode(columnIndex)) {
  120.             addToForwardPath(columnIndex, 1);
  121.             forwardPath.add(path);
  122.             forwardGains.add(forwardPathGain);
  123.             return;
  124.         }
  125.  
  126.         int rowIndex = -1;
  127.         rowIndex = getNextNode(rowIndex + 1, columnIndex);
  128.         while (rowIndex != -1) {
  129.             addToForwardPath(columnIndex, adjMatrix[rowIndex][columnIndex]);
  130.             traverseForwardPath(rowIndex);
  131.             removeFromForwardPath(columnIndex, adjMatrix[rowIndex][columnIndex]);
  132.             rowIndex = getNextNode(rowIndex + 1, columnIndex);
  133.         }
  134.     }
  135.  
  136.     private void removeFromForwardPath(int currentNode, int gain) {
  137.         String toBeRemoved = "x" + currentNode;
  138.         if (path.contains(toBeRemoved))
  139.             path = path.replace(toBeRemoved, "");
  140.         forwardPathGain /= gain;
  141.  
  142.     }
  143.  
  144.     private void addToForwardPath(int currentNode, int gain) {
  145.         path += "x" + currentNode;
  146.         forwardPathGain *= gain;
  147.     }
  148.  
  149.     private int getNextNode(int rowIndex, int columnIndex) {
  150.         for (; rowIndex < this.numberOfNodes; rowIndex++)
  151.             if (adjMatrix[rowIndex][columnIndex] != 0)
  152.                 return rowIndex;
  153.         return -1;
  154.     }
  155.  
  156.     private boolean isLastNode(int columnIndex) {
  157.         for (int i = 0; i < this.numberOfNodes; i++)
  158.             if (adjMatrix[i][columnIndex] != 0)
  159.                 return false;
  160.         return true;
  161.     }
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement