-
Notifications
You must be signed in to change notification settings - Fork 9
/
extensions.rb
62 lines (53 loc) · 1.48 KB
/
extensions.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true
require 'rdf'
# Monkey-patch RDF::Enumerable to add `:existentials` and `:univerals` accessors
module RDF
module Enumerable
# Existential quantifiers defined on this enumerable
# @return [Array<RDF::Query::Variable>]
attr_accessor :existentials
# Universal quantifiers defined on this enumerable
# @return [Array<RDF::Query::Variable>]
attr_accessor :universals
##
# An enumerable contains another enumerable if every statement in other is a statement in self
#
# @param [RDF::Enumerable] other
# @return [Boolean]
def contain?(other)
other.all? {|statement| has_statement?(statement)}
end
end
class Statement
# Override variable?
def variable?
to_a.any? {|term| !term.is_a?(RDF::Term) || term.variable?} || graph_name && graph_name.variable?
end
# Transform Statement into an SXP
# @return [Array]
def to_sxp_bin
[(variable? ? :pattern : :triple), subject, predicate, object, graph_name].compact
end
##
# Returns an S-Expression (SXP) representation
#
# @return [String]
def to_sxp
to_sxp_bin.to_sxp
end
end
class Query::Solution
# Transform Statement into an SXP
# @return [Array]
def to_sxp_bin
[:solution] + bindings.map {|k, v| Query::Variable.new(k, v).to_sxp_bin}
end
##
# Returns an S-Expression (SXP) representation
#
# @return [String]
def to_sxp
to_sxp_bin.to_sxp
end
end
end