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();
}
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;
}
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);
}
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);
}
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);
}
}
}
Aggregations