Class: Factbase::Rules
- Inherits:
-
Object
- Object
- Factbase::Rules
- Defined in:
- lib/factbase/rules.rb
Overview
A decorator of a Factbase, that checks rules on every set.
Say, you want every fact to have foo
property. You want any attempt to insert a fact without this property to lead to a runtime error. Here is how:
fb = Factbase.new
fb = Factbase::Rules.new(fb, '(exists foo)')
fb.txn do |fbt|
f = fbt.insert
f. = 3 # No exception here
end # Runtime exception here (transaction won't commit)
- Author
-
Yegor Bugayenko ([email protected])
- Copyright
-
Copyright © 2024-2025 Yegor Bugayenko
- License
-
MIT
Defined Under Namespace
Classes: Check, Fact, Later, Query
Instance Method Summary collapse
-
#initialize(fb, rules, check = Check.new(rules), uid: nil) ⇒ Rules
constructor
A new instance of Rules.
- #insert ⇒ Object
- #query(term, maps = nil) ⇒ Object
- #txn ⇒ Object
Constructor Details
#initialize(fb, rules, check = Check.new(rules), uid: nil) ⇒ Rules
Returns a new instance of Rules.
30 31 32 33 34 35 36 37 38 |
# File 'lib/factbase/rules.rb', line 30 def initialize(fb, rules, check = Check.new(rules), uid: nil) raise 'The "fb" is nil' if fb.nil? @fb = fb raise 'The "rules" is nil' if rules.nil? @rules = rules raise 'The "check" is nil' if check.nil? @check = check @uid = uid end |
Instance Method Details
#insert ⇒ Object
40 41 42 |
# File 'lib/factbase/rules.rb', line 40 def insert Fact.new(@fb.insert, @check, @fb) end |
#query(term, maps = nil) ⇒ Object
44 45 46 |
# File 'lib/factbase/rules.rb', line 44 def query(term, maps = nil) Query.new(@fb.query(term, maps), @check, self) end |
#txn ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/factbase/rules.rb', line 48 def txn before = @check later = Later.new(@uid) @check = later @fb.txn do |fbt| churn = Factbase::Churn.new yield Factbase::Tallied.new(Factbase::Rules.new(fbt, @rules, @check, uid: @uid), churn) @check = before unless churn.zero? fbt.query('(always)').each do |f| next unless later.include?(f) @check.it(f, @fb) end end end end |