source: tol/tolbase/lib/byswidget/bfunctions/bftoltcl.tcl @ 00feab6

1941-docker-image-to-testdevel
Last change on this file since 00feab6 was 00feab6, checked in by Liván Ramírez Dorta <livanr68@…>, 18 years ago

Some functions in the menu "Features" not working because of errors in TclRefToNameTol

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

  • Property mode set to 100644
File size: 15.9 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 $type $name] 0]
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  if {![catch {set lst [::tol::info variable $gra]}]} {
421    return [expr [lsearch $lst $obj] >= 0 ? 1 : 0]
422  }
423  # error
424  return 0
425}
426
427
428#/////////////////////////////////////////////////////////////////////////////
429proc TclSetVar {gra obj value} {
430#
431# PURPOSE: Get the exits of variable TOL
432#
433# PARAMETERS:
434#   gra: grammar of variable TOL
435#   obj: name of variable TOL
436#   value: value of variable
437#
438# RETURN: 1 -> Exists
439#         0 -> No
440#
441#/////////////////////////////////////////////////////////////////////////////
442  # exists ?
443  if {[TclInfoExists $gra $obj]} {
444    set cmd "$gra $obj := $value ;"
445  } else {
446    set cmd "$gra $obj = $value ;"
447  }
448  # eval
449  ::tol::console eval $cmd
450}
451
452
453#/////////////////////////////////////////////////////////////////////////////
454proc TclTolEval {cmd} {
455#
456# PURPOSE: Execute sentence TOL
457#
458# PARAMETERS:
459#   cmd: sentenc
460#
461#/////////////////////////////////////////////////////////////////////////////
462  # trace
463  if {([info exists ::project::data(EvalTra)]) && \
464      ($::project::data(EvalTra))} {
465    ::TolConsole::HciWriter "$cmd \n"
466  } 
467  ::tol::console eval $cmd
468}
469
470
471
472#///////////////////////////////////////////////////////////////////////////
473proc TolValidateData {P typFie} {
474#
475# PURPOSE: Verifies that a given value observes the rules of a data type
476#
477# PARAMETERS:
478#   P      : value to verify
479#   typFie : data type
480#
481# RETURN: 1 -> OK, 0 -> NO
482#
483#///////////////////////////////////////////////////////////////////////////
484  set ok 1
485  # si no hay ningun caracter no comprobamos
486  if {![string length $P]} {
487    return $ok
488  }
489  # ralizamos las comprobacines
490  set sepDec {.}
491  set typeField [string tolower [lindex $typFie 0]]
492  set lonField  [string tolower [lindex $typFie 1]]
493 
494  switch $typeField {   
495    "real" {
496      #Datos numéricos de punto flotante entre –3.40E + 38 y 3.40E + 38.
497      # El tamaño de almacenamiento es 4 bytes. En SQL Server, el sinónimo
498      # de real es float(24).
499      set ok [expr {([string match {[-+]} $P] || [string is double $P]) && \
500                     $P>=[expr -3.40 * pow(10,38)] && \
501                     $P<=[expr  3.40 * pow(10,38)]}]
502    }
503   
504    "text" {
505      # Todo tipo de cadenas de caracteres
506     
507      set ok 1
508    }
509   
510    "date" {
511      # Todo tipo de cadenas de caracteres
512     
513      set ok 1
514    }
515   
516    default {puts "TolValidateData:\n\
517                   WARNING: Tipo no reconocido $typeField"}
518  }
519}
520
521  proc TolGetFieldValues { tableName field getConnection} {
522   
523    set txtSql \
524    "select distinct $field \n \
525     from $tableName \n \
526     order by $field"
527    # se ejecuta la query
528    if { [catch { set lstInfoValues [$getConnection $txtSql] } result ] } {         
529        ShowMessage E "$result" "." ;# si ha habido error, se muestra
530    } else { ;# devolvemos el valor
531      # tratamos la lista de valores para no tener { } en valores con espacios
532      set lstInfoVal {}
533      foreach it $lstInfoValues {
534        lappend lstInfoVal [lindex $it 0]
535      }
536      return $lstInfoVal
537      #return $lstInfoValues
538    }   
539  }
540
541#///////////////////////////////////////////////////////////////////////////
542  proc TolGetDelimeterField {type} {
543# PURPUSE: Comprobamos el tipo de un campo e indicamos si sus valores deben
544#          ir precedidos por algun caracter especial. Por ejemplo los textos
545#          van entre comillas, los numeros no cesesitan comillas
546#          TODO: Igual hay que usar las las funciones de TOL
547#
548# RETURN: El texto a concatenar a al valor del campo
549#
550#///////////////////////////////////////////////////////////////////////////
551
552  set delimeter {}
553  set typeField [string tolower [lindex $type 0]]
554  switch $typeField {
555    "real" {
556      # no necesita ningun delimitador
557    }
558    # datetime y smalldatetime
559    "date" {
560      set delimeter {' '}
561      set gestor [string trim [TclGetVar Text DBA] {"}]
562
563      switch $gestor {
564        "SqlServer" {
565          set delimeter {{convert(datetime,'} {',121)}}       
566        }
567        "Oracle" - "Pos" {
568          set delimeter {{to_date('} {','yyyy/mm/dd')}}       
569        }
570      }
571     }
572    # Cadenas de caracteres
573    "text" {
574      set delimeter {' '}
575    }
576   
577   
578    default {puts "TolGetDelimeterField:\n\
579                   WARNING: Tipo no reconocido $typeField"}
580  }
581  #puts "TolGetDelimeterField: $delimeter"
582  return $delimeter
583}
Note: See TracBrowser for help on using the repository browser.