summaryrefslogtreecommitdiff
path: root/src/Units/Units_Sentence.cxx
blob: 52228f00c18f48fb0554b500f3e8efc0231b1934 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// File:	Units_Sentence.cxx
// Created:	Wed Jun 24 12:48:56 1992
// Author:	Gilles DEBARBOUILLE
//		<gde@phobox>

#include <Units_NoSuchType.hxx>
#include <Units_Sentence.ixx>
#include <Units_Token.hxx>
#include <Units_ShiftedToken.hxx>
#include <Units_TokensSequence.hxx>
#include <Units_Operators.hxx>


static Handle(Units_Token) CreateTokenForNumber(const Standard_CString str)
{
  TCollection_AsciiString tstr = str[0];
  Standard_Boolean IsPoint = Standard_False;
  Standard_Integer len = strlen(str);
  for(Standard_Integer in=1; in < len; in++) {
    if( str[in]=='0' || str[in]=='1' || str[in]=='2' || str[in]=='3' || 
        str[in]=='4' || str[in]=='5' || str[in]=='6' || str[in]=='7' || 
        str[in]=='8' || str[in]=='9' ) {
      tstr.AssignCat(str[in]);
    }
    else if( str[in]=='.' && !IsPoint ) {
      tstr.AssignCat(str[in]);
      IsPoint = Standard_True;
    }
    else
      break;
  }
  return new Units_Token(tstr.ToCString(), "0");
}


//=======================================================================
//function : Units_Sentence
//purpose  : 
//=======================================================================

Units_Sentence::Units_Sentence(const Handle(Units_Lexicon)& alexicon,
			       const Standard_CString astring)
{
  Standard_Integer index;
  Standard_Integer i,limchain;
//  Standard_Integer k,l;
//  char chain[255];
  Handle(Units_Token) token;
  Handle(Units_Token) referencetoken;
  
  thesequenceoftokens = new Units_TokensSequence();
  Handle(Units_TokensSequence) lstlexicon=alexicon->Sequence();
  Units_NoSuchType_Raise_if(lstlexicon.IsNull(),"BAD LEXICON descriptor");
  limchain=strlen(astring);
/*
  k=0;
  for(l=0; l<255; l++) {
    chain[l]=0;
  }
*/
  i=0;

  TCollection_AsciiString tmpstr = astring;
  //Handle(Units_Token) tmptoken;
  TCollection_AsciiString PrevMean;
  TCollection_AsciiString PrevWord;
  while(i < limchain) {
    Standard_Boolean IsFound = Standard_False;
    TCollection_AsciiString LastWord = "";
    for(index=1; index<=lstlexicon->Length(); index++) {
      referencetoken = lstlexicon->Value(index);
      TCollection_AsciiString aword = referencetoken->Word();
      int num = tmpstr.Search(aword);
      if( num==1 && aword.Length()>=LastWord.Length() ) {
        token = referencetoken->Creates();
        LastWord = aword;
        IsFound = Standard_True;
      }
    }
    if(!IsFound) {
      // may be it is a number(degree)?
      LastWord = tmpstr.SubString(1,1);
      if(!LastWord.IsIntegerValue()) {
        // unknown expression - not create sentene
        thesequenceoftokens->Clear();
#ifdef DEB
        cout<<"Warning: can not create correct sentence from string: "<<astring<<endl;
#endif
        return;
      }
      else {
        // create token for number
        token = CreateTokenForNumber(tmpstr.ToCString());
        LastWord = token->Word();
      }
    }
    if(i>0) {
      // make additional checking
      if( ( token->Mean()=="M" && ( PrevMean=="M" || PrevMean=="MU" || PrevMean=="0" ) ) ||
          ( token->Mean()=="U" && ( PrevMean=="U" || PrevMean=="0" )                   ) ||
          ( token->Mean()=="O" && ( PrevMean=="M" || PrevMean=="O" )                   ) ||
          ( ( token->Word()=="+" || token->Word()=="-" ) && PrevWord!="("              ) ||
          ( token->Mean()=="S" && ( PrevMean=="M" )                                    ) ||
          ( token->Mean()=="0" && ( PrevMean=="M" || PrevMean=="U" || PrevMean=="MU" ) ) )  {
        // incorrect situation
        thesequenceoftokens->Clear();
#ifdef DEB
        cout<<"Warning: can not create correct sentence from string: "<<astring<<endl;
#endif
        return;
      }
    }
    thesequenceoftokens->Append(token);
    PrevMean = token->Mean();
    PrevWord = token->Word();
    i = i + LastWord.Length();
    tmpstr.Remove(1,LastWord.Length());
  }

//  for(i=1; i<=thesequenceoftokens->Length(); i++) {
//    token = thesequenceoftokens->Value(i);
//    cout<<"i="<<i<<"  token:  "<<token->Word().ToCString()<<"   "<<token->Mean().ToCString()
//      <<"   "<<token->Value()<<endl;
//  }

/*
  while(i < limchain) {
    Standard_Integer itrouv=0;
    for(index=1; index<=lstlexicon->Length(); index++) {
      referencetoken = lstlexicon->Value(index);
      if(i+referencetoken->Length() <= limchain) {
        if(referencetoken <= &astring[i]) {
          if(k) {
            token = new Units_Token(chain,"0");
            thesequenceoftokens->Append(token);
            k=0;
            for(l=0; l<255; l++) {
              chain[l]=0;
            }
          }
          token = referencetoken->Creates();
          thesequenceoftokens->Append(token);
          i = i + token->Length();
          itrouv=1;
          break;
        }
      }
    }
    if(!itrouv) {
      chain[k++]=astring[i++];
    }
  }
  if(k) {
    token = new Units_Token(chain, "0");
    thesequenceoftokens->Append(token);
  }
*/
}


//=======================================================================
//function : SetConstants
//purpose  : 
//=======================================================================

void Units_Sentence::SetConstants()
{
  Standard_Integer index;
  Standard_Real value;
  Handle(Units_Token) token;
  TCollection_AsciiString string;

  for(index=1;index<=thesequenceoftokens->Length();index++) {
    token = thesequenceoftokens->Value(index);
    if(token->Value()==0.) {
      string = token->Word();
      if(string.IsRealValue()) {
        value = string.RealValue();
        token->Mean("K");
        token->Value(value);
      }
    }
  }
}


//=======================================================================
//function : CalculateLocal
//purpose  : auxilary
//=======================================================================
static Handle(Units_Token) CalculateLocal(const Handle(Units_TokensSequence)& aSeq)
{
  //cout<<endl;
  //for(int index=1; index<=aSeq->Length(); index++) {
  //  Handle(Units_Token) tok = aSeq->Value(index);
  //  cout<<tok->Word()<<" ";
  //}
  //cout<<endl;
  Handle(Units_Token) tok1,tok2;
  Standard_Integer i,j;

  if(aSeq->Length()==1) {
    return aSeq->Value(1);
  }

  // case of unar sign
  if(aSeq->Length()==2) {
    if(aSeq->Value(1)->Word()=="+")
      aSeq->Remove(1);
    if(aSeq->Value(1)->Word()=="-") {
      tok2 = aSeq->Value(2);
      TCollection_AsciiString aword = "-";
      aword.AssignCat(tok2->Word());
      tok1 = new Units_Token(aword.ToCString(), tok2->Mean().ToCString(),
                             tok2->Value()*(-1.0), tok2->Dimensions());
      aSeq->Remove(1);
      aSeq->SetValue(1,tok1);
    }
    return aSeq->Value(1);
  }

  Standard_Boolean IsBracket = Standard_True;
  while(IsBracket) {
    for(i=1; i<=aSeq->Length(); i++) {
      if(aSeq->Value(i)->Word()=="(") {
        Handle(Units_TokensSequence) TmpSeq = new Units_TokensSequence;
        Standard_Integer NbBrackets = 1;
        for(j=i+1; j<=aSeq->Length(); j++) {
          if(aSeq->Value(j)->Word()==")")
            NbBrackets--;
          if(aSeq->Value(j)->Word()=="(")
            NbBrackets++;
          if(NbBrackets>0)
            TmpSeq->Append(aSeq->Value(j));
          else
            break;
        }
        tok1 = CalculateLocal(TmpSeq);
        aSeq->Remove(i+1,j);
        aSeq->SetValue(i,tok1);
        break;
      }
    }
    if(i>aSeq->Length()) {
      IsBracket = Standard_False;
      // there are not brackets => make calculations
      // step 1: operation '**'
      for(i=1; i<=aSeq->Length(); i++) {
        if(aSeq->Value(i)->Word()=="**") {
          tok1 = aSeq->Value(i-1);
          tok2 = aSeq->Value(i+1);
          tok1 = pow(tok1,tok2);
          aSeq->Remove(i);
          aSeq->Remove(i);
          aSeq->SetValue(i-1,tok1);
          i--;
        }
      }
      // step 2: operation '*', '.' and '/'
      for(i=1; i<=aSeq->Length(); i++) {
        if(aSeq->Value(i)->Mean()=="O") {
          tok1 = aSeq->Value(i-1);
          tok2 = aSeq->Value(i+1);
          if(aSeq->Value(i)->Word()=="/")
            tok1 = tok1 / tok2;
          else
            tok1 = tok1 * tok2;
          aSeq->Remove(i);
          aSeq->Remove(i);
          aSeq->SetValue(i-1,tok1);
          i--;
        }
      }
      // now aSeq have to include only one result token
    }
  }
  return tok1;
}


//=======================================================================
//function : Evaluate
//purpose  : 
//=======================================================================

Handle(Units_Token) Units_Sentence::Evaluate()
{
  Handle(Units_Token) rtoken,ktoken;
  if(thesequenceoftokens->Length()==0)
    return rtoken;

/* old variant
  Standard_Integer index;
  static char *ooper[6] = { "+", "-", "*", ".", "/", "**" };
  Standard_Integer numterm,i,j,k,maxlevel,curlevel,level[255];
  Handle(Units_Token) currenttoken;
  TCollection_AsciiString string;
  TCollection_AsciiString mean;
  TCollection_AsciiString oper;
  
  numterm=curlevel=i=0;
  for(index=1;index<=thesequenceoftokens->Length();index++) {
    string = thesequenceoftokens->Value(index)->Word();

    if( string=="(" ) {
      level[numterm]=curlevel;
      curlevel++;
    }
    else if( string==")" ) {
      curlevel--;
      level[numterm]=curlevel;
    }
    else {
      level[numterm]=curlevel;
    }
    numterm++;
  }

  //if(IPrint==1) {
  //  cout<<endl;
  //  for(index=1; index<=thesequenceoftokens->Length(); index++) {
  //    Handle(Units_Token) tok = thesequenceoftokens->Value(index);
  //    cout<<tok->Word()<<" ";
  //  }
  //  cout<<endl;
  //  for(index=1; index<=thesequenceoftokens->Length(); index++) {
  //    cout<<level[index-1]<<" ";
  //  }
  //  cout<<endl;
  //}

  if(curlevel) {
    cout<<" Incorrect number of brackets"<<endl;
    return new Units_Token();
  }

  for(k=0; k<6; k++) {
    for(index=1,i=0; index<=thesequenceoftokens->Length(); index++,i++)	{
      if( thesequenceoftokens->Value(index)->Word() == ooper[k] ) {
        j=i+1;
        curlevel=level[j];
        while(j < numterm && level[j] >= curlevel)
          level[j++]++;
        j=i-1;
        curlevel=level[j];
        while(j >= 0 && level[j] >= curlevel)
          level[j--]++;
      }
    }
  }

  Handle(Units_Dimensions) aDim;

  numterm--;
  while(numterm>0) {

    maxlevel=0;
    for(i=0; i<=numterm; i++)if(level[i] > maxlevel)
      maxlevel=level[i];

    i=0;
    while(level[i] != maxlevel)
      i++;

    oper.Clear();

    k=i;
    while( k<=numterm && level[k]==maxlevel ) {
      ktoken=thesequenceoftokens->Value(k+1);
      mean=ktoken->Mean();
      if(mean=="O") {
        oper = ktoken->Word();
      }
      else if(mean!="S") {
        if     (oper == "/")  rtoken = rtoken / ktoken;
        else if(oper == "+")  rtoken = rtoken + ktoken;
        else if(oper == "-")  rtoken = rtoken - ktoken;
        else if(oper == "*")  rtoken = rtoken * ktoken;
        else if(oper == "**") rtoken = pow(rtoken,ktoken);
        else if(oper == ".")  rtoken = rtoken * ktoken;
        else                  rtoken = ktoken->Creates();
      }
      k++;
    }
    thesequenceoftokens->SetValue(i+1,rtoken);
    level[i]=maxlevel-1;

    if( (i+1) != k ) {
      thesequenceoftokens->Remove(i+2,k);
      j=i;
      while(k <= numterm)
        level[++j]=level[k++];
      numterm=j;
    }
  }

  rtoken = thesequenceoftokens->Value(1)->Creates();

  if( rtoken->Value()==0. ) {
    cout << "*BAD token value from unit '" << rtoken->Word() << "'" << endl;
    rtoken->Value(1.);
  }
*/ // end old variant

  // variant skl 15.09.2005
  rtoken = CalculateLocal(thesequenceoftokens);

  //rtoken->Dump(0,1);

  return rtoken;
}