/*
(c) Copyright 2008, 2009 Hewlett-Packard Development Company, LP
All rights reserved.
$Id$
*/
/*
(c) Copyright 2008 Hewlett-Packard Development Company, LP
All rights reserved.
$Id$
*/
package com.hp.jena3.rules.retelike.impl.tests;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import org.junit.Test;
import static org.junit.Assert.*;
import com.hp.jena.graph.Node;
import com.hp.jena.rules.retelike.impl.*;
import com.hp.jena.rules.retelike.impl.scratch.Functor;
import static com.hp.jena.rules.retelike.impl.NodeTerm.create;
import static com.hp.jena3.rules.retelike.impl.tests.TestBindings.nodeBindings;
import static com.hp.jena.shell.test.utils.NodeCreateUtils.*;
import static com.hp.jena.test.JenaTestBase.*;
public class TestNodeTerm
{
@Test public void ensureGetNodeFetchesNode()
{
ensureGetNodeFetchesNode( node( "x" ) );
ensureGetNodeFetchesNode( node( "?x" ) );
ensureGetNodeFetchesNode( node( "17" ) );
ensureGetNodeFetchesNode( node( "_blank" ) );
ensureGetNodeFetchesNode( node( "'chat'en" ) );
ensureGetNodeFetchesNode( functor( "f(x)" ) );
}
private static Node functor( String template )
{
StringTokenizer st = new StringTokenizer( template, " ()" );
List<Node> terms = new ArrayList<Node>();
while (st.hasMoreTokens()) terms.add( node( st.nextToken() ) );
return new Functor( terms );
}
private void ensureGetNodeFetchesNode( Node n )
{ assertSame( n, create( n ).getNode() ); }
@Test public void ensureCreateMatchesVariability()
{
assertInstanceOf( NodeTerm.NodeTermConstant.class, create( node( "x" ) ) );
assertInstanceOf( NodeTerm.NodeTermConstant.class, create( node( "17" ) ) );
assertInstanceOf( NodeTerm.NodeTermConstant.class, create( node( "_b" ) ) );
assertInstanceOf( NodeTerm.NodeTermConstant.class, create( node( "'hello'" ) ) );
assertInstanceOf( NodeTerm.NodeTermVariable.class, create( node( "?x" ) ) );
assertInstanceOf( NodeTerm.NodeTermFunctor.class, create( functor( "f(?x))" ) ) );
}
@Test public void ensureIsVariableTrueIffVariable()
{
assertTrue( NodeTerm.create( node( "?x" ) ).isVariable() );
assertFalse( NodeTerm.create( node( "x" ) ).isVariable() );
assertFalse( NodeTerm.create( node( "_b" ) ).isVariable() );
assertFalse( NodeTerm.create( node( "17" ) ).isVariable() );
assertFalse( NodeTerm.create( node( "'hello'" ) ).isVariable() );
assertFalse( NodeTerm.create( functor( "f('x')" ) ).isVariable() );
}
@Test public void ensureHashcodeSameAsNode()
{
ensureHasSameHashcodeAsNode( node( "x" ) );
ensureHasSameHashcodeAsNode( node( "17" ) );
ensureHasSameHashcodeAsNode( node( "_anon" ) );
ensureHasSameHashcodeAsNode( node( "'string'" ) );
ensureHasSameHashcodeAsNode( node( "?y" ) );
ensureHasSameHashcodeAsNode( functor( "f(x)" ) );
}
private void ensureHasSameHashcodeAsNode( Node n )
{ assertEquals( n.hashCode(), create( n ).hashCode() ); }
@Test public void ensureEqualityBasedOnNode()
{
ensureEqualityFor( node( "x" ), node( "y" ) );
ensureEqualityFor( node( "x" ), node( "x" ) );
ensureEqualityFor( node( "1" ), node( "1" ) );
ensureEqualityFor( node( "2" ), node( "2" ) );
ensureEqualityFor( node( "?x" ), node( "?x" ) );
ensureEqualityFor( node( "?x" ), node( "?y" ) );
}
private void ensureEqualityFor( Node A, Node B )
{
if (A.equals( B )) assertEquals( create( A ), create( B ) );
else assertNotEquals( create( A ), create( B ) );
}
@Test public void ensureEqualLiteralsMatchWithoutBinding()
{
ensureBindingWorks( "a", "a", "" );
ensureBindingWorks( "_b", "_b", "" );
ensureBindingWorks( "17", "17", "" );
}
@Test public void ensureDifferentLiteralsDoNotMatch()
{
assertFalse( create( node( "a" ) ).bind( node( "b" ), empty() ) );
assertFalse( create( node( "a" ) ).bind( node( "17" ), empty() ) );
assertFalse( create( node( "1" ) ).bind( node( "'hello'" ), empty() ) );
assertFalse( create( node( "_b" ) ).bind( node( "x" ), empty() ) );
}
private Bindings<Node, Node> empty()
{ return nodeBindings( "" ); }
@Test public void ensureBindingWorks()
{
ensureBindingWorks( "a", "a", "" );
ensureBindingWorks( "17", "17", "" );
ensureBindingWorks( "'hello'", "'hello'", "" );
ensureBindingWorks( "?x", "a", "?x=a" );
ensureBindingWorks( "?x", "b", "?x=b" );
ensureBindingWorks( "?x", "1", "?x=1" );
ensureBindingWorks( "?y", "'two'", "?y='two'" );
ensureBindingWorks( "f(x)", "f(x)", "" );
ensureBindingWorks( "f(?x)", "f(x)", "?x=x");
ensureBindingWorks( "f(?x ?y)", "f(x 17)", "?x=x,?y=17");
ensureBindingWorks( "f(?x ?x)", "f(1 1)", "?x=1");
}
@Test public void ensureBindingFails()
{
ensureBindingFails( "f(x)", "a" );
ensureBindingFails( "f(x)", "f(y)" );
ensureBindingFails( "f(x)", "g(x)" );
ensureBindingFails( "f(x)", "f(x x)" );
ensureBindingFails( "f(a ?x)", "f(b x)" );
ensureBindingFails( "f(?x a)", "f(x b)" );
}
private void ensureBindingFails( String term, String node )
{
String failMessage = "term " + term + " should not bind " + node + ".";
Bindings<Node, Node> b = new Bindings<Node, Node>();
boolean didBind = create( fnode( term ) ).bind( fnode( node ), b );
// assertEquals( failMessage, nodeBindings( "" ), b );
assertFalse( failMessage, didBind );
}
private void ensureBindingWorks( String term, String node, String expected )
{
Bindings<Node, Node> b = new Bindings<Node, Node>();
assertTrue( "term should bind node", create( fnode( term ) ).bind( fnode( node ), b ) );
assertEquals( nodeBindings( expected ), b );
}
private static Node fnode( String term )
{
return term.matches( "[A-Za-z]+\\(.*" ) ? functor( term ) : node( term );
}
@Test public void ensureLiteralNodesAreNotReplaced()
{
ensureLiteralNodesAreNotReplaced( node( "17" ) );
ensureLiteralNodesAreNotReplaced( node( "'hello'" ) );
ensureLiteralNodesAreNotReplaced( node( "eh:/thingy" ) );
ensureLiteralNodesAreNotReplaced( node( "'something'xsd:string" ) );
}
@Test public void ensureVariableNodesAreReplaced()
{
ensureVariableNodesAreReplaced( "?x", "?x=42", "42" );
ensureVariableNodesAreReplaced( "?x", "?x=eh:/it", "eh:/it" );
ensureVariableNodesAreReplaced( "?x", "?y=42", "?x" );
ensureVariableNodesAreReplaced( "f(?x)", "?x=19", "f(19)" );
}
private void ensureVariableNodesAreReplaced( String var, String binding, String expected )
{
String [] bs = binding.split( "=" );
Bindings<Node, Node> b = binding( fnode( bs[0] ), fnode( bs[1] ) );
Node e = expected == null ? null : fnode( expected );
assertEquals( e, create( fnode( var ) ).subst( null, b ) );
}
private Bindings<Node, Node> binding( Node bound, Node value )
{
return new Bindings<Node, Node>().set( bound, value );
}
private void ensureLiteralNodesAreNotReplaced( Node node )
{
Bindings<Node, Node> b = new Bindings<Node, Node>().set( node( "?x" ), node( "42" ) );
assertSame( node, NodeTerm.createConstant( node ).subst( null, b ) );
}
}