[188] | 1 | function Viewer() { |
---|
| 2 | this.Mode = { |
---|
| 3 | LINK: 0, |
---|
| 4 | EMBEDDED: 1, |
---|
| 5 | TEXTAREA: 2, |
---|
| 6 | CODE: 3 |
---|
| 7 | }; |
---|
| 8 | |
---|
| 9 | this._context = undefined; |
---|
[217] | 10 | this._urlToXML = "http://www.framsticks.com/files/apps/config/f0def.xml"; |
---|
[188] | 11 | this._xml = undefined; |
---|
| 12 | this._model = new Model(); |
---|
| 13 | this._part = new Part(); |
---|
| 14 | this._joint = new Joint(); |
---|
| 15 | this._neuro = new Neuro(); |
---|
| 16 | this._neuroConn = new NeuroConn(); |
---|
| 17 | this._parts = []; |
---|
| 18 | this._joints = []; |
---|
| 19 | this._neurons = []; |
---|
| 20 | this._neuroConns = []; |
---|
| 21 | this._neuroClasses = {}; |
---|
| 22 | this._graphicsEngine = undefined; |
---|
| 23 | this._neuronDrawer = undefined; |
---|
| 24 | this.downloadXML(); |
---|
| 25 | this.parseGeneXml(); |
---|
[208] | 26 | this.VisualizationSettings = { |
---|
| 27 | context: undefined, |
---|
| 28 | width: undefined, |
---|
| 29 | height: undefined |
---|
| 30 | }; |
---|
| 31 | this.NetworkSettings = { |
---|
| 32 | context: undefined, |
---|
| 33 | width: undefined, |
---|
| 34 | height: undefined |
---|
| 35 | }; |
---|
[188] | 36 | } |
---|
| 37 | |
---|
| 38 | Viewer.prototype.downloadXML = function () { |
---|
| 39 | var local = this; |
---|
| 40 | $.ajax({ |
---|
| 41 | url: local._urlToXML, |
---|
| 42 | dataType: "xml", |
---|
| 43 | async: false, |
---|
| 44 | success: function (xml) { |
---|
| 45 | var xmlDoc = $.parseXML(xml); |
---|
| 46 | $xml = $(xmlDoc); |
---|
| 47 | local._xml = $(xml); |
---|
| 48 | }, |
---|
| 49 | error: function () { |
---|
| 50 | alert("Can't download file f0def.xml"); |
---|
| 51 | } |
---|
| 52 | }) |
---|
| 53 | }; |
---|
| 54 | |
---|
| 55 | Viewer.prototype.parseGeneXml = function () { |
---|
| 56 | this._parseClass($(this._xml.find("CLASS"))); |
---|
| 57 | this._parseNeuroClass($(this._xml.find("NEUROCLASS"))) |
---|
| 58 | }; |
---|
| 59 | |
---|
| 60 | Viewer.prototype._parseClass = function (nodes) { |
---|
| 61 | var local = this; |
---|
| 62 | |
---|
| 63 | nodes.each(function (entry) { |
---|
| 64 | var node = $(nodes[entry]); |
---|
| 65 | |
---|
| 66 | if (node.attr("NAME") == "Model") { |
---|
| 67 | local._model.setModel(node); |
---|
| 68 | } |
---|
| 69 | else if (node.attr("NAME") == "Part") |
---|
| 70 | local._part.setModel(node); |
---|
| 71 | else if (node.attr("NAME") == "Joint") |
---|
| 72 | local._joint.setModel(node); |
---|
| 73 | else if (node.attr("NAME") == "Neuro") |
---|
| 74 | local._neuro.setModel(node); |
---|
| 75 | else if (node.attr("NAME") == "NeuroConn") |
---|
| 76 | local._neuroConn.setModel(node); |
---|
| 77 | else { |
---|
| 78 | console.log("Could not recognize NAME:", node.attr("NAME")); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | }); |
---|
| 82 | }; |
---|
| 83 | |
---|
| 84 | Viewer.prototype._parseNeuroClass = function (data) { |
---|
| 85 | |
---|
| 86 | for (var i = 0; i < data.length; i++) { |
---|
| 87 | var neuroClass = new NeuroClass(); |
---|
| 88 | this._neuroClasses[data[i].attributes[0].value] = neuroClass; |
---|
| 89 | neuroClass.setModel(data); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | }; |
---|
| 93 | |
---|
| 94 | Viewer.prototype.analyseLine = function (line) { |
---|
| 95 | //ignore comment |
---|
| 96 | if (line[0] == '#' || line == "") |
---|
| 97 | return; |
---|
| 98 | |
---|
| 99 | var object; |
---|
| 100 | var type = line[0]; |
---|
| 101 | if (type == "p") { |
---|
| 102 | object = $.extend(true, {}, this._part); |
---|
| 103 | this._parts.push(object); |
---|
| 104 | } |
---|
| 105 | else if (type == "j") { |
---|
| 106 | object = $.extend(true, {}, this._joint); |
---|
| 107 | this._joints.push(object); |
---|
| 108 | } |
---|
| 109 | else if (type == "n") { |
---|
| 110 | object = $.extend(true, {}, this._neuro); |
---|
| 111 | this._neurons.push(object); |
---|
| 112 | } |
---|
| 113 | else if (type == "c") { |
---|
| 114 | object = $.extend(true, {}, this._neuroConn); |
---|
| 115 | this._neuroConns.push(object); |
---|
| 116 | } |
---|
| 117 | else if (type == "m") { |
---|
| 118 | object = $.extend(true, {}, this._model); |
---|
| 119 | this._modelOne = object; |
---|
| 120 | } |
---|
| 121 | else |
---|
| 122 | throw new Error("Undefined element: \"" + type + "\""); |
---|
| 123 | |
---|
| 124 | //remove char and ":" |
---|
| 125 | line = line.substring(2); |
---|
| 126 | var lines = line.match(/([^,"]+|"[^"]+")+/g); |
---|
| 127 | |
---|
| 128 | if (lines != null) |
---|
| 129 | lines.forEach(function (value) { |
---|
| 130 | value = value.trim(); |
---|
| 131 | if (value == "") |
---|
| 132 | object.setValue(); |
---|
| 133 | else if (value.indexOf("=") == -1) { |
---|
| 134 | object.setValue(value); |
---|
| 135 | } |
---|
| 136 | else { |
---|
| 137 | var name = value.substring(0, value.indexOf("=")); |
---|
| 138 | var val = value.substring(value.indexOf("=") + 1); |
---|
| 139 | object.setValue(name, val); |
---|
| 140 | } |
---|
| 141 | }) |
---|
| 142 | }; |
---|
| 143 | |
---|
| 144 | Viewer.prototype.getCreature = function (mode, content) { |
---|
| 145 | |
---|
| 146 | var lines = undefined; |
---|
| 147 | if (mode == this.Mode.LINK) |
---|
| 148 | lines = this._getCreatureFromLink(content); |
---|
| 149 | else if (mode == this.Mode.EMBEDDED) |
---|
| 150 | lines = this._getCreatureFromEmbeddedCode(content); |
---|
| 151 | else if (mode == this.Mode.TEXTAREA) |
---|
| 152 | lines = this._getCreatureFromTextArea(content); |
---|
| 153 | else if (mode == this.Mode.CODE) |
---|
| 154 | lines = this._getCreatureFromCode(content); |
---|
| 155 | else { |
---|
| 156 | throw "Wrong mode in function getCreature"; |
---|
| 157 | } |
---|
| 158 | return lines; |
---|
| 159 | }; |
---|
| 160 | |
---|
| 161 | Viewer.prototype._getCreatureFromLink = function (link) { |
---|
| 162 | var lines = undefined; |
---|
| 163 | $.ajax({ |
---|
| 164 | url: link, |
---|
| 165 | async: false, |
---|
| 166 | dataType: "text", |
---|
| 167 | success: function (data) { |
---|
| 168 | lines = data.split("\n"); |
---|
| 169 | |
---|
| 170 | }, |
---|
| 171 | error: function () { |
---|
| 172 | alert("Can't download creature"); |
---|
| 173 | } |
---|
| 174 | }); |
---|
| 175 | return lines; |
---|
| 176 | }; |
---|
| 177 | |
---|
| 178 | Viewer.prototype._getCreatureFromTextArea = function (area) { |
---|
| 179 | var lines = undefined; |
---|
| 180 | lines = area.val(); |
---|
| 181 | lines = lines.split("\n"); |
---|
| 182 | return lines; |
---|
| 183 | }; |
---|
| 184 | |
---|
| 185 | Viewer.prototype._getCreatureFromCode = function (code) { |
---|
| 186 | var lines = code; |
---|
| 187 | lines = lines.split("\n"); |
---|
| 188 | return lines; |
---|
| 189 | }; |
---|
| 190 | |
---|
| 191 | Viewer.prototype._getCreatureFromEmbeddedCode = function (link) { |
---|
| 192 | var lines = undefined; |
---|
| 193 | |
---|
| 194 | $.ajax({ |
---|
| 195 | url: link, |
---|
| 196 | async: false, |
---|
| 197 | dataType: "html", |
---|
| 198 | success: function (data) { |
---|
| 199 | var geno = $($(data).find("div")[0]).comments().html(); |
---|
| 200 | geno = geno.replace("FRED_GEN", ""); |
---|
| 201 | lines = geno.split("\n"); |
---|
| 202 | }, |
---|
| 203 | error: function () { |
---|
| 204 | alert("Can't download creature"); |
---|
| 205 | } |
---|
| 206 | }); |
---|
| 207 | |
---|
| 208 | return lines; |
---|
| 209 | }; |
---|
| 210 | |
---|
| 211 | Viewer.prototype.parseCreature = function (mode, content) { |
---|
| 212 | var lines = this.getCreature(mode, content); |
---|
| 213 | lines.splice(0, 1); |
---|
| 214 | var local = this; |
---|
| 215 | lines.forEach(function (value) { |
---|
| 216 | local.analyseLine(value); |
---|
| 217 | }); |
---|
| 218 | }; |
---|
| 219 | |
---|
| 220 | Viewer.prototype.renderCreature = function () { |
---|
| 221 | |
---|
| 222 | if ($("#axisBox").is(":checked")) |
---|
| 223 | this._graphicsEngine.showPartAxis(); |
---|
| 224 | |
---|
| 225 | for (var i = 0; i < this._parts.length; i++) |
---|
| 226 | this._graphicsEngine.addPart(this._parts[i]) |
---|
| 227 | |
---|
| 228 | for (var i = 0; i < this._joints.length; i++) { |
---|
| 229 | this._graphicsEngine.addJoint(this._joints[i]); |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | }; |
---|
| 233 | |
---|
| 234 | Viewer.prototype.run = function (mode, content) { |
---|
| 235 | this.parseCreature(mode, content); |
---|
| 236 | |
---|
[208] | 237 | if (this.VisualizationSettings.context) { |
---|
| 238 | this._graphicsEngine = new GraphicsEngine(this.VisualizationSettings.context, this.VisualizationSettings.width, this.VisualizationSettings.height); |
---|
[188] | 239 | this._graphicsEngine.initializeScene(); |
---|
| 240 | this.renderCreature(); |
---|
| 241 | this._graphicsEngine.debugTest(); |
---|
| 242 | this._graphicsEngine.renderScene(); |
---|
| 243 | } |
---|
[208] | 244 | if (this.NetworkSettings.context) { |
---|
| 245 | this._neuronDrawer = new NeuronDrawer(this.NetworkSettings.context, this.NetworkSettings.width, this.NetworkSettings.height ); |
---|
| 246 | this._neuronDrawer.initializeNewCanvas(); |
---|
[216] | 247 | var layout = new SmartLayout(this._neurons, this._neuroConns); |
---|
| 248 | layout.create(); |
---|
| 249 | this._neuronDrawer.drawNeuralNetwork(this._neurons, this._neuroConns, layout.einfos, this._neuroClasses); |
---|
[208] | 250 | this._neuronDrawer.finalize(); |
---|
[188] | 251 | } |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | function openTextAreaCreature() { |
---|
| 255 | var viewer = new Viewer(); |
---|
| 256 | |
---|
[208] | 257 | viewer.VisualizationSettings.context = $("#container"); |
---|
| 258 | viewer.VisualizationSettings.width = 400; |
---|
| 259 | viewer.VisualizationSettings.height = 400; |
---|
| 260 | |
---|
| 261 | viewer.NetworkSettings.context = "containerNeuron"; |
---|
[188] | 262 | viewer.run(viewer.Mode.TEXTAREA, $("#geno")); |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | function openEmbeddedCreature() { |
---|
| 266 | var viewer = new Viewer(); |
---|
[208] | 267 | viewer.VisualizationSettings.context = $("#container1"); |
---|
| 268 | viewer.VisualizationSettings.width = 400; |
---|
| 269 | viewer.VisualizationSettings.height = 400; |
---|
| 270 | viewer.NetworkSettings.context = "containerNeuron1"; |
---|
| 271 | viewer.NetworkSettings.width = 400; |
---|
| 272 | viewer.NetworkSettings.height = 400; |
---|
[188] | 273 | viewer.run(viewer.Mode.EMBEDDED, "http://ec.framsticks.com/www/index.php?PAGE=view_genotype&ID=55"); |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | function openCodeCreature() { |
---|
| 277 | var viewer = new Viewer(); |
---|
[208] | 278 | viewer.VisualizationSettings.context = $("#container2"); |
---|
| 279 | viewer.VisualizationSettings.width = 400; |
---|
| 280 | viewer.VisualizationSettings.height = 400; |
---|
| 281 | viewer.NetworkSettings.context = "containerNeuron2"; |
---|
| 282 | viewer.NetworkSettings.width = 400; |
---|
| 283 | viewer.NetworkSettings.height = 400; |
---|
[188] | 284 | viewer.run(viewer.Mode.CODE, "//0\np:\np: x=1"); |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | function openFileCreature() { |
---|
| 288 | var viewer = new Viewer(); |
---|
[208] | 289 | //var debugCreatureName = "example4"; |
---|
| 290 | //var debugCreatureName = "caterpillar"; |
---|
[214] | 291 | //var debugCreatureName = "Quadro"; |
---|
| 292 | var debugCreatureName = "spider"; |
---|
[216] | 293 | viewer.VisualizationSettings.context = $("#container3"); |
---|
| 294 | viewer.VisualizationSettings.width = 400; |
---|
| 295 | viewer.VisualizationSettings.height = 400; |
---|
[208] | 296 | viewer.NetworkSettings.context = "containerNeuron3"; |
---|
[216] | 297 | viewer.NetworkSettings.width = 400; |
---|
| 298 | viewer.NetworkSettings.height = 400; |
---|
[188] | 299 | viewer.run(viewer.Mode.LINK, "http://localhost:63343/FramestickFavi/creatures/" + debugCreatureName + ".txt"); |
---|
| 300 | } |
---|