Search in sources :

Example 1 with CoordinationType

use of org.lflang.TargetProperty.CoordinationType in project lingua-franca by lf-lang.

the class FedASTUtils method addNetworkSenderReaction.

/**
 * Add a network sender reaction for a given input port 'source' to
 * source's parent reactor. This reaction will react to the 'source'
 * and then send a message on the network destined for the destinationFederate.
 *
 * @note Used in federated execution
 *
 * @param source The source port instance.
 * @param destination The destination port instance.
 * @param connection The network connection.
 * @param sourceFederate The source federate.
 * @param leftBankIndex The left bank index or -1 if the left reactor is not in a bank.
 * @param leftChannelIndex The left channel index or -1 if the left port is not a multiport.
 * @param destinationFederate The destination federate.
 * @param generator The GeneratorBase instance used to perform some target-specific actions
 * @param coordination One of CoordinationType.DECENTRALIZED or CoordinationType.CENTRALIZED.
 * @param serializer The serializer used on the connection
 */
private static void addNetworkSenderReaction(PortInstance source, PortInstance destination, Connection connection, FederateInstance sourceFederate, int leftBankIndex, int leftChannelIndex, FederateInstance destinationFederate, GeneratorBase generator, CoordinationType coordination, SupportedSerializers serializer) {
    LfFactory factory = LfFactory.eINSTANCE;
    // Assume all the types are the same, so just use the first on the right.
    Type type = EcoreUtil.copy(source.getDefinition().getType());
    VarRef sourceRef = factory.createVarRef();
    VarRef destRef = factory.createVarRef();
    Reactor parent = (Reactor) connection.eContainer();
    Reaction networkSenderReaction = factory.createReaction();
    // These reactions do not require any dependency relationship
    // to other reactions in the container.
    generator.makeUnordered(networkSenderReaction);
    // If the sender or receiver is in a bank of reactors, then we want
    // these reactions to appear only in the federate whose bank ID matches.
    generator.setReactionBankIndex(networkSenderReaction, leftBankIndex);
    // The connection is 'physical' if it uses the ~> notation.
    if (connection.isPhysical()) {
        sourceFederate.outboundP2PConnections.add(destinationFederate);
    } else {
        // to make P2P connections
        if (coordination == CoordinationType.DECENTRALIZED) {
            sourceFederate.outboundP2PConnections.add(destinationFederate);
        }
    }
    // Record this action in the right federate.
    // The ID of the receiving port (rightPort) is the position
    // of the action in this list.
    int receivingPortID = destinationFederate.networkMessageActions.size();
    // Establish references to the involved ports.
    sourceRef.setContainer(source.getParent().getDefinition());
    sourceRef.setVariable(source.getDefinition());
    destRef.setContainer(destination.getParent().getDefinition());
    destRef.setVariable(destination.getDefinition());
    // Configure the sending reaction.
    networkSenderReaction.getTriggers().add(sourceRef);
    networkSenderReaction.setCode(factory.createCode());
    networkSenderReaction.getCode().setBody(generator.generateNetworkSenderBody(sourceRef, destRef, receivingPortID, sourceFederate, leftBankIndex, leftChannelIndex, destinationFederate, InferredType.fromAST(type), connection.isPhysical(), connection.getDelay(), serializer));
    // Add the sending reaction to the parent.
    parent.getReactions().add(networkSenderReaction);
    // Add the network sender reaction to the federate instance's list
    // of network reactions
    sourceFederate.networkReactions.add(networkSenderReaction);
}
Also used : VarRef(org.lflang.lf.VarRef) InferredType(org.lflang.InferredType) Type(org.lflang.lf.Type) CoordinationType(org.lflang.TargetProperty.CoordinationType) LfFactory(org.lflang.lf.LfFactory) Reactor(org.lflang.lf.Reactor) Reaction(org.lflang.lf.Reaction)

Aggregations

InferredType (org.lflang.InferredType)1 CoordinationType (org.lflang.TargetProperty.CoordinationType)1 LfFactory (org.lflang.lf.LfFactory)1 Reaction (org.lflang.lf.Reaction)1 Reactor (org.lflang.lf.Reactor)1 Type (org.lflang.lf.Type)1 VarRef (org.lflang.lf.VarRef)1