Advertisement
Guest User

Untitled

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