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

📄 webonttests.java

📁 jena2.5.4推理机系统的一种最基本实现 HP实验室出品
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            long t1 = System.currentTimeMillis();
            InfGraph graph = m_reasoner.bind(input.getGraph());
            boolean correct = !graph.validate().isValid();
            m_lastTestDuration = System.currentTimeMillis() - t1;
            return correct;
        }
        else if (test.hasProperty(RDF.type, OWLTest.ConsistencyTest)) {
            System.out.println("Starting: " + test);
            Model input = getDoc(test, RDFTest.inputDocument);
            long t1 = System.currentTimeMillis();
            InfGraph graph = m_reasoner.bind(input.getGraph());
            boolean correct = graph.validate().isValid();
            long t2 = System.currentTimeMillis();
            m_lastTestDuration = t2 - t1;
            return correct;
        }
        else {
            for (StmtIterator i = test.listProperties(RDF.type); i.hasNext();) {
                System.out.println("Test type = " + i.nextStatement().getObject());
            }
            throw new ReasonerException("Unknown test type");
        }
    }

    /**
     * Load the premises or conclusions for the test, optional performing
     * import processing.
     */
    public Model getDoc( Resource test, Property docType, boolean processImports ) throws IOException {
        if (processImports) {
            Model result = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, null);
            StmtIterator si = test.listProperties(docType);
            while (si.hasNext()) {
                String fname = si.nextStatement().getObject().toString() + ".rdf";
                loadFile(fname, result);
            }
            return result;
        }
        else {
            return getDoc(test, docType);
        }
    }

    /**
     * Load the premises or conclusions for the test.
     */
    public Model getDoc( Resource test, Property docType ) throws IOException {
        Model result = ModelFactory.createDefaultModel();
        StmtIterator si = test.listProperties(docType);
        while (si.hasNext()) {
            String fname = si.nextStatement().getObject().toString() + ".rdf";
            loadFile(fname, result);
        }
        return result;
    }

    /**
     * Utility to load a file into a model a Model. Files are assumed to be
     * relative to the BASE_URI.
     *
     * @param file the file name, relative to baseDir
     * @return the loaded Model
     */
    public static Model loadFile( String file, Model model ) throws IOException {
        String langType = "RDF/XML";
        if (file.endsWith(".nt")) {
            langType = "N-TRIPLE";
        }
        else if (file.endsWith("n3")) {
            langType = "N3";
        }
        String fname = file;
        if (fname.startsWith(BASE_URI)) {
            fname = fname.substring(BASE_URI.length());
        }
        Reader reader = new BufferedReader(new FileReader(BASE_TESTDIR + fname));
        model.read(reader, BASE_URI + fname, langType);
        return model;
    }

    /**
     * Test a conclusions graph against a result graph. This works by
     * translating the conclusions graph into a find query which contains one
     * variable for each distinct bNode in the conclusions graph.
     */
    public boolean testEntailment( Model conclusions, InfGraph inf ) {
        List queryRoots = listQueryRoots( conclusions );
        Model result = ModelFactory.createDefaultModel();

        for (Iterator i = queryRoots.iterator(); i.hasNext(); ) {
            Resource root = (Resource) i.next();

            for (StmtIterator j = root.listProperties();  j.hasNext(); ) {
                Statement rootQuery = j.nextStatement();
                Resource subject = rootQuery.getSubject();
                RDFNode object = rootQuery.getObject();

                OntModel premises = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM, null );
                premises.setStrictMode( false );

                if (subject.isAnon()) {
                    // subject is assumed to be an expression
                    addSubGraph( subject, premises );
                }
                if (object instanceof Resource && ((Resource) object).isAnon()) {
                    addSubGraph( (Resource) object, premises );
                }

                // add the resulting triples to the graph
                try {
                    ExtendedIterator k =inf.find( rootQuery.getSubject().asNode(),
                                                  rootQuery.getPredicate().asNode(),
                                                  rootQuery.getObject().asNode(),
                                                  premises.getGraph() );
                    while (k.hasNext()) {
                        //Triple t = (Triple) k.next();
                        Object x = k.next();
                        Triple t = (Triple) x;
                        LogFactory.getLog( getClass() ).debug( "testEntailment got triple " + t );
                        result.getGraph().add( t );
                    }

                    // transcribe the premises into the results
                    result.add( premises );
                }
                catch (DIGErrorResponseException e) {
                    LogFactory.getLog( getClass() ).error( "DIG reasoner returned error: " + e.getMessage() );
                    return false;
                }
            }
        }

        result.write( System.out, "RDF/XML-ABBREV" );
        // now check that the conclusions, framed as a query, holds
        QueryHandler qh = result.queryHandler();
        Query query = WGReasonerTester.graphToQuery(conclusions.getGraph());
        Iterator i = qh.prepareBindings(query, new Node[] {}).executeBindings();
        return i.hasNext();
    }



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

    /** Load all of the known manifest files into a single model */
    protected Model loadAllTestDefinitions() {
        System.out.print("Loading manifests ");
        System.out.flush();
        Model testDefs = ModelFactory.createDefaultModel();
        int count = 0;
        for (int idir = 0; idir < TEST_DIRS.length; idir++) {
            File dir = new File(BASE_TESTDIR + TEST_DIRS[idir]);
            String[] manifests = dir.list(new FilenameFilter() {
                public boolean accept( File df, String name ) {
                    return name.startsWith("Manifest") && name.endsWith(".rdf") &&
                            (s_includeModified || !name.endsWith("-mod.rdf"));
                }
            });
            if (manifests == null) {
                System.err.println( "No manifests for " + BASE_TESTDIR + TEST_DIRS[idir] );
            }
            else {
                for (int im = 0; im < manifests.length; im++) {
                    String manifest = manifests[im];
                    File mf = new File(dir, manifest);
                    try {
                        testDefs.read(new FileInputStream(mf), "file:" + mf);
                        count++;
                        if (count % 8 == 0) {
                            System.out.print(".");
                            System.out.flush();
                        }
                    }
                    catch (FileNotFoundException e) {
                        System.out.println("File not readable - " + e);
                    }
                }
            }
        }
        System.out.println("loaded");
        return testDefs;
    }

    /**
     * Initialize the result model.
     */
    protected void initResults() {
        m_testResults = ModelFactory.createDefaultModel();
        m_jena2 = m_testResults.createResource(BASE_RESULTS_URI + "#jena2");
        m_jena2
                .addProperty(
                        RDFS.comment,
                        m_testResults
                                .createLiteral(
                                        "<a xmlns=\"http://www.w3.org/1999/xhtml\" href=\"http://jena.sourceforce.net/\">Jena2</a> includes a rule-based inference engine for RDF processing, "
                                                + "supporting both forward and backward chaining rules. Its OWL rule set is designed to provide sound "
                                                + "but not complete instance resasoning for that fragment of OWL/Full limited to the OWL/lite vocabulary. In"
                                                + "particular it does not support unionOf/complementOf.", true));
        m_jena2.addProperty(RDFS.label, "Jena2");
        m_testResults.setNsPrefix("results", OWLResults.NS);
    }


    /**
     * Return a list of all tests of the given type, according to the current
     * filters
     */
    public List findTestsOfType( Resource testType ) {
        ArrayList result = new ArrayList();
        StmtIterator si = m_testDefinitions.listStatements(null, RDF.type, testType);
        while (si.hasNext()) {
            Resource test = si.nextStatement().getSubject();
            boolean accept = true;

            // Check test status
            Literal status = (Literal) test.getProperty(RDFTest.status).getObject();
            if (s_approvedOnly) {
                accept = status.getString().equals(STATUS_FLAGS[0]);
            }
            else {
                accept = false;
                for (int i = 0; i < STATUS_FLAGS.length; i++) {
                    if (status.getString().equals(STATUS_FLAGS[i])) {
                        accept = true;
                        break;
                    }
                }
            }

            // Check for blocked tests
            for (int i = 0; i < BLOCKED_TESTS.length; i++) {
                if (BLOCKED_TESTS[i].equals(test.toString())) {
                    accept = false;
                }
            }

            // Check test level
            if (accept) {
                boolean reject = true;
                for (StmtIterator i = test.listProperties( OWLTest.level ); i.hasNext(); ) {
                    if (ACCEPTABLE_TEST_LEVELS.contains( i.nextStatement().getResource() )) {
                        reject = false;
                    }
                }

                if (reject) {
                    LogFactory.getLog( getClass() ).debug( "Ignoring test " + test + " because it either has no test level defined, or an unacceptable test level" );
                    accept = false;
                }
            }

            // End of filter tests
            if (accept) {
                result.add(test);
            }
        }
        return result;
    }

    /**
     * The query roots of are the set of subjects we want to ask the DIG
     * reasoner about ... we interpret this as every named resource in the given model
     */
    protected List listQueryRoots( Model m ) {
        List roots = new ArrayList();

        for (ResIterator i = m.listSubjects(); i.hasNext(); ) {
            Resource subj = i.nextResource();
            if (!subj.isAnon()) {
                roots.add( subj );
            }
        }

        for (Iterator i = roots.iterator(); i.hasNext();  ) {
            LogFactory.getLog( getClass() ).debug( "Found query root: " + i.next() );
        }
        return roots;
    }

    /**
     * Add the reachable sub-graph from root, unless it traverses a predicate
     * that we might be trying to establish.
     * @param root
     * @param premises
     */
    protected void addSubGraph( Resource root, Model premises ) {
        List q = new ArrayList();
        Set seen = new HashSet();
        q.add( root );

        while (!q.isEmpty()) {
            Resource r = (Resource) q.remove( 0 );

            if (!seen.contains( r )) {
                for (StmtIterator i = r.listProperties(); i.hasNext(); ) {
                    Statement s = i.nextStatement();

                    if (safePremise( s.getPredicate() )) {
                        premises.add( s );
                        if (s.getObject() instanceof Resource) {
                            q.add( s.getObject() );
                        }
                    }
                }
                seen.add( r );
            }
        }
    }

    /**
     * <p>Answer true if p is a property that is safe to add as a premise without
     * assertng what we are trying to find out.  Properties ruled out by this
     * test are owl:equivalentClass, owl:equivalentProperty, etc.
     * @param p A property to test
     * @return True if p is safe to add to the premises
     */
    protected boolean safePremise( Property p ) {
        return !(UNSAFE_PREMISE_PREDICATES.contains( p ));
    }


    //==============================================================================
    // Inner class definitions
    //==============================================================================

}


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