org.apache.jena.riot.system.StreamRDFLib Java Examples

The following examples show how to use org.apache.jena.riot.system.StreamRDFLib. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RDFStar2RDFTest.java    From RDFstarTools with Apache License 2.0 6 votes vote down vote up
protected Graph convertAndLoadIntoGraph( String filename )
{
    final String fullFilename = getClass().getResource("/TurtleStar/"+filename).getFile();
    String result;
    try( ByteArrayOutputStream os = new ByteArrayOutputStream() ) {
        new RDFStar2RDF().convert(fullFilename, os);
        result = os.toString();
    }
    catch ( IOException e ) {
        fail( "Closing the output stream failed: " + e.getMessage() );
        return null;
    }

	final StringReader reader = new StringReader(result);
       final Graph g = ModelFactory.createDefaultModel().getGraph();
       final StreamRDF dest = StreamRDFLib.graph(g);

       RDFParser.create()
                .source(reader)
                .lang(Lang.TURTLE)
                .parse(dest);

	return g;
}
 
Example #2
Source File: DatasetDeclarationPlan.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
private void addNamedGraph(Binding binding, Context context, DatasetGraph dsg, Expr sourceExpr) {
	String sourceURI = evalSourceURI(binding, context, sourceExpr);
	final String absURI = baseURI(sourceURI, baseURI);
	Dataset dataset = ContextUtils.getDataset(context);
	Node n = NodeFactory.createURI(absURI);
	Graph g = dsg.getGraph(n);
	if (g == null) {
		g = GraphFactory.createJenaDefaultGraph();
		dsg.addGraph(n, g);
	}
	// default: check the dataset
	if (dataset.containsNamedModel(absURI)) {
		Graph dg = dataset.getNamedModel(absURI).getGraph();
		GraphUtil.addInto(g, dg);
		return;
	}
	// fallback: load as RDF graph
	StreamRDF dest = StreamRDFLib.graph(g);
	ContextUtils.loadGraph(context, sourceURI, absURI, dest);
}
 
Example #3
Source File: RDFStarUtils.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
/**
 * Adds the RDF* data from the given Turtle* serialization
 * to the given {@link Graph}. 
 */
static public void populateGraphFromTurtleStarSnippet( Graph graph, String snippet )
{
	final StringReader reader = new StringReader(snippet);
    final StreamRDF dest = StreamRDFLib.graph(graph);

    RDFParser.create()
             .source(reader)
             .lang(LangTurtleStar.TURTLESTAR)
             .parse(dest);
}
 
Example #4
Source File: RdflintParserRdfxml.java    From rdflint with MIT License 5 votes vote down vote up
@Override
public void parse(Graph g, List<LintProblem> problems) {
  // validation
  Context context = new Context();

  List<LintProblem> parseProblemList = new LinkedList<>();
  ReaderRIOTRDFXML2 reader = new ReaderRIOTRDFXML2(
      new RdflintParserErrorHandler(parseProblemList),
      this.validators);

  ContentType ct = Lang.RDFXML.getContentType();
  InputStream validateIn = new ByteArrayInputStream(this.text.getBytes(StandardCharsets.UTF_8));
  try {
    reader.read(validateIn, baseUri, ct, StreamRDFLib.graph(g), context);

    if (!parseProblemList.isEmpty()) {
      problems.addAll(parseProblemList);
      return;
    }
    problems.addAll(reader.getDiagnosticList());

  } catch (Exception ex) {
    if (!parseProblemList.isEmpty()) {
      problems.addAll(parseProblemList);
      return;
    }
    String msg = ex.getMessage() != null ? ex.getMessage() : ex.toString();
    if (logger.isTraceEnabled()) {
      logger.trace("parse error: " + msg);
      problems.add(new LintProblem(
          LintProblem.ErrorLevel.ERROR,
          null,
          new LintProblemLocation(1, 1),
          null, msg));
    }
  }
}
 
Example #5
Source File: AbstractTestDeltaLink.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
public void datasource_init_01() {
    DeltaLink dLink = getLink();
    Id dsRef = dLink.newDataSource("datasource_15", "http://example/uri");
    assertEquals(1, dLink.listDatasets().size());
    DataSourceDescription dsd = dLink.getDataSourceDescriptionByURI("http://example/uri-not-present");
    String url = dLink.initialState(dsRef);
    assertNotNull(url);
    RDFDataMgr.parse(StreamRDFLib.sinkNull(), url);
}
 
Example #6
Source File: DatasetDeclarationPlan.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
private void addDefaultGraph(Binding binding, Context context, DatasetGraph dsg, Expr sourceExpr) {
		String sourceURI = evalSourceURI(binding, context, sourceExpr);
		final String absURI = baseURI(sourceURI, baseURI);
		// default: check the dataset
		Dataset dataset = ContextUtils.getDataset(context);
		if (dataset.containsNamedModel(absURI)) {
//			Node n = NodeFactory.createURI(absURI);
			Graph g = dataset.getNamedModel(absURI).getGraph();
			GraphUtil.addInto(dsg.getDefaultGraph(), g);
			return;
		}
		// fallback: load as RDF graph
		StreamRDF dest = StreamRDFLib.graph(dsg.getDefaultGraph());
		ContextUtils.loadGraph(context, sourceURI, absURI, dest);
	}
 
Example #7
Source File: load.java    From rdf-delta with Apache License 2.0 4 votes vote down vote up
public static void main(String...argv) {
    Logger LOG = LoggerFactory.getLogger("Load");

    if ( argv.length == 0 ) {
        System.err.println("Usage: load FILE...");
        System.exit(1);
    }

    String[] args = argv; //new String[] {"/home/afs/Datasets/BSBM/bsbm-5m.nt.gz"};

    String DIR = "ZoneX";
    String URL = "http://localhost:1066/";
    String DS  = "DS";

    FileOps.ensureDir(DIR);
    FileOps.clearDirectory(DIR);
    Zone zone = Zone.connect(Location.create(DIR));
    DeltaLink dLink = DeltaLinkHTTP.connect(URL);

    DeltaClient dClient = DeltaClient.create(zone, dLink);
    Id dsRef = dClient.createDataSource(DS, "http://example/"+DS, LocalStorageType.TDB, SyncPolicy.TXN_RW);

    long count = -99;
    Timer timer = new Timer();
    timer.startTimer();
    try ( DeltaConnection dConn = dClient.get(dsRef) ) {
        DatasetGraph dsg = dConn.getDatasetGraph();
        StreamRDF dest = StreamRDFLib.dataset(dsg);
        StreamRDFCounting cdest = StreamRDFLib.count(dest);
        Txn.executeWrite(dsg, ()->{
            for ( String fn : args ) {
                System.out.printf("File: %s\n", fn);
                RDFDataMgr.parse(cdest, fn);
            }
        });
        count = cdest.count();
    }
    long x = timer.endTimer();
    double seconds = x/1000.0;
    FmtLog.info(LOG, "Time  = %.2fs\n", seconds);
    FmtLog.info(LOG, "Count = %,d\n", count);
    FmtLog.info(LOG, "Rate  = %,.2f TPS\n", count/seconds);
}
 
Example #8
Source File: DatasetDeclarationPlan.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
private void loadGraph(Binding binding, Context context, SPARQLExtQuery generate, Graph graph) {
	QueryExecutor queryExecutor = ContextUtils.getQueryExecutor(context);
	StreamRDF dest = StreamRDFLib.graph(graph);
	Context newContext = ContextUtils.fork(context).setGenerateOutput(dest).fork();
	queryExecutor.execGenerateFromQuery(generate, binding, newContext);
}
 
Example #9
Source File: LangTurtleStarTest.java    From RDFstarTools with Apache License 2.0 3 votes vote down vote up
protected Graph loadGraphFromTurtleStarFile( String filename ) {

		final String fullFilename = getClass().getResource("/TurtleStar/"+filename).getFile();

        final Graph g = ModelFactory.createDefaultModel().getGraph();
        final StreamRDF dest = StreamRDFLib.graph(g);

		assertEquals( LangTurtleStar.TURTLESTAR, RDFLanguages.filenameToLang(fullFilename) );

        RDFParser.create()
                 .source( "file://" + fullFilename )
                 .parse(dest);

		return g;
	}