KickJava   Java API By Example, From Geeks To Geeks.

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


1 /******************************************************************
2  * File: ListEqual.java
3  * Created by: Dave Reynolds
4  * Created on: 23-Sep-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP, all rights reserved.
7  * [See end of file]
8  * $Id: ListEqual.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 java.util.*;
13
14 import com.hp.hpl.jena.reasoner.rulesys.*;
15 import com.hp.hpl.jena.graph.*;
16
17 /**
18  * Test if the two argument lists contain the same semantic elements.
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 ListEqual 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 "listEqual";
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         Node n0 = getArg(0, args, context);
53         Node n1 = getArg(1, args, context);
54         return listEqual(n0, n1, context);
55     }
56     
57     /**
58      * Test two RDF lists for semantic equality. Expensive.
59      */

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