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

📄 webonttests.java

📁 Jena推理机
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************
 * Source code information
 * -----------------------
 * Original author    Ian Dickinson, HP Labs Bristol
 * Author email       ian.dickinson@hp.com
 * Package            @package@
 * Web site           http://jena.sourceforge.net/
 * Created            20-Apr-2004
 * Filename           $RCSfile: WebOntTests.java,v $
 * Revision           $Revision: 1.12 $
 * Release status     $State: Exp $
 *
 * Last modified on   $Date: 2007/01/02 11:52:53 $
 *               by   $Author: andy_seaborne $
 *
 * (c) Copyright 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
 *****************************************************************************/

// Package
///////////////

package com.hp.hpl.jena.reasoner.dig.test;


// Imports
///////////////
import com.hp.hpl.jena.graph.query.*;
import com.hp.hpl.jena.graph.*;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.ontology.OntDocumentManager;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.reasoner.*;
import com.hp.hpl.jena.reasoner.dig.*;
import com.hp.hpl.jena.reasoner.dig.DIGReasoner;
import com.hp.hpl.jena.reasoner.dig.DIGReasonerFactory;
import com.hp.hpl.jena.reasoner.test.WGReasonerTester;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.vocabulary.*;

import java.io.*;
import java.util.*;

import org.apache.commons.logging.LogFactory;



/**
 * <p>
 * Test harness for running the WebOnt working group tests on the DIG reasoner
 * interface. This class is derived from Dave's
 * {@link com.hp.hpl.jena.reasoner.rulesys.test.WebOntTestHarness WebOntTestHarness}.
 * </p>
 *
 * @author Ian Dickinson, HP Labs ( <a href="mailto:Ian.Dickinson@hp.com">email
 *         </a>)
 * @version Release @release@ ($Id: eclipse-template.txt,v 1.2 2003/10/20
 *          22:03:02 ian_dickinson Exp $)
 */
public class WebOntTests
{
    // Constants
    //////////////////////////////////

    /** The base directory for the working group test files to use */
    public static final String BASE_TESTDIR = "testing/wg/";

    /** The base URI in which the files are purported to reside */
    public static String BASE_URI = "http://www.w3.org/2002/03owlt/";

    /** The namespace for terms in the owl test ontology */
    public static final String OTEST_NS = BASE_URI + "testOntology#";

    /** The base URI for the results file */
    public static String BASE_RESULTS_URI = "http://jena.sourceforge.net/data/owl-results.rdf";

    /** The list of subdirectories to process (omits the rdf/rdfs dirs) */
    public static final String[] TEST_DIRS = {
            "AllDifferent",
            "AnnotationProperty",
            "DatatypeProperty",
            "FunctionalProperty",
            "I3.2",
            "I3.4",
            "I4.1",
            "I4.5",
            "I4.6",
            "I5.1",
            "I5.2",
            "I5.21",
            "I5.24",
            "I5.26",
            "I5.3",
            "I5.5",
            "I5.8",
            "InverseFunctionalProperty",
            "Nothing",
            "Restriction",
            "SymmetricProperty",
            "Thing",
            "TransitiveProperty",
            "Class",
            "allValuesFrom",
            "amp-in-url",
            "cardinality",
            "complementOf",
            "datatypes",
            "differentFrom",
            "disjointWith",
            "distinctMembers",
            "equivalentClass",
            "equivalentProperty",
            "imports",
            "intersectionOf",
            "inverseOf",
            "localtests",
            "maxCardinality",
            "miscellaneous",
            "oneOf",
            "sameAs",
            "someValuesFrom",
            "statement-entailment",
            "unionOf",
            "xmlbase",
            "description-logic",
             "extra-credit",
    };

    /**
     * List of tests that are blocked because they test language features
     * beyond OWL DL
     */
    public static final String[] BLOCKED_TESTS = {};

    /**
     * The list of status values to include. If approvedOnly then only the
     * first entry is allowed
     */
    public static final String[] STATUS_FLAGS = {"APPROVED", "PROPOSED"};

    /** List of acceptable test levels */
    public static final List ACCEPTABLE_TEST_LEVELS = Arrays.asList( new Resource[] {OWLTest.Lite, OWLTest.DL} );

    /** List of predicates we don't want in the premises (because we will try to prove them) */
    protected static List UNSAFE_PREMISE_PREDICATES = new ArrayList();
    static {
        UNSAFE_PREMISE_PREDICATES.add( OWL.equivalentClass );
        UNSAFE_PREMISE_PREDICATES.add( OWL.equivalentProperty);
        UNSAFE_PREMISE_PREDICATES.add( OWL.sameAs );
        UNSAFE_PREMISE_PREDICATES.add( RDFS.subClassOf );
        UNSAFE_PREMISE_PREDICATES.add( RDFS.subPropertyOf );
        UNSAFE_PREMISE_PREDICATES.add( DAML_OIL.sameClassAs );
        UNSAFE_PREMISE_PREDICATES.add( DAML_OIL.sameIndividualAs );
        UNSAFE_PREMISE_PREDICATES.add( DAML_OIL.samePropertyAs );
        UNSAFE_PREMISE_PREDICATES.add( DAML_OIL.subClassOf );
        UNSAFE_PREMISE_PREDICATES.add( DAML_OIL.subPropertyOf );
    }


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

    /** Set to true to include modified test versions */
    protected static boolean s_includeModified = false;

    /** Set to true to use approved tests only */
    protected static boolean s_approvedOnly = true;


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

    /** The reasoner being tested */
    private DIGReasoner m_reasoner;

    /** The total set of known tests */
    private Model m_testDefinitions;

    /** The number of tests run */
    private int m_testCount = 0;

    /** The time cost in ms of the last test to be run */
    private long m_lastTestDuration = 0;

    /** Number of tests passed */
    private int m_passCount = 0;

    /** The model describing the results of the run */
    private Model m_testResults;

    /**
     * The resource which acts as a description for the Jena2 instance being
     * tested
     */
    private Resource m_jena2;


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

    public WebOntTests() {
        m_testDefinitions = loadAllTestDefinitions();
        DIGReasonerFactory drf = (DIGReasonerFactory) ReasonerRegistry.theRegistry().getFactory( DIGReasonerFactory.URI );
        m_reasoner = (DIGReasoner) drf.createWithOWLAxioms( null );
        initResults();
    }


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

    public static void main( String[] args ) throws IOException {
        String resultFile = "owl-results.rdf";
        String testName = null;

        if (args.length >= 1) {
            testName = args[0];
        }

        WebOntTests harness = new WebOntTests();

        // initialise the document manager
        OntDocumentManager.getInstance().addAltEntry( "http://www.w3.org/2002/03owlt/miscellaneous/consistent002",
                                                        "file:testing/wg/miscellaneous/consistent002.rdf" );
        OntDocumentManager.getInstance().addAltEntry( "http://www.w3.org/2002/03owlt/miscellaneous/consistent001",
                                                        "file:testing/wg/miscellaneous/consistent001.rdf" );

        if (testName == null) {
            harness.runTests();
        }
        else {
            harness.runTest( testName );
        }

        RDFWriter writer = harness.m_testResults.getWriter("RDF/XML-ABBREV");
        OutputStream stream = new FileOutputStream(resultFile);
        writer.setProperty("showXmlDeclaration", "true");
        harness.m_testResults.setNsPrefix("", "http://www.w3.org/1999/xhtml");
        writer.write(harness.m_testResults, stream, BASE_RESULTS_URI);
    }

    /**
     * Run all relevant tests.
     */
    public void runTests() {
        System.out.println("Testing " + (s_approvedOnly ? "only APPROVED" : "APPROVED and PROPOSED"));
        System.out.println("Positive entailment: ");
        runTests(findTestsOfType(OWLTest.PositiveEntailmentTest));
        System.out.println("\nNegative entailment: ");
        runTests(findTestsOfType(OWLTest.NegativeEntailmentTest));
        System.out.println("\nTrue tests: ");
        runTests(findTestsOfType(OWLTest.TrueTest));
        System.out.println("\nOWL for OWL tests: ");
        runTests(findTestsOfType(OWLTest.OWLforOWLTest));
        System.out.println("\nImport entailment tests: ");
        runTests(findTestsOfType(OWLTest.ImportEntailmentTest));
        System.out.println("\nInconsistency tests: ");
        runTests(findTestsOfType(OWLTest.InconsistencyTest));
        System.out.println("\nPassed " + m_passCount + " out of " + m_testCount);
    }

    /**
     * Run all tests in the given list.
     */
    public void runTests( List tests ) {
        for (Iterator i = tests.iterator(); i.hasNext();) {
            runTest((Resource) i.next());
        }
    }

    /**
     * Run a single test of any sort, performing any appropriate logging and
     * error reporting.
     */
    public void runTest( String test ) {
        runTest(m_testDefinitions.getResource(test));
    }

    /**
     * Run a single test of any sort, performing any appropriate logging and
     * error reporting.
     */
    public void runTest( Resource test ) {
        System.out.println("Running " + test);
        boolean success = false;
        boolean fail = false;
        try {
            success = doRunTest(test);
        }
        catch (Exception e) {
            fail = true;
            System.err.print("\nException: " + e);
            e.printStackTrace();
        }
        m_testCount++;

        if (success) {
            System.out.print((m_testCount % 40 == 0) ? ".\n" : ".");
            System.out.flush();
            m_passCount++;
        }
        else {
            System.out.println("\nFAIL: " + test);
        }
        Resource resultType = null;

        if (fail) {
            resultType = OWLResults.FailingRun;
        }
        else {
            if (test.hasProperty(RDF.type, OWLTest.NegativeEntailmentTest)
                    || test.hasProperty(RDF.type, OWLTest.ConsistencyTest)) {
                resultType = success ? OWLResults.PassingRun : OWLResults.FailingRun;
            }
            else {
                resultType = success ? OWLResults.PassingRun : OWLResults.IncompleteRun;
            }
        }

        // log to the rdf result format
        m_testResults.createResource()
                     .addProperty(RDF.type, OWLResults.TestRun)
                     .addProperty(RDF.type, resultType)
                     .addProperty(OWLResults.test, test)
                     .addProperty(OWLResults.system, m_jena2);
    }

    /**
     * Run a single test of any sort, return true if the test succeeds.
     */
    public boolean doRunTest( Resource test )
        throws IOException
    {
        if (test.hasProperty(RDF.type, OWLTest.PositiveEntailmentTest)
                || test.hasProperty(RDF.type, OWLTest.NegativeEntailmentTest)
                || test.hasProperty(RDF.type, OWLTest.OWLforOWLTest)
                || test.hasProperty(RDF.type, OWLTest.ImportEntailmentTest)
                || test.hasProperty(RDF.type, OWLTest.TrueTest)) {
            // Entailment tests
            System.out.println("Starting: " + test);
            boolean processImports = test.hasProperty( RDF.type, OWLTest.ImportEntailmentTest );
            Model premises = getDoc( test, RDFTest.premiseDocument, processImports );
            Model conclusions = getDoc( test, RDFTest.conclusionDocument );

            long t1 = System.currentTimeMillis();
            boolean correct = testEntailment( conclusions, m_reasoner.bind( premises.getGraph() ) );
            m_lastTestDuration = System.currentTimeMillis() - t1;

            if (test.hasProperty(RDF.type, OWLTest.NegativeEntailmentTest)) {
                correct = !correct;
            }
            return correct;
        }
        else if (test.hasProperty(RDF.type, OWLTest.InconsistencyTest)) {
            System.out.println("Starting: " + test);
            Model input = getDoc(test, RDFTest.inputDocument);

⌨️ 快捷键说明

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