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

Multi period expansion planning #20

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ pet-mit
sylvlecl
tbittar
vargastat
Yann-Temudjin
78 changes: 78 additions & 0 deletions grammar/Expr.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright (c) 2024, RTE (https://www.rte-france.com)

See AUTHORS.txt

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.

SPDX-License-Identifier: MPL-2.0

This file is part of the Antares project.
*/

grammar Expr;

/* To match the whole input */
fullexpr: expr EOF;

expr
: atom # unsignedAtom
| IDENTIFIER '.' IDENTIFIER # portField
| '-' expr # negation
| '(' expr ')' # expression
| expr op=('/' | '*') expr # muldiv
| expr op=('+' | '-') expr # addsub
| expr COMPARISON expr # comparison
| IDENTIFIER '(' expr ')' # function
| IDENTIFIER '[' shift (',' shift)* ']' # timeShift
| IDENTIFIER '[' expr (',' expr )* ']' # timeIndex
| IDENTIFIER '[' shift1=shift '..' shift2=shift ']' # timeShiftRange
| IDENTIFIER '[' expr '..' expr ']' # timeRange
;

atom
: NUMBER # number
| IDENTIFIER # identifier
;

// a shift is required to be either "t" or "t + ..." or "t - ..."
// Note: simply defining it as "shift: TIME ('+' | '-') expr" won't work
// because the minus sign will not have the expected precedence:
// "t - d + 1" would be equivalent to "t - (d + 1)"
shift: TIME shift_expr?;

// Because the shift MUST start with + or -, we need
// to differentiate it from generic "expr".
// A shift expression can only be extended to the right by a
// "right_expr" which cannot start with a + or -,
// unlike shift_expr itself.
// TODO: the grammar is still a little weird, because we
// allow more things in the "expr" parts of those
// shift expressions than on their left-most part
// (port fields, nested time shifts and so on).
shift_expr
: shift_expr op=('*' | '/') right_expr # shiftMuldiv
| shift_expr op=('+' | '-') right_expr # shiftAddsub
| op=('+' | '-') atom # signedAtom
| op=('+' | '-') '(' expr ')' # signedExpression
;

right_expr
: right_expr op=('/' | '*') right_expr # rightMuldiv
| '(' expr ')' # rightExpression
| atom # rightAtom
;


fragment DIGIT : [0-9] ;
fragment CHAR : [a-zA-Z_];
fragment CHAR_OR_DIGIT : (CHAR | DIGIT);

NUMBER : DIGIT+ ('.' DIGIT+)?;
TIME : 't';
IDENTIFIER : CHAR CHAR_OR_DIGIT*;
COMPARISON : ( '=' | '>=' | '<=' );

WS: (' ' | '\t' | '\r'| '\n') -> skip;
17 changes: 17 additions & 0 deletions grammar/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Expression grammar definition

[Expr.g4](Expr.g4) defines the grammar for mathematical expressions
use for defining constraints, objective, etc.

[ANTLR](https://www.antlr.org) needs to be used to generate the associated
parser code, which must be written to [andromede.expression.parsing.antlr](/src/andromede/expression/parsing/antlr)
package. **No other files are expected to be present in that package**.

To achieve this you may use the provided `generate-parser.sh` script after having installed
antlr4-tools (`pip install -r requirements-dev.txt` in root directory).

You may also, for example, use the ANTLR4 PyCharm plugin.

We use the visitor and not the listener in order to translate ANTLR AST
into our own AST, so the options `-visitor` and `-no-listener` need to
to be used.
5 changes: 5 additions & 0 deletions grammar/generate-parser.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

script_file=$(readlink -f -- "$0")
script_dir=$(dirname -- "${script_file}")
antlr4 -Dlanguage=Python3 -Werror -no-listener -visitor -o ${script_dir}/../src/andromede/expression/parsing/antlr Expr.g4
2 changes: 1 addition & 1 deletion models-design/model_library/stock_final_level.drawio
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<mxCell id="48" value="r(h+1) = r(h) + u_in(h) - u_out(h)" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;spacingLeft=4;spacingRight=4;overflow=hidden;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;rotatable=0;whiteSpace=wrap;html=1;" parent="39" vertex="1">
<mxGeometry y="270" width="270" height="30" as="geometry"/>
</mxCell>
<mxCell id="49" value="flow_s, flow_c, flow_out : Flow" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;spacingLeft=4;spacingRight=4;overflow=hidden;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;rotatable=0;whiteSpace=wrap;html=1;" parent="39" vertex="1">
<mxCell id="49" value="flow_s, flow_in, flow_out : Flow" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;spacingLeft=4;spacingRight=4;overflow=hidden;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;rotatable=0;whiteSpace=wrap;html=1;" parent="39" vertex="1">
<mxGeometry y="300" width="270" height="30" as="geometry"/>
</mxCell>
<mxCell id="50" value="flow_s.f = u_out - u_in" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;spacingLeft=4;spacingRight=4;overflow=hidden;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;rotatable=0;whiteSpace=wrap;html=1;" parent="39" vertex="1">
Expand Down
Loading
Loading