001: /******************************************************************
002: * File: BaseBuiltin.java
003: * Created by: Dave Reynolds
004: * Created on: 10-Jun-2003
005: *
006: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: BaseBuiltin.java,v 1.14 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:
014: /**
015: * Dummy implementation of the Builtin interface that specific
016: * implementations can inherit from.
017: *
018: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
019: * @version $Revision: 1.14 $ on $Date: 2008/01/02 12:06:20 $
020: */
021: public abstract class BaseBuiltin implements Builtin {
022:
023: /** Base URI for jena builtins */
024: public static final String BASE_URI = "http://jena.hpl.hp.com/2003/RuleBuiltin/";
025:
026: /**
027: * Return the full URI which identifies this built in.
028: */
029: public String getURI() {
030: return BASE_URI + getName();
031: }
032:
033: /**
034: * Return the expected number of arguments for this functor or 0 if the number is flexible.
035: */
036: public int getArgLength() {
037: return 0;
038: }
039:
040: /**
041: * Check the argument length.
042: */
043: public void checkArgs(int length, RuleContext context) {
044: int expected = getArgLength();
045: if (expected > 0 && expected != length) {
046: throw new BuiltinException(this , context, "builtin "
047: + getName() + " requires " + expected
048: + " arguments but saw " + length);
049: }
050: }
051:
052: /**
053: * This method is invoked when the builtin is called in a rule body.
054: * @param args the array of argument values for the builtin, this is an array
055: * of Nodes, some of which may be Node_RuleVariables.
056: * @param length the length of the argument list, may be less than the length of the args array
057: * for some rule engines
058: * @param context an execution context giving access to other relevant data
059: * @return return true if the buildin predicate is deemed to have succeeded in
060: * the current environment
061: */
062: public boolean bodyCall(Node[] args, int length, RuleContext context) {
063: throw new BuiltinException(this , context, "builtin "
064: + getName() + " not usable in rule bodies");
065: }
066:
067: /**
068: * This method is invoked when the builtin is called in a rule head.
069: * Such a use is only valid in a forward rule.
070: * @param args the array of argument values for the builtin, this is an array
071: * of Nodes.
072: * @param length the length of the argument list, may be less than the length of the args array
073: * for some rule engines
074: * @param context an execution context giving access to other relevant data
075: */
076: public void headAction(Node[] args, int length, RuleContext context) {
077: throw new BuiltinException(this , context, "builtin "
078: + getName() + " not usable in rule heads");
079: }
080:
081: /**
082: * Returns false if this builtin has side effects when run in a body clause,
083: * other than the binding of environment variables.
084: */
085: public boolean isSafe() {
086: // Default is safe!
087: return true;
088: }
089:
090: /**
091: * Returns false if this builtin is non-monotonic. This includes non-monotonic checks like noValue
092: * and non-monotonic actions like remove/drop. A non-monotonic call in a head is assumed to
093: * be an action and makes the overall rule and ruleset non-monotonic.
094: * Most JenaRules are monotonic deductive closure rules in which this should be false.
095: */
096: public boolean isMonotonic() {
097: return true;
098: }
099:
100: /**
101: * Return the n'th argument node after dererencing by what ever type of
102: * rule engine binding environment is appropriate.
103: */
104: public Node getArg(int n, Node[] args, RuleContext context) {
105: return context.getEnv().getGroundVersion(args[n]);
106: }
107:
108: }
109:
110: /*
111: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
112: All rights reserved.
113:
114: Redistribution and use in source and binary forms, with or without
115: modification, are permitted provided that the following conditions
116: are met:
117:
118: 1. Redistributions of source code must retain the above copyright
119: notice, this list of conditions and the following disclaimer.
120:
121: 2. Redistributions in binary form must reproduce the above copyright
122: notice, this list of conditions and the following disclaimer in the
123: documentation and/or other materials provided with the distribution.
124:
125: 3. The name of the author may not be used to endorse or promote products
126: derived from this software without specific prior written permission.
127:
128: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
129: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
130: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
131: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
132: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
133: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
134: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
135: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
136: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
137: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
138: */
|