certainTrustTViz.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /**
  2. * CertainTrust SDK
  3. *
  4. * Implements the computational trust model "CertainTrust"
  5. * in JavaScript.
  6. * See <http://www.tk.informatik.tu-darmstadt.de/de/research/smart-security-and-trust/> for further details.
  7. *
  8. *
  9. * Telecooperation Department, Technische Universität Darmstadt
  10. * <http://www.tk.informatik.tu-darmstadt.de/>
  11. *
  12. * Prof. Dr. Max Mühlhäuser <max@informatik.tu-darmstadt.de>
  13. * Florian Volk <florian.volk@cased.de>
  14. *
  15. *
  16. * @author Daniel Dieth
  17. * @author David Kalnischkies
  18. * @author Maria Pelevina
  19. * @version 1.0
  20. */
  21. /* This Source Code Form is subject to the terms of the Mozilla Public
  22. * License, v. 2.0. If a copy of the MPL was not distributed with this
  23. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  24. //##########################################################################################
  25. // CertainTrustTViz constructor
  26. //##########################################################################################
  27. var CertainTrustTViz = function(data, config) {
  28. this.NR = CertainTrustTVizElement.length();
  29. CertainTrustTVizElement.push(this);
  30. this.certainTrusts = [];
  31. this.add(data);
  32. // set sane defaults for config if nothing is set
  33. if (config === undefined) config = {};
  34. if (config.id === undefined) config.id = 'certaintrust-tviz-' + this.NR;
  35. // design your widget
  36. if (config.canvas === undefined) config.canvas = {};
  37. if (config.canvas.height === undefined) config.canvas.height = 440;
  38. if (config.canvas.width === undefined) config.canvas.width = 440;
  39. if (config.canvas.inner === undefined) config.canvas.inner = 30;
  40. if (config.middle === undefined) config.middle = 'AVERAGE';
  41. if (config.onClick === undefined) config.onClick = undefined;
  42. if (config.onMiddleClick === undefined) config.onMiddleClick = undefined;
  43. this.ID = config.id;
  44. this.config = config;
  45. var element = document.createElement('div');
  46. element.setAttribute('id', this.ID);
  47. var dom = this.certainTrusts[0]._insertElement(config, element);
  48. this.update();
  49. if (dom !== undefined)
  50. return dom;
  51. };
  52. CertainTrustTViz.prototype.update = function() {
  53. var xmlns = 'http://www.w3.org/2000/svg';
  54. var dotx = this.config.canvas.height / 2;
  55. var doty = this.config.canvas.width / 2;
  56. var rotate = 360 - 45;
  57. var circle_y = this.config.canvas.inner;
  58. var circle_height = dotx - (2 * circle_y);
  59. var rotationstep = 360 / this.certainTrusts.length;
  60. var element = document.getElementById(this.ID);
  61. // see if we have to redraw thanks to an added/removed CertainTrust element
  62. if (element.hasChildNodes() === true) {
  63. var old_arcs = document.getElementById(this.ID + '-arcs');
  64. if (old_arcs.childNodes.length !== this.certainTrusts.length)
  65. element.removeChild(element.firstChild);
  66. }
  67. var arcs = document.createElementNS(xmlns, 'g');
  68. arcs.setAttribute('stroke', 'none');
  69. arcs.setAttribute('class', 'certaintrust-tviz-arcs');
  70. arcs.setAttribute('id', this.ID + '-arcs');
  71. // first time run or removed by the if above: full build
  72. if (element.hasChildNodes() === false)
  73. {
  74. // prepare the various elements we need
  75. var axes = document.createElementNS(xmlns, 'g');
  76. axes.setAttribute('fill', 'none');
  77. axes.setAttribute('stroke', 'grey');
  78. axes.setAttribute('class', 'certaintrust-tviz-axes');
  79. var desc = document.createElementNS(xmlns, 'g');
  80. for (var a = 0; a < this.certainTrusts.length; ++a) {
  81. var rotation = (rotate + (rotationstep * a)) % 360;
  82. // an axis for each arc to show of how big each arc could be
  83. var axis = this._generateAxis(xmlns, dotx, doty, circle_height, circle_y, rotation);
  84. axes.appendChild(axis);
  85. // the arc representing the CertainTrust element
  86. var arc = this._generateArc(xmlns, this.certainTrusts[a], dotx, doty, circle_height, circle_y, rotation, rotationstep);
  87. arcs.appendChild(arc);
  88. // label for each arc
  89. var label = this._generateArcLabel(xmlns, this.certainTrusts[a], dotx, doty, circle_height, circle_y, rotation, rotationstep);
  90. desc.appendChild(label);
  91. }
  92. if (this.config.middle !== 'NONE') {
  93. var total = this._generateTotalInTheMiddle(xmlns, dotx, doty);
  94. desc.appendChild(total);
  95. }
  96. // draw circles to indicate how large the arcs could be
  97. var circles = this._generateCircles(xmlns, circle_y, circle_height);
  98. // our new tviz
  99. var svg = document.createElementNS(xmlns, 'svg');
  100. svg.setAttribute('height', this.config.canvas.height);
  101. svg.setAttribute('width', this.config.canvas.width);
  102. svg.appendChild(circles);
  103. svg.appendChild(arcs);
  104. svg.appendChild(axes);
  105. svg.appendChild(desc);
  106. // finally, insert tviz
  107. element.appendChild(svg);
  108. } else {
  109. // we have most of the image, so just change what has changed
  110. element = element.firstChild;
  111. // get the new arcs set
  112. for (var a = 0; a < this.certainTrusts.length; ++a) {
  113. var rotation = (rotate + (rotationstep * a)) % 360;
  114. var arc = this._generateArc(xmlns, this.certainTrusts[a], dotx, doty, circle_height, circle_y, rotation, rotationstep);
  115. arcs.appendChild(arc);
  116. }
  117. // find the old arcs set and exchange it with the new set
  118. var old_arcs = document.getElementById(this.ID + '-arcs');
  119. element.insertBefore(arcs, old_arcs);
  120. element.removeChild(old_arcs);
  121. if (this.config.middle !== 'NONE') {
  122. // and now change the text of label in the middle
  123. var total = document.getElementById(this.ID + '-middle');
  124. total.firstChild.nodeValue = this._calcMiddleLabel(this.certainTrusts);
  125. }
  126. }
  127. };
  128. CertainTrustTViz.prototype.add = function() {
  129. for (var i = 0; i < arguments.length; ++i) {
  130. var ct = arguments[i];
  131. if (ct instanceof Array) {
  132. for (var j = 0; j < ct.length; ++j) {
  133. var c = ct[j];
  134. this.certainTrusts.push(c);
  135. c.addObserver(this);
  136. }
  137. } else {
  138. this.certainTrusts.push(ct);
  139. ct.addObserver(this);
  140. }
  141. }
  142. // don't trigger on first call from constructor
  143. if (this.ID !== undefined)
  144. this.update();
  145. };
  146. CertainTrustTViz.prototype.remove = function() {
  147. for (var i = 0; i < arguments.length; ++i) {
  148. var ct = arguments[i];
  149. if (ct instanceof CertainTrust) {
  150. for (var j = 0; j < ct.length; ++j) {
  151. for (var k = 0; k < this.certainTrusts.length; ++k) {
  152. if (this.certainTrusts[k] !== ct[j])
  153. continue;
  154. ct[j].deleteObserver(this);
  155. this.certainTrusts.splice(k, 1);
  156. break;
  157. }
  158. }
  159. } else {
  160. for (var k = 0; k < this.certainTrusts.length; ++k) {
  161. if (this.certainTrusts[k].getName() !== ct)
  162. continue;
  163. this.certainTrusts[k].deleteObserver(this);
  164. this.certainTrusts.splice(k, 1);
  165. break;
  166. }
  167. }
  168. }
  169. this.update();
  170. };
  171. CertainTrustTViz.prototype._generateAxis = function(xmlns, dotx, doty, circle_height, circle_y, rotation) {
  172. var point = this.certainTrusts[0]._pointOnCircle(dotx, doty, rotation, circle_y);
  173. var axis = document.createElementNS(xmlns, 'g');
  174. axis.setAttribute('fill', 'none');
  175. axis.setAttribute('stroke', 'grey');
  176. var placement;
  177. if (rotation >= 270 && rotation < 360 || rotation >= 0 && rotation < 90) {
  178. axis.setAttribute('transform', 'translate(' + point[1] + ',' + point[0] + ') rotate(' + (rotation) + ')');
  179. axis.setAttribute('class', 'certaintrust-tviz-axis-left');
  180. placement = -1;
  181. } else {
  182. axis.setAttribute('transform', 'translate(' + point[1] + ',' + point[0] + ') rotate(' + (rotation - 180) + ')');
  183. axis.setAttribute('class', 'certaintrust-tviz-axis-right');
  184. placement = 1;
  185. }
  186. var axis_line = document.createElementNS(xmlns, 'line');
  187. axis_line.setAttribute('fill', 'inherit');
  188. axis_line.setAttribute('stroke', 'inherit');
  189. axis_line.setAttribute('x1', 0);
  190. axis_line.setAttribute('y1', 0);
  191. axis_line.setAttribute('x2', 0);
  192. axis_line.setAttribute('y2', placement * circle_height);
  193. axis.appendChild(axis_line);
  194. for (var t = 0; t < 6; ++t) {
  195. var y = placement * (circle_height * (t / 5));
  196. var tick_line = document.createElementNS(xmlns, 'line');
  197. tick_line.setAttribute('fill', 'inherit');
  198. tick_line.setAttribute('stroke', 'inherit');
  199. tick_line.setAttribute('x1', 0);
  200. tick_line.setAttribute('y1', y);
  201. tick_line.setAttribute('x2', placement * 6);
  202. tick_line.setAttribute('y2', y);
  203. var tick_text = document.createElementNS(xmlns, 'text');
  204. tick_text.setAttribute('fill', 'grey');
  205. tick_text.setAttribute('x', placement * 9);
  206. tick_text.setAttribute('y', y);
  207. tick_text.setAttribute('dy', '.5em');
  208. tick_text.setAttribute('font-size', '.5em');
  209. var msg = document.createTextNode(t * 20);
  210. tick_text.appendChild(msg);
  211. var tick = document.createElementNS(xmlns, 'g');
  212. tick.setAttribute('fill', 'inherit');
  213. tick.setAttribute('stroke', 'inherit');
  214. tick.appendChild(tick_line);
  215. tick.appendChild(tick_text);
  216. axis.appendChild(tick);
  217. }
  218. return axis;
  219. };
  220. CertainTrustTViz.prototype._generateArc = function(xmlns, certainTrust, dotx, doty, circle_height, circle_y, rotation, rotationstep) {
  221. var arc_width = rotationstep * certainTrust.getC();
  222. var arc_free = (rotationstep - arc_width) / 2;
  223. var arc_low_left = certainTrust._pointOnCircle(dotx, doty, (rotation + arc_free), circle_y);
  224. var arc_low_right = certainTrust._pointOnCircle(dotx, doty, (rotation + rotationstep - arc_free), circle_y);
  225. var arc_height = (circle_height * certainTrust.getT()) + circle_y;
  226. var arc_high_left = certainTrust._pointOnCircle(dotx, doty, (rotation + arc_free), arc_height);
  227. var arc_high_right = certainTrust._pointOnCircle(dotx, doty, (rotation + rotationstep - arc_free), arc_height);
  228. var color = certainTrust._getColor(certainTrust.getC(), certainTrust.getT(), certainTrust.getF());
  229. var arc = document.createElementNS(xmlns, 'path');
  230. arc.setAttribute('fill', 'rgb(' + Math.round(color[0]) + ',' + Math.round(color[1]) + ',' + Math.round(color[2]) + ')');
  231. arc.setAttribute('stroke', 'inherit');
  232. arc.setAttribute('d', 'M' + arc_low_left[1] + ' ' + arc_low_left[0] +
  233. 'A' + circle_y + ',' + circle_y + ' 0 0,1 ' + arc_low_right[1] + ',' + arc_low_right[0] +
  234. 'L' + arc_high_right[1] + ' ' + arc_high_right[0] +
  235. 'A' + arc_height + ',' + arc_height + ' 0 0,0 ' + arc_high_left[1] + ',' + arc_high_left[0] +
  236. 'z');
  237. arc.setAttribute('title', 'Trust: ' + certainTrust.getT() + '\nCertainty: ' + certainTrust.getC());
  238. if (this.config.onClick !== undefined)
  239. {
  240. var onClick = this.config.onClick;
  241. arc.addEventListener("click", function(e) { onClick(certainTrust); }, false);
  242. }
  243. return arc;
  244. };
  245. CertainTrustTViz.prototype._generateArcLabel = function(xmlns, certainTrust, dotx, doty, circle_height, circle_y, rotation, rotationstep) {
  246. var label = document.createElementNS(xmlns, 'text');
  247. label.setAttribute('fill', 'black');
  248. var outerpoint = this.certainTrusts[0]._pointOnCircle(dotx, doty, (rotation + (rotationstep / 2)), (circle_height + circle_y));
  249. label.setAttribute('x', outerpoint[1]);
  250. label.setAttribute('y', outerpoint[0]);
  251. label.setAttribute('dy', '.5em');
  252. label.setAttribute('text-anchor', 'middle');
  253. var msg = document.createTextNode(certainTrust.getName());
  254. label.appendChild(msg);
  255. return label;
  256. };
  257. CertainTrustTViz.prototype._generateTotalInTheMiddle = function(xmlns, dotx, doty) {
  258. var total = document.createElementNS(xmlns, 'text');
  259. total.setAttribute('fill', 'grey');
  260. total.setAttribute('x', dotx);
  261. total.setAttribute('y', doty);
  262. total.setAttribute('dy', '.5em');
  263. total.setAttribute('text-anchor', 'middle');
  264. total.setAttribute('id', this.ID + '-middle');
  265. var msg = document.createTextNode(this._calcMiddleLabel(this.certainTrusts));
  266. total.appendChild(msg);
  267. if (this.config.onMiddleClick !== undefined)
  268. {
  269. var onMiddleClick = this.config.onMiddleClick;
  270. total.addEventListener("click", function(e) { onMiddleClick(); }, false);
  271. }
  272. return total;
  273. };
  274. CertainTrustTViz.prototype._generateCircles = function(xmlns, circle_y, circle_height) {
  275. var circles = document.createElementNS(xmlns, 'g');
  276. circles.setAttribute('fill', 'none');
  277. circles.setAttribute('stroke', 'lightgrey');
  278. circles.setAttribute('class', 'certaintrust-tviz-circles');
  279. for (var i = 0; i < 11; ++i) {
  280. var circle = document.createElementNS(xmlns, 'circle');
  281. circle.setAttribute('cx', '50%');
  282. circle.setAttribute('cy', '50%');
  283. var r = circle_y + (circle_height * (i / 10));
  284. circle.setAttribute('r', r);
  285. circle.setAttribute('fill', 'inherit');
  286. circle.setAttribute('stroke', 'inherit');
  287. circles.appendChild(circle);
  288. }
  289. return circles;
  290. };
  291. // Calculate the average over all included CertainTrusts
  292. CertainTrustTViz.prototype._calcMiddleLabel = function(indata) {
  293. if (this.config.middle === "NONE")
  294. return "";
  295. else if (this.config.middle === "AVERAGE") {
  296. var avg = 0.0;
  297. for (var i = 0; i < indata.length; i++) {
  298. avg += indata[i].getExpectation();
  299. }
  300. return (Math.round(avg/indata.length*1000)/10) + "%";
  301. } else if (this.config.middle === "AND") {
  302. var result = indata[0];
  303. for (var i = 1; i < indata.length; i++) {
  304. result = result.AND(indata[i]);
  305. }
  306. return (Math.round(result.getExpectation()*1000)/10) + "%";
  307. } else
  308. return this.config.middle(indata);
  309. };
  310. // global var for storing and accessing all the widgets
  311. var CertainTrustTVizElement = { _elements: [],
  312. ById: function(id) {
  313. for (var i = 0; i < CertainTrustTVizElement._elements.length; ++i) {
  314. if (CertainTrustTVizElement._elements[i].ID === id)
  315. return CertainTrustTVizElement._elements[i];
  316. }
  317. return null;
  318. },
  319. ByNr: function(nr) { return CertainTrustTVizElement._elements[nr]; },
  320. push: function(cte) { CertainTrustTVizElement._elements.push(cte); },
  321. length: function() { return CertainTrustTVizElement._elements.length; }
  322. };