rgen 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,132 @@
1
+ require 'rgen/metamodel_builder'
2
+ require 'rgen/instantiator'
3
+ require 'rgen/name_helper'
4
+ require 'rexml/parsers/sax2parser'
5
+ require 'rexml/sax2listener'
6
+
7
+ module RGen
8
+
9
+ class XMLInstantiator < Instantiator
10
+ include REXML::SAX2Listener
11
+ include NameHelper
12
+
13
+ class << self
14
+ def tag_ns_map
15
+ @tag_ns_map ||={}
16
+ @tag_ns_map
17
+ end
18
+ end
19
+
20
+ class XMLElementDescriptor
21
+ attr_reader :tag, :mod, :parent, :attributes
22
+ attr_accessor :object, :children
23
+ def initialize(tag, mod, parent, children, attributes)
24
+ @tag, @mod, @parent, @children, @attributes = tag, mod, parent, children, attributes
25
+ @parent.children << self if @parent
26
+ end
27
+ end
28
+
29
+ NamespaceDescriptor = Struct.new(:prefix, :target)
30
+
31
+ def self.map_tag_ns(from, to, prefix="")
32
+ tag_ns_map[from] = NamespaceDescriptor.new(prefix, to)
33
+ end
34
+
35
+ def initialize(env, mod, createMM=false)
36
+ super(env,mod)
37
+ @env = env
38
+ @default_module = mod
39
+ @createMM = createMM
40
+ @stack = []
41
+ end
42
+
43
+ def instantiate_file(file)
44
+ File.open(file) { |f| parse(f.read)}
45
+ resolve
46
+ end
47
+
48
+ def instantiate(text)
49
+ parse(text)
50
+ resolve
51
+ end
52
+
53
+ def parse(src)
54
+ parser = REXML::Parsers::SAX2Parser.new(src)
55
+ parser.listen(self)
56
+ parser.parse
57
+ end
58
+
59
+ def start_element(ns, tag, qtag, attributes)
60
+ ns_desc = self.class.tag_ns_map[ns]
61
+ tag = ns_desc.nil? ? qtag : ns_desc.prefix+tag
62
+ mod = (ns_desc && ns_desc.target) || @default_module
63
+ @stack.push XMLElementDescriptor.new(tag, mod, @stack[-1], [], attributes)
64
+ end
65
+
66
+ def end_element(uri, localname, qname)
67
+ elementDesc = @stack.pop
68
+ obj = callBuildMethod(:new_object, NameError, :create_class, elementDesc.mod, elementDesc)
69
+ @env << obj
70
+ elementDesc.object = obj
71
+ elementDesc.children.each { |c|
72
+ callBuildMethod(:assoc_p2c, NoMethodError, :create_p2c_assoc, elementDesc, c)
73
+ }
74
+ elementDesc.attributes.each_pair {|k,v|
75
+ callBuildMethod(:set_attribute, NoMethodError, :create_attribute, elementDesc, k, v)
76
+ }
77
+ # optionally prune children to save memory
78
+ #elementDesc.children = nil
79
+ end
80
+
81
+ def callBuildMethod(method, exception, mmMethod, *args)
82
+ begin
83
+ send(method, *args)
84
+ rescue exception
85
+ if @createMM
86
+ send(mmMethod, *args)
87
+ send(method, *args)
88
+ else
89
+ raise
90
+ end
91
+ end
92
+ end
93
+
94
+ # Model and Metamodel builder methods
95
+ # These methods are to be overwritten by specific instantiators
96
+ def new_object(mod, node)
97
+ mod.const_get(saneClassName(node)).new
98
+ end
99
+
100
+ def create_class(mod, node)
101
+ mod.const_set(saneClassName(node), Class.new(RGen::MetamodelBuilder::MMBase))
102
+ end
103
+
104
+ def assoc_p2c(parent, child)
105
+ parent.object.addGeneric(saneMethodName(child), child.object)
106
+ child.object.setGeneric("parent", parent.object)
107
+ end
108
+
109
+ def create_p2c_assoc(parent, child)
110
+ parent.object.class.has_many(saneMethodName(child), child.object.class)
111
+ child.object.class.has_one("parent", RGen::MetamodelBuilder::MMBase)
112
+ end
113
+
114
+ def set_attribute(node, attr, value)
115
+ node.object.setGeneric(normalize(attr), value)
116
+ end
117
+
118
+ def create_attribute(node, attr, value)
119
+ node.object.class.has_one(normalize(attr))
120
+ end
121
+
122
+ def saneClassName(node)
123
+ firstToUpper(normalize(node.tag)).sub(/^Class$/, 'Clazz')
124
+ end
125
+
126
+ def saneMethodName(node)
127
+ firstToLower(normalize(node.tag)).sub(/^class$/, 'clazz')
128
+ end
129
+
130
+ end
131
+
132
+ end
@@ -28,6 +28,9 @@ class MetamodelBuilderTest < Test::Unit::TestCase
28
28
  assert_raise StandardError do
29
29
  sc.an_array = "a string"
30
30
  end
31
+
32
+ assert_equal HasOneTestClass.one_attributes, ["name", "an_array"]
33
+ assert_equal HasOneTestClass.many_attributes, []
31
34
  end
32
35
 
33
36
  class HasManyTestClass < RGen::MetamodelBuilder::MMBase
@@ -49,6 +52,8 @@ class MetamodelBuilderTest < Test::Unit::TestCase
49
52
  assert_raise StandardError do
50
53
  o.addString(:notastring)
51
54
  end
55
+ assert_equal HasManyTestClass.one_attributes, []
56
+ assert_equal HasManyTestClass.many_attributes, ["string"]
52
57
  end
53
58
 
54
59
  class OneClass < RGen::MetamodelBuilder::MMBase
@@ -86,6 +91,53 @@ class MetamodelBuilderTest < Test::Unit::TestCase
86
91
  oc.removeManyClasses mc
87
92
  assert !oc.manyClasses.include?(mc)
88
93
  assert_equal nil, mc.oneClass
94
+
95
+ assert_equal OneClass.one_attributes, []
96
+ assert_equal OneClass.many_attributes, ["manyClasses"]
97
+ assert_equal ManyClass.one_attributes, ["oneClass"]
98
+ assert_equal ManyClass.many_attributes, []
99
+ end
100
+
101
+ class OneClass2 < RGen::MetamodelBuilder::MMBase
102
+ end
103
+ class ManyClass2 < RGen::MetamodelBuilder::MMBase
104
+ end
105
+ ManyClass2.many_to_one 'oneClass', OneClass2, 'manyClasses'
106
+
107
+ def test_one_to_many2
108
+ oc = OneClass2.new
109
+ assert_respond_to oc, :manyClasses
110
+ assert oc.manyClasses.empty?
111
+
112
+ mc = ManyClass2.new
113
+ assert_respond_to mc, :oneClass
114
+ assert_respond_to mc, :oneClass=
115
+ assert_nil mc.oneClass
116
+
117
+ # put the OneClass into the ManyClass
118
+ mc.oneClass = oc
119
+ assert_equal oc, mc.oneClass
120
+ assert oc.manyClasses.include?(mc)
121
+
122
+ # remove the OneClass from the ManyClass
123
+ mc.oneClass = nil
124
+ assert_equal nil, mc.oneClass
125
+ assert !oc.manyClasses.include?(mc)
126
+
127
+ # put the ManyClass into the OneClass
128
+ oc.addManyClasses mc
129
+ assert oc.manyClasses.include?(mc)
130
+ assert_equal oc, mc.oneClass
131
+
132
+ # remove the ManyClass from the OneClass
133
+ oc.removeManyClasses mc
134
+ assert !oc.manyClasses.include?(mc)
135
+ assert_equal nil, mc.oneClass
136
+
137
+ assert_equal OneClass2.one_attributes, []
138
+ assert_equal OneClass2.many_attributes, ["manyClasses"]
139
+ assert_equal ManyClass2.one_attributes, ["oneClass"]
140
+ assert_equal ManyClass2.many_attributes, []
89
141
  end
90
142
 
91
143
  class AClassOO < RGen::MetamodelBuilder::MMBase
@@ -124,6 +176,11 @@ class MetamodelBuilderTest < Test::Unit::TestCase
124
176
  ac.bClass = nil
125
177
  assert_equal nil, ac.bClass
126
178
  assert_equal nil, bc.aClass
179
+
180
+ assert_equal AClassOO.one_attributes, ["bClass"]
181
+ assert_equal AClassOO.many_attributes, []
182
+ assert_equal BClassOO.one_attributes, ["aClass"]
183
+ assert_equal BClassOO.many_attributes, []
127
184
  end
128
185
 
129
186
  class AClassMM < RGen::MetamodelBuilder::MMBase
@@ -171,5 +228,34 @@ class MetamodelBuilderTest < Test::Unit::TestCase
171
228
  ac.removeBClasses bc
172
229
  assert !ac.bClasses.include?(bc)
173
230
  assert !bc.aClasses.include?(ac)
231
+
232
+ assert_equal AClassMM.one_attributes, []
233
+ assert_equal AClassMM.many_attributes, ["bClasses"]
234
+ assert_equal BClassMM.one_attributes, []
235
+ assert_equal BClassMM.many_attributes, ["aClasses"]
236
+ end
237
+
238
+ class SomeSuperClass < RGen::MetamodelBuilder::MMBase
239
+ has_one "name"
240
+ has_many "others"
241
+ end
242
+
243
+ class SomeSubClass < SomeSuperClass
244
+ has_one "subname"
245
+ has_many "subothers"
246
+ end
247
+
248
+ class OtherSubClass < SomeSuperClass
249
+ has_one "othersubname"
250
+ has_many "othersubothers"
251
+ end
252
+
253
+ def test_inheritance
254
+ assert_equal SomeSuperClass.one_attributes, ["name"]
255
+ assert_equal SomeSuperClass.many_attributes, ["others"]
256
+ assert_equal SomeSubClass.one_attributes.sort, ["name", "subname"]
257
+ assert_equal SomeSubClass.many_attributes.sort, ["others", "subothers"]
258
+ assert_equal OtherSubClass.one_attributes.sort, ["name", "othersubname"]
259
+ assert_equal OtherSubClass.many_attributes.sort, ["others", "othersubothers"]
174
260
  end
175
261
  end
@@ -10,13 +10,12 @@ class MetamodelGeneratorTest < Test::Unit::TestCase
10
10
  OUTPUT_DIR = File.dirname(__FILE__)+"/metamodel_generator_test"
11
11
  MM_FILE = OUTPUT_DIR+"/TestModel.rb"
12
12
 
13
- include XMIClassInstantiator
14
13
  include MMGen::MetamodelGenerator
15
14
 
16
15
  def test_generator
17
16
  envUML = RGen::Environment.new
18
17
  File.open(MODEL_DIR+"/testmodel.xml") { |f|
19
- instantiateUMLClassModel(envUML, f.read)
18
+ XMIClassInstantiator.new.instantiateUMLClassModel(envUML, f.read)
20
19
  }
21
20
 
22
21
  rootPackage = envUML.find(:class => UMLClassModel::UMLPackage).select{|p| p.name == "HouseMetamodel"}.first
data/test/rgen_test.rb CHANGED
@@ -11,3 +11,4 @@ require 'xmi_class_instantiator_test'
11
11
  require 'xmi_object_instantiator_test'
12
12
  require 'metamodel_generator_test'
13
13
  require 'transformer_test'
14
+ require 'xml_instantiator_test'
@@ -1,8 +1,12 @@
1
1
  $:.unshift File.join(File.dirname(__FILE__),"..","lib")
2
+ $:.unshift File.join(File.dirname(__FILE__),"..","test")
2
3
 
3
4
  require 'test/unit'
4
5
  require 'rgen/transformer'
5
6
  require 'rgen/environment'
7
+ require 'uml/uml_classmodel'
8
+ require 'ea/xmi_class_instantiator'
9
+ require 'xmi_instantiator_test/class_model_checker'
6
10
 
7
11
  class TransformerTest < Test::Unit::TestCase
8
12
 
@@ -173,4 +177,27 @@ class TransformerTest < Test::Unit::TestCase
173
177
  assert_equal 2, env_out.elements.size
174
178
  assert (t.trans(froms)-env_out.elements).empty?
175
179
  end
180
+
181
+ class CopyTransformer < RGen::Transformer
182
+ include UMLClassModel
183
+ def transform
184
+ trans(:class => UMLPackage)
185
+ end
186
+ constants.each{|c| copy const_get(c) if c =~ /^UML/}
187
+ end
188
+
189
+ MODEL_DIR = File.join(File.dirname(__FILE__),"xmi_instantiator_test")
190
+
191
+ include ClassModelChecker
192
+
193
+ def test_copyTransformer
194
+ envIn = RGen::Environment.new
195
+ envOut = RGen::Environment.new
196
+ File.open(MODEL_DIR+"/testmodel.xml") { |f|
197
+ XMIClassInstantiator.new.instantiateUMLClassModel(envIn, f.read)
198
+ }
199
+ CopyTransformer.new(envIn, envOut).transform
200
+ checkClassModel(envOut)
201
+ end
202
+
176
203
  end
@@ -1,107 +1,24 @@
1
1
  $:.unshift File.join(File.dirname(__FILE__),"..","lib")
2
+ $:.unshift File.join(File.dirname(__FILE__),"..","test")
2
3
 
3
4
  require 'test/unit'
4
5
  require 'ea/xmi_class_instantiator'
6
+ require 'rgen/transformer'
7
+ require 'xmi_instantiator_test/class_model_checker'
5
8
 
6
9
  class XmiClassInstantiatorTest < Test::Unit::TestCase
7
10
 
8
11
  MODEL_DIR = File.join(File.dirname(__FILE__),"xmi_instantiator_test")
9
12
 
10
- include XMIClassInstantiator
11
-
13
+ include ClassModelChecker
14
+
12
15
  def test_model
13
16
  envUML = RGen::Environment.new
14
17
  File.open(MODEL_DIR+"/testmodel.xml") { |f|
15
- instantiateUMLClassModel(envUML, f.read)
18
+ XMIClassInstantiator.new.instantiateUMLClassModel(envUML, f.read)
16
19
  }
17
-
18
- # check main package
19
- mainPackage = envUML.elements.select {|e| e.is_a? UMLPackage and e.name == "HouseMetamodel"}.first
20
- assert_not_nil mainPackage
21
-
22
- # check Rooms package
23
- assert mainPackage.subpackages.is_a?(Array)
24
- assert_equal 1, mainPackage.subpackages.size
25
- assert mainPackage.subpackages[0].is_a?(UMLPackage)
26
- roomsPackage = mainPackage.subpackages[0]
27
- assert_equal "Rooms", roomsPackage.name
28
-
29
- # check main package classes
30
- assert mainPackage.classes.is_a?(Array)
31
- assert_equal 3, mainPackage.classes.size
32
- assert mainPackage.classes.all?{|c| c.is_a?(UMLClass)}
33
- houseClass = mainPackage.classes.select{|c| c.name == "House"}.first
34
- personClass = mainPackage.classes.select{|c| c.name == "Person"}.first
35
- meetingPlaceClass = mainPackage.classes.select{|c| c.name == "MeetingPlace"}.first
36
- assert_not_nil houseClass
37
- assert_not_nil personClass
38
- assert_not_nil meetingPlaceClass
20
+ checkClassModel(envUML)
21
+ end
39
22
 
40
- # check Rooms package classes
41
- assert roomsPackage.classes.is_a?(Array)
42
- assert_equal 3, roomsPackage.classes.size
43
- assert roomsPackage.classes.all?{|c| c.is_a?(UMLClass)}
44
- roomClass = roomsPackage.classes.select{|c| c.name == "Room"}.first
45
- kitchenClass = roomsPackage.classes.select{|c| c.name == "Kitchen"}.first
46
- bathroomClass = roomsPackage.classes.select{|c| c.name == "Bathroom"}.first
47
- assert_not_nil roomClass
48
- assert_not_nil kitchenClass
49
- assert_not_nil bathroomClass
50
-
51
- # check Room inheritance
52
- assert roomClass.subclasses.is_a?(Array)
53
- assert_equal 2, roomClass.subclasses.size
54
- assert_not_nil roomClass.subclasses.select{|c| c.name == "Kitchen"}.first
55
- assert_not_nil roomClass.subclasses.select{|c| c.name == "Bathroom"}.first
56
- assert kitchenClass.superclasses.is_a?(Array)
57
- assert_equal 2, kitchenClass.superclasses.size
58
- assert_equal roomClass.object_id, kitchenClass.superclasses.select{|c| c.name == "Room"}.first.object_id
59
- assert_equal meetingPlaceClass.object_id, kitchenClass.superclasses.select{|c| c.name == "MeetingPlace"}.first.object_id
60
- assert bathroomClass.superclasses.is_a?(Array)
61
- assert_equal 1, bathroomClass.superclasses.size
62
- assert_equal roomClass.object_id, bathroomClass.superclasses[0].object_id
63
23
 
64
- # check House-Room "part of" association
65
- assert houseClass.localCompositeEnds.otherEnd.clazz.is_a?(Array)
66
- assert_equal 1, houseClass.localCompositeEnds.size
67
- roomEnd = houseClass.localCompositeEnds[0].otherEnd
68
- assert_equal UMLAggregation, roomEnd.assoc.class
69
- assert_equal roomClass.object_id, roomEnd.clazz.object_id
70
- assert_equal "room", roomEnd.role
71
- assert_equal "1..*", roomEnd.multiplicity
72
- assert_equal 1, roomEnd.lowerMult
73
- assert_equal :many, roomEnd.upperMult
74
-
75
- assert roomClass.remoteCompositeEnds.clazz.is_a?(Array)
76
- assert_equal 1, roomClass.remoteCompositeEnds.size
77
- assert_equal houseClass.object_id, roomClass.remoteCompositeEnds[0].clazz.object_id
78
- assert_equal "house", roomClass.remoteCompositeEnds[0].role
79
-
80
- # check House OUT associations
81
- assert houseClass.remoteNavigableEnds.is_a?(Array)
82
- assert_equal 2, houseClass.remoteNavigableEnds.size
83
- bathEnd = houseClass.remoteNavigableEnds.select{|e| e.role == "bathroom"}.first
84
- kitchenEnd = houseClass.remoteNavigableEnds.select{|e| e.role == "kitchen"}.first
85
- assert_not_nil bathEnd
86
- assert_not_nil kitchenEnd
87
- assert_equal UMLAssociation, bathEnd.assoc.class
88
- assert_equal UMLAssociation, kitchenEnd.assoc.class
89
- assert_equal "1", kitchenEnd.multiplicity
90
- assert_equal 1, kitchenEnd.lowerMult
91
- assert_equal 1, kitchenEnd.upperMult
92
-
93
- # check House IN associations
94
- assert houseClass.localNavigableEnds.is_a?(Array)
95
- assert_equal 3, houseClass.localNavigableEnds.size
96
- homeEnd = houseClass.localNavigableEnds.select{|e| e.role == "home"}.first
97
- assert_not_nil homeEnd
98
- assert_equal UMLAssociation, homeEnd.assoc.class
99
- assert_equal "0..*", homeEnd.multiplicity
100
- assert_equal 0, homeEnd.lowerMult
101
- assert_equal :many, homeEnd.upperMult
102
-
103
- # check House all associations
104
- assert houseClass.assocEnds.is_a?(Array)
105
- assert_equal 4, houseClass.assocEnds.size
106
- end
107
24
  end
@@ -0,0 +1,97 @@
1
+ require 'uml/uml_classmodel'
2
+
3
+ module ClassModelChecker
4
+ include UMLClassModel
5
+
6
+ def checkClassModel(envUML)
7
+
8
+ # check main package
9
+ mainPackage = envUML.elements.select {|e| e.is_a? UMLPackage and e.name == "HouseMetamodel"}.first
10
+ assert_not_nil mainPackage
11
+
12
+ # check Rooms package
13
+ assert mainPackage.subpackages.is_a?(Array)
14
+ assert_equal 1, mainPackage.subpackages.size
15
+ assert mainPackage.subpackages[0].is_a?(UMLPackage)
16
+ roomsPackage = mainPackage.subpackages[0]
17
+ assert_equal "Rooms", roomsPackage.name
18
+
19
+ # check main package classes
20
+ assert mainPackage.classes.is_a?(Array)
21
+ assert_equal 3, mainPackage.classes.size
22
+ assert mainPackage.classes.all?{|c| c.is_a?(UMLClass)}
23
+ houseClass = mainPackage.classes.select{|c| c.name == "House"}.first
24
+ personClass = mainPackage.classes.select{|c| c.name == "Person"}.first
25
+ meetingPlaceClass = mainPackage.classes.select{|c| c.name == "MeetingPlace"}.first
26
+ assert_not_nil houseClass
27
+ assert_not_nil personClass
28
+ assert_not_nil meetingPlaceClass
29
+
30
+ # check Rooms package classes
31
+ assert roomsPackage.classes.is_a?(Array)
32
+ assert_equal 3, roomsPackage.classes.size
33
+ assert roomsPackage.classes.all?{|c| c.is_a?(UMLClass)}
34
+ roomClass = roomsPackage.classes.select{|c| c.name == "Room"}.first
35
+ kitchenClass = roomsPackage.classes.select{|c| c.name == "Kitchen"}.first
36
+ bathroomClass = roomsPackage.classes.select{|c| c.name == "Bathroom"}.first
37
+ assert_not_nil roomClass
38
+ assert_not_nil kitchenClass
39
+ assert_not_nil bathroomClass
40
+
41
+ # check Room inheritance
42
+ assert roomClass.subclasses.is_a?(Array)
43
+ assert_equal 2, roomClass.subclasses.size
44
+ assert_not_nil roomClass.subclasses.select{|c| c.name == "Kitchen"}.first
45
+ assert_not_nil roomClass.subclasses.select{|c| c.name == "Bathroom"}.first
46
+ assert kitchenClass.superclasses.is_a?(Array)
47
+ assert_equal 2, kitchenClass.superclasses.size
48
+ assert_equal roomClass.object_id, kitchenClass.superclasses.select{|c| c.name == "Room"}.first.object_id
49
+ assert_equal meetingPlaceClass.object_id, kitchenClass.superclasses.select{|c| c.name == "MeetingPlace"}.first.object_id
50
+ assert bathroomClass.superclasses.is_a?(Array)
51
+ assert_equal 1, bathroomClass.superclasses.size
52
+ assert_equal roomClass.object_id, bathroomClass.superclasses[0].object_id
53
+
54
+ # check House-Room "part of" association
55
+ assert houseClass.localCompositeEnds.otherEnd.clazz.is_a?(Array)
56
+ assert_equal 1, houseClass.localCompositeEnds.size
57
+ roomEnd = houseClass.localCompositeEnds[0].otherEnd
58
+ assert_equal UMLAggregation, roomEnd.assoc.class
59
+ assert_equal roomClass.object_id, roomEnd.clazz.object_id
60
+ assert_equal "room", roomEnd.role
61
+ assert_equal "1..*", roomEnd.multiplicity
62
+ assert_equal 1, roomEnd.lowerMult
63
+ assert_equal :many, roomEnd.upperMult
64
+
65
+ assert roomClass.remoteCompositeEnds.clazz.is_a?(Array)
66
+ assert_equal 1, roomClass.remoteCompositeEnds.size
67
+ assert_equal houseClass.object_id, roomClass.remoteCompositeEnds[0].clazz.object_id
68
+ assert_equal "house", roomClass.remoteCompositeEnds[0].role
69
+
70
+ # check House OUT associations
71
+ assert houseClass.remoteNavigableEnds.is_a?(Array)
72
+ assert_equal 2, houseClass.remoteNavigableEnds.size
73
+ bathEnd = houseClass.remoteNavigableEnds.select{|e| e.role == "bathroom"}.first
74
+ kitchenEnd = houseClass.remoteNavigableEnds.select{|e| e.role == "kitchen"}.first
75
+ assert_not_nil bathEnd
76
+ assert_not_nil kitchenEnd
77
+ assert_equal UMLAssociation, bathEnd.assoc.class
78
+ assert_equal UMLAssociation, kitchenEnd.assoc.class
79
+ assert_equal "1", kitchenEnd.multiplicity
80
+ assert_equal 1, kitchenEnd.lowerMult
81
+ assert_equal 1, kitchenEnd.upperMult
82
+
83
+ # check House IN associations
84
+ assert houseClass.localNavigableEnds.is_a?(Array)
85
+ assert_equal 3, houseClass.localNavigableEnds.size
86
+ homeEnd = houseClass.localNavigableEnds.select{|e| e.role == "home"}.first
87
+ assert_not_nil homeEnd
88
+ assert_equal UMLAssociation, homeEnd.assoc.class
89
+ assert_equal "0..*", homeEnd.multiplicity
90
+ assert_equal 0, homeEnd.lowerMult
91
+ assert_equal :many, homeEnd.upperMult
92
+
93
+ # check House all associations
94
+ assert houseClass.assocEnds.is_a?(Array)
95
+ assert_equal 4, houseClass.assocEnds.size
96
+ end
97
+ end
@@ -7,12 +7,12 @@ class XmiObjectInstantiatorTest < Test::Unit::TestCase
7
7
 
8
8
  MODEL_DIR = File.join(File.dirname(__FILE__),"xmi_instantiator_test")
9
9
 
10
- include XMIObjectInstantiator
11
-
10
+ include UMLObjectModel
11
+
12
12
  def test_model
13
13
  envUML = RGen::Environment.new
14
14
  File.open(MODEL_DIR+"/testmodel.xml") { |f|
15
- instantiateUMLObjectModel(envUML, f.read)
15
+ XMIObjectInstantiator.new.instantiateUMLObjectModel(envUML, f.read)
16
16
  }
17
17
 
18
18
  # check main package
@@ -0,0 +1,7 @@
1
+ <TestModel xmlns:MNS="testmodel.org/myNamespace" >
2
+ <MNS:House>
3
+ <MNS:Room id="1" name="Kitchen" />
4
+ <MNS:Room id="2" name="TomsRoom" />
5
+ </MNS:House>
6
+ <Person name="Tom" room="2" />
7
+ </TestModel>
@@ -0,0 +1,83 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),"..","lib")
2
+
3
+ require 'test/unit'
4
+ require 'rgen/xml_instantiator'
5
+ require 'rgen/environment'
6
+ require 'rgen/model_dumper'
7
+
8
+ module EmptyMM
9
+ end
10
+
11
+ module DefaultMM
12
+ module MNS
13
+ class Room < RGen::MetamodelBuilder::MMBase; end
14
+ end
15
+ class Person < RGen::MetamodelBuilder::MMBase; end
16
+ Person.one_to_one 'personalRoom', MNS::Room, 'inhabitant'
17
+ end
18
+
19
+ class XMLInstantiatorTest < Test::Unit::TestCase
20
+
21
+ XML_DIR = File.join(File.dirname(__FILE__),"xml_instantiator_test")
22
+
23
+ include RGen::ModelDumper
24
+
25
+ class MyInstantiator < RGen::XMLInstantiator
26
+
27
+ map_tag_ns "testmodel.org/myNamespace", DefaultMM::MNS
28
+
29
+ # resolve :type do
30
+ # @env.find(:xmi_id => getType).first
31
+ # end
32
+
33
+ resolve_by_id :personalRoom, :id => :getId, :src => :room
34
+
35
+ end
36
+
37
+ def test_custom
38
+ env = RGen::Environment.new
39
+ inst = MyInstantiator.new(env, DefaultMM, true)
40
+ inst.instantiate_file(File.join(XML_DIR,"testmodel.xml"))
41
+
42
+ house = env.find(:class => DefaultMM::MNS::House).first
43
+ assert_not_nil house
44
+ assert_equal 2, house.room.size
45
+
46
+ rooms = env.find(:class => DefaultMM::MNS::Room)
47
+ assert_equal 2, rooms.size
48
+ assert_equal 0, (house.room - rooms).size
49
+ rooms.each {|r| assert r.parent == house}
50
+ tomsRoom = rooms.select{|r| r.name == "TomsRoom"}.first
51
+ assert_not_nil tomsRoom
52
+
53
+ persons = env.find(:class => DefaultMM::Person)
54
+ assert_equal 1, persons.size
55
+ tom = persons.select{|p| p.name == "Tom"}.first
56
+ assert_not_nil tom
57
+
58
+ assert tom.personalRoom == tomsRoom
59
+ end
60
+
61
+ def test_default
62
+ env = RGen::Environment.new
63
+ inst = RGen::XMLInstantiator.new(env, EmptyMM, true)
64
+ inst.instantiate_file(File.join(XML_DIR,"testmodel.xml"))
65
+
66
+ house = env.find(:class => EmptyMM::MNS_House).first
67
+ assert_not_nil house
68
+ assert_equal 2, house.mNS_Room.size
69
+
70
+ rooms = env.find(:class => EmptyMM::MNS_Room)
71
+ assert_equal 2, rooms.size
72
+ assert_equal 0, (house.mNS_Room - rooms).size
73
+ rooms.each {|r| assert r.parent == house}
74
+ tomsRoom = rooms.select{|r| r.name == "TomsRoom"}.first
75
+ assert_not_nil tomsRoom
76
+
77
+ persons = env.find(:class => EmptyMM::Person)
78
+ assert_equal 1, persons.size
79
+ tom = persons.select{|p| p.name == "Tom"}.first
80
+ assert_not_nil tom
81
+ end
82
+
83
+ end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: rgen
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.0
7
- date: 2006-09-03 00:00:00 +02:00
6
+ version: 0.3.0
7
+ date: 2006-10-08 00:00:00 +02:00
8
8
  summary: Ruby Modelling and Generator Framework
9
9
  require_paths:
10
10
  - lib
@@ -48,13 +48,15 @@ files:
48
48
  - lib/rgen/array_extensions.rb
49
49
  - lib/rgen/auto_class_creator.rb
50
50
  - lib/rgen/environment.rb
51
+ - lib/rgen/instantiator.rb
51
52
  - lib/rgen/metamodel_builder
52
53
  - lib/rgen/metamodel_builder.rb
54
+ - lib/rgen/model_dumper.rb
53
55
  - lib/rgen/name_helper.rb
54
56
  - lib/rgen/template_language
55
57
  - lib/rgen/template_language.rb
56
58
  - lib/rgen/transformer.rb
57
- - lib/rgen/xml_instantiator
59
+ - lib/rgen/xml_instantiator.rb
58
60
  - lib/rgen/metamodel_builder/builder_extensions.rb
59
61
  - lib/rgen/metamodel_builder/builder_runtime.rb
60
62
  - lib/rgen/metamodel_builder/build_helper.rb
@@ -62,9 +64,6 @@ files:
62
64
  - lib/rgen/template_language/output_handler.rb
63
65
  - lib/rgen/template_language/template_container.rb
64
66
  - lib/rgen/template_language/template_helper.rb
65
- - lib/rgen/xml_instantiator/dependency_resolver.rb
66
- - lib/rgen/xml_instantiator/xml_instantiator.rb
67
- - lib/rgen/xml_instantiator/xml_parser.rb
68
67
  - lib/uml/objectmodel_instantiator.rb
69
68
  - lib/uml/uml_classmodel.rb
70
69
  - lib/uml/uml_objectmodel.rb
@@ -81,6 +80,8 @@ files:
81
80
  - test/xmi_class_instantiator_test.rb
82
81
  - test/xmi_instantiator_test
83
82
  - test/xmi_object_instantiator_test.rb
83
+ - test/xml_instantiator_test
84
+ - test/xml_instantiator_test.rb
84
85
  - test/metamodel_generator_test/expected_result.txt
85
86
  - test/metamodel_generator_test/TestModel.rb
86
87
  - test/template_language_test/expected_result.txt
@@ -93,8 +94,10 @@ files:
93
94
  - test/template_language_test/templates/index/c
94
95
  - test/template_language_test/templates/index/chapter.tpl
95
96
  - test/template_language_test/templates/index/c/cmod.tpl
97
+ - test/xmi_instantiator_test/class_model_checker.rb
96
98
  - test/xmi_instantiator_test/testmodel.eap
97
99
  - test/xmi_instantiator_test/testmodel.xml
100
+ - test/xml_instantiator_test/testmodel.xml
98
101
  - README
99
102
  - CHANGELOG
100
103
  - MIT-LICENSE