001: /*
002: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP, all rights reserved.
003: [See end of file]
004: $Id: Dyadic.java,v 1.6 2008/01/02 12:07:57 andy_seaborne Exp $
005: */
006: package com.hp.hpl.jena.graph.query;
007:
008: import com.hp.hpl.jena.graph.query.Expression.Application;
009: import com.hp.hpl.jena.shared.JenaException;
010:
011: /**
012: A base class for dyadic expressions with a built-in Valuator; subclasses must
013: define an evalObject or evalBool method which will be supplied with the
014: evaluated operands.
015:
016: @author kers
017: */
018: public abstract class Dyadic extends Application {
019: protected Expression L;
020: protected Expression R;
021: protected String F;
022:
023: public Dyadic(Expression L, String F, Expression R) {
024: this .L = L;
025: this .F = F;
026: this .R = R;
027: }
028:
029: public int argCount() {
030: return 2;
031: }
032:
033: public Expression getArg(int i) {
034: return i == 0 ? L : R;
035: }
036:
037: public String getFun() {
038: return F;
039: }
040:
041: /**
042: Answer the Object result of evaluating this dyadic expression with
043: the given arguments <code>l</code> and <code>r</code>.
044: Either this method or <code>evalBool</code> <i>must</i> be
045: over-ridden in concrete sub-classes.
046: */
047: public Object evalObject(Object l, Object r) {
048: return evalBool(l, r) ? Boolean.TRUE : Boolean.FALSE;
049: }
050:
051: /**
052: Answer the boolean result of evaluating this dyadic expression with
053: the given arguments <code>l</code> and <code>r</code>.
054: Either this method or <code>evalObject</code> <i>must</i> be
055: over-ridden in concrete sub-classes.
056: */
057: public boolean evalBool(Object l, Object r) {
058: Object x = evalObject(l, r);
059: if (x instanceof Boolean)
060: return ((Boolean) x).booleanValue();
061: throw new JenaException("not Boolean: " + x);
062: }
063:
064: public Valuator prepare(VariableIndexes vi) {
065: final Valuator l = L.prepare(vi), r = R.prepare(vi);
066: return new Valuator() {
067: public boolean evalBool(IndexValues iv) {
068: return ((Boolean) evalObject(iv)).booleanValue();
069: }
070:
071: public Object evalObject(IndexValues iv) {
072: return Dyadic.this .evalObject(l.evalObject(iv), r
073: .evalObject(iv));
074: }
075:
076: };
077: }
078:
079: public String toString() {
080: return L.toString() + " " + F + " " + R.toString();
081: }
082:
083: public static Expression and(Expression L, Expression R) {
084: return new Dyadic(L, ExpressionFunctionURIs.AND, R) {
085: public boolean evalBool(Object x, Object y) {
086: return ((Boolean) x).booleanValue()
087: && ((Boolean) y).booleanValue();
088: }
089: };
090: }
091: }
092:
093: /*
094: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
095: All rights reserved.
096:
097: Redistribution and use in source and binary forms, with or without
098: modification, are permitted provided that the following conditions
099: are met:
100:
101: 1. Redistributions of source code must retain the above copyright
102: notice, this list of conditions and the following disclaimer.
103:
104: 2. Redistributions in binary form must reproduce the above copyright
105: notice, this list of conditions and the following disclaimer in the
106: documentation and/or other materials provided with the distribution.
107:
108: 3. The name of the author may not be used to endorse or promote products
109: derived from this software without specific prior written permission.
110:
111: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
112: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
113: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
114: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
115: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
116: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
117: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
118: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
119: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
120: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
121: */
|