Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Waterloo Model CAST translation #671

Merged
merged 45 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
ca9cf3e
Added a method for reading instruction blocks
Nov 18, 2023
6827096
simplified the if statement parsing
Nov 18, 2023
9d47e48
Reordered function defs
Nov 18, 2023
dbef827
Unary operator is no longer a special case
Nov 18, 2023
b699786
Removed an outdated local variable
Nov 18, 2023
8f7d242
Added CAST translation for the MATLAB function def
Nov 18, 2023
c81b8a3
Moved a few more tokens into the done category
Nov 18, 2023
7d9fb0a
Added a data logging feature
Nov 18, 2023
944c363
Code cleanup
Nov 18, 2023
16debe8
Added better operator and call handlers
Nov 19, 2023
b312e22
Added a comment
Nov 19, 2023
4c30a04
Added a function test with an anonymous function argument
Nov 19, 2023
fb3df46
Removed redundant assignments in matrix and operator tests
Nov 19, 2023
e10e013
Added a method for reading instruction blocks
Nov 18, 2023
7b99982
simplified the if statement parsing
Nov 18, 2023
f5c4a75
Reordered function defs
Nov 18, 2023
eea66cc
Unary operator is no longer a special case
Nov 18, 2023
344719f
Removed an outdated local variable
Nov 18, 2023
467883a
Added CAST translation for the MATLAB function def
Nov 18, 2023
c223020
Moved a few more tokens into the done category
Nov 18, 2023
6eeb9a8
Added a data logging feature
Nov 18, 2023
bbb07a4
Code cleanup
Nov 18, 2023
b0e0ee0
Added better operator and call handlers
Nov 19, 2023
d7773fb
Added a comment
Nov 19, 2023
d9c51b0
Added a function test with an anonymous function argument
Nov 19, 2023
f580fa9
Removed redundant assignments in matrix and operator tests
Nov 19, 2023
1a6cae8
Merge branch 'jastier/function_defs' of github.com:ml4ai/skema into j…
Nov 19, 2023
390c8af
Changed function test names
Nov 19, 2023
cbe3eee
Added a test for the spread operator
Nov 19, 2023
78f0304
Added PEMDAS checking to the operator tests
Nov 19, 2023
60a1b41
Added operator assignment to the assignmen tests
Nov 19, 2023
2b24965
Added a test for commands without arguments
Nov 19, 2023
e245622
Removed two outdated methods
Nov 19, 2023
0a3a0df
Removed a debug method
Nov 19, 2023
96f7054
Added a CI test for reading file input
Nov 19, 2023
4f04226
Fixed a filename
Nov 19, 2023
2261931
Fixed a filename
Nov 19, 2023
1b4931a
Merge branch 'main' into jastier/function_defs
jastier Nov 20, 2023
7458f5e
Added some default values
Nov 20, 2023
e38c85e
Merge branch 'jastier/function_defs' of github.com:ml4ai/skema into j…
Nov 20, 2023
4731afc
Simplified the tokens file
Nov 20, 2023
e042c46
Replaced 'None' ModelIf values with empty lists
Nov 20, 2023
808833e
Cleaned up tests
Nov 20, 2023
12df9d8
Merge branch 'main' into jastier/function_defs
jastier Nov 20, 2023
4f01fd9
Merge branch 'jastier/function_defs' of github.com:ml4ai/skema into j…
Nov 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
268 changes: 128 additions & 140 deletions skema/program_analysis/CAST/matlab/matlab_to_cast.py

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions skema/program_analysis/CAST/matlab/tests/data/matlab.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
% matlab test file, do not delete
y = b
27 changes: 18 additions & 9 deletions skema/program_analysis/CAST/matlab/tests/test_assignment.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
from skema.program_analysis.CAST.matlab.tests.utils import (check, cast)
from skema.program_analysis.CAST2FN.model.cast import Assignment

# Test CAST from assignment
from skema.program_analysis.CAST2FN.model.cast import (
Assignment,
Operator
)

def test_boolean():
""" Test assignment of literal boolean types. """
# we translate these MATLAB keywords into capitalized strings for Python
nodes = cast("x = true; y = false")
check(nodes[0], Assignment(left = "x", right = "True"))
check(nodes[1], Assignment(left = "y", right = "False"))

def test_number_zero_integer():
""" Test assignment of integer and real numbers."""
nodes = cast("x = 0")
check(nodes[0], Assignment(left = "x", right = 0))
check(cast("x = 0")[0], Assignment(left = "x", right = 0))

def test_number_zero_real():
""" Test assignment of integer and real numbers."""
nodes = cast("y = 0.0")
check(nodes[0], Assignment(left = "y", right = 0.0))
check(cast("y = 0.0")[0], Assignment(left = "y", right = 0.0))

def test_number_nonzero():
""" Test assignment of integer and real numbers."""
nodes = cast("z = 1.8")
check(nodes[0], Assignment(left = "z", right = 1.8))
check(cast("z = 1.8")[0], Assignment(left = "z", right = 1.8))

def test_string():
""" Test assignment of single and double quoted strings."""
Expand All @@ -39,3 +38,13 @@ def test_identifier():
nodes = cast("x = y; r = x")
check(nodes[0], Assignment(left = 'x', right = 'y'))
check(nodes[1], Assignment(left = 'r', right = 'x'))

def test_operator():
""" Test assignment of operator"""
check(
cast("Vtot = V1PF+V1AZ;")[0],
Assignment(
left = "Vtot",
right = Operator(op = "+",operands = ["V1PF", "V1AZ"])
)
)
8 changes: 6 additions & 2 deletions skema/program_analysis/CAST/matlab/tests/test_command.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from skema.program_analysis.CAST.matlab.tests.utils import (check, cast)
from skema.program_analysis.CAST2FN.model.cast import (Call)
from skema.program_analysis.CAST2FN.model.cast import Call

def test_command():
""" Test the MATLAB command syntax elements"""

nodes = cast("clear all;")
check(nodes[0], Call(func = "clear", arguments=["all"]))

def test_command_no_args():
""" Test the MATLAB command syntax elements"""
nodes = cast("validate;")
check(nodes[0], Call(func = "validate", arguments=[]))
52 changes: 10 additions & 42 deletions skema/program_analysis/CAST/matlab/tests/test_conditional.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,22 @@

def test_if():
""" Test CAST from MATLAB 'if' conditional logic."""

source = """
if x == 5
y = 6
end
"""

check(
cast(source)[0],
ModelIf(
# if
expr = Operator(op = "==", operands = ["x", 5]),
# then
body = [Assignment(left="y", right = 6)]
body = [Assignment(left="y", right = 6)],
orelse = []
)
)

def test_if_else():
""" Test CAST from MATLAB 'if else' conditional logic."""

source = """
if x > 5
y = 6
Expand All @@ -36,18 +32,14 @@ def test_if_else():
foo = 'bar'
end
"""

check(
cast(source)[0],
ModelIf(
# if
expr = Operator(op = ">", operands = ["x", 5]),
# then
body = [
Assignment(left="y", right = 6),
Assignment(left="three", right = 3)
],
# else
orelse = [
Assignment(left="y", right = "x"),
Assignment(left="foo", right = "'bar'")
Expand All @@ -58,7 +50,6 @@ def test_if_else():

def test_if_elseif():
""" Test CAST from MATLAB 'if elseif else' conditional logic."""

source = """
if x >= 5
y = 6
Expand All @@ -69,30 +60,19 @@ def test_if_elseif():
check(
cast(source)[0],
ModelIf(
# if
expr = Operator(op = ">=", operands = ["x", 5]),
# then
body = [
Assignment(left="y", right = 6)
],
# else
orelse = [
ModelIf(
# if
expr = Operator(op = "<=", operands = ["x", 0]),
# then
body = [
Assignment(left="y", right = "x")
]
)
body = [Assignment(left="y", right = 6)],
orelse = [ModelIf(
expr = Operator(op = "<=", operands = ["x", 0]),
body = [Assignment(left="y", right = "x")],
orelse = [])
]
)
)


def test_if_elseif_else():
""" Test CAST from MATLAB 'if elseif else' conditional logic."""

source = """
if x > 5
a = 6
Expand All @@ -102,28 +82,16 @@ def test_if_elseif_else():
c = 0
end
"""

check(
cast(source)[0],
ModelIf(
# if
expr = Operator(op = ">", operands = ["x", 5]),
# then
body = [
Assignment(left="a", right = 6)
],
# else
body = [Assignment(left="a", right = 6)],
orelse = [
ModelIf(
# if
expr = Operator(op = ">", operands = ["x", 0]),
# then
body = [
Assignment(left="b", right = "x")
],
orelse = [
Assignment(left="c", right = 0)
]
body = [Assignment(left="b", right = "x")],
orelse = [Assignment(left="c", right = 0)]
)
]
)
Expand Down
11 changes: 11 additions & 0 deletions skema/program_analysis/CAST/matlab/tests/test_file_ingest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from skema.program_analysis.CAST.matlab.matlab_to_cast import MatlabToCast
from skema.program_analysis.CAST.matlab.tests.utils import (check, cast)
from skema.program_analysis.CAST2FN.model.cast import Assignment

def test_file_ingest():
""" Test the ability of the CAST translator to read from a file"""
filename = "skema/program_analysis/CAST/matlab/tests/data/matlab.m"
cast = MatlabToCast(source_path = filename).out_cast
module = cast.nodes[0]
nodes = module.body
check(nodes[0], Assignment(left = "y", right = "b"))
62 changes: 30 additions & 32 deletions skema/program_analysis/CAST/matlab/tests/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,68 @@
from skema.program_analysis.CAST2FN.model.cast import (
Assignment,
Call,
FunctionDef,
Operator
)

# Test CAST from functions
def no_test_function_definition():
def test_definition():
""" Test function definition """
source = """
function both = add_them(x, y)
both = x + y
end
"""
nodes = cast(source)
assert len(nodes) == 1

def test_literal_args():
""" Test function call with literal arguments """
nodes = cast("x = both(3, 5)")
nodes = cast(source)[0]
check(
nodes[0],
Assignment(
left = "x",
right = Call(
func = "both",
arguments = [3, 5]
)
cast(source)[0],
FunctionDef(
name = "both",
func_args = ["x", "y"],
body = [
Assignment(
left = "both",
right = Operator (op = "+", operands = ["x", "y"])
)
]
)
)

def test_inline_operator_args():
def test_call_with_literal_args():
""" Test function call with literal arguments """
check(cast("both(3, 5)")[0], Call(func = "both", arguments = [3, 5]))

def test_call_with_operator_args():
""" Test function call with Operator arguments """
nodes = cast("foo(x < a, -6)")
check(
nodes[0],
Call(
func = "foo",
arguments = [
Operator (
op = "<",
operands = ["x", "a"]
),
Operator (
op = "-",
operands = [6]
),
Operator(op = "<", operands = ["x", "a"]),
Operator(op = "-", operands = [6]),
]
)
)

def test_nested_calls():
def test_call_with_call_args():
""" Test function call with matrix of function call arguments """
nodes = cast("foo(bar(x), baz(y))")
check(
nodes[0],
Call(
func = "foo",
arguments = [
Call (
func = "bar",
arguments = ["x"]
),
Call (
func = "baz",
arguments = ["y"]
),
Call (func = "bar", arguments = ["x"]),
Call (func = "baz", arguments = ["y"]),
]
)
)

def test_call_with_anonymous_call_arg():
nodes = cast("foo{x}(y)")
check(
nodes[0],
Call(func = Call (func = "foo", arguments = ["x"]),arguments = ["y"])
)
24 changes: 8 additions & 16 deletions skema/program_analysis/CAST/matlab/tests/test_matrix.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
from skema.program_analysis.CAST.matlab.tests.utils import (check, cast)
from skema.program_analysis.CAST2FN.model.cast import (
Assignment,
LiteralValue
)

# Test CAST using matrices
def test_matrix_empty():
""" Test assignment of empty matrices."""
nodes = cast("x = [];")
check(nodes[0], Assignment(left = 'x', right = []))
def test_empty():
""" Test empty matrix."""
check(cast("[];")[0], [])

def test_matrix_boolean():
""" Test assignment of empty matrices."""
nodes = cast("x = [true false];")
check(nodes[0], Assignment(left = 'x', right = ["True", "False"]))
def test_booleans():
""" Test matrix with MATLAB booleans."""
check(cast("[true false];")[0], ["True", "False"])

def test_matrix_values():
def test_values():
""" Test assignment 1 dimensional matrix value."""
nodes = cast("x = [1 x 'Bob' ]")
assert len(nodes) == 1
check(nodes[0], Assignment(left = 'x', right = [1, 'x', "'Bob'"]))
check(cast("[1 x 'Bob' ]")[0], [1, 'x', "'Bob'"])

Loading
Loading