01: /*
02: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */
18: package org.mandarax.lib.math;
19:
20: import org.mandarax.kernel.*;
21:
22: /**
23: * Abstract superclass for double arithmetic predicates with three arguments.
24: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
25: * @version 3.4 <7 March 05>
26: * @since 1.4 (in 1.3 in the renamed package org.mandarax.math)
27: */
28: public abstract class TernaryDoubleArithmeticPredicate extends
29: org.mandarax.lib.AbstractPredicate {
30:
31: private static Class[] structure = { Double.class, Double.class,
32: Double.class };
33:
34: /**
35: * Evaluate the three values using the predicate.
36: * @return tre or false
37: * @param d1 value 1
38: * @param d2 value 2
39: * @param d3 value 3
40: */
41: protected abstract boolean evaluate(double d1, double d2, double d3);
42:
43: /**
44: * Get the structure of the predicate.
45: * @return the structure of the predicate
46: */
47: public Class[] getStructure() {
48: return structure;
49: }
50:
51: /**
52: * Perform the function or predicate using an array of terms as parameters.
53: * @return java.lang.Object
54: * @param parameter org.mandarax.kernel.Term[]
55: * @param session a session object
56: * @throws java.lang.UnsupportedOperationException
57: * @throws java.lang.IllegalArgumentException
58: */
59: public Object perform(Term[] parameter, Session session)
60: throws IllegalArgumentException,
61: UnsupportedOperationException {
62: double d1, d2, d3;
63:
64: try {
65: d1 = ((Number) (parameter[0].resolve(session)))
66: .doubleValue();
67: d2 = ((Number) (parameter[1].resolve(session)))
68: .doubleValue();
69: d3 = ((Number) (parameter[2].resolve(session)))
70: .doubleValue();
71: } catch (Exception x) {
72: throw new IllegalArgumentException();
73: }
74:
75: return (evaluate(d1, d2, d3)) ? Boolean.TRUE : Boolean.FALSE;
76: }
77:
78: /**
79: * Convert the object to a string.
80: * @return a string
81: */
82: public String toString() {
83: return (getName() == null) ? super.toString() : getName();
84: }
85: }
|