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

📄 testlist.java

📁 Jena推理机
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        public ApplyTest() {super("ApplyTest");}
        
        public void runTest() {
            Model m = ModelFactory.createDefaultModel();
            m.read( "file:testing/ontology/list5.rdf" );
           
            RDFList root = getListRoot( m );
            
            class MyApply implements RDFList.ApplyFn {
               String collect = "";
               public void apply( RDFNode n ) {
                   collect = collect + ((Resource) n).getLocalName();  
               } 
            }
            
            MyApply f = new MyApply();
            root.apply( f );
            
            assertEquals( "Result of apply should be concatentation of local names", "abcde", f.collect );
       }
    }
    
    
    protected static class ReduceTest extends ListTest {
        public ReduceTest() {super("ReduceTest");}
        
        public void runTest() {
            Model m = ModelFactory.createDefaultModel();
            m.read( "file:testing/ontology/list5.rdf" );
           
            RDFList root = getListRoot( m );
            
            RDFList.ReduceFn f = new RDFList.ReduceFn() {
               public Object reduce( RDFNode n, Object acc ) {
                   return ((String) acc) + ((Resource) n).getLocalName();  
               } 
            };
            
            assertEquals( "Result of reduce should be concatentation of local names", "abcde", root.reduce( f, "" ) );
       }
    }
    
    protected static class Map1Test extends ListTest {
        public Map1Test() {super("Map1Test");}
        
        public void runTest() {
            Model m = ModelFactory.createDefaultModel();
            m.read( "file:testing/ontology/list5.rdf" );
           
            RDFList root = getListRoot( m );
            iteratorTest( root.mapWith( new Map1() {public Object map1(Object x){return ((Resource) x).getLocalName();} } ), 
                          new Object[] {"a","b","c","d","e"} );            
        }
    }
    
    protected static class RemoveTest extends ListTest {
        public RemoveTest() {super( "RemoveTest" );}
        
        public void runTest() {
            Model m = ModelFactory.createDefaultModel();
            
            Resource nil = m.getResource( RDF.nil.getURI() );
            RDFList list0 = (RDFList) nil.as( RDFList.class );
            RDFList list1 = (RDFList) nil.as( RDFList.class );
            
            Resource r0 = m.createResource( NS + "x" );
            Resource r1 = m.createResource( NS + "y" );
            Resource r2 = m.createResource( NS + "z" );
            
            for (int i = 0;  i < 10;  i++) {
                list0 = list0.cons( r0 );
                list1 = list1.cons( r1 );
            }
            
            // delete the elements of list0 one at a time
            while (!list0.isEmpty()) {
                list0 = list0.removeHead();
                checkValid( "removeTest0", list0, true );
            }
            
            
            // delete all of list1 in one go
            list1.removeList();
            
            // model should now be empty
            assertEquals( "Model should be empty after deleting two lists", 0, m.size() );
            
            // selective remove
            RDFList list2 = ((RDFList) nil.as( RDFList.class ))
                            .cons( r2 )
                            .cons( r1 )
                            .cons( r0 );
           
            assertTrue( "list should contain x ", list2.contains( r0 ));
            assertTrue( "list should contain y ", list2.contains( r1 ));
            assertTrue( "list should contain z ", list2.contains( r2 ));
            
            list2 = list2.remove( r1 );
            assertTrue( "list should contain x ", list2.contains( r0 ));
            assertTrue( "list should contain y ", !list2.contains( r1 ));
            assertTrue( "list should contain z ", list2.contains( r2 ));
            
            list2 = list2.remove( r0 );
            assertTrue( "list should contain x ", !list2.contains( r0 ));
            assertTrue( "list should contain y ", !list2.contains( r1 ));
            assertTrue( "list should contain z ", list2.contains( r2 ));
            
            list2 = list2.remove( r2 );
            assertTrue( "list should contain x ", !list2.contains( r0 ));
            assertTrue( "list should contain y ", !list2.contains( r1 ));
            assertTrue( "list should contain z ", !list2.contains( r2 ));
            assertTrue( "list should be empty", list2.isEmpty() );
        }
    }
    
    
    protected static class ListEqualsTest extends ListTest {
        public ListEqualsTest() {super("ListEqualsTest");}
        
        public void runTest() {
            Model m = ModelFactory.createDefaultModel();
           
            Resource nil = m.getResource( RDF.nil.getURI() );
            RDFList nilList = (RDFList) nil.as( RDFList.class );
            
            // create a list of foos
            Resource[] r0 = new Resource[] {
                m.createResource( NS + "a" ),   // canonical
                m.createResource( NS + "b" ),
                m.createResource( NS + "c" ),
                m.createResource( NS + "d" ),
                m.createResource( NS + "e" )
            };
            Resource[] r1 = new Resource[] {
                m.createResource( NS + "a" ),   // same
                m.createResource( NS + "b" ),
                m.createResource( NS + "c" ),
                m.createResource( NS + "d" ),
                m.createResource( NS + "e" )
            };
            Resource[] r2 = new Resource[] {
                m.createResource( NS + "a" ),   // one shorter
                m.createResource( NS + "b" ),
                m.createResource( NS + "c" ),
                m.createResource( NS + "d" )
            };
            Resource[] r3 = new Resource[] {
                m.createResource( NS + "a" ),   // elements swapped
                m.createResource( NS + "b" ),
                m.createResource( NS + "d" ),
                m.createResource( NS + "c" ),
                m.createResource( NS + "e" )
            };
            Resource[] r4 = new Resource[] {
                m.createResource( NS + "a" ),   // different name
                m.createResource( NS + "b" ),
                m.createResource( NS + "c" ),
                m.createResource( NS + "D" ),
                m.createResource( NS + "e" )
            };
            
            Object[][] testSpec = new Object[][] {
                {r0, r1, Boolean.TRUE},
                {r0, r2, Boolean.FALSE},
                {r0, r3, Boolean.FALSE},
                {r0, r4, Boolean.FALSE},
                {r1, r2, Boolean.FALSE},
                {r1, r3, Boolean.FALSE},
                {r1, r4, Boolean.FALSE},
                {r2, r3, Boolean.FALSE},
                {r2, r4, Boolean.FALSE},
            };
            
            for (int i = 0;  i < testSpec.length;  i++) {
                RDFList l0 = nilList.append( Arrays.asList( (Object[]) testSpec[i][0] ).iterator() );
                RDFList l1 = nilList.append( Arrays.asList( (Object[]) testSpec[i][1] ).iterator() );
                boolean expected = ((Boolean) testSpec[i][2]).booleanValue();
                
                assertEquals( "sameListAs testSpec[" + i + "] incorrect", expected, l0.sameListAs( l1 ) );
                assertEquals( "sameListAs testSpec[" + i + "] (swapped) incorrect", expected, l1.sameListAs( l0 ) );
            }
       }
    }
    
    protected static class ListSubclassTest extends ListTest {
        public ListSubclassTest() {
            super( "ListSubClassTest");
        }
        
        public void runTest() {
            Model m = ModelFactory.createDefaultModel();
            
            String NS = "http://example.org/test#";
            Resource a = m.createResource( NS + "a" );
            Resource b = m.createResource( NS + "b" );
            
            Resource cell0 = m.createResource();
            Resource cell1 = m.createResource();
            cell0.addProperty( RDF.first, a );
            cell0.addProperty( RDF.rest, cell1 );
            cell1.addProperty( RDF.first, b );
            cell1.addProperty( RDF.rest, RDF.nil );
            
            UserList ul = new UserListImpl( cell0.asNode(), (EnhGraph) m );
            
            assertEquals( "User list length ", 2, ul.size() );
            assertEquals( "head of user list ", a, ul.getHead() );
            
            RDFList l = (RDFList) ul.as( RDFList.class );
            assertNotNull( "RDFList facet of user-defined list subclass", l );
        }
    }
    
    /** A simple extension to RDFList to test user-subclassing of RDFList */
    protected static interface UserList extends RDFList {
    }
    
    /** Impl of a simple extension to RDFList to test user-subclassing of RDFList */
    protected static class UserListImpl extends RDFListImpl implements UserList {
        public UserListImpl( Node n, EnhGraph g ) {
            super( n, g );
        }
    }

    
    protected static class UserDefinedListTest extends ListTest {
        public UserDefinedListTest() {
            super( "UserDefinedListTest");
        }
        
        public void runTest() {
            BuiltinPersonalities.model.add( UserDefList.class, UserDefListImpl.factory );
            
            Model m = ModelFactory.createDefaultModel();
            
            String NS = "http://example.org/test#";
            Resource a = m.createResource( NS + "a" );
            Resource b = m.createResource( NS + "b" );
            
            Resource empty = m.createResource( UserDefListImpl.NIL.getURI() );
            UserDefList ul = (UserDefList) empty.as( UserDefList.class );
            assertNotNull( "UserList facet of empty list", ul );
            
            UserDefList ul0 = (UserDefList) ul.cons( b );
            ul0 = (UserDefList) ul0.cons( a );
            assertEquals( "should be length 2", 2, ul0.size() );
            assertTrue( "first statement", m.contains( ul0, UserDefListImpl.FIRST, a ) );
        }
    }
    
    protected static interface UserDefList extends RDFList {}
    
    protected static class UserDefListImpl extends RDFListImpl implements UserDefList {
        public static final String NS = "http://example.org/testlist#";
        public static final Property FIRST = ResourceFactory.createProperty( NS+"first" );
        public static final Property REST = ResourceFactory.createProperty( NS+"rest" );
        public static final Resource NIL = ResourceFactory.createResource( NS+"nil" );
        public static final Resource LIST = ResourceFactory.createResource( NS+"List" );
        
        /**
         * A factory for generating UserDefList facets from nodes in enhanced graphs.
         */
        public static Implementation factory = new Implementation() {
            public EnhNode wrap( Node n, EnhGraph eg ) { 
                if (canWrap( n, eg )) {
                    UserDefListImpl impl = new UserDefListImpl( n, eg );
                    
                    Model m = impl.getModel();
                    impl.m_listFirst = (Property) FIRST.inModel( m );
                    impl.m_listRest = (Property) REST.inModel( m );
                    impl.m_listNil = (Resource) NIL.inModel( m );
                    impl.m_listType = (Resource) LIST.inModel( m );
                    
                    return impl;
                }
                else {
                    throw new JenaException( "Cannot convert node " + n + " to UserDefList");
                } 
            }
                
            public boolean canWrap( Node node, EnhGraph eg ) {
                Graph g = eg.asGraph();
                
                return  node.equals( NIL.asNode() ) || 
                        g.contains( node, FIRST.asNode(), Node.ANY ) ||
                        g.contains( node, REST.asNode(), Node.ANY ) ||
                        g.contains( node, RDF.type.asNode(), LIST.asNode() );
            }
        };

        /** This method returns the Java class object that defines which abstraction facet is presented */
        public Class listAbstractionClass() { 
            return UserDefList.class; 
        }

        public UserDefListImpl( Node n, EnhGraph g ) {
            super( n, g );
        }
        
    }

}


/*
    (c) Copyright 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 + -