001: /*
002: * (c) Copyright 2007, 2008 Hewlett-Packard Development Company, LP
003: * All rights reserved.
004: * [See end of file]
005: */
006:
007: package arq.examples.engine;
008:
009: import com.hp.hpl.jena.sparql.ARQInternalErrorException;
010: import com.hp.hpl.jena.sparql.algebra.Op;
011: import com.hp.hpl.jena.sparql.algebra.Transform;
012: import com.hp.hpl.jena.sparql.algebra.TransformBase;
013: import com.hp.hpl.jena.sparql.algebra.Transformer;
014: import com.hp.hpl.jena.sparql.algebra.op.OpBGP;
015: import com.hp.hpl.jena.sparql.core.DatasetGraph;
016: import com.hp.hpl.jena.sparql.engine.Plan;
017: import com.hp.hpl.jena.sparql.engine.QueryEngineFactory;
018: import com.hp.hpl.jena.sparql.engine.QueryEngineRegistry;
019: import com.hp.hpl.jena.sparql.engine.QueryIterator;
020: import com.hp.hpl.jena.sparql.engine.binding.Binding;
021: import com.hp.hpl.jena.sparql.engine.main.QueryEngineMain;
022: import com.hp.hpl.jena.sparql.util.Context;
023:
024: import com.hp.hpl.jena.query.Query;
025:
026: /** Example skeleton for a query engine.
027: * To just enxtend ARQ by custom basic graph pattern matching (a very common case)
028: * see the arq.examples.extend.bgp package
029: *
030: * @author Andy Seaborne
031: */
032:
033: public class MyQueryEngine extends QueryEngineMain {
034: // Do nothing template for a query engine.
035:
036: public MyQueryEngine(Query query, DatasetGraph dataset,
037: Binding initial, Context context) {
038: super (query, dataset, initial, context);
039: }
040:
041: public MyQueryEngine(Query query, DatasetGraph dataset) {
042: // This will default to the global context with no initial settings
043: this (query, dataset, null, null);
044: }
045:
046: public QueryIterator eval(Op op, DatasetGraph dsg, Binding binding,
047: Context context) {
048: // To extend: rewrite op with a Transform
049:
050: Transform transform = new MyTransform();
051: op = Transformer.transform(transform, op);
052:
053: return super .eval(op, dsg, binding, context);
054: }
055:
056: // ---- Registration of the factory for this query engine class.
057:
058: // Query engine factory.
059: // Call MyQueryEngine.register() to add to the global query engine registry.
060:
061: static QueryEngineFactory factory = new MyQueryEngineFactory();
062:
063: static public QueryEngineFactory getFactory() {
064: return factory;
065: }
066:
067: static public void register() {
068: QueryEngineRegistry.addFactory(factory);
069: }
070:
071: static public void unregister() {
072: QueryEngineRegistry.removeFactory(factory);
073: }
074:
075: }
076:
077: class MyTransform extends TransformBase {
078: // Example, do nothing tranform.
079: public Op transform(OpBGP opBGP) {
080: return opBGP;
081: }
082: }
083:
084: class MyQueryEngineFactory implements QueryEngineFactory {
085: // Accept any dataset for query execution
086: public boolean accept(Query query, DatasetGraph dataset,
087: Context context) {
088: return true;
089: }
090:
091: public Plan create(Query query, DatasetGraph dataset,
092: Binding initial, Context context) {
093: // Create a query engine instance.
094: MyQueryEngine engine = new MyQueryEngine(query, dataset,
095: initial, context);
096: return engine.getPlan();
097: }
098:
099: public boolean accept(Op op, DatasetGraph dataset, Context context) { // Refuse to accept algebra expressions directly.
100: return false;
101: }
102:
103: public Plan create(Op op, DatasetGraph dataset,
104: Binding inputBinding, Context context) { // Shodul notbe called because acceept/Op is false
105: throw new ARQInternalErrorException(
106: "MyQueryEngine: factory calleddirectly with an algebra expression");
107: }
108: }
109:
110: /*
111: * (c) Copyright 2007, 2008 Hewlett-Packard Development Company, LP
112: * All rights reserved.
113: *
114: * Redistribution and use in source and binary forms, with or without
115: * modification, are permitted provided that the following conditions
116: * are met:
117: * 1. Redistributions of source code must retain the above copyright
118: * notice, this list of conditions and the following disclaimer.
119: * 2. Redistributions in binary form must reproduce the above copyright
120: * notice, this list of conditions and the following disclaimer in the
121: * documentation and/or other materials provided with the distribution.
122: * 3. The name of the author may not be used to endorse or promote products
123: * derived from this software without specific prior written permission.
124: *
125: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
126: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
127: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
128: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
129: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
130: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
131: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
132: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
133: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
134: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
135: */
|