class Wongi::Engine::Network
Attributes
alpha_hash[RW]
alpha_top[RW]
beta_top[RW]
overlays[R]
productions[R]
queries[RW]
results[RW]
Public Class Methods
new()
click to toggle source
# File lib/wongi-engine/network.rb, line 45 def initialize @overlays = [base_overlay] self.alpha_top = AlphaMemory.new(Template.new(:_, :_, :_), self) self.alpha_hash = { alpha_top.template => alpha_top } self.beta_top = RootNode.new(nil) beta_top.rete = self beta_top.seed self.queries = {} self.results = {} @revns = {} @productions = {} @collectors = {} @collectors[:error] = [] end
statements()
click to toggle source
# File lib/wongi-engine/network.rb, line 34 def statements alpha_top.wmes end
Public Instance Methods
<<(something)
click to toggle source
# File lib/wongi-engine/network.rb, line 155 def <<(something) if something.respond_to?(:install) something.install(self) else case something when Array assert(WME.new(*something)) when WME assert something # when Wongi::RDF::Statement # assert WME.new( something.subject, something.predicate, something.object, self ) # when Wongi::RDF::Document # something.statements.each do |st| # assert WME.new( st.subject, st.predicate, st.object, self ) # end when Network something.wmes.each { |wme| assert(wme) } else raise Error, "I don't know how to accept a #{something.class}" end self end end
alphas()
click to toggle source
# File lib/wongi-engine/network.rb, line 75 def alphas alpha_hash.values end
assert(wme)
click to toggle source
# File lib/wongi-engine/network.rb, line 116 def assert(wme) default_overlay.assert(wme) end
base_overlay()
click to toggle source
def import thing
case thing when String, Numeric, TrueClass, FalseClass, NilClass, Wongi::RDF::Node thing when Symbol thing else thing end
end
# File lib/wongi-engine/network.rb, line 90 def base_overlay @base_overlay ||= Overlay.new(self) end
Also aliased as: default_overlay
cache(s, p, o)
click to toggle source
# File lib/wongi-engine/network.rb, line 209 def cache(s, p, o) compile_alpha Template.new(s, p, o) end
compile_alpha(condition)
click to toggle source
# File lib/wongi-engine/network.rb, line 191 def compile_alpha(condition) template = Template.new :_, :_, :_ template.subject = condition.subject unless Template.variable?(condition.subject) template.predicate = condition.predicate unless Template.variable?(condition.predicate) template.object = condition.object unless Template.variable?(condition.object) # puts "COMPILED CONDITION #{condition} WITH KEY #{key}" return alpha_hash[template] if alpha_hash.key?(template) alpha = AlphaMemory.new(template, self) alpha_hash[template] = alpha initial_fill alpha alpha end
current_overlay()
click to toggle source
@private
# File lib/wongi-engine/network.rb, line 108 def current_overlay overlays.last end
debug!()
click to toggle source
# File lib/wongi-engine/network.rb, line 18 def debug! extend NetworkParts::Debug end
dump()
click to toggle source
# File lib/wongi-engine/network.rb, line 63 def dump beta_top.dump end
each(*args, &block)
click to toggle source
# File lib/wongi-engine/network.rb, line 245 def each(*args, &block) current_overlay.each(*args, &block) end
entity(subject)
click to toggle source
# File lib/wongi-engine/network.rb, line 112 def entity(subject) current_overlay.entity(subject) end
execute(name, valuations)
click to toggle source
# File lib/wongi-engine/network.rb, line 230 def execute(name, valuations) beta = queries[name] raise Error, "Undefined query #{name}; known queries are #{queries.keys}" unless beta beta.subst valuations end
exists?(wme)
click to toggle source
# File lib/wongi-engine/network.rb, line 241 def exists?(wme) !find(wme.subject, wme.predicate, wme.object).nil? end
find(*args)
click to toggle source
# File lib/wongi-engine/network.rb, line 253 def find(*args) each(*args).first end
initial_fill(alpha)
click to toggle source
# File lib/wongi-engine/network.rb, line 213 def initial_fill(alpha) default_overlay.each(:_, :_, :_).to_a.each do |wme| alpha.activate wme if wme =~ alpha.template end end
inspect()
click to toggle source
# File lib/wongi-engine/network.rb, line 237 def inspect "<Rete>" end
install_query(query)
click to toggle source
# File lib/wongi-engine/network.rb, line 186 def install_query(query) derived = query.import_into self prepare_query derived.name, derived.conditions, derived.parameters, derived.actions end
install_rule(rule)
click to toggle source
# File lib/wongi-engine/network.rb, line 179 def install_rule(rule) derived = rule.import_into self production = build_production(rule.name, beta_top, derived.conditions, [], derived.actions, false) productions[rule.name] = production if rule.name production end
prepare_query(name, conditions, parameters, actions = [])
click to toggle source
# File lib/wongi-engine/network.rb, line 223 def prepare_query(name, conditions, parameters, actions = []) query = queries[name] = RootNode.new(nil) query.rete = self query.seed(parameters.to_h { |param| [param, nil] }) results[name] = build_production(name, query, conditions, parameters, actions, true) end
query(name, &block)
click to toggle source
# File lib/wongi-engine/network.rb, line 149 def query(name, &block) q = DSL::Query.new name q.instance_eval(&block) self << q end
rdf!()
click to toggle source
# File lib/wongi-engine/network.rb, line 22 def rdf! unless defined? Wongi::RDF::DocumentSupport begin require 'wongi-rdf' rescue LoadError raise "'wongi-rdf' is required for RDF support" end end extend Wongi::RDF::DocumentSupport class << self def statements alpha_top.wmes end end @namespaces = {} @blank_counter = 1 @ns_counter = 0 @used_blanks = {} end
real_assert(wme)
click to toggle source
@private
# File lib/wongi-engine/network.rb, line 125 def real_assert(wme) alphas_for(wme).each { |a| a.activate wme } wme end
real_retract(wme)
click to toggle source
@private
# File lib/wongi-engine/network.rb, line 131 def real_retract(wme) # p real_retract: {wme:} alphas_for(wme).each { |a| a.deactivate wme } end
remove_production(pnode)
click to toggle source
# File lib/wongi-engine/network.rb, line 219 def remove_production(pnode) delete_node_with_ancestors pnode end
retract(wme, options = {})
click to toggle source
# File lib/wongi-engine/network.rb, line 120 def retract(wme, options = {}) default_overlay.retract(wme, options) end
rule(name = nil, &block)
click to toggle source
# File lib/wongi-engine/network.rb, line 143 def rule(name = nil, &block) r = DSL::Rule.new(name || generate_rule_name) r.instance_eval(&block) self << r end
select(*args, &block)
click to toggle source
# File lib/wongi-engine/network.rb, line 249 def select(*args, &block) each(*args, &block) end
with_overlay(&block)
click to toggle source
# File lib/wongi-engine/network.rb, line 67 def with_overlay(&block) child = current_overlay.new_child add_overlay(child) block.call(child) ensure remove_overlay(child) end
wmes()
click to toggle source
# File lib/wongi-engine/network.rb, line 136 def wmes each.to_a end
Also aliased as: statements, facts
Protected Instance Methods
alpha_activate(alpha, wme)
click to toggle source
# File lib/wongi-engine/network.rb, line 285 def alpha_activate(alpha, wme) alpha.activate(wme) end
alphas_for(wme)
click to toggle source
# File lib/wongi-engine/network.rb, line 263 def alphas_for(wme) s = wme.subject p = wme.predicate o = wme.object [ lookup(s, p, o), lookup(s, p, :_), lookup(s, :_, o), lookup(:_, p, o), lookup(s, :_, :_), lookup(:_, p, :_), lookup(:_, :_, o), lookup(:_, :_, :_), ].compact!.tap(&:uniq!) end
best_alpha(template)
click to toggle source
# File lib/wongi-engine/network.rb, line 289 def best_alpha(template) alpha_hash.inject(nil) do |best, (_, alpha)| if template =~ alpha.template && (best.nil? || alpha.size < best.size) alpha else best end end end
build_production(name, root, conditions, parameters, actions, alpha_deaf)
click to toggle source
# File lib/wongi-engine/network.rb, line 299 def build_production(name, root, conditions, parameters, actions, alpha_deaf) compiler = Compiler.new(self, root, conditions, parameters, alpha_deaf) ProductionNode.new(name, compiler.compile, actions).tap do |production| production.compilation_context = compiler production.refresh end end
delete_node_with_ancestors(node)
click to toggle source
# File lib/wongi-engine/network.rb, line 307 def delete_node_with_ancestors(node) delete_node_with_ancestors node.partner if node.is_a?(NccNode) # the root node should not be deleted return unless node.parent node.tokens.dup.each do |token| overlays.each do |overlay| overlay.remove_own_token(token) end end node.parent.children.delete node delete_node_with_ancestors(node.parent) if node.parent.children.empty? end
generate_rule_name()
click to toggle source
# File lib/wongi-engine/network.rb, line 259 def generate_rule_name "rule_#{productions.length}" end
lookup(s, p, o)
click to toggle source
# File lib/wongi-engine/network.rb, line 279 def lookup(s, p, o) key = Template.new(s, p, o) # puts "Lookup for #{key}" alpha_hash[key] end
Private Instance Methods
add_overlay(o)
click to toggle source
@private
# File lib/wongi-engine/network.rb, line 98 def add_overlay(o) overlays << o end
remove_overlay(o)
click to toggle source
@private
# File lib/wongi-engine/network.rb, line 103 def remove_overlay(o) overlays.delete(o) unless o == default_overlay end