Search in sources :

Example 21 with Dataset

use of org.apache.jena.query.Dataset in project jena by apache.

the class AbstractCompressedWholeFileQuadInputFormatTests method writeGoodTuples.

private void writeGoodTuples(OutputStream output, int num) {
    Dataset ds = DatasetFactory.createGeneral();
    Model m = ModelFactory.createDefaultModel();
    Resource currSubj = m.createResource("http://example.org/subjects/0");
    Property predicate = m.createProperty("http://example.org/predicate");
    for (int i = 0; i < num; i++) {
        if (i % 100 == 0) {
            ds.addNamedModel("http://example.org/graphs/" + (i / 100), m);
            m = ModelFactory.createDefaultModel();
        }
        if (i % 10 == 0) {
            currSubj = m.createResource("http://example.org/subjects/" + (i / 10));
        }
        m.add(currSubj, predicate, m.createTypedLiteral(i));
    }
    if (!m.isEmpty()) {
        ds.addNamedModel("http://example.org/graphs/extra", m);
    }
    this.writeTuples(ds, output);
}
Also used : Dataset(org.apache.jena.query.Dataset) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) Property(org.apache.jena.rdf.model.Property)

Example 22 with Dataset

use of org.apache.jena.query.Dataset in project jena by apache.

the class FusekiBuilder method buildDataService.

/** Build a DatasetRef starting at Resource svc */
private static DataService buildDataService(Resource svc, DatasetDescriptionRegistry dsDescMap) {
    if (log.isDebugEnabled())
        log.debug("Service: " + nodeLabel(svc));
    Resource datasetDesc = ((Resource) getOne(svc, "fu:dataset"));
    Dataset ds = getDataset(datasetDesc, dsDescMap);
    // In case the assembler included ja:contents
    DataService dataService = new DataService(ds.asDatasetGraph());
    addServiceEP(dataService, OperationName.Query, svc, pServiceQueryEP);
    addServiceEP(dataService, OperationName.Update, svc, pServiceUpdateEP);
    addServiceEP(dataService, OperationName.Upload, svc, pServiceUploadEP);
    addServiceEP(dataService, OperationName.GSP_R, svc, pServiceReadGraphStoreEP);
    addServiceEP(dataService, OperationName.GSP_RW, svc, pServiceReadWriteGraphStoreEP);
    addServiceEP(dataService, OperationName.Quads_R, svc, pServiceReadQuadsEP);
    addServiceEP(dataService, OperationName.Quads_RW, svc, pServiceReadWriteQuadsEP);
    // In the config file they are also implicit when using GSP.
    if (!dataService.getOperation(OperationName.GSP_RW).isEmpty() || !dataService.getOperation(OperationName.Quads_RW).isEmpty()) {
        dataService.addEndpoint(OperationName.Quads_RW, "");
    } else if (!dataService.getOperation(OperationName.GSP_R).isEmpty() || !dataService.getOperation(OperationName.Quads_R).isEmpty()) {
        dataService.addEndpoint(OperationName.Quads_R, "");
    }
    return dataService;
}
Also used : Dataset(org.apache.jena.query.Dataset) Resource(org.apache.jena.rdf.model.Resource) DataService(org.apache.jena.fuseki.server.DataService)

Example 23 with Dataset

use of org.apache.jena.query.Dataset in project jena by apache.

the class TestUtils method renameGraph.

/**
     * Renames a graph of a dataset producing a new dataset so as to not modify
     * the original dataset
     * 
     * @param ds
     *            Dataset
     * @param oldUri
     *            Old URI
     * @param newUri
     *            New URI
     * @return New Dataset
     */
public static Dataset renameGraph(Dataset ds, String oldUri, String newUri) {
    Dataset dest = DatasetFactory.createTxnMem();
    if (oldUri == null) {
        // Rename default graph
        dest.addNamedModel(newUri, ds.getDefaultModel());
    } else {
        // Copy across default graph
        dest.setDefaultModel(ds.getDefaultModel());
    }
    Iterator<String> uris = ds.listNames();
    while (uris.hasNext()) {
        String uri = uris.next();
        if (uri.equals(oldUri)) {
            // Rename named graph
            if (newUri == null) {
                dest.setDefaultModel(ds.getNamedModel(oldUri));
            } else {
                dest.addNamedModel(newUri, ds.getNamedModel(oldUri));
            }
        } else {
            // Copy across named graph
            dest.addNamedModel(uri, ds.getNamedModel(uri));
        }
    }
    return dest;
}
Also used : Dataset(org.apache.jena.query.Dataset)

Example 24 with Dataset

use of org.apache.jena.query.Dataset in project jena by apache.

the class FusekiCmd method processModulesAndArgs.

@Override
protected void processModulesAndArgs() {
    int x = 0;
    Logger log = Fuseki.serverLog;
    if (contains(argFusekiConfig))
        fusekiConfigFile = getValue(argFusekiConfig);
    ArgDecl assemblerDescDecl = new ArgDecl(ArgDecl.HasValue, "desc", "dataset");
    if (contains(argMem))
        x++;
    if (contains(argFile))
        x++;
    if (contains(assemblerDescDecl))
        x++;
    if (contains(argTDB))
        x++;
    if (contains(argMemTDB))
        x++;
    if (fusekiConfigFile != null) {
        if (x >= 1)
            throw new CmdException("Dataset specified on the command line but a configuration file also given.");
    } else {
        if (x != 1)
            throw new CmdException("Required: either --config=FILE or one of --mem, --file, --loc or --desc");
    }
    if (contains(argMem)) {
        log.info("Dataset: in-memory");
        dsg = DatasetGraphFactory.create();
    }
    if (contains(argFile)) {
        dsg = DatasetGraphFactory.create();
        // replace by RiotLoader after ARQ refresh.
        String filename = getValue(argFile);
        log.info("Dataset: in-memory: load file: " + filename);
        if (!FileOps.exists(filename))
            throw new CmdException("File not found: " + filename);
        Lang language = RDFLanguages.filenameToLang(filename);
        if (language == null)
            throw new CmdException("Can't guess language for file: " + filename);
        InputStream input = IO.openFile(filename);
        if (RDFLanguages.isQuads(language))
            RDFDataMgr.read(dsg, filename);
        else
            RDFDataMgr.read(dsg.getDefaultGraph(), filename);
    }
    if (contains(argMemTDB)) {
        log.info("TDB dataset: in-memory");
        dsg = TDBFactory.createDatasetGraph();
    }
    if (contains(argTDB)) {
        String dir = getValue(argTDB);
        if (Objects.equals(dir, Names.memName)) {
            log.info("TDB dataset: in-memory");
        } else {
            if (!FileOps.exists(dir))
                throw new CmdException("Directory not found: " + dir);
            log.info("TDB dataset: directory=" + dir);
        }
        dsg = TDBFactory.createDatasetGraph(dir);
    }
    // Otherwise
    if (contains(assemblerDescDecl)) {
        log.info("Dataset from assembler");
        Dataset ds = modDataset.createDataset();
        if (ds != null)
            dsg = ds.asDatasetGraph();
    }
    if (contains(argFusekiConfig)) {
        if (dsg != null)
            throw new CmdException("(internal error) Dataset specificed on the command line but a a configuration file also given.");
        fusekiConfigFile = getValue(argFusekiConfig);
    }
    if (contains(argPort)) {
        String portStr = getValue(argPort);
        try {
            port = Integer.parseInt(portStr);
        } catch (NumberFormatException ex) {
            throw new CmdException(argPort.getKeyName() + " : bad port number: " + portStr);
        }
    }
    if (contains(argMgtPort)) {
        String mgtPortStr = getValue(argMgtPort);
        try {
            mgtPort = Integer.parseInt(mgtPortStr);
        } catch (NumberFormatException ex) {
            throw new CmdException(argMgtPort.getKeyName() + " : bad port number: " + mgtPortStr);
        }
    }
    if (contains(argLocalhost))
        listenLocal = true;
    if (fusekiConfigFile == null && dsg == null)
        throw new CmdException("No dataset defined and no configuration file: " + argUsage);
    if (dsg != null) {
        if (getPositional().size() == 0)
            throw new CmdException("No dataset path name given");
        if (getPositional().size() > 1)
            throw new CmdException("Multiple dataset path names given");
        datasetPath = getPositionalArg(0);
        if (datasetPath.length() > 0 && !datasetPath.startsWith("/"))
            throw new CmdException("Dataset path name must begin with a /: " + datasetPath);
        allowUpdate = contains(argAllowUpdate);
    }
    if (contains(argTimeout)) {
        String str = getValue(argTimeout);
        ARQ.getContext().set(ARQ.queryTimeout, str);
    }
    if (contains(argJettyConfig)) {
        jettyConfigFile = getValue(argJettyConfig);
        if (!FileOps.exists(jettyConfigFile))
            throw new CmdException("No such file: " + jettyConfigFile);
    }
    if (contains(argBasicAuth)) {
        authConfigFile = getValue(argBasicAuth);
        if (!FileOps.exists(authConfigFile))
            throw new CmdException("No such file: " + authConfigFile);
    }
    if (contains(argHome)) {
        List<String> args = super.getValues(argHome);
        homeDir = args.get(args.size() - 1);
    }
    if (contains(argPages)) {
        List<String> args = super.getValues(argPages);
        pagesDir = args.get(args.size() - 1);
    }
    if (contains(argGZip)) {
        if (!hasValueOfTrue(argGZip) && !hasValueOfFalse(argGZip))
            throw new CmdException(argGZip.getNames().get(0) + ": Not understood: " + getValue(argGZip));
        enableCompression = super.hasValueOfTrue(argGZip);
    }
    if (contains(argUber))
        SPARQLServer.überServlet = true;
    if (contains(argGSP)) {
        SPARQLServer.überServlet = true;
        Fuseki.graphStoreProtocolPostCreate = true;
    }
}
Also used : CmdException(jena.cmd.CmdException) InputStream(java.io.InputStream) Dataset(org.apache.jena.query.Dataset) ArgDecl(jena.cmd.ArgDecl) Lang(org.apache.jena.riot.Lang) Logger(org.slf4j.Logger)

Example 25 with Dataset

use of org.apache.jena.query.Dataset in project jena by apache.

the class SPARQL_QueryGeneral method datasetFromDescriptionWeb.

/**
     * Construct a Dataset based on a dataset description. Loads graph from the
     * web.
     */
protected Dataset datasetFromDescriptionWeb(HttpAction action, DatasetDescription datasetDesc) {
    try {
        if (datasetDesc == null)
            return null;
        if (datasetDesc.isEmpty())
            return null;
        List<String> graphURLs = datasetDesc.getDefaultGraphURIs();
        List<String> namedGraphs = datasetDesc.getNamedGraphURIs();
        if (graphURLs.size() == 0 && namedGraphs.size() == 0)
            return null;
        Dataset dataset = DatasetFactory.create();
        // Look in cache for loaded graphs!!
        // ---- Default graph
        {
            Model model = ModelFactory.createDefaultModel();
            for (String uri : graphURLs) {
                if (uri == null || uri.equals(""))
                    throw new InternalErrorException("Default graph URI is null or the empty string");
                try {
                    GraphLoadUtils.loadModel(model, uri, MaxTriples);
                    action.log.info(format("[%d] Load (default graph) %s", action.id, uri));
                } catch (RiotException ex) {
                    action.log.info(format("[%d] Parsing error loading %s: %s", action.id, uri, ex.getMessage()));
                    ServletOps.errorBadRequest("Failed to load URL (parse error) " + uri + " : " + ex.getMessage());
                } catch (Exception ex) {
                    action.log.info(format("[%d] Failed to load (default) %s: %s", action.id, uri, ex.getMessage()));
                    ServletOps.errorBadRequest("Failed to load URL " + uri);
                }
            }
            dataset.setDefaultModel(model);
        }
        // ---- Named graphs
        if (namedGraphs != null) {
            for (String uri : namedGraphs) {
                if (uri == null || uri.equals(""))
                    throw new InternalErrorException("Named graph URI is null or the empty string");
                try {
                    Model model = ModelFactory.createDefaultModel();
                    GraphLoadUtils.loadModel(model, uri, MaxTriples);
                    action.log.info(format("[%d] Load (named graph) %s", action.id, uri));
                    dataset.addNamedModel(uri, model);
                } catch (RiotException ex) {
                    action.log.info(format("[%d] Parsing error loading %s: %s", action.id, uri, ex.getMessage()));
                    ServletOps.errorBadRequest("Failed to load URL (parse error) " + uri + " : " + ex.getMessage());
                } catch (Exception ex) {
                    action.log.info(format("[%d] Failed to load (named graph) %s: %s", action.id, uri, ex.getMessage()));
                    ServletOps.errorBadRequest("Failed to load URL " + uri);
                }
            }
        }
        return dataset;
    } catch (ActionErrorException ex) {
        throw ex;
    } catch (Exception ex) {
        action.log.info(format("[%d] SPARQL parameter error: " + ex.getMessage(), action.id, ex));
        ServletOps.errorBadRequest("Parameter error: " + ex.getMessage());
        return null;
    }
}
Also used : RiotException(org.apache.jena.riot.RiotException) Dataset(org.apache.jena.query.Dataset) Model(org.apache.jena.rdf.model.Model) InternalErrorException(org.apache.jena.atlas.lib.InternalErrorException) RiotException(org.apache.jena.riot.RiotException) InternalErrorException(org.apache.jena.atlas.lib.InternalErrorException)

Aggregations

Dataset (org.apache.jena.query.Dataset)725 Test (org.junit.Test)401 Model (org.apache.jena.rdf.model.Model)173 Ignore (org.junit.Ignore)93 URI (java.net.URI)80 Resource (org.apache.jena.rdf.model.Resource)70 QueryExecution (org.apache.jena.query.QueryExecution)53 ArrayList (java.util.ArrayList)46 QuerySolution (org.apache.jena.query.QuerySolution)46 ResultSet (org.apache.jena.query.ResultSet)45 WonNodeInformationService (won.protocol.service.WonNodeInformationService)28 Statement (org.apache.jena.rdf.model.Statement)26 Node (org.apache.jena.graph.Node)23 InputStream (java.io.InputStream)22 RDFNode (org.apache.jena.rdf.model.RDFNode)21 RdfUtils (won.protocol.util.RdfUtils)19 WonMessage (won.protocol.message.WonMessage)17 Literal (org.apache.jena.rdf.model.Literal)16 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)16 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)16