Modification Prefix Replaceable and Redeclare

1 Replaceable

A declared element of a class must have the prefix replaceable in order to be redeclared, apart from redeclarations that only change the attribute value and/or restricts the variability and possibly the dimension sizes. The main motivation for this rule is that a class needs to be designed for inheritance that includes redeclaration. Otherwise unexpected errors may occur.


x
 
1
class MicroCircuit
2
  replaceable Resistor R1 (R = 4.0);
3
end MicroCircuit;
4

2 Connector Classes

2
 
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

3 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

13
 
1
model SuperTempResistor                "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
  parameter Boolean Super         = true;
10
equation
11
  v = i*(R + RT*(Temp - Tref));
12
end SuperTempResistor;
13

4 Redeclare

The redeclare prefix is used at the front of a modifier containing a new variable or type declaration that replaces the original variable or type declaration element in the modified class.The reason for requiring a special keyword like redeclare is to reduce the risk for accidental programming errors that otherwise might occur when whole declarations are replaced by declarations of different types.


2
 
1
class TempMicroCiruit = MicroCircuit(redeclare TempResistor R1);
2


In order to allow further redeclaration of a redeclared element the replaceable prefix should be present also in the modifier.


2
 
1
class TempMicroCiruit2 = MicroCircuit(redeclare replaceable TempResistor R1);
2


In the class
SuperTempMicroCircuit the keyword replaceable is left out, which means that no further redeclaration is possible.


2
 
1
class SuperTempMicroCiruit = TempMicroCiruit2(redeclare SuperTempResistor R1);
2

4.1 Flattening of MicroCircuit, TempMicroCiruit2

2
 
1
instantiateModel(MicroCircuit)
2

2
 
1
instantiateModel(TempMicroCiruit2)
2

2
 
1
instantiateModel(SuperTempMicroCiruit)
2