Search in sources :

Example 6 with Reaction

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

the class PythonReactorGenerator method generatePythonClass.

/**
 * Generate a Python class corresponding to decl
 * @param instance The reactor instance to be generated
 * @param pythonClasses The class definition is appended to this code builder
 * @param federate The federate instance for the reactor instance
 * @param instantiatedClasses A list of visited instances to avoid generating duplicates
 */
public static String generatePythonClass(ReactorInstance instance, FederateInstance federate, List<String> instantiatedClasses, ReactorInstance main, PythonTypes types) {
    CodeBuilder pythonClasses = new CodeBuilder();
    ReactorDecl decl = instance.getDefinition().getReactorClass();
    Reactor reactor = ASTUtils.toDefinition(decl);
    String className = instance.getDefinition().getReactorClass().getName();
    if (instance != main && !federate.contains(instance) || instantiatedClasses == null || // Do not generate code for delay reactors in Python
    className.contains(GeneratorBase.GEN_DELAY_CLASS_NAME)) {
        return "";
    }
    if (federate.contains(instance) && !instantiatedClasses.contains(className)) {
        pythonClasses.pr(generatePythonClassHeader(className));
        // Generate preamble code
        pythonClasses.indent();
        pythonClasses.pr(PythonPreambleGenerator.generatePythonPreambles(reactor.getPreambles()));
        // Handle runtime initializations
        pythonClasses.pr(generatePythonConstructor(decl, types));
        pythonClasses.pr(PythonParameterGenerator.generatePythonGetters(decl));
        List<Reaction> reactionToGenerate = ASTUtils.allReactions(reactor);
        if (reactor.isFederated()) {
            // Filter out reactions that are automatically generated in C in the top level federated reactor
            reactionToGenerate.removeIf(it -> !federate.contains(it) || federate.networkReactions.contains(it));
        }
        pythonClasses.pr(PythonReactionGenerator.generatePythonReactions(reactor, reactionToGenerate));
        pythonClasses.unindent();
        instantiatedClasses.add(className);
    }
    for (ReactorInstance child : instance.children) {
        pythonClasses.pr(generatePythonClass(child, federate, instantiatedClasses, main, types));
    }
    return pythonClasses.getCode();
}
Also used : ReactorInstance(org.lflang.generator.ReactorInstance) ReactorDecl(org.lflang.lf.ReactorDecl) Reactor(org.lflang.lf.Reactor) Reaction(org.lflang.lf.Reaction) CodeBuilder(org.lflang.generator.CodeBuilder)

Example 7 with Reaction

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

the class PortInstanceTests method newReaction.

/**
 * Return a new reaction triggered by the specified port.
 * @param trigger The triggering port.
 */
protected ReactionInstance newReaction(PortInstance trigger) {
    Reaction r = factory.createReaction();
    ReactionInstance result = new ReactionInstance(r, trigger.getParent(), false, trigger.getDependentReactions().size());
    trigger.getDependentReactions().add(result);
    trigger.getParent().reactions.add(result);
    return result;
}
Also used : ReactionInstance(org.lflang.generator.ReactionInstance) Reaction(org.lflang.lf.Reaction)

Example 8 with Reaction

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

the class FedASTUtils method addNetworkInputControlReaction.

/**
 * Add a network control reaction for a given input port 'destination' to
 * destination's parent reactor. This reaction will block for
 * any valid logical time until it is known whether the trigger for the
 * action corresponding to the given port is present or absent.
 *
 * @note Used in federated execution
 *
 * @param source The output port of the source federate reactor.
 *  Added as a trigger to the network control reaction to preserve the
 *  overall dependency structure of the program across federates.
 * @param destination The input port of the destination federate reactor.
 * @param recevingPortID The ID of the receiving port
 * @param bankIndex The bank index of the receiving federate, or -1 if not in a bank.
 * @param instance The federate instance is used to keep track of all
 *  network input ports globally
 * @param generator The GeneratorBase instance used to perform some target-specific actions
 */
private static void addNetworkInputControlReaction(PortInstance source, PortInstance destination, int recevingPortID, int bankIndex, FederateInstance instance, GeneratorBase generator) {
    LfFactory factory = LfFactory.eINSTANCE;
    Reaction reaction = factory.createReaction();
    VarRef sourceRef = factory.createVarRef();
    VarRef destRef = factory.createVarRef();
    // 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(reaction, bankIndex);
    // Create a new action that will be used to trigger the
    // input control reactions.
    Action newTriggerForControlReactionInput = factory.createAction();
    newTriggerForControlReactionInput.setOrigin(ActionOrigin.LOGICAL);
    // Set the container and variable according to the network port
    destRef.setContainer(destination.getParent().getDefinition());
    destRef.setVariable(destination.getDefinition());
    sourceRef.setContainer(source.getParent().getDefinition());
    sourceRef.setVariable(source.getDefinition());
    Reactor top = destination.getParent().getParent().reactorDefinition;
    newTriggerForControlReactionInput.setName(ASTUtils.getUniqueIdentifier(top, "inputControlReactionTrigger"));
    // Add the newly created Action to the action list of the federated reactor.
    top.getActions().add(newTriggerForControlReactionInput);
    // Create the trigger for the reaction
    VarRef newTriggerForControlReaction = factory.createVarRef();
    newTriggerForControlReaction.setVariable(newTriggerForControlReactionInput);
    // Add the appropriate triggers to the list of triggers of the reaction
    reaction.getTriggers().add(newTriggerForControlReaction);
    // Add the original output port of the source federate
    // as a trigger to keep the overall dependency structure.
    // This is useful when assigning levels.
    reaction.getTriggers().add(sourceRef);
    // Add this trigger to the list of disconnected network reaction triggers
    instance.remoteNetworkReactionTriggers.add(sourceRef);
    // Add the destination port as an effect of the reaction
    reaction.getEffects().add(destRef);
    // Generate code for the network input control reaction
    reaction.setCode(factory.createCode());
    TimeValue maxSTP = findMaxSTP(destination.getDefinition(), instance, generator, destination.getParent().reactorDefinition);
    reaction.getCode().setBody(generator.generateNetworkInputControlReactionBody(recevingPortID, maxSTP));
    generator.makeUnordered(reaction);
    // Insert the reaction
    top.getReactions().add(reaction);
    // Add the trigger for this reaction to the list of triggers, used to actually
    // trigger the reaction at the beginning of each logical time.
    instance.networkInputControlReactionsTriggers.add(newTriggerForControlReactionInput);
    // Add the network input control reaction to the federate instance's list
    // of network reactions
    instance.networkReactions.add(reaction);
}
Also used : VarRef(org.lflang.lf.VarRef) Action(org.lflang.lf.Action) LfFactory(org.lflang.lf.LfFactory) Reaction(org.lflang.lf.Reaction) Reactor(org.lflang.lf.Reactor) TimeValue(org.lflang.TimeValue)

Example 9 with Reaction

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

the class FedASTUtils method addNetworkReceiverReaction.

/**
 * Add a network receiver reaction for a given input port 'destination' to
 * destination's parent reactor. This reaction will react to a generated
 * 'networkAction' (triggered asynchronously, e.g., by federate.c). This
 * 'networkAction' will contain the actual message that is sent by the sender
 * in 'action->value'. This value is forwarded to 'destination' in the network
 * receiver reaction.
 *
 * @note: Used in federated execution
 *
 * @param networkAction The network action (also, @see createNetworkAction)
 * @param source The source port instance.
 * @param destination The destination port instance.
 * @param connection The network connection.
 * @param sourceFederate The source federate.
 * @param destinationFederate The destination federate.
 * @param rightBankIndex The right bank index or -1 if the right reactor is not in a bank.
 * @param rightChannelIndex The right channel index or -1 if the right port is not a multiport.
 * @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 addNetworkReceiverReaction(Action networkAction, PortInstance source, PortInstance destination, Connection connection, FederateInstance sourceFederate, FederateInstance destinationFederate, int rightBankIndex, int rightChannelIndex, GeneratorBase generator, CoordinationType coordination, SupportedSerializers serializer) {
    LfFactory factory = LfFactory.eINSTANCE;
    VarRef sourceRef = factory.createVarRef();
    VarRef destRef = factory.createVarRef();
    Reactor parent = (Reactor) connection.eContainer();
    Reaction networkReceiverReaction = factory.createReaction();
    // These reactions do not require any dependency relationship
    // to other reactions in the container.
    generator.makeUnordered(networkReceiverReaction);
    // 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(networkReceiverReaction, rightBankIndex);
    // The connection is 'physical' if it uses the ~> notation.
    if (connection.isPhysical()) {
        destinationFederate.inboundP2PConnections.add(sourceFederate);
    } else {
        // to make P2P connections
        if (coordination == CoordinationType.DECENTRALIZED) {
            destinationFederate.inboundP2PConnections.add(sourceFederate);
        }
    }
    // 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());
    if (!connection.isPhysical()) {
        // If the connection is not physical,
        // add the original output port of the source federate
        // as a trigger to keep the overall dependency structure.
        // This is useful when assigning levels.
        VarRef senderOutputPort = factory.createVarRef();
        senderOutputPort.setContainer(source.getParent().getDefinition());
        senderOutputPort.setVariable(source.getDefinition());
        networkReceiverReaction.getTriggers().add(senderOutputPort);
        // Add this trigger to the list of disconnected network reaction triggers
        destinationFederate.remoteNetworkReactionTriggers.add(senderOutputPort);
    }
    // Add the input port at the receiver federate reactor as an effect
    networkReceiverReaction.getEffects().add(destRef);
    VarRef triggerRef = factory.createVarRef();
    // Establish references to the action.
    triggerRef.setVariable(networkAction);
    // Add the action as a trigger to the receiver reaction
    networkReceiverReaction.getTriggers().add(triggerRef);
    // Generate code for the network receiver reaction
    networkReceiverReaction.setCode(factory.createCode());
    networkReceiverReaction.getCode().setBody(generator.generateNetworkReceiverBody(networkAction, sourceRef, destRef, receivingPortID, sourceFederate, destinationFederate, rightBankIndex, rightChannelIndex, ASTUtils.getInferredType(networkAction), connection.isPhysical(), serializer));
    // Add the receiver reaction to the parent
    parent.getReactions().add(networkReceiverReaction);
    // Add the network receiver reaction to the federate instance's list
    // of network reactions
    destinationFederate.networkReactions.add(networkReceiverReaction);
}
Also used : VarRef(org.lflang.lf.VarRef) LfFactory(org.lflang.lf.LfFactory) Reactor(org.lflang.lf.Reactor) Reaction(org.lflang.lf.Reaction)

Example 10 with Reaction

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

the class FederateInstance method indexExcludedTopLevelReactions.

/**
 * Build an index of reactions at the top-level (in the
 * federatedReactor) that don't belong to this federate
 * instance. This index is put in the excludeReactions
 * class variable.
 *
 * @param federatedReactor The top-level federated reactor
 */
private void indexExcludedTopLevelReactions(Reactor federatedReactor) {
    boolean inFederate = false;
    if (excludeReactions != null) {
        throw new IllegalStateException("The index for excluded reactions at the top level is already built.");
    }
    excludeReactions = new LinkedHashSet<Reaction>();
    // Construct the set of excluded reactions for this federate.
    // If a reaction is a network reaction that belongs to this federate, we
    // don't need to perform this analysis.
    Iterable<Reaction> reactions = IterableExtensions.filter(ASTUtils.allReactions(federatedReactor), it -> {
        return !networkReactions.contains(it);
    });
    for (Reaction react : reactions) {
        // Create a collection of all the VarRefs (i.e., triggers, sources, and effects) in the react
        // signature that are ports that reference federates.
        // We then later check that all these VarRefs reference this federate. If not, we will add this
        // react to the list of reactions that have to be excluded (note that mixing VarRefs from
        // different federates is not allowed).
        List<VarRef> allVarRefsReferencingFederates = new ArrayList<VarRef>();
        // Add all the triggers that are outputs
        Stream<VarRef> triggersAsVarRef = react.getTriggers().stream().filter(it -> it instanceof VarRef).map(it -> (VarRef) it);
        allVarRefsReferencingFederates.addAll(triggersAsVarRef.filter(it -> it.getVariable() instanceof Output).collect(Collectors.toList()));
        // Add all the sources that are outputs
        allVarRefsReferencingFederates.addAll(react.getSources().stream().filter(it -> it.getVariable() instanceof Output).collect(Collectors.toList()));
        // Add all the effects that are inputs
        allVarRefsReferencingFederates.addAll(react.getEffects().stream().filter(it -> it.getVariable() instanceof Input).collect(Collectors.toList()));
        inFederate = containsAllVarRefs(allVarRefsReferencingFederates);
        if (!inFederate) {
            excludeReactions.add(react);
        }
    }
}
Also used : VarRef(org.lflang.lf.VarRef) Variable(org.lflang.lf.Variable) Delay(org.lflang.lf.Delay) ErrorReporter(org.lflang.ErrorReporter) Action(org.lflang.lf.Action) Input(org.lflang.lf.Input) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Instantiation(org.lflang.lf.Instantiation) TriggerInstance(org.lflang.generator.TriggerInstance) Reaction(org.lflang.lf.Reaction) Objects(com.google.common.base.Objects) TimeValue(org.lflang.TimeValue) LinkedHashSet(java.util.LinkedHashSet) GeneratorBase(org.lflang.generator.GeneratorBase) Set(java.util.Set) PortInstance(org.lflang.generator.PortInstance) ReactionInstance(org.lflang.generator.ReactionInstance) Collectors(java.util.stream.Collectors) ActionInstance(org.lflang.generator.ActionInstance) ReactorInstance(org.lflang.generator.ReactorInstance) List(java.util.List) Stream(java.util.stream.Stream) IterableExtensions(org.eclipse.xtext.xbase.lib.IterableExtensions) Reactor(org.lflang.lf.Reactor) ActionOrigin(org.lflang.lf.ActionOrigin) VarRef(org.lflang.lf.VarRef) ASTUtils(org.lflang.ASTUtils) Output(org.lflang.lf.Output) Timer(org.lflang.lf.Timer) TriggerRef(org.lflang.lf.TriggerRef) Input(org.lflang.lf.Input) Output(org.lflang.lf.Output) ArrayList(java.util.ArrayList) Reaction(org.lflang.lf.Reaction)

Aggregations

Reaction (org.lflang.lf.Reaction)14 Reactor (org.lflang.lf.Reactor)11 VarRef (org.lflang.lf.VarRef)9 Action (org.lflang.lf.Action)6 LfFactory (org.lflang.lf.LfFactory)6 ArrayList (java.util.ArrayList)4 List (java.util.List)4 TimeValue (org.lflang.TimeValue)4 GeneratorBase (org.lflang.generator.GeneratorBase)4 ActionOrigin (org.lflang.lf.ActionOrigin)4 Connection (org.lflang.lf.Connection)4 Delay (org.lflang.lf.Delay)4 Instantiation (org.lflang.lf.Instantiation)4 Parameter (org.lflang.lf.Parameter)4 Type (org.lflang.lf.Type)4 Value (org.lflang.lf.Value)4 Collectors (java.util.stream.Collectors)3 EcoreUtil (org.eclipse.emf.ecore.util.EcoreUtil)3 ASTUtils (org.lflang.ASTUtils)3 InferredType (org.lflang.InferredType)3