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

📄 rdfquery.java

📁 Java实现的数据库和RDF之间的转换程序。
💻 JAVA
字号:
/*
 * (c) Copyright 2001, 2002, 2003, Hewlett-Packard Development Company, LP
 * [See end of file]
 */


package rdfquery;

import junit.framework.* ;
import java.io.* ;

import com.hp.hpl.jena.util.* ;
import com.hp.hpl.jena.rdql.* ;
import com.hp.hpl.jena.rdql.test.* ;
import com.hp.hpl.jena.rdf.model.* ;
import com.hp.hpl.jena.shared.*;

import com.hp.hpl.jena.vocabulary.ResultSet ;

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

//import com.hp.hpl.jena.mem.ModelMem ;
//import com.hp.hpl.jena.reasoner.* ;

/** A program to execute queries from the command line.
 *
 *  Queries can specify the source file so this can be used as a simple
 *  script engine for RDQL.
 *
 *  <pre>
 *  Usage: [--xml|--ntriple] [--data URL] [queryString | --query file]") ;
 *     --query file         Read one query from a file
 *     --rdfs               Use an RDFS reasoner around the data
 *     --reasoner URI       Set the reasoner URI explicitly.
 *     --vocab URL | File   Specify a separate vocabulary (may also be in the data)
 *     --xml                Data source is XML (default)
 *     --ntriple            Data source is n-triple
 *     --n3                 Data source is N3
 *     --data URL | File    Data source (can also be part of query)
 *     --time               Print some time information
 *     --test [file]        Run the test suite
 *     --format FMT         One of text, html, tuples, dump or none
 *     --verbose            Verbose - more messages
 *     --quiet              Quiet - less messages
 * Options for data and format override the query specified source.
 * </pre>
 *
 * @author  Andy Seaborne
 * @version $Id: rdfquery.java,v 1.19 2003/12/08 10:48:29 andy_seaborne Exp $
 */

// To do: formalise the use of variables and separate out the command line processor
// so the "query" routine can be used for testing from other main's.

public class rdfquery
{
    static public boolean displayTime = false ;
    static public int messageLevel = 0 ;
    static public boolean debug   = false ;
    static public boolean dumpModel = false ;

    static final int FMT_NONE    = -1 ;
    static final int FMT_TUPLES  = 0 ;
    static final int FMT_TEXT    = 1 ;
    static final int FMT_HTML    = 2 ;
    static final int FMT_DUMP    = 3 ;

    static public int outputFormat = FMT_TEXT ;
    static String dbUser = "" ;
    static String dbPassword = "" ;
    static String dbType = "" ;
    static String dbName = "" ;
    static String dbDriver = null ;

    //static final String defaultReasonerURI =  "http://www.hpl.hp.com/semweb/2003/RDFSReasoner1" ;
    //static String reasonerURI = defaultReasonerURI ;
    static String vocabularyURI = null ;
    static Model vocabulary = null ;

    static boolean applyRDFS = false ;

    static protected Log logger = LogFactory.getLog( rdfquery.class );

    static void allTests()
    {
        // This should load all the built in tests.
        // It does not load the external test scripts.

        TestSuite ts = new TestSuite("RDQL") ;
        ts.addTest(TestExpressions.suite()) ;
        //ts.addTest(QueryTestScripts.suite()) ;
        ts.addTest(QueryTestProgrammatic.suite()) ;
        junit.textui.TestRunner.run(ts) ;
    }


    // Execute one query, with stats etc., print results
    static public void query(String s, String dataURL, String language)
           throws Exception
    {
        //try {
            boolean doBlank = false ;

            if ( messageLevel >= 2 )
            {
                System.out.println("Query:") ;
                System.out.println(s) ;
                if ( ! s.endsWith("\n") )
                    System.out.println() ;
                doBlank = true ;
            }

            long startTime = System.currentTimeMillis();
            long loadTime = -1 ;

            Query query = new Query(s) ;
            if ( displayTime )
                // Do again after classloading has occured.
                query = new Query(s) ;

            if ( messageLevel > 0 )
            {
                System.out.println("Parsed query:") ;
                String tmp = query.toString() ;
                System.out.print(tmp) ;
                if ( ! tmp.endsWith("\n") )
                    System.out.println() ;
                doBlank = true ;
            }

            if ( dataURL == null && query.getSourceURL() == null )
            {
                System.err.println("RDQL: no data source");
                return ;
            }

            if ( dataURL != null )
            {
                long startLoadTime = System.currentTimeMillis();
                query.setSource(ModelLoader.loadModel(dataURL, language,
                                                      dbUser, dbPassword,
                                                      dbName, dbType, dbDriver)) ;
                Model m = query.getSource() ;
                // ------------

                if ( applyRDFS )
                {
                    Model model = null ;
                    if ( vocabularyURI != null )
                    {
                        vocabulary = ModelLoader.loadModel(vocabularyURI, null) ;
                        model = ModelFactory.createRDFSModel(m, vocabulary) ;
                    }
                    else
                    {
                        model = ModelFactory.createRDFSModel(m) ;
                    }
                    query.setSource(model) ;
                }
                loadTime = System.currentTimeMillis() - startLoadTime ;
                query.loadTime = loadTime ;
            }

            QueryExecution qe = new QueryEngine(query) ;
            qe.init() ;
            if ( dumpModel )
            {
                try {
                    if ( doBlank )
                        System.out.println() ;
                    doBlank = true ;
                    Model model = query.getSource() ;
                    RDFWriter w = model.getWriter("N-TRIPLE") ;
                    PrintWriter pw = new PrintWriter(System.out) ;
                    pw.println("# Model --------------------------------------------------------------------------------") ;
                    w.write(model, pw, "http://unset/") ;
                    pw.println("# Model --------------------------------------------------------------------------------") ;
                    pw.flush() ;
                } catch (JenaException refEx) { logger.error("rdfquery: Failed to write model") ; System.exit(1) ; }
            }
            QueryResults results = qe.exec() ;
            QueryResultsFormatter fmt = new QueryResultsFormatter(results) ;

            if ( outputFormat == FMT_NONE)
                fmt.consume() ;
            else
            {
                if ( doBlank ) System.out.println() ;

                if ( outputFormat == FMT_DUMP )
                {
                    Model m = fmt.toModel() ;
                    RDFWriter rdfw = m.getWriter("N3") ;
                    m.setNsPrefix("rs", ResultSet.getURI()) ;
                    rdfw.write(m, System.out, null) ;
                }
                else
                {
                    db_rdf.information.resultStr="";
                    PrintWriter pw = new PrintWriter(System.out) ;
                    switch(outputFormat)
                    {
                        case FMT_TEXT:   fmt.printAll(pw) ; break ;
                        case FMT_HTML:   fmt.printHTML(pw) ; break ;
                        case FMT_TUPLES: fmt.dump(pw, true) ; break ;
                        default: break ;
                    }
                    //System.out.print(db_rdf.information.resultStr);
                    pw.flush() ;
                }
                doBlank = true ;
            }

            fmt.close() ;
            results.close() ;
            qe.close() ;

            long finishTime = System.currentTimeMillis();
            long totalTime = finishTime-startTime ;

            if ( messageLevel > 0 )
            {
                if ( doBlank ) System.out.println() ;
                System.out.println("Results: "+fmt.numRows()) ;
                doBlank = true ;
            }

            if ( displayTime )
            {
                if ( doBlank ) System.out.println() ;
                System.out.println("Query parse:     "+formatlong(query.parseTime)   +" ms") ;
                System.out.println("Query build:     "+formatlong(query.buildTime)   +" ms") ;
                System.out.println("Data load time:  "+formatlong(query.loadTime)    +" ms") ;
                System.out.println("Query execute:   "+formatlong(query.executeTime) +" ms") ;
                System.out.println("Query misc:      "+formatlong(totalTime-query.parseTime-query.buildTime-query.loadTime-query.executeTime)+" ms") ;
                System.out.println("Query total:     "+formatlong(totalTime)         +" ms") ;
                doBlank = true ;
            }

            if ( query.getSource() != null )
                query.getSource().close() ;
            /*
            PrintWriter pw = new PrintWriter(System.out) ;
            resultsIter.print(pw) ;
            pw.flush() ;
            */
        /*}
        catch (QueryException qEx)
        {
            System.err.println(qEx.getMessage()) ;
            System.exit(9) ;
        }*/
    }

    static String formatlong(long x)
    {
        StringBuffer sbuff = new StringBuffer() ;
        sbuff.append(Long.toString(x)) ;
        for ( int i = sbuff.length() ; i < 4 ; i++ ) sbuff.append(" ") ;
        return sbuff.toString() ;
    }
 }

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