diff --git a/org.lflang.tests/src/org/lflang/tests/compiler/LetInferenceTests.java b/org.lflang.tests/src/org/lflang/tests/compiler/LetInferenceTests.java index 60cfa39725..3e40927de0 100644 --- a/org.lflang.tests/src/org/lflang/tests/compiler/LetInferenceTests.java +++ b/org.lflang.tests/src/org/lflang/tests/compiler/LetInferenceTests.java @@ -115,10 +115,9 @@ public void testLet() throws Exception { TreeIterator it = model.eResource().getAllContents(); while (it.hasNext()) { EObject obj = it.next(); - if (!(obj instanceof Reactor)) { + if (!(obj instanceof Reactor reactor)) { continue; } - Reactor reactor = (Reactor) obj; if (reactor.isMain()) { mainDef = LfFactory.eINSTANCE.createInstantiation(); mainDef.setName(reactor.getName()); diff --git a/org.lflang/src/lib/c/reactor-c b/org.lflang/src/lib/c/reactor-c index 158be58c62..d43e973780 160000 --- a/org.lflang/src/lib/c/reactor-c +++ b/org.lflang/src/lib/c/reactor-c @@ -1 +1 @@ -Subproject commit 158be58c625ce8378be9e3a5a5f6414cab946dfe +Subproject commit d43e9737804f2d984d52a99cac20d8e57adad543 diff --git a/org.lflang/src/org/lflang/federated/generator/FedGenerator.java b/org.lflang/src/org/lflang/federated/generator/FedGenerator.java index a96c1d17a2..9a98e90291 100644 --- a/org.lflang/src/org/lflang/federated/generator/FedGenerator.java +++ b/org.lflang/src/org/lflang/federated/generator/FedGenerator.java @@ -26,6 +26,7 @@ import org.eclipse.xtext.generator.JavaIoFileSystemAccess; import org.eclipse.xtext.resource.XtextResource; import org.eclipse.xtext.resource.XtextResourceSet; +import org.eclipse.xtext.util.RuntimeIOException; import org.eclipse.xtext.xbase.lib.CollectionLiterals; import org.eclipse.xtext.xbase.lib.Exceptions; import org.eclipse.xtext.xbase.lib.Pair; @@ -176,30 +177,29 @@ public boolean doGenerate(Resource resource, LFGeneratorContext context) throws return false; } - Map codeMapMap = compileFederates(context, lf2lfCodeMapMap, (subContexts) -> { + Map codeMapMap = compileFederates(context, lf2lfCodeMapMap, subContexts -> { if (context.getTargetConfig().dockerOptions == null) return; - final List services = new ArrayList(); + final List services = new ArrayList<>(); // 1. create a Dockerfile for each federate - subContexts.forEach((subContext) -> { - // Inherit Docker options from main context + for (SubContext subContext : subContexts) {// Inherit Docker options from main context subContext.getTargetConfig().dockerOptions = context.getTargetConfig().dockerOptions; var dockerGenerator = dockerGeneratorFactory(subContext); var dockerData = dockerGenerator.generateDockerData(); try { dockerData.writeDockerFile(); } catch (IOException e) { - Exceptions.sneakyThrow(e); + throw new RuntimeIOException(e); } services.add(dockerData); - }); + } // 2. create a docker-compose.yml for the federation try { // FIXME: https://issue.lf-lang.org/1559 // It appears that the rtiHost information should come from federationRTIproperties, // which is a kludge and not in scope here. - (new FedDockerComposeGenerator(context, "localhost")).writeDockerComposeFile(services); + new FedDockerComposeGenerator(context, "localhost").writeDockerComposeFile(services); } catch (IOException e) { - Exceptions.sneakyThrow(e); + throw new RuntimeIOException(e); } }); diff --git a/org.lflang/src/org/lflang/generator/GeneratorBase.java b/org.lflang/src/org/lflang/generator/GeneratorBase.java index 0a9c2064d7..ef35d22be1 100644 --- a/org.lflang/src/org/lflang/generator/GeneratorBase.java +++ b/org.lflang/src/org/lflang/generator/GeneratorBase.java @@ -175,7 +175,7 @@ public abstract class GeneratorBase extends AbstractLFValidator { /** * A list ot AST transformations to apply before code generation */ - private List astTransformations = new ArrayList(); + private final List astTransformations = new ArrayList<>(); /** * Create a new GeneratorBase object. diff --git a/org.lflang/src/org/lflang/generator/GeneratorUtils.java b/org.lflang/src/org/lflang/generator/GeneratorUtils.java index ec6e167ef2..32ad03e08b 100644 --- a/org.lflang/src/org/lflang/generator/GeneratorUtils.java +++ b/org.lflang/src/org/lflang/generator/GeneratorUtils.java @@ -20,6 +20,7 @@ import org.eclipse.xtext.validation.CheckMode; import org.eclipse.xtext.validation.IResourceValidator; import org.eclipse.xtext.validation.Issue; +import org.eclipse.xtext.xbase.lib.IteratorExtensions; import org.lflang.ASTUtils; import org.lflang.ErrorReporter; @@ -45,6 +46,7 @@ import org.lflang.lf.Reactor; import org.lflang.lf.TargetDecl; import org.lflang.util.FileUtil; +import org.lflang.util.IteratorUtil; /** * A helper class with functions that may be useful for code @@ -188,30 +190,7 @@ public static void accommodatePhysicalActionsIfPresent( * {@code resource} */ public static Iterable findAll(Resource resource, Class nodeType) { - Iterator contents = resource.getAllContents(); - assert contents != null : "Although getAllContents is not marked as NotNull, it should be."; - EObject temp = null; - while (!nodeType.isInstance(temp) && contents.hasNext()) temp = contents.next(); - EObject next_ = temp; - return () -> new Iterator<>() { - EObject next = next_; - - @Override - public boolean hasNext() { - return nodeType.isInstance(next); - } - - @Override - public T next() { - // This cast is safe if hasNext() holds. - assert hasNext() : "next() was called on an Iterator when hasNext() was false."; - //noinspection unchecked - T current = (T) next; - next = null; - while (!nodeType.isInstance(next) && contents.hasNext()) next = contents.next(); - return current; - } - }; + return () -> IteratorExtensions.filter(resource.getAllContents(), nodeType); } /** diff --git a/org.lflang/src/org/lflang/generator/ReactorInstance.java b/org.lflang/src/org/lflang/generator/ReactorInstance.java index 305ef420c4..c7f59211f7 100644 --- a/org.lflang/src/org/lflang/generator/ReactorInstance.java +++ b/org.lflang/src/org/lflang/generator/ReactorInstance.java @@ -746,12 +746,8 @@ protected void createReactionInstances() { /** * Returns the built-in trigger or create a new one if none exists. */ - protected TriggerInstance getOrCreateBuiltinTrigger(BuiltinTriggerRef trigger) { - if (!builtinTriggers.containsKey(trigger.getType())) { - builtinTriggers.put(trigger.getType(), - new TriggerInstance<>(trigger.getType(), trigger, this)); - } - return builtinTriggers.get(trigger.getType()); + protected TriggerInstance getOrCreateBuiltinTrigger(BuiltinTriggerRef trigger) { + return builtinTriggers.computeIfAbsent(trigger.getType(), ref -> TriggerInstance.builtinTrigger(trigger, this)); } //////////////////////////////////////// diff --git a/org.lflang/src/org/lflang/generator/TriggerInstance.java b/org.lflang/src/org/lflang/generator/TriggerInstance.java index e0342ff853..47212ff0e8 100644 --- a/org.lflang/src/org/lflang/generator/TriggerInstance.java +++ b/org.lflang/src/org/lflang/generator/TriggerInstance.java @@ -30,18 +30,19 @@ STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY import java.util.Set; import org.lflang.lf.BuiltinTrigger; +import org.lflang.lf.BuiltinTriggerRef; import org.lflang.lf.TriggerRef; import org.lflang.lf.Variable; import org.lflang.lf.impl.VariableImpl; /** Instance of a trigger (port, action, or timer). - * + * * @author Marten Lohstroh * @author Edward A. Lee * @author Alexander Schulz-Rosengarten */ public class TriggerInstance extends NamedInstance { - + /** Construct a new instance with the specified definition * and parent. E.g., for a action instance, the definition * is Action, and for a port instance, it is Port. These are @@ -53,33 +54,21 @@ public class TriggerInstance extends NamedInstance { protected TriggerInstance(T definition, ReactorInstance parent) { super(definition, parent); } - + /** * Construct a new instance for a special builtin trigger. - * This constructor must be used with Variable or BuiltinTriggerVariable - * as generic type T. - * - * @param type The builtin trigger type. - * @param type The actual trigger definition. + * + * @param trigger The actual trigger definition. * @param parent The reactor instance that creates this instance. */ - public TriggerInstance(BuiltinTrigger type, TriggerRef trigger, ReactorInstance parent) { - super((T)(new BuiltinTriggerVariable(type, trigger)), parent); - builtinTriggerType = type; + static TriggerInstance builtinTrigger(BuiltinTriggerRef trigger, ReactorInstance parent) { + return new TriggerInstance<>(new BuiltinTriggerVariable(trigger), parent); } - + ///////////////////////////////////////////// //// Public Methods /** - * Return the built-in trigger type or null if this is not a - * built-in trigger. - */ - public BuiltinTrigger getBuiltinTriggerType() { - return builtinTriggerType; - } - - /** * Return the reaction instances that are triggered or read by this trigger. * If this port is an output, then the reaction instances * belong to the parent of the port's parent. If the port @@ -90,18 +79,18 @@ public Set getDependentReactions() { return dependentReactions; } - /** + /** * Return the reaction instances that may send data via this port. * If this port is an input, then the reaction instance * belongs to parent of the port's parent. If it is an output, - * the the reaction instance belongs to the port's parent. + * the reaction instance belongs to the port's parent. */ public Set getDependsOnReactions() { return dependsOnReactions; - }; - - /** - * Return the name of this trigger. + } + + /** + * Return the name of this trigger. * @return The name of this trigger. */ @Override @@ -113,52 +102,52 @@ public String getName() { * Return true if this trigger is "shutdown". */ public boolean isShutdown() { - return builtinTriggerType == BuiltinTrigger.SHUTDOWN; + return isBuiltInType(BuiltinTrigger.SHUTDOWN); } /** * Return true if this trigger is "startup"./ */ public boolean isStartup() { - return builtinTriggerType == BuiltinTrigger.STARTUP; + return isBuiltInType(BuiltinTrigger.STARTUP); } /** * Return true if this trigger is "startup"./ */ public boolean isReset() { - return builtinTriggerType == BuiltinTrigger.RESET; + return isBuiltInType(BuiltinTrigger.RESET); } - /** - * Return true if this trigger is a builtin one. - */ - public boolean isBuiltinTrigger() { - return builtinTriggerType != null; + ///////////////////////////////////////////// + //// Private Methods + + private boolean isBuiltInType(BuiltinTrigger type) { + return this.definition instanceof BuiltinTriggerVariable + && ((BuiltinTriggerRef) ((BuiltinTriggerVariable) this.definition).definition).getType().equals(type); } ///////////////////////////////////////////// //// Protected Fields - - BuiltinTrigger builtinTriggerType = null; - - /** + + + /** * Reaction instances that are triggered or read by this trigger. * If this port is an output, then the reaction instances * belong to the parent of the port's parent. If the port * is an input, then the reaction instances belong to the * port's parent. */ - Set dependentReactions = new LinkedHashSet(); + Set dependentReactions = new LinkedHashSet<>(); - /** + /** * Reaction instances that may send data via this port. * If this port is an input, then the reaction instance * belongs to parent of the port's parent. If it is an output, - * the the reaction instance belongs to the port's parent. + * the reaction instance belongs to the port's parent. */ - Set dependsOnReactions = new LinkedHashSet(); - + Set dependsOnReactions = new LinkedHashSet<>(); + ///////////////////////////////////////////// //// Special class for builtin triggers @@ -166,23 +155,23 @@ public boolean isBuiltinTrigger() { * This class allows to have BuiltinTriggers represented by a Variable type. */ static public class BuiltinTriggerVariable extends VariableImpl { - + /** The builtin trigger type represented by this variable. */ public final BuiltinTrigger type; - + /** The actual TriggerRef definition in the AST. */ public final TriggerRef definition; - - public BuiltinTriggerVariable(BuiltinTrigger type, TriggerRef trigger) { - this.type = type; + + public BuiltinTriggerVariable(BuiltinTriggerRef trigger) { + this.type = trigger.getType(); this.definition = trigger; } - + @Override public String getName() { return this.type.name().toLowerCase(); } - + @Override public void setName(String newName) { throw new UnsupportedOperationException(