-
Notifications
You must be signed in to change notification settings - Fork 0
/
coord.py
137 lines (92 loc) · 3.73 KB
/
coord.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
127
128
129
130
131
132
133
134
135
136
137
import subprocess
import re
### some methods ###
def list_to_string(list):
"""Takes a list and returns it as a string.
['help', 'me'] becomes '[help,me]'"""
return_value = "["
for member in list:
return_value += str(member) + ","
# remove last comma
return_value = return_value[0:-1] + "]"
return return_value
# TODO: how to do search?
def run_maude(configuration, filename, command="rew"):
"""Runs maude and returns the output configuration as a string.
filenames is a list of filenames. """
print(configuration)
# regex to match with the maude configuration from the maude output
config_regex = re.compile("result .*?: (.*)")
# pipe for communication with process
pipe = subprocess.PIPE
"""
args = [
"maude", "-no-banner", "-no-wrap", "prolog_message_generation.maude"
]
"""
args = [
"maude", "-no-banner", "-no-wrap", filename
]
print(command + " " + configuration + " .")
maude_proc = subprocess.Popen(args, stdout=pipe, stdin=pipe, universal_newlines=True)
out = maude_proc.communicate(input=command + " " + configuration + " .")
maude_proc.terminate()
print(maude_proc.stdout)
print(out[0])
config_match = re.search(config_regex, out[0])
config = config_match.group(1)
return config
def get_queries(maude_config):
"""Takes a maude configuration as a string and returns all of the prolog queries in
a list."""
# regex to find all queries for prolog :
# it matches with anything on the form queryInside(something(something))
# where something(something) is what's saved when regex searching
query_regex = re.compile("queryInside\((.*?\(.*?\))\)")
query_matches = re.findall(query_regex, config)
#print(query_matches)
#for a in query_matches:
#print(a)
return query_matches
def run_prolog(queries):
"""Runs prolog and returns the results as a list of strings.
It's assumed that the prolog program returns its result so that the
predicates are separated by #."""
# pipe for communication with process
pipe = subprocess.PIPE
cmd_args = [
"swipl",
"-s", "pythoninteraction.pl", # obs: the filename might change
"-g", "handle_queries_test(" + queries + ")",
"-t", "halt"
]
prolog_proc = subprocess.run(cmd_args, stdout=pipe, universal_newlines=True)
#print(prolog_proc.stdout)
result_list = prolog_proc.stdout.split("#")
#for term in result_list:
#print(term)
return result_list
def remove_queries(configuration):
"""Takes in a config all terms of the type queryInside(...)"""
temp = configuration
output = re.sub("queryInside\(.*?\(.*?\)\)\s*", "", temp)
return output
### start of script ###
# run maude (rewrite) with the initial configuration and store the configuration as a string
config = run_maude("init", "prolog_message_generation.maude")
#config = run_maude("init", ["family.maude", "prolog_message_generation.maude"])
# get prolog queries from the configuration
queries_for_prolog = get_queries(config)
# while there are queries for prolog from maude, keep on running
while queries_for_prolog:
# run prolog and get a list with the new data
prolog_terms = run_prolog(list_to_string(queries_for_prolog))
# remove any old queries from the configuration
config_with_queries_removed = remove_queries(config)
# add the terms from prolog to the configuration
for term in prolog_terms:
config_with_queries_removed += " " + term
# run maude with the new configuration
config = run_maude(config_with_queries_removed, "prolog_message_generation.maude")
# check if there are any queries for prolog
queries_for_prolog = get_queries(config)