Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rendering of '0' time in diagram synthesis #811

Merged
merged 8 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -428,11 +428,14 @@ class LinguaFrancaShapeExtensions extends AbstractSynthesisExtensions {
).boldLineSelectionStyle

val labelParts = newArrayList
if (timer.offset !== TimerInstance.DEFAULT_OFFSET) {
labelParts += timer.offset.toString
if (timer.offset !== TimerInstance.DEFAULT_OFFSET && timer.offset !== null) {
labelParts += timer.offset.toString
}
if (timer.period !== TimerInstance.DEFAULT_PERIOD) {
labelParts += timer.period.toString
if (timer.period !== TimerInstance.DEFAULT_PERIOD && timer.period !== null) {
if (timer.offset === TimerInstance.DEFAULT_OFFSET) {
labelParts += timer.offset.toString
}
labelParts += timer.period.toString
}
if (!labelParts.empty) {
node.addOutsideBottomCenteredNodeLabel(labelParts.join("(", ", ", ")")[it], 8)
Expand Down
4 changes: 3 additions & 1 deletion org.lflang/src/org/lflang/ASTUtils.xtend
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,9 @@ class ASTUtils {
* @param t The time to be converted
* @return A textual representation
*/
def static String toText(Time t) '''«t.interval» «t.unit.toString»'''
def static String toText(Time t) {
return t.toTimeValue.toString
}

/**
* Convert a value to its textual representation as it would
Expand Down
12 changes: 6 additions & 6 deletions org.lflang/src/org/lflang/JavaAstUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,14 @@ public static String addZeroToLeadingDot(String literal) {
}

/**
* Assuming that the given value denotes a valid time,
* Assuming that the given value denotes a valid time literal,
* return a time value.
*/
public static TimeValue getTimeValue(Value v) {
if (v.getParameter() != null) {
return getDefaultAsTimeValue(v.getParameter());
} else if (v.getTime() != null) {
public static TimeValue getLiteralTimeValue(Value v) {;
if (v.getTime() != null) {
return toTimeValue(v.getTime());
} else if (v.getLiteral() != null && v.getLiteral().equals("0")) {
return TimeValue.ZERO;
} else {
return null;
}
Expand All @@ -200,7 +200,7 @@ public static TimeValue getDefaultAsTimeValue(Parameter p) {
if (isOfTimeType(p)) {
var init = p.getInit().get(0);
if (init != null) {
return getTimeValue(init);
return getLiteralTimeValue(init);
}
}
return null;
Expand Down
4 changes: 2 additions & 2 deletions org.lflang/src/org/lflang/ModelInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private void collectOverflowingNodes() {
// Visit all deadlines in the model; detect possible overflow.
for (var deadline : filter(toIterable(model.eAllContents()), Deadline.class)) {
// If the time value overflows, mark this deadline as overflowing.
if (isTooLarge(JavaAstUtils.getTimeValue(deadline.getDelay()))) {
if (isTooLarge(JavaAstUtils.getLiteralTimeValue(deadline.getDelay()))) {
this.overflowingDeadlines.add(deadline);
}

Expand Down Expand Up @@ -207,7 +207,7 @@ private boolean detectOverflow(Set<Instantiation> visited, Parameter current) {
} else {
// The right-hand side of the assignment is a
// constant; check whether it is too large.
if (isTooLarge(JavaAstUtils.getTimeValue(assignment.getRhs().get(0)))) {
if (isTooLarge(JavaAstUtils.getLiteralTimeValue(assignment.getRhs().get(0)))) {
this.overflowingAssignments.add(assignment);
overflow = true;
}
Expand Down
14 changes: 6 additions & 8 deletions org.lflang/src/org/lflang/federated/CGeneratorExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.lflang.generator.ReactorInstance;
import org.lflang.lf.Delay;
import org.lflang.lf.Input;
import org.lflang.lf.Parameter;
import org.lflang.lf.Port;
import org.lflang.lf.Reactor;
import org.lflang.lf.ReactorDecl;
Expand Down Expand Up @@ -232,19 +233,16 @@ public static String createPortStatusFieldForInput(Input input,
public static String getNetworkDelayLiteral(Delay delay, CGenerator generator) {
String additionalDelayString = "NEVER";
if (delay != null) {
Parameter p = delay.getParameter();
TimeValue tv;
if (delay.getParameter() != null) {
// The parameter has to be parameter of the main reactor.
// And that value has to be a Time.
Value value = delay.getParameter().getInit().get(0);
if (value.getTime() != null) {
additionalDelayString = Long.toString(JavaAstUtils.getTimeValue(value).toNanoSeconds());
} else if (value.getLiteral() != null) {
// If no units are given, e.g. "0", then use the literal.
additionalDelayString = value.getLiteral();
}
tv = JavaAstUtils.getDefaultAsTimeValue(p);
} else {
additionalDelayString = Long.toString(JavaAstUtils.toTimeValue(delay.getTime()).toNanoSeconds());
tv = JavaAstUtils.toTimeValue(delay.getTime());
}
additionalDelayString = Long.toString(tv.toNanoSeconds());
}
return additionalDelayString;
}
Expand Down
2 changes: 1 addition & 1 deletion org.lflang/src/org/lflang/federated/FedASTUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ private static TimeValue findMaxSTP(Variable port,
}

return STPList.stream()
.map(JavaAstUtils::getTimeValue)
.map(JavaAstUtils::getLiteralTimeValue)
.filter(Objects::nonNull)
.reduce(TimeValue.ZERO, TimeValue::max);
}
Expand Down
16 changes: 2 additions & 14 deletions org.lflang/src/org/lflang/generator/ActionInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,10 @@ public ActionInstance(Action definition, ReactorInstance parent) {
}
if (definition != null) {
if (definition.getMinDelay() != null) {
Parameter parm = definition.getMinDelay().getParameter();
if (parm != null) {
this.minDelay = JavaAstUtils.getTimeValue(
parent.lookupParameterInstance(parm).init.get(0));
} else {
this.minDelay = JavaAstUtils.getTimeValue(definition.getMinDelay());
}
this.minDelay = parent.getTimeValue(definition.getMinDelay());
}
if (definition.getMinSpacing() != null) {
Parameter parm = definition.getMinSpacing().getParameter();
if (parm != null) {
this.minSpacing = JavaAstUtils.getTimeValue(
parent.lookupParameterInstance(parm).init.get(0));
} else {
this.minSpacing = JavaAstUtils.getTimeValue(definition.getMinSpacing());
}
this.minSpacing = parent.getTimeValue(definition.getMinSpacing());
}
if (definition.getOrigin() == ActionOrigin.PHYSICAL) {
physical = true;
Expand Down
7 changes: 1 addition & 6 deletions org.lflang/src/org/lflang/generator/DeadlineInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ public class DeadlineInstance {
*/
public DeadlineInstance(Deadline definition, ReactionInstance reaction) {
if (definition.getDelay() != null) {
Parameter parm = definition.getDelay().getParameter();
if (parm != null) {
this.maxDelay = JavaAstUtils.getTimeValue(reaction.parent.initialParameterValue(parm).get(0));
} else {
this.maxDelay = JavaAstUtils.getTimeValue(definition.getDelay());
}
this.maxDelay = reaction.parent.getTimeValue(definition.getDelay());
} else {
this.maxDelay = TimeValue.ZERO;
}
Expand Down
33 changes: 33 additions & 0 deletions org.lflang/src/org/lflang/generator/ReactorInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY

import org.lflang.ASTUtils;
import org.lflang.ErrorReporter;
import org.lflang.JavaAstUtils;
import org.lflang.TimeValue;
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.Input;
import org.lflang.lf.Instantiation;
import org.lflang.lf.Output;
Expand Down Expand Up @@ -673,6 +676,36 @@ public int width(WidthSpec widthSpec) {
}
return ASTUtils.width(widthSpec, instantiations());
}

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

/**
* 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 JavaAstUtils.getLiteralTimeValue(lookupParameterInstance(p).init.get(0));
} else {
return JavaAstUtils.toTimeValue(d.getTime());
}
}

//////////////////////////////////////////////////////
//// Protected fields.
Expand Down
14 changes: 2 additions & 12 deletions org.lflang/src/org/lflang/generator/TimerInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,10 @@ public TimerInstance(Timer definition, ReactorInstance parent) {
}
if (definition != null) {
if (definition.getOffset() != null) {
if (definition.getOffset().getParameter() != null) {
Parameter parm = definition.getOffset().getParameter();
this.offset = JavaAstUtils.getTimeValue(parent.initialParameterValue(parm).get(0));
} else {
this.offset = JavaAstUtils.getTimeValue(definition.getOffset());
}
this.offset = parent.getTimeValue(definition.getOffset());
}
if (definition.getPeriod() != null) {
if (definition.getPeriod().getParameter() != null) {
Parameter parm = definition.getPeriod().getParameter();
this.period = JavaAstUtils.getTimeValue(parent.initialParameterValue(parm).get(0));
} else {
this.period = JavaAstUtils.getTimeValue(definition.getPeriod());
}
this.period = parent.getTimeValue(definition.getPeriod());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion org.lflang/src/org/lflang/generator/c/CGenerator.xtend
Original file line number Diff line number Diff line change
Expand Up @@ -1528,7 +1528,7 @@ class CGenerator extends GeneratorBase {
val reactorInstance = main.getChildReactorInstance(federate.instantiation)
for (param : reactorInstance.parameters) {
if (param.name.equalsIgnoreCase("STP_offset") && param.type.isTime) {
val stp = param.init.get(0).getTimeValue
val stp = param.init.get(0).getLiteralTimeValue
if (stp !== null) {
pr('''
set_stp_offset(«stp.timeInTargetLanguage»);
Expand Down
9 changes: 2 additions & 7 deletions org.lflang/src/org/lflang/validation/LFValidator.xtend
Original file line number Diff line number Diff line change
Expand Up @@ -1260,13 +1260,8 @@ class LFValidator extends BaseLFValidator {
error("Invalid time literal.",
Literals.VALUE__LITERAL)
}
} else if (value.code !== null && !value.code.isZero) {
if (value.code.isInteger) {
error("Missing time unit", Literals.VALUE__CODE)
} else {
error("Invalid time literal.",
Literals.VALUE__CODE)
}
} else if (value.code !== null) {
error("Invalid time literal.", Literals.VALUE__CODE)
}
}
}
Expand Down