PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/ontopia-engine/src/main/java/net/ontopia/topicmaps/cmdlineutils/statistics/TopicAssocDep.java

http://ontopia.googlecode.com/
Java | 398 lines | 248 code | 60 blank | 90 comment | 64 complexity | d03ce96e3010e32a19738ecb342cbc3e MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. package net.ontopia.topicmaps.cmdlineutils.statistics;
  2. import java.util.*;
  3. import java.lang.reflect.Array;
  4. import net.ontopia.topicmaps.core.*;
  5. import net.ontopia.topicmaps.core.index.*;
  6. import net.ontopia.topicmaps.cmdlineutils.sanity.AssociationSanity;
  7. import net.ontopia.utils.*;
  8. import net.ontopia.topicmaps.utils.*;
  9. import net.ontopia.infoset.core.*;
  10. /**
  11. * Class used for locating associations and their types.
  12. */
  13. public class TopicAssocDep {
  14. private TopicMapIF tm;
  15. private HashMap assocTypes, assocRoleTypes, assocDetails, associations;
  16. private StringifierIF ts = TopicStringifiers.getDefaultStringifier();
  17. private String[] roles;
  18. public TopicAssocDep(TopicMapIF tm) throws NullPointerException {
  19. this.tm = tm;
  20. associations = new HashMap();
  21. traverse(tm.getAssociations());
  22. }
  23. /**
  24. * Returns a set of keys for the associations
  25. */
  26. public Collection getAssociations() {
  27. return associations.keySet();
  28. }
  29. /**
  30. * Gets the id for the association type.
  31. */
  32. public String getAssociationTypeId(String key) {
  33. if (key == null || !associations.containsKey(key))
  34. return "null";
  35. return ((InternalAssociation)associations.get(key)).getAssociationTypeId();
  36. }
  37. /**
  38. * Gets the name of the association.
  39. */
  40. public String getAssociationType(String key) {
  41. if (key == null || !associations.containsKey(key))
  42. return "null";
  43. return ((InternalAssociation)associations.get(key)).getAssociationType();
  44. }
  45. /**
  46. * Gets the number of times this association occurres in the topicmap.
  47. */
  48. public int getNumberOfOccurrences(String key) {
  49. return ((InternalAssociation)associations.get(key)).getNumberOfOccurrences();
  50. }
  51. /**
  52. * Returns a hashmap containg the id of the assocrltype, and the name of assocrltype
  53. */
  54. public HashMap getAssociationRoleTypes(String key) {
  55. if (key == null || !associations.containsKey(key))
  56. return null;
  57. return ((InternalAssociation)associations.get(key)).getAssociationRoleTypes();
  58. }
  59. /**
  60. * Return a hashmap containg the id and the name of all the topictypes for all the
  61. * topics that play in this association.
  62. */
  63. public HashMap getAssociationRoles(String key) {
  64. if (key == null || !associations.containsKey(key))
  65. return null;
  66. return ((InternalAssociation)associations.get(key)).getAssociationRoles();
  67. }
  68. /**
  69. * Returns a Collection of the different associations that have this associationtype.
  70. */
  71. public Collection getAssociationDetails(String key) {
  72. if (key == null || !associations.containsKey(key))
  73. return null;
  74. return ((InternalAssociation)associations.get(key)).getAssociationDetails();
  75. }
  76. /**
  77. * Gets the association roles ordered aplhabetically
  78. */
  79. public String[] getAssociationRoleTypesOrdered(String key) {
  80. if (key == null || !associations.containsKey(key))
  81. return null;
  82. return ((InternalAssociation)associations.get(key)).getAssociationRoleTypesOrdered();
  83. }
  84. public String[] getAssociationDetails(String key, AssociationIF association) {
  85. if (key == null || !associations.containsKey(key))
  86. return null;
  87. return ((InternalAssociation)associations.get(key)).getAssociationDetails(association);
  88. }
  89. //----------------------------------------------------
  90. // Internal methods
  91. //----------------------------------------------------
  92. //Traverses the Topic Map.
  93. private void traverse(Collection assocs) {
  94. Iterator it = assocs.iterator();
  95. while (it.hasNext()) {
  96. AssociationIF temp = (AssociationIF)it.next();
  97. String key = createKey(temp);
  98. InternalAssociation assoc;
  99. if (associations.containsKey(key)) {
  100. assoc = (InternalAssociation)associations.get(key);
  101. assoc.increment();
  102. }
  103. else {
  104. assoc = new InternalAssociation(temp);
  105. associations.put(key, assoc);
  106. }
  107. assoc.addAssociation(temp);
  108. }
  109. }
  110. //Creates a string key for the association.
  111. private String createKey(AssociationIF aif) {
  112. HashMap types = new HashMap();
  113. String assocname = "";
  114. try {
  115. assocname = ts.toString(aif.getType());
  116. } catch (NullPointerException e) {
  117. assocname = "null";
  118. }
  119. //Role names as an array of strings.
  120. Collection roles = aif.getRoles();
  121. Iterator it = roles.iterator();
  122. int size = roles.size();
  123. int i = 0;
  124. String [] rolenames = new String[size];
  125. //Places the rolesnames in the array.
  126. while (it.hasNext()) {
  127. AssociationRoleIF arif = (AssociationRoleIF)it.next();
  128. if (arif.getType() != null)
  129. rolenames[i] = getName(arif.getType());
  130. else
  131. rolenames[i] = "null";
  132. i++;
  133. }
  134. //Sorts the rolenames in lexiographical order.
  135. sortKey(rolenames);
  136. //Places the assocname first in the return string.
  137. String retur = assocname;
  138. //Adds the sorted rolenames using a '$'as a delimiter
  139. for (i = 0; i < rolenames.length; i++) {
  140. retur += "$" + rolenames[i];
  141. }
  142. return retur;
  143. }
  144. /**
  145. * Insert Sort, which sorts an array of strings in lexiographical order
  146. */
  147. private void sortKey(String [] names) {
  148. for (int i = 0; i+1 < names.length; i++) {
  149. if (names[i].compareTo(names[i+1]) > 0){
  150. //Found one, shuffle it to the lowest index possible.
  151. String temp = names[i]; names[i] = names[i+1]; names[i+1] = temp;
  152. int j = i; boolean done = false;
  153. while (j != 0 && !done) {
  154. if (names[j].compareTo(names[j-1]) < 0) {
  155. temp = names[j];
  156. names[j] = names[j-1];
  157. names[j-1] = temp;
  158. } else done = true;
  159. j--;
  160. }//end of while.
  161. }//end of if
  162. }//end of for
  163. }//end of method
  164. public String[] sortAlpha(Collection collection) {
  165. String[] retur = new String[collection.size()];
  166. Iterator it = collection.iterator();
  167. int k = 0;
  168. for (int i = 0; i < collection.size(); i++) {
  169. retur[i] = (String)it.next();
  170. }
  171. //Starting at the first index in the array.
  172. for (int i = 0; i < retur.length - 1; i++) {
  173. if (retur[i].compareTo(retur[i+1]) > 0){
  174. //Found one, shuffle it to the lowest index possible.
  175. String temp = retur[i]; retur[i] = retur[i+1]; retur[i+1] = temp;
  176. int j = i;
  177. boolean done = false;
  178. while (j != 0 && !done) {
  179. if (retur[j].compareTo(retur[j-1]) < 0) {
  180. temp = retur[j]; retur[j] = retur[j-1]; retur[j-1] = temp;
  181. } else done = true;
  182. j--;
  183. }//end of while.
  184. }//end of if
  185. }//end of for
  186. return retur;
  187. }
  188. //Gets the name of the topic
  189. private String getName(TopicIF topic) {
  190. if (topic == null)
  191. return "null";
  192. else {
  193. String name = ts.toString(topic);
  194. if (name.equalsIgnoreCase("[No name]"))
  195. name = "{topicid: " + topic.getObjectId() + "}";
  196. return name;
  197. }
  198. }
  199. /**
  200. * Innerclass that is used to represent each type of association, and
  201. * also keep track of all the different assocations of this type.
  202. */
  203. private class InternalAssociation {
  204. AssociationIF association;
  205. TopicIF associationtype;
  206. HashMap roleTypes;
  207. HashMap assocRoles;
  208. int number_of_assocs;
  209. Collection associations;
  210. String[] roles;
  211. InternalAssociation(AssociationIF association) {
  212. this.associationtype = association.getType();
  213. this.association = association;
  214. roleTypes = addAssocRoleTypes(association);
  215. number_of_assocs = 1;
  216. assocRoles = new HashMap();
  217. associations = new ArrayList();
  218. //roles = getAssociationRoleTypesOrdered();
  219. }
  220. /**
  221. * Returns the object id for the association type
  222. */
  223. protected String getAssociationTypeId() {
  224. return (associationtype == null ? "null" : associationtype.getObjectId());
  225. }
  226. /**
  227. * Returns the association type.
  228. */
  229. protected String getAssociationType() {
  230. return getName(associationtype);
  231. }
  232. /**
  233. * Returns the number of occurrences for this association
  234. */
  235. protected int getNumberOfOccurrences() {
  236. return number_of_assocs;
  237. }
  238. /**
  239. * Returns a hashmap containg the id of the assocrltype, and the name of assocrltype
  240. */
  241. protected HashMap getAssociationRoleTypes() {
  242. return roleTypes;
  243. }
  244. /**
  245. * Return a hashmap containg the id and the name of all the topictypes for all the
  246. * topics that play in this association.
  247. */
  248. protected HashMap getAssociationRoles() {
  249. return assocRoles;
  250. }
  251. /**
  252. * Returns all the associations of this type.
  253. */
  254. protected Collection getAssociationDetails() {
  255. return associations;
  256. }
  257. /**
  258. * Returns the players of of this association ordred by the role types.
  259. */
  260. protected String[] getAssociationDetails(AssociationIF association) {
  261. String[] result = new String[association.getRoles().size()];
  262. Iterator it = association.getRoles().iterator();
  263. while (it.hasNext()) {
  264. AssociationRoleIF role = (AssociationRoleIF)it.next();
  265. String name = getName(role.getType());
  266. roles = getAssociationRoleTypesOrdered();
  267. for (int i = 0; i < roles.length; i++) {
  268. // Inserts the correct player name and id in the correct alphabetical order.
  269. if (name.equals(roles[i]) && result[i] == null) {
  270. result[i] = getName(role.getPlayer()) + "$" + role.getPlayer().getObjectId();
  271. }
  272. }
  273. }
  274. return result;
  275. }
  276. /**
  277. * Gets the assoc types ordered.
  278. */
  279. protected String[] getAssociationRoleTypesOrdered() {
  280. Collection roletypes = new ArrayList();
  281. Iterator it = association.getRoles().iterator();
  282. while (it.hasNext()) {
  283. String name = getName(((AssociationRoleIF)it.next()).getType());
  284. roletypes.add(name);
  285. }
  286. return sortAlpha(roletypes);
  287. }
  288. /**
  289. * Increments the number of associations with the same role types.
  290. */
  291. protected void increment() {
  292. number_of_assocs++;
  293. }
  294. /**
  295. * Adds an association to the collection of assocation with the same
  296. * types, and roles.
  297. */
  298. protected void addAssociation(AssociationIF association) {
  299. associations.add(association);
  300. addAssociationRoles(association);
  301. }
  302. /**
  303. * Gets the id for the assocrltypes, and their names.
  304. */
  305. private HashMap addAssocRoleTypes(AssociationIF association) {
  306. HashMap temp = new HashMap();
  307. Iterator it = association.getRoles().iterator();
  308. while (it.hasNext()) {
  309. AssociationRoleIF assocrl = (AssociationRoleIF) it.next();
  310. TopicIF type = assocrl.getType();
  311. if (type != null)
  312. temp.put(type.getObjectId(), getName(type));
  313. else
  314. temp.put("null", getName(null));
  315. }
  316. return temp;
  317. }
  318. /**
  319. * Adds the id for all the different topic types that play a role
  320. * in this association.
  321. */
  322. private void addAssociationRoles(AssociationIF association) {
  323. Iterator it = association.getRoles().iterator();
  324. while (it.hasNext()) {
  325. AssociationRoleIF assocrl = (AssociationRoleIF)it.next();
  326. if (assocrl.getPlayer() == null) {
  327. if (!assocRoles.containsValue(getName(null))) assocRoles.put("null", getName(null));
  328. } else {
  329. Iterator iter = assocrl.getPlayer().getTypes().iterator();
  330. while (iter.hasNext()) {
  331. TopicIF temp_topic = (TopicIF)iter.next();
  332. assocRoles.put(temp_topic.getObjectId(), getName(temp_topic));
  333. }
  334. }
  335. }
  336. }
  337. } // inner class
  338. }//end of class.