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