AGJenaUtils.java

package com.franz.agjena.examples;


import com.franz.agbase.Triple;
import com.franz.agbase.AllegroGraph;
import com.franz.agbase.TriplesIterator;
import com.franz.agjena.AllegroGraphModel;
import com.franz.agjena.exceptions.NiceException;
import com.franz.agjena.query.AllegroGraphQuery;
import com.franz.agjena.query.AllegroGraphQueryExecutionFactory;
import com.franz.agjena.query.AllegroGraphQueryFactory;
import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.ResultSetFactory;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.impl.ModelCom;
import com.hp.hpl.jena.sparql.core.DatasetImpl;
import com.hp.hpl.jena.sparql.resultset.ResultSetRewindable;

public class AGJenaUtils {
    
    /**
     * Execute 'sparqlQuery' against 'model' and print a tabular result.
     */
    public static void doQuery (String sparqlQuery, Model model) {
        AllegroGraphQuery query = AllegroGraphQueryFactory.create(sparqlQuery) ;
        System.out.println("Query: " + sparqlQuery);
        QueryExecution qe = AllegroGraphQueryExecutionFactory.create(query, model) ;
        ResultSetRewindable rs = ResultSetFactory.makeRewindable(qe.execSelect()) ;
        ResultSetFormatter.out(rs) ;
        qe.close() ; 
    }
    
    /**
     * Execute 'sparqlQuery' against 'graph' and print a tabular result.
     */
    public static void doQuery (String sparqlQuery, Graph graph) {
        AllegroGraphQuery query = AllegroGraphQueryFactory.create(sparqlQuery) ;
        System.out.println("Query: " + sparqlQuery);
        // THERE MUST BE A BETTER WAY TO DEFINE THE DATASET
        QueryExecution qe = AllegroGraphQueryExecutionFactory.create(query, new DatasetImpl(new ModelCom(graph))) ;
        ResultSetRewindable rs = ResultSetFactory.makeRewindable(qe.execSelect()) ;
        ResultSetFormatter.out(rs) ;
        qe.close() ; 
    }

    /**
     * Print all quads in 'model'.
     */
    public static void printModel (Model model) {
        AllegroGraphModel agm = (AllegroGraphModel)model;
        System.out.println("Dump of statements in model '" + agm.getName());
        AllegroGraph agStore = agm.getAllegroGraphStore();
        try {
            String graph = "";  
            if (agm.getName()!=null) graph = "<"+agm.getName()+">";
            TriplesIterator csr = agStore.getStatements(null, null, null, graph);
            while (csr.hasNext()) {
                Triple st = (Triple)csr.next();
                System.out.println(st);
            }
        } catch (Exception ex) {throw new NiceException(ex);}
        
    }
    
}

Up | Next

Copyright © 2014 Franz Inc., All Rights Reserved | Privacy Statement
Delicious Google Buzz Twitter Google+