Ejemplo n.º 1
0
def test_attvars_get():
    a = AttVar()
    t1 = a.get_attribute("x")
    assert t1[0] is None
    assert t1[1] == -1

    val1 = Callable.build("1")
    a.add_attribute("x", val1)
    t2 = a.get_attribute("x")
    assert t2[0] is val1
    assert t2[1] == 0
Ejemplo n.º 2
0
def test_several_attvars_same_map():
    a = AttVar()
    b = AttVar()
    assert a.attmap is b.attmap

    val1 = Callable.build("1")
    val2 = Callable.build("2")
    a.add_attribute("x", val1)
    assert a.attmap is not b.attmap
    b.add_attribute("x", val2)
    assert a.attmap is b.attmap

    a.add_attribute("y", val1)
    a.add_attribute("z", val2)
    assert a.attmap is not b.attmap
Ejemplo n.º 3
0
def test_several_attvars_same_map():
    a = AttVar()
    b = AttVar()
    assert a.attmap is b.attmap

    val1 = Callable.build("1")
    val2 = Callable.build("2")
    a.add_attribute("x", val1)
    assert a.attmap is not b.attmap
    b.add_attribute("x", val2)
    assert a.attmap is b.attmap

    a.add_attribute("y", val1)
    a.add_attribute("z", val2)
    assert a.attmap is not b.attmap
Ejemplo n.º 4
0
def test_attvars_get():
    a = AttVar()
    t1 = a.get_attribute("x")
    assert t1[0] is None
    assert t1[1] == -1

    val1 = Callable.build("1")
    a.add_attribute("x", val1)
    t2 = a.get_attribute("x")
    assert t2[0] is val1
    assert t2[1] == 0
Ejemplo n.º 5
0
 def new_attvar(self):
     result = AttVar()
     result.created_after_choice_point = self
     return result
Ejemplo n.º 6
0
def test_simple_hooks():
    hp = Heap()
    v = BindingVar()
    a = AttVar()
    v.unify(a, hp)
    assert hp.hook is None
    v.unify(Number(1), hp)
    assert hp.hook.attvar == a

    hp = Heap()
    v1 = BindingVar()
    v2 = BindingVar()
    a1 = AttVar()
    a2 = AttVar()
    v1.unify(a1, hp)
    assert hp.hook is None
    v2.unify(a2, hp)
    assert hp.hook is None
    v1.unify(v2, hp)
    assert hp.hook.attvar == a1

    hp = Heap()
    v1 = BindingVar()
    v2 = BindingVar()
    v3 = BindingVar()
    a1 = AttVar()
    a2 = AttVar()
    a3 = AttVar()
    v1.unify(a1, hp)
    v2.unify(a2, hp)
    v3.unify(a3, hp)

    v1.unify(v2, hp)
    v2.unify(v3, hp)
    assert hp.hook.attvar == a2
    assert hp.hook.next.attvar == a1
    assert hp.hook.next.next is None

    hp = Heap()
    v1 = BindingVar()
    v2 = BindingVar()
    a1 = AttVar()
    a2 = AttVar()
    v1.unify(a1, hp)
    v2.unify(a2, hp)
    assert hp.hook is None
    v1.unify(v2, hp)
    assert hp.hook.attvar == a1
    v1.unify(Number(1), hp)
    assert hp.hook.attvar == a2
    assert hp.hook.next.attvar == a1
    assert hp.hook.next.next is None

    hp = Heap()
    v1 = BindingVar()
    v2 = BindingVar()
    a1 = AttVar()
    a2 = AttVar()
    v1.unify(a1, hp)
    v2.unify(a2, hp)
    t1 = Callable.build("f", [v1, v2])
    t2 = Callable.build("f", [Atom("a"), Atom("b")])
    t1.unify(t2, hp)
    assert hp.hook.attvar == a2
    assert hp.hook.next.attvar == a1
    assert hp.hook.next.next is None

    hp = Heap()
    v = BindingVar()
    av = AttVar()
    v.unify(av, hp)
    assert hp.hook is None
    a = Callable.build("a")
    v.unify(a, hp)
    assert hp.hook.attvar == av
    v.unify(a, hp)
    assert hp.hook.attvar == av
    assert hp.hook.next is None
Ejemplo n.º 7
0
 def new_attvar(self):
     result = AttVar()
     result.created_after_choice_point = self
     return result
Ejemplo n.º 8
0
def test_attvars_with_maps():
    a = AttVar()
    assert a.attmap is AttVar.attmap
    assert a.attmap.indexes == {}
    assert a.value_list == []

    val1 = Callable.build("1")
    a.add_attribute("x", val1)
    assert a.attmap is not AttVar.attmap
    assert a.attmap.indexes == {"x": 0}
    assert a.value_list == [val1]

    m1 = a.attmap
    a.del_attribute("x")
    assert m1 is a.attmap
    assert a.value_list == [None]

    a.add_attribute("x", val1)
    assert a.attmap is m1
    assert a.value_list == [val1]

    val2 = Callable.build("2")
    a.add_attribute("y", val2)
    m2 = a.attmap
    assert m2.indexes == {"x": 0, "y": 1}
    assert a.value_list == [val1, val2]

    a.del_attribute("x")
    assert a.attmap is m2
    assert a.value_list == [None, val2]

    a.del_attribute("y")
    assert a.attmap is m2
    assert a.value_list == [None, None]

    val3 = Callable.build("3")
    a.add_attribute("z", val3)
    m3 = a.attmap
    assert m3.indexes == {"x": 0, "y": 1, "z": 2}
    assert a.value_list == [None, None, val3]

    a.add_attribute("x", val1)
    assert a.attmap is m3
    assert a.value_list == [val1, None, val3]
Ejemplo n.º 9
0
def test_attvars_with_maps():
    a = AttVar()
    assert a.attmap is AttVar.attmap
    assert a.attmap.indexes == {}
    assert a.value_list == []

    val1 = Callable.build("1")
    a.add_attribute("x", val1)
    assert a.attmap is not AttVar.attmap
    assert a.attmap.indexes == {"x": 0}
    assert a.value_list == [val1]

    m1 = a.attmap
    a.del_attribute("x")
    assert m1 is a.attmap
    assert a.value_list == [None]

    a.add_attribute("x", val1)
    assert a.attmap is m1
    assert a.value_list == [val1]

    val2 = Callable.build("2")
    a.add_attribute("y", val2)
    m2 = a.attmap
    assert m2.indexes == {"x": 0, "y": 1}
    assert a.value_list == [val1, val2]

    a.del_attribute("x")
    assert a.attmap is m2
    assert a.value_list == [None, val2]

    a.del_attribute("y")
    assert a.attmap is m2
    assert a.value_list == [None, None]

    val3 = Callable.build("3")
    a.add_attribute("z", val3)
    m3 = a.attmap
    assert m3.indexes == {"x": 0, "y": 1, "z": 2}
    assert a.value_list == [None, None, val3]

    a.add_attribute("x", val1)
    assert a.attmap is m3
    assert a.value_list == [val1, None, val3]