ontomde-uml2-java 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,188 @@
1
+ # define enum as string compatibility layer
2
+ #
3
+ # Feature introduced for FLEX for Java 1.5 / Java 1.4 interoperability
4
+ #
5
+ #
6
+
7
+ module Mrdf_Model
8
+ def java_addEnumCompatibilityLayerProperties!
9
+ uml_Class_all.each {|c|
10
+ next unless c.kind_of?(Muml_Class)
11
+ c.uml_ownedAttribute.each {|e|
12
+ e.java_addEnumStringAttribute!
13
+ }
14
+ }
15
+ end
16
+ def java_addEnumCompatibilityLayerMethods!
17
+ uml_Class_all.each {|c|
18
+ next unless c.kind_of?(Muml_Class)
19
+ c.java_addEnumPostDeserialiseUpdate!
20
+ }
21
+ end
22
+ end
23
+
24
+ module Muml_Classifier
25
+ def java_isEnumStringMapping?(oa)
26
+ return !oa.umlx_stringMappingForEnum.empty?
27
+ end
28
+
29
+ def java_writeEnumStringMappingGetterCode(oa)
30
+ # getter for compatibility layer (enum visible as string)
31
+ enum_oa=oa.umlx_stringMappingForEnum_one
32
+ if oa.umlx_upperValueIsOne?
33
+ write "/* TPL:0012b */ return (#{enum_oa.java_NameProperty}==null) ? null : (#{enum_oa.java_NameProperty}.name()); "
34
+
35
+ else
36
+ write %{ /*TPL:0012c */
37
+ java.util.Set<java.lang.String> ret=new java.util.HashSet<java.lang.String>();
38
+ for ( java.util.Iterator<#{enum_oa.uml_type_one.java_qualifiedName}> iter = #{enum_oa.java_NameProperty}.iterator(); iter.hasNext();) {
39
+ ret.add(iter.next().name());
40
+ }
41
+ return ret;\n}
42
+ end
43
+ end
44
+ def java_writeEnumStringMappingSetterCode(oa)
45
+ oa_enum=oa.umlx_stringMappingForEnum_one
46
+ #Note: throws a runtime exception if enum is not found
47
+ #Note: valueOf throws null pointer exception
48
+ if oa.umlx_upperValueIsOne?
49
+ write "#{oa_enum.java_NameProperty}= #{getterParameterName(oa)}==null ? null : Enum.valueOf(#{oa_enum.uml_type_one.java_qualifiedName}.class,#{getterParameterName(oa)}); /* TPL:0014c*/"
50
+ else
51
+ # TODO: provide java implementation
52
+ end
53
+ end
54
+
55
+ end
56
+ module Muml_Property
57
+ #string version of this enum property
58
+ rdf_safe_attr_reader_many :umlx_stringMappingForEnum
59
+
60
+ #add string as enum compatibility layer string
61
+ def java_addEnumStringAttribute!
62
+ return unless uml_type_one.kind_of?(Muml_Enumeration)
63
+ c=umlx_owner_one
64
+ a=self
65
+ #log.debug { "adding attribute for #{self} #{c}" }
66
+ rr=c.umlx_createAndAddProperty(a.rdf_uri+"_asString",a.uml_name_one+"AsString")
67
+ rr.uml_type=c.umlx_dataType_string
68
+ rr.uml_visibility=::Cuml_VisibilityKind::Private
69
+
70
+ rr.umlx_stringMappingForEnum=a
71
+ rr.uml_isDerived=RDF_TRUE
72
+ rr.uml_upperValue=a.uml_upperValue
73
+ rr.uml_lowerValue=a.uml_lowerValue
74
+ end
75
+ end
76
+
77
+ module Muml_Class
78
+ #returns true if class has at least one enum property
79
+ #Inherited properties are not taken into account.
80
+ def java_hasEnumDirectProperties?
81
+ uml_ownedAttribute.each { |a|
82
+ return true if a.uml_type_one.kind_of?(Muml_Enumeration)
83
+ }
84
+ return false
85
+ end
86
+
87
+ def java_addEnumPostDeserialiseUpdate!
88
+ #TODO optimize
89
+ #return unless java_hasEnumDirectProperties?
90
+
91
+ #rr=umlx_createAndAddOperation(self.rdf_uri+"_"+"readResolve","readResolve")
92
+ #rr.uml_raisedException_add(umlx_getOrCreateClass("java.io.ObjectStreamException"))
93
+ #rr.uml_visibility=::Cuml_VisibilityKind::Private
94
+ #rr.java_code="updateEnumFromEnumAsString();\nreturn this;\n"
95
+
96
+ #rp=rr.umlx_createAndAddReturnParameter(rr.rdf_uri+"_ret")
97
+ #rp.uml_type=umlx_getOrCreateClass("java.lang.Object")
98
+
99
+
100
+ #u=umlx_createAndAddOperation(self.rdf_uri+"_"+"upd_Enum","updateEnumFromEnumAsString")
101
+
102
+
103
+ #code=%{Sy stem.out.println("readResolve #{java_qualifiedName}");\n}
104
+ #code=code+(uml_generalization.empty? ? "//" : "")+"super.#{u.java_Name}();\n"
105
+
106
+ uml_ownedAttribute.each { |a|
107
+ next unless a.uml_type_one.kind_of?(Muml_Enumeration)
108
+ a.java_setter_property_inv.each { |g|
109
+ g.java_code=g.java_code_one+<<END
110
+ \n#{a.umlx_stringMappingForEnum_one.java_Name}=#{a.java_Name}.name();
111
+ END
112
+ }
113
+ a.umlx_stringMappingForEnum_one.java_getter_property_inv.each { |g|
114
+ #asString is not updated when loaded from base
115
+ g.java_code=%{#{a.umlx_stringMappingForEnum_one.java_Name}= #{a.java_Name}==null ? null : #{a.java_Name}.name();\n}+g.java_code_one
116
+ }
117
+ a.umlx_stringMappingForEnum_one.java_setter_property_inv.each { |g|
118
+ g.java_code=g.java_code_one+<<END
119
+ \n#{a.java_Name}=Enum.valueOf(#{a.uml_type_one.java_qualifiedName}.class,#{a.umlx_stringMappingForEnum_one.java_Name});
120
+ END
121
+ }
122
+
123
+ #code=code+<<END
124
+ #if(#{a.umlx_stringMappingForEnum_one.java_Name}!=null) {
125
+ # #{a.java_Name}=Enum.valueOf(#{a.uml_type_one.java_qualifiedName}.class,#{a.umlx_stringMappingForEnum_one.java_Name});
126
+ #} else {
127
+ # #{a.java_Name}=null;
128
+ #}
129
+ #END
130
+ }
131
+ #u.java_code=code;
132
+
133
+ return
134
+ end
135
+
136
+ end
137
+
138
+
139
+ # #Generates a custom serializer
140
+ # def java_addReadWriteExternal!
141
+ # return unless java_hasEnumDirectProperties?
142
+ # java_addReadExternal!
143
+ # java_addWriteExternal!
144
+ # write("super(output)")
145
+ # end
146
+ # def java_addReadExternal!
147
+ # m=umlx_createAndAddOperation(action.rdf_uri+"_"+"readExternal","readExternal")
148
+ # p=afo.umlx_createAndAddParameter(m.rdf_uri+"input","input")
149
+ # p.uml_class=umlx_getOrCreateClass("java.io.ObjectInput")
150
+ # m.uml_raisedException_add(umlx_getOrCreateClass("java.lang.ClassNotFoundException"))
151
+ # m.uml_raisedException_add(umlx_getOrCreateClass("java.io.IOException"))
152
+ # m.java_code=java_getReadExternalCode("input")
153
+ # return m
154
+ # end
155
+ # def java_addWriteExternal!
156
+ # m=umlx_createAndAddOperation(action.rdf_uri+"_"+"writeExternal","writeExternal")
157
+ # p=afo.umlx_createAndAddParameter(m.rdf_uri+"output","output")
158
+ # p.uml_class=umlx_getOrCreateClass("java.io.ObjectOuput")
159
+ # m.uml_raisedException_add(umlx_getOrCreateClass("java.io.IOException"))
160
+ # m.java_code=java_getWriteExternalCode("output")
161
+ # return m
162
+ # end
163
+ # def java_getWriteExternalCode(varname)
164
+ # code="super(#{varname});\n"
165
+ # uml_ownedAttribute.each { |a|
166
+ # //TODO: collection of Enumeration
167
+ # if(a.uml_type_one.kind_of?(Muml_Enumeration)) {
168
+ # code=code+"#{varname}.writeObject(#{a.java_Name}.name());\n"
169
+ # } else {
170
+ # code=code+"#{varname}.writeObject(#{a.java_Name});\n"
171
+ # }
172
+ # }
173
+ # return code
174
+ # end
175
+ # def java_getReadExternalCode(varname)
176
+ # code="super(#{varname});\n"
177
+ # uml_ownedAttribute.each { |a|
178
+ # //TODO: collection of Enumeration
179
+ # if(a.uml_type_one.kind_of?(Muml_Enumeration)) {
180
+ # code=code+"#{a.java_Name}=(#{a.uml_type_one.java_qualifiedName})Enum.value(#{a.uml_type_one.java_qualifiedName},(String)#{varname}.readObject());\n"
181
+ # } else {
182
+ # code=code+"#{a.java_Name}=(#{a.uml_type_one.java_qualifiedName})#{varname}.readObject();\n"
183
+ # }
184
+ # }
185
+ # return code
186
+ # end
187
+ #
188
+ #end
@@ -0,0 +1,91 @@
1
+ module Muml_Classifier
2
+
3
+ # adds a equals and a hash method to the current class.
4
+ # Signature is computed from attributes stereoptyped EqualsValue
5
+ # NOTE:
6
+ # method is not generated if there is no attribute stereotyped EqualsValue
7
+ def java_addEqualsAndHashOperations(callSuperEquals)
8
+ ret=Set.new
9
+ uml_ownedAttribute.each {|biz|
10
+ ret << biz if biz.umlx_hasStereotype?("EqualsValue")
11
+ }
12
+ if !ret.empty?
13
+ puts "generate hash and equals for #{self.java_Name}"
14
+ java_addEqualsOperation!(ret, callSuperEquals)
15
+ java_addHashCodeOperation!(ret)
16
+ end
17
+ return nil
18
+ end
19
+
20
+ #adds a equals method
21
+ def java_addEqualsOperation!(attributesInSignature, callSuperEquals)
22
+ me=self.umlx_createAndAddOperation(self.rdf_uri+"_equals")
23
+ me.uml_name="equals"
24
+ # me.uml_class=self
25
+ me.uml_visibility=Cuml_VisibilityKind::Public
26
+ rme=me.umlx_createAndAddReturnParameter("#{me.rdf_uri}_ret")
27
+ rme.uml_type=umlx_getOrCreateDataType("boolean")
28
+ rpe=me.umlx_createAndAddParameter("#{me.rdf_uri}_par", "o")
29
+ rpe.uml_type=umlx_getOrCreateClass("Object")
30
+ me.java_annotation_add("@Override")
31
+ meCode = <<CODE
32
+ if(this == o) {
33
+ return true;
34
+ }
35
+ if(o==null || !(o instanceof #{self.java_Name})) {
36
+ return false;
37
+ }
38
+ CODE
39
+ if callSuperEquals
40
+ meCode << <<CODE
41
+ if !super.equals(o) {
42
+ return false;
43
+ }
44
+ CODE
45
+ else
46
+ meCode << "// do not use super.equals()\n"
47
+ end
48
+ meCode << <<CODE
49
+ final #{self.java_Name} _o = (#{self.java_Name}) o;
50
+ CODE
51
+ attributesInSignature.each { |a|
52
+ meCode << <<CODE
53
+ if (#{a.java_Name} != null ?
54
+ !#{a.java_Name}.equals(_o.#{a.java_Name}) :
55
+ _o.#{a.java_Name} != null) {
56
+ return false;
57
+ }
58
+ CODE
59
+ }
60
+ meCode << <<CODE
61
+ return true;
62
+ CODE
63
+ me.java_code = meCode
64
+ return me
65
+ end
66
+
67
+ #adds a hash method
68
+ def java_addHashCodeOperation!(attributesInSignature)
69
+ me=self.umlx_createAndAddOperation(self.rdf_uri+"_hash")
70
+ me.uml_name="hashCode"
71
+ me.uml_class=self
72
+ me.uml_visibility=Cuml_VisibilityKind::Public
73
+ rme=me.umlx_createAndAddReturnParameter("#{me.rdf_uri}_ret")
74
+ rme.uml_type=umlx_getOrCreateDataType("int")
75
+ me.java_annotation_add("@Override")
76
+ meCode = <<CODE
77
+ final int prime = 31;
78
+ int result = 1;
79
+ CODE
80
+ attributesInSignature.each { |a|
81
+ meCode << <<CODE
82
+ result = prime * result + ((#{a.java_Name} == null) ? 0 : #{a.java_Name}.hashCode());
83
+ CODE
84
+ }
85
+ meCode << <<CODE
86
+ return result;
87
+ CODE
88
+ me.java_code = meCode
89
+ return me
90
+ end
91
+ end
@@ -0,0 +1,56 @@
1
+ #
2
+ # processor to implement method as groovy code
3
+ # (experimental alpha feature)
4
+ #
5
+
6
+ module Mrdf_Model
7
+ #enhance model operation by enabling groovy implementations.
8
+ #only method with no code are enhanced.
9
+ def java_addGroovyImplementations!
10
+
11
+ end
12
+ end
13
+
14
+
15
+ module Muml_Operation
16
+
17
+ #implement a method as groovy code.
18
+ def umlx_implementMethodAsGroovy!
19
+ code="groovy.lang.Binding binding = new groovy.lang.Binding();\n"
20
+ code=code+"// binding java variables to groovy variables\n"
21
+ uml_ownedParameter.each { |p|
22
+ tpl=JavaMapping.instance.getTemplate(p.uml_type_one)
23
+ c=tpl.getGroovyCast(p)
24
+ if(c.nil?)
25
+ code=code+%{//TODO: binding.setVariable("#{p.java_Name}", #{p.java_Name} );\n}
26
+ else
27
+ code=code+%{//TODO: binding.setVariable("#{p.java_Name}",);\n}
28
+ end
29
+ }
30
+ code=code+"\n//running groovy \n"
31
+
32
+ code=code+%{groovy.lang.GroovyShell shell = new groovy.lang.GroovyShell(binding);\n}
33
+ code=code+%{Object value = shell.evaluate( "println 'Hello World!'; "+ getGroovyCode);\n}
34
+
35
+ code=code+"\n//retrieving return value from groovy (if any)\n"
36
+
37
+ r=umlx_returnResult_one0
38
+ if !r.nil?
39
+ tpl=JavaMapping.instance.getTemplate(r.uml_type_one)
40
+ code=code+%{\nthrow(new java.lang.RuntimeException("process return value");\n}
41
+ else
42
+ code=code+%{\n//noting to return\n}
43
+ end
44
+
45
+ #assert value.equals(new Integer(20));
46
+ #assert binding.getVariable("x").equals(new Integer(123));
47
+
48
+ end
49
+ end
50
+
51
+ class JavaMapping
52
+ #return a cast suitable for using with groovy binding.setVariable
53
+ def getGroovyCast(param)
54
+ return nil
55
+ end
56
+ end