KickJava   Java API By Example, From Geeks To Geeks.

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


1 /******************************************************************
2  * File: Min.java
3  * Created by: Dave Reynolds
4  * Created on: 22-Sep-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP, all rights reserved.
7  * [See end of file]
8  * $Id: Min.java,v 1.7 2005/02/21 12:17:30 andy_seaborne Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner.rulesys.builtins;
11
12
13 import com.hp.hpl.jena.reasoner.rulesys.*;
14 import com.hp.hpl.jena.graph.*;
15
16 /**
17  * Bind the third arg to the min of the first two args.
18  *
19  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
20  * @version $Revision: 1.7 $ on $Date: 2005/02/21 12:17:30 $
21  */

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

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

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

49     public boolean bodyCall(Node[] args, int length, RuleContext context) {
50         checkArgs(length, context);
51         BindingEnvironment env = context.getEnv();
52         Node n1 = getArg(0, args, context);
53         Node n2 = getArg(1, args, context);
54         if (n1.isLiteral() && n2.isLiteral()) {
55             Object JavaDoc v1 = n1.getLiteral().getValue();
56             Object JavaDoc v2 = n2.getLiteral().getValue();
57             Node res = null;
58             if (v1 instanceof Number JavaDoc && v2 instanceof Number JavaDoc) {
59                 Number JavaDoc nv1 = (Number JavaDoc)v1;
60                 Number JavaDoc nv2 = (Number JavaDoc)v2;
61                 if (v1 instanceof Float JavaDoc || v1 instanceof Double JavaDoc
62                 || v2 instanceof Float JavaDoc || v2 instanceof Double JavaDoc) {
63                     res = (nv1.doubleValue() > nv2.doubleValue()) ? n2 : n1;
64                 } else {
65                     res = (nv1.longValue() > nv2.longValue()) ? n2 : n1;
66                 }
67                 env.bind(args[2], res);
68                 return true;
69             }
70         }
71         // Doesn't (yet) handle partially bound cases
72
return false;
73     }
74     
75 }
76
77 /*
78     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
79     All rights reserved.
80
81     Redistribution and use in source and binary forms, with or without
82     modification, are permitted provided that the following conditions
83     are met:
84
85     1. Redistributions of source code must retain the above copyright
86        notice, this list of conditions and the following disclaimer.
87
88     2. Redistributions in binary form must reproduce the above copyright
89        notice, this list of conditions and the following disclaimer in the
90        documentation and/or other materials provided with the distribution.
91
92     3. The name of the author may not be used to endorse or promote products
93        derived from this software without specific prior written permission.
94
95     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
96     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
97     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
98     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
99     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
100     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
101     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
102     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
103     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
104     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
105 */
Popular Tags