forked from TeamCohen/TensorLog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.py
More file actions
46 lines (41 loc) · 1.53 KB
/
Copy pathlist.py
File metadata and controls
46 lines (41 loc) · 1.53 KB
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
import sys
import getopt
#
#
#
from tensorlog import declare
from tensorlog import tensorlog
from tensorlog import matrixdb
if __name__ == "__main__":
def usage():
print "usage: python -m list --db dbspec [--mode mode]"
print " without mode specified: lists the relations in the database"
print " with mode specified: lists the facts in one relation in .cfacts format"
print "usage: python -m list --prog progspec [--ruleIds]"
print " list the all rule ids"
argspec = ["db=","mode=","prog=","ruleIds"]
try:
optlist,args = getopt.getopt(sys.argv[1:], 'x', argspec)
except getopt.GetoptError:
usage()
raise
optdict = dict(optlist)
db = tensorlog.parseDBSpec(optdict['--db']) if '--db' in optdict else None
if db and (not '--mode' in optdict):
db.listing()
elif db and ('--mode' in optdict):
try:
functor,rest = optdict['--mode'].split("/")
arity = int(rest)
m = db.matEncoding[(functor,arity)]
for goal,weight in db.matrixAsPredicateFacts(functor,arity,m).items():
print '\t'.join([goal.functor] + goal.args + ['%g' % (weight)])
except Exception:
usage()
assert False,'mode should be of the form functor/arity for something in the database'
elif '--prog' in optdict:
prog = tensorlog.parseProgSpec(optdict['--prog'],db,proppr=True)
for rid in prog.ruleIds:
print '\t'.join(['ruleid',rid])
else:
usage()