Skip to content

Commit cfbfcff

Browse files
authored
Merge pull request #46 from jClugstor/parse_annotations
Add support for parsing annotations
2 parents 861a0e4 + 2c09148 commit cfbfcff

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

src/evaluator.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ function eval_AST(eq::BaseModelicaAnyEquation)
4949
return equation
5050
end
5151

52+
function eval_AST(annotation::BaseModelicaAnnotation)
53+
# Annotations are metadata and don't produce equations
54+
# For now, return nothing (they are parsed but ignored during evaluation)
55+
return nothing
56+
end
57+
5258
function eval_AST(eq::BaseModelicaSimpleEquation)
5359
lhs = eval_AST(eq.lhs)
5460
rhs = eval_AST(eq.rhs)
@@ -102,7 +108,7 @@ function eval_AST(model::BaseModelicaModel)
102108
end
103109
end
104110

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

107113
#vars_and_pars = merge(Dict(vars .=> vars), Dict(pars .=> pars))
108114
#println(vars_and_pars)

src/parser.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
BaseModelicaForEquation(index, equations)
2121
BaseModelicaIfEquation(ifs, thens)
2222
BaseModelicaAnyEquation(equation, description)
23+
BaseModelicaAnnotation(annotation_content)
2324
BaseModelicaForIndex(ident, expression)
2425
BaseModelicaComposition(components, equations, initial_equations)
2526
BaseModelicaLongClass(name, description, composition)
@@ -301,6 +302,7 @@ function BaseModelicaComposition(input_list)
301302
push!(initial_equations, input)
302303
elseif input isa BaseModelicaAnyEquation
303304
push!(equations, input)
305+
elseif input isa BaseModelicaAnnotation
304306
end
305307
end
306308
BaseModelicaComposition(components, equations, initial_equations)
@@ -437,7 +439,7 @@ spc = Drop(Star(Space()))
437439
subscript = (E":" > BMColon) | expression
438440
array_subscripts.matcher = E"[" + subscript + Star(E"," + subscript) + E"]" |>
439441
BaseModelicaArraySubscripts
440-
annotation_comment = E"annotation" + class_modification
442+
annotation_comment = E"annotation" + class_modification |> BaseModelicaAnnotation
441443
comment.matcher = (string_comment + annotation_comment[0:1]) |> (x -> length(x) == 1 ? x[1] : BaseModelicaString(join([string(elem) for elem in x], " ")))
442444

443445
enumeration_literal = IDENT + comment
@@ -463,9 +465,9 @@ spc = Drop(Star(Space()))
463465
statement = Delayed()
464466
base_partition = Delayed()
465467
composition = Star(decoration[0:1] + generic_element + E";" + spc) + spc +
466-
Star((spc + e"equation" + spc + Star(spc + equation + E";" + spc)) |
468+
Star((spc + e"equation" + spc + Star(spc + (equation | annotation_comment) + E";" + spc)) |
467469
(e"initial equation" + spc +
468-
Star(spc + initial_equation + E";" + spc)) |
470+
Star(spc + (initial_equation | annotation_comment) + E";" + spc)) |
469471
(e"initial"[0:1] + e"algorithm" + Star(statement + E";"))) +
470472
(decoration[0:1] + E"external" + language_specification[0:1] + external_function_call[0:1] + annotation_comment[0:1] + E";")[0:1] +
471473
Star(base_partition) + (annotation_comment + E";")[0:1] |>

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ if GROUP == "All" || GROUP == "Core"
2424
unary_minus_test = only(PC.parse_one("-5", BM.arithmetic_expression))
2525
@test unary_minus_test isa BM.BaseModelicaUnaryMinus
2626
@test BM.eval_AST(unary_minus_test) == -5.0
27+
28+
# Test annotation parsing (issue #38)
29+
annotation_test = only(PC.parse_one("annotation(experiment(StartTime = 0, StopTime = 2.0))", BM.annotation_comment))
30+
@test annotation_test isa BM.BaseModelicaAnnotation
31+
@test BM.eval_AST(annotation_test) === nothing
2732

2833
newton_path = joinpath(
2934
dirname(dirname(pathof(BM))), "test", "testfiles", "NewtonCoolingBase.mo")
@@ -41,6 +46,15 @@ if GROUP == "All" || GROUP == "Core"
4146
negative_system = BM.baseModelica_to_ModelingToolkit(negative_package)
4247
@test negative_system isa ODESystem
4348
@test parse_basemodelica("testfiles/NegativeVariable.mo") isa ODESystem
49+
50+
# Test experiment annotation parsing (issue #38)
51+
experiment_path = joinpath(
52+
dirname(dirname(pathof(BM))), "test", "testfiles", "Experiment.mo")
53+
experiment_package = BM.parse_file(experiment_path)
54+
@test experiment_package isa BM.BaseModelicaPackage
55+
experiment_system = BM.baseModelica_to_ModelingToolkit(experiment_package)
56+
@test experiment_system isa ODESystem
57+
@test parse_basemodelica("testfiles/Experiment.mo") isa ODESystem
4458
end
4559
end
4660
end

test/testfiles/Experiment.mo

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package 'Experiment'
2+
model 'Experiment'
3+
Real 'x';
4+
equation
5+
der('x') = 'x';
6+
annotation(experiment(StartTime = 0, StopTime = 2.0, Tolerance = 1e-06, Interval = 0.004));
7+
end 'Experiment';
8+
end 'Experiment';

0 commit comments

Comments
 (0)