-
Notifications
You must be signed in to change notification settings - Fork 9
/
parse
executable file
·175 lines (163 loc) · 6.19 KB
/
parse
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env ruby
require 'rubygems'
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", 'lib')))
require "bundler/setup"
require 'logger'
require 'rdf/n3'
require 'rdf/ntriples'
require 'rdf/trig'
require 'getoptlong'
require 'open-uri'
def run(input, **options)
require 'profiler' if options[:profile]
reader_class = RDF::Reader.for(options[:input_format].to_sym)
raise "Reader not found for #{options[:input_format]}" unless reader_class
start = Time.new
num = 0
Profiler__::start_profile if options[:profile]
if options[:think]
# Parse into a new reasoner and evaluate
reader_class.new(input, options[:parser_options].merge(logger: nil)) do |reader|
reasoner = RDF::N3::Reasoner.new(reader, options[:parser_options])
reasoner.reason!(options)
repo = RDF::Repository.new
if options[:conclusions]
repo << reasoner.conclusions
elsif options[:data]
repo << reasoner.data
else
repo << reasoner
end
num = repo.count
options[:output].puts repo.dump(options[:output_format], prefixes: reader.prefixes, standard_prefixes: true, logger: options[:logger])
end
elsif options[:output_format] == :ntriples || options[:quiet]
reader_class.new(input, options[:parser_options]).each do |statement|
num += 1
if options[:errors] && statement.invalid?
$stderr.puts "Invalid statement at #{r.lineno}: #{statement.inspect}"
elsif options[:quiet]
print "." if options[:quiet] == 1
else
options[:output].puts statement.to_ntriples
end
end
elsif options[:output_format] == :sxp
reader_class.new(input, options[:parser_options]) do |reader|
reasoner = RDF::N3::Reasoner.new(reader)
SXP::Generator.print(reasoner.to_sxp_bin)
end
elsif options[:output_format] == :inspect
reader_class.new(input, options[:parser_options]).each do |statement|
num += 1
options[:output].puts statement.inspect
end
else
reader = reader_class.new(input, options[:parser_options])
repo = RDF::Repository.new << reader
num = repo.count
options[:output].puts repo.dump(options[:output_format], prefixes: reader.prefixes, standard_prefixes: true, logger: options[:logger])
end
if options[:profile]
Profiler__::stop_profile
Profiler__::print_profile($stderr)
end
puts
secs = Time.new - start
puts "Parsed #{num} statements in #{secs} seconds @ #{num/secs} statements/second."
rescue Exception => e
fname = input.respond_to?(:path) ? input.path : "-stdin-"
STDERR.puts("Error in #{fname}: #{e.message}")
STDERR.puts "Backtrace: " + e.backtrace.join("\n ")
raise e
end
logger = Logger.new(STDERR)
logger.level = Logger::WARN
logger.formatter = lambda {|severity, datetime, progname, msg| "#{severity}: #{msg}\n"}
parser_options = {
base_uri: "http://example.com",
logger: logger,
validate: false,
}
options = {
parser_options: parser_options,
logger: logger,
output: STDOUT,
output_format: :n3,
input_format: :n3,
}
input = nil
OPT_ARGS = [
["--apply", GetoptLong::REQUIRED_ARGUMENT, "Apply rules from specified file"],
["--conclusions", GetoptLong::NO_ARGUMENT, "Remove all except conclusions"],
["--canonicalize", GetoptLong::NO_ARGUMENT, "Canonize all terms"],
["--data", GetoptLong::NO_ARGUMENT, "Remove all except plain RDF triples (formulae, forAll, etc)v"],
["--debug", GetoptLong::NO_ARGUMENT, "Debugging output"],
["--errors", GetoptLong::NO_ARGUMENT, "Display invalid statements"],
["--execute", "-e", GetoptLong::REQUIRED_ARGUMENT, "Run against source in argument"],
["--format", GetoptLong::REQUIRED_ARGUMENT, "Output format, any RDF format symbol, sxp, or inspect"],
["--help", "-?", GetoptLong::NO_ARGUMENT, "print this message"],
["--input-format", GetoptLong::REQUIRED_ARGUMENT, "Format of the input file, defaults to n3"],
["--info", GetoptLong::NO_ARGUMENT, "Show progress on execution"],
["--output", "-o", GetoptLong::REQUIRED_ARGUMENT, "Save output to file"],
["--profile", GetoptLong::NO_ARGUMENT, "Show an execution profile"],
["--quiet", GetoptLong::NO_ARGUMENT, "Do not show parser output"],
["--rules", GetoptLong::NO_ARGUMENT, "Run rules adding to the store"],
["--think", GetoptLong::NO_ARGUMENT, "Run rules until until no more triples generated"],
["--uri", GetoptLong::REQUIRED_ARGUMENT, "Default base URI"],
["--validate", GetoptLong::NO_ARGUMENT, "Run parser in strict validation mode"],
#["--verbose", GetoptLong::NO_ARGUMENT, "Verbose output"],
]
def usage
STDERR.puts %{
n3 version #{RDF::N3::VERSION}
Exersize N3 parser/reasoner
Usage: #{$0} [options] file ...
}.gsub(/^ /, '')
width = OPT_ARGS.map do |o|
l = o.first.length
l += o[1].length + 2 if o[1].is_a?(String)
l
end.max
OPT_ARGS.each do |o|
s = " %-*s " % [width, (o[1].is_a?(String) ? "#{o[0,2].join(', ')}" : o[0])]
s += o.last
STDERR.puts s
end
exit(1)
end
opts = GetoptLong.new(*OPT_ARGS.map {|o| o[0..-2]})
opts.each do |opt, arg|
case opt
when '--apply' then # Read rules
when '--conclusions' then options[:conclusions] = true
when '--canonicalize' then parser_options[:canonicalize] = true
when "--data" then options[:data] = true
when '--debug' then logger.level = Logger::DEBUG
when '--errors' then options[:errors] = true
when '--execute' then input = arg
when '--format' then options[:output_format] = arg.to_sym
when "--help" then usage()
when '--info' then logger.level = Logger::INFO
when '--input-format' then options[:input_format] = arg.to_sym
when '--output' then options[:output] = File.open(arg, "w")
when '--profile' then options[:profile] = true
when '--quiet'
options[:quiet] = true
logger.level = Logger::FATAL
when '--rules' then options[:rules] = true
when '--think' then options[:think] = true
when '--uri' then parser_options[:base_uri] = arg
when '--validate' then parser_options[:debug] ||= 1
when '--verbose' then $verbose = true
end
end
if ARGV.empty?
s = input ? input : $stdin.read
run(StringIO.new(s), **options)
else
ARGV.each do |test_file|
run(Kernel.open(test_file), **options)
end
end
puts