forked from modelica/ModelicaSpecification
-
Notifications
You must be signed in to change notification settings - Fork 2
/
inheritance.tex
1143 lines (970 loc) · 55.9 KB
/
inheritance.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\chapter{Inheritance, Modification, and Redeclaration}\label{inheritance-modification-and-redeclaration}
One of the major benefits of object-orientation is the ability to \emph{extend} the behavior and properties of an existing class.
The original class, known as the \willintroduce{base class}, is extended to create a more specialized version of that class, known as the \willintroduce{derived class}.
In this process, the data and behavior of the original class in the form of variable declarations, equations, and certain other contents are reused, or \emph{inherited}, by the derived class.
In fact, the inherited contents is copied from the superclass into the derived class, but before copying certain operations, such as type expansion, checking, and modification, are performed on the inherited contents when appropriate.
This chapter describes the inheritance concept in Modelica, together with the related concepts modification and redeclaration.
\section{Inheritance -- Extends Clause}\label{inheritance-extends-clause}
The class \lstinline!A! is called a \firstuse{base class} of \lstinline!B!, if \lstinline!B! extends \lstinline!A!.
The converse relation is then expressed as \lstinline!B! being a \firstuse{derived class} of \lstinline!A!, or as \lstinline!B! being \firstuse{derived from} \lstinline!A!.
This relation is specified by an \lstinline!extends!-clause in \lstinline!B! or in one of \lstinline!B!'s base classes.
A class inherits all elements from its base classes, and may modify all non-final elements inherited from base classes, as explained below.
The \lstinline!extends!-clause is used to specify inheritance from a base class into an (enclosing) class containing the \lstinline!extends!-clause.
It is an unnamed element of a class definition that uses a name and an optional modification to specify a base class of the class defined using the class definition.
The syntax of the \lstinline!extends!-clause is as follows:
\begin{lstlisting}[language=grammar]
extends-clause :
extends name [ class-modification ] [annotation]
\end{lstlisting}%
\indexinline{extends}
The name of the base class is looked up in the partially flattened enclosing class (\cref{enclosing-classes}) of the \lstinline!extends!-clause.
The found base class is flattened with a new environment and the partially flattened enclosing class of the \lstinline!extends!-clause.
The new environment is the result of merging
\begin{itemize}
\item
arguments of all enclosing class environments that match names in the flattened base class
\item
the optional \lstinline!class-modification! of the \lstinline!extends!-clause
\end{itemize}
in that order.
\begin{example}
\begin{lstlisting}[language=modelica]
class A
parameter Real a, b;
end A;
class B
extends A(b = 2);
end B;
class C
extends B(a = 1);
end C;
\end{lstlisting}
\end{example}
The elements of the flattened base class become elements of the flattened enclosing class, and are added at the place of the \lstinline!extends!-clause: specifically components and classes, the equation sections, algorithm sections, optional \lstinline!external!-clause, and the contents of the annotation at the end of the class, but excluding \lstinline!import!-clauses.
\begin{nonnormative}
From the example above we get the following flattened class:
\begin{lstlisting}[language=modelica]
class Cinstance
parameter Real a = 1;
parameter Real b = 2;
end Cinstance;
\end{lstlisting}
The ordering of the merging rules ensures that, given classes \lstinline!A! and \lstinline!B! defined above,
\begin{lstlisting}[language=modelica]
class C2
B bcomp(b = 3);
end C2;
\end{lstlisting}
yields an instance with \lstinline!bcomp.b = 3!, which overrides \lstinline!b = 2!.
\end{nonnormative}
The declaration elements of the flattened base class shall either:
\begin{itemize}
\item
Not already exist in the partially flattened enclosing class
(i.e., have different names).
\item
The new element is a long form of redeclare or uses the \lstinline!class extends A! syntax, see \cref{redeclaration}.
\item
Be exactly identical to any element of the flattened enclosing class
with the same name and the same level of protection (public or
protected) and same contents. In this case, the first element in order
(can be either inherited or local) is kept. It is recommended to give
a warning for this case; unless it can be guaranteed that the
identical contents will behave in the same way.
\end{itemize}
Otherwise the model is incorrect.
\begin{nonnormative}
Clarifiying order:
\begin{lstlisting}[language=modelica]
function A
input Real a;
input Real b;
end A;
function B
extends A;
input Real a;
end B;
// The inputs of B are {a, b} in that order; the "input Real a;" is ignored.
\end{lstlisting}
\end{nonnormative}
Equations of the flattened base class that are syntactically equivalent
to equations in the flattened enclosing class are discarded. This
feature is deprecated, and it is recommended to give a warning when
discarding them and for the future give a warning about all forms of
equivalent equations due to inheritance.
\begin{nonnormative}
Equations that are mathematically equivalent but not syntactically equivalent are not discarded, hence yield an overdetermined system of equations.
\end{nonnormative}
\subsection{Multiple Inheritance}\label{multiple-inheritance}
Multiple inheritance is possible since multiple \lstinline!extends!-clauses can be present in a class.
\begin{nonnormative}
As stated in \cref{the-inherited-contents-of-the-element}, it is illegal for an \lstinline!extends!-clause to influence the lookup of the class name of any \lstinline!extends!-clause in the same class definition.
\end{nonnormative}
\subsection{Inheritance of Protected and Public Elements}\label{inheritance-of-protected-and-public-elements}
If an \lstinline!extends!-clause is used under the \lstinline!protected! heading, all elements of the base class become protected elements of the current class.
If an \lstinline!extends!-clause is a public element, all elements of the base class are inherited with their own protection.
The eventual headings \lstinline!protected! and \lstinline!public! from the base class do not affect the consequent elements of the current class (i.e., headings \lstinline!protected! and \lstinline!public! are not inherited).
\subsection{Restrictions on the Kind of Base Class}\label{restrictions-on-the-kind-of-base-class}
Since specialized classes of different kinds have different properties, see \cref{specialized-classes}, only specialized classes that are \emph{in some sense compatible} to each other can be derived from each other via inheritance.
The following table shows which kind of specialized class can be used in an \lstinline!extends!-clause of another kind of specialized class (the grey cells mark the few exceptional cases, where a specialized class can be derived from a specialized class of another kind):
% The PDF contains an excessive amount of vertical space here, but it seems like a bad idea to try to make it go away
% by adding LaTeX spacing commands that will cause problems later when things move around.
\begin{center}
% LaTeXML does not handle resizebox, so don't use it for HTML.
\ifpdf\resizebox{\textwidth}{!}{\else\fi%
\begin{tabular}{|c||c|c|c|c|c|c|c|c|c|c|c|c|}
\hline
& \multicolumn{12}{c|}{\tablehead{Base Class}} \\
\hline
\tablehead{Derived} & \multirow{2}{*}{\lstinline!package!} & \multirow{2}{*}{\lstinline!operator!} & \multirow{2}{*}{\lstinline!function!} & {\lstinline!operator!} & \multirow{2}{*}{\lstinline!type!} & \multirow{2}{*}{\lstinline!record!} & {\lstinline!operator!} & {\lstinline!expandable!} & \multirow{2}{*}{\lstinline!connector!} & \multirow{2}{*}{\lstinline!block!} & \multirow{2}{*}{\lstinline!model!} & \multirow{2}{*}{\lstinline!class!} \\
\tablehead{Class} & & & & {\lstinline!function!} & & & {\lstinline!record!} & {\lstinline!connector!} & & & & \\
\hline
\hline
{\lstinline!package!} & yes & & & & & & & & & & & \cellcolor{lightgray}yes \\
\hline
{\lstinline!operator!} & & yes & & & & & & & & & & \cellcolor{lightgray}yes \\
\hline
{\lstinline!function!} & & & yes & & & & & & & & & \cellcolor{lightgray}yes \\
\hline
{\lstinline!operator!} & & & \cellcolor{lightgray} & \multirow{2}{*}{yes} & & & & & & & & \cellcolor{lightgray} \\
{\lstinline!function!} & & & \multirow{-2}{*}{\cellcolor{lightgray}yes} & & & & & & & & & \multirow{-2}{*}{\cellcolor{lightgray}yes} \\
\hline
{\lstinline!type!} & & & & & yes & & & & & & & \cellcolor{lightgray}yes \\
\hline
{\lstinline!record!} & & & & & & yes & & & & & & \cellcolor{lightgray}yes \\
\hline
{\lstinline!operator!} & & & & & & & \multirow{2}{*}{yes} & & & & & \cellcolor{lightgray} \\
{\lstinline!record!} & & & & & & & & & & & & \multirow{-2}{*}{\cellcolor{lightgray}yes} \\
\hline
{\lstinline!expandable!} & & & & & & & & \multirow{2}{*}{yes} & & & & \cellcolor{lightgray} \\
{\lstinline!connector!} & & & & & & & & & & & & \multirow{-2}{*}{\cellcolor{lightgray}yes} \\
\hline
{\lstinline!connector!} & & & & & \cellcolor{lightgray}yes & \cellcolor{lightgray}yes & \cellcolor{lightgray}yes & & yes & & & \cellcolor{lightgray}yes \\
\hline
{\lstinline!block!} & & & & & & \cellcolor{lightgray}yes & & & & yes & & \cellcolor{lightgray}yes \\
\hline
{\lstinline!model!} & & & & & & \cellcolor{lightgray}yes & & & & \cellcolor{lightgray}yes & yes & \cellcolor{lightgray}yes \\
\hline
{\lstinline!class!} & & & & & & & & & & & & yes \\
\hline
\end{tabular}
% Close resizebox.
\ifpdf}\else\fi%
\end{center}
If a derived class is inherited from another type of specialized class,
then the result is a specialized class of the derived class type.
\begin{nonnormative}
For example, if a \lstinline!block! inherits from a \lstinline!record!, then the result is a \lstinline!block!.
\end{nonnormative}
All specialized classes can be derived from \lstinline!class!\indexinline{class}, provided that the resulting class fulfills the restriction of the specialized class.
A \lstinline!class! may only contain class definitions, annotations, and \lstinline!extends!-clauses (having any other contents is deprecated).
\begin{nonnormative}
It is recommended to use the most specific specialized class.
\end{nonnormative}
The specialized classes \lstinline!package!, \lstinline!operator!, \lstinline!function!,
\lstinline!type,! \lstinline!record!,
\lstinline!operator record!, and \lstinline!expandable connector! can only be derived from their
own kind and from \lstinline!class!.
\begin{nonnormative}
E.g.\ a package can only be base class for packages.
All other kinds of classes can use the \lstinline!import!-clause to use the contents of a package.
\end{nonnormative}
\begin{example}
\begin{lstlisting}[language=modelica]
record RecordA
$\ldots$
end RecordA;
package PackageA
$\ldots$
end PackageA;
package PackageB
extends PackageA; // fine
end PackageB;
model ModelA
extends RecordA; // fine
end ModelA;
model ModelB
extends PackageA; // error, inheritance not allowed
end ModelB;
\end{lstlisting}
\end{example}
\subsection{Require Transitively Non-Replaceable}\label{restrictions-on-base-classes-and-constraining-types-to-be-transitively-non-replaceable}\label{require-transitively-non-replaceable}
The class name used after \lstinline!extends! for base classes and for constraining classes must use a class reference considered transitively non-replaceable, see definition in \cref{transitively-non-replaceable}.
For a replaceable component declaration without \lstinline[language=grammar]!constraining-clause! the class must use a class reference considered transitively non-replaceable.
\begin{nonnormative}
The requirement to use a transitively non-replaceable name excludes the long form of redeclare, i.e.\ \lstinline!redeclare model extends M $\ldots$! where \lstinline!M! must be an inherited replaceable class.
\end{nonnormative}
\begin{nonnormative}
The rule for a replaceable component declaration without \lstinline[language=grammar]!constraining-clause! implies that constraining classes are always transitively non-replaceable -- both
if explicitly given or implicitly by the declaration.
\end{nonnormative}
\section{Modifications}\label{modifications}
A \firstuse{modification} is part of an element.
It modifies the instance generated by that element.
A modification contains \willintroduce{element modifications} (e.g., \lstinline!vcc(unit = "V") = 1000!) and \willintroduce{element-redeclarations} (e.g., \lstinline!redeclare type Voltage = Real(unit="V")!).
There are three kinds of constructs in the Modelica language in which modifications can occur:
\begin{itemize}
\item
variable declarations
\item
short class declarations
\item
\lstinline!extends!-clauses
\end{itemize}
A modifier modifies one or more declarations from a class by changing some aspect(s) of the declarations.
The most common kind of modifier just changes the \emph{default value} or the \lstinline!start!-attribute in a binding equation; the value and/or \lstinline!start!-attribute should be compatible with the variable according to \cref{type-compatible-expressions}.
An \firstuse{element modification} overrides the declaration equation in the class used by the instance generated by the modified element.
\begin{example}
Modifying the default \lstinline!start! value of the \lstinline!altitude! variable:
\begin{lstlisting}[language=modelica]
Real altitude(start = 59404);
\end{lstlisting}
\end{example}
A modification (e.g., \lstinline!C1 c1(x = 5)!) is called a \firstuse{modification equation}, if the modified variable (here: \lstinline!c1.x!) is a non-parameter variable.
\begin{nonnormative}
The modification equation is created, if the modified component (here: \lstinline!c1!) is also created (see \cref{class-declarations}). In most cases
a modification equation for a non-parameter variable requires that the variable was declared with a declaration equation, see \cref{balanced-models};
in those cases the declaration equation is replaced by the modification equation.
\end{nonnormative}
A more dramatic change is to modify the \emph{type} and/or the \emph{prefixes} and possibly the \emph{dimension sizes} of a declared element.
This kind of modification is called an \firstuse{element-redeclaration}\index{redeclaration!element} (\cref{redeclaration}) and requires the special keyword \lstinline!redeclare! to be used in the modifier in order to reduce the risk for accidental modeling errors.
In most cases a declaration that can be redeclared must include the prefix \lstinline!replaceable!\indexinline{replaceable} (\cref{redeclaration}).
The modifier value (and class for redeclarations) is found in the context in which the modifier occurs, see also \cref{simple-name-lookup}.
\begin{example}
Scope for modifiers:
\begin{lstlisting}[language=modelica]
model B
parameter Real x;
package Medium = Modelica.Media.PartialMedium;
end B;
model C
parameter Real x = 2;
package Medium = Modelica.Media.PartialMedium;
B b(x = x, redeclare package Medium = Medium);
// The 'x' and 'Medium' being modified are declared in the model B.
// The modifiers '= x' and '= Medium' are found in the model C.
end C;
model D
parameter Real x = 3;
package Medium = Modelica.Media.PartialMedium;
C c(b(x = x, redeclare package Medium = Medium));
// The 'x' and 'Medium' being modified are declared in the model B.
// The modifiers '= x' and '= Medium' are found in the model D.
end D;
\end{lstlisting}
\end{example}
When present, the description-string of a modifier overrides the existing description.
\subsection{Syntax of Modifications and Redeclarations}\label{syntax-of-modifications-and-redeclarations}
The syntax is defined in the grammar, \cref{modification}.
\subsection{Modification Environment}\label{modification-environment}
The \firstuse{modification environment} of a class contains arguments which modify elements of the class (e.g., parameter changes) when the class is flattened.
The modification environment is built by merging class modifications, where outer modifications override inner modifications.
\begin{nonnormative}
This should not be confused with \lstinline!inner outer! prefixes described in \cref{instance-hierarchy-name-lookup-of-inner-declarations}.
\end{nonnormative}
\subsection{Merging of Modifications}\label{merging-of-modifications}
Merging of modifiers means that outer modifiers override inner modifiers. The merging is hierarchical, and a value for an entire non-simple component overrides value modifiers for all components, and it is an error if this overrides a \lstinline!final! prefix for a component, or if value for a simple component would override part of the value of a non-simple component. When merging modifiers each modification keeps its own \lstinline!each! prefix.
\begin{example}
The following larger example demonstrates several aspects:
\begin{lstlisting}[language=modelica]
class C1
class C11
parameter Real x;
end C11;
end C1;
class C2
class C21
$\ldots$
end C21;
end C2;
class C3
extends C1;
C11 t(x = 3); // ok, C11 has been inherited from C1
C21 u; // ok, even though C21 is inherited below
extends C2;
end C3;
\end{lstlisting}
The following example demonstrates overriding part of non-simple component:
\begin{lstlisting}[language=modelica]
record A
parameter Real x;
parameter Real y;
end A;
model B
parameter A a = A(2, 3);
end B;
model C
B b1(a(x = 4)); // Error since attempting to override value for a.x when a has a value.
end C;
\end{lstlisting}
The modification environment of the declaration of \lstinline!t! is
(\lstinline!x = 3!). The modification environment is built by merging class modifications, as shown by:
\begin{lstlisting}[language=modelica]
class C1
parameter Real a;
end C1;
class C2
parameter Real b;
parameter Real c;
end C2;
class C3
parameter Real x1; // No default value
parameter Real x2 = 2; // Default value 2
parameter C1 x3; // No default value for x3.a
parameter C2 x4(b = 4); // x4.b has default value 4
parameter C1 x5(a = 5); // x5.a has default value 5
extends C1; // No default value for inherited element a
extends C2(b = 6, c = 77); // Inherited b has default value 6
end C3;
class C4
extends C3(x2 = 22, x3(a = 33), x4(c = 44), x5 = x3, a = 55, b = 66);
end C4;
\end{lstlisting}
Outer modifications override inner modifications, e.g., \lstinline!b = 66!
overrides the nested class modification of extends \lstinline!C2(b = 6)!.
This is known as merging of modifications: merge\lstinline!((b = 66), (b = 6))!
becomes \lstinline!(b = 66)!.
A flattening of class \lstinline!C4! will give an object with the following variables:
\begin{center}
\begin{tabular}{l|l}
\hline
\tablehead{Variable} & \tablehead{Default value}\\
\hline
\hline
{\lstinline!x1!} & \textit{none}\\ % Look of \emph won't be as expected inside example.
{\lstinline!x2!} & 22\\
{\lstinline!x3.a!} & 33\\
{\lstinline!x4.b!} & 4\\
{\lstinline!x4.c!} & 44\\
{\lstinline!x5.a!} & {\lstinline!x3.a!}\\
{\lstinline!a!} & 55\\
{\lstinline!b!} & 66\\
{\lstinline!c!} & 77\\
\hline
\end{tabular}
\end{center}
\end{example}
\subsection{Single Modification}\label{single-modification}
Two arguments of a modification shall not modify the same element, attribute, or description-string. When using qualified names the different qualified names starting with the same identifier are merged into one modifier. If a modifier with a qualified name has the \lstinline!each! or \lstinline!final! prefix, that prefix is only seen as applied to the final part of the name.
\begin{example}
\begin{lstlisting}[language=modelica]
class C1
Real x[3];
end C1;
class C2 = C1(x = ones(3), x = ones(3)); // Error: x designated twice
class C3
class C4
Real x;
end C4;
C4 a(final x.unit = "V", x.displayUnit = "mV", x = 5.0);
// Ok, different attributes designated (unit, displayUnit and value)
// identical to:
C4 b(x(final unit = "V", displayUnit = "mV") = 5.0));
end C3;
\end{lstlisting}
The following examples are incorrect:
\begin{lstlisting}[language=modelica]
m1(r = 1.5, r = 1.6) // Multiple modifier for r (its value)
m1(r = 1.5, r = 1.5) // Multiple modifier for r (its value) - even if identical
m1(r.start = 2, r(start = 3)) // Multiple modifier for r.start
m1(x.r = 1.5 "x", x.r(start = 2.0) "y")) // Multiple description-string for x.r
m1(r = R(), r(y = 2)) // Multiple modifier for r.y - both direct value and
// part of record
\end{lstlisting}
The following examples are correct:
\begin{lstlisting}[language=modelica]
m1(r = 1.5, r(start = 2.0))
m1(r = 1.6, r "x")
m1(r = R(), r(y(min = 2)))
\end{lstlisting}
\end{example}
\subsection{Modifiers for Array Elements}\label{modifiers-for-array-elements}
The following rules apply to modifiers:
\begin{itemize}
\item
The \lstinline!each!\indexinline{each} keyword on a modifier requires that it is applied in an array declaration/modification, and the modifier is applied individually to each element of the enclosing array (with regard to the position of \lstinline!each!).
In case of nested modifiers this implies it is applied individually to each element of each element of the enclosing array; see example.
If the modified element is a vector and the modifier does not contain the \lstinline!each! prefix, the modification is split such that the first element in the vector is applied to the first element of the vector of elements, the second to the second element, until the last element of the vector is applied to the last element of the array; it is an error if these sizes do not match.
Matrices and general arrays of elements are treated by viewing those as vectors of vectors etc.
\item
If a nested modifier is split, the split is propagated to all elements of the nested modifier, and if they are modified by the \lstinline!each! keyword the split is inhibited for those elements.
If the nested modifier that is split in this way contains re-declarations that are split, it is illegal.
\end{itemize}
\begin{example}
\begin{lstlisting}[language=modelica]
model C
parameter Real a[3];
parameter Real d;
end C;
model B
C c[5](each a = {1, 2, 3}, d = {1, 2, 3, 4, 5});
parameter Real b = 0;
end B;
\end{lstlisting}
This implies \lstinline!c[i].a[j] = j! and \lstinline!c[i].d = i!.
\begin{lstlisting}[language=modelica]
model D
B b(each c.a = {3, 4, 5}, c.d = {2, 3, 4, 5, 6});
// Equivalent to:
B b2(c(each a = {3, 4, 5}, d = {2, 3, 4, 5, 6}));
end D;
\end{lstlisting}
This implies \lstinline!b.c[i].a[j] = 2+j! and \lstinline!b.c[i].d = 1+i!.
\begin{lstlisting}[language=modelica]
model E
B b[2](each c(each a = {1, 2, 3}, d = {1, 2, 3, 4, 5}), p = {1, 2});
// Without the first each one would have to use:
B b2[2](c(each a = {1, 2, 3}, d = fill({1, 2, 3, 4, 5}, 2)), p = {1, 2});
end E;
\end{lstlisting}
This implies \lstinline!b[k].c[i].a[j] = j!, \lstinline!b[k].c[i].d = i!, and \lstinline!b[k].p = k!. For \lstinline!c.a! the additional (outer) \lstinline!each! has no effect, but it is necessary for \lstinline!c.d!.
Specifying array dimensions after the type works the same as specifying them after the variable name.
\begin{lstlisting}[language=modelica]
model F
Real fail1[2](each start = {1, 2}); // Illegal
Real work1[2](each start = 1); // Legal
Real[2] fail2(each start = {1, 2}); // Illegal
Real[2] work2(each start = 2); // Legal
end F;
\end{lstlisting}
\end{example}
\subsection{Final Element Modification Prevention}\label{final-element-modification-prevention}
An element defined as final by the \lstinline!final!\indexinline{final} prefix in an element modification or declaration cannot be modified by a modification or by a redeclaration. All elements of a final element are also final.
\begin{nonnormative}
Setting the value of a parameter in an experiment environment is conceptually treated as a modification. This implies that a final modification equation
of a parameter cannot be changed in a simulation environment.
\end{nonnormative}
\begin{example}
Final component modification.
\begin{lstlisting}[language=modelica]
type Angle =
Real(final quantity = "Angle", final unit = "rad", displayUnit = "deg");
model TransferFunction
parameter Real b[:] = {1} "numerator coefficient vector";
parameter Real a[:] = {1, 1} "denominator coefficient vector";
$\ldots$
end TransferFunction;
model PI "PI controller"
parameter Real k = 1 "gain";
parameter Real T = 1 "time constant";
TransferFunction tf(final b = k * {T, 1}, final a = {T, 0});
end PI;
model Test
PI c1(k = 2, T = 3); // fine, will indirectly change tf.b to 2 * {3, 1}
PI c2(tf(b = {1})); // error, b is declared as final
end Test;
\end{lstlisting}
\end{example}
\begin{example}
Final class declaration.
\begin{lstlisting}[language=modelica]
model Test2
final model MyTF = TransferFunction(b = {1, 2});
/* Equivalently:
final model MyTF = TransferFunction(final a, final b = {1, 2});
*/
MyTF tf1; // fine
MyTF tf2(a = {1, 2}); // error, all elements in MyTF are final
model M = MyTF(a = {4}); // error, all elements in MyTF are final
model TFX
extends MyTF; // fine
Real foo = 1.0;
end TFX;
TFX tfx(foo = 2.0); // fine, foo is not from MyRF
TFX tfx2(a = {1, 3}); // error, all elements from MyTF are final
model TFX3 = TFX(a = {1, 4}); // error, all elements from MyTF are final
end Test2;
\end{lstlisting}
\end{example}
\subsection{Removing Modifiers -- break}\label{removing-modifiers-break}
Modifications may contain the special keyword \lstinline!break! instead of an expression.
The intention of \lstinline!break! is to remove the value.
The modifiers using \lstinline!break! are merged using the same rule as other modifications, and follow the same restrictions so they cannot override a final modifier.
During flattening of an instantiated model, remaining \lstinline!break! modifications (i.e., the ones that are not further overriden) are treated as if the expression was missing.
The \lstinline!break! modifier for a variable of a simple type can be applied to the value and/or to specific attributes.
It is possible to override even if no value is present, either because there was no expression originally or because \lstinline!break! overrides another \lstinline!break!.
\begin{nonnormative}
In a dialog, a tool may hide the keyword \lstinline!break! and show an empty input field, without the overriden modification.
It should also be possible to remove this modifier to restore the overriden modification.
There are also other uses of the keyword \lstinline!break!, but importantly it is not an expression and thus it cannot be used as a sub-expression.
\end{nonnormative}
\begin{example}
Remove unwanted defaults for parameters:
\begin{lstlisting}
partial model PartialStraightPipe
parameter Real roughness = 2.5e-5 "Average height of surface asperities";
parameter Real height_ab (unit = "m" ) = 0 "Height between a and b";
...
end PartialStraightPipe;
model StaticPipe
extends PartialStraightPipe;
parameter Real p_a_start = system.p_start;
...
end StaticPipe;
model MyPipe "Without defaults"
extends StaticPipe(
p_a_start = break,
roughness = break,
height_ab = break);
end MyPipe;
\end{lstlisting}
Replace a given parameter value by an initial computation:
\begin{lstlisting}
model A
parameter Real diameter = 1;
final parameter radius = diameter / 2;
end A;
model B "Initial equation for diameter"
extends A( diameter(fixed = false) = break );
parameter Real square=2;
initial equation
// solving equation below for diameter
square = f(diameter);
end B;
\end{lstlisting}
Replace the value for an inherited variable with a value computed from an algorithm:
\begin{lstlisting}
model A
Real x = 1;
end A;
model B "Computing x instead"
extends A(x=break);
algorithm
x:=0;
while ...
x := x + ...
end while;
end B;
\end{lstlisting}
Note that this is only legal because the modifier is modifying an inherited declaration.
Due to \cref{balanced-models} it is not legal to construct the corresponding component declaration, \lstinline!A a(x=break);!.
\end{example}
\section{Redeclaration}\label{redeclaration}
A \lstinline!redeclare!\indexinline{redeclare} construct in a modifier replaces the declaration of a local class or component with another declaration.
A \lstinline!redeclare! construct as an element replaces the declaration of a local class or component with another declaration.
Both \lstinline!redeclare! constructs work in the same way.
The \lstinline!redeclare! construct as an element requires that the element is inherited, and cannot be combined with a modifier of the same element in the \lstinline!extends!-clause.
For modifiers, the redeclare of classes uses the \lstinline[language=grammar]!short-class-definition! construct, which is a special case of normal class definitions and semantically behaves as the corresponding \lstinline[language=grammar]!class-definition!.
A modifier with the keyword \lstinline!replaceable!\indexinline{replaceable} is automatically seen as being a \lstinline!redeclare!.
In redeclarations some parts of the original declaration is
automatically inherited by the new declaration. This is intended to make
it easier to write declarations by not having to repeat common parts of
the declarations, and does in particular apply to prefixes that must be
identical. The inheritance only applies to the declaration itself and
not to elements of the declaration.
The general rule is that if no prefix within one of the following groups
is present in the new declaration the old prefixes of that kind are
preserved.
The groups that are valid for both classes and components:
\begin{itemize}
\item
\lstinline!public!, \lstinline!protected!
\item
\lstinline!inner!, \lstinline!outer!
\item
constraining type according to rules in \cref{constraining-type}.
\end{itemize}
The groups that are only valid for components:
\begin{itemize}
\item
\lstinline!flow!, \lstinline!stream!
\item
\lstinline!discrete!, \lstinline!parameter!, \lstinline!constant!
\item
\lstinline!input!, \lstinline!output!
\item
array dimensions
\end{itemize}
Note that if the old declaration was a short class definition with array
dimensions the array dimensions are not automatically preserved, and
thus have to be repeated in the few cases they are used.
Replaceable component array declarations with array sizes on the left of
the component are seen as syntactic sugar for having all arrays sizes on
the right of the component; and thus can be redeclared in a consistent
way.
\begin{nonnormative}
Note: The inheritance is from the original declaration. In most
cases replaced or original does not matter. It does matter if a user
redeclares a variable to be a parameter and then redeclares it without
parameter.
\end{nonnormative}
\begin{nonnormative}
\begin{lstlisting}[language=modelica]
model HeatExchanger
replaceable parameter GeometryRecord geometry;
replaceable input Real u[2];
end HeatExchanger;
HeatExchanger(
/*redeclare*/ replaceable /*parameter*/ GeoHorizontal geometry,
redeclare /*input*/ Modelica.Units.SI.Angle u /*[2]*/);
// The semantics ensure that parts in /*.*/ are automatically added
// from the declarations in HeatExchanger.
\end{lstlisting}
Example of arrays on the left of the component name:
\begin{lstlisting}[language=modelica]
model M
replaceable Real [4] x[2];
// Seen as syntactic sugar for "replaceable Real x[2, 4];"
// Note the order.
end M;
M m(redeclare Modelica.Units.SI.Length x[2, 4]); // Valid redeclare of the type
\end{lstlisting}
\end{nonnormative}
\subsection{The ``class extends'' Redeclaration Mechanism}\label{the-class-extends-redeclaration-mechanism}
A class declaration of the type \lstinline!redeclare class extends B($\ldots$)!, where \lstinline!class! as usual can be replaced by any other specialized class, replaces the inherited class \lstinline!B! with another declaration that extends the inherited class where the optional class-modification is applied to the inherited class.
Inherited \lstinline!B! here means that the class containing \lstinline!redeclare class extends B($\ldots$)! should also inherit another declaration of \lstinline!B! from one of its \lstinline!extends!-clauses.
The new declaration should explicitly include \lstinline!redeclare!.
\begin{nonnormative}
Since the rule about applying the optional class-modification implies that all declarations are inherited with modifications applied, there is no need
to apply modifiers to the new declaration.
\end{nonnormative}
% henrikt-ma: Adding index entry for 'replaceable' here, since I can't find a place where the term is really explained in terms of using the corresponding keyword.
For \lstinline!redeclare class extends B($\ldots$)! the inherited class is subject to the same restrictions as a redeclare of the inherited element, and the original class \lstinline!B! should be \firstuse{replaceable}, and the new element is only replaceable if the new definition is replaceable.
In contrast to normal extends it is not subject to the restriction that \lstinline!B! should be transitively non-replaceable (since \lstinline!B! should be replaceable).
The syntax rule for \lstinline!class extends! construct is in the definition of the
\lstinline!class-specifier! nonterminal (see also class declarations in \cref{class-declarations}):
\begin{lstlisting}[language=grammar]
class-definition :
[ encapsulated ] class-prefixes
class-specifier
class-specifier : long-class-specifier | $\ldots$
long-class-specifier : $\ldots$
| extends IDENT [ class-modification ] description-string
composition end IDENT
\end{lstlisting}
The nonterminal \lstinline!class-definition! is referenced in several places in the
grammar, including the following case which is used in some examples
below, including \lstinline!package extends! and \lstinline!model extends!:
\begin{lstlisting}[language=grammar]
element :
import-clause |
extends-clause |
[ redeclare ]
[ final ]
[ inner ] [ outer ]
( ( class-definition | component-clause) |
replaceable ( class-definition | component-clause)
[constraining-clause comment])
\end{lstlisting}
\begin{nonnormative}
Example to extend from existing packages:
\begin{lstlisting}[language=modelica]
package PowerTrain // library from someone else
replaceable package GearBoxes
$\ldots$
end GearBoxes;
end PowerTrain;
package MyPowerTrain
extends PowerTrain; // use all classes from PowerTrain
redeclare package extends GearBoxes // add classes to sublibrary
$\ldots$
end GearBoxes;
end MyPowerTrain;
\end{lstlisting}
Example for an advanced type of package structuring with constraining types:
\begin{lstlisting}[language=modelica]
partial package PartialMedium "Generic medium interface"
constant Integer nX "number of substances";
replaceable partial model BaseProperties
Real X[nX];
$\ldots$
end BaseProperties;
replaceable partial function dynamicViscosity
input Real p;
output Real eta;
$\ldots$
end dynamicViscosity;
end PartialMedium;
package MoistAir "Special type of medium"
extends PartialMedium(nX=2);
redeclare model extends BaseProperties(T(stateSelect = StateSelect.prefer))
// replaces BaseProperties by a new implementation and
// extends from Baseproperties with modification
// note, nX = 2 (!)
equation
X = {0, 1};
$\ldots$
end BaseProperties;
redeclare function extends dynamicViscosity
// replaces dynamicViscosity by a new implementation and
// extends from dynamicViscosity
algorithm
eta := 2 * p;
end dynamicViscosity;
end MoistAir;
\end{lstlisting}
Note, since \lstinline!MostAir! extends from \lstinline!PartialMedium!,
constant \lstinline!nX! = 2 in package \lstinline!MoistAir! and the model
\lstinline!BaseProperties! and the function \lstinline!dynamicViscosity! is present
in \lstinline!MoistAir!. By the following definitions, the available
\lstinline!BaseProperties! model is replaced by another implementation which
extends from the \lstinline!BaseProperties! model that has been temporarily
constructed during the extends of package \lstinline!MoistAir! from
\lstinline!PartialMedium!. The redeclared \lstinline!BaseProperties! model
references constant \lstinline!nX! which is 2, since by construction the
redeclared \lstinline!BaseProperties! model is in a package with \lstinline!nX! = 2.
This definition is compact but is difficult to understand. At a
first glance an alternative exists that is more straightforward and
easier to understand:
\begin{lstlisting}[language=modelica]
package MoistAir2 "Alternative definition that does not work"
extends PartialMedium(nX=2,
redeclare model BaseProperties = MoistAir_BaseProperties,
redeclare function dynamicViscosity = MoistAir_dynamicViscosity);
model MoistAir_BaseProperties
// wrong model since nX has no value
extends PartialMedium.BaseProperties;
equation
X = {1, 0};
end MoistAir_BaseProperties;
function MoistAir_dynamicViscosity
extends PartialMedium.dynamicViscosity;
algorithm
eta := p;
end MoistAir_dynamicViscosity;
end MoistAir2;
\end{lstlisting}
Here, the usual approach is used to extend (here from \lstinline!PartialMedium!) and in the modifier perform all redeclarations.
In order to perform these redeclarations, corresponding implementations of all elements of \lstinline!PartialMedium! have to be given under a different name, such as \lstinline!MoistAir2.MoistAir_BaseProperties!, since the name \lstinline!BaseProperties! already exists due to \lstinline!extends PartialMedium!.
Then it is possible in the modifier to redeclare \lstinline!PartialMedium.BaseProperties! to \lstinline!MoistAir2.MoistAir_BaseProperties!.
Besides the drawback that the namespace is polluted by elements that have different names but the same implementation (e.g.\ \lstinline!MoistAir2.BaseProperties! is identical to \lstinline!MoistAir2.MoistAir_BaseProperties!) the whole construction does not work if arrays are present that depend on constants in \lstinline!PartialMedium!, such as \lstinline!X[nX]!:
The problem is that \lstinline!MoistAir_BaseProperties! extends from \lstinline!PartialMedium.BaseProperties! where the constant \lstinline!nX! does not yet have a value.
This means that the dimension of array \lstinline!X! is undefined and model \lstinline!MoistAir_BaseProperties! is wrong.
With this construction, all constant definitions have to be repeated whenever these constants shall be used, especially in \lstinline!MoistAir_BaseProperties! and \lstinline!MoistAir_dynamicViscosity!.
For larger models this is not practical and therefore the only practically useful definition is the complicated construction in the previous example with \lstinline!redeclare model extends BaseProperties!.
To detect this issue the rule on lookup of composite names (\cref{composite-name-lookup}) ensures that \lstinline!PartialMedium.dynamicViscosity! is incorrect in a simulation model.
\end{nonnormative}
\subsection{Constraining Type}\label{constraining-type}
In a replaceable declaration the optional \lstinline[language=grammar]!constraining-clause! defines a constraining type.
Any modifications following the constraining type name are applied both for the purpose of defining the actual constraining type and they are automatically applied in the declaration and in any subsequent redeclaration.
The precedence order is that declaration modifiers override constraining type modifiers.
If the \lstinline[language=grammar]!constraining-clause! is not present in the original declaration (i.e., the non-redeclared declaration):
\begin{itemize}
\item
The type of the declaration is also used as a constraining type.
\item
The modifiers for subsequent redeclarations and constraining type are the modifiers on the component or \lstinline[language=grammar]!short-class-definition! if that is used in the original declaration, otherwise empty.
\end{itemize}
The syntax of a \lstinline[language=grammar]!constraining-clause!\indexinline{constrainedby} is as follows:
\begin{lstlisting}[language=grammar]
constraining-clause :
constrainedby name [ class-modification ]
\end{lstlisting}
\begin{example}
Merging of modifiers:
\begin{lstlisting}[language=modelica]
class A
parameter Real x;
end A;
class B
parameter Real x = 3.14, y; // B is a subtype of A
end B;
class C
replaceable A a(x = 1);
end C;
class D
extends C(redeclare B a(y = 2));
end D;
\end{lstlisting}
which is equivalent to defining \lstinline!D! as
\begin{lstlisting}[language=modelica]
class D
B a(x = 1, y = 2);
end D;
\end{lstlisting}
A modification of the constraining type is automatically applied
in subsequent redeclarations:
\begin{lstlisting}[language=modelica]
model ElectricalSource
replaceable SineSource source constrainedby MO(final n=5);
$\ldots$
end ElectricalSource;
model TrapezoidalSource
extends ElectricalSource(
redeclare Trapezoidal source); // source.n=5
end TrapezoidalSource;
\end{lstlisting}
A modification of the base type without a constraining type is
automatically applied in subsequent redeclarations:
\begin{lstlisting}[language=modelica]
model Circuit
replaceable model NonlinearResistor = Resistor(R=100);
$\ldots$
end Circuit;
model Circuit2
extends Circuit(
redeclare replaceable model NonlinearResistor
= ThermoResistor(T0 = 300));
// As a result of the modification on the base type,
// the default value of R is 100
end Circuit2;
model Circuit3
extends Circuit2(
redeclare replaceable model NonlinearResistor
= Resistor(R = 200));
// The T0 modification is not applied because it did not
// appear in the original declaration
end Circuit3;
\end{lstlisting}
\lstinline!Circuit2! is intended to illustrate that a user can still select
any resistor model (including the original one, as is done in \lstinline!Circuit3!),
since the constraining type is kept from the original declaration if not
specified in the redeclare. Thus it is easy to select an advanced
resistor model, without limiting the possible future changes.
A redeclaration can redefine the constraining type:
\begin{lstlisting}[language=modelica]
model Circuit4
extends Circuit2(
redeclare replaceable model NonlinearResistor
= ThermoResistor constrainedby ThermoResistor);
end Circuit4;
model Circuit5
extends Circuit4(
redeclare replaceable model NonlinearResistor = Resistor); // illegal
end Circuit5;
\end{lstlisting}
\end{example}
The class or type of component shall be a subtype of the constraining
type. In a redeclaration of a replaceable element, the class or type of
a component must be a subtype of the constraining type. The constraining
type of a replaceable redeclaration must be a subtype of the
constraining type of the declaration it redeclares. In an element
modification of a replaceable element, the modifications are applied
both to the actual type and to the constraining type.
In an element-redeclaration of a replaceable element the modifiers of
the replaced constraining type are merged to both the new declaration
and to the new constraining type, using the normal rules where outer
modifiers override inner modifiers.
When a class is flattened as a constraining type, the flattening of its
replaceable elements will use the constraining type and not the actual
default types.
The number of dimension in the constraining type should correspond to
the number of dimensions in the type-part. Similarly the type used in a
redeclaration must have the same number of dimensions as the type of
redeclared element.
\begin{example}
\begin{lstlisting}[language=modelica]
replaceable T1 x[n] constrainedby T2;
replaceable type T=T1[n] constrainedby T2;
replaceable T1[n] x constrainedby T2;
\end{lstlisting}