Commit de2b9abb authored by Theresa Pollinger's avatar Theresa Pollinger
Browse files

some modularity established

parent 3433dc92
...@@ -2,21 +2,13 @@ ...@@ -2,21 +2,13 @@
# http://cmd2.readthedocs.io # http://cmd2.readthedocs.io
import cmd2 as cmd import cmd2 as cmd
# https://github.com/pytransitions/transitions
from transitions import Machine, State
from collections import OrderedDict
# strings:
# http://mattoc.com/python-yes-no-prompt-cli.html # http://mattoc.com/python-yes-no-prompt-cli.html
from distutils.util import strtobool from distutils.util import strtobool
from pathlib import Path
# https://github.com/phfaist/pylatexenc for directly converting Latex commands to unicode # https://github.com/phfaist/pylatexenc for directly converting Latex commands to unicode
from pylatexenc.latex2text import LatexNodes2Text from pylatexenc.latex2text import LatexNodes2Text
import pyparsing as pp import pyparsing as pp
import re
from pde_state_machine import * from pde_state_machine import *
from string_handling import *
# This "main class" is two things: a REPL loop, by subclassing the cmd2 Cmd class # This "main class" is two things: a REPL loop, by subclassing the cmd2 Cmd class
# and a state machine as given by the pytransitions package # and a state machine as given by the pytransitions package
...@@ -44,8 +36,6 @@ class Interview(cmd.Cmd): ...@@ -44,8 +36,6 @@ class Interview(cmd.Cmd):
# self.greeting() # self.greeting()
self.update_prompt() self.update_prompt()
self.state_machine.prompted = False
#### functions for user interaction #### functions for user interaction
......
...@@ -427,38 +427,39 @@ class PDE_States: ...@@ -427,38 +427,39 @@ class PDE_States:
return return
parameter_name = get_first_word(userstring) parameter_name = get_first_word(userstring)
self.simdata["parameters"][parameter_name] = {} with CriticalSubdict(self.simdata["parameters"]) as psubdict:
with CriticalSubdict(self.simdata["parameters"][parameter_name]) as subdict: self.simdata["parameters"][parameter_name] = {}
# create mmt theory with CriticalSubdict(self.simdata["parameters"][parameter_name]) as subdict:
self.new_theory(parameter_name) # create mmt theory
# we might need the other parameters created so far, so use them self.new_theory(parameter_name)
for otherparamentry in get_recursively(self.simdata["parameters"], "theoryname"): # we might need the other parameters created so far, so use them
self.include_in(parameter_name, otherparamentry) for otherparamentry in get_recursively(self.simdata["parameters"], "theoryname"):
self.include_in(parameter_name, otherparamentry)
# sanitize userstring - check if this works for all cases
parsestring = add_ods(userstring) # sanitize userstring - check if this works for all cases
if parsestring.startswith(parameter_name + "(") or parsestring.startswith(parameter_name + " ("):#todo make smarter for more dimensions parsestring = add_ods(userstring)
parsestring = remove_apply_brackets(parsestring) if parsestring.startswith(parameter_name + "(") or parsestring.startswith(parameter_name + " ("):#todo make smarter for more dimensions
parsestring = functionize(parsestring, self.simdata["domain"]["name"]) parsestring = remove_apply_brackets(parsestring)
# self.poutput(parsestring) parsestring = functionize(parsestring, self.simdata["domain"]["name"])
reply_pconstant = self.mmtinterface.mmt_new_decl("param", parameter_name, parsestring) # self.poutput(parsestring)
reply_pconstant = self.mmtinterface.query_for(parameter_name) reply_pconstant = self.mmtinterface.mmt_new_decl("param", parameter_name, parsestring)
subdict["theoryname"] = parameter_name reply_pconstant = self.mmtinterface.query_for(parameter_name)
subdict["string"] = userstring subdict["theoryname"] = parameter_name
subdict["parsestring"] = parsestring subdict["string"] = userstring
subdict["type"] = self.get_inferred_type(parameter_name, parameter_name) subdict["parsestring"] = parsestring
subdict["type"] = self.get_inferred_type(parameter_name, parameter_name)
# if not reply_pconstant.hasDefinition(parameter_name) and not self.cheating:
# InterviewError("Please define this parameter.") # if not reply_pconstant.hasDefinition(parameter_name) and not self.cheating:
# InterviewError("Please define this parameter.")
# create view
self.new_view(subdict) # create view
self.mmtinterface.mmt_new_decl("ptype", subdict["viewname"], self.new_view(subdict)
"ptype = " + subdict["type"]) self.mmtinterface.mmt_new_decl("ptype", subdict["viewname"],
self.mmtinterface.mmt_new_decl("param", subdict["viewname"], "ptype = " + subdict["type"])
"param = " + parameter_name) self.mmtinterface.mmt_new_decl("param", subdict["viewname"],
self.poutput("Ok, " + parsestring) "param = " + parameter_name)
self.please_prompt("Are these all the parameters?", lambda: self.trigger('parameters_parsed')) self.poutput("Ok, " + parsestring)
self.please_prompt("Are these all the parameters?", lambda: self.trigger('parameters_parsed'))
def parameters_exit(self): def parameters_exit(self):
# print(str(self.simdata["parameters"])) # print(str(self.simdata["parameters"]))
...@@ -472,82 +473,83 @@ class PDE_States: ...@@ -472,82 +473,83 @@ class PDE_States:
self.simdata["pdes"]["pdes"] = [] self.simdata["pdes"]["pdes"] = []
def pdes_handle_input(self, userstring): def pdes_handle_input(self, userstring):
self.simdata["pdes"]["pdes"].append({}) with CriticalSubdict(self.simdata["pdes"]["pdes"]) as psubdict:
with CriticalSubdict(self.simdata["pdes"]["pdes"][-1]) as subdict: self.simdata["pdes"]["pdes"].append({})
subdict["theoryname"] = "ephpde" + str(len(self.simdata["pdes"]["pdes"])) with CriticalSubdict(self.simdata["pdes"]["pdes"][-1]) as subdict:
self.new_theory(subdict["theoryname"]) subdict["theoryname"] = "ephpde" + str(len(self.simdata["pdes"]["pdes"]))
self.new_theory(subdict["theoryname"])
# TODO use symbolic computation to order into LHS and RHS
parts = re.split("=", userstring) # TODO use symbolic computation to order into LHS and RHS
parts = re.split("=", userstring)
if len(parts) is not 2:
raise InterviewError("This does not look like an equation.") if len(parts) is not 2:
raise InterviewError("This does not look like an equation.")
# store the info
subdict["string"] = userstring # store the info
subdict["lhsstring"] = parts[0].strip() subdict["string"] = userstring
subdict["rhsstring"] = parts[1].strip()#TODO expand subdict["lhsstring"] = parts[0].strip()
subdict["rhsstring_expanded"] = self.try_expand(subdict["rhsstring"]) subdict["rhsstring"] = parts[1].strip()#TODO expand
subdict["rhsstring_expanded"] = self.try_expand(subdict["rhsstring"])
# to make the left-hand side a function on x, place " [ variablename : domainname ] " in front
if parts[0].find("x") > -1: # to make the left-hand side a function on x, place " [ variablename : domainname ] " in front
parts[0] = " [ x : " + self.simdata["domain"]["name"] + " ] " + parts[0] if parts[0].find("x") > -1:
# right-hand side: infer type, make function if not one yet parts[0] = " [ x : " + self.simdata["domain"]["name"] + " ] " + parts[0]
if not type_is_function_from(self.get_inferred_type(subdict["theoryname"], parts[1]), # right-hand side: infer type, make function if not one yet
self.simdata["domain"]["name"]): if not type_is_function_from(self.get_inferred_type(subdict["theoryname"], parts[1]),
parts[1] = " [ x : " + self.simdata["domain"]["name"] + " ] " + parts[1] self.simdata["domain"]["name"]):
parts[1] = " [ x : " + self.simdata["domain"]["name"] + " ] " + parts[1]
# in lhs replace all unknown names used by more generic ones and add lambda clause in front
for unkname in get_recursively(self.simdata["unknowns"], "theoryname"): # in lhs replace all unknown names used by more generic ones and add lambda clause in front
parts[0] = parts[0].replace(unkname, " any" + unkname) for unkname in get_recursively(self.simdata["unknowns"], "theoryname"):
parts[0] = " [ any" + unkname + " : " + self.simdata["unknowns"][unkname]["type"] + " ] " + parts[0] parts[0] = parts[0].replace(unkname, " any" + unkname)
# and include the original ones as theory parts[0] = " [ any" + unkname + " : " + self.simdata["unknowns"][unkname]["type"] + " ] " + parts[0]
inc = self.include_in(subdict["theoryname"], unkname) # and include the original ones as theory
for parname in get_recursively(self.simdata["parameters"], "theoryname"): inc = self.include_in(subdict["theoryname"], unkname)
inc = self.include_in(subdict["theoryname"], parname) for parname in get_recursively(self.simdata["parameters"], "theoryname"):
inc = self.include_in(subdict["theoryname"], parname)
# send declarations to mmt
self.mmtinterface.mmt_new_decl("lhs", subdict["theoryname"], " mylhs = " + parts[0]) # send declarations to mmt
reply_lhsconstant = self.mmtinterface.query_for(subdict["theoryname"]) self.mmtinterface.mmt_new_decl("lhs", subdict["theoryname"], " mylhs = " + parts[0])
reply_lhsconstant = self.mmtinterface.query_for(subdict["theoryname"])
self.mmtinterface.mmt_new_decl("rhs", subdict["theoryname"], " myrhs = " + parts[1])
reply_rhsconstant = self.mmtinterface.query_for(subdict["theoryname"]) self.mmtinterface.mmt_new_decl("rhs", subdict["theoryname"], " myrhs = " + parts[1])
reply_rhsconstant = self.mmtinterface.query_for(subdict["theoryname"])
# create view
self.new_view(subdict) # create view
ltype = self.get_inferred_type(subdict["theoryname"], "mylhs") self.new_view(subdict)
eqtype = get_last_type(ltype) ltype = self.get_inferred_type(subdict["theoryname"], "mylhs")
rtype = self.get_inferred_type(subdict["theoryname"], "myrhs") eqtype = get_last_type(ltype)
self.mmtinterface.mmt_new_decl("eqtype", subdict["viewname"], rtype = self.get_inferred_type(subdict["theoryname"], "myrhs")
"eqtype = " + eqtype) self.mmtinterface.mmt_new_decl("eqtype", subdict["viewname"],
self.mmtinterface.mmt_new_decl("lhs", subdict["viewname"], "eqtype = " + eqtype)
"lhs = " + "mylhs") self.mmtinterface.mmt_new_decl("lhs", subdict["viewname"],
self.mmtinterface.mmt_new_decl("rhs", subdict["viewname"], "lhs = " + "mylhs")
"rhs = " + "myrhs") self.mmtinterface.mmt_new_decl("rhs", subdict["viewname"],
self.mmtinterface.mmt_new_decl("pde", subdict["viewname"], "rhs = " + "myrhs")
"pde = " + "[u](mylhs u) funcEq myrhs") self.mmtinterface.mmt_new_decl("pde", subdict["viewname"],
"pde = " + "[u](mylhs u) funcEq myrhs")
reply = self.mmtinterface.query_for(subdict["theoryname"])
reply = self.mmtinterface.query_for(subdict["theoryname"])
for unkname in get_recursively(self.simdata["unknowns"], "theoryname"):
op = subdict["lhsstring"].replace(unkname, "") for unkname in get_recursively(self.simdata["unknowns"], "theoryname"):
op = op.strip() op = subdict["lhsstring"].replace(unkname, "")
op = op.strip()
# store the info
subdict["op"] = op # store the info
subdict["lhsparsestring"] = parts[0] subdict["op"] = op
subdict["rhsparsestring"] = parts[1] subdict["lhsparsestring"] = parts[0]
subdict["rhsparsestring"] = parts[1]
# TODO query number of effective pdes and unknowns from mmt for higher dimensional PDEs
# => can assume each to be ==1 for now # TODO query number of effective pdes and unknowns from mmt for higher dimensional PDEs
numpdesgiven = len(self.simdata["pdes"]["pdes"]) # => can assume each to be ==1 for now
self.poutput("Ok, " + reply.tostring()) numpdesgiven = len(self.simdata["pdes"]["pdes"])
if numpdesgiven == len(self.simdata["unknowns"]): self.poutput("Ok, " + reply.tostring())
self.trigger('pdes_parsed') if numpdesgiven == len(self.simdata["unknowns"]):
elif numpdesgiven > len(self.simdata["unknowns"]): self.trigger('pdes_parsed')
self.poutput("now that's too many PDEs. Please go back and add more unknowns.") elif numpdesgiven > len(self.simdata["unknowns"]):
else: self.poutput("now that's too many PDEs. Please go back and add more unknowns.")
self.poutput("More PDEs, please!") else:
self.poutput("More PDEs, please!")
def pdes_exit(self): def pdes_exit(self):
self.poutput("These are all the PDEs needed.") self.poutput("These are all the PDEs needed.")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment