-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexecdatalog.py
More file actions
120 lines (93 loc) · 2.91 KB
/
Copy pathexecdatalog.py
File metadata and controls
120 lines (93 loc) · 2.91 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
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
import os
import subprocess
import json
def load_file(filename):
content = []
with open(filename, "rt") as infile:
for line in infile:
content.append(line.strip())
return content
SECTION_DATA = ";;# data"
SECTION_QUERY = ";;# query"
SECTION_ARGS = ";;# args"
# the path to the node.js based datalog helper
BINPATH = "./dltools/nodeinterp/"
def run_datalog(kb, query, inputs, silent=False, subresults=False):
fullinput = ""
if SECTION_DATA not in kb:
fullinput += SECTION_DATA + "\n"
fullinput += kb
fullinput += "\n"
if not type(inputs) is str:
inputs = json.dumps(inputs)
if SECTION_QUERY not in inputs:
fullinput += SECTION_QUERY + "\n"
fullinput += query
fullinput += "\n"
if SECTION_ARGS not in inputs:
fullinput += SECTION_ARGS + "\n"
fullinput += inputs
fullinput += "\n"
# print(fullinput)
fullinput = fullinput.encode("utf-8")
cmd = ["node", "interpreter.js", "-"]
result = subprocess.run(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
input=fullinput,
cwd=BINPATH,
)
stdout = result.stdout.decode("utf-8")
stderr = result.stderr.decode("utf-8").strip()
stdout = stdout.strip()
if stdout.startswith("[") and stdout.endswith("]"):
stdout = stdout[1:-1]
stdout = stdout.strip()
if not silent:
print("-" * 80, "STDERR")
print(stderr)
print("-" * 80, "STDOUT")
print(stdout)
print("-" * 80)
if not subresults:
return stdout
allresults = []
latest_result = None
for line in stderr.split("\n"):
line = line.strip()
if not line.startswith("IM_"):
continue
if line.startswith("IM_QUERY:"):
if latest_result is not None:
allresults.append(latest_result)
latest_result = None
latest_result = {"query": None, "args": None, "result": None}
line = line[len("IM_QUERY:"):]
line = json.loads(line)
latest_result["query"] = line
elif line.startswith("IM_ARGS:"):
line = line[len("IM_ARGS:"):]
line = json.loads(line)
latest_result["args"] = line
elif line.startswith("IM_RESULT:"):
line = line[len("IM_RESULT:"):]
line = json.loads(line)
latest_result["result"] = line
if latest_result is not None:
allresults.append(latest_result)
return stdout, allresults
def main():
print("testing datalog execution")
test_match = os.path.join(BINPATH, "test_match.dl")
content = "\n".join(load_file(test_match))
query = """
[:find (count ?e)
:where [?e ":event/minute" ?minute]
[?e ":event/type" "card"]]
"""
inputs = []
run_datalog(content, query, inputs)
print("all done")
if __name__ == "__main__":
main()