Skip to content

Commit 4172e7a

Browse files
committed
Add existential modifier to variables. Note that this is not preserved from queries, which record the binding using the variable name, without other qualities.
1 parent 034507d commit 4172e7a

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

lib/rdf/query/variable.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ class Variable
6767
# an optional variable value
6868
# @param [Boolean] distinguished (true)
6969
# defaults to false, unless name looks like a bnode
70-
def initialize(name = nil, value = nil, distinguished: true)
70+
def initialize(name = nil, value = nil, distinguished: true, existential: false)
7171
@name = (name || "g#{__id__.to_i.abs}").to_sym
7272
@value = value
7373
@distinguished = distinguished
74+
@existential = existential
7475
end
7576

7677
##
@@ -124,6 +125,23 @@ def distinguished=(value)
124125
@distinguished = value
125126
end
126127

128+
##
129+
# Returns `true` if this variable is existential.
130+
#
131+
# @return [Boolean]
132+
def existential?
133+
@existential
134+
end
135+
136+
##
137+
# Sets if variable is existential or univeresal.
138+
# By default, variables are universal
139+
#
140+
# @return [Boolean]
141+
def existential=(value)
142+
@existential = value
143+
end
144+
127145
##
128146
# Rebinds this variable to the given `value`.
129147
#
@@ -211,6 +229,7 @@ def ===(other)
211229
#
212230
# Non-distinguished variables are indicated with a double `??`
213231
#
232+
# Existential variables are indicated using a single `$`, or with `$$` if also non-distinguished
214233
# @example
215234
# v = Variable.new("a")
216235
# v.to_s => '?a'
@@ -219,7 +238,7 @@ def ===(other)
219238
#
220239
# @return [String]
221240
def to_s
222-
prefix = distinguished? ? '?' : "??"
241+
prefix = distinguished? ? (existential? ? '$' : '?') : (existential? ? '$$' : '??')
223242
unbound? ? "#{prefix}#{name}" : "#{prefix}#{name}=#{value}"
224243
end
225244
end # Variable

spec/query_variable_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@
2929
end
3030
end
3131

32+
context "existential" do
33+
subject { var.existential = true; var }
34+
it "is existential" do
35+
expect(subject).to be_existential
36+
end
37+
38+
it "can be made universal" do
39+
subject.existential = false
40+
expect(subject).not_to be_existential
41+
end
42+
43+
it "has a string representation" do
44+
expect(subject.to_s).to eq "$x"
45+
end
46+
end
47+
3248
context "non-distinguished" do
3349
subject { var.distinguished = false; var }
3450
it "is nondistinguished" do
@@ -47,6 +63,22 @@
4763
it "has a string representation" do
4864
expect(subject.to_s).to eq "??x"
4965
end
66+
67+
context "existential" do
68+
subject { var.existential = true; var.distinguished = false; var }
69+
it "is existential" do
70+
expect(subject).to be_existential
71+
end
72+
73+
it "can be made universal" do
74+
subject.existential = false
75+
expect(subject).not_to be_existential
76+
end
77+
78+
it "has a string representation" do
79+
expect(subject.to_s).to eq "$$x"
80+
end
81+
end
5082
end
5183

5284
it "is convertible to a symbol" do

0 commit comments

Comments
 (0)