Package ptolemy.kernel

Examples of ptolemy.kernel.CompositeEntity


     *  as a new library.
     */
    public static void openLibrary(Configuration configuration, File file)
            throws Exception {

        CompositeEntity library = null;
        final CompositeEntity libraryContainer = (CompositeEntity) configuration
                .getEntity("actor library");

        final ModelDirectory directory = (ModelDirectory) configuration
                .getEntity(Configuration._DIRECTORY_NAME);

        if (directory == null) {
            return;
        }

        if (libraryContainer == null) {
            return;
        }

        StringAttribute alternateLibraryBuilderAttribute = (StringAttribute) libraryContainer
                .getAttribute("_alternateLibraryBuilder");

        // If the _alternateLibraryBuilder attribute is present,
        // then we use the specified class to build the library
        // instead of just reading the moml.

        if (alternateLibraryBuilderAttribute != null) {
            // Get the class that will build the library from the plugins
            String libraryBuilderClassName = alternateLibraryBuilderAttribute
                    .getExpression();

            // Dynamically load the library builder and build the library
            Class libraryBuilderClass = Class.forName(libraryBuilderClassName);
            LibraryBuilder libraryBuilder = (LibraryBuilder) libraryBuilderClass
                    .newInstance();

            // Set the attributes defined in the moml to the attributes of the
            // LibraryBuilder
            libraryBuilder.addAttributes(alternateLibraryBuilderAttribute
                    .attributeList());

            try {
                library = libraryBuilder.buildLibrary(libraryContainer
                        .workspace());
            } catch (Exception ex) {
                ex.printStackTrace();
                throw new Exception("Cannot create library with "
                        + "LibraryBuilder: ", ex);
            }
        }

        // If we have a jar URL, convert spaces to %20
        URL fileURL = JNLPUtilities.canonicalizeJarURL(file.toURI().toURL());

        String identifier = fileURL.toExternalForm();

        // Check to see whether the library is already open.
        Effigy libraryEffigy = directory.getEffigy(identifier);

        if (libraryEffigy == null) {
            if (library == null) {
                // Only do this if the library hasn't been set above
                // by a LibraryBuilder
                // No previous libraryEffigy exists that is identified
                // by this URL.  Parse the user library into the
                // workspace of the actor library.
                MoMLParser parser = new MoMLParser(libraryContainer.workspace());

                // Set the ErrorHandler so that if we have
                // compatibility problems between devel and production
                // versions, we can skip that element.
                //MoMLParser.setErrorHandler(new VergilErrorHandler());
View Full Code Here


        if (entity == null) {
            throw new NullPointerException("Save in library failed. "
                    + "entity was null, cannot save a null entity.");
        }
        try {
            CompositeEntity libraryInstance = (CompositeEntity) configuration
                    .getEntity("actor library." + USER_LIBRARY_NAME);

            if (libraryInstance == null) {
                throw new IOException("Save In Library failed: "
                        + "Could not find user library with name \""
                        + USER_LIBRARY_NAME + "\".");
            }

            // Note that the library in the configuration is an
            // instance of another model.  We have to go get the
            // original model to make sure that the change propagates
            // back to the file from which the library is loaded from.
            Tableau libraryTableau = configuration.openModel(libraryInstance);
            PtolemyEffigy libraryEffigy = (PtolemyEffigy) libraryTableau
                    .getContainer();
            CompositeEntity library = (CompositeEntity) libraryEffigy
                    .getModel();

            StringWriter buffer = new StringWriter();

            // Check whether there is already something existing in the
            // user library with this name.
            if (library == null) {
                throw new InternalErrorException("Save in library failed. "
                        + "libraryEffigy.getModel() returned null.");
            }
            if (library.getEntity(entity.getName()) != null) {
                throw new NameDuplicationException(
                        entity,
                        "Save In Library failed: An object"
                                + " already exists in the user library with name "
                                + "\"" + entity.getName() + "\".");
            }

            entity.exportMoML(buffer, 1);

            ChangeRequest request = new MoMLChangeRequest(entity, library,
                    buffer.toString());
            library.requestChange(request);
        } catch (IOException ex) {
            // Ignore.
        } catch (KernelException ex) {
            // Ignore.
        }
View Full Code Here

            if (immediateContainer == null) {
                MessageHandler.error("State has no container!");
                return;
            }

            final CompositeEntity container = (CompositeEntity) immediateContainer
                    .getContainer();

            if (container == null) {
                MessageHandler.error("State container has no container!");
                return;
            }

            // Open a dialog to get the refinement name and class.
            Query query = new Query();
            String defaultName = container.uniqueName(state.getName());
            query.addLine("Name", "Name", defaultName);

            // See whether the configuration offers state refinements.
            Configuration configuration = ((FSMGraphController) getController())
                    .getConfiguration();
            Entity refinements = configuration.getEntity("_stateRefinements");

            // Default choices.
            String[] choiceClasses = { "ptolemy.domains.fsm.modal.Refinement",
                    "ptolemy.domains.fsm.modal.ModalController" };
            String[] choiceNames = { "Default Refinement",
                    "State Machine Refinement" };

            // Check the configuration to see whether the default is overridden.
            if (refinements instanceof CompositeEntity) {
                // There is a specification.
                List refinementList = ((CompositeEntity) refinements)
                        .entityList();
                choiceNames = new String[refinementList.size()];
                choiceClasses = new String[refinementList.size()];

                Iterator iterator = refinementList.iterator();
                int count = 0;

                while (iterator.hasNext()) {
                    Entity entity = (Entity) iterator.next();
                    choiceNames[count] = entity.getName();
                    choiceClasses[count++] = entity.getClass().getName();
                }
            }

            query
                    .addChoice("Class", "Class", choiceNames, choiceNames[0],
                            true);

            // FIXME: Need a frame owner for first arg.
            // Perhaps calling getController(), which returns a GraphController
            // will be a good start.
            ComponentDialog dialog = new ComponentDialog(null,
                    "Specify Refinement", query);

            if (!dialog.buttonPressed().equals("OK")) {
                return;
            }

            final String newName = query.getStringValue("Name");

            if (container.getEntity(newName) != null) {
                MessageHandler.error("There is already a refinement with name "
                        + newName + ".");
                return;
            }

            int choiceIndex = query.getIntValue("Class");
            String newClass = choiceClasses[choiceIndex];

            String currentRefinements = state.refinementName.getExpression();

            if ((currentRefinements == null) || currentRefinements.equals("")) {
                currentRefinements = newName;
            } else {
                currentRefinements = currentRefinements.trim() + ", " + newName;
            }

            String moml;

            // The MoML we create depends on whether the configuration
            // specified a set of prototype refinements.
            if (refinements instanceof CompositeEntity) {
                String choiceName = choiceNames[choiceIndex];
                Entity template = ((CompositeEntity) refinements)
                        .getEntity(choiceName);
                String templateDescription = template.exportMoML(newName);
                moml = "<group>" + templateDescription + "<entity name=\""
                        + state.getName(container)
                        + "\"><property name=\"refinementName\" value=\""
                        + currentRefinements + "\"/></entity></group>";
            } else {
                moml = "<group><entity name=\"" + newName + "\" class=\""
                        + newClass + "\"/>" + "<entity name=\""
                        + state.getName(container)
                        + "\"><property name=\"refinementName\" value=\""
                        + currentRefinements + "\"/></entity></group>";
            }

            MoMLChangeRequest change = new MoMLChangeRequest(this, container,
                    moml) {
                protected void _execute() throws Exception {
                    super._execute();

                    // Mirror the ports of the container in the refinement.
                    // Note that this is done here rather than as part of
                    // the MoML because we have set protected variables
                    // in the refinement to prevent it from trying to again
                    // mirror the changes in the container.
                    Entity entity = container.getEntity(newName);

                    // Get the initial port configuration from the container.
                    Iterator ports = container.portList().iterator();

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

                        try {
                            // NOTE: This is awkward.
                            if (entity instanceof Refinement) {
                                ((Refinement) entity).setMirrorDisable(true);
                            } else if (entity instanceof ModalController) {
                                ((ModalController) entity)
                                        .setMirrorDisable(true);
                            }

                            Port newPort = entity.newPort(port.getName());

                            if (newPort instanceof RefinementPort
                                    && port instanceof IOPort) {
                                try {
                                    ((RefinementPort) newPort)
                                            .setMirrorDisable(true);

                                    if (((IOPort) port).isInput()) {
                                        ((RefinementPort) newPort)
                                                .setInput(true);
                                    }

                                    if (((IOPort) port).isOutput()) {
                                        ((RefinementPort) newPort)
                                                .setOutput(true);
                                    }

                                    if (((IOPort) port).isMultiport()) {
                                        ((RefinementPort) newPort)
                                                .setMultiport(true);
                                    }

                                    /* No longer needed since Yuhong modified
                                     * the type system to allow UNKNOWN. EAL
                                     if (port instanceof TypedIOPort
                                     && newPort instanceof TypedIOPort) {
                                     ((TypedIOPort)newPort).setTypeSameAs(
                                     (TypedIOPort)port);
                                     }
                                     */
                                } finally {
                                    ((RefinementPort) newPort)
                                            .setMirrorDisable(false);
                                }
                            }
                        } finally {
                            // NOTE: This is awkward.
                            if (entity instanceof Refinement) {
                                ((Refinement) entity).setMirrorDisable(false);
                            } else if (entity instanceof ModalController) {
                                ((ModalController) entity)
                                        .setMirrorDisable(false);
                            }
                        }
                    }

                    if (_configuration != null) {
                        // Look inside.
                        _configuration.openModel(entity);
                    }
                }
            };

            container.requestChange(change);
        }
View Full Code Here

            }

            State state = (State) target;

            // Check that all these containers exist.
            CompositeEntity immediateContainer = (CompositeEntity) state
                    .getContainer();

            if (immediateContainer == null) {
                MessageHandler.error("State has no container!");
                return;
            }

            final CompositeEntity container = (CompositeEntity) immediateContainer
                    .getContainer();

            if (container == null) {
                MessageHandler.error("State container has no container!");
                return;
            }

            TypedActor[] refinements;

            try {
                refinements = state.getRefinement();
            } catch (Exception ex) {
                MessageHandler.error("Invalid refinements.", ex);
                return;
            }

            if ((refinements == null) || (refinements.length < 1)) {
                MessageHandler.error("No refinements to remove.");
                return;
            }

            String[] choices = new String[refinements.length];

            for (int i = 0; i < refinements.length; i++) {
                choices[i] = ((Nameable) refinements[i]).getName();
            }

            // Open a dialog to get the refinement name and class.
            Query query = new Query();
            query.addChoice("Refinement", "Refinement", choices, choices[0],
                    false);

            // FIXME: Need a frame owner for first arg.
            // Perhaps calling getController(), which returns a GraphController
            // will be a good start.
            ComponentDialog dialog = new ComponentDialog(null,
                    "Specify Refinement", query);

            if (!dialog.buttonPressed().equals("OK")) {
                return;
            }

            String refinementName = query.getStringValue("Refinement");
            StringBuffer newRefinements = new StringBuffer();
            String currentRefinements = state.refinementName.getExpression();
            StringTokenizer tokenizer = new StringTokenizer(currentRefinements,
                    ",");

            while (tokenizer.hasMoreTokens()) {
                String token = tokenizer.nextToken();

                if (!token.trim().equals(refinementName)) {
                    if (newRefinements.length() > 0) {
                        newRefinements.append(", ");
                    }

                    newRefinements.append(token.trim());
                }
            }

            // Check to see whether any other state or transition has
            // this refinment, and if not, remove it from its container.
            Iterator states = immediateContainer.entityList().iterator();
            boolean foundOne = false;

            while (states.hasNext()) {
                NamedObj other = (NamedObj) states.next();

                if ((other != state) && other instanceof State) {
                    String refinementList = ((State) other).refinementName
                            .getExpression();

                    if (refinementList == null) {
                        continue;
                    }

                    tokenizer = new StringTokenizer(refinementList, ",");

                    while (tokenizer.hasMoreTokens()) {
                        String token = tokenizer.nextToken();

                        if (token.equals(refinementName)) {
                            foundOne = true;
                            break;
                        }
                    }

                    if (foundOne) {
                        break;
                    }
                }
            }

            if (!foundOne) {
                Iterator transitions = immediateContainer.relationList()
                        .iterator();

                while (transitions.hasNext()) {
                    NamedObj other = (NamedObj) transitions.next();

                    if (other instanceof Transition) {
                        String refinementList = ((Transition) other).refinementName
                                .getExpression();

                        if (refinementList == null) {
                            continue;
                        }

                        tokenizer = new StringTokenizer(refinementList, ",");

                        while (tokenizer.hasMoreTokens()) {
                            String token = tokenizer.nextToken();

                            if (token.equals(refinementName)) {
                                foundOne = true;
                                break;
                            }
                        }

                        if (foundOne) {
                            break;
                        }
                    }
                }
            }

            String removal = "";

            if (!foundOne) {
                removal = "<deleteEntity name=\"" + refinementName + "\"/>";
            }

            String moml = "<group><entity name=\"" + state.getName(container)
                    + "\"><property name=\"refinementName\" value=\""
                    + newRefinements.toString() + "\"/></entity>" + removal
                    + "</group>";
            MoMLChangeRequest change = new MoMLChangeRequest(this, container,
                    moml);
            container.requestChange(change);
        }
View Full Code Here

            MatchCallback callback) throws Exception {
        MoMLParser parser = new MoMLParser();
        TransformationRule rule = (TransformationRule) parser.parse(null,
                new File(ruleXMLFile).toURI().toURL());
        parser.reset();
        CompositeEntity host = (CompositeEntity) parser.parse(null, new File(
                hostXMLFile).toURI().toURL());

        GraphMatcher matcher = new GraphMatcher();
        if (callback != null) {
            matcher.setMatchCallback(callback);
View Full Code Here

            Attribute attribute = (Attribute) attributes.next();
            _collectConstraints(attribute);
        }

        if (container instanceof CompositeEntity) {
            CompositeEntity composite = (CompositeEntity) container;

            for (Iterator entities = composite.entityList().iterator(); entities
                    .hasNext();) {
                _collectConstraints((Entity) entities.next());
            }
        }
View Full Code Here

        if (includePorts && object instanceof Entity) {
            Entity entity = (Entity) object;
            collection.addAll((Collection<?>) entity.portList());
        }
        if (object instanceof CompositeEntity) {
            CompositeEntity entity = (CompositeEntity) object;
            if (includeEntities) {
                collection.addAll((Collection<?>) entity.entityList());
            }
            if (includeRelations) {
                collection.addAll((Collection<?>) entity.relationList());
            }
        }
        return collection;
    }
View Full Code Here

            if (immediateContainer == null) {
                MessageHandler.error("Transition has no container!");
                return;
            }

            final CompositeEntity container = (CompositeEntity) immediateContainer
                    .getContainer();

            if (container == null) {
                MessageHandler.error("Transition container has no container!");
                return;
            }

            // Open a dialog to get the refinement name and class.
            Query query = new Query();
            String defaultName = container.uniqueName(transition.getName());
            query.addLine("Name", "Name", defaultName);

            // See whether the configuration offers transition refinements.
            Configuration configuration = ((FSMGraphController) getController())
                    .getConfiguration();
            Entity refinements = configuration
                    .getEntity("_transitionRefinements");

            // Default choices.
            String[] choiceClasses = { "ptolemy.domains.fsm.modal.TransitionRefinement" };
            String[] choiceNames = { "Default Refinement" };

            // Check the configuration to see whether the default is overridden.
            if (refinements instanceof CompositeEntity) {
                // There is a specification.
                List refinementList = ((CompositeEntity) refinements)
                        .entityList();
                choiceNames = new String[refinementList.size()];
                choiceClasses = new String[refinementList.size()];

                Iterator iterator = refinementList.iterator();
                int count = 0;

                while (iterator.hasNext()) {
                    Entity entity = (Entity) iterator.next();
                    choiceNames[count] = entity.getName();
                    choiceClasses[count++] = entity.getClass().getName();
                }
            }

            query
                    .addChoice("Class", "Class", choiceNames, choiceNames[0],
                            true);

            // FIXME: Need a frame owner for first arg.
            // Perhaps calling getController(), which returns a GraphController
            // will be a good start.
            ComponentDialog dialog = new ComponentDialog(null,
                    "Specify Refinement", query);

            if (!dialog.buttonPressed().equals("OK")) {
                return;
            }

            final String newName = query.getStringValue("Name");

            if (container.getEntity(newName) != null) {
                MessageHandler.error("There is already a refinement with name "
                        + newName + ".");
                return;
            }

            int choiceIndex = query.getIntValue("Class");
            String newClass = choiceClasses[choiceIndex];

            String currentRefinements = transition.refinementName
                    .getExpression();

            if ((currentRefinements == null) || currentRefinements.equals("")) {
                currentRefinements = newName;
            } else {
                currentRefinements = currentRefinements.trim() + ", " + newName;
            }

            String moml;

            // The MoML we create depends on whether the configuration
            // specified a set of prototype refinements.
            if (refinements instanceof CompositeEntity) {
                String choiceName = choiceNames[choiceIndex];
                Entity template = ((CompositeEntity) refinements)
                        .getEntity(choiceName);
                String templateDescription = template.exportMoML(newName);
                moml = "<group>" + templateDescription + "<relation name=\""
                        + transition.getName(container)
                        + "\"><property name=\"refinementName\" value=\""
                        + currentRefinements + "\"/></relation></group>";
            } else {
                moml = "<group><entity name=\"" + newName + "\" class=\""
                        + newClass + "\"/>" + "<relation name=\""
                        + transition.getName(container)
                        + "\"><property name=\"refinementName\" value=\""
                        + currentRefinements + "\"/></relation></group>";
            }

            MoMLChangeRequest change = new MoMLChangeRequest(this, container,
                    moml) {
                protected void _execute() throws Exception {
                    super._execute();

                    // Mirror the ports of the container in the refinement.
                    // Note that this is done here rather than as part of
                    // the MoML because we have set protected variables
                    // in the refinement to prevent it from trying to again
                    // mirror the changes in the container.
                    Entity entity = container.getEntity(newName);

                    // Get the initial port configuration from the container.
                    Iterator ports = container.portList().iterator();

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

                        try {
                            // NOTE: This is awkward.
                            if (entity instanceof Refinement) {
                                ((Refinement) entity).setMirrorDisable(true);
                            } else if (entity instanceof ModalController) {
                                ((ModalController) entity)
                                        .setMirrorDisable(true);
                            }

                            Port newPort = entity.newPort(port.getName());

                            if (newPort instanceof RefinementPort
                                    && port instanceof IOPort) {
                                try {
                                    ((RefinementPort) newPort)
                                            .setMirrorDisable(true);

                                    if (((IOPort) port).isInput()) {
                                        ((RefinementPort) newPort)
                                                .setInput(true);
                                    }

                                    if (((IOPort) port).isOutput()) {
                                        ((RefinementPort) newPort)
                                                .setOutput(true);
                                    }

                                    if (((IOPort) port).isMultiport()) {
                                        ((RefinementPort) newPort)
                                                .setMultiport(true);
                                    }

                                    /* No longer needed since Yuhong modified
                                     * the type system to allow UNKNOWN. EAL
                                     if (port instanceof TypedIOPort
                                     && newPort instanceof TypedIOPort) {
                                     ((TypedIOPort)newPort).setTypeSameAs(
                                     (TypedIOPort)port);
                                     }
                                     */
                                } finally {
                                    ((RefinementPort) newPort)
                                            .setMirrorDisable(false);
                                }
                            }
                        } finally {
                            // NOTE: This is awkward.
                            if (entity instanceof Refinement) {
                                ((Refinement) entity).setMirrorDisable(false);
                            } else if (entity instanceof ModalController) {
                                ((ModalController) entity)
                                        .setMirrorDisable(false);
                            }
                        }
                    }

                    if (_configuration != null) {
                        // Look inside.
                        _configuration.openModel(entity);
                    }
                }
            };

            container.requestChange(change);
        }
View Full Code Here

                // If the above code did not find an effigy to put
                // the new effigy within, then put it into the
                // directory directly or the specified container.
                if (!isContainerSet) {
                    if (container == null) {
                        CompositeEntity directory = getDirectory();
                        effigy.setName(directory.uniqueName(entity.getName()));
                        effigy.setContainer(directory);
                    } else {
                        effigy.setName(container.uniqueName(entity.getName()));
                        effigy.setContainer(container);
                    }
View Full Code Here

            }

            Transition transition = (Transition) target;

            // Check that all these containers exist.
            CompositeEntity immediateContainer = (CompositeEntity) transition
                    .getContainer();

            if (immediateContainer == null) {
                MessageHandler.error("Transition has no container!");
                return;
            }

            final CompositeEntity container = (CompositeEntity) immediateContainer
                    .getContainer();

            if (container == null) {
                MessageHandler.error("Transition container has no container!");
                return;
            }

            TypedActor[] refinements;

            try {
                refinements = transition.getRefinement();
            } catch (Exception ex) {
                MessageHandler.error("Invalid refinements.", ex);
                return;
            }

            if ((refinements == null) || (refinements.length < 1)) {
                MessageHandler.error("No refinements to remove.");
                return;
            }

            String[] choices = new String[refinements.length];

            for (int i = 0; i < refinements.length; i++) {
                choices[i] = ((Nameable) refinements[i]).getName();
            }

            // Open a dialog to get the refinement name and class.
            Query query = new Query();
            query.addChoice("Refinement", "Refinement", choices, choices[0],
                    false);

            // FIXME: Need a frame owner for first arg.
            // Perhaps calling getController(), which returns a GraphController
            // will be a good start.
            ComponentDialog dialog = new ComponentDialog(null,
                    "Specify Refinement", query);

            if (!dialog.buttonPressed().equals("OK")) {
                return;
            }

            String refinementName = query.getStringValue("Refinement");
            StringBuffer newRefinements = new StringBuffer();
            String currentRefinements = transition.refinementName
                    .getExpression();
            StringTokenizer tokenizer = new StringTokenizer(currentRefinements,
                    ",");

            while (tokenizer.hasMoreTokens()) {
                String token = tokenizer.nextToken();

                if (!token.trim().equals(refinementName)) {
                    if (newRefinements.length() > 0) {
                        newRefinements.append(", ");
                    }

                    newRefinements.append(token.trim());
                }
            }

            // Check to see whether any other transition has
            // this refinment, and if not, remove it from its container.
            Iterator transitions = immediateContainer.relationList().iterator();
            boolean foundOne = false;

            while (transitions.hasNext()) {
                NamedObj other = (NamedObj) transitions.next();

                if ((other != transition) && other instanceof Transition) {
                    String refinementList = ((Transition) other).refinementName
                            .getExpression();

                    if (refinementList == null) {
                        continue;
                    }

                    tokenizer = new StringTokenizer(refinementList, ",");

                    while (tokenizer.hasMoreTokens()) {
                        String token = tokenizer.nextToken();

                        if (token.equals(refinementName)) {
                            foundOne = true;
                            break;
                        }
                    }

                    if (foundOne) {
                        break;
                    }
                }
            }

            if (!foundOne) {
                Iterator states = immediateContainer.entityList().iterator();

                while (states.hasNext()) {
                    NamedObj other = (NamedObj) states.next();

                    if (other instanceof State) {
                        String refinementList = ((State) other).refinementName
                                .getExpression();

                        if (refinementList == null) {
                            continue;
                        }

                        tokenizer = new StringTokenizer(refinementList, ",");

                        while (tokenizer.hasMoreTokens()) {
                            String token = tokenizer.nextToken();

                            if (token.equals(refinementName)) {
                                foundOne = true;
                                break;
                            }
                        }

                        if (foundOne) {
                            break;
                        }
                    }
                }
            }

            String removal = "";

            if (!foundOne) {
                removal = "<deleteEntity name=\"" + refinementName + "\"/>";
            }

            String moml = "<group><relation name=\""
                    + transition.getName(container)
                    + "\"><property name=\"refinementName\" value=\""
                    + newRefinements.toString() + "\"/></relation>" + removal
                    + "</group>";
            MoMLChangeRequest change = new MoMLChangeRequest(this, container,
                    moml);
            container.requestChange(change);
        }
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.