Opened 7 years ago

Last modified 7 years ago

#4674 new defect

Wrong return values for getParameterValue for inherited classes

Reported by: Niklas Worschech Owned by: somebody
Priority: critical Milestone: Future
Component: Interactive Environment Version:
Keywords: getParameterValue Cc: florian.werner@…

Description

For this model

package ParameterExtendsTest
  model BaseRecord
    parameter Real a = 1;
    parameter Real b = 1;
    parameter Real c = 1;
    annotation (Icon(coordinateSystem(preserveAspectRatio=false)), Diagram(coordinateSystem(
            preserveAspectRatio=false)));
  end BaseRecord;

  model SpecialRecord
    extends ParameterExtendsTest.BaseRecord(
    a = 2.1,
    b = 4.1,
    c = 8.1);
    extends ParameterExtendsTest.Base2(d = 16.1);
    parameter Real e = 32.1;

    annotation (Icon(coordinateSystem(preserveAspectRatio=false)), Diagram(coordinateSystem(
            preserveAspectRatio=false)));
  end SpecialRecord;

  model Base2
    parameter Real d = 1;
    annotation (Icon(coordinateSystem(preserveAspectRatio=false)), Diagram(coordinateSystem(
            preserveAspectRatio=false)));
  end Base2;
end ParameterExtendsTest;

The OpenModelica API call for getting a parameter value returns not the overwritten value from the extended model

classes:= getInheritedClasses(ParameterExtendsTest.SpecialRecord);getErrorString();
for i in 1:size(classes,1) loop
  params := getParameterNames(classes[i]);getErrorString();
 for j in 1:size(params,1) loop
    print("parameter = "+params[j]+ " value=" +getParameterValue(classes[i],params[j]) +"\n");
  end for;
end for;

the output is:

parameter = a value=1
parameter = b value=1
parameter = c value=1
parameter = d value=1

for

params:= getParameterNames(ParameterExtendsTest.SpecialRecord);getErrorString();
for i in 1:size(params,1) loop
    print("parameter = "+params[i]+ " value=" +getParameterValue(ParameterExtendsTest.SpecialRecord,params[i]) +"\n");
end for;

it only returns:
parameter = e value=32.1

But we expect:

parameter = a value=2.1
parameter = b value=4.1
parameter = c value=8.1
parameter = d value=16.1
parameter = e value=32.1

Attachments (2)

ParameterExtendsTest.mos (927 bytes ) - added by Niklas Worschech 7 years ago.
ParameterExtendsTest.mo (875 bytes ) - added by Niklas Worschech 7 years ago.

Download all attachments as: .zip

Change History (10)

comment:1 by Niklas Worschech, 7 years ago

is there a workaround for that?

comment:2 by Adrian Pop, 7 years ago

This is done by design, as we are interested in the parameters from the code, not the ones after the instantiation.
You can use getExtendsModifierNames and getExtendsModifierValues to get the modified values from extends. See some examples here:
https://github.com/OpenModelica/OpenModelica-testsuite/blob/master/openmodelica/interactive-API/interactive_api_calls.mos

comment:3 by Niklas Worschech, 7 years ago

if I use getExtendsModifierValue in a mos script I get the error could not find class
getExtendsModifierValue in global scope. Do I have to pass the classname as first parameter and as second parameter the name of the component as it is described the system documentation?

comment:4 by Adrian Pop, 7 years ago

It should be like this:

class ClassBeingExtended
  extends TheBaseClass(TheNameOfTheParameter = 10);
end ClassBeingExtended;

You should call it like:

getExtendsModifierValue(ClassBeingExtended, TheBaseClass, TheNameOfTheParameter);

comment:5 by Niklas Worschech, 7 years ago

I still get the error that getExtendsModifierValue could not be found. Do I have to import something for that?

comment:6 by Adrian Pop, 7 years ago

Can you attach the script and the model?

by Niklas Worschech, 7 years ago

Attachment: ParameterExtendsTest.mos added

by Niklas Worschech, 7 years ago

Attachment: ParameterExtendsTest.mo added

comment:7 by Adrian Pop, 7 years ago

Component: FrontendInteractive Environment

I found the issue. If you use it like this:

getExtendsModifierValue(
  ParameterExtendsTest.SpecialRecord,
  ParameterExtendsTest.BaseRecord, 
  a);

then it works fine, but if you give it a string:

getExtendsModifierValue(
  ParameterExtendsTest.SpecialRecord,
  ParameterExtendsTest.BaseRecord, 
  "a");

which is basically what you give it, then it fails. It should work with:

getExtendsModifierValue(
  ParameterExtendsTest.SpecialRecord,
  stringTypeName(classes[i]),
  stringVariableName(params[j]);

but currently it doesn't, I will see if I can make it work like this.

comment:8 by Adrian Pop, 7 years ago

Added new API:

loadFile("Ticket4674.mo");
getInstantiatedParametersAndValues(Ticket4674.SpecialRecord); getErrorString();

results in:

adrpo33@ida-0030 MINGW64 /c/home/adrpo33/dev/OMTesting/bugs/4674
$ ~/dev/OpenModelica/build/bin/omc Ticket4674.mos
true
{"a = 2.1","b = 4.1","c = 8.1","d = 16.1","e = 32.1"}
""

@niklwors: you can see the implementation here:
https://github.com/OpenModelica/OMCompiler/pull/2155/
if you want to change how the output should be.

Note: See TracTickets for help on using tickets.