Search in sources :

Example 46 with Graph

use of org.apache.jena.graph.Graph in project jena by apache.

the class TestLPBRuleEngine method testSaturateTabledGoals.

@Test
public void testSaturateTabledGoals() throws Exception {
    final int MAX = 1024;
    // Set the cache size very small just for this test
    System.setProperty("jena.rulesys.lp.max_cached_tabled_goals", "" + MAX);
    try {
        Graph data = Factory.createGraphMem();
        data.add(new Triple(a, ty, C1));
        List<Rule> rules = Rule.parseRules("[r1:  (?x p ?t) <- (?x rdf:type C1), makeInstance(?x, p, C2, ?t)]" + "[r2:  (?t rdf:type C2) <- (?x rdf:type C1), makeInstance(?x, p, C2, ?t)]");
        FBRuleInfGraph infgraph = (FBRuleInfGraph) createReasoner(rules).bind(data);
        LPBRuleEngine engine = getEngineForGraph(infgraph);
        assertEquals(0, engine.activeInterpreters.size());
        assertEquals(0, engine.tabledGoals.size());
        // Let's ask about lots of unknown subjects
        for (int i = 0; i < MAX * 128; i++) {
            Node test = NodeFactory.createURI("test" + i);
            ExtendedIterator<Triple> it = infgraph.find(test, ty, C2);
            assertFalse(it.hasNext());
            it.close();
        }
        // Let's see how many were cached
        assertEquals(MAX, engine.tabledGoals.size());
        // and no leaks of activeInterpreters (this will happen if we forget
        // to call hasNext above)
        assertEquals(0, engine.activeInterpreters.size());
    } finally {
        System.clearProperty("jena.rulesys.lp.max_cached_tabled_goals");
    }
}
Also used : Triple(org.apache.jena.graph.Triple) FBRuleInfGraph(org.apache.jena.reasoner.rulesys.FBRuleInfGraph) FBRuleInfGraph(org.apache.jena.reasoner.rulesys.FBRuleInfGraph) Graph(org.apache.jena.graph.Graph) Node(org.apache.jena.graph.Node) Rule(org.apache.jena.reasoner.rulesys.Rule) Test(org.junit.Test)

Example 47 with Graph

use of org.apache.jena.graph.Graph in project jena by apache.

the class REST_Quads method doPostTriplesGSP.

protected void doPostTriplesGSP(HttpAction action, Lang lang) {
    action.beginWrite();
    try {
        DatasetGraph dsg = action.getActiveDSG();
        //log.info(format("[%d] ** Content-length: %d", action.id, action.request.getContentLength())) ;  
        String name = action.request.getRequestURL().toString();
        if (!name.endsWith("/"))
            name = name + "/";
        name = name + (++counter);
        Node gn = NodeFactory.createURI(name);
        Graph g = dsg.getGraph(gn);
        RDFDataMgr.read(g, action.request.getInputStream(), name, lang);
        log.info(format("[%d] Location: %s", action.id, name));
        action.response.setHeader("Location", name);
        action.commit();
        successCreated(action);
    } catch (IOException ex) {
        action.abort();
    } finally {
        action.endWrite();
    }
}
Also used : DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Graph(org.apache.jena.graph.Graph) Node(org.apache.jena.graph.Node) IOException(java.io.IOException) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Example 48 with Graph

use of org.apache.jena.graph.Graph in project jena by apache.

the class SPARQL_REST_R method doGet.

@Override
protected void doGet(HttpAction action) {
    // Assume success - do the set up before grabbing the lock.
    // Sets content type.
    MediaType mediaType = HttpAction.contentNegotationRDF(action);
    ServletOutputStream output;
    try {
        output = action.response.getOutputStream();
    } catch (IOException ex) {
        errorOccurred(ex);
        output = null;
    }
    TypedOutputStream out = new TypedOutputStream(output, mediaType);
    Lang lang = RDFLanguages.contentTypeToLang(mediaType.getContentType());
    if (action.verbose)
        log.info(format("[%d]   Get: Content-Type=%s, Charset=%s => %s", action.id, mediaType.getContentType(), mediaType.getCharset(), lang.getName()));
    action.beginRead();
    setCommonHeaders(action.response);
    try {
        Target target = determineTarget(action);
        if (log.isDebugEnabled())
            log.debug("GET->" + target);
        boolean exists = target.exists();
        if (!exists)
            errorNotFound("No such graph: <" + target.name + ">");
        // If we want to set the Content-Length, we need to buffer.
        //response.setContentLength(??) ;
        String ct = lang.getContentType().toHeaderString();
        action.response.setContentType(ct);
        Graph g = target.graph();
        //Special case RDF/XML to be the plain (faster, less readable) form
        RDFFormat fmt = (lang == Lang.RDFXML) ? RDFFormat.RDFXML_PLAIN : RDFWriterRegistry.defaultSerialization(lang);
        RDFDataMgr.write(out, g, fmt);
        success(action);
    } finally {
        action.endRead();
    }
}
Also used : Graph(org.apache.jena.graph.Graph) ServletOutputStream(javax.servlet.ServletOutputStream) MediaType(org.apache.jena.atlas.web.MediaType) TypedOutputStream(org.apache.jena.atlas.web.TypedOutputStream) IOException(java.io.IOException)

Example 49 with Graph

use of org.apache.jena.graph.Graph in project jena by apache.

the class SPARQL_REST_RW method addDataIntoTxn.

/** Directly add data in a transaction.
     * Assumes recovery from parse errors by transaction abort.
     * Return whether the target existed before.
     * @param action
     * @param cleanDest Whether to remove data first (true = PUT, false = POST)
     * @return whether the target existed beforehand
     */
protected static boolean addDataIntoTxn(HttpAction action, boolean overwrite) {
    action.beginWrite();
    Target target = determineTarget(action);
    boolean existedBefore = false;
    try {
        if (log.isDebugEnabled())
            log.debug("  ->" + target);
        existedBefore = target.exists();
        Graph g = target.graph();
        if (overwrite && existedBefore)
            clearGraph(target);
        StreamRDF sink = StreamRDFLib.graph(g);
        incomingData(action, sink);
        action.commit();
        return existedBefore;
    } catch (RiotException ex) {
        // Parse error
        action.abort();
        errorBadRequest(ex.getMessage());
        return existedBefore;
    } catch (Exception ex) {
        // Something else went wrong.  Backout.
        action.abort();
        errorOccurred(ex.getMessage());
        return existedBefore;
    } finally {
        action.endWrite();
    }
}
Also used : Graph(org.apache.jena.graph.Graph) RiotException(org.apache.jena.riot.RiotException) StreamRDF(org.apache.jena.riot.system.StreamRDF) RiotException(org.apache.jena.riot.RiotException) IOException(java.io.IOException)

Example 50 with Graph

use of org.apache.jena.graph.Graph in project jena by apache.

the class sdbload method loadOne.

private void loadOne(String filename, boolean replace) {
    Model model = null;
    Dataset dataset = null;
    PrefixMapping pmap;
    Lang lang = RDFLanguages.filenameToLang(filename);
    if (lang == null)
        throw new CmdException("Data syntax not recognized: " + filename);
    // --graph or not
    if (modGraph.getGraphName() != null) {
        model = modGraph.getModel(getStore());
        pmap = model;
    } else {
        dataset = SDBFactory.connectDataset(getStore());
        pmap = dataset.asDatasetGraph().getDefaultGraph().getPrefixMapping();
    }
    // For monitoring only.
    Graph monitorGraph = (model == null) ? null : model.getGraph();
    if (replace) {
        if (model != null)
            model.removeAll();
        else
            dataset.asDatasetGraph().clear();
    }
    boolean showProgress = isVerbose() || getModTime().timingEnabled();
    if (showProgress)
        output.print("Start load: %s", filename);
    StreamRDF stream = streamToStore(pmap, getStore());
    if (modGraph.getGraphName() != null) {
        Node gn = NodeFactory.createURI(modGraph.getGraphName());
        stream = StreamRDFLib.extendTriplesToQuads(gn, stream);
    }
    ProgressMonitor progress = null;
    if (showProgress) {
        progress = new ProgressMonitor(filename, 100_000, 10, output);
        stream = new ProgressStreamRDF(stream, progress);
    }
    if (progress != null)
        progress.start();
    // Load!
    RDFDataMgr.parse(stream, filename, lang);
    if (progress != null) {
        progress.finish();
        progress.finishMessage();
    }
}
Also used : ProgressMonitor(org.apache.jena.atlas.lib.ProgressMonitor) PrefixMapping(org.apache.jena.shared.PrefixMapping) Graph(org.apache.jena.graph.Graph) ModGraph(sdb.cmd.ModGraph) ProgressStreamRDF(org.apache.jena.riot.system.ProgressStreamRDF) CmdException(jena.cmd.CmdException) ProgressStreamRDF(org.apache.jena.riot.system.ProgressStreamRDF) StreamRDF(org.apache.jena.riot.system.StreamRDF) Dataset(org.apache.jena.query.Dataset) Node(org.apache.jena.graph.Node) Model(org.apache.jena.rdf.model.Model) Lang(org.apache.jena.riot.Lang)

Aggregations

Graph (org.apache.jena.graph.Graph)247 Test (org.junit.Test)90 BaseTest (org.apache.jena.atlas.junit.BaseTest)56 Triple (org.apache.jena.graph.Triple)56 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)54 Node (org.apache.jena.graph.Node)46 AbstractTestGraph (org.apache.jena.graph.test.AbstractTestGraph)16 InfGraph (org.apache.jena.reasoner.InfGraph)16 ContractTest (org.xenei.junit.contract.ContractTest)16 Model (org.apache.jena.rdf.model.Model)14 MultiUnion (org.apache.jena.graph.compose.MultiUnion)9 Delta (org.apache.jena.graph.compose.Delta)8 BuilderGraph (org.apache.jena.sparql.sse.builders.BuilderGraph)8 StreamRDF (org.apache.jena.riot.system.StreamRDF)7 PrefixMapping (org.apache.jena.shared.PrefixMapping)7 GraphUnionRead (org.apache.jena.sparql.graph.GraphUnionRead)7 IOException (java.io.IOException)6 Dataset (org.apache.jena.query.Dataset)6 Rule (org.apache.jena.reasoner.rulesys.Rule)6 ConfigTest (org.apache.jena.tdb.ConfigTest)6