/context-management/UserContextRefinement/src/test/java/xbneditor/CountGraphErrors.java

https://github.com/societies/SOCIETIES-Platform · Java · 371 lines · 251 code · 71 blank · 49 comment · 20 complexity · 1ce3a9577ee8dc622f6702a64223fa58 MD5 · raw file

  1. package xbneditor;
  2. import java.io.*;
  3. import java.util.*;
  4. /**
  5. * This is a class that takes in a generated Bayesian network and a gold
  6. * standard bayesian network and will look for different graph error
  7. * calculations that have been found within them.
  8. *
  9. * @author Laura Kruse
  10. * @version v1.0
  11. */
  12. public class CountGraphErrors {
  13. private LinkedList currentbn;
  14. private int[][] goldstandard;
  15. private int[][] current;
  16. private Hashtable nodenames;
  17. private Hashtable mapped;
  18. private String filename;
  19. private int size;
  20. /**
  21. * This takes in the current Bayesian network and the filename of
  22. * the gold standard network so that the generated network can be
  23. * compared to the gold standard network for errors.
  24. *
  25. * @param currentbn the Bayesian Network that was generated
  26. * @param filename the name of the file in which the gold standard
  27. * network is stored
  28. */
  29. public CountGraphErrors(LinkedList currentbn, String filename) {
  30. //System.out.println("LinkedList, String constructor");
  31. this.currentbn = currentbn;
  32. this.filename = filename;
  33. size = currentbn.size();
  34. goldstandard = new int[size][size];
  35. current = new int[size][size];
  36. nodenames = new Hashtable();
  37. mapped = new Hashtable();
  38. populateHash();
  39. }
  40. /**
  41. * This function takes in two file names. One is the generated
  42. * bayesian network and the other is the gold standard bayesian
  43. * network.
  44. *
  45. * @param fncurrent the generated bayesian network
  46. * @param filename the gold standard bayesian network
  47. */
  48. public CountGraphErrors(String fncurrent, String filename) {
  49. //System.out.println("String, String Constructor");
  50. //System.out.println("K2: " + fncurrent);
  51. //System.out.println("GS: " + filename);
  52. FileIO file;
  53. file = new FileIO();
  54. currentbn = file.load(fncurrent);
  55. this.filename = filename;
  56. size = currentbn.size();
  57. //System.out.println("size = " + size);
  58. goldstandard = new int[size][size];
  59. current = new int[size][size];
  60. nodenames = new Hashtable();
  61. mapped = new Hashtable();
  62. populateHash();
  63. }
  64. /**
  65. * This function locates all of the names in the network and puts them
  66. * into a hash table so that it can be easily determined where a
  67. * node is in the comparison array.
  68. */
  69. private void populateHash() {
  70. String name;
  71. for(int i=0;i<size;i++) {
  72. name = ((Item) currentbn.get(i)).getItem().getBlockName();
  73. nodenames.put(name, new Integer(i));
  74. mapped.put(new Integer(i), name);
  75. }
  76. getBNStandard();
  77. getGoldStandard();
  78. }
  79. /**
  80. * This function retrieves the generated bayesian network and stores
  81. * the location of the arcs and direction in an array.
  82. */
  83. private void getBNStandard() {
  84. Item item;
  85. String name1;
  86. String name2;
  87. int num1;
  88. int num2;
  89. int parents;
  90. for(int i=0;i<size;i++) {
  91. item = (Item) currentbn.get(i);
  92. name1 = item.getItem().getBlockName();
  93. num1 = ((Integer) nodenames.get(name1)).intValue();
  94. parents = item.numParents();
  95. for(int j=0;j<parents;j++) {
  96. name2 = item.getParent(j).getItem().getBlockName();
  97. num2 = ((Integer) nodenames.get(name2)).intValue();
  98. current[num1][num2] = 1;
  99. }
  100. }
  101. //return current;
  102. }
  103. /**
  104. * This function retireves the gold standard bayesian network and
  105. * puts the location of the arcs of the nodes into an array.
  106. */
  107. private void getGoldStandard() {
  108. String content;
  109. if(filename != null) {
  110. content = readGSFile(filename);
  111. // look for the different file format cases here
  112. // decide what function to parse them based on this
  113. // this should return a populated table
  114. if(content.indexOf("BIF VERSION=\"0.3\"") > 0) {
  115. getXMLGoldStandard(content, true);
  116. } else if(content.indexOf("<BIF>") > 0) {
  117. getXMLGoldStandard(content, false);
  118. } else {
  119. getBIFGoldStandard(content);
  120. }
  121. }
  122. //return goldstandard;
  123. }
  124. private String readGSFile(String filename) {
  125. StringBuffer sbuf;
  126. BufferedReader infile;
  127. sbuf = new StringBuffer();
  128. try {
  129. infile = new BufferedReader(new FileReader(filename));
  130. while(infile.ready()) {
  131. sbuf.append(infile.readLine());
  132. }
  133. } catch(FileNotFoundException fnfe) {
  134. fnfe.printStackTrace();
  135. } catch(IOException ioe) {
  136. ioe.printStackTrace();
  137. }
  138. return sbuf.toString();
  139. }
  140. private void getXMLGoldStandard(String sbuf, boolean currentVer) {
  141. int startDefinition;
  142. int endDefinition;
  143. if(currentVer) {
  144. startDefinition = sbuf.indexOf("<DEFINITION>");
  145. endDefinition = sbuf.indexOf("</DEFINITION>");
  146. } else {
  147. startDefinition = sbuf.indexOf("<PROBABILITY>");
  148. endDefinition = sbuf.indexOf("</PROBABILITY>");
  149. }
  150. while(startDefinition > 0) {
  151. getXMLGoldStandardNames(sbuf.substring(startDefinition, endDefinition));
  152. if(currentVer) {
  153. startDefinition = sbuf.indexOf("<DEFINITION>", endDefinition);
  154. endDefinition = sbuf.indexOf("</DEFINITION>", startDefinition);
  155. } else {
  156. startDefinition = sbuf.indexOf("<PROBABILITY>", endDefinition);
  157. endDefinition = sbuf.indexOf("</PROBABILITY>", startDefinition);
  158. }
  159. }
  160. }
  161. private void getBIFGoldStandard(String sbuf) {
  162. int startDefinition;
  163. int endDefinition;
  164. startDefinition = sbuf.indexOf("probability (");
  165. endDefinition = sbuf.indexOf("}", startDefinition);
  166. while(startDefinition > 0) {
  167. getBIFGoldStandardNames(sbuf.substring(startDefinition, endDefinition));
  168. startDefinition = sbuf.indexOf("probability (", endDefinition);
  169. endDefinition = sbuf.indexOf("}", startDefinition);
  170. }
  171. }
  172. private void getXMLGoldStandardNames(String sbuf) {
  173. int startFor;
  174. int endFor;
  175. int startGiven;
  176. int endGiven;
  177. String name1;
  178. String name2;
  179. int num1;
  180. int num2;
  181. startFor = sbuf.indexOf("<FOR>");
  182. endFor = sbuf.indexOf("</FOR>");
  183. name1 = sbuf.substring(startFor + 5, endFor);
  184. num1 = ((Integer) nodenames.get(name1)).intValue();
  185. startGiven = sbuf.indexOf("<GIVEN>", endFor);
  186. endGiven = sbuf.indexOf("</GIVEN>", startGiven);
  187. while(startGiven > startFor) {
  188. name2 = sbuf.substring(startGiven + 7, endGiven);
  189. num2 = ((Integer) nodenames.get(name2)).intValue();
  190. // do something to put the 1 or 0 in the table here
  191. goldstandard[num1][num2] = 1;
  192. startGiven = sbuf.indexOf("<GIVEN>", endGiven);
  193. endGiven = sbuf.indexOf("</GIVEN>", startGiven);
  194. }
  195. }
  196. private void getBIFGoldStandardNames(String sbuf) {
  197. int startFor;
  198. int endFor;
  199. int startGiven;
  200. int endGiven;
  201. String name1;
  202. String name2;
  203. String tmp;
  204. int num1;
  205. int num2;
  206. startFor = sbuf.indexOf("(") + 1;
  207. endFor = sbuf.indexOf(")", startFor) - 1;
  208. tmp = trimexcess(sbuf.substring(startFor, endFor));
  209. if((startGiven = tmp.indexOf(" ")) > 0) {
  210. name1 = tmp.substring(0, startGiven);
  211. tmp = trimexcess(tmp.substring(startGiven));
  212. if((endGiven = tmp.indexOf(" ")) < 0) {
  213. endGiven = tmp.length();
  214. }
  215. } else {
  216. name1 = tmp;
  217. endGiven = -1;
  218. }
  219. num1 = ((Integer) nodenames.get(name1)).intValue();
  220. while(startGiven > 0) {
  221. name2 = tmp.substring(0, endGiven);
  222. num2 = ((Integer) nodenames.get(name2)).intValue();
  223. goldstandard[num1][num2] = 1;
  224. tmp = trimexcess(tmp.substring(endGiven));
  225. endGiven = tmp.indexOf(" ");
  226. if(endGiven < 0 && tmp.length() <= 0) {
  227. startGiven = -1;
  228. } else if(endGiven < 0) {
  229. endGiven = tmp.length();
  230. }
  231. }
  232. }
  233. private String trimexcess(String sbuf) {
  234. sbuf = sbuf.replace('"', ' ');
  235. sbuf = sbuf.replace('|', ' ');
  236. sbuf = sbuf.replace(',', ' ');
  237. return sbuf.trim();
  238. }
  239. public String countErrorAddition() {
  240. StringBuffer ret;
  241. ret = new StringBuffer();
  242. for(int i=0;i<size;i++) {
  243. for(int j=i+1;j<size;j++) {
  244. if(current[i][j] + current[j][i] > goldstandard[i][j] + goldstandard[j][i]) {
  245. if(current[i][j] == 1) {
  246. ret.append(mapped.get(new Integer(j)) + "->" + mapped.get(new Integer(i)) + " ");
  247. } else {
  248. ret.append(mapped.get(new Integer(i)) + "->" + mapped.get(new Integer(j)) + " ");
  249. }
  250. }
  251. }
  252. }
  253. //System.out.println("Addition: " + ret);
  254. return ret.toString();
  255. }
  256. public String countErrorDeletion() {
  257. StringBuffer ret;
  258. ret = new StringBuffer();
  259. for(int i=0;i<size;i++) {
  260. for(int j=i+1;j<size;j++) {
  261. if(current[i][j] + current[j][i] < goldstandard[i][j] + goldstandard[j][i]) {
  262. if(goldstandard[i][j] == 1) {
  263. ret.append(mapped.get(new Integer(j)) + "->" + mapped.get(new Integer(i)) + " ");
  264. } else {
  265. ret.append(mapped.get(new Integer(i)) + "->" + mapped.get(new Integer(j)) + " ");
  266. }
  267. }
  268. }
  269. }
  270. //System.out.println("Deletion: " + ret);
  271. return ret.toString();
  272. }
  273. public String countErrorReversal() {
  274. StringBuffer ret;
  275. ret = new StringBuffer();
  276. for(int i=0;i<size;i++) {
  277. for(int j=i+1;j<size;j++) {
  278. if((current[i][j] == goldstandard[j][i] && goldstandard[j][i] == 1) || (current[j][i] == goldstandard[i][j] && goldstandard[i][j] == 1)) {
  279. if(current[i][j] == goldstandard[j][i] && goldstandard[j][i] == 1) {
  280. ret.append(mapped.get(new Integer(j)) + "->" + mapped.get(new Integer(i)) + " ");
  281. } else {
  282. ret.append(mapped.get(new Integer(i)) + "->" + mapped.get(new Integer(j)) + " ");
  283. }
  284. }
  285. }
  286. }
  287. //System.out.println("Reversal: " + ret);
  288. return ret.toString();
  289. }
  290. }