Module: OMF::Rete::Store
- Included in:
- AlphaStore, NamedAlphaStore, ObjectStore, PredicateStore
- Defined in:
- lib/omf_rete/store.rb,
lib/omf_rete/store/alpha_store.rb,
lib/omf_rete/store/object_store.rb,
lib/omf_rete/store/predicate_store.rb,
lib/omf_rete/store/named_alpha_store.rb
Defined Under Namespace
Modules: Alpha Classes: AlphaStore, AlreadyRegisteredPredicateException, NamedAlphaStore, NotImplementedException, ObjectStore, PredicateStore, StoreException, UnknownPredicateException, UnknownSubscriptionException, WrongNameException, WrongPatternLengthException
Constant Summary collapse
- DEF_TYPE =
:alpha
Class Method Summary collapse
Instance Method Summary collapse
-
#add(*els) ⇒ Object
(also: #add_fact)
alias.
- #addTuple(tarray) ⇒ Object
-
#confirmLength(tuple) ⇒ Object
Return true if tuple (or pattern) is a valid one for this store.
- #createTSet(description, indexPattern) ⇒ Object
-
#find(pattern) ⇒ Object
Return a set of tuples which match
pattern
. -
#on_query(&requestProc) ⇒ Object
Register a function to be called whenever a query is performed on the store.
-
#query(query, out_pattern = nil) ⇒ Object
Run a query against the store.
-
#remove(*els) ⇒ Object
(also: #remove_fact)
alias.
-
#removeTuple(*els) ⇒ Object
Remove a tuple from the store.
-
#subscribe(name, query, out_pattern = nil, &block) ⇒ Object
(also: #add_rule)
end.
- #unsubscribe(name_or_plan) ⇒ Object
Class Method Details
.create(length = -1,, opts = {}) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/omf_rete/store.rb', line 14 def self.create(length = -1, opts = {}) case (type = opts[:type] || DEF_TYPE) when :alpha require 'omf_rete/store/alpha_store' return OMF::Rete::Store::AlphaStore.new(length, opts) when :named_alpha require 'omf_rete/store/named_alpha_store' return OMF::Rete::Store::NamedAlphaStore.new(opts.delete(:name), length, opts) when :predicate require 'omf_rete/store/predicate_store' return PredicateStore.new(opts) when :object require 'omf_rete/store/object_store' return ObjectStore.new(opts) else raise "Unknown store type '#{type}'" end end |
Instance Method Details
#add(*els) ⇒ Object Also known as: add_fact
alias
85 86 87 |
# File 'lib/omf_rete/store.rb', line 85 def add(*els) addTuple(els) end |
#addTuple(tarray) ⇒ Object
80 81 82 |
# File 'lib/omf_rete/store.rb', line 80 def addTuple(tarray) raise NotImplementedException.new end |
#confirmLength(tuple) ⇒ Object
Return true if tuple (or pattern) is a valid one for this store
130 131 132 |
# File 'lib/omf_rete/store.rb', line 130 def confirmLength(tuple) tuple.is_a?(Array) && tuple.length == @length end |
#createTSet(description, indexPattern) ⇒ Object
122 123 124 125 126 |
# File 'lib/omf_rete/store.rb', line 122 def createTSet(description, indexPattern) tset = OMF::Rete::IndexedTupleSet.new(description, indexPattern) registerTSet(tset, description) tset end |
#find(pattern) ⇒ Object
Return a set of tuples which match pattern
. Pattern is a tuples of the same length this store is configured for where any non-nil element is matched directly and any nil element is considered a wildcard.
108 109 110 |
# File 'lib/omf_rete/store.rb', line 108 def find(pattern) raise NotImplementedException.new end |
#on_query(&requestProc) ⇒ Object
Register a function to be called whenever a query is performed on the store. The arguments to the proc are identical to that of query
. The returned tuple set is added to the store and returned with any other tuples stored which match the query pattern.
118 119 120 |
# File 'lib/omf_rete/store.rb', line 118 def on_query(&requestProc) raise NotImplementedException.new end |
#query(query, out_pattern = nil) ⇒ Object
Run a query against the store. This is essentially a short lived subscription may not be catch everything if there are inserts at the same time.
71 72 73 74 75 76 77 78 |
# File 'lib/omf_rete/store.rb', line 71 def query(query, out_pattern = nil) result = [] plan = subscribe(null, query, out_pattern) do |t| result << t end unsubscribe(plan) result end |
#remove(*els) ⇒ Object Also known as: remove_fact
alias
97 98 99 |
# File 'lib/omf_rete/store.rb', line 97 def remove(*els) removeTuple(els) end |
#removeTuple(*els) ⇒ Object
Remove a tuple from the store
92 93 94 |
# File 'lib/omf_rete/store.rb', line 92 def removeTuple(*els) raise NotImplementedException.new end |
#subscribe(name, query, out_pattern = nil, &block) ⇒ Object Also known as: add_rule
end
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/omf_rete/store.rb', line 39 def subscribe(name, query, out_pattern = nil, &block) if name && @plans[name] raise StoreException.new "Already have subscription '#{name}'." end require 'omf_rete/planner/plan_builder' pb = OMF::Rete::Planner::PlanBuilder.new(query, self) pb.build plan = pb.materialize(out_pattern, &block) if name @plans[name] = plan end plan end |
#unsubscribe(name_or_plan) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/omf_rete/store.rb', line 56 def unsubscribe(name_or_plan) if name_or_plan.is_a? OMF::Rete::Planner::FinalPlan plan = name_or_plan else plan = @plans.delete(name_or_plan) end unless plan raise UnknownSubscriptionException.new("Unknown subscription '#{name_or_plan}'") end plan.detach end |