With the release of 13.3 you might have encountered a few error messages, such as “invalid extends-clause,” “invalid class extends,” and “Invalid lookup in a non-encapsulated class”, for your previously working models. The background for these new errors is that we continuously introduce new checks to ensure full compliance with the Modelica language, making sure that the models our customers develop are robust and can easily be shared with other Modelica tools.
In most cases the needed changes are rather simple to do and in this post we aim to give some tips and tricks to help you transition. In this post we focus on the three mentioned error messages, but feel free to add comments or questions if you have other related issues.
Patch release
As we have come to recognize that certain errors affected our users more then expected we have decided to release a patch release in August. In this release some of these errors will be transformed into warnings instead. Giving you time to update your models while still enjoying the new features of 13.3. These warnings will then be changed to errors in a later release. This will also be our general process in the future - when introducing stricter checks we will start with 1 - 2 release cycles with warning messages before doing the full switch to reporting them as errors.
Invalid extends-clause
Invalid extends-clause
In[]:=
modelMyIcon |
endMyIcon; |
In[]:=
packageMyLibrary |
extendsMyIcon; |
endMyLibrary; |
In[]:=
WSMLink`Library`WSMValidate["MyLibrary"]["Messages"]//Grid
Out[]=
Error | Invalid extends-clause. MyLibrary is a package and MyIcon is a model, but a package cannot extend a model. |
What does it protect against?
What does it protect against?
Different specializations (package, model, type...) are used for different things, so combining them is usually not possible (except in special cases). For instance, a package cannot extend a model. Packages serve as containers for elements such as models, types, constants, or even other packages. They are not intended to contain equations of their own. By disallowing packages from extending models, we eliminate the possibility of a package extending a model that includes equations.
How to fix it?
How to fix it?
It can be fixed by matching the specializations.
In[]:=
packageMyIcon |
endMyIcon; |
In[]:=
packageMyLibrary |
extendsMyIcon; |
endMyLibrary; |
In[]:=
WSMLink`Library`WSMValidate["MyLibrary"]["Messages"]
Out[]=
{}
What does the specification say?
What does the specification say?
“One of the major benefits of object-orientation is the ability to extend the behavior and properties of an existing class. The original class, known as the base class, is extended to create a more specialized version of that class, known as the 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 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.”
“The specialized classes package, operator, function, type, record, operator record, and expandable connector can only be derived from their own kind and from class.”
What is a class?
What is a class?
Modelica specification: https://specification.modelica.org/maint/3.5/class-predefined-types-and-declarations.html
“The fundamental structuring unit of modeling in Modelica is the class. Classes provide the structure for objects, also known as instances. Classes can contain equations which provide the basis for the executable code that is used for computation in Modelica. Conventional algorithmic code can also be part of classes. All data objects in Modelica are instantiated from classes, including the basic data types – Real, Integer, String, Boolean – and enumeration types, which are built-in classes or class schemata.”
Invalid class extends
Invalid class extends
In[]:=
partialpackagepartialPackage |
replaceablefunctionf |
inputReali; |
outputRealo; |
endf; |
endpartialPackage; |
In[]:=
packageMyPackage |
extendspartialPackage; |
|
functionextendsf |
algorithm |
o:=i; |
endf; |
endMyPackage; |
In[]:=
WSMLink`Library`WSMValidate["MyPackage"]["Messages"]//Grid
Out[]=
Error | Invalid class extends. Every class extends declaration must have either redeclare or replaceable as a prefix. |
What does it protect against?
What does it protect against?
When a modeler extends a class, there can be ambiguity regarding whether the intention is to create a new local definition of the class or simply update the definition in the parent class. By utilizing the “replaceable” and “redeclare” keywords, this ambiguity is eliminated.
How to fix it?
How to fix it?
When defining a class that can be redeclared, prepend the class name with the prefix ‘replaceable.’ Additionally, when modifying the replaceable class include the prefix ‘redeclare’.
partialpackagepartialPackage |
replaceablefunctionf |
inputReali; |
outputRealo; |
endf; |
endpartialPackage; |
packageMyPackage |
extendspartialPackage; |
|
redeclarefunctionextendsf |
algorithm |
o:=i; |
endf; |
endMyPackage; |
In[]:=
WSMLink`Library`WSMValidate["MyPackage"]["Messages"]
Out[]=
{}
What does the specification say?
What does the specification say?
“A more dramatic change is to modify the type and/or the prefixes and possibly the dimension sizes of a declared element. This kind of modification is called an element-redeclaration (section 7.3) and requires the special keyword 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 replaceable (section 7.3).”
Invalid lookup in a non encapsulated class
Invalid lookup in a non encapsulated class
In[]:=
modelMyModel |
typemyNames=enumeration(a"alpha",b"beta"); |
endMyModel; |
In[]:=
modelNewModel |
MyModel.myNamesname=MyModel.myNames.a; |
endNewModel; |
In[]:=
Column[Flatten[WSMLink`Library`WSMValidate["NewModel"]["Messages"]]]
Out[]=
Error |
NewModel:2 Invalid lookup in a non encapsulated class. When looking up MyModel.myNames in scope NewModel, myNames is not encapsulated. Since MyModel is not a package nor an operator, myNames must be looked up among encapsulated element only. |
What does it protect against?
What does it protect against?
This error ensures that things like enumeration, functions, types etc, can be used in another class without instantiating the class in which they are defined.
How to fix it?
How to fix it?
Define the type definitions in a package instead.
In[]:=
packageMyPackage |
typemyNames=enumeration(a"alpha",b"beta"); |
endMyPackage; |
In[]:=
modelNewModel |
MyPackage.myNamesname=MyPackage.myNames.a; |
endNewModel; |
In[]:=
WSMLink`Library`WSMValidate["NewModel"]["Messages"]
Out[]=
{}
If you want to have the type definitions in a Model instead, then add an "encapsulated" keyword before your object declaration inside a non-Package object.
What does the specification say?
What does the specification say?
“If the identifier denotes a class, that class is temporarily flattened (as if instantiating a component without modifiers of this class, see section 7.2.2 and using the enclosing classes of the denoted class). The rest of the name (e.g., B or B.C) is looked up among the declared named elements of the temporary flattened class. The lookup will only find the element (assuming it exists) in the following cases:
– If the class is declared as package or operator (but not operator record or operator function) all elements can be found.
– An element can be found if it is declared as encapsulated.
– A deprecated case is that if the class satisfies the requirements for a package (without being declared as such), it is still treated as a package.”
– If the class is declared as package or operator (but not operator record or operator function) all elements can be found.
– An element can be found if it is declared as encapsulated.
– A deprecated case is that if the class satisfies the requirements for a package (without being declared as such), it is still treated as a package.”
Learn more...
Learn more...
Modelica language specification: https://specification.modelica.org/maint/3.6/MLS.html
Learn Modelica: https://mbe.modelica.university/
CITE THIS NOTEBOOK
CITE THIS NOTEBOOK
Troubleshooting System Modeler error messages
by Ankit Naik
Wolfram Community, STAFF PICKS, July 18, 2023
https://community.wolfram.com/groups/-/m/t/2969985
by Ankit Naik
Wolfram Community, STAFF PICKS, July 18, 2023
https://community.wolfram.com/groups/-/m/t/2969985