⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstracttestreifier.java

📁 Jena推理机
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
  (c) Copyright 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
  [See end of file]
  $Id: AbstractTestReifier.java,v 1.32 2007/01/02 11:50:06 andy_seaborne Exp $
*/

package com.hp.hpl.jena.graph.test;

import java.util.Collections;

import com.hp.hpl.jena.db.impl.DBReifier;
import com.hp.hpl.jena.graph.*;
import com.hp.hpl.jena.graph.impl.GraphBase;
import com.hp.hpl.jena.shared.*;
import com.hp.hpl.jena.util.CollectionFactory;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.vocabulary.RDF;

/**
    Abstract base class for reification tests. 
 	@author kers
*/
public abstract class AbstractTestReifier extends GraphTestBase
    {
    protected static final ReificationStyle Minimal = ReificationStyle.Minimal;
    protected static final ReificationStyle Standard = ReificationStyle.Standard;
    protected static final ReificationStyle Convenient = ReificationStyle.Convenient;

    protected static final Triple ALL = Triple.create( "?? ?? ??" );
    
    public AbstractTestReifier( String name )
        { super( name ); }
        
    public abstract Graph getGraph();
    
    public abstract Graph getGraph( ReificationStyle style );

    protected final Graph getGraphWith( String facts )
        {
        Graph result = getGraph();
        graphAdd( result, facts );
        return result;
        }
        
    /**
        Answer the empty graph if cond is false, otherwise the graph with the given facts.
    */
    protected final Graph graphWithUnless( boolean cond, String facts )
        { return graphWith( cond ? "" : facts ); }
          
    protected final Graph graphWithIf( boolean cond, String facts )
        { return graphWithUnless( !cond, facts ); }
            
    public void testStyle()
        {
        assertSame( Minimal, getGraph( Minimal ).getReifier().getStyle() );    
        assertSame( Standard, getGraph( Standard ).getReifier().getStyle() );    
        assertSame( Convenient, getGraph( Convenient ).getReifier().getStyle() );    
        }
        
    public void testEmptyReifiers()
        {
        assertFalse( getGraphWith( "x R y" ).getReifier().findExposed( ALL ).hasNext() );
        assertFalse( getGraphWith( "x R y; p S q" ).getReifier().findExposed( ALL ).hasNext() );
        }
        
    public void testSameReifier()
        {
        Graph G = getGraph();
        Reifier R1 = G.getReifier();
        G.add( triple( "x R y" ) );
        assertTrue( "same reifier", R1 == G.getReifier() );
        }
    
    public void testReifierClosed()
        {
        Graph g = getGraph();
        Reifier r = g.getReifier();
        g.close();
        }
        
    public void testParent()
        {
        Graph G = getGraph(), H = getGraph();
        assertTrue( "correct reifier (G)", G == G.getReifier().getParentGraph() );
        assertTrue( "correct reifier (H)", H == H.getReifier().getParentGraph() );
        }
        
    public void testIntercept()
        {
        Graph g = getGraph( Convenient );
        Reifier r = g.getReifier();
        Node S = node( "sub" ), O = node( "obj" );
        Node RS = node( "http://example.org/type" );
    /* */
        assertFalse( "reifier must not intercept quadlet", r.handledAdd( Triple.create( S, RDF.Nodes.type,  RS )  ) );
        assertFalse( "reifier must not intercept quadlet", r.handledAdd( Triple.create( S, S,  RDF.Nodes.subject )  ) );
        assertFalse( "reifier must not intercept quadlet", r.handledAdd( Triple.create( S, S,  RDF.Nodes.type )  ) );
    /* */
        assertTrue( "reifier must intercept quadlet", r.handledAdd( Triple.create( S, RDF.Nodes.predicate, O ) ) );
        assertTrue( "reifier must intercept quadlet", r.handledAdd( Triple.create( S, RDF.Nodes.type,  RDF.Nodes.Statement )  ) );
        }

    /**
        Check that the standard reifier will note, but not hide, reification quads.
    */
    public void testStandard()
        {
        Graph g = getGraph( Standard );
        assertFalse( g.getReifier().hasTriple( triple( "s p o" ) ) );
        g.add( Triple.create( "x rdf:subject s" ) );
        assertEquals( 1, g.size() );
        g.add( Triple.create( "x rdf:predicate p" ) );
        assertEquals( 2, g.size() );  
        g.add( Triple.create( "x rdf:object o" ) );
        assertEquals( 3, g.size() );            
        g.add( Triple.create( "x rdf:type rdf:Statement" ) );
        assertEquals( 4, g.size() );
        assertTrue( g.getReifier().hasTriple( triple( "s p o" ) ) );                      
        }
        
    /**
        Test that the Standard reifier will expose implicit quads arising from reifyAs().
    */
    public void testStandardExplode()
        {
        Graph g = getGraph( Standard );
        g.getReifier().reifyAs( node( "a" ), triple( "p Q r" ) );
        Graph r = Factory.createDefaultGraph( Minimal );
        graphAdd( r, "a rdf:type rdf:Statement; a rdf:subject p; a rdf:predicate Q; a rdf:object r" );
        assertEquals( 4, g.size() );
        assertIsomorphic( r, g );
        }
        
    public void testMinimalExplode()
        {
        Graph g = getGraph( Minimal );
        g.getReifier().reifyAs( node( "a" ), triple( "p Q r" ) );
        assertEquals( 0, g.size() );
        }
        
    public void testReificationTriplesConvenient()
        { testReificationTriples( Convenient ); }
        
    public void testReificationTriplesStandard()
        { testReificationTriples( Standard ); }
    
    public void testReificationQuadletsMinimal()
        { testReificationTriples( Minimal ); }
        
    /**
        test that a reifier with the given style sees [or not, if it's minimal] the reification quads
        that are inserted through its graph.
    */
    protected void testReificationTriples( ReificationStyle style )
        {
        Graph g = getGraph( style );
        Graph quadlets = getReificationTriples( g.getReifier() );
        String S1 = "SSS rdf:predicate PPP", S2 = "SSS rdf:subject SSS";
        g.add( triple( S1 ) );
        assertIsomorphic( graphWithUnless( style == Minimal, S1 ), quadlets );
        g.add( triple( S2 ) );
        assertIsomorphic( graphWithUnless( style == Minimal, S1 + "; " + S2 ), quadlets );
        assertEquals( "convenient hides quadlets", style == Convenient, g.size() == 0 );
        }
        
    /**
         Ensure that over-specifying a reification means that we don't get a triple
         back. Goodness knows why this test wasn't in right from the beginning.
    */
    public void testOverspecificationSuppressesReification()
        {
        Graph g = getGraph( Standard );
        Reifier r = g.getReifier();
        graphAdd( g, "x rdf:subject A; x rdf:predicate P; x rdf:object O; x rdf:type rdf:Statement" );
        assertEquals( triple( "A P O" ), r.getTriple( node( "x" ) ) );
        try 
            { graphAdd( g, "x rdf:subject BOOM" ); 
            assertEquals( null, r.getTriple( node( "x" ) ) ); }
        catch (AlreadyReifiedException e) 
            {
            if (r instanceof DBReifier) { /* System.err.println( "! Db reifier must fix over-specification problem" ); */ }
            else throw e;
            }
        }
    
    public void testReificationSubjectClash()
        {
        testReificationClash( "x rdf:subject SS" );
        }    
    
    public void testReificationPredicateClash()
        {
        testReificationClash( "x rdf:predicate PP" );
        }    
    
    public void testReificationObjectClash()
        {
        testReificationClash( "x rdf:object OO" );
        }
        
    /**
     * @param clashingStatement
     */
    protected void testReificationClash( String clashingStatement )
        {
        Graph g = getGraph( Standard );
        Triple SPO = Triple.create( "S P O" );
        g.getReifier().reifyAs( node( "x" ), SPO );
        assertTrue( g.getReifier().hasTriple( SPO ) );
        try
            {
            graphAdd( g,  clashingStatement );
            assertEquals( null, g.getReifier().getTriple( node( "x" ) ) );
            assertFalse( g.getReifier().hasTriple( SPO ) );
            }
        catch (AlreadyReifiedException e)
            {
            if (g.getReifier() instanceof DBReifier) { /* System.err.println( "! Db reifier must fix over-specification problem" ); */ }
            else throw e;
            }
        }

    public void testManifestQuadsStandard()
        { testManifestQuads( Standard ); }
        
    public void testManifestQuadsConvenient()
        { testManifestQuads( Convenient ); }
        
    public void testManifestQuadsMinimal()
        { testManifestQuads( Minimal ); }
        
    /**
        Test that reifying a triple explicitly has some effect on the graph only for Standard
        reifiers.
     */
    public void testManifestQuads( ReificationStyle style )
        {
        Graph g = getGraph( style );   
        Reifier r = g.getReifier();
        r.reifyAs( node( "A" ), triple( "S P O" ) );
        String reified = "A rdf:type rdf:Statement; A rdf:subject S; A rdf:predicate P; A rdf:object O";
        assertIsomorphic( graphWithIf( style == Standard, reified ), g );
        }
        
    public void testHiddenVsReificationMinimal()
        { testHiddenVsReification( Minimal ); }
        
    public void testHiddenVsStandard()
        { testHiddenVsReification( Standard ); }
        
    public void testHiddenVsReificationConvenient()
        { testHiddenVsReification( Convenient ); }
        
    public void testHiddenVsReification( ReificationStyle style )
        {
        Graph g = getGraph( style );
        Reifier r = g.getReifier();
        r.reifyAs( node( "A" ), triple( "S P O" ) );
        assertEquals( style == Standard, r.findEither( ALL, false ).hasNext() );    
        }
      
    public void testRetrieveTriplesByNode()
        {
        Graph G = getGraph();
        Reifier R = G.getReifier();
        Node N = Node.createAnon(), M = Node.createAnon();
        R.reifyAs( N, triple( "x R y" ) );
        assertEquals( "gets correct triple", triple( "x R y" ), R.getTriple( N ) );
        R.reifyAs( M, triple( "p S q" ) );
        assertDiffer( "the anon nodes must be distinct", N, M );
        assertEquals( "gets correct triple", triple( "p S q" ), R.getTriple( M ) );
    /* */
        assertTrue( "node is known bound", R.hasTriple( M ) );
        assertTrue( "node is known bound", R.hasTriple( N ) );
        assertFalse( "node is known unbound", R.hasTriple( Node.createURI( "any:thing" ) ) );
    /* */
//      Graph GR = R.getReifiedTriples();
//      assertTrue( "reified triples", getGraphWith( "x R y; p S q" ).isIsomorphicWith(GR) );
//      assertTrue( "untouched graph", getGraph().isIsomorphicWith(G) );
        }
        
    public void testRetrieveTriplesByTriple()
        {
        Graph G = getGraph();
        Reifier R = G.getReifier();
        Triple T = triple( "x R y" ), T2 = triple( "y R x" );
        Node N = node( "someNode" );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -