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