001: /******************************************************************
002: * File: LPBindingEnvironment.java
003: * Created by: Dave Reynolds
004: * Created on: 25-Jul-2003
005: *
006: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: LPBindingEnvironment.java,v 1.7 2008/01/02 12:06:17 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.reasoner.rulesys.impl;
010:
011: import com.hp.hpl.jena.graph.*;
012: import com.hp.hpl.jena.reasoner.TriplePattern;
013: import com.hp.hpl.jena.reasoner.rulesys.BindingEnvironment;
014: import com.hp.hpl.jena.reasoner.rulesys.Node_RuleVariable;
015:
016: /**
017: * Implementation of the binding environment interface for use in LP
018: * backward rules.
019: *
020: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
021: * @version $Revision: 1.7 $ on $Date: 2008/01/02 12:06:17 $
022: */
023: public class LPBindingEnvironment implements BindingEnvironment {
024:
025: /** The interpreter which holds the context for this environment */
026: protected LPInterpreter interpreter;
027:
028: /**
029: * Constructor.
030: */
031: public LPBindingEnvironment(LPInterpreter interpeter) {
032: this .interpreter = interpeter;
033: }
034:
035: /**
036: * Return the most ground version of the node. If the node is not a variable
037: * just return it, if it is a varible bound in this environment return the binding,
038: * if it is an unbound variable return the variable.
039: */
040: public Node getGroundVersion(Node node) {
041: return LPInterpreter.deref(node);
042: }
043:
044: /**
045: * Bind a variable in the current envionment to the given value.
046: * Checks that the new binding is compatible with any current binding.
047: * @param var a Node_RuleVariable defining the variable to bind
048: * @param value the value to bind
049: * @return false if the binding fails
050: */
051: public boolean bind(Node var, Node value) {
052: Node dvar = var;
053: if (dvar instanceof Node_RuleVariable)
054: dvar = ((Node_RuleVariable) dvar).deref();
055: if (dvar instanceof Node_RuleVariable) {
056: interpreter.bind(dvar, value);
057: return true;
058: } else {
059: return var.sameValueAs(value);
060: }
061:
062: }
063:
064: /**
065: * Instantiate a triple pattern against the current environment.
066: * This version handles unbound varibles by turning them into bNodes.
067: * @param clause the triple pattern to match
068: * @param env the current binding environment
069: * @return a new, instantiated triple
070: */
071: public Triple instantiate(TriplePattern pattern) {
072: Node s = getGroundVersion(pattern.getSubject());
073: if (s.isVariable())
074: s = Node.createAnon();
075: Node p = getGroundVersion(pattern.getPredicate());
076: if (p.isVariable())
077: p = Node.createAnon();
078: Node o = getGroundVersion(pattern.getObject());
079: if (o.isVariable())
080: o = Node.createAnon();
081: return new Triple(s, p, o);
082: }
083:
084: }
085:
086: /*
087: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
088: All rights reserved.
089:
090: Redistribution and use in source and binary forms, with or without
091: modification, are permitted provided that the following conditions
092: are met:
093:
094: 1. Redistributions of source code must retain the above copyright
095: notice, this list of conditions and the following disclaimer.
096:
097: 2. Redistributions in binary form must reproduce the above copyright
098: notice, this list of conditions and the following disclaimer in the
099: 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: */
|