Java Code Examples for org.apache.jena.graph.Graph#close()

The following examples show how to use org.apache.jena.graph.Graph#close() . 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: RdfSyntaxValidator.java    From rdflint with MIT License 6 votes vote down vote up
@Override
public void validateFile(LintProblemSet problems, String path, String parentPath) {
  String baseUri = this.getParameters().getBaseUri();
  Graph g = Factory.createGraphMem();
  String filename = path.substring(parentPath.length() + 1);
  String subdir = filename.substring(0, filename.lastIndexOf('/') + 1);
  List<LintProblem> problemList = new LinkedList<>();
  try {
    RdflintParser.source(Paths.get(path))
        .base(baseUri + subdir)
        .parse(g, problemList);
  } catch (Exception ex) {
    if (problemList.isEmpty()) {
      problemList.add(new LintProblem(
          ErrorLevel.ERROR, this,
          new LintProblemLocation(1, 1),
          "parseError", ex.getMessage()));
    }
  }
  g.close();
  problemList.forEach(p -> problems.addProblem(filename, p));
}
 
Example 2
Source File: RdflintParserTurtleTest.java    From rdflint with MIT License 6 votes vote down vote up
@Test
public void testValidator() throws Exception {
  String rootPath = getTestRdfsPath() + "turtle/needtrim.ttl";

  List<RdfValidator> validators = new LinkedList<>();
  validators.add(new TrimValidator());
  Graph g = Factory.createGraphMem();
  List<LintProblem> problems = new LinkedList<>();
  RdflintParser.source(Paths.get(rootPath))
      .lang(Lang.TURTLE)
      .validators(validators)
      .parse(g, problems);

  assertFalse(g.size() == 0);
  g.close();
  assertFalse(problems.size() == 0);
  assertEquals(
      "com.github.imas.rdflint.validator.impl.needTrimLiteral",
      problems.get(0).getKey());
}
 
Example 3
Source File: RdflintParserRdfxmlTest.java    From rdflint with MIT License 6 votes vote down vote up
@Test
public void testInvalid() throws Exception {
  String rootPath = getTestRdfsPath() + "rdfxml/invalid.rdf";

  Graph g = Factory.createGraphMem();
  List<LintProblem> problems = new LinkedList<>();
  RdflintParser.source(Paths.get(rootPath))
      .lang(Lang.RDFXML)
      .base("http://example.com/")
      .parse(g, problems);

  g.close();
  assertFalse(problems.size() == 0);
  assertEquals(
      "com.github.imas.rdflint.validator.impl.parseWarning",
      problems.get(0).getKey());
  System.out.println(problems.get(0).getArguments()[0]);
}
 
Example 4
Source File: RdflintParserRdfxmlTest.java    From rdflint with MIT License 6 votes vote down vote up
@Test
public void testInvalidWhitespace() throws Exception {
  String rootPath = getTestRdfsPath() + "rdfxml/invalid_whitespace.rdf";

  Graph g = Factory.createGraphMem();
  List<LintProblem> problems = new LinkedList<>();
  RdflintParser.source(Paths.get(rootPath))
      .lang(Lang.RDFXML)
      .base("http://example.com/")
      .parse(g, problems);

  g.close();
  assertFalse(problems.size() == 0);
  assertEquals(
      "com.github.imas.rdflint.validator.impl.parseWarning",
      problems.get(0).getKey());
  System.out.println(problems.get(0).getArguments()[0]);
}
 
Example 5
Source File: RdflintParserRdfxmlTest.java    From rdflint with MIT License 6 votes vote down vote up
@Test
public void testValidator() throws Exception {
  String rootPath = getTestRdfsPath() + "rdfxml/needtrim.rdf";

  List<RdfValidator> validators = new LinkedList<>();
  validators.add(new TrimValidator());
  Graph g = Factory.createGraphMem();
  List<LintProblem> problems = new LinkedList<>();
  RdflintParser.source(Paths.get(rootPath))
      .lang(Lang.RDFXML)
      .base("http://example.com/")
      .validators(validators)
      .parse(g, problems);

  assertFalse(g.size() == 0);
  g.close();
  assertFalse(problems.size() == 0);
  assertEquals(
      "com.github.imas.rdflint.validator.impl.needTrimLiteral",
      problems.get(0).getKey());
}
 
Example 6
Source File: UndefinedSubjectValidator.java    From rdflint with MIT License 5 votes vote down vote up
private static Set<String> loadSubjects(RDFParserBuilder builder, String startswith, Lang lang) {
  Graph g = Factory.createGraphMem();
  try {
    builder.base(startswith).lang(lang).parse(g);
    return g.find().toList().stream()
        .filter(t -> t.getSubject().isURI())
        .map(t -> t.getSubject().getURI())
        .collect(Collectors.toSet());
  } catch (Exception ex) {
    logger.warn(String.format("loadSubjects: skip %s", startswith));
  } finally {
    g.close();
  }
  return null;
}
 
Example 7
Source File: RdflintParserTurtleTest.java    From rdflint with MIT License 5 votes vote down vote up
@Test
public void testValid() throws Exception {
  String rootPath = getTestRdfsPath() + "turtle/valid.ttl";

  Graph g = Factory.createGraphMem();
  List<LintProblem> problems = new LinkedList<>();
  RdflintParser.source(Paths.get(rootPath)).lang(Lang.TURTLE).parse(g, problems);

  assertFalse(g.size() == 0);
  g.close();
  assertEquals(0, problems.size());
}
 
Example 8
Source File: RdflintParserTurtleTest.java    From rdflint with MIT License 5 votes vote down vote up
@Test
public void testInvalid() throws Exception {
  String rootPath = getTestRdfsPath() + "turtle/invalid.ttl";

  Graph g = Factory.createGraphMem();
  List<LintProblem> problems = new LinkedList<>();
  RdflintParser.source(Paths.get(rootPath)).lang(Lang.TURTLE).parse(g, problems);

  g.close();
  assertFalse(problems.size() == 0);
  assertEquals(
      "com.github.imas.rdflint.validator.impl.parseWarning",
      problems.get(0).getKey());
}
 
Example 9
Source File: RdflintParserRdfxmlTest.java    From rdflint with MIT License 5 votes vote down vote up
@Test
public void testValid() throws Exception {
  String rootPath = getTestRdfsPath() + "rdfxml/valid.rdf";

  Graph g = Factory.createGraphMem();
  List<LintProblem> problems = new LinkedList<>();
  RdflintParser.source(Paths.get(rootPath))
      .lang(Lang.RDFXML)
      .base("http://example.com/")
      .parse(g, problems);

  assertFalse(g.size() == 0);
  g.close();
  assertEquals(0, problems.size());
}
 
Example 10
Source File: ExTDB6.java    From xcurator with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    /// turn off the "No BGP optimizer warning"
    TDB.setOptimizerWarningFlag(false);

    final IRIFactory iriFactory = IRIFactory.semanticWebImplementation();

    final String DATASET_DIR_NAME = "data0";
    final Dataset data0 = TDBFactory.createDataset( DATASET_DIR_NAME );

    // show the currently registered names
    for (Iterator<String> it = data0.listNames(); it.hasNext(); ) {
        out.println("NAME="+it.next());
    }

    out.println("getting named model...");
    /// this is the OWL portion
    final Model model = data0.getNamedModel( MY_NS );
    out.println("Model := "+model);

    out.println("getting graph...");
    /// this is the DATA in that MODEL
    final Graph graph = model.getGraph();
    out.println("Graph := "+graph);

    if (graph.isEmpty()) {
        final Resource product1 = model.createResource(
                iriFactory.construct( MY_NS +"product/1" )
                    .toString() );

        final Property hasName = model.createProperty( MY_NS, "#hasName");
        final Statement stmt = model.createStatement(
                product1, hasName, model.createLiteral("Beach Ball","en") );
        out.println("Statement = " + stmt);

        model.add(stmt);

        // just for fun
        out.println("Triple := " + stmt.asTriple().toString());
    } else {
        out.println("Graph is not Empty; it has "+graph.size()+" Statements");
        long t0, t1;
        t0 = System.currentTimeMillis();
        final Query q = QueryFactory.create(
                "PREFIX exns: <"+MY_NS+"#>\n"+
                "PREFIX exprod: <"+MY_NS+"product/>\n"+
                " SELECT * "
                // if you don't provide the Model to the
                // QueryExecutionFactory below, then you'll need
                // to specify the FROM;
                // you *can* always specify it, if you want
                // +" FROM <"+MY_NS+">\n"
                // +" WHERE { ?node <"+MY_NS+"#hasName> ?name }"
                // +" WHERE { ?node exns:hasName ?name }"
                // +" WHERE { exprod:1 exns:hasName ?name }"
                +" WHERE { ?res ?pred ?obj }"
                );
        out.println("Query := "+q);
        t1 = System.currentTimeMillis();
        out.println("QueryFactory.TIME="+(t1 - t0));

        t0 = System.currentTimeMillis();
        final QueryExecution qExec = QueryExecutionFactory
                // if you query the whole DataSet,
                // you have to provide a FROM in the SparQL
                //.create(q, data0);
                .create(q, model);
        t1 = System.currentTimeMillis();
        out.println("QueryExecutionFactory.TIME="+(t1 - t0));

        try {
            t0 = System.currentTimeMillis();
            ResultSet rs = qExec.execSelect();
            t1 = System.currentTimeMillis();
            out.println("executeSelect.TIME="+(t1 - t0));
            while (rs.hasNext()) {
                QuerySolution sol = rs.next();
                out.println("Solution := "+sol);
                for (Iterator<String> names = sol.varNames(); names.hasNext(); ) {
                    final String name = names.next();
                    out.println("\t"+name+" := "+sol.get(name));
                }
            }
        } finally {
            qExec.close();
        }
    }
    out.println("closing graph");
    graph.close();
    out.println("closing model");
    model.close();
    //out.println("closing DataSetGraph");
    //dsg.close();
    out.println("closing DataSet");
    data0.close();
}