Skip to content

Commit

Permalink
Cleanup after #1457 merge
Browse files Browse the repository at this point in the history
  • Loading branch information
oowekyala committed Nov 9, 2022
1 parent 3bc4f6b commit 71ef8d0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 76 deletions.
10 changes: 5 additions & 5 deletions org.lflang/src/org/lflang/federated/generator/FedEmitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ Map<Path, CodeMap> generateFederate(

String federateCode = String.join(
"\n",
(new FedTargetEmitter()).generateTarget(context, numOfFederates, federate, fileConfig, errorReporter, federationRTIProperties),
(new FedImportEmitter()).generateImports(federate, fileConfig),
(new FedPreambleEmitter()).generatePreamble(federate, fileConfig, federationRTIProperties, errorReporter),
(new FedReactorEmitter()).generateReactorDefinitions(federate),
(new FedMainEmitter()).generateMainReactor(
new FedTargetEmitter().generateTarget(context, numOfFederates, federate, fileConfig, errorReporter, federationRTIProperties),
new FedImportEmitter().generateImports(federate, fileConfig),
new FedPreambleEmitter().generatePreamble(federate, fileConfig, federationRTIProperties, errorReporter),
new FedReactorEmitter().generateReactorDefinitions(federate),
new FedMainEmitter().generateMainReactor(
federate,
originalMainReactor,
errorReporter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ String generatePreamble(FederateInstance federate, FedFileConfig fileConfig, Lin
%s
=}
""".formatted(
p.getVisibility() == null ? "":p.getVisibility() + " ",
p.getVisibility() == null ? "" : p.getVisibility() + " ",
toText(p.getCode())
));
}
Expand All @@ -43,4 +43,4 @@ String generatePreamble(FederateInstance federate, FedFileConfig fileConfig, Lin

return preambleCode.getCode();
}
}
}
46 changes: 16 additions & 30 deletions org.lflang/src/org/lflang/generator/ts/TSConstructorGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import org.lflang.TargetConfig
import org.lflang.generator.PrependOperator
import org.lflang.generator.getTargetInitializer
import org.lflang.joinWithLn
import org.lflang.lf.Action
import org.lflang.lf.Parameter
import org.lflang.lf.Reactor
import java.util.*
Expand All @@ -18,17 +17,16 @@ import java.util.*
* and code to register reactions. This generator also generates federate port action
* registrations.
*/
class TSConstructorGenerator (
private val tsGenerator: TSGenerator,
class TSConstructorGenerator(
private val errorReporter: ErrorReporter,
private val reactor : Reactor
private val reactor: Reactor
) {

private fun initializeParameter(p: Parameter): String =
"${p.name}: ${TSTypes.getTargetType(p)} = ${TSTypes.getTargetInitializer(p)}"

private fun generateConstructorArguments(reactor: Reactor): String {
val arguments = LinkedList<String>()
val arguments = StringJoiner(", \n")
if (reactor.isMain || reactor.isFederated) {
arguments.add("timeout: TimeValue | undefined = undefined")
arguments.add("keepAlive: boolean = false")
Expand All @@ -48,14 +46,13 @@ class TSConstructorGenerator (
arguments.add("fail?: () => void")
}

return arguments.joinToString(", \n")
return arguments.toString()
}

private fun generateSuperConstructorCall(reactor: Reactor, isFederate: Boolean): String {
private fun generateSuperConstructorCall(reactor: Reactor, isFederate: Boolean): String =
if (reactor.isMain) {
if (isFederate) {
return with(PrependOperator) {
"""
"""
| var federateConfig = defaultFederateConfig;
| if (__timeout !== undefined) {
| federateConfig.executionTimeout = __timeout;
Expand All @@ -65,37 +62,26 @@ class TSConstructorGenerator (
| federateConfig.keepAlive = __keepAlive;
| super(federateConfig, success, fail);
""".trimMargin()
}
} else {
return "super(timeout, keepAlive, fast, success, fail);"
"super(timeout, keepAlive, fast, success, fail);"
}
} else {
"super(parent);"
}
else {
return "super(parent);"
}
}

// If the app is federated, register its
// networkMessageActions with the RTIClient
private fun generateFederatePortActionRegistrations(networkMessageActions: List<String>): String {
val connectionInstantiations = LinkedList<String>()
for ((fedPortID, actionName) in networkMessageActions.withIndex()) {
val registration = """
this.registerFederatePortAction(${fedPortID}, this.${actionName});
"""
connectionInstantiations.add(registration)
private fun generateFederatePortActionRegistrations(networkMessageActions: List<String>): String =
networkMessageActions.withIndex().joinWithLn { (fedPortID, actionName) ->
"this.registerFederatePortAction($fedPortID, this.$actionName);"
}
return connectionInstantiations.joinToString("\n")
}

// Generate code for setting target configurations.
private fun generateTargetConfigurations(targetConfig: TargetConfig): String {
val targetConfigurations = LinkedList<String>()
if ((reactor.isMain) &&
targetConfig.coordinationOptions.advance_message_interval != null) {
targetConfigurations.add("this.setAdvanceMessageInterval(${targetConfig.coordinationOptions.advance_message_interval.toTsTime()})")
}
return targetConfigurations.joinToString("\n")
val interval = targetConfig.coordinationOptions.advance_message_interval
return if ((reactor.isMain) && interval != null) {
"this.setAdvanceMessageInterval(${interval.toTsTime()})"
} else ""
}

fun generateConstructor(
Expand Down
2 changes: 1 addition & 1 deletion org.lflang/src/org/lflang/generator/ts/TSGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class TSGenerator(
dockerGenerator: TSDockerGenerator,
preambles: List<Preamble>
) {
var tsFileName = fileConfig.name
val tsFileName = fileConfig.name

val tsFilePath = tsFileConfig.tsSrcGenPath().resolve("$tsFileName.ts")

Expand Down
66 changes: 28 additions & 38 deletions org.lflang/src/org/lflang/generator/ts/TSReactorGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ class TSReactorGenerator(
) {

companion object {
const val MIN_OUTPUT_DELAY_STATEMENT = """
| if (defaultFederateConfig.minOutputDelay !== undefined) {
| __app.setMinDelayFromPhysicalActionToFederateOutput(defaultFederateConfig.minOutputDelay);
| }
|"""
const val MIN_OUTPUT_DELAY_STATEMENT =
"""
if (defaultFederateConfig.minOutputDelay !== undefined) {
__app.setMinDelayFromPhysicalActionToFederateOutput(defaultFederateConfig.minOutputDelay);
}
"""
}

// Initializer functions
Expand Down Expand Up @@ -58,25 +59,18 @@ class TSReactorGenerator(
// assignment variable ("__CL" + the parameter's name). That variable will
// be undefined if the command line argument wasn't specified. Otherwise
// use undefined in the constructor.
val mainReactorParams = StringJoiner(", ")
for (parameter in defn.reactorClass.toDefinition().parameters) {

if (mainParameters.contains(parameter)) {
mainReactorParams.add("__CL" + parameter.name)
} else {
mainReactorParams.add("undefined")
}
val mainReactorParams = defn.reactorClass.toDefinition().parameters.joinWithCommas { p ->
if (p in mainParameters) "__CL" + p.name
else "undefined"
}

return with(PrependOperator) {
"""
|// ************* Instance $fullName of class ${defn.reactorClass.name}
|let __app;
|if (!__noStart) {
| __app = new $fullName(__timeout, __keepAlive, __fast, __federationID, $mainReactorParams);
|}
"""
}.trimMargin()
return """
|// ************* Instance $fullName of class ${defn.reactorClass.name}
|let __app;
|if (!__noStart) {
| __app = new $fullName(__timeout, __keepAlive, __fast, __federationID, $mainReactorParams);
|}
""".trimMargin()
}

/** Generate code to call the _start function on the main App
Expand All @@ -89,27 +83,23 @@ class TSReactorGenerator(
"""
|// ************* Starting Runtime for ${defn.name} + of class ${defn.reactorClass.name}.
|if (!__noStart && __app) {
${if (isFederate) MIN_OUTPUT_DELAY_STATEMENT else "|"}
${" |"..MIN_OUTPUT_DELAY_STATEMENT.takeIf { isFederate }.orEmpty()}
| __app._start();
|}
|
"""
}.trimMargin()
}.trimMargin()
}

private fun generateReactorPreambles(preambles: List<Preamble>): String {
val preambleCodes = LinkedList<String>()

for (preamble in preambles) {
preambleCodes.add(with(PrependOperator) {
private fun generateReactorPreambles(preambles: List<Preamble>): String =
preambles.joinToString("\n") { preamble ->
with(PrependOperator) {
"""
|// *********** From the preamble, verbatim:
|${preamble.code.toText()}
|
|// *********** End of preamble."""}.trimMargin())
|// *********** From the preamble, verbatim:
${" |"..preamble.code.toText()}
|// *********** End of preamble."""
}.trimMargin()
}
return preambleCodes.joinToString("\n")
}

fun generateReactor(reactor: Reactor): String {
var reactorName = reactor.name
Expand Down Expand Up @@ -150,7 +140,7 @@ class TSReactorGenerator(
val actionGenerator = TSActionGenerator(reactor.actions, networkMessageActions)
val portGenerator = TSPortGenerator(reactor.inputs, reactor.outputs)

val constructorGenerator = TSConstructorGenerator(tsGenerator, errorReporter, reactor)
val constructorGenerator = TSConstructorGenerator(errorReporter, reactor)
return with(PrependOperator) {
"""
|// =============== START reactor class ${reactor.name}
Expand Down Expand Up @@ -178,8 +168,8 @@ class TSReactorGenerator(
): String {
return with(PrependOperator) {
"""
|${generateMainReactorInstance(mainDef, mainParameters)}
|${generateRuntimeStart(mainDef)}
${" |"..generateMainReactorInstance(mainDef, mainParameters)}
${" |"..generateRuntimeStart(mainDef)}
|
"""
}.trimMargin()
Expand Down

0 comments on commit 71ef8d0

Please sign in to comment.