Redeclaration


By using the keyword
redeclare it is possible to modify the type and/or the prefixes and possibly the dimension sizes of a declared element. This kind of modification is called a redeclaration. In most cases a declaration that can be redeclared must include the prefix replaceable.

The element in a redeclaration must have a type that is either a subtype of the replaceable element in the modified class or a subtype of a constraining type. An example is shown below with the concepts of redeclaration.


1 Connector Classes

x
 
1
type Voltage = Real(unit = "V");
2

2
 
1
type Current = Real(unit = "A");
2

5
 
1
connector Pin
2
  Voltage     v;
3
  flow Current   i;
4
end Pin;
5

2 Resistor and TempResistor

9
 
1
model Resistor
2
  Pin       p, n                "Positive and Negative Pins";
3
  Voltage     v;
4
  Current     i;
5
  parameter Real R(unit = "Ohm")          "Resistance";
6
equation
7
  v = R*i;
8
end Resistor;
9

12
 
1
model TempResistor                  "Temperature dependent Resistor"
2
  Pin       p, n                "Positive and Negative Pins";
3
  Voltage     v;
4
  Current     i;
5
  parameter Real R(unit = "Ohm")          "Resistance at reference Temperature";
6
  parameter Real RT(unit = "Ohm/degC")   = 0     "Temperature dependent Resistance";
7
  parameter Real Tref(unit = "degC")   = 20    "Reference Temperature";
8
  Real        Temp           = 20    "Actual Temperature";
9
equation
10
  v = i*(R + RT*(Temp - Tref));
11
end TempResistor;
12

3 MiniCircuit

4
 
1
class MiniCircuit
2
  replaceable discrete Resistor R1;
3
end MiniCircuit;
4


The redeclaration of the
R1 element changes its type from Resistor to TempResistor and its prefix from discrete to parameter.


4
 
1
class MiniCircuitRed
2
  MiniCircuit tempcircuit(redeclare parameter TempResistor R1);
3
end MiniCircuitRed;
4


The MiniCircuitRed class can be rewritten as the MiniCircuit2


4
 
1
class MiniCircuit2
2
  parameter TempResistor R1;
3
end MiniCircuit2;
4

4 Redeclaration with Extends Clause

The next example illustrates a redeclaration used in an extends clause.


4
 
1
class RedA
2
  parameter Real x;
3
end RedA;
4

5
 
1
class RedA2                    // A2 is a subtype of A
2
  parameter Real x = 3.14;
3
  parameter Real y;
4
end RedA2;
5

4
 
1
class RedB
2
  replaceable RedA a(x = 1);
3
end RedB;
4


The redeclaration
redeclare RedA2 a(y=2) is used in an extends clause to replace the original declaration RedA a(x=1) from class RedB, but merging the original nested modifier (x=1) with the modifiers in the redeclaration to effectively get RedA2 a(x=1,y=2). The type of the redeclared field a in class RedB is changed from RedA to RedA2 in class RedB2 which is correct since RedA2 is a subtype of RedA.


4
 
1
class RedB2
2
  extends RedB(redeclare RedA2 a(y = 2));    // The result after redeclaration is RedA2 a(x = 1, y = 2)
3
end RedB2;
4


The class RedB, is equivalent to the class B2expanded below.


4
 
1
class B2expanded
2
  RedA2 a(x = 1, y = 2);
3
end B2expanded;
4

4.1 Flattening RedB, B2expanded

2
 
1
instantiateModel(RedB2)
2

2
 
1
instantiateModel(B2expanded)
2