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");
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
Aggregations