📄 abstracttestreifiedstatements.java
字号:
/*
(c) Copyright 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
[See end of file]
$Id: AbstractTestReifiedStatements.java,v 1.17 2007/01/02 11:48:26 andy_seaborne Exp $
*/
package com.hp.hpl.jena.rdf.model.test;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.util.CollectionFactory;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.graph.test.*;
import java.util.*;
/**
@author kers
*/
public abstract class AbstractTestReifiedStatements extends ModelTestBase
{
public AbstractTestReifiedStatements( String name )
{
super( name );
}
public abstract Model getModel();
private Model model;
private Resource S;
private Property P;
private RDFNode O;
private Statement SPO;
private Statement SPO2;
private static final String aURI = "jena:test/reifying#someURI";
private static final String anotherURI = "jena:test/reifying#anotherURI";
private static final String anchor = "jena:test/Reifying#";
public void setUp()
{
model = getModel();
Resource S2 = model.createResource( anchor + "subject2" );
S = model.createResource( anchor + "subject" );
P = model.createProperty( anchor + "predicate" );
O = model.createLiteral( anchor + "object" );
SPO = model.createStatement( S, P, O );
SPO2 = model.createStatement( S2, P, O );
}
/**
the simplest case: if we assert all the components of a reification quad,
we can get a ReifiedStatement that represents the reified statement.
*/
public void testBasicReification()
{
if (model.getReificationStyle() != ModelFactory.Minimal)
{ Resource R = model.createResource( aURI );
model.add( R, RDF.type, RDF.Statement );
model.add( R, RDF.subject, S );
model.add( R, RDF.predicate, P );
model.add( R, RDF.object, O );
RDFNode rs = R.as( ReifiedStatement.class );
assertEquals( "can recover statement", SPO, ((ReifiedStatement) rs).getStatement() ); }
}
/**
check that, from a model with any combination of the statements given,
we can convert R into a ReifiedStatement iff the four components of the
quad are in the model.
*/
public void testReificationCombinations()
{
Resource RR = model.createResource( aURI ), SS = model.createResource( anotherURI );
Property PP = (Property) RR.as( Property.class );
Object [][] statements =
{
{ model.createStatement( RR, RDF.type, RDF.Statement ), new Integer(1) },
{ model.createStatement( RR, RDF.subject, SS ), new Integer(2) },
{ model.createStatement( RR, RDF.predicate, PP ), new Integer(4) },
{ model.createStatement( RR, RDF.object, O ), new Integer(8) },
{ model.createStatement( SS, PP, O ), new Integer(16) },
{ model.createStatement( RR, PP, O ), new Integer(32) },
{ model.createStatement( SS, RDF.subject, SS ), new Integer(64) },
{ model.createStatement( SS, RDF.predicate, PP ), new Integer(128) },
{ model.createStatement( SS, RDF.object, O ), new Integer(256) },
{ model.createStatement( SS, RDF.type, RDF.Statement ), new Integer(512) }
};
if (model.getReificationStyle() != ModelFactory.Minimal)
testCombinations( model, RR, 0, statements, statements.length );
}
/**
walk down the set of statements (represented as an array), recursing with and
without each statement being present. The mask bits record those statements
that are in the model. At the bottom of the recursion (n == 0), check that R
can be reified exactly when all four quad components are present; the other
statements don't matter.
*/
private void testCombinations( Model m, Resource R, int mask, Object [][] statements, int n )
{
if (n == 0)
{
try
{
// System.err.println( "| hello. mask = " + mask );
ReifiedStatement rs = (ReifiedStatement) R.as( ReifiedStatement.class );
// System.err.println( "+ we constructed " + rs );
assertTrue( "should not reify: not all components present [" + mask + "]: " + rs, (mask & 15) == 15 );
// System.err.println( "+ and we passed the assertion." );
}
catch (DoesNotReifyException e)
{ // System.err.println( "+ we exploded" );
assertFalse( "should reify: all components present", mask == 15 ); }
}
else
{
int i = n - 1;
Statement s = (Statement) statements[i][0];
int bits = ((Integer) statements[i][1]).intValue();
testCombinations( m, R, mask, statements, i );
m.add( s );
testCombinations( m, R, mask + bits, statements, i );
m.remove( s );
}
}
public void testThisWillBreak()
{
Resource R = model.createResource( aURI );
SPO.createReifiedStatement( aURI );
model.add( R, RDF.subject, R );
}
/**
"dirty" reifications - those with conflicting quadlets - should fail.
*/
public void testDirtyReification()
{
Resource R = model.createResource( aURI );
model.add( R, RDF.type, RDF.Statement );
model.add( R, RDF.subject, S );
model.add( R, RDF.subject, P );
testDoesNotReify( "boo", R );
}
public void testDoesNotReify( String title, Resource r )
{
try { r.as( ReifiedStatement.class ); fail( title + " (" + r + ")" ); }
catch (DoesNotReifyException e) { /* that's what we expect */ }
}
public void testConversion()
{
final String uri = "spoo:handle";
model.createReifiedStatement( uri, SPO );
ReifiedStatement rs2 = (ReifiedStatement) model.createResource( uri ).as( ReifiedStatement.class );
assertEquals( "recover statement", SPO, rs2.getStatement() );
}
public void testDoesNotReifyUnknown()
{
testDoesNotReify( "model should not reify rubbish", model.createResource( "spoo:rubbish" ) );
}
public void testQuintetOfQuadlets()
{
Resource rs = model.createResource();
rs.addProperty( RDF.type, RDF.Statement );
model.createResource().addProperty( RDF.value, rs );
rs.addProperty( RDF.subject, model.createResource() );
rs.addProperty( RDF.predicate, model.createProperty( "http://example.org/foo" ) );
rs.addProperty( RDF.object, model.createResource() );
rs.addProperty( RDF.object, model.createResource() );
StmtIterator it = model.listStatements();
while (it.hasNext())
{
Statement s = it.nextStatement();
assertFalse(s.getObject().equals(s.getSubject()));
}
}
public void testConstructionByURI()
{
ReifiedStatement rs = model.createReifiedStatement( "spoo:handle", SPO );
ReifiedStatement rs2 = SPO.createReifiedStatement( "spoo:gripper");
assertEquals( "recover statement (URI)", SPO, rs.getStatement() );
assertEquals( "recover URI", "spoo:handle", rs.getURI() );
assertEquals( "recover URI", "spoo:gripper", rs2.getURI() );
}
public void testStatementAndModel( String title, ReifiedStatement rs, Model m, Statement st )
{
assertEquals( title + ": recover statement", st, rs.getStatement() );
assertEquals( title + ": recover model", m, rs.getModel() );
}
public void testConstructionFromStatements()
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -