01: /*
02: * (c) Copyright 2007, 2008 Hewlett-Packard Development Company, LP
03: * All rights reserved.
04: * [See end of file]
05: */
06:
07: package arq.examples.bgpmatching;
08:
09: import com.hp.hpl.jena.rdf.model.Model;
10: import com.hp.hpl.jena.rdf.model.ModelFactory;
11: import com.hp.hpl.jena.rdf.model.Property;
12: import com.hp.hpl.jena.rdf.model.Resource;
13:
14: import com.hp.hpl.jena.sparql.engine.main.StageGenerator;
15: import com.hp.hpl.jena.sparql.util.QueryExecUtils;
16: import com.hp.hpl.jena.sparql.util.StringUtils;
17:
18: import com.hp.hpl.jena.query.*;
19:
20: /** Example to execute a query but handle the
21: * basic graph patterns in the query in some special way.
22: * Stages are one step in executing a basic graph pattern (BGP).
23: * A StageGenerator builds a StageList and the stage list
24: * is executes with the output (a QueryIterator) of the previous
25: * stage fed int the current stage.
26: */
27:
28: public class StageAltMain {
29: static String NS = "http://example/";
30:
31: public static void main(String[] argv) {
32: String[] queryString = { "PREFIX ns: <" + NS + ">",
33: "SELECT ?v ", "{ ?s ns:p1 'xyz' ;", " ns:p2 ?v }" };
34:
35: // The stage generator to be used for a query execution
36: // is read from the context. There is a global context, which
37: // is cloned when a query execution object (query engine) is
38: // created.
39:
40: StageGenerator stageGenAlt = new StageGeneratorAlt();
41:
42: // The normal stage generator is registerd in the global context.
43: // This can be replaced, so that every query execution uses the
44: // alternative stage generator, or the cloned context can be
45: // alter so that just one query execution is affected.
46:
47: // Change the stage generator for all queries ...
48: if (false)
49: ARQ.getContext().set(ARQ.stageGenerator, stageGenAlt);
50:
51: Query query = QueryFactory.create(StringUtils.join("\n",
52: queryString));
53: QueryExecution engine = QueryExecutionFactory.create(query,
54: makeData());
55:
56: // ... or set on a per-execution basis.
57: if (true)
58: engine.getContext().set(ARQ.stageGenerator, stageGenAlt);
59:
60: QueryExecUtils.executeQuery(query, engine);
61: }
62:
63: private static Model makeData() {
64: Model model = ModelFactory.createDefaultModel();
65: Resource r = model.createResource(NS + "r");
66: Property p1 = model.createProperty(NS + "p1");
67: Property p2 = model.createProperty(NS + "p2");
68: model.add(r, p1, "xyz");
69: model.add(r, p2, "abc");
70: return model;
71: }
72: }
73:
74: /*
75: * (c) Copyright 2007, 2008 Hewlett-Packard Development Company, LP
76: * All rights reserved.
77: *
78: * Redistribution and use in source and binary forms, with or without
79: * modification, are permitted provided that the following conditions
80: * are met:
81: * 1. Redistributions of source code must retain the above copyright
82: * notice, this list of conditions and the following disclaimer.
83: * 2. Redistributions in binary form must reproduce the above copyright
84: * notice, this list of conditions and the following disclaimer in the
85: * documentation and/or other materials provided with the distribution.
86: * 3. The name of the author may not be used to endorse or promote products
87: * derived from this software without specific prior written permission.
88: *
89: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
90: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
91: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
92: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
93: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
94: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
95: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
96: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
97: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
98: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
99: */
|