Example #1
0
 /**
  * puts entity in container
  *
  * @param container The new container value
  * @param entity The new container value
  * @exception NameDuplicationException Description of the Exception
  * @exception IllegalActionException Description of the Exception
  */
 private static void setContainer(CompositeEntity container, NamedObj entity)
     throws NameDuplicationException, IllegalActionException {
   if (entity instanceof Attribute) {
     ((Attribute) entity).setContainer(container);
   } else if (entity instanceof ComponentEntity) {
     ((ComponentEntity) entity).setContainer(container);
   }
 }
Example #2
0
 /**
  * Remove all the attributes in the given class from the given container and all of its children
  * including ports, entities and relations, but not including attributes.
  *
  * @param container The container.
  * @param attributeClass The attribute class.
  * @exception IllegalActionException If an attribute cannot be removed.
  */
 public static void deepRemoveAttributes(
     NamedObj container, Class<? extends Attribute> attributeClass) throws IllegalActionException {
   List<Object> attributes = new LinkedList<Object>(container.attributeList(attributeClass));
   for (Object attribute : attributes) {
     try {
       ((Attribute) attribute).setContainer(null);
     } catch (NameDuplicationException e) {
       // This should not happen.
     }
   }
   for (Object child : getChildren(container, false, true, true, true)) {
     deepRemoveAttributes((NamedObj) child, attributeClass);
   }
 }
  /**
   * Evaluate the specified command.
   *
   * @param command The command.
   * @return The return value of the command, or null if there is none.
   * @exception Exception If something goes wrong processing the command.
   */
  public String evaluateCommand(String command) throws Exception {
    if (command.trim().equals("")) {
      return "";
    }

    PtParser parser = new PtParser();
    ASTPtRootNode node = parser.generateSimpleAssignmentParseTree(command);
    String targetName = null;

    // Figure out if we got an assignment... if so, then get the
    // identifier name and only evaluated the expression part.
    if (node instanceof ASTPtAssignmentNode) {
      ASTPtAssignmentNode assignmentNode = (ASTPtAssignmentNode) node;
      targetName = assignmentNode.getIdentifier();
      node = assignmentNode.getExpressionTree();
    }

    final NamedObj model = ((ExpressionShellEffigy) getContainer()).getModel();
    ParserScope scope =
        new ModelScope() {
          public ptolemy.data.Token get(String name) throws IllegalActionException {
            Variable result = getScopedVariable(null, model, name);

            if (result != null) {
              return result.getToken();
            } else {
              return null;
            }
          }

          public ptolemy.data.type.Type getType(String name) throws IllegalActionException {
            Variable result = getScopedVariable(null, model, name);

            if (result != null) {
              return result.getType();
            } else {
              return null;
            }
          }

          public InequalityTerm getTypeTerm(String name) throws IllegalActionException {
            Variable result = getScopedVariable(null, model, name);

            if (result != null) {
              return result.getTypeTerm();
            } else {
              return null;
            }
          }

          public Set identifierSet() {
            return getAllScopedVariableNames(null, model);
          }
        };

    Token result = _evaluator.evaluateParseTree(node, scope);

    // If a target was specified, instantiate a new token.
    if (targetName != null) {
      Attribute attribute = model.getAttribute(targetName);

      if (attribute != null && !(attribute instanceof Parameter)) {
        attribute.setContainer(null);
        attribute = null;
      }

      if (attribute == null) {
        attribute = new Parameter(model, targetName);
      }

      ((Parameter) attribute).setToken(result);
    }

    if (result == null) {
      return "";
    } else {
      return result.toString();
    }
  }