예제 #1
0
	def getRules(self, without=None, only=None):
		if not only: only = {}
		if not without: without = {}
		if not self.rules:
			tree = XSDTree.makeOne("verification.xsd")

			if not tree:
				return self.rules

			root = tree.getroot()
			prefix = root.prefix

			verificationTypes = tree.xpath('//' + prefix + ':complexType[contains(@name, "Verification")]', namespaces = {prefix: XSD_NS})
			for verificationType in verificationTypes:
				name = verificationType.get('name')
				name = name[0].upper() + name[1:-len('Verification')]
				self._setDomains(prefix, verificationType, name)

			ruleNodes = tree.xpath('//' + prefix + ':element[contains(@name, "Rule")]', namespaces = {prefix: XSD_NS})
			for ruleNode in ruleNodes:
				mathNodes = ruleNode.xpath('.//math:math', namespaces={"math":MATHML_NS})
				if mathNodes:
					mathDoc = MathDOM()
					mathDoc._etree = etree.XML(etree.tostring(mathNodes[0]), parser=mathDoc._parser)
					if mathDoc is None:
						raise NoMathMLException("The 'math' construction has errors")

					self.rules[ruleNode.get('name')] = {'domains': self.domains[ruleNode.get('name')] if self.domains.has_key(ruleNode.get('name')) else [],
					                                             'forInChIList': mathNodes[0].getparent().get('forInChIList'),
					                                             'math': mathDoc}

		rules = {}
		for rule in self.rules:
			rules[rule] = self.rules[rule].copy()
		for fakeRule in without:
			if fakeRule in rules:
				del rules[fakeRule]

		for fakeRule in only:
			if not (fakeRule in rules) and "copy" in only[fakeRule] and only[fakeRule]["copy"] in rules:
				rules[fakeRule] = rules[only[fakeRule]["copy"]]
				rules[fakeRule].update(only[fakeRule])

		if only:
			for rule in self.rules:
				if not rule in only:
					del rules[rule]

		return rules
예제 #2
0
파일: check.py 프로젝트: aip/NodeSoftware
	def getRules(self):
		if not self.rules:
			tree = XSDTree.makeOne(VERIFICATION_FILE_ABS_PATH)

			if not tree:
				return self.rules

			root = tree.getroot()
			prefix = root.prefix

			verificationTypes = tree.xpath('//' + prefix + ':complexType[contains(@name, "Verification")]', namespaces = {prefix: XSD_NS})
			for verificationType in verificationTypes:
				name = verificationType.get('name')
				name = name[0].upper() + name[1:-len('Verification')]
				self._setDomains(prefix, verificationType, name)

			ruleNodes = tree.xpath('//' + prefix + ':element[contains(@name, "Rule")]', namespaces = {prefix: XSD_NS})
			for ruleNode in ruleNodes:
				mathNodes = ruleNode.xpath('.//math:math', namespaces = {"math": MATHML_NS})
				if mathNodes:
					mathDoc = MathDOM()
					mathDoc._etree = etree.XML(etree.tostring(mathNodes[0]), parser = mathDoc._parser)
					if mathDoc._etree is None:
						raise NoMathMLException("The 'math' construction has errors")

					self.rules.add(Rule(ruleNode.get('name'), mathDoc, mathNodes[0].getparent().get('forInChIList'), self._domains[ruleNode.get('name')] if self._domains.has_key(ruleNode.get('name')) else []))

		rules = set()
		for rule in self.rules:
			rules.add(rule.copy())

		for rule in self.addRules:
			rules.add(rule)

		for rule in self.delRules:
			if rule in rules:
				rules.remove(rule)

		for rule in self.useRules:
			rules.add(rule)

		if self.useRules:
			for rule in self.rules:
				if not rule in self.useRules:
					if rule in rules:
						rules.remove(rule)

		return rules
예제 #3
0
def handle_term(term):
    print("ORIGINAL:")
    print(term)
    print()

    doc = None
    for term_type in term_parsers.known_types():
        print("Trying to parse '%s' ..." % term_type, end=' ')
        try:
            doc = MathDOM.fromString(term, term_type)
            print("done.")
            break
        except ParseException as e:
            print("Parsing as %s failed: %s" % (term_type, str(e).encode('UTF-8')))

    if doc is None:
        print("The term is not parsable.")
        sys.exit(0)


    print("MathML parsing done.")
    print()
    write_infix(doc)

    print()
    print("Exchanging '+' and '-' ...")
    print()
    for apply_tag in doc.xpath('//math:apply[math:plus or math:minus and count(math:*) > 2]'):
        operator = apply_tag.operatorname()
        if operator == 'plus':
            apply_tag.set_operator('minus')
        elif operator == 'minus' and apply_tag.operand_count() > 1:
            apply_tag.set_operator('plus')
    write_infix(doc)

    print()
    print("Searching for negative numbers using XPath expression '//math:cn[number() < 0]' ...")
    print()
    for cn_tag in doc.xpath('//math:cn[number() < 0]'):
        value = cn_tag.value()
        print("%s [%s]" % (value, type(value)))

    print()
    print("Serializing all sub-terms using XPath '//math:apply' ...")
    print()

    for apply_tag in doc.xpath('//math:apply'):
        print(apply_tag.serialize(converter=infix_converter))

    print()
    print('Done.')
예제 #4
0
def handle_term(term):
    print "ORIGINAL:"
    print term
    print

    doc = None
    for term_type in term_parsers.known_types():
        print "Trying to parse '%s' ..." % term_type,
        try:
            doc = MathDOM.fromString(term, term_type)
            print "done."
            break
        except ParseException, e:
            print "Parsing as %s failed: %s" % (term_type, unicode(e).encode('UTF-8'))
예제 #5
0
    def getRules(self):
        if not self.rules:
            tree = XSDTree.makeOne(VERIFICATION_FILE_PATH)

            if not tree:
                return self.rules

            root = tree.getroot()
            prefix = root.prefix

            verificationTypes = tree.xpath(
                '//' + prefix +
                ':complexType[contains(@name, "Verification")]',
                namespaces={prefix: XSD_NS})
            for verificationType in verificationTypes:
                name = verificationType.get('name')
                name = name[0].upper() + name[1:-len('Verification')]
                self._setDomains(prefix, verificationType, name)

            ruleNodes = tree.xpath('//' + prefix +
                                   ':element[contains(@name, "Rule")]',
                                   namespaces={prefix: XSD_NS})
            for ruleNode in ruleNodes:
                mathNodes = ruleNode.xpath('.//math:math',
                                           namespaces={"math": MATHML_NS})
                if mathNodes:
                    mathDoc = MathDOM()
                    mathDoc._etree = etree.XML(etree.tostring(mathNodes[0]),
                                               parser=mathDoc._parser)
                    if mathDoc._etree is None:
                        raise NoMathMLException(
                            "The 'math' construction has errors")

                    self.rules.add(
                        Rule(
                            ruleNode.get('name'), mathDoc,
                            mathNodes[0].getparent().get('forInChIList'),
                            self._domains[ruleNode.get('name')]
                            if self._domains.has_key(
                                ruleNode.get('name')) else []))

        rules = set()
        for rule in self.rules:
            rules.add(rule.copy())

        for rule in self.addRules:
            rules.add(rule)

        for rule in self.delRules:
            if rule in rules:
                rules.remove(rule)

        for rule in self.useRules:
            rules.add(rule)

        if self.useRules:
            for rule in self.rules:
                if not rule in self.useRules:
                    if rule in rules:
                        rules.remove(rule)

        return rules
예제 #6
0
           <mo>]</mo>
          </mrow>
          <mo>-</mo>
          <mrow>
           <mi>&#952;2</mi>
           <mo>[</mo>
           <mi>t</mi>
           <mo>]</mo>
          </mrow>
         </mrow>
         <mo>]</mo>
        </mrow>
        <mn>2</mn>
       </msup>
       <mo>&#8290;</mo>
       <msub>
        <mi>m</mi>
        <mn>3</mn>
       </msub>
      </mrow>
     </mrow>
     <mo>)</mo>
    </mrow>
   </mrow>
   <mo>)</mo>
  </mrow>
 </mrow>
</math>"""

doc = MathDOM.fromString(MathMLeqn)