source: tol/tolbase/lib/byswidget/bfunctions/bftoltcl.tcl @ 7cdaf52

1941-docker-image-to-testdevel
Last change on this file since 7cdaf52 was 7cdaf52, checked in by Jorge Pérez <josp.jorge@…>, 18 years ago

related to change in toltcl API

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

  • Property mode set to 100644
File size: 15.8 KB
Line 
1#/////////////////////////////////////////////////////////////////////////////
2# FILE    : bftoltcl.tcl -> BfT
3# PURPOSE : Bayes Function TolTcl. Functions for easy Tol-Tcl comunication
4#/////////////////////////////////////////////////////////////////////////////
5
6variable TclData
7
8#///////////////////////////////////////////////////////////////////////////
9  proc TolDateFormat2TclDateFormat {format} {
10# PURPUSE: Translates TolDateFormat to TclDateFomat
11#
12# PARAMETERS:
13#   format : TOL date format
14# RETURN: Tcl date format
15#
16#///////////////////////////////////////////////////////////////////////////
17  regsub  %u $format "" format
18  regsub  %c $format "" format
19  regsub  %W $format "%A" format
20  regsub  %N $format "%B" format
21  regsub  %w $format "%a" format
22  regsub  %n $format "%b" format
23  regsub  %h $format "%H" format
24  regsub  %i $format "%M" format
25  regsub  %s $format "%S" format
26  return $format
27}
28
29#/////////////////////////////////////////////////////////////////////////////
30  proc GetUniqueName {gra {seed _TOLTK}} {
31#
32# PURPOSE: Gets a unique Name for a given grammar. Optionally user can give
33#          the initial part of the name
34#
35# PARAMETERS:
36#   gra : TOL Grammar
37#   seed: initial part of the name
38#
39#/////////////////////////////////////////////////////////////////////////////
40  set i 1
41  set end 0
42  set name $seed
43  while {[string equal $end 0]} {
44    if [catch {::tol::info variable $gra $name}] {
45      set end 1
46    } else {
47      set name ${seed}_$i
48      incr i
49    }
50  }
51  return $name
52}
53
54#/////////////////////////////////////////////////////////////////////////////
55  proc GetGlobalUniqueName {{seed _TOLTK}} {
56#
57# PURPOSE: Gets a unique Name independent of grammar. Optionally user can give
58#          the initial part of the name
59#
60# PARAMETERS:
61#   seed: initial part of the name
62#
63#/////////////////////////////////////////////////////////////////////////////
64  set i 1
65  set end 0
66  set name $seed
67  # list of grammars
68  set lstGra [::tol::info grammars]
69
70  while {[string equal $end 0]} {
71    set end 1
72    foreach gra $lstGra {
73      set inf [TclInfoVar $gra $name]
74      if {[string length $inf]} {
75        set end 0
76      }
77    }
78    if { !$end } {
79      set name ${seed}_$i
80    }
81    incr i
82  }
83  return $name
84}
85
86#/////////////////////////////////////////////////////////////////////////////
87proc TclInfoVar {gra obj} {
88#
89# PURPOSE: Get the value of variable TOL
90#
91# PARAMETERS:
92#   gra: grammar of variable TOL
93#   obj: name of variable TOL
94#
95# RETURN: output [::tol::info var obg] information in "array get"
96#
97#/////////////////////////////////////////////////////////////////////////////
98  # eval
99  if {![catch {set lst [::tol::info variable [list $gra $obj]]}]} {
100    set lstNam [list CONTENT PATH DESC INDEXES ISFILE\
101                     HASSUBSET SUBTYPE STRUCT]
102    for {set i 0} {$i < [llength $lstNam]} {incr i} {
103      set inf([lindex $lstNam $i]) [lindex $lst [expr $i+2]]
104    }
105    #parray inf
106    return [array get inf]
107  }
108  # error
109  return {}
110}
111
112
113#/////////////////////////////////////////////////////////////////////////////
114proc TclInfoRef {obj args} {
115#
116# PURPOSE: Get the value os Set TOL
117#
118# PARAMETERS:
119#   obj:  reference of TCL
120#   args: indix of set
121#
122# RETURN: output [::tol::info var ref] information in "array get"
123#
124#/////////////////////////////////////////////////////////////////////////////
125  # eval
126  set cmd "::tol::info variable {$obj $args}"
127  #puts "TclInfoRef: cmd=$cmd"
128  if {![catch {set lst [eval $cmd]}]} {
129    set lstNam [list GRAMMAR NAME CONTENT PATH DESC ISFILE\
130                     HASSUBSET SUBTYPE STRUCT]
131    for {set i 0} {$i < [llength $lstNam]} {incr i} {
132      set inf([lindex $lstNam $i]) [lindex $lst [expr $i]]
133    }
134    #parray inf
135    return [array get inf]
136  }
137  return {}
138}
139
140
141#/////////////////////////////////////////////////////////////////////////////
142proc TclInfoFun {gra fun} {
143#
144# PURPOSE: Get the value of Set TOL
145#
146# PARAMETERS:
147#   gra: grammars of functions TOL
148#   fun: name of function
149#
150# RETURN: output [::tol::info functions gre fun] information in "array get"
151#
152#/////////////////////////////////////////////////////////////////////////////
153  # eval
154  set cmd "::tol::info functions $gra $fun"
155  #puts "cmd: $cmd"
156  if {![catch {set lst [eval $cmd]}]} {
157    set lstNam [list CONTENT PATH DESC]
158    for {set i 0} {$i < [llength $lstNam]} {incr i} {
159      set inf([lindex $lstNam $i]) [lindex $lst $i]
160    }
161    #parray inf
162    return [array get inf]
163  }
164  return {}
165}
166
167#/////////////////////////////////////////////////////////////////////////////
168proc TclInfoInc {fil} {
169#
170# PURPOSE: Get the value of file included in TOL
171#
172# PARAMETERS:
173#   fil: name of file included en TOL
174#
175# RETURN: output [::tol::info included fil] information in "array get"
176#
177#/////////////////////////////////////////////////////////////////////////////
178# eval
179  set cmd "::tol::info included $fil"
180  #puts "cmd: $cmd"
181  if {![catch {set lst [eval $cmd]}]} {
182    set lstNam [list CONTENT PATH DESC HASSUBSET SUBTYPE STRUCT]
183    for {set i 0} {$i < [llength $lstNam]} {incr i} {
184      set inf([lindex $lstNam $i]) [lindex $lst $i]
185    }
186    #parray inf
187    return [array get inf]
188  }
189  return {}
190}
191
192#/////////////////////////////////////////////////////////////////////////////
193proc TclRefToNameTol { ref } {
194#
195# PURPOSE: Obtain the name of an object TOL, of the form, name of the joint
196#          father, if it has, and indices of the set, if it has
197#
198# PARAMETERS:
199#   ref: reference to object TOL
200#
201# RETURN: name in format: namaVariable (variable without father)
202#                         namVarFat[x][y][z] (variable with father)
203#/////////////////////////////////////////////////////////////////////////////
204  set ref [lindex $ref 0]
205  #puts "TclRefToNameTol: ref=$ref"
206  # the object belongs to a set?
207  if {[expr [llength $ref] == 1]} {
208    set namTol $ref
209  } else  {
210    #array set infRef [TclInfoRef [lrange $ref 0 1]]
211    array set infRef [TclInfoRef $ref]
212    set namTol $infRef(NAME)
213    #puts "TclRefToNameTol: namTol=$namTol"
214    # name ?
215  #if {[string length $namTol]} {
216      # ckeck parameters
217      #if {[expr [llength $ref] > 2]} {
218        #set lstInx [lrange $ref 2 end]
219        #foreach ind $lstInx {
220          #set namTol $namTol\[$ind\]
221        #}
222      #}
223    #} else  {
224      #set namTol {}
225    #}
226  #}
227  #puts "TclRefToNameTol: namTol=$namTol"
228  return $namTol
229}
230
231
232#/////////////////////////////////////////////////////////////////////////////
233proc TclGetVar {gra obj {property CONTENT}} {
234#
235# PURPOSE: Get a property of a variable TOL (default: returns value)
236#
237# PARAMETERS:
238#   gra: grammar of variable TOL
239#   obj: name of variable TOL
240#   pro: property to return
241#
242# RETURN: the property value requested of the TOL object
243#
244#/////////////////////////////////////////////////////////////////////////////
245  array set tmparray [TclInfoVar $gra $obj]
246  set property [string toupper $property]
247  if { [lsearch [array names tmparray] $property] != -1 } {
248    return $tmparray($property)
249  }
250}
251
252
253#/////////////////////////////////////////////////////////////////////////////
254proc TclGetRef {obj args {property CONTENT}} {
255#
256# PURPOSE: Get a property of a TOL reference (default: returns value)
257#
258# PARAMETERS:
259#   obj: name of TOL object
260#   args: list with reference indexes
261#   property: property to return
262#
263# RETURN: the property value requested of the TOL object
264#
265#/////////////////////////////////////////////////////////////////////////////
266  array set tmparray [eval TclInfoRef $obj $args]
267  set property [string toupper $property]
268  if { [lsearch [array names tmparray] $property] != -1 } {
269    return $tmparray($property)
270  }
271}
272
273
274#/////////////////////////////////////////////////////////////////////////////
275proc TclGetFromSet { type set args } { 
276#
277# PURPOSE: returns the value of a reference in a set that is a real date or
278#          serie element
279# ARGUMETS:
280#  type: grammar of the element beeing accesed in the set
281#  set:  name of the tol set
282#  args: names and indices for indexing the set
283#
284#/////////////////////////////////////////////////////////////////////////////
285  switch -regexp type {
286    [Rr]eal { set type Real }
287    [Tt]ext -
288    [Tt]exto { set type Text }
289    [Dd]ate -
290    [Ff]echa { set type Date }
291  }
292
293  set name [GetGlobalUniqueName _TclGetFromSet]
294  set str "$type $name = $set"
295  foreach arg $args {
296    if { ! [regexp "^\[0-9\]*$" $arg] } {
297      set str [concat $str "\[\"$arg\"\]"]
298    } else {
299      set str [concat $str "\[$arg\]"]
300    }
301  }
302
303  ::tol::console eval $str
304  set result [lindex [::tol::info var [ list $type $name ] ] 2]
305  ::tol::console stack release $name
306  return $result
307}
308
309
310#/////////////////////////////////////////////////////////////////////////////
311proc TclLst2TolSet {lst args} {
312#
313# PURPOSE: returns a TOL code that when is evaluated creates a set equivalent
314#          to a tcl list.
315# ARGUMETS:
316#   lst  -> List to convert to TOL set (code)
317#   args  -> Function's arguments as pairs "-key value"
318#    -level  : Number of sublist levels to transform in TOL Set
319#    -grammar: Gramatica de los elementos del ultimo nivel
320#    -struct : Name of struct
321#
322#/////////////////////////////////////////////////////////////////////////////
323  array set opt [list \
324    -level   1 \
325    -grammar Text \
326    -struct  ""
327  ]
328  array set opt $args
329 
330  # name of struct ?
331  if {[string length $opt(-struct)]} {
332    set struct $opt(-struct)
333  } else {
334    switch -- $opt(-grammar) {
335      Real     { set struct SetOfReal }
336      Date     { set struct SetOfDate }
337      default  { set struct SetOfText }
338    }
339  } 
340  if {![llength $lst]} {
341    return "Copy(Empty)"
342  }
343  if [expr $opt(-level) <= 1] {
344    switch -- $opt(-grammar) {
345      Real     { set code "${struct}([join $lst ,])" }
346      Date     { set code "${struct}([join $lst {,}])" }
347      default  { set code "${struct}(\"[join $lst \",\"]\")" }
348    }
349  } else {
350    set sSet ""
351    foreach ite $lst {
352      lappend sSet [TclLst2TolSet $ite \
353        -level [expr $opt(-level)-1] \
354        -grammar $opt(-grammar)      \
355        -struct  $opt(-struct) ]
356    }
357    set code "SetOfSet([join $sSet ,])"
358  }
359  return $code
360}
361
362#/////////////////////////////////////////////////////////////////////////////
363proc TolSet2TclLst {settol} {
364#
365# PURPOSE: returns a TCL code that when is evaluated creates a set equivalent
366#          to a tol set.
367# ARGUMETS:
368#   settol  -> set tol
369#
370#///////////////////////////////////////////////////////////////////////////// 
371  set name [GetGlobalUniqueName __TolSet2TclLst__]
372  set cmd  "Text $name = TolSet2TclLst($settol);"
373  ::tol::console eval $cmd
374  set result [TclGetVar Text $name]
375  ::tol::console stack release $name
376  if {[string length $result]} {
377    eval set result $result
378  }
379  return $result
380}
381
382#/////////////////////////////////////////////////////////////////////////////
383proc TclGetIdFromName { name cjto {refs ""}} {
384#
385# PURPOSE: returns the access index equivalent to name in the set cjto
386# ARGUMETS:
387#  name: a text indexing the set that should be converted to index
388#  cjto: the TOL set
389#  refs: names and indices indexing the set
390#
391#/////////////////////////////////////////////////////////////////////////////
392  if { $refs ne "" } {
393    if { [TclGetRef $cjto $refs GRAMMAR] != "Set" } { return }
394    set n [lindex [TclGetRef $cjto $refs] 0]
395  } else {
396    set n [lindex [TclGetVar Set $cjto] 0]
397  }
398 
399  for {set i 1} { $i <= $n } { incr i } {
400    if { [TclGetRef $cjto [linsert $refs end $i] NAME] == $name } {
401      return $i
402    }
403  }
404}
405
406#/////////////////////////////////////////////////////////////////////////////
407proc TclInfoExists {gra obj} {
408#
409# PURPOSE: Get the exits of variable TOL
410#
411# PARAMETERS:
412#   gra: grammar of variable TOL
413#   obj: name of variable TOL
414#
415# RETURN: 1 -> Exists
416#         0 -> No
417#
418#/////////////////////////////////////////////////////////////////////////////
419  # eval
420  expr { ! [ catch { tol::info var [ list $gra $obj ] } ] }
421}
422
423
424#/////////////////////////////////////////////////////////////////////////////
425proc TclSetVar {gra obj value} {
426#
427# PURPOSE: Get the exits of variable TOL
428#
429# PARAMETERS:
430#   gra: grammar of variable TOL
431#   obj: name of variable TOL
432#   value: value of variable
433#
434# RETURN: 1 -> Exists
435#         0 -> No
436#
437#/////////////////////////////////////////////////////////////////////////////
438  # exists ?
439  if {[TclInfoExists $gra $obj]} {
440    set cmd "$gra $obj := $value ;"
441  } else {
442    set cmd "$gra $obj = $value ;"
443  }
444  # eval
445  ::tol::console eval $cmd
446}
447
448
449#/////////////////////////////////////////////////////////////////////////////
450proc TclTolEval {cmd} {
451#
452# PURPOSE: Execute sentence TOL
453#
454# PARAMETERS:
455#   cmd: sentenc
456#
457#/////////////////////////////////////////////////////////////////////////////
458  # trace
459  if {([info exists ::project::data(EvalTra)]) && \
460      ($::project::data(EvalTra))} {
461    ::TolConsole::HciWriter "$cmd \n"
462  } 
463  ::tol::console eval $cmd
464}
465
466
467
468#///////////////////////////////////////////////////////////////////////////
469proc TolValidateData {P typFie} {
470#
471# PURPOSE: Verifies that a given value observes the rules of a data type
472#
473# PARAMETERS:
474#   P      : value to verify
475#   typFie : data type
476#
477# RETURN: 1 -> OK, 0 -> NO
478#
479#///////////////////////////////////////////////////////////////////////////
480  set ok 1
481  # si no hay ningun caracter no comprobamos
482  if {![string length $P]} {
483    return $ok
484  }
485  # ralizamos las comprobacines
486  set sepDec {.}
487  set typeField [string tolower [lindex $typFie 0]]
488  set lonField  [string tolower [lindex $typFie 1]]
489 
490  switch $typeField {   
491    "real" {
492      #Datos numéricos de punto flotante entre –3.40E + 38 y 3.40E + 38.
493      # El tamaño de almacenamiento es 4 bytes. En SQL Server, el sinónimo
494      # de real es float(24).
495      set ok [expr {([string match {[-+]} $P] || [string is double $P]) && \
496                     $P>=[expr -3.40 * pow(10,38)] && \
497                     $P<=[expr  3.40 * pow(10,38)]}]
498    }
499   
500    "text" {
501      # Todo tipo de cadenas de caracteres
502     
503      set ok 1
504    }
505   
506    "date" {
507      # Todo tipo de cadenas de caracteres
508     
509      set ok 1
510    }
511   
512    default {puts "TolValidateData:\n\
513                   WARNING: Tipo no reconocido $typeField"}
514  }
515}
516
517  proc TolGetFieldValues { tableName field getConnection} {
518   
519    set txtSql \
520    "select distinct $field \n \
521     from $tableName \n \
522     order by $field"
523    # se ejecuta la query
524    if { [catch { set lstInfoValues [$getConnection $txtSql] } result ] } {         
525        ShowMessage E "$result" "." ;# si ha habido error, se muestra
526    } else { ;# devolvemos el valor
527      # tratamos la lista de valores para no tener { } en valores con espacios
528      set lstInfoVal {}
529      foreach it $lstInfoValues {
530        lappend lstInfoVal [lindex $it 0]
531      }
532      return $lstInfoVal
533      #return $lstInfoValues
534    }   
535  }
536
537#///////////////////////////////////////////////////////////////////////////
538  proc TolGetDelimeterField {type} {
539# PURPUSE: Comprobamos el tipo de un campo e indicamos si sus valores deben
540#          ir precedidos por algun caracter especial. Por ejemplo los textos
541#          van entre comillas, los numeros no cesesitan comillas
542#          TODO: Igual hay que usar las las funciones de TOL
543#
544# RETURN: El texto a concatenar a al valor del campo
545#
546#///////////////////////////////////////////////////////////////////////////
547
548  set delimeter {}
549  set typeField [string tolower [lindex $type 0]]
550  switch $typeField {
551    "real" {
552      # no necesita ningun delimitador
553    }
554    # datetime y smalldatetime
555    "date" {
556      set delimeter {' '}
557      set gestor [string trim [TclGetVar Text DBA] {"}]
558
559      switch $gestor {
560        "SqlServer" {
561          set delimeter {{convert(datetime,'} {',121)}}       
562        }
563        "Oracle" - "Pos" {
564          set delimeter {{to_date('} {','yyyy/mm/dd')}}       
565        }
566      }
567     }
568    # Cadenas de caracteres
569    "text" {
570      set delimeter {' '}
571    }
572   
573   
574    default {puts "TolGetDelimeterField:\n\
575                   WARNING: Tipo no reconocido $typeField"}
576  }
577  #puts "TolGetDelimeterField: $delimeter"
578  return $delimeter
579}
Note: See TracBrowser for help on using the repository browser.