predicate 2.2.0 → 2.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/predicate/nodes/dyadic_comp.rb +4 -0
- data/lib/predicate/nodes/eq.rb +7 -2
- data/lib/predicate/nodes/in.rb +22 -14
- data/lib/predicate/version.rb +1 -1
- data/spec/nodes/eq/test_and.rb +2 -2
- data/spec/nodes/in/test_and.rb +13 -1
- data/spec/predicate/test_constants.rb +7 -1
- data/spec/test_predicate.rb +6 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: db81e00071c6190ac6692ccd9a3b5cd384ba7c0ee9a636d4b4b75c841a84f628
|
4
|
+
data.tar.gz: dbcf66021eee203a6f943f2ca5ca02a101a9306aa8ee4b1ffaeb3131d0d0c524
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cfef5e3953b9b9416be3a72305b0a33ebf063548735bd7a477f1f41aadac0936ef4cfc477092c5de4946e0df77b53706d306579f17aacfd91a7a6b97371f0ece
|
7
|
+
data.tar.gz: 664b4ee4aba8a4e354b70021944fdf76796a8aee3558616d7f58472e0f11a607b4ae23694d15a44af203f56e960c06d22a04393db24eb5c92d25452be7b8585d
|
data/lib/predicate/nodes/eq.rb
CHANGED
@@ -13,9 +13,14 @@ class Predicate
|
|
13
13
|
return self if constants == other.constants
|
14
14
|
return contradiction
|
15
15
|
when In
|
16
|
-
return
|
16
|
+
return super unless var_against_literal? && other.var_against_literal?
|
17
|
+
mine = self.right.value
|
18
|
+
hers = other.right.value
|
19
|
+
return self if hers.include?(mine)
|
20
|
+
contradiction
|
21
|
+
else
|
22
|
+
super
|
17
23
|
end
|
18
|
-
super
|
19
24
|
rescue NotSupportedError
|
20
25
|
super
|
21
26
|
end
|
data/lib/predicate/nodes/in.rb
CHANGED
@@ -16,23 +16,27 @@ class Predicate
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def &(other)
|
19
|
-
|
20
|
-
|
19
|
+
case other
|
20
|
+
when Eq
|
21
|
+
other & self
|
22
|
+
when In
|
23
|
+
# we only optimize is same free variables
|
24
|
+
fv = free_variables
|
25
|
+
return super unless fv.size == 1 && fv == other.free_variables
|
21
26
|
|
22
|
-
|
23
|
-
|
24
|
-
return super unless fv.size == 1 && fv == other.free_variables
|
27
|
+
# we only optimize if both right terms are literals
|
28
|
+
return super unless right.literal? and other.right.literal?
|
25
29
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
30
|
+
intersection = right.value & other.right.value
|
31
|
+
if intersection.empty?
|
32
|
+
Factory.contradiction
|
33
|
+
elsif intersection.size == 1
|
34
|
+
Factory.eq(fv.first, [:literal, intersection.first])
|
35
|
+
else
|
36
|
+
Factory.in(fv.first, intersection)
|
37
|
+
end
|
34
38
|
else
|
35
|
-
|
39
|
+
super
|
36
40
|
end
|
37
41
|
end
|
38
42
|
|
@@ -65,5 +69,9 @@ class Predicate
|
|
65
69
|
values.include?(identifier.evaluate(tuple))
|
66
70
|
end
|
67
71
|
|
72
|
+
def var_against_literal?
|
73
|
+
left.identifier? && right.literal?
|
74
|
+
end
|
75
|
+
|
68
76
|
end
|
69
77
|
end
|
data/lib/predicate/version.rb
CHANGED
data/spec/nodes/eq/test_and.rb
CHANGED
@@ -15,7 +15,7 @@ class Predicate
|
|
15
15
|
end
|
16
16
|
|
17
17
|
context 'with an IN on same variable and literal' do
|
18
|
-
let(:right){ Factory.in(:x, [
|
18
|
+
let(:right){ Factory.in(:x, [2,4]) }
|
19
19
|
|
20
20
|
it{ should be(left) }
|
21
21
|
end
|
@@ -27,4 +27,4 @@ class Predicate
|
|
27
27
|
end
|
28
28
|
|
29
29
|
end
|
30
|
-
end
|
30
|
+
end
|
data/spec/nodes/in/test_and.rb
CHANGED
@@ -9,11 +9,17 @@ class Predicate
|
|
9
9
|
subject{ left & right }
|
10
10
|
|
11
11
|
context 'with an eq on same variable' do
|
12
|
-
let(:right){ Factory.eq(:x,
|
12
|
+
let(:right){ Factory.eq(:x, 2) }
|
13
13
|
|
14
14
|
it{ should be(right) }
|
15
15
|
end
|
16
16
|
|
17
|
+
context 'with an eq on same variable yielding a contradiction' do
|
18
|
+
let(:right){ Factory.eq(:x, 3) }
|
19
|
+
|
20
|
+
it{ should eql(Factory.contradiction) }
|
21
|
+
end
|
22
|
+
|
17
23
|
context 'with an in on same variable' do
|
18
24
|
let(:right){ Factory.in(:x, [2, 4, 5]) }
|
19
25
|
|
@@ -21,6 +27,12 @@ class Predicate
|
|
21
27
|
end
|
22
28
|
|
23
29
|
context 'with an in on same variable, leading to a singleton' do
|
30
|
+
let(:right){ Factory.in(:x, [2]) }
|
31
|
+
|
32
|
+
it{ should eql(Factory.eq(:x, 2)) }
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'with an eq on same variable, leading to a singleton' do
|
24
36
|
let(:right){ Factory.in(:x, [2, 5]) }
|
25
37
|
|
26
38
|
it{ should eql(Factory.eq(:x, 2)) }
|
@@ -84,11 +84,17 @@ class Predicate
|
|
84
84
|
end
|
85
85
|
|
86
86
|
context "on and (one eq, one in, same variable)" do
|
87
|
-
let(:pred){ p.in(:x, [
|
87
|
+
let(:pred){ p.in(:x, [2,8]) & p.eq(:x, 2) }
|
88
88
|
|
89
89
|
it{ should eq({x: 2}) }
|
90
90
|
end
|
91
91
|
|
92
|
+
context "on and (one eq, one in, yielding contradiction)" do
|
93
|
+
let(:pred){ p.in(:x, [3,8]) & p.eq(:x, 2) }
|
94
|
+
|
95
|
+
it{ should eq({}) }
|
96
|
+
end
|
97
|
+
|
92
98
|
context "on and (contradiction)" do
|
93
99
|
let(:pred){ p.eq(:x, 2) & p.eq(:x, 4) }
|
94
100
|
|
data/spec/test_predicate.rb
CHANGED
@@ -117,6 +117,12 @@ describe "Predicate.and" do
|
|
117
117
|
subject{ Predicate.and(Predicate.eq(:x, 12), Predicate.eq(:y, 12)) }
|
118
118
|
|
119
119
|
it_should_behave_like "a predicate"
|
120
|
+
|
121
|
+
it 'detects contraditions' do
|
122
|
+
p1 = Predicate.in(:a, [10,11])
|
123
|
+
p2 = Predicate.eq(:a, 7)
|
124
|
+
expect(p1 & p2).to eq(Predicate.contradiction)
|
125
|
+
end
|
120
126
|
end
|
121
127
|
|
122
128
|
describe "Predicate.or" do
|
@@ -130,4 +136,3 @@ describe "Predicate.not" do
|
|
130
136
|
|
131
137
|
it_should_behave_like "a predicate"
|
132
138
|
end
|
133
|
-
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: predicate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.2.
|
4
|
+
version: 2.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bernard Lambeau
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-01-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sexpr
|