rdf-n3 2.2.0 → 3.1.2

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 (101) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +192 -69
  3. data/UNLICENSE +1 -1
  4. data/VERSION +1 -1
  5. data/lib/rdf/n3.rb +11 -8
  6. data/lib/rdf/n3/algebra.rb +204 -0
  7. data/lib/rdf/n3/algebra/builtin.rb +79 -0
  8. data/lib/rdf/n3/algebra/formula.rb +446 -0
  9. data/lib/rdf/n3/algebra/list/append.rb +42 -0
  10. data/lib/rdf/n3/algebra/list/first.rb +24 -0
  11. data/lib/rdf/n3/algebra/list/in.rb +48 -0
  12. data/lib/rdf/n3/algebra/list/last.rb +24 -0
  13. data/lib/rdf/n3/algebra/list/length.rb +24 -0
  14. data/lib/rdf/n3/algebra/list/member.rb +44 -0
  15. data/lib/rdf/n3/algebra/list_operator.rb +83 -0
  16. data/lib/rdf/n3/algebra/log/conclusion.rb +65 -0
  17. data/lib/rdf/n3/algebra/log/conjunction.rb +36 -0
  18. data/lib/rdf/n3/algebra/log/content.rb +34 -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/n3_string.rb +34 -0
  23. data/lib/rdf/n3/algebra/log/not_equal_to.rb +23 -0
  24. data/lib/rdf/n3/algebra/log/not_includes.rb +27 -0
  25. data/lib/rdf/n3/algebra/log/output_string.rb +40 -0
  26. data/lib/rdf/n3/algebra/log/parsed_as_n3.rb +36 -0
  27. data/lib/rdf/n3/algebra/log/semantics.rb +40 -0
  28. data/lib/rdf/n3/algebra/math/absolute_value.rb +36 -0
  29. data/lib/rdf/n3/algebra/math/acos.rb +26 -0
  30. data/lib/rdf/n3/algebra/math/acosh.rb +26 -0
  31. data/lib/rdf/n3/algebra/math/asin.rb +26 -0
  32. data/lib/rdf/n3/algebra/math/asinh.rb +26 -0
  33. data/lib/rdf/n3/algebra/math/atan.rb +26 -0
  34. data/lib/rdf/n3/algebra/math/atanh.rb +26 -0
  35. data/lib/rdf/n3/algebra/math/ceiling.rb +28 -0
  36. data/lib/rdf/n3/algebra/math/cos.rb +40 -0
  37. data/lib/rdf/n3/algebra/math/cosh.rb +38 -0
  38. data/lib/rdf/n3/algebra/math/difference.rb +40 -0
  39. data/lib/rdf/n3/algebra/math/equal_to.rb +54 -0
  40. data/lib/rdf/n3/algebra/math/exponentiation.rb +35 -0
  41. data/lib/rdf/n3/algebra/math/floor.rb +28 -0
  42. data/lib/rdf/n3/algebra/math/greater_than.rb +41 -0
  43. data/lib/rdf/n3/algebra/math/less_than.rb +41 -0
  44. data/lib/rdf/n3/algebra/math/negation.rb +38 -0
  45. data/lib/rdf/n3/algebra/math/not_equal_to.rb +25 -0
  46. data/lib/rdf/n3/algebra/math/not_greater_than.rb +25 -0
  47. data/lib/rdf/n3/algebra/math/not_less_than.rb +25 -0
  48. data/lib/rdf/n3/algebra/math/product.rb +20 -0
  49. data/lib/rdf/n3/algebra/math/quotient.rb +36 -0
  50. data/lib/rdf/n3/algebra/math/remainder.rb +35 -0
  51. data/lib/rdf/n3/algebra/math/rounded.rb +26 -0
  52. data/lib/rdf/n3/algebra/math/sin.rb +40 -0
  53. data/lib/rdf/n3/algebra/math/sinh.rb +38 -0
  54. data/lib/rdf/n3/algebra/math/sum.rb +40 -0
  55. data/lib/rdf/n3/algebra/math/tan.rb +40 -0
  56. data/lib/rdf/n3/algebra/math/tanh.rb +38 -0
  57. data/lib/rdf/n3/algebra/not_implemented.rb +13 -0
  58. data/lib/rdf/n3/algebra/resource_operator.rb +123 -0
  59. data/lib/rdf/n3/algebra/str/concatenation.rb +27 -0
  60. data/lib/rdf/n3/algebra/str/contains.rb +33 -0
  61. data/lib/rdf/n3/algebra/str/contains_ignoring_case.rb +33 -0
  62. data/lib/rdf/n3/algebra/str/ends_with.rb +33 -0
  63. data/lib/rdf/n3/algebra/str/equal_ignoring_case.rb +34 -0
  64. data/lib/rdf/n3/algebra/str/format.rb +17 -0
  65. data/lib/rdf/n3/algebra/str/greater_than.rb +38 -0
  66. data/lib/rdf/n3/algebra/str/less_than.rb +33 -0
  67. data/lib/rdf/n3/algebra/str/matches.rb +37 -0
  68. data/lib/rdf/n3/algebra/str/not_equal_ignoring_case.rb +17 -0
  69. data/lib/rdf/n3/algebra/str/not_greater_than.rb +17 -0
  70. data/lib/rdf/n3/algebra/str/not_less_than.rb +17 -0
  71. data/lib/rdf/n3/algebra/str/not_matches.rb +18 -0
  72. data/lib/rdf/n3/algebra/str/replace.rb +35 -0
  73. data/lib/rdf/n3/algebra/str/scrape.rb +35 -0
  74. data/lib/rdf/n3/algebra/str/starts_with.rb +33 -0
  75. data/lib/rdf/n3/algebra/time/day.rb +35 -0
  76. data/lib/rdf/n3/algebra/time/day_of_week.rb +27 -0
  77. data/lib/rdf/n3/algebra/time/gm_time.rb +29 -0
  78. data/lib/rdf/n3/algebra/time/hour.rb +35 -0
  79. data/lib/rdf/n3/algebra/time/in_seconds.rb +59 -0
  80. data/lib/rdf/n3/algebra/time/local_time.rb +29 -0
  81. data/lib/rdf/n3/algebra/time/minute.rb +35 -0
  82. data/lib/rdf/n3/algebra/time/month.rb +35 -0
  83. data/lib/rdf/n3/algebra/time/second.rb +35 -0
  84. data/lib/rdf/n3/algebra/time/timezone.rb +36 -0
  85. data/lib/rdf/n3/algebra/time/year.rb +29 -0
  86. data/lib/rdf/n3/extensions.rb +221 -0
  87. data/lib/rdf/n3/format.rb +66 -1
  88. data/lib/rdf/n3/list.rb +630 -0
  89. data/lib/rdf/n3/reader.rb +834 -492
  90. data/lib/rdf/n3/reasoner.rb +282 -0
  91. data/lib/rdf/n3/refinements.rb +178 -0
  92. data/lib/rdf/n3/repository.rb +332 -0
  93. data/lib/rdf/n3/terminals.rb +80 -0
  94. data/lib/rdf/n3/vocab.rb +36 -3
  95. data/lib/rdf/n3/writer.rb +476 -239
  96. metadata +187 -68
  97. data/AUTHORS +0 -1
  98. data/History.markdown +0 -99
  99. data/lib/rdf/n3/patches/array_hacks.rb +0 -53
  100. data/lib/rdf/n3/reader/meta.rb +0 -641
  101. data/lib/rdf/n3/reader/parser.rb +0 -237
@@ -1,237 +0,0 @@
1
- # coding: utf-8
2
- # Simple parser to go through productions without attempting evaluation
3
- module RDF::N3
4
- module Parser
5
- START = 'http://www.w3.org/2000/10/swap/grammar/n3#document'
6
- R_WHITESPACE = Regexp.compile('\A\s*(?:#.*$)?')
7
- R_MLSTRING = Regexp.compile("^.*([^\"\\\\]*)\"\"\"")
8
- SINGLE_CHARACTER_SELECTORS = %{\t\r\n !\"#$\%&'()*.,+/;<=>?[\\]^`{|}~}
9
- NOT_QNAME_CHARS = SINGLE_CHARACTER_SELECTORS + "@"
10
- NOT_NAME_CHARS = NOT_QNAME_CHARS + ":"
11
-
12
- def error(str)
13
- log_error(str, lineno: @lineno, exception: RDF::ReaderError)
14
- end
15
-
16
- def parse(prod)
17
- todo_stack = [{prod: prod, terms: nil}]
18
- while !todo_stack.empty?
19
- pushed = false
20
- if todo_stack.last[:terms].nil?
21
- todo_stack.last[:terms] = []
22
- tok = self.token
23
- #log_debug("parse tok: '#{tok}'") {"prod #{todo_stack.last[:prod]}"}
24
-
25
- # Got an opened production
26
- onStart(abbr(todo_stack.last[:prod]))
27
- break if tok.nil?
28
-
29
- cur_prod = todo_stack.last[:prod]
30
- prod_branch = @branches[cur_prod]
31
- error("No branches found for '#{abbr(cur_prod)}'") if prod_branch.nil?
32
- sequence = prod_branch[tok]
33
- if sequence.nil?
34
- dump_stack(todo_stack) if $verbose
35
- expected = prod_branch.values.uniq.map {|u| u.map {|v| abbr(v).inspect}.join(",")}
36
- error("Found '#{tok}' when parsing a #{abbr(cur_prod)}. expected #{expected.join(' | ')}")
37
- end
38
- #log_debug("sequence") {sequence.inspect}
39
- todo_stack.last[:terms] += sequence
40
- end
41
-
42
- #log_debug("parse") {todo_stack.last.inspect}
43
- while !todo_stack.last[:terms].to_a.empty?
44
- term = todo_stack.last[:terms].shift
45
- if term.is_a?(String)
46
- log_debug("parse term(string)") {term.to_s}
47
- word = buffer[0, term.length]
48
- if word == term
49
- onToken(term, word)
50
- consume(term.length)
51
- elsif '@' + word.chop == term && @keywords.include?(word.chop)
52
- onToken(term, word.chop)
53
- consume(term.length - 1)
54
- else
55
- error("Found '#{buffer[0, 10]}...'; #{term} expected")
56
- end
57
- elsif regexp = @regexps[term]
58
- if abbr(term) == 'string' && buffer[0, 3] == '"""'
59
- # Read until end of multi-line comment if this is the start of a multi-line comment
60
- string = '"""'
61
- consume(3)
62
- next_line = buffer
63
- #log_debug("ml-str(start)") {next_line.dump}
64
- until md = R_MLSTRING.match(next_line)
65
- begin
66
- string += next_line
67
- next_line = readline
68
- rescue EOFError
69
- error("EOF reached searching for end of multi-line comment")
70
- end
71
- end
72
- string += md[0].to_s
73
- consume(md[0].to_s.length)
74
- onToken('string', string)
75
- #log_debug("ml-str now") {buffer.dump}
76
- else
77
- md = regexp.match(buffer)
78
- error("Token(#{abbr(term)}) '#{buffer[0, 10]}...' should match #{regexp}") unless md
79
- log_debug("parse") {"term(#{abbr(term)}:regexp): #{term}, #{regexp}.match('#{buffer[0, 10]}...') => '#{md.inspect.force_encoding(Encoding::UTF_8)}'"}
80
- onToken(abbr(term), md.to_s)
81
- consume(md[0].length)
82
- end
83
- else
84
- log_debug("parse term(push)") {term}
85
- todo_stack << {prod: term, terms: nil}
86
- pushed = true
87
- break
88
- end
89
- self.token
90
- end
91
-
92
- while !pushed && todo_stack.last[:terms].to_a.empty?
93
- todo_stack.pop
94
- self.onFinish
95
- end
96
- end
97
- while !todo_stack.empty?
98
- todo_stack.pop
99
- self.onFinish
100
- end
101
- end
102
-
103
- # Memoizer for get_token
104
- def token
105
- unless @memo.has_key?(@pos)
106
- tok = self.get_token
107
- @memo[@pos] = tok
108
- log_debug("token") {"'#{tok}'('#{buffer[0, 10]}...')"} if buffer
109
- end
110
- @memo[@pos]
111
- end
112
-
113
- def get_token
114
- whitespace
115
-
116
- return nil if buffer.nil?
117
-
118
- ch2 = buffer[0, 2]
119
- return ch2 if %w(=> <= ^^).include?(ch2)
120
-
121
- ch = buffer[0, 1]
122
- @keyword_mode = false if ch == '.' && @keyword_mode
123
-
124
- return ch if SINGLE_CHARACTER_SELECTORS.include?(ch)
125
- return ":" if ch == ":"
126
- return "0" if "+-0123456789".include?(ch)
127
-
128
- if ch == '@'
129
- return '@' if @pos > 0 && @line[@pos-1, 1] == '"'
130
-
131
- j = 0
132
- j += 1 while buffer[j+1, 1] && !NOT_NAME_CHARS.include?(buffer[j+1, 1])
133
- name = buffer[1, j]
134
- if name == 'keywords'
135
- @keywords = []
136
- @keyword_mode = true
137
- end
138
- return '@' + name
139
- end
140
-
141
- j = 0
142
- j += 1 while buffer[j, 1] && !NOT_QNAME_CHARS.include?(buffer[j, 1])
143
- word = buffer[0, j]
144
- error("Tokenizer expected qname, found #{buffer[0, 10]}") unless word
145
- if @keyword_mode
146
- @keywords << word
147
- elsif @keywords.include?(word)
148
- if word == 'keywords'
149
- @keywords = []
150
- @keyword_mode = true
151
- end
152
- return '@' + word.to_s # implicit keyword
153
- end
154
-
155
- 'a'
156
- end
157
-
158
- def whitespace
159
- while buffer && md = R_WHITESPACE.match(buffer)
160
- return unless md[0].length > 0
161
- consume(md[0].length)
162
- #log_debug("ws") {"'#{md[0]}', pos=#{@pos}"}
163
- end
164
- end
165
-
166
- def readline
167
- @line = @input.readline
168
- @lineno += 1
169
- @line.force_encoding(Encoding::UTF_8)
170
- log_debug("readline[#{@lineno}]") {@line.dump}
171
- @pos = 0
172
- @line
173
- rescue EOFError
174
- @line, @pos = nil, 0
175
- end
176
-
177
- # Return data from current off set to end of line
178
- def buffer
179
- @line[@pos, @line.length - @pos] unless @line.nil?
180
- end
181
-
182
- # Cause n characters of line to be consumed. Read new line while line is empty or until eof
183
- def consume(n)
184
- @memo = {}
185
- @pos += n
186
- readline while @line && @line.length <= @pos
187
- #log_debug("consume[#{n}]") {buffer}
188
- end
189
-
190
- def abbr(prodURI)
191
- prodURI.to_s.split('#').last
192
- end
193
-
194
- def onStart(prod)
195
- $stdout.puts ' ' * @productions.length + prod
196
- @productions << prod
197
- end
198
-
199
- def onFinish
200
- prod = @productions.pop()
201
- $stdout.puts ' ' * @productions.length + '/' + prod
202
- end
203
-
204
- def onToken(prod, tok)
205
- $stdout.puts ' ' * @productions.length + "#{prod}(#{tok})"
206
- end
207
-
208
- def dump_stack(stack)
209
- STDERR.puts "\nstack trace:"
210
- stack.reverse.each do |se|
211
- STDERR.puts "#{se[:prod]}"
212
- STDERR.puts " " + case se[:terms]
213
- when nil then "nil"
214
- when [] then "empty"
215
- else se[:terms].join(",\n ")
216
- end
217
- end
218
- end
219
-
220
- def test(input, branches, regexps)
221
- # FIXME: for now, read in entire doc, eventually, process as stream
222
- @input = input.respond_to?(:read) ? (input.rewind; input) : StringIO.new(input.to_s)
223
- @lineno = 0
224
- readline # Prime the pump
225
- $stdout ||= STDOUT
226
-
227
- @memo = {}
228
- @keyword_mode = false
229
- @keywords = %w(a is of this has)
230
- @productions = []
231
-
232
- @branches = branches
233
- @regexps = regexps
234
- parse(START.to_sym)
235
- end
236
- end
237
- end