Class: Wongi::Engine::GenericProductionRule

Inherits:
Object
  • Object
show all
Includes:
DSLExtensions
Defined in:
lib/wongi-engine/dsl/generic_production_rule.rb

Direct Known Subclasses

NccProductionRule, ProductionRule, Query, VariantRule

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSLExtensions

create_extension

Constructor Details

#initialize(name) ⇒ GenericProductionRule

Returns a new instance of GenericProductionRule.



31
32
33
34
35
36
37
38
# File 'lib/wongi-engine/dsl/generic_production_rule.rb', line 31

def initialize name
  @name = name
  @conditions = []
  @actions = []
  @current_section = nil
  @acceptors = {}
  GenericProductionRule.sections.each { |section| @acceptors[section] ||= [] }
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



5
6
7
# File 'lib/wongi-engine/dsl/generic_production_rule.rb', line 5

def actions
  @actions
end

#conditionsObject

Returns the value of attribute conditions.



5
6
7
# File 'lib/wongi-engine/dsl/generic_production_rule.rb', line 5

def conditions
  @conditions
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/wongi-engine/dsl/generic_production_rule.rb', line 4

def name
  @name
end

Class Method Details

.section(s, *aliases) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/wongi-engine/dsl/generic_production_rule.rb', line 11

def section s, *aliases
  unless sections.include?(s)
    sections << s
    define_method s do |&d|
      @current_section = s
      instance_eval &d
    end
    aliases.each { |a| alias_method a, s }
  end
end

.sectionsObject



22
23
24
# File 'lib/wongi-engine/dsl/generic_production_rule.rb', line 22

def sections
  @sections ||= []
end

Instance Method Details

#import_into(rete) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/wongi-engine/dsl/generic_production_rule.rb', line 40

def import_into rete

  copy = self.class.new @name

  copy.conditions = @acceptors[:forall].map do |condition|
    if condition.respond_to? :import_into
      condition.import_into(rete)
    else
      condition
    end
  end

  copy.actions = @acceptors[:make].map do |action|
    if action.respond_to? :import_into
      action.import_into(rete)
    else
      action
    end
  end

  copy
rescue Exception => e
  e1 = Exception.new "in rule #{name}: #{e}"
  e1.set_backtrace e.backtrace
  raise e1
end