summaryrefslogtreecommitdiff
path: root/support/consdiag/consdiag.py
blob: b328f5ee9ff7dbb2b33caa55d159c290f6a7722f (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
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
import string

"""
1998 Manuel Gutierrez Algaba
You are free to use , modify, distribute and copy this piece of
python code, if you keep this free copyright notice in it.

consdiag.py Version 1.1

constructor of diagrams: 

Given a frame of classes and relationships among clases, it 
generates latex code ,that represents those classes in the
Rumbaugh OO notation.
"""

"""
The first idea is this :
If we have a box representing the class we stablish *contact_points* 
in its borders, so we can connect them , the positions inside 
a box are absolute to a relative point, the self.x , self.y in the
a_simple_class.

- a_simple_class : just draw a box , with the name of the class ,
attributes, functions, an the multiplicity of that class.

- union_derivation : draws two simple classes , one of them the 
mather class, and the other, the derived class. It also
supports several levels of derivation.

- union_association : draw two simple classes, one of them has
0:n instances of the other or viceversa.

- union_aggregation: as derivation but with aggregates

Although it'd be really wise to do a class with the common
attributes of these three classes and inheritate from it, I've not
done it. So many functions seem to be redundant and what's worse,
some comments to common functions are done in just one class.
Sorry for that... Read the simpler classes first.

The position of the boxes are determined by the position of a base
a_simple_class, whose contact_points will be supplied to the boxes
conected (by unions) to it , so that the other boxes can obtain 
an absolute positioning.

This model of contact points is not valid when you want to inheritate
from several classes at the same time. :(

So  this piece of code is 
finished 3-1-99. Except for bugs.

It's a nice piece of code for creating very simple drawings.

"""

"""
Here are some nice nice constant to scale the drawings.
Possibly, it'd be wiser to do some more.

"""
sscale="0.2"
typeofletter = "\\small"

"""
 In this part we declare a higher level of interfaces
for functions that generate Latex code .
It'll be interesting that more of this kind of functions
would be created. So that we isolate low level details...

"""

def generate_move(f,x,y ):
    f.write(" \\move("+`x` +" "+ `y`+" )")

def rgenerate_move(f,x,y ):
    f.write(" \\rmove("+`x` +" "+ `y`+" )")

"""
a_placed_text and a_placed_line are objects for drawing
small elements in the class, elements that don't play a main
role in the structure of positioning of the draw.

"""

class a_placed_text:
    def __init__(self,x,y,text):
	self.x=x
	self.y=y
	self.text=text

    def generate_move(self,f):
	rgenerate_move(f,self.x, self.y)
	f.write("\\rlvec (0 0) ")
	f.write("\\htext { "+self.text+" }\n ")
	rgenerate_move(f,-self.x, -self.y)


class a_placed_line:
    def __init__(self,x,y,x1,y1):
	self.x=x
	self.y=y
	self.x1=x1
	self.y1= y1

    def generate_move(self,f):
	rgenerate_move(f,self.x, self.y)
	f.write("\\rlvec ("+`self.x1`+" "+`self.y1`+") ")
	f.write("\\rlvec ("+`-self.x1`+" "+`-self.y1`+") ")
	rgenerate_move(f,-self.x, -self.y)


'''
scale: it is just a factor for adjusting the length of the lines.
This class is a  simple abstraction for storing the scale. 

'''
class scale:
    def __init__(self,l):
	self.l = l

    def what_scale(self):
	return self.l

"""
line, horizontal_line, vertical_line,... are simple objects intentended
to keep information of a line to draw. Remember that we don't draw
until we have settled the absolute positions, that is, when we've
drawn another parts of the drawing, ... 
All this objects are intentended to generate relative positioning
"""

class line:
    def __init__(self,length,dx,dy):
	self.l = length
	self.dx = dx
	self.dy = dy
    def what_length(self):
	return self.l
    def what_x(self):
	return self.dx
    def what_y(self):
	return self.dy

class horizontal_line(line):
    def __init__(self,dx):
	line.__init__(self,dx,dx,0)

class vertical_line(line):
    def __init__(self,dy):
	line.__init__(self,dy,0,dy)

class diagonal_line(line):
    def __init__(self,dx,dy):
	line.__init__(self,dy+dx,dx,dy)

"""
contact_point: It's another rather simple storage of information,
it could named 'label', it helps to know where we can join one
class with any other.

"""

class contact_point:
    def __init__(self,name):
	self.name = name

    def what_name(self):
	return self.name
    def what_x(self):
	return self.x
    def what_y(self):
	return self.y
    def do_x(self,x):
	self.x = x
    def do_y(self,y):
	self.y = y

"""
a_simple_class:
This must generate code for  a class, if we say :
 clase frutas, attributes: perecedera , dulce, seca, carnosa, 
del tiempo, tropical,

it must generate code that shows: 


__________
| fruits |
|--------|
|cheap   |
|sweet   |
|dry     |
|....    |
|________|

or even :

__________
| fruits |
|--------|
|cheap   |
|sweet   |
|dry     |
|        |
|        |
----------
|eat     |
|buy     |
|take    |
|      / |
|_____/_3|

Where the first section has got the name of the class,
the second one , the types of operations of the class 'fruits',
and the number 3, means that this draw of the class fruit is
the third one that can be seen in the documentation.

This class is the base for building more complex drawings as
derivations an associations.

"""

class a_simple_class(scale):
    def __init__(self):
	scale.__init__(self,2)
	# it holds all the lines and contact points that make the
	# drawing
	self.list_of_lines = []
	# in the example of the fruit, they'd be : sweet, dry, cheap
	self.list_of_atributes = []
	# in the example of the fruit, they'd be : eat, buy, take
	self.list_of_functions = []
	self.x = 0
	self.y = 0
	# x and y are attributes for calculating where we are ,
	# when we draw 'virtually' the lines, so we can calculate
	# every contact point.
	# self.origin_x an self_origin_y are useful when actually
	# drawing the class, they're the absolute positioning
	# that are set by the topology all the whole drawing.
	self.origin_x = 0
	self.origin_y = 0
	self.the_contact_points = {}
	# this in the drawing of the fruit would be 3
	self._index_of_class = 0

    def do_origin_x(self, x):
	self.origin_x = x

    def do_origin_y(self, y):
	self.origin_y = y

    ''' propagate_origin are functions useful when we have several
    levels of nesting, for example in an union-derivation, in 
    that case and when we start to say to lower drawings which
    are their absolute positions. Then they propagate those 
    absolute positions, in a simple class they have no much
    sense. But they are needed !!
    '''
    def propagate_origin_x(self,x):
	self.do_origin_x(x)

    def propagate_origin_y(self,y):
	self.do_origin_y(y)

    def do_name_clase(self,name_clase):
	''' in the example of the fruit, this would set self.name_clase
	to fruit '''
	self.name_clase = string.replace(name_clase,"_","\_")
	self.calculate_width()
    
    def do_attributes(self, list_of_atributes):
	for i in  list_of_atributes: 
	    self.list_of_atributes.append(string.replace(i,"_","\_"))
	self.calculate_width()

    def calculate_width(self):
	" \
	calculate_width has the responsability of set the width \
 	of the class, so it must guess which of the words is the \
	longest one \
	"
	j = len(self.name_clase)
	for i in self.list_of_atributes+self.list_of_functions :
	    if len(i) > j:
		j = len(i)
	if j>30:
	    self.width_of_the_box = j /2.5
	else:
	    self.width_of_the_box = j /2.2 

    def do_functions(self, list_of_functions):
	for i in  list_of_functions: 
	    self.list_of_functions.append(string.replace(i,"_","\_"))
	self.calculate_width()

    def index_of_class(self, n):
	self._index_of_class = n

    ''' virtually_drawing, only calculates where we are moving to so we can decide on which position we set the contact points '''
    def virtually_drawing(self,l):
	for i in l:
	    if isinstance(i,vertical_line):
		self.y = self.y + i.what_y()		
	    elif isinstance(i, horizontal_line):
		self.x = self.x + i.what_x()
	    elif isinstance(i, contact_point):
		i.do_x(self.x)
		i.do_y(self.y)
		self.the_contact_points[i.what_name()] = i
	    self.list_of_lines.append(i)

    def do_lines(self):
	" this draw the lines of the drawing, but ... virtually!"
	self.do_upwards_line()
	self.do_lines_of_the_right()
	self.do_downwards_line()
	self.do_lines_of_the_left()

    def do_upwards_line(self):
	j = self.width_of_the_box 
	parte_uno = j * self.what_scale() / 2	
	self.virtually_drawing([horizontal_line(parte_uno),
				 contact_point('superior'),
				 horizontal_line(parte_uno)])


    def do_lines_of_the_left(self):
	" really it draws one single line!"
	j = len(self.list_of_atributes+self.list_of_functions) + 4
	if len(self.list_of_atributes) <2 :
	    j = j + 3
	adding = []

	self.virtually_drawing([vertical_line(j),
				contact_point('leftsided'),
				vertical_line(j)] + adding)


    def do_lines_of_the_right(self):
	" really it draws one single line!"
	j = len(self.list_of_atributes+self.list_of_functions) + 4
	if len(self.list_of_atributes) <2 :
	    j = j + 3
	# at the end of this line is the place where we can
	# insert the index of the class
	adding = []
	if self._index_of_class != 0:
	    adding = [a_placed_line(-3.5,0,3.5,3.5),
		      a_placed_text(-2.5,1, `self._index_of_class`)]
	self.virtually_drawing([vertical_line(-j),
				 contact_point('rightsided'),
				 vertical_line(-j)]+ adding)


    def do_downwards_line(self):
	j = self.width_of_the_box
	parte_uno = j * self.what_scale() / 2	
	self.virtually_drawing([horizontal_line(-parte_uno),
				 contact_point('inferior'),
				 horizontal_line(-parte_uno)])

    def give_me_contact_point(self,i):
	return self.the_contact_points[i]

    def imprime_list_of_lines(self):
	" This function is really useful for debugging purposes !!"
	self.x = 0
	self.y = 0
	print self.list_of_lines
	for i in self.list_of_lines:
	    if isinstance(i,vertical_line):
		self.y = self.y + i.what_y()
		print "vertical_line", self.y
	    elif isinstance(i, horizontal_line):
		self.x = self.x + i.what_x()
		print "horizontal_line", self.x
	    else: # it's a contact point
		print "contact point ", i.what_name(),  i.what_x(), i.what_y()		
		
    def generate_headings(self,f):
	" this function is only needed when the simple class is the \
	only one drawing, so it must generate the latex command of \
	the preface of the drawing! "
	global sscale, typeofletter
	fi = open(f,'w')
	fi.write(  "\\begin{center}\n"+ "\\begin{texdraw}\n"+ typeofletter+"  \\drawdim {cm}\n \setunitscale "+sscale + " \\linewd 0.02\n" )
	return fi

    def generate_latex_code(self,f):
	" This function is only  needed when the simple class is the \
	only one drawing, then it generates latex heading , footnote,\
	and  the class itself"
	fi =self.generate_headings(f)
	self.core_generate_latex_code(fi)
	self.generate_footnote(fi)

    def core_generate_latex_code(self,fi):
	" Draw the lines, and all the words of the drawing \
	of the simple class "
	self.genera_labels(fi)
	for i in self.list_of_lines:
	    if isinstance(i,vertical_line):
		fi.write("\\rlvec(0 "+`i.what_y()`+" )\n")
	    elif isinstance(i, horizontal_line):
		fi.write( "\\rlvec( "+`i.what_x()`+" 0)\n")
	    elif isinstance(i, a_placed_text):
		i.generate_move(fi)
	    elif isinstance(i, a_placed_line):
		i.generate_move(fi)


    def generate_footnote(self,fi):
	fi.write( "\\end{texdraw}\n"+  "\\end{center}\n")
	fi.close()

    def adjusted_text(self,f, x, y ,e):
	" It's a higher level way of placing an object in x,y"
	f.write("\\htext("+`x`+" "+`y`+" ) { "+e+" }\n ")

    def genera_labels(self,f):
	" this function really draws the simple class"
	self.generate_label_name_of_class(f)
	self.generate_atributes(f)
	self.generate_functions(f)
	self.draw_line_of_the_box_of_the_name_of_the_class(f)
	self.draw_line_of_the_box_of_the_functions_of_the_class(f)

    def generate_label_name_of_class(self, f):
	generate_move(f,self.origin_x, self.origin_y )
	f.write("\\rlvec(0 0) \\textref h:L v:C ")
	self.adjusted_text(f, 0.5 + self.origin_x, -1 + self.origin_y, self.name_clase)

    def generate_atributes(self,f):
	generate_move(f,self.origin_x, self.origin_y )
	f.write("\\rlvec(0 0) \\textref h:L v:C ")
	counter = -2.8
	for i in self.list_of_atributes:
	    counter = counter - 1.7
	    self.adjusted_text(f,0.5 + self.origin_x, counter + -1 + self.origin_y, i)

    def generate_functions(self,f):
	" generate the functions of the class, in the example of fruits \
	they'd be eat,take, buy "
	generate_move(f,self.origin_x, self.origin_y )
	f.write("\\rlvec(0 0) \\textref h:L v:C ")
	counter = -self.poor_adjustment()
	for i in self.list_of_functions:
	    counter = counter - 1.6
	    self.adjusted_text(f,0.5 + self.origin_x, counter + -1 + self.origin_y, i)

    def draw_line_of_the_box_of_the_name_of_the_class(self,f):
	" this line is the one below the name of the class"
	j = self.width_of_the_box
	j = j * self.what_scale() 
	generate_move(f, self.origin_x, self.origin_y - 2.4)
	f.write("\\rlvec("+`j` +" 0 )")
	generate_move(f, self.origin_x, self.origin_y )

    def draw_line_of_the_box_of_the_functions_of_the_class(self,f):
	" this line is the one above the functions of the class"
	if self.list_of_functions == []:
	    return
	j = self.width_of_the_box
	j = j * self.what_scale() 
	yoffset = self.poor_adjustment()
	generate_move(f, self.origin_x, self.origin_y - yoffset)
	print self.name_clase,yoffset
	f.write("\\rlvec("+`j` +" 0 )")
	generate_move(f, self.origin_x, self.origin_y )

    def poor_adjustment(self):
	yoffset = len(self.list_of_atributes)* 2.9
	if yoffset < 10 :
	    yoffset = 10
	return yoffset

''' 
union_derivation is intended to do things like :

    ------
    |    | 
    | a  |
    |----|
      |
      |
      d
   ----------
   |        | 
   |        |
  ---      ---
  |b|      |c|
  ---      ---

where : a,b,c are  simple classes or another union_derivationes.

d is a triangle :)
'''


''' 
See the comments of simple_class first , because I won t repeat
them here again . 

'''
'''
Terminology, the mother class is sometimes called part_a,
the first daughter class is sometimes called part_b, and 
the rest of daughter classes are called parts_c 
'''

class  union_derivation(scale):
    def __init__(self):
	scale.__init__(self,2)
	self.x = 0
	self.y = 0
	self.the_contact_points={}
	self.parts_c = []
	self.groups_of_lines=[]

    def give_me_contact_point(self,i):
	return self.the_contact_points[i]

    def do_origin_x(self, x):
	self.origin_x = x

    def do_origin_y(self, y):
	self.origin_y = y

    ''' 
    Here the propagations are more serious than in a simple_class \
    the require absolute positioning of the class involved in the\
    drawing '''
    def propagate_origin_x(self,x):
	print "propagating x at",x
	self.part_a.do_origin_x(x)
	cp = self.part_a.give_me_contact_point('inferior')
	self.do_origin_x ( cp.what_x() + self.part_a.origin_x )

    def propagate_origin_y(self,y):
	self.part_a.do_origin_y(y)
	cp = self.part_a.give_me_contact_point('inferior')
	self.do_origin_y ( cp.what_y() + self.part_a.origin_y )

    def virtually_drawing(self,l, list_of_lines):
	for i in l:
	    if isinstance(i,vertical_line):
		self.y = self.y + i.what_y()		
	    elif isinstance(i, horizontal_line):
		self.x = self.x + i.what_x()
	    elif isinstance(i, diagonal_line):
		self.y = self.y + i.what_y() 
		self.x = self.x + i.what_x() 
	    elif isinstance(i, a_placed_text):
		pass
	    elif isinstance(i, a_placed_line):
		pass
	    elif isinstance(i, contact_point) :
		i.do_x(self.x)
		i.do_y(self.y)
		self.the_contact_points[i.what_name()] = i
	    list_of_lines.append(i)

    def add_a_new_derivation(self,c):
	" This function adds a derivation to a compound class-derived\
	class"
	self.parts_c.append(c)
	cp = self.give_me_contact_point(`2*len(self.parts_c) - 1`+' next_derivation')
	print "Adding new derivation at ",  cp.what_x(),  cp.what_y()
        j = self.what_scale() / 2 
	k = []
	# all this draws the arm the joins a daughter with the 
	# rest of the family
	# besides, notice, that odd (1,3,5) contact points are 
	# places for new daughters of the family, and even (0,2,4)
	# are places where the 'current' daughter is actually 
	# placed
	self.virtual_move(self.give_me_contact_point(`2*len(self.parts_c) -1`+' next_derivation'))
	self.virtually_drawing([horizontal_line( j * self.last_width),
				contact_point(`2*len(self.parts_c) +1`+' next_derivation'),
				vertical_line( -j * 2),
				contact_point(`2*len(self.parts_c)`+' next_derivation')], k)
	self.groups_of_lines.append(list(k))
	self.last_width = c.width_of_the_box * 2 + 2
	    
    def virtual_move(self,c):
	self.x = c.what_x()
	self.y = c.what_y()

    def add_union(self, a, b):
	" a is the union_derivation or the class mother and\
	b is the derived class "
	if isinstance(a, union_derivation):
	    self.add_a_new_derivation(b)
	else:
	    self.part_a = a
	    self.part_b = b
	    self.last_width = self.part_b.width_of_the_box *1.5 + 2
	    self.width_of_the_box = self.last_width
	    self.create_the_derivation(a,b)

    def create_the_derivation(self,a,b): 
	cp = a.give_me_contact_point('inferior')
	self.do_origin_x ( cp.what_x() )
	self.do_origin_y ( cp.what_y() )
	print "line 269, consdiag",self.x, self.y
        j = self.what_scale() / 2 
	k = []
	# here it's drawn the arm that joins the mother class
	# and the first daughter. Besides we stablish some contact
	# points, 0 next_derivation, is the place of the first daughter
	# 1 next_derivation the place where the next daughter could
	# join the family
	self.virtually_drawing([vertical_line(- j * 7),
				diagonal_line(j,-j),
				contact_point('1 next_derivation'),
				horizontal_line(-2*j),
				diagonal_line(j,j),
				diagonal_line(-j,-j),
				horizontal_line(-12*j),
				vertical_line( -j * 2),
				contact_point('0 next_derivation')],k)
	self.groups_of_lines.append(list(k))
	

    def generate_headings(self,f):
	global sscale
	fi = open(f,'w')
	fi.write(  "\\begin{center}\n"+ "\\begin{texdraw}\n"+ typeofletter+ "   \\drawdim {cm}\n \setunitscale "+sscale+ " \\linewd 0.02\n" )
	return fi


    def generate_latex_lines(self, lines,fi):
	for i in lines:
	    if isinstance(i,vertical_line):
		fi.write("\\rlvec(0 "+`i.what_y()`+" )\n")
	    elif isinstance(i, horizontal_line):
		fi.write("\\rlvec( "+`i.what_x()`+" 0)\n")
	    elif isinstance(i,diagonal_line):
		fi.write("\\rlvec( "+`i.what_x()`+" "+ `i.what_y()`+")\n")
	    elif isinstance(i, a_placed_text):
		i.generate_move(fi)



    def generate_latex_code(self,fo):
	fi = self.generate_headings(fo)

	self.core_generate_latex_code(fi)

	self.generate_latex_foot(fi)

    def core_generate_latex_code(self,fi):
	self.part_a.core_generate_latex_code(fi)
	generate_move(fi, self.origin_x, self.origin_y)
	self.generate_latex_lines(self.groups_of_lines[0],fi)
	
	cp = self.give_me_contact_point('0 next_derivation')
	# once we've drawn part_a , then we can know where part_b
	# should lie...
	self.part_b.propagate_origin_x ( cp.what_x() + self.origin_x - 3)
	self.part_b.propagate_origin_y ( cp.what_y() + self.origin_y )
	self.part_b.core_generate_latex_code(fi)
	self.generate_rest_derivations(fi)

    def generate_latex_foot(self,fi):
	fi.write( "\\end{texdraw}\n"+  "\\end{center}\n")
	fi.close()

    def generate_rest_derivations(self,f):
	j = 1
	for i in self.parts_c:
	    # the even next derivations (1,3,5,...) mark the
	    # place where new daughters will be placed
	    cp = self.give_me_contact_point(`2*j -1 `+' next_derivation')
	    generate_move(f,cp.what_x() + self.origin_x, cp.what_y() + self.origin_y )
	    self.generate_latex_lines(self.groups_of_lines[j],f)
	    cp = self.give_me_contact_point(`2*j `+' next_derivation')
	    i.propagate_origin_x ( cp.what_x() + self.origin_x - 3)
	    i.propagate_origin_y ( cp.what_y() + self.origin_y )
	    i.core_generate_latex_code(f)
	    j = j + 1


''' 
this is the class that intends to do :

--------|         |----- 
|       |1:1   1:2|     |
|   a   |----------  b  |
|       |         |     |
|-------|         |_____| 

where a and b are simple classes !!

I have done not much else in this class, and you should understand 
this one if you have understood the previous ones.

'''

class  union_association(scale):
    def __init__(self):
	scale.__init__(self,2)
	self.x = 0
	self.y = 0
	self.the_contact_points={}
	self.united = []
	self.parts_c = []
	self.groups_of_lines=[]

    def give_me_contact_point(self,i):
	return self.the_contact_points[i]

    def do_origin_x(self, x):
	self.origin_x = x

    def do_origin_y(self, y):
	self.origin_y = y

    def propagate_origin_x(self,x):
	print "propagating x at",x
	self.part_a.do_origin_x(x)
	cp = self.part_a.give_me_contact_point('inferior')
	self.do_origin_x ( cp.what_x() + self.part_a.origin_x )

    def propagate_origin_y(self,y):
	self.part_a.do_origin_y(y)
	cp = self.part_a.give_me_contact_point('inferior')
	self.do_origin_y ( cp.what_y() + self.part_a.origin_y )

    def virtually_drawing(self,l, list_of_lines):
	for i in l:
	    if isinstance(i,vertical_line):
		self.y = self.y + i.what_y()		
	    elif isinstance(i, horizontal_line):
		self.x = self.x + i.what_x()
	    elif isinstance(i, diagonal_line):
		self.y = self.y + i.what_y()		
		self.x = self.x + i.what_x() 
	    elif isinstance(i, a_placed_text ):
		# this sort doesn't affects internal positioning
		pass
	    elif isinstance(i, contact_point): 
		# it's a contact point
		i.do_x(self.x)
		i.do_y(self.y)
		self.the_contact_points[i.what_name()] = i
	    # otherwise it doesn't affect to the positioning
	    list_of_lines.append(i)

    def add_a_new_association(self,c):
	pass
	    
    def virtual_move(self,c):
	self.x = c.what_x()
	self.y = c.what_y()

    def add_union(self, a, b, texta= None, textb = None):
	" a is the union_derivation or the class mother and\
	b is the derived class "
	if isinstance(a, union_association):
	    self.add_a_new_association(b)
	else:
	    self.part_a = a
	    self.part_b = b
	    self.create_the_association(a,b,texta, textb)

    def create_the_association(self,a,b,texta,textb): 
        j = self.what_scale() / 2 
	cp = a.give_me_contact_point('rightsided')
	self.do_origin_x ( cp.what_x() )
	self.do_origin_y ( cp.what_y() )
	print "line 269, consdiag",self.x, self.y
	k = []
	f = (len(texta) + len(textb)) * 1.6
	self.virtually_drawing([ a_placed_text(0.1*j,j,texta),
				 horizontal_line(f*j),
				 a_placed_text(-j*1.3*len(textb),j,textb),
				 contact_point("rightsided")
				 ], k)
	self.groups_of_lines.append(list(k))
	

    def generate_headings(self,f):
	global sscale
	fi = open(f,'w')
	fi.write(  "\\begin{center}\n"+ "\\begin{texdraw}\n"+ typeofletter+ "   \\drawdim {cm}\n \setunitscale "+sscale+ " \\linewd 0.02\n" )
	return fi


    def generate_latex_lines(self, lines,fi):
	for i in lines:
	    if isinstance(i,vertical_line):
		fi.write("\\rlvec(0 "+`i.what_y()`+" )\n")
	    elif isinstance(i, horizontal_line):
		fi.write("\\rlvec( "+`i.what_x()`+" 0)\n")
	    elif isinstance(i,diagonal_line):
		fi.write("\\rlvec( "+`i.what_x()`+" "+ `i.what_y()`+")\n")
	    elif isinstance(i, a_placed_text):
		i.generate_move(fi)

    def generate_latex_code(self,fo):
	fi = self.generate_headings(fo)

	self.core_generate_latex_code(fi)

	self.generate_latex_foot(fi)

    def core_generate_latex_code(self,fi):
	self.part_a.core_generate_latex_code(fi)
	generate_move(fi, self.origin_x, self.origin_y)
	self.generate_latex_lines(self.groups_of_lines[0],fi)
	
	cp = self.give_me_contact_point('rightsided')
	self.part_b.propagate_origin_x ( cp.what_x() + self.origin_x )
	self.part_b.propagate_origin_y ( cp.what_y() + self.origin_y  + 4) 
	self.part_b.core_generate_latex_code(fi)
	self.generate_rest_derivations(fi)

    def generate_latex_foot(self,fi):
	fi.write( "\\end{texdraw}\n"+  "\\end{center}\n")
	fi.close()

    def generate_rest_derivations(self,f):
	pass 


class  union_aggregation(union_derivation):
    def __init__(self):
	union_derivation.__init__(self)

    def create_the_derivation(self,a,b): 
	" This creates another kind of derivation"
	cp = a.give_me_contact_point('inferior')
	self.do_origin_x ( cp.what_x() )
	self.do_origin_y ( cp.what_y() )
	print "line 269, consdiag",self.x, self.y
        j = self.what_scale() / 2 
	k = []
	# here it's drawn the arm that joins the mother class
	# and the first daughter. Besides we stablish some contact
	# points, 0 next_derivation, is the place of the first daughter
	# 1 next_derivation the place where the next daughter could
	# join the family
	self.virtually_drawing([
	    diagonal_line(j,-j), # we draw    \
	    diagonal_line(-j,-j), #           /
	    diagonal_line(j,j),  # now we return back
	    diagonal_line(-j,j),
	    diagonal_line(-j,-j), # we draw the another /
	    diagonal_line(j,-j),  #                     \   
	    vertical_line(- j * 8),
	    contact_point('1 next_derivation'),
	    horizontal_line(-12*j), # a bit of adjustment here
	    vertical_line( -j * 2),
	    contact_point('0 next_derivation')
	    ],k)
	self.groups_of_lines.append(list(k))
	


def test():
    " this test creates several levels of inheritances"
    c = a_simple_class()
    c.do_name_clase('fruta')
    c.do_attributes(['perecedera' , 'dulce', 'seca', 'carnosa', 
		    'del tiempo', 'tropical'])
    c.do_lines()
    #c.imprime_list_of_lines()
    c.generate_latex_code('result')

    d = a_simple_class()
    d.do_name_clase('empleado')
    d.do_attributes(['paga por horas' , 'sueldo'])
    d.do_lines()
    #d.imprime_list_of_lines()
    d.generate_latex_code('result2')
    e = union_derivation()
    e.add_union(c,d)
    f = a_simple_class()
#    f.do_name_clase('employee')
#    f.do_attributes(['weekly wage' , 'social insecurity'])
#    f.do_lines()
#    e.add_union(e,f)
    g= a_simple_class()
    g.do_name_clase('python')
    g.do_attributes(['easily workable', 'non strict typing'])
    g.do_lines()
    e.add_union(e,g)
    h = a_simple_class()
    h.do_name_clase('fruta')
    h.do_attributes(['perecedera' , 'dulce', 'seca', 'carnosa', 
		    'del tiempo', 'tropical'])
    h.do_lines()
    j = a_simple_class()
    j.do_name_clase('empleado')
    j.do_attributes(['paga por horas' , 'sueldo'])
    j.do_lines()
    i = union_derivation()
    i.add_union(j,h)
    e.add_union(e,i)
    e.generate_latex_code('result3')


def test1():
    " This test make an association"
    c = a_simple_class()
    c.do_name_clase('enterprise')
    c.do_attributes(['name' , 'profits', 'established', 'fiscal paradises', 
		    'frauds', 'bad tricks'])
    c.do_lines()
    #c.imprime_list_of_lines()
    c.generate_latex_code('result')

    d = a_simple_class()
    d.do_name_clase('worker')
    d.do_attributes(['wage' , 'timetable', 'seniority'])
    d.do_lines()
    #d.imprime_list_of_lines()
    #d.generate_latex_code('result2')
    e = union_association()
    e.add_union(c,d,"0:n","0:1")
    e.generate_latex_code('drawing7.tex')

def test2():
    " This tests uses the section of functions in every class, and the marks of repeated-class"
    c = a_simple_class()
    c.do_name_clase('frutas')
    c.do_attributes(['name' , 'dulce', 'seca', 'carnosa', 
		    'del tiempo', 'tropical'])
    c.do_functions(['comer','tirar','vender'])
    c.index_of_class(3)
    c.do_lines()
    #c.imprime_list_of_lines()
    c.generate_latex_code('result3')

def _generate_single_class():
    c = a_simple_class()
    c.do_name_clase('enterprise')
    c.do_attributes(['name' , 'benefits', 'money', 'health', 
		    'hobbies', 'dreams', 'under_score.'])
    c.do_lines()

    c.generate_latex_code('drawing1.tex')

def _generate_single_class_long_names():
    c = a_simple_class()
    c.do_name_clase('person')
    c.do_attributes(['name' , 'sex', 'money', 'health', 
		    'hobbies', 'dreams', 'opinion about latest polls',
		     'position when playing soccer',
		     'favourite scripting language'])
    c.do_lines()
    c.generate_latex_code('drawing2.tex')

def _generate_single_class_function_and_mark():
    c = a_simple_class()
    c.do_name_clase('person')
    c.do_attributes(['name' , 'sex', 'money', 'health', 
		    'hobbies', 'dreams'])
    c.do_functions(['dream', 'love', 'play soccer', 'diving'])
    c.index_of_class(3)

    c.do_lines()
    c.generate_latex_code('drawing3.tex')

def _generate_simple_inheritance():
    c = a_simple_class()
    c.do_name_clase('person')
    c.do_attributes(['name' , 'sex', 'money', 'health', 
		    'hobbies', 'dreams'])
    c.do_functions(['dream', 'love', 'play soccer', 'diving'])

    c.do_lines()
    d = a_simple_class()
    d.do_name_clase('work')
    d.do_attributes(['salary' , 'post',
		    'labor union', 'productivity', 'qualifications',
		     'capabilities'])
    d.do_functions(['work', 'strike', 'be ill', 'be fired'])

    d.do_lines()
    e = union_derivation()
    e.add_union(c,d)
    e.generate_latex_code('drawing4.tex')

def _generate_multiple_inheritance():
    c = a_simple_class()
    c.do_name_clase('person')
    c.do_attributes(['name' , 'sex', 'money', 'health', 
		    'hobbies', 'dreams'])

    c.do_lines()
    d = a_simple_class()
    d.do_name_clase('worker')
    d.do_attributes(['salary' , 'post',
		    'labor union', 'productivity', 'qualifications',
		     'capabilities'])

    d.do_lines()
    f = a_simple_class()
    f.do_name_clase('father')
    f.do_attributes(['busy' , 'tenderness',
		    'stubornness', 'caress', 'kisses', 'capabilities'])

    f.do_lines()
    g = a_simple_class()
    g.do_name_clase('chess player')
    g.do_attributes(['elo' , 'cruelty',
		    'stubornness', 'bad tricks', 'intelligence', 'openings knowledge'])

    g.do_lines()
    e = union_derivation()
    e.add_union(c,d)
    e.add_union(e,f)
    e.add_union(e,g)
    e.generate_latex_code('drawing5.tex')

def _generate_multiple_inheritance_with_multiple_levels():
    d = a_simple_class()
    d.do_name_clase('worker')
    d.do_attributes(['salary' , 'post',
		    'labor union', 'productivity', 'qualifications',
		     'capabilities'])


    d.do_lines()
    f = a_simple_class()
    f.do_name_clase('computer scientist')
    f.do_attributes(['languages' , 'preferred OS',
		    'preferred editor', 'hackerism', 'ATBB'])

    f.do_lines()

    h = union_derivation()
    h.add_union(d,f)

    c = a_simple_class()
    c.do_name_clase('person')
    c.do_attributes(['name' , 'sex', 'money', 'health', 
		    'hobbies', 'dreams'])
    c.do_lines()

    e = union_derivation()
    e.add_union(c,h)
    e.generate_latex_code('drawing6.tex')

def _generate_associations():
    test1()


def _generate_multiple_aggregation():
    c = a_simple_class()
    c.do_name_clase('person')
    c.do_attributes(['name' , 'sex', 'money', 'health', 
		    'hobbies', 'dreams'])

    c.do_lines()
    d = a_simple_class()
    d.do_name_clase('worker')
    d.do_attributes(['salary' , 'post',
		    'labor union', 'productivity', 'qualifications',
		     'capabilities'])

    d.do_lines()
    f = a_simple_class()
    f.do_name_clase('father')
    f.do_attributes(['busy' , 'tenderness',
		    'stubornness', 'caress', 'kisses', 'capabilities'])

    f.do_lines()
    g = a_simple_class()
    g.do_name_clase('chess player')
    g.do_attributes(['elo' , 'cruelty',
		    'stubornness', 'bad tricks', 'intelligence', 'openings knowledge'])

    g.do_lines()
    e = union_aggregation()
    e.add_union(c,d)
    e.add_union(e,f)
    e.add_union(e,g)
    e.generate_latex_code('drawing8.tex')


if __name__=='__main__':
    _generate_single_class()
    _generate_single_class_long_names()
    _generate_single_class_function_and_mark()
    _generate_simple_inheritance()
    _generate_multiple_inheritance()
    _generate_multiple_inheritance_with_multiple_levels()
    _generate_associations()
    _generate_multiple_aggregation()