001: /******************************************************************
002: * File: ListLength.java
003: * Created by: Dave Reynolds
004: * Created on: 22-Sep-2003
005: *
006: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP, all rights reserved.
007: * [See end of file]
008: * $Id: ListLength.java,v 1.9 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.reasoner.rulesys.*;
012: import com.hp.hpl.jena.vocabulary.RDF;
013: import com.hp.hpl.jena.graph.*;
014:
015: /**
016: * Bind the second arg to the length of the first arg treated as a list.
017: * Fails if the list is malformed.
018: *
019: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
020: * @version $Revision: 1.9 $ on $Date: 2008/01/02 12:06:20 $
021: */
022: public class ListLength 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 "listLength";
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 2;
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: int len = getLength(getArg(0, args, context), context);
053: if (len == -1) {
054: return false;
055: } else {
056: env.bind(args[1], Util.makeIntNode(len));
057: return true;
058: }
059: }
060:
061: /**
062: * Return the length of the RDF list rooted at the given node.
063: * @param node the start of the list
064: * @param context the context through which the data values can be found
065: * @return the length or -1 for a malformed list.
066: */
067: protected static int getLength(Node node, RuleContext context) {
068: if (node.equals(RDF.Nodes.nil)) {
069: return 0;
070: } else {
071: Node next = Util
072: .getPropValue(node, RDF.Nodes.rest, context);
073: if (next == null) {
074: return -1;
075: } else {
076: int sublen = getLength(next, context);
077: if (sublen == -1) {
078: return -1;
079: } else {
080: return 1 + sublen;
081: }
082: }
083: }
084: }
085: }
086:
087: /*
088: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
089: All rights reserved.
090:
091: Redistribution and use in source and binary forms, with or without
092: modification, are permitted provided that the following conditions
093: are met:
094:
095: 1. Redistributions of source code must retain the above copyright
096: notice, this list of conditions and the following disclaimer.
097:
098: 2. Redistributions in binary form must reproduce the above copyright
099: notice, this list of conditions and the following disclaimer in the
100: documentation and/or other materials provided with the distribution.
101:
102: 3. The name of the author may not be used to endorse or promote products
103: derived from this software without specific prior written permission.
104:
105: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
106: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
107: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
108: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
109: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
110: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
111: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
112: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
113: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
114: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
115: */
|