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 cycle checking when calculating min delay from nearest physical action #2459

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
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 @@ -686,22 +686,44 @@ public String toString() {
* otherwise
*/
public TimeValue findNearestPhysicalActionTrigger(ReactionInstance reaction) {
Set<ReactionInstance> visited = new HashSet<>();
return findNearestPhysicalActionTriggerRecursive(visited, reaction);
}

/**
* Find the nearest (shortest) path to a physical action trigger from
* this 'reaction' in terms of minimum delay by recursively visiting
* upstream triggers and reactions until a physical action is reached.
*
* @param visited A set of reactions that have been visited used to avoid deep loops
* @param reaction The reaction to start with
* @return The minimum delay found to the nearest physical action and TimeValue.MAX_VALUE
* otherwise
*/
private TimeValue findNearestPhysicalActionTriggerRecursive(Set<ReactionInstance> visited, ReactionInstance reaction) {
System.out.println(reaction);
TimeValue minDelay = TimeValue.MAX_VALUE;
for (TriggerInstance<? extends Variable> trigger : reaction.triggers) {
if (trigger.getDefinition() instanceof Action action) {
ActionInstance actionInstance = (ActionInstance) trigger;
if (action.getOrigin() == ActionOrigin.PHYSICAL) {
System.out.println("Physical action: " + action);
lsk567 marked this conversation as resolved.
Show resolved Hide resolved
if (actionInstance.getMinDelay().isEarlierThan(minDelay)) {
minDelay = actionInstance.getMinDelay();
}
} else if (action.getOrigin() == ActionOrigin.LOGICAL) {
}
else if (action.getOrigin() == ActionOrigin.LOGICAL) {
System.out.println("Logical action: " + action);
System.out.println("Depends on: " + actionInstance.getDependsOnReactions());
lsk567 marked this conversation as resolved.
Show resolved Hide resolved
// Logical action
// Follow it upstream inside the reactor
for (ReactionInstance uReaction : actionInstance.getDependsOnReactions()) {
// Avoid a loop
if (!Objects.equal(uReaction, reaction)) {
// Avoid a potentially deep loop by checking the visited set.
if (!visited.contains(uReaction)) {
visited.add(uReaction); // Mark the upstream reaction as visited.
System.out.println("Upstream reaction: " + uReaction);
lsk567 marked this conversation as resolved.
Show resolved Hide resolved
TimeValue uMinDelay =
actionInstance.getMinDelay().add(findNearestPhysicalActionTrigger(uReaction));
actionInstance.getMinDelay().add(findNearestPhysicalActionTriggerRecursive(visited, uReaction));
if (uMinDelay.isEarlierThan(minDelay)) {
minDelay = uMinDelay;
}
Expand All @@ -713,7 +735,8 @@ public TimeValue findNearestPhysicalActionTrigger(ReactionInstance reaction) {
// Outputs of contained reactions
PortInstance outputInstance = (PortInstance) trigger;
for (ReactionInstance uReaction : outputInstance.getDependsOnReactions()) {
TimeValue uMinDelay = findNearestPhysicalActionTrigger(uReaction);
visited.add(uReaction); // Mark the upstream reaction as visited.
lsk567 marked this conversation as resolved.
Show resolved Hide resolved
TimeValue uMinDelay = findNearestPhysicalActionTriggerRecursive(visited, uReaction);
if (uMinDelay.isEarlierThan(minDelay)) {
minDelay = uMinDelay;
}
Expand Down
Loading