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

📄 damltest.java

📁 jena2.5.4推理机系统的一种最基本实现 HP实验室出品
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
     * Test adding a model to an existing model
     */
    public void testModelAdd() {
        DAMLModel m = getCleanModel();

        // create a daml model
        m.read( "file:testing/ontology/daml/test-add-0.daml" );
        assertTrue( "loadStatus should be true for successful load", m.getLoadSuccessful() );

        // create a normal rdf model
        Model m0 = ModelFactory.createDefaultModel();
        m0.read( "file:testing/ontology/daml/test-add-1.daml" );

        // should be 0 instances in the daml model so far
        assertEquals( "Instance count in DAML model should be 0", 0, countIteration( m.listDAMLInstances(), true, "instance in test add" ) );

        // now add the RDF data
        m.add( m0 );

        // now should be 1 instances in the daml model
        assertEquals( "Instance count in DAML model should be 1", 1, countIteration( m.listDAMLInstances(), true, "instance in test add" ) );
    }


    /**
     * Testing equality: case DatatypeProperty
     */
    public void testDatatypeProperty()

    {
        eqTest(new EqualityTest("DatatypeProperty") {
                String xml() {
                    return "<daml:DatatypeProperty/>";
                }
                void java(DAMLModel m) {
                    m.createDAMLDatatypeProperty(null);
                }
        });
    }
    /**
     * Testing equality: case ObjectProperty
     */
    public void testObjectProperty()

    {
        eqTest(new EqualityTest("ObjectProperty") {
                String xml() {
                    return "<daml:ObjectProperty/>";
                }
                void java(DAMLModel m) {
                    m.createDAMLObjectProperty(null);
                }
        });
    }
    /**
     * Testing equality: case Property
     */
    public void testPropertyEq()

    {
        eqTest(new EqualityTest("Property") {
                String xml() {
                    return "<daml:Property/>";
                }
                void java(DAMLModel m) {
                    m.createDAMLProperty(null);
                }
        });
    }


    /**
     * Testing equality: case Datatype
     */
    public void testDatatypeEq2()

    {
        eqTest(new EqualityTest("Datatype") {
                String xml() {
                    return "<daml:Datatype rdf:about='http://www.w3.org/2000/10/XMLSchema#string'/>";
                }
                void java(DAMLModel m) {
                    m.createDAMLDatatype("http://www.w3.org/2000/10/XMLSchema#string");
                }
        });
    }

    public void testDatatypeRange()

    {
        eqTest(new EqualityTest("Datatype Range") {
                String xml() {
                    // Example taken from the DAML+OIL walk-thru
                    return
"<daml:DatatypeProperty rdf:ID='shoesize'>"+
 " <rdf:type rdf:resource='http://www.daml.org/2001/03/daml+oil#UniqueProperty'/>" +
 " <daml:range rdf:resource='http://www.w3.org/2000/10/XMLSchema#decimal'/>" +
"</daml:DatatypeProperty>" +
"<daml:Datatype rdf:about='http://www.w3.org/2000/10/XMLSchema#decimal'/>";
                }
                void java(DAMLModel m) {
                    DAMLDatatypeProperty shoeSize=m.createDAMLDatatypeProperty("http://example.org/#shoesize");
                    shoeSize.setIsUnique(true);
                    shoeSize.prop_range().add(
                        m.createDAMLDatatype("http://www.w3.org/2000/10/XMLSchema#decimal") );
                }
        });
    }


    /**
     * Concept: the EqualityTest object embodies some java code that adds
     * stuff to a DAMLModel, and some xml that is the body of an RDF/XML doc.
     * This test checks that these two different ways of describing a DAML
     * model are the same.
     *
     */
    private void eqTest(EqualityTest test) {
        DAMLModel m1 = getCleanModel();
        test.java(m1);

        Model m2 = ModelFactory.createDefaultModel();
        Reader rdr = new StringReader(
          "<rdf:RDF " +
    "xmlns:daml='http://www.daml.org/2001/03/daml+oil#' " +
    "xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>"
     + test.xml()
       + "</rdf:RDF>");
       m2.read(rdr,"http://example.org/");

       if (! m1.getBaseModel().isIsomorphicWith(m2) ) {
           System.out.println("Java:");
           m1.write(System.out,"RDF/XML-ABBREV");
           System.out.println("XML:");
           m2.write(System.out,"RDF/XML-ABBREV");
       }

       assertTrue("java code and xml should be equivalent",m1.getBaseModel().isIsomorphicWith(m2));

   }

   static abstract private class EqualityTest {
       String name;
       EqualityTest(String nm) {
           name = nm;
       }
       public String toString() {
           return name;
       }
       abstract void java(DAMLModel m);
       abstract String xml();
   }


    /**
     * Dump the model out to a file for debugging
     */
    public void dumpModel( Model m ) {
        dumpModel( m, "model-out.rdf" );
    }
    public void dumpModel( Model m, String fileName ) {
        try {
            OutputStream f = new FileOutputStream(fileName);
            m.write( f, "RDF/XML-ABBREV" );
            f.close();
        }
        catch (Exception e) {
           LogFactory.getLog( getClass() ).debug( "Exception while dumping model: " + e, e );
        }

    }


    /**
     * Count the number of things in an iterator, optionally logging them
     */
    private int countIteration( Iterator i, boolean doLog, String message ) {
        int count = 0;
        for (;  i.hasNext();  count++) {
            Object x = i.next();

            if (doLog) {
                LogFactory.getLog( getClass() ).debug( "counting iteration, " + message + x );
            }
        }

        return count;
    }

    private int countClasses( DAMLModel m ) {
        return countIteration( m.listDAMLClasses(), true, "class = " );
    }

    private int countProperties( DAMLModel m ) {
        return countIteration( m.listDAMLProperties(), true, "property = " );
    }

    private DAMLModel getCleanModel() {
        DAMLModel m = ModelFactory.createDAMLModel();
        m.getDocumentManager().setProcessImports(true);
        m.getDocumentManager().clearCache();
        m.getDocumentManager().setMetadataSearchPath( "file:etc/ont-policy-test.rdf", true );
        List ll = new ArrayList();
        for (Iterator i = m.getImportModelMaker().listModels(); i.hasNext(); ll.add( i.next() ) );
        for (Iterator i = ll.iterator(); i.hasNext(); ) {
            String mName = (String) i.next();
            m.getImportModelMaker().removeModel(mName);
            log.debug( "Removing " + mName );
        }
        return m;
    }

    private void checkValidLists( Model m, String label ) {
        log.debug( "Checking lists in DAMLTest - " + label );
        for (StmtIterator i = m.listStatements(); i.hasNext(); ) {
            Statement s = i.nextStatement();
            RDFNode n = s.getObject();
            if (n instanceof Resource && ((Resource) n).canAs( RDFList.class )) {
                if (!((RDFList) n.as(RDFList.class)).isValid()) {
                    log.debug( "!!Found invalid list in " + label + " - " + n );
                    assertTrue( "DAML list not valid ", false );
                }
            }
        }
    }

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


}


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