【问题标题】:How to replace deprecated function RectangularNode in AnyLogic?如何替换 AnyLogic 中已弃用的函数 RectangularNode?
【发布时间】:2020-10-22 13:11:40
【问题描述】:

我正在尝试运行几年前开发的 AnyLogic 模型,但我无权联系编写它的人。我对Java的了解非常有限。该模型是基于从 XML 文件中读取的数据构建的。第一步是从 XML 文件中读取布局数据并绘制布局。它曾经可以正常工作,但随着 AnyLogic 版本的更改,它停止了工作。我收到以下错误:

" (RectangularNode) : 空间标记元素的创建没有完成,请使用initialize()函数。"

突出显示为错误源的代码是:

                    ((Agent)temp).setLocation(shapeLayoutObj);

有趣的是,在此错误发生之前确实绘制了几个 RectangularNodes。周围的代码如下。上面在控制台窗口中标识为错误源的语句靠近下面提供的代码的底部。

/* Layout drawing */
//Initializing objects
int scale=30;
Layout layout= CMSDDocument.getDataSection().getLayout();
List<LayoutObject> layoutObj =CMSDDocument.getDataSection().getLayoutObject();
double xLoc, yLoc, width, depth; 
double xOffset = 50.0;
double yOffset = 100.0;
String layoutName;
width = layout.getBoundary().getWidth().doubleValue()*scale;
depth = layout.getBoundary().getDepth().doubleValue()*scale;
RectangularNode shapeCellArea = new RectangularNode(this, SHAPE_DRAW_2D3D, true, xOffset, yOffset, 0.0, width, depth, 0, null, Color.red, 2.0, LINE_STYLE_SOLID, POSITION_CHOICE_RANDOM, new Attractor(0,0,0));
ShapeText text = new ShapeText(SHAPE_DRAW_2D3D,true,xOffset+width/2,yOffset-20,0.0,0.0,red,"SHOP FLOOR",new Font ("Calibri", Font.BOLD , 15),TextAlignment.ALIGNMENT_CENTER);
presentation.add(shapeCellArea);
presentation.add(text);
LinkedHashMap<String,RectangularNode> floorLayout=new LinkedHashMap<String,RectangularNode>();
boolean found;
int j;
//For every layout description
for(int i= 0; i<layout.getPlacement().size();i++){
    //Getting the name and the description
    layoutName = layout.getPlacement().get(i).getLayoutElementIdentifier();
    xLoc = layout.getPlacement().get(i).getLocation().getX().doubleValue()*scale + xOffset;
    yLoc = layout.getPlacement().get(i).getLocation().getY().doubleValue()*scale + yOffset;
    found = false;
    j = 0;
    //Looking for the layout object with tthe same name as the layout description
    while (!found && j<layoutObj.size()){
        if (layoutName.equals(layoutObj.get(j).getIdentifier())) {
            //Getting layou values
            width = layoutObj.get(j).getBoundary().getWidth().doubleValue()*scale;
            depth = layoutObj.get(j).getBoundary().getDepth().doubleValue()*scale;
            RectangularNode shapeLayoutObj=null;
            //Configure the shape
            if(layoutName.contains("Mc")){
                shapeLayoutObj = new RectangularNode(this, SHAPE_DRAW_2D3D, true, xLoc, yLoc, 0.0, width, depth, 0, null, Color.black, 2.0, LINE_STYLE_DASHED, POSITION_CHOICE_BY_ATTRACTORS,new Attractor(width/2,depth/2,0));
            }
            else{
                shapeLayoutObj = new RectangularNode(this, SHAPE_DRAW_2D3D, true, xLoc, yLoc, 0.0, width, depth, 0, null, Color.black, 2.0, LINE_STYLE_DASHED, POSITION_CHOICE_RANDOM);
            }
            text = new ShapeText(SHAPE_DRAW_2D3D,true,xLoc+width/2,yLoc-15,0.0,0.0,black,layoutName,new Font ("Calibri", Font.PLAIN , 11),TextAlignment.ALIGNMENT_CENTER);
            presentation.add(shapeLayoutObj);
            presentation.add(text);
            //Associate the shape with a machine or a delay
            Iterator<ArrayList<Machine>> cell=machineList.iterator();
            boolean foundAgent=false;
            while(cell.hasNext()&&!foundAgent){
            Iterator<Machine> machines=cell.next().iterator();
                while(machines.hasNext()&&!foundAgent){
                    Machine temp=((Machine)machines.next());
                    if(temp.name.equals(layoutName)){
                        foundAgent=true;
                        ((Agent)temp).setLocation(shapeLayoutObj);
                        ShapeButton button = new ShapeButton(Main.this, true, xLoc,yLoc+depth+5,40, 20.0,controlDefault, controlDefault, 
                        true,new Font("Dialog", 0, 11 ),"Go to" ) {
                            public void action(){
                                getExperimentHost().setPresentable(temp);
                            }  
                        };
                        presentation.add(button);
                    }
                }
            }

我从过去的交流中了解到 AnyLogic 从 Java Swing(在开发模型时的 AnyLogic7 中)切换到了基于 Web 的 UI,我想知道这个错误是否是由于该切换造成的。在 AnyLogic 帮助文件中,RectangularNode 函数被标识为“在 8.4 版本中已弃用,将在下一个版本中删除”。我现在使用的是 AnyLogic 8 Personal Learning Edition 8.5.2,因此我认为此功能已被删除。希望错误信息这么说!还希望帮助文件建议与不推荐使用的功能相对应的新功能。令人困惑的是,“矩形节点”在当前版本中继续显示为流程建模库选项板上的空间标记元素。猜猜那个元素的底层代码已经改变了。

我查看了 AnyLogic 的帮助,我认为“ShapeRectangle”可能是我应该用来替换“RectangularNode”函数但“ShapeRectangle”函数不允许定义吸引子的新函数。并且“ShapeRectangle”函数似乎对应于 Presentation Palette 中的 Rectangle 元素,而不是 Process Modeling Library 中的 Rectangular 节点。

请指点我正确的方向。我应该替换 RectangularNode 的所有实例还是错误是别的?如果我应该替换 RectangularNode 函数,我应该改用什么函数?

【问题讨论】:

    标签: java deprecated anylogic


    【解决方案1】:

    一位乐于助人的同事指出: "

    1. 看起来只有带有 ShapeDraw 参数的 RectangularNode 构造函数被弃用了。基本的不是。
    2. RectangularNode 的父类AreaNode 有添加吸引子的方法。

    这会让我想到: a) 你不应该替换 RectangularNode。 ShapeRectangle 与形状有关,但您不能仅将形状用作节点。 b) AreaNode 是 RectangularNode 的超类,具有添加吸引子的方法,因此您可以使用它们。”

    根据建议和一些试验和错误,我使用以下语句替换已弃用的 RectangularNode 版本。编译器不再显示有关 RectangularNode 被弃用的消息。

    // Replacement for above statement added below by SJ on 7/3/20
    RectangularNode shapeCellArea = new RectangularNode(this);
    shapeCellArea.setPos(xOffset, yOffset, 0.0);
    shapeCellArea.setLineColor(Color.red);
    shapeCellArea.setLineStyle(LINE_STYLE_SOLID);
    shapeCellArea.setLineWidth(2.0);
    shapeCellArea.setPositionChoiceMode(POSITION_CHOICE_RANDOM);
    shapeCellArea.setSize(width,depth);
    shapeCellArea.addAttractor(new Attractor(0,0,0));
    // ShapeDrawMode SHAPE_DRAW_2D3D is default mode so don't have to set that.
    // end of code added by SJ on 7/3/20
    

    很遗憾,上述更改并未消除错误。仍在尝试找出错误消息并将其作为新问题发布。

    --------- 上述进度更新于 2020 年 7 月 4 日发布。以下附加信息于 2020 年 7 月 17 日添加 ------------

    通过一些在线搜索并在@Florian How can i create Path space markup element in Anylogic via Code 对另一个问题的回答的指导下,我通过将所有节点添加到网络、将网络添加到关卡、然后初始化关卡来解决这个问题。更新后的代码如下。

    Network netwkSJ = new Network(this, "mynetwork"); 
    ....
    LinkedHashMap<String,RectangularNode> floorLayout=new LinkedHashMap<String,RectangularNode>();
    
    // the loop in the original question here 
    // added the following statement within the loop
    .......
                netwkSJ.add(shapeLayoutObj); 
                floorLayout.put(layoutName, shapeLayoutObj); 
    
    // Commented out the statement that was generating the error in the loop
    // and moved it within the new loop below
                            //((Agent)temp).setLocation(shapeLayoutObj); // statement giving error
    
    .......
    
    Level level = new Level(this, "myLevel", SHAPE_DRAW_2D3D, 0);
    level.add(netwkSJ);
    level.initialize(); // cannot be changed after initialization!
    
    Set set = floorLayout.entrySet();
    //  iterating through elements of floorLayout to link machines to corresponding layout locations
             floorLayout.forEach((Name,node) -> {
             //Associate the node with a machine 
             Iterator<ArrayList<Machine>> cell=machineList.iterator();
             boolean foundAgent=false;
             while(cell.hasNext()&&!foundAgent){
                Iterator<Machine> machines=cell.next().iterator();
                    while(machines.hasNext()&&!foundAgent){
                        Machine temp=((Machine)machines.next());
                        if(temp.name.equals(Name)){
                            foundAgent=true;            
                            ((Agent)temp).setLocation(node); 
                            // System.out.println(Name + " -> " );
                                }  
                            };
                        };
                });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-20
      • 1970-01-01
      • 1970-01-01
      • 2013-05-08
      • 2019-12-15
      • 2020-02-27
      • 2013-01-13
      • 2013-04-17
      相关资源
      最近更新 更多