1

我尝试制作一个参数化库。我使用包和连接器作为参数工作正常。

使用模型作为参数也是可能的。但是,如果在库中使用模型来使用扩展构建新模型,那么这是不允许的,我理解。

我想知道库是否包含具有内部/外部连接器样式的模型,是否允许让内部模型成为库的参数?

下面用一个简单的例子来说明问题。TEST 是库,Fish3b 是应用程序。当我在库 TEST 中运行示例时,一切正常,但是当我有一个单独的应用程序文件时,它就不行了。

错误文本是:找不到运行 JModelica 2.4 的 AquariumType 的类声明

package TEST

   model FishType1
       outer Real T;
       Real health;
   equation
       health = 30-T;
   end FishType1;

   model FishType2
       outer Real T;
       Real health;
   equation
       health = 32-T;
   end FishType2;

   package Equipment
       model AquariumType
           replaceable model FishType
           end FishType;       
           FishType fish;
           inner Real T;
       equation
          T = 29;
       end AquariumType;
   end Equipment;

   // Adapt AquariumType model to actual fish
   model Aquarium
       import TEST.Equipment.AquariumType;
       extends AquariumType(redeclare model FishType=FishType2);
   end Aquarium;

   // Example
   model Example
       Aquarium aquarium;
   end Example;

end TEST;

下面是从上面的库导入的应用程序代码示例 - 我认为这是一些错误。

   encapsulated package Fish3b

       model FishType3
           outer Real T;
           Real health;
       equation
           health = 34-T;
       end FishType3;

       // Adapt package Equipment with AquariumType model to actual fish
       package Equipment3 
           import TEST.Equipment;
           extends Equipment.AquariumType(redeclare model   FishType=FishType3);
       end Equipment3;

       // Example
       model Example
           import Fish3b.Equipment3;
           Equipment3.AquariumType aquarium;
       end Example;

   end Fish3b;
4

3 回答 3

2

感谢“tbeu”的评论!

我已经修改了代码,以便不会从模型扩展包 Equipment。下面更新的代码也更好地代表了我的潜在“真实”问题。这一切都有效。谢谢!

更新后的库文件 TEST2.mo:

   package TEST2

       model FishType1
           outer Real T;
           Real health;
       equation
           health = 30-T;
       end FishType1;

       model FishType2
           outer Real T;
           Real health;
       equation
           health = 32-T;
       end FishType2;

       package Equipment
           replaceable model FishType
           end FishType;

           constant Integer dummy = 1;

           model AquariumType
               FishType fish;
               inner Real T;
           equation
               T = 29;
           end AquariumType;
       end Equipment;

       // Adapt package Equipment to the actual fish
       package Equipment1
           import TEST2.Equipment;
           extends Equipment(redeclare model FishType=FishType1);
       end Equipment1;

       // Example
       model Example
           Equipment1.AquariumType aquarium;
       end Example;

    end TEST2;

而现在使用上述库 TEST2 的应用程序代码 T2_Fish3:

   encapsulated package T2_Fish3

       model FishType3
           outer Real T;
           Real health;
       equation
           health = 34-T;
       end FishType3;

       // Adapt package Equipment to the actual fish
       package Equipment3
           import TEST2.Equipment;
           extends Equipment(redeclare model FishType=FishType3);
       end Equipment3;

       // Example
       model Example
           Equipment3.AquariumType aquarium;
       end Example;

   end T2_Fish3;
于 2019-03-13T07:54:00.473 回答
1

janpeter 的回答很有效。

另一种避免引入名为“FishType1”、“FishType3”等的模型的替代方法是使用“重新声明模型扩展”,如下所示(对于 Equipment1,Test2 可以保持不变或相同的变化),但它使用更高级的构造。

encapsulated package T2_Fish3

       // Adapt package Equipment to the actual fish
       package Equipment3
           import TEST2.Equipment;
           extends Equipment;

           redeclare model extends FishType
            outer Real T;
            Real health;
           equation 
             health = 32-T;
           end FishType;
       end Equipment3;

       // Example
       model Example
           Equipment3.AquariumType aquarium;
       end Example;

end T2_Fish3;

此外,可以将常见的“外部 Real T”移动到基本模型 FishType,从而导致:

package TEST2

    package Equipment
        replaceable model FishType
          outer Real T;
        end FishType;

        constant Integer dummy = 1;

        model AquariumType
            FishType fish;
            inner Real T;
        equation 
            T = 29;
        end AquariumType;
    end Equipment;

    // Adapt package Equipment to the actual fish
    package Equipment1
      import TEST2.Equipment;

      extends Equipment;
      redeclare model extends FishType
        Real health;
      equation 
        health = 30 - T;
      end FishType;
    end Equipment1;

    // Example
    model Example
        Equipment1.AquariumType aquarium;
    end Example;

end TEST2;

encapsulated package T2_Fish3

       // Adapt package Equipment to the actual fish
       package Equipment3
           import TEST2.Equipment;
           extends Equipment;

           redeclare model extends FishType
            Real health;
           equation 
             health = 32-T;
           end FishType;
       end Equipment3;

       // Example
       model Example
           Equipment3.AquariumType aquarium;
       end Example;

end T2_Fish3;
于 2019-03-13T10:46:23.087 回答
0

之前使用 Hans Olsson 的输入更新了代码,但与上面建议的更正略有不同。我设法避免“从可替换模型扩展”,但我不太确定这有多重要,请参阅我之前的评论。

另外,我想我想保持不同鱼的明确身份,并在包装​​设备外声明它们。

    package TEST3

        partial model FishBase
            outer Real T;
        end FishBase;

        model FishType1
            extends FishBase;
            Real health;
        equation
            health = 30 - T;
        end FishType1;

        model FishType2
            extends FishBase;
            Real health;
        equation
            health = 32 - T;
        end FishType2;  

        package Equipment
            replaceable model FishType
            end FishType;

            constant Integer dummy = 1;

            model AquariumType
                FishType fish;
                inner Real T;
            equation
                T = 29;
            end AquariumType;
        end Equipment;

        // Adapt package Equipment to the actual fish
        package Equipment3
            import TEST3.Equipment;
            extends Equipment;
            redeclare model FishType=FishType2;
        end Equipment3;

        // Example
        model Example
            Equipment3.AquariumType aquarium;
        end Example;

    end TEST3;

以及应用程序代码示例:

    encapsulated package T3_Fish3

        model FishType3
            import TEST3.FishBase;
            extends FishBase;
            Real health;
        equation
            health = 34-T;
        end FishType3;

        // Adapt package Equipment to the actual fish
        package Equipment3
            import TEST3.Equipment;
            extends Equipment(redeclare model FishType=FishType3);
        end Equipment3;

        // Example
        model Example
            Equipment3.AquariumType aquarium;
        end Example;

    end T3_Fish3;
于 2019-03-14T15:09:14.863 回答