Search in sources :

Example 86 with Model

use of org.apache.jena.rdf.model.Model in project jena by apache.

the class T_TransSystem method describeWithAbort.

public static int describeWithAbort(String queryStr, DatasetGraph dsg, long abortTime) {
    int counter = 0;
    Query query = QueryFactory.create(queryStr, Syntax.syntaxARQ);
    try (QueryExecution qExec = QueryExecutionFactory.create(query, DatasetFactory.wrap(dsg))) {
        qExec.setTimeout(abortTime);
        Model model = qExec.execDescribe();
        //ResultSet rs = qExec.execSelect() ;
        for (Iterator<Statement> stmIterator = model.listStatements(); stmIterator.hasNext(); ) {
            stmIterator.next();
            counter++;
        }
        return counter;
    }
}
Also used : Statement(org.apache.jena.rdf.model.Statement) Model(org.apache.jena.rdf.model.Model)

Example 87 with Model

use of org.apache.jena.rdf.model.Model in project jena by apache.

the class ResultSetFactory method result.

/**
     * Read in any kind of result kind (result set, boolean, graph)
     */
public static SPARQLResult result(String filenameOrURI, ResultsFormat format) {
    if (format == null)
        format = ResultsFormat.guessSyntax(filenameOrURI);
    if (format == null) {
        Log.warn(ResultSet.class, "Null format - defaulting to XML");
        format = ResultsFormat.FMT_RS_XML;
    }
    if (format.equals(ResultsFormat.FMT_TEXT)) {
        Log.error(ResultSet.class, "Can't read a text result set");
        throw new ResultSetException("Can't read a text result set");
    }
    if (format.equals(ResultsFormat.FMT_RS_XML) || format.equals(ResultsFormat.FMT_RS_JSON) || format.equals(ResultsFormat.FMT_RS_TSV) || format.equals(ResultsFormat.FMT_RS_CSV)) {
        InputStream in = null;
        try {
            in = FileManager.get().open(filenameOrURI);
            if (in == null)
                throw new NotFoundException(filenameOrURI);
        } catch (NotFoundException ex) {
            throw new NotFoundException("File not found: " + filenameOrURI);
        }
        SPARQLResult x = null;
        if (format.equals(ResultsFormat.FMT_RS_JSON))
            return JSONInput.make(in, GraphFactory.makeDefaultModel());
        else if (format.equals(ResultsFormat.FMT_RS_XML))
            return XMLInput.make(in, GraphFactory.makeDefaultModel());
        else if (format.equals(ResultsFormat.FMT_RS_TSV)) {
            ResultSet rs = TSVInput.fromTSV(in);
            return new SPARQLResult(rs);
        } else if (format.equals(ResultsFormat.FMT_RS_CSV)) {
            ResultSet rs = CSVInput.fromCSV(in);
            return new SPARQLResult(rs);
        } else if (format.equals(ResultsFormat.FMT_RS_BIO)) {
            ResultSet rs = BIOInput.fromBIO(in);
            return new SPARQLResult(rs);
        }
    }
    if (ResultsFormat.isRDFGraphSyntax(format)) {
        Model model = FileManager.get().loadModel(filenameOrURI);
        return new SPARQLResult(model);
    }
    Log.error(ResultSet.class, "Unknown result set syntax: " + format);
    return null;
}
Also used : InputStream(java.io.InputStream) Model(org.apache.jena.rdf.model.Model) NotFoundException(org.apache.jena.shared.NotFoundException)

Example 88 with Model

use of org.apache.jena.rdf.model.Model in project jena by apache.

the class AssemblerUtils method readAssemblerFile.

public static Model readAssemblerFile(String assemblerFile) {
    Model spec = null;
    try {
        spec = RDFDataMgr.loadModel(assemblerFile);
    } catch (Exception ex) {
        throw new ARQException("Failed reading assembler description: " + ex.getMessage());
    }
    spec.add(modelExtras);
    return spec;
}
Also used : ARQException(org.apache.jena.sparql.ARQException) Model(org.apache.jena.rdf.model.Model) TypeNotUniqueException(org.apache.jena.sparql.util.TypeNotUniqueException) ARQException(org.apache.jena.sparql.ARQException)

Example 89 with Model

use of org.apache.jena.rdf.model.Model in project jena by apache.

the class DescribeBNodeClosure method describe.

// Check all named graphs
@Override
public void describe(Resource r) {
    // Default model.
    Closure.closure(otherModel(r, dataset.getDefaultModel()), false, acc);
    // Find all the named graphs in which this resource
    // occurs as a subject.  Faster than iterating in the
    // names of graphs in the case of very large numbers
    // of graphs, few of which contain the resource, in
    // some kind of persistent storage.
    QuerySolutionMap qsm = new QuerySolutionMap();
    qsm.add("s", r);
    QueryExecution qExec = QueryExecutionFactory.create(query, dataset, qsm);
    ResultSet rs = qExec.execSelect();
    for (; rs.hasNext(); ) {
        QuerySolution qs = rs.next();
        String gName = qs.getResource("g").getURI();
        Model model = dataset.getNamedModel(gName);
        Resource r2 = otherModel(r, model);
        Closure.closure(r2, false, acc);
    }
    //        // Named graphs
    //        for ( Iterator<String> iter = dataset.listNames() ; iter.hasNext() ; )
    //        {
    //            String name = iter.next();
    //            Model model =  dataset.getNamedModel(name) ;
    //            Resource r2 = otherModel(r, model) ;
    //            Closure.closure(r2, false, acc) ;
    //        }
    Closure.closure(r, false, acc);
}
Also used : Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource)

Example 90 with Model

use of org.apache.jena.rdf.model.Model in project jena by apache.

the class QueryExecUtils method outputResultSet.

@SuppressWarnings("deprecation")
public static void outputResultSet(ResultSet results, Prologue prologue, ResultsFormat outputFormat) {
    // Proper ResultSet formats.
    Lang lang = ResultsFormat.convert(outputFormat);
    if (lang != null) {
        ResultSetMgr.write(System.out, results, lang);
        System.out.flush();
        return;
    }
    // Old way.
    boolean done = false;
    if (prologue == null)
        prologue = new Prologue(globalPrefixMap);
    if (outputFormat.equals(ResultsFormat.FMT_UNKNOWN))
        outputFormat = ResultsFormat.FMT_TEXT;
    if (outputFormat.equals(ResultsFormat.FMT_NONE) || outputFormat.equals(ResultsFormat.FMT_COUNT)) {
        int count = ResultSetFormatter.consume(results);
        if (outputFormat.equals(ResultsFormat.FMT_COUNT)) {
            System.out.println("Count = " + count);
        }
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_RDF_XML) || outputFormat.equals(ResultsFormat.FMT_RDF_N3) || outputFormat.equals(ResultsFormat.FMT_RDF_TTL)) {
        Model m = RDFOutput.encodeAsModel(results);
        m.setNsPrefixes(prologue.getPrefixMapping());
        m.setNsPrefix("rs", ResultSetGraphVocab.getURI());
        RDFDataMgr.write(System.out, m, Lang.TURTLE);
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_RS_XML)) {
        ResultSetFormatter.outputAsXML(System.out, results);
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_RS_JSON)) {
        ResultSetFormatter.outputAsJSON(System.out, results);
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_RS_SSE)) {
        ResultSetFormatter.outputAsSSE(System.out, results, prologue);
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_TEXT)) {
        ResultSetFormatter.out(System.out, results, prologue);
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_TUPLES)) {
        PlainFormat pFmt = new PlainFormat(System.out, prologue);
        ResultSetApply a = new ResultSetApply(results, pFmt);
        a.apply();
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_RS_CSV)) {
        ResultSetFormatter.outputAsCSV(System.out, results);
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_RS_TSV)) {
        ResultSetFormatter.outputAsTSV(System.out, results);
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_RS_BIO)) {
        ResultSetFormatter.outputAsBIO(System.out, results);
        done = true;
    }
    if (!done)
        System.err.println("Unknown format request: " + outputFormat);
    results = null;
    System.out.flush();
}
Also used : Prologue(org.apache.jena.sparql.core.Prologue) ResultSetApply(org.apache.jena.sparql.resultset.ResultSetApply) Model(org.apache.jena.rdf.model.Model) Lang(org.apache.jena.riot.Lang) PlainFormat(org.apache.jena.sparql.resultset.PlainFormat)

Aggregations

Model (org.apache.jena.rdf.model.Model)444 Test (org.junit.Test)177 BaseTest (org.apache.jena.atlas.junit.BaseTest)96 Resource (org.apache.jena.rdf.model.Resource)87 StringReader (java.io.StringReader)43 Dataset (org.apache.jena.query.Dataset)31 RDFReader (org.apache.jena.rdf.model.RDFReader)30 Property (org.apache.jena.rdf.model.Property)24 DatasetAccessor (org.apache.jena.query.DatasetAccessor)20 InfModel (org.apache.jena.rdf.model.InfModel)20 Node (org.apache.jena.graph.Node)19 Statement (org.apache.jena.rdf.model.Statement)18 RDFConnection (org.apache.jena.rdfconnection.RDFConnection)15 Reader (java.io.Reader)14 JsonString (org.apache.jena.atlas.json.JsonString)14 AbstractFusekiTest (org.apache.jena.fuseki.AbstractFusekiTest)14 FusekiTest (org.apache.jena.fuseki.FusekiTest)14 Graph (org.apache.jena.graph.Graph)14 JenaException (org.apache.jena.shared.JenaException)14 Triple (org.apache.jena.graph.Triple)13