Skip to content

Instantly share code, notes, and snippets.

@bibstha
Created February 20, 2015 09:29
Show Gist options
  • Save bibstha/848b083eecc5cd33add6 to your computer and use it in GitHub Desktop.
Save bibstha/848b083eecc5cd33add6 to your computer and use it in GitHub Desktop.
require 'ruleby'
include Ruleby
class CoffeeOperationRulebook < Rulebook
# Note the rules do not have to be in order
def rules
rule [Machine, :m, m.status == :GRINDING] do |v, e|
puts "Grinding complete, puring coffee"
v[:m].status = :POURING
e.modify v[:m]
end
rule [Machine, :m, m.status == :READY] do |v, e|
puts "Machine Ready, griding coffee"
v[:m].status = :GRINDING
e.modify v[:m]
end
rule [Machine, :m, m.status == :POURING] do |v, e|
puts "Pouring complete, stop machine"
v[:m].status = :READY
end
end
end
class Machine
attr_accessor :status
end
engine :coffee_operation do |e|
m = Machine.new.tap { |m| m.status = :READY }
CoffeeOperationRulebook.new(e).rules
e.assert m
e.match
end%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment