KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > query > PatternStageCompiler


1 /*
2   (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: PatternStageCompiler.java,v 1.7 2005/02/21 11:52:24 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.graph.query;
8
9 import com.hp.hpl.jena.graph.*;
10
11 /**
12     PatternStageCompiler serves two purposes: it contains the standard algorithm
13     for compiling patterns-as-triples to patterns-as-Pattern(s), and it has the
14     standard implementation of PatternCompiler in terms of ordinary Elements.
15     
16     @author kers
17 */

18 public final class PatternStageCompiler implements PatternCompiler
19     {
20     /** no state, so the constructor is boring.
21     */

22     public PatternStageCompiler()
23         {}
24       
25     /**
26         to compile an array of triples, compile each triple and form the corresponding
27         array of Patterns. *preserve the order*.
28     */

29     public static Pattern [] compile( PatternCompiler compiler, Mapping map, Triple [] source )
30         {
31         Pattern [] compiled = new Pattern[source.length];
32         for (int i = 0; i < source.length; i += 1) compiled[i] = compile( compiler, source[i], map );
33         return compiled;
34         }
35        
36     /**
37         to compile a triple, compile each node and form a Pattern from the resulting Elements.
38     */

39     private static Pattern compile( PatternCompiler compiler, Triple t, Mapping map )
40         {
41         Node S = t.getSubject(), P = t.getPredicate(), O = t.getObject();
42         return new Pattern( compile( compiler, S, map ), compile( compiler, P, map ), compile( compiler, O, map ) );
43         }
44         
45     /**
46         to compile a Node, special-case variables and ANYs. Surely this code is
47         better in Node, but I really don't like exporting so much query information
48         into the Node data-type. Hmm. Perhaps some node types are better off in
49         the .query package.
50     */

51     private static Element compile( PatternCompiler compiler, Node X, Mapping map )
52         {
53         if (X.equals( Query.ANY )) return compiler.any();
54         if (X.isVariable())
55             {
56             if (map.hasBound( X ))
57                 return compiler.bound( X, map.indexOf( X ) );
58             else
59                 return compiler.bind( X, map.newIndex( X ) );
60             }
61         return compiler.fixed( X );
62         }
63
64     /*
65         satisfy the interface
66     */

67     public Element fixed( Node value )
68         { return new Fixed( value ); }
69         
70     public Element bound( Node n, int index )
71         { return new Bound( index ); }
72         
73     public Element bind( Node n, int index )
74         { return new Bind( index ); }
75         
76     public Element any()
77         { return Element.ANY; }
78     }
79 /*
80     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
81     All rights reserved.
82
83     Redistribution and use in source and binary forms, with or without
84     modification, are permitted provided that the following conditions
85     are met:
86
87     1. Redistributions of source code must retain the above copyright
88        notice, this list of conditions and the following disclaimer.
89
90     2. Redistributions in binary form must reproduce the above copyright
91        notice, this list of conditions and the following disclaimer in the
92        documentation and/or other materials provided with the distribution.
93
94     3. The name of the author may not be used to endorse or promote products
95        derived from this software without specific prior written permission.
96
97     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
98     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
99     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
100     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
101     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
102     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
103     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
104     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
105     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
106     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
107 */
Popular Tags