Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 7 additions & 1 deletion src/evaluator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ function eval_AST(eq::BaseModelicaAnyEquation)
return equation
end

function eval_AST(annotation::BaseModelicaAnnotation)
# Annotations are metadata and don't produce equations
# For now, return nothing (they are parsed but ignored during evaluation)
return nothing
end

function eval_AST(eq::BaseModelicaSimpleEquation)
lhs = eval_AST(eq.lhs)
rhs = eval_AST(eq.rhs)
Expand Down Expand Up @@ -102,7 +108,7 @@ function eval_AST(model::BaseModelicaModel)
end
end

eqs = [eval_AST(eq) for eq in equations]
eqs = filter(x -> x !== nothing, [eval_AST(eq) for eq in equations])

#vars_and_pars = merge(Dict(vars .=> vars), Dict(pars .=> pars))
#println(vars_and_pars)
Expand Down
8 changes: 5 additions & 3 deletions src/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
BaseModelicaForEquation(index, equations)
BaseModelicaIfEquation(ifs, thens)
BaseModelicaAnyEquation(equation, description)
BaseModelicaAnnotation(annotation_content)
BaseModelicaForIndex(ident, expression)
BaseModelicaComposition(components, equations, initial_equations)
BaseModelicaLongClass(name, description, composition)
Expand Down Expand Up @@ -301,6 +302,7 @@ function BaseModelicaComposition(input_list)
push!(initial_equations, input)
elseif input isa BaseModelicaAnyEquation
push!(equations, input)
elseif input isa BaseModelicaAnnotation
end
end
BaseModelicaComposition(components, equations, initial_equations)
Expand Down Expand Up @@ -437,7 +439,7 @@ spc = Drop(Star(Space()))
subscript = (E":" > BMColon) | expression
array_subscripts.matcher = E"[" + subscript + Star(E"," + subscript) + E"]" |>
BaseModelicaArraySubscripts
annotation_comment = E"annotation" + class_modification
annotation_comment = E"annotation" + class_modification |> BaseModelicaAnnotation
comment.matcher = (string_comment + annotation_comment[0:1]) |> (x -> length(x) == 1 ? x[1] : BaseModelicaString(join([string(elem) for elem in x], " ")))

enumeration_literal = IDENT + comment
Expand All @@ -463,9 +465,9 @@ spc = Drop(Star(Space()))
statement = Delayed()
base_partition = Delayed()
composition = Star(decoration[0:1] + generic_element + E";" + spc) + spc +
Star((spc + e"equation" + spc + Star(spc + equation + E";" + spc)) |
Star((spc + e"equation" + spc + Star(spc + (equation | annotation_comment) + E";" + spc)) |
(e"initial equation" + spc +
Star(spc + initial_equation + E";" + spc)) |
Star(spc + (initial_equation | annotation_comment) + E";" + spc)) |
(e"initial"[0:1] + e"algorithm" + Star(statement + E";"))) +
(decoration[0:1] + E"external" + language_specification[0:1] + external_function_call[0:1] + annotation_comment[0:1] + E";")[0:1] +
Star(base_partition) + (annotation_comment + E";")[0:1] |>
Expand Down
14 changes: 14 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ if GROUP == "All" || GROUP == "Core"
unary_minus_test = only(PC.parse_one("-5", BM.arithmetic_expression))
@test unary_minus_test isa BM.BaseModelicaUnaryMinus
@test BM.eval_AST(unary_minus_test) == -5.0

# Test annotation parsing (issue #38)
annotation_test = only(PC.parse_one("annotation(experiment(StartTime = 0, StopTime = 2.0))", BM.annotation_comment))
@test annotation_test isa BM.BaseModelicaAnnotation
@test BM.eval_AST(annotation_test) === nothing

newton_path = joinpath(
dirname(dirname(pathof(BM))), "test", "testfiles", "NewtonCoolingBase.mo")
Expand All @@ -41,6 +46,15 @@ if GROUP == "All" || GROUP == "Core"
negative_system = BM.baseModelica_to_ModelingToolkit(negative_package)
@test negative_system isa ODESystem
@test parse_basemodelica("testfiles/NegativeVariable.mo") isa ODESystem

# Test experiment annotation parsing (issue #38)
experiment_path = joinpath(
dirname(dirname(pathof(BM))), "test", "testfiles", "Experiment.mo")
experiment_package = BM.parse_file(experiment_path)
@test experiment_package isa BM.BaseModelicaPackage
experiment_system = BM.baseModelica_to_ModelingToolkit(experiment_package)
@test experiment_system isa ODESystem
@test parse_basemodelica("testfiles/Experiment.mo") isa ODESystem
end
end
end
8 changes: 8 additions & 0 deletions test/testfiles/Experiment.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package 'Experiment'
model 'Experiment'
Real 'x';
equation
der('x') = 'x';
annotation(experiment(StartTime = 0, StopTime = 2.0, Tolerance = 1e-06, Interval = 0.004));
end 'Experiment';
end 'Experiment';
Loading