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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6f717f6bf0c4cddf18b0a7762f6a85dd47200ec21c274fb974755319862420f1
4
- data.tar.gz: 48ce7004023d6121f650ab05d9893c2d25ff3a60324450b773ec4629e643b752
3
+ metadata.gz: db81e00071c6190ac6692ccd9a3b5cd384ba7c0ee9a636d4b4b75c841a84f628
4
+ data.tar.gz: dbcf66021eee203a6f943f2ca5ca02a101a9306aa8ee4b1ffaeb3131d0d0c524
5
5
  SHA512:
6
- metadata.gz: eb29761546c30faca593aa4dd5749367fce9e3a3d85404a49d32db310c73471a9008b01f046828070583cb043f3142e1e10688be06a2a5dca4d318c144b1f33a
7
- data.tar.gz: d8852634d7c9d33199544fa2ee0ef70f92198b379d6f7e6ed670d40266e8799e7ff7d804551cb1bcaf19c4b6390715217e665e8a03b4c50a5213be10c3e3c626
6
+ metadata.gz: cfef5e3953b9b9416be3a72305b0a33ebf063548735bd7a477f1f41aadac0936ef4cfc477092c5de4946e0df77b53706d306579f17aacfd91a7a6b97371f0ece
7
+ data.tar.gz: 664b4ee4aba8a4e354b70021944fdf76796a8aee3558616d7f58472e0f11a607b4ae23694d15a44af203f56e960c06d22a04393db24eb5c92d25452be7b8585d
@@ -22,5 +22,9 @@ class Predicate
22
22
  @free_variables ||= left.free_variables | right.free_variables
23
23
  end
24
24
 
25
+ def var_against_literal?
26
+ left.identifier? && right.literal?
27
+ end
28
+
25
29
  end
26
30
  end
@@ -13,9 +13,14 @@ class Predicate
13
13
  return self if constants == other.constants
14
14
  return contradiction
15
15
  when In
16
- return self if other.right.literal?
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
@@ -16,23 +16,27 @@ class Predicate
16
16
  end
17
17
 
18
18
  def &(other)
19
- # we only optimize with another In
20
- return super unless other.is_a?(In)
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
- # we only optimize is same free variables
23
- fv = free_variables
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
- # we only optimize if both right terms are literals
27
- return super unless right.literal? and other.right.literal?
28
-
29
- intersection = right.value & other.right.value
30
- if intersection.empty?
31
- Factory.contradiction
32
- elsif intersection.size == 1
33
- Factory.eq(fv.first, [:literal, intersection.first])
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
- Factory.in(fv.first, intersection)
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
@@ -2,7 +2,7 @@ class Predicate
2
2
  module Version
3
3
  MAJOR = 2
4
4
  MINOR = 2
5
- TINY = 0
5
+ TINY = 1
6
6
  end
7
7
  VERSION = "#{Version::MAJOR}.#{Version::MINOR}.#{Version::TINY}"
8
8
  end
@@ -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, [3,4]) }
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
@@ -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, 3) }
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, [4,8]) & p.eq(:x, 2) }
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
 
@@ -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.0
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: 2019-06-07 00:00:00.000000000 Z
11
+ date: 2020-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sexpr