Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Side by Side Diff: tools/testing/dart/dependency_graph.dart

Issue 841193003: cleanup to tools/testing/dart (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: one last bit Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/testing/dart/compiler_configuration.dart ('k') | tools/testing/dart/drt_updater.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library dependency_graph;
6
7 import 'dart:async';
8 import 'utils.dart';
9
10
11 /*
12 * [Graph] represents a datastructure for representing an DAG (directed acyclic
13 * graph). Each node in the graph is in a given [NodeState] and can have data
14 * attachted to it with [Node.userData].
15 *
16 * It's interface consists basically of these methods:
17 * - newNode: Adds a new node to the graph with the given dependencies and
18 * the given user data. The node is in the [NodeState.Initialized]
19 * state.
20 * - changeState: Changes the state of a node.
21 * - sealGraph: Makes the graph immutable.
22 * - stateCount: Counts the number of nodes who are in a given [NodeState].
23 *
24 * Users of a [Graph] can listen for events by subscribing to the [events]
25 * stream. Three types of events will be fired (after the graph was modified):
26 * - NodeAddedEvent: Fired after a node was added ot the graph.
27 * - StateChangedEvent: Fired after the state of a node changed.
28 * - GraphSealedEvent: Fired after the graph was marked as immutable/sealed.
29 */
30 class Graph {
31 final _nodes = new Set<Node>();
32 final _eventController = new StreamController<GraphEvent>();
33 final _stateCounts = new Map<NodeState, int>();
34 var _eventStream;
35 bool _isSealed = false;
36
37 Graph() {
38 _eventStream = _eventController.stream.asBroadcastStream();
39 }
40
41 Iterable<Node> get nodes => _nodes;
42 Stream<GraphEvent> get events => _eventStream;
43 bool get isSealed => _isSealed;
44
45 int stateCount(NodeState state) {
46 int count = _stateCounts[state];
47 return count == null ? 0 : count;
48 }
49
50 void DumpCounts() {
51 for (var state in _stateCounts.keys) {
52 print("Count[$state] = ${_stateCounts[state]}");
53 }
54 }
55
56 void sealGraph() {
57 assert(!_isSealed);
58 _isSealed = true;
59 _emitEvent(new GraphSealedEvent());
60 }
61
62 Node newNode(Object userData, Iterable<Node> dependencies) {
63 assert(!_isSealed);
64
65 var node = new Node._(userData);
66 _nodes.add(node);
67
68 for (var dependency in dependencies) {
69 dependency._neededFor.add(node);
70 node._dependencies.add(dependency);
71 }
72
73 _emitEvent(new NodeAddedEvent(node));
74
75 _stateCounts.putIfAbsent(node.state, () => 0);
76 _stateCounts[node.state] += 1;
77
78 return node;
79 }
80
81 void changeState(Node node, NodeState newState) {
82 var fromState = node.state;
83 node._state = newState;
84
85 _stateCounts[fromState] -= 1;
86 _stateCounts.putIfAbsent(newState, () => 0);
87 _stateCounts[newState] += 1;
88
89 _emitEvent(new StateChangedEvent(node, fromState, newState));
90 }
91
92 _emitEvent(GraphEvent event) {
93 // We emit events asynchronously so the graph can be build up in small
94 // batches and the events are delivered in small batches.
95 Timer.run(() {
96 _eventController.add(event);
97 });
98 }
99 }
100
101 class Node extends UniqueObject {
102 final Object _userData;
103 NodeState _state = NodeState.Initialized;
104 Set<Node> _dependencies = new Set<Node>();
105 Set<Node> _neededFor = new Set<Node>();
106
107 Node._(this._userData);
108
109 Object get userData => _userData;
110 NodeState get state => _state;
111 Iterable<Node> get dependencies => _dependencies;
112 Iterable<Node> get neededFor => _neededFor;
113 }
114
115 class NodeState extends UniqueObject {
116 static NodeState Initialized = new NodeState._("Initialized");
117 static NodeState Waiting = new NodeState._("Waiting");
118 static NodeState Enqueuing = new NodeState._("Enqueuing");
119 static NodeState Processing = new NodeState._("Running");
120 static NodeState Successful = new NodeState._("Successful");
121 static NodeState Failed = new NodeState._("Failed");
122 static NodeState UnableToRun = new NodeState._("UnableToRun");
123
124 final String name;
125
126 NodeState._(this.name);
127
128 String toString() => name;
129 }
130
131 abstract class GraphEvent {}
132
133 class GraphSealedEvent extends GraphEvent {}
134
135 class NodeAddedEvent extends GraphEvent {
136 final Node node;
137
138 NodeAddedEvent(this.node);
139 }
140
141 class StateChangedEvent extends GraphEvent {
142 final Node node;
143 final NodeState from;
144 final NodeState to;
145
146 StateChangedEvent(this.node, this.from, this.to);
147 }
OLDNEW
« no previous file with comments | « tools/testing/dart/compiler_configuration.dart ('k') | tools/testing/dart/drt_updater.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698