Skip to content

Commit

Permalink
remove all uses of Delay from the code base
Browse files Browse the repository at this point in the history
  • Loading branch information
cmnrd committed Mar 23, 2022
1 parent 647782c commit a5a073b
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 96 deletions.
19 changes: 7 additions & 12 deletions org.lflang/src/org/lflang/ASTUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
import org.lflang.lf.Assignment;
import org.lflang.lf.Code;
import org.lflang.lf.Connection;
import org.lflang.lf.Delay;
import org.lflang.lf.Element;
import org.lflang.lf.Expression;
import org.lflang.lf.ImportedReactor;
Expand Down Expand Up @@ -414,7 +413,7 @@ private static List<Connection> rerouteViaDelay(Connection connection,
*/
private static Instantiation getDelayInstance(Reactor delayClass,
Connection connection, String generic, Boolean defineWidthFromConnection) {
Delay delay = connection.getDelay();
Expression delay = connection.getDelay();
Instantiation delayInstance = factory.createInstantiation();
delayInstance.setReactorClass(delayClass);
if (!StringExtensions.isNullOrEmpty(generic)) {
Expand All @@ -441,12 +440,15 @@ private static Instantiation getDelayInstance(Reactor delayClass,
}
Assignment assignment = factory.createAssignment();
assignment.setLhs(delayClass.getParameters().get(0));
if (delay.getParameter() != null) {
if (delay instanceof ParameterReference) {
var expr = factory.createParameterReference();
expr.setParameter(delay.getParameter());
expr.setParameter(((ParameterReference)delay).getParameter());
assignment.getRhs().add(expr);
} else if (delay instanceof Time){
assignment.getRhs().add(delay);
} else {
assignment.getRhs().add(delay.getTime());
// the validator ensures that a delay is only a Time or a ParameterReference
throw new RuntimeException("Unexpected expression type");
}
delayInstance.getParameters().add(assignment);
delayInstance.setName("delay"); // This has to be overridden.
Expand Down Expand Up @@ -916,13 +918,6 @@ public static String toText(Expression expr) {
}
}

public static String toText(Delay d) {
if (d.getParameter() != null) {
return d.getParameter().getName();
}
return toText(d.getTime());
}

/**
* Return a string of the form either "name" or "container.name" depending
* on in which form the variable reference was given.
Expand Down
3 changes: 0 additions & 3 deletions org.lflang/src/org/lflang/AstExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,6 @@ fun TypeParm.toText(): String =
fun Element.toText(): String =
literal?.withoutQuotes()?.trim() ?: id ?: ""


fun Delay.toText(): String = ASTUtils.toText(this)

fun Time.toTimeValue(): TimeValue = TimeValue(interval.toLong(), TimeUnit.fromName(this.unit))


Expand Down
12 changes: 6 additions & 6 deletions org.lflang/src/org/lflang/federated/CGeneratorExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
import org.lflang.generator.c.CGenerator;
import org.lflang.generator.c.CUtil;
import org.lflang.lf.Action;
import org.lflang.lf.Delay;
import org.lflang.lf.Expression;
import org.lflang.lf.Input;
import org.lflang.lf.Parameter;
import org.lflang.lf.ParameterReference;
import org.lflang.lf.Reactor;
import org.lflang.lf.ReactorDecl;
import org.lflang.lf.VarRef;
Expand Down Expand Up @@ -230,17 +231,16 @@ public static String createPortStatusFieldForInput(Input input) {
* @param generator
* @return
*/
public static String getNetworkDelayLiteral(Delay delay) {
public static String getNetworkDelayLiteral(Expression delay) {
String additionalDelayString = "NEVER";
if (delay != null) {
Parameter p = delay.getParameter();
TimeValue tv;
if (delay.getParameter() != null) {
if (delay instanceof ParameterReference) {
// The parameter has to be parameter of the main reactor.
// And that value has to be a Time.
tv = ASTUtils.getDefaultAsTimeValue(p);
tv = ASTUtils.getDefaultAsTimeValue(((ParameterReference)delay).getParameter());
} else {
tv = ASTUtils.toTimeValue(delay.getTime());
tv = ASTUtils.getLiteralTimeValue(delay);
}
additionalDelayString = Long.toString(tv.toNanoSeconds());
}
Expand Down
5 changes: 2 additions & 3 deletions org.lflang/src/org/lflang/federated/FedASTUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import org.lflang.lf.Action;
import org.lflang.lf.ActionOrigin;
import org.lflang.lf.Connection;
import org.lflang.lf.Delay;
import org.lflang.lf.Expression;
import org.lflang.lf.Instantiation;
import org.lflang.lf.LfFactory;
Expand Down Expand Up @@ -125,7 +124,7 @@ private static Action createNetworkAction(
// provided using after is enforced by setting
// the minDelay.
if (connection.getDelay() != null) {
action.setMinDelay(connection.getDelay().getTime());
action.setMinDelay(connection.getDelay());
}
} else {
action.setOrigin(ActionOrigin.LOGICAL);
Expand Down Expand Up @@ -580,7 +579,7 @@ private static void addNetworkOutputControlReaction(
int channelIndex,
int receivingFedID,
GeneratorBase generator,
Delay delay
Expression delay
) {
LfFactory factory = LfFactory.eINSTANCE;
Reaction reaction = factory.createReaction();
Expand Down
8 changes: 4 additions & 4 deletions org.lflang/src/org/lflang/federated/FederateInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
import org.lflang.generator.TriggerInstance;
import org.lflang.lf.Action;
import org.lflang.lf.ActionOrigin;
import org.lflang.lf.Delay;
import org.lflang.lf.Expression;
import org.lflang.lf.Input;
import org.lflang.lf.Instantiation;
import org.lflang.lf.Output;
Expand Down Expand Up @@ -119,7 +119,7 @@ public FederateInstance(
/**
* A list of outputs that can be triggered directly or indirectly by physical actions.
*/
public Set<Delay> outputsConnectedToPhysicalActions = new LinkedHashSet<>();
public Set<Expression> outputsConnectedToPhysicalActions = new LinkedHashSet<>();

/**
* The host, if specified using the 'at' keyword.
Expand All @@ -137,7 +137,7 @@ public FederateInstance(
* may may include null, meaning that there is a connection
* from the federate instance that has no delay.
*/
public Map<FederateInstance, Set<Delay>> dependsOn = new LinkedHashMap<>();
public Map<FederateInstance, Set<Expression>> dependsOn = new LinkedHashMap<>();

/**
* The directory, if specified using the 'at' keyword.
Expand All @@ -155,7 +155,7 @@ public FederateInstance(
* may may include null, meaning that there is a connection
* from the federate instance that has no delay.
*/
public Map<FederateInstance, Set<Delay>> sendsTo = new LinkedHashMap<>();
public Map<FederateInstance, Set<Expression>> sendsTo = new LinkedHashMap<>();

/**
* The user, if specified using the 'at' keyword.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.lflang.federated.serialization.SupportedSerializers;
import org.lflang.generator.c.CUtil;
import org.lflang.lf.Action;
import org.lflang.lf.Delay;
import org.lflang.lf.Expression;
import org.lflang.lf.VarRef;

/**
Expand Down Expand Up @@ -71,7 +71,7 @@ public static String generateNetworkSenderBody(
FederateInstance receivingFed,
InferredType type,
boolean isPhysical,
Delay delay,
Expression delay,
SupportedSerializers serializer,
CoordinationType coordinationType
) {
Expand Down
11 changes: 0 additions & 11 deletions org.lflang/src/org/lflang/generator/ExpressionGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.lflang.ASTUtils;
import org.lflang.TimeValue;
import org.lflang.lf.Assignment;
import org.lflang.lf.Delay;
import org.lflang.lf.Expression;
import org.lflang.lf.Instantiation;
import org.lflang.lf.Parameter;
Expand Down Expand Up @@ -130,16 +129,6 @@ public String getTargetTime(Time t) {
return timeInTargetLanguage.apply(new TimeValue(t.getInterval(), TimeUnit.fromName(t.getUnit())));
}

/**
* Return the time specified by {@code d}, expressed as
* code that is valid for some target languages.
*/
public String getTargetTime(Delay d) {
return d.getParameter() != null ? ASTUtils.toText(d) : timeInTargetLanguage.apply(
ASTUtils.toTimeValue(d.getTime()) // The time is given as a parameter reference.
);
}

/**
* Return the time specified by {@code v}, expressed as
* code that is valid for some target languages.
Expand Down
22 changes: 6 additions & 16 deletions org.lflang/src/org/lflang/generator/GeneratorBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
import org.lflang.graph.InstantiationGraph;
import org.lflang.lf.Action;
import org.lflang.lf.Connection;
import org.lflang.lf.Delay;
import org.lflang.lf.Expression;
import org.lflang.lf.Instantiation;
import org.lflang.lf.LfFactory;
Expand Down Expand Up @@ -715,7 +714,7 @@ public String generateNetworkSenderBody(
FederateInstance receivingFed,
InferredType type,
boolean isPhysical,
Delay delay,
Expression delay,
SupportedSerializers serializer
) {
throw new UnsupportedOperationException("This target does not support network connections between federates.");
Expand Down Expand Up @@ -753,7 +752,7 @@ public String generateNetworkOutputControlReactionBody(
int receivingFederateID,
int sendingBankIndex,
int sendingChannelIndex,
Delay delay
Expression delay
) {
throw new UnsupportedOperationException("This target does not support network connections between federates.");
}
Expand Down Expand Up @@ -1226,10 +1225,10 @@ private void replaceConnectionFromFederate(
&& targetConfig.coordination != CoordinationType.DECENTRALIZED) {
// Map the delays on connections between federates.
// First see if the cache has been created.
Set<Delay> dependsOnDelays = dstFederate.dependsOn.get(srcFederate);
Set<Expression> dependsOnDelays = dstFederate.dependsOn.get(srcFederate);
if (dependsOnDelays == null) {
// If not, create it.
dependsOnDelays = new LinkedHashSet<Delay>();
dependsOnDelays = new LinkedHashSet<Expression>();
dstFederate.dependsOn.put(srcFederate, dependsOnDelays);
}
// Put the delay on the cache.
Expand All @@ -1240,9 +1239,9 @@ private void replaceConnectionFromFederate(
dependsOnDelays.add(null);
}
// Map the connections between federates.
Set<Delay> sendsToDelays = srcFederate.sendsTo.get(dstFederate);
Set<Expression> sendsToDelays = srcFederate.sendsTo.get(dstFederate);
if (sendsToDelays == null) {
sendsToDelays = new LinkedHashSet<Delay>();
sendsToDelays = new LinkedHashSet<Expression>();
srcFederate.sendsTo.put(dstFederate, sendsToDelays);
}
if (connection.getDelay() != null) {
Expand Down Expand Up @@ -1357,13 +1356,4 @@ public static String getTargetTime(Expression expr) {
}
return ASTUtils.toText(expr);
}

// FIXME: this should be placed in ExpressionGenerator
public static String getTargetTime(Delay d) {
if (d.getParameter() != null) {
return ASTUtils.toText(d);
} else {
return getTargetTime(d.getTime());
}
}
}
17 changes: 1 addition & 16 deletions org.lflang/src/org/lflang/generator/ReactorInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
import org.lflang.generator.TriggerInstance.BuiltinTriggerVariable;
import org.lflang.lf.Action;
import org.lflang.lf.Connection;
import org.lflang.lf.Delay;
import org.lflang.lf.Expression;
import org.lflang.lf.Input;
import org.lflang.lf.Instantiation;
Expand All @@ -51,6 +50,7 @@ STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
import org.lflang.lf.Reaction;
import org.lflang.lf.Reactor;
import org.lflang.lf.ReactorDecl;
import org.lflang.lf.Time;
import org.lflang.lf.Timer;
import org.lflang.lf.TriggerRef;
import org.lflang.lf.VarRef;
Expand Down Expand Up @@ -654,21 +654,6 @@ public TimeValue getTimeValue(Expression expr) {
return ASTUtils.getLiteralTimeValue(expr);
}
}

/**
* Assuming that the given delay denotes a valid time, return a time value.
*
* If the delay is given as a parameter reference, this will look up the
* precise time value assigned to this reactor instance.
*/
public TimeValue getTimeValue(Delay d) {
Parameter p = d.getParameter();
if (p != null) {
return ASTUtils.getLiteralTimeValue(lookupParameterInstance(p).getInitialValue().get(0));
} else {
return ASTUtils.toTimeValue(d.getTime());
}
}

//////////////////////////////////////////////////////
//// Protected fields.
Expand Down
16 changes: 6 additions & 10 deletions org.lflang/src/org/lflang/generator/c/CFederateGenerator.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package org.lflang.generator.c;

import java.util.Map;

import org.lflang.ASTUtils;
import org.lflang.TargetConfig;
import org.lflang.TargetProperty.CoordinationType;
import org.lflang.federated.FederateInstance;
import org.lflang.generator.CodeBuilder;
import org.lflang.generator.GeneratorBase;
import org.lflang.generator.ParameterInstance;
import org.lflang.generator.ReactorInstance;
import org.lflang.lf.Delay;
import org.lflang.lf.Expression;
import org.lflang.lf.ParameterReference;

/**
* Generate code for federate related functionality
Expand Down Expand Up @@ -76,15 +71,16 @@ public static String generateFederateNeighborStructure(FederateInstance federate
// There is at least one delay, so find the minimum.
// If there is no delay at all, this is encoded as NEVER.
code.pr("candidate_tmp = FOREVER;");
for (Delay delay : delays) {
for (Expression delay : delays) {
if (delay == null) {
// Use NEVER to encode no delay at all.
code.pr("candidate_tmp = NEVER;");
} else {
var delayTime = GeneratorBase.getTargetTime(delay);
if (delay.getParameter() != null) {
if (delay instanceof ParameterReference) {
// The delay is given as a parameter reference. Find its value.
delayTime = GeneratorBase.timeInTargetLanguage(ASTUtils.getDefaultAsTimeValue(delay.getParameter()));
final var param = ((ParameterReference)delay).getParameter();
delayTime = GeneratorBase.timeInTargetLanguage(ASTUtils.getDefaultAsTimeValue(param));
}
code.pr(String.join("\n",
"if ("+delayTime+" < candidate_tmp) {",
Expand Down
6 changes: 3 additions & 3 deletions org.lflang/src/org/lflang/generator/c/CGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
import org.lflang.generator.c.InteractingContainedReactors;
import org.lflang.lf.Action;
import org.lflang.lf.ActionOrigin;
import org.lflang.lf.Delay;
import org.lflang.lf.Expression;
import org.lflang.lf.Input;
import org.lflang.lf.Instantiation;
import org.lflang.lf.Mode;
Expand Down Expand Up @@ -2476,7 +2476,7 @@ public String generateNetworkSenderBody(
FederateInstance receivingFed,
InferredType type,
boolean isPhysical,
Delay delay,
Expression delay,
SupportedSerializers serializer
) {
return CNetworkGenerator.generateNetworkSenderBody(
Expand Down Expand Up @@ -2537,7 +2537,7 @@ public String generateNetworkOutputControlReactionBody(
int receivingFederateID,
int sendingBankIndex,
int sendingChannelIndex,
Delay delay
Expression delay
) {
return CNetworkGenerator.generateNetworkOutputControlReactionBody(
port,
Expand Down
6 changes: 3 additions & 3 deletions org.lflang/src/org/lflang/generator/c/CNetworkGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import org.lflang.federated.CGeneratorExtension;
import org.lflang.federated.FederateInstance;
import org.lflang.lf.Expression;
import org.lflang.lf.VarRef;
import org.lflang.lf.Action;
import org.lflang.lf.Delay;
import org.lflang.lf.Port;

import java.util.regex.Pattern;
Expand Down Expand Up @@ -162,7 +162,7 @@ public static String generateNetworkSenderBody(
FederateInstance receivingFed,
InferredType type,
boolean isPhysical,
Delay delay,
Expression delay,
SupportedSerializers serializer,
CTypes types,
CoordinationType coordinationType
Expand Down Expand Up @@ -321,7 +321,7 @@ public static String generateNetworkOutputControlReactionBody(
int receivingFederateID,
int sendingBankIndex,
int sendingChannelIndex,
Delay delay
Expression delay
) {
// Store the code
var result = new CodeBuilder();
Expand Down
Loading

0 comments on commit a5a073b

Please sign in to comment.