Пример #1
0
 def prop_valid_zones(self, element, prop_key, prop_value):
     v1 = self.query_engine.variable()
     for l0 in unify(v1, element):
         for l1 in unify(prop_key, self.query_engine.atom('zone')):
             for z in self.model.get_zones():
                 for l2 in unify(prop_value, self.query_engine.atom(z)):
                     yield False
Пример #2
0
 def attr(self, element, key, value):
     elt = to_python(element)
     self._debug("DEBUG: element", repr(elt))
     for k, v in elt.items():
         for x in unify(key, self.query_engine.atom(k)):
             for y in unify(value, self.query_engine.atom(v)):
                 self._debug("DEBUG: attr: %s=%s" % (k, v))
                 yield False
Пример #3
0
def test_unify_complex_atoms_not_matching():
    yp = YP()
    v1 = yp.variable()
    a1 = yp.functor("point", [v1, 2])
    a2 = yp.functor("point", [1, 1])
    r = [v1.get_value() for x in unify(a1, a2)]
    assert r == []
Пример #4
0
def test_unify_lists_with_makelist():
    yp = YP()
    v1 = yp.variable()
    l1 = yp.listpair(yp.atom("a"), yp.listpair(yp.atom("b"), yp.ATOM_NIL))
    l2 = yp.makelist([yp.atom("a"), v1])
    r = [v1.get_value() for x in unify(l1, l2)]
    assert r == [yp.atom("b")]
Пример #5
0
def test_unify_variable_with_variable():
    yp = YP()
    v1 = yp.variable()
    v2 = yp.variable()
    r = [(v1.get_value(), v2.get_value()) for x in unify(v1, v2)]
    assert len(r) == 1
    assert r[0][0] == r[0][1]
Пример #6
0
def test_unify_variable_unbound():
    yp = YP()
    a1 = yp.atom('tom')
    v1 = yp.variable()
    r = [v1.get_value() for x in unify(v1, a1)]
    # assert r ==[False]
    assert r == [yp.atom('tom')]
Пример #7
0
 def optional_attr(self, element, key, value, default):
     # key and default need to be instantiated
     elt = to_python(element)
     self._debug("DEBUG: element", repr(elt))
     v = elt.get(to_python(key), to_python(default))
     for y in unify(value, self.query_engine.atom(v)):
         self._debug("DEBUG: optional_attr: %s=%s" % (to_python(key), v))
         yield False
Пример #8
0
def test_unify_complex_atoms():
    yp = YP()
    v1 = yp.variable()
    v2 = yp.variable()
    a1 = yp.functor("point", [v1, 2])
    a2 = yp.functor("point", [1, v2])
    r = [(v1.get_value(), v2.get_value()) for x in unify(a1, a2)]
    assert r == [(1, 2)]
Пример #9
0
def test_unify_complex_atoms_free_variable():
    yp = YP()
    v1 = yp.variable()
    v2 = yp.variable()
    a1 = yp.functor("point", [v1, 2])
    a2 = yp.functor("point", [v2, 2])
    r = [(v1.get_value(), v2.get_value()) for x in unify(a1, a2)]
    assert len(r) == 1
    assert r[0][0] == r[0][1]
Пример #10
0
 def get_xpath_value(self, query, variable):
     try:
         self._debug("DEBUG: query: %s" % (to_python(query)))
         r = self.xml_tree.xpath(to_python(query))
         for y in r:
             self._debug("DEBUG: %s = %s" % (to_python(query), repr(y)))
             for _ in unify(variable, self.query_engine.atom(y)):
                 yield False
     except lxml.etree.XPathEvalError as e:
         print("ERROR (xpath):", e, to_python(query))
Пример #11
0
def load_xml_tree(xml_tree, filename):
    # first, filename must be bound to an Atom.
    fn_a = get_value(filename)
    if not isinstance(fn_a, Atom):
        # we could raise an exception, but we will just return and not
        # yield any results
        return
    with open(to_python(fn_a), 'r') as f:
        tree = dxml.parse(f)
        a = Atom(tree)
        for r in unify(xml_tree, a):
            yield False
Пример #12
0
def get_xpath_value(xml_tree, query, variable):
    '''xpath(Tree, Query, Variable).
    runs the xpath query Query against the XML tree Tree, and
    stores its result in Variable.'''
    # check if tree and query are bound to a value
    tree_a = get_value(xml_tree)
    if not isinstance(tree_a, Atom):
        return
    query_a = get_value(query)
    if not isinstance(query, Atom):
        return
    # do the XPath query
    root = to_python(tree_a).getroot()
    q = to_python(query_a)
    for v in root.findall(q):
        for r in unify(variable, Atom(v)):
            yield False
Пример #13
0
 def lowercase(self, val1, val2):
     self._debug("DEBUG: lowercase: %s=%s" % (val1, val2))
     # val1 must be instantiated
     v = self.query_engine.atom(to_python(val1).lower())
     for x in unify(val2, v):
         yield False
Пример #14
0
def test_unify_atoms_equal():
    yp = YP()
    a1 = yp.atom('tom')
    a2 = yp.atom('tom')
    r = list(unify(a1, a2))
    assert r == [False]
Пример #15
0
def test_unify_atoms_different():
    yp = YP()
    a1 = yp.atom('tom')
    a2 = yp.atom('jerry')
    r = list(unify(a1, a2))
    assert r == []
Пример #16
0
def test_unify_variable_integer():
    yp = YP()
    v1 = yp.variable()
    r = [v1.get_value() for x in unify(v1, 5)]
    assert r == [5]
Пример #17
0
 def func(arg1):
     # check if arg1 instantiated
     if isinstance(arg1, Atom) and arg1.name() != "blue":
         side_effects.append('not blue')
     for l1 in unify(arg1, yp.atom("blue")):
         yield False
Пример #18
0
def test_unify_variable_complex_atom():
    yp = YP()
    a1 = yp.functor("point", [1, 1])
    v1 = yp.variable()
    r = [v1.get_value() for x in unify(v1, a1)]
    assert r == [a1]