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

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

Issue 21001003: test.py: First step towards support of caching dart2js compilations across runtimes (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 4 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
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
ricow1 2013/08/01 18:23:52 Since this is general purpose it would be benefici
kustermann 2013/08/05 07:29:17 Done.
10 class Graph {
11 final _nodes = new Set<Node>();
12 final _eventController = new StreamController<GraphEvent>();
13 final _stateCounts = new Map<NodeState, int>();
14 var _eventStream;
15 bool _isSealed = false;
16
17 Graph() {
18 _eventStream = _eventController.stream.asBroadcastStream();
19 }
20
21 Iterable<Node> get nodes => _nodes;
22 Stream<GraphEvent> get events => _eventStream;
23 bool get isSealed => _isSealed;
24
25 int stateCount(NodeState state) {
26 int count = _stateCounts[state];
27 return count == null ? 0 : count;
28 }
29
30 void DumpCounts() {
31 for (var state in _stateCounts.keys) {
32 print("Count[$state] = ${_stateCounts[state]}");
33 }
34 }
35
36 void sealGraph() {
37 assert(!_isSealed);
38 _isSealed = true;
39 _emitEvent(new GraphSealedEvent());
40 }
41
42 Node newNode(Object userData, Iterable<Node> dependencies) {
43 assert(!_isSealed);
44
45 var node = new Node._(userData);
46 _nodes.add(node);
47
48 for (var dependency in dependencies) {
49 dependency._neededFor.add(node);
50 node._dependencies.add(dependency);
51 }
52
53 _emitEvent(new NodeAddedEvent(node));
54
55 _stateCounts.putIfAbsent(node.state, () => 0);
56 _stateCounts[node.state] += 1;
57
58 return node;
59 }
60
61 void changeState(Node node, NodeState newState) {
62 var fromState = node.state;
63 node._state = newState;
64
65 _stateCounts[fromState] -= 1;
66 _stateCounts.putIfAbsent(newState, () => 0);
67 _stateCounts[newState] += 1;
68
69 _emitEvent(new StateChangedEvent(node, fromState, newState));
70 }
71
72 _emitEvent(GraphEvent event) {
73 // We emit events asynchronously so the graph can be build up in a small
ricow1 2013/08/01 18:23:52 up in a small -> in small
kustermann 2013/08/05 07:29:17 Done.
74 // batches and the events are delivered in small batches.
75 Timer.run(() {
76 _eventController.add(event);
77 });
78 }
79 }
80
81 class Node extends UniqueObject {
82 final Object _userData;
83 NodeState _state = NodeState.Initialized;
84 Set<Node> _dependencies = new Set<Node>();
85 Set<Node> _neededFor = new Set<Node>();
86
87 Node._(this._userData);
88
89 Object get userData => _userData;
90 NodeState get state => _state;
91 Iterable<Node> get dependencies => _dependencies;
92 Iterable<Node> get neededFor => _neededFor;
93 }
94
95 class NodeState extends UniqueObject {
96 static NodeState Initialized = new NodeState._("Initialized");
97 static NodeState Waiting = new NodeState._("Waiting");
98 static NodeState Enqueuing = new NodeState._("Enqueuing");
99 static NodeState Processing = new NodeState._("Running");
100 static NodeState Successful = new NodeState._("Successful");
101 static NodeState Failed = new NodeState._("Failed");
102 static NodeState UnableToRun = new NodeState._("UnableToRun");
103
104 final String name;
105
106 NodeState._(this.name);
107
108 String toString() => name;
109 }
110
111 abstract class GraphEvent {}
112
113 class GraphSealedEvent extends GraphEvent {}
114
115 class NodeAddedEvent extends GraphEvent {
116 final Node node;
117
118 NodeAddedEvent(this.node);
119 }
120
121 class StateChangedEvent extends GraphEvent {
122 final Node node;
123 final NodeState from;
124 final NodeState to;
125
126 StateChangedEvent(this.node, this.from, this.to);
127 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698