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 to not include downstream reactions in highlighted cycles #1270

Merged
merged 1 commit into from
Jul 5, 2022
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 @@ -32,7 +32,7 @@ STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
import java.util.Set;

import org.lflang.generator.ReactionInstance.Runtime;
import org.lflang.graph.DirectedGraph;
import org.lflang.graph.PrecedenceGraph;
import org.lflang.lf.Variable;

/**
Expand All @@ -54,7 +54,7 @@ STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* @author{Marten Lohstroh <[email protected]>}
* @author{Edward A. Lee <[email protected]>}
*/
public class ReactionInstanceGraph extends DirectedGraph<ReactionInstance.Runtime> {
public class ReactionInstanceGraph extends PrecedenceGraph<ReactionInstance.Runtime> {

/**
* Create a new graph by traversing the maps in the named instances
Expand Down
36 changes: 22 additions & 14 deletions org.lflang/src/org/lflang/generator/ReactorInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,26 +244,34 @@ public void clearCaches(boolean includingRuntimes) {
public Set<NamedInstance<?>> getCycles() {
if (depth != 0) return root().getCycles();
if (cachedCycles != null) return cachedCycles;
Set<ReactionInstance> reactions = new LinkedHashSet<>();
cachedCycles = new LinkedHashSet<>();

ReactionInstanceGraph reactionRuntimes = assignLevels();
for (ReactionInstance.Runtime runtime : reactionRuntimes.nodes()) {
reactions.add(runtime.getReaction());
}
Set<PortInstance> ports = new LinkedHashSet<>();
// Need to figure out which ports are involved in the cycles.
// It may not be all ports that depend on this reaction.
for (ReactionInstance r : reactions) {
for (TriggerInstance<? extends Variable> p : r.effects) {
if (p instanceof PortInstance) {
findPaths((PortInstance)p, reactions, ports);
if (reactionRuntimes.nodes().size() > 0) {
Set<ReactionInstance> reactions = new LinkedHashSet<>();
Set<PortInstance> ports = new LinkedHashSet<>();
// There are cycles. But the nodes set includes not
// just the cycles, but also nodes that are downstream of the
// cycles. Use Tarjan's algorithm to get just the cycles.
var cycleNodes = reactionRuntimes.getCycles();
for (var cycle : cycleNodes) {
for (ReactionInstance.Runtime runtime : cycle) {
reactions.add(runtime.getReaction());
}
}
// Need to figure out which ports are involved in the cycles.
// It may not be all ports that depend on this reaction.
for (ReactionInstance r : reactions) {
for (TriggerInstance<? extends Variable> p : r.effects) {
if (p instanceof PortInstance) {
findPaths((PortInstance)p, reactions, ports);
}
}
}
cachedCycles.addAll(reactions);
cachedCycles.addAll(ports);
}

cachedCycles = new LinkedHashSet<>();
cachedCycles.addAll(reactions);
cachedCycles.addAll(ports);
return cachedCycles;
}

Expand Down