KickJava   Java API By Example, From Geeks To Geeks.

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


1 /******************************************************************
2  * File: MakeInstance.java
3  * Created by: Dave Reynolds
4  * Created on: 02-Jun-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: MakeInstance.java,v 1.11 2005/02/21 12:17:29 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.reasoner.rulesys.impl.BBRuleContext;
14 //import com.hp.hpl.jena.util.PrintUtil;
15
import com.hp.hpl.jena.graph.*;
16
17 /**
18  * Create or lookup an anonymous instance of a property value. Syntax of the call is:
19  * <pre>
20  * makeInstance(X, P, D, T) or makeInstance(X, P, T)
21  * </pre>
22  * where X is the instance and P the property for which a temporary
23  * value is required, T will be bound to the temp value (a bNode) and D is
24  * an optional type cor the T value.
25  *
26  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
27  * @version $Revision: 1.11 $ on $Date: 2005/02/21 12:17:29 $
28  */

29 public class MakeInstance extends BaseBuiltin {
30
31     /**
32      * Return a name for this builtin, normally this will be the name of the
33      * functor that will be used to invoke it.
34      */

35     public String JavaDoc getName() {
36         return "makeInstance";
37     }
38
39     /**
40      * This method is invoked when the builtin is called in a rule body.
41      * @param args the array of argument values for the builtin, this is an array
42      * of Nodes, some of which may be Node_RuleVariables.
43      * @param length the length of the argument list, may be less than the length of the args array
44      * for some rule engines
45      * @param context an execution context giving access to other relevant data
46      * @return return true if the buildin predicate is deemed to have succeeded in
47      * the current environment
48      */

49     public boolean bodyCall(Node[] args, int length, RuleContext context) {
50 // System.out.println("MakeInstance on ");
51
// for (int i = 0; i < length; i++) {
52
// System.out.println(" - " + PrintUtil.print(args[i]));
53
// }
54
if (length == 3 || length == 4) {
55             Node inst = getArg(0, args, context);
56             Node prop = getArg(1, args, context);
57             Node pclass = length == 4 ? getArg(2, args, context) : null;
58             if (context instanceof BBRuleContext) {
59                 Node temp = ((BBRuleContext)context).getTemp(inst, prop, pclass);
60                 return context.getEnv().bind(args[length-1], temp);
61             } else {
62                 throw new BuiltinException(this, context, "builtin " + getName() + " only usable in backward/hybrid rule sets");
63             }
64         } else {
65             throw new BuiltinException(this, context, "builtin " + getName() + " requries 3 or 4 arguments");
66         }
67     }
68  
69 }
70
71
72 /*
73     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
74     All rights reserved.
75
76     Redistribution and use in source and binary forms, with or without
77     modification, are permitted provided that the following conditions
78     are met:
79
80     1. Redistributions of source code must retain the above copyright
81        notice, this list of conditions and the following disclaimer.
82
83     2. Redistributions in binary form must reproduce the above copyright
84        notice, this list of conditions and the following disclaimer in the
85        documentation and/or other materials provided with the distribution.
86
87     3. The name of the author may not be used to endorse or promote products
88        derived from this software without specific prior written permission.
89
90     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
91     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
92     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
93     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
94     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
95     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
96     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
97     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
98     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
99     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
100 */
Popular Tags