1 /**********************************************************
2 * Version $Id$
3 *********************************************************/
4
5 ///////////////////////////////////////////////////////////
6 // //
7 // //
8 // //
9 ///////////////////////////////////////////////////////////
10
11 //---------------------------------------------------------
12 #include "bsl_interpreter.h"
13 #include "basistypen.h"
14 #include "pars_all.h"
15 #include "auswert_anweisung.h"
16
17
18 ///////////////////////////////////////////////////////////
19 // //
20 // //
21 // //
22 ///////////////////////////////////////////////////////////
23
24 //---------------------------------------------------------
25 CBSL_Interpreter *g_pInterpreter = NULL;
26
g_Add_Grid(CSG_Grid * pGrid)27 void g_Add_Grid (CSG_Grid *pGrid)
28 {
29 if( g_pInterpreter )
30 {
31 CSG_Grid *p = SG_Create_Grid(*pGrid);
32
33 p->Set_Name(pGrid->Get_Name());
34
35 g_pInterpreter->Get_Parameters()->Get_Parameter("OUTPUT")->asGridList()->Add_Item(p);
36 }
37 }
38
39 //---------------------------------------------------------
40 bool g_bProgress = true;
41
g_Set_Progress(int i,int n)42 bool g_Set_Progress (int i, int n)
43 {
44 return( g_bProgress ? SG_UI_Process_Set_Progress(i, n) : SG_UI_Process_Get_Okay() );
45 }
46
47
48 ///////////////////////////////////////////////////////////
49 // //
50 // //
51 // //
52 ///////////////////////////////////////////////////////////
53
54 //---------------------------------------------------------
CBSL_Interpreter(bool bFile)55 CBSL_Interpreter::CBSL_Interpreter(bool bFile)
56 {
57 m_bFile = bFile;
58
59 //-----------------------------------------------------
60 Set_Name (m_bFile ? _TL("BSL from File") : _TL("BSL"));
61
62 Set_Author (SG_T("SAGA User Group Associaton (c) 2009"));
63
64 Set_Description (_TW(
65 "Boehner's Simple Language (BSL) is a macro script language for grid cell based calculations. "
66 "BSL has been developed by C. Trachinow and J. Boehner originally as part of the grid analysis "
67 "software SADO, 'System fuer die Analyse Diskreter Oberflaechen'. \n"
68 "\n"
69 "References:\n"
70 "Boehner, J., Koethe, R., Trachinow, C. (1997): "
71 "Weiterentwicklung der automatischen Reliefanalyse auf der Basis von digitalen Gelaendemodellen. � "
72 "G�ttinger Geogr. Abh. 100: 3-21."
73 ));
74
75 //-----------------------------------------------------
76 Parameters.Add_Grid_List(
77 NULL, "OUTPUT" , _TL("Output"),
78 _TL(""),
79 PARAMETER_OUTPUT_OPTIONAL
80 );
81
82 if( m_bFile )
83 {
84 Parameters.Add_FilePath(
85 NULL, "BSL" , _TL("BSL Script"),
86 _TL(""),
87 CSG_String::Format(SG_T("%s|%s|%s|%s|%s|%s"),
88 _TL("BSL Files (*.bsl)") , SG_T("*.bsl"),
89 _TL("Text Files (*.txt)") , SG_T("*.txt"),
90 _TL("All Files") , SG_T("*.*")
91 )
92 );
93 }
94 else
95 {
96 Parameters.Add_String(
97 NULL, "BSL" , _TL("BSL Script"),
98 _TL(""),
99 _TW(
100 "Matrix R(), NIR(), NDVI, RANGE;\n"
101 "Point p;\n"
102 "\n"
103 "NDVI = R;\n"
104 "RANGE = R;\n"
105 "\n"
106 "foreach p in R do\n"
107 "{\n"
108 " NDVI[p] = (NIR[p] - R[p]) / (NIR[p] + R[p]);\n"
109 " RANGE[p] = max8(p, R) - min8(p, R);\n"
110 "}\n"
111 "\n"
112 "showMatrix(NDVI);\n"
113 "showMatrix(RANGE);\n"
114 ), true
115 );
116 }
117
118 Parameters.Add_Value(
119 NULL, "PROGRESS" , _TL("Show Progress"),
120 _TL(""),
121 PARAMETER_TYPE_Bool, true
122 );
123 }
124
125 //---------------------------------------------------------
~CBSL_Interpreter(void)126 CBSL_Interpreter::~CBSL_Interpreter(void)
127 {
128 g_pInterpreter = NULL;
129 }
130
131
132 ///////////////////////////////////////////////////////////
133 // //
134 // //
135 // //
136 ///////////////////////////////////////////////////////////
137
138 //---------------------------------------------------------
On_Execute(void)139 bool CBSL_Interpreter::On_Execute(void)
140 {
141 //-----------------------------------------------------
142 Parameters("OUTPUT")->asGridList()->Del_Items();
143
144 g_bProgress = Parameters("PROGRESS")->asBool();
145
146 if( m_bFile )
147 {
148 CSG_File Stream;
149
150 if( !Stream.Open(Parameters("BSL")->asString(), SG_FILE_R, false) )
151 {
152 return( false );
153 }
154
155 Stream.Read(m_BSL, Stream.Length());
156 }
157 else
158 {
159 m_BSL = Parameters("BSL")->asString();
160 }
161
162 //-----------------------------------------------------
163 if( !Parse_Vars(false) )
164 {
165 return( false );
166 }
167
168 //-----------------------------------------------------
169 CSG_Parameters Input(this, _TL("Input"), _TL(""), SG_T("INPUT"), true);
170
171 FindMemoryGrids();
172
173 for(T_InputText::iterator it=InputGrids.begin(); it!=InputGrids.end(); it++)
174 {
175 CSG_String sName(it->c_str());
176
177 Input.Add_Grid(NULL, sName, sName, _TL(""), PARAMETER_INPUT, true);
178 }
179
180 DeleteVarList();
181 DeleteAnweisungList(AnweisungList);
182
183 if( Dlg_Parameters(&Input, _TL("Input")) == false )
184 {
185 return( false );
186 }
187
188 //-----------------------------------------------------
189 if( !Parse_Vars(true) )
190 {
191 return( false );
192 }
193
194 //-----------------------------------------------------
195 g_pInterpreter = this;
196
197 if( GetMemoryGrids(&Input) )
198 {
199 try
200 {
201 ausfuehren_anweisung(AnweisungList);
202 }
203 catch(BBFehlerAusfuehren x)
204 {
205 if( x.Text == "" )
206 Message_Add(_TL("unknown error: execution"));
207 else
208 Message_Fmt("\nerror: %s\n", CSG_String(x.Text.c_str()).c_str());
209 }
210 catch(BBFehlerUserbreak x)
211 {
212 if( x.Text == "" )
213 Message_Add(_TL("unknown error: user break"));
214 else
215 Message_Fmt("\nerror: %s\n", CSG_String(x.Text.c_str()).c_str());
216 }
217 }
218
219 g_pInterpreter = NULL;
220
221 DeleteVarList();
222 DeleteAnweisungList(AnweisungList);
223
224 return( true );
225 }
226
227
228 ///////////////////////////////////////////////////////////
229 // //
230 // //
231 // //
232 ///////////////////////////////////////////////////////////
233
234 //---------------------------------------------------------
Parse_Vars(bool bFlag)235 bool CBSL_Interpreter::Parse_Vars(bool bFlag)
236 {
237 InputText.clear();
238
239 CSG_String s(m_BSL);
240
241 while( s.Length() > 0 )
242 {
243 InputText.push_back(s.BeforeFirst('\n').b_str());
244
245 s = s.AfterFirst('\n');
246 }
247
248 InputText.push_back("\t\n\n");
249
250 //-----------------------------------------------------
251 try
252 {
253 int zeile = 0;
254 int p = 0;
255 isSyntaxCheck = true;
256
257 ParseVars (zeile, p);
258 AddMatrixPointVariables(bFlag);
259 pars_ausdruck (zeile, p);
260
261 return( true );
262 }
263 catch (BBFehlerException)
264 {
265 Message_Fmt("\nerror in line %d: %s\n", FehlerZeile, CSG_String(FehlerString.c_str()).c_str());
266
267 DeleteVarList();
268 DeleteAnweisungList(AnweisungList);
269
270 return( false );
271 }
272 }
273
274
275 ///////////////////////////////////////////////////////////
276 // //
277 // //
278 // //
279 ///////////////////////////////////////////////////////////
280
281 //---------------------------------------------------------
282