diff --git a/src/evaluator.jl b/src/evaluator.jl index db6eb3c..f92f008 100644 --- a/src/evaluator.jl +++ b/src/evaluator.jl @@ -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) @@ -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) diff --git a/src/parser.jl b/src/parser.jl index 398afb6..ba3ebb1 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -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) @@ -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) @@ -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 @@ -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] |> diff --git a/test/runtests.jl b/test/runtests.jl index f88bbcd..ff5b29c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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") @@ -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 diff --git a/test/testfiles/Experiment.mo b/test/testfiles/Experiment.mo new file mode 100644 index 0000000..1ed8bda --- /dev/null +++ b/test/testfiles/Experiment.mo @@ -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'; \ No newline at end of file