factbase 0.0.31 → 0.0.33
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -2
- data/lib/factbase/syntax.rb +1 -1
- data/lib/factbase/term.rb +8 -1
- data/lib/factbase.rb +8 -3
- data/test/factbase/test_inv.rb +1 -1
- data/test/factbase/test_looged.rb +4 -4
- data/test/factbase/test_pre.rb +1 -1
- data/test/factbase/test_query.rb +10 -0
- data/test/factbase/test_syntax.rb +3 -3
- data/test/factbase/test_term.rb +6 -1
- data/test/test_factbase.rb +14 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4434f46130fcb63e9fa798f3c6bacf8441e91a680f789015faf03ba5cb2296b3
|
4
|
+
data.tar.gz: 2d461a92eb2121ca9394e9c07128afa48e3f62a06290678eb68a869fb6fdd501
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a8683f418df4a0bd0de1d1f8be145a2eb4b1ea2e3bb7993355df88b591b6195dbac3654da7222ff3d66c1f3255c4dfb98520ff68f45721527138e258726d454
|
7
|
+
data.tar.gz: 027e1d0262aeca3ef34ea2a9ab3bdca0e6f6975c0f5478bee62e707f15668281bf2950be04f6cfca636d55a0b79ba9cd38e9a5679d8f63b7d800f69943b9b40c
|
data/README.md
CHANGED
@@ -56,8 +56,7 @@ assert(f2.query('(eq foo 42)').each.to_a.size == 1)
|
|
56
56
|
|
57
57
|
All terms available in a query:
|
58
58
|
|
59
|
-
* `()`
|
60
|
-
* `(nil)` is false
|
59
|
+
* `(always)` and `(never)` are "true" and "false"
|
61
60
|
* `(not t)` inverses the `t` if it's boolean (exception otherwise)
|
62
61
|
* `(or t1 t2 ...)` returns true if at least one argument is true
|
63
62
|
* `(and t1 t2 ...)` returns true if all arguments are true
|
data/lib/factbase/syntax.rb
CHANGED
data/lib/factbase/term.rb
CHANGED
@@ -43,6 +43,8 @@ class Factbase::Term
|
|
43
43
|
# @return [bool] TRUE if matches
|
44
44
|
def evaluate(fact)
|
45
45
|
send(@op, fact)
|
46
|
+
rescue NoMethodError => _e
|
47
|
+
raise "Term '#{@op}' is not defined"
|
46
48
|
end
|
47
49
|
|
48
50
|
# Put it into the context: let it see the entire array of maps.
|
@@ -87,11 +89,16 @@ class Factbase::Term
|
|
87
89
|
|
88
90
|
private
|
89
91
|
|
90
|
-
def
|
92
|
+
def always(_fact)
|
91
93
|
assert_args(0)
|
92
94
|
true
|
93
95
|
end
|
94
96
|
|
97
|
+
def never(_fact)
|
98
|
+
assert_args(0)
|
99
|
+
false
|
100
|
+
end
|
101
|
+
|
95
102
|
def not(fact)
|
96
103
|
assert_args(1)
|
97
104
|
!only_bool(the_value(0, fact))
|
data/lib/factbase.rb
CHANGED
@@ -29,7 +29,7 @@ require 'yaml'
|
|
29
29
|
# License:: MIT
|
30
30
|
class Factbase
|
31
31
|
# Current version of the gem (changed by .rultor.yml on every release)
|
32
|
-
VERSION = '0.0.
|
32
|
+
VERSION = '0.0.33'
|
33
33
|
|
34
34
|
# Constructor.
|
35
35
|
def initialize(facts = [])
|
@@ -86,8 +86,13 @@ class Factbase
|
|
86
86
|
copy = this.dup
|
87
87
|
yield copy
|
88
88
|
@mutex.synchronize do
|
89
|
-
|
90
|
-
|
89
|
+
after = Marshal.load(copy.export)
|
90
|
+
after.each_with_index do |m, i|
|
91
|
+
@maps << {} if i >= @maps.size
|
92
|
+
m.each do |k, v|
|
93
|
+
@maps[i][k] = v
|
94
|
+
end
|
95
|
+
end
|
91
96
|
end
|
92
97
|
end
|
93
98
|
|
data/test/factbase/test_inv.rb
CHANGED
@@ -48,19 +48,19 @@ class TestLooged < Minitest::Test
|
|
48
48
|
fb = Factbase.new
|
49
49
|
fb.insert
|
50
50
|
fb.insert
|
51
|
-
assert_equal(2, Factbase::Looged.new(fb, Loog::NULL).query('()').each(&:to_s))
|
51
|
+
assert_equal(2, Factbase::Looged.new(fb, Loog::NULL).query('(always)').each(&:to_s))
|
52
52
|
end
|
53
53
|
|
54
54
|
def test_returns_int_when_empty
|
55
55
|
fb = Factbase.new
|
56
|
-
assert_equal(0, Factbase::Looged.new(fb, Loog::NULL).query('()').each(&:to_s))
|
56
|
+
assert_equal(0, Factbase::Looged.new(fb, Loog::NULL).query('(always)').each(&:to_s))
|
57
57
|
end
|
58
58
|
|
59
59
|
def test_logs_when_enumerator
|
60
60
|
fb = Factbase::Looged.new(Factbase.new, Loog::NULL)
|
61
|
-
assert_equal(0, fb.query('()').each.to_a.size)
|
61
|
+
assert_equal(0, fb.query('(always)').each.to_a.size)
|
62
62
|
fb.insert
|
63
|
-
assert_equal(1, fb.query('()').each.to_a.size)
|
63
|
+
assert_equal(1, fb.query('(always)').each.to_a.size)
|
64
64
|
end
|
65
65
|
|
66
66
|
def test_proper_logging
|
data/test/factbase/test_pre.rb
CHANGED
data/test/factbase/test_query.rb
CHANGED
@@ -90,6 +90,16 @@ class TestQuery < Minitest::Test
|
|
90
90
|
assert_equal(1, maps.size)
|
91
91
|
end
|
92
92
|
|
93
|
+
def test_deleting_nothing
|
94
|
+
maps = []
|
95
|
+
maps << { 'foo' => [42] }
|
96
|
+
maps << { 'bar' => [4, 5] }
|
97
|
+
maps << { 'bar' => 5 }
|
98
|
+
q = Factbase::Query.new(maps, Mutex.new, '(never)')
|
99
|
+
assert_equal(0, q.delete!)
|
100
|
+
assert_equal(3, maps.size)
|
101
|
+
end
|
102
|
+
|
93
103
|
def test_to_array
|
94
104
|
maps = []
|
95
105
|
maps << { 'foo' => [42] }
|
@@ -41,13 +41,12 @@ class TestSyntax < Minitest::Test
|
|
41
41
|
|
42
42
|
def test_simple_parsing
|
43
43
|
[
|
44
|
-
'()',
|
45
44
|
'(foo)',
|
46
45
|
'(foo (bar) (zz 77) )',
|
47
46
|
"(eq foo \n\n 'Hello, world!'\n)\n",
|
48
47
|
"(eq x 'Hello, \\' \n) \\' ( world!')",
|
49
48
|
"# this is a comment\n(eq foo # test\n 42)\n\n# another comment\n",
|
50
|
-
"(or ( a 4) (b 5) () (and () (c 5) \t\t(r 7 w8s w8is 'Foo')))"
|
49
|
+
"(or ( a 4) (b 5) (always) (and (always) (c 5) \t\t(r 7 w8s w8is 'Foo')))"
|
51
50
|
].each do |q|
|
52
51
|
Factbase::Syntax.new(q).to_term
|
53
52
|
end
|
@@ -67,7 +66,7 @@ class TestSyntax < Minitest::Test
|
|
67
66
|
'(eq t 2024-05-25T19:43:48Z)',
|
68
67
|
'(eq t 3.1415926)',
|
69
68
|
'(eq t 3.0e+21)',
|
70
|
-
"(foo (x (f (t (y 42 'Hey you'))) (
|
69
|
+
"(foo (x (f (t (y 42 'Hey you'))) (never) (r 3)) y z)"
|
71
70
|
].each do |q|
|
72
71
|
assert_equal(q, Factbase::Syntax.new(q).to_term.to_s)
|
73
72
|
end
|
@@ -91,6 +90,7 @@ class TestSyntax < Minitest::Test
|
|
91
90
|
def test_broken_syntax
|
92
91
|
[
|
93
92
|
'',
|
93
|
+
'()',
|
94
94
|
'(foo',
|
95
95
|
'(foo 1) (bar 2)',
|
96
96
|
'some text',
|
data/test/factbase/test_term.rb
CHANGED
@@ -82,8 +82,13 @@ class TestTerm < Minitest::Test
|
|
82
82
|
assert(!t.evaluate(fact('bar' => [100])))
|
83
83
|
end
|
84
84
|
|
85
|
+
def test_false_matching
|
86
|
+
t = Factbase::Term.new(:never, [])
|
87
|
+
assert(!t.evaluate(fact('foo' => [100])))
|
88
|
+
end
|
89
|
+
|
85
90
|
def test_not_matching
|
86
|
-
t = Factbase::Term.new(:not, [Factbase::Term.new(:
|
91
|
+
t = Factbase::Term.new(:not, [Factbase::Term.new(:always, [])])
|
87
92
|
assert(!t.evaluate(fact('foo' => [100])))
|
88
93
|
end
|
89
94
|
|
data/test/test_factbase.rb
CHANGED
@@ -107,7 +107,7 @@ class TestFactbase < Minitest::Test
|
|
107
107
|
|
108
108
|
def test_all_decorators
|
109
109
|
[
|
110
|
-
Factbase::Rules.new(Factbase.new, '()'),
|
110
|
+
Factbase::Rules.new(Factbase.new, '(always)'),
|
111
111
|
Factbase::Inv.new(Factbase.new) { |_, _| true },
|
112
112
|
Factbase::Pre.new(Factbase.new) { |_| true },
|
113
113
|
Factbase::Looged.new(Factbase.new, Loog::NULL),
|
@@ -126,7 +126,19 @@ class TestFactbase < Minitest::Test
|
|
126
126
|
end
|
127
127
|
d.import(d.export)
|
128
128
|
assert_equal(4, d.size)
|
129
|
-
assert_equal(4, d.query('()').each.to_a.size)
|
129
|
+
assert_equal(4, d.query('(always)').each.to_a.size)
|
130
130
|
end
|
131
131
|
end
|
132
|
+
|
133
|
+
def test_txn_inside_query
|
134
|
+
fb = Factbase.new
|
135
|
+
fb.insert.foo = 42
|
136
|
+
fb.query('(exists foo)').each do |f|
|
137
|
+
fb.txn do |fbt|
|
138
|
+
fbt.insert.bar = 33
|
139
|
+
end
|
140
|
+
f.xyz = 1
|
141
|
+
end
|
142
|
+
assert_equal(1, fb.query('(exists xyz)').each.to_a.size)
|
143
|
+
end
|
132
144
|
end
|