001: /*
002: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: PatternStageCompiler.java,v 1.10 2008/01/02 12:07:58 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.graph.query;
008:
009: import com.hp.hpl.jena.graph.*;
010:
011: /**
012: PatternStageCompiler serves two purposes: it contains the standard algorithm
013: for compiling patterns-as-triples to patterns-as-Pattern(s), and it has the
014: standard implementation of PatternCompiler in terms of ordinary Elements.
015:
016: @author kers
017: */
018: public final class PatternStageCompiler implements PatternCompiler {
019: /** no state, so the constructor is boring.
020: */
021: public PatternStageCompiler() {
022: }
023:
024: /**
025: to compile an array of triples, compile each triple and form the corresponding
026: array of Patterns. *preserve the order*.
027: */
028: public static Pattern[] compile(PatternCompiler compiler,
029: Mapping map, Triple[] source) {
030: Pattern[] compiled = new Pattern[source.length];
031: for (int i = 0; i < source.length; i += 1)
032: compiled[i] = compile(compiler, source[i], map);
033: return compiled;
034: }
035:
036: /**
037: to compile a triple, compile each node and form a Pattern from the resulting Elements.
038: */
039: private static Pattern compile(PatternCompiler compiler, Triple t,
040: Mapping map) {
041: Node S = t.getSubject(), P = t.getPredicate(), O = t
042: .getObject();
043: return new Pattern(compile(compiler, S, map), compile(compiler,
044: P, map), compile(compiler, O, map));
045: }
046:
047: /**
048: to compile a Node, special-case variables and ANYs. Surely this code is
049: better in Node, but I really don't like exporting so much query information
050: into the Node data-type. Hmm. Perhaps some node types are better off in
051: the .query package.
052: */
053: private static Element compile(PatternCompiler compiler, Node X,
054: Mapping map) {
055: if (X.equals(Query.ANY))
056: return compiler.any();
057: if (X.isVariable()) {
058: if (map.hasBound(X))
059: return compiler.bound(X, map.indexOf(X));
060: else
061: return compiler.bind(X, map.newIndex(X));
062: }
063: return compiler.fixed(X);
064: }
065:
066: /*
067: satisfy the interface
068: */
069: public Element fixed(Node value) {
070: return new Fixed(value);
071: }
072:
073: public Element bound(Node n, int index) {
074: return new Bound(index);
075: }
076:
077: public Element bind(Node n, int index) {
078: return new Bind(index);
079: }
080:
081: public Element any() {
082: return Element.ANY;
083: }
084: }
085: /*
086: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
087: All rights reserved.
088:
089: Redistribution and use in source and binary forms, with or without
090: modification, are permitted provided that the following conditions
091: are met:
092:
093: 1. Redistributions of source code must retain the above copyright
094: notice, this list of conditions and the following disclaimer.
095:
096: 2. Redistributions in binary form must reproduce the above copyright
097: notice, this list of conditions and the following disclaimer in the
098: documentation and/or other materials provided with the distribution.
099:
100: 3. The name of the author may not be used to endorse or promote products
101: derived from this software without specific prior written permission.
102:
103: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
104: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
105: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
106: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
107: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
108: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
109: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
110: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
111: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
112: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
113: */
|