001: /******************************************************************
002: * File: CountLiteralValues.java
003: * Created by: Dave Reynolds
004: * Created on: 24-Aug-2003
005: *
006: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: CountLiteralValues.java,v 1.8 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.graph.*;
013: import java.util.*;
014:
015: /**
016: * CountLiteralValues(X, P, C) sets C to be the number of semantically
017: * distinct values for P on resource X. This is expensive.
018: *
019: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
020: * @version $Revision: 1.8 $ on $Date: 2008/01/02 12:06:20 $
021: */
022: public class CountLiteralValues 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 "countLiteralValues";
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 3;
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: ArrayList values = new ArrayList();
051: Node a0 = getArg(0, args, context);
052: Node a1 = getArg(1, args, context);
053: for (Iterator ni = context.find(a0, a1, null); ni.hasNext();) {
054: Node v = ((Triple) ni.next()).getObject();
055: if (v.isLiteral()) {
056: // Can't just use contains because distinct objects may
057: // be semantically equal
058: boolean gotit = false;
059: for (Iterator i = values.iterator(); i.hasNext();) {
060: if (v.sameValueAs(i.next())) {
061: gotit = true;
062: break;
063: }
064: }
065: if (!gotit) {
066: values.add(v);
067: }
068: }
069: }
070: return context.getEnv().bind(args[2],
071: Util.makeIntNode(values.size()));
072: }
073:
074: }
075:
076: /*
077: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
078: All rights reserved.
079:
080: Redistribution and use in source and binary forms, with or without
081: modification, are permitted provided that the following conditions
082: are met:
083:
084: 1. Redistributions of source code must retain the above copyright
085: notice, this list of conditions and the following disclaimer.
086:
087: 2. Redistributions in binary form must reproduce the above copyright
088: notice, this list of conditions and the following disclaimer in the
089: documentation and/or other materials provided with the distribution.
090:
091: 3. The name of the author may not be used to endorse or promote products
092: derived from this software without specific prior written permission.
093:
094: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
095: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
096: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
097: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
098: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
099: 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: */
|