001: /******************************************************************
002: * File: ListContains.java
003: * Created by: Dave Reynolds
004: * Created on: 23-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: ListContains.java,v 1.9 2008/01/02 12:06:21 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: * Returns true if the first argument is a list which contains the second argument.
017: * Can't be used as a generator.
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:21 $
021: */
022: public class ListContains 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 "listContains";
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: Node n0 = getArg(0, args, context);
052: Node n1 = getArg(1, args, context);
053: return listContains(n0, n1, context);
054: }
055:
056: /**
057: * Return true if the first argument is a list which contains
058: * the second argument.
059: */
060: protected static boolean listContains(Node list, Node element,
061: RuleContext context) {
062: if (list == null || list.equals(RDF.Nodes.nil)) {
063: return false;
064: } else {
065: Node elt = Util
066: .getPropValue(list, RDF.Nodes.first, context);
067: if (elt.sameValueAs(element)) {
068: return true;
069: } else {
070: Node next = Util.getPropValue(list, RDF.Nodes.rest,
071: context);
072: return listContains(next, element, context);
073: }
074: }
075: }
076: }
077:
078: /*
079: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
080: All rights reserved.
081:
082: Redistribution and use in source and binary forms, with or without
083: modification, are permitted provided that the following conditions
084: are met:
085:
086: 1. Redistributions of source code must retain the above copyright
087: notice, this list of conditions and the following disclaimer.
088:
089: 2. Redistributions in binary form must reproduce the above copyright
090: notice, this list of conditions and the following disclaimer in the
091: documentation and/or other materials provided with the distribution.
092:
093: 3. The name of the author may not be used to endorse or promote products
094: derived from this software without specific prior written permission.
095:
096: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
097: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
098: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
099: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
100: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
101: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
102: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
103: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
104: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
105: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
106: */
|