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;
008:
009: import java.util.ArrayList;
010: import java.util.List;
011:
012: import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
013: import com.hp.hpl.jena.graph.Node;
014: import com.hp.hpl.jena.graph.Triple;
015: import com.hp.hpl.jena.rdf.model.*;
016: import com.hp.hpl.jena.sparql.algebra.Algebra;
017: import com.hp.hpl.jena.sparql.algebra.Op;
018: import com.hp.hpl.jena.sparql.algebra.op.OpBGP;
019: import com.hp.hpl.jena.sparql.algebra.op.OpFilter;
020: import com.hp.hpl.jena.sparql.core.BasicPattern;
021: import com.hp.hpl.jena.sparql.core.Var;
022: import com.hp.hpl.jena.sparql.engine.QueryIterator;
023: import com.hp.hpl.jena.sparql.engine.ResultSetStream;
024: import com.hp.hpl.jena.sparql.engine.binding.Binding;
025: import com.hp.hpl.jena.sparql.expr.E_LessThan;
026: import com.hp.hpl.jena.sparql.expr.Expr;
027: import com.hp.hpl.jena.sparql.expr.NodeValue;
028: import com.hp.hpl.jena.sparql.expr.ExprVar;
029: import com.hp.hpl.jena.sparql.util.FmtUtils;
030:
031: import com.hp.hpl.jena.query.ResultSet;
032: import com.hp.hpl.jena.query.ResultSetFormatter;
033:
034: /** Build an algebra expression and execute it */
035:
036: public class AlgebraExec {
037: public static void main(String[] argv) {
038: String BASE = "http://example/";
039: BasicPattern bp = new BasicPattern();
040: Var var_x = Var.alloc("x");
041: Var var_z = Var.alloc("z");
042:
043: // ---- Build expression
044: bp.add(new Triple(var_x, Node.createURI(BASE + "p"), var_z));
045: Op op = new OpBGP(bp);
046: //Expr expr = ExprUtils.parse("?z < 2 ") ;
047: Expr expr = new E_LessThan(new ExprVar(var_z), NodeValue
048: .makeNodeInteger(2));
049: op = OpFilter.filter(expr, op);
050:
051: // ---- Example setup
052: Model m = makeModel();
053: m.write(System.out, "TTL");
054: System.out.println("--------------");
055: System.out.print(op);
056: System.out.println("--------------");
057:
058: // ---- Execute expression
059: QueryIterator qIter = Algebra.exec(op, m.getGraph());
060:
061: // -------- Either read the query iterator directly ...
062: if (false) {
063: for (; qIter.hasNext();) {
064: Binding b = qIter.nextBinding();
065: Node n = b.get(var_x);
066: System.out.println(FmtUtils.stringForNode(n));
067: System.out.println(b);
068: }
069: qIter.close();
070: } else {
071: // -------- Or make ResultSet from it (but not both - reading an
072: // iterator consumes the current solution)
073: List varNames = new ArrayList();
074: varNames.add("x");
075: varNames.add("z");
076: ResultSet rs = new ResultSetStream(varNames, m, qIter);
077: ResultSetFormatter.out(rs);
078: qIter.close();
079: }
080: System.exit(0);
081: }
082:
083: private static Model makeModel() {
084: String BASE = "http://example/";
085: Model model = ModelFactory.createDefaultModel();
086: model.setNsPrefix("", BASE);
087: Resource r1 = model.createResource(BASE + "r1");
088: Resource r2 = model.createResource(BASE + "r2");
089: Property p1 = model.createProperty(BASE + "p");
090: Property p2 = model.createProperty(BASE + "p2");
091: RDFNode v1 = model.createTypedLiteral("1",
092: XSDDatatype.XSDinteger);
093: RDFNode v2 = model.createTypedLiteral("2",
094: XSDDatatype.XSDinteger);
095:
096: r1.addProperty(p1, v1).addProperty(p1, v2);
097: r1.addProperty(p2, v1).addProperty(p2, v2);
098: r2.addProperty(p1, v1).addProperty(p1, v2);
099:
100: return model;
101: }
102:
103: }
104:
105: /*
106: * (c) Copyright 2007, 2008 Hewlett-Packard Development Company, LP
107: * All rights reserved.
108: *
109: * Redistribution and use in source and binary forms, with or without
110: * modification, are permitted provided that the following conditions
111: * are met:
112: * 1. Redistributions of source code must retain the above copyright
113: * notice, this list of conditions and the following disclaimer.
114: * 2. Redistributions in binary form must reproduce the above copyright
115: * notice, this list of conditions and the following disclaimer in the
116: * documentation and/or other materials provided with the distribution.
117: * 3. The name of the author may not be used to endorse or promote products
118: * derived from this software without specific prior written permission.
119: *
120: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
121: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
122: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
123: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
124: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
125: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
126: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
127: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
128: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
129: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
130: */
|