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

📄 abstracttestquery1.java

📁 Jena推理机
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	protected void loadGraph ( Graph g ) {
        Reifier r = g.getReifier();
		for (int i = 0; i < statementList.length; i++) {
			StringTokenizer st = new StringTokenizer( statementList[i] );
			String k = st.nextToken();
			if ( k.charAt(0) == 'N' )
                r.reifyAs( makeResource( k ), nextTriple( st.nextToken(), st ) );
			else
                g.add( nextTriple( k, st ) );
			stmtCnt++;
		}
	}
    
    protected Triple nextTriple( String k, StringTokenizer st )
        { Node s = makeResource( k );
        Node p = makeResource( st.nextToken() );
        Node o = makeObject( st.nextToken() );
        return Triple.create( s, p, o ); }

    final Node V1 = node( "?v1" );
    final Node V2 = node( "?v2" );
    final Node V3 = node( "?v3" );
    final Node V4 = node( "?v4" );
    final Node V5 = node( "?v5" );

    final Node Ptitle = makeResource("Ptitle");
    final Node Psex = makeResource("Psex");
    final Node Pname = makeResource("Pname");
    final Node Pmgr = makeResource("Pmgr");
    final Node Pcard = makeResource("Pcard");
    final Node Pcardmax = makeResource("Pcardmax");
    final Node Prange = makeResource("Prange");
    final Node Pdept = makeResource("Pdept");
    final Node S1 = makeResource("S1");
    
        // object constants
    final Node Ogrunt = makeObject("Ogrunt");
    final Node Ofemale = makeObject("Ofemale");
    final Node Omale = makeObject("Omale");
    final Node Obigboss = makeObject("Obigboss");
    final Node Oboss = makeObject("Oboss");
    final Node Oshane = makeObject("Oshane");
    final Node Oliteral = makeObject("Oliteral");
    final Node Oresource = makeObject("Oresource");
    final Node Oapp = makeObject("Oapp");
    final Node Ogenesis = makeObject("Ogenesis");

    final Node O1 = makeObject("O1");
             
    public void test0()
        {
        Query query = new Query();
        query.addMatch( V1, Ptitle, Ogrunt );
        query.addMatch( V1, Psex, Ofemale );
        query.addMatch( V1, Pname, V3 );        
        checkCount( 1, standard(), query, new Node[] {V1,V3} );
        }
        
    /**    
        Q1: get names of managers of female grunts;  this has a joining variable.
    */
    public void test1()
        {
        Query query = new Query();
        query.addMatch( V1, Ptitle, Ogrunt );
        query.addMatch( V1, Psex, Ofemale );
        query.addMatch( V1, Pmgr, V2 );
        query.addMatch( V2, Pname, V3 );    
        checkCount( 1, standard(), query, new Node[] {V1,V3} );
        }
        
    /**
        Q2: get names of female grunts with female managers
    */
    public void test2()
        {
        Query query = new Query();
        query.addMatch( V1, Ptitle, Ogrunt );
        query.addMatch( V1, Psex, Ofemale );
        query.addMatch( V1, Pmgr, V2 );
        query.addMatch( V2, Psex, Ofemale );
        query.addMatch( V1, Pname, V3 );     
        checkCount( 0, standard(), query, new Node[] {V1,V3} ); 
        }
        
    /**
        Q3.0: get all properties of the bigboss
    */
    public void test3a()
        {
        Query query = new Query();
        query.addMatch( V1, Ptitle, Obigboss );
        query.addMatch( V1, Pmgr, Oshane );
        query.addMatch( V1, V2, V3 );
        checkCount( 4, standard(), query, new Node[] {V1,V2,V3} );
        }
        
    /**
        Q3: get all properties of female grunts with male managers
        this has a predicate variable. for standard reification, it
        requires a multi-stage query. for convenient, minimal, it can
        be done as a single stage (since reification is not queried). 
    */
    public void test3b()
        {
        Query query = new Query();
        query.addMatch( V1, Ptitle, Ogrunt );
        query.addMatch( V1, Psex, Ofemale );
        query.addMatch( V1, Pmgr, V2 );
        query.addMatch( V2, Psex, Omale );
        query.addMatch( V1, V3, V4 );   
        checkCount( 6, standard(), query, new Node[] {V1,V3,V4} );
        }   
         
    /**
        Q4: get all single-valued, required, literal properties of the bigboss
        similar to Q3 in terms of stages.
    */
    public void test4()
        {
        Query query = new Query();
        query.addMatch( V1, Ptitle, Obigboss );
        query.addMatch( V1, Pmgr, Oshane );
        query.addMatch( V2, Pcard, O1 );
        query.addMatch( V2, Prange, Oliteral );
        query.addMatch( V1, V2, V3 );
        checkCount( 2, standard(), query, new Node[] {V2,V3} );
        }
        
    /**
        Q5: list the name and gender of martin's boss, where the pmgr property
        is determined by a query).
        similar to Q3 in terms of stages.     
    */
    public void test5()
        {
        Query query = new Query();
        query.addMatch( V1, Pcardmax, O1 );        // get the mgr property
        query.addMatch( V1, Prange, Oresource );
        query.addMatch( S1, V1, V2 );         // get mm's mgr
        query.addMatch( V2, Pname, V3 );
        query.addMatch( V2, Psex, V4 );     
        checkCount( 1, standard(), query, new Node[] {V2,V3,V4} );      
        }
        
    /**
        Q6: list the reified subjects, predicates and objects.
        should return nothing for minimal, convenient reification.
    */
    public void test6()
        {
        Query query = new Query();
        query.addMatch( V1, RDF.Nodes.subject, V2 ); 
        query.addMatch( V1, RDF.Nodes.predicate, V3 );
        query.addMatch( V1, RDF.Nodes.object, V4 );
    /* */
        checkCount( 27, standard(), query, new Node[] {V2,V3,V4} );
        checkCount( 0, convenient(), query, new Node[] {V2,V3,V4} );
        }
        
    /**
        Q7: list the reified predicates about the bigboss.
        should return nothing for minimal, convenient reification.
    */
    public void test7()
        {
        Query query = new Query();
        query.addMatch( V1, RDF.Nodes.subject, V2 ); 
        query.addMatch( V1, RDF.Nodes.predicate, Ptitle );
        query.addMatch( V1, RDF.Nodes.object, Obigboss );
        query.addMatch( V3, RDF.Nodes.subject, V2 ); 
        query.addMatch( V3, RDF.Nodes.predicate, V4 ); 
    /* */
        checkCount( 4, standard(), query, new Node[] {V2,V3} );
        }
        
    /**
        Q8: list the reification quads for the bigboss.
        should return nothing for minimal, convenient reification.   
    */
    public void test8()
        {
        Query query = new Query();
        query.addMatch( V1, RDF.Nodes.subject, V2 ); 
        query.addMatch( V1, RDF.Nodes.predicate, Ptitle );
        query.addMatch( V1, RDF.Nodes.object, Obigboss );
        query.addMatch( V3, RDF.Nodes.subject, V2 ); 
        query.addMatch( V3, V4, V5 ); // V4 and V5 serve duty as ANY
        checkCount( 16, standard(), query, new Node[] {V3} );
        }
        
    /**
        Check that the number of results obtained from the query over the graph is
        that expected.
        
     	@param expected the number of results expected from the query
     	@param g the graph to run the query over
     	@param q the query to apply to the graph
     	@param results the results-variable array
     */
    private void checkCount( int expected, Graph g, Query q, Node [] results ) 
       {
       BindingQueryPlan plan = g.queryHandler().prepareBindings( q, results );
       ExtendedIterator it = plan.executeBindings();
       assertEquals( "number of reified statements", expected, queryResultCount( it ) ); 
       it.close();
	   }
    
    /**
        Answer the number of elements in the iterator; each such element should be
        a List (and we make sure size() works on it, don't know why).
        
     	@param it the iterator to run down
     	@return the number of elements in that iterator
     */
    protected int queryResultCount( ExtendedIterator it ) {
        int n = 0;
        while (it.hasNext()) {
            n++;
            ((List) it.next()).size();  // hedgehog asks, do we need to check this works?
        }
        return n;
    }

    }


/*
    (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007 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.
*/

⌨️ 快捷键说明

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