Пример #1
0
def test_parens(tell, qeval):
    tell("paren1(a, b, c) if (a = b and b = c);")
    tell("paren2(a, b, c) if ((a = b and b = c));")
    tell("paren3(a, b, c) if (a = b) and (b = c);")
    tell("paren4(a, b, c, d) if (a = b and b = c and c = d);")
    tell("paren5(a, b, c) if ((a = b) and (b = c));")

    assert qeval("paren1(1, 1, 1)")
    assert not qeval("paren1(0, 1, 1)")
    assert not qeval("paren1(1, 1, 0)")
    assert not qeval("paren1(1, 0, 1)")

    assert qeval("paren2(1, 1, 1)")
    assert not qeval("paren2(0, 1, 1)")
    assert not qeval("paren2(1, 1, 0)")
    assert not qeval("paren2(1, 0, 1)")

    assert qeval("paren3(1, 1, 1)")
    assert not qeval("paren3(0, 1, 1)")
    assert not qeval("paren3(1, 1, 0)")
    assert not qeval("paren3(1, 0, 1)")

    assert qeval("paren4(1, 1, 1, 1)")
    assert not qeval("paren4(0, 1, 1, 1)")
    assert not qeval("paren4(1, 1, 0, 1)")
    assert not qeval("paren4(1, 1, 1, 0)")

    assert qeval("paren5(1, 1, 1)")
    assert not qeval("paren5(0, 1, 1)")
    assert not qeval("paren5(1, 1, 0)")
    assert not qeval("paren5(1, 0, 1)")
Пример #2
0
def test_rule_ordering(tell, qeval, externals):
    tell("f(_: Foo{}) if cut and 1 = 2;")
    tell('f(_: Foo{name: "test"});')

    assert qeval('f(new Foo( name: "test" )) ')
    assert qeval('x = new Foo( name: "test" ) and f(x) ')
    assert not qeval('f(new Foo( name: "nope" )) ')
    assert not qeval('x = new Foo( name: "nope" ) and f(x) ')
Пример #3
0
def test_argument_patterns(tell, qeval, qvar, externals):
    tell("matchesFoo(name, foo: Foo) if name = foo.name")

    assert qeval('matchesFoo(sam, new Foo(name: "sam"))')
    assert qeval('matchesFoo(sam, new Bar(name: "sam"))')
    assert not qeval('matchesFoo("alex", new Foo(name: "sam"))')
    assert not qeval('matchesFoo("alex", new Bar(name: "sam"))')
    assert not qeval('matchesFoo("alex", new Qux())')
Пример #4
0
def test_recursive_rule(tell, qeval, qvar):
    tell('derive("apple", "orange")')
    tell('derive("orange", "avacado")')
    tell('derive("avacado", "juniper_berry")')
    results = qvar('derive("apple", x)', "x")
    assert results == ["orange"]
    tell("derives(a, b) if derive(a, b);")
    tell("derives(a, b) if derive(a, z) and derives(z, b);")
    assert qeval('derives("apple", "juniper_berry")')
    results = qvar('derives("apple", x)', "x")
    assert results == ["orange", "avacado", "juniper_berry"]
Пример #5
0
def test_defining_things(tell, qeval):
    facts = [
        'apple("orange")',
        'thing("with", "two")',
        'thing("with", "a", "lot", "of", "arguments", 1, 2, 3, 4, 5)',
        'thing(with("nested"), "stuff")',
        "dream(within(a(dream(within(a(dream(within(a(_dream)))))))))",
        'embedded("strings")',
    ]
    for f in facts:
        tell(f)
    for f in facts:
        assert qeval(f)
Пример #6
0
def test_disjunctive_rule(tell, qeval):
    tell("or_eq(a, b) if 1 = 0 or a = b;")
    assert qeval("or_eq(1, 1)")

    tell("and_or_eq(a, b, c) if (a = b and b = c) or 1 = 0")
    assert not qeval("and_or_eq(1, 1, 0)")
    assert qeval("and_or_eq(1, 1, 1)")

    assert qeval("1=0 or (1=1 and 1=1)")
    assert not qeval("1=0 or (1=0 and 1=1)")

    # not sure if these test anything but :)
    assert qeval("1=0 or (1=0 or 1=1)")
    assert not qeval("1=0 or (1=0 or 1=0)")

    assert qeval("1=1 and (1=0 or 1=1)")
    assert not qeval("1=0 and (1=0 or 1=1)")
Пример #7
0
def test_negation(tell, qeval):
    tell('b("apple")')
    assert qeval('b("apple")')
    assert not qeval('not (b("apple"))')
    assert qeval('not (b("notanapple"))')
Пример #8
0
def test_missing_rule(tell, qeval):
    tell("a(x) if b(x) and c(x);")
    tell('b("apple")')
    tell('c("apple")')
    assert not qeval('d("apple")')
Пример #9
0
def test_define_rule(tell, qeval):
    tell("a(x) if b(x) and c(x);")
    tell('b("apple")')
    tell('c("apple")')
    assert qeval('a("apple")')
Пример #10
0
def test_query_multiple(tell, qvar):
    tell('a("foo")')
    tell('a("bar")')
    tell('a("baz")')
    results = qvar("a(x)", "x")
    assert results == ["foo", "bar", "baz"]
Пример #11
0
def test_load_file(load_file, tell, qeval, qvar):
    load_file(Path(__file__).parent / "policies/test.polar")
    assert qeval('test("true")')
    tell('b("foo")')
    assert qvar("a(x)", "x", one=True) == "foo"
Пример #12
0
def test_arities(tell, qeval):
    tell("f(1);")
    tell("f(x, y);")
    assert qeval("f(1)")
    assert not qeval("f(2)")
    assert qeval("f(2, 3)")
Пример #13
0
def test_unify_class_fields(tell, qeval, qvar, externals):
    tell("check(name, new Foo(name: name))")

    assert qeval('check("sam", new Foo(name: "sam"))')
    assert not qeval('check("alex", new Foo(name: "sam"))')
Пример #14
0
def test_dictionaries(tell, qeval, qvar):
    # basic dictionary lookup
    tell('dict({hello: "world", foo: "bar"})')
    assert qeval('dict(d) and d.hello = "world"')

    ### dictionary lookups with variable fields ###
    tell("attr(d, k, d.(k))")

    # k = "hello", {hello: "steve"}.(k) = "steve"
    assert qeval('attr({hello: "steve"}, "hello", "steve")')

    # k = "hello", {hello: "steve"}.(k) = value, value = "steve"
    assert qvar('attr({hello: "steve"}, "hello", value)', "value", one=True) == "steve"

    # k = key, {hello: "steve"}.(k) = "steve", key = "hello"
    assert qvar('attr({hello: "steve"}, key, "steve")', "key", one=True) == "hello"

    ### nested lookups ###
    assert qeval(
        'attr({hello: {this: {is: "nested"}}}, "hello", {this: {is: "nested"}})'
    )

    tell("deepget(d, d.hello.this.is)")
    assert qeval('deepget({hello: {this: {is: "nested"}}}, "nested")')

    tell("myget(d, d.get.inner)")
    assert qeval('myget({get: {inner: "nested"}}, "nested")')

    tell("x({a: {b:{c:123}}})")
    tell("x({a: {y:{c:456}}})")
    assert qvar("x(d) and d.a.(k).c = value", "value") == [123, 456]

    tell("lookup(dict, result) if result = dict.a.b.c;")
    assert qeval('lookup({a: {b: {c: "nested"}}}, "nested")')

    ### more basic lookup tests ###
    tell('user({name: "steve", job: "programmer", state: "NY"})')
    tell('user({name: "alex", job: "programmer", state: "CO"})')
    tell('user({name: "graham", job: "business", state: "NY"})')
    assert qeval('user(d) and d.name = "steve"')
    assert qvar('user({job: "programmer", name: name, state: state})', "name") == [
        "steve",
        "alex",
    ]