KickJava   Java API By Example, From Geeks To Geeks.

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


1 /******************************************************************
2  * File: CountLiteralValues.java
3  * Created by: Dave Reynolds
4  * Created on: 24-Aug-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: CountLiteralValues.java,v 1.5 2005/02/21 12:17:09 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.graph.*;
14 import java.util.*;
15
16 /**
17  * CountLiteralValues(X, P, C) sets C to be the number of semantically
18  * distinct values for P on resource X. This is expensive.
19  *
20  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
21  * @version $Revision: 1.5 $ on $Date: 2005/02/21 12:17:09 $
22  */

23 public class CountLiteralValues 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 "countLiteralValues";
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 3;
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         ArrayList values = new ArrayList();
52         Node a0 = getArg(0, args, context);
53         Node a1 = getArg(1, args, context);
54         for (Iterator ni = context.find(a0, a1, null); ni.hasNext(); ) {
55             Node v = ((Triple)ni.next()).getObject();
56             if (v.isLiteral()) {
57                 // Can't just use contains because distinct objects may
58
// be semantically equal
59
boolean gotit = false;
60                 for (Iterator i = values.iterator(); i.hasNext(); ) {
61                     if (v.sameValueAs(i.next())) {
62                         gotit = true;
63                         break;
64                     }
65                 }
66                 if (!gotit) {
67                     values.add(v);
68                 }
69             }
70         }
71         return context.getEnv().bind(args[2], Util.makeIntNode(values.size()));
72     }
73     
74 }
75
76 /*
77     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
78     All rights reserved.
79
80     Redistribution and use in source and binary forms, with or without
81     modification, are permitted provided that the following conditions
82     are met:
83
84     1. Redistributions of source code must retain the above copyright
85        notice, this list of conditions and the following disclaimer.
86
87     2. Redistributions in binary form must reproduce the above copyright
88        notice, this list of conditions and the following disclaimer in the
89        documentation and/or other materials provided with the distribution.
90
91     3. The name of the author may not be used to endorse or promote products
92        derived from this software without specific prior written permission.
93
94     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
95     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
96     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
97     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
98     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
99     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
100     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
101     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
102     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
103     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
104 */
Popular Tags