class CLIPS

Attributes

activations[R]

@return [Set] The currently activated Facts awaiting processing

agenda[R]

@return [Set] The Rules waiting to be processed

default_facts[R]

@return [Hash] The set of facts that will be instantiated on each reset

facts[R]

@return [Set] Just the Facts

rules[R]

@return [Hash] The set of Rules and their names

Public Class Methods

new() click to toggle source
# File lib/clips.rb, line 21
def initialize
    @activations = Set.new
    @agenda = Set.new
    @default_facts = Hash.new
    @facts = Set.new
    @rules = Hash.new
end

Public Instance Methods

add(*fields) click to toggle source

@overload add(name, rule)

Add the named {Rule}
@param [String] name    The name of the {Rule}
@param [Rule]   rule      The {Rule} to add
@return [CLIPS]

@overload add(*fields)

Add a fact using the given field values
@return [CLIPS]
# File lib/clips.rb, line 37
def add(*fields)
    if (2==fields.length) and fields.last&.is_a?(Rule)
        self.rules[fields.first.to_sym] ||= fields.last
    else   # Assume the item to be a Fact
        fields[0] = fields.first.to_sym
        # Add the new Fact to the activations list for processing during the next call to run()
        self.activations.add(fields)
        self.facts.add(fields)
        fields
    # else
    #     raise ArgumentError.new("Invalid fact")
    end
    self
end
clear() click to toggle source

Remove all Facts and return self @return [CLIPS]

# File lib/clips.rb, line 54
def clear
    self.activations.clear
    self.agenda.clear
    self.default_facts.clear
    self.facts.clear
    self.rules.clear
    self
end
delete(*fact) click to toggle source

Remove the given fact @return [CLIPS]

# File lib/clips.rb, line 65
def delete(*fact)
    fact[0] = fact.first.to_sym
    if self.facts.delete?(fact)
        self.activations.delete(fact)
    end
    self
end
reset() click to toggle source

Delete everything but the rules, then add the default facts @return [CLIPS] self

# File lib/clips.rb, line 75
def reset
    self.activations.clear
    self.agenda.clear
    self.facts.clear

    self.default_facts.each do |name, fact_set|
        fact_set.each {|fact| self.add(*fact)}
    end

    self
end
run(limit=nil) click to toggle source

@param [Integer] limit The maximum number of rules that will fire before the function returns. The limit is disabled if nil. @return [Integer] The number of rules that were fired

# File lib/clips.rb, line 89
def run(limit=nil)
    self.rules.each do |name, rule|
        self.agenda.add(rule) if rule.patterns.empty?
    end

    rule_counter = 0
    begin
        # If all of a Rule's patterns match, and any of those patterns are on the activation list, then fire the Rule
        self.rules.each do |name, rule|
            activated = false
            _all = rule.patterns.all? do |pattern|
                activated = true if not activated and self.activations.include?(pattern)
                self.facts.include?(pattern)
            end
            self.agenda.add(rule) if activated and _all
        end

        self.activations.clear     # All activations have been processed, so clear the Set

        # Fire all of the Rules on the agenda in salience-order
        sorted_agenda = self.agenda.sort {|a,b| b.salience <=> a.salience}
        self.agenda.clear
        sorted_agenda.each do |rule|
            rule.run!(self)
            rule_counter += 1

            break if rule_counter == limit
        end
    end until self.agenda.empty? and self.activations.empty?

    rule_counter
end