Package ptolemy.kernel

Examples of ptolemy.kernel.CompositeEntity


            if (port.isInput() && port instanceof WirelessIOPort) {
                // Found the port.
                Entity container = (Entity) (port.getContainer());
                String channelName = ((WirelessIOPort) port).outsideChannel
                        .stringValue();
                CompositeEntity container2 = (CompositeEntity) container
                        .getContainer();

                if (container2 == null) {
                    throw new IllegalActionException(this,
                            "The container does not have a container.");
                }

                Entity channel = container2.getEntity(channelName);

                if (channel instanceof AtomicWirelessChannel) {
                    Parameter channelProperties = ((AtomicWirelessChannel) channel).defaultProperties;

                    // Only set up the type constraint if the type of the
View Full Code Here


     *  Return null if either there is no container or the container has no
     *  director.
     *  @return The director that invokes this actor.
     */
    public Director getDirector() {
        CompositeEntity container = (CompositeEntity) getContainer();

        if (container instanceof CompositeActor) {
            return ((CompositeActor) container).getDirector();
        }

View Full Code Here

     */
    public Manager getManager() {
        try {
            _workspace.getReadAccess();

            CompositeEntity container = (CompositeEntity) getContainer();

            if (container instanceof CompositeActor) {
                return ((CompositeActor) container).getManager();
            }

View Full Code Here

                _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

                    + "(Container is '" + _current + "').",
                    _currentExternalEntity(), _getLineNumber(),
                    _getColumnNumber());
        }

        CompositeEntity container = (CompositeEntity) _current;
        ComponentEntity previous = _searchForEntity(entityName, _current);
        Class newClass = null;
        ComponentEntity reference = null;

        if (className != null) {
            // A class name is given.
            reference = searchForClass(className, source);

            // If no source is specified and no reference was found,
            // search for a class definition in context.
            if ((reference == null) && (source == null)) {
                // Allow the class name to be local in the current context
                // or defined in scope. Search for a class definition that
                // matches in the current context.
                reference = _searchForClassInContext(className, /* source*/
                null);
            }

            if (reference == null) {
                // No previously defined class with this name.
                // First attempt to instantiate a Java class.
                // If we throw an error or exception be sure to save the
                // original error message before we go off and try to fix the
                // error.  Sometimes, the original error message is the true
                // cause of the problem, and we should always provide the user
                // with the cause of the original error in the unlikely event
                // that our error correction fails
                try {
                    newClass = Class.forName(className, true, _classLoader);
                } catch (Exception ex) {
                    // NOTE: Java sometimes throws ClassNotFoundException
                    // and sometimes NullPointerException when the class
                    // does not exist.  Hence the broad catch here.
                    try {
                        reference = _attemptToFindMoMLClass(className, source);
                    } catch (Exception ex2) {
                        // If we are running inside an applet, then
                        // we may end up getting a SecurityException,
                        // so we want to be sure to not throw away ex2
                        throw new IllegalActionException(null, ex2,
                                "Cannot find class: " + className);
                    }
                } catch (Error error) {
                    // Java might throw a ClassFormatError, but
                    // we usually get and XmlException
                    // NOTE: The following error message is for
                    // the programmer, not for the user. EAL
                    StringBuffer errorMessage = new StringBuffer();

                    if (error instanceof ExceptionInInitializerError) {
                        // Running a Python applet may cause
                        // an ExceptionInInitializerError
                        // There was a problem in the initializer, but
                        // we can get the original exception that was
                        // thrown.
                        Throwable staticThrowable = ((ExceptionInInitializerError) error)
                                .getCause();

                        // I think we should report the cause and a stack
                        // trace for all the exceptions thrown here,
                        // but it sure makes the output ugly.
                        // Instead, I just debug from here -cxh
                        errorMessage.append("ExceptionInInitializerError: "
                                + "Caused by:\n "
                                + KernelException
                                        .stackTraceToString(staticThrowable));
                    } else {
                        // If there is a class format error in the
                        // code generator, then we may end up obscuring
                        // that error, requiring debugging here.
                        // We use error.toString() here instead of
                        // error.getMessage() so that the name of the
                        // actual class that caused the error is reported.
                        // This is critical if the problem is a class not
                        // found error.  If we use error.getMessage()
                        // and try to open up
                        // actor/lib/comm/demo/SerialPort/SerialPort.xml
                        // when the Java Serial Comm API is not installed,
                        // we get
                        // Error encounted in:
                        // <entity name="SerialComm" class="ptolemy.actor.lib...
                        // -- ptolemy.actor.lib.comm.SerialComm:
                        // javax/comm/SerialPortEventListener
                        // ptolemy.actor.lib.comm.SerialComm: XmlException:
                        // Could not find 'ptolemy/actor/lib/comm/SerialComm.xml'..
                        // If we use toString(), we get:
                        // Error encounted in:
                        // <entity name="SerialComm" class="ptolemy.actor.lib..
                        // -- ptolemy.actor.lib.comm.SerialComm:
                        // java.lang.NoClassDefFoundError: javax/comm/SerialPortEventListener
                        // ptolemy.actor.lib.comm.SerialComm: XmlException:
                        // Could not find 'ptolemy/actor/lib/comm/SerialComm.xml'..
                        // It is critical that the error include the
                        // NoClassDefFoundError string -cxh
                        errorMessage.append(className + ": \n "
                                + error.toString() + "\n");
                    }

                    try {
                        reference = _attemptToFindMoMLClass(className, source);
                    } catch (XmlException ex2) {
                        throw new Exception("-- " + errorMessage.toString()
                                + className + ": XmlException:\n"
                                + ex2.getMessage());
                    } catch (ClassFormatError ex3) {
                        throw new Exception("-- :" + errorMessage.toString()
                                + className + ": ClassFormatError: "
                                + "found invalid Java class file.\n"
                                + ex3.getMessage());
                    } catch (Exception ex4) {
                        throw new Exception("-- " + errorMessage.toString()
                                + className + ": Exception:\n"
                                + ex4.getMessage());
                    }
                }
            }
        }

        if (previous != null) {
            if (newClass != null) {
                _checkClass(previous, newClass, "entity named \"" + entityName
                        + "\" exists and is not an instance of " + className);
            }

            return previous;
        }

        // No previous entity.  Class name is required.
        _checkForNull(className, "Cannot create entity without a class name.");

        // Next check to see whether the class extends a named entity.
        if (reference == null) {
            // Not a named entity. Instantiate a Java class.
            if (_current != null) {
                // Not a top-level entity.
                // 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.getEntity(entityName) != null) {
                        throw new IllegalActionException(
                                container,
                                "Cannot create entity because a subclass or instance "
                                        + "contains an entity with the same name: "
                                        + derived.getEntity(entityName)
                                                .getFullName());
                    }
                }

                _checkClass(_current, CompositeEntity.class,
                        "Cannot create an entity inside an element that "
                                + "is not a CompositeEntity. It is: "
                                + _current);

                Object[] arguments = new Object[2];

                arguments[0] = _current;
                arguments[1] = entityName;

                NamedObj newEntity = _createInstance(newClass, arguments);

                newEntity.propagateExistence();

                _loadIconForClass(className, newEntity);

                _addParamsToParamsToParse(newEntity);

                return newEntity;
            } else {
                // Top-level entity.  Instantiate in the workspace.
                // Note that there cannot possibly be any propagation here.
                Object[] arguments = new Object[1];
                arguments[0] = _workspace;

                NamedObj result = _createInstance(newClass, arguments);
                result.setName(entityName);
                _loadIconForClass(className, result);
                return result;
            }
        } else {
            // Extending a previously defined entity.  Check to see that
            // it was defined to be a class definition.
            if (!reference.isClassDefinition()) {
                throw new MissingClassException(
                        "Attempt to extend an entity that "
                                + "is not a class: " + reference.getFullName(),
                        reference.getFullName(), _currentExternalEntity(),
                        _getLineNumber(), _getColumnNumber());
            }

            // 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.
            // If the container is null, then we can't possibly get
            // a name collision.
            List derivedList = null;

            if (container != null) {
                derivedList = container.getDerivedList();

                Iterator derivedObjects = derivedList.iterator();

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

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

View Full Code Here

        // Need to use the depth to determine how much MoML to export.
        StringWriter buffer = new StringWriter();
        toDelete.exportMoML(buffer, depth);
        moml.append(buffer.toString());

        CompositeEntity container = (CompositeEntity) toDelete.getContainer();

        if (container instanceof HandlesInternalLinks) {
            return moml.toString();
        }

        // Now create the undo that will recreate any links
        // the are deleted as a side effect.
        // This is a little tricky, because if the entity
        // is defined in the base class, then we only want
        // to include links that override the base class.
        HashSet filter = new HashSet();
        if (toDelete.getDerivedLevel() == Integer.MAX_VALUE) {
            // The entity is not derived, so all links should be included.
            // NOTE: cannot use the relationlist as returned as it is
            // unmodifiable and we need to add in the entity being deleted.
            filter.addAll(toDelete.linkedRelationList());
        } else {
            // The entity is derived, so we should only include
            // relations that are not derived.
            Iterator relations = toDelete.linkedRelationList().iterator();
            while (relations.hasNext()) {
                Relation relation = (Relation) relations.next();
                if (relation != null) {
                    if (relation.getDerivedLevel() == Integer.MAX_VALUE) {
                        filter.add(relation);
                    }
                }
            }
        }
        filter.add(toDelete);

        // The parent container can do the filtering and generate the MoML.
        moml.append(container.exportLinks(0, filter));

        // Finally move back to context if needed
        moml.append(UndoContext.moveContextEnd(_current, toDelete));

        return moml.toString();
View Full Code Here

        // Need to use the depth to determine how much MoML to export.
        StringWriter buffer = new StringWriter();
        toDelete.exportMoML(buffer, depth);
        moml.append(buffer.toString());

        CompositeEntity container = (CompositeEntity) toDelete.getContainer();

        if (container instanceof HandlesInternalLinks) {
            return moml.toString();
        }

        // Generate undo to recreate the links.
        // This is a little tricky, because if the relation
        // is defined in the base class, then we only want
        // to include links that override the base class.
        HashSet filter = new HashSet();
        if (toDelete.getDerivedLevel() == Integer.MAX_VALUE) {
            // The entity is not derived, so all links should be included.
            // NOTE: cannot use the relationlist as returned as it is
            // unmodifiable and we need to add in the entity being deleted.
            filter.addAll(toDelete.linkedObjectsList());
        } else {
            // The relation is derived, so we should only include
            // relations that are not derived.
            Iterator objects = toDelete.linkedObjectsList().iterator();
            while (objects.hasNext()) {
                NamedObj portOrRelation = (Relation) objects.next();
                if (portOrRelation != null) {
                    if (portOrRelation.getDerivedLevel() == Integer.MAX_VALUE) {
                        filter.add(portOrRelation);
                    }
                }
            }
        }
        filter.add(toDelete);

        // The parent container can do the filtering and generate the
        // MoML.
        moml.append(container.exportLinks(0, filter));

        // Move back to context if needed.
        moml.append(UndoContext.moveContextEnd(_current, toDelete));

        return moml.toString();
View Full Code Here

            throw new XmlException("Element link requires two relations.",
                    _currentExternalEntity(), _getLineNumber(),
                    _getColumnNumber());
        }

        CompositeEntity context = (CompositeEntity) _current;

        // Get relations.
        ComponentRelation relation1 = context.getRelation(relation1Name);
        _checkForNull(relation1, "No relation named \"" + relation1Name
                + "\" in " + context.getFullName());

        // Get relations.
        ComponentRelation relation2 = context.getRelation(relation2Name);
        _checkForNull(relation2, "No relation named \"" + relation2Name
                + "\" in " + context.getFullName());

        // Ensure that derived objects aren't changed.
        // We have to prohit adding links between class
        // elements because this operation cannot be undone, and
        // it will not be persistent.
        if (_isLinkInClass(context, relation1, relation2)) {
            throw new IllegalActionException(relation1, relation2,
                    "Cannot link relations when both"
                            + " are part of the class definition.");
        }

        relation1.link(relation2);

        // Propagate. Get the derived list for relation1,
        // then use its container as the context in which to
        // find relation2.
        Iterator derivedObjects = relation1.getDerivedList().iterator();

        while (derivedObjects.hasNext()) {
            ComponentRelation derivedRelation1 = (ComponentRelation) derivedObjects
                    .next();
            CompositeEntity derivedContext = (CompositeEntity) derivedRelation1
                    .getContainer();
            ComponentRelation derivedRelation2 = derivedContext
                    .getRelation(relation2Name);
            derivedRelation1.link(derivedRelation2);
        }

        // Handle the undo aspect.
View Full Code Here

            }
        }

        if (container instanceof CompositeEntity
                && !(container instanceof FSMActor)) {
            CompositeEntity model = (CompositeEntity) container;

            // Loop over all the actor instance classes.
            for (Iterator entities = model.deepEntityList().iterator(); entities
                    .hasNext();) {
                ComponentEntity entity = (ComponentEntity) entities.next();
                String className = ModelTransformer.getInstanceClassName(
                        entity, _options);
                SootClass entityClass = Scene.v()
View Full Code Here

                    "Cannot graphically edit a model "
                            + "that is not a CompositeEntity. Model is a "
                            + model);
        }

        CompositeEntity entity = (CompositeEntity) model;

        ActorGraphFrame frame = new ActorGraphFrame(entity, this,
                defaultLibrary);
        setFrame(frame);
        frame.setBackground(BACKGROUND_COLOR);
View Full Code Here

TOP

Related Classes of ptolemy.kernel.CompositeEntity

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.