KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > rulesys > impl > LPBindingEnvironment


1 /******************************************************************
2  * File: LPBindingEnvironment.java
3  * Created by: Dave Reynolds
4  * Created on: 25-Jul-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: LPBindingEnvironment.java,v 1.4 2005/02/21 12:17:41 andy_seaborne Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner.rulesys.impl;
11
12 import com.hp.hpl.jena.graph.*;
13 import com.hp.hpl.jena.reasoner.TriplePattern;
14 import com.hp.hpl.jena.reasoner.rulesys.BindingEnvironment;
15 import com.hp.hpl.jena.reasoner.rulesys.Node_RuleVariable;
16
17 /**
18  * Implementation of the binding environment interface for use in LP
19  * backward rules.
20  *
21  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
22  * @version $Revision: 1.4 $ on $Date: 2005/02/21 12:17:41 $
23  */

24 public class LPBindingEnvironment implements BindingEnvironment {
25     
26     /** The interpreter which holds the context for this environment */
27     protected LPInterpreter interpreter;
28     
29     /**
30      * Constructor.
31      */

32     public LPBindingEnvironment(LPInterpreter interpeter) {
33         this.interpreter = interpeter;
34     }
35     
36     /**
37      * Return the most ground version of the node. If the node is not a variable
38      * just return it, if it is a varible bound in this environment return the binding,
39      * if it is an unbound variable return the variable.
40      */

41     public Node getGroundVersion(Node node) {
42         return LPInterpreter.deref(node);
43     }
44     
45     /**
46      * Bind a variable in the current envionment to the given value.
47      * Checks that the new binding is compatible with any current binding.
48      * @param var a Node_RuleVariable defining the variable to bind
49      * @param value the value to bind
50      * @return false if the binding fails
51      */

52     public boolean bind(Node var, Node value) {
53         Node dvar = var;
54         if (dvar instanceof Node_RuleVariable) dvar = ((Node_RuleVariable)dvar).deref();
55         if (dvar instanceof Node_RuleVariable) {
56             interpreter.bind(dvar, value);
57             return true;
58         } else {
59             return var.sameValueAs(value);
60         }
61
62     }
63     
64      
65     /**
66      * Instantiate a triple pattern against the current environment.
67      * This version handles unbound varibles by turning them into bNodes.
68      * @param clause the triple pattern to match
69      * @param env the current binding environment
70      * @return a new, instantiated triple
71      */

72     public Triple instantiate(TriplePattern pattern) {
73         Node s = getGroundVersion(pattern.getSubject());
74         if (s.isVariable()) s = Node.createAnon();
75         Node p = getGroundVersion(pattern.getPredicate());
76         if (p.isVariable()) p = Node.createAnon();
77         Node o = getGroundVersion(pattern.getObject());
78         if (o.isVariable()) o = Node.createAnon();
79         return new Triple(s, p, o);
80     }
81
82
83 }
84
85
86 /*
87     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
88     All rights reserved.
89
90     Redistribution and use in source and binary forms, with or without
91     modification, are permitted provided that the following conditions
92     are met:
93
94     1. Redistributions of source code must retain the above copyright
95        notice, this list of conditions and the following disclaimer.
96
97     2. Redistributions in binary form must reproduce the above copyright
98        notice, this list of conditions and the following disclaimer in the
99        documentation and/or other materials provided with the distribution.
100
101     3. The name of the author may not be used to endorse or promote products
102        derived from this software without specific prior written permission.
103
104     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
105     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
106     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
107     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
108     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
109     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
110     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
111     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
112     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
113     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
114 */
Popular Tags