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

📄 damltest.java

📁 jena2.5.4推理机系统的一种最基本实现 HP实验室出品
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        DAMLClass humanBody = (DAMLClass) m.getDAMLValue( ns + "HumanBody" ).as( DAMLClass.class );
        assertNotNull( "Class humanBody should not be null", humanBody );
        PropertyAccessor propUnion = humanBody.prop_unionOf();
        assertNotNull( "Property accessor should not be null", propUnion );
        assertEquals( "Should be two value in union", 1, propUnion.count() );
        DAMLCommon union = propUnion.getDAMLValue();
        assertNotNull( "Union should not be null", union );
        assertTrue( "Union value should be a list", union.canAs( DAMLList.class ) );
        assertEquals( "Should be two values in list", 2, ((DAMLList) union.as( DAMLList.class )).getCount() );
    }


    /**
     * Tests on lists
     */
    public void testList()    {
        DAMLModel m = getCleanModel();

        m.read( "file:testing/ontology/daml/daml_oil_2001_03/daml+oil-ex.daml", "http://www.daml.org/2001/03/daml+oil-ex", null );
        assertTrue( "loadStatus should be true for successful load", m.getLoadSuccessful() );
        String ns = "http://www.daml.org/2001/03/daml+oil-ex#";

        // get the Person class
        DAMLClass person = m.getDAMLClass( ns + "Person" );
        assertNotNull( "Person class should not be null", person );
        DAMLClass man = m.getDAMLClass( ns + "Man" );
        assertNotNull( "Man class should not be null", man );
        DAMLClass woman = m.getDAMLClass( ns + "Woman" );
        assertNotNull( "Woman class should not be null", woman );

        // check some basic characteristics of the list
        DAMLList union = (DAMLList) person.prop_disjointUnionOf().get().as( DAMLList.class );
        assertNotNull( "union should not be null", union );
        assertEquals( "union should have two values", 2, union.getCount() );

        // man should be the first element in the list
        RDFNode val1 = union.getFirst();
        assertEquals( "Man should be the first element in the list", man, val1 );

        // woman should be the other element in the list
        DAMLList tail = union.getRest();
        assertNotNull( "Tail of list should not be null", tail );

        RDFNode val2 = tail.getFirst();
        assertNotNull( "head of tail should not be null", val2 );
        assertEquals( "Woman should be the first element in the tail of the list", woman, val2 );

        DAMLList tail2 = tail.getRest();
        assertNotNull( "Tail of tail should not be null", tail2 );
        assertTrue( "Remainder of list should be empty", tail2.isEmpty() );

        // ontologically nonsensical ... just want to test list manipulations
        DAMLClass car = m.getDAMLClass( ns + "Car" );
        assertNotNull( "Class Car should not be null", car );
        union.add( car );
        // DEBUG dumpModel( m.getModel() );
        assertEquals( "Union should contain three elements", 3, union.getCount() );
    }


    /**
     * Tests on instances
     */
    public void testInstance() {
        DAMLModel m = getCleanModel();

        m.read( "file:testing/ontology/daml/daml_oil_2001_03/daml+oil-ex.daml", "http://www.daml.org/2001/03/daml+oil-ex", null );
        assertTrue( "loadStatus should be true for successful load", m.getLoadSuccessful() );
        String ns = "http://www.daml.org/2001/03/daml+oil-ex#";

        // count the number of instances loaded from the standard example
        assertEquals( "Number of instances should be 7", 7,
                      countIteration( m.listDAMLInstances(), false, " instance = " ) );

        // test listing the instances of a class
        DAMLClass person = m.getDAMLClass( ns + "Person" );
        assertNotNull( "Person DAML class should not be null", person );
        int nPerson = countIteration( person.getInstances(), true, "instance of person" );
        assertEquals( "There should be 4 instances of Person in the model", 4, nPerson );
    }


    /**
     * Tests on DAML datatypes
     */
    public void testDatatype() {
        DAMLModel m = getCleanModel();

        m.read( "file:testing/ontology/daml/daml_oil_2001_03/daml+oil-ex.daml", "http://www.daml.org/2001/03/daml+oil-ex", null );
        assertTrue( "loadStatus should be true for successful load", m.getLoadSuccessful() );
        String ns = "http://www.daml.org/2001/03/daml+oil-ex#";

        // get the Person class
        DAMLInstance ian = m.getDAMLInstance( ns + "Ian" );
        assertNotNull( "Instance Ian should not be null", ian );
        DAMLProperty shirtsize = m.getDAMLProperty( ns + "shirtsize" );
        assertNotNull( "Property shirtsize should not be null", shirtsize );
        DAMLProperty shoesize = m.getDAMLProperty( ns + "shoesize" );
        assertNotNull( "Property shoesize should not be null", shoesize );

        DAMLDataInstance sSize = (DAMLDataInstance) ian.getRequiredProperty( shirtsize ).getObject().as( DAMLDataInstance.class );
        assertNotNull( "Object ian should have a shirtsize", sSize );
        Object x = sSize.getValue();
        assertNotNull( "Value of shirtsize should not be null", x );
        assertEquals( "Shirt size should be a string", String.class, x.getClass() );
        assertEquals( "Shirt size should be \"12\"", "12", x );
    }


    public void testDataInstance() {
        DAMLModel m = getCleanModel();
        DAMLDataInstance di = m.createDAMLDataInstance( new Integer( 9 ) );
        assertNotNull( "Failed to create data instance ", di );
        assertEquals( "data instance URI not correct ", TypeMapper.getInstance().getTypeByName( XSD.xint.getURI() ), di.getDatatype() );

        String NS="http://example.org/eg#";
        DAMLClass person = m.createDAMLClass( NS+"Person");
        DAMLInstance peter = m.createDAMLInstance( person, NS+"peter");
        DAMLDatatypeProperty p = m.createDAMLDatatypeProperty( NS+"shirtsize");
        peter.addProperty( p, di );

        // just to confirm we are getting reasonable output m.write( System.out, "RDF/XML-ABBREV" );
    }


    /**
     * Test the removal of DAML objects. We'll load a model, then
     * delete everything in it one step at a time.
     */
    public void testRemove() {
        DAMLModel m = ModelFactory.createDAMLModel();
        m.getDocumentManager().setProcessImports( false );
        m.read( "file:testing/ontology/daml/daml_oil_2001_03/daml+oil-ex.daml", "http://www.daml.org/2001/03/daml+oil-ex", null );
        assertTrue( "loadStatus should be true for successful load", m.getLoadSuccessful() );

        List cache = new ArrayList();
        checkValidLists( m, "baseline" );

        // remove classes, properties and instances
        for (Iterator i = m.listDAMLClasses();  i.hasNext(); ) {
            cache.add( i.next() );
        }
        while (!cache.isEmpty()) {
            DAMLClass c = (DAMLClass) cache.remove( 0 );
            log.debug( "Removing class " + c );
            c.remove();
            checkValidLists( m, "remove class" );
        }

        for (Iterator i = m.listDAMLInstances();  i.hasNext(); ) {
            cache.add( i.next() );
        }
        while (!cache.isEmpty()) {
            DAMLInstance c = (DAMLInstance) cache.remove( 0 );
            log.debug( "Removing instance " + c );
            c.remove();
            checkValidLists( m, "remove instance" );
        }

        for (Iterator i = m.listDAMLProperties();  i.hasNext(); ) {
            cache.add( i.next() );
        }
        while (!cache.isEmpty()) {
            DAMLProperty c = (DAMLProperty) cache.remove( 0 );
            log.debug( "Removing property " + c );
            c.remove();
            checkValidLists( m, "remove property" );
        }

        boolean notBuiltin = false;
        for (Iterator i = m.listDAMLClasses(); i.hasNext();  ) {
            Resource x = (Resource) i.next();
            if (x.equals( DAML_OIL.Thing ) ||
                x.equals( DAML_OIL.Nothing ) ||
                x.hasProperty( DAML_OIL.complementOf, DAML_OIL.Nothing )) {
                // is a builtin
            }
            else {
                log.debug( "Unexpected class remains: " + x );
                for (StmtIterator j = ((Resource) x).listProperties(); j.hasNext(); ) {
                    log.debug( " ... has prop " + j.next() );
                }
                notBuiltin = true;
            }
        }
        assertFalse( "Should be no more classes", notBuiltin );
        assertFalse( "Should be no more properties", m.listDAMLProperties().hasNext() );
        assertFalse( "Should be no more instances", m.listDAMLInstances().hasNext() );
    }


    /**
     * Test the creation of new DAML values
     */
    public void testCreate()

    {
        DAMLModel m = getCleanModel();

        String cURI = "http://dickinson-i-4/daml/tests/gen#A";
        DAMLClass c = m.createDAMLClass( cURI );
        assertNotNull( "Failed to create new DAML Class " + cURI, c );

        // check that we can find this class again
        boolean found = false;
        for (Iterator i = m.listDAMLClasses();  i.hasNext();   ) {
            if (((DAMLClass) i.next()).equals( c )) {
                found = true;
            }
        }
        assertTrue( "Could not see class after it was created", found );

        // now create a new instance
        DAMLInstance x = (DAMLInstance) m.createDAMLValue( "http://dickinson-i-4/daml/tests/gen#x", c );
        assertNotNull( "Failed to create new DAML instance", x );

        found = false;
        for (Iterator i = m.listDAMLInstances();  i.hasNext();   ) {
            if (((DAMLInstance) i.next()).equals( x )) {
                found = true;
            }
        }
        assertTrue( "Could not see instance after it was created", found );
    }



    /**
     * Testing restrictions
     */
    public void testRestriction() {
        DAMLModel m = getCleanModel();

        m.read( "file:testing/ontology/daml/daml_oil_2001_03/daml+oil-ex.daml", "http://www.daml.org/2001/03/daml+oil-ex", null );
        assertTrue( "loadStatus should be true for successful load", m.getLoadSuccessful() );
        String ns = "http://www.daml.org/2001/03/daml+oil-ex#";

        // bug report by Michael Sintek: class cast exception from getting the value of the property iterator
        // get a reference to the Person class
        DAMLClass person = m.getDAMLClass( ns + "Person" );

        // now get a restriction, which we can find as one of the super-classes of Person
        for (Iterator i = person.getSuperClasses();  i.hasNext(); ) {
            Resource r = (Resource) i.next();

            if (r instanceof DAMLRestriction) {
                DAMLRestriction restriction = (DAMLRestriction) r;
                PropertyAccessor onPropertyAccessor = restriction.prop_onProperty();

                // now get the value from the restriction
                int count = onPropertyAccessor.count();

                if (count >= 1) {
                    Object x = onPropertyAccessor.get();
                    Object y = onPropertyAccessor.getAll(  ).next();

                    assertNotNull( "Failed to access value of property accessor on restriction", x );
                    assertNotNull( "Failed to access value of property accessor on restriction", y );
                }
            }
        }
   }


    /**

⌨️ 快捷键说明

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