Package com.hp.jena.ymris.yast.test

Source Code of com.hp.jena.ymris.yast.test.TestMatching

/* 
  (c) Copyright 2009 Hewlett-Packard Development Company, LP
  [See end of file]
  $Id$
*/
package com.hp.jena.ymris.yast.test;

import org.junit.Test;
import static org.junit.Assert.*;

import java.util.*;

import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.test.*;
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 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 TestMatching
    {
    protected static final TranslationContext noContext = null;
    protected static final Node [] noBindings = null;
   
    @Test public void testPlainLiteralMatching()
        {
        YLiteral yl = new YLiteral( "hello, hello" );
        Match m = yl.matchBy( noContext );
        assertTrue( m.match( Node.createLiteral( "hello, hello" ), noBindings ) );
        assertFalse( m.match( Node.createLiteral( "hello, goodbye" ), noBindings ) );
        }
   
    @Test public void testLanguagedLiteralMatching()
        {
        YLiteral yl = new YLiteral( "hello", "en-uk", null );
        Match m = yl.matchBy( noContext );
        assertTrue( m.match( Node.createLiteral( "hello", "en-uk", null ), noBindings ) );
        assertFalse( m.match( Node.createLiteral( "hello", "fr", null ), noBindings ) );
        }
   
    @Test public void testURIMatching()
        {
        YURI u = new YURI( "eh:/babylon" );
        Match m = u.matchBy( noContext );
        assertTrue( m.match( Node.createURI( "eh:/babylon" ), noBindings ) );
        assertFalse( m.match( Node.createURI( "eh:/rupert" ), noBindings ) );
        }
   
    @Test public void testVarMatching()
        { // 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 );
        Match xMatch = xVar.matchBy( tc ), yMatch = yVar.matchBy( tc );
        Node xValue = create( "xValue" ), yValue = create( "yValue" );
        Node [] bindings = new Node[] { null, xValue, null };
        assertTrue( xMatch.match( xValue, bindings ) );
        assertFalse( xMatch.match( yValue, bindings ) );
        assertNull( bindings[yOffset] );
        assertTrue( yMatch.match( yValue, bindings ) );
        assertEquals( yValue, bindings[yOffset] );
        }
   
    @Test public void testNilMatching()
        {
        YNode nil = YLiteral.NIL;
        Match m = nil.matchBy( noContext );
        assertTrue( m.match( RDF.Nodes.nil, noBindings ) );
        assertFalse( m.match( NodeCreateUtils.create( "notNil" ), noBindings ) );
        }
   
    @Test public void testTrueMatching()
        {
        YLiteral b = YLiteral.TRUE;
        Node matchingLiteral = NodeCreateUtils.create( "'true'xsd:boolean" );
        Node wrongLiteral = NodeCreateUtils.create( "'true'" );
        Match m = b.matchBy( noContext );
        assertTrue( m.match( matchingLiteral, noBindings ) );
        assertFalse( m.match( wrongLiteral, noBindings ) );
        }
   
    @Test public void testFalseMatching()
        {
        YLiteral b = YLiteral.FALSE;
        Node matchingLiteral = NodeCreateUtils.create( "'false'xsd:boolean" );
        Node wrongLiteral = NodeCreateUtils.create( "'false'" );
        Match m = b.matchBy( noContext );
        assertTrue( m.match( matchingLiteral, noBindings ) );
        assertFalse( m.match( wrongLiteral, noBindings ) );
        }
   
    @Test public void testEmptyFunctorMatching()
        {
        YFunctor yf = new YFunctor( "f", new ArrayList<YNode>() );
        Match m = yf.matchBy( noContext );
        assertTrue( m.match( makeFunctor( "f" ), noBindings ) );
        assertFalse( m.match( makeFunctor( "g" ), noBindings ) );
        assertFalse( m.match( create( "me" ), noBindings ) );
        }
   
    @Test public void testFixedFunctorMatching()
        {
        testFixedFunctorMatching( new YURI( "eh:/x" ) );
        testFixedFunctorMatching( YLiteral.NIL );
        testFixedFunctorMatching( YLiteral.TRUE );
        testFixedFunctorMatching( YLiteral.FALSE );
        }

    private void testFixedFunctorMatching( YNode fixed )
        {
        YFunctor yf = new YFunctor( "f", list( fixed ) );
        Match m = yf.matchBy( noContext );
        assertTrue( m.match( makeFunctor( "f", fixed.asNode() ), noBindings ) );
        assertFalse( m.match( makeFunctor( "g", node( "eh:/x" ) ), noBindings ) );
        assertFalse( m.match( makeFunctor( "f", node( "eh:/y" ) ), noBindings ) );
        assertFalse( m.match( makeFunctor( "f", node( "17" ) ), noBindings ) );
        assertFalse( m.match( create( "me" ), 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[2];
        Match m = yf.matchBy( new TranslationContext( vo ) );
        assertTrue( m.match( makeFunctor( "f", node( "x" ) ), bindings ) );
        assertEquals( node( "x" ), bindings[1] );
        }

    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.
*/ 
TOP

Related Classes of com.hp.jena.ymris.yast.test.TestMatching

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.