graph2D.component.ts 24.2 KB
Newer Older
1
import { Component } from '@angular/core';
Diego Rey's avatar
Diego Rey committed
2
3
import { GHCIService } from '../../../shared/services/ghci.service';
import functionPlot from 'function-plot';
4
import { Animation, toJSON, triggerDownload } from './graph2D.helper';
Diego Rey's avatar
Diego Rey committed
5

6
7
@Component({
    moduleId: module.id,
Diego Rey's avatar
Diego Rey committed
8
9
    selector: 'graph2D-component',
    templateUrl: './graph2D.component.html',
10
    host: {
Diego Rey's avatar
Diego Rey committed
11

12
13
    }
})
Diego Rey's avatar
Diego Rey committed
14
export class Graph2DComponent {
Diego Rey's avatar
Diego Rey committed
15

16
17
    private ghciServiceSub: any;

18
19
    private instance: null;

20
    animation: Animation = {
21
22
23
24
25
26
27
        data: [],
        timer: null,
        currentFrame: 0,
        speed: 1000,
        playing: false,
        init: false
    };
Diego Rey's avatar
Diego Rey committed
28

29
    public constructor(private ghciService: GHCIService) {
30
        this.ghciServiceSub = ghciService.messages.subscribe(
Diego Rey's avatar
Diego Rey committed
31
            canvas => {
32
33
34
35
36
37
38
39
40
41
42
43
44
45
                switch(canvas.tipo) { 
                    case 'graph': {
                        var jsonCanvas = JSON.parse(canvas.resultado);
                        let fun = eval(this.generarFuncion(jsonCanvas));
                        var conjs = this.obtenerConjunto(jsonCanvas.funs[0]);
    
                        var d = conjs + "}";
    
                        var obj = JSON.parse(d);
                        //Para las funciones 
                        if (obj.conj.sets.fdom == "function(x)") {
                            var nom = jsonCanvas.funs[0].dom;
                            var elemento = this.recursionfuncion(jsonCanvas.funs[0].sets, nom);
                            obj.conj.sets.fdom = function (x) { return eval(elemento) }
46
                        }
47
48
49
50
                        if (obj.conj.sets.fcod == "function(x)") {
                            var nom = jsonCanvas.funs[0].cod;
                            var elemento = this.recursionfuncion(jsonCanvas.funs[0].sets, nom);
                            obj.conj.sets.fcod = function (x) { return (eval(elemento)) }
51
                        }
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
                        //para Enumerados
                        if (obj.conj.dom == 'Numer') {
                            var cantElementos = obj.conj.sets.fdom.length;
                            var j = 0;
                            for (var fun of obj.conj.sets.fdom) {
                                //var newstr = nuevo2.replace(fun, j); 
                                j = j + 1;
                            }
    
                        }
                        if (obj.conj.cod == 'Numer') {
                            var cantElementos = obj.conj.sets.fcod.length;
                            var j = 0;
                            for (var fun of obj.conj.sets.fcod) {
                                //var newstr = nuevo2.replace(fun, j); 
                                j = j + 1;
                            }
    
                        }
    
                        var colores = ['pink', 'red', 'blue', 'orange', 'green']
                        var num = this.getRandomArbitrary(0, 4);
                        var color = colores[num];
    
                        var tipoGraf;
                        if (obj.conj.baseDom != 'R'){
                            tipoGraf = 'scatter';
                        }else{
                            tipoGraf = 'polyline';
                        }
                        this.instance = functionPlot({
                            target: '#graph2D-container',
                            width: 620,
                            height: 450,
                            conj:obj.conj,
                            data: [{
                                sampler: 'builtIn',
                                fn: function(scope) {
                                  return fun(scope.x)
                                },
                                graphType: tipoGraf,
                                color: color
                            }],
                            plugins: [
                                functionPlot.plugins.zoomBox()
                            ]
                        })
                        break; 
100
                    }
101
                    case 'canvas': {
102
                        var shapesData = JSON.parse(toJSON(canvas.resultado));
103
                        var shapesDataNormalized = this.normalizeShapesData(shapesData);
104
                        this.instance = null;
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
                        this.instance = functionPlot({
                            target: '#graph2D-container',
                            width: 800,
                            height: 700,
                            xAxis: {
                                label: 'x - axis',
                                scale: 'linear',
                                domain: {
                                    initial: [-10, 10],
                                    type: 'discrete'
                                }
                            },
                            data: shapesDataNormalized,
                            plugins: [
                                functionPlot.plugins.zoomBox()
                            ]
                        })
                        break; 
123
                    }
124
                    case 'animacion': {                        
125
126
127
128
129
                        if(!this.animation.init) {
                            this.animation.init = true;
                            var animationData = canvas.resultado.map(res => JSON.parse(res));
                            for (var frame of animationData) {
                                this.animation.data.push(this.normalizeShapesData(frame));
130
                            }
131
132
133
134
                            this.runAnimation();
                        }
                        break; 
                    }
135
                }
Diego Rey's avatar
Diego Rey committed
136
137
            },
            error => {
138
139
140
            }
        )
    }
Diego Rey's avatar
Diego Rey committed
141

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
    /**
     * Angular lifecycle hook.
     * called after Angular has fully initialized a component's view.
     */
    ngAfterViewInit() {
        if (!this.instance) {
            this.instance = functionPlot({
                target: '#graph2D-container',
                width: 800,
                height: 700,
                xAxis: {
                    label: 'x - axis',
                    scale: 'linear',
                    domain: {
                        initial: [-10, 10],
                        type: 'discrete'
                    }
                },
                data: [],
                plugins: [
                    functionPlot.plugins.zoomBox()
                ]
            })
        }
    }

    /**
     * Angular lifecycle hook.
     * called when a directive, pipe, or service is destroyed.
     */
    ngOnDestroy() {
        if (this.ghciServiceSub) {
          this.ghciServiceSub.unsubscribe();
        }
    }

178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
    /**
     * @name updateFrame
     * @desc update data for Function Plot and redraw the graph
     */
    public updateFrame = function(d) {
        if (this.instance) {
            this.instance.options.data = d;
            this.instance.draw();
        } else {
            this.instance = functionPlot({
                target: '#graph2D-container',
                width: 800,
                height: 700,
                xAxis: {
                    label: 'x - axis',
                    scale: 'linear',
                    domain: {
                        initial: [-10, 10],
                        type: 'discrete'
                    }
                },
                data: d,
                plugins: [
                    functionPlot.plugins.zoomBox()
                ]
Diego Rey's avatar
Diego Rey committed
203
            })
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
        }
    }
    /**
     * @name runAnimation
     * @desc Run Shapes Animation
     */
    public runAnimation = function() {
        var $this = this;
        if ($this.animation.timer !== null) return;
        $this.animation.timer = setInterval(function(){
            $this.updateFrame($this.animation.data[$this.animation.currentFrame]);
            $this.animation.currentFrame = ($this.animation.currentFrame + 1) % $this.animation.data.length;    
        }, $this.animation.speed);
        $this.animation.playing = true;
    }
    /**
     * @name pauseAnimation
     * @desc Pause Shapes Animation
     */
    public pauseAnimation = function() {
        var $this = this;
        clearInterval($this.animation.timer);
        $this.animation.timer = null;
        $this.animation.playing = false;
Diego Rey's avatar
Diego Rey committed
228
229
    }

230
231
232
233
234
    /**
     * @name zoomOut
     * @desc Zoom Out Button Control
     */
    public zoomOut = function () {
Diego Rey's avatar
Diego Rey committed
235
        this.instance.zoomOut();
236
    }
237
238
239
240
241
    /**
     * @name zoomIn
     * @desc Zoom In Button Control 
     */
    public zoomIn = function () {
Diego Rey's avatar
Diego Rey committed
242
243
        this.instance.zoomIn();
    }
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
    /**
     * @name recenterPlot
     * @desc center the plot and it returns to the initial state.
     */
    public recenterPlot = function () {
        this.instance.recenter();
    }
    /**
     * @name cleanPlot
     * @desc remove all the graph from the instance.
     */
    public cleanPlot = function () {
    }

    /**
     * @name exportPlot
     * @desc Download Plot as an SVG image.
     */
    public exportPlot = function() {
        // Objects
        var svg = document.querySelector('svg');
        var canvas = document.createElement("canvas");

        // Set dimensions of the image
        var svgSize = svg.getBoundingClientRect();
        canvas.width = svgSize.width;
        canvas.height = svgSize.height;

        // Convert SVG DOM structure to xml
        var ctx = canvas.getContext('2d');
        var data = new XMLSerializer().serializeToString(svg);

        // URL Object used to parse, construct, normalise, and encode URLs.
        var DOMURL = window.URL || (<any>window).webkitURL || window;

        var img = new Image();
        var svgBlob = new Blob([data], { type: 'image/svg+xml;charset=utf-8' });
        var url = DOMURL.createObjectURL(svgBlob);

        img.onload = function () {
            ctx.drawImage(img, 0, 0);
            DOMURL.revokeObjectURL(url);

            var imgURI = canvas
                .toDataURL('image/png')
                .replace('image/png', 'image/octet-stream');

            triggerDownload(imgURI);
        };
        img.src = url;
    }

296

297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
    /**
     * @name normalizeRectData
     * @desc Normalize Rectangle data for Function Plot Library 
     * @param {Object} rectData Data of Rectangle to be normalized
     * @returns {Object}
     */
    public normalizeRectData = function ($rectData) {
        var $rectNormalized:any = {};
        var $shape:any = {};
        $shape.w = $rectData.w;
        $shape.h = $rectData.h;
        $shape.x = $rectData.x;
        $shape.y = $rectData.y;
        $rectData.color && ($shape.fill = $rectData.color);
        $rectData.rot !== 'undefined' && ($shape.rotation = $rectData.rot);

        $rectNormalized.shape = $shape;
        $rectNormalized.graphType = 'shape';
        $rectNormalized.shapeType = 'rect';
    
        return $rectNormalized;
    }
    /**
     * @name normalizeCircleData
     * @desc Normalize Circle data for Function Plot Library 
     * @param {Object} circleData Data of Circle to be normalized
     * @returns {Object}
     */
    public normalizeCircleData = function ($circleData) {
        var $circleNormalized:any = {};
        var $shape:any = {};
        $shape.r = $circleData.r;
        $shape.x = $circleData.x;
        $shape.y = $circleData.y;
        $circleData.color && ($shape.fill = $circleData.color);
        $circleData.rot !== 'undefined' && ($shape.rotation = $circleData.rot);

        $circleNormalized.shape = $shape;
        $circleNormalized.graphType = 'shape';
        $circleNormalized.shapeType = 'circle';
    
        return $circleNormalized;
    }
340
    /**
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
     * @name normalizeTextData
     * @desc Normalize Text data for Function Plot Library 
     * @param {Object} textData Data of Text to be normalized
     * @returns {Object}
     */
    public normalizeTextData = function ($textData) {
        var $textNormalized:any = {};
        var $shape:any = {};
        $shape.text = $textData.text;
        $shape.size = $textData.size;
        $shape.x = $textData.x;
        $shape.y = $textData.y;
        $textData.color && ($shape.fill = $textData.color);
        $textData.rot !== 'undefined' && ($shape.rotation = $textData.rot);

        $textNormalized.shape = $shape;
        $textNormalized.graphType = 'shape';
        $textNormalized.shapeType = 'text';
    
        return $textNormalized;
    }
362
363
364
365
    /**
     * @name normalizeLineData
     * @desc Normalize Line data for Function Plot Library 
     * @param {Object} lineData Data of Line to be normalized
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
     * @returns {Object}
     */
    // public normalizeLineData = function ($lineData) {
    //     var $lineNormalized:any = {};
    //     var $points = []
    //     for (var p of $lineData.pts) {
    //         $points.push([p[0],p[1]]);
    //     }
    //     $lineNormalized.points = $points;
    //     $lineNormalized.color = $lineData.color;
    //     $lineNormalized.rotation = $lineData.rot;
    //     $lineNormalized.fnType = 'points';
    //     $lineNormalized.polylineType = 'line';
    //     $lineNormalized.graphType = 'polyline';
    
    //     return $lineNormalized;
    // }
383
384
385
386
    /**
     * @name normalizePolygonData
     * @desc Normalize Polygon data for Function Plot Library 
     * @param {Object} textData Data of Polygon to be normalized
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
     * @returns {Object}
     */
    // public normalizePolygonData = function ($textData) {
    //     var $PoligonNormalized:any = {};
    //     var $shape:any = {};
    //     $shape.text = $textData.text;
    //     $shape.size = $textData.size;
    //     $shape.x = $textData.x;
    //     $shape.y = $textData.y;
    //     $textData.color && ($shape.fill = $textData.color);
    //     $textData.rot !== 'undefined' && ($shape.rotation = $textData.rot);

    //     $textNormalized.shape = $shape;
    //     $textNormalized.graphType = 'shape';
    //     $textNormalized.shapeType = 'text';
    
    //     return $textNormalized;
    // }
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426

    /**
     * @name normalizeShapesData
     * @desc Normalize Shapes data for Function Plot Library 
     * @param {Array} shapesData Data of Shapes to be normalized 
     * @returns {Array}
     */
    public normalizeShapesData = function (shapesData) {
        var normalized:Array<Object> = [];
        for (var shape of shapesData) {
            switch(shape.kind) { 
                case 'rect': { 
                    normalized.push(this.normalizeRectData(shape)); 
                    break; 
                } 
                case 'circle': { 
                    normalized.push(this.normalizeCircleData(shape)); 
                    break; 
                } 
                case 'text': { 
                    normalized.push(this.normalizeTextData(shape)); 
                    break; 
427
428
429
430
431
432
433
434
435
                }
                // case 'line': { 
                //     normalized.push(this.normalizeLineData(shape)); 
                //     break; 
                // }
                // case 'polygon': { 
                //     normalized.push(this.normalizePolygonData(shape)); 
                //     break; 
                // } 
436
437
438
439
440
            } 
        }
        return normalized;
    }

441
442


Diego Rey's avatar
Diego Rey committed
443
    getRandomArbitrary = function (min, max) {
444
445
        return Math.round(Math.random() * (max - min) + min);
    }
Diego Rey's avatar
Diego Rey committed
446
447
448
449
450
451
452
453
454
455

    generarFuncion = function (graph) {
        var funcionString = '';
        var grafica;
        for (var fun of graph.funs) {
            funcionString = 'var ' + fun.fun + ' = function(' + fun.args.join() + '){\n return ' + this.generarExpresion(fun.bdy) + '}\n' + funcionString;

            if (fun.fun == graph.graph) {
                funcionString += 'return ' + fun.fun + '(' + fun.args.join() + ');\n'
                grafica = fun;
456
457
            }
        }
Diego Rey's avatar
Diego Rey committed
458
459
460
461
        funcionString = '(' + grafica.args.join() + ',delta,hayPunto)=>{\n' + funcionString + '}';

        return funcionString;
    }
462

Diego Rey's avatar
Diego Rey committed
463
    generarExpresion = function (exp) {
464
465
        var expresion = '';
        if (exp.kind == 'cnd') {
Diego Rey's avatar
Diego Rey committed
466
            expresion = ' (' + this.generarExpresion(exp.cond) + '?' + this.generarExpresion(exp.exp1) + ':' + this.generarExpresion(exp.exp2) + ') ';
467
        } else if (exp.kind == 'bop') {
Diego Rey's avatar
Diego Rey committed
468
469
470
471
472
473
474
475
476
            if (exp.op == '==') {
                expresion = ' Math.abs((' + this.generarExpresion(exp.exp1) + ') - (' + this.generarExpresion(exp.exp2) + ')) < delta && hayPunto() ';
            } else if (exp.op == '/=') {
                expresion = ' Math.abs((' + this.generarExpresion(exp.exp1) + ') - (' + this.generarExpresion(exp.exp2) + ')) > delta ||  Math.abs((' + this.generarExpresion(exp.exp1) + ') - (' + this.generarExpresion(exp.exp2) + ')) < delta && !hayPunto()  ';
            } else if (exp.op == '^') {
                expresion = ' Math.pow(' + this.generarExpresion(exp.exp1) + ',' + this.generarExpresion(exp.exp2) + ') ';
            } else {
                expresion = ' (' + this.generarExpresion(exp.exp1) + ')' + exp.op + '(' + this.generarExpresion(exp.exp2) + ') ';
            }
477
        } else if (exp.kind == 'uop') {
Diego Rey's avatar
Diego Rey committed
478
            expresion = ' ' + exp.op + ' ' + this.generarExpresion(exp.exp) + ' ';
479
        } else if (exp.kind == 'app') {
480
481
482
483
484
485
486
487
488
489
			
			if (exp.fun == 'cos') {
				exp.fun = 'Math.cos'
			} else if (exp.fun == 'sin') {
				exp.fun = 'Math.sin'
			} else if (exp.fun == 'round') {
				exp.fun = 'Math.round'
			}
			expresion = ' ' + exp.fun + '(' + exp.args.map(e => this.generarExpresion(e)).join() + ') ';

490
        } else if (exp.kind == 'tup') {
Diego Rey's avatar
Diego Rey committed
491
            expresion = ' (' + exp.exps.map(e => this.generarExpresion(e)).join() + ') ';
492
        } else if (exp.kind == 'lit') {
Diego Rey's avatar
Diego Rey committed
493
            expresion = ' ' + exp.val + ' ';
494
        } else if (exp.kind == 'var') {
Diego Rey's avatar
Diego Rey committed
495
            expresion = ' ' + exp.var + ' ';
496
        } else {
Diego Rey's avatar
Diego Rey committed
497
            expresion = ' undefined ';
498
        }
Diego Rey's avatar
Diego Rey committed
499

500
501
        return expresion;
    }
502

Diego Rey's avatar
Diego Rey committed
503
    //Nuevo 20-07-2018
504
505


Diego Rey's avatar
Diego Rey committed
506
    obtenerConjunto = function (grf) {
507
508

        var setf = '\"sets\": {';
Diego Rey's avatar
Diego Rey committed
509
510
        var dominio = '{\"conj\": {';
        if (grf.dom == 'R') {
511
512
            dominio += "\"radio\": 0.3, \"baseDom\": \"R\", \"dom\": \"R\"";
            setf += "\"fdom\": \"R\",";
Diego Rey's avatar
Diego Rey committed
513
        } else if (grf.dom == 'Z') {
514
515
            dominio += "\"radio\": 2, \"baseDom\": \"Z\", \"dom\": \"Z\"";
            setf += "\"fdom\": \"Z\",";
Diego Rey's avatar
Diego Rey committed
516
        } else if (grf.dom == 'N') {
517
518
            dominio += "\"radio\":2, \"baseDom\": \"N\", \"dom\": \"N\"";
            setf += "\"fdom\": \"N\",";
Diego Rey's avatar
Diego Rey committed
519
        } else {
520
            var nom = grf.dom;
Diego Rey's avatar
Diego Rey committed
521
            if (Array.isArray(grf.sets[0][nom])) {
522
523
                var arreglo = grf.sets[0][nom];
                var arreglo2 = [];
Diego Rey's avatar
Diego Rey committed
524
525
                for (var item of arreglo) {
                    arreglo2.push("\"" + item + "\"");
526
527
528
                }
                dominio += "\"radio\":2, \"baseDom\": \"N\", \"dom\": \"Numer\"";
                setf += "\"fdom\": [" + arreglo2 + "], ";
Diego Rey's avatar
Diego Rey committed
529
530
531
            } else {

                dominio += this.recursivoDom(grf.sets, nom);
532
                setf += "\"fdom\":\"function(x)\",";
Diego Rey's avatar
Diego Rey committed
533
            }
534
        }
Diego Rey's avatar
Diego Rey committed
535
536
        dominio += ", ";
        if (grf.cod == 'R') {
537
538
            dominio += "\"baseCod\": \"R\", \"cod\": \"R\" ,";
            setf += "\"fcod\": \"R\"";
Diego Rey's avatar
Diego Rey committed
539
        } else if (grf.cod == 'Z') {
540
541
            dominio += "\"baseCod\": \"Z\", \"cod\": \"Z\" ,";
            setf += "\"fcod\": \"Z\"";
Diego Rey's avatar
Diego Rey committed
542
        } else if (grf.cod == 'N') {
543
544
            dominio += "\"baseCod\": \"N\", \"cod\": \"N\" ,";
            setf += "\"fcod\": \"N\"";
Diego Rey's avatar
Diego Rey committed
545
        } else {
546
            var nom = grf.cod;
Diego Rey's avatar
Diego Rey committed
547
            if (Array.isArray(grf.sets[0][nom])) {
548
549
                var arreglo = grf.sets[0][nom];
                var arreglo2 = [];
Diego Rey's avatar
Diego Rey committed
550
551
                for (var item of arreglo) {
                    arreglo2.push("\"" + item + "\"");
552
553
                }
                dominio += "\"baseCod\": \"N\", \"cod\": \"Numer\" ,";
Diego Rey's avatar
Diego Rey committed
554
555
556
                setf += '\"fcod\":[' + arreglo2 + ']';
            } else {
                dominio += this.recursivoCod(grf.sets, nom);
557
                setf += "\"fcod\": \"function(x)\"";
Diego Rey's avatar
Diego Rey committed
558
            }
559
560
561
562
        }
        return dominio + setf + "}}";
    }

Diego Rey's avatar
Diego Rey committed
563
    recursionfuncion = function (func, nombre) {
564
565
        var fun = func[0][nombre].set;
        var resul = "";
Diego Rey's avatar
Diego Rey committed
566
567
568
569
570
        if (fun == 'R' || fun == 'Z' || fun == 'N') {
            resul += this.generarF(func[0][nombre].cond);
        } else {
            resul += this.generarF(func[0][nombre].cond) + " && " + this.recursionfuncion(func, fun);

571
572
573
574
        }
        return resul;
    }

Diego Rey's avatar
Diego Rey committed
575
    recursivoDom = function (sets, nom) {
576
        var domin = "";
Diego Rey's avatar
Diego Rey committed
577
        if (sets[0][nom].set == 'R') {
578
            domin += "\"radio\": 0.3, \"baseDom\": \"R\", \"dom\": \"Func\"";
Diego Rey's avatar
Diego Rey committed
579
        } else if (sets[0][nom].set == 'Z') {
580
            domin += "\"radio\": 2, \"baseDom\": \"Z\", \"dom\": \"Func\"";
Diego Rey's avatar
Diego Rey committed
581
        } else if (sets[0][nom].set == 'N') {
582
            domin += "\"radio\": 2, \"baseDom\": \"N\", \"dom\": \"Func\"";
Diego Rey's avatar
Diego Rey committed
583
        } else {
584
            var nombre = sets[0][nom].set;
Diego Rey's avatar
Diego Rey committed
585
            domin = this.recursivoDom(sets, nombre);
586
587
588
589
        }
        return domin;
    }

Diego Rey's avatar
Diego Rey committed
590
591

    recursivoCod = function (sets, nom) {
592
        var coodo = "";
Diego Rey's avatar
Diego Rey committed
593
        if (sets[0][nom].set == 'R') {
594
            coodo += "\"baseCod\": \"R\", \"cod\": \"Func\",";
Diego Rey's avatar
Diego Rey committed
595
        } else if (sets[0][nom].set == 'Z') {
596
            coodo += "\"baseCod\": \"Z\", \"cod\": \"Func\",";
Diego Rey's avatar
Diego Rey committed
597
        } else if (sets[0][nom].set == 'N') {
598
            coodo += "\"baseCod\": \"N\", \"cod\": \"Func\",";
Diego Rey's avatar
Diego Rey committed
599
        } else {
600
            var nombre = sets[0][nom].set;
Diego Rey's avatar
Diego Rey committed
601
            coodo += this.recursivoDom(sets, nombre);
602
603
604
        }
        return coodo;
    }
Diego Rey's avatar
Diego Rey committed
605
606

    generarF = function (exp) {
607
608
        var expresion = '';
        if (exp.kind == 'cond') {
Diego Rey's avatar
Diego Rey committed
609
            expresion = ' (' + this.generarF(exp.cond) + '?' + this.generarF(exp.exp1) + ':' + this.generarF(exp.exp2) + ') ';
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
        } else if (exp.kind == 'bop') {
            if (exp.op == '==') {
                expresion = ' Math.abs((' + this.generarF(exp.exp1) + ') - (' + this.generarF(exp.exp2) + ')) == 0 ';
            } else if (exp.op == '/=') {
                expresion = ' Math.abs((' + this.generarF(exp.exp1) + ') - (' + this.generarF(exp.exp2) + ')) == 0 ||  Math.abs((' + this.generarF(exp.exp1) + ') - (' + this.generarF(exp.exp2) + ')) == 0  ';
            } else if (exp.op == '^') {
                expresion = ' Math.pow(' + this.generarF(exp.exp1) + ',' + this.generarF(exp.exp2) + ') ';
            } else {
                expresion = ' (' + this.generarF(exp.exp1) + ')' + exp.op + '(' + this.generarF(exp.exp2) + ') ';
            }
        } else if (exp.kind == 'uop') {
            expresion = ' ' + exp.op + ' ' + this.generarF(exp.exp) + ' ';
        } else if (exp.kind == 'app') {
            if (exp.fun == 'cos') {
                exp.fun = 'Math.cos'
625
            } else if (exp.fun == 'sin') {
626
627
628
629
630
631
632
633
634
635
                exp.fun = 'Math.sin'
            } else if (exp.fun == 'round') {
                exp.fun = 'Math.round'
            }
            expresion = ' ' + exp.fun + '(' + exp.args.map(e => this.generarF(e)).join() + ') ';
        } else if (exp.kind == 'tup') {
            expresion = ' (' + exp.exps.map(e => this.generarF(e)).join() + ') ';
        } else if (exp.kind == 'lit') {
            expresion = ' ' + exp.val + ' ';
        } else if (exp.kind == 'var') {
Diego Rey's avatar
Diego Rey committed
636
            expresion = ' ' + exp.var + ' ';
637
        } else {
Diego Rey's avatar
Diego Rey committed
638
            expresion = ' undefined ';
639
        }
Diego Rey's avatar
Diego Rey committed
640

641
642
643
        return expresion;
    }

Diego Rey's avatar
Diego Rey committed
644
    generarFun = function (graph) {
645
646
647
648
649
650
651
652
653
654
        var funcionString = '';
        var grafica;
        for (var fun of graph.funs) {
            funcionString = 'var ' + fun.fun + ' = function(' + fun.args.join() + '){\n return ' + this.generarF(fun.bdy) + '}\n' + funcionString;

            if (fun.fun == graph.graph) {
                funcionString += 'return ' + fun.fun + '(' + fun.args.join() + ');\n'
                grafica = fun;
            }
        }
Diego Rey's avatar
Diego Rey committed
655
        funcionString = '(' + grafica.args.join() + ')=>{\n' + funcionString + '}';
656
657
658

        return funcionString;
    }
Diego Rey's avatar
Diego Rey committed
659

660

661

662
663
}