Class: KBS::DSL::RuleBuilder

Inherits:
Object
  • Object
show all
Includes:
ConditionHelpers
Defined in:
lib/kbs/dsl/rule_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ConditionHelpers

#any, #between, #equals, #greater_than, #less_than, #not_equal, #one_of, #range, #satisfies

Constructor Details

#initialize(name) ⇒ RuleBuilder



10
11
12
13
14
15
16
17
18
# File 'lib/kbs/dsl/rule_builder.rb', line 10

def initialize(name)
  @name = name
  @description = nil
  @priority = 0
  @conditions = []
  @action_block = nil
  @current_condition_group = []
  @negated = false
end

Instance Attribute Details

#action_blockObject (readonly)

Returns the value of attribute action_block.



8
9
10
# File 'lib/kbs/dsl/rule_builder.rb', line 8

def action_block
  @action_block
end

#conditionsObject (readonly)

Returns the value of attribute conditions.



8
9
10
# File 'lib/kbs/dsl/rule_builder.rb', line 8

def conditions
  @conditions
end

#descriptionObject (readonly)

Returns the value of attribute description.



8
9
10
# File 'lib/kbs/dsl/rule_builder.rb', line 8

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/kbs/dsl/rule_builder.rb', line 8

def name
  @name
end

Instance Method Details

#absent(type, pattern = {}, &block) ⇒ Object



75
76
77
# File 'lib/kbs/dsl/rule_builder.rb', line 75

def absent(type, pattern = {}, &block)
  without.on(type, pattern, &block)
end

#action(&block) ⇒ Object



87
88
89
# File 'lib/kbs/dsl/rule_builder.rb', line 87

def action(&block)
  perform(&block)
end

#buildObject



99
100
101
102
103
104
# File 'lib/kbs/dsl/rule_builder.rb', line 99

def build
  Rule.new(@name,
    conditions: @conditions,
    action: @action_block,
    priority: @priority)
end

#desc(description) ⇒ Object



20
21
22
23
# File 'lib/kbs/dsl/rule_builder.rb', line 20

def desc(description)
  @description = description
  self
end

#execute(&block) ⇒ Object



91
92
93
# File 'lib/kbs/dsl/rule_builder.rb', line 91

def execute(&block)
  perform(&block)
end

#exists(type, pattern = {}, &block) ⇒ Object



71
72
73
# File 'lib/kbs/dsl/rule_builder.rb', line 71

def exists(type, pattern = {}, &block)
  on(type, pattern, &block)
end

#fact(type, pattern = {}, &block) ⇒ Object



67
68
69
# File 'lib/kbs/dsl/rule_builder.rb', line 67

def fact(type, pattern = {}, &block)
  on(type, pattern, &block)
end

#given(type, pattern = {}, &block) ⇒ Object

Aliases for readability



59
60
61
# File 'lib/kbs/dsl/rule_builder.rb', line 59

def given(type, pattern = {}, &block)
  on(type, pattern, &block)
end

#lacks(type, pattern = {}, &block) ⇒ Object



83
84
85
# File 'lib/kbs/dsl/rule_builder.rb', line 83

def lacks(type, pattern = {}, &block)
  without.on(type, pattern, &block)
end

#matches(type, pattern = {}, &block) ⇒ Object



63
64
65
# File 'lib/kbs/dsl/rule_builder.rb', line 63

def matches(type, pattern = {}, &block)
  on(type, pattern, &block)
end

#missing(type, pattern = {}, &block) ⇒ Object



79
80
81
# File 'lib/kbs/dsl/rule_builder.rb', line 79

def missing(type, pattern = {}, &block)
  without.on(type, pattern, &block)
end

#on(type, pattern = {}, &block) ⇒ Object

Primary DSL keywords (avoid Ruby reserved words)



32
33
34
35
36
37
38
39
# File 'lib/kbs/dsl/rule_builder.rb', line 32

def on(type, pattern = {}, &block)
  if block_given?
    pattern = pattern.merge(evaluate_block(&block))
  end
  @conditions << Condition.new(type, pattern, negated: @negated)
  @negated = false
  self
end

#perform(&block) ⇒ Object



53
54
55
56
# File 'lib/kbs/dsl/rule_builder.rb', line 53

def perform(&block)
  @action_block = block
  self
end

#priority(level = nil) ⇒ Object



25
26
27
28
29
# File 'lib/kbs/dsl/rule_builder.rb', line 25

def priority(level = nil)
  return @priority if level.nil?
  @priority = level
  self
end

#then(&block) ⇒ Object



95
96
97
# File 'lib/kbs/dsl/rule_builder.rb', line 95

def then(&block)
  perform(&block)
end

#without(type = nil, pattern = {}, &block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/kbs/dsl/rule_builder.rb', line 41

def without(type = nil, pattern = {}, &block)
  if type
    # Direct negation: without(:problem)
    @negated = true
    on(type, pattern, &block)
  else
    # Chaining: without.on(:problem)
    @negated = true
    self
  end
end