-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleaner_adi.dart
176 lines (151 loc) · 4.38 KB
/
cleaner_adi.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import 'dart:io';
/*
Encapsulates the data model for the program.
*/
class ADIGraph {
List<int> _relations = []; //Stores relationships between nodes as Adjacency Descriptive Integers.
List<String> _data = []; //Stores the data about each node.
bool isInGraph({input: String}) => _data.contains(input);
int nodeIndex({input: String}) => _data.indexOf(input);
/*
Add a node to the graph.
*/
void addNode({name: String}) {
if (isInGraph(input: name)) {
throw Exception;
}
else {
_relations.add(0);
_data.add(name);
};
}
/*
Connect a node with and a list of other nodes.
If the connecting nodes are not yet in the graph,
add them to the end of the container.
*/
void connectNodes({data: String, nodes: List}) {
//If the subject data is not in the graph, add it.
if (!isInGraph(input: data)) {
stdout.writeln('$data not found, adding $data to graph.');
this.addNode(name: data);
};
//Make the connection using the magic of ADIs.
for (int i = 0; i < nodes.length; i++) {
int n = nodeIndex(input: data);
if (isInGraph(input: nodes[i])) {
int f = nodeIndex(input: nodes[i]);
_relations[n] |= (1 << f);
} else {
this.addNode(name: nodes[i]);
int f = nodeIndex(input: nodes[i]);
_relations[n] |= (1 << f);
};
};
}
/*
Print all nodes in the graph, in order of addition.
*/
void printNodes(){
for (String datum in _data){
stdout.writeln(datum);
};
}
/*
List nodes that share an edge with a given node.
*/
void listConnections({node: String}) {
if(isInGraph(input: node)) {
int r = _relations[nodeIndex(input: node)];
for (int i = 0; i < _data.length; i++) {
if ((r & (1 << i)) != 0) {
stdout.writeln(_data[i]);
};
};
stdout.writeln();
} else {
throw Exception;
};
}
} // END class ADIGraph
/*
Encapsulates the controller of the program.
*/
class Commands {
void printPeople({graph: ADIGraph}){
graph.printNodes();
}
void addToGraph({graph: ADIGraph}){
String candidate;
stdout.write('\nWho would you like to add to the graph?\nName: ');
candidate = stdin.readLineSync();
try{
graph.addNode(name: candidate);
} catch (e) {
stdout.write('$candidate is already a graph member.\n');
};
}
void addRelationships({graph: ADIGraph}){
stdout.write('\nWhose friends are these? ');
String subject = stdin.readLineSync();
stdout.write('Please enter a friend\'s name: ');
List<String> group = [];
String friend = stdin.readLineSync();
group.add(friend);
bool flag = true;
while (flag) {
stdout.write('Enter the next friend\'s name (press Enter to finish): ');
friend = stdin.readLineSync();
if (friend.isNotEmpty) {
group.add(friend);
} else {
flag = false;
};
};
graph.connectNodes(data: subject, nodes: group);
stdout.write('Done!\n');
}
void printFriends({graph: ADIGraph}) {
stdout.writeln('\nWhose friends do you want to see? ');
String subject = stdin.readLineSync();
try {
stdout.writeln('$subject\'s friends are:');
graph.listConnections(node: subject);
} catch (e) {
stdout.writeln('Error: $subject is not part of the graph.');
};
}
void showMenu(){
stdout.write('\n\'a\': Add someone to the graph. \n\'c\': Connect friends to graph member. \n\'f\': Print someone\'s friend list. \n\'p\': Print graph members. \n\'m\': View this menu. \n\'q\': Quit the program.\n\n');
}
} //END class Commands
void RunProgram({input: Object}){
Commands menu = new Commands();
String command;
while (command != 'q') {
stdout.writeln('\n'+('-'*60));
stdout.write('Welcome to the Social Graph. What would you like to do? \n(enter \'m\' to view the menu and \'q\' to quit): ');
command = stdin.readLineSync();
switch (command) {
case 'a':
menu.addToGraph(graph: input);
break;
case 'c':
menu.addRelationships(graph: input);
break;
case 'f':
menu.printFriends(graph: input);
break;
case 'm':
menu.showMenu();
break;
case 'p':
menu.printPeople(graph: input);
break;
};
};
}
void main() {
ADIGraph social_graph = new ADIGraph();
RunProgram(input: social_graph);
}