forked from ML-KULeuven/problog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step-by-step.py
executable file
·126 lines (92 loc) · 3.43 KB
/
step-by-step.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#! /usr/bin/env python
"""
Part of the ProbLog distribution.
Copyright 2015 KU Leuven, DTAI Research Group
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from __future__ import print_function
import sys
import time
import os
from problog.program import PrologFile
from problog.logic import Term
from problog.evaluator import SemiringSymbolic, Evaluator
from problog.engine import DefaultEngine
from problog.ddnnf_formula import DDNNF
from problog.cnf_formula import CNF
from problog.sdd_formula import SDD
class Timer(object) :
def __init__(self, msg) :
self.message = msg
self.start_time = None
def __enter__(self) :
self.start_time = time.time()
def __exit__(self, *args) :
print ('%s: %.4fs' % (self.message, time.time()-self.start_time))
def main(filename, with_dot, knowledge) :
dotprefix = None
if with_dot :
dotprefix = os.path.splitext(filename)[0] + '_'
model = PrologFile(filename)
engine = DefaultEngine(label_all=True)
with Timer('parsing') :
db = engine.prepare(model)
print ('\n=== Database ===')
print (db)
print ('\n=== Queries ===')
queries = engine.query(db, Term( 'query', None ))
print ('Queries:', ', '.join([ str(q[0]) for q in queries ]))
print ('\n=== Evidence ===')
evidence = engine.query(db, Term( 'evidence', None, None ))
print ('Evidence:', ', '.join([ '%s=%s' % ev for ev in evidence ]))
print ('\n=== Ground Program ===')
with Timer('ground') :
gp = engine.ground_all(db)
print (gp)
if dotprefix != None :
with open(dotprefix + 'gp.dot', 'w') as f :
print ( gp.toDot(), file=f)
print ('\n=== Acyclic Ground Program ===')
with Timer('acyclic') :
gp = gp.makeAcyclic()
print (gp)
if dotprefix != None :
with open(dotprefix + 'agp.dot', 'w') as f :
print ( gp.toDot(), file=f)
if knowledge == 'sdd' :
print ('\n=== SDD compilation ===')
with Timer('compile') :
nnf = SDD.createFrom(gp)
if dotprefix != None :
nnf.saveSDDToDot(dotprefix + 'sdd.dot')
else :
print ('\n=== Conversion to CNF ===')
with Timer('convert to CNF') :
cnf = CNF.createFrom(gp)
print ('\n=== Compile to d-DNNF ===')
with Timer('compile') :
nnf = DDNNF.createFrom(cnf)
if dotprefix != None :
with open(dotprefix + 'nnf.dot', 'w') as f :
print ( nnf.toDot(), file=f)
print ('\n=== Evaluation result ===')
with Timer('evaluate') :
result = nnf.evaluate()
for it in result.items() :
print ('%s : %s' % (it))
if __name__ == '__main__' :
import argparse
argparser = argparse.ArgumentParser()
argparser.add_argument('filename')
argparser.add_argument('--with-dot', action='store_true')
argparser.add_argument('-k', '--knowledge', choices=['nnf','sdd'], default='sdd')
args = argparser.parse_args()
main(**vars(args))