/*
(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 static org.junit.Assert.*;
import java.util.*;
import org.junit.Test;
import com.hp.jena.graph.*;
import com.hp.jena.rules.ast.EntityName;
import com.hp.jena.rules.ast.StringInLanguage;
import com.hp.jena.rules.retelike.impl.*;
import com.hp.jena.rules.retelike.impl.scratch.Entries;
import com.hp.jena.shell.test.utils.GraphCreateUtils;
import com.hp.jena.ymris.util.*;
import static com.hp.jena.shell.test.utils.NodeCreateUtils.*;
public class TestConclusions
{
@Test public void ensureGeneratesSingletonResults()
{
testGeneratesConclusionTriples( "?s p o", "?s=y", "y p o" );
testGeneratesConclusionTriples( "s ?p o", "?p=y", "s y o" );
testGeneratesConclusionTriples( "s p ?o", "?o=y", "s p y" );
}
@Test public void ensureGeneratesMultipleResults()
{
testGeneratesConclusionTriples( "?s p o; ?s q r", "?s=y", "y p o; y q r" );
testGeneratesConclusionTriples( "s ?p o; ?p q r", "?p=y", "s y o; y q r" );
testGeneratesConclusionTriples( "s p ?o; s ?o r", "?o=y", "s p y; s y r" );
}
private void testGeneratesConclusionTriples
( String conclusions, String bindings, String expected )
{
String [] kv = bindings.split( "=" );
String K = kv[0].trim(), V = kv[1].trim();
testGeneratesConclusionTriples( conclusions, K, V, expected );
}
private void testGeneratesConclusionTriples
( String conclusions, String boundVar, String boundValue, String expected )
{
EntityName name = new EntityName( "anon", new ArrayList<StringInLanguage>() );
Node X = node( boundVar ), Y = node( boundValue );
ConsumeToSet<Triple> t = new ConsumeToSet<Triple>();
Conclude x = new Conclude( null, name, tripleTerms( conclusions ), t );
x.consume( binding( X, Y ) );
assertEquals( GraphCreateUtils.tripleSet( expected ), new HashSet<Triple>( t.elements() ) );
}
private Set<TripleTerm> tripleTerms( String termString )
{
Set<TripleTerm> result = new HashSet<TripleTerm>();
for (String oneTerm: termString.split( " *; *" ))
result.add( TestTripleTerm.tripleTerm( oneTerm ) );
return result;
}
private Entries binding( Node x, Node y )
{ return new Entries().set( x, y ); }
}