@@ -67,8 +67,27 @@ def print_instances(self):
6767 "=========="
6868 print "Instance #%i" % (idx + 1 )
6969 print self ._instances [idx ]
70-
71- class Part21Loader :
70+
71+ class Part21EntityInstance :
72+ """
73+ A class to represent a Part21 instance as defined in one Part21 file
74+ A Part21EntityInstance is defined by the following arguments:
75+ entity_name: a string
76+ entity_attributes: a list of strings to represent an attribute.
77+ For instance, the following expression:
78+ #4 = PRODUCT_DEFINITION_SHAPE('$','$',#5);
79+ will result in :
80+ entity : <class 'config_control_design.product_definition_shape'>
81+ entity_instance_attributes: ['$','$','#5']
82+ """
83+ def __init__ (self ,entity_name ,attributes ):
84+ self ._entity
85+ self ._attributes_definition = attributes
86+ print self ._entity_name
87+ print self ._attributes_definition
88+
89+
90+ class Part21Parser :
7291 """
7392 Loads all instances definition of a Part21 file into memory.
7493 Two dicts are created:
@@ -79,31 +98,65 @@ class Part21Loader:
7998 def __init__ (self , filename ):
8099 self ._filename = filename
81100 # the schema
82- self ._schema = ""
101+ self ._schema_name = ""
83102 # the dict self._instances contain instance definition
84103 self ._instances_definition = {}
85104 # this dict contains lists of 0 ancestors, 1 ancestor, etc.
86105 # initializes this dict
87106 self ._number_of_ancestors = {}
88107 for i in range (2000 ):
89108 self ._number_of_ancestors [i ]= []
90- print "Parsing file %s..." % filename ,
91109 self .parse_file ()
92- print "done."
93110 # reduce number_of_ancestors dict
94111 for item in self ._number_of_ancestors .keys ():
95112 if len (self ._number_of_ancestors [item ])== 0 :
96113 del self ._number_of_ancestors [item ]
97114
98- def get_schema (self ):
99- return self ._schema
115+ def get_schema_name (self ):
116+ return self ._schema_name
117+ print schema_name
100118
101119 def get_number_of_instances (self ):
102120 return len (self ._instances_definition .keys ())
103-
121+
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+
104148 def parse_file (self ):
149+ init_time = time .time ()
150+ print "Parsing file %s..." % self ._filename ,
105151 fp = open (self ._filename )
106- for line in fp :
152+ while True :
153+ line = fp .readline ()
154+ if not line :
155+ break
156+ # there may be a multline definition. In this case, we read lines untill we found
157+ # a ;
158+ while (not line .endswith (";\r \n " )): #its a multiline
159+ line = line .replace ("\r \n " ,"" ) + fp .readline ()
107160 # parse line
108161 match_instance_definition = INSTANCE_DEFINITION_RE .search (line ) # id,name,attrs
109162 if match_instance_definition :
@@ -113,13 +166,33 @@ def parse_file(self):
113166 number_of_ancestors = entity_attrs .count ('#' )
114167 # fill number of ancestors dict
115168 self ._number_of_ancestors [number_of_ancestors ].append (instance_int_id )
116- self ._instances_definition [instance_int_id ] = [entity_name ,entity_attrs ]
169+ # parse attributes string
170+ entity_attrs_list = self .parse_attributes (entity_attrs )
171+ # then finally append this instance to the disct instance
172+ self ._instances_definition [instance_int_id ] = (entity_name ,entity_attrs_list )
117173 else : #does not match with entity instance definition, parse the header
118174 if line .startswith ('FILE_SCHEMA' ):
119175 #identify the schema name
120- self ._schema = line .split ("'" )[1 ].split ("'" )[0 ].split (" " )[0 ].lower ()
176+ self ._schema_name = line .split ("'" )[1 ].split ("'" )[0 ].split (" " )[0 ].lower ()
121177 fp .close ()
122-
178+ print 'done in %fs.' % (time .time ()- init_time )
179+ print 'schema: - %s entities %i' % (self ._schema_name ,len (self ._instances_definition .keys ()))
180+
181+ class EntityInstancesFactory (object ):
182+ '''
183+ This class creates entity instances from the str definition
184+ For instance, the definition:
185+ 20: ('CARTESIAN_POINT', ["''", '(5.,125.,20.)'])
186+ will result in:
187+ p = ARRAY(1,3,REAL)
188+ p.[1]=REAL(5)
189+ p.[2] = REAL(125)
190+ p.[3] = REAL(20)
191+ new_instance = cartesian_point(STRING(''),p)
192+ '''
193+ def __init__ (self , schema_name , instance_definition ):
194+ # First try to import the schema module
195+ pass
123196class Part21Population (object ):
124197 def __init__ (self , part21_loader ):
125198 """ Take a part21_loader a tries to create entities
@@ -185,19 +258,15 @@ def map_express_to_python(self,attr):
185258if __name__ == "__main__" :
186259 import time
187260 import sys
188- sys .path .append (".." )
189- from config_control_design import *
190- t1 = time .time ()
191- print "Loading file into memory"
192- #file = Part21Loader("as1-oc-214.stp")
261+ #sys.path.append("..")
262+ #from config_control_design import *
263+ #p21loader = Part21Loader("as1-oc-214.stp")
193264 #file = Part21Loader("as1-tu-203.stp")
194265 #file = Part21Loader("HAYON.stp")
195- p21loader = Part21Loader ("as1.stp" )
196- t2 = time .time ()
197- print "Loading schema took: %s s \n " % ((t2 - t1 ))
198- t1 = time .time ()
199- print "Creating instances"
200- p21population = Part21Population (p21loader )
201- p21population .create_entity_instances ()
202- t2 = time .time ()
203- print "Creating instances took: %s s \n " % ((t2 - t1 ))
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))
0 commit comments