Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # initialize a decider class
- decider = Undecided::Decider.new
- # Call decide method passing an string as a boolean expression
- # and a hash with values (values must be boolean type or (1 or 0)
- expression = 'A&B'
- values = { A:1, B:1 }
- decider.decide(expression, values)
- # Return true cuz A&B means that (A==true & B==true)
- class ToDo
- # A to do list should have an array of tasks
- attr_reader :tasks, :name
- # let's define our tasks
- def initialize(name, tasks)
- @name = name
- @tasks = tasks
- end
- # o we can simply add more task to our array
- def add_task(task)
- @tasks << task
- end
- # Retrieve our task list as a hash
- def task_list
- @tasks.map { |task| [task.name, task.completed] }.to_h
- end
- end
- # task class that has two attributes, name and completed
- # it will behave like a hash with Key (name) and Value (completed?)
- class Task
- attr_accessor :name, :completed
- def initialize(name, completed)
- @name = name
- @completed = completed
- end
- end
- # Rule class that has a boolean expression and a to do model that will return
- # if the evaluation is positive
- class Rule
- attr_reader :expression, :to_do
- def initialize(expression, to_do)
- @expression = expression
- @to_do = to_do
- end
- end
- class Flow
- attr_accessor :current_todo
- def initialize(rules)
- @rules = rules
- # initialize a decider class
- @decider = Undecided::Decider.new
- end
- def start(to_do)
- @current_todo = to_do.nil? ? next_to_do : to_do
- end
- # Iterate every rule to match the true one
- def next_to_do
- @rules.each do |rule|
- next if !next?(rule)
- @current_todo = rule.to_do
- break
- end
- # return the same if none of the rules are true
- end
- # check if the rule is met
- def next?(rule)
- puts rule.expression
- puts @current_todo.task_list
- @decider.decide(rule.expression, @current_todo.task_list, false)
- end
- end
- # let's define our clases
- # tasks
- task_a = Task.new(:task_a, false)
- task_b = Task.new(:task_b, true)
- task_c = Task.new(:task_c, false)
- task_d = Task.new(:task_d, true)
- task_e = Task.new(:task_e, false)
- # todos
- todo_a = ToDo.new('todo_a', [task_a, task_d])
- todo_b = ToDo.new('todo_b', [task_a, task_b, task_e])
- todo_c = ToDo.new('todo_c', [task_c, task_d])
- todo_d = ToDo.new('todo_d', [task_e, task_b])
- # rules
- rules = [
- Rule.new('!a&!b', todo_b),
- Rule.new('a&b&c!d|(d|c)', todo_c),
- Rule.new('!a&!c', todo_d),
- Rule.new('b&e', todo_b)
- ]
- # flow
- flow = Flow.new(rules)
- # let's start with todo_a
- flow.start todo_a
- # #<ToDo:0x00000001c61118 @tasks=[#<Task:0x00000001a2d9c8 @name=:a, @completed=false>, #<Task:0x000000023d63f8 @name=:d, @completed=true>], @name="todo_a">
- # Now let's move to another to do list
- # 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
- flow.next_to_do
- ##<ToDo:0x0000000215b7e0 @tasks=[#<Task:0x00000002265550 @name=:a, @completed=false>, #<Task:0x00000002247b68 @name=:b, @completed=true>, #<Task:0x000000021c4998 @name=:e, @completed=false>], @name="todo_b">
- # if we check the new todo list
- flow.current_todo.name
- # 'todo_b'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement