Skip to content
Snippets Groups Projects
Commit 6f3405d7 authored by Lucas-He's avatar Lucas-He
Browse files

Added a python file to connect to the seerver and run the WBouncingScroll.

parent a2e6a7c3
No related branches found
No related tags found
No related merge requests found
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'
X_ref = {'uri': 'http://mathhub.info/FrameIT/frameworld?WBouncingScroll/Problem?xposition'}
Y_ref = {'uri': 'http://mathhub.info/FrameIT/frameworld?WBouncingScroll/Problem?yposition'}
Xv_ref = {'uri': 'http://mathhub.info/FrameIT/frameworld?WBouncingScroll/Problem?xvelocity'}
Yv_ref = {'uri': 'http://mathhub.info/FrameIT/frameworld?WBouncingScroll/Problem?yvelocity'}
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}
}
g = -200.0
X_fact = create_R_fact(380.0, X_ref)
Y_fact = create_R_fact(300.0, Y_ref)
Xv_fact = create_R_fact(-490.0, Xv_ref)
Yv_fact = create_R_fact(150.0, Yv_ref)
G_fact = create_R_fact(g, 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)
fact_list = [X_fact, Y_fact, Xv_fact, Yv_fact, G_fact, B_fact, walls_fact]
fact_list_encoded = json.dumps(fact_list).encode('utf-8')
scroll_assignment_list = [{"fact": X_ref, "assignment": {"kind": "OMS", "uri": 'http://mathhub.info/FrameIT/frameworld?DefaultSituationSpace/SituationTheory1?fact1'}},
{"fact": Y_ref, "assignment": {"kind": "OMS", "uri": 'http://mathhub.info/FrameIT/frameworld?DefaultSituationSpace/SituationTheory1?fact2'}},
{"fact": Xv_ref, "assignment": {"kind": "OMS", "uri": 'http://mathhub.info/FrameIT/frameworld?DefaultSituationSpace/SituationTheory1?fact3'}},
{"fact": Yv_ref, "assignment": {"kind": "OMS", "uri": 'http://mathhub.info/FrameIT/frameworld?DefaultSituationSpace/SituationTheory1?fact4'}},
{"fact": G_ref, "assignment": {"kind": "OMS", "uri": 'http://mathhub.info/FrameIT/frameworld?DefaultSituationSpace/SituationTheory1?fact5'}},
{"fact": B_ref, "assignment": {"kind": "OMS", "uri": 'http://mathhub.info/FrameIT/frameworld?DefaultSituationSpace/SituationTheory1?fact6'}},
{"fact": W_ref, "assignment": {"kind": "OMS", "uri": 'http://mathhub.info/FrameIT/frameworld?DefaultSituationSpace/SituationTheory1?fact7'}}]
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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment