Package ptolemy.kernel

Examples of ptolemy.kernel.Entity


            // Mirror the change in mirror ports.
            ModalModel container = (ModalModel) getContainer();
            Iterator entities = container.entityList().iterator();

            while (entities.hasNext()) {
                Entity entity = (Entity) entities.next();
                Port mirrorPort = entity.getPort(getName());

                if (mirrorPort instanceof RefinementPort) {
                    RefinementPort castPort = (RefinementPort) mirrorPort;
                    boolean disableStatus = castPort._mirrorDisable;

                    try {
                        castPort._mirrorDisable = true;
                        castPort.setOutput(isOutput);
                    } finally {
                        castPort._mirrorDisable = disableStatus;
                    }

                    // If the entity is a controller, then set the
                    // port to also be an input.
                    if (entity.getName().equals("_Controller")) {
                        boolean controlPortStatus = castPort._mirrorDisable;

                        try {
                            castPort._mirrorDisable = true;
                            castPort.setInput(true);
View Full Code Here


                            ((CTReceiver) receivers[i][j]).setSignalType(type);
                        }
                    }
                }

                Entity actor = (Entity) port.getContainer();

                if ((type == CONTINUOUS)
                        && (actor != CTScheduler.this.getContainer()
                                .getContainer())
                        && !_continuousActors.contains(actor)) {
View Full Code Here

            super.actionPerformed(event);

            NamedObj object = getTarget();

            if (object instanceof Entity) {
                Entity entity = (Entity) object;
                try {
                    UserActorLibrary.saveComponentInLibrary(_configuration,
                            entity);
                } catch (Exception ex) {
                    // We catch exceptions here because this method used to
                    // not throw Exceptions, and we don't want to break compatibility.
                    MessageHandler.error("Failed to save \"" + entity.getName()
                            + "\".");
                }
            }
        }
View Full Code Here

            // Create mirror ports.
            Iterator entities = entityList().iterator();

            while (entities.hasNext()) {
                Entity entity = (Entity) entities.next();

                if (entity instanceof ModalController) {
                    if (entity.getPort(name) == null) {
                        try {
                            ((ModalController) entity)._mirrorDisable = true;

                            /*Port newPort = */entity.newPort(name);

                            /* No longer needed since Yuhong modified
                             * the type system to allow UNKNOWN. EAL
                             if (newPort instanceof TypedIOPort) {
                             ((TypedIOPort)newPort).setTypeSameAs(port);
                             }
                             */
                        } finally {
                            ((ModalController) entity)._mirrorDisable = false;
                        }
                    }
                } else if (entity instanceof Refinement) {
                    if (entity.getPort(name) == null) {
                        try {
                            ((Refinement) entity)._mirrorDisable = true;

                            /*Port newPort = */entity.newPort(name);

                            /* No longer needed since Yuhong modified
                             * the type system to allow UNKNOWN. EAL
                             if (newPort instanceof TypedIOPort) {
                             ((TypedIOPort)newPort).setTypeSameAs(port);
View Full Code Here

                _inConnectionsChanged = true;

                while (insidePorts.hasNext()) {
                    ComponentPort insidePort = (ComponentPort) insidePorts
                            .next();
                    Entity portContainer = (Entity) insidePort.getContainer();

                    // Avoid an infinite loop where notifications are traded.
                    if (!(portContainer instanceof CompositeActor)
                            || !((CompositeActor) portContainer)._inConnectionsChanged) {
                        portContainer.connectionsChanged(insidePort);
                    }
                }
            } finally {
                _inConnectionsChanged = false;
            }
View Full Code Here

            }

            // Clear all of the contained actor's input ports.
            for (Iterator actors = deepEntityList().iterator(); actors
                    .hasNext();) {
                Entity actor = (Entity) actors.next();
                Iterator ports = actor.portList().iterator();

                while (ports.hasNext()) {
                    IOPort port = (IOPort) ports.next();

                    if (port.isInput()) {
View Full Code Here

            //                     model, body.getThisLocal(),
            //                     model, body.getThisLocal(),
            //                     modelClass);
            for (Iterator entities = model.deepEntityList().iterator(); entities
                    .hasNext();) {
                Entity entity = (Entity) entities.next();
                String fieldName = ModelTransformer.getFieldNameForEntity(
                        entity, model);
                SootField field = modelClass.getFieldByName(fieldName);
                String className = ModelTransformer.getInstanceClassName(
                        entity, options);
                SootClass theClass = Scene.v().loadClassAndSupport(className);
                SootMethod preinitializeMethod = SootUtilities
                        .searchForMethodByName(theClass, "preinitialize");
                Local actorLocal = Jimple.v().newLocal("actor",
                        RefType.v(theClass));
                body.getLocals().add(actorLocal);

                // Get the field.
                units.insertBefore(Jimple.v().newAssignStmt(
                        actorLocal,
                        Jimple.v().newInstanceFieldRef(thisLocal,
                                field.makeRef())), insertPoint);
                units.insertBefore(Jimple.v().newInvokeStmt(
                        Jimple.v().newVirtualInvokeExpr(actorLocal,
                                preinitializeMethod.makeRef())), insertPoint);
            }

            //           units.add(Jimple.v().newReturnVoidStmt());
        }

        {
            // populate the initialize method
            SootMethod classMethod = modelClass.getMethodByName("initialize");
            JimpleBody body = (JimpleBody) classMethod.getActiveBody();
            Stmt insertPoint = body.getFirstNonIdentityStmt();

            Chain units = body.getUnits();
            Local thisLocal = body.getThisLocal();

            Local actorLocal = Jimple.v().newLocal("actor", actorType);
            body.getLocals().add(actorLocal);

            for (Iterator entities = model.deepEntityList().iterator(); entities
                    .hasNext();) {
                Entity entity = (Entity) entities.next();
                String fieldName = ModelTransformer.getFieldNameForEntity(
                        entity, model);
                SootField field = modelClass.getFieldByName(fieldName);
                String className = ModelTransformer.getInstanceClassName(
                        entity, options);
                SootClass theClass = Scene.v().loadClassAndSupport(className);
                SootMethod initializeMethod = SootUtilities
                        .searchForMethodByName(theClass, "initialize");

                // Set the field.
                units.insertBefore(Jimple.v().newAssignStmt(
                        actorLocal,
                        Jimple.v().newInstanceFieldRef(thisLocal,
                                field.makeRef())), insertPoint);
                units.insertBefore(Jimple.v().newInvokeStmt(
                        Jimple.v().newVirtualInvokeExpr(actorLocal,
                                initializeMethod.makeRef())), insertPoint);
            }

            //           units.add(Jimple.v().newReturnVoidStmt());
        }

        {
            // populate the prefire method
            SootMethod classMethod = modelClass.getMethodByName("prefire");
            JimpleBody body = (JimpleBody) classMethod.getActiveBody();
            Chain units = body.getUnits();
            Stmt insertPoint = (Stmt) units.getLast();

            Local thisLocal = body.getThisLocal();

            Local prefireReturnsLocal = Jimple.v().newLocal("preReturns",
                    BooleanType.v());
            body.getLocals().add(prefireReturnsLocal);

            // Prefire the controller.
            Local actorLocal = Jimple.v().newLocal("actor", actorType);
            body.getLocals().add(actorLocal);

            String fieldName = ModelTransformer.getFieldNameForEntity(
                    controller, model);
            SootField field = modelClass.getFieldByName(fieldName);
            String className = ModelTransformer.getInstanceClassName(
                    controller, options);
            SootClass theClass = Scene.v().loadClassAndSupport(className);
            SootMethod actorPrefireMethod = SootUtilities
                    .searchForMethodByName(theClass, "prefire");

            units.insertBefore(
                    Jimple.v().newAssignStmt(
                            actorLocal,
                            Jimple.v().newInstanceFieldRef(thisLocal,
                                    field.makeRef())), insertPoint);
            units.insertBefore(Jimple.v().newAssignStmt(
                    prefireReturnsLocal,
                    Jimple.v().newVirtualInvokeExpr(actorLocal,
                            actorPrefireMethod.makeRef())), insertPoint);

            units.insertBefore(Jimple.v().newReturnStmt(prefireReturnsLocal),
                    insertPoint);

            LocalSplitter.v().transform(body, phaseName + ".lns");
            LocalNameStandardizer.v().transform(body, phaseName + ".lns");
            TypeResolver.resolve(body, Scene.v());
        }

        {
            // populate the fire method
            SootMethod classMethod = modelClass.getMethodByName("fire");
            JimpleBody body = (JimpleBody) classMethod.getActiveBody();
            Stmt insertPoint = body.getFirstNonIdentityStmt();

            Chain units = body.getUnits();
            Local thisLocal = body.getThisLocal();

            Local indexLocal = Jimple.v().newLocal("index", IntType.v());
            body.getLocals().add(indexLocal);

            Local tokenLocal = Jimple.v().newLocal("token",
                    PtolemyUtilities.tokenType);
            body.getLocals().add(tokenLocal);

            // Transfer Inputs from input ports.
            for (Iterator ports = model.inputPortList().iterator(); ports
                    .hasNext();) {
                IOPort port = (IOPort) ports.next();
                int rate = 1;

                String fieldName = ModelTransformer.getFieldNameForPort(port,
                        model);
                SootField field = modelClass.getFieldByName(fieldName);

                // Get a reference to the port.
                Local portLocal = Jimple.v().newLocal("port",
                        PtolemyUtilities.ioportType);
                body.getLocals().add(portLocal);

                Local tempPortLocal = Jimple.v().newLocal("tempPort",
                        PtolemyUtilities.ioportType);
                body.getLocals().add(tempPortLocal);
                units.insertBefore(Jimple.v().newAssignStmt(
                        tempPortLocal,
                        Jimple.v().newInstanceFieldRef(thisLocal,
                                field.makeRef())), insertPoint);
                units.insertBefore(Jimple.v().newAssignStmt(
                        portLocal,
                        Jimple.v().newCastExpr(tempPortLocal,
                                PtolemyUtilities.ioportType)), insertPoint);

                for (int i = 0; i < port.getWidth(); i++) {
                    // The list of initializer instructions.
                    List initializerList = new LinkedList();
                    initializerList.add(Jimple.v().newAssignStmt(indexLocal,
                            IntConstant.v(0)));

                    // The list of body instructions.
                    List bodyList = new LinkedList();

                    // Read
                    bodyList.add(Jimple.v().newAssignStmt(
                            tokenLocal,
                            Jimple.v().newVirtualInvokeExpr(portLocal,
                                    PtolemyUtilities.getMethod.makeRef(),
                                    IntConstant.v(i))));

                    // Write
                    bodyList.add(Jimple.v().newInvokeStmt(
                            Jimple.v()
                                    .newVirtualInvokeExpr(
                                            portLocal,
                                            PtolemyUtilities.sendInsideMethod
                                                    .makeRef(),
                                            IntConstant.v(i), tokenLocal)));

                    // Increment the index.
                    bodyList.add(Jimple.v()
                            .newAssignStmt(
                                    indexLocal,
                                    Jimple.v().newAddExpr(indexLocal,
                                            IntConstant.v(1))));

                    Expr conditionalExpr = Jimple.v().newLtExpr(indexLocal,
                            IntConstant.v(rate));

                    SootUtilities.createForLoopBefore(body, insertPoint,
                            initializerList, bodyList, conditionalExpr);
                }
            }

            {
                // Fire the controller.
                Local actorLocal = Jimple.v().newLocal("actor", actorType);
                body.getLocals().add(actorLocal);

                String fieldName = ModelTransformer.getFieldNameForEntity(
                        controller, model);
                SootField field = modelClass.getFieldByName(fieldName);
                String className = ModelTransformer.getInstanceClassName(
                        controller, options);
                SootClass theClass = Scene.v().loadClassAndSupport(className);
                SootMethod actorFireMethod = SootUtilities
                        .searchForMethodByName(theClass, "fire");

                units.insertBefore(Jimple.v().newAssignStmt(
                        actorLocal,
                        Jimple.v().newInstanceFieldRef(thisLocal,
                                field.makeRef())), insertPoint);
                units.insertBefore(Jimple.v().newInvokeStmt(
                        Jimple.v().newVirtualInvokeExpr(actorLocal,
                                actorFireMethod.makeRef())), insertPoint);
            }

            // Transfer outputs from output ports
            for (Iterator ports = model.outputPortList().iterator(); ports
                    .hasNext();) {
                IOPort port = (IOPort) ports.next();
                int rate = DFUtilities.getTokenProductionRate(port);

                String fieldName = ModelTransformer.getFieldNameForPort(port,
                        model);
                SootField field = modelClass.getFieldByName(fieldName);

                // Get a reference to the port.
                Local portLocal = Jimple.v().newLocal("port",
                        PtolemyUtilities.ioportType);
                body.getLocals().add(portLocal);

                Local tempPortLocal = Jimple.v().newLocal("tempPort",
                        PtolemyUtilities.ioportType);
                body.getLocals().add(tempPortLocal);
                units.insertBefore(Jimple.v().newAssignStmt(
                        tempPortLocal,
                        Jimple.v().newInstanceFieldRef(thisLocal,
                                field.makeRef())), insertPoint);
                units.insertBefore(Jimple.v().newAssignStmt(
                        portLocal,
                        Jimple.v().newCastExpr(tempPortLocal,
                                PtolemyUtilities.ioportType)), insertPoint);

                for (int i = 0; i < port.getWidth(); i++) {
                    // The list of initializer instructions.
                    List initializerList = new LinkedList();
                    initializerList.add(Jimple.v().newAssignStmt(indexLocal,
                            IntConstant.v(0)));

                    // The list of body instructions.
                    List bodyList = new LinkedList();

                    // Read
                    bodyList.add(Jimple.v().newAssignStmt(
                            tokenLocal,
                            Jimple.v().newVirtualInvokeExpr(portLocal,
                                    PtolemyUtilities.getInsideMethod.makeRef(),
                                    IntConstant.v(i))));

                    // Write
                    bodyList.add(Jimple.v().newInvokeStmt(
                            Jimple.v().newVirtualInvokeExpr(portLocal,
                                    PtolemyUtilities.sendMethod.makeRef(),
                                    IntConstant.v(i), tokenLocal)));

                    // Increment the index.
                    bodyList.add(Jimple.v()
                            .newAssignStmt(
                                    indexLocal,
                                    Jimple.v().newAddExpr(indexLocal,
                                            IntConstant.v(1))));

                    Expr conditionalExpr = Jimple.v().newLtExpr(indexLocal,
                            IntConstant.v(rate));

                    SootUtilities.createForLoopBefore(body, insertPoint,
                            initializerList, bodyList, conditionalExpr);
                }
            }

            // Return.
            //            units.add(Jimple.v().newReturnVoidStmt());
            LocalSplitter.v().transform(body, phaseName + ".lns");
            LocalNameStandardizer.v().transform(body, phaseName + ".lns");
            TypeResolver.resolve(body, Scene.v());
        }

        {
            // populate the postfire method
            SootMethod classMethod = modelClass.getMethodByName("postfire");
            JimpleBody body = (JimpleBody) classMethod.getActiveBody();
            Stmt insertPoint = body.getFirstNonIdentityStmt();

            Chain units = body.getUnits();
            Local thisLocal = body.getThisLocal();

            Local postfireReturnsLocal = Jimple.v().newLocal("postfireReturns",
                    BooleanType.v());
            body.getLocals().add(postfireReturnsLocal);

            // Postfire the controller.
            Local actorLocal = Jimple.v().newLocal("actor", actorType);
            body.getLocals().add(actorLocal);

            String fieldName = ModelTransformer.getFieldNameForEntity(
                    controller, model);
            SootField field = modelClass.getFieldByName(fieldName);
            String className = ModelTransformer.getInstanceClassName(
                    controller, options);
            SootClass theClass = Scene.v().loadClassAndSupport(className);
            SootMethod actorPostfireMethod = SootUtilities
                    .searchForMethodByName(theClass, "postfire");

            units.insertBefore(
                    Jimple.v().newAssignStmt(
                            actorLocal,
                            Jimple.v().newInstanceFieldRef(thisLocal,
                                    field.makeRef())), insertPoint);
            units.insertBefore(Jimple.v().newAssignStmt(
                    postfireReturnsLocal,
                    Jimple.v().newVirtualInvokeExpr(actorLocal,
                            actorPostfireMethod.makeRef())), insertPoint);

            units.insertBefore(Jimple.v().newReturnStmt(postfireReturnsLocal),
                    insertPoint);
            LocalSplitter.v().transform(body, phaseName + ".lns");
            LocalNameStandardizer.v().transform(body, phaseName + ".lns");
            TypeResolver.resolve(body, Scene.v());
        }

        {
            // populate the wrapup method
            SootMethod classMethod = modelClass.getMethodByName("wrapup");
            JimpleBody body = (JimpleBody) classMethod.getActiveBody();
            Stmt insertPoint = body.getFirstNonIdentityStmt();

            Chain units = body.getUnits();
            Local thisLocal = body.getThisLocal();

            Local actorLocal = Jimple.v().newLocal("actor", actorType);
            body.getLocals().add(actorLocal);

            for (Iterator entities = model.deepEntityList().iterator(); entities
                    .hasNext();) {
                Entity entity = (Entity) entities.next();
                String fieldName = ModelTransformer.getFieldNameForEntity(
                        entity, model);
                SootField field = modelClass.getFieldByName(fieldName);
                String className = ModelTransformer.getInstanceClassName(
                        entity, options);
View Full Code Here

                Local tokenLocal = Jimple.v().newLocal("token",
                        PtolemyUtilities.tokenType);
                _body.getLocals().add(tokenLocal);

                Entity entityContainer = FieldsForEntitiesTransformer
                        .getEntityContainerOfObject(result);
                String deepName = result.getName(entityContainer);

                _units.insertBefore(Jimple.v().newAssignStmt(containerLocal,
                        thisLocal), _insertPoint);
View Full Code Here

        if (transition == null) {
            throw new IllegalActionException(this,
                    "Action has no container transition.");
        }

        Entity fsm = (Entity) transition.getContainer();

        if (fsm == null) {
            throw new IllegalActionException(this, transition,
                    "Transition has no container.");
        }

        IOPort port = (IOPort) fsm.getPort(name);

        if (port == null) {
            throw new IllegalActionException(fsm, this,
                    "Cannot find port with name: " + name);
        }
View Full Code Here

                _checkForNull(entityName, "No name for element \"class\"");

                // For undo purposes need to know if the entity existed
                // already
                Entity entity = _searchForEntity(entityName, _current);
                boolean existedAlready = (entity != null);

                if (!existedAlready) {
                    NamedObj candidate = _createEntity(className, entityName,
                            source);

                    if (candidate instanceof Entity) {
                        entity = (Entity) candidate;
                    } else {
                        throw new IllegalActionException(_current,
                                "Attempt to create a class named " + entityName
                                        + " from a class that "
                                        + "is not a subclass of Entity: "
                                        + className);
                    }
                }

                // NOTE: The entity may be at the top level, in
                // which case _deleteRequests is null.
                if (_deleteRequests != null) {
                    _deleteRequestStack.push(_deleteRequests);
                    pushedDeleteRequests = true;
                }

                _deleteRequests = new LinkedList();

                // NOTE: The entity may be at the top level, in
                // which case _linkRequests is null.
                if (_linkRequests != null) {
                    _linkRequestStack.push(_linkRequests);
                    pushedLinkRequests = true;
                }

                _linkRequests = new LinkedList();

                if (_current != null) {
                    _pushContext();
                } else if (_toplevel == null) {
                    // NOTE: Used to set _toplevel to newEntity, but
                    // this isn't quite right because the entity may have a
                    // composite name.
                    _toplevel = entity.toplevel();

                    // Ensure that if any change requests occur as a
                    // consequence of adding items to this top level,
                    // that execution of those change requests is deferred
                    // until endDocument().
                    _toplevel.setDeferringChangeRequests(true);

                    // As early as possible, set URL attribute.
                    // This is needed in case any of the parameters
                    // refer to files whose location is relative
                    // to the URL location.
                    if (_xmlFile != null) {
                        // Add a URL attribute to the toplevel to
                        // indicate where it was read from.
                        URIAttribute attribute = new URIAttribute(_toplevel,
                                "_uri");
                        attribute.setURL(_xmlFile);
                    }
                }

                boolean converted = false;

                if (!existedAlready) {
                    entity.setClassDefinition(true);

                    // Adjust the classname and superclass of the object.
                    // NOTE: This used to set the class name to entity.getFullName(),
                    // and superclass to className.  Now that we've consolidated
                    // these, we set the class name to the value of "extends"
                    // attribute that was used to create this.
                    entity.setClassName(className);
                } else {
                    // If the object is not already a class, then convert
                    // it to one.
                    if (!entity.isClassDefinition()) {
                        entity.setClassDefinition(true);
                        converted = true;
                    }
                }

                _current = entity;

                _namespace = _DEFAULT_NAMESPACE;

                if (_undoEnabled) {
                    // Handle the undo aspect.
                    if (existedAlready) {
                        if (!converted) {
                            _undoContext.appendUndoMoML("<class name=\""
                                    + entityName + "\" >\n");

                            // Need to continue undoing and use an end tag
                            _undoContext.appendClosingUndoMoML("</class>\n");
                        } else {
                            // Converting from entity to class, so reverse this.
                            _undoContext.appendUndoMoML("<entity name=\""
                                    + entityName + "\" >\n");

                            // Need to continue undoing and use an end tag
                            _undoContext.appendClosingUndoMoML("</entity>\n");
                        }

                        _undoContext.setChildrenUndoable(true);
                    } else {
                        _undoContext.appendUndoMoML("<deleteEntity name=\""
                                + entityName + "\" />\n");

                        // Do not need to continue generating undo MoML
                        // as the deleteEntity takes care of all
                        // contained MoML
                        _undoContext.setChildrenUndoable(false);
                        _undoContext.setUndoable(false);

                        // Prevent any further undo entries for this context.
                        _undoEnabled = false;
                    }
                }

                //////////////////////////////////////////////////////////////
                //// configure
            } else if (elementName.equals("configure")) {
                _checkClass(_current, Configurable.class,
                        "Element \"configure\" found inside an element that "
                                + "does not implement Configurable. It is: "
                                + _current);
                _configureSource = (String) _attributes.get("source");
                _currentCharData = new StringBuffer();

                // Count configure tags so that they can nest.
                _configureNesting++;

                //////////////////////////////////////////////////////////////
                //// deleteEntity
            } else if (elementName.equals("deleteEntity")) {
                String entityName = (String) _attributes.get("name");
                _checkForNull(entityName,
                        "No name for element \"deleteEntity\"");

                // Link is stored and processed last, but before deletions.
                DeleteRequest request = new DeleteRequest(_DELETE_ENTITY,
                        entityName, null);

                // Only defer if we are in a class, entity, or model context,
                // which is equivalent to the _current being an instance of
                // InstantiableNamedObj.
                if ((_deleteRequests != null)
                        && _current instanceof InstantiableNamedObj) {
                    _deleteRequests.add(request);
                } else {
                    // Very likely, the context is null, in which
                    // case the following will throw an exception.
                    // We defer to it in case somehow a link request
                    // is being made at the top level with a non-null
                    // context (e.g. via a change request).
                    request.execute();
                }

                // NOTE: deleteEntity is not supposed to have anything
                // inside it, so we do not push the context.
                //////////////////////////////////////////////////////////////
                //// deletePort
            } else if (elementName.equals("deletePort")) {
                String portName = (String) _attributes.get("name");
                _checkForNull(portName, "No name for element \"deletePort\"");

                // The entity attribute is optional.
                String entityName = (String) _attributes.get("entity");

                // Delete the corresponding ParameterPort, if any.
                Port toDelete = null;
                try {
                    toDelete = _searchForPort(portName);
                } catch (XmlException ex) {
                    // Ignore, there is no port by that name.
                }
                // Find the corresponding ParameterPort and delete it
                if (toDelete != null) {
                    NamedObj container = toDelete.getContainer();
                    if (container != null && container instanceof Entity) {
                        Attribute attribute = ((Entity) container)
                                .getAttribute(portName);
                        if (attribute != null
                                && attribute instanceof PortParameter) {
                            DeleteRequest request = new DeleteRequest(
                                    _DELETE_PROPERTY, attribute.getName(), null);
                            // Only defer if we are in a class, entity, or
                            // model context, which is equivalent to the
                            // _current being an instance of
                            // InstantiableNamedObj.
                            if ((_deleteRequests != null)
                                    && _current instanceof InstantiableNamedObj) {
                                _deleteRequests.add(request);
                            } else {
                                // Very likely, the context is null, in which
                                // case the following will throw an exception.
                                // We defer to it in case somehow a link request
                                // is being made at the top level with a non-null
                                // context (e.g. via a change request).
                                request.execute();
                            }
                        }
                    }
                }

                // Link is stored and processed last, but before deletions.
                DeleteRequest request = new DeleteRequest(_DELETE_PORT,
                        portName, entityName);

                // Only defer if we are in a class, entity, or model context,
                // which is equivalent to the _current being an instance of
                // InstantiableNamedObj.
                if ((_deleteRequests != null)
                        && _current instanceof InstantiableNamedObj) {
                    _deleteRequests.add(request);
                } else {
                    // Very likely, the context is null, in which
                    // case the following will throw an exception.
                    // We defer to it in case somehow a link request
                    // is being made at the top level with a non-null
                    // context (e.g. via a change request).
                    request.execute();
                }

                // NOTE: deletePort is not supposed to have anything
                // inside it, so we do not push the context.
                //////////////////////////////////////////////////////////////
                //// deleteProperty
            } else if (elementName.equals("deleteProperty")) {
                String propName = (String) _attributes.get("name");
                _checkForNull(propName,
                        "No name for element \"deleteProperty\"");

                // Link is stored and processed last, but before deletions.
                DeleteRequest request = new DeleteRequest(_DELETE_PROPERTY,
                        propName, null);

                // We use toDelete to find any PortParameters
                Attribute toDelete = _searchForAttribute(propName);

                // Only defer if we are in a class, entity, or model context,
                // which is equivalent to the _current being an instance of
                // InstantiableNamedObj.
                if ((_deleteRequests != null)
                        && _current instanceof InstantiableNamedObj) {
                    _deleteRequests.add(request);
                } else {
                    // Very likely, the context is null, in which
                    // case the following will throw an exception.
                    // We defer to it in case somehow a link request
                    // is being made at the top level with a non-null
                    // context (e.g. via a change request).
                    request.execute();
                }

                // Find the corresponding PortParameter and delete it
                NamedObj container = toDelete.getContainer();
                if (container != null && container instanceof Entity) {
                    Port port = ((Entity) container).getPort(propName);
                    if (port != null && port instanceof ParameterPort) {
                        request = new DeleteRequest(_DELETE_PORT, port
                                .getName(), container.getFullName());
                        // Only defer if we are in a class, entity, or
                        // model context, which is equivalent to the
                        // _current being an instance of
                        // InstantiableNamedObj.
                        if ((_deleteRequests != null)
                                && _current instanceof InstantiableNamedObj) {
                            _deleteRequests.add(request);
                        } else {
                            // Very likely, the context is null, in which
                            // case the following will throw an exception.
                            // We defer to it in case somehow a link request
                            // is being made at the top level with a non-null
                            // context (e.g. via a change request).
                            request.execute();
                        }
                    }
                }

                // NOTE: deleteProperty is not supposed to have anything
                // inside it, so we do not push the context.
                //////////////////////////////////////////////////////////////
                //// deleteRelation
            } else if (elementName.equals("deleteRelation")) {
                String relationName = (String) _attributes.get("name");
                _checkForNull(relationName,
                        "No name for element \"deleteRelation\"");

                // Link is stored and processed last, but before deletions.
                DeleteRequest request = new DeleteRequest(_DELETE_RELATION,
                        relationName, null);

                // Only defer if we are in a class, entity, or model context,
                // which is equivalent to the _current being an instance of
                // InstantiableNamedObj.
                if ((_deleteRequests != null)
                        && _current instanceof InstantiableNamedObj) {
                    _deleteRequests.add(request);
                } else {
                    // Very likely, the context is null, in which
                    // case the following will throw an exception.
                    // We defer to it in case somehow a link request
                    // is being made at the top level with a non-null
                    // context (e.g. via a change request).
                    request.execute();
                }

                // NOTE: deleteRelation is not supposed to have anything
                // inside it, so we do not push the context.

                //////////////////////////////////////////////////////////////
                //// director
            } else if (elementName.equals("director")) {
                // NOTE: The director element is deprecated.
                // Use a property instead.  This is kept here so that
                // this parser can read older MoML files.
                // NOTE: We do not check for a previously existing director.
                // There is presumably no harm in just creating a new one.
                String className = (String) _attributes.get("class");
                _checkForNull(className, "No class for element \"director\"");

                String dirName = (String) _attributes.get("name");
                _checkForNull(dirName, "No name for element \"director\"");
                _checkClass(_current, CompositeActor.class,
                        "Element \"director\" found inside an element that "
                                + "is not a CompositeActor. It is: " + _current);

                Object[] arguments = new Object[2];
                arguments[0] = _current;
                arguments[1] = dirName;

                // NamedObj container = _current;
                _pushContext();

                Class newClass = Class.forName(className, true, _classLoader);

                // NOTE: No propagation occurs here... Hopefully, deprecated
                // elements are not used with class structures.
                _current = _createInstance(newClass, arguments);
                _namespace = _DEFAULT_NAMESPACE;

                //////////////////////////////////////////////////////////////
                //// display
            } else if (elementName.equals("display")) {
                String displayName = (String) _attributes.get("name");
                if (_current != null) {

                    // Propagate.
                    Iterator derivedObjects = _current.getDerivedList()
                            .iterator();
                    String currentName = _current.getName();
                    while (derivedObjects.hasNext()) {
                        NamedObj derived = (NamedObj) derivedObjects.next();

                        // If the derived object has the same
                        // name as the old name, then we assume it
                        // should change.
                        if (derived.getName().equals(currentName)) {
                            if (displayName != null) {
                                if (displayName.equals(currentName)) {
                                    // The displayName is the same as the
                                    // name, so it should be reset to null.
                                    derived.setDisplayName(null);
                                } else {
                                    derived.setDisplayName(displayName);
                                }
                            }
                        }
                    }

                    // Now change the display name.
                    String oldDisplayName = _current.getDisplayName();
                    if (displayName != null) {
                        if (displayName.equals(currentName)
                                || displayName.equals("")) {
                            // The displayName is the same as the
                            // name, so it should be reset to null.
                            _current.setDisplayName(null);
                        } else {
                            _current.setDisplayName(displayName);
                        }

                        // Handle the undo aspect if needed
                        if (_undoEnabled) {
                            // Simply create in the undo MoML another display element.
                            _undoContext.appendUndoMoML("<display name=\""
                                    + StringUtilities
                                            .escapeForXML(oldDisplayName)
                                    + "\"/>\n");

                            // Do not need to continue generating undo MoML
                            // as rename does not have any child elements
                            _undoContext.setChildrenUndoable(false);
                        }
                    }
                }

                //////////////////////////////////////////////////////////////
                //// doc
            } else if (elementName.equals("doc")) {
                _currentDocName = (String) _attributes.get("name");
                _currentCharData = new StringBuffer();

                // Count doc tags so that they can nest.
                _docNesting++;

                //////////////////////////////////////////////////////////////
                //// entity
            } else if (elementName.equals("entity")
                    || elementName.equals("model")) {
                // NOTE: The "model" element is deprecated.  It is treated
                // exactly as an entity.
                String className = (String) _attributes.get("class");
                String entityName = (String) _attributes.get("name");
                _checkForNull(entityName, "No name for element \"entity\"");

                String source = (String) _attributes.get("source");

                // For undo purposes need to know if the entity existed
                // already
                Entity entity = _searchForEntity(entityName, _current);
                boolean existedAlready = (entity != null);
                boolean converted = false;

                if (existedAlready) {
                    // Check whether it was previously a class, in which case
                    // it is being converted to an entity.
                    if (entity.isClassDefinition()) {
                        entity.setClassDefinition(false);
                        converted = true;
                    }
                } else {
                    NamedObj candidate = _createEntity(className, entityName,
                            source);

                    if (candidate instanceof Entity) {
                        entity = (Entity) candidate;
                        entity.setClassName(className);
                    } else {
                        throw new IllegalActionException(_current,
                                "Attempt to create an entity named "
                                        + entityName + " from a class that "
                                        + "is not a subclass of Entity: "
                                        + className);
                    }
                }

                // NOTE: The entity may be at the top level, in
                // which case _deleteRequests is null.
                if (_deleteRequests != null) {
                    _deleteRequestStack.push(_deleteRequests);
                    pushedDeleteRequests = true;
                }

                _deleteRequests = new LinkedList();

                // NOTE: The entity may be at the top level, in
                // which case _linkRequests is null.
                if (_linkRequests != null) {
                    _linkRequestStack.push(_linkRequests);
                    pushedLinkRequests = true;
                }

                _linkRequests = new LinkedList();

                if (_current != null) {
                    _pushContext();
                } else if (_toplevel == null) {
                    // NOTE: We used to set _toplevel to newEntity, but
                    // this isn't quite right because the entity may have a
                    // composite name.
                    _toplevel = entity.toplevel();

                    // Ensure that if any change requests occur as a
                    // consequence of adding items to this top level,
                    // that execution of those change requests is deferred
                    // until endDocument().
                    _toplevel.setDeferringChangeRequests(true);

                    // As early as possible, set URL attribute.
                    // This is needed in case any of the parameters
                    // refer to files whose location is relative
                    // to the URL location.
                    if (_xmlFile != null) {
                        // Add a URL attribute to the toplevel to
                        // indicate where it was read from.
                        URIAttribute attribute = new URIAttribute(_toplevel,
                                "_uri");
                        attribute.setURL(_xmlFile);
                    }
                }

                _current = entity;

                _namespace = _DEFAULT_NAMESPACE;

                if (_undoEnabled) {
                    // Handle the undo aspect.
                    if (existedAlready) {
                        if (!converted) {
                            _undoContext.appendUndoMoML("<entity name=\""
                                    + entityName + "\" >\n");

                            // Need to continue undoing and use an end tag
                            _undoContext.appendClosingUndoMoML("</entity>\n");
                        } else {
                            // Converted from a class to an entity, so reverse this.
                            _undoContext.appendUndoMoML("<class name=\""
                                    + entityName + "\" >\n");

                            // Need to continue undoing and use an end tag
                            _undoContext.appendClosingUndoMoML("</class>\n");
                        }

                        _undoContext.setChildrenUndoable(true);
                    } else {
                        _undoContext.appendUndoMoML("<deleteEntity name=\""
                                + entityName + "\" />\n");

                        // Do not need to continue generating undo MoML
                        // as the deleteEntity takes care of all
                        // contained MoML
                        _undoContext.setChildrenUndoable(false);
                        _undoContext.setUndoable(false);

                        // Prevent any further undo entries for this context.
                        _undoEnabled = false;
                    }
                }

                //////////////////////////////////////////////////////////////
                //// group
            } else if (elementName.equals("group")) {
                String groupName = (String) _attributes.get("name");

                if (groupName != null) {
                    // Defining a namespace.
                    _namespaces.push(_namespace);
                    _namespaceTranslations.push(_namespaceTranslationTable);
                    _namespacesPushed = true;

                    if (groupName.equals("auto")) {
                        _namespace = _AUTO_NAMESPACE;
                        _namespaceTranslationTable = new HashMap();
                    } else {
                        _namespace = groupName;
                    }
                } else {
                    _namespaces.push(_DEFAULT_NAMESPACE);
                    _namespaceTranslations.push(_namespaceTranslationTable);
                    _namespacesPushed = true;
                    _namespace = _DEFAULT_NAMESPACE;
                    _namespaceTranslationTable = new HashMap();
                }

                // Link and unlink requests are processed when the
                // group closes.
                // NOTE: The entity may be at the top level, in
                // which case _deleteRequests is null.
                if (_deleteRequests != null) {
                    _deleteRequestStack.push(_deleteRequests);
                    pushedDeleteRequests = true;
                }

                _deleteRequests = new LinkedList();

                // NOTE: The entity may be at the top level, in
                // which case _linkRequests is null.
                if (_linkRequests != null) {
                    _linkRequestStack.push(_linkRequests);
                    pushedLinkRequests = true;
                }

                _linkRequests = new LinkedList();

                // Handle the undo aspect.
                if (_undoEnabled) {
                    // NOTE: for groups with namespaces, rely on the names
                    // already being part of undo MoML names instead of
                    // tracking the namespace prefix
                    _undoContext.appendUndoMoML("<group>\n");

                    // Need to continue undoing and use an end tag
                    _undoContext.appendClosingUndoMoML("</group>\n");
                    _undoContext.setChildrenUndoable(true);
                }

                //////////////////////////////////////////////////////////////
                //// input
            } else if (elementName.equals("input")) {
                String source = (String) _attributes.get("source");
                _checkForNull(source, "No source for element \"input\"");

                boolean skip = false;

                if (inputFileNamesToSkip != null) {
                    // If inputFileNamesToSkip contains a string
                    // that matches the end of source, then skip
                    // parsing the source file.  We use this for testing
                    // configurations that have optional parts like
                    // Matlab or javacomm.
                    Iterator inputFileNames = inputFileNamesToSkip.iterator();

                    while (inputFileNames.hasNext()) {
                        String inputFileName = (String) inputFileNames.next();

                        if (source.endsWith(inputFileName)) {
                            skip = true;
                            break;
                        }
                    }
                }

                if (!skip) {
                    // NOTE: The base attribute has been deprecated.  Ignore.
                    // Read external file in the current context, but with
                    // a new parser.
                    boolean modified = isModified();
                    MoMLParser newParser = new MoMLParser(_workspace,
                            _classLoader);

                    newParser.setContext(_current);
                    setModified(modified);

                    _parse(newParser, _base, source);
                }

                //////////////////////////////////////////////////////////////
                //// link
            } else if (elementName.equals("link")) {
                String portName = (String) _attributes.get("port");

                // Port can be null if we are linking two vertices.
                // _checkForNull(portName, "No port for element \"link\"");
                // Relation attribute now optional
                String relationName = (String) _attributes.get("relation");
                String insertAtSpec = (String) _attributes.get("insertAt");
                String insertInsideAtSpec = (String) _attributes
                        .get("insertInsideAt");

                // Link is stored and processed last, but before deletions.
                LinkRequest request;

                if (portName != null) {
                    request = new LinkRequest(portName, relationName,
                            insertAtSpec, insertInsideAtSpec);
                } else {
                    String relation1Name = (String) _attributes
                            .get("relation1");
                    String relation2Name = (String) _attributes
                            .get("relation2");
                    request = new LinkRequest(relation1Name, relation2Name);
                }

                if (_linkRequests != null) {
                    _linkRequests.add(request);
                } else {
                    // Very likely, the context is null, in which
                    // case the following will throw an exception.
                    // We defer to it in case somehow a link request
                    // is being made at the top level with a non-null
                    // context (e.g. via a change request).
                    request.execute();
                }

                //////////////////////////////////////////////////////////////
                //// port
            } else if (elementName.equals("port")) {
                String className = (String) _attributes.get("class");
                String portName = (String) _attributes.get("name");
                _checkForNull(portName, "No name for element \"port\"");

                _checkClass(_current, Entity.class,
                        "Element \"port\" found inside an element that "
                                + "is not an Entity. It is: " + _current);

                Entity container = (Entity) _current;

                Class newClass = null;

                if ((className != null) && !className.trim().equals("")) {
                    newClass = Class.forName(className, true, _classLoader);
                }

                Port port = container.getPort(portName);

                // Flag used to generate correct undo MoML
                boolean alreadyExisted = (port != null);

                if (port != null) {
                    if (newClass != null) {
                        // Previously existing port with the specified name.
                        _checkClass(port, newClass, "port named \"" + portName
                                + "\" exists and is not an instance of "
                                + className);
                    }
                } else {
                    // No previously existing port with this name.
                    // First check that there will be no name collision
                    // when this is propagated. Note that we need to
                    // include all derived objects, irrespective of whether
                    // they are locally changed.
                    List derivedList = container.getDerivedList();
                    Iterator derivedObjects = derivedList.iterator();

                    while (derivedObjects.hasNext()) {
                        Entity derived = (Entity) derivedObjects.next();

                        if (derived.getPort(portName) != null) {
                            throw new IllegalActionException(
                                    container,
                                    "Cannot create port because a subclass or instance "
                                            + "contains a port with the same name: "
                                            + derived.getPort(portName)
                                                    .getFullName());
                        }
                    }

                    if (newClass == null) {
                        // Classname is not given.  Invoke newPort() on the
                        // container.
                        port = container.newPort(portName);

                        if ((_topObjectsCreated != null)
                                && (container == _originalContext)) {
                            _topObjectsCreated.add(port);
                        }

                        // Propagate.
                        // NOTE: Propagated ports will not use newPort(),
                        // but rather will use clone. Classes that override
                        // newPort() to perform special actions will no longer
                        // work, possibly!
                        port.propagateExistence();
                    } else {
                        // Classname is given.
                        Object[] arguments = new Object[2];
                        arguments[0] = container;
                        arguments[1] = portName;
                        port = (Port) _createInstance(newClass, arguments);

                        // Propagate.
                        port.propagateExistence();
                    }
                }

                _pushContext();
                _current = port;

                _namespace = _DEFAULT_NAMESPACE;

                // Handle the undo aspect if needed
                if (_undoEnabled) {
                    if (alreadyExisted) {
                        // Simply create in the undo MoML the same port
                        _undoContext.appendUndoMoML("<port name=\"" + portName
                                + "\" ");

                        // Also add in the class if given
                        if (className != null) {
                            _undoContext.appendUndoMoML("class=\"" + className
                                    + "\" ");
                        }

                        _undoContext.appendUndoMoML(">\n");

                        // Need to continue undoing and use an end tag
                        _undoContext.appendClosingUndoMoML("</port>\n");
                        _undoContext.setChildrenUndoable(true);
                    } else {
                        // Need to delete the port in the undo MoML
                        _undoContext.appendUndoMoML("<deletePort name=\""
                                + portName + "\" />\n");

                        // Do not need to continue generating undo MoML
                        // as the deletePort takes care of all
                        // contained MoML
                        _undoContext.setChildrenUndoable(false);
                        _undoContext.setUndoable(false);

                        // Prevent any further undo entries for this context.
                        _undoEnabled = false;
                    }
                }

                // NOTE: The direction attribute is deprecated, but
                // supported nonetheless. This is not propagated, but
                // hopefully deprecated attributes are not used with
                // the class mechanism.
                if (port instanceof IOPort) {
                    String direction = (String) _attributes.get("direction");

                    if (direction != null) {
                        IOPort ioport = (IOPort) port;
                        boolean isOutput = direction.equals("output")
                                || direction.equals("both");
                        boolean isInput = direction.equals("input")
                                || direction.equals("both");

                        // If this object is a derived object, then its I/O status
                        // cannot be changed.
                        if (alreadyExisted
                                && (ioport.getDerivedLevel() < Integer.MAX_VALUE)) {
                            if ((ioport.isInput() != isInput)
                                    || (ioport.isOutput() != isOutput)) {
                                throw new IllegalActionException(
                                        ioport,
                                        "Cannot change whether this port is "
                                                + "an input or output. That property is "
                                                + "fixed by the class definition.");
                            }
                        }

                        ioport.setOutput(isOutput);
                        ioport.setInput(isInput);
                    }
                }

                //////////////////////////////////////////////////////////////
                //// property
            } else if (elementName.equals("property")) {
                String createIfNecessary = (String) _attributes
                        .get("createIfNecessary");
                String className = (String) _attributes.get("class");
                String propertyName = (String) _attributes.get("name");
                _checkForNull(propertyName, "No name for element \"property\"");
                if (createIfNecessary != null
                        && createIfNecessary.equals("true") && _current != null
                        && propertyName != null
                        && _current.getAttribute(propertyName) != null) {
                    // The createIfNecessary="true" and the property
                    // already exists
                } else {
                    // If the createIfNecessary property is true and
                    // there already is a property with this name, we
                    // don't handle this property
                    String value = (String) _attributes.get("value");
                    _handlePropertyElement(className, propertyName, value);
                }

                //////////////////////////////////////////////////////////////
                //// relation
            } else if (elementName.equals("relation")) {
                String className = (String) _attributes.get("class");
                String relationName = (String) _attributes.get("name");
                _checkForNull(relationName, "No name for element \"relation\"");
                _checkClass(_current, CompositeEntity.class,
                        "Element \"relation\" found inside an element that "
                                + "is not a CompositeEntity. It is: "
                                + _current);

                CompositeEntity container = (CompositeEntity) _current;
                Class newClass = null;

                if (className != null) {
                    newClass = Class.forName(className, true, _classLoader);
                }

                Relation relation = container.getRelation(relationName);

                // Flag used to generate correct undo MoML
                boolean alreadyExisted = (relation != null);

                if (relation == null) {
                    // No previous relation with this name.
                    // First check that there will be no name collision
                    // when this is propagated. Note that we need to
                    // include all derived objects, irrespective of whether
                    // they are locally changed.
                    List derivedList = container.getDerivedList();
                    Iterator derivedObjects = derivedList.iterator();

                    while (derivedObjects.hasNext()) {
                        CompositeEntity derived = (CompositeEntity) derivedObjects
                                .next();

                        if (derived.getRelation(relationName) != null) {
                            throw new IllegalActionException(
                                    container,
                                    "Cannot create relation because a subclass or instance "
                                            + "contains a relation with the same name: "
                                            + derived.getRelation(relationName)
                                                    .getFullName());
                        }
                    }

                    NamedObj newRelation = null;
                    _pushContext();

                    if (newClass == null) {
                        // No classname. Use the newRelation() method.
                        newRelation = container.newRelation(relationName);

                        if ((_topObjectsCreated != null)
                                && (container == _originalContext)) {
                            _topObjectsCreated.add(newRelation);
                        }

                        // Propagate.
                        // NOTE: Propagated relations will not use newRelation(),
                        // but rather will use clone. Classes that rely
                        // on newRelation(), will no longer work, possibly!
                        newRelation.propagateExistence();
                    } else {
                        Object[] arguments = new Object[2];
                        arguments[0] = (CompositeEntity) _current;
                        arguments[1] = relationName;
                        newRelation = _createInstance(newClass, arguments);

                        // Propagate.
                        newRelation.propagateExistence();
                    }

                    _namespace = _DEFAULT_NAMESPACE;
                    _current = newRelation;

                } else {
                    // Previously existing relation with the specified name.
                    if (newClass != null) {
                        _checkClass(relation, newClass, "relation named \""
                                + relationName
                                + "\" exists and is not an instance of "
                                + className);
                    }

                    _pushContext();

                    _current = relation;
                    _namespace = _DEFAULT_NAMESPACE;
                }

                // Handle the undo aspect if needed
                if (_undoEnabled) {
                    if (alreadyExisted) {
                        // Simply create in the undo MoML the same relation
                        _undoContext.appendUndoMoML("<relation name=\""
                                + relationName + "\" ");

                        // Also add in the class if given
                        if (className != null) {
                            _undoContext.appendUndoMoML("class=\"" + className
                                    + "\" ");
                        }

                        _undoContext.appendUndoMoML(">\n");

                        // Need to continue undoing and use an end tag
                        _undoContext.appendClosingUndoMoML("</relation>\n");
                        _undoContext.setChildrenUndoable(true);
                    } else {
                        // Need to delete the realtion in the undo MoML
                        _undoContext.appendUndoMoML("<deleteRelation name=\""
                                + relationName + "\" />\n");

                        // Do not need to continue generating undo MoML
                        // as the deleteRelation takes care of all
                        // contained MoML
                        _undoContext.setChildrenUndoable(false);
                        _undoContext.setUndoable(false);

                        // Prevent any further undo entries for this context.
                        _undoEnabled = false;
                    }
                }

                //////////////////////////////////////////////////////////////
                //// rename
            } else if (elementName.equals("rename")) {
                String newName = (String) _attributes.get("name");
                _checkForNull(newName, "No new name for element \"rename\"");

                if (_current != null) {
                    String oldName = _current.getName();

                    // Ensure that derived objects aren't changed.
                    if (!oldName.equals(newName)
                            && (_current.getDerivedLevel() < Integer.MAX_VALUE)) {
                        throw new IllegalActionException(
                                _current,
                                "Cannot change the name to "
                                        + newName
                                        + ". The name is fixed by the class definition.");
                    }

                    // Propagate.  Note that a rename in a derived class
                    // could cause a NameDuplicationException.  We have to
                    // be able to unroll the changes if that occurs.
                    Iterator derivedObjects = _current.getDerivedList()
                            .iterator();
                    Set changedName = new HashSet();
                    HashMap changedClassName = new HashMap();
                    NamedObj derived = null;

                    try {
                        while (derivedObjects.hasNext()) {
                            derived = (NamedObj) derivedObjects.next();

                            // If the derived object has the same
                            // name as the old name, then we assume it
                            // should change.
                            if (derived.getName().equals(oldName)) {
                                derived.setName(newName);
                                changedName.add(derived);
                            }

                            // Also need to modify the class name of
                            // the instance or derived class if the
                            // class or base class changes its name.
                            if (derived instanceof Instantiable) {
                                Instantiable parent = ((Instantiable) derived)
                                        .getParent();

                                // This relies on the depth-first search
                                // order of the getDerivedList() method
                                // to be sure that the base class will
                                // already be in the changedName set if
                                // its name will change.
                                if ((parent != null)
                                        && ((parent == _current) || changedName
                                                .contains(parent))) {
                                    String previousClassName = derived
                                            .getClassName();
                                    int last = previousClassName
                                            .lastIndexOf(oldName);

                                    if (last < 0) {
                                        throw new InternalErrorException(
                                                "Expected instance "
                                                        + derived.getFullName()
                                                        + " to have class name ending with "
                                                        + oldName
                                                        + " but its class name is "
                                                        + previousClassName);
                                    }

                                    String newClassName = newName;

                                    if (last > 0) {
                                        newClassName = previousClassName
                                                .substring(0, last)
                                                + newName;
                                    }

                                    derived.setClassName(newClassName);
                                    changedClassName.put(derived,
                                            previousClassName);
                                }
                            }
                        }
                    } catch (NameDuplicationException ex) {
                        // Unravel the name changes before
                        // rethrowing the exception.
                        Iterator toUndo = changedName.iterator();

                        while (toUndo.hasNext()) {
                            NamedObj revert = (NamedObj) toUndo.next();
                            revert.setName(oldName);
                        }

                        Iterator classNameFixes = changedClassName.entrySet()
                                .iterator();

                        while (classNameFixes.hasNext()) {
                            Map.Entry revert = (Map.Entry) classNameFixes
                                    .next();
                            NamedObj toFix = (NamedObj) revert.getKey();
                            String previousClassName = (String) revert
                                    .getValue();
                            toFix.setClassName(previousClassName);
                        }

                        throw new IllegalActionException(_current, ex,
                                "Propagation to instance and/or derived class causes"
                                        + "name duplication: "
                                        + derived.getFullName());
                    }

                    _current.setName(newName);

                    // Handle the undo aspect if needed
View Full Code Here

TOP

Related Classes of ptolemy.kernel.Entity

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.