Package ptolemy.math

Examples of ptolemy.math.Fraction


                remainingActors.remove(actor);
            }

            clusteredActors.add(actor);

            entityToFiringsPerIteration.put(actor, new Fraction(1));
            pendingActors.addLast(actor);

            while (!pendingActors.isEmpty()) {
                Actor currentActor = (Actor) pendingActors.removeFirst();
                Iterator actorPorts = ((ComponentEntity) currentActor)
                        .portList().iterator();

                while (actorPorts.hasNext()) {
                    IOPort currentPort = (IOPort) actorPorts.next();
                    _propagatePort(container, currentPort,
                            entityToFiringsPerIteration, externalRates,
                            remainingActors, pendingActors, clusteredActors,
                            clusteredExternalPorts);
                }
            }

            // Now we have _clusteredActors, which contains actors in
            // one cluster (they are connected). Find the LCM of their
            // denominator and normalize their firings. This means firings
            // of actors are only normalized within their cluster.
            int lcm = 1;

            for (Iterator actors = clusteredActors.iterator(); actors.hasNext();) {
                Actor currentActor = (Actor) actors.next();
                Fraction fraction = (Fraction) entityToFiringsPerIteration
                        .get(currentActor);
                int denominator = fraction.getDenominator();
                lcm = Fraction.lcm(lcm, denominator);
            }

            // Got the normalizing factor.
            Fraction lcmFraction = new Fraction(lcm);

            for (Iterator actors = clusteredActors.iterator(); actors.hasNext();) {
                Actor currentActor = (Actor) actors.next();
                Fraction repetitions = ((Fraction) entityToFiringsPerIteration
                        .get(currentActor)).multiply(lcmFraction);

                if (repetitions.getDenominator() != 1) {
                    throw new InternalErrorException(
                            "Failed to properly perform"
                                    + " fraction normalization.");
                }

                entityToFiringsPerIteration.put(currentActor, repetitions);
            }

            for (Iterator externalPorts = clusteredExternalPorts.iterator(); externalPorts
                    .hasNext();) {
                IOPort port = (IOPort) externalPorts.next();
                Fraction rate = ((Fraction) externalRates.get(port))
                        .multiply(lcmFraction);

                if (rate.getDenominator() != 1) {
                    throw new InternalErrorException(
                            "Failed to properly perform"
                                    + " fraction normalization.");
                }
View Full Code Here


        if (_debugging && VERBOSE) {
            _debug("Multiplying firings by vectorizationFactor = "
                    + vectorizationFactor);
        }

        Fraction lcmFraction = new Fraction(vectorizationFactor);

        // Use entrySet here for performance reasons.
        for (Iterator actorMapEntries = entityToFiringsPerIteration.entrySet()
                .iterator(); actorMapEntries.hasNext();) {
            Map.Entry actors = (Map.Entry) actorMapEntries.next();
            Fraction repetitions = (Fraction) actors.getValue();
            repetitions = repetitions.multiply(lcmFraction);

            // FIXME: Doing the conversion to Integer here is bizarre,
            // since they are integers coming in.
            actors.setValue(Integer.valueOf(repetitions.getNumerator()));
        }

        // Go through the ports and normalize the external production
        // and consumption rates by the same factor.
        // Use entrySet here for performance reasons.
        for (Iterator portMapEntries = externalRates.entrySet().iterator(); portMapEntries
                .hasNext();) {
            Map.Entry ports = (Map.Entry) portMapEntries.next();
            Fraction rate = (Fraction) ports.getValue();
            rate = rate.multiply(lcmFraction);

            // FIXME: Doing the conversion to Integer here is bizarre,
            // since they are integers coming in.
            ports.setValue(Integer.valueOf(rate.getNumerator()));
        }
    }
View Full Code Here

            Map externalRates) {

        Iterator inputPorts = container.inputPortList().iterator();
        while (inputPorts.hasNext()) {
            IOPort inputPort = (IOPort) inputPorts.next();
            Fraction rate = (Fraction) externalRates.get(inputPort);
            if (rate.equals(Fraction.ZERO)) {

                // Check to make sure that if this port is an external
                // input port, then it does not drive the same relation as some
                // other output port or some other external input port.
                // This results in a non-deterministic merge and is illegal.

                Iterator connectedPorts = inputPort.deepInsidePortList()
                        .iterator();

                // Make sure any connected output ports are connected on
                // the inside.
                while (connectedPorts.hasNext()) {
                    IOPort connectedPort = (IOPort) connectedPorts.next();

                    // connectPort might be connected on the inside to the
                    // currentPort, which is legal.  The container argument
                    // is always the container of the director, so any port
                    // that has that container must be connected on the inside.
                    if (connectedPort.isOutput()
                            && (connectedPort.getContainer() != container)) {
                        throw new NotSchedulableException(inputPort,
                                connectedPort,
                                "External input port drive the same relation "
                                        + "as an output port. "
                                        + "This is not legal in SDF.");
                    } else if (connectedPort.isInput()
                            && (connectedPort.getContainer() == container)) {
                        throw new NotSchedulableException(inputPort,
                                connectedPort,
                                "External input port drives the same relation "
                                        + "as another external input port. "
                                        + "This is not legal in SDF.");
                    }
                }

                boolean isDirectionConnection = true;
                List insideSinkPorts = inputPort.insideSinkPortList();

                // A dangling port has zero rate.
                if (insideSinkPorts.isEmpty()) {
                    isDirectionConnection = false;
                } else {
                    // If the zero external port rate is due to the rate
                    // propagation from a contained actor (i.e., connected to the
                    // zero rate port of the actor), then the zero external rate
                    // must be preserved.
                    Iterator sinkPorts = insideSinkPorts.iterator();
                    while (sinkPorts.hasNext()) {
                        IOPort sinkPort = (IOPort) sinkPorts.next();
                        if (sinkPort.getContainer() != container) {
                            isDirectionConnection = false;
                            break;
                        }
                    }
                }

                if (isDirectionConnection) {
                    externalRates.put(inputPort, new Fraction(1));
                    Iterator sinkPorts = insideSinkPorts.iterator();
                    while (sinkPorts.hasNext()) {
                        IOPort sinkPort = (IOPort) sinkPorts.next();
                        externalRates.put(sinkPort, new Fraction(1));
                    }
                }
            }
        }
    }
View Full Code Here

                connectedRate = DFUtilities.getRate(connectedPort);
            }

            // currentFiring is the firing ratio that we've already
            // calculated for currentActor
            Fraction currentFiring = (Fraction) entityToFiringsPerIteration
                    .get(currentActor);

            // Compute the firing ratio that we think connected actor
            // should have, based on its connection to currentActor
            Fraction desiredFiring;

            // HDF actors might have zero rates...
            if ((currentRate == 0) && (connectedRate > 0)) {
                // The current port of the current actor has a rate
                // of 0, and the current connected port of the
                // connected actor has a positive integer rate.
                // therefore, we must set the firing count of
                // the connected actor to 0 so that it will
                // not appear in the final static schedule.
                desiredFiring = Fraction.ZERO;
            } else if ((currentRate > 0) && (connectedRate == 0)) {
                // The current port of the current actor has a
                // positive integer rate, and the current
                // connected port of the connected actor has
                // rate of 0. therefore, we set the firing
                // count of the current actor to 0 so that
                // it will not appear in the final static schedule.
                currentFiring = Fraction.ZERO;

                // Update the entry in the firing table.
                entityToFiringsPerIteration.put(currentActor, currentFiring);

                // Set the firing count of the connected actor to
                // be 1.
                desiredFiring = new Fraction(1);
            } else if ((currentRate == 0) && (connectedRate == 0)) {
                // Give the connected actor the same rate as the
                // current actor.
                desiredFiring = currentFiring;
            } else {
                // Both the rates are non zero, so we can just do the
                // regular actor propagation.
                desiredFiring = currentFiring.multiply(new Fraction(
                        currentRate, connectedRate));
            }

            // Now, compare the firing ratio that was computed before
            // with what we just determined.
            // This should be either
            // the firing that we computed previously, or null
            // if the port is an external port, or _minusOne if
            // we have not computed the firing ratio for this actor yet.
            Fraction presentFiring = (Fraction) entityToFiringsPerIteration
                    .get(connectedActor);

            if (_debugging && VERBOSE) {
                _debug("presentFiring of connectedActor " + connectedActor
                        + " = " + presentFiring);
            }

            // if (presentFiring == null) {
            // Make sure to check for presentFiring == null here so that
            // we avoid a NullPointerException if the model is ill formed.
            // I had problems here with a bug in Publisher.clone() and
            // Subscriber.clone() where presentFiring was null.
            // Getting a NullPointerException is bad, we should check
            // for null and try to give a better message.
            if (connectedActor == model || presentFiring == null) {
                // We've gotten out to an external port.
                // Temporarily create the entry in the firing table.
                // This is possibly rather fragile.
                entityToFiringsPerIteration.put(connectedActor, desiredFiring);

                // Compute the external rate for this port.
                Fraction rate = currentFiring.multiply(new Fraction(
                        currentRate, 1));
                Fraction previousRate = (Fraction) externalRates
                        .get(connectedPort);

                if (previousRate == null) {
                    // This can happen if we somehow have a link to a port
                    // within a class definition.
View Full Code Here

        for (Iterator actors = allActorList.iterator(); actors.hasNext();) {
            ComponentEntity actor = (ComponentEntity) actors.next();

            // Remove this actor from the firing sequence if it will
            // not be fired.
            Fraction firing = (Fraction) entityToFiringsPerIteration.get(actor);

            if (_debugging && VERBOSE) {
                _debug("Actor " + actor.getName() + "fires "
                        + firing.getNumerator() + " times.");
            }

            if (firing.getNumerator() == 0) {
                if (_debugging && VERBOSE) {
                    _debug("and will be removed because "
                            + "it is not being fired.");
                }
View Full Code Here

TOP

Related Classes of ptolemy.math.Fraction

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.