KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > rulesys > builtins > Difference


1 /******************************************************************
2  * File: Difference.java
3  * Created by: Dave Reynolds
4  * Created on: 12-Apr-2005
5  *
6  * (c) Copyright 2005, Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: Difference.java,v 1.1 2005/04/12 10:55:45 der Exp $
9  *****************************************************************/

10
11 package com.hp.hpl.jena.reasoner.rulesys.builtins;
12
13 import com.hp.hpl.jena.reasoner.rulesys.*;
14 import com.hp.hpl.jena.graph.*;
15
16 /**
17  * Bind the third argument to the arithetic difference between the
18  * first and second aguments.
19  *
20  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
21  * @version $Revision: 1.1 $
22  */

23
24 public class Difference extends BaseBuiltin {
25
26     /**
27      * Return a name for this builtin, normally this will be the name of the
28      * functor that will be used to invoke it.
29      */

30     public String JavaDoc getName() {
31         return "difference";
32     }
33     
34     /**
35      * Return the expected number of arguments for this functor or 0 if the number is flexible.
36      */

37     public int getArgLength() {
38         return 3;
39     }
40
41     /**
42      * This method is invoked when the builtin is called in a rule body.
43      * @param args the array of argument values for the builtin, this is an array
44      * of Nodes, some of which may be Node_RuleVariables.
45      * @param length the length of the argument list, may be less than the length of the args array
46      * for some rule engines
47      * @param context an execution context giving access to other relevant data
48      * @return return true if the buildin predicate is deemed to have succeeded in
49      * the current environment
50      */

51     public boolean bodyCall(Node[] args, int length, RuleContext context) {
52         checkArgs(length, context);
53         BindingEnvironment env = context.getEnv();
54         Node n1 = getArg(0, args, context);
55         Node n2 = getArg(1, args, context);
56         if (n1.isLiteral() && n2.isLiteral()) {
57             Object JavaDoc v1 = n1.getLiteral().getValue();
58             Object JavaDoc v2 = n2.getLiteral().getValue();
59             Node sum = null;
60             if (v1 instanceof Number JavaDoc && v2 instanceof Number JavaDoc) {
61                 Number JavaDoc nv1 = (Number JavaDoc)v1;
62                 Number JavaDoc nv2 = (Number JavaDoc)v2;
63                 if (v1 instanceof Float JavaDoc || v1 instanceof Double JavaDoc
64                 || v2 instanceof Float JavaDoc || v2 instanceof Double JavaDoc) {
65                     sum = Util.makeDoubleNode(nv1.doubleValue() - nv2.doubleValue());
66                 } else {
67                     sum = Util.makeLongNode(nv1.longValue() - nv2.longValue());
68                 }
69                 env.bind(args[2], sum);
70                 return true;
71             }
72         }
73         // Doesn't (yet) handle partially bound cases
74
return false;
75     }
76     
77 }
78
79
80 /*
81     (c) Copyright 2005 Hewlett-Packard Development Company, LP
82     All rights reserved.
83
84     Redistribution and use in source and binary forms, with or without
85     modification, are permitted provided that the following conditions
86     are met:
87
88     1. Redistributions of source code must retain the above copyright
89        notice, this list of conditions and the following disclaimer.
90
91     2. Redistributions in binary form must reproduce the above copyright
92        notice, this list of conditions and the following disclaimer in the
93        documentation and/or other materials provided with the distribution.
94
95     3. The name of the author may not be used to endorse or promote products
96        derived from this software without specific prior written permission.
97
98     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
99     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 */

109
Popular Tags