source: js/viewer-f0/js/Viewer.js @ 189

Last change on this file since 189 was 189, checked in by mmichalski, 10 years ago

Download xml from framestick site

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