Advertisement
FNSY

Untitled

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