- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » Member not replaced as expected, why?
Member not replaced as expected, why?
Member not replaced as expected, why?
Aims: all the derived classes should inherit some default equations from their base class. When the default equation is not valid for a derived class then it should redeclare it.
Here is a somewhat silly minimalistic example.
Code:
package Pkg
class Equations
Real x;
end Equations;
class DefaultEquations
extends Equations;
equation
x = 0.0;
end DefaultEquations;
class Base
replaceable DefaultEquations equations extends Equations;
end Base;
end Pkg;
model DuplicateEquations
import Pkg.*;
class CustomizedClass
extends Base;
redeclare Equations equations;
equation
equations.x = 3;
end CustomizedClass;
CustomizedClass customized;
end DuplicateEquations;
For some mysterious reason, the default equation is not overriden:
Code:
omc Test.mo Package.mo
class DuplicateEquations
Real customized.equations.x;
equation
customized.equations.x = 0.0;
customized.equations.x = 3.0;
end DuplicateEquations;
Why is this happening? Why are both x=0 and x=3 generated?
If I comment out the package declaration I get only the expected x=3 equation.
Re: Member not replaced as expected, why?
Hi,
This is also the behavior of Dymola. You get that extra equation.
You can however do this (redeclare the type, not the component) to get the wanted behavior.
File: Redeclare.mo
Code:
package Pkg
class Equations
Real x;
end Equations;
class DefaultEquations
extends Equations;
equation
x = 0.0;
end DefaultEquations;
class Base
replaceable class T = DefaultEquations;
replaceable T equations extends Equations;
end Base;
model DuplicateEquations
import Pkg.*;
class CustomizedClass
extends Base(redeclare class T = Equations);
equation
equations.x = 3;
end CustomizedClass;
CustomizedClass customized;
end DuplicateEquations;
end Pkg;
File: Redeclare.mos
Code:
loadFile("Redeclare.mo"); getErrorString();
instantiateModel(Pkg.DuplicateEquations); getErrorString();
Command line execution:
Code:
adrpo@ida-liu050 ~/dev/OpenModelica/build/bin/bah
$ ../omc.exe Redeclare.mos > trace-Redeclare.mos.txt
File:trace-Redeclare.mos.txt
Code:
true
""
"class Pkg.DuplicateEquations
Real customized.equations.x;
equation
customized.equations.x = 3.0;
end Pkg.DuplicateEquations;
"
""
Cheers,
Adrian Pop/
- adrpo
- 885 Posts
- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » Member not replaced as expected, why?