Advertisement
FNSY

Untitled

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