Пример #1
0
 def write_macro_for_listof(self, sbml_class):
     if sbml_class['hasListOf']:
         if 'lo_elementName' in sbml_class \
                 and sbml_class['lo_elementName'] != '':
             lo_name = strFunctions.upper_first(
                 sbml_class['lo_elementName'])
         else:
             lo_name = strFunctions.cap_list_of_name(sbml_class['name'])
         self.write_line('\\newcommand{0}\\{1}{2}{0}\\defRef{0}{1}{2}'
                         '{0}{3}{2}{2}'
                         .format(self.start_b, lo_name, self.end_b,
                                 strFunctions.make_class(lo_name)))
         # hack for render
         if sbml_class['name'] == 'GradientBase':
             self.write_line('\\newcommand{0}\\{4}{2}{0}\\defRef{0}{1}{2}'
                             '{0}{3}{2}{2}'
                             .format(self.start_b, lo_name, self.end_b,
                                     strFunctions.make_class(lo_name),
                                     'ListOfGradientBases'))
         elif sbml_class['name'] == 'RenderPoint':
             self.write_line('\\newcommand{0}\\{4}{2}{0}\\defRef{0}{1}{2}'
                             '{0}{3}{2}{2}'
                             .format(self.start_b, lo_name, self.end_b,
                                     strFunctions.make_class(lo_name),
                                     'ListOfRenderPoints'))
    def __init__(self, object_desc, spec_name, number, package, pkg_ref):
        # members from object
        self.name = object_desc['name']
        self.fullname = spec_name
        self.number = number
        self.package = package.lower()
        self.pkg_ref = pkg_ref

        # useful repeated text strings
        self.valid = '\\validRule{'
        self.start_b = '{'
        self.end_b = '}'

        self.formatted_name = '\{}'.format(object_desc['name'])
        self.indef = strFunctions.get_indefinite(object_desc['name'])
        self.indef_u = strFunctions.upper_first(self.indef)

        self.reqd_att = []
        self.opt_att = []
        self.reqd_elem = []
        self.opt_elem = []
        self.reqd_child_lo_elem = []
        self.opt_child_lo_elem = []

        self.parse_attributes(self, object_desc['attribs'])
        self.rules = []
Пример #3
0
    def __init__(self, name, extension, object_desc):
        BaseFile.BaseFile.__init__(self, name, extension)

        # change the comment delimiter and line length
        self.comment = '%'
        self.line_length = 72

        self.package = object_desc['name']
        self.fullname = object_desc['fullname']
        self.sbml_classes = object_desc['sbmlElements']
        self.offset = object_desc['offset']
        self.plugins = object_desc['plugins']
        self.enums = object_desc['enums']
        self.level = object_desc['sbml_level']
        self.version = object_desc['sbml_version']
        self.pkg_version = object_desc['pkg_version']

        self.prim_class = []

        self.start_b = '{'
        self.end_b = '}'

        # expand the information for the classes
        self.fulltexname = strFunctions.texify(self.fullname)
        self.upper_package = strFunctions.upper_first(self.package)
        self.sort_class_names(self.sbml_classes)
        self.sort_attribute_names(self.sbml_classes)
        self.sort_enum_names(self.enums)

        self.full_pkg_command = '\\{}Package'.format(self.fulltexname)
        self.brief_pkg_command = '\\{}'.format(self.upper_package)
Пример #4
0
def get_class(name, root_object):
    if name.startswith('listOf') or name.startswith('ListOf'):
        name = strFunctions.singular(name[6:])
    else:
        name = strFunctions.upper_first(name)
    if root_object is None:
        return None
    elif root_object['baseElements'] is None:
        return None
    else:
        for i in range(0, len(root_object['baseElements'])):
            if name == root_object['baseElements'][i]['name']:
                return root_object['baseElements'][i]
Пример #5
0
    def write_body_for_lo_class(self, sbml_class, nested=False):
        lo_name = 'default'
        if sbml_class['hasListOf']:
            if 'lo_elementName' in sbml_class \
                    and sbml_class['lo_elementName'] != '':
                lo_name = strFunctions.upper_first(
                    sbml_class['lo_elementName'])
            else:
                lo_name = strFunctions.cap_list_of_name(sbml_class['name'])

        # do not write if already written
        if lo_name in self.classes_written:
            return
        self.classes_written.append(lo_name)

        if 'texname' in sbml_class:
            classname = sbml_class['texname']
        else:
            classname = sbml_class['name']

        self.write_comment_line('---------------------------------------------'
                                '------------')
        self.write_line('\subsection{0}The \class{0}{1}{2} class{2}'
                        .format(self.start_b, lo_name, self.end_b))
        self.write_line('\label{}{}-class{}'
                        .format(self.start_b, lo_name.lower(), self.end_b))
        self.skip_line()
        self.write_to_do('explain {}'.format(lo_name))

        self.write_line('The \\{0} object derives from the \\class{1}ListOf{2}'
                        ' and inherits the core attributes and subobjects '
                        'from the \\class{1}SBase{2} class.  It contains '
                        'one or more objects of type \\{3}.'
                        .format(lo_name, self.start_b, self.end_b, classname))
        self.skip_line()

        written = False
        for i in range(0, len(sbml_class['lo_attribs'])):
            if not written:
                self.write_line('In addition the  \\{} object has the '
                                'following attributes.'
                                .format(lo_name))
                self.skip_line()
                written = True
            att = sbml_class['lo_attribs'][i]
            self.write_attibute_paragraph(att, lo_name)

        if not nested:
            self.write_body_for_class(self.get_class(sbml_class['name']))
Пример #6
0
def get_static_extension_attribs(num_versions):
    attribs = []
    att = dict({'name': 'packageName',
                'capAttName': 'PackageName',
                'attTypeCode': 'std::string&',
                'attType': 'string',
                'memberName': ''})
    attribs.append(att)
    att = dict({'name': 'defaultLevel',
                'capAttName': 'DefaultLevel',
                'attTypeCode': 'unsigned int',
                'attType': 'unsigned integer',
                'memberName': 3})
    attribs.append(att)
    att = dict({'name': 'defaultVersion',
                'capAttName': 'DefaultVersion',
                'attTypeCode': 'unsigned int',
                'attType': 'unsigned integer',
                'memberName': 1})
    attribs.append(att)
    att = dict({'name': 'defaultPackageVersion',
                'capAttName': 'DefaultPackageVersion',
                'attTypeCode': 'unsigned int',
                'attType': 'unsigned integer',
                'memberName': 1})
    attribs.append(att)
    for i in range(0, num_versions):
        name = 'xmlnsL3V1V{0}'.format(i+1)
        cap_name = strFunctions.upper_first(name)
        att = dict({'name': name,
                    'capAttName': cap_name,
                    'attTypeCode': 'std::string&',
                    'attType': 'string',
                    'memberName': 'fix this'})
        attribs.append(att)

    return attribs
    def write_attribute_type_rule(self, attribute):
        att_type = attribute['type']
        name = strFunctions.wrap_token(attribute['texname'], self.package)
        if att_type == 'SId':
            return
        elif att_type == 'SIdRef':
            ref_name = strFunctions.upper_first(attribute['name'])
            # hack for render
            if ref_name == 'StartHead' or ref_name == 'EndHead':
                ref_name = 'LineEnding'
            text = 'The value of the attribute {} of {} {} object must be ' \
                   'the identifier of an existing \{} object defined in the ' \
                   'enclosing \Model object.'\
                .format(name, self.indef, self.formatted_name, ref_name)
        elif att_type == 'string':
            text = 'The attribute {} on {} {} must have a value of data ' \
                   'type {}.'\
                .format(name, self.indef, self.formatted_name,
                        strFunctions.wrap_token('string'))
        elif att_type == 'int' or att_type == 'uint':
            text = 'The attribute {} on {} {} must have a value of data ' \
                   'type {}.'\
                .format(name, self.indef, self.formatted_name,
                        strFunctions.wrap_token('integer'))
        elif att_type == 'double':
            text = 'The attribute {} on {} {} must have a value of data ' \
                   'type {}.'\
                .format(name, self.indef, self.formatted_name,
                        strFunctions.wrap_token('double'))
        elif att_type == 'boolean' or att_type == 'bool':
            text = 'The attribute {} on {} {} must have a value of data ' \
                   'type {}.'\
                .format(name, self.indef, self.formatted_name,
                        strFunctions.wrap_token('boolean'))
        elif att_type == 'enum':
            enum_name = strFunctions.texify(attribute['element'])
            enums = attribute['parent']['root']['enums']
            enum_values = self.parse_enum_values(attribute['element'], enums)
            text = 'The value of the attribute {0} of {1} {2} object must ' \
                   'conform to the syntax of SBML data type {3} and ' \
                   'may only take on the allowed values of {3} defined ' \
                   'in SBML; that is the value must be one of the ' \
                   'following {4}.'.format(name, self.indef,
                                           self.formatted_name,
                                           strFunctions.wrap_enum(enum_name),
                                           enum_values)
        elif att_type == 'array':
            text = 'The value of the attribute {} of {} {} object must ' \
                   'be an array of values of type {}.'\
                .format(name, self.indef, self.formatted_name,
                        strFunctions.wrap_token(attribute['element']))
        elif att_type == 'element' and attribute['element'] == 'RelAbsVector':
            text = 'The value of the attribute {0} of {1} {2} object must ' \
                   'conform to the syntax of SBML data type \\RelAbsVector ' \
                   'i.e. a string encoding optionally an absolute number ' \
                   'followed by an optional relative number followed ' \
                   'by a \\% sign. Adding spaces between the ' \
                   'coordinates is encouraged, but not required.'\
                .format(name, self.indef, self.formatted_name)
        else:
            text = 'FIX ME: Encountered an unknown attribute type {} in ' \
                   'ValidationRulesForClass:write_attribute_type_rule'\
                .format(att_type)

        ref = '{}, {}.'\
            .format(self.pkg_ref, strFunctions.wrap_section(self.name))
        sev = 'ERROR'
        return dict({'number': self.number, 'text': text,
                     'reference': ref, 'severity': sev})
Пример #8
0
 def expand_attributes(attributes):
     for i in range(0, len(attributes)):
         capname = strFunctions.upper_first(attributes[i]['name'])
         attributes[i]['capAttName'] = capname
         attributes[i]['memberName'] = 'm' + capname
         attributes[i]['pluralName'] = \
             strFunctions.plural(attributes[i]['name'])
         attributes[i]['isEnum'] = False
         att_type = attributes[i]['type']
         if att_type == 'SId' or att_type == 'SIdRef':
             attributes[i]['attType'] = 'string'
             attributes[i]['attTypeCode'] = 'std::string&'
             attributes[i]['CType'] = 'const char *'
             attributes[i]['isNumber'] = False
         elif att_type == 'UnitSId' or att_type == 'UnitSIdRef':
             attributes[i]['attType'] = 'string'
             attributes[i]['attTypeCode'] = 'std::string&'
             attributes[i]['CType'] = 'const char *'
             attributes[i]['isNumber'] = False
         elif att_type == 'string':
             attributes[i]['attType'] = 'string'
             attributes[i]['attTypeCode'] = 'std::string&'
             attributes[i]['CType'] = 'const char *'
             attributes[i]['isNumber'] = False
         elif att_type == 'double':
             attributes[i]['attType'] = 'double'
             attributes[i]['attTypeCode'] = 'double'
             attributes[i]['CType'] = 'double'
             attributes[i]['isNumber'] = True
         elif att_type == 'int':
             attributes[i]['attType'] = 'integer'
             attributes[i]['attTypeCode'] = 'int'
             attributes[i]['CType'] = 'int'
             attributes[i]['isNumber'] = True
         elif att_type == 'uint':
             attributes[i]['attType'] = 'unsigned integer'
             attributes[i]['attTypeCode'] = 'unsigned int'
             attributes[i]['CType'] = 'unsigned int'
             attributes[i]['isNumber'] = True
         elif att_type == 'bool' or att_type == 'boolean':
             attributes[i]['attType'] = 'boolean'
             attributes[i]['attTypeCode'] = 'bool'
             attributes[i]['CType'] = 'int'
             attributes[i]['isNumber'] = False
         elif att_type == 'enum':
             attributes[i]['isEnum'] = True
             attributes[i]['attType'] = 'enum'
             attributes[i]['attTypeCode'] = attributes[i]['element'] + '_t'
             attributes[i]['CType'] = attributes[i]['element'] + '_t'
             attributes[i]['isNumber'] = False
         elif att_type == 'element':
             attributes[i]['attType'] = 'element'
             if attributes[i]['name'] == 'math':
                 attributes[i]['attTypeCode'] = 'ASTNode*'
                 attributes[i]['CType'] = 'ASTNode_t*'
             else:
                 attributes[i]['attTypeCode'] = attributes[i]['element']+'*'
                 attributes[i]['CType'] = attributes[i]['element']+'_t*'
             attributes[i]['isNumber'] = False
         elif att_type == 'lo_element':
             name = strFunctions.list_of_name(attributes[i]['element'])
             attributes[i]['attType'] = 'lo_element'
             attributes[i]['attTypeCode'] = name
             attributes[i]['CType'] = name + '_t'
             attributes[i]['memberName'] = 'm' + name
             attributes[i]['isNumber'] = False
         else:
             attributes[i]['attType'] = 'FIX ME'
             attributes[i]['attTypeCode'] = 'FIX ME'
             attributes[i]['CType'] = 'FIX ME'
             attributes[i]['isNumber'] = False
     return attributes
def parse_deviser_xml(filename):
    """
    Parses the given filename and returns a dictionary with
    the definition contained in it
    """

    sbml_elements = []
    elements = []
    plugins = []
    enums = []

    dom = parse(filename)

    temp = get_value(dom.documentElement, 'name')
    # we expect this to be lower case
    package_name = temp.lower()
    number = to_int(get_value(dom.documentElement, 'number'))
    offset = to_int(get_value(dom.documentElement, 'offset'))
    fullname = get_value(dom.documentElement, 'fullname')
    required = to_bool(get_value(dom.documentElement, 'required'))

    # get package information (assume we want the first only)
    sbml_level = 3
    sbml_version = 1
    pkg_version = 1
    for node in dom.getElementsByTagName('pkgVersion'):
        sbml_level = to_int(get_value(node, 'level'))
        sbml_version = to_int(get_value(node, 'version'))
        pkg_version = to_int(get_value(node, 'pkg_version'))
        break

    concrete_dict = dict({})

    # read concrete versions of abstract classes and fill dictionary
    for node in dom.getElementsByTagName('element'):
        element_name = get_value(node, 'name')
        concrete_list = []
        for concrete in node.getElementsByTagName('concrete'):
            concrete_list.append(
                dict(
                    {'name': get_value(concrete, "name"),
                     'element': get_value(concrete, "element")}
                ))
        concrete_dict[element_name] = concrete_list

    # read element
    for node in dom.getElementsByTagName('element'):

        element_name = get_value(node, 'name')
        base_class = get_value(node, 'baseClass')
        type_code = get_value(node, 'typeCode')
        has_math = to_bool(get_value(node, 'hasMath'))
        has_children = to_bool(get_value(node, 'hasChildren'))
        has_list_of = to_bool(get_value(node, 'hasListOf'))
        abstract = to_bool(get_value(node, 'abstract'))
        children_overwrite_element_name = to_bool(
            get_value(node, 'childrenOverwriteElementName'))

        temp = get_value(node, 'elementName')
        # we expect this to be camel case starting with lower
        if temp is not None:
            xml_element_name = strFunctions.lower_first(temp)
        else:
            xml_element_name = ''

        temp = get_value(node, 'listOfName')
        # we expect this to be camel case starting with lower
        if temp is not None:
            xml_lo_element_name = strFunctions.lower_first(temp)
        else:
            xml_lo_element_name = ''

        temp = get_value(node, 'listOfClassName')
        # we expect this to be camel case starting with upper
        if temp is not None:
            lo_class_name = strFunctions.upper_first(temp)
        else:
            lo_class_name = ''

        add_decls = get_value(node, 'additionalDecls')
        add_defs = get_value(node, 'additionalDefs')

        attributes = []

        # add attributes
        for attr in node.getElementsByTagName('attribute'):

            attr_name = get_value(attr, 'name')
            required = to_bool(get_value(attr, 'required'))
            attr_type = standardize_types(get_value(attr, 'type'))
            attr_abstract = to_bool(get_value(attr, 'abstract'))
            attr_element = get_value(attr, 'element')

            attribute_dict = dict({'type': attr_type,
                                   'reqd': required,
                                   'name': attr_name,
                                   'element': attr_element,
                                   'abstract': attr_abstract,
                                   })
            if attr_abstract:
                attribute_dict['concrete'] = concrete_dict[attr_element]

            attributes.append(attribute_dict)

        lo_attributes = []

        # add attributes
        for attr in node.getElementsByTagName('listOfAttribute'):

            attr_name = get_value(attr, 'name')
            required = to_bool(get_value(attr, 'required'))
            attr_type = get_value(attr, 'type')
            attr_abstract = to_bool(get_value(attr, 'abstract'))
            attr_element = get_value(attr, 'element')

            attribute_dict = dict({'type': attr_type,
                                   'reqd': required,
                                   'name': attr_name,
                                   'element': attr_element,
                                   'abstract': attr_abstract})
            if attr_abstract:
                attribute_dict['concrete'] = concrete_dict[attr_element]

            lo_attributes.append(attribute_dict)

        # construct element
        element = dict({'name': element_name,
                        'package': package_name,
                        'typecode': type_code,
                        'hasListOf': has_list_of,
                        'attribs': attributes,
                        'lo_attribs': lo_attributes,
                        'hasChildren': has_children,
                        'hasMath': has_math,
                        'childrenOverwriteElementName':
                            children_overwrite_element_name,
                        'baseClass': base_class,
                        'abstract': abstract
                        })
        if xml_element_name is not None:
            element['elementName'] = xml_element_name

        if xml_lo_element_name is not None:
            element['lo_elementName'] = xml_lo_element_name

        if lo_class_name is not None:
            element['lo_class_name'] = lo_class_name

        if add_decls is not None:
            if os.path.exists(os.path.dirname(filename) + '/' + add_decls):
                add_decls += os.path.dirname(filename) + '/'
            element['addDecls'] = add_decls

        if add_defs is not None:
            if os.path.exists(os.path.dirname(filename) + '/' + add_defs):
                add_defs += os.path.dirname(filename) + '/'
            element['addDefs'] = add_defs

        if abstract:
            element['concrete'] = concrete_dict[element_name]

        elements.append(dict({'name': element_name,
                              'typecode': type_code,
                              'isListOf': has_list_of,
                              'listOfName': xml_lo_element_name
                              if xml_lo_element_name is not None else '',
                              'listOfClassName': lo_class_name
                              if lo_class_name is not None else ''}))
        sbml_elements.append(element)

    for node in dom.getElementsByTagName('plugin'):

        plug_elements = []
        plug_lo_elements = []
        ext_point = get_value(node, 'extensionPoint')
        add_decls = get_value(node, 'additionalDecls')
        add_defs = get_value(node, 'additionalDefs')

        # read references to elements
        for reference in node.getElementsByTagName('reference'):
            temp = find_element(elements, get_value(reference, 'name'))
            if temp is not None:
                plug_elements.append(temp)

        # look for references to ListOf elements
        for reference in node.getElementsByTagName('reference'):
            temp = find_lo_element(elements, get_value(reference, 'name'))
            if temp is not None:
                plug_lo_elements.append(temp)

        attributes = []

        # read additional attributes
        for attr in node.getElementsByTagName('attribute'):

            attr_name = get_value(attr, 'name')
            required = to_bool(get_value(attr, 'required'))
            attr_type = get_value(attr, 'type')

            attr_abstract = to_bool(get_value(attr, 'abstract'))
            attr_element = get_value(attr, 'element')

            attribute_dict = dict({'type': attr_type,
                                   'reqd': required,
                                   'name': attr_name,
                                   'element': attr_element,
                                   'abstract': attr_abstract
                                   })
            if attr_abstract:
                attribute_dict['concrete'] = concrete_dict[attr_element]

            attributes.append(attribute_dict)

        plugin_dict = dict({'sbase': ext_point,
                            'extension': plug_elements,
                            'attribs': attributes,
                            'lo_extension': plug_lo_elements})

        if add_decls is not None:
            if os.path.exists(os.path.dirname(filename) + '/' + add_decls):
                add_decls += os.path.dirname(filename) + '/'
            plugin_dict['addDecls'] = add_decls

        if add_defs is not None:
            if os.path.exists(os.path.dirname(filename) + '/' + add_defs):
                add_defs += os.path.dirname(filename) + '/'
            plugin_dict['addDefs'] = add_defs

        plugins.append(plugin_dict)

    for node in dom.getElementsByTagName('enum'):
        values = []
        enum_name = get_value(node, 'name')

        for val in node.getElementsByTagName('enumValue'):
            values.append(dict({'name': get_value(val, 'name'),
                                'value': get_value(val, 'value')}))

        enums.append(dict({'name': enum_name, 'values': values}))

    package = dict({'name': package_name,
                    'elements': elements,
                    'plugins': plugins,
                    'number': number,
                    'sbmlElements': sbml_elements,
                    'enums': enums,
                    'offset': offset,
                    'fullname': fullname,
                    'sbml_level': sbml_level,
                    'sbml_version': sbml_version,
                    'pkg_version': pkg_version,
                    'required': required
                    })

    # link elements
    for elem in package['elements']:
        elem['root'] = package
        if 'attribs' in elem:
            for attr in elem['attribs']:
                attr['parent'] = elem
                attr['root'] = package

    for elem in package['sbmlElements']:
        elem['root'] = package
        if 'attribs' in elem:
            for attr in elem['attribs']:
                attr['parent'] = elem
                attr['root'] = package
        if 'concrete' in elem:
            for attr in elem['concrete']:
                attr['parent'] = elem
                attr['root'] = package

    return package