Skip to content

Commit

Permalink
Include LF_FEDERATES_BIN_DIRECTORY is cmake for federaes + Align reac…
Browse files Browse the repository at this point in the history
…tor-c
  • Loading branch information
ChadliaJerad committed Jun 20, 2024
1 parent 142930a commit 855fffa
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,20 @@
public class CExtensionUtils {

// Regular expression pattern for shared_ptr types.
static final Pattern sharedPointerVariable =
Pattern.compile("^(/\\*.*?\\*/)?std::shared_ptr<(?<type>((/\\*.*?\\*/)?(\\S+))+)>$");
static final Pattern sharedPointerVariable = Pattern
.compile("^(/\\*.*?\\*/)?std::shared_ptr<(?<type>((/\\*.*?\\*/)?(\\S+))+)>$");

/**
* Generate C code that initializes network actions.
*
* <p>These network actions will be triggered by federate.c whenever a message is received from
* <p>
* 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) {
Expand Down Expand Up @@ -89,8 +92,11 @@ public static String initializeTriggersForNetworkActions(
/**
* Generate C code that holds a sorted list of STP structs by time.
*
* <p>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
* <p>
* 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.
Expand All @@ -106,8 +112,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<Action> networkActions =
federate.stpToNetworkActionMap.get(federate.staaOffsets.get(i));
List<Action> networkActions = federate.stpToNetworkActionMap.get(federate.staaOffsets.get(i));

code.pr("staa_lst[" + i + "] = (staa_t*) malloc(sizeof(staa_t));");
code.pr(
Expand Down Expand Up @@ -140,15 +145,17 @@ 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
* @return A string containing the appropriate variable
*/
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
Expand All @@ -160,15 +167,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.
*
* <p>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
* <p>
* 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.
Expand Down Expand Up @@ -212,11 +227,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()));
}
Expand All @@ -229,16 +242,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.
*
* <p>Clock synchronization can be enabled using the clock-sync target property.
* <p>
* Clock synchronization can be enabled using the clock-sync target property.
*
* @see <a
* href="https://github.com/icyphy/lingua-franca/wiki/Distributed-Execution#clock-synchronization">Documentation</a>
* href=
* "https://github.com/icyphy/lingua-franca/wiki/Distributed-Execution#clock-synchronization">Documentation</a>
*/
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()
Expand All @@ -260,12 +277,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.
*
* <p>Clock synchronization can be enabled using the clock-sync target property.
* <p>
* Clock synchronization can be enabled using the clock-sync target property.
*
* @see <a
* href="https://github.com/icyphy/lingua-franca/wiki/Distributed-Execution#clock-synchronization">Documentation</a>
* href=
* "https://github.com/icyphy/lingua-franca/wiki/Distributed-Execution#clock-synchronization">Documentation</a>
*/
public static void addClockSyncCompileDefinitions(FederateInstance federate) {

Expand Down Expand Up @@ -296,10 +316,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();

Expand All @@ -310,6 +329,8 @@ public static void generateCMakeInclude(
"add_compile_definitions(LF_PACKAGE_DIRECTORY=\"" + fileConfig.srcPkgPath + "\")");
cmakeIncludeCode.pr(
"add_compile_definitions(LF_SOURCE_GEN_DIRECTORY=\"" + fileConfig.getSrcGenPath() + "\")");
cmakeIncludeCode.pr(
"add_compile_definitions(LF_FEDERATES_BIN_DIRECTORY=\"" + fileConfig.getFedBinPath() + "\")");
try (var srcWriter = Files.newBufferedWriter(cmakeIncludePath)) {
srcWriter.write(cmakeIncludeCode.getCode());
}
Expand All @@ -320,7 +341,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
Expand Down Expand Up @@ -393,14 +415,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(
Expand Down Expand Up @@ -457,26 +478,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);
}

Expand All @@ -485,39 +507,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);
}

Expand All @@ -538,7 +562,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) {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/resources/lib/c/reactor-c

0 comments on commit 855fffa

Please sign in to comment.