bel_parser 1.0.0.alpha.44-java → 1.0.0.alpha.45-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 416b33009e3d409133f8223a32ef0176301d12bc
4
- data.tar.gz: 3c678c41ae5e773e8110657c49674d8be19c1a65
3
+ metadata.gz: 69df1fed65007b73516917a30f7db3d8bd7afbad
4
+ data.tar.gz: 4ef2f2d87fae044628aea867fba96e240a9f64f0
5
5
  SHA512:
6
- metadata.gz: 7606e22fb75a654fdd58ab057e97bf5c8c8c58df71bff2b23b88e940f10853194972b67e90bbb43d8dc66c5dbd369320c13bbfb0aecf41893d8730c9efc65731
7
- data.tar.gz: 65cb139c3a41b56a5741a095bc3601931880704b837b387fa69bc5ef34cc2ffb05dfb9ce8bbf456432f5b8ccc9edd0549dca655c1b569baf2c1d9ca2e25d5c59
6
+ metadata.gz: 5a698dbe8e5708c6c89ef32d5a322fc04c4bee3bf493a644877fee5ee91ad0eea3dc0ca34696c21ff6090b47aa2f5c4155de025b70a6f10216a0a28ca9509f65
7
+ data.tar.gz: 94ff77d63bd1a5ec4a502fa536a5c6d31afae1779f3d6c67e763682999cdf632e73dda5c655bbd607f19f80b23def3361c7655cc08b2b61680bc9d67c1becc73
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0.alpha.44
1
+ 1.0.0.alpha.45
data/bin/bel2_validator CHANGED
@@ -2,13 +2,69 @@
2
2
  $LOAD_PATH.unshift(
3
3
  File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib'))
4
4
 
5
- unless ARGV.first
6
- program = File.basename($PROGRAM_NAME)
7
- $stderr.puts <<-USAGE.gsub(/ {4}/, '')
8
- usage: #{program} [BEL specification version] [PREFIX=URI]...
5
+ require 'optparse'
6
+ require 'bel_parser'
7
+
8
+ options = {
9
+ uri_reader: BELParser::Resource.default_uri_reader,
10
+ url_reader: BELParser::Resource.default_url_reader,
11
+ spec: BELParser::Language.specification(
12
+ BELParser::Language.latest_supported_version)
13
+ }
14
+ OptionParser.new do |opts|
15
+ opts.banner = <<-USAGE.gsub(/^ {4}/, '')
16
+ Validates BEL 2.0 expressions.
17
+
18
+ Read from a BEL file.
19
+ usage: #$PROGRAM_NAME --file [FILE]
20
+
21
+ Read from standard input.
22
+ usage: #$PROGRAM_NAME
9
23
  USAGE
10
- exit 1
11
- end
24
+
25
+ opts.on('-f', '--file FILE', 'BEL script file to read.') do |bel|
26
+ options[:file] = bel
27
+ end
28
+
29
+ opts.on(
30
+ '-v',
31
+ '--[no-]verbose',
32
+ 'Enable verbose logging.') do |verbose|
33
+ options[:verbose] = verbose
34
+ end
35
+
36
+ opts.on(
37
+ '-s',
38
+ '--specification VERSION',
39
+ 'BEL specification version (e.g. 1.0, 2.0).') do |spec|
40
+ unless BELParser::Language.defines_version?(spec)
41
+ $stderr.puts %(Invalid BEL specification "#{spec}")
42
+ exit 1
43
+ end
44
+
45
+ options[:spec] = BELParser::Language.specification(spec)
46
+ end
47
+
48
+ opts.on(
49
+ '-q',
50
+ '--sparql-endpoint URL',
51
+ 'SPARQL endpoint URL containing BEL RDF resources.') do |sparql|
52
+
53
+ require 'bel_parser/resource/sparql_reader'
54
+ options[:uri_reader] = BELParser::Resource::SPARQLReader.new(sparql)
55
+ end
56
+
57
+ if RUBY_ENGINE =~ /^jruby/i
58
+ opts.on(
59
+ '-t',
60
+ '--read-jena-tdb DIRECTORY',
61
+ 'Jena TDB directory containing BEL RDF resources.') do |tdb|
62
+
63
+ require 'bel_parser/resource/jena_tdb_reader'
64
+ options[:uri_reader] = BELParser::Resource::JenaTDBReader.new(tdb)
65
+ end
66
+ end
67
+ end.parse!
12
68
 
13
69
  def syntax_results(results)
14
70
  results.select do |res|
@@ -22,22 +78,22 @@ def semantics_results(results)
22
78
  end
23
79
  end
24
80
 
25
- require 'bel_parser'
26
81
  require 'bel_parser/expression/validator'
27
82
  require 'bel_parser/resource/resource_url_reader'
28
83
 
29
- resource_reader = BELParser::Resource::ResourceURLReader.new(true)
30
- namespaces =
84
+ uri_reader, url_reader = options.values_at(:uri_reader, :url_reader)
85
+
86
+ namespaces =
31
87
  Hash[
32
88
  ARGV.map do |ns|
33
89
  prefix, identifier = ns.split('=')
34
- dataset = resource_reader.retrieve_resource(identifier)
35
- dataset ? [prefix, dataset] : nil
90
+ dataset = uri_reader.retrieve_resource(identifier)
91
+ dataset ? [prefix, [:uri, dataset]] : nil
36
92
  end.compact
37
93
  ]
38
94
 
39
95
  BELParser::Expression::Validator
40
- .new(ARGV.first, namespaces, resource_reader)
96
+ .new(options[:spec], namespaces, uri_reader, url_reader)
41
97
  .each($stdin) do |(line_number, line, ast, results)|
42
98
  puts "#{line_number}: #{line}"
43
99
  puts " AST Type: #{ast.type}"
@@ -4,9 +4,10 @@ $LOAD_PATH.unshift(
4
4
 
5
5
  require 'optparse'
6
6
  require 'bel_parser'
7
- require 'bel_parser/resource/resource_url_reader'
8
7
 
9
8
  options = {
9
+ uri_reader: BELParser::Resource.default_uri_reader,
10
+ url_reader: BELParser::Resource.default_url_reader,
10
11
  spec: BELParser::Language.specification(
11
12
  BELParser::Language.latest_supported_version)
12
13
  }
@@ -25,25 +26,43 @@ OptionParser.new do |opts|
25
26
  options[:file] = bel
26
27
  end
27
28
 
29
+ opts.on(
30
+ '-v',
31
+ '--[no-]verbose',
32
+ 'Enable verbose logging.') do |verbose|
33
+ options[:verbose] = verbose
34
+ end
35
+
28
36
  opts.on(
29
37
  '-s',
30
38
  '--specification VERSION',
31
- 'BEL specification version (e.g. 1.0, 2.0)') do |spec|
39
+ 'BEL specification version (e.g. 1.0, 2.0).') do |spec|
40
+ unless BELParser::Language.defines_version?(spec)
41
+ $stderr.puts %(Invalid BEL specification "#{spec}")
42
+ exit 1
43
+ end
32
44
 
33
- unless BELParser::Language.defines_version?(spec)
34
- $stderr.puts %(Invalid BEL specification "#{spec}")
35
- exit 1
45
+ options[:spec] = BELParser::Language.specification(spec)
36
46
  end
37
47
 
38
- options[:spec] = BELParser::Language.specification(spec)
39
- end
40
-
41
48
  opts.on(
42
- '-r',
43
- '--[no-]reuse-resource-databases',
44
- 'Enable to reuse resource databases if they exist.') do |reuse|
49
+ '-q',
50
+ '--sparql-endpoint URL',
51
+ 'SPARQL endpoint URL containing BEL RDF resources.') do |sparql|
45
52
 
46
- options[:reuse] = reuse
53
+ require 'bel_parser/resource/sparql_reader'
54
+ options[:uri_reader] = BELParser::Resource::SPARQLReader.new(sparql)
55
+ end
56
+
57
+ if RUBY_ENGINE =~ /^jruby/i
58
+ opts.on(
59
+ '-t',
60
+ '--read-jena-tdb DIRECTORY',
61
+ 'Jena TDB directory containing BEL RDF resources.') do |tdb|
62
+
63
+ require 'bel_parser/resource/jena_tdb_reader'
64
+ options[:uri_reader] = BELParser::Resource::JenaTDBReader.new(tdb)
65
+ end
47
66
  end
48
67
  end.parse!
49
68
 
@@ -55,17 +74,20 @@ io =
55
74
  $stdin
56
75
  end
57
76
 
58
- rr = BELParser::Resource::ResourceURLReader.new(options[:reuse])
77
+ uri_reader = options[:uri_reader]
78
+ url_reader = options[:url_reader]
79
+
59
80
  namespaces = Hash[
60
81
  ARGV.map do |ns|
61
82
  prefix, identifier = ns.split('=')
62
- dataset = rr.retrieve_resource(identifier)
83
+ dataset = uri_reader.retrieve_resource(identifier)
63
84
  dataset ? [prefix, dataset] : nil
64
85
  end.compact
65
86
  ]
66
87
 
67
88
  initial_state = {
68
- resource_reader: rr,
89
+ uri_reader: uri_reader,
90
+ url_reader: url_reader,
69
91
  specification: spec,
70
92
  namespace_definitions: namespaces
71
93
  }
@@ -80,7 +102,8 @@ module BELParser
80
102
  SYN_WARN = BELParser::Language::Syntax::SyntaxWarning
81
103
  SEM_WARN = BELParser::Language::Semantics::SemanticsWarning
82
104
 
83
- def initialize(initial_state, io)
105
+ def initialize(initial_state, io, verbose)
106
+ @verbose = verbose
84
107
  @validator =
85
108
  Validator.new(
86
109
  StateAggregator.new(
@@ -96,7 +119,7 @@ module BELParser
96
119
  select(syntax_errors, SYN_WARN) +
97
120
  select(syntax_errors, SEM_WARN)
98
121
 
99
- report(line_number, ast_node, errors, warnings)
122
+ report(line_number, line, ast_node, errors, warnings)
100
123
 
101
124
  unless errors.empty?
102
125
  puts "Exiting due to error on line #{line_number}: #{line}"
@@ -111,22 +134,30 @@ module BELParser
111
134
  list.select { |item| item.is_a?(cls) }
112
135
  end
113
136
 
114
- def report(line_number, ast, errors, warnings)
137
+ def report(line_number, line, ast, errors, warnings)
115
138
  log = !errors.empty? || !warnings.empty?
116
139
  if log
117
140
  puts "On line #{line_number} for #{ast.type}"
118
- puts " Errors:"
119
- errors.each do |err|
120
- puts " #{err}"
141
+ puts ' Line:'
142
+ puts " #{line}"
143
+ unless errors.empty?
144
+ puts ' Errors:'
145
+ errors.each do |err|
146
+ puts " #{err}"
147
+ end
121
148
  end
122
- puts " Warnings:"
123
- warnings.each do |warn|
124
- puts " #{warn}"
149
+ unless warnings.empty?
150
+ puts ' Warnings:'
151
+ warnings.each do |warn|
152
+ puts " #{warn}"
153
+ end
125
154
  end
155
+ else
156
+ puts "Line OK: #{line}" if @verbose
126
157
  end
127
158
  end
128
159
  end
129
160
  end
130
161
  end
131
162
 
132
- BELParser::Script::Reader.new(initial_state, io).read
163
+ BELParser::Script::Reader.new(initial_state, io, options[:verbose]).read
@@ -12,7 +12,8 @@ module BEL::Translator::Plugins
12
12
  def initialize(io, options = {})
13
13
  @io = io
14
14
  @state = {
15
- resource_reader: BELParser::Resource::ResourceURLReader.new(true),
15
+ uri_reader: BELParser::Resource.default_uri_reader,
16
+ url_reader: BELParser::Resource.default_url_reader,
16
17
  specification: BELParser::Language.specification('2.0'),
17
18
  namespace_definitions: {}
18
19
  }
@@ -19,10 +19,10 @@ module BELParser
19
19
  :nested_statement
20
20
  ]
21
21
 
22
- def initialize(specification_version, namespaces, resource_reader)
23
- @spec = BELParser::Language.specification(specification_version)
22
+ def initialize(specification, namespaces, uri_reader, url_reader)
23
+ @spec = specification
24
24
  @validator = BELParser::Language::ExpressionValidator.new(
25
- @spec, namespaces, resource_reader)
25
+ @spec, namespaces, uri_reader, url_reader)
26
26
  end
27
27
 
28
28
  def each(io)
@@ -52,19 +52,18 @@ if __FILE__ == $PROGRAM_NAME
52
52
  exit 1
53
53
  end
54
54
 
55
- require 'bel_parser/resource/resource_url_reader'
56
-
57
- resource_reader = BELParser::Resource::ResourceURLReader.new
55
+ uri_reader = BELParser::Resource.default_uri_reader
56
+ url_reader = BELParser::Resource.default_url_reader
58
57
  namespaces =
59
58
  Hash[
60
59
  ARGV[1..-1]
61
60
  .map do |ns|
62
61
  prefix, identifier = ns.split('=')
63
- [prefix, resource_reader.retrieve_resource(identifier)]
62
+ [prefix, uri_reader.retrieve_resource(identifier)]
64
63
  end
65
64
  ]
66
65
  BELParser::Expression::Validator
67
- .new(ARGV.first, namespaces, resource_reader)
66
+ .new(ARGV.first, namespaces, uri_reader, url_reader)
68
67
  .each($stdin) do |(line_number, line, ast, results)|
69
68
  results.select { |res| res.is_a? BELParser::Language::Syntax::SyntaxError }
70
69
  puts "#{line_number}: #{line}"
@@ -1,17 +1,15 @@
1
- require 'bel_parser/quoting'
2
-
3
1
  module BELParser
4
2
  module Language
5
3
  # ApplyNamespaceEncoding applies _namespace_ and _encoding_ properties to
6
4
  # {BELParser::Parsers::AST::Parameter} child nodes.
7
5
  class ApplyNamespaceEncoding
8
6
  include AST::Processor::Mixin
9
- include BELParser::Quoting
10
7
 
11
- def initialize(language_spec, namespace_hash, resource_reader)
12
- @language_spec = language_spec
13
- @namespace_hash = namespace_hash
14
- @resource_reader = resource_reader
8
+ def initialize(language_spec, namespace_hash, uri_reader, url_reader)
9
+ @language_spec = language_spec
10
+ @namespace_hash = namespace_hash
11
+ @uri_reader = uri_reader
12
+ @url_reader = url_reader
15
13
  end
16
14
 
17
15
  def on_observed_term(observed_term_node)
@@ -51,8 +49,9 @@ module BELParser
51
49
  end
52
50
 
53
51
  def on_parameter(parameter_node)
54
- @resolved_dataset = nil
55
- @prefix = nil
52
+ @type = nil
53
+ @dataset = nil
54
+ @prefix = nil
56
55
  process(parameter_node.prefix)
57
56
  process(parameter_node.value)
58
57
  end
@@ -60,28 +59,26 @@ module BELParser
60
59
  def on_prefix(prefix_node)
61
60
  return prefix_node unless prefix_node.identifier
62
61
 
63
- @prefix = prefix_node.identifier.string_literal
64
- dataset = @namespace_hash[@prefix]
65
- return prefix_node unless dataset
66
-
67
- @resolved_dataset = dataset
68
- prefix_node.namespace = dataset
62
+ @prefix = prefix_node.identifier.string_literal
63
+ @type, @dataset = @namespace_hash[@prefix]
64
+ return prefix_node unless @dataset
69
65
 
66
+ prefix_node.namespace = @dataset
70
67
  prefix_node
71
68
  end
72
69
 
73
70
  def on_value(value_node)
74
- return value_node unless @resolved_dataset
75
- value_node.namespace = @resolved_dataset
76
- identifier = @resolved_dataset.identifier
77
- value_literal = unquote(value_node.children[0].string_literal)
71
+ return value_node unless @dataset
72
+ value_node.namespace = @dataset
73
+ identifier = @dataset.identifier
74
+ value_literal = value_node.children[0].string_literal
78
75
 
79
- value =
80
- @resource_reader
81
- .retrieve_value_from_resource(identifier, value_literal)
76
+ reader = @type == :uri ? @uri_reader : @url_reader
77
+ values = reader.retrieve_value_from_resource(identifier, value_literal)
82
78
 
83
79
  value_node.prefix = @prefix
84
- if value
80
+ if values
81
+ value = values.first
85
82
  value_node.encoding =
86
83
  value
87
84
  .encodings
@@ -8,13 +8,13 @@ module BELParser
8
8
  # when supplied a {BELParser::Language::Specification} and Hash of
9
9
  # namespaces.
10
10
  class ExpressionValidator
11
- def initialize(spec, namespaces, resource_reader)
11
+ def initialize(spec, namespaces, uri_reader, url_reader)
12
12
  @spec = spec
13
13
  @namespaces = namespaces || {}
14
14
  @syntax_functions = Syntax.syntax_functions
15
15
  @semantics_functions = Semantics.semantics_functions
16
16
  @transform =
17
- ApplyNamespaceEncoding.new(@spec, @namespaces, resource_reader)
17
+ ApplyNamespaceEncoding.new(@spec, @namespaces, uri_reader, url_reader)
18
18
  end
19
19
 
20
20
  # Validate the syntax and semantics of
@@ -266,6 +266,9 @@ module BELParser
266
266
  def domain
267
267
  children[1]
268
268
  end
269
+
270
+ # Get/Set the uri property.
271
+ attr_accessor :uri
269
272
  end
270
273
 
271
274
  # AST node representing the function of a BEL term.
@@ -317,6 +320,9 @@ module BELParser
317
320
  def domain
318
321
  children[1]
319
322
  end
323
+
324
+ # Get/Set the uri property.
325
+ attr_accessor :uri
320
326
  end
321
327
 
322
328
  # AST node representing a keyword (e.g. definitions).
@@ -363,6 +369,11 @@ module BELParser
363
369
  children[0] && children[0].is_a?(Pattern)
364
370
  end
365
371
 
372
+ # Determine if this is a URI domain.
373
+ def uri?
374
+ children[0] && children[0].is_a?(Uri)
375
+ end
376
+
366
377
  # Determine if this is a URL domain.
367
378
  def url?
368
379
  children[0] && children[0].is_a?(Url)
@@ -394,6 +405,26 @@ module BELParser
394
405
  end
395
406
  end
396
407
 
408
+ # AST node representing a URI.
409
+ class Uri < Node
410
+ # AST node type
411
+ @ast_type = :uri
412
+ # Urls have semantic meaning
413
+ @has_semantics = true
414
+
415
+ # New Uri AST node.
416
+ #
417
+ # @see Node#initialize Node class for basic properties
418
+ def initialize(children = [], properties = {})
419
+ super(Uri.ast_type, children, properties)
420
+ end
421
+
422
+ # Get the uri's string.
423
+ def string
424
+ children[0]
425
+ end
426
+ end
427
+
397
428
  # AST node representing a Pattern.
398
429
  class Pattern < Node
399
430
  # AST node type
@@ -1029,6 +1060,10 @@ module BELParser
1029
1060
  Url.new(children, props)
1030
1061
  end
1031
1062
 
1063
+ def uri(*children, **props)
1064
+ Uri.new(children, props)
1065
+ end
1066
+
1032
1067
  def pattern(*children, **props)
1033
1068
  Pattern.new(children, props)
1034
1069
  end