PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/ontobuilder.gui/src/ac/technion/iem/ontobuilder/gui/match/MatchInformationGui.java

http://ontobuilder.googlecode.com/
Java | 451 lines | 343 code | 31 blank | 77 comment | 29 complexity | dfcf7ccbd2e1528b795311d27241f595 MD5 | raw file
Possible License(s): CPL-1.0, GPL-2.0, LGPL-2.1, 0BSD
  1. package ac.technion.iem.ontobuilder.gui.match;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.text.NumberFormat;
  7. import java.util.ArrayList;
  8. import java.util.Hashtable;
  9. import java.util.Iterator;
  10. import java.util.List;
  11. import java.util.Map;
  12. import javax.swing.JTable;
  13. import javax.swing.SwingConstants;
  14. import ac.technion.iem.ontobuilder.core.ontology.OntologyUtilities;
  15. import ac.technion.iem.ontobuilder.core.ontology.Term;
  16. import ac.technion.iem.ontobuilder.core.utils.properties.PropertiesHandler;
  17. import ac.technion.iem.ontobuilder.gui.application.ApplicationUtilities;
  18. import ac.technion.iem.ontobuilder.gui.ontology.OntologyGui;
  19. import ac.technion.iem.ontobuilder.gui.utils.StringUtilitiesGui;
  20. import ac.technion.iem.ontobuilder.gui.utils.graphs.GraphUtilities;
  21. import ac.technion.iem.ontobuilder.matching.match.Match;
  22. import ac.technion.iem.ontobuilder.matching.match.MatchInformation;
  23. import ac.technion.iem.ontobuilder.matching.match.MatchInformationFormatTypesEnum;
  24. import ac.technion.iem.ontobuilder.matching.match.Mismatch;
  25. import ac.technion.iem.ontobuilder.matching.meta.match.MatchedAttributePair;
  26. import ac.technion.iem.ontobuilder.matching.utils.SchemaMatchingsUtilities;
  27. import ac.technion.iem.ontobuilder.matching.utils.SchemaTranslator;
  28. import com.jgraph.JGraph;
  29. import com.jgraph.graph.ConnectionSet;
  30. import com.jgraph.graph.DefaultEdge;
  31. import com.jgraph.graph.DefaultGraphCell;
  32. import com.jgraph.graph.DefaultGraphModel;
  33. import com.jgraph.graph.DefaultPort;
  34. /**
  35. * <p>
  36. * Title: MatchInformationGui
  37. * </p>
  38. * <p>
  39. * Description: Implements the methods of the MatchInformation used by the GUI
  40. * </p>
  41. */
  42. public class MatchInformationGui
  43. {
  44. private MatchInformation _matchInformation;
  45. public MatchInformationGui(MatchInformation matchInformation)
  46. {
  47. _matchInformation = matchInformation;
  48. }
  49. /**
  50. * Creates a JTABLE object containing the matches (target term,candidate term effectiveness)
  51. *
  52. * @return {@link JTable}
  53. */
  54. public JTable getMatchTable()
  55. {
  56. Object data[][] = getTableModelData();
  57. Object columnNames[] =
  58. {
  59. _matchInformation.getTargetOntology().getName(),
  60. _matchInformation.getCandidateOntology().getName(),
  61. ApplicationUtilities.getResourceString("ontology.match.effectiveness")
  62. };
  63. return new JTable(new MatchTableModel(columnNames, data));
  64. }
  65. /**
  66. * Converts matches to an object array
  67. *
  68. * @return Object[][] - each row holds target term, candidate term, effectiveness
  69. */
  70. public Object[][] getTableModelData()
  71. {
  72. ArrayList<Match> matches = _matchInformation.getMatches();
  73. Object data[][] = new Object[matches.size()][3];
  74. NumberFormat nf = NumberFormat.getInstance();
  75. for (int i = 0; i < matches.size(); i++)
  76. {
  77. Match match = (Match) matches.get(i);
  78. data[i][0] = match.getTargetTerm();
  79. data[i][1] = match.getCandidateTerm();
  80. data[i][2] = (match.getEffectiveness() >= 0 ? nf.format(match.getEffectiveness() * 100) +
  81. "%" : "n/a");
  82. }
  83. return data;
  84. }
  85. /**
  86. * Creates a JTABLE object containing the target mismatches (target terms)
  87. *
  88. * @return {@link JTable}
  89. */
  90. public JTable getMismatchTargetOntologyTable()
  91. {
  92. ArrayList<Mismatch> mismatchesTargetOntology = _matchInformation
  93. .getMismatchesTargetOntology();
  94. Object data[][] = new Object[mismatchesTargetOntology.size()][1];
  95. Object columnNames[] =
  96. {
  97. _matchInformation.getTargetOntology().getName()
  98. };
  99. for (int i = 0; i < mismatchesTargetOntology.size(); i++)
  100. {
  101. Term term = ((Mismatch) mismatchesTargetOntology.get(i)).getTerm();
  102. data[i][0] = term;
  103. }
  104. return new JTable(new MatchTableModel(columnNames, data));
  105. }
  106. /**
  107. * Creates a JTABLE object containing the candidate mismatches (candidate terms)
  108. *
  109. * @return {@link JTable}
  110. */
  111. JTable getMismatchCandidateOntologyTable()
  112. {
  113. ArrayList<Mismatch> mismatchesCandidateOntology = _matchInformation
  114. .getMismatchesCandidateOntology();
  115. Object data[][] = new Object[mismatchesCandidateOntology.size()][1];
  116. Object columnNames[] =
  117. {
  118. _matchInformation.getCandidateOntology().getName()
  119. };
  120. for (int i = 0; i < mismatchesCandidateOntology.size(); i++)
  121. {
  122. Term term = ((Mismatch) mismatchesCandidateOntology.get(i)).getTerm();
  123. data[i][0] = term;
  124. }
  125. return new JTable(new MatchTableModel(columnNames, data));
  126. }
  127. /**
  128. * Creates a JTABLE object containing all mismatches
  129. *
  130. * @return {@link JTable}
  131. */
  132. public JTable getMismatchTable()
  133. {
  134. ArrayList<Mismatch> mismatchesTargetOntology = _matchInformation
  135. .getMismatchesTargetOntology();
  136. ArrayList<Mismatch> mismatchesCandidateOntology = _matchInformation
  137. .getMismatchesCandidateOntology();
  138. Object data[][] = new Object[Math.max(mismatchesTargetOntology.size(),
  139. mismatchesCandidateOntology.size())][2];
  140. Object columnNames[] =
  141. {
  142. _matchInformation.getTargetOntology().getName(),
  143. _matchInformation.getCandidateOntology().getName()
  144. };
  145. for (int i = 0; i < mismatchesTargetOntology.size(); i++)
  146. {
  147. Term term = ((Mismatch) mismatchesTargetOntology.get(i)).getTerm();
  148. data[i][0] = term;
  149. }
  150. for (int i = 0; i < mismatchesCandidateOntology.size(); i++)
  151. {
  152. Term term = ((Mismatch) mismatchesCandidateOntology.get(i)).getTerm();
  153. data[i][1] = term;
  154. }
  155. return new JTable(new MatchTableModel(columnNames, data));
  156. }
  157. /**
  158. * Calculates precision and recall of the match;
  159. *
  160. * @return {@link JTable} holds target,candidate names, algorithm, total count of matches
  161. * precision and recall
  162. */
  163. public JTable getStatistics()
  164. {
  165. return getStatistics(null);
  166. }
  167. /**
  168. * Calculates precision and recall of the match;
  169. *
  170. * @return {@link JTable} holds target,candidate names, algorithm, total count of matches
  171. * precision and recall
  172. */
  173. public JTable getStatistics(SchemaTranslator exactMapping)
  174. {
  175. double recall = -1;
  176. double precision = -1;
  177. if (exactMapping != null)
  178. {
  179. recall = _matchInformation.getRecall(exactMapping);
  180. precision = _matchInformation.getPrecision(exactMapping);
  181. }
  182. Object columnNames[] =
  183. {
  184. PropertiesHandler.getResourceString("ontology.match.statistics"),
  185. PropertiesHandler.getResourceString("ontology.match.value")
  186. };
  187. NumberFormat nf = NumberFormat.getInstance();
  188. Object data[][] =
  189. {
  190. {
  191. PropertiesHandler.getResourceString("ontology.match.target"),
  192. _matchInformation.getTargetOntology().getName()
  193. },
  194. {
  195. PropertiesHandler.getResourceString("ontology.match.candidate"),
  196. _matchInformation.getCandidateOntology().getName()
  197. },
  198. {
  199. PropertiesHandler.getResourceString("ontology.match.algorithm"),
  200. _matchInformation.getAlgorithm() != null ? _matchInformation.getAlgorithm()
  201. .getName() : _matchInformation.getMetaAlgorithm().getAlgorithmName()
  202. },
  203. {
  204. PropertiesHandler.getResourceString("ontology.match.totalTarget"),
  205. new Integer(_matchInformation.getTargetOntologyTermsTotal())
  206. },
  207. {
  208. PropertiesHandler.getResourceString("ontology.match.totalCandidate"),
  209. new Integer(_matchInformation.getCandidateOntologyTermsTotal())
  210. },
  211. {
  212. PropertiesHandler.getResourceString("ontology.match.totalMatches"),
  213. new Integer(_matchInformation.getMatches().size())
  214. },
  215. {
  216. PropertiesHandler.getResourceString("ontology.match.recall"),
  217. (recall != -1 ? nf.format(recall * 100) + "%" : "N/A")
  218. },
  219. {
  220. PropertiesHandler.getResourceString("ontology.match.precision"),
  221. (precision != -1 ? nf.format(precision * 100) + "%" : "N/A")
  222. },
  223. // {PropertiesHandler.getResourceString("ontology.match.overall"),nf.format(getOverallMatchConfidence()*100)+"%"}
  224. };
  225. return new JTable(new MatchTableModel(columnNames, data));
  226. }
  227. /**
  228. * Create a full report containing information about the MatchInformation object (algorithm,
  229. * threshold,recall, precision, matches and mismatces)
  230. *
  231. * @return String with the full report
  232. */
  233. public String getReport()
  234. {
  235. NumberFormat nf = NumberFormat.getInstance();
  236. StringBuffer sb = new StringBuffer("");
  237. sb.append(PropertiesHandler.getResourceString("ontology.match.target")).append(": ")
  238. .append(_matchInformation.getTargetOntology().getName()).append("\n");
  239. sb.append(PropertiesHandler.getResourceString("ontology.match.candidate")).append(": ")
  240. .append(_matchInformation.getCandidateOntology().getName()).append("\n");
  241. sb.append("\n");
  242. sb.append(PropertiesHandler.getResourceString("ontology.match.algorithm")).append(": ")
  243. .append(_matchInformation.getAlgorithm().getName()).append("\n");
  244. sb.append(PropertiesHandler.getResourceString("ontology.match.threshold")).append(": ")
  245. .append(nf.format(_matchInformation.getAlgorithm().getThreshold()) + "%").append("\n");
  246. sb.append("\n");
  247. sb.append(PropertiesHandler.getResourceString("ontology.match.totalTarget")).append(": ")
  248. .append(_matchInformation.getTargetOntologyTermsTotal()).append("\n");
  249. sb.append(PropertiesHandler.getResourceString("ontology.match.totalCandidate"))
  250. .append(": ").append(_matchInformation.getCandidateOntologyTermsTotal()).append("\n");
  251. sb.append(PropertiesHandler.getResourceString("ontology.match.totalMatches")).append(": ")
  252. .append(_matchInformation.getMatches().size()).append("\n");
  253. sb.append(PropertiesHandler.getResourceString("ontology.match.recall"))
  254. .append(": ")
  255. .append(
  256. nf.format(_matchInformation.getMatches().size() /
  257. (double) _matchInformation.getTargetOntologyTermsTotal() * 100) +
  258. "%").append("\n");
  259. sb.append("\n");
  260. sb.append(PropertiesHandler.getResourceString("mergewizard.matchInformation.matches"))
  261. .append("\n");
  262. sb.append(StringUtilitiesGui.getJTableStringRepresentation(getMatchTable()));
  263. sb.append("\n\n");
  264. sb.append(PropertiesHandler.getResourceString("mergewizard.matchInformation.mismatches"))
  265. .append("\n");
  266. sb.append(StringUtilitiesGui.getJTableStringRepresentation(getMismatchTable()));
  267. sb.append("\n\n");
  268. sb.append(
  269. PropertiesHandler.getResourceString("mergewizard.matchInformation.mismatchesTarget"))
  270. .append("\n");
  271. sb.append(StringUtilitiesGui.getJTableStringRepresentation(getMismatchTargetOntologyTable()));
  272. sb.append("\n\n");
  273. sb.append(
  274. PropertiesHandler.getResourceString("mergewizard.matchInformation.mismatchesCandidate"))
  275. .append("\n");
  276. sb.append(StringUtilitiesGui
  277. .getJTableStringRepresentation(getMismatchCandidateOntologyTable()));
  278. return sb.toString();
  279. }
  280. /**
  281. * @return {@link JGraph} with the matches
  282. */
  283. public JGraph getGraph()
  284. {
  285. return getGraph(null);
  286. }
  287. /**
  288. * @return {@link JGraph} with the matches
  289. */
  290. public JGraph getGraph(SchemaTranslator exact)
  291. {
  292. SchemaTranslator st = new SchemaTranslator(_matchInformation, true, false);
  293. // List badMatches = SchemaMatchingsUtilities.diffBestMatches(st, exact);
  294. List<?> goodMatches = (exact != null ? SchemaMatchingsUtilities.intersectMappings(st,
  295. exact, _matchInformation.getMatrix()) : new ArrayList<Object>());
  296. List<?> allMatches = st.getMatches();
  297. JGraph graph = new JGraph(new DefaultGraphModel());
  298. graph.setEditable(false);
  299. ArrayList<DefaultGraphCell> cells = new ArrayList<DefaultGraphCell>();
  300. // ConnectionSet for the Insert method
  301. ConnectionSet cs = new ConnectionSet();
  302. // Hashtable for Attributes (Vertex to Map)
  303. Hashtable<DefaultGraphCell, Map<?, ?>> attributes = new Hashtable<DefaultGraphCell, Map<?, ?>>();
  304. OntologyGui targetOntologyGui = new OntologyGui(_matchInformation.getTargetOntology());
  305. OntologyGui candidateOntologyGui = new OntologyGui(_matchInformation.getCandidateOntology());
  306. DefaultGraphCell targetCell = targetOntologyGui.addToGraph(cells, attributes, cs);
  307. DefaultGraphCell candidateCell = candidateOntologyGui.addToGraph(cells, attributes, cs);
  308. NumberFormat nf = NumberFormat.getInstance();
  309. // Create match edges
  310. for (Iterator<?> i = allMatches.iterator(); i.hasNext();)
  311. {
  312. MatchedAttributePair match = (MatchedAttributePair) i.next();
  313. MatchedAttributePair pair;
  314. Term cand, target;
  315. cand = _matchInformation.getMatrix().getTermByName(match.getAttribute2());
  316. target = _matchInformation.getMatrix().getTermByName(match.getAttribute1());
  317. pair = new MatchedAttributePair(OntologyUtilities.oneIdRemoval(target.toStringVs2()),
  318. OntologyUtilities.oneIdRemoval(cand.toStringVs2()), match.getMatchedPairWeight());
  319. String targetTerm = match.getAttribute1();
  320. String candidateTerm = match.getAttribute2();
  321. DefaultGraphCell targetTermCell = null, candidateTermCell = null;
  322. boolean targetSelected = false;
  323. boolean candidateSelected = false;
  324. for (Iterator<DefaultGraphCell> j = cells.iterator(); j.hasNext();)
  325. {
  326. Object object = j.next();
  327. if (object instanceof DefaultGraphCell)
  328. {
  329. DefaultGraphCell cell = (DefaultGraphCell) object;
  330. Object userObject = cell.getUserObject();
  331. if (!targetSelected && userObject != null &&
  332. OntologyUtilities.oneIdRemoval(userObject.toString()).equals(targetTerm))
  333. {
  334. targetTermCell = cell;
  335. targetSelected = true;
  336. }
  337. else if (!candidateSelected && userObject != null &&
  338. OntologyUtilities.oneIdRemoval(userObject.toString()).equals(candidateTerm))
  339. {
  340. candidateTermCell = cell;
  341. candidateSelected = true;
  342. }
  343. }
  344. if (candidateSelected && targetSelected)
  345. break;
  346. }
  347. if (targetTermCell != null && candidateTermCell != null)
  348. {
  349. DefaultPort toCandidatePort = new DefaultPort("toCandidate");
  350. targetTermCell.add(toCandidatePort);
  351. String portName;
  352. if (goodMatches.contains(pair))
  353. {
  354. portName = "toGoodTarget";
  355. }
  356. else
  357. {
  358. portName = "toBadTarget";
  359. }
  360. DefaultPort toTargetPort = new DefaultPort(portName);
  361. candidateTermCell.add(toTargetPort);
  362. DefaultEdge edge = new DefaultEdge(nf.format(match.getMatchedPairWeight()));
  363. cs.connect(edge, toCandidatePort, true);
  364. cs.connect(edge, toTargetPort, false);
  365. cells.add(edge);
  366. }
  367. }
  368. // Insert the cells (View stores attributes)
  369. graph.getModel().insert(cells.toArray(), cs, null, attributes);
  370. GraphUtilities.alignHierarchy(graph, targetCell, SwingConstants.LEFT, 10, 10);
  371. // Calculate the offset for the graph right hierarchy
  372. int width = PropertiesHandler.getIntProperty("graph.cell.width");
  373. int interspace = PropertiesHandler.getIntProperty("graph.cell.hierarchyInterspace");
  374. int leftDepth = GraphUtilities.getDepth(targetCell) + 1;
  375. int rightDepth = GraphUtilities.getDepth(candidateCell) + 1;
  376. int off = (width / 2 + 10) * (leftDepth + rightDepth - 2) + width * 2 + interspace;
  377. GraphUtilities.alignHierarchy(graph, candidateCell, SwingConstants.RIGHT, off - 10, 10);
  378. GraphUtilities.alignMatches(graph);
  379. return graph;
  380. }
  381. /**
  382. * @param file - a {@link File} to write to
  383. */
  384. public void save(File file) throws IOException
  385. {
  386. save(file, MatchInformationFormatTypesEnum.TEXT_FORMAT);
  387. }
  388. /**
  389. * @param file - a {@link File} to write to
  390. * @param format - 1 to XML, 0 for text
  391. */
  392. public void save(File file, MatchInformationFormatTypesEnum format) throws IOException
  393. {
  394. switch (format)
  395. {
  396. case XML_FORMAT:
  397. _matchInformation.saveToXML(file);
  398. break;
  399. case TEXT_FORMAT:
  400. saveToText(file);
  401. break;
  402. default:
  403. throw new IOException(
  404. PropertiesHandler.getResourceString("error.matchInformation.fileFormat"));
  405. }
  406. }
  407. /**
  408. * Writes a report of the the match information
  409. *
  410. * @param file - a {@link File} to write to
  411. * @throws IOException
  412. */
  413. public void saveToText(File file) throws IOException
  414. {
  415. BufferedWriter out = new BufferedWriter(new FileWriter(file));
  416. out.write(getReport());
  417. out.flush();
  418. out.close();
  419. }
  420. }