def parse_component_by_typename(self, node, type): """ Parses components defined directly by component name. @param node: Node containing the <Component> element @type node: xml.etree.Element @param type: Type of this component. @type type: string @raise ParseError: Raised when the component does not have an id. """ if self.current_context.context_type == Context.GLOBAL: # Global component instatiation if 'id' in node.lattrib: id = node.lattrib['id'] else: self.raise_error('Component must have an id') type = node.tag component = Component(id, self.current_context, type, None) self.current_context.add_component(component) else: # Child instantiation if 'id' in node.lattrib: id = node.lattrib['id'] type = node.tag else: id = node.tag type = '__type_inherited__' component = Component(id, self.current_context, type) self.current_context.add_child(component) for key in node.attrib: if key.lower() not in ['extends', 'id', 'type']: param = Parameter(key, '__dimension_inherited__') param.set_value(node.attrib[key]) component.add_parameter(param) self.push_context(component.context) self.process_nested_tags(node) self.pop_context()
def parse_component_by_typename(self, node, type): """ Parses components defined directly by component name. @param node: Node containing the <Component> element @type node: xml.etree.Element @param type: Type of this component. @type type: string @raise ParseError: Raised when the component does not have an id. """ if self.current_context.context_type == Context.GLOBAL: # Global component instatiation if "id" in node.lattrib: id = node.lattrib["id"] else: self.raise_error("Component must have an id") type = node.tag component = Component(id, self.current_context, type, None) self.current_context.add_component(component) else: # Child instantiation if "id" in node.lattrib: id = node.lattrib["id"] type = node.tag else: id = node.tag type = "__type_inherited__" component = Component(id, self.current_context, type) self.current_context.add_child(component) for key in node.attrib: if key.lower() not in ["extends", "id", "type"]: param = Parameter(key, "__dimension_inherited__") param.set_value(node.attrib[key]) component.add_parameter(param) self.push_context(component.context) self.process_nested_tags(node) self.pop_context()
def parse_parameter(self, node): """ Parses <Parameter> @param node: Node containing the <Parameter> element @type node: xml.etree.Element @raise ParseError: Raised when the parameter does not have a name. @raise ParseError: Raised when the parameter does not have a dimension. """ try: name = node.lattrib['name'] except: self.raise_error('Parameter must have a name') try: dimension = node.lattrib['dimension'] except: self.raise_error('Parameter must have a dimension') parameter = Parameter(name, dimension) self.current_context.add_parameter(parameter)
def parse_component(self, node): """ Parses <Component> @param node: Node containing the <ComponentType> element @type node: xml.etree.Element """ if 'id' in node.lattrib: id = node.lattrib['id'] else: self.raise_error('Component must have an id') if 'type' in node.lattrib: type = node.lattrib['type'] else: type = None if type == None: if 'extends' in node.lattrib: extends = node.lattrib['extends'] else: self.raise_error('Component must have a type or must ' + 'extend another component') else: extends = None component = Component(id, self.current_context, type, extends) self.current_context.add_component(component) for key in node.attrib: if key.lower() not in ['extends', 'id', 'type']: param = Parameter(key, '__dimension_inherited__') param.set_value(node.attrib[key]) component.add_parameter(param) self.push_context(component.context) self.process_nested_tags(node) self.pop_context()
def resolve_extended_component(self, context, component): """ Resolves the specified component's parameters from it's base component. @param context: Context object containing the component. @type context: lems.model.context.Context @param component: Component to be resolved. @type component: lems.model.component.Component @raise ModelError: Raised when the base component cannot be resolved. @raise ModelError: Raised when a parameter in the base component is redefined in this component. @note: Consider changing Component.id to Component.name and merging this method with resolve_extended_component_type. """ base = context.lookup_component(component.extends) if base == None: self.raise_error( 'Base component {0} not found for component {1}'.format( component.extends, component.id), context) if base.extends: self.resolve_extended_component(context, base) this_context = component.context base_context = base.context for pn in base_context.parameters: if pn in this_context.parameters: pt = this_context.parameters[pn] if pt.dimension == '__dimension_inherited__': pb = base_context.parameters[pn] this_context.parameters[pn] = Parameter( pt.name, pb.dimension, pt.fixed, pt.value) else: self.raise_error(('Parameter {0} in {1} is redefined ' 'in {2}').format(pn, base_type.name, component_type.name), context) else: this_context.parameters[pn] = base_context.parameters[pn].\ copy() component.component_type = base.component_type component.extends = None
def parse_component(self, node): """ Parses <Component> @param node: Node containing the <ComponentType> element @type node: xml.etree.Element """ if "id" in node.lattrib: id = node.lattrib["id"] else: self.raise_error("Component must have an id") if "type" in node.lattrib: type = node.lattrib["type"] else: type = None if type == None: if "extends" in node.lattrib: extends = node.lattrib["extends"] else: self.raise_error("Component must have a type or must " + "extend another component") else: extends = None component = Component(id, self.current_context, type, extends) self.current_context.add_component(component) for key in node.attrib: if key.lower() not in ["extends", "id", "type"]: param = Parameter(key, "__dimension_inherited__") param.set_value(node.attrib[key]) component.add_parameter(param) self.push_context(component.context) self.process_nested_tags(node) self.pop_context()
def parse_fixed(self, node): """ Parses <Fixed> @param node: Node containing the <Fixed> element @type node: xml.etree.Element @raise ParseError: Raised when """ try: parameter = node.lattrib['parameter'] except: self.raise_error('Parameter to be fixed must be specified') try: value = node.lattrib['value'] except: self.raise_error('Value to be fixed must be specified') if self.current_context.lookup_parameter(parameter) == None: self.current_context.add_parameter( Parameter(parameter, '__dimension_inherited__')) self.current_context.lookup_parameter(parameter).fix_value(value)
def resolve_component_from_type(self, context, component): """ Resolves the specified component's parameters from component type. @param context: Context object containing the component. @type context: lems.model.context.Context @param component: Component to be resolved. @type component: lems.model.component.Component @raise ModelError: Raised when the component type cannot be resolved. """ component_type = context.lookup_component_type( component.component_type) if component_type == None: print component.id, component.component_type self.raise_error( 'Type {0} not found for component {1}'.format( component.component_type, component.id), context) this_context = component.context type_context = component_type.context for pn in type_context.parameters: pt = type_context.parameters[pn] if pn in this_context.parameters: pc = this_context.parameters[pn] if pc.value: value = pc.value else: value = pt.value if pc.dimension == '__dimension_inherited__': if pt.fixed: np = Parameter(pn, pt.dimension, pt.fixed, value) else: np = Parameter(pn, pt.dimension, pc.fixed, value) this_context.parameters[pn] = np else: this_context.parameters[pn] = pt.copy() self.resolve_parameter_value(this_context.parameters[pn], this_context) for pn in this_context.parameters: pc = this_context.parameters[pn] if pc.dimension == '__dimension_inherited__': if pn in type_context.texts: pc.dimension = '__text__' this_context.texts[pn] = type_context.texts[pn] elif pn in type_context.paths: pc.dimension = '__path__' this_context.paths[pn] = type_context.paths[pn] elif pn in type_context.links: pc.dimension = '__link__' this_context.links[pn] = type_context.links[pn] elif pn in type_context.component_refs: pc.dimension = '__component_ref__' cf = type_context.component_refs[pn] this_context.component_refs[pn] = pc.value for bpn in type_context.behavior_profiles: bp = type_context.behavior_profiles[bpn].copy() this_context.behavior_profiles[bpn] = bp if bpn == type_context.selected_behavior_profile.name: this_context.selected_behavior_profile = bp for port in type_context.event_in_ports: this_context.event_in_ports.append(port) for port in type_context.event_out_ports: this_context.event_out_ports.append(port) self.resolve_component_structure_from_type(this_context, type_context, component)