rdf-n3 3.1.1 → 3.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (124) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +148 -69
  3. data/UNLICENSE +1 -1
  4. data/VERSION +1 -1
  5. data/lib/rdf/n3.rb +8 -8
  6. data/lib/rdf/n3/algebra.rb +147 -68
  7. data/lib/rdf/n3/algebra/builtin.rb +79 -0
  8. data/lib/rdf/n3/algebra/formula.rb +355 -94
  9. data/lib/rdf/n3/algebra/list/append.rb +33 -4
  10. data/lib/rdf/n3/algebra/list/first.rb +24 -0
  11. data/lib/rdf/n3/algebra/list/in.rb +42 -3
  12. data/lib/rdf/n3/algebra/list/last.rb +17 -4
  13. data/lib/rdf/n3/algebra/list/length.rb +24 -0
  14. data/lib/rdf/n3/algebra/list/member.rb +39 -2
  15. data/lib/rdf/n3/algebra/list_operator.rb +83 -0
  16. data/lib/rdf/n3/algebra/log/conclusion.rb +57 -1
  17. data/lib/rdf/n3/algebra/log/conjunction.rb +28 -1
  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 +55 -30
  21. data/lib/rdf/n3/algebra/log/includes.rb +58 -1
  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 +34 -3
  39. data/lib/rdf/n3/algebra/math/equal_to.rb +54 -0
  40. data/lib/rdf/n3/algebra/math/exponentiation.rb +29 -3
  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 +31 -2
  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 +14 -3
  49. data/lib/rdf/n3/algebra/math/quotient.rb +30 -3
  50. data/lib/rdf/n3/algebra/math/remainder.rb +29 -3
  51. data/lib/rdf/n3/algebra/math/rounded.rb +20 -3
  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 +35 -4
  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 +21 -3
  60. data/lib/rdf/n3/algebra/str/contains.rb +28 -4
  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 +12 -4
  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 +33 -5
  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 +28 -5
  73. data/lib/rdf/n3/algebra/str/scrape.rb +31 -5
  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 +180 -21
  87. data/lib/rdf/n3/format.rb +65 -0
  88. data/lib/rdf/n3/list.rb +630 -0
  89. data/lib/rdf/n3/reader.rb +762 -485
  90. data/lib/rdf/n3/reasoner.rb +57 -68
  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 +35 -7
  95. data/lib/rdf/n3/writer.rb +208 -148
  96. metadata +110 -52
  97. data/AUTHORS +0 -1
  98. data/History.markdown +0 -99
  99. data/lib/rdf/n3/algebra/log/equalTo.rb +0 -7
  100. data/lib/rdf/n3/algebra/log/notEqualTo.rb +0 -7
  101. data/lib/rdf/n3/algebra/log/notIncludes.rb +0 -12
  102. data/lib/rdf/n3/algebra/log/outputString.rb +0 -7
  103. data/lib/rdf/n3/algebra/math/absoluteValue.rb +0 -9
  104. data/lib/rdf/n3/algebra/math/equalTo.rb +0 -9
  105. data/lib/rdf/n3/algebra/math/greaterThan.rb +0 -9
  106. data/lib/rdf/n3/algebra/math/integerQuotient.rb +0 -9
  107. data/lib/rdf/n3/algebra/math/lessThan.rb +0 -9
  108. data/lib/rdf/n3/algebra/math/memberCount.rb +0 -9
  109. data/lib/rdf/n3/algebra/math/notEqualTo.rb +0 -9
  110. data/lib/rdf/n3/algebra/math/notGreaterThan.rb +0 -9
  111. data/lib/rdf/n3/algebra/math/notLessThan.rb +0 -9
  112. data/lib/rdf/n3/algebra/str/containsIgnoringCase.rb +0 -9
  113. data/lib/rdf/n3/algebra/str/endsWith.rb +0 -9
  114. data/lib/rdf/n3/algebra/str/equalIgnoringCase.rb +0 -9
  115. data/lib/rdf/n3/algebra/str/greaterThan.rb +0 -9
  116. data/lib/rdf/n3/algebra/str/lessThan.rb +0 -9
  117. data/lib/rdf/n3/algebra/str/notEqualIgnoringCase.rb +0 -9
  118. data/lib/rdf/n3/algebra/str/notGreaterThan.rb +0 -9
  119. data/lib/rdf/n3/algebra/str/notLessThan.rb +0 -9
  120. data/lib/rdf/n3/algebra/str/notMatches.rb +0 -9
  121. data/lib/rdf/n3/algebra/str/startsWith.rb +0 -56
  122. data/lib/rdf/n3/patches/array_hacks.rb +0 -53
  123. data/lib/rdf/n3/reader/meta.rb +0 -641
  124. data/lib/rdf/n3/reader/parser.rb +0 -239
@@ -1,239 +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}'", depth: depth) {"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", depth: depth) {sequence.inspect}
39
- todo_stack.last[:terms] += sequence
40
- end
41
-
42
- #log_debug("parse", depth: depth) {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)", depth: depth) {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)", depth: depth) {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", depth: depth) {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", depth: depth) {"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)", depth: depth) {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", depth: depth) {"'#{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", depth: depth) {"'#{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}]", depth: depth) {@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}]", depth: depth) {buffer}
188
- end
189
-
190
- def abbr(prodURI)
191
- prodURI.to_s.split('#').last
192
- end
193
-
194
- def depth; (@productions || []).length; end
195
-
196
- def onStart(prod)
197
- $stdout.puts ' ' * @productions.length + prod
198
- @productions << prod
199
- end
200
-
201
- def onFinish
202
- prod = @productions.pop()
203
- $stdout.puts ' ' * @productions.length + '/' + prod
204
- end
205
-
206
- def onToken(prod, tok)
207
- $stdout.puts ' ' * @productions.length + "#{prod}(#{tok})"
208
- end
209
-
210
- def dump_stack(stack)
211
- STDERR.puts "\nstack trace:"
212
- stack.reverse.each do |se|
213
- STDERR.puts "#{se[:prod]}"
214
- STDERR.puts " " + case se[:terms]
215
- when nil then "nil"
216
- when [] then "empty"
217
- else se[:terms].join(",\n ")
218
- end
219
- end
220
- end
221
-
222
- def test(input, branches, regexps)
223
- # FIXME: for now, read in entire doc, eventually, process as stream
224
- @input = input.respond_to?(:read) ? (input.rewind; input) : StringIO.new(input.to_s)
225
- @lineno = 0
226
- readline # Prime the pump
227
- $stdout ||= STDOUT
228
-
229
- @memo = {}
230
- @keyword_mode = false
231
- @keywords = %w(a is of this has)
232
- @productions = []
233
-
234
- @branches = branches
235
- @regexps = regexps
236
- parse(START.to_sym)
237
- end
238
- end
239
- end