| 1 | package com.hcarralero.toljava.ejemplos; |
|---|
| 2 | |
|---|
| 3 | //Se deben importar del paquete com.hcarralero.toljava todas las clases a usarse, |
|---|
| 4 | //para este ejemplo se requieren las clases: TOLMachine, TOLMachineException, TOLDate y TOLDateException. |
|---|
| 5 | import com.hcarralero.toljava.TOLMachine; |
|---|
| 6 | import com.hcarralero.toljava.TOLMachineException; |
|---|
| 7 | import com.hcarralero.toljava.TOLObject; |
|---|
| 8 | import com.hcarralero.toljava.TOLRSet; |
|---|
| 9 | import com.hcarralero.toljava.TOLVariable; |
|---|
| 10 | |
|---|
| 11 | /** |
|---|
| 12 | * 10mo Ejemplo de TOLJava para Consola: |
|---|
| 13 | * 1) Se crea una instancia de TOLMachine. |
|---|
| 14 | * 2) Se ejecuta una sentencia TOL para definir el Set set1. |
|---|
| 15 | * 3) Se procesa el resultado. |
|---|
| 16 | * 4) Se muestran los mensajes devueltos por el intéprete de TOL. |
|---|
| 17 | * 5) Se obtiene el elemento de la posición 3 y se muestra. |
|---|
| 18 | * 6) Se obtiene el tamaño de set1 y se muestra. |
|---|
| 19 | * 7) Se obtienen todos los elementos del conjunto y se muestran. |
|---|
| 20 | * |
|---|
| 21 | * @author Humberto Andrés Carralero López (hcarralero@gmail.com) |
|---|
| 22 | * @version 1.0 |
|---|
| 23 | * @see com.hcarralero.toljava.TOLMachine, com.hcarralero.toljava.TOLMachineException, |
|---|
| 24 | * com.hcarralero.toljava.TOLReal, com.hcarralero.toljava.TOLCode. |
|---|
| 25 | */ |
|---|
| 26 | public class Ejemplo10 { |
|---|
| 27 | |
|---|
| 28 | /** |
|---|
| 29 | * @param args los argumenetos de la lÃnea de comandos. |
|---|
| 30 | */ |
|---|
| 31 | public static void main(String[] args) { |
|---|
| 32 | //Se crea una instancia de la clase TOLMachine. |
|---|
| 33 | TOLMachine tm= new TOLMachine(); |
|---|
| 34 | //En una cadena se almacena el código TOL que se ejecutará; en este caso |
|---|
| 35 | //el código es para definir un Set de 5 números reales" |
|---|
| 36 | // String code= "Set set1 = SetOfText(\"alfa01\", \"beta01\", \"alfa02\", \"beta03\", \"alfa04\");"; |
|---|
| 37 | // String code= "Set set1 = SetOfReal(5, 1.4, .23, 456, 83.85);"; |
|---|
| 38 | String code= "#Require MMS;"+ |
|---|
| 39 | "NameBlock forecast = MMS::Container::LoadFile(\"/home/hcarralero/Objetos-MMS/MPAPME.AutPEmMEm.Trim_20110606_BSR_1.0_Point__1.0.oza\");"+ |
|---|
| 40 | "Set simInputs = Select(forecast::GetModel(?)::GetDataSet(?)::GetVariables(?), Real (NameBlock v) {"+ |
|---|
| 41 | "TextSub(v::GetName(?), -5, -1) == \"_Comb\""+ |
|---|
| 42 | "});"+ |
|---|
| 43 | "Set inputs = EvalSet(simInputs, Set (NameBlock v) {"+ |
|---|
| 44 | "Serie scenarioData = v::GetData(?);"+ |
|---|
| 45 | "Text inputName = TextSub(v::GetName(?), 1, TextFind(v::GetName(?), \"_\")-1);"+ |
|---|
| 46 | "NameBlock v0 = forecast::GetModel(?)::GetDataSet(?)::GetVariable(inputName);"+ |
|---|
| 47 | "Serie inputData = v0::GetData(?);"+ |
|---|
| 48 | "[[ inputName, inputData, scenarioData ]]"+ |
|---|
| 49 | "});"+ |
|---|
| 50 | "Set outputs= EvalSet(forecast::GetModel.Forecast(?)::GetSubmodels(?), Set (NameBlock submodel) {"+ |
|---|
| 51 | "Text outputName = submodel::GetName(?);"+ |
|---|
| 52 | "Serie outputData = submodel::GetObservations(?);"+ |
|---|
| 53 | "Serie scenarioData = submodel::GetObservations.Forecast.Mean(?);"+ |
|---|
| 54 | "Set outputExprterms= submodel::GetExpTerms(?);"+ |
|---|
| 55 | "Set outputInputs = Select(outputExprterms, Real (NameBlock expr) {"+ |
|---|
| 56 | "TextSub(expr::GetName(?), -9, -1) == \"_Comb_Ext\""+ |
|---|
| 57 | "});"+ |
|---|
| 58 | "Set simOutputInputsNames= EvalSet(outputInputs, Text (NameBlock oinput) {"+ |
|---|
| 59 | "Text inputName = TextSub(oinput::GetName(?), 1, TextFind(oinput::GetName(?), \"_\")-1)"+ |
|---|
| 60 | "});"+ |
|---|
| 61 | "[[ outputName, outputData, scenarioData, simOutputInputsNames ]]"+ |
|---|
| 62 | "});"; |
|---|
| 63 | //Se invoca el intérprete de TOL para ejecutar el código pasado por parámetro. |
|---|
| 64 | //El resultado es un arreglo de cadenas Object. |
|---|
| 65 | Object res[]= tm.execute(code); |
|---|
| 66 | System.out.println(); |
|---|
| 67 | if (res==null) { |
|---|
| 68 | //Si el resultado es nulo, entonces se ha producido un error en el acceso a TOL. |
|---|
| 69 | System.out.println("ERROR ACCEDIENDO A TOL->execute"); |
|---|
| 70 | } else { |
|---|
| 71 | //Se ha logrado acceder al intérprete y ejecutar el código. |
|---|
| 72 | int ne= 0; |
|---|
| 73 | try { |
|---|
| 74 | //La primera cadena representa el número de errores detectado por el Intérprete de TOL. |
|---|
| 75 | ne= Integer.parseInt((String)res[0]); |
|---|
| 76 | if (ne>0) { |
|---|
| 77 | //El intérprete de TOL reportó al menos un error. |
|---|
| 78 | System.out.println("Se ha(n) detectado "+ne+" error(es)"); |
|---|
| 79 | } else { |
|---|
| 80 | //El intérprete de TOL no reportó errores. |
|---|
| 81 | System.out.println("No se han detectado errores"); |
|---|
| 82 | } |
|---|
| 83 | //Los siguientes objetos que conforman la respuesta del intérprete pueden ser: |
|---|
| 84 | //Warnings, Errores o mensajes generados por el código TOL ejecutado. |
|---|
| 85 | //Para procesar los mensajes generados debe comprender el formato de |
|---|
| 86 | //estos mensajes. (Consulte la documentación de TOL). |
|---|
| 87 | for (int i= 1; i<res.length; ++i) { |
|---|
| 88 | //Cada objeto es una cadena |
|---|
| 89 | String strRes= (String)res[i]; |
|---|
| 90 | //Se obtiene la longitud de la cadena. |
|---|
| 91 | int l= strRes.length(); |
|---|
| 92 | if (strRes.charAt(0)=='<'&&strRes.charAt(2)=='>'&&(strRes.charAt(1)=='E'||strRes.charAt(1)=='W')&&l>=7) { |
|---|
| 93 | //En este caso el mensaje es un 'W'arning o un 'E'rror. |
|---|
| 94 | System.out.println("\t"+strRes.substring(3, l-5)); |
|---|
| 95 | } else { |
|---|
| 96 | //En este caso es un mensaje común. |
|---|
| 97 | System.out.println("\t"+strRes); |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | TOLVariable toInputs= tm.getVariable("inputs"); |
|---|
| 101 | TOLObject to= toInputs.getValue(); |
|---|
| 102 | System.out.println("\tinputs==>"+to.toString()); |
|---|
| 103 | to= tm.getSetElement("inputs", 1); |
|---|
| 104 | System.out.println("\tinputs[1])==>"+to.toString()); |
|---|
| 105 | int size= tm.getSetSize("inputs"); |
|---|
| 106 | //Se imprime el tamaño de "inputs" |
|---|
| 107 | System.out.println("Cantidad de inputs=> ["+size+"]"); |
|---|
| 108 | //Se obtienen todos los elementos del conjunto |
|---|
| 109 | TOLObject tos[]= tm.getSetElements("inputs"); |
|---|
| 110 | //Se imprime el arreglo que contiene los elementos del conjunto |
|---|
| 111 | System.out.println("Elementos de inputs:"); |
|---|
| 112 | for (int i= 0; i<tos.length; ++i) { |
|---|
| 113 | System.out.println("\tinputs["+(i+1)+"]==>"+tos[i].toString()); |
|---|
| 114 | } |
|---|
| 115 | for (int i= 0; i<size; ++i) { |
|---|
| 116 | System.out.println("\tinputs["+(i+1)+"]==>"+tm.getSetElement("inputs", i+1).toString()); |
|---|
| 117 | } |
|---|
| 118 | } catch (NumberFormatException ex) { |
|---|
| 119 | //Algo inusual (ERROR INTERNO DE TOLJava) ha ocurrido. |
|---|
| 120 | System.out.println("ERROR DE ACCESO AL NÃMERO DE ERRORES"); |
|---|
| 121 | } catch (TOLMachineException ex) { |
|---|
| 122 | //Algún error ha ocurrido en el acceso a la variable f. |
|---|
| 123 | System.out.println("ERROR accediendo a 'inputs': "+ex.getMessage()); |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | } |
|---|