From 2065f1a0c87acae96e8a8fbd3a4a6a78cc1e41db Mon Sep 17 00:00:00 2001 From: Marten Lohstroh Date: Mon, 6 Jun 2022 16:59:59 -0700 Subject: [PATCH 001/343] Added beginnings of dealing with federated stuff with new design --- org.lflang/src/org/lflang/ASTUtils.java | 15 +++++++++----- .../org/lflang/federated/FooBarGenerator.java | 17 ++++++++++++++++ .../src/org/lflang/generator/LFGenerator.java | 20 +++++++++++++++---- 3 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 org.lflang/src/org/lflang/federated/FooBarGenerator.java diff --git a/org.lflang/src/org/lflang/ASTUtils.java b/org.lflang/src/org/lflang/ASTUtils.java index 0f39e701a5..09369e0dc2 100644 --- a/org.lflang/src/org/lflang/ASTUtils.java +++ b/org.lflang/src/org/lflang/ASTUtils.java @@ -284,7 +284,15 @@ public static Reactor getEnclosingReactor(EObject obj) { } return null; } - + + + public static Reactor findMainReactor(Resource resource) { + return IteratorExtensions.findFirst( + Iterators.filter(resource.getAllContents(), Reactor.class), + Reactor::isMain + ); + } + /** * Find the main reactor and change it to a federated reactor. * Return true if the transformation was successful (or the given resource @@ -292,10 +300,7 @@ public static Reactor getEnclosingReactor(EObject obj) { */ public static boolean makeFederated(Resource resource) { // Find the main reactor - Reactor r = IteratorExtensions.findFirst( - Iterators.filter(resource.getAllContents(), Reactor.class), - Reactor::isMain - ); + Reactor r = findMainReactor(resource); if (r == null) { return false; } diff --git a/org.lflang/src/org/lflang/federated/FooBarGenerator.java b/org.lflang/src/org/lflang/federated/FooBarGenerator.java new file mode 100644 index 0000000000..f4bfce9910 --- /dev/null +++ b/org.lflang/src/org/lflang/federated/FooBarGenerator.java @@ -0,0 +1,17 @@ +package org.lflang.federated; + +import org.eclipse.emf.ecore.resource.Resource; +import org.lflang.ErrorReporter; +import org.lflang.FileConfig; +import org.lflang.generator.LFGeneratorContext; + +public class FooBarGenerator { + + public FooBarGenerator(FileConfig fileConfig, ErrorReporter errorReporter) { + + } + public boolean doGenerate(Resource resource, LFGeneratorContext context) { + System.out.println("Federated program detected."); + return false; + } +} diff --git a/org.lflang/src/org/lflang/generator/LFGenerator.java b/org.lflang/src/org/lflang/generator/LFGenerator.java index ac0c462c84..08b1c4a915 100644 --- a/org.lflang/src/org/lflang/generator/LFGenerator.java +++ b/org.lflang/src/org/lflang/generator/LFGenerator.java @@ -1,5 +1,6 @@ package org.lflang.generator; +import java.io.File; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; @@ -16,8 +17,10 @@ import org.lflang.ErrorReporter; import org.lflang.FileConfig; import org.lflang.Target; +import org.lflang.federated.FooBarGenerator; import org.lflang.generator.c.CGenerator; import org.lflang.generator.python.PythonGenerator; +import org.lflang.lf.Reactor; import org.lflang.scoping.LFGlobalScopeProvider; import com.google.inject.Inject; @@ -148,7 +151,10 @@ public void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) { final LFGeneratorContext lfContext = LFGeneratorContext.lfGeneratorContextOf(context, resource); if (lfContext.getMode() == LFGeneratorContext.Mode.LSP_FAST) return; // The fastest way to generate code is to not generate any code. + final Target target = Target.fromDecl(ASTUtils.targetDecl(resource)); + final Reactor main = ASTUtils.findMainReactor(resource); + assert target != null; FileConfig fileConfig; @@ -158,11 +164,17 @@ public void doGenerate(Resource resource, IFileSystemAccess2 fsa, throw new RuntimeIOException("Error during FileConfig instantiation", e); } final ErrorReporter errorReporter = lfContext.constructErrorReporter(fileConfig); - final GeneratorBase generator = createGenerator(target, fileConfig, errorReporter); - if (generator != null) { - generator.doGenerate(resource, lfContext); - generatorErrorsOccurred = generator.errorsOccurred(); + if (main.isFederated()) { + generatorErrorsOccurred = (new FooBarGenerator(fileConfig, errorReporter)).doGenerate(resource, lfContext); + } else { + + final GeneratorBase generator = createGenerator(target, fileConfig, errorReporter); + + if (generator != null) { + generator.doGenerate(resource, lfContext); + generatorErrorsOccurred = generator.errorsOccurred(); + } } if (errorReporter instanceof LanguageServerErrorReporter) { ((LanguageServerErrorReporter) errorReporter).publishDiagnostics(); From c5e411ea35bf2f9ddba5126abfc1e7667e6effa2 Mon Sep 17 00:00:00 2001 From: Soroush Bateni Date: Mon, 6 Jun 2022 17:33:50 -0700 Subject: [PATCH 002/343] Added a new FedFileConfig. Deprecated the old one. --- .../src/org/lflang/federated/FedASTUtils.java | 19 +++++- .../org/lflang/federated/FedFileConfig.java | 31 +++++----- .../lflang/federated/OldFedFileConfig.java | 60 +++++++++++++++++++ .../federated/launcher/FedCLauncher.java | 5 +- .../federated/launcher/FedLauncher.java | 5 +- .../src/org/lflang/generator/LFGenerator.java | 8 ++- .../org/lflang/generator/c/CGenerator.java | 3 +- .../generator/python/PythonGenerator.java | 3 +- 8 files changed, 108 insertions(+), 26 deletions(-) create mode 100644 org.lflang/src/org/lflang/federated/OldFedFileConfig.java diff --git a/org.lflang/src/org/lflang/federated/FedASTUtils.java b/org.lflang/src/org/lflang/federated/FedASTUtils.java index 0f6738d198..08487bd332 100644 --- a/org.lflang/src/org/lflang/federated/FedASTUtils.java +++ b/org.lflang/src/org/lflang/federated/FedASTUtils.java @@ -35,7 +35,10 @@ import java.util.Optional; import java.util.stream.Collectors; +import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.xtext.xbase.lib.IteratorExtensions; + import org.lflang.ASTUtils; import org.lflang.InferredType; import org.lflang.TargetProperty.CoordinationType; @@ -56,6 +59,8 @@ import org.lflang.lf.VarRef; import org.lflang.lf.Variable; +import com.google.common.collect.Iterators; + /** * A helper class for AST transformations needed for federated * execution. @@ -76,7 +81,19 @@ public class FedASTUtils { public static List safe(List list) { return list == null ? Collections.emptyList() : list; } - + + /** + * FIXME + * @param resource + * @return + */ + public static Reactor findFederatedReactor(Resource resource) { + return IteratorExtensions.findFirst( + Iterators.filter(resource.getAllContents(), Reactor.class), + Reactor::isFederated + ); + } + /** * Create a "network action" in the reactor that contains the given * connection and return it. diff --git a/org.lflang/src/org/lflang/federated/FedFileConfig.java b/org.lflang/src/org/lflang/federated/FedFileConfig.java index b6bd0c61b6..6cd5d8f909 100644 --- a/org.lflang/src/org/lflang/federated/FedFileConfig.java +++ b/org.lflang/src/org/lflang/federated/FedFileConfig.java @@ -26,6 +26,9 @@ package org.lflang.federated; import java.io.IOException; +import java.nio.file.Path; + +import org.eclipse.emf.ecore.resource.Resource; import org.lflang.FileConfig; @@ -38,24 +41,18 @@ * */ public class FedFileConfig extends FileConfig { - - /** Name of the federate for this FedFileConfig */ - protected final String federateName; - + + public FedFileConfig(Resource resource, Path srcGenBasePath, boolean useHierarchicalBin) throws IOException { + super(resource, srcGenBasePath, useHierarchicalBin); + + } + /** - * Create an instance of FedFileConfig for federate 'federateName' from an existing - * 'fileConfig' instance (an instance of 'FileConfig'). - * - * @param fileConfig The existing instance of the 'FileConfig' class. - * @param federateName The name of the federate. - * @throws IOException + * FIXME + * @return */ - public FedFileConfig(final FileConfig fileConfig, final String federateName) throws IOException { - super(fileConfig.resource, fileConfig.getSrcGenBasePath(), fileConfig.useHierarchicalBin); - - this.federateName = federateName; - // The generated code for each federate should be located at fileConfig.srcGenPath + "/federateName/" - this.srcGenPath = fileConfig.getSrcGenPath().resolve(federateName); - } + public Path getFedGenPath() { + return srcPkgPath.resolve("fed-gen"); + } } diff --git a/org.lflang/src/org/lflang/federated/OldFedFileConfig.java b/org.lflang/src/org/lflang/federated/OldFedFileConfig.java new file mode 100644 index 0000000000..c9a00a9244 --- /dev/null +++ b/org.lflang/src/org/lflang/federated/OldFedFileConfig.java @@ -0,0 +1,60 @@ +/************* + * Copyright (c) 2019-2021, The University of California at Berkeley. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ***************/ + +package org.lflang.federated; + +import java.io.IOException; +import java.nio.file.Path; + +import org.eclipse.emf.ecore.resource.Resource; + +import org.lflang.FileConfig; + +/** + * A child class of @see FileConfig that extends the base functionality to add support + * for federated execution. The code generator should create one instance of this class + * for each federate. + * + * @author Soroush Bateni + */ +@Deprecated +public class OldFedFileConfig extends FileConfig { + + /** Name of the federate for this FedFileConfig */ + protected final String federateName; + + /** + * Create an instance of FedFileConfig for federate 'federateName' from an existing + * 'fileConfig' instance (an instance of 'FileConfig'). + * + * @param fileConfig The existing instance of the 'FileConfig' class. + * @param federateName The name of the federate. + * @return + * @throws IOException FIXME + */ + public OldFedFileConfig(final FileConfig fileConfig, final String federateName) throws IOException { + super(fileConfig.resource, fileConfig.getSrcGenBasePath(), fileConfig.useHierarchicalBin); + + this.federateName = federateName; + // The generated code for each federate should be located at fileConfig.srcGenPath + "/federateName/" + this.srcGenPath = fileConfig.getSrcGenPath().resolve(federateName); + } +} diff --git a/org.lflang/src/org/lflang/federated/launcher/FedCLauncher.java b/org.lflang/src/org/lflang/federated/launcher/FedCLauncher.java index db0a8675a8..e34497012c 100644 --- a/org.lflang/src/org/lflang/federated/launcher/FedCLauncher.java +++ b/org.lflang/src/org/lflang/federated/launcher/FedCLauncher.java @@ -33,6 +33,7 @@ import org.lflang.TargetConfig; import org.lflang.federated.FedFileConfig; import org.lflang.federated.FederateInstance; +import org.lflang.federated.OldFedFileConfig; import org.lflang.generator.c.CCompiler; /** @@ -67,10 +68,10 @@ public FedCLauncher( @Override protected String compileCommandForFederate(FederateInstance federate) { - FedFileConfig fedFileConfig = null; + OldFedFileConfig fedFileConfig = null; TargetConfig localTargetConfig = targetConfig; try { - fedFileConfig = new FedFileConfig(fileConfig, federate.name); + fedFileConfig = new OldFedFileConfig(fileConfig, federate.name); } catch (IOException e) { errorReporter.reportError("Failed to create file config for federate "+federate.name); return ""; diff --git a/org.lflang/src/org/lflang/federated/launcher/FedLauncher.java b/org.lflang/src/org/lflang/federated/launcher/FedLauncher.java index 779520e1ec..c90bd130e8 100644 --- a/org.lflang/src/org/lflang/federated/launcher/FedLauncher.java +++ b/org.lflang/src/org/lflang/federated/launcher/FedLauncher.java @@ -39,6 +39,7 @@ import org.lflang.TargetProperty.ClockSyncMode; import org.lflang.federated.FedFileConfig; import org.lflang.federated.FederateInstance; +import org.lflang.federated.OldFedFileConfig; /** * Utility class that can be used to create a launcher for federated LF programs. @@ -204,7 +205,7 @@ public void createLauncher( int federateIndex = 0; for (FederateInstance federate : federates) { if (federate.isRemote) { - FedFileConfig fedFileConfig = new FedFileConfig(fileConfig, federate.name); + OldFedFileConfig fedFileConfig = new OldFedFileConfig(fileConfig, federate.name); Path fedRelSrcGenPath = fedFileConfig.getSrcGenBasePath().relativize(fedFileConfig.getSrcGenPath()); if(distCode.length() == 0) distCode.append(distHeader + "\n"); String logFileName = String.format("log/%s_%s.log", fedFileConfig.name, federate.name); @@ -395,7 +396,7 @@ private String getDistCode( FederateInstance federate, Path fedRelSrcGenPath, String logFileName, - FedFileConfig fedFileConfig, + OldFedFileConfig fedFileConfig, String compileCommand) { return String.join("\n", "echo \"Making directory "+path+" and subdirectories src-gen, bin, and log on host "+getUserHost(federate.user, federate.host)+"\"", diff --git a/org.lflang/src/org/lflang/generator/LFGenerator.java b/org.lflang/src/org/lflang/generator/LFGenerator.java index 08b1c4a915..260ad4b12e 100644 --- a/org.lflang/src/org/lflang/generator/LFGenerator.java +++ b/org.lflang/src/org/lflang/generator/LFGenerator.java @@ -17,6 +17,8 @@ import org.lflang.ErrorReporter; import org.lflang.FileConfig; import org.lflang.Target; +import org.lflang.federated.FedASTUtils; +import org.lflang.federated.FedFileConfig; import org.lflang.federated.FooBarGenerator; import org.lflang.generator.c.CGenerator; import org.lflang.generator.python.PythonGenerator; @@ -59,6 +61,9 @@ private FileConfig createFileConfig(final Target target, IFileSystemAccess2 fsa, LFGeneratorContext context) throws IOException { Path srcGenBasePath = FileConfig.getSrcGenRoot(fsa); + if (FedASTUtils.findFederatedReactor(resource) != null) { + return new FedFileConfig(resource, srcGenBasePath, context.useHierarchicalBin()); + } // Since our Eclipse Plugin uses code injection via guice, we need to // play a few tricks here so that FileConfig does not appear as an // import. Instead we look the class up at runtime and instantiate it if @@ -153,7 +158,6 @@ public void doGenerate(Resource resource, IFileSystemAccess2 fsa, if (lfContext.getMode() == LFGeneratorContext.Mode.LSP_FAST) return; // The fastest way to generate code is to not generate any code. final Target target = Target.fromDecl(ASTUtils.targetDecl(resource)); - final Reactor main = ASTUtils.findMainReactor(resource); assert target != null; @@ -165,7 +169,7 @@ public void doGenerate(Resource resource, IFileSystemAccess2 fsa, } final ErrorReporter errorReporter = lfContext.constructErrorReporter(fileConfig); - if (main.isFederated()) { + if (FedASTUtils.findFederatedReactor(resource) != null) { generatorErrorsOccurred = (new FooBarGenerator(fileConfig, errorReporter)).doGenerate(resource, lfContext); } else { diff --git a/org.lflang/src/org/lflang/generator/c/CGenerator.java b/org.lflang/src/org/lflang/generator/c/CGenerator.java index 41f983e581..29e9fe9296 100644 --- a/org.lflang/src/org/lflang/generator/c/CGenerator.java +++ b/org.lflang/src/org/lflang/generator/c/CGenerator.java @@ -66,6 +66,7 @@ STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY import org.lflang.TimeValue; import org.lflang.federated.FedFileConfig; import org.lflang.federated.FederateInstance; +import org.lflang.federated.OldFedFileConfig; import org.lflang.federated.launcher.FedCLauncher; import org.lflang.federated.serialization.FedROS2CPPSerialization; import org.lflang.federated.serialization.SupportedSerializers; @@ -536,7 +537,7 @@ public void doGenerate(Resource resource, LFGeneratorContext context) { // If federated, append the federate name to the file name. // Only generate one output if there is no federation. try { - fileConfig = new FedFileConfig(fileConfig, federate.name); + fileConfig = new OldFedFileConfig(fileConfig, federate.name); } catch (IOException e) { Exceptions.sneakyThrow(e); } diff --git a/org.lflang/src/org/lflang/generator/python/PythonGenerator.java b/org.lflang/src/org/lflang/generator/python/PythonGenerator.java index c6e4204a68..c79977bc9f 100644 --- a/org.lflang/src/org/lflang/generator/python/PythonGenerator.java +++ b/org.lflang/src/org/lflang/generator/python/PythonGenerator.java @@ -52,6 +52,7 @@ import org.lflang.TargetProperty; import org.lflang.federated.FedFileConfig; import org.lflang.federated.FederateInstance; +import org.lflang.federated.OldFedFileConfig; import org.lflang.federated.launcher.FedPyLauncher; import org.lflang.federated.serialization.FedNativePythonSerialization; import org.lflang.federated.serialization.SupportedSerializers; @@ -666,7 +667,7 @@ public void doGenerate(Resource resource, LFGeneratorContext context) { var lfModuleName = isFederated ? fileConfig.name + "_" + federate.name : fileConfig.name; if (isFederated) { try { - fileConfig = new FedFileConfig(fileConfig, federate.name); + fileConfig = new OldFedFileConfig(fileConfig, federate.name); } catch (IOException e) { throw Exceptions.sneakyThrow(e); } From 2fbd0478e8839323eeb82982f2ba5e12ce983c79 Mon Sep 17 00:00:00 2001 From: Soroush Bateni Date: Mon, 6 Jun 2022 18:27:00 -0700 Subject: [PATCH 003/343] Add generation of .lf files --- .../org/lflang/federated/FedFileConfig.java | 10 ++++- .../org/lflang/federated/FedGenerator.java | 41 +++++++++++++++++++ .../org/lflang/federated/FooBarGenerator.java | 17 -------- .../src/org/lflang/generator/LFGenerator.java | 16 ++++---- 4 files changed, 59 insertions(+), 25 deletions(-) create mode 100644 org.lflang/src/org/lflang/federated/FedGenerator.java delete mode 100644 org.lflang/src/org/lflang/federated/FooBarGenerator.java diff --git a/org.lflang/src/org/lflang/federated/FedFileConfig.java b/org.lflang/src/org/lflang/federated/FedFileConfig.java index 6cd5d8f909..ba988f712d 100644 --- a/org.lflang/src/org/lflang/federated/FedFileConfig.java +++ b/org.lflang/src/org/lflang/federated/FedFileConfig.java @@ -25,6 +25,7 @@ package org.lflang.federated; +import java.io.File; import java.io.IOException; import java.nio.file.Path; @@ -47,12 +48,19 @@ public FedFileConfig(Resource resource, Path srcGenBasePath, boolean useHierarch } + public FedFileConfig(FileConfig fileConfig) throws IOException { + super(fileConfig.resource, fileConfig.getSrcGenBasePath(), fileConfig.useHierarchicalBin); + } + /** * FIXME * @return */ public Path getFedGenPath() { - return srcPkgPath.resolve("fed-gen"); + return srcPkgPath.resolve("fed-gen").resolve(this.name); } + Path getFedSrcPath() { + return getFedGenPath().resolve("src"); + } } diff --git a/org.lflang/src/org/lflang/federated/FedGenerator.java b/org.lflang/src/org/lflang/federated/FedGenerator.java new file mode 100644 index 0000000000..8c699d3a93 --- /dev/null +++ b/org.lflang/src/org/lflang/federated/FedGenerator.java @@ -0,0 +1,41 @@ +package org.lflang.federated; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.xtext.nodemodel.util.NodeModelUtils; + +import org.lflang.ASTUtils; +import org.lflang.ErrorReporter; +import org.lflang.generator.LFGeneratorContext; +import org.lflang.lf.Instantiation; +import org.lflang.lf.Reactor; + +public class FedGenerator { + + private FedFileConfig fileConfig; + + public FedGenerator(FedFileConfig fileConfig, ErrorReporter errorReporter) { + + this.fileConfig = fileConfig; + } + public boolean doGenerate(Resource resource, LFGeneratorContext context) throws IOException { + Reactor fedReactor = FedASTUtils.findFederatedReactor(resource); + for (Instantiation fedInstantiation : fedReactor.getInstantiations()) { + System.out.println("Generating code for federate " + fedInstantiation.getName() + " in directory " + + fileConfig.getFedSrcPath()); + Files.createDirectories(fileConfig.getFedSrcPath()); + + Path lfFilePath = fileConfig.getFedSrcPath().resolve(fedInstantiation.getName() + ".lf"); + try (var srcWriter = Files.newBufferedWriter(lfFilePath)) { + srcWriter.write(NodeModelUtils.getNode(fedReactor.eContainer()).getText()); + } + + } + return false; + } + + // private void generateFederate(); +} diff --git a/org.lflang/src/org/lflang/federated/FooBarGenerator.java b/org.lflang/src/org/lflang/federated/FooBarGenerator.java deleted file mode 100644 index f4bfce9910..0000000000 --- a/org.lflang/src/org/lflang/federated/FooBarGenerator.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.lflang.federated; - -import org.eclipse.emf.ecore.resource.Resource; -import org.lflang.ErrorReporter; -import org.lflang.FileConfig; -import org.lflang.generator.LFGeneratorContext; - -public class FooBarGenerator { - - public FooBarGenerator(FileConfig fileConfig, ErrorReporter errorReporter) { - - } - public boolean doGenerate(Resource resource, LFGeneratorContext context) { - System.out.println("Federated program detected."); - return false; - } -} diff --git a/org.lflang/src/org/lflang/generator/LFGenerator.java b/org.lflang/src/org/lflang/generator/LFGenerator.java index 260ad4b12e..8e17db4889 100644 --- a/org.lflang/src/org/lflang/generator/LFGenerator.java +++ b/org.lflang/src/org/lflang/generator/LFGenerator.java @@ -1,6 +1,5 @@ package org.lflang.generator; -import java.io.File; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; @@ -19,10 +18,9 @@ import org.lflang.Target; import org.lflang.federated.FedASTUtils; import org.lflang.federated.FedFileConfig; -import org.lflang.federated.FooBarGenerator; +import org.lflang.federated.FedGenerator; import org.lflang.generator.c.CGenerator; import org.lflang.generator.python.PythonGenerator; -import org.lflang.lf.Reactor; import org.lflang.scoping.LFGlobalScopeProvider; import com.google.inject.Inject; @@ -61,9 +59,6 @@ private FileConfig createFileConfig(final Target target, IFileSystemAccess2 fsa, LFGeneratorContext context) throws IOException { Path srcGenBasePath = FileConfig.getSrcGenRoot(fsa); - if (FedASTUtils.findFederatedReactor(resource) != null) { - return new FedFileConfig(resource, srcGenBasePath, context.useHierarchicalBin()); - } // Since our Eclipse Plugin uses code injection via guice, we need to // play a few tricks here so that FileConfig does not appear as an // import. Instead we look the class up at runtime and instantiate it if @@ -170,7 +165,14 @@ public void doGenerate(Resource resource, IFileSystemAccess2 fsa, final ErrorReporter errorReporter = lfContext.constructErrorReporter(fileConfig); if (FedASTUtils.findFederatedReactor(resource) != null) { - generatorErrorsOccurred = (new FooBarGenerator(fileConfig, errorReporter)).doGenerate(resource, lfContext); + try { + generatorErrorsOccurred = (new FedGenerator( + new FedFileConfig(fileConfig), + errorReporter)).doGenerate(resource, lfContext); + } catch (IOException e) { + throw new RuntimeIOException("Error during federated code generation", e); + } + } else { final GeneratorBase generator = createGenerator(target, fileConfig, errorReporter); From 90d719e6f4b3fad70a246d34e624e5f0663b2d22 Mon Sep 17 00:00:00 2001 From: Soroush Bateni Date: Mon, 6 Jun 2022 18:30:54 -0700 Subject: [PATCH 004/343] Factored out code generation for each federate --- .../org/lflang/federated/FedGenerator.java | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/org.lflang/src/org/lflang/federated/FedGenerator.java b/org.lflang/src/org/lflang/federated/FedGenerator.java index 8c699d3a93..6e69699a09 100644 --- a/org.lflang/src/org/lflang/federated/FedGenerator.java +++ b/org.lflang/src/org/lflang/federated/FedGenerator.java @@ -24,18 +24,28 @@ public FedGenerator(FedFileConfig fileConfig, ErrorReporter errorReporter) { public boolean doGenerate(Resource resource, LFGeneratorContext context) throws IOException { Reactor fedReactor = FedASTUtils.findFederatedReactor(resource); for (Instantiation fedInstantiation : fedReactor.getInstantiations()) { - System.out.println("Generating code for federate " + fedInstantiation.getName() + " in directory " - + fileConfig.getFedSrcPath()); - Files.createDirectories(fileConfig.getFedSrcPath()); - - Path lfFilePath = fileConfig.getFedSrcPath().resolve(fedInstantiation.getName() + ".lf"); - try (var srcWriter = Files.newBufferedWriter(lfFilePath)) { - srcWriter.write(NodeModelUtils.getNode(fedReactor.eContainer()).getText()); - } - + generateFederate(fedInstantiation); } return false; } - // private void generateFederate(); + /** + * Generate a .lf file for federate {@code fed}. + * @param fed + * @throws IOException + */ + private void generateFederate(Instantiation fed) throws IOException { + System.out.println("##### Generating code for federate " + fed.getName() + " in directory " + + fileConfig.getFedSrcPath()); + Files.createDirectories(fileConfig.getFedSrcPath()); + + Path lfFilePath = fileConfig.getFedSrcPath().resolve(fed.getName() + ".lf"); + + // FIXME: Do magic... + + try (var srcWriter = Files.newBufferedWriter(lfFilePath)) { + srcWriter.write(NodeModelUtils.getNode(fed.eContainer()).getText()); + } + + } } From f8e9096b98374af279986bdeadfb325ee8ac28bb Mon Sep 17 00:00:00 2001 From: Marten Lohstroh Date: Tue, 7 Jun 2022 18:37:16 -0700 Subject: [PATCH 005/343] WIP. Might abandon this effort altogether in favor of an LFGenerator --- .../src/org/lflang/federated/FedFileConfig.java | 8 ++++++-- .../src/org/lflang/federated/FedGenerator.java | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/org.lflang/src/org/lflang/federated/FedFileConfig.java b/org.lflang/src/org/lflang/federated/FedFileConfig.java index ba988f712d..78bbdf949e 100644 --- a/org.lflang/src/org/lflang/federated/FedFileConfig.java +++ b/org.lflang/src/org/lflang/federated/FedFileConfig.java @@ -53,13 +53,17 @@ public FedFileConfig(FileConfig fileConfig) throws IOException { } /** - * FIXME - * @return + * Return the path to the root of a LF project generated on the basis of a + * federated LF program currently under compilation. */ public Path getFedGenPath() { return srcPkgPath.resolve("fed-gen").resolve(this.name); } + /** + * Return the path for storing generated LF sources that jointly constitute a + * federation. + */ Path getFedSrcPath() { return getFedGenPath().resolve("src"); } diff --git a/org.lflang/src/org/lflang/federated/FedGenerator.java b/org.lflang/src/org/lflang/federated/FedGenerator.java index 6e69699a09..dabd863f86 100644 --- a/org.lflang/src/org/lflang/federated/FedGenerator.java +++ b/org.lflang/src/org/lflang/federated/FedGenerator.java @@ -11,6 +11,7 @@ import org.lflang.ErrorReporter; import org.lflang.generator.LFGeneratorContext; import org.lflang.lf.Instantiation; +import org.lflang.lf.LfFactory; import org.lflang.lf.Reactor; public class FedGenerator { @@ -23,6 +24,13 @@ public FedGenerator(FedFileConfig fileConfig, ErrorReporter errorReporter) { } public boolean doGenerate(Resource resource, LFGeneratorContext context) throws IOException { Reactor fedReactor = FedASTUtils.findFederatedReactor(resource); + + // Create proxies for all instances. + for (Instantiation fedInstantiation : fedReactor.getInstantiations()) { + // this.proxies.add ... + } + + // Generate code for each federate for (Instantiation fedInstantiation : fedReactor.getInstantiations()) { generateFederate(fedInstantiation); } @@ -43,8 +51,13 @@ private void generateFederate(Instantiation fed) throws IOException { // FIXME: Do magic... + // Create import statements for definition of class that fed is an instance of. + + // Go through all instances and + try (var srcWriter = Files.newBufferedWriter(lfFilePath)) { - srcWriter.write(NodeModelUtils.getNode(fed.eContainer()).getText()); + srcWriter.write(NodeModelUtils.getNode(LfFactory.eINSTANCE.createReactor()).getText()); + //srcWriter.write(NodeModelUtils.getNode(fed.eContainer()).getText()); } } From e120a6615a3de4c29f96d8f1233fe827148de35b Mon Sep 17 00:00:00 2001 From: Peter Donovan Date: Mon, 13 Jun 2022 01:23:21 -0700 Subject: [PATCH 006/343] [tests] Exclude fed-gen from test dir indexing. --- org.lflang.tests/src/org/lflang/tests/TestRegistry.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/org.lflang.tests/src/org/lflang/tests/TestRegistry.java b/org.lflang.tests/src/org/lflang/tests/TestRegistry.java index 1bed12bce7..8debf1e0f7 100644 --- a/org.lflang.tests/src/org/lflang/tests/TestRegistry.java +++ b/org.lflang.tests/src/org/lflang/tests/TestRegistry.java @@ -76,7 +76,7 @@ public Set getTests(Target t, TestCategory c) { * test file that has a directory in its path that matches an entry in this * array will not be discovered. */ - public static final String[] IGNORED_DIRECTORIES = {"failing", "knownfailed", "failed"}; + public static final String[] IGNORED_DIRECTORIES = {"failing", "knownfailed", "failed", "fed-gen"}; /** * Path to the root of the repository. From acb9e0098e5f7deb5bfdb3996dd7211efe8c9e9a Mon Sep 17 00:00:00 2001 From: Soroush Bateni Date: Wed, 15 Jun 2022 19:17:18 -0500 Subject: [PATCH 007/343] WIP: Moved logic to FedGenerator --- .idea/codeStyles/Project.xml | 19 +- .../lflang/federated/CGeneratorExtension.java | 250 ------- .../src/org/lflang/federated/FedASTUtils.java | 693 +++++++----------- .../federated/FedConnectionInstance.java | 93 +++ .../org/lflang/federated/FedGenerator.java | 395 +++++++++- .../lflang/federated/FederateInstance.java | 52 +- .../federated/PythonGeneratorExtension.java | 209 ------ .../extensions/CGeneratorExtension.java | 685 +++++++++++++++++ .../extensions/FedExtensionFactory.java | 17 + .../extensions/FedGeneratorExtension.java | 101 +++ .../extensions/PythonGeneratorExtension.java | 134 ++++ .../org/lflang/generator/GeneratorBase.java | 349 +-------- .../org/lflang/generator/c/CGenerator.java | 205 ------ .../lflang/generator/c/CNetworkGenerator.java | 331 +-------- .../generator/c/CReactionGenerator.java | 2 +- .../generator/c/CTriggerObjectsGenerator.java | 2 +- .../python/PythonNetworkGenerator.java | 52 +- 17 files changed, 1762 insertions(+), 1827 deletions(-) delete mode 100644 org.lflang/src/org/lflang/federated/CGeneratorExtension.java create mode 100644 org.lflang/src/org/lflang/federated/FedConnectionInstance.java delete mode 100644 org.lflang/src/org/lflang/federated/PythonGeneratorExtension.java create mode 100644 org.lflang/src/org/lflang/federated/extensions/CGeneratorExtension.java create mode 100644 org.lflang/src/org/lflang/federated/extensions/FedExtensionFactory.java create mode 100644 org.lflang/src/org/lflang/federated/extensions/FedGeneratorExtension.java create mode 100644 org.lflang/src/org/lflang/federated/extensions/PythonGeneratorExtension.java diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml index 5a907b1337..6e940cc4c4 100644 --- a/.idea/codeStyles/Project.xml +++ b/.idea/codeStyles/Project.xml @@ -47,6 +47,7 @@ +