From d518b37962e08cca6ae8376fa6682036689856cf Mon Sep 17 00:00:00 2001 From: Chadlia Jerad Date: Thu, 20 Jun 2024 09:55:33 +0100 Subject: [PATCH] Include LF_FEDERATES_BIN_DIRECTORY in cmake for federaes + Align reactor-c --- .../federated/extensions/CExtensionUtils.java | 162 ++++++++++-------- 1 file changed, 93 insertions(+), 69 deletions(-) diff --git a/core/src/main/java/org/lflang/federated/extensions/CExtensionUtils.java b/core/src/main/java/org/lflang/federated/extensions/CExtensionUtils.java index ad982431a2..768dcdb648 100644 --- a/core/src/main/java/org/lflang/federated/extensions/CExtensionUtils.java +++ b/core/src/main/java/org/lflang/federated/extensions/CExtensionUtils.java @@ -36,17 +36,20 @@ public class CExtensionUtils { // Regular expression pattern for shared_ptr types. - static final Pattern sharedPointerVariable = - Pattern.compile("^(/\\*.*?\\*/)?std::shared_ptr<(?((/\\*.*?\\*/)?(\\S+))+)>$"); + static final Pattern sharedPointerVariable = Pattern + .compile("^(/\\*.*?\\*/)?std::shared_ptr<(?((/\\*.*?\\*/)?(\\S+))+)>$"); /** * Generate C code that initializes network actions. * - *

These network actions will be triggered by federate.c whenever a message is received from + *

+ * These network actions will be triggered by federate.c whenever a message is + * received from * the network. * * @param federate The federate. - * @param main The main reactor that contains the federate (used to lookup references). + * @param main The main reactor that contains the federate (used to lookup + * references). */ public static String initializeTriggersForNetworkActions( FederateInstance federate, ReactorInstance main) { @@ -92,8 +95,11 @@ public static String initializeTriggersForNetworkActions( /** * Generate C code that holds a sorted list of STAA structs by time. * - *

For decentralized execution, on every logical timestep, a thread will iterate through each - * staa struct, wait for the designated offset time, and set the associated port status to absent + *

+ * For decentralized execution, on every logical timestep, a thread will iterate + * through each + * staa struct, wait for the designated offset time, and set the associated port + * status to absent * if it isn't known. * * @param federate The federate. @@ -109,8 +115,7 @@ public static String stpStructs(FederateInstance federate) { // main reactor for each Action. for (int i = 0; i < federate.staaOffsets.size(); ++i) { // Find the corresponding ActionInstance. - List networkActions = - federate.staToNetworkActionMap.get(federate.staaOffsets.get(i)); + List networkActions = federate.staToNetworkActionMap.get(federate.staaOffsets.get(i)); code.pr("staa_lst[" + i + "] = (staa_t*) malloc(sizeof(staa_t));"); code.pr( @@ -143,7 +148,8 @@ public static String stpStructs(FederateInstance federate) { } /** - * Create a port status field variable for a network input port "input" in the self struct of a + * Create a port status field variable for a network input port "input" in the + * self struct of a * reactor. * * @param input The network input port @@ -151,7 +157,8 @@ public static String stpStructs(FederateInstance federate) { */ public static String createPortStatusFieldForInput(Input input) { StringBuilder builder = new StringBuilder(); - // If it is not a multiport, then we could re-use the port trigger, and nothing needs to be done + // If it is not a multiport, then we could re-use the port trigger, and nothing + // needs to be done if (ASTUtils.isMultiport(input)) { // If it is a multiport, then create an auxiliary list of port // triggers for each channel of @@ -163,15 +170,23 @@ public static String createPortStatusFieldForInput(Input input) { } /** - * Given a connection 'delay' expression, return a string that represents the interval_t value of + * Given a connection 'delay' expression, return a string that represents the + * interval_t value of * the additional delay that needs to be applied to the outgoing message. * - *

The returned additional delay in absence of after on network connection (i.e., if delay is - * passed as a null) is NEVER. This has a special meaning in C library functions that send network - * messages that carry timestamps (@see lf_send_tagged_message and lf_send_port_absent_to_federate - * in lib/core/federate.c). In this case, the sender will send its current tag as the timestamp of - * the outgoing message without adding a microstep delay. If the user has assigned an after delay - * to the network connection (that can be zero) either as a time value (e.g., 200 msec) or as a + *

+ * The returned additional delay in absence of after on network connection + * (i.e., if delay is + * passed as a null) is NEVER. This has a special meaning in C library functions + * that send network + * messages that carry timestamps (@see lf_send_tagged_message and + * lf_send_port_absent_to_federate + * in lib/core/federate.c). In this case, the sender will send its current tag + * as the timestamp of + * the outgoing message without adding a microstep delay. If the user has + * assigned an after delay + * to the network connection (that can be zero) either as a time value (e.g., + * 200 msec) or as a * literal (e.g., a parameter), that delay in nsec will be returned. * * @param delay The delay associated with a connection. @@ -215,11 +230,9 @@ public static void handleCompileDefinitions( } private static void handleAdvanceMessageInterval(FederateInstance federate) { - var advanceMessageInterval = - federate.targetConfig.get(CoordinationOptionsProperty.INSTANCE).advanceMessageInterval; + var advanceMessageInterval = federate.targetConfig.get(CoordinationOptionsProperty.INSTANCE).advanceMessageInterval; if (advanceMessageInterval != null) { - federate - .targetConfig + federate.targetConfig .get(CompileDefinitionsProperty.INSTANCE) .put("ADVANCE_MESSAGE_INTERVAL", String.valueOf(advanceMessageInterval.toNanoSeconds())); } @@ -232,16 +245,20 @@ static boolean clockSyncIsOn(FederateInstance federate, RtiConfig rtiConfig) { } /** - * Initialize clock synchronization (if enabled) and its related options for a given federate. + * Initialize clock synchronization (if enabled) and its related options for a + * given federate. * - *

Clock synchronization can be enabled using the clock-sync target property. + *

+ * Clock synchronization can be enabled using the clock-sync target property. * * @see Documentation + * href= + * "https://github.com/icyphy/lingua-franca/wiki/Distributed-Execution#clock-synchronization">Documentation */ public static void initializeClockSynchronization( FederateInstance federate, RtiConfig rtiConfig, MessageReporter messageReporter) { - // Check if clock synchronization should be enabled for this federate in the first place + // Check if clock synchronization should be enabled for this federate in the + // first place if (clockSyncIsOn(federate, rtiConfig)) { messageReporter .nowhere() @@ -263,12 +280,15 @@ public static void initializeClockSynchronization( } /** - * Initialize clock synchronization (if enabled) and its related options for a given federate. + * Initialize clock synchronization (if enabled) and its related options for a + * given federate. * - *

Clock synchronization can be enabled using the clock-sync target property. + *

+ * Clock synchronization can be enabled using the clock-sync target property. * * @see Documentation + * href= + * "https://github.com/icyphy/lingua-franca/wiki/Distributed-Execution#clock-synchronization">Documentation */ public static void addClockSyncCompileDefinitions(FederateInstance federate) { @@ -299,10 +319,9 @@ public static void generateCMakeInclude( FederateInstance federate, FederationFileConfig fileConfig) throws IOException { Files.createDirectories(fileConfig.getSrcPath().resolve("include")); - Path cmakeIncludePath = - fileConfig - .getSrcPath() - .resolve("include" + File.separator + federate.name + "_extension.cmake"); + Path cmakeIncludePath = fileConfig + .getSrcPath() + .resolve("include" + File.separator + federate.name + "_extension.cmake"); CodeBuilder cmakeIncludeCode = new CodeBuilder(); @@ -324,7 +343,8 @@ public static void generateCMakeInclude( } /** - * Generate code that sends the neighbor structure message to the RTI. See {@code + * Generate code that sends the neighbor structure message to the RTI. See + * {@code * MSG_TYPE_NEIGHBOR_STRUCTURE} in {@code federated/net_common.h}. * * @param federate The federate that is sending its neighbor structure @@ -397,14 +417,13 @@ public static String generateFederateNeighborStructure(FederateInstance federate // Use NEVER to encode no delay at all. code.pr("candidate_tmp = NEVER;"); } else { - var delayTime = - delay instanceof ParameterReference - // In that case use the default value. - ? CTypes.getInstance() - .getTargetTimeExpr( - ASTUtils.getDefaultAsTimeValue( - ((ParameterReference) delay).getParameter())) - : CTypes.getInstance().getTargetExpr(delay, InferredType.time()); + var delayTime = delay instanceof ParameterReference + // In that case use the default value. + ? CTypes.getInstance() + .getTargetTimeExpr( + ASTUtils.getDefaultAsTimeValue( + ((ParameterReference) delay).getParameter())) + : CTypes.getInstance().getTargetExpr(delay, InferredType.time()); code.pr( String.join( @@ -461,26 +480,27 @@ public static String surroundWithIfElseFederated(String insideIf, String insideE return surroundWithIfFederated(insideIf); } else { return """ - #ifdef FEDERATED - %s - #else - %s - #endif // FEDERATED - """ + #ifdef FEDERATED + %s + #else + %s + #endif // FEDERATED + """ .formatted(insideIf, insideElse); } } /** - * Surround {@code code} with blocks to ensure that code only executes if the program is + * Surround {@code code} with blocks to ensure that code only executes if the + * program is * federated. */ public static String surroundWithIfFederated(String code) { return """ - #ifdef FEDERATED - %s - #endif // FEDERATED - """ + #ifdef FEDERATED + %s + #endif // FEDERATED + """ .formatted(code); } @@ -489,39 +509,41 @@ public static String surroundWithIfElseFederatedCentralized(String insideIf, Str return surroundWithIfFederatedCentralized(insideIf); } else { return """ - #ifdef FEDERATED_CENTRALIZED - %s - #else - %s - #endif // FEDERATED_CENTRALIZED - """ + #ifdef FEDERATED_CENTRALIZED + %s + #else + %s + #endif // FEDERATED_CENTRALIZED + """ .formatted(insideIf, insideElse); } } /** - * Surround {@code code} with blocks to ensure that code only executes if the program is federated + * Surround {@code code} with blocks to ensure that code only executes if the + * program is federated * and has a centralized coordination. */ public static String surroundWithIfFederatedCentralized(String code) { return """ - #ifdef FEDERATED_CENTRALIZED - %s - #endif // FEDERATED_CENTRALIZED - """ + #ifdef FEDERATED_CENTRALIZED + %s + #endif // FEDERATED_CENTRALIZED + """ .formatted(code); } /** - * Surround {@code code} with blocks to ensure that code only executes if the program is federated + * Surround {@code code} with blocks to ensure that code only executes if the + * program is federated * and has a decentralized coordination. */ public static String surroundWithIfFederatedDecentralized(String code) { return """ - #ifdef FEDERATED_DECENTRALIZED - %s - #endif // FEDERATED_DECENTRALIZED - """ + #ifdef FEDERATED_DECENTRALIZED + %s + #endif // FEDERATED_DECENTRALIZED + """ .formatted(code); } @@ -542,7 +564,9 @@ public static String generateSerializationIncludes(FederateInstance federate) { return code.getCode(); } - /** Generate cmake-include code needed for enabled serializers of the federate. */ + /** + * Generate cmake-include code needed for enabled serializers of the federate. + */ public static String generateSerializationCMakeExtension(FederateInstance federate) { CodeBuilder code = new CodeBuilder(); for (SupportedSerializers serializer : federate.enabledSerializers) {