/*
(c) Copyright 2009 Hewlett-Packard Development Company, LP
[See end of file]
$Id$
*/
package com.hp.jena.ymris.yast.test;
import static org.junit.Assert.*;
import java.util.*;
import org.junit.Test;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.test.NodeCreateUtils;
import com.hp.hpl.jena.reasoner.rulesys.Functor;
import com.hp.hpl.jena.reasoner.rulesys.Functor.FunctorDatatype;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.jena.ymris.deploy.local.*;
import com.hp.jena.ymris.yast.*;
import com.hp.jena.ymris.yast.expr.HowTo;
import static com.hp.hpl.jena.graph.test.GraphTestBase.node;
import static com.hp.hpl.jena.graph.test.NodeCreateUtils.create;
import static com.hp.jena.ymris.util.CollectionUtils.list;
public class TestSubst
{
protected static final VariableOffsets noOffsets = null;
protected static final Node [] noBindings = null;
private static final TranslationContext noContext = null;
protected final HowTo howTo = null;
@Test public void testPlainLiteralSubst()
{
YLiteral yl = new YLiteral( "goodbye" );
Subst s = yl.substBy( noContext );
assertEquals( node( "'goodbye'" ), s.subst( noBindings ) );
}
@Test public void testLanguagedLiteralSubst()
{
YLiteral yl = new YLiteral( "hello", "en-uk", null );
Subst s = yl.substBy( noContext );
assertEquals( node( "'hello'en-uk" ), s.subst( noBindings ) );
}
@Test public void testURISubst()
{
YURI u = new YURI( "eh:/babylon" );
Subst s = u.substBy( noContext );
assertEquals( node( "babylon" ), s.subst( noBindings ) );
}
@Test public void testVarSubst()
{ // Ikky.
VariableOffsets vo = new VariableOffsets();
int xOffset = vo.offsetFor( "x" ), yOffset = vo.offsetFor( "y" );
YVar xVar = new YVar( "x" ), yVar = new YVar( "y" );
TranslationContext tc = new TranslationContext( vo );
Subst xSubst = xVar.substBy( tc ), ySubst = yVar.substBy( tc );
Node xValue = create( "xValue" ), yValue = create( "yValue" );
Node [] bindings = new Node[] { null, xValue, yValue };
assertEquals( xValue, xSubst.subst( bindings ) );
assertEquals( yValue, ySubst.subst( bindings ) );
}
@Test public void testNilSubst()
{
Subst s = YLiteral.NIL.substBy( noContext );
assertEquals( RDF.Nodes.nil, s.subst( noBindings ) );
}
@Test public void testTrueSubst()
{
YLiteral b = YLiteral.TRUE;
Node matchingLiteral = NodeCreateUtils.create( "'true'xsd:boolean" );
Subst s = b.substBy( noContext );
assertEquals( matchingLiteral, s.subst( noBindings ) );
}
@Test public void testFalseSubst()
{
YLiteral b = YLiteral.FALSE;
Node matchingLiteral = NodeCreateUtils.create( "'false'xsd:boolean" );
Subst s = b.substBy( noContext );
assertEquals( matchingLiteral, s.subst( noBindings ) );
}
@Test public void testEmptyFunctorMatching()
{
YFunctor yf = new YFunctor( "f", new ArrayList<YNode>() );
Subst s = yf.substBy( noContext );
assertEquals( makeFunctor( "f" ), s.subst( noBindings ) );
}
@Test public void testFixedFunctorSubst()
{
testFixedFunctorSubst( new YURI( "eh:/x" ) );
testFixedFunctorSubst( YLiteral.NIL );
testFixedFunctorSubst( YLiteral.TRUE );
testFixedFunctorSubst( YLiteral.FALSE );
}
private void testFixedFunctorSubst( YNode fixed )
{
YFunctor yf = new YFunctor( "f", list( fixed ) );
Subst s = yf.substBy( noContext );
assertEquals( makeFunctor( "f", fixed.asNode() ), s.subst( noBindings ) );
}
@Test public void testVariableFunctorMatching()
{
YVar var = new YVar( "v" );
YFunctor yf = new YFunctor( "f", list( var ) );
VariableOffsets vo = new VariableOffsets();
Node [] bindings = new Node[] {null, node( "x" )};
Subst s = yf.substBy( new TranslationContext( vo ) );
assertEquals( makeFunctor( "f", node( "x" ) ), s.subst( bindings ) );
}
private Node makeFunctor( String f, Node... args )
{
Functor func = new Functor( f, Arrays.asList( args ) );
return Node.createUncachedLiteral( func, "", FunctorDatatype.theFunctorDatatype );
}
}
/*
(c) Copyright 2009 Hewlett-Packard Development Company, LP
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/