/
Demo.java
170 lines (144 loc) · 5.14 KB
/
Demo.java
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
package net.fortytwo.ripple;
import gnu.getopt.Getopt;
import gnu.getopt.LongOpt;
import net.fortytwo.ripple.cli.RippleCommandLine;
import net.fortytwo.ripple.config.SailConfiguration;
import net.fortytwo.ripple.model.Model;
import net.fortytwo.ripple.model.impl.sesame.SesameModel;
import net.fortytwo.ripple.query.LazyStackEvaluator;
import net.fortytwo.ripple.query.QueryEngine;
import net.fortytwo.ripple.query.StackEvaluator;
import org.apache.log4j.BasicConfigurator;
import org.openrdf.sail.Sail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Properties;
/**
* Demo application.
*
* @author Joshua Shinavier (http://fortytwo.net)
*/
public final class Demo {
private static final Logger logger = LoggerFactory.getLogger(Demo.class);
private Demo() {
}
private static void demo(final InputStream in,
final PrintStream out,
final PrintStream err)
throws RippleException {
// Create a Sesame triple store.
URIMap uriMap = new URIMap();
SailConfiguration sailConfig = new SailConfiguration(uriMap);
Sail sail = sailConfig.getSail();
// Attach a Ripple model to the repository.
Model model = new SesameModel(sail);
// Attach a query engine to the model.
StackEvaluator evaluator = new LazyStackEvaluator();
QueryEngine qe
= new QueryEngine(model, evaluator, out, err);
// Attach an interpreter to the query engine and let it query from
// standard input.
RippleCommandLine r = new RippleCommandLine(qe, in);
r.run();
// Shut down.
model.shutDown();
}
private static void printUsage() {
System.out.println("Usage: ripple [options] [configuration file]");
System.out.println("Options:\n"
+ " -h Print this help and exit\n"
+ " -q Suppress normal output\n"
+ " -v Print version information and exit");
System.out.println("For more information, please see:\n"
+ " <URL:http://ripple.fortytwo.net/>.");
}
private static void printVersion() {
System.out.println(Ripple.getName() + " " + Ripple.getVersion());
// Would be nice: list of libraries
}
public static void main(final String[] args) {
BasicConfigurator.configure();
// Default values.
boolean showVersion = false, showHelp = false;
File configFile = null;
// Long options are available but are not advertised.
LongOpt[] longOptions = {
new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h'),
// TODO: unused
new LongOpt("quiet", LongOpt.NO_ARGUMENT, null, 'q'),
new LongOpt("version", LongOpt.NO_ARGUMENT, null, 'v')};
Getopt g = new Getopt(Ripple.getName(), args, "hqv", longOptions);
int c;
while ((c = g.getopt()) != -1) {
switch (c) {
case 'h':
case 0:
showHelp = true;
break;
case 'q':
case 1:
break;
case 'v':
case 2:
showVersion = true;
break;
case '?':
// Note: getopt() already printed an error
printUsage();
System.exit(1);
break;
default:
System.err.print("getopt() returned " + c + "\n");
}
}
int i = g.getOptind();
if (i < args.length) {
// Too many non-option arguments.
if (args.length - i > 1) {
printUsage();
System.exit(1);
}
configFile = new File(args[i]);
}
if (showHelp) {
printUsage();
System.exit(0);
}
if (showVersion) {
printVersion();
System.exit(0);
}
try {
// Load Ripple configuration.
if (null == configFile) {
Ripple.initialize();
} else {
Properties p = new Properties();
try {
p.load(new FileInputStream(configFile));
} catch (IOException e) {
throw new RippleException(e);
}
Ripple.initialize(p);
}
} catch (RippleException e) {
System.err.println("Initialization error: " + e);
logger.error("initialization error", e);
System.exit(1);
}
try {
demo(System.in, System.out, System.err);
} catch (RippleException e) {
System.out.println("Exiting with error: " + e);
logger.error("exiting with error", e);
System.exit(1);
}
// Exit despite any remaining active threads.
System.exit(0);
}
}