KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP, all rights reserved.
3   [See end of file]
4   $Id: Dyadic.java,v 1.3 2005/02/21 11:52:15 andy_seaborne Exp $
5 */

6 package com.hp.hpl.jena.graph.query;
7
8 import com.hp.hpl.jena.graph.query.Expression.Application;
9 import com.hp.hpl.jena.shared.JenaException;
10
11 /**
12     A base class for dyadic expressions with a built-in Valuator; subclasses must
13     define an evalObject or evalBool method which will be supplied with the
14     evaluated operands.
15     
16     @author kers
17 */

18 public abstract class Dyadic extends Application
19     {
20     protected Expression L;
21     protected Expression R;
22     protected String JavaDoc F;
23     
24     public Dyadic( Expression L, String JavaDoc F, Expression R )
25         {
26         this.L = L;
27         this.F = F;
28         this.R = R;
29         }
30     
31     public int argCount()
32         { return 2; }
33     
34     public Expression getArg( int i )
35         { return i == 0 ? L : R; }
36     
37     public String JavaDoc getFun()
38         { return F; }
39     
40     /**
41         Answer the Object result of evaluating this dyadic expression with
42         the given arguments <code>l</code> and <code>r</code>.
43         Either this method or <code>evalBool</code> <i>must</i> be
44         over-ridden in concrete sub-classes.
45     */

46     public Object JavaDoc evalObject( Object JavaDoc l, Object JavaDoc r )
47         { return evalBool( l, r ) ? Boolean.TRUE : Boolean.FALSE; }
48     
49     /**
50         Answer the boolean result of evaluating this dyadic expression with
51         the given arguments <code>l</code> and <code>r</code>.
52         Either this method or <code>evalObject</code> <i>must</i> be
53         over-ridden in concrete sub-classes.
54     */

55     public boolean evalBool( Object JavaDoc l, Object JavaDoc r )
56         { Object JavaDoc x = evalObject( l, r );
57         if (x instanceof Boolean JavaDoc) return ((Boolean JavaDoc) x).booleanValue();
58         throw new JenaException( "not Boolean: " + x );
59         }
60     
61     public Valuator prepare( VariableIndexes vi )
62         {
63         final Valuator l = L.prepare( vi ), r = R.prepare( vi );
64         return new Valuator()
65             {
66             public boolean evalBool( IndexValues iv)
67                 {
68                 return ((Boolean JavaDoc) evalObject( iv )).booleanValue();
69                 }
70     
71             public Object JavaDoc evalObject( IndexValues iv )
72                 {
73                 return Dyadic.this.evalObject( l.evalObject( iv ), r.evalObject( iv ) );
74                 }
75                 
76             };
77         }
78     
79     public String JavaDoc toString()
80         { return L.toString() + " " + F + " " + R.toString(); }
81
82     public static Expression and( Expression L, Expression R )
83     {
84     return new Dyadic( L, ExpressionFunctionURIs.AND, R )
85         {
86         public boolean evalBool( Object JavaDoc x, Object JavaDoc y )
87             { return ((Boolean JavaDoc) x).booleanValue() && ((Boolean JavaDoc) y).booleanValue(); }
88         };
89     }
90     }
91
92 /*
93 (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
94 All rights reserved.
95
96 Redistribution and use in source and binary forms, with or without
97 modification, are permitted provided that the following conditions
98 are met:
99
100 1. Redistributions of source code must retain the above copyright
101    notice, this list of conditions and the following disclaimer.
102
103 2. Redistributions in binary form must reproduce the above copyright
104    notice, this list of conditions and the following disclaimer in the
105    documentation and/or other materials provided with the distribution.
106
107 3. The name of the author may not be used to endorse or promote products
108    derived from this software without specific prior written permission.
109
110 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
111 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
112 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
113 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
114 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
115 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
116 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
117 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
118 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
119 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
120 */
Popular Tags