Permalink
Browse files

lots of cleanup - got rid of old DSLs

  • Loading branch information...
jkutner committed Jun 4, 2011
1 parent a3084a7 commit 09059e1312da46b3427307c0e5504bb936a3be84
Showing with 53 additions and 630 deletions.
  1. +7 −7 examples/ticket.rb
  2. +31 −33 lib/core/atoms.rb
  3. +13 −13 lib/core/nodes.rb
  4. +0 −238 lib/dsl/letigre.rb
  5. +0 −314 lib/dsl/steel.rb
  6. +2 −9 lib/rulebook.rb
  7. +0 −16 tests/duck_type.rb
View
@@ -45,8 +45,8 @@ def rules
# This is uses the letigre syntax... but we can mix and match syntaxes in
# the same rule set.
rule :New_Ticket, {:priority => 10}, # :duration => 10},
'Customer as :c',
'Ticket as :ticket where #customer == #:c #&& #status == :New' do |vars|
[Customer, :c],
[Ticket, :ticket, {m.customer => :c}, m.status == :New] do |vars|
puts 'New : ' + vars[:ticket].to_s
end
@@ -74,15 +74,15 @@ def rules
end
rule :Escalate,
'Customer as :c',
'Ticket as :ticket where #customer == #:c #&& #status == :Escalate' do |vars|
[Customer, :c],
[Ticket, :ticket, {m.customer => :c}, m.status == :Escalate] do |vars|
puts 'Email : ' + vars[:ticket].to_s
end
rule :Done,
'Customer as :c',
'Ticket as :ticket where #customer == #:c #&& #status == :Done' do |vars|
puts 'Done : ' + vars[:ticket].to_s
[Customer, :c],
[Ticket, :ticket, {m.customer => :c}, m.status == :Done] do |vars|
puts 'Done : ' + vars[:ticket].to_s
end
end
end
View
@@ -14,17 +14,17 @@ module Ruleby
module Core
class Atom
attr_reader :tag, :proc, :method, :deftemplate
attr_reader :tag, :proc, :method_name, :template
def initialize(tag, method, deftemplate, &block)
def initialize(tag, method_name, template, &block)
@tag = tag
@method = method
@deftemplate = deftemplate
@method_name = method_name
@template = template
@proc = Proc.new(&block) if block_given?
end
def to_s
return "#{self.class},#{@tag},#{@method},#{@deftemplate}"
"#{self.class},#{@tag},#{@method_name},#{@template}"
end
end
@@ -36,13 +36,13 @@ def to_s
# So there are no references to other atoms.
class PropertyAtom < Atom
def ==(atom)
return shareable?(atom) && @tag == atom.tag
shareable?(atom) && @tag == atom.tag
end
def shareable?(atom)
return PropertyAtom === atom &&
@method == atom.method &&
@deftemplate == atom.deftemplate &&
PropertyAtom === atom &&
@method_name == atom.method_name &&
@template == atom.template &&
@proc == atom.proc
end
end
@@ -53,30 +53,28 @@ class FunctionAtom < Atom
def initialize(tag, template, arguments, block)
@tag = tag
@method = nil
@deftemplate = template
@method_name = nil
@template = template
@arguments = arguments
@proc = block
end
def shareable?(atom)
FunctionAtom === atom &&
@deftemplate == atom.deftemplate &&
@template == atom.template &&
@arguments == atom.arguments &&
@proc == atom.proc
end
def to_s
return "#{self.class},#{@deftemplate},#{@arguments.inspect}"
"#{self.class},#{@template},#{@arguments.inspect}"
end
end
# TODO use this
class BlockAtom < PropertyAtom
def shareable?(atom)
return super &&
BlockAtom === atom &&
@proc == atom.proc
super && BlockAtom === atom && @proc == atom.proc
end
end
@@ -88,15 +86,15 @@ def shareable?(atom)
# So there are no references to other atoms.
class EqualsAtom < PropertyAtom
attr_reader :value
def initialize(tag, method, deftemplate, value)
super(tag,method,deftemplate)
def initialize(tag, method_name, template, value)
super(tag,method_name,template)
@value = value
end
def shareable?(atom)
return EqualsAtom === atom &&
@method == atom.method &&
@deftemplate == atom.deftemplate
EqualsAtom === atom &&
@method_name == atom.method_name &&
@template == atom.template
end
end
@@ -106,16 +104,16 @@ def shareable?(atom)
#
# It is only used at the start of a pattern.
class HeadAtom < Atom
def initialize(tag, deftemplate)
if deftemplate.mode == :equals
super tag, :class, deftemplate do |t| t == deftemplate.clazz end
elsif deftemplate.mode == :inherits
super tag, :class, deftemplate do |t| t === deftemplate.clazz end
def initialize(tag, template)
if template.mode == :equals
super tag, :class, template do |t| t == template.clazz end
elsif template.mode == :inherits
super tag, :class, template do |t| t === template.clazz end
end
end
def shareable?(atom)
return HeadAtom === atom && @deftemplate == atom.deftemplate
HeadAtom === atom && @template == atom.template
end
end
@@ -128,8 +126,8 @@ def shareable?(atom)
class ReferenceAtom < Atom
attr_reader :vars
def initialize(tag, method, vars, deftemplate, &block)
super(tag, method, deftemplate, &block)
def initialize(tag, method_name, vars, template, &block)
super(tag, method_name, template, &block)
@vars = vars # list of referenced variable names
end
@@ -138,21 +136,21 @@ def shareable?(atom)
end
def ==(atom)
return ReferenceAtom === atom &&
ReferenceAtom === atom &&
@proc == atom.proc &&
@tag == atom.tag &&
@vars == atom.vars &&
@deftemplate == atom.deftemplate
@template == atom.template
end
def to_s
return super + ", vars=#{vars.join(',')}"
super + ", vars=#{vars.join(',')}"
end
end
# This is an atom that references another atom that is in the same pattern.
# Note that in a SelfReferenceAtom, the 'vars' argument must be a list of the
# *methods* that this atom references (not the variable names)!
# *method_names* that this atom references (not the variable names)!
class SelfReferenceAtom < ReferenceAtom
end
View
@@ -370,15 +370,15 @@ def initialize(bucket, atom)
end
def ==(node)
return AtomNode === node && @atom == node.atom
AtomNode === node && @atom == node.atom
end
def shareable?(node)
return @atom.shareable?(node.atom)
@atom.shareable?(node.atom)
end
def to_s
super + " - #{@atom.method}"
super + " - #{@atom.method_name}"
end
end
@@ -406,7 +406,7 @@ def retract(assertable)
end
def assert(assertable)
k = assertable.fact.object.send(@atom.method)
k = assertable.fact.object.send(@atom.method_name)
# TODOwe need to do this for ALL tags if this node is shared
assertable.add_tag(@atom.tag, k)
@@ -424,15 +424,15 @@ def assert(assertable)
# is matched exactly (ignoring inheritance).
class TypeNode < HashedNode
def hash_by(atom)
atom.deftemplate.clazz
atom.template.clazz
end
def retract(fact)
propagate_retract fact, @values.values.inject({}){|p,c| p.merge(c)} #(@values[k] ? @values[k] : {})
end
def assert(fact)
k = fact.object.send(@atom.method)
k = fact.object.send(@atom.method_name)
# TODO we should create the Assertion object here, not in propogate
propagate_assert fact, (@values[k] ? @values[k] : {})
rescue NoMethodError => e
@@ -468,7 +468,7 @@ def assert(fact)
class PropertyNode < AtomNode
def assert(assertable)
begin
val = assertable.fact.object.send(@atom.method)
val = assertable.fact.object.send(@atom.method_name)
assertable.add_tag(@atom.tag, val)
rescue NoMethodError => e
@bucket.add_error Error.new(:no_method, :warn, {
@@ -483,7 +483,7 @@ def assert(assertable)
rescue Exception => e
@bucket.add_error Error.new(:proc_call, :error, {
:object => assertable.fact.object.to_s,
:method => @atom.method,
:method => @atom.method_name,
:value => val.to_s,
:message => e.message
})
@@ -500,7 +500,7 @@ def hash_by(atom)
end
end
# This node class is used for matching properties of a fact.
# This node class is used conditions that are simply a function, which returns true or false.
class FunctionNode < AtomNode
def assert(assertable)
begin
@@ -522,7 +522,7 @@ def assert(assertable)
# invoked by the two input node.
class ReferenceNode < AtomNode
def match(left_context,right_fact)
val = right_fact.object.send(@atom.method)
val = right_fact.object.send(@atom.method_name)
args = [val]
match = left_context.match
@atom.vars.each do |var|
@@ -558,7 +558,7 @@ def assert(assertable)
end
def match(fact)
args = [fact.object.send(@atom.method)]
args = [fact.object.send(@atom.method_name)]
@atom.vars.each do |var|
args.push fact.object.send(var)
end
@@ -596,11 +596,11 @@ def assert(assertable)
if atom == @pattern.head
# TODO once we fix up TypeNode, we won't need this
mr[atom.tag] = fact.object
elsif !mr.key?(atom.tag) and atom.method
elsif !mr.key?(atom.tag) and atom.method_name
# this is a big hack for the sake of performance. should really do whats described below
unless atom.tag.is_a?(GeneratedTag)
# TODO it should be possible to get rid of this, and just capture it in the Nodes above
mr[atom.tag] = fact.object.send(atom.method)
mr[atom.tag] = fact.object.send(atom.method_name)
end
end
end
Oops, something went wrong.

0 comments on commit 09059e1

Please sign in to comment.