rdf-n3 3.0.1 → 3.2.0

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.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +198 -76
  3. data/UNLICENSE +1 -1
  4. data/VERSION +1 -1
  5. data/lib/rdf/n3/algebra/builtin.rb +79 -0
  6. data/lib/rdf/n3/algebra/formula.rb +446 -0
  7. data/lib/rdf/n3/algebra/list/append.rb +42 -0
  8. data/lib/rdf/n3/algebra/list/first.rb +24 -0
  9. data/lib/rdf/n3/algebra/list/in.rb +48 -0
  10. data/lib/rdf/n3/algebra/list/iterate.rb +96 -0
  11. data/lib/rdf/n3/algebra/list/last.rb +24 -0
  12. data/lib/rdf/n3/algebra/list/length.rb +24 -0
  13. data/lib/rdf/n3/algebra/list/member.rb +44 -0
  14. data/lib/rdf/n3/algebra/list_operator.rb +96 -0
  15. data/lib/rdf/n3/algebra/log/conclusion.rb +65 -0
  16. data/lib/rdf/n3/algebra/log/conjunction.rb +36 -0
  17. data/lib/rdf/n3/algebra/log/content.rb +34 -0
  18. data/lib/rdf/n3/algebra/log/dtlit.rb +41 -0
  19. data/lib/rdf/n3/algebra/log/equal_to.rb +34 -0
  20. data/lib/rdf/n3/algebra/log/implies.rb +102 -0
  21. data/lib/rdf/n3/algebra/log/includes.rb +70 -0
  22. data/lib/rdf/n3/algebra/log/langlit.rb +41 -0
  23. data/lib/rdf/n3/algebra/log/n3_string.rb +34 -0
  24. data/lib/rdf/n3/algebra/log/not_equal_to.rb +23 -0
  25. data/lib/rdf/n3/algebra/log/not_includes.rb +27 -0
  26. data/lib/rdf/n3/algebra/log/output_string.rb +40 -0
  27. data/lib/rdf/n3/algebra/log/parsed_as_n3.rb +36 -0
  28. data/lib/rdf/n3/algebra/log/semantics.rb +40 -0
  29. data/lib/rdf/n3/algebra/math/absolute_value.rb +36 -0
  30. data/lib/rdf/n3/algebra/math/acos.rb +26 -0
  31. data/lib/rdf/n3/algebra/math/acosh.rb +26 -0
  32. data/lib/rdf/n3/algebra/math/asin.rb +26 -0
  33. data/lib/rdf/n3/algebra/math/asinh.rb +26 -0
  34. data/lib/rdf/n3/algebra/math/atan.rb +26 -0
  35. data/lib/rdf/n3/algebra/math/atanh.rb +26 -0
  36. data/lib/rdf/n3/algebra/math/ceiling.rb +28 -0
  37. data/lib/rdf/n3/algebra/math/cos.rb +40 -0
  38. data/lib/rdf/n3/algebra/math/cosh.rb +38 -0
  39. data/lib/rdf/n3/algebra/math/difference.rb +40 -0
  40. data/lib/rdf/n3/algebra/math/equal_to.rb +54 -0
  41. data/lib/rdf/n3/algebra/math/exponentiation.rb +35 -0
  42. data/lib/rdf/n3/algebra/math/floor.rb +28 -0
  43. data/lib/rdf/n3/algebra/math/greater_than.rb +41 -0
  44. data/lib/rdf/n3/algebra/math/less_than.rb +41 -0
  45. data/lib/rdf/n3/algebra/math/negation.rb +38 -0
  46. data/lib/rdf/n3/algebra/math/not_equal_to.rb +25 -0
  47. data/lib/rdf/n3/algebra/math/not_greater_than.rb +25 -0
  48. data/lib/rdf/n3/algebra/math/not_less_than.rb +25 -0
  49. data/lib/rdf/n3/algebra/math/product.rb +20 -0
  50. data/lib/rdf/n3/algebra/math/quotient.rb +36 -0
  51. data/lib/rdf/n3/algebra/math/remainder.rb +35 -0
  52. data/lib/rdf/n3/algebra/math/rounded.rb +26 -0
  53. data/lib/rdf/n3/algebra/math/sin.rb +40 -0
  54. data/lib/rdf/n3/algebra/math/sinh.rb +38 -0
  55. data/lib/rdf/n3/algebra/math/sum.rb +40 -0
  56. data/lib/rdf/n3/algebra/math/tan.rb +40 -0
  57. data/lib/rdf/n3/algebra/math/tanh.rb +38 -0
  58. data/lib/rdf/n3/algebra/not_implemented.rb +13 -0
  59. data/lib/rdf/n3/algebra/resource_operator.rb +122 -0
  60. data/lib/rdf/n3/algebra/str/concatenation.rb +27 -0
  61. data/lib/rdf/n3/algebra/str/contains.rb +33 -0
  62. data/lib/rdf/n3/algebra/str/contains_ignoring_case.rb +33 -0
  63. data/lib/rdf/n3/algebra/str/ends_with.rb +33 -0
  64. data/lib/rdf/n3/algebra/str/equal_ignoring_case.rb +34 -0
  65. data/lib/rdf/n3/algebra/str/format.rb +17 -0
  66. data/lib/rdf/n3/algebra/str/greater_than.rb +38 -0
  67. data/lib/rdf/n3/algebra/str/less_than.rb +33 -0
  68. data/lib/rdf/n3/algebra/str/matches.rb +37 -0
  69. data/lib/rdf/n3/algebra/str/not_equal_ignoring_case.rb +17 -0
  70. data/lib/rdf/n3/algebra/str/not_greater_than.rb +17 -0
  71. data/lib/rdf/n3/algebra/str/not_less_than.rb +17 -0
  72. data/lib/rdf/n3/algebra/str/not_matches.rb +18 -0
  73. data/lib/rdf/n3/algebra/str/replace.rb +35 -0
  74. data/lib/rdf/n3/algebra/str/scrape.rb +35 -0
  75. data/lib/rdf/n3/algebra/str/starts_with.rb +33 -0
  76. data/lib/rdf/n3/algebra/time/day.rb +35 -0
  77. data/lib/rdf/n3/algebra/time/day_of_week.rb +27 -0
  78. data/lib/rdf/n3/algebra/time/gm_time.rb +29 -0
  79. data/lib/rdf/n3/algebra/time/hour.rb +35 -0
  80. data/lib/rdf/n3/algebra/time/in_seconds.rb +59 -0
  81. data/lib/rdf/n3/algebra/time/local_time.rb +29 -0
  82. data/lib/rdf/n3/algebra/time/minute.rb +35 -0
  83. data/lib/rdf/n3/algebra/time/month.rb +35 -0
  84. data/lib/rdf/n3/algebra/time/second.rb +35 -0
  85. data/lib/rdf/n3/algebra/time/timezone.rb +36 -0
  86. data/lib/rdf/n3/algebra/time/year.rb +29 -0
  87. data/lib/rdf/n3/algebra.rb +210 -0
  88. data/lib/rdf/n3/extensions.rb +221 -0
  89. data/lib/rdf/n3/format.rb +66 -1
  90. data/lib/rdf/n3/list.rb +630 -0
  91. data/lib/rdf/n3/reader.rb +774 -497
  92. data/lib/rdf/n3/reasoner.rb +282 -0
  93. data/lib/rdf/n3/refinements.rb +178 -0
  94. data/lib/rdf/n3/repository.rb +332 -0
  95. data/lib/rdf/n3/terminals.rb +78 -0
  96. data/lib/rdf/n3/vocab.rb +36 -3
  97. data/lib/rdf/n3/writer.rb +461 -250
  98. data/lib/rdf/n3.rb +11 -8
  99. metadata +177 -49
  100. data/AUTHORS +0 -1
  101. data/History.markdown +0 -99
  102. data/lib/rdf/n3/patches/array_hacks.rb +0 -53
  103. data/lib/rdf/n3/reader/meta.rb +0 -641
  104. data/lib/rdf/n3/reader/parser.rb +0 -237
@@ -0,0 +1,35 @@
1
+ module RDF::N3::Algebra::Time
2
+ ##
3
+ # For a date-time, its time:second is the seconds component.
4
+ #
5
+ # @see https://www.w3.org/TR/xpath-functions/#func-seconds-from-dateTime
6
+ class Second < RDF::N3::Algebra::ResourceOperator
7
+ NAME = :timeSecond
8
+ URI = RDF::N3::Time.second
9
+
10
+ ##
11
+ # The time:second operator takes string or dateTime and extracts the seconds component.
12
+ #
13
+ # @param [RDF::Term] resource
14
+ # @param [:subject, :object] position
15
+ # @return [RDF::Term]
16
+ # @see RDF::N3::ResourceOperator#evaluate
17
+ def resolve(resource, position:)
18
+ case position
19
+ when :subject
20
+ return nil unless resource.literal?
21
+ resource = resource.as_datetime
22
+ RDF::Literal(resource.object.strftime("%S").to_i)
23
+ when :object
24
+ return nil unless resource.literal? || resource.variable?
25
+ resource
26
+ end
27
+ end
28
+
29
+ ##
30
+ # There is no second unless it was specified in the lexical form
31
+ def valid?(subject, object)
32
+ subject.value.match?(%r(^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}))
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,36 @@
1
+ module RDF::N3::Algebra::Time
2
+ ##
3
+ # For a date-time, its time:timeZone is the trailing timezone offset part, e.g. "-05:00".
4
+ #
5
+ # @see https://www.w3.org/TR/xpath-functions/#func-timezone-from-dateTime
6
+ class Timezone < RDF::N3::Algebra::ResourceOperator
7
+ NAME = :timeTimezone
8
+ URI = RDF::N3::Time.timeZone
9
+
10
+ ##
11
+ # The time:timeZone operator takes string or dateTime and extracts the timeZone component.
12
+ #
13
+ # @param [RDF::Term] resource
14
+ # @param [:subject, :object] position
15
+ # @return [RDF::Term]
16
+ # @see RDF::N3::ResourceOperator#evaluate
17
+ def resolve(resource, position:)
18
+ case position
19
+ when :subject
20
+ return nil unless resource.literal?
21
+ resource = resource.as_datetime
22
+ RDF::Literal(resource.object.strftime("%Z"))
23
+ when :object
24
+ return nil unless resource.literal? || resource.variable?
25
+ resource
26
+ end
27
+ end
28
+
29
+ ##
30
+ # There is no timezone unless it was specified in the lexical form and is not "Z"
31
+ def valid?(subject, object)
32
+ md = subject.value.match(%r(^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(Z|[\+-][\d-]+)))
33
+ md && md[1].to_s != 'Z'
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,29 @@
1
+ module RDF::N3::Algebra::Time
2
+ ##
3
+ # For a date-time, its time:year is the year component.
4
+ #
5
+ # @see https://www.w3.org/TR/xpath-functions/#func-year-from-dateTime
6
+ class Year < RDF::N3::Algebra::ResourceOperator
7
+ NAME = :timeYear
8
+ URI = RDF::N3::Time.year
9
+
10
+ ##
11
+ # The time:year operator takes string or dateTime and extracts the year component.
12
+ #
13
+ # @param [RDF::Term] resource
14
+ # @param [:subject, :object] position
15
+ # @return [RDF::Term]
16
+ # @see RDF::N3::ResourceOperator#evaluate
17
+ def resolve(resource, position:)
18
+ case position
19
+ when :subject
20
+ return nil unless resource.literal?
21
+ resource = resource.as_datetime
22
+ RDF::Literal(resource.object.strftime("%Y").to_i)
23
+ when :object
24
+ return nil unless resource.literal? || resource.variable?
25
+ resource
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,210 @@
1
+ $:.unshift(File.expand_path("../..", __FILE__))
2
+ require 'sparql/algebra'
3
+ require 'sxp'
4
+
5
+ module RDF::N3
6
+ # Based on the SPARQL Algebra, operators for executing a patch
7
+ #
8
+ # @author [Gregg Kellogg](http://greggkellogg.net/)
9
+ module Algebra
10
+ autoload :Builtin, 'rdf/n3/algebra/builtin'
11
+ autoload :Formula, 'rdf/n3/algebra/formula'
12
+ autoload :ListOperator, 'rdf/n3/algebra/list_operator'
13
+ autoload :NotImplemented, 'rdf/n3/algebra/not_implemented'
14
+ autoload :ResourceOperator, 'rdf/n3/algebra/resource_operator'
15
+
16
+ module List
17
+ def vocab; RDF::N3::List.to_uri; end
18
+ module_function :vocab
19
+ autoload :Append, 'rdf/n3/algebra/list/append'
20
+ autoload :First, 'rdf/n3/algebra/list/first'
21
+ autoload :In, 'rdf/n3/algebra/list/in'
22
+ autoload :Iterate, 'rdf/n3/algebra/list/iterate'
23
+ autoload :Last, 'rdf/n3/algebra/list/last'
24
+ autoload :Length, 'rdf/n3/algebra/list/length'
25
+ autoload :Member, 'rdf/n3/algebra/list/member'
26
+ end
27
+
28
+ module Log
29
+ def vocab; RDF::N3::Log.to_uri; end
30
+ module_function :vocab
31
+ autoload :Conclusion, 'rdf/n3/algebra/log/conclusion'
32
+ autoload :Conjunction, 'rdf/n3/algebra/log/conjunction'
33
+ autoload :Content, 'rdf/n3/algebra/log/content'
34
+ autoload :DtLit, 'rdf/n3/algebra/log/dtlit'
35
+ autoload :EqualTo, 'rdf/n3/algebra/log/equal_to'
36
+ autoload :Implies, 'rdf/n3/algebra/log/implies'
37
+ autoload :Includes, 'rdf/n3/algebra/log/includes'
38
+ autoload :LangLit, 'rdf/n3/algebra/log/langlit'
39
+ autoload :N3String, 'rdf/n3/algebra/log/n3_string'
40
+ autoload :NotEqualTo, 'rdf/n3/algebra/log/not_equal_to'
41
+ autoload :NotIncludes, 'rdf/n3/algebra/log/not_includes'
42
+ autoload :OutputString, 'rdf/n3/algebra/log/output_string'
43
+ autoload :ParsedAsN3, 'rdf/n3/algebra/log/parsed_as_n3'
44
+ autoload :Semantics, 'rdf/n3/algebra/log/semantics'
45
+ end
46
+
47
+ module Math
48
+ def vocab; RDF::N3::Math.to_uri; end
49
+ module_function :vocab
50
+ autoload :AbsoluteValue, 'rdf/n3/algebra/math/absolute_value'
51
+ autoload :ACos, 'rdf/n3/algebra/math/acos'
52
+ autoload :ASin, 'rdf/n3/algebra/math/asin'
53
+ autoload :ATan, 'rdf/n3/algebra/math/atan'
54
+ autoload :ACosH, 'rdf/n3/algebra/math/acosh'
55
+ autoload :ASinH, 'rdf/n3/algebra/math/asinh'
56
+ autoload :ATanH, 'rdf/n3/algebra/math/atanh'
57
+ autoload :Ceiling, 'rdf/n3/algebra/math/ceiling'
58
+ autoload :Cos, 'rdf/n3/algebra/math/cos'
59
+ autoload :CosH, 'rdf/n3/algebra/math/cosh'
60
+ autoload :Difference, 'rdf/n3/algebra/math/difference'
61
+ autoload :EqualTo, 'rdf/n3/algebra/math/equal_to'
62
+ autoload :Exponentiation, 'rdf/n3/algebra/math/exponentiation'
63
+ autoload :Floor, 'rdf/n3/algebra/math/floor'
64
+ autoload :GreaterThan, 'rdf/n3/algebra/math/greater_than'
65
+ autoload :LessThan, 'rdf/n3/algebra/math/less_than'
66
+ autoload :Negation, 'rdf/n3/algebra/math/negation'
67
+ autoload :NotEqualTo, 'rdf/n3/algebra/math/not_equal_to'
68
+ autoload :NotGreaterThan, 'rdf/n3/algebra/math/not_greater_than'
69
+ autoload :NotLessThan, 'rdf/n3/algebra/math/not_less_than'
70
+ autoload :Product, 'rdf/n3/algebra/math/product'
71
+ autoload :Quotient, 'rdf/n3/algebra/math/quotient'
72
+ autoload :Remainder, 'rdf/n3/algebra/math/remainder'
73
+ autoload :Rounded, 'rdf/n3/algebra/math/rounded'
74
+ autoload :Sin, 'rdf/n3/algebra/math/sin'
75
+ autoload :SinH, 'rdf/n3/algebra/math/sinh'
76
+ autoload :Sum, 'rdf/n3/algebra/math/sum'
77
+ autoload :Tan, 'rdf/n3/algebra/math/tan'
78
+ autoload :TanH, 'rdf/n3/algebra/math/tanh'
79
+ end
80
+
81
+ module Str
82
+ def vocab; RDF::N3::Str.to_uri; end
83
+ module_function :vocab
84
+ autoload :Concatenation, 'rdf/n3/algebra/str/concatenation'
85
+ autoload :Contains, 'rdf/n3/algebra/str/contains'
86
+ autoload :ContainsIgnoringCase, 'rdf/n3/algebra/str/contains_ignoring_case'
87
+ autoload :EndsWith, 'rdf/n3/algebra/str/ends_with'
88
+ autoload :EqualIgnoringCase, 'rdf/n3/algebra/str/equal_ignoring_case'
89
+ autoload :Format, 'rdf/n3/algebra/str/format'
90
+ autoload :GreaterThan, 'rdf/n3/algebra/str/greater_than'
91
+ autoload :LessThan, 'rdf/n3/algebra/str/less_than'
92
+ autoload :Matches, 'rdf/n3/algebra/str/matches'
93
+ autoload :NotEqualIgnoringCase, 'rdf/n3/algebra/str/not_equal_ignoring_case'
94
+ autoload :NotGreaterThan, 'rdf/n3/algebra/str/not_greater_than'
95
+ autoload :NotLessThan, 'rdf/n3/algebra/str/not_less_than'
96
+ autoload :NotMatches, 'rdf/n3/algebra/str/not_matches'
97
+ autoload :Replace, 'rdf/n3/algebra/str/replace'
98
+ autoload :Scrape, 'rdf/n3/algebra/str/scrape'
99
+ autoload :StartsWith, 'rdf/n3/algebra/str/starts_with'
100
+ end
101
+
102
+ module Time
103
+ def vocab; RDF::N3::Time.to_uri; end
104
+ module_function :vocab
105
+ autoload :DayOfWeek, 'rdf/n3/algebra/time/day_of_week'
106
+ autoload :Day, 'rdf/n3/algebra/time/day'
107
+ autoload :GmTime, 'rdf/n3/algebra/time/gm_time'
108
+ autoload :Hour, 'rdf/n3/algebra/time/hour'
109
+ autoload :InSeconds, 'rdf/n3/algebra/time/in_seconds'
110
+ autoload :LocalTime, 'rdf/n3/algebra/time/local_time'
111
+ autoload :Minute, 'rdf/n3/algebra/time/minute'
112
+ autoload :Month, 'rdf/n3/algebra/time/month'
113
+ autoload :Second, 'rdf/n3/algebra/time/second'
114
+ autoload :Timezone, 'rdf/n3/algebra/time/timezone'
115
+ autoload :Year, 'rdf/n3/algebra/time/year'
116
+ end
117
+
118
+ def for(uri)
119
+ {
120
+ RDF::N3::List.append => List.const_get(:Append),
121
+ RDF::N3::List.first => List.const_get(:First),
122
+ RDF::N3::List.in => List.const_get(:In),
123
+ RDF::N3::List.iterate => List.const_get(:Iterate),
124
+ RDF::N3::List.last => List.const_get(:Last),
125
+ RDF::N3::List.length => List.const_get(:Length),
126
+ RDF::N3::List.member => List.const_get(:Member),
127
+
128
+ RDF::N3::Log.conclusion => Log.const_get(:Conclusion),
129
+ RDF::N3::Log.conjunction => Log.const_get(:Conjunction),
130
+ RDF::N3::Log.content => Log.const_get(:Content),
131
+ RDF::N3::Log.dtlit => Log.const_get(:DtLit),
132
+ RDF::N3::Log.equalTo => Log.const_get(:EqualTo),
133
+ RDF::N3::Log.implies => Log.const_get(:Implies),
134
+ RDF::N3::Log.includes => Log.const_get(:Includes),
135
+ RDF::N3::Log.langlit => Log.const_get(:LangLit),
136
+ RDF::N3::Log.n3String => Log.const_get(:N3String),
137
+ RDF::N3::Log.notEqualTo => Log.const_get(:NotEqualTo),
138
+ RDF::N3::Log.notIncludes => Log.const_get(:NotIncludes),
139
+ RDF::N3::Log.outputString => Log.const_get(:OutputString),
140
+ RDF::N3::Log.parsedAsN3 => Log.const_get(:ParsedAsN3),
141
+ RDF::N3::Log.semantics => Log.const_get(:Semantics),
142
+ RDF::N3::Log.supports => NotImplemented,
143
+
144
+ RDF::N3::Math.absoluteValue => Math.const_get(:AbsoluteValue),
145
+ RDF::N3::Math.acos => Math.const_get(:ACos),
146
+ RDF::N3::Math.asin => Math.const_get(:ASin),
147
+ RDF::N3::Math.atan => Math.const_get(:ATan),
148
+ RDF::N3::Math.acosh => Math.const_get(:ACosH),
149
+ RDF::N3::Math.asinh => Math.const_get(:ASinH),
150
+ RDF::N3::Math.atanh => Math.const_get(:ATanH),
151
+ RDF::N3::Math.ceiling => Math.const_get(:Ceiling),
152
+ RDF::N3::Math.ceiling => Math.const_get(:Ceiling),
153
+ RDF::N3::Math.cos => Math.const_get(:Cos),
154
+ RDF::N3::Math.cosh => Math.const_get(:CosH),
155
+ RDF::N3::Math.difference => Math.const_get(:Difference),
156
+ RDF::N3::Math.equalTo => Math.const_get(:EqualTo),
157
+ RDF::N3::Math.exponentiation => Math.const_get(:Exponentiation),
158
+ RDF::N3::Math.floor => Math.const_get(:Floor),
159
+ RDF::N3::Math.greaterThan => Math.const_get(:GreaterThan),
160
+ RDF::N3::Math.lessThan => Math.const_get(:LessThan),
161
+ RDF::N3::Math.negation => Math.const_get(:Negation),
162
+ RDF::N3::Math.notEqualTo => Math.const_get(:NotEqualTo),
163
+ RDF::N3::Math.notGreaterThan => Math.const_get(:NotGreaterThan),
164
+ RDF::N3::Math.notLessThan => Math.const_get(:NotLessThan),
165
+ RDF::N3::Math.product => Math.const_get(:Product),
166
+ RDF::N3::Math.quotient => Math.const_get(:Quotient),
167
+ RDF::N3::Math.remainder => Math.const_get(:Remainder),
168
+ RDF::N3::Math.rounded => Math.const_get(:Rounded),
169
+ RDF::N3::Math.sin => Math.const_get(:Sin),
170
+ RDF::N3::Math.sinh => Math.const_get(:SinH),
171
+ RDF::N3::Math.tan => Math.const_get(:Tan),
172
+ RDF::N3::Math.tanh => Math.const_get(:TanH),
173
+ RDF::N3::Math[:sum] => Math.const_get(:Sum),
174
+
175
+ RDF::N3::Str.concatenation => Str.const_get(:Concatenation),
176
+ RDF::N3::Str.contains => Str.const_get(:Contains),
177
+ RDF::N3::Str.containsIgnoringCase => Str.const_get(:ContainsIgnoringCase),
178
+ RDF::N3::Str.containsRoughly => NotImplemented,
179
+ RDF::N3::Str.endsWith => Str.const_get(:EndsWith),
180
+ RDF::N3::Str.equalIgnoringCase => Str.const_get(:EqualIgnoringCase),
181
+ RDF::N3::Str.format => Str.const_get(:Format),
182
+ RDF::N3::Str.greaterThan => Str.const_get(:GreaterThan),
183
+ RDF::N3::Str.lessThan => Str.const_get(:LessThan),
184
+ RDF::N3::Str.matches => Str.const_get(:Matches),
185
+ RDF::N3::Str.notEqualIgnoringCase => Str.const_get(:NotEqualIgnoringCase),
186
+ RDF::N3::Str.notGreaterThan => Str.const_get(:NotGreaterThan),
187
+ RDF::N3::Str.notLessThan => Str.const_get(:NotLessThan),
188
+ RDF::N3::Str.notMatches => Str.const_get(:NotMatches),
189
+ RDF::N3::Str.replace => Str.const_get(:Replace),
190
+ RDF::N3::Str.scrape => Str.const_get(:Scrape),
191
+ RDF::N3::Str.startsWith => Str.const_get(:StartsWith),
192
+
193
+ RDF::N3::Time.dayOfWeek => Time.const_get(:DayOfWeek),
194
+ RDF::N3::Time.day => Time.const_get(:Day),
195
+ RDF::N3::Time.gmTime => Time.const_get(:GmTime),
196
+ RDF::N3::Time.hour => Time.const_get(:Hour),
197
+ RDF::N3::Time.inSeconds => Time.const_get(:InSeconds),
198
+ RDF::N3::Time.localTime => Time.const_get(:LocalTime),
199
+ RDF::N3::Time.minute => Time.const_get(:Minute),
200
+ RDF::N3::Time.month => Time.const_get(:Month),
201
+ RDF::N3::Time.second => Time.const_get(:Second),
202
+ RDF::N3::Time.timeZone => Time.const_get(:Timezone),
203
+ RDF::N3::Time.year => Time.const_get(:Year),
204
+ }[uri]
205
+ end
206
+ module_function :for
207
+ end
208
+ end
209
+
210
+
@@ -0,0 +1,221 @@
1
+ # frozen_string_literal: true
2
+ require 'rdf'
3
+ require 'rdf/n3/terminals'
4
+
5
+ # Monkey-patch RDF::Enumerable to add `:existentials` and `:univerals` accessors
6
+ module RDF
7
+ module Enumerable
8
+ # Existential quantifiers defined on this enumerable
9
+ # @return [Array<RDF::Query::Variable>]
10
+ attr_accessor :existentials
11
+
12
+ # Universal quantifiers defined on this enumerable
13
+ # @return [Array<RDF::Query::Variable>]
14
+ attr_accessor :universals
15
+ end
16
+
17
+ class List
18
+ ##
19
+ # A list is variable if any of its members are variable?
20
+ #
21
+ # @return [Boolean]
22
+ def variable?
23
+ to_a.any?(&:variable?)
24
+ end
25
+
26
+ # Transform Statement into an SXP
27
+ # @return [Array]
28
+ def to_sxp_bin
29
+ to_a.to_sxp_bin
30
+ end
31
+
32
+ ##
33
+ # Returns an S-Expression (SXP) representation
34
+ #
35
+ # @return [String]
36
+ def to_sxp(**options)
37
+ to_a.to_sxp_bin.to_sxp(**options)
38
+ end
39
+ end
40
+
41
+ module Value
42
+ ##
43
+ # Returns `true` if `self` is a {RDF::N3::Algebra::Formula}.
44
+ #
45
+ # @return [Boolean]
46
+ def formula?
47
+ false
48
+ end
49
+
50
+ # By default, returns itself. Can be used for terms such as blank nodes to be turned into non-disinguished variables.
51
+ #
52
+ # @param [RDF::Node] scope
53
+ # return [RDF::Query::Variable]
54
+ def to_ndvar(scope)
55
+ self
56
+ end
57
+ end
58
+
59
+ module Term
60
+ ##
61
+ # Is this the same term? Like `#eql?`, but no variable matching
62
+ def sameTerm?(other)
63
+ eql?(other)
64
+ end
65
+
66
+ ##
67
+ # Parse the value as a numeric literal, or return 0.
68
+ #
69
+ # @return [RDF::Literal::Numeric]
70
+ def as_number
71
+ RDF::Literal(0)
72
+ end
73
+
74
+ ##
75
+ # Parse the value as a dateTime literal, or return now.
76
+ #
77
+ # @return [RDF::Literal::DateTime]
78
+ def as_datetime
79
+ RDF::Literal::DateTime.new(DateTime.now)
80
+ end
81
+ end
82
+
83
+ class Literal
84
+ include RDF::N3::Terminals
85
+
86
+ ##
87
+ # Parse the value as a numeric literal, or return 0.
88
+ #
89
+ # @return [RDF::Literal::Numeric]
90
+ def as_number
91
+ return self if self.is_a?(RDF::Literal::Numeric)
92
+ case value
93
+ when DOUBLE then RDF::Literal::Double.new(value)
94
+ when DECIMAL then RDF::Literal::Decimal.new(value)
95
+ when INTEGER then RDF::Literal::Integer.new(value)
96
+ else
97
+ RDF::Literal(0)
98
+ end
99
+ end
100
+
101
+ ##
102
+ # Parse the value as a dateTime literal, or return now.
103
+ #
104
+ # @return [RDF::Literal::DateTime]
105
+ def as_datetime
106
+ return self if is_a?(RDF::Literal::DateTime)
107
+ mvalue = value
108
+ mvalue = "#{mvalue}-01" if mvalue.match?(%r(^\d{4}$))
109
+ mvalue = "#{mvalue}-01" if mvalue.match?(%r(^\d{4}-\d{2}$))
110
+ RDF::Literal::DateTime.new(::DateTime.iso8601(mvalue), lexical: value)
111
+ rescue
112
+ RDF::Literal(0)
113
+ end
114
+ end
115
+
116
+ class Node
117
+ # Transform to a nondistinguished exisetntial variable in a formula scope
118
+ #
119
+ # @param [RDF::Node] scope
120
+ # return [RDF::Query::Variable]
121
+ def to_ndvar(scope)
122
+ label = "#{id}_#{scope ? scope.id : 'base'}_undext"
123
+ RDF::Query::Variable.new(label, existential: true, distinguished: false)
124
+ end
125
+ end
126
+
127
+ class Query
128
+ class Pattern
129
+ ##
130
+ # Overrides `#initialize!` to turn blank nodes into non-distinguished variables, if the `:ndvars` option is set.
131
+ alias_method :orig_initialize!, :initialize!
132
+ def initialize!
133
+ if @options[:ndvars]
134
+ @graph_name = @graph_name.to_ndvar(nil) if @graph_name
135
+ @subject = @subject.to_ndvar(@graph_name)
136
+ @predicate = @predicate.to_ndvar(@graph_name)
137
+ @object = @object.to_ndvar(@graph_name)
138
+ end
139
+ orig_initialize!
140
+ end
141
+
142
+ ##
143
+ # Checks pattern equality against a statement, considering nesting an lists.
144
+ #
145
+ # * A pattern which has a pattern as a subject or an object, matches
146
+ # a statement having a statement as a subject or an object using {#eql?}.
147
+ #
148
+ # @param [Statement] other
149
+ # @return [Boolean]
150
+ #
151
+ # @see RDF::URI#==
152
+ # @see RDF::Node#==
153
+ # @see RDF::Literal#==
154
+ # @see RDF::Query::Variable#==
155
+ def eql?(other)
156
+ return false unless other.is_a?(RDF::Statement) && (self.graph_name || false) == (other.graph_name || false)
157
+
158
+ [:subject, :predicate, :object].each do |part|
159
+ case o = self.send(part)
160
+ when RDF::Query::Pattern, RDF::List
161
+ return false unless o.eql?(other.send(part))
162
+ else
163
+ return false unless o == other.send(part)
164
+ end
165
+ end
166
+ true
167
+ end
168
+ end
169
+
170
+ class Solution
171
+ # Transform Statement into an SXP
172
+ # @return [Array]
173
+ def to_sxp_bin
174
+ [:solution] + bindings.map do |k, v|
175
+ existential = k.to_s.end_with?('ext')
176
+ k = k.to_s.sub(/_(?:und)?ext$/, '').to_sym
177
+ distinguished = !k.to_s.end_with?('undext')
178
+ Query::Variable.new(k, v, existential: existential, distinguished: distinguished).to_sxp_bin
179
+ end
180
+ end
181
+ end
182
+
183
+ class Variable
184
+ ##
185
+ # True if the other is the same variable
186
+ def sameTerm?(other)
187
+ other.is_a?(::RDF::Query::Variable) && name.eql?(other.name)
188
+ end
189
+
190
+ ##
191
+ # Parse the value as a numeric literal, or return 0.
192
+ #
193
+ # @return [RDF::Literal::Numeric]
194
+ def as_number
195
+ RDF::Literal(0)
196
+ end
197
+ end
198
+ end
199
+ end
200
+
201
+ module SPARQL
202
+ module Algebra
203
+ class Operator
204
+ ##
205
+ # Map of related formulae, indexed by graph name.
206
+ #
207
+ # @return [Hash{RDF::Resource => RDF::N3::Algebra::Formula}]
208
+ def formulae
209
+ @options.fetch(:formulae, {})
210
+ end
211
+
212
+ # Updates the operands for this operator.
213
+ #
214
+ # @param [Array] ary
215
+ # @return [Array]
216
+ def operands=(ary)
217
+ @operands = ary
218
+ end
219
+ end
220
+ end
221
+ end
data/lib/rdf/n3/format.rb CHANGED
@@ -15,7 +15,7 @@ module RDF::N3
15
15
  # @example Obtaining serialization format file extension mappings
16
16
  # RDF::Format.file_extensions #=> {n3: "text/n3"}
17
17
  #
18
- # @see http://www.w3.org/TR/rdf-testcases/#ntriples
18
+ # @see https://www.w3.org/TR/rdf-testcases/#ntriples
19
19
  class Format < RDF::Format
20
20
  content_type 'text/n3', extension: :n3, aliases: %w(text/rdf+n3;q=0.2 application/rdf+n3;q=0.2)
21
21
  content_encoding 'utf-8'
@@ -27,5 +27,70 @@ module RDF::N3
27
27
  def self.symbols
28
28
  [:n3, :notation3]
29
29
  end
30
+
31
+ ##
32
+ # Hash of CLI commands appropriate for this format
33
+ # @return [Hash{Symbol => Hash}]
34
+ def self.cli_commands
35
+ {
36
+ reason: {
37
+ description: "Reason over formulae.",
38
+ help: "reason [--think] file\nPerform Notation-3 reasoning.",
39
+ parse: false,
40
+ # Only shows when input and output format set
41
+ filter: {format: :n3},
42
+ repository: RDF::N3::Repository.new,
43
+ lambda: ->(argv, **options) do
44
+ repository = options[:repository]
45
+ result_repo = RDF::N3::Repository.new
46
+ RDF::CLI.parse(argv, format: :n3, list_terms: true, **options) do |reader|
47
+ reasoner = RDF::N3::Reasoner.new(reader, **options)
48
+ reasoner.reason!(**options)
49
+ if options[:conclusions]
50
+ result_repo << reasoner.conclusions
51
+ elsif options[:data]
52
+ result_repo << reasoner.data
53
+ else
54
+ result_repo << reasoner
55
+ end
56
+ end
57
+
58
+ # Replace input repository with results
59
+ repository.clear!
60
+ repository << result_repo
61
+ end,
62
+ options: [
63
+ RDF::CLI::Option.new(
64
+ symbol: :conclusions,
65
+ datatype: TrueClass,
66
+ control: :checkbox,
67
+ use: :optional,
68
+ on: ["--conclusions"],
69
+ description: "Exclude formulae and statements in the original dataset."),
70
+ RDF::CLI::Option.new(
71
+ symbol: :data,
72
+ datatype: TrueClass,
73
+ control: :checkbox,
74
+ use: :optional,
75
+ on: ["--data"],
76
+ description: "Only results from default graph, excluding formulae or variables."),
77
+ RDF::CLI::Option.new(
78
+ symbol: :strings,
79
+ datatype: TrueClass,
80
+ control: :checkbox,
81
+ use: :optional,
82
+ on: ["--strings"],
83
+ description: "Returns the concatenated strings from log:outputString."),
84
+ RDF::CLI::Option.new(
85
+ symbol: :think,
86
+ datatype: TrueClass,
87
+ control: :checkbox,
88
+ use: :optional,
89
+ on: ["--think"],
90
+ description: "Continuously execute until results stop growing."),
91
+ ]
92
+ },
93
+ }
94
+ end
30
95
  end
31
96
  end