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