001: /******************************************************************
002: * File: Max.java
003: * Created by: Dave Reynolds
004: * Created on: 22-Sep-2003
005: *
006: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP, all rights reserved.
007: * [See end of file]
008: * $Id: Max.java,v 1.12 2008/01/02 12:06:21 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.reasoner.rulesys.builtins;
010:
011: import com.hp.hpl.jena.reasoner.rulesys.*;
012: import com.hp.hpl.jena.graph.*;
013:
014: /**
015: * Bind the third arg to the max of the first two args.
016: *
017: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
018: * @version $Revision: 1.12 $ on $Date: 2008/01/02 12:06:21 $
019: */
020: public class Max extends BaseBuiltin {
021:
022: /**
023: * Return a name for this builtin, normally this will be the name of the
024: * functor that will be used to invoke it.
025: */
026: public String getName() {
027: return "max";
028: }
029:
030: /**
031: * Return the expected number of arguments for this functor or 0 if the number is flexible.
032: */
033: public int getArgLength() {
034: return 3;
035: }
036:
037: /**
038: * This method is invoked when the builtin is called in a rule body.
039: * @param args the array of argument values for the builtin, this is an array
040: * of Nodes, some of which may be Node_RuleVariables.
041: * @param length the length of the argument list, may be less than the length of the args array
042: * for some rule engines
043: * @param context an execution context giving access to other relevant data
044: * @return return true if the buildin predicate is deemed to have succeeded in
045: * the current environment
046: */
047: public boolean bodyCall(Node[] args, int length, RuleContext context) {
048: checkArgs(length, context);
049: BindingEnvironment env = context.getEnv();
050: Node n1 = getArg(0, args, context);
051: Node n2 = getArg(1, args, context);
052: if (n1.isLiteral() && n2.isLiteral()) {
053: Object v1 = n1.getLiteralValue();
054: Object v2 = n2.getLiteralValue();
055: Node res = null;
056: if (v1 instanceof Number && v2 instanceof Number) {
057: Number nv1 = (Number) v1;
058: Number nv2 = (Number) v2;
059: if (v1 instanceof Float || v1 instanceof Double
060: || v2 instanceof Float || v2 instanceof Double) {
061: res = (nv1.doubleValue() > nv2.doubleValue()) ? n1
062: : n2;
063: } else {
064: res = (nv1.longValue() > nv2.longValue()) ? n1 : n2;
065: }
066: return env.bind(args[2], res);
067: }
068: }
069: // Doesn't (yet) handle partially bound cases
070: return false;
071: }
072:
073: }
074:
075: /*
076: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
077: All rights reserved.
078:
079: Redistribution and use in source and binary forms, with or without
080: modification, are permitted provided that the following conditions
081: are met:
082:
083: 1. Redistributions of source code must retain the above copyright
084: notice, this list of conditions and the following disclaimer.
085:
086: 2. Redistributions in binary form must reproduce the above copyright
087: notice, this list of conditions and the following disclaimer in the
088: documentation and/or other materials provided with the distribution.
089:
090: 3. The name of the author may not be used to endorse or promote products
091: derived from this software without specific prior written permission.
092:
093: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
094: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
095: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
096: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
097: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
098: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
099: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
100: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
101: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
102: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
103: */
|