PageRenderTime 50ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/ontobuilder.matching/src/ac/technion/iem/ontobuilder/matching/match/MatchInformation.java

http://ontobuilder.googlecode.com/
Java | 942 lines | 611 code | 86 blank | 245 comment | 107 complexity | be981e31ba8ce3234b3b0423597303d3 MD5 | raw file
Possible License(s): CPL-1.0, GPL-2.0, LGPL-2.1, 0BSD
  1. /**
  2. * @authors: Haggai Roitman , Giovanni Modica
  3. * This class was modified by haggai 6/12/03
  4. * changes: using MatchMatrix to hold full match matrix information
  5. * Information stored in this class: <br>
  6. * terms of both ontologies, matches, mismatches of Target/Candidate Ontology,
  7. * target/candidate Ontology, matchMatrix, algorithm and MetaAlgorithm.
  8. * for later use for 1:1 best mappings
  9. */
  10. package ac.technion.iem.ontobuilder.matching.match;
  11. import java.io.BufferedWriter;
  12. import java.io.File;
  13. import java.io.FileWriter;
  14. import java.io.IOException;
  15. import java.io.OutputStreamWriter;
  16. import java.text.NumberFormat;
  17. import java.util.ArrayList;
  18. import java.util.Iterator;
  19. import org.jdom.DocType;
  20. import org.jdom.Element;
  21. import org.jdom.output.XMLOutputter;
  22. import ac.technion.iem.ontobuilder.core.ontology.Ontology;
  23. import ac.technion.iem.ontobuilder.core.ontology.OntologyUtilities;
  24. import ac.technion.iem.ontobuilder.core.ontology.Term;
  25. import ac.technion.iem.ontobuilder.core.utils.files.StringOutputStream;
  26. import ac.technion.iem.ontobuilder.matching.algorithms.line1.misc.Algorithm;
  27. import ac.technion.iem.ontobuilder.matching.algorithms.line2.meta.MetaAlgorithm;
  28. import ac.technion.iem.ontobuilder.matching.meta.match.MatchMatrix;
  29. import ac.technion.iem.ontobuilder.matching.utils.SchemaMatchingsUtilities;
  30. import ac.technion.iem.ontobuilder.matching.utils.SchemaTranslator;
  31. /**
  32. * <p>Title: MatchInformation</p>
  33. */
  34. public class MatchInformation
  35. {
  36. protected ArrayList<Match> matches;
  37. protected ArrayList<Mismatch> mismatchesTargetOntology;
  38. protected ArrayList<Mismatch> mismatchesCandidateOntology;
  39. protected Ontology targetOntology;
  40. protected int totalTargetTerms;
  41. protected Ontology candidateOntology;
  42. protected int totalCandidateTerms;
  43. protected Algorithm algorithm;
  44. protected MetaAlgorithm metaAlgorithm;
  45. protected double[][] matchMatrix;
  46. protected ArrayList<?> originalTargetTerms;
  47. protected ArrayList<?> originalCandidateTerms;
  48. protected MatchMatrix match_Matrix;
  49. /**
  50. * Default constructor allocates matches, target mismatches and candidate mismatches arrays
  51. * lists. also creates an empty matchMatrix
  52. */
  53. public MatchInformation()
  54. {
  55. matches = new ArrayList<Match>();
  56. mismatchesTargetOntology = new ArrayList<Mismatch>();
  57. mismatchesCandidateOntology = new ArrayList<Mismatch>();
  58. // gabi - 5/3 adding to MatchInformation the MatchMatrix
  59. matchMatrix = new double[totalCandidateTerms][totalTargetTerms];
  60. }
  61. /**
  62. * @returns {@link MatchMatrix} - holds the confidence match matrix and terms of 2 ontologies
  63. * (candidate,target)
  64. */
  65. // added haggai 6/12/03
  66. public MatchMatrix getMatrix()
  67. {
  68. return match_Matrix;
  69. }
  70. /**
  71. * @param matrix holds the {@link MatchMatrix} confidence match matrix and terms of 2 ontologies (candidate,target)
  72. */
  73. public void setMatrix(MatchMatrix matrix)
  74. {
  75. match_Matrix = matrix;
  76. }
  77. // end haggai
  78. /**
  79. * Updates match information to be as the supplied one
  80. *
  81. * @param newMatch - a {@link MatchInformation} object containing matched,
  82. * mismatchesTargetOntology/mismatchescandidateOntology, and a matchMatrix.
  83. */
  84. public void updateFromMatch(MatchInformation newMatch)
  85. {
  86. matches.clear();
  87. matches.addAll(newMatch.getMatches());
  88. mismatchesTargetOntology.clear();
  89. mismatchesTargetOntology.addAll(newMatch.getMismatchesTargetOntology());
  90. mismatchesCandidateOntology.clear();
  91. mismatchesCandidateOntology.addAll(newMatch.getMismatchesCandidateOntology());
  92. for (int i = 0; i < matchMatrix.length; i++)
  93. {
  94. for (int j = 0; j < matchMatrix[i].length; j++)
  95. {
  96. matchMatrix[i][j] = newMatch.getMatchMatrix()[i][j];
  97. }
  98. }
  99. }
  100. // end gabi
  101. /**
  102. * Assumes the matrix provided fits to the ontologies stored at the MatchMatrix object
  103. *
  104. * @param matrix - a confidence match matrix
  105. */
  106. public void setMatchMatrix(double matrix[][])
  107. {
  108. matchMatrix = matrix;
  109. }
  110. /**
  111. * @return double[][] - a confidence match matrix
  112. */
  113. public double[][] getMatchMatrix()
  114. {
  115. return matchMatrix;
  116. }
  117. /**
  118. * @param targetTerms - a list of terms of the target ontology
  119. */
  120. public void setOriginalTargetTerms(ArrayList<?> targetTerms)
  121. {
  122. originalTargetTerms = new ArrayList<Object>(targetTerms);
  123. }
  124. /**
  125. * @param CandidateTerms - a list of terms of the candidate ontology
  126. */
  127. public void setOriginalCandidateTerms(ArrayList<?> CandidateTerms)
  128. {
  129. originalCandidateTerms = new ArrayList<Object>(CandidateTerms);
  130. }
  131. /**
  132. * @param total - number of target terms
  133. */
  134. public void setTargetOntologyTermsTotal(int total)
  135. {
  136. totalTargetTerms = total;
  137. }
  138. /**
  139. * @return int - number of target terms
  140. */
  141. public int getTargetOntologyTermsTotal()
  142. {
  143. return totalTargetTerms;
  144. }
  145. /**
  146. * @param total - number of candidate terms
  147. */
  148. public void setCandidateOntologyTermsTotal(int total)
  149. {
  150. totalCandidateTerms = total;
  151. }
  152. /**
  153. * @return int - number of candidate terms
  154. */
  155. public int getCandidateOntologyTermsTotal()
  156. {
  157. return totalCandidateTerms;
  158. }
  159. /**
  160. * @return int - number of matches terms
  161. */
  162. public int getTotalMatches()
  163. {
  164. return matches.size();
  165. }
  166. /**
  167. * @param targetOntology the target {@link Ontology}
  168. */
  169. public void setTargetOntology(Ontology targetOntology)
  170. {
  171. this.targetOntology = targetOntology;
  172. }
  173. /**
  174. * @return {@link Ontology} the targetOntology
  175. */
  176. public Ontology getTargetOntology()
  177. {
  178. return targetOntology;
  179. }
  180. /**
  181. * @param candidateOntology the candidate {@link Ontology}
  182. */
  183. public void setCandidateOntology(Ontology candidateOntology)
  184. {
  185. this.candidateOntology = candidateOntology;
  186. }
  187. /**
  188. * @return {@link Ontology} candidateOntology
  189. */
  190. public Ontology getCandidateOntology()
  191. {
  192. return candidateOntology;
  193. }
  194. /**
  195. * @param algorithm - an {@link Algorithm} for the match
  196. */
  197. public void setAlgorithm(Algorithm algorithm)
  198. {
  199. this.algorithm = algorithm;
  200. }
  201. /**
  202. * @return {@link Algorithm} the algorithm for the match
  203. */
  204. public Algorithm getAlgorithm()
  205. {
  206. return algorithm;
  207. }
  208. /**
  209. * @return ArrayList with all matches
  210. */
  211. public ArrayList<Match> getMatches()
  212. {
  213. return matches;
  214. }
  215. /**
  216. * Adds a match to the matches list, removes match from mismatch list(if exist their)
  217. *
  218. * @param targetTerm the target {@link Term}
  219. * @param candidateTerm the candidate {@link Term}
  220. * @param effectiveness
  221. */
  222. public void addMatch(Term targetTerm, Term candidateTerm, double effectiveness)
  223. {
  224. addMatch(new Match(targetTerm, candidateTerm, effectiveness));
  225. }
  226. /**
  227. * Adds a match to the matches list, removes match from mismatch list(if exist their)
  228. *
  229. * @param match a {@link Match}
  230. */
  231. public void addMatch(Match match)
  232. {
  233. matches.add(match);
  234. for (Iterator<Mismatch> i = mismatchesTargetOntology.iterator(); i.hasNext();)
  235. {
  236. Mismatch mismatch = (Mismatch) i.next();
  237. if (match.getTargetTerm().equals(mismatch.getTerm()))
  238. i.remove();
  239. }
  240. for (Iterator<Mismatch> i = mismatchesCandidateOntology.iterator(); i.hasNext();)
  241. {
  242. Mismatch mismatch = (Mismatch) i.next();
  243. if (match.getCandidateTerm().equals(mismatch.getTerm()))
  244. i.remove();
  245. }
  246. }
  247. /**
  248. * Removes a match from the matches list, adds it as a mismatch list(if exist their) if any of
  249. * terms in the match don't have any other matchs, it will be added to the mismatched list
  250. *
  251. * @param index index of the match to remove
  252. */
  253. public void removeMatch(int index)
  254. {
  255. Match match = (Match) matches.get(index);
  256. matches.remove(index);
  257. Term targetTerm = match.getTargetTerm();
  258. Term candidateTerm = match.getCandidateTerm();
  259. boolean addTargetTermToMismatches = true, addCandidateTermToMismatches = true;
  260. for (Iterator<Match> i = matches.iterator(); i.hasNext();)
  261. {
  262. Match auxMatch = (Match) i.next();
  263. if (auxMatch.getTargetTerm().equals(targetTerm))
  264. addTargetTermToMismatches = false;
  265. if (auxMatch.getCandidateTerm().equals(candidateTerm))
  266. addCandidateTermToMismatches = false;
  267. }
  268. if (addTargetTermToMismatches)
  269. mismatchesTargetOntology.add(new Mismatch(targetTerm));
  270. if (addCandidateTermToMismatches)
  271. mismatchesCandidateOntology.add(new Mismatch(candidateTerm));
  272. }
  273. /**
  274. * Checks if term has a match
  275. *
  276. * @param t {@link Term} from either ontologies
  277. * @return boolean - <code>true</code> if have at list one match
  278. * @throws NullPointerException - in case term is empty
  279. */
  280. public boolean isMatched(Term t)
  281. {
  282. if (t == null)
  283. {
  284. throw new NullPointerException("Term is null");
  285. }
  286. for (Iterator<Match> i = matches.iterator(); i.hasNext();)
  287. {
  288. Match match = (Match) i.next();
  289. if (match.getTargetTerm() == null || match.getCandidateTerm() == null)
  290. {
  291. throw new NullPointerException("Match has null terms!");
  292. }
  293. if (match.getTargetTerm().equals(t) || match.getCandidateTerm().equals(t))
  294. return true;
  295. }
  296. return false;
  297. }
  298. /**
  299. * @return ArrayList of target ontology {@link Mismatch}
  300. */
  301. public ArrayList<Mismatch> getMismatchesTargetOntology()
  302. {
  303. return mismatchesTargetOntology;
  304. }
  305. /**
  306. * @return {@link MetaAlgorithm}
  307. */
  308. public MetaAlgorithm getMetaAlgorithm()
  309. {
  310. return metaAlgorithm;
  311. }
  312. /**
  313. * @param metaAlgorithm - a {@link MetaAlgorithm} object to use for the match
  314. */
  315. public void setMetaAlgorithm(MetaAlgorithm metaAlgorithm)
  316. {
  317. this.metaAlgorithm = metaAlgorithm;
  318. }
  319. /**
  320. * Assumes legal term (exist in the target ontology) is entered
  321. *
  322. * @param {@link Mismatch}
  323. */
  324. public void addMismatchTargetOntology(Mismatch mismatch)
  325. {
  326. if (!mismatchesTargetOntology.contains(mismatch))
  327. mismatchesTargetOntology.add(mismatch);
  328. }
  329. /**
  330. * @return ArrayList - of candidate ontology {@link Mismatch}
  331. */
  332. public ArrayList<Mismatch> getMismatchesCandidateOntology()
  333. {
  334. return mismatchesCandidateOntology;
  335. }
  336. /**
  337. * Assumes legal term (exist in the candidate ontology) is entered
  338. *
  339. * @param {@link Mismatch}
  340. */
  341. public void addMismatchCandidateOntology(Mismatch mismatch)
  342. {
  343. if (!mismatchesCandidateOntology.contains(mismatch))
  344. mismatchesCandidateOntology.add(mismatch);
  345. }
  346. /**
  347. * Returns the match effectiveness of 2 terms, return 0 can't find a match
  348. *
  349. * @param t1 target {@link Term}
  350. * @param t2 candidate {@link Term}
  351. * @return double - match effectiveness
  352. */
  353. public double getMatchConfidence(Term t1, Term t2)
  354. {
  355. for (Iterator<Match> i = matches.iterator(); i.hasNext();)
  356. {
  357. Match match = (Match) i.next();
  358. Term targetTerm = match.getTargetTerm();
  359. Term candidateTerm = match.getCandidateTerm();
  360. if ((OntologyUtilities.isEqualTerms(targetTerm, t1) && OntologyUtilities.isEqualTerms(
  361. candidateTerm, t2)) ||
  362. (OntologyUtilities.isEqualTerms(targetTerm, t2) && OntologyUtilities.isEqualTerms(
  363. candidateTerm, t1)))
  364. return match.getEffectiveness();
  365. // if((OntologyUtilities.oneIdRemoval(targetTerm.toString()).equals(OntologyUtilities.oneIdRemoval(t1.toString())))
  366. // ||
  367. // (OntologyUtilities.oneIdRemoval(targetTerm.toStringVs2()).equals(OntologyUtilities.oneIdRemoval(t1.toStringVs2()))
  368. // ||
  369. // (OntologyUtilities.oneIdRemoval(targetTerm.toString()).equals(OntologyUtilities.oneIdRemoval(t1.toString())))
  370. // ||
  371. // (OntologyUtilities.oneIdRemoval(targetTerm.toStringVs2()).equals(OntologyUtilities.oneIdRemoval(t2.toString()))
  372. // ||
  373. // (OntologyUtilities.oneIdRemoval(targetTerm.toStringVs2()).equals(OntologyUtilities.oneIdRemoval(t2.toString())))
  374. // || candidateTerm.equals(t1))
  375. // return match.getEffectiveness();
  376. }
  377. return 0;
  378. }
  379. /**
  380. * Retrieves a match, returns null if none is found
  381. *
  382. * @param t candidate {@link Term}
  383. * @return {@link Match}
  384. */
  385. public Match getMatchForCandidate(Term t)
  386. {
  387. for (Iterator<Match> i = matches.iterator(); i.hasNext();)
  388. {
  389. Match match = (Match) i.next();
  390. Term candidateTerm = match.getCandidateTerm();
  391. if (candidateTerm.equals(t))
  392. {
  393. System.out.println("found a match for " + t.getName());
  394. return match;
  395. }
  396. }
  397. System.out.println("no match was found for " + t.getName());
  398. return null;
  399. }
  400. /**
  401. * Calculates the average effectiveness of the matches
  402. *
  403. * @return double - average effectiveness
  404. */
  405. public double getOverallMatchConfidence()
  406. {
  407. if (matches.size() == 0)
  408. return 0;
  409. double overall = 0;
  410. for (Iterator<Match> i = matches.iterator(); i.hasNext();)
  411. {
  412. Match match = (Match) i.next();
  413. overall += match.getEffectiveness();
  414. }
  415. return overall / (double) matches.size();
  416. }
  417. /**
  418. * Creates a new MatchInformation object from 2 existing one. uses a new provided trashhold to
  419. * decided on matches. (note if both matchInformations contain the same match, the method will
  420. * average between their values and decides if it's still a match (effectiveness>trashhold).
  421. *
  422. * @param matchInfo1 the first {@link MatchInformation}
  423. * @param matchInfo2 the second {@link MatchInformation}
  424. * @param threshold a value from which to decide if the effectiveness is high enought for a
  425. * match
  426. * @return {@link MatchInformation}
  427. */
  428. public static MatchInformation combineMatches(MatchInformation matchInfo1,
  429. MatchInformation matchInfo2, double threshold)
  430. {
  431. MatchInformation match = new MatchInformation();
  432. ArrayList<?> matches1 = matchInfo1.getMatches();
  433. ArrayList<?> matches2 = matchInfo2.getMatches();
  434. // Average common matches
  435. for (Iterator<?> i = matches1.iterator(); i.hasNext();)
  436. {
  437. Match m1 = (Match) i.next();
  438. for (Iterator<?> j = matches2.iterator(); j.hasNext();)
  439. {
  440. Match m2 = (Match) j.next();
  441. if (m1.equals(m2))
  442. {
  443. double avg = (m1.getEffectiveness() + m2.getEffectiveness()) / (double) 2;
  444. if (avg >= threshold)
  445. {
  446. Match prevMatch = match.getMatchForCandidate(m1.getCandidateTerm());
  447. if (prevMatch != null && avg > prevMatch.getEffectiveness())
  448. {
  449. match.matches.remove(prevMatch);
  450. match.addMatch(m1.getTargetTerm(), m1.getCandidateTerm(), avg);
  451. }
  452. else if (prevMatch == null)
  453. match.addMatch(m1.getTargetTerm(), m1.getCandidateTerm(), avg);
  454. }
  455. }
  456. }
  457. }
  458. // Average matches only on m1
  459. for (Iterator<?> i = matches1.iterator(); i.hasNext();)
  460. {
  461. Match m = (Match) i.next();
  462. if (!match.matches.contains(m))
  463. {
  464. double avg = m.getEffectiveness() / (double) 2;
  465. if (avg >= threshold)
  466. {
  467. Match prevMatch = match.getMatchForCandidate(m.getCandidateTerm());
  468. if (prevMatch != null && avg > prevMatch.getEffectiveness())
  469. {
  470. match.matches.remove(prevMatch);
  471. match.addMatch(m.getTargetTerm(), m.getCandidateTerm(), avg);
  472. }
  473. else if (prevMatch == null)
  474. match.addMatch(m.getTargetTerm(), m.getCandidateTerm(), avg);
  475. }
  476. }
  477. }
  478. // Average matches only on m2
  479. for (Iterator<?> i = matches2.iterator(); i.hasNext();)
  480. {
  481. Match m = (Match) i.next();
  482. if (!match.matches.contains(m))
  483. {
  484. double avg = m.getEffectiveness() / (double) 2;
  485. if (avg >= threshold)
  486. {
  487. Match prevMatch = match.getMatchForCandidate(m.getCandidateTerm());
  488. if (prevMatch != null && avg > prevMatch.getEffectiveness())
  489. {
  490. match.matches.remove(prevMatch);
  491. match.addMatch(m.getTargetTerm(), m.getCandidateTerm(), avg);
  492. }
  493. else if (prevMatch == null)
  494. match.addMatch(m.getTargetTerm(), m.getCandidateTerm(), avg);
  495. }
  496. }
  497. }
  498. // Add mismatches
  499. ArrayList<Mismatch> mismatchesTarget1 = matchInfo1.getMismatchesTargetOntology();
  500. ArrayList<Mismatch> mismatchesCandidate1 = matchInfo1.getMismatchesCandidateOntology();
  501. for (Iterator<Mismatch> i = mismatchesTarget1.iterator(); i.hasNext();)
  502. {
  503. Mismatch m = (Mismatch) i.next();
  504. if (!match.isMatched(m.getTerm()))
  505. match.addMismatchTargetOntology(new Mismatch(m.getTerm()));
  506. }
  507. for (Iterator<Mismatch> i = mismatchesCandidate1.iterator(); i.hasNext();)
  508. {
  509. Mismatch m = (Mismatch) i.next();
  510. if (!match.isMatched(m.getTerm()))
  511. match.addMismatchCandidateOntology(new Mismatch(m.getTerm()));
  512. }
  513. // Add mismatches
  514. ArrayList<Mismatch> mismatchesTarget2 = matchInfo2.getMismatchesTargetOntology();
  515. ArrayList<Mismatch> mismatchesCandidate2 = matchInfo2.getMismatchesCandidateOntology();
  516. for (Iterator<Mismatch> i = mismatchesTarget2.iterator(); i.hasNext();)
  517. {
  518. Mismatch m = (Mismatch) i.next();
  519. if (!match.isMatched(m.getTerm()))
  520. match.addMismatchTargetOntology(new Mismatch(m.getTerm()));
  521. }
  522. for (Iterator<Mismatch> i = mismatchesCandidate2.iterator(); i.hasNext();)
  523. {
  524. Mismatch m = (Mismatch) i.next();
  525. if (!match.isMatched(m.getTerm()))
  526. match.addMismatchCandidateOntology(new Mismatch(m.getTerm()));
  527. }
  528. match.setTargetOntology(matchInfo1.getTargetOntology());
  529. match.setCandidateOntology(matchInfo1.getCandidateOntology());
  530. match.setAlgorithm(matchInfo1.getAlgorithm());
  531. match.setTargetOntologyTermsTotal(matchInfo1.getTargetOntologyTermsTotal());
  532. match.setCandidateOntologyTermsTotal(matchInfo1.getCandidateOntologyTermsTotal());
  533. return match;
  534. }
  535. /**
  536. * Creates a match Information Element from the matchInformation object
  537. *
  538. * @return {@link Element}
  539. */
  540. public Element getXMLRepresentation()
  541. {
  542. NumberFormat nf = NumberFormat.getInstance();
  543. Element matchInformationElement = new Element("matchInformation");
  544. Element targetOntologyElement = new Element("targetOntology");
  545. matchInformationElement.addContent(targetOntologyElement);
  546. targetOntologyElement.setAttribute("name", targetOntology.getName());
  547. Element targetTermsElement = new Element("terms");
  548. targetOntologyElement.addContent(targetTermsElement);
  549. ArrayList<?> targetTerms = OntologyUtilities.denormalizeTerms(OntologyUtilities
  550. .filterTermListRemovingTermsOfClass(
  551. OntologyUtilities.getTermsOfClass(targetOntology, "input"), "hidden"));
  552. targetOntologyElement.setAttribute("totalTerms", targetTerms.size() + "");
  553. for (Iterator<?> i = targetTerms.iterator(); i.hasNext();)
  554. targetTermsElement.addContent(((Term) i.next()).getInputFullNameAsXML());
  555. Element candidateOntologyElement = new Element("candidateOntology");
  556. matchInformationElement.addContent(candidateOntologyElement);
  557. candidateOntologyElement.setAttribute("name", candidateOntology.getName());
  558. Element candidateTermsElement = new Element("terms");
  559. candidateOntologyElement.addContent(candidateTermsElement);
  560. ArrayList<?> candidateTerms = OntologyUtilities.denormalizeTerms(OntologyUtilities
  561. .filterTermListRemovingTermsOfClass(
  562. OntologyUtilities.getTermsOfClass(candidateOntology, "input"), "hidden"));
  563. candidateOntologyElement.setAttribute("totalTerms", candidateTerms.size() + "");
  564. for (Iterator<?> i = candidateTerms.iterator(); i.hasNext();)
  565. candidateTermsElement.addContent(((Term) i.next()).getInputFullNameAsXML());
  566. Element algorithmElement = new Element("algorithm").setText(algorithm.getName());
  567. matchInformationElement.addContent(algorithmElement);
  568. algorithmElement.setAttribute("threshold", nf.format(algorithm.getThreshold()));
  569. Element statisticsElement = new Element("statistics").addContent(new Element("recall")
  570. .setText(nf.format((candidateTerms.size() - mismatchesCandidateOntology.size()) /
  571. (double) candidateTerms.size() * 100)));
  572. matchInformationElement.addContent(statisticsElement);
  573. Element matchesElement = new Element("matches");
  574. matchInformationElement.addContent(matchesElement);
  575. matchesElement.setAttribute("total", matches.size() + "");
  576. for (Iterator<Match> i = matches.iterator(); i.hasNext();)
  577. {
  578. Match match = (Match) i.next();
  579. Element matchElement = new Element("match");
  580. matchesElement.addContent(matchElement);
  581. Element targetElement = new Element("target");
  582. matchElement.addContent(targetElement);
  583. targetElement.addContent(match.getTargetTerm().getInputFullNameAsXML());
  584. Element candidateElement = new Element("candidate");
  585. matchElement.addContent(candidateElement);
  586. candidateElement.addContent(match.getCandidateTerm().getInputFullNameAsXML());
  587. if (match.getEffectiveness() >= 0)
  588. matchElement.setAttribute("confidence", nf.format(match.getEffectiveness()));
  589. }
  590. Element mismatchesElement = new Element("mismatches");
  591. matchInformationElement.addContent(mismatchesElement);
  592. Element targetMismatchesElement = new Element("targetMismatches");
  593. mismatchesElement.addContent(targetMismatchesElement);
  594. for (Iterator<Mismatch> i = mismatchesTargetOntology.iterator(); i.hasNext();)
  595. targetMismatchesElement.addContent(((Mismatch) i.next()).getTerm()
  596. .getInputFullNameAsXML());
  597. Element candidateMismatchesElement = new Element("candidateMismatches");
  598. mismatchesElement.addContent(candidateMismatchesElement);
  599. for (Iterator<Mismatch> i = mismatchesCandidateOntology.iterator(); i.hasNext();)
  600. candidateMismatchesElement.addContent(((Mismatch) i.next()).getTerm()
  601. .getInputFullNameAsXML());
  602. return matchInformationElement;
  603. }
  604. /**
  605. * Prints according to the .dtds files (matchInformation.dtd)
  606. *
  607. * @param file - a {@link File} to write to
  608. * @throws IOException
  609. */
  610. public void saveToXML(File file) throws IOException
  611. {
  612. org.jdom.Element ontologyElement = getXMLRepresentation();
  613. DocType ontologyDocType = new DocType("matchInformation", "matchInformation.dtd");
  614. org.jdom.Document ontologyDocument = new org.jdom.Document(ontologyElement, ontologyDocType);
  615. BufferedWriter out = new BufferedWriter(new FileWriter(file));
  616. XMLOutputter fmt = new XMLOutputter();// new XMLOutputter(" ",true);
  617. fmt.output(ontologyDocument, out);
  618. out.close();
  619. }
  620. /**
  621. * @return String - XML representation in "matchInformation" docType
  622. * @throws IOException
  623. */
  624. public String getXMLRepresentationAsString() throws IOException
  625. {
  626. org.jdom.Element ontologyElement = getXMLRepresentation();
  627. DocType ontologyDocType = new DocType("matchInformation");
  628. org.jdom.Document ontologyDocument = new org.jdom.Document(ontologyElement, ontologyDocType);
  629. StringOutputStream xmlRepresentation = new StringOutputStream();
  630. BufferedWriter out = new BufferedWriter(new OutputStreamWriter(xmlRepresentation));
  631. XMLOutputter fmt = new XMLOutputter();// new XMLOutputter(" ",true);
  632. fmt.output(ontologyDocument, out);
  633. out.close();
  634. return xmlRepresentation.toString();
  635. }
  636. /**
  637. * Removes all matches/mismatches that are instance of "group", and replaces decomposed terms
  638. * with their parents
  639. */
  640. public void denormalize()
  641. {
  642. for (Iterator<Match> i = matches.listIterator(); i.hasNext();)
  643. {
  644. Match match = (Match) i.next();
  645. Term targetTerm = match.getTargetTerm();
  646. Term candidateTerm = match.getCandidateTerm();
  647. if (targetTerm.isInstanceOf("group") || candidateTerm.isInstanceOf("group"))
  648. {
  649. i.remove();
  650. addMismatchTargetOntology(new Mismatch(targetTerm));
  651. addMismatchCandidateOntology(new Mismatch(candidateTerm));
  652. }
  653. else
  654. {
  655. if (targetTerm.isDecomposedTerm())
  656. match.setTargetTerm(targetTerm.getParent());
  657. if (candidateTerm.isDecomposedTerm())
  658. match.setCandidateTerm(candidateTerm.getParent());
  659. }
  660. }
  661. ArrayList<Match> matchesAux = new ArrayList<Match>();
  662. for (Iterator<Match> i = matches.listIterator(); i.hasNext();)
  663. {
  664. Match match = (Match) i.next();
  665. int index = matchesAux.indexOf(match);
  666. if (index != -1)
  667. {
  668. Match matchAux = (Match) matchesAux.get(index);
  669. if (matchAux.getEffectiveness() >= match.getEffectiveness())
  670. i.remove();
  671. else
  672. matchesAux.set(index, match);
  673. }
  674. else
  675. matchesAux.add(match);
  676. }
  677. matches = matchesAux;
  678. for (Iterator<Mismatch> i = mismatchesTargetOntology.listIterator(); i.hasNext();)
  679. if (((Mismatch) i.next()).getTerm().isInstanceOf("group"))
  680. i.remove();
  681. for (Iterator<Mismatch> i = mismatchesCandidateOntology.listIterator(); i.hasNext();)
  682. if (((Mismatch) i.next()).getTerm().isInstanceOf("group"))
  683. i.remove();
  684. }
  685. /**
  686. * @param termName target term name
  687. * @return int - target Index (return -1 if fails to find)
  688. */
  689. // gabi17/2/03
  690. public int getTargetTermIndex(String termName)
  691. {
  692. if (originalCandidateTerms == null)
  693. {
  694. System.out.println("getTargetTerms got NULL");
  695. System.exit(1);
  696. }
  697. for (int i = 0; i < originalTargetTerms.size(); i++)
  698. {
  699. Term term = (Term) (originalTargetTerms.get(i));
  700. if ((term.getName()).equals(termName))
  701. return i;
  702. }
  703. return -1;
  704. }
  705. // gabi 24/3/2002
  706. /**
  707. * @param termName target term name
  708. * @return Term - target term (return null if fails to find)
  709. */
  710. public Term getTargetTerm(String termName)
  711. {
  712. if (originalTargetTerms == null)
  713. {
  714. System.out.println("getTargetTe got NULL");
  715. System.exit(1);
  716. }
  717. for (int i = 0; i < originalTargetTerms.size(); i++)
  718. {
  719. Term term = (Term) (originalTargetTerms.get(i));
  720. if ((term.getName()).equals(termName))
  721. return term;
  722. }
  723. return null;
  724. }
  725. // gabi
  726. /**
  727. * @param termName candidate term name
  728. * @return Term - reference to the candidate term in the matches
  729. */
  730. public Term getCandidateTermInMatches(String termName)
  731. {
  732. for (int i = 0; i < matches.size(); i++)
  733. {
  734. Match match = (Match) (matches.get(i));
  735. Term term = match.getCandidateTerm();
  736. if ((term.getName()).equals(termName))
  737. return term;
  738. }
  739. return null;
  740. }
  741. /**
  742. * @param termName target term name
  743. * @return Term - reference to the target term in the matches
  744. */
  745. public Term getTargetTermInMatches(String termName)
  746. {
  747. for (int i = 0; i < matches.size(); i++)
  748. {
  749. Match match = (Match) (matches.get(i));
  750. Term term = match.getTargetTerm();
  751. if ((OntologyUtilities.oneIdRemoval(term.toString())).equals(termName) ||
  752. (OntologyUtilities.oneIdRemoval(term.toStringVs2())).equals(termName))
  753. return term;
  754. }
  755. return null;
  756. }
  757. /**
  758. * @param termName candidate term name
  759. * @return int - candidate Index (return -1 if fails to find)
  760. */
  761. public int getCandidateTermIndex(String termName)
  762. {
  763. if (originalCandidateTerms == null)
  764. {
  765. System.out.println("getCandidateTermIndex got NULL");
  766. System.exit(1);
  767. }
  768. for (int i = 0; i < originalCandidateTerms.size(); i++)
  769. {
  770. Term term = (Term) (originalCandidateTerms.get(i));
  771. if ((term.getName()).equals(termName))
  772. return i;
  773. }
  774. return -1;
  775. }
  776. /**
  777. * @return ArrayList - original Candidate Terms
  778. */
  779. public ArrayList<?> getOriginalCandidateTerms()
  780. {
  781. return originalCandidateTerms;
  782. }
  783. /**
  784. * @return ArrayList - original Target Terms
  785. */
  786. public ArrayList<?> getOriginalTargetTerms()
  787. {
  788. return originalTargetTerms;
  789. }
  790. /**
  791. * @param target term name
  792. * @param candidate term name
  793. * @param score Effectiveness of match
  794. * @return boolean true if exist in the match list
  795. */
  796. public boolean isExistSameMatch(String target, String candidate, double score)
  797. {
  798. Iterator<Match> it = matches.iterator();
  799. while (it.hasNext())
  800. {
  801. Match match = (Match) it.next();
  802. if (match.getTargetTerm().toString().equals(target) &&
  803. match.getCandidateTerm().toString().equals(candidate) &&
  804. match.getEffectiveness() == score)
  805. {
  806. return true;
  807. }
  808. }
  809. return false;
  810. }
  811. /**
  812. * @param st
  813. * @return double - the maximal Recall between the 2 parsings of the terms
  814. */
  815. public double getRecall(SchemaTranslator st)
  816. {
  817. SchemaTranslator st1_ = new SchemaTranslator(this, true, false);
  818. SchemaTranslator st2_ = new SchemaTranslator(this, true, true);
  819. return Math.max(SchemaMatchingsUtilities.calculateRecall(st, st1_),
  820. SchemaMatchingsUtilities.calculateRecall(st, st2_));
  821. }
  822. /**
  823. * @param st
  824. * @return double - the maximal Precision between the 2 parsings of the terms
  825. */
  826. public double getPrecision(SchemaTranslator st)
  827. {
  828. SchemaTranslator st1_ = new SchemaTranslator(this, true, false);
  829. SchemaTranslator st2_ = new SchemaTranslator(this, true, true);
  830. return Math.max(SchemaMatchingsUtilities.calculatePrecision(st, st1_),
  831. SchemaMatchingsUtilities.calculatePrecision(st, st2_));
  832. }
  833. /**
  834. * @param matches a list of {@link Match}
  835. */
  836. public void setMatches(ArrayList<Match> matches)
  837. {
  838. this.matches = matches;
  839. }
  840. /**
  841. * @param t a target {@link Term}
  842. * @return {@link Match} - the match for the target term, returns null if none is found
  843. */
  844. public Match getMatchForTarget(Term t)
  845. {
  846. for (Iterator<Match> i = matches.iterator(); i.hasNext();)
  847. {
  848. Match match = (Match) i.next();
  849. Term targetTerm = match.getTargetTerm();
  850. if (targetTerm.equals(t))
  851. return match;
  852. }
  853. return null;
  854. }
  855. // end gabi
  856. }