-
Notifications
You must be signed in to change notification settings - Fork 510
Added Alternative DependencySorter to deal with cycles #219
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
Open
mjtalbot
wants to merge
4
commits into
master
Choose a base branch
from
dependency_sorter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
aad97b3
Added an alternative dependency sorter that will attempt to return a …
mjtalbot af6a7da
Replaced dependency sorter with a sorter using Tarjans algorithm to d…
mjtalbot 8aad656
cleaned up tests, and clearly highlight how isolated nodes will be tr…
mjtalbot 5451394
Minor tweak to keep the default sorter simpler and renamed the test f…
luigi-rosso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
import "dart:collection"; | ||
|
||
import "package:graphs/graphs.dart"; | ||
|
||
import "actor_component.dart"; | ||
|
||
class DependencySorter { | ||
|
@@ -24,7 +27,7 @@ class DependencySorter { | |
return true; | ||
} | ||
if (_temp.contains(n)) { | ||
print("Dependency cycle!"); | ||
// cycle detected! | ||
return false; | ||
} | ||
|
||
|
@@ -44,3 +47,60 @@ class DependencySorter { | |
return true; | ||
} | ||
} | ||
|
||
/// Sorts dependencies for Actors even when cycles are present | ||
/// | ||
/// Any nodes that form part of a cycle can be found in `cycleNodes` after | ||
/// `sort`. NOTE: Nodes isolated by cycles will not be found in `_order` or | ||
/// `cycleNodes` e.g. `A -> B <-> C -> D` isolates D when running a sort based | ||
/// on A | ||
class TarjansDependencySorter extends DependencySorter { | ||
HashSet<ActorComponent> _cycleNodes; | ||
HashSet<ActorComponent> get cycleNodes => _cycleNodes; | ||
|
||
TarjansDependencySorter() { | ||
_perm = HashSet<ActorComponent>(); | ||
_temp = HashSet<ActorComponent>(); | ||
_cycleNodes = HashSet<ActorComponent>(); | ||
} | ||
|
||
@override | ||
bool visit(ActorComponent n) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh this is nicer! |
||
if (cycleNodes.contains(n)) { | ||
// skip any nodes on a known cycle. | ||
return true; | ||
} | ||
|
||
return super.visit(n); | ||
} | ||
|
||
@override | ||
List<ActorComponent> sort(ActorComponent root) { | ||
_order = <ActorComponent>[]; | ||
|
||
if (!visit(root)) { | ||
// if we detect cycles, go find them all | ||
_perm.clear(); | ||
_temp.clear(); | ||
_cycleNodes.clear(); | ||
_order.clear(); | ||
|
||
var cycles = stronglyConnectedComponents<ActorComponent>( | ||
[root], (ActorComponent node) => node.dependents); | ||
|
||
cycles.forEach((cycle) { | ||
// cycles of len 1 are not cycles. | ||
if (cycle.length > 1) { | ||
cycle.forEach((cycleMember) { | ||
_cycleNodes.add(cycleMember); | ||
}); | ||
} | ||
}); | ||
|
||
// revisit the tree, skipping nodes on any cycle. | ||
visit(root); | ||
} | ||
|
||
return _order; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,7 @@ author: "Rive Team <[email protected]>" | |
homepage: https://github.com/2d-inc/Flare-Flutter | ||
environment: | ||
sdk: ">=2.1.0 <3.0.0" | ||
|
||
dependencies: | ||
graphs: ^0.2.0 | ||
dev_dependencies: | ||
test: ^1.9.4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,259 @@ | ||
import 'dart:typed_data'; | ||
|
||
import 'package:flare_dart/actor_artboard.dart'; | ||
import 'package:flare_dart/actor_color.dart'; | ||
import 'package:flare_dart/actor_component.dart'; | ||
import 'package:flare_dart/actor_drop_shadow.dart'; | ||
import 'package:flare_dart/actor_inner_shadow.dart'; | ||
import 'package:flare_dart/actor_layer_effect_renderer.dart'; | ||
import 'package:flare_dart/actor_node.dart'; | ||
import 'package:flare_dart/actor.dart'; | ||
import 'package:flare_dart/dependency_sorter.dart'; | ||
|
||
import 'package:test/test.dart'; | ||
|
||
class DummyActor extends Actor { | ||
@override | ||
Future<bool> loadAtlases(List<Uint8List> rawAtlases) { | ||
throw UnimplementedError(); | ||
} | ||
|
||
@override | ||
ColorFill makeColorFill() { | ||
throw UnimplementedError(); | ||
} | ||
|
||
@override | ||
ColorStroke makeColorStroke() { | ||
throw UnimplementedError(); | ||
} | ||
|
||
@override | ||
ActorDropShadow makeDropShadow() { | ||
throw UnimplementedError(); | ||
} | ||
|
||
@override | ||
GradientFill makeGradientFill() { | ||
throw UnimplementedError(); | ||
} | ||
|
||
@override | ||
GradientStroke makeGradientStroke() { | ||
throw UnimplementedError(); | ||
} | ||
|
||
@override | ||
ActorInnerShadow makeInnerShadow() { | ||
throw UnimplementedError(); | ||
} | ||
|
||
@override | ||
ActorLayerEffectRenderer makeLayerEffectRenderer() { | ||
throw UnimplementedError(); | ||
} | ||
|
||
@override | ||
RadialGradientFill makeRadialFill() { | ||
throw UnimplementedError(); | ||
} | ||
|
||
@override | ||
RadialGradientStroke makeRadialStroke() { | ||
throw UnimplementedError(); | ||
} | ||
|
||
@override | ||
Future<Uint8List> readOutOfBandAsset(String filename, context) { | ||
throw UnimplementedError(); | ||
} | ||
} | ||
|
||
String orderString(List<ActorComponent> order) { | ||
String output = order.fold( | ||
'', | ||
(previousValue, actorComponent) => | ||
previousValue + ' ' + actorComponent.name); | ||
return output.trim(); | ||
} | ||
|
||
void main() { | ||
group("Simple Cycle:", () { | ||
ActorArtboard artboard; | ||
final actor = DummyActor(); | ||
artboard = ActorArtboard(actor); | ||
final nodeA = ActorNode()..name = 'A'; | ||
final nodeB = ActorNode()..name = 'B'; | ||
final nodeC = ActorNode()..name = 'C'; | ||
final nodeD = ActorNode()..name = 'D'; | ||
|
||
/// | ||
/// [root] <- [A] <- [B] <- [D] | ||
/// A | A | ||
/// | +------+ | ||
/// [C] | ||
artboard.addDependency(nodeA, artboard.root); | ||
artboard.addDependency(nodeB, nodeA); | ||
artboard.addDependency(nodeC, nodeA); | ||
artboard.addDependency(nodeD, nodeB); | ||
artboard.addDependency(nodeB, nodeD); | ||
|
||
test("DependencySorter cannot order", () { | ||
expect(DependencySorter().sort(artboard.root), equals(null)); | ||
}); | ||
|
||
test("TarjansDependencySorter orders", () { | ||
var order = TarjansDependencySorter().sort(artboard.root); | ||
expect(order.length, equals(3)); | ||
expect(orderString(order), equals('Unnamed A C')); | ||
}); | ||
}); | ||
group("No cycle:", () { | ||
final actor = DummyActor(); | ||
final artboard = ActorArtboard(actor); | ||
final nodeA = ActorNode()..name = 'A'; | ||
final nodeB = ActorNode()..name = 'B'; | ||
final nodeC = ActorNode()..name = 'C'; | ||
final nodeD = ActorNode()..name = 'D'; | ||
|
||
/// | ||
/// [root] <- [A] <- [B] <- [D] | ||
/// A A | ||
/// | | | ||
/// [C]-----+ | ||
artboard.addDependency(nodeA, artboard.root); | ||
artboard.addDependency(nodeB, nodeA); | ||
artboard.addDependency(nodeC, nodeA); | ||
artboard.addDependency(nodeD, nodeB); | ||
artboard.addDependency(nodeC, nodeB); | ||
|
||
test("DependencySorter orders", () { | ||
var order = DependencySorter().sort(artboard.root); | ||
expect(order, isNotNull); | ||
expect(orderString(order), 'Unnamed A B C D'); | ||
}); | ||
|
||
test("DependencySorter orders", () { | ||
var order = TarjansDependencySorter().sort(artboard.root); | ||
expect(order, isNotNull); | ||
expect(orderString(order), 'Unnamed A B C D'); | ||
}); | ||
}); | ||
|
||
group("Complex Cycle A:", () { | ||
final actor = DummyActor(); | ||
final artboard = ActorArtboard(actor); | ||
final nodeA = ActorNode()..name = 'A'; | ||
final nodeB = ActorNode()..name = 'B'; | ||
final nodeC = ActorNode()..name = 'C'; | ||
final nodeD = ActorNode()..name = 'D'; | ||
|
||
/// | ||
/// +------+ | ||
/// | v | ||
/// [root] <- [A] <- [B] <- [D] | ||
/// A | | ||
/// | | | ||
/// [C]<-----------+ | ||
/// | ||
artboard.addDependency(nodeA, artboard.root); | ||
artboard.addDependency(nodeB, nodeA); | ||
artboard.addDependency(nodeD, nodeB); | ||
artboard.addDependency(nodeB, nodeD); | ||
artboard.addDependency(nodeC, nodeA); | ||
artboard.addDependency(nodeD, nodeC); | ||
|
||
test("DependencySorter cannot order", () { | ||
expect(DependencySorter().sort(artboard.root), equals(null)); | ||
}); | ||
|
||
test("TarjansDependencySorter orders", () { | ||
var order = TarjansDependencySorter().sort(artboard.root); | ||
expect(order, isNotNull); | ||
expect(orderString(order), equals('Unnamed A C')); | ||
}); | ||
}); | ||
|
||
group("Complex Cycle B, F is isolated:", () { | ||
ActorArtboard artboard; | ||
|
||
final actor = DummyActor(); | ||
artboard = ActorArtboard(actor); | ||
final nodeA = ActorNode()..name = 'A'; | ||
final nodeB = ActorNode()..name = 'B'; | ||
final nodeC = ActorNode()..name = 'C'; | ||
final nodeD = ActorNode()..name = 'D'; | ||
final nodeE = ActorNode()..name = 'E'; | ||
final nodeF = ActorNode()..name = 'F'; | ||
|
||
/// | ||
/// +-------------+ | ||
/// | | | ||
/// | [F] | | ||
/// | v v | ||
/// [root] <- [A] <- [B] <- [C] <- [D] | ||
/// A | | ||
/// | | | ||
/// [E]<-----------+ | ||
/// | ||
artboard.addDependency(nodeA, artboard.root); | ||
artboard.addDependency(nodeB, nodeA); | ||
artboard.addDependency(nodeC, nodeB); | ||
artboard.addDependency(nodeD, nodeC); | ||
artboard.addDependency(nodeB, nodeD); | ||
artboard.addDependency(nodeE, nodeA); | ||
artboard.addDependency(nodeC, nodeE); | ||
artboard.addDependency(nodeF, nodeC); | ||
|
||
test("TarjansDependencySorter orders", () { | ||
var dependencySorter = TarjansDependencySorter(); | ||
var order = dependencySorter.sort(artboard.root); | ||
expect(orderString(order), equals('Unnamed A E')); | ||
expect(dependencySorter.cycleNodes, containsAll([nodeB, nodeC, nodeD])); | ||
expect(dependencySorter.cycleNodes, contains(nodeF), | ||
skip: "Node F is isolated by a cycle, and does not " | ||
" exist in 'order' or in 'cycleNodes'"); | ||
}); | ||
}); | ||
|
||
group("Complex Cycle C, F is not isolated:", () { | ||
ActorArtboard artboard; | ||
|
||
final actor = DummyActor(); | ||
artboard = ActorArtboard(actor); | ||
final nodeA = ActorNode()..name = 'A'; | ||
final nodeB = ActorNode()..name = 'B'; | ||
final nodeC = ActorNode()..name = 'C'; | ||
final nodeD = ActorNode()..name = 'D'; | ||
final nodeE = ActorNode()..name = 'E'; | ||
final nodeF = ActorNode()..name = 'F'; | ||
|
||
/// | ||
/// +---------------+ | ||
/// | | | ||
/// | [F]---+ | | ||
/// | v | v | ||
/// [root] <- [A] <- [B] <- [C] <-+- [D] | ||
/// A | | | ||
/// | | | | ||
/// [E]<-----------+ | | ||
/// A | | ||
/// +------------------+ | ||
artboard.addDependency(nodeA, artboard.root); | ||
artboard.addDependency(nodeB, nodeA); | ||
artboard.addDependency(nodeC, nodeB); | ||
artboard.addDependency(nodeD, nodeC); | ||
artboard.addDependency(nodeB, nodeD); | ||
artboard.addDependency(nodeE, nodeA); | ||
artboard.addDependency(nodeC, nodeE); | ||
artboard.addDependency(nodeF, nodeC); | ||
artboard.addDependency(nodeF, nodeE); | ||
|
||
test("TarjansDependencySorter orders", () { | ||
var dependencySorter = TarjansDependencySorter(); | ||
var order = dependencySorter.sort(artboard.root); | ||
expect(orderString(order), equals('Unnamed A E F')); | ||
expect(dependencySorter.cycleNodes, containsAll([nodeB, nodeC, nodeD])); | ||
}); | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.