factbase 0.0.26 → 0.0.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/factbase/syntax.rb +2 -1
- data/lib/factbase/term.rb +9 -0
- data/lib/factbase.rb +1 -1
- data/test/factbase/test_syntax.rb +2 -0
- data/test/factbase/test_term.rb +11 -1
- 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: 29134d600523642a5dd215f28aa9b48ec3337fec96d4dc41ea0500a3a4ee46c5
|
|
4
|
+
data.tar.gz: 2335e295a7b5dfe6f3cb1eb5ace692cfbc32d788c91906a6f609249b26c4da13
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bf293f3f8ce5b6f1a515a071f771f1a8d84149e0198b0f11073900ed55a6b218bc2d4dcc65ecc439b777fb8163bb13b2f5d24bea9f2600639276183218f2d9f5
|
|
7
|
+
data.tar.gz: ec4c0b3a780ea432b8560485b4a8c467d2b7fc165a1d29fd27b1e03d35f7f481cc11584b3f60100f4a63b308d84023304eb92f8f970cc105cff7e9eba06df0af
|
data/lib/factbase/syntax.rb
CHANGED
|
@@ -43,6 +43,7 @@ class Factbase::Syntax
|
|
|
43
43
|
@ast ||= to_ast(@tokens, 0)
|
|
44
44
|
term = @ast[0]
|
|
45
45
|
raise 'No terms found' if term.nil?
|
|
46
|
+
raise 'Not a term' unless term.is_a?(Factbase::Term)
|
|
46
47
|
term
|
|
47
48
|
end
|
|
48
49
|
|
|
@@ -81,7 +82,7 @@ class Factbase::Syntax
|
|
|
81
82
|
list = []
|
|
82
83
|
acc = ''
|
|
83
84
|
string = false
|
|
84
|
-
@query.to_s.chars.each do |c|
|
|
85
|
+
@query.to_s.gsub(/#.*$/, '').chars.each do |c|
|
|
85
86
|
if ['\'', '"'].include?(c)
|
|
86
87
|
if string && acc[acc.length - 1] == '\\'
|
|
87
88
|
acc = acc[0..-2]
|
data/lib/factbase/term.rb
CHANGED
|
@@ -133,6 +133,15 @@ class Factbase::Term
|
|
|
133
133
|
fact[k].size
|
|
134
134
|
end
|
|
135
135
|
|
|
136
|
+
def type(fact)
|
|
137
|
+
assert_args(1)
|
|
138
|
+
o = @operands[0]
|
|
139
|
+
raise "A symbol expected: #{o}" unless o.is_a?(Symbol)
|
|
140
|
+
k = o.to_s
|
|
141
|
+
return 'nil' if fact[k].nil?
|
|
142
|
+
fact[k].class.to_s
|
|
143
|
+
end
|
|
144
|
+
|
|
136
145
|
def arithmetic(op, fact)
|
|
137
146
|
assert_args(2)
|
|
138
147
|
o = @operands[0]
|
data/lib/factbase.rb
CHANGED
|
@@ -45,6 +45,7 @@ class TestSyntax < Minitest::Test
|
|
|
45
45
|
'(foo)',
|
|
46
46
|
'(foo (bar) (zz 77) )',
|
|
47
47
|
"(eq foo \n\n 'Hello, world!'\n)\n",
|
|
48
|
+
"# this is a comment\n(eq foo # test\n 42)\n\n# another comment\n",
|
|
48
49
|
"(or ( a 4) (b 5) () (and () (c 5) \t\t(r 7 8s 8is 'Foo')))"
|
|
49
50
|
].each do |q|
|
|
50
51
|
Factbase::Syntax.new(q).to_term
|
|
@@ -90,6 +91,7 @@ class TestSyntax < Minitest::Test
|
|
|
90
91
|
[
|
|
91
92
|
'',
|
|
92
93
|
'(foo',
|
|
94
|
+
'some text',
|
|
93
95
|
'"hello, world!',
|
|
94
96
|
'(foo 7',
|
|
95
97
|
"(foo 7 'Dude'",
|
data/test/factbase/test_term.rb
CHANGED
|
@@ -111,6 +111,16 @@ class TestTerm < Minitest::Test
|
|
|
111
111
|
assert(!t.eval(fact('foo' => 100)))
|
|
112
112
|
end
|
|
113
113
|
|
|
114
|
+
def test_type_matching
|
|
115
|
+
t = Factbase::Term.new(:type, [:foo])
|
|
116
|
+
assert_equal('Integer', t.eval(fact('foo' => 42)))
|
|
117
|
+
assert_equal('Array', t.eval(fact('foo' => [1, 2, 3])))
|
|
118
|
+
assert_equal('String', t.eval(fact('foo' => 'Hello, world!')))
|
|
119
|
+
assert_equal('Float', t.eval(fact('foo' => 3.14)))
|
|
120
|
+
assert_equal('Time', t.eval(fact('foo' => Time.now)))
|
|
121
|
+
assert_equal('nil', t.eval(fact))
|
|
122
|
+
end
|
|
123
|
+
|
|
114
124
|
def test_or_matching
|
|
115
125
|
t = Factbase::Term.new(
|
|
116
126
|
:or,
|
|
@@ -139,7 +149,7 @@ class TestTerm < Minitest::Test
|
|
|
139
149
|
|
|
140
150
|
private
|
|
141
151
|
|
|
142
|
-
def fact(map)
|
|
152
|
+
def fact(map = {})
|
|
143
153
|
Factbase::Fact.new(Mutex.new, map)
|
|
144
154
|
end
|
|
145
155
|
end
|