Newer
Older
import http.client
import json
import time
HOST = 'localhost' #'127.0.0.1'
PORT = 8085
headers = {'Content-type': 'application/json'}
bouncing_scroll = 'http://mathhub.info/FrameIT/frameworld?WBouncingScroll'
Lucas-He
committed
pos_ref = {'uri': 'http://mathhub.info/FrameIT/frameworld?WBouncingScroll/Problem?position'}
vel_ref = {'uri': 'http://mathhub.info/FrameIT/frameworld?WBouncingScroll/Problem?velocity'}
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
G_ref = {'uri': 'http://mathhub.info/FrameIT/frameworld?WBouncingScroll/Problem?g_base'}
B_ref = {'uri': 'http://mathhub.info/FrameIT/frameworld?WBouncingScroll/Problem?bounce'}
W_ref = {'uri': 'http://mathhub.info/FrameIT/frameworld?WBouncingScroll/Problem?walls'}
real_list = 'http://mathhub.info/FrameIT/frameworld?DefaultSituationSpace/SituationTheory2?real_list'
list_type = 'http://gl.mathhub.info/MMT/LFX/Datatypes?ListSymbols?ListType'
product = 'http://gl.mathhub.info/MMT/LFX/Sigma?Symbols?Product'
real_lit = 'http://mathhub.info/MitM/Foundation?RealLiterals?real_lit'
cons = 'http://gl.mathhub.info/MMT/LFX/Datatypes?ListSymbols?cons'
cons_nil = 'http://gl.mathhub.info/MMT/LFX/Datatypes?ListSymbols?nil_constant'
tuple_mmt = 'http://gl.mathhub.info/MMT/LFX/Sigma?Symbols?Tuple'
def list_element_4tuple(x1, y1, x2, y2):
return {'applicant': {'uri': tuple_mmt, 'kind': 'OMS'},
'arguments': [{'applicant': {'uri': tuple_mmt, 'kind': 'OMS'},
'arguments': [{'float': x1, 'kind': 'OMF'},
{'float': y1, 'kind': 'OMF'}],
'kind': 'OMA'},
{'applicant': {'uri': tuple_mmt, 'kind': 'OMS'},
'arguments': [{'float': x2, 'kind': 'OMF'},
{'float': y2, 'kind': 'OMF'}],
'kind': 'OMA'}],
'kind': 'OMA'}
def list_element_4tuple_plus_nil(x1, y1, x2, y2):
return {'applicant': {'uri': cons, 'kind': 'OMS'},
'arguments': [{'applicant': {'uri': cons_nil, 'kind': 'OMS'},
'arguments': [{'applicant': {'uri': product, 'kind': 'OMS'},
'arguments': [{'applicant': {'uri': product, 'kind': 'OMS'},
'arguments': [{'uri': real_lit, 'kind': 'OMS'},
{'uri': real_lit, 'kind': 'OMS'}],
'kind': 'OMA'},
{'applicant': {'uri': product, 'kind': 'OMS'},
'arguments': [{'uri': real_lit, 'kind': 'OMS'},
{'uri': real_lit, 'kind': 'OMS'}],
'kind': 'OMA'}],
'kind': 'OMA'}],
'kind': 'OMA'},
{'applicant': {'uri': tuple_mmt, 'kind': 'OMS'},
'arguments': [{'applicant': {'uri': tuple_mmt, 'kind': 'OMS'},
'arguments': [{'float': x1, 'kind': 'OMF'},
{'float': y1, 'kind': 'OMF'}],
'kind': 'OMA'},
{'applicant': {'uri': tuple_mmt, 'kind': 'OMS'},
'arguments': [{'float': x2, 'kind': 'OMF'},
{'float': y2, 'kind': 'OMF'}],
'kind': 'OMA'}],
'kind': 'OMA'}],
'kind': 'OMA'}
def cons_2elements(e1, e2):
return {'applicant': {'uri': cons, 'kind': 'OMS'},
'arguments': [e1, e2],
'kind': 'OMA'}
def construct_list_4tuple_df(values):
# Values needs to be of the form [[x1, y1, x2, y2], [x1, y1, x2, y2], ...]
prev_ele = {}
for ci, cv in enumerate(values[::-1]):
if ci == 0:
prev_ele = list_element_4tuple_plus_nil(cv[0], cv[1], cv[2], cv[3])
else:
cur_ele = list_element_4tuple(cv[0], cv[1], cv[2], cv[3])
prev_ele = cons_2elements(prev_ele, cur_ele)
return prev_ele
def create_walls_fact(values):
# Values needs to be of the form [[x1, y1, x2, y2], [x1, y1, x2, y2], ...]
return {'ref': W_ref,
'label': 'Walls',
'tp': {'applicant': {'uri': list_type, 'kind': 'OMS'},
'arguments': [{'applicant': {'uri': product, 'kind': 'OMS'},
'arguments': [{'applicant': {'uri': product, 'kind': 'OMS'},
'arguments': [{'uri': real_lit, 'kind': 'OMS'},
{'uri': real_lit, 'kind': 'OMS'}],
'kind': 'OMA'},
{'applicant': {'uri': product, 'kind': 'OMS'},
'arguments': [{'uri': real_lit, 'kind': 'OMS'},
{'uri': real_lit, 'kind': 'OMS'}],
'kind': 'OMA'}],
'kind': 'OMA'}],
'kind': 'OMA'},
'kind': 'general',
'df': construct_list_4tuple_df(values)
}
def create_R_fact(val, ref):
return {"ref": ref,
"label": "X",
"kind": "general",
"tp": {'uri': 'http://mathhub.info/MitM/Foundation?RealLiterals?real_lit',
'kind': 'OMS'},
"df": {"kind": "OMF", "float": val}
}
Lucas-He
committed
def create_point_fact(val, ref):
return {"ref": ref,
"label": "P",
"kind": "general",
"tp": {"kind": "OMS", "uri": "http://mathhub.info/MitM/core/geometry?3DGeometry?point"},
"df": {"kind": "OMA", "applicant": {"kind": "OMS", "uri": "http://gl.mathhub.info/MMT/LFX/Sigma?Symbols?Tuple"},
"arguments": [{"kind": "OMF", "float": val[0]},
{"kind": "OMF", "float": val[1]},
{"kind": "OMF", "float": val[2]}]}}
g = -200.0
Lucas-He
committed
pos_fact = create_point_fact([0.0, 300.0, 380.0], pos_ref)
vel_fact = create_point_fact([0.0, 150.0, -490.0], vel_ref)
G_fact = create_point_fact([0.0, g, 0.0], G_ref)
B_fact = create_R_fact(0.8, B_ref)
#walls_fact = create_walls_fact([[-10000.0, 0.0, 10000.0, 0.0]])
walls_list = [[-1.0, 0.0, 401.0, 0.0],
[0.0, -1.0, 0.01, 401.0],
[400.0, -1.0, 400.01, 401.0],
[-1.0, 400.0, 401.0, 400.0],
[50.0, 200.0, 120.0, 270.0],
[150.0, 100.0, 200.0, 100.0],
[150.0, 285.0, 200.0, 200.0],
[230.0, 360.0, 300.0, 300.0],
[300.0, 100.0, 385.0, 150.0],
[50.0, 50.0, 100.0, 135.0]]
walls_fact = create_walls_fact(walls_list)
Lucas-He
committed
fact_list = [pos_fact, vel_fact, G_fact, B_fact, walls_fact]
fact_list_encoded = json.dumps(fact_list).encode('utf-8')
Lucas-He
committed
scroll_assignment_list = [{"fact": pos_ref, "assignment": {"kind": "OMS", "uri": 'http://mathhub.info/FrameIT/frameworld?DefaultSituationSpace/SituationTheory1?fact1'}},
{"fact": vel_ref, "assignment": {"kind": "OMS", "uri": 'http://mathhub.info/FrameIT/frameworld?DefaultSituationSpace/SituationTheory1?fact2'}},
{"fact": G_ref, "assignment": {"kind": "OMS", "uri": 'http://mathhub.info/FrameIT/frameworld?DefaultSituationSpace/SituationTheory1?fact3'}},
{"fact": B_ref, "assignment": {"kind": "OMS", "uri": 'http://mathhub.info/FrameIT/frameworld?DefaultSituationSpace/SituationTheory1?fact4'}},
{"fact": W_ref, "assignment": {"kind": "OMS", "uri": 'http://mathhub.info/FrameIT/frameworld?DefaultSituationSpace/SituationTheory1?fact5'}}]
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
scroll_application = {"scroll": bouncing_scroll,
"assignments": scroll_assignment_list}
scroll_encoded = json.dumps(scroll_application).encode('utf-8')
t1 = time.time()
conn = http.client.HTTPConnection(HOST, port=PORT)
conn.request("POST", "/fact/bulkadd", headers=headers, body=fact_list_encoded)
response = conn.getresponse()
data = response.read()
print(response.status, response.reason)
print(time.time() - t1)
data_dict = json.loads(data.decode('utf-8'))
t2 = time.time()
conn = http.client.HTTPConnection(HOST, port=PORT)
conn.request("POST", "/scroll/apply", headers=headers, body=scroll_encoded)
response = conn.getresponse()
data = response.read()
print(response.status, response.reason)
print(time.time() - t2)
data_dict = json.loads(data.decode('utf-8'))
for i in range(len(data_dict["acquiredFacts"])):
print(data_dict["acquiredFacts"][i])
rel_fact = data_dict['acquiredFacts'][0]["df"]["arguments"]
values = []
def rec_vals(facts):
for rf in facts:
if rf["applicant"]["uri"] == cons_nil:
continue
elif len(rf["arguments"]) == 3:
rfa = rf["arguments"]
cvals = []
for ri2, rrf in enumerate(rfa):
cvals.extend([x["float"] for x in rrf["arguments"]])
values.append(cvals)
continue
else:
rec_vals(rf["arguments"])
return
rec_vals(rel_fact)
print("Step\tX\tY\tXv\tYv\tht\tm")
for i, v in enumerate(values):
print("%d\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f" % (i, v[0], v[1], v[2], v[3], v[4], v[5]))
import matplotlib.pyplot as plt
import numpy as np
def get_xy(xs, ys, xv, yv, ct):
rx = xs + ct * xv
ry = ys + ct * yv + 0.5 * g * ct**2
return [rx, ry]
plt.figure()
for wl in walls_list:
plt.plot([wl[0], wl[2]], [wl[1], wl[3]], color="blue", linewidth=3)
for i, vl in enumerate(values):
pvals = np.array([get_xy(vl[0], vl[1], vl[2], vl[3], cct) for cct in np.arange(0, vl[4], 0.01)])
plt.scatter(pvals[:, 0], pvals[:, 1], color="green", s=1)
plt.show()