File: gen.tcl

package info (click to toggle)
ferret 0.7-2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,820 kB
  • ctags: 1,494
  • sloc: tcl: 22,364; makefile: 112
file content (597 lines) | stat: -rw-r--r-- 13,982 bytes parent folder | download
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
;# gen.tcl -- Gerwin
;#
;# Generation Routines
;#
;# Copyright (C) 2002-2008 Jose E. Marchesi
;#
;# Time-stamp: "08/11/16 19:19:51 jemarch"

;# This program is free software; you can redistribute it and/or
;# modify it under the terms of the GNU General Public License as
;# published by the Free Software Foundation; either version 3 of
;# the License, or (at your option) any later version.

;# This program is distributed in the hope that it will be useful,
;# but WITHOUT ANY WARRANTY; without even the implied warranty of
;# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;# GNU General Public License for more details.

;# You should have received a copy of the GNU General Public License
;# along with this program.  If not, see
;# <http://www.gnu.org/licenses/>.

;#
;# gen_move_relation_attributes TABLE RELATION NULL
;#
;# Pass all attributes from RELATION  to TABLE, with NULL

proc gen_move_relation_attributes {table relation null} {

    foreach ak [gob_relation_get_attributes $relation] {

	set ak [lindex $ak 0] ;# the attribute name

	;# See if there is already an attribute named $ak on the table
	if {[gob_table_attribute_exist $table $ak]} then {

	    ;# Rename the attribute with #
	    set nname "${relation}%${ak}"

	} else {
	    
	    set nname $ak
	}


	gob_table_attribute_add $table $nname \
	    [gob_relation_get_attribute_domain $relation $ak] $null
    }

}

;#
;# gen_move_key_attributes_as_foreign_keys TABLE ENTITY NULL
;#
;# Pass all attributes from the ENTITY key to TABLE, with NULL

proc gen_move_entity_key_attributes_as_foreign_keys {table entity null} {

    global gen_current_relation

    foreach ak [gob_entity_get_key $entity] {

	;# See if there is already an attribute named $ak on the table
	if {[gob_table_attribute_exist $table $ak]} then {
	    
	    ;# See if the table and the entity have the same name (reflexive relation)
	    if {$table == $entity} then {

		set nname "${entity}%${gen_current_relation}%${ak}"

	    } else {
		
		;# Rename the attribute with #
		set nname "${entity}%${ak}"
	    }


	} else {
	    
	    set nname $ak
	}
	

	gob_table_attribute_add $table $nname \
	    [gob_entity_get_attribute_domain $entity $ak] $null
	gob_table_add_attribute_to_foreign_keys $table $nname $entity
    }

}


;#
;# gen_ER_to_TD
;#
;# Generates the TD in the td page from the ER in the er page


proc gen_ER_to_TD {} {

    global gmainframe

    global gcanvas
    global gcanvas_td
    global gerwin_entities
    global gerwin_relations
    global gerwin_tables
    global gm_entity
    global gm_relation
    global gerwin_table
    global gerwin_relation
    global gerwin_entity
    global gen_current_relation
    global gm_managed
    global gm_flkey

    ;# Scale the TD canvas
    set re_sreg [${gcanvas} cget -scrollregion]
    ${gcanvas_td} configure -scrollregion $re_sreg


    ;# All entities must have a primary key!
    foreach e $gerwin_entities {

	if {[llength [gob_entity_get_key $e]] == 0} then {

	    gui_notice "$e have not a primary key! All entities must have a primary key to create tables"

	    return
	}
    }


    ;# Erase all possible previous tables
    foreach t $gerwin_tables {
	
	gob_delete_table $t

    }
    ;# Erase all possible previous flkeys
    if {[info exist gm_flkey]} then {
	unset gm_flkey
    }

    set gerwin_tables {}
    
    ;# Erase the td canvas
    ${gcanvas_td} delete all


    ;# Examine entities 
    foreach e $gerwin_entities {

	;# Create a new table
	gob_create_table $e {} {} {}
	
	;# The table have the same attributes than the entity
	foreach a [gob_entity_get_attributes $e] {
	    
	    gob_table_attribute_add $e [lindex $a 0] [lindex $a 1] 1

	}
	

	;# Also, both have the same key
	foreach k [gob_entity_get_key $e] {

	    gob_table_add_attribute_to_key $e $k
	    gob_table_set_attribute_null $e $k 0

	}


	set temp($e,xpos) $gm_entity($e,xpos)
	set temp($e,ypos) $gm_entity($e,ypos)
    }


    ;# Examine relations
    foreach r $gerwin_relations {

	set gen_current_relation $r

	set nentities [gob_relation_get_num_entities $r]
	if {$nentities == 2} then {
	    ;# Binary relation
	    
	    if {$gerwin_relation($r,reflexive)} then {
		;# Reflexive relation
		set entity1 [lindex [lindex $gerwin_relation($r,entities) 0] 0]
		set entity2 $entity1

		;# Get the cards
		set cardmax1 [lindex [lindex [lindex $gerwin_relation($r,entities) 0] 1] 1]
		set cardmax2 [lindex [lindex [lindex $gerwin_relation($r,entities) 1] 1] 1]
		set cardmin1 [lindex [lindex [lindex $gerwin_relation($r,entities) 0] 1] 0]
		set cardmin2 [lindex [lindex [lindex $gerwin_relation($r,entities) 1] 1] 0]
	    } else {
		;# Non-reflexive relations

		set entity1 [lindex [lindex $gerwin_relation($r,entities) 0] 0]
		set entity2 [lindex [lindex $gerwin_relation($r,entities) 1] 0]
		set cardmax1 [gob_relation_get_entity_max_card $r $entity1]
		set cardmin1 [gob_relation_get_entity_min_card $r $entity1]
		set cardmax2 [gob_relation_get_entity_max_card $r $entity2]
		set cardmin2 [gob_relation_get_entity_min_card $r $entity2]
	    
	    }

	    switch $cardmax1 {

		1 {
		    switch $cardmax2 {

			1 {
			    ;# /1 -> /1 RELATIONS
			    if {($cardmin1 == 1) && ($cardmin2 == 1)} then {

				;# Move the entity1 key to entity2
				gen_move_entity_key_attributes_as_foreign_keys $entity2 $entity1 0

				;# Insert the relation attributes into entity2
				gen_move_relation_attributes $entity2 $r 1
			    }
			    if {($cardmin1 == 0) && ($cardmin2 == 1)} then {

				;# Move the entity2 keys to entity1
				gen_move_entity_key_attributes_as_foreign_keys $entity1 $entity2 0

				;# Insert the relation attributes into entity1
				gen_move_relation_attributes $entity1 $r 1
			    }
			    if {($cardmin1 == 1) && ($cardmin2 == 0)} then {

				;# Move the entity1 keys to entity2
				gen_move_entity_key_attributes_as_foreign_keys $entity2 $entity1 0

				;# Insert the relation attributes into entity2
				gen_move_relation_attributes $entity2 $r 1

			    }
			    if {($cardmin1 == 0) && ($cardmin2 == 0)} then {

				;# Move the entity 1 keys to entity 2
				gen_move_entity_key_attributes_as_foreign_keys $entity2 $entity1 1

				;# Insert the relation attributes into entity2
				gen_move_relation_attributes $entity2 $r 1
			    }
			}
			N {
			    ;# /1 -> /N relations

			    if {($cardmin1 == 1) && ($cardmin2 == 1)} then {

				;# Move entity 1 keys to entity 2
				gen_move_entity_key_attributes_as_foreign_keys $entity2 $entity1 0

				;# Insert the relation attributes into entity2
				gen_move_relation_attributes $entity2 $r 1
			    }
			    if {($cardmin1 == 0) && ($cardmin2 == 1)} then {

				;# Move entity 1 keys to entity 2
				gen_move_entity_key_attributes_as_foreign_keys $entity2 $entity1 0

				;# Insert the relation attributes into entity2
				gen_move_relation_attributes $entity2 $r 1

			    }
			    if {($cardmin1 == 1) && ($cardmin2 == 0)} then {

				;# Move entity 1 keys to entity 2
				gen_move_entity_key_attributes_as_foreign_keys $entity2 $entity1 1

				;# Insert the relation attributes into entity2
				gen_move_relation_attributes $entity2 $r 1

			    }
			    if {($cardmin1 == 0) && ($cardmin2 == 0)} then {

				;# Move entity 1 keys to entity 2
				gen_move_entity_key_attributes_as_foreign_keys $entity2 $entity1 1

				;# Insert the relation attributes into entity2
				gen_move_relation_attributes $entity2 $r 1
			    }

			}
		    }

		}

		N {
		    
		    switch $cardmax2 {


			1 {
			    ;# /N -> /1 RELATIONS
			    if {($cardmin1 == 1) && ($cardmin2 == 1)} then {

				;# Move entity 2 keys to entity 1
				gen_move_entity_key_attributes_as_foreign_keys $entity1 $entity2 0

				;# Insert the relation attributes into entity1
				gen_move_relation_attributes $entity1 $r 1

			    }
			    if {($cardmin1 == 0) && ($cardmin2 == 1)} then {

				;# Move entity 2 keys to entity 1
				gen_move_entity_key_attributes_as_foreign_keys $entity1 $entity2 0

				;# Insert the relation attributes into entity1
				gen_move_relation_attributes $entity1 $r 1

			    }
			    if {($cardmin1 == 1) && ($cardmin2 == 0)} then {
				
				;# Move entity 2 keys to entity 1
				gen_move_entity_key_attributes_as_foreign_keys $entity1 $entity2 1

				;# Insert the relation attributes into entity1
				gen_move_relation_attributes $entity1 $r 1
				
			    }
			    if {($cardmin1 == 0) && ($cardmin2 == 0)} then {

				;# Move entity 2 keys to entity 1
				gen_move_entity_key_attributes_as_foreign_keys $entity1 $entity2 1

				;# Insert the relation attributes into entity1
				gen_move_relation_attributes $entity1 $r 1

			    }

			}
			
			N { ;# /N -> /N

			    ;# Create a new table
			    gob_create_table $r {} {} {}
			    
			    ;# Add the keys from the related entities
			    foreach ak [gob_entity_get_key $entity1] {

				;# See if a rename is necessary
				if {[gob_table_attribute_exist $r $ak]} then {

				    ;# Rename the attribute with #
				    set nname "${entity1}%${ak}"
				} else {
				    set nname $ak
				}
				

				gob_table_attribute_add $r $nname \
				    [gob_entity_get_attribute_domain $entity1 $ak] 0
				gob_table_add_attribute_to_foreign_keys $r \
				    $nname $entity1

				gob_table_add_attribute_to_key $r $nname

				
			    }
			    foreach ak [gob_entity_get_key $entity2] {
				
				;# See if a rename is necessary
				if {[gob_table_attribute_exist $r $ak]} then {

				    ;# Rename the attribute with #
				    set nname "${entity2}%${ak}"
				} else {
				    set nname $ak
				}


				gob_table_attribute_add $r $nname \
				    [gob_entity_get_attribute_domain $entity2 $ak] 0
				gob_table_add_attribute_to_foreign_keys $r \
				    $nname $entity2

				gob_table_add_attribute_to_key $r $nname

				
			    }
			    
			    ;# Insert the relation attributes into the new table
			    set toattr $r
			    foreach ak [gob_relation_get_attributes $r] {

				gob_table_attribute_add $toattr [lindex $ak 0] [lindex $ak 1] \
					1
			    }

			    ;# Set the geometry for the new table
			    set temp($r,xpos) $gm_relation($r,xpos)
			    set temp($r,ypos) $gm_relation($r,ypos)
			}

		    }

		}

	    }

	} else {

	    ;# Relations with >2 links
	    ;# All cards are N

	    ;# Create a new table
	    gob_create_table $r {} {} {}
	    ;# Set the geometry for the new table
	    set temp($r,xpos) $gm_relation($r,xpos)
	    set temp($r,ypos) $gm_relation($r,ypos)

	    foreach e [gob_relation_get_entities $r] {

		set e [lindex $e 0]

		;# Add the keys to the foreign keys to the new table,
		;# and to the primary key.
		foreach ak [gob_entity_get_key $e] {

		    ;# See if a rename is necessary
		    if {[gob_table_attribute_exist $r $ak]} then {

			;# Rename the attribute with #
			set nname "${e}%${ak}"
		    } else {
			set nname $ak
		    }


		    gob_table_attribute_add $r $nname \
			[gob_entity_get_attribute_domain $e $ak] 0

		    gob_table_add_attribute_to_foreign_keys $r \
			$nname $e

		    gob_table_add_attribute_to_key $r $nname

		}

	    }

	}

    }

    ;# Draw all the tables
    foreach t $gerwin_tables {

	gm_draw_table $t $temp($t,xpos) $temp($t,ypos)

    }

    ;# Finally, update all the links
    foreach t $gerwin_tables {

	set gm_managed {}
	gm_table_update_links $t 
    }

}


;#
;# gen_TD_to_Output FORMAT
;#
;# Generates the Output corresponding to the actual TD contents 
;# (that is, the actual table space)
;#
;# FORMAT  is the Output format.

proc gen_TD_to_Output {format} {

    global output_pages
    global output_pages_widget
    global gtext_output
    global gerwin_output_formats

    ;# Do nothing if FORMAT is empty or is not a valid output format
    if {$format == ""} then {
	return
    }
    if {[lsearch $gerwin_output_formats $format] == -1} then {
	return
    }


    ;# Create or switch to the good output page

    ;# See if FORMAT have alreay a page in the output area
    set index [lsearch $output_pages $format]
    if {$index != -1} then {

	;# Make the page visible
	${output_pages_widget} raise [lindex $output_pages $index]

    } else {
	
	;# Create a new page for the format
	gui_create_output_page $format

    }

    ;# Make the text widget editable
    $gtext_output($format) configure -state normal

    ;# Call to the appropiate generation routine
    omit_clear
    gen_TD_to_format_${format}

    ;# Make the text widget no editable
    $gtext_output($format) configure -state disabled

    ;# Finally, colorize gtext_output($format)
    gen_colorize $format
}


;#
;# gen_colorize FORMAT
;#
;# Colorizes the generated text for FORMAT

proc gen_colorize {format} {

    global gtext_output
    global output_colors

    set gtext $gtext_output($format)
    set colors $output_colors($format)

    ;# Rewind the text widget
    ${gtext} mark set insert 1.0

    ;# Foreach colors 
    foreach color $colors {

	set er [lindex $color 0]
	set col [lindex $color 1]

	;# Find all occurrences of ER on the text widget, and assign it
	;# the tag "col"
	${gtext} mark set traveller 1.0
	while {1} {

	    set sresult [${gtext} search -regexp -nocase -count foundnum -- $er [${gtext} index traveller] end]
	    if {$sresult == ""} then {
		;# No more matches
		break
	    }

	    ${gtext} mark set traveller $sresult 
	    ${gtext} tag add $col [${gtext} index traveller]  "[${gtext} index traveller] + $foundnum chars"
	    ${gtext} mark set traveller "[${gtext} index traveller] + [expr $foundnum + 1] chars"
	}

	;# Colorize the tag
	${gtext} tag configure $col -foreground $col
    }


}


;#
;# omit_line LINE
;#
;# Outputs LINE to the right output text widget

proc omit_line {line} {

    global gerwin_output_active_format
    global gtext_output

    $gtext_output($gerwin_output_active_format) insert end "${line}\n"

}


;#
;# omit_clear
;#
;# Clear the right output text widget

proc omit_clear {} {
    
    global gerwin_output_active_format
    global gtext_output

    $gtext_output($gerwin_output_active_format) delete 1.0 end
}