Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. # initialize a decider class
  2. decider = Undecided::Decider.new
  3.  
  4. # Call decide method passing an string as a boolean expression
  5. # and a hash with values (values must be boolean type or (1 or 0)
  6.  
  7. expression = 'A&B'
  8. values = { A:1, B:1 }
  9. decider.decide(expression, values)
  10. # Return true cuz A&B means that (A==true & B==true)
  11.  
  12.  
  13. class ToDo
  14. # A to do list should have an array of tasks
  15. attr_reader :tasks, :name
  16.  
  17. # let's define our tasks
  18. def initialize(name, tasks)
  19. @name = name
  20. @tasks = tasks
  21. end
  22.  
  23. # o we can simply add more task to our array
  24. def add_task(task)
  25. @tasks << task
  26. end
  27.  
  28. # Retrieve our task list as a hash
  29. def task_list
  30. @tasks.map { |task| [task.name, task.completed] }.to_h
  31. end
  32. end
  33.  
  34. # task class that has two attributes, name and completed
  35. # it will behave like a hash with Key (name) and Value (completed?)
  36. class Task
  37. attr_accessor :name, :completed
  38.  
  39. def initialize(name, completed)
  40. @name = name
  41. @completed = completed
  42. end
  43. end
  44.  
  45. # Rule class that has a boolean expression and a to do model that will return
  46. # if the evaluation is positive
  47. class Rule
  48. attr_reader :expression, :to_do
  49.  
  50. def initialize(expression, to_do)
  51. @expression = expression
  52. @to_do = to_do
  53. end
  54. end
  55.  
  56. class Flow
  57. attr_accessor :current_todo
  58. def initialize(rules)
  59. @rules = rules
  60. # initialize a decider class
  61. @decider = Undecided::Decider.new
  62. end
  63.  
  64. def start(to_do)
  65. @current_todo = to_do.nil? ? next_to_do : to_do
  66. end
  67.  
  68. # Iterate every rule to match the true one
  69. def next_to_do
  70. @rules.each do |rule|
  71. next if !next?(rule)
  72. @current_todo = rule.to_do
  73. break
  74. end
  75. # return the same if none of the rules are true
  76. end
  77.  
  78. # check if the rule is met
  79. def next?(rule)
  80. puts rule.expression
  81. puts @current_todo.task_list
  82. @decider.decide(rule.expression, @current_todo.task_list, false)
  83. end
  84. end
  85.  
  86.  
  87. # let's define our clases
  88.  
  89. # tasks
  90. task_a = Task.new(:task_a, false)
  91. task_b = Task.new(:task_b, true)
  92. task_c = Task.new(:task_c, false)
  93. task_d = Task.new(:task_d, true)
  94. task_e = Task.new(:task_e, false)
  95.  
  96. # todos
  97. todo_a = ToDo.new('todo_a', [task_a, task_d])
  98. todo_b = ToDo.new('todo_b', [task_a, task_b, task_e])
  99. todo_c = ToDo.new('todo_c', [task_c, task_d])
  100. todo_d = ToDo.new('todo_d', [task_e, task_b])
  101.  
  102. # rules
  103. rules = [
  104. Rule.new('!a&!b', todo_b),
  105. Rule.new('a&b&c!d|(d|c)', todo_c),
  106. Rule.new('!a&!c', todo_d),
  107. Rule.new('b&e', todo_b)
  108. ]
  109.  
  110. # flow
  111. flow = Flow.new(rules)
  112.  
  113. # let's start with todo_a
  114.  
  115. flow.start todo_a
  116. # #<ToDo:0x00000001c61118 @tasks=[#<Task:0x00000001a2d9c8 @name=:a, @completed=false>, #<Task:0x000000023d63f8 @name=:d, @completed=true>], @name="todo_a">
  117.  
  118. # Now let's move to another to do list
  119. # check that the first rule should be positive cuz todo_a hasn't a 'd' task, so it will be false, and the 'a' task is false
  120.  
  121. flow.next_to_do
  122. ##<ToDo:0x0000000215b7e0 @tasks=[#<Task:0x00000002265550 @name=:a, @completed=false>, #<Task:0x00000002247b68 @name=:b, @completed=true>, #<Task:0x000000021c4998 @name=:e, @completed=false>], @name="todo_b">
  123.  
  124. # if we check the new todo list
  125. flow.current_todo.name
  126. # 'todo_b'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement