Commit f69c8dc9 authored by Thierry Martinez's avatar Thierry Martinez

Conditionals over events

parent 73aaf75a
......@@ -86,7 +86,8 @@ numerical_simulation(Time) :-
[
numerical_simulation:variable/2,
numerical_simulation:equation/2,
numerical_simulation:parameter/2
numerical_simulation:parameter/2,
numerical_simulation:conditional_event/1
],
(
numerical_simulation:solve(
......@@ -110,6 +111,10 @@ numerical_simulation(Time) :-
:- dynamic(parameter/2).
:- dynamic(conditional_event/1).
solve(
Time, Method, ErrorEpsilonAbsolute, ErrorEpsilonRelative, InitialStepSize,
MaximumStepSize, Precision
......@@ -197,12 +202,15 @@ gather_initial_parameter_values(InitialParameterValues) :-
gather_events(Events) :-
findall(
(ConditionIndexed -> ParameterIndex = ValueIndexed),
Event,
(
item([kind: event, item: event(Condition, Parameter = Value)]),
convert_condition(Condition, ConditionIndexed),
parameter(Parameter, ParameterIndex),
convert_expression(Value, ValueIndexed)
convert_expression(Value, ValueIndexed),
Event = (ConditionIndexed -> ParameterIndex = ValueIndexed)
;
conditional_event(Event)
),
Events
).
......@@ -262,6 +270,19 @@ convert_name(Parameter, p(ParameterIndex)) :-
).
convert_expression(if Condition then IfTrue else IfFalse, IndexedExpression) :-
!,
count(parameter_counter, ParameterIndex),
convert_condition(Condition, IndexedCondition),
assertz(conditional_event(IndexedCondition -> ParameterIndex = 1)),
assertz(conditional_event(not IndexedCondition -> ParameterIndex = 0)),
convert_expression(IfTrue, IndexedIfTrue),
convert_expression(IfFalse, IndexedIfFalse),
IndexedExpression =
p(ParameterIndex) * IndexedIfTrue
+ (1 - p(ParameterIndex)) * IndexedIfFalse.
convert_expression(Expression, IndexedExpression) :-
grammar_map(
arithmetic_expression,
......
......@@ -16,4 +16,10 @@ test('events') :-
command(present(a)),
numerical_simulation(20).
test('conditional') :-
clear_model,
command(if a < 0.5 then 'MA'(2) else 'MA'(1) for a => b),
command(present(a)),
numerical_simulation(20).
:- end_tests(numerical_simulation).
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment