def test_blacklist(): context = Context() context.valasp_blacklist(PredicateName('number'), [2]) model = context.valasp_run_solver(["number(1)."]) assert str(model) == "[number(1)]" with pytest.raises(RuntimeError): context.valasp_run_solver(["number(1,2)."])
def test_auto_blacklist(): context = Context() @context.valasp(with_fun=Fun.TUPLE) class Edge: source: Integer dest: Integer Edge(Tuple([Number(1), Number(2)])) assert str(context.valasp_run_solver(["edge(1,2)."])) == '[edge(1,2)]' with pytest.raises(RuntimeError): context.valasp_run_solver(["edge(1,2). edge((1,2))."])
def test_int(): context = Context() @context.valasp() class Node: value: Integer Node(Number(0)) model = context.valasp_run_solver(["node(0)."]) assert str(model) == '[node(0)]' with pytest.raises(RuntimeError): context.valasp_run_solver(["node(a)."])
def test_add_validator(): class PositiveNumber: def __init__(self, value: Symbol): if not (1 <= value.number): raise ValueError("not a positive number") self.value = value context = Context() context.valasp_register_class(PositiveNumber) context.valasp_add_validator(PredicateName('positiveNumber'), 1) model = context.valasp_run_solver(["positiveNumber(1)."]) assert str(model) == '[positiveNumber(1)]' with pytest.raises(RuntimeError): context.valasp_run_solver(["positiveNumber(0)."])
def test_wrap_context_object(): class MyContext: def foo(self, x): return x.number + 1 context = Context(wrap=[MyContext()]) assert str(context.valasp_run_solver(['a(@foo(1)).'])) == '[a(2)]'
def test_string(): context = Context() @context.valasp() class Name: value: String Name(QString('mario')) model = context.valasp_run_solver(['name("mario").']) assert str(model) == '[name("mario")]' with pytest.raises(RuntimeError): context.valasp_run_solver(["name(mario)."]) with pytest.raises(RuntimeError): context.valasp_run_solver(["name(123)."])
def test_disable_auto_blacklist(): context = Context() @context.valasp(auto_blacklist=False) class Edge: source: Integer dest: Integer Edge(Function('edge', [Number(1), Number(2)])) assert str(context.valasp_run_solver(["edge(1,2). edge((1,2))." ])) == '[edge(1,2), edge((1,2))]'
def test_check_method(): context = Context() @context.valasp() class Node: value: int def check(self): if not (1 <= self.value <= 10): raise ValueError("must be in 1..10") Node(Number(1)) model = context.valasp_run_solver(["node(10)."]) assert str(model) == '[node(10)]' with pytest.raises(RuntimeError): context.valasp_run_solver(["node(0)."]) with pytest.raises(RuntimeError): context.valasp_run_solver(["node(11)."])
def test_complex_type(): context = Context() @context.valasp() class Node: value: Integer @context.valasp(with_fun=Fun.TUPLE) class Edge: from_: Node to: Node def check_ordered(self): if not (self.from_ < self.to): raise ValueError("nodes must be ordered") Edge(Tuple([Number(1), Number(2)])) model = context.valasp_run_solver(['node(1). node(2). edge(1,2).']) assert str(model) == '[node(1), node(2), edge(1,2)]' with pytest.raises(RuntimeError): context.valasp_run_solver(['node(1). node(2). edge(2,1).'])
def test_date_as_fun(): context = Context() @context.valasp(validate_predicate=False, with_fun=Fun.IMPLICIT) class Date: year: int month: int day: int def __post_init__(self): datetime.datetime(self.year, self.month, self.day) @context.valasp() class Birthday: name: String date: Date Birthday( Function('birthday', [ QString('mario'), Function('date', [Number(1983), Number(9), Number(12)]) ])) model = context.valasp_run_solver(['birthday("sofia", date(2019,6,25)).']) assert str(model) == '[birthday("sofia",date(2019,6,25))]' model = context.valasp_run_solver([ 'birthday("sofia", date(2019,6,25)).', 'birthday("leonardo", date(2018,2,1)).' ]) assert str( model ) == '[birthday("sofia",date(2019,6,25)), birthday("leonardo",date(2018,2,1))]' with pytest.raises(RuntimeError): context.valasp_run_solver(['birthday("bigel", date(1982,123)).']) with pytest.raises(RuntimeError): context.valasp_run_solver(['birthday("no one", date(2019,2,29)).']) with pytest.raises(RuntimeError): context.valasp_run_solver(['birthday("sofia", (2019,6,25)).'])
def test_register_term(): context = Context() context.valasp_register_term('filename', PredicateName('successor'), ['x'], ['return x.number + 1']) model = context.valasp_run_solver(["one(@successor(0))."]) assert str(model) == '[one(1)]'
def test_run_solver(): context = Context() assert str(context.valasp_run_solver(["hello_world."])) == "[hello_world]"
def test_blacklist_all_arities(): context = Context() context.valasp_blacklist(PredicateName('number')) with pytest.raises(RuntimeError): context.valasp_run_solver(["number(1)."])