source: tol/TolJava/TOLJava/src/com/hcarralero/toljava/TOLMachine.java @ e946b884

1941-docker-image-to-testdevel
Last change on this file since e946b884 was 9887b93, checked in by Humberto Carralero <hacarralero@…>, 15 years ago

Importing TolJava

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@3900 be4846ae-f051-483a-831f-b64e7d08527f

  • Property mode set to 100644
File size: 24.1 KB
Line 
1package com.hcarralero.toljava;
2
3/**
4 * Representa a una instancia de la Máquina Virtual de TOL.
5 *
6 * @author Humberto Andrés Carralero López (hcarralero@gmail.com)
7 * @version 1.0
8 * @see com.hcarralero.toljava.TOLMachineException
9 */
10public class TOLMachine {
11
12    //Inicializa una instancia de TOL
13    private native String nativeInitTOL();
14
15    //Ejecuta código TOL
16    private native Object[] nativeExecute(String code);
17
18    //Ejecuta código TOL
19    private native Object nativeCallCode(String codeName, Object[] parameters);
20
21    //Obtiene una variable del entorno de ejecución de TOL
22    private native Object nativeGetVariable(String varName);
23
24    //Cambia el valor a una variable del entorno ejecución de TOL
25    private native Object nativeSetVariableValue(String varName, TOLLObject varValue);
26
27    //Obtiene un arreglo con todas las viariables tipo Text definidas en TOL
28    private native TOLVariable[] nativeGetTextVariables();
29
30    //Obtiene un arreglo con todas las viariables tipo Real definidas en TOL
31    private native TOLVariable[] nativeGetRealVariables();
32
33    //Obtiene un arreglo con todas las viariables tipo Date definidas en TOL
34    private native TOLVariable[] nativeGetDateVariables();
35
36    //Obtiene un arreglo con todas las viariables tipo Complex definidas en TOL
37    private native TOLVariable[] nativeGetComplexVariables();
38
39    //Obtiene un arreglo con todas las viariables tipo Matrix definidas en TOL
40    private native TOLVariable[] nativeGetMatrixVariables();
41
42    //Obtiene un arreglo con todas las viariables tipo VMatrix definidas en TOL
43    private native TOLVariable[] nativeGetVMatrixVariables();
44
45    //Obtiene un arreglo con todas las viariables tipo Polyn definidas en TOL
46    private native TOLVariable[] nativeGetPolynVariables();
47
48    //Obtiene un arreglo con todas las viariables tipo Polyn definidas en TOL
49    private native TOLVariable[] nativeGetRatioVariables();
50
51    //Obtiene un arreglo con todas las viariables tipo Code definidas en TOL
52    private native TOLVariable[] nativeGetCodeVariables();
53
54    //Obtiene un arreglo con todas las viariables tipo Serie definidas en TOL
55    private native TOLVariable[] nativeGetSerieVariables();
56
57    //Obtiene un arreglo con todas las viariables tipo TimeSet definidas en TOL
58    private native TOLVariable[] nativeGetTimeSetVariables();
59
60    //Obtiene un arreglo con todas las viariables tipo Set definidas en TOL
61    private native TOLVariable[] nativeGetSetVariables();
62
63    //Obtiene un arreglo con todos los NameBlocks definidos en TOL
64    private native TOLNameBlock[] nativeGetNameBlocks();
65
66    //Obtiene el valor de una serie en una fecha dada
67    private native Object nativeSerieGetValue(String varname, TOLDate date);
68
69    //Obtiene el valor de una serie en un índice dado
70    private native Object nativeSerieGetValue(String varname, int index);
71
72    //Obtiene un arreglo de Items de una serie a partir de una fecha dada
73    private native Object[] nativeSerieGetItems(String varname, TOLDate date, int size);
74
75    //Obtiene un arreglo de Items de una serie a partir de una posición dada
76    private native Object[] nativeSerieGetItems(String varname, int index, int size);
77
78    //Obtiene la primera fecha de una serie
79    private native Object nativeSerieGetFirstDate(String varname);
80
81    //Obtiene la última fecha de una serie
82    private native Object nativeSerieGetLastDate(String varname);
83
84    //Obtiene un arreglo de fechas de un TimeSet a partir de una fecha dada
85    private native Object[] nativeTimeSetGetDates(String varname, TOLDate date, int size);
86
87    //Obtiene la fecha previa a una dada en un TimeSet
88    private native Object nativeTimeSetGetPrevDate(String varname, TOLDate date);
89
90    //Obtiene la fecha siguiente a una dada en un TimeSet
91    private native Object nativeTimeSetGetNextDate(String varname, TOLDate date);
92
93    //Obtiene el tamaño de un Set
94    private native Object nativeSetGetSize(String varname);
95
96    //Obtiene el elemento de la posición index de un conjunto
97    private native Object nativeSetGetElement(String varname, int index);
98
99    //Obtiene todos los elementos de un conjunto como un arreglo
100    private native Object[] nativeSetGetElements(String varname);
101
102    /**
103     * Crea una instancia de la Máquina Virtual de TOL y la inicializa
104     *
105     */
106    public TOLMachine() {
107        nativeInitTOL();
108    }
109
110    /**
111     * Inicializa la Máquina Virtual de TOL.
112     *
113     */
114    public void initTOL() {
115        nativeInitTOL();
116    }
117
118    /**
119     * Ejecuta en la Máquina Virtual de TOL el código pasado por parámetro
120     *
121     * @param code código TOL a ejecutarse
122     * @return un arreglo de objetos cuyo primer elemento es el número de errores
123     * que se han producido tanto en la compilación como en la ejecución, el resto
124     * de los elementos son los mensajes generados por la Máquina Virtual de TOL,
125     * lo cual es variable. (Consulte la documentación de TOL para revisar el
126     * formato de los mensajes producidos y poder procesarlos)
127     */
128    public Object[] execute(String code) {
129        return nativeExecute(code);
130    }
131
132    /**
133     * Hace una llamada a una función asignada a una variable de tipo Code
134     *
135     * @param codeName nombre de la variable tipo Code a la que se hace la llamada
136     * @param parameters arreglo contiendo los parámetros que se deben pasar a la
137     * función que se llama; cada elemento de este arreglo puede ser un objeto
138     * perteneciente a una clase derivada de <code>TOLLObject</code> o una cadena con el
139     * nombre de una variable definida en TOL
140     * @return un objeto con el valor retornado por la función que se ha invocado
141     * @throws com.hcarralero.toljava.TOLMachineException cuando algún parámetro no es
142     * correcto, el nombre de la función a invocar no se ha encontrado u ocurre alguna
143     * otra anomalía en la evaluación
144     */
145    public TOLObject callCode(String codeName, Object[] parameters) throws TOLMachineException {
146//        String strParameters[] = new String[parameters.length];
147//        for (int i = 0; i < parameters.length; ++i) {
148//            if (parameters[i].getClass() == TOLText.class) {
149//                TOLText tt = (TOLText) parameters[i];
150//                strParameters[i] = "\"" + tt.getText() + "\"";
151//            } else {
152//                if (parameters[i].getClass() == TOLReal.class) {
153//                    TOLReal tr = (TOLReal) parameters[i];
154//                    strParameters[i] = "" + tr.getValue();
155//                } else {
156//                    if (parameters[i].getClass() == TOLDate.class) {
157//                        TOLDate td = (TOLDate) parameters[i];
158//                        strParameters[i] = "y" + td.getYear() + "m" + td.getMonth() + "d" +
159//                                              td.getDay() + "h" + td.getHour() + "i" +
160//                                                td.getMinute() + "s" + td.getSecond();
161//                    } else {
162//                        if (parameters[i].getClass() == TOLComplex.class) {
163//                            TOLComplex tc = (TOLComplex) parameters[i];
164//                            double x = tc.getX();
165//                            double y = tc.getY();
166//                            if (y != 0) {
167//                                strParameters[i] = "" + tc.getY() + "*i";
168//                            }
169//                            if (x > 0) {
170//                                strParameters[i] += "+" + tc.getX();
171//                            }
172//                            if (x < 0) {
173//                                strParameters[i] += tc.getX();
174//                            }
175//                            if (x == 0 && y == 0) {
176//                                strParameters[i] += "0";
177//                            }
178//                        } else {
179//                            if (parameters[i].getClass() == TOLMatrix.class) {
180//                                TOLMatrix tm = (TOLMatrix) parameters[i];
181//                                int rows= tm.getRows();
182//                                int columns= tm.getColumns();
183//                                if (rows==0||columns==0) {
184//                                    strParameters[i] = "UnknownMatrix";
185//                                } else {
186//                                    strParameters[i] = "(";
187//                                    for (int m= 0; m<rows; ++m) {
188//                                        strParameters[i] += "(";
189//                                        boolean coma= false;
190//                                        for (int n= 0; n<columns; ++n) {
191//                                            if (!coma) {
192//                                                coma= true;
193//                                            } else {
194//                                                strParameters[i] += ",";
195//                                            }
196//                                            try {
197//                                                strParameters[i] += ""+tm.getCellValue(m, n);
198//                                            } catch (TOLMatrixException ex) {
199//                                            }
200//                                        }
201//                                        strParameters[i] += ")";
202//                                    }
203//                                    strParameters[i] += ")";
204//                                }
205//                            } else {
206//                                if (parameters[i].getClass() == TOLPolyn.class) {
207//                                    TOLPolyn tp = (TOLPolyn) parameters[i];
208//                                    strParameters[i]= tp.toString();
209//                                } else {
210//                                    if (parameters[i].getClass() == TOLRatio.class) {
211//                                        TOLPolyn tp = (TOLPolyn) parameters[i];
212//                                        strParameters[i]= tp.toString();
213//                                    } else {
214//                                        strParameters[i]= parameters[i].toString();
215//                                    }
216//                                }
217//                            }
218//                        }
219//                    }
220//                }
221//            }
222//            strParameters[i] = parameters[i].toString();
223//        }
224//        Object res= nativeCallCode(codeName, strParameters);
225        Object res= nativeCallCode(codeName, parameters);
226        if (res.getClass()==TOLMachineException.class) {
227            TOLMachineException e= (TOLMachineException)res;
228            throw e;
229        }
230        return (TOLObject)res;
231    }
232
233    /**
234     * Determina el valor de una variable dado su nombre como parámetro
235     *
236     * @param varName nombre de la variable en la Máquina Virtual de TOL
237     * @return una variable instanciada con los datos obtenidos de TOL
238     * @throws TOLMachineException cuando la variable NO SE ENCONTRÓ
239     */
240    public TOLVariable getVariable(String varName) throws TOLMachineException {
241        Object o = nativeGetVariable(varName);
242        if (o.getClass() == TOLMachineException.class) {
243            TOLMachineException e = (TOLMachineException) o;
244            throw e;
245        }
246        TOLVariable var = (TOLVariable) o;
247        return var;
248    }
249
250    /**
251     * Cambia el valor de la variable cuyo nombre se indica por el valor pasado
252     * como parámetro
253     *
254     * @param varName nombre de la variable
255     * @param varValue nuevo valor de la variable
256     * @throws TOLMachineException cuando no se encuentra la variable o se intenta
257     * asignar un valor de un tipo erróneo
258     */
259    public void setVariableValue(String varName, TOLLObject varValue) throws TOLMachineException {
260        Object o = nativeSetVariableValue(varName, varValue);
261        if (o != null && o.getClass() == TOLMachineException.class) {
262            TOLMachineException e = (TOLMachineException) o;
263            throw e;
264        }
265    }
266
267    /**
268     * Obtiene un arreglo con todas las variables de tipo Text
269     *
270     * @return el arreglo con todas las variables de tipo Text
271     */
272    public TOLVariable[] getTextVariables() {
273        return nativeGetTextVariables();
274    }
275
276    /**
277     * Obtiene un arreglo con todas las variables de tipo Real
278     *
279     * @return el arreglo con todas las variables de tipo Real
280     */
281    public TOLVariable[] getRealVariables() {
282        return nativeGetRealVariables();
283    }
284
285    /**
286     * Obtiene un arreglo con todas las variables de tipo Date
287     *
288     * @return el arreglo con todas las variables de tipo Date
289     */
290    public TOLVariable[] getDateVariables() {
291        return nativeGetDateVariables();
292    }
293
294    /**
295     * Obtiene un arreglo con todas las variables de tipo Complex
296     *
297     * @return el arreglo con todas las variables de tipo Complex
298     */
299    public TOLVariable[] getComplexVariables() {
300        return nativeGetComplexVariables();
301    }
302
303    /**
304     * Obtiene un arreglo con todas las variables de tipo Matrix
305     *
306     * @return el arreglo con todas las variables de tipo Matrix
307     */
308    public TOLVariable[] getMatrixVariables() {
309        return nativeGetMatrixVariables();
310    }
311
312    /**
313     * Obtiene un arreglo con todas las variables de tipo VMatrix
314     *
315     * @return el arreglo con todas las variables de tipo VMatrix
316     */
317    public TOLVariable[] getVMatrixVariables() {
318        return nativeGetVMatrixVariables();
319    }
320
321    /**
322     * Obtiene un arreglo con todas las variables de tipo Polyn
323     *
324     * @return el arreglo con todas las variables de tipo Polyn
325     */
326    public TOLVariable[] getPolynVariables() {
327        return nativeGetPolynVariables();
328    }
329
330    /**
331     * Obtiene un arreglo con todas las variables de tipo Ratio
332     *
333     * @return el arreglo con todas las variables de tipo Ratio
334     */
335    public TOLVariable[] getRatioVariables() {
336        return nativeGetRatioVariables();
337    }
338
339    /**
340     * Obtiene un arreglo con todas las variables de tipo Code
341     *
342     * @return el arreglo con todas las variables de tipo Code
343     */
344    public TOLVariable[] getCodeVariables() {
345        return nativeGetCodeVariables();
346    }
347
348    /**
349     * Obtiene un arreglo con todas las variables de tipo Serie
350     *
351     * @return el arreglo con todas las variables de tipo Serie
352     */
353    public TOLVariable[] getSerieVariables() {
354        return nativeGetSerieVariables();
355    }
356
357    /**
358     * Obtiene un arreglo con todas las variables de tipo TimeSet
359     *
360     * @return el arreglo con todas las variables de tipo TimeSet
361     */
362    public TOLVariable[] getTimeSetVariables() {
363        return nativeGetTimeSetVariables();
364    }
365
366    /**
367     * Obtiene un arreglo con todas las variables de tipo Set
368     *
369     * @return el arreglo con todas las variables de tipo Set
370     */
371    public TOLVariable[] getSetVariables() {
372        return nativeGetSetVariables();
373    }
374
375    /**
376     * Obtiene un arreglo con todas los NameBlocks definidos en TOL
377     *
378     * @return el arreglo con todas los NameBlocks definidos en TOL
379     */
380    public TOLNameBlock[] getNameBlocks() {
381        return nativeGetNameBlocks();
382    }
383
384    /**
385     * Obtiene el valor de una serie en una fecha dada
386     *
387     * @param varname nombre de la serie en TOL
388     * @param date fecha en al que se quiere el valor
389     * @return el valor de la serie en la fecha dada
390     * @throws com.hcarralero.toljava.TOLMachineException si ocurre algún error accediendo a la
391     *         Serie en TOL
392     */
393    public double getSerieValue(String varname, TOLDate date) throws TOLMachineException {
394        Object res= nativeSerieGetValue(varname, date);
395        if (res.getClass()==TOLMachineException.class) {
396            TOLMachineException e= (TOLMachineException)res;
397            throw e;
398        }
399        return ((Double)res).doubleValue();
400    }
401
402    /**
403     * Obtiene el valor de una serie en una posición dada
404     *
405     * @param varname nombre de la Serie en TOL
406     * @param index posición del elemento en la serie
407     * @return el valor de la serie en la posición dada
408     * @throws com.hcarralero.toljava.TOLMachineException si ocurre algún error accediendo a la
409     *         Serie en TOL
410     */
411    public double getSerieValue(String varname, int index) throws TOLMachineException {
412        Object res= nativeSerieGetValue(varname, index);
413        if (res.getClass()==TOLMachineException.class) {
414            TOLMachineException e= (TOLMachineException)res;
415            throw e;
416        }
417        return ((Double)res).doubleValue();
418    }
419
420    /**
421     * Obtiene un arreglo de items de una serie a partir de una fecha dada
422     *
423     * @param varname nombre de la Serie en TOL
424     * @param date fecha a partir de la cual se obtendrán los elementos
425     * @param size tamaño del arreglo que se obtiene
426     * @return el arreglo de items de la serie a partir de la fecha dada
427     * @throws com.hcarralero.toljava.TOLMachineException si ocurre algún error accediendo a la Serie en TOL
428     */
429    public TSItem[] getSerieItems(String varname, TOLDate date, int size) throws TOLMachineException {
430        Object[] res= this.nativeSerieGetItems(varname, date, size);
431        if (res==null||res.length==0) {
432            return null;
433        } else {
434            if (res[0].getClass()==TOLMachineException.class) {
435                TOLMachineException e= (TOLMachineException)res[0];
436                throw e;
437            } else {
438                return (TSItem[])res;
439            }
440        }
441    }
442
443    /**
444     * Obtiene un arreglo de items de una serie a partir de una posición dada
445     *
446     * @param varname nombre de la Serie en TOL
447     * @param index posición del primer elemento en la serie
448     * @param size tamaño del arreglo que se obtiene
449     * @return el arreglo de items de la serie a partir de la posición dada
450     * @throws com.hcarralero.toljava.TOLMachineException si ocurre algún error accediendo a la Serie en TOL
451     */
452    public TSItem[] getSerieItems(String varname, int index, int size) throws TOLMachineException {
453        Object[] res= this.nativeSerieGetItems(varname, index, size);
454        if (res==null||res.length==0) {
455            return null;
456        } else {
457            if (res[0].getClass()==TOLMachineException.class) {
458                TOLMachineException e= (TOLMachineException)res[0];
459                throw e;
460            } else {
461                return (TSItem[])res;
462            }
463        }
464    }
465
466    /**
467     * Obtiene la primera fecha de una serie
468     *
469     * @param varname nombre de la Serie en TOL
470     * @return la primera fecha de la serie
471     * @throws com.hcarralero.toljava.TOLMachineException si ocurre algún error accediendo a la Serie en TOL
472     */
473    public TOLDate getSerieFirstDate(String varname) throws TOLMachineException {
474        Object res= this.nativeSerieGetFirstDate(varname);
475        if (res.getClass()==TOLMachineException.class) {
476            TOLMachineException e= (TOLMachineException)res;
477            throw e;
478        } else {
479            return (TOLDate)res;
480        }
481    }
482
483    /**
484     * Obtiene la última fecha de una serie
485     *
486     * @param varname nombre de la Serie en TOL
487     * @return la última fecha de la serie
488     * @throws com.hcarralero.toljava.TOLMachineException si ocurre algún error accediendo a la Serie en TOL
489     */
490    public TOLDate getSerieLastDate(String varname) throws TOLMachineException {
491        Object res= this.nativeSerieGetLastDate(varname);
492        if (res.getClass()==TOLMachineException.class) {
493            TOLMachineException e= (TOLMachineException)res;
494            throw e;
495        } else {
496            return (TOLDate)res;
497        }
498    }
499
500    /**
501     * Obtiene la fecha previa a una dada en un TimeSet
502     *
503     * @param varname nombre del TimeSet en TOL
504     * @param date fecha de la cual se obtendrá la previa
505     * @return la fecha previa a la dada en el TimeSet
506     * @throws com.hcarralero.toljava.TOLMachineException si ocurre algún error accediendo al TimeSet en TOL
507     */
508    public TOLDate getTimeSetPrevDate(String varname, TOLDate date) throws TOLMachineException {
509        Object res= this.nativeTimeSetGetPrevDate(varname, date);
510        if (res.getClass()==TOLMachineException.class) {
511            TOLMachineException e= (TOLMachineException)res;
512            throw e;
513        } else {
514            return (TOLDate)res;
515        }
516    }
517
518    /**
519     * Obtiene la fecha siguiente a una dada en un TimeSet
520     *
521     * @param varname nombre del TimeSet en TOL
522     * @param date fecha de la cual se obtendrá la siguiente
523     * @return la fecha siguiente a la dada en el TimeSet
524     * @throws com.hcarralero.toljava.TOLMachineException si ocurre algún error accediendo al TimeSet en TOL
525     */
526    public TOLDate getTimeSetNextDate(String varname, TOLDate date) throws TOLMachineException {
527        Object res= this.nativeTimeSetGetNextDate(varname, date);
528        if (res.getClass()==TOLMachineException.class) {
529            TOLMachineException e= (TOLMachineException)res;
530            throw e;
531        } else {
532            return (TOLDate)res;
533        }
534    }
535
536    /**
537     * Obtiene un arreglo de fechas de un TimeSet a partir de una fecha dada
538     *
539     * @param varname nombre del TimeSet en TOL
540     * @param date fecha inicial en el arreglo
541     * @param size tamaño del arreglo
542     * @return el arreglo de fechas del TimeSet a partir de la fecha dada
543     * @throws com.hcarralero.toljava.TOLMachineException si ocurre algún error accediendo al TimeSet en TOL
544     */
545    public TOLDate[] getTimeSetDates(String varname, TOLDate date, int size) throws TOLMachineException {
546        Object[] res= this.nativeTimeSetGetDates(varname, date, size);
547        if (res==null||res.length==0) {
548            return null;
549        } else {
550            if (res[0].getClass()==TOLMachineException.class) {
551                TOLMachineException e= (TOLMachineException)res[0];
552                throw e;
553            } else {
554                return (TOLDate[])res;
555            }
556        }
557    }
558
559    /**
560     * Obtiene el tamaño (número de elementos) de un Set
561     *
562     * @param varname nombre del Set en TOL
563     * @return el tamaño (número de elementos) del Set
564     * @throws com.hcarralero.toljava.TOLMachineException si ocurre algún error accediendo al Set en TOL
565     */
566    public int getSetSize(String varname) throws TOLMachineException {
567        Object res= this.nativeSetGetSize(varname);
568        if (res.getClass()==TOLMachineException.class) {
569            TOLMachineException e= (TOLMachineException)res;
570            throw e;
571        } else {
572            return ((Integer)res).intValue();
573        }
574    }
575
576    /**
577     * Obtiene un arreglo todos los elementos de un Set
578     *
579     * @param varname nombre del Set en TOL
580     * @return el arreglo todos los elementos de un Set
581     * @throws com.hcarralero.toljava.TOLMachineException si ocurre algún error accediendo al Set en TOL
582     */
583    public TOLObject[] getSetElements(String varname) throws TOLMachineException {
584        Object[] res= this.nativeSetGetElements(varname);
585        if (res==null||res.length==0) {
586            return null;
587        } else {
588            if (res[0].getClass()==TOLMachineException.class) {
589                TOLMachineException e= (TOLMachineException)res[0];
590                throw e;
591            } else {
592                return (TOLObject[])res;
593            }
594        }
595    }
596
597    /**
598     * Obtiene el elemento de la posición index en un Set
599     *
600     * @param varname nombre del Set en TOL
601     * @param index posición del elemento en el Set
602     * @return el elemento de la posición index en el Set
603     * @throws com.hcarralero.toljava.TOLMachineException si ocurre algún error accediendo al Set en TOL
604     */
605    public TOLObject getSetElement(String varname, int index) throws TOLMachineException {
606        Object res= this.nativeSetGetElement(varname, index);
607        if (res.getClass()==TOLMachineException.class) {
608            TOLMachineException e= (TOLMachineException)res;
609            throw e;
610        } else {
611            return (TOLObject)res;
612        }
613    }
614
615    //Indica si ya se ha cargado en memoria la biblioteca TOLJavaJNI
616    private static boolean cargadoTOLJavaJNI = false;
617
618    //Carga en memoria la biblioteca TOLJavaJNI
619    static {
620        if (!cargadoTOLJavaJNI) {
621            System.loadLibrary("TOLJavaJNI");
622            cargadoTOLJavaJNI = true;
623        }
624    }
625
626}
Note: See TracBrowser for help on using the repository browser.