3030# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131
3232import re
33+ import Utils
3334
3435INSTANCE_DEFINITION_RE = re .compile ("#(\d+)[^\S\n ]?=[^\S\n ]?(.*?)\((.*)\)[^\S\n ]?;[\\ r]?$" )
3536
@@ -119,32 +120,6 @@ def get_schema_name(self):
119120 def get_number_of_instances (self ):
120121 return len (self ._instances_definition .keys ())
121122
122- def parse_attributes (self , attr_str ):
123- """
124- This method takes a string an returns a list of attributes (but without any mapping).
125- For instance:
126- input = "'',(#11,#15,#19,#23,#27),#31"
127- output = ['',(#11,#15,#19,#23,#27),'#31']
128- """
129- aggr_scope_level = 0
130- attrs = []
131- current_attr = ''
132- previous_ch = ''
133- for ch in attr_str :
134- if ch == ',' and aggr_scope_level == 0 :
135- attrs .append (current_attr )
136- current_attr = ''
137- else :
138- if ch == '(' :
139- aggr_scope_level += 1
140- elif ch == ')' :
141- aggr_scope_level -= 1
142- current_attr += ch
143- previous_ch = ch
144- # finally add the last attr when exiting loop
145- attrs .append (current_attr )
146- return attrs
147-
148123 def parse_file (self ):
149124 init_time = time .time ()
150125 print "Parsing file %s..." % self ._filename ,
@@ -155,8 +130,8 @@ def parse_file(self):
155130 break
156131 # there may be a multline definition. In this case, we read lines untill we found
157132 # a ;
158- while (not line .endswith (";\r \n " )): #its a multiline
159- line = line .replace ("\r \n " ,"" ) + fp .readline ()
133+ # while (not line.endswith(";\r\n")): #its a multiline
134+ # line = line.replace("\r\n","") + fp.readline()
160135 # parse line
161136 match_instance_definition = INSTANCE_DEFINITION_RE .search (line ) # id,name,attrs
162137 if match_instance_definition :
@@ -167,7 +142,7 @@ def parse_file(self):
167142 # fill number of ancestors dict
168143 self ._number_of_ancestors [number_of_ancestors ].append (instance_int_id )
169144 # parse attributes string
170- entity_attrs_list = self . parse_attributes (entity_attrs )
145+ entity_attrs_list , str_len = Utils . process_nested_parent_str (entity_attrs )
171146 # then finally append this instance to the disct instance
172147 self ._instances_definition [instance_int_id ] = (entity_name ,entity_attrs_list )
173148 else : #does not match with entity instance definition, parse the header
@@ -185,21 +160,23 @@ class EntityInstancesFactory(object):
185160 20: ('CARTESIAN_POINT', ["''", '(5.,125.,20.)'])
186161 will result in:
187162 p = ARRAY(1,3,REAL)
188- p.[1]= REAL(5)
163+ p.[1] = REAL(5)
189164 p.[2] = REAL(125)
190165 p.[3] = REAL(20)
191166 new_instance = cartesian_point(STRING(''),p)
192167 '''
193168 def __init__ (self , schema_name , instance_definition ):
194169 # First try to import the schema module
195170 pass
171+
196172class Part21Population (object ):
197173 def __init__ (self , part21_loader ):
198174 """ Take a part21_loader a tries to create entities
199175 """
200176 self ._part21_loader = part21_loader
201177 self ._aggregate_scope = []
202178 self ._aggr_scope = False
179+ self .create_entity_instances ()
203180
204181 def create_entity_instances (self ):
205182 """ Starts entity instances creation
@@ -210,63 +187,21 @@ def create_entity_instances(self):
210187
211188 def create_entity_instance (self , instance_id ):
212189 instance_definition = self ._part21_loader ._instances_definition [instance_id ]
190+ print "Instance definition to process" ,instance_definition
213191 # first find class name
214192 class_name = instance_definition [0 ].lower ()
193+ print "Class name:%s" % class_name
215194 object_ = globals ()[class_name ]
216195 # then attributes
217196 #print object_.__doc__
218197 instance_attributes = instance_definition [1 ]
219- #print instance_attributes
220- # find attributes
221- attributes = instance_attributes .split ("," )
222- instance_attributes = []
223- #print attributes
224- for attr in attributes :
225- if attr [0 ]== "(" :#new aggregate_scope
226- #print "new aggregate scope"
227- self ._aggr_scope = True
228- at = self .map_express_to_python (attr [1 :])
229- #self._aggregate_scope.append(at)
230- elif attr [- 1 ]== ")" :
231- #print "end aggregate scope"
232- at = self .map_express_to_python (attr [:- 1 ])
233- self ._aggregate_scope .append (at )
234- self ._aggr_scope = False
235- else :
236- at = self .map_express_to_python (attr )
237- if self ._aggr_scope :
238- self ._aggregate_scope .append (at )
239- if len (self ._aggregate_scope )> 0 and not self ._aggr_scope :
240- instance_attributes .append (self ._aggregate_scope )
241- self ._aggregate_scope = []
242- elif len (self ._aggregate_scope )> 0 and self ._aggr_scope :
243- pass
244- else :
245- instance_attributes .append (at )
198+ print "instance_attributes:" ,instance_attributes
246199 a = object_ (* instance_attributes )
247200
248-
249- def map_express_to_python (self ,attr ):
250- """ Map EXPRESS to python"""
251- if attr in ["$" ,"''" ]: #optional argument
252- return None
253- elif attr .startswith ('#' ): #entity_id
254- return attr [1 :]
255- else :
256- return map_string_to_num (attr )
257-
258201if __name__ == "__main__" :
259202 import time
260203 import sys
261- #sys.path.append("..")
262- #from config_control_design import *
263- #p21loader = Part21Loader("as1-oc-214.stp")
264- #file = Part21Loader("as1-tu-203.stp")
265- #file = Part21Loader("HAYON.stp")
266- p21loader = Part21Parser ("as1.stp" )
267- print p21loader ._instances_definition
268- #print "Creating instances"
269- #p21population = Part21Population(p21loader)
270- #p21population.create_entity_instances()
271- #t2 = time.time()
272- #print "Creating instances took: %s s \n" % ((t2-t1))
204+ from config_control_design import *
205+ p21loader = Part21Parser ("gasket1.p21" )
206+ print "Creating instances"
207+ p21population = Part21Population (p21loader )
0 commit comments