/**
*
*/
package com.hp.jena.rules.functions.jena2;
import java.util.HashMap;
import java.util.List;
import com.hp.jena.graph.Node;
import com.hp.jena.rules.functions.ApplyableBase;
import com.hp.jena.rules.retelike.impl.Bindings;
import com.hp.jena.rules.retelike.impl.ExecContext;
import com.hp.jena.shared.JenaException;
import com.hp.jena.ymris.util.Pair;
public class MakeInstance extends ApplyableBase
{
// * Create or lookup an anonymous instance of a property value. Syntax of the call is:
// * <pre>
// * makeInstance(X, P, D, T) or makeInstance(X, P, T)
// * </pre>
// * where X is the instance and P the property for which a temporary
// * value is required, T will be bound to the temp value (a bNode) and D is
// * an optional type cor the T value.
protected final Node X, P, D, T;
static private HashMap<Object, Node> bnodes;
public MakeInstance( List<Node> nargs )
{
X = nargs.get( 0 );
P = nargs.get( 1 );
T = nargs.get( nargs.size() - 1 );
D = nargs.size() == 4 ? nargs.get( 2 ) : null;
}
@Override public boolean evalBool( ExecContext c, Bindings<Node, Node> item )
{
Node xv = eval( item, X ), pv = eval( item, P );
Node dv = D == null ? null : eval( item, D );
if (dv == null)
{
throw new JenaException( "OOPS, we didn't do MakeInstance with no specified class" );
}
else
{
Object key = Pair.create( xv, Pair.create( pv, dv ) );
Node already = bnodes.get( key );
if (already == null) bnodes.put( key, already = c.createBNode() );
return ApplyableBase.bind( item, T, already );
}
}
}