Package ptolemy.actor.util

Examples of ptolemy.actor.util.Time


                _zeroReference = 0.0;
                return;
            }

            double[] diff = new double[entryArray.length - 1];
            Time firstEntryTime = ((DEEvent) entryArray[0]).timeStamp();
            Time lastEntryTime = ((DEEvent) entryArray[entryArray.length - 1])
                    .timeStamp();

            if (firstEntryTime.isInfinite()
                    && firstEntryTime.equals(lastEntryTime)) {
                // To avoid setting NaN or 0.0
                // for the width, apparently due to simultaneous events,
                // we leave it unchanged instead.
                return;
            }

            double average = lastEntryTime.subtract(firstEntryTime)
                    .getDoubleValue();
            average = average / (entryArray.length - 1);

            double effectiveAverage = 0;
            int effectiveSamples = 0;
View Full Code Here


     @exception IllegalActionException If the serviceTime is invalid,
     *   or if an error occurs sending the output token.
     */
    public void fire() throws IllegalActionException {
        super.fire();
        Time currentTime = getDirector().getModelTime();

        // Consume the input.
        if (input.hasToken(0)) {
            _queue.put(input.get(0));
            size.send(0, new IntToken(_queue.size()));
        }

        // If appropriate, produce output.
        if (_queue.size() > 0 && currentTime.compareTo(_nextTimeFree) == 0) {
            Token outputToken = (Token) _queue.take();
            output.send(0, outputToken);
            size.send(0, new IntToken(_queue.size()));
            // Indicate that the server is free.
            _nextTimeFree = Time.NEGATIVE_INFINITY;
View Full Code Here

     */
    public boolean postfire() throws IllegalActionException {
        serviceTime.update();
        double serviceTimeValue = ((DoubleToken) serviceTime.getToken())
                .doubleValue();
        Time currentTime = getDirector().getModelTime();

        if (_nextTimeFree.equals(Time.NEGATIVE_INFINITY) && _queue.size() > 0) {
            _nextTimeFree = currentTime.add(serviceTimeValue);
            getDirector().fireAt(this, _nextTimeFree);
        }
        return super.postfire();
    }
View Full Code Here

     *   or writing inputs or outputs.
     */
    public void fire() throws IllegalActionException {
        super.fire();

        Time currentTime = getDirector().getModelTime();

        if (_debugging) {
            _debug("---------------------------------");
            _debug("Current time is: " + currentTime);
        }

        if (message.hasToken(0) && power.hasToken(0) && duration.hasToken(0)) {
            double powerValue = ((DoubleToken) power.get(0)).doubleValue();

            if (_debugging) {
                _debug("Received message with power: " + powerValue);
            }

            if (powerValue < _powerThreshold) {
                // The signal it too weak to be detected, simply drop it.
                message.get(0);
                duration.get(0);

                if (_debugging) {
                    _debug("Message power is below threshold. Ignoring.");
                }
            } else {
                // Record the reception.
                Reception reception = new Reception();
                reception.data = message.get(0);
                reception.power = powerValue;
                reception.arrivalTime = currentTime.getDoubleValue();
                reception.collided = false;
                reception.duration = ((DoubleToken) duration.get(0))
                        .doubleValue();

                if (_debugging) {
                    _debug("Message is above threshold and has duration: "
                            + reception.duration);
                }

                // Update the total power density.
                _totalPower = _totalPower + reception.power;

                // Put the new reception into the list of prior receptions.
                Time time = currentTime.add(reception.duration);
                reception.expiration = time.getDoubleValue();

                _receptions.add(reception);

                // Schedule this actor to be fired at the end of
                // the duration of the message.
View Full Code Here

     *  then produce an output token with value given by the <i>value</i>
     *  parameter.
     */
    public void fire() throws IllegalActionException {
        double eventTimeValue = ((DoubleToken) time.getToken()).doubleValue();
        Time eventTime = new Time(getDirector(), eventTimeValue);

        if (getDirector().getModelTime().equals(eventTime)) {
            output.send(0, value.getToken());
        }

View Full Code Here

        if (eventTime >= 0.0) {
            Director director = getDirector();

            if (director != null) {
                director.fireAt(this, new Time(director, eventTime));
            } else {
                throw new IllegalActionException(this, "No director.");
            }
        }
    }
View Full Code Here

    public void attributeChanged(Attribute attribute)
            throws IllegalActionException {
        if (attribute == startTime) {
            double startTimeValue = ((DoubleToken) startTime.getToken())
                    .doubleValue();
            _startTime = new Time(this, startTimeValue);
        } else if (attribute == stopTime) {
            double stopTimeValue = ((DoubleToken) stopTime.getToken())
                    .doubleValue();
            _stopTime = new Time(this, stopTimeValue);
        } else if (attribute == stopWhenQueueIsEmpty) {
            _stopWhenQueueIsEmpty = ((BooleanToken) stopWhenQueueIsEmpty
                    .getToken()).booleanValue();
        } else if (attribute == synchronizeToRealTime) {
            _synchronizeToRealTime = ((BooleanToken) synchronizeToRealTime
View Full Code Here

     *  in a DE model. If there is no event in the event queue, a positive
     *  infinity object is returned.
     *  @return The time stamp of the next event in the event queue.
     */
    public Time getModelNextIterationTime() {
        Time aFutureTime = Time.POSITIVE_INFINITY;

        // Record the model next iteration time as the tag of the the earliest
        // event in the queue.
        if (_eventQueue.size() > 0) {
            aFutureTime = _eventQueue.get().timeStamp();
        }

        // Iterate the event queue to find the earliest event with a bigger tag
        // ((either timestamp or microstop). If such an event exists,
        // use its time as the model next iteration time. If no such event
        // exists, it means that the model next iteration time still needs to
        // be resolved. In other words, the model next iteration time is
        // just the current time.
        Object[] events = _eventQueue.toArray();
        for (int i = 0; i < events.length; i++) {
            DEEvent event = (DEEvent) events[i];
            Time eventTime = event.timeStamp();
            int eventMicrostep = event.microstep();
            if (eventTime.compareTo(getModelTime()) > 0
                    || eventMicrostep > _microstep) {
                aFutureTime = eventTime;
                break;
            }
        }

        // Go through hierarchy to find the minimum step.
        Director executiveDirector = ((CompositeActor) getContainer())
                .getExecutiveDirector();
        if (executiveDirector != null) {
            Time aFutureTimeOfUpperLevel = executiveDirector
                    .getModelNextIterationTime();
            if (aFutureTime.compareTo(aFutureTimeOfUpperLevel) > 0) {
                aFutureTime = aFutureTimeOfUpperLevel;
            }
        }
View Full Code Here

            return result;
        }

        // If embedded, check the timestamp of the next event to decide
        // whether this director is ready to fire.
        Time modelTime = getModelTime();
        Time nextEventTime = Time.POSITIVE_INFINITY;

        if (!_eventQueue.isEmpty()) {
            nextEventTime = _eventQueue.get().timeStamp();
        }

        // If the model time is larger (later) than the first event
        // in the queue, catch up with the current model time by discarding
        // the old events. This can occur, for example, if we are in a
        // modal model and this director was in an inactive mode before
        // we reached the time of the event.
        while (modelTime.compareTo(nextEventTime) > 0) {
            _eventQueue.take();

            if (!_eventQueue.isEmpty()) {
                nextEventTime = _eventQueue.get().timeStamp();
            } else {
                nextEventTime = Time.POSITIVE_INFINITY;
            }
        }

        // If model time is strictly less than the next event time,
        // then there are no events on the event queue with this
        // model time, and hence, if there are also no input events,
        // then there is nothing to do, and we can return false.
        if (!nextEventTime.equals(modelTime)) {
            // If the event timestamp is greater than the model timestamp,
            // we check if there's any external input.
            CompositeActor container = (CompositeActor) getContainer();
            Iterator inputPorts = container.inputPortList().iterator();
            boolean hasInput = false;
View Full Code Here

            // TESTIT
            if (actorToFire == null) {
                // If the actorToFire is not set yet,
                // find the actor associated with the event just found,
                // and update the current tag with the event tag.
                Time currentTime;

                if (_synchronizeToRealTime) {
                    // If synchronized to the real time.
                    synchronized (_eventQueue) {
                        while (!_stopRequested && !_stopFireRequested) {
                            lastFoundEvent = _eventQueue.get();
                            currentTime = lastFoundEvent.timeStamp();

                            long elapsedTime = System.currentTimeMillis()
                                    - _realStartTime;

                            // NOTE: We assume that the elapsed time can be
                            // safely cast to a double.  This means that
                            // the DE domain has an upper limit on running
                            // time of Double.MAX_VALUE milliseconds.
                            double elapsedTimeInSeconds = elapsedTime / 1000.0;
                            ptolemy.actor.util.Time elapsed
                                    = new ptolemy.actor.util.Time(this, elapsedTimeInSeconds);
                            if (currentTime.compareTo(elapsed) <= 0) {
                                break;
                            }

                            // NOTE: We used to do the following, but it had a limitation.
                            // In particular, if any user code also calculated the elapsed
                            // time and then constructed a Time object to post an event
                            // on the event queue, there was no assurance that the quantization
                            // would be the same, and hence it was possible for that event
                            // to be in the past when posted, even if done in the same thread.
                            // To ensure that the comparison of current time against model time
                            // always yields the same result, we have to do the comparison using
                            // the Time class, which is what the event queue does.
                            /*
                            if (currentTime.getDoubleValue() <= elapsedTimeInSeconds) {
                                break;
                            }*/

                            long timeToWait = (long) (currentTime.subtract(
                                    elapsed).getDoubleValue() * 1000.0);

                            if (timeToWait > 0) {
                                if (_debugging) {
                                    _debug("Waiting for real time to pass: "
                                            + timeToWait);
                                }

                                try {
                                    // NOTE: The built-in Java wait() method
                                    // does not release the
                                    // locks on the workspace, which would block
                                    // UI interactions and may cause deadlocks.
                                    // SOLUTION: workspace.wait(object, long).
                                    _workspace.wait(_eventQueue, timeToWait);
                                    // If we get here and either stop() or stopFire()
                                    // was called, then it is not time to process any event,
                                    // so we should leave it in the event queue.
                                    if (_stopRequested || _stopFireRequested) {
                                        return null;
                                    }
                                } catch (InterruptedException ex) {
                                    // Continue executing.
                                }
                            }
                        } // while
                    } // sync
                } // if (_synchronizeToRealTime)

                // Consume the earliest event from the queue. The event must be
                // obtained here, since a new event could have been enqueued
                // into the queue while the queue was waiting. For example,
                // an IO interrupt event.
                // FIXME: The above statement is misleading. How could the
                // newly inserted event happen earlier than the previously
                // first event in the queue? It may be possible in the
                // distributed DE models, but should not happen in DE models.
                // Will this cause problems, such as setting time backwards?
                // TESTIT How to??
                synchronized (_eventQueue) {
                    lastFoundEvent = _eventQueue.take();
                    currentTime = lastFoundEvent.timeStamp();
                    actorToFire = lastFoundEvent.actor();

                    // NOTE: The _enqueueEvent method discards the events
                    // for disabled actors.
                    if ((_disabledActors != null)
                            && _disabledActors.contains(actorToFire)) {
                        // This actor has requested not to be fired again.
                        if (_debugging) {
                            _debug("Skipping disabled actor: ",
                                    ((Nameable) actorToFire).getFullName());
                        }

                        actorToFire = null;

                        // start a new iteration of the loop:
                        // LOOPLABEL::GetNextEvent
                        continue;
                    }

                    // Advance the current time to the event time.
                    // NOTE: This is the only place that the model time changes.
                    setModelTime(currentTime);

                    // Advance the current microstep to the event microstep.
                    _microstep = lastFoundEvent.microstep();
                }

                // Exceeding stop time means the current time is strictly
                // bigger than the model stop time.
                if (currentTime.compareTo(getModelStopTime()) > 0) {
                    if (_debugging) {
                        _debug("Current time has passed the stop time.");
                    }

                    _exceedStopTime = true;
View Full Code Here

TOP

Related Classes of ptolemy.actor.util.Time

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.