Package ptolemy.data

Examples of ptolemy.data.ObjectToken


     if the file cannot be opened, or if the Java Media Framework
     *  throws an exception.
     *  @return super.postfire()
     */
    public boolean postfire() throws IllegalActionException {
        ObjectToken objectToken;
        Token token = input.get(0);

        try {
            objectToken = (ObjectToken) token;
        } catch (ClassCastException ex) {
            throw new IllegalActionException(this, ex, "Failed to cast "
                    + token.getClass() + " to an ObjectToken.\n"
                    + "The VideoPlayer actor expects to be connected to "
                    + "actors like the StreamLoader.\n"
                    + "Try connecting other actors to "
                    + "actor.lib.image.ImageDisplay.");
        }

        DataSource input = (DataSource) objectToken.getValue();

        if (_player != null) {
            _player.removeControllerListener(this);
        }

View Full Code Here


        }
    }

    public boolean postfire() throws IllegalActionException {
        if (getDirector().getModelTime().getDoubleValue() == 0.0) {
            output.send(0, new ObjectToken(_dataSource));
        }

        return super.postfire();
    }
View Full Code Here

     if the file cannot be opened, or if the Java Media Framework
     *  throws an exception.
     *  @return super.postfire()
     */
    public boolean postfire() throws IllegalActionException {
        ObjectToken objectToken = (ObjectToken) input.get(0);
        DataSource input = (DataSource) objectToken.getValue();

        if (_player != null) {
            _player.removeControllerListener(this);
        }

View Full Code Here

                    Attribute period = director.getAttribute("period");
                    if (period != null) {
                        Double periodValue = ((DoubleToken) ((Variable) period)
                                .getToken()).doubleValue();
                        if (periodValue != 0.0) {
                            return new ObjectToken("_currentTime");
                        }
                    }
                    return new DoubleToken("0.0");
                } else if (name.equals("iteration")) {
                    return new ObjectToken("$actorSymbol(iterationCount)");
                }

                for (int i = 0; i < _actor.inputPortList().size(); i++) {
                    if (((IOPort) _actor.inputPortList().get(i)).getName()
                            .equals(name)) {
                        return new ObjectToken("$ref(" + name + ")");
                    }
                }

                Attribute attribute = _actor.getAttribute(name);

                if (attribute == null) {
                    attribute = ModelScope
                            .getScopedVariable(null, _actor, name);
                }

                if (attribute != null) {
                    return new ObjectToken("$val(" + name + ")");
                }

                /*
                 for (int i = 0; i < _actor.attributeList().size(); i++) {
                 if (((Attribute) _actor.attributeList().get(i))
View Full Code Here

            }

            returnValue = new ArrayToken(temp);
        } else {
            // Package into an ObjectToken.
            returnValue = new ObjectToken(object);
        }

        return returnValue;
    }
View Full Code Here

    /** FIXME. Placeholder for a function that will return a model.
     */
    public static ObjectToken model(String classname)
            throws IllegalActionException {
        return new ObjectToken(classname);
    }
View Full Code Here

                }
            }

            if (matchInput.getWidth() > 0 && matchInput.hasToken(0)
                    && _lastModel != null) {
                ObjectToken token = (ObjectToken) matchInput.get(0);
                MatchResult match = (MatchResult) token.getValue();
                if (match != null) {
                    CompositeEntity host = (CompositeEntity) match
                            .get(getPattern());
                    if (_lastModel != host && !_lastModel.deepContains(host)) {
                        throw new IllegalActionException(this,
                                "The match result cannot be used with the "
                                        + "current model.");
                    }
                    GraphTransformer.transform(this, match);
                    modelOutput.send(0, new ActorToken(_lastModel));
                }
            }

            if (trigger.getWidth() > 0 && trigger.hasToken(0)
                    && !_lastResults.isEmpty()) {
                trigger.get(0);
                _lastResultsOperation = LastResultsOperation.REMOVE_FIRST;
                MatchResult result = _lastResults.peek();
                matchOutput.send(0, new ObjectToken(result));
            }
        } catch (TransformationException e) {
            throw new IllegalActionException(this, e,
                    "Unable to transform model.");
        }
View Full Code Here

                                throw new Exception(
                                        "Unknown object type: expected Set or List.");
                            }

                            result.addAll((Collection) ob);
                            return new ObjectToken(result);
                        } else {
                            throw new Exception(
                                    "Unknown object types: expected Collection.");
                        }
                    } else {
                        return a.add(b);
                    }
                } catch (Exception ex) {
                    throw new FunctionCallException("$add", args[0], args[1],
                            ex);
                }
            }

            public int arity() {
                return 2;
            }
        }));

        env.bind("$mul", _theContext.createFunction(new Function() {
            // Compute the multiply operation on scalar arguments,
            // or the set intersection operation on sets.
            public Object apply(Object[] args) {
                try {
                    Token a = (Token) args[0];
                    Token b = (Token) args[1];

                    if (a instanceof ObjectToken && b instanceof ObjectToken) {
                        Object oa = ((ObjectToken) a).getValue();
                        Object ob = ((ObjectToken) b).getValue();

                        if (oa instanceof Set && ob instanceof Collection) {
                            Set result = new HashSet((Set) oa);
                            result.retainAll((Collection) ob);
                            return new ObjectToken(result);
                        } else {
                            throw new InterpreterException(
                                    "Unknown object types: expected Set and Collection.");
                        }
                    } else {
                        return a.multiply(b);
                    }
                } catch (Exception ex) {
                    throw new FunctionCallException("$mul", args[0], args[1],
                            ex);
                }
            }

            public int arity() {
                return 2;
            }
        }));

        env.bind("$sub", _theContext.createFunction(new Function() {
            // Compute the subtraction operation on scalar arguments,
            // or the set subtraction operation on sets.
            public Object apply(Object[] args) {
                try {
                    Token a = (Token) args[0];
                    Token b = (Token) args[1];

                    if (a instanceof ObjectToken && b instanceof ObjectToken) {
                        Object oa = ((ObjectToken) a).getValue();
                        Object ob = ((ObjectToken) b).getValue();

                        if (oa instanceof Collection
                                && ob instanceof Collection) {
                            Collection result;

                            if (oa instanceof Set) {
                                result = new HashSet((Set) oa);
                            } else if (oa instanceof List) {
                                result = new ArrayList((List) oa);
                            } else {
                                throw new Exception(
                                        "Unknown object type: expected Set or List.");
                            }

                            result.removeAll((Collection) ob);
                            return new ObjectToken(result);
                        } else {
                            throw new InterpreterException(
                                    "Unknown object types: expected Collection.");
                        }
                    } else {
                        return a.subtract(b);
                    }
                } catch (Exception ex) {
                    throw new FunctionCallException("$sub", args[0], args[1],
                            ex);
                }
            }

            public int arity() {
                return 2;
            }
        }));

        env.bind("$div", _theContext.createFunction(new Function() {
            public Object apply(Object[] args) {
                try {
                    Token a = (Token) args[0];
                    Token b = (Token) args[1];
                    return a.divide(b);
                } catch (Exception ex) {
                    throw new FunctionCallException("$div", args[0], args[1],
                            ex);
                }
            }

            public int arity() {
                return 2;
            }
        }));

        env.bind("$mod", _theContext.createFunction(new Function() {
            public Object apply(Object[] args) {
                try {
                    Token a = (Token) args[0];
                    Token b = (Token) args[1];
                    return a.modulo(b);
                } catch (Exception ex) {
                    throw new FunctionCallException("$mod", args[0], args[1],
                            ex);
                }
            }

            public int arity() {
                return 2;
            }
        }));

        env.bind("$size", _theContext.createFunction(new Function() {
            // Compute the number of elements in the given set,
            // list, or array.
            public Object apply(Object[] args) {
                try {
                    Token a = (Token) args[0];

                    if (a instanceof ObjectToken) {
                        Object oa = ((ObjectToken) a).getValue();

                        if (oa instanceof Collection) {
                            return new IntToken(((Collection) oa).size());
                        } else {
                            throw new InterpreterException(
                                    "Unknown object type: expected Collection.");
                        }
                    } else if (a instanceof ArrayToken) {
                        return _theContext.createInteger(((ArrayToken) a)
                                .length());
                    } else {
                        throw new InterpreterException(
                                "Unknown type: expected Array, Set, or List");
                    }
                } catch (Exception ex) {
                    throw new FunctionCallException("$size", args[0], ex);
                }
            }

            public int arity() {
                return 1;
            }
        }));

        env.bind("$createList", _theContext.createFunction(new Function() {
            // Create a list that contains the results of applying
            // the second argument (a one argument function) to
            // every element in the first argument (a collection).
            public Object apply(Object[] args) {
                try {
                    Collection c = _theContext.getCollection(args[0]);
                    FunctionToken f = (FunctionToken) args[1];
                    Object[] argument = new Object[1];
                    List res = new ArrayList();

                    for (Iterator i = c.iterator(); i.hasNext();) {
                        argument[0] = i.next();

                        Object listFragment = _theContext.applyFunction(f,
                                argument);
                        res.addAll(_theContext.getCollection(listFragment));
                    }

                    return _theContext.createList(res);
                } catch (Exception ex) {
                    throw new FunctionCallException("Failed to create list.",
                            args[0], args[1], ex);
                }
            }

            public int arity() {
                return 2;
            }
        }));

        env.bind("$createSet", _theContext.createFunction(new Function() {
            // Create a set that contains the results of applying
            // the second argument (a one argument function) to
            // every element in the first argument (a collection).
            public Object apply(Object[] args) {
                try {
                    Collection c = _theContext.getCollection(args[0]);
                    FunctionToken f = (FunctionToken) args[1];
                    Object[] argument = new Object[1];
                    Set res = new HashSet();

                    for (Iterator i = c.iterator(); i.hasNext();) {
                        argument[0] = i.next();

                        Object setFragment = _theContext.applyFunction(f,
                                argument);
                        res.addAll(_theContext.getCollection(setFragment));
                    }

                    return _theContext.createSet(res);
                } catch (Exception ex) {
                    throw new FunctionCallException("Failed to create set.",
                            args[0], args[1], ex);
                }
            }

            public int arity() {
                return 2;
            }
        }));

        env.bind("$createMap", _theContext.createFunction(new Function() {
            // Create a map that contains the results of applying
            // the second argument (a one argument function) to
            // every element in the first argument (a collection).
            public Object apply(Object[] args) {
                try {
                    Collection c = _theContext.getCollection(args[0]);
                    FunctionToken f = (FunctionToken) args[1];
                    Object[] argument = new Object[1];
                    Map res = new HashMap();

                    for (Iterator i = c.iterator(); i.hasNext();) {
                        argument[0] = i.next();

                        Object mapFragment = _theContext.applyFunction(f,
                                argument);
                        res.putAll(_theContext.getMap(mapFragment));
                    }

                    return _theContext.createMap(res);
                } catch (Exception ex) {
                    throw new FunctionCallException("Failed to create map.",
                            args[0], args[1], ex);
                }
            }

            public int arity() {
                return 2;
            }
        }));

        env.bind("$iterate", _theContext.createProcedure(new Procedure() {
            // Invoke the second argument (a one argument
            // procedure) on every element of the first argument
            // (a collection).
            public void call(Object[] args) {
                try {
                    Collection c = _theContext.getCollection(args[0]);
                    Object proc = args[1];
                    Object[] argument = new Object[1];

                    for (Iterator i = c.iterator(); i.hasNext();) {
                        argument[0] = i.next();
                        _theContext.callProcedure(proc, argument);
                    }
                } catch (Exception ex) {
                    throw new FunctionCallException("Iteration failed.",
                            args[0], args[1], ex);
                }
            }

            public int arity() {
                return 2;
            }
        }));

        env.bind("listToArray", _theContext.createFunction(new Function() {
            // Convert the given list to an array.
            public Object apply(Object[] args) {
                try {
                    ObjectToken input = (ObjectToken) args[0];
                    List inputList = (List) input.getValue();
                    Token[] tokens = new Token[inputList.size()];
                    tokens = (Token[]) inputList.toArray(tokens);
                    return new ArrayToken(tokens);
                } catch (Exception ex) {
                    throw new FunctionCallException("listToArray", args[0], ex);
                }
            }

            public int arity() {
                return 1;
            }
        }));

        env.bind("listToMatrix", _theContext.createFunction(new Function() {
            // Convert the given list to an array.
            public Object apply(Object[] args) {
                try {
                    ObjectToken input = (ObjectToken) args[0];
                    List inputList = (List) input.getValue();
                    Token[] tokens = new Token[inputList.size()];
                    tokens = (Token[]) inputList.toArray(tokens);

                    int rows = _theContext.intValue(args[1]);
                    int columns = _theContext.intValue(args[2]);
                    return MatrixToken.arrayToMatrix(tokens, rows, columns);
                } catch (Exception ex) {
                    throw new FunctionCallException("listToArray", args[0], ex);
                }
            }

            public int arity() {
                return 3;
            }
        }));

        //
        // Xilinx SystemBuilder
        //
        //        constant YSCALE:   INT19 := conv_signed( integer( 1.164 * 256), 19 );
        //        constant RSCALE:   INT19 := conv_signed( integer( 1.596 * 256), 19 );
        //        constant GUSCALE:  INT19 := conv_signed( integer(-0.392 * 256), 19 );
        //        constant GVSCALE:  INT19 := conv_signed( integer(-0.813 * 256), 19 );
        //        constant BSCALE:   INT19 := conv_signed( integer( 2.017 * 256), 19 );
        //        constant YOFFSET:  INT19 := conv_signed(                    16, 19 );
        //        constant UVOFFSET: INT19 := conv_signed(                   128, 19 );
        //
        //        constant UINT9_zero: UINT9 := (others => '0' );
        //
        //        function INT19_mul( a: INT19; b: INT19 ) return INT19;
        //        function RGBCLIP( a: INT19 ) return UINT8;
        env.bind("UINT9_zero", _theContext.createInteger(0));
        env.bind("YSCALE", _theContext.createInteger((int) (1.164 * 256)));
        env.bind("RSCALE", _theContext.createInteger((int) (1.596 * 256)));
        env.bind("GUSCALE", _theContext.createInteger((int) (-0.392 * 256)));
        env.bind("GVSCALE", _theContext.createInteger((int) (-0.813 * 256)));
        env.bind("BSCALE", _theContext.createInteger((int) (2.017 * 256)));

        env.bind("YOFFSET", _theContext.createInteger(16));
        env.bind("UVOFFSET", _theContext.createInteger(128));

        env.bind("INT19_mul", _theContext.createFunction(new Function() {
            public Object apply(Object[] args) {
                try {
                    IntToken a = (IntToken) args[0];
                    IntToken b = (IntToken) args[1];
                    int res = (a.intValue() * b.intValue()); // & 0x7ffff;
                    return _theContext.createInteger(res);
                } catch (Exception ex) {
                    throw new InterpreterException(
                            "Function 'RGBCLIP': Cannot apply.", ex);
                }
            }

            public int arity() {
                return 2;
            }
        }));

        env.bind("RGBCLIP", _theContext.createFunction(new Function() {
            public Object apply(Object[] args) {
                try {
                    Token a = (Token) args[0];

                    if (a instanceof IntToken) {
                        int n = ((IntToken) a).intValue() / 256;
                        int res = (n > 255) ? 255 : ((n < 0) ? 0 : n);
                        return _theContext.createInteger(res);
                    } else {
                        throw new InterpreterException(
                                "RGBCLIP needs an IntToken.");
                    }
                } catch (Exception ex) {
                    throw new InterpreterException(
                            "Function 'RGBCLIP': Cannot apply.", ex);
                }
            }

            public int arity() {
                return 1;
            }
        }));

        env.bind("readByte", _theContext.createFunction(new Function() {
            public Object apply(Object[] args) {
                try {
                    Token a = (Token) args[0];

                    if (a instanceof ObjectToken) {
                        InputStream s = (InputStream) ((ObjectToken) a)
                                .getValue();
                        return _theContext.createInteger(s.read());
                    } else {
                        throw new InterpreterException("readByte needs a file.");
                    }
                } catch (Exception ex) {
                    throw new InterpreterException(
                            "Function 'readByte': Cannot apply.", ex);
                }
            }

            public int arity() {
                return 1;
            }
        }));

        env.bind("openFile", _theContext.createFunction(new Function() {
            public Object apply(Object[] args) {
                try {
                    Token a = (Token) args[0];

                    if (a instanceof StringToken) {
                        InputStream s = new FileInputStream(((StringToken) a)
                                .stringValue());
                        return new ObjectToken(s);
                    } else {
                        throw new InterpreterException(
                                "openFile needs a StringToken.");
                    }
                } catch (Exception ex) {
View Full Code Here

                                + token.toString());
                    }

                    if (functionToken.getFunction() instanceof LoopFunction) {
                        _evaluatedChildToken = functionToken.apply(new Token[] {
                                new ObjectToken(node.jjtGetChild(1)) });
                        return;
                    }
                }
                super.visitFunctionApplicationNode(node);
            }
View Full Code Here

            for (Object element : _collection) {
                Token elementToken;
                if (element instanceof Token) {
                    elementToken = (Token) element;
                } else {
                    elementToken = new ObjectToken(element);
                }
                _evaluator.setCurrentValue(currentValue);
                _evaluator.setElementToken(elementToken);
                currentValue = _evaluator.evaluateParseTree(tree);
            }
View Full Code Here

TOP

Related Classes of ptolemy.data.ObjectToken

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.