📄 bindingvector.java
字号:
/******************************************************************
* File: BindingVector.java
* Created by: Dave Reynolds
* Created on: 28-Apr-03
*
* (c) Copyright 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
* [See end of file]
* $Id: BindingVector.java,v 1.25 2007/01/02 11:48:41 andy_seaborne Exp $
*****************************************************************/
package com.hp.hpl.jena.reasoner.rulesys.impl;
import com.hp.hpl.jena.graph.*;
import com.hp.hpl.jena.reasoner.*;
import com.hp.hpl.jena.reasoner.rulesys.*;
import com.hp.hpl.jena.util.PrintUtil;
import java.util.*;
/**
* An implementation of a binding environment that maintains
* a single array of bound values for the variables in a rule.
* Stack management is done externally. This is intended for use in
* the Brule system and so also supports variable-variable bindings by
* use of reference chains.
*
* @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
* @version $Revision: 1.25 $ on $Date: 2007/01/02 11:48:41 $
*/
public class BindingVector implements BindingEnvironment {
/** The current binding set */
protected Node[] environment;
/**
* Constructor - create an empty binding environment
*/
public BindingVector(int size) {
environment = new Node[size];
}
/**
* Constructor - create a binding environment from a vector of bindings
*/
public BindingVector(Node [] env) {
environment = env;
}
/**
* Constructor - create a binding environment which is a copy
* of the given environment
*/
public BindingVector(BindingVector clone) {
Node[] orig = clone.environment;
environment = new Node[orig.length];
System.arraycopy(orig, 0, environment, 0, orig.length);
}
/**
* Return the current array of bindings. Useful for fast access to
* serveral bindings, not useful for doing updates.
*/
public Node[] getEnvironment() {
return environment;
}
/**
* If the node is a variable then return the current binding (null if not bound)
* otherwise return the node itself.
*/
public Node getBinding(Node node) {
if (node instanceof Node_RuleVariable) {
Node val = environment[((Node_RuleVariable)node).getIndex()];
if (val instanceof Node_RuleVariable) {
return getBinding(val);
} else {
return val;
}
} else if (node instanceof Node_ANY) {
return null;
} else if (Functor.isFunctor(node)) {
Functor functor = (Functor)node.getLiteralValue();
if (functor.isGround()) return node;
Node[] args = functor.getArgs();
ArrayList boundargs = new ArrayList(args.length);
for (int i = 0; i < args.length; i++) {
Object binding = getBinding(args[i]);
if (binding == null) {
// Not sufficently bound to instantiate functor yet
return null;
}
boundargs.add(binding);
}
Functor newf = new Functor( functor.getName(), boundargs );
return Functor.makeFunctorNode( newf );
} else {
return node;
}
}
/**
* Return the most ground version of the node. If the node is not a variable
* just return it, if it is a varible bound in this enviroment return the binding,
* if it is an unbound variable return the variable.
*/
public Node getGroundVersion(Node node) {
Node bind = getBinding(node);
if (bind == null) {
return node;
} else {
return bind;
}
}
/**
* Bind the ith variable in the current envionment to the given value.
* Checks that the new binding is compatible with any current binding.
* Handles aliased variables.
* @return false if the binding fails
*/
public boolean bind(int i, Node value) {
Node node = environment[i];
if (node == null) {
environment[i] = value;
return true;
} else if (node instanceof Node_RuleVariable) {
environment[i] = value;
return bind(((Node_RuleVariable)node).getIndex(), value);
} else {
return node.sameValueAs(value);
}
}
/**
* Bind a variable in the current envionment to the given value.
* Checks that the new binding is compatible with any current binding.
* @param var a Node_RuleVariable defining the variable to bind
* @param value the value to bind
* @return false if the binding fails
*/
public boolean bind(Node var, Node value) {
if (var instanceof Node_RuleVariable) {
return bind(((Node_RuleVariable)var).getIndex(), value);
} else {
return var.sameValueAs(value);
}
}
/**
* Bind the variables in a goal pattern using the binding environment, to
* generate a more specialized goal
* @param goal the TriplePattern to be instantiated
* @return a TriplePattern obtained from the goal by substituting current bindinds
*/
public TriplePattern partInstantiate(TriplePattern goal) {
return new TriplePattern(
getGroundVersion(goal.getSubject()),
getGroundVersion(goal.getPredicate()),
getGroundVersion(goal.getObject())
);
}
// Replaced by version below for consistency with stack variant
// /**
// * Instatiate a goal pattern using the binding environment
// * @param goal the TriplePattern to be instantiated
// * @return an instantiated Triple
// */
// public Triple instantiate(TriplePattern goal) {
// return new Triple(
// getGroundVersion(goal.getSubject()),
// getGroundVersion(goal.getPredicate()),
// getGroundVersion(goal.getObject())
// );
// }
/**
* Instantiate a triple pattern against the current environment.
* This version handles unbound varibles by turning them into bNodes.
* @param clause the triple pattern to match
* @param env the current binding environment
* @return a new, instantiated triple
*/
public Triple instantiate(TriplePattern pattern) {
Node s = getGroundVersion(pattern.getSubject());
if (s.isVariable()) s = Node.createAnon();
Node p = getGroundVersion(pattern.getPredicate());
if (p.isVariable()) p = Node.createAnon();
Node o = getGroundVersion(pattern.getObject());
if (o.isVariable()) o = Node.createAnon();
return new Triple(s, p, o);
}
/**
* Printable form
*/
public String toString() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -