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

Angular bracket imports for reusable reactor modules #2404

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion core/src/main/java/org/lflang/LinguaFranca.xtext
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Model:
/**
* Import declaration.
*/
Import: 'import' reactorClasses+=ImportedReactor (',' reactorClasses+=ImportedReactor)* 'from' importURI=STRING ';'?;
Import: 'import' reactorClasses+=ImportedReactor (',' reactorClasses+=ImportedReactor)* 'from' (importURI=STRING | '<' importPackage=Path '>') ';'?;
lhstrh marked this conversation as resolved.
Show resolved Hide resolved

ReactorDecl: Reactor | ImportedReactor;

Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/org/lflang/ast/IsEqual.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public Boolean caseModel(Model object) {
public Boolean caseImport(Import object) {
return new ComparisonMachine<>(object, Import.class)
.equalAsObjects(Import::getImportURI)
.equalAsObjects(Import::getImportPackage)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain how this works?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this effectively function like "equal to importURI OR equal to importPackage"?

.listsEquivalent(Import::getReactorClasses)
.conclusion;
}
Expand Down
8 changes: 5 additions & 3 deletions core/src/main/java/org/lflang/ast/ToLf.java
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,11 @@ public MalleableString caseImport(Import object) {
.append("import ")
// TODO: This is a place where we can use conditional parentheses.
.append(list(", ", "", "", false, true, true, object.getReactorClasses()))
.append(" from \"")
.append(object.getImportURI())
.append("\"")
.append(" from ")
.append(
object.getImportURI() != null
? "\"" + object.getImportURI() + "\""
: "<" + object.getImportPackage() + ">")
.get();
}

Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/org/lflang/ast/ToSExpr.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ public SExpr caseImport(Import object) {
// reactorClasses+=ImportedReactor)* 'from' importURI=STRING ';'?;
return sList(
"import",
new SAtom<>(object.getImportURI()),
new SAtom<>(
object.getImportURI() != null ? object.getImportURI() : object.getImportPackage()),
sList("reactors", object.getReactorClasses()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@ String generateImports(FederateInstance federate, FederationFileConfig fileConfi
.forEach(
i -> {
visitedImports.add(i);
Path importPath = fileConfig.srcPath.resolve(i.getImportURI()).toAbsolutePath();
i.setImportURI(
fileConfig.getSrcPath().relativize(importPath).toString().replace('\\', '/'));
if (i.getImportURI() != null) {
Path importPath = fileConfig.srcPath.resolve(i.getImportURI()).toAbsolutePath();
i.setImportURI(
fileConfig.getSrcPath().relativize(importPath).toString().replace('\\', '/'));
} else {
Path importPath = fileConfig.srcPath.resolve(i.getImportPackage()).toAbsolutePath();
i.setImportPackage(fileConfig.getSrcPath().relativize(importPath).toString());
}
});

var importStatements = new CodeBuilder();
Expand Down
13 changes: 11 additions & 2 deletions core/src/main/java/org/lflang/scoping/LFScopeProviderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@
package org.lflang.scoping;

import static java.util.Collections.emptyList;
import static org.lflang.ast.ASTUtils.*;
import static org.lflang.ast.ASTUtils.allActions;
import static org.lflang.ast.ASTUtils.allInputs;
import static org.lflang.ast.ASTUtils.allOutputs;
import static org.lflang.ast.ASTUtils.allParameters;
import static org.lflang.ast.ASTUtils.allTimers;
import static org.lflang.ast.ASTUtils.allWatchdogs;
import static org.lflang.ast.ASTUtils.toDefinition;

import com.google.inject.Inject;
import java.util.ArrayList;
Expand Down Expand Up @@ -104,7 +110,10 @@ public IScope getScope(EObject context, EReference reference) {
* statement.
*/
protected IScope getScopeForImportedReactor(ImportedReactor context, EReference reference) {
String importURI = ((Import) context.eContainer()).getImportURI();
String importURI =
((Import) context.eContainer()).getImportURI() != null
? ((Import) context.eContainer()).getImportURI()
: ((Import) context.eContainer()).getImportPackage();
var importedURI =
scopeProvider.resolve(importURI == null ? "" : importURI, context.eResource());
if (importedURI != null) {
Expand Down
20 changes: 20 additions & 0 deletions test/Python/src/FederatedImportLibrary.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Test the new import statement, with the import path enclosed in angle brackets, in federated LF programs
target Python {
timeout: 2 sec
}

import Count from <lib/Count.lf>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tanneberger, tagging you because it still appears to be unclear what should be inside the angular brackets. I thought the notation was reserved for packages, not local paths?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the old version. I still need to implement what we discussed last time. My focus here was to fix the formatting errors causing the check to fail. I'm currently working on a new version where we shorten the paths inside the angular brackets to <root/file.lf> for downloaded Lingo packages and lib/file.lf for locally defined libraries

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean "lib/file.lf" for locally defined libraries? My assumption had been that angular brackets are only for published packages, not locally defined ones.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, use angular brackets for published packages and quotation marks for locally defined libraries. In my previous comment, I was referring to "lib/file.lf", but I mistakenly omitted the quotation marks


reactor Printer {
input i

reaction(i) {=
print(f"Count: {i.value}")
=}
}

federated reactor {
c = new Count()
p = new Printer()
c.out -> p.i
}
20 changes: 20 additions & 0 deletions test/Python/src/ImportLibrary.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Test the new import statement with the import path enclosed in angle brackets
target Python {
timeout: 2 sec
}

import Count from <lib/Count.lf>

reactor Printer {
input i

reaction(i) {=
print(f"Count: {i.value}")
=}
}

main reactor {
c = new Count()
p = new Printer()
c.out -> p.i
}
Loading