Skip to content

Commit

Permalink
Reformat file and compare definitions with XMILE specification
Browse files Browse the repository at this point in the history
  • Loading branch information
alexprey committed Sep 20, 2017
1 parent 6c7972b commit d6ee6ee
Showing 1 changed file with 103 additions and 12 deletions.
115 changes: 103 additions & 12 deletions pysd/py_backend/xmile/SMILE2Py.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,115 @@

# Here we define which python function each XMILE keyword corresponds to
functions = {
"abs": "abs", "int": "int", "exp": "np.exp", "inf": "np.inf", "log10": "np.log10",
"pi": "np.pi", "sin": "np.sin", "cos": "np.cos", "sqrt": "np.sqrt", "tan": "np.tan",
"lognormal": "np.random.lognormal", "normal": "np.random.normal",
"poisson": "np.random.poisson", "ln": "np.log", "exprnd": "np.random.exponential",
"random": "np.random.rand", "min": "min", "max": "max", "arccos": "np.arccos",
"arcsin": "np.arcsin", "arctan": "np.arctan",
# ===
# 3.5.1 Mathematical Functions
# http://docs.oasis-open.org/xmile/xmile/v1.0/csprd01/xmile-v1.0-csprd01.html#_Toc398039980
# ===

"abs": "abs",
"arccos": "np.arccos",
"arcsin": "np.arcsin",
"arctan": "np.arctan",
"cos": "np.cos",
"sin": "np.sin",
"tan": "np.tan",
"exp": "np.exp",
"inf": "np.inf",
"int": "int",
"ln": "np.log",
"log10": "np.log10",
"max": "max",
"min": "min",
"pi": "np.pi",
"sqrt": "np.sqrt",

# ===
# 3.5.2 Statistical Functions
# http://docs.oasis-open.org/xmile/xmile/v1.0/csprd01/xmile-v1.0-csprd01.html#_Toc398039981
# ===

"exprnd": "np.random.exponential",
"lognormal": "np.random.lognormal",
"normal": "np.random.normal",
"poisson": "np.random.poisson",
"random": "np.random.rand",

# ====
# 3.5.3 Delay Functions
# http://docs.oasis-open.org/xmile/xmile/v1.0/csprd01/xmile-v1.0-csprd01.html#_Toc398039982
# ====

# "delay" !TODO!
# "delay1" !TODO!
# "delay2" !TODO!
# "delay3" !TODO!
# "delayn" !TODO!
# "forcst" !TODO!
# "smth1" !TODO!
# "smth3" !TODO!
# "smthn" !TODO!
# "trend" !TODO!

# ===
# 3.5.4 Test Input Functions
# http://docs.oasis-open.org/xmile/xmile/v1.0/csprd01/xmile-v1.0-csprd01.html#_Toc398039983
# ===

# !TODO!
# This function works with wrong way for XMILE specification: http://docs.oasis-open.org/xmile/xmile/v1.0/csprd01/xmile-v1.0-csprd01.html#_Toc398039983
# ----------------------------------------------------------------------------------------------
# PULSE: Generate a one-DT wide pulse at the given time
# Parameters: 2 or 3: (magnitude, first time[, interval])
# Without interval or when interval = 0, the PULSE is generated only once
# Example: PULSE(20, 12, 5) generates a pulse value of 20/DT at time 12, 17, 22, etc.
# ----------------------------------------------------------------------------------------------
"pulse": "functions.pulse"
"step": "functions.step",

# "ramp" !TODO!

# ===
# 3.5.5 Time Functions
# http://docs.oasis-open.org/xmile/xmile/v1.0/csprd01/xmile-v1.0-csprd01.html#_Toc398039984
# ===
# Should we include as function list or it provided by another way?

# "dt" !TODO!
# "starttime" !TODO!
# "stoptime" !TODO!
# "time" !TODO!

# ===
# 3.5.6 Miscellaneous Functions
# http://docs.oasis-open.org/xmile/xmile/v1.0/csprd01/xmile-v1.0-csprd01.html#_Toc398039985
# ===
"if_then_else": "functions.if_then_else",
"step": "functions.step", "pulse": "functions.pulse"
# "init" !TODO!
# "previous" !TODO!
# "self" !TODO!
}

prefix_operators = {
"not": " not ",
"-": "-", "+": " ",
"-": "-",
"+": " ",
}

infix_operators = {
"and": " and ", "or": " or ",
"=": "==", "<=": "<=", "<": "<", ">=": ">=", ">": ">", "<>": "!=",
"^": "**", "+": "+", "-": "-",
"*": "*", "/": "/", "mod": "%",
"and": " and ",
"or": " or ",
"=": "==",
"<=": "<=",
"<": "<",
">=": ">=",
">": ">",
"<>": "!=",
"^": "**",
"+": "+",
"-": "-",
"*": "*",
"/": "/",
"mod": "%",
}

builders = {}
Expand Down Expand Up @@ -79,6 +168,8 @@ def parse(self, text, context='eqn'):
If context is set to equation, lone identifiers will be parsed as calls to elements
If context is set to definition, lone identifiers will be cleaned and returned.
"""
# !TODO! Should remove the inline comments from `text` before parsing the grammar
# http://docs.oasis-open.org/xmile/xmile/v1.0/csprd01/xmile-v1.0-csprd01.html#_Toc398039973
self.ast = self.grammar.parse(text)
self.context = context
return self.visit(self.ast)
Expand Down

0 comments on commit d6ee6ee

Please sign in to comment.