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

Remove unchecked compilation warnings #1638

Merged
merged 4 commits into from
Mar 13, 2023
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 @@ -115,10 +115,9 @@ public void testLet() throws Exception {
TreeIterator<EObject> 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());
Expand Down
2 changes: 1 addition & 1 deletion org.lflang/src/lib/c/reactor-c
16 changes: 8 additions & 8 deletions org.lflang/src/org/lflang/federated/generator/FedGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -176,30 +177,29 @@ public boolean doGenerate(Resource resource, LFGeneratorContext context) throws
return false;
}

Map<Path, CodeMap> codeMapMap = compileFederates(context, lf2lfCodeMapMap, (subContexts) -> {
Map<Path, CodeMap> codeMapMap = compileFederates(context, lf2lfCodeMapMap, subContexts -> {
if (context.getTargetConfig().dockerOptions == null) return;
final List<DockerData> services = new ArrayList();
final List<DockerData> 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);
}
});

Expand Down
2 changes: 1 addition & 1 deletion org.lflang/src/org/lflang/generator/GeneratorBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public abstract class GeneratorBase extends AbstractLFValidator {
/**
* A list ot AST transformations to apply before code generation
*/
private List<AstTransformation> astTransformations = new ArrayList();
private final List<AstTransformation> astTransformations = new ArrayList<>();

/**
* Create a new GeneratorBase object.
Expand Down
27 changes: 3 additions & 24 deletions org.lflang/src/org/lflang/generator/GeneratorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -188,30 +190,7 @@ public static void accommodatePhysicalActionsIfPresent(
* {@code resource}
*/
public static <T> Iterable<T> findAll(Resource resource, Class<T> nodeType) {
Iterator<EObject> 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);
lhstrh marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
8 changes: 2 additions & 6 deletions org.lflang/src/org/lflang/generator/ReactorInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -746,12 +746,8 @@ protected void createReactionInstances() {
/**
* Returns the built-in trigger or create a new one if none exists.
*/
protected TriggerInstance<? extends Variable> 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<BuiltinTriggerVariable> getOrCreateBuiltinTrigger(BuiltinTriggerRef trigger) {
return builtinTriggers.computeIfAbsent(trigger.getType(), ref -> TriggerInstance.builtinTrigger(trigger, this));
}

////////////////////////////////////////
Expand Down
89 changes: 39 additions & 50 deletions org.lflang/src/org/lflang/generator/TriggerInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends Variable> extends NamedInstance<T> {

/** 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
Expand All @@ -53,33 +54,21 @@ public class TriggerInstance<T extends Variable> extends NamedInstance<T> {
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<BuiltinTriggerVariable> 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
Expand All @@ -90,18 +79,18 @@ public Set<ReactionInstance> 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<ReactionInstance> getDependsOnReactions() {
return dependsOnReactions;
};
/**
* Return the name of this trigger.
}

/**
* Return the name of this trigger.
* @return The name of this trigger.
*/
@Override
Expand All @@ -113,76 +102,76 @@ 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<ReactionInstance> dependentReactions = new LinkedHashSet<ReactionInstance>();
Set<ReactionInstance> 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<ReactionInstance> dependsOnReactions = new LinkedHashSet<ReactionInstance>();
Set<ReactionInstance> dependsOnReactions = new LinkedHashSet<>();

/////////////////////////////////////////////
//// Special class for builtin triggers

/**
* 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(
Expand Down