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

📄 damltest.java

📁 jena2.5.4推理机系统的一种最基本实现 HP实验室出品
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/*****************************************************************************
 * Source code information
 * -----------------------
 * Original author    Ian Dickinson, HP Labs Bristol
 * Author email       Ian.Dickinson@hp.com
 * Package            Jena
 * Created            10 Nov 2000
 * Filename           $RCSfile: DAMLTest.java,v $
 * Revision           $Revision: 1.30 $
 * Release status     Preview-release $State: Exp $
 *
 * Last modified on   $Date: 2007/01/02 11:52:00 $
 *               by   $Author: andy_seaborne $
 *
 * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
 * (see footer for full conditions)
 *****************************************************************************/

// Package
///////////////
package com.hp.hpl.jena.ontology.daml.impl.test;


// Imports
///////////////
import junit.framework.*;

import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.datatypes.TypeMapper;
import com.hp.hpl.jena.ontology.OntDocumentManager;
import com.hp.hpl.jena.ontology.daml.*;
import com.hp.hpl.jena.vocabulary.*;

import java.util.*;

import java.io.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * Legacy JUnit regression tests for the Jena DAML model.
 *
 * @author Ian Dickinson, HP Labs (<a href="mailto:Ian.Dickinson@hp.com">email</a>)
 * @version CVS info: $Id: DAMLTest.java,v 1.30 2007/01/02 11:52:00 andy_seaborne Exp $,
 */
public class DAMLTest
    extends TestCase
{
    // Constants
    //////////////////////////////////


    // Static variables
    //////////////////////////////////

    private static Log log = LogFactory.getLog( DAMLTest.class );


    // Instance variables
    //////////////////////////////////

    // Constructors
    //////////////////////////////////

    /**
     * Constructor requires that all tests be named
     *
     * @param name The name of this test
     */
    public DAMLTest( String name ) {
        super( name );
    }



    // External signature methods
    //////////////////////////////////



    // Internal implementation methods
    //////////////////////////////////

    /**
     * Set up the test conditions
     */
    public void setUp() {
        // reset default condition
        OntDocumentManager.getInstance().reset( true );
    }


    /**
     * Release objects no longer needede when we're done
     */
    public void tearDown() {
    }


    // Test cases
    /////////////

    /**
     * Test the various pathways through loading the ontology from a source document.
     */
    public void testLoadOntology()

    {
        DAMLModel m = getCleanModel();

        // first do the model read with all options turned on
        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( "Load success status should be true", m.getLoadSuccessful() );
        assertEquals( "Count of number of classes in daml store (2001/03, import)", 31, countClasses( m ) );
        // Replaced by der after reasoner update, uncertain whether the "new" results are correct yet!
//        assertEquals( "Property count ", 66, countProperties( m ) );
        assertEquals( "Property count ", 68, countProperties( m ) );
        //dumpModel( m );

        // now turn off importing - should only get the classes and properties in the source doc
        m = getCleanModel();
        m.getLoader().setLoadImportedOntologies( 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( "Load success status should be true", m.getLoadSuccessful() );
        assertEquals( "Count of number of classes in daml store (2001/03, no import)", 16, countClasses( m ) );

        // test case for bug reported by Charlie Abela: must be able to load instance files that import
        // their own class declarations
        m = getCleanModel();
        m.read( "file:testing/ontology/daml/test-instance-load.daml" );
        assertTrue( "Load status should be true", m.getLoadSuccessful() );
        Resource pugh = m.getResource( "http://dickinson-i-4/daml/tests/test-instance-load.daml#pugh" );
        assertNotNull( "Resource for officer Pugh should not be null", pugh );
        DAMLInstance pughInst = (DAMLInstance) pugh.as( DAMLInstance.class );
        assertNotNull( pughInst );

        // test case for bug report by Michael Sintek
        // try to ascertain the most specific class we can at load time -
        // case in point is shoesize in standard example ontology
        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( "Load success status should be true", m.getLoadSuccessful() );
        DAMLProperty shoesize = (DAMLProperty) m.getProperty( "http://www.daml.org/2001/03/daml+oil-ex#shoesize" ).as( DAMLProperty.class );
        assertNotNull( "Failed to find shoesize property in example ontology", shoesize );
        assertEquals( "shoesize should be a unique property", true, shoesize.isUnique() );
    }


    /**
     * Test case for testing rdf:type traversal
     */
    public void testRDFType()

    {
        String ns = "http://dickinson-i-4/daml/tests/test-cases.daml#";

        DAMLModel m = getCleanModel();
        m.read( "file:testing/ontology/daml/test-cases.daml" );

        //dumpModel( m );

        // first find fido
        DAMLInstance fido = m.getDAMLInstance( ns + "fido" );
        assertNotNull( "fido instance should not be null", fido );

        // lookup some classes for convenience
        DAMLClass cDog = m.getDAMLClass( ns + "Dog" );
        assertNotNull( "Dog class should not be null", cDog );

        DAMLClass cVertebrate = m.getDAMLClass( ns + "Vertebrate" );
        assertNotNull( "Vertebrate class should not be null", cVertebrate );

        DAMLClass cPet = m.getDAMLClass( ns + "Pet" );
        assertNotNull( "Pet class should not be null", cPet );

        // fido is a Dog, a vertebrate and a pet
        assertTrue( "fido should be member of class Dog", fido.hasRDFType( cDog ) );
        assertTrue( "fido should be member of class Vertebrate", fido.hasRDFType( cVertebrate ) );
        assertTrue( "fido should be member of class Vertebrate (by URL)", fido.hasRDFType( ns + "Vertebrate" ) );
        assertTrue( "fido should be member of class Pet", fido.hasRDFType( cPet ) );

        // fido is not a class
        assertTrue( "fido should not be a class", !fido.hasRDFType( DAML_OIL.Class ) );

        // fido is a companion, even though this class is not defined in the current ontology
        assertTrue( "fido should be a companion", fido.hasRDFType( ns + "Companion" ) );

        // fido is a Thing (all things DAML are Things)
        // disabled pending adding DAML semantic rules assertTrue( "fido should be a thing", fido.hasRDFType( DAML_OIL.Thing ) );

        // get some more classes
        DAMLClass cA = m.getDAMLClass( ns + "A" );
        assertNotNull( "Class A should not be null", cA );

        DAMLClass cB = m.getDAMLClass( ns + "B" );
        assertNotNull( "Class B should not be null", cB );

        DAMLInstance ab = m.getDAMLInstance( ns + "ab" );
        assertNotNull( "Instance ab should not be null", ab );

        // note that cA --subclass--> cB --subclass--> cA   is a loop
        assertTrue( "ab should be an A", ab.hasRDFType( cA ) );
        assertTrue( "ab should be a B", ab.hasRDFType( cB ) );

        // how many ways do I know thee? let me count the ways ...
        assertEquals( "Number of classes fido belongs to (closure) should be 8",
                      8, countIteration( fido.getRDFTypes( true ), true, "fido member of class " ) );
        assertEquals( "Number of classes fido belongs to (non-closure) should be 3",
                      3, countIteration( fido.getRDFTypes( false ), true, "fido member of non-closed class " ) );

        // some tests on the built-in classes
        DAMLProperty queenOf = m.getDAMLProperty( ns + "queen-of" );
        assertNotNull( "queen-of property should be defined", queenOf );
        assertTrue( "an UnabmbiguousProperty should be an ObjectProperty", queenOf.hasRDFType( DAML_OIL.UnambiguousProperty ) );
        assertTrue( "an UnabmbiguousProperty should be an ObjectProperty", queenOf.hasRDFType( DAML_OIL.ObjectProperty ) );
        assertTrue( "an UnabmbiguousProperty should be an rdf:Property",   queenOf.hasRDFType( RDF.Property ) );
    }


    /**
     * Test some of the properties of DAML classes
     */
    public void testClass() {
        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 a reference to the Person class
        DAMLClass person = m.getDAMLClass( ns + "Person" );
        assertNotNull( "Person class should not be null", person );
        assertTrue( "Person should be a named class", person.isNamedClass() );

        // count the super-classes of a Person
        int sCount0 = countIteration( person.prop_subClassOf().getAll(  ), true, "super-class of Person (prop_subClassOf) " );
        int sCount1 = countIteration( person.getSuperClasses(), true, "super-class of Person (getSuperClasses) " );
        assertEquals( "person should have 10 super-classes (by prop_subClassOf)", 10, sCount0 );
        assertEquals( "person should have 9 super-classes (by getSuperClasses)", 9, sCount1 );

        // count the number of sub-classes of a Person
        assertEquals( "person should have 3 sub-classes", 3,
                      countIteration( person.getSubClasses(), true, "Person super-class of: " ) );

        // person is a disjoint union of Man and Woman
        assertTrue( "Person should be a disjoint union", person.isDisjointUnion() );
        DAMLList mw = person.prop_disjointUnionOf().getList();
        assertNotNull( "Value of disjoint union should not be null", mw );
        assertEquals( "Person should be a disjoint union of size 2", 2, mw.getCount() );

        // Female is disjoint with Male
        DAMLClass female = m.getDAMLClass( ns + "Female" );
        assertNotNull( "Class Female should not be null", female );
        DAMLClass male = m.getDAMLClass( ns + "Male" );
        assertNotNull( "Class Male should not be null", male );
        assertTrue( "Female should be disjoint with male", female.prop_disjointWith().hasValue( male ) );

        // HumanBeing is the same class as Person
        DAMLClass humanBeing = m.getDAMLClass( ns + "HumanBeing" );
        assertNotNull( "Class humanBeing should not be null", humanBeing );
        assertTrue( "Person should be same class as HumanBeing", humanBeing.prop_sameClassAs().hasValue( person ) );

        // TallMan is an intersection of Man and TallThing

⌨️ 快捷键说明

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