File: cannot-py.lp

package info (click to toggle)
gringo 5.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,128 kB
  • sloc: cpp: 210,867; ansic: 37,507; python: 11,271; yacc: 825; javascript: 627; sh: 368; xml: 364; makefile: 102
file content (61 lines) | stat: -rw-r--r-- 1,888 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#script (python)

import clingo

class Thing:
    def __init__(self):
        pass

def main(prg):
    prg.ground([("base", [])])

    # extract relevant theory atoms and minimize the number of messages
    with prg.backend() as backend:
        lits = []
        atoms = []
        for atom in prg.theory_atoms:
            if atom.term.name == "cannot" and len(atom.term.arguments) == 0:
                a = Thing()
                a.literal = atom.literal
                a.elements = []
                for elem in atom.elements:
                    e = Thing()

                    # format message
                    t = elem.terms
                    e.message = t[0].name[1:-1]
                    for term in t[1:]:
                        e.message = e.message.replace("%", str(term), 1)

                    # create condition to minimize
                    condition = elem.condition[:]
                    condition.append(atom.literal)
                    if len(condition) > 1:
                        atom = backend.add_atom()
                        backend.add_rule([atom], condition)
                        condition[0] = atom
                    lits.append((condition[0], 1))
                    e.literal = condition[0]

                    a.elements.append(e)
                atoms.append(a)
        backend.add_minimize(0, lits)

    # solve and print debug messages
    with prg.solve(yield_=True) as it:
        for m in it:
            isTrue = lambda x: m.is_true(x.literal)
            invalid = False
            for atom in filter(isTrue, atoms):
                if not invalid:
                    print("ERROR: invalid answer set")
                    invalid = True
                for elem in filter(isTrue, atom.elements):
                    print ("- cannot {}".format(elem.message))

#end.

#theory cannot {
    t { };
    &cannot/0: t, head
}.