modelica_rs/
lib.rs

1//! # Wrapping the Modelica language in Rust
2//!
3//! This crate is a wrapper for the Modelica language, used primarily for
4//! modeling physical systems.
5
6/// Most declared blocks in Modelica are classes
7pub trait ModelicaClass {
8    fn extract_class_instances(raw_data: &str) -> Vec<impl ModelicaClass>;
9}
10
11/// A `package` holds a collection of Modelica entities.
12#[derive(Debug)]
13pub struct ModelicaPackage {
14    pub name: String,
15}
16
17/// A `type` may only be predefined types, enumerations, array of `type`, or classes extending from `type`
18pub struct ModelicaType {
19    pub name: String,
20}
21
22/// A `model` is a class that defines a set of variables that are connected to other connectors or to variables outside the model.
23///
24/// A model may also contain equations, algorithm sections, and initial
25/// equations.
26pub struct ModelicaModel {
27    pub name: String,
28    pub content: String,
29    pub connectors: Vec<ModelicaConnector>,
30}
31
32impl ModelicaModel {
33    pub fn new(name: String, content: String) -> Self {
34        ModelicaModel {
35            name,
36            content,
37            connectors: Vec::new(),
38        }
39    }
40}
41
42/// A `block` is a class that defines a set of variables that are connected to other connectors or to variables outside the model.
43///
44/// A block may also contain equations, algorithm sections, and initial
45/// equations.
46#[derive(Debug)]
47pub struct ModelicaBlock {
48    pub name: String,
49    pub content: String,
50    pub connectors: Vec<ModelicaConnector>,
51}
52
53impl ModelicaBlock {
54    pub fn new(name: String, content: String) -> Self {
55        ModelicaBlock {
56            name,
57            content,
58            connectors: Vec::new(),
59        }
60    }
61}
62
63/// A `connector` is a class that defines a set of variables that are connected to other connectors or to
64/// variables outside the model.
65///
66/// A connector may also contain equations, algorithm sections, and initial
67/// equations.
68#[derive(Debug)]
69pub struct ModelicaConnector {
70    pub name: String,
71    pub r#type: String,
72}
73
74/// Content ignored by the Modelica translator
75///
76/// Text on a line following the // character pair is ignored by the Modelica translator.
77/// Also, text located between the character pairs /* and */ is ignored by the Modelica translator.
78pub struct ModelicaComment {
79    pub value: String,
80}
81
82pub enum ModelicaAccessControl {
83    Public,
84    Protected,
85}
86
87/// Reserved Modelica keywords
88#[allow(dead_code)]
89pub enum ModelicaKeyword {
90    Algorithm,
91    And,
92    Annotation,
93    Block,
94    Break,
95    Class,
96    Connect,
97    Connector,
98    Constant,
99    ConstrainedBy,
100    Der,
101    Discrete,
102    Each,
103    Else,
104    ElseIf,
105    ElseWhen,
106    Encapsulated,
107    End,
108    Enumeration,
109    Equation,
110    Expandable,
111    Extends,
112    External,
113    False,
114    Final,
115    Flow,
116    For,
117    Function,
118    If,
119    Import,
120    Impure,
121    In,
122    Initial,
123    Inner,
124    Input,
125    Loop,
126    Model,
127    Not,
128    Operator,
129    Or,
130    Outer,
131    Output,
132    Package,
133    Parameter,
134    Partial,
135    Protected,
136    Public,
137    Pure,
138    Record,
139    Redeclare,
140    Replaceable,
141    Return,
142    Stream,
143    Time,
144    Then,
145    True,
146    Type,
147    When,
148    While,
149    Within,
150}
151
152/// Reserved Modelica prefixes
153#[allow(dead_code)]
154pub enum ModelicaPrefix {
155    Flow,
156    Stream,
157    Discrete,
158    Parameter,
159    Constant,
160    Input,
161    Output,
162}
163
164#[allow(dead_code)]
165fn get_keyword_str(keyword: ModelicaKeyword) -> &'static str {
166    match keyword {
167        ModelicaKeyword::Algorithm => "algorithm",
168        ModelicaKeyword::And => "and",
169        ModelicaKeyword::Annotation => "annotation",
170        ModelicaKeyword::Block => "block",
171        ModelicaKeyword::Break => "break",
172        ModelicaKeyword::Class => "class",
173        ModelicaKeyword::Connect => "connect",
174        ModelicaKeyword::Connector => "connector",
175        ModelicaKeyword::Constant => "constant",
176        ModelicaKeyword::ConstrainedBy => "constrainedby",
177        ModelicaKeyword::Der => "der",
178        ModelicaKeyword::Discrete => "discrete",
179        ModelicaKeyword::Each => "each",
180        ModelicaKeyword::Else => "else",
181        ModelicaKeyword::ElseIf => "elseif",
182        ModelicaKeyword::ElseWhen => "elsewhen",
183        ModelicaKeyword::Encapsulated => "encapsulated",
184        ModelicaKeyword::End => "end",
185        ModelicaKeyword::Enumeration => "enumeration",
186        ModelicaKeyword::Equation => "equation",
187        ModelicaKeyword::Expandable => "expandable",
188        ModelicaKeyword::Extends => "extends",
189        ModelicaKeyword::External => "external",
190        ModelicaKeyword::False => "false",
191        ModelicaKeyword::Final => "final",
192        ModelicaKeyword::Flow => "flow",
193        ModelicaKeyword::For => "for",
194        ModelicaKeyword::Function => "function",
195        ModelicaKeyword::If => "if",
196        ModelicaKeyword::Import => "import",
197        ModelicaKeyword::Impure => "impure",
198        ModelicaKeyword::In => "in",
199        ModelicaKeyword::Initial => "initial",
200        ModelicaKeyword::Inner => "inner",
201        ModelicaKeyword::Input => "input",
202        ModelicaKeyword::Loop => "loop",
203        ModelicaKeyword::Model => "model",
204        ModelicaKeyword::Not => "not",
205        ModelicaKeyword::Operator => "operator",
206        ModelicaKeyword::Or => "or",
207        ModelicaKeyword::Outer => "outer",
208        ModelicaKeyword::Output => "output",
209        ModelicaKeyword::Package => "package",
210        ModelicaKeyword::Parameter => "parameter",
211        ModelicaKeyword::Partial => "partial",
212        ModelicaKeyword::Protected => "protected",
213        ModelicaKeyword::Public => "public",
214        ModelicaKeyword::Pure => "pure",
215        ModelicaKeyword::Record => "record",
216        ModelicaKeyword::Redeclare => "redeclare",
217        ModelicaKeyword::Replaceable => "replaceable",
218        ModelicaKeyword::Return => "return",
219        ModelicaKeyword::Stream => "stream",
220        ModelicaKeyword::Time => "time",
221        ModelicaKeyword::Then => "then",
222        ModelicaKeyword::True => "true",
223        ModelicaKeyword::Type => "type",
224        ModelicaKeyword::When => "when",
225        ModelicaKeyword::While => "while",
226        ModelicaKeyword::Within => "within",
227    }
228}
229
230#[allow(dead_code)]
231fn get_keyword(keyword: &str) -> Option<ModelicaKeyword> {
232    match keyword {
233        "algorithm" => Some(ModelicaKeyword::Algorithm),
234        "and" => Some(ModelicaKeyword::And),
235        "annotation" => Some(ModelicaKeyword::Annotation),
236        "block" => Some(ModelicaKeyword::Block),
237        "break" => Some(ModelicaKeyword::Break),
238        "class" => Some(ModelicaKeyword::Class),
239        "connect" => Some(ModelicaKeyword::Connect),
240        "connector" => Some(ModelicaKeyword::Connector),
241        "constant" => Some(ModelicaKeyword::Constant),
242        "constrainedby" => Some(ModelicaKeyword::ConstrainedBy),
243        "der" => Some(ModelicaKeyword::Der),
244        "discrete" => Some(ModelicaKeyword::Discrete),
245        "each" => Some(ModelicaKeyword::Each),
246        "else" => Some(ModelicaKeyword::Else),
247        "elseif" => Some(ModelicaKeyword::ElseIf),
248        "elsewhen" => Some(ModelicaKeyword::ElseWhen),
249        "encapsulated" => Some(ModelicaKeyword::Encapsulated),
250        "end" => Some(ModelicaKeyword::End),
251        "enumeration" => Some(ModelicaKeyword::Enumeration),
252        "equation" => Some(ModelicaKeyword::Equation),
253        "expandable" => Some(ModelicaKeyword::Expandable),
254        "extends" => Some(ModelicaKeyword::Extends),
255        "external" => Some(ModelicaKeyword::External),
256        "false" => Some(ModelicaKeyword::False),
257        "final" => Some(ModelicaKeyword::Final),
258        "flow" => Some(ModelicaKeyword::Flow),
259        "for" => Some(ModelicaKeyword::For),
260        "function" => Some(ModelicaKeyword::Function),
261        "if" => Some(ModelicaKeyword::If),
262        "import" => Some(ModelicaKeyword::Import),
263        "impure" => Some(ModelicaKeyword::Impure),
264        "in" => Some(ModelicaKeyword::In),
265        "initial" => Some(ModelicaKeyword::Initial),
266        "inner" => Some(ModelicaKeyword::Inner),
267        "input" => Some(ModelicaKeyword::Input),
268        "loop" => Some(ModelicaKeyword::Loop),
269        "model" => Some(ModelicaKeyword::Model),
270        "not" => Some(ModelicaKeyword::Not),
271        "operator" => Some(ModelicaKeyword::Operator),
272        "or" => Some(ModelicaKeyword::Or),
273        "outer" => Some(ModelicaKeyword::Outer),
274        "output" => Some(ModelicaKeyword::Output),
275        "package" => Some(ModelicaKeyword::Package),
276        "parameter" => Some(ModelicaKeyword::Parameter),
277        "partial" => Some(ModelicaKeyword::Partial),
278        "protected" => Some(ModelicaKeyword::Protected),
279        "public" => Some(ModelicaKeyword::Public),
280        "pure" => Some(ModelicaKeyword::Pure),
281        "record" => Some(ModelicaKeyword::Record),
282        "redeclare" => Some(ModelicaKeyword::Redeclare),
283        "replaceable" => Some(ModelicaKeyword::Replaceable),
284        "return" => Some(ModelicaKeyword::Return),
285        "stream" => Some(ModelicaKeyword::Stream),
286        "time" => Some(ModelicaKeyword::Time),
287        "then" => Some(ModelicaKeyword::Then),
288        "true" => Some(ModelicaKeyword::True),
289        "type" => Some(ModelicaKeyword::Type),
290        "when" => Some(ModelicaKeyword::When),
291        "while" => Some(ModelicaKeyword::While),
292        "within" => Some(ModelicaKeyword::Within),
293        _ => None,
294    }
295}
296
297/// Reserved Modelica operators
298#[rustfmt::skip]
299pub enum ModelicaOperator {
300    PostfixArrayIndex, // [] | arr[index]
301    PostfixAccess, // . |  obj.property
302    PostfixFunctionCall, // funcName (functionArguments ) | sin(4.36)
303    ArrayConstruction, // {expressions } | {2, 3}
304    HorizontalConcatenation, // [expressions ] | [5, 6]
305    VerticalConcatenation, // [expressions; expressions...] | [2, 3; 7, 8]
306    Exponentiation, // ^ | 2 ^ 3
307    Multiplicative, // * | 2 * 3
308    Divisive, // / | 2 / 3
309    ElementwiseMultiplicative, // .* | [1, 2; 3, 4] .* [2, 3; 5, 6]
310    ElementwiseDivisive, // ./ | [1, 2; 3, 4] ./ [2, 3; 5, 6]
311    Additive, // + | 1 + 2
312    Subtractive, // - | 2 - 1
313    AdditiveUnary, // + | +2
314    SubtractiveUnary, // - | -2
315    ArrayElementwiseAdditive, // .+ | [1, 2; 3, 4] .+ [2, 3; 5, 6]
316    ArrayElementwiseSubtractive, // .- | [1, 2; 3, 4] .- [2, 3; 5, 6]
317    RelationatLessThan, // < | 2 < 3
318    RelationatLessThanOrEqual, // <= | 2 <= 3
319    RelationatGreaterThan, // > | 2 > 3
320    RelationatGreaterThanOrEqual, // >= | 2 >= 3
321    RelationatEqual, // == | 2 == 3
322    RelationatNotEqual, // <> | 2 <> 3
323    UnaryNegation, // not expr | not b1
324    LogicalAnd, // and | b1 and b2
325    LogicalOr, // or | b1 or b2
326    ArrayRange, // expr : expr OR expr : expr : expr  | 1 : 5 OR start : step : stop
327    Conditional, // if expr then expr else expr | if b then 3 else x
328    NamedArgument, // ident = expr | x = 2.26
329    AbsoluteValue, // abs(expr) | abs(-2.26)
330    Sign, // sign(expr) | sign(-2.26)
331    SquareRoot, // sqrt(expr) | sqrt(2.26)
332    IntegerConvert, // Integer(e) | Integer(enum1)
333    EnumTypeName, // EnumTypeName(i) | EnumTypeName(3)
334    StringConvert, // String(..., <options>) | String(345) ; options are minimumLength, leftJustified, significantDigits
335    EventDivision, // div(expr, expr) | div(5, 2)
336    EventModulus, // mod(expr, expr) | mod(5, 2)
337    EventRemainder, // rem(expr, expr) | rem(5, 2)
338    EventCeiling, // ceil(expr) | ceil(2.26)
339    EventFloor, // floor(expr) | floor(2.26)
340    EventInteger, // integer(expr) | integer(2.26)
341    Sine, // sin(expr) | sin(2.26)
342    Cosine, // cos(expr) | cos(2.26)
343    Tangent, // tan(expr) | tan(2.26)
344    InverseSine, // asin(expr) | asin(2.26)
345    InverseCosine, // acos(expr) | acos(2.26)
346    InverseTangent, // atan(expr) | atan(2.26)
347    PrincipalValueInverseTangent, // atan2(expr, expr) | atan2(2.26, 3.14)
348    HyperbolicSine, // sinh(expr) | sinh(2.26)
349    HyperbolicCosine, // cosh(expr) | cosh(2.26)
350    HyperbolicTangent, // tanh(expr) | tanh(2.26)
351    ExponentialBaseE, // exp(expr) | exp(2.26)
352    NaturalBaseE, // log(expr) | log(2.26)
353    LogBase10, // log10(expr) | log10(2.26)
354    Derivative, // der(expr) | der(x)
355    TimeDelay, // delay(expr, delayTime, <delayMax>) | delay(x, 2)
356    Occurrences, // cardinality(c) | cardinality(?) ; DEPRECATED
357    HomotopyInitialization, // homotopy(actual, simplified) | homotopy(?)
358    SignDependentSlope, // semiLinear(x, k+, k-) | semiLinear(?)
359    InStream, // inStream(expr) | inStream(x)
360    ActualStream, // actualStream(expr) | actualStream(x)
361    VariableSpeedTransport, //spatialDistribution(...) | spatialDistribution(?)
362    InstanceName, // getInstanceName() | getInstanceName()
363    InitialPredicate, // initial() | initial()
364    TerminalPredicate, // terminal() | terminal()
365    NoEvent, // noEvent(expr) | noEvent(x)
366    Smooth, // smooth(p, expr) | smooth(3, x)
367    Pre, // pre(expr) | pre(x)
368    Edge, // edge(expr) | edge(x)
369    Change, // change(expr) | change(x)
370    ReInit, // reinit(x, expr) | reinit(x, 2)
371}