certainTrustTViz.js 13 KB

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