KickJava   Java API By Example, From Geeks To Geeks.

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


1 /******************************************************************
2  * File: ListLength.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: ListLength.java,v 1.6 2005/02/21 12:17:27 andy_seaborne Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner.rulesys.builtins;
11
12 import com.hp.hpl.jena.reasoner.rulesys.*;
13 import com.hp.hpl.jena.vocabulary.RDF;
14 import com.hp.hpl.jena.graph.*;
15
16 /**
17  * Bind the second arg to the length of the first arg treated as a list.
18  * Fails if the list is malformed.
19  *
20  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
21  * @version $Revision: 1.6 $ on $Date: 2005/02/21 12:17:27 $
22  */

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

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

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

50     public boolean bodyCall(Node[] args, int length, RuleContext context) {
51         checkArgs(length, context);
52         BindingEnvironment env = context.getEnv();
53         int len = getLength(getArg(0, args, context), context);
54         if (len == -1) {
55             return false;
56         } else {
57             env.bind(args[1], Util.makeIntNode(len));
58             return true;
59         }
60     }
61     
62     /**
63      * Return the length of the RDF list rooted at the given node.
64      * @param node the start of the list
65      * @param context the context through which the data values can be found
66      * @return the length or -1 for a malformed list.
67      */

68     protected static int getLength(Node node, RuleContext context ) {
69          if (node.equals(RDF.Nodes.nil)) {
70              return 0;
71          } else {
72              Node next = Util.getPropValue(node, RDF.Nodes.rest, context);
73              if (next == null) {
74                  return -1;
75              } else {
76                  int sublen = getLength(next, context);
77                  if (sublen == -1) {
78                      return -1;
79                  } else {
80                      return 1 + sublen;
81                  }
82              }
83          }
84     }
85 }
86
87
88
89 /*
90     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
91     All rights reserved.
92
93     Redistribution and use in source and binary forms, with or without
94     modification, are permitted provided that the following conditions
95     are met:
96
97     1. Redistributions of source code must retain the above copyright
98        notice, this list of conditions and the following disclaimer.
99
100     2. Redistributions in binary form must reproduce the above copyright
101        notice, this list of conditions and the following disclaimer in the
102        documentation and/or other materials provided with the distribution.
103
104     3. The name of the author may not be used to endorse or promote products
105        derived from this software without specific prior written permission.
106
107     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
108     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
109     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
110     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
111     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
112     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
113     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
114     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
115     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
116     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
117 */
Popular Tags