Advertisement
kimo12

Untitled

Apr 13th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 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 Scanner scan;
  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. 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. System.out.println(this.loopsGains.get(i));
  79. }
  80. }
  81.  
  82. private int[][] copyMatrix(int[][] copyTo, int[][] copyFrom) {
  83. for (int i = 0; i < copyFrom.length; i++)
  84. for (int j = 0; j < copyFrom.length; j++)
  85. copyTo[i][j] = copyFrom[i][j];
  86. return copyTo;
  87. }
  88.  
  89. private int[] getLoopGainsArray(String gainString) {
  90. gainString = gainString.substring(1, gainString.length()).replace("x", " ");
  91. String[] arrayString = gainString.split(" ");
  92. int[] array = new int[arrayString.length];
  93. for (int i = 0; i < arrayString.length; i++) {
  94. array[i] = Integer.parseInt(arrayString[i]);
  95. }
  96. return array;
  97. }
  98.  
  99. private int getSimpleLoopGain(String gainString) {
  100. int[] array = getLoopGainsArray(gainString);
  101. int column, row;
  102. int gain = 1;
  103. for (int i = 1; i < array.length; i++) {
  104. column = array[i - 1];
  105. row = array[i];
  106. gain *= adjMatrix[row][column];
  107. }
  108. return gain;
  109. // x1x2x3x4x1
  110. }
  111.  
  112. private void addFinalNode() {
  113. boolean addColumn = false;
  114. for (int i = 0; i < this.adjMatrix.length; i++)
  115. if (this.adjMatrix[i][this.numberOfNodes - 1] != 0) {
  116. addColumn = true;
  117. break;
  118. }
  119.  
  120. if (addColumn == true) {
  121. int newDimension = this.numberOfNodes + 1;
  122. int[][] copy = new int[newDimension][newDimension];
  123. makeAllZeros(copy);
  124. copy = copyMatrix(copy, adjMatrix);
  125. copy[newDimension - 1][newDimension - 2] = 1;
  126. adjMatrix = copy;
  127. this.numberOfNodes = newDimension;
  128. }
  129. }
  130.  
  131. private void loops() {
  132. // searching for self loops
  133. for (int i = 0; i < numberOfNodes; i++) {
  134. if (adjMatrix[i][i] != 0) {
  135. String selfLoop = "x" + i + "x" + i;
  136. loopsPath.add(selfLoop);
  137. loopsGains.add(adjMatrix[i][i]);
  138. }
  139. }
  140. // finiding possible loops path
  141. for (int i = 1; i < numberOfNodes; i++) {
  142. for (int j = i - 1; j >= 0; j--) {
  143. if (adjMatrix[j][i] != 0) {
  144. // from i to j (reverse path) j <==== i
  145. Point x = new Point(j, i);
  146. searchLoops.add(x);
  147. }
  148. }
  149. }
  150. // search in the forwarc pathes for path from j to i
  151. // if found j ===> i then the is a loop j => i => j
  152. for (int i = 0; i < this.forwardPath.size(); i++) {
  153. String current = this.forwardPath.get(i);
  154. for (int j = 0; j < this.searchLoops.size(); j++) {
  155. int x = this.searchLoops.get(j).x;
  156. String from = "x" + x;
  157. if (current.contains(from)) {
  158. int y = this.searchLoops.get(j).y;
  159. String to = "x" + y;
  160. if (current.contains(to)) {
  161. // this is a loop we must calculate the gain and print
  162. // the loop path
  163. int start = current.lastIndexOf(from);
  164. int end = current.lastIndexOf(to) + 1;
  165. String loop = current.substring(start, end + 1) + current.substring(start, start + 2);
  166. // Chech that this loop isn't already taken
  167. boolean exist = false;
  168. for (int k = 0; k < this.loopsPath.size(); k++) {
  169. if (loop.equals(this.loopsPath.get(k))) {
  170. exist = true;
  171. }
  172. }
  173. if (!exist) {
  174. loopsPath.add(loop);
  175. loopsGains.add(getSimpleLoopGain(loop));
  176. }
  177. }
  178. }
  179. }
  180. }
  181.  
  182. }
  183.  
  184. private void traverseForwardPath(int columnIndex) {
  185.  
  186. if (isLastNode(columnIndex)) {
  187. addToForwardPath(columnIndex, 1);
  188. forwardPath.add(path);
  189. forwardGains.add(forwardPathGain);
  190. path = path.replace("x" + columnIndex, "");
  191. return;
  192. }
  193.  
  194. int rowIndex = -1;
  195. rowIndex = getNextNode(rowIndex + 1, columnIndex);
  196. while (rowIndex != -1) {
  197. if (rowIndex <= columnIndex) {
  198. rowIndex = getNextNode(rowIndex + 1, columnIndex);
  199. continue;
  200. }
  201. addToForwardPath(columnIndex, adjMatrix[rowIndex][columnIndex]);
  202. traverseForwardPath(rowIndex);
  203. removeFromForwardPath(columnIndex, adjMatrix[rowIndex][columnIndex]);
  204. rowIndex = getNextNode(rowIndex + 1, columnIndex);
  205. }
  206. }
  207.  
  208. private void removeFromForwardPath(int currentNode, int gain) {
  209. String toBeRemoved = "x" + currentNode;
  210. if (path.contains(toBeRemoved))
  211. path = path.replace(toBeRemoved, "");
  212. forwardPathGain /= gain;
  213.  
  214. }
  215.  
  216. private void addToForwardPath(int currentNode, int gain) {
  217. path += "x" + currentNode;
  218. forwardPathGain *= gain;
  219. }
  220.  
  221. private int getNextNode(int rowIndex, int columnIndex) {
  222. for (; rowIndex < this.numberOfNodes; rowIndex++)
  223. if (adjMatrix[rowIndex][columnIndex] != 0)
  224. return rowIndex;
  225. return -1;
  226. }
  227.  
  228. private boolean isLastNode(int columnIndex) {
  229. for (int i = 0; i < this.numberOfNodes; i++)
  230. if (adjMatrix[i][columnIndex] != 0)
  231. return false;
  232. return true;
  233. }
  234.  
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement