001: /******************************************************************
002: * File: MakeInstance.java
003: * Created by: Dave Reynolds
004: * Created on: 02-Jun-2003
005: *
006: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: MakeInstance.java,v 1.14 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.reasoner.rulesys.impl.BBRuleContext; //import com.hp.hpl.jena.util.PrintUtil;
013: import com.hp.hpl.jena.graph.*;
014:
015: /**
016: * Create or lookup an anonymous instance of a property value. Syntax of the call is:
017: * <pre>
018: * makeInstance(X, P, D, T) or makeInstance(X, P, T)
019: * </pre>
020: * where X is the instance and P the property for which a temporary
021: * value is required, T will be bound to the temp value (a bNode) and D is
022: * an optional type cor the T value.
023: *
024: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
025: * @version $Revision: 1.14 $ on $Date: 2008/01/02 12:06:21 $
026: */
027: public class MakeInstance extends BaseBuiltin {
028:
029: /**
030: * Return a name for this builtin, normally this will be the name of the
031: * functor that will be used to invoke it.
032: */
033: public String getName() {
034: return "makeInstance";
035: }
036:
037: /**
038: * This method is invoked when the builtin is called in a rule body.
039: * @param args the array of argument values for the builtin, this is an array
040: * of Nodes, some of which may be Node_RuleVariables.
041: * @param length the length of the argument list, may be less than the length of the args array
042: * for some rule engines
043: * @param context an execution context giving access to other relevant data
044: * @return return true if the buildin predicate is deemed to have succeeded in
045: * the current environment
046: */
047: public boolean bodyCall(Node[] args, int length, RuleContext context) {
048: // System.out.println("MakeInstance on ");
049: // for (int i = 0; i < length; i++) {
050: // System.out.println(" - " + PrintUtil.print(args[i]));
051: // }
052: if (length == 3 || length == 4) {
053: Node inst = getArg(0, args, context);
054: Node prop = getArg(1, args, context);
055: Node pclass = length == 4 ? getArg(2, args, context) : null;
056: if (context instanceof BBRuleContext) {
057: Node temp = ((BBRuleContext) context).getTemp(inst,
058: prop, pclass);
059: return context.getEnv().bind(args[length - 1], temp);
060: } else {
061: throw new BuiltinException(this , context, "builtin "
062: + getName()
063: + " only usable in backward/hybrid rule sets");
064: }
065: } else {
066: throw new BuiltinException(this , context, "builtin "
067: + getName() + " requries 3 or 4 arguments");
068: }
069: }
070:
071: }
072:
073: /*
074: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
075: All rights reserved.
076:
077: Redistribution and use in source and binary forms, with or without
078: modification, are permitted provided that the following conditions
079: are met:
080:
081: 1. Redistributions of source code must retain the above copyright
082: notice, this list of conditions and the following disclaimer.
083:
084: 2. Redistributions in binary form must reproduce the above copyright
085: notice, this list of conditions and the following disclaimer in the
086: documentation and/or other materials provided with the distribution.
087:
088: 3. The name of the author may not be used to endorse or promote products
089: derived from this software without specific prior written permission.
090:
091: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
092: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
093: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
094: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
095: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
096: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
097: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
098: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
099: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
100: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
101: */
|