org.apache.jena.graph.Graph Java Examples
The following examples show how to use
org.apache.jena.graph.Graph.
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: CustomQueryValidator.java From rdflint with MIT License | 6 votes |
@Override public void validateTripleSet(LintProblemSet problems, String file, List<Triple> tripeSet) { if (this.getParameters().getRules() == null) { return; } // execute sparql & custom validation Graph g = Factory.createGraphMem(); tripeSet.forEach(g::add); Model m = ModelFactory.createModelForGraph(g); this.getParameters().getRules().stream() .filter(r -> file.matches(r.getTarget())) .forEach(r -> { Query query = QueryFactory.create(r.getQuery()); QueryExecution qe = QueryExecutionFactory.create(query, m); Binding binding = new Binding(); binding.setVariable("rs", qe.execSelect()); binding.setVariable("log", new ProblemLogger(this, problems, file, r.getName())); GroovyShell shell = new GroovyShell(binding, new CompilerConfiguration()); shell.evaluate(r.getValid()); }); }
Example #2
Source File: SHACLUtil.java From shacl with Apache License 2.0 | 6 votes |
/** * Creates a shapes Model for a given input Model. * The shapes Model is the union of the input Model with all graphs referenced via * the sh:shapesGraph property (and transitive includes or shapesGraphs of those). * @param model the Model to create the shapes Model for * @return a shapes graph Model */ private static Model createShapesModel(Dataset dataset) { Model model = dataset.getDefaultModel(); Set<Graph> graphs = new HashSet<Graph>(); Graph baseGraph = model.getGraph(); graphs.add(baseGraph); for(Statement s : model.listStatements(null, SH.shapesGraph, (RDFNode)null).toList()) { if(s.getObject().isURIResource()) { String graphURI = s.getResource().getURI(); Model sm = dataset.getNamedModel(graphURI); graphs.add(sm.getGraph()); // TODO: Include includes of sm } } if(graphs.size() > 1) { MultiUnion union = new MultiUnion(graphs.iterator()); union.setBaseGraph(baseGraph); return ModelFactory.createModelForGraph(union); } else { return model; } }
Example #3
Source File: ClassMetadata.java From shacl with Apache License 2.0 | 6 votes |
private Object nearestObject(Graph graph, Function<ClassMetadata,Object> supplier, Set<Node> visited) { if(!visited.contains(classNode)) { Object result = supplier.apply(this); if(result != null) { return result; } visited.add(classNode); for(ClassMetadata superClass : getSuperClasses(graph)) { result = superClass.nearestObject(graph, supplier, visited); if(result != null) { return result; } } } return null; }
Example #4
Source File: RDF2RDFStarTest.java From RDFstarTools with Apache License 2.0 | 6 votes |
@Test public void nestedSubject() { final String filename = "NestedSubject.ttl"; final Graph g = convertAndLoadIntoGraph(filename); assertEquals( 1, g.size() ); final Triple t = g.find().next(); assertTrue( t.getSubject() instanceof Node_Triple ); assertFalse( t.getPredicate() instanceof Node_Triple ); assertFalse( t.getObject() instanceof Node_Triple ); assertEquals( DCTerms.source.asNode(), t.getPredicate() ); final Triple et = ( (Node_Triple) t.getSubject() ).get(); assertEquals( DCTerms.creator.asNode(), et.getPredicate() ); }
Example #5
Source File: ClassPropertyMetadata.java From shacl with Apache License 2.0 | 6 votes |
ClassPropertyMetadata(Node classNode, Node predicate, boolean inverse, Graph graph) { this.inverse = inverse; this.predicate = predicate; // Init from SHACL shapes if(SHACLUtil.exists(graph)) { if(JenaNodeUtil.isInstanceOf(classNode, SH.Shape.asNode(), graph)) { initFromShape(classNode, graph); } ExtendedIterator<Triple> it = graph.find(null, SH.targetClass.asNode(), classNode); while(it.hasNext()) { Node shape = it.next().getSubject(); initFromShape(shape, graph); } } if(!inverse) { for(Plugin plugin : plugins) { plugin.init(this, classNode, graph); } } }
Example #6
Source File: LangTurtleStarTest.java From RDFstarTools with Apache License 2.0 | 6 votes |
@Test public void stringParse6() { final String x = "<<<s> <p> <o>>> <p2> <o2> ; <p3> <o3> ."; final Graph g = RDFStarUtils.createGraphFromTurtleStarSnippet(x); assertEquals( 2, g.size() ); final Iterator<Triple> it = g.find(); final Triple t1 = it.next(); assertTrue( t1.getSubject() instanceof Node_Triple ); assertFalse( t1.getPredicate() instanceof Node_Triple ); assertFalse( t1.getObject() instanceof Node_Triple ); final Triple t2 = it.next(); assertTrue( t2.getSubject() instanceof Node_Triple ); assertFalse( t2.getPredicate() instanceof Node_Triple ); assertFalse( t2.getObject() instanceof Node_Triple ); final Triple et1 = ( (Node_Triple) t1.getSubject() ).get(); final Triple et2 = ( (Node_Triple) t2.getSubject() ).get(); assertTrue( et1 == et2 ); }
Example #7
Source File: ClassMetadata.java From shacl with Apache License 2.0 | 6 votes |
public synchronized Set<PathMetadata> getGroupPaths(Node group, Graph graph) { if(groupPaths == null) { groupPaths = new HashMap<>(); if(JenaNodeUtil.isInstanceOf(classNode, SH.Shape.asNode(), graph)) { addGroupProperties(classNode, graph, SH.parameter.asNode()); addGroupProperties(classNode, graph, SH.property.asNode()); } ExtendedIterator<Triple> it = graph.find(null, SH.targetClass.asNode(), classNode); while(it.hasNext()) { Node shape = it.next().getSubject(); addGroupProperties(shape, graph, SH.parameter.asNode()); addGroupProperties(shape, graph, SH.property.asNode()); } } return groupPaths.get(group); }
Example #8
Source File: LangTurtleStarTest.java From RDFstarTools with Apache License 2.0 | 6 votes |
@Test public void stringParse4() { final String x = "<< <s> <p> <<<s> <p> <o>>> >> <p2> <o2> ."; final Graph g = RDFStarUtils.createGraphFromTurtleStarSnippet(x); assertEquals( 1, g.size() ); final Triple t = g.find().next(); assertTrue( t.getSubject() instanceof Node_Triple ); assertFalse( t.getPredicate() instanceof Node_Triple ); assertFalse( t.getObject() instanceof Node_Triple ); final Triple et = ( (Node_Triple) t.getSubject() ).get(); assertFalse( et.getSubject() instanceof Node_Triple ); assertFalse( et.getPredicate() instanceof Node_Triple ); assertTrue( et.getObject() instanceof Node_Triple ); }
Example #9
Source File: RDF2RDFStarTest.java From RDFstarTools with Apache License 2.0 | 6 votes |
@Test public void nestedObject() { final String filename = "NestedObject.ttl"; final Graph g = convertAndLoadIntoGraph(filename); assertEquals( 1, g.size() ); final Triple t = g.find().next(); assertFalse( t.getSubject() instanceof Node_Triple ); assertFalse( t.getPredicate() instanceof Node_Triple ); assertTrue( t.getObject() instanceof Node_Triple ); final Triple et = ( (Node_Triple) t.getObject() ).get(); assertEquals( DCTerms.creator.asNode(), et.getPredicate() ); }
Example #10
Source File: TestRDFChangesGraph.java From rdf-delta with Apache License 2.0 | 6 votes |
private Graph replay() { IO.close(bout); final boolean DEBUG = false; if ( DEBUG ) { System.out.println("== Graph =="); RDFDataMgr.write(System.out, baseGraph, Lang.NQ); System.out.println("== Replay =="); String x = StrUtils.fromUTF8bytes(bout.toByteArray()); System.out.print(x); System.out.println("== =="); } // A completely separate graph (different dataset) Graph graph2 = txnGraph(); try(ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray())) { RDFPatchOps.applyChange(graph2, bin); if ( DEBUG ) { System.out.println("== Graph outcome =="); RDFDataMgr.write(System.out, graph2, Lang.NT); System.out.println("== =="); } return graph2; } catch (IOException ex) { IO.exception(ex); return null; } }
Example #11
Source File: DatasetLoader.java From rdflint with MIT License | 6 votes |
static Model loadRdfSet(RdfLintParameters params, String targetDir) throws IOException { String parentPath = new File(targetDir).getCanonicalPath(); String baseUri = params.getBaseUri(); Graph g = Factory.createGraphMem(); Files.walk(Paths.get(parentPath)) .filter(e -> e.toString().endsWith(".rdf") || e.toString().endsWith(".ttl")) .forEach(e -> { Graph gf = Factory.createGraphMem(); String filename = e.toString().substring(parentPath.length() + 1); String subdir = filename.substring(0, filename.lastIndexOf('/') + 1); RDFParser.source(e.toString()).base(baseUri + subdir).parse(gf); List<Triple> lst = gf.find().toList(); gf.close(); lst.forEach(g::add); gf.getPrefixMapping().getNsPrefixMap() .forEach((k, v) -> g.getPrefixMapping().setNsPrefix(k, v)); }); return ModelFactory.createModelForGraph(g); }
Example #12
Source File: RDF2RDFStarTest.java From RDFstarTools with Apache License 2.0 | 6 votes |
@Test public void doubleNestedObject() { final String filename = "DoubleNestedObject.ttl"; final Graph g = convertAndLoadIntoGraph(filename); assertEquals( 1, g.size() ); final Triple t = g.find().next(); assertFalse( t.getSubject() instanceof Node_Triple ); assertFalse( t.getPredicate() instanceof Node_Triple ); assertTrue( t.getObject() instanceof Node_Triple ); final Triple et = ( (Node_Triple) t.getObject() ).get(); assertFalse( et.getSubject() instanceof Node_Triple ); assertFalse( et.getPredicate() instanceof Node_Triple ); assertTrue( et.getObject() instanceof Node_Triple ); final Triple eet = ( (Node_Triple) et.getObject() ).get(); assertEquals( DCTerms.created.asNode(), eet.getPredicate() ); }
Example #13
Source File: RdflintParserRdfxmlTest.java From rdflint with MIT License | 6 votes |
@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 #14
Source File: DatasetDeclarationPlan.java From sparql-generate with Apache License 2.0 | 6 votes |
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 #15
Source File: RdflintParserTurtleTest.java From rdflint with MIT License | 6 votes |
@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 #16
Source File: JenaNodeUtil.java From shacl with Apache License 2.0 | 5 votes |
public static Node getObject(Node subject, Node predicate, Graph graph) { ExtendedIterator<Triple> it = graph.find(subject, predicate, Node.ANY); try { return it.hasNext() ? it.next().getObject() : null; } finally { it.close(); } }
Example #17
Source File: DatasetWrappingDatasetGraph.java From shacl with Apache License 2.0 | 5 votes |
@Override public Graph getGraph(Node graphNode) { try { Model model = dataset.getNamedModel(graphNode.getURI()); if(model != null) { return model.getGraph(); } else { return null; } } catch(Exception ex) { throw new IllegalArgumentException("Exception accessing named graph " + graphNode, ex); } }
Example #18
Source File: OntologyOptimizations.java From shacl with Apache License 2.0 | 5 votes |
public String getKeyIfEnabledFor(Graph graph) { if(enabled && graph instanceof OptimizedMultiUnion) { Graph baseGraph = JenaUtil.getBaseGraph(graph); if(baseGraph instanceof OntologyOptimizableGraph) { if(((OntologyOptimizableGraph)baseGraph).isUsingOntologyOptimizations()) { return ((OntologyOptimizableGraph)baseGraph).getOntologyGraphKey(); } } } return null; }
Example #19
Source File: JenaUtil.java From shacl with Apache License 2.0 | 5 votes |
public static void collectBaseGraphs(Graph graph, Set<Graph> baseGraphs) { if(graph instanceof MultiUnion) { MultiUnion union = (MultiUnion)graph; collectBaseGraphs(union.getBaseGraph(), baseGraphs); for(Object subGraph : union.getSubGraphs()) { collectBaseGraphs((Graph)subGraph, baseGraphs); } } else if(graph != null) { baseGraphs.add(graph); } }
Example #20
Source File: DeltaLinkEvents.java From rdf-delta with Apache License 2.0 | 5 votes |
/** * Enable graph events from applying patches to this dataset. * Depends on the DatasetGraph returning the same (or at least "right") * graph java object each time which is */ public static void enableGraphEvents(DeltaLink dLink, Id dsRef, DatasetGraph dsg) { DatasetGraph dsg2 = DSG.stableViewGraphs(dsg); Function<Node, Graph> router = gn-> (gn==null) ? dsg2.getDefaultGraph() : dsg2.getGraph(gn); enableGraphEvents(dLink, dsRef, router); }
Example #21
Source File: JenaNodeUtil.java From shacl with Apache License 2.0 | 5 votes |
public static List<Node> getSubjects(Node predicate, Node object, Graph graph) { List<Node> results = new LinkedList<>(); ExtendedIterator<Triple> it = graph.find(Node.ANY, predicate, object); while(it.hasNext()) { results.add(it.next().getSubject()); } return results; }
Example #22
Source File: OntologyOptimizations.java From shacl with Apache License 2.0 | 5 votes |
public ClassMetadata getClassMetadata(Node cls, Graph graph, String graphKey) { Object cacheKey = ClassMetadata.createKey(cls, graphKey); ClassMetadata classMetadata = (ClassMetadata)OntologyOptimizations.get().getObject(cacheKey); if(classMetadata == null) { classMetadata = new ClassMetadata(cls, graphKey); OntologyOptimizations.get().putObject(cacheKey, classMetadata); } return classMetadata; }
Example #23
Source File: EntityIterator.java From Stargraph with MIT License | 5 votes |
private Iterator<Node> createIterator() { Model model = core.getGraphModel(); Graph g = model.getGraph(); ExtendedIterator<Triple> exIt = g.find(Node.ANY, null, null); ExtendedIterator<Node> subjIt = exIt.mapWith(Triple::getSubject); exIt = g.find(null, null, Node.ANY); ExtendedIterator<Node> objIt = exIt.mapWith(Triple::getObject); return Iterators.concat(subjIt, objIt); }
Example #24
Source File: TestRDFChangesGraph.java From rdf-delta with Apache License 2.0 | 5 votes |
private static void check(Graph graph, Triple...quads) { if ( quads.length == 0 ) { assertTrue(graph.isEmpty()); return; } List<Triple> listExpected = Arrays.asList(quads); List<Triple> listActual = Iter.toList(graph.find()); assertEquals(listActual.size(), listExpected.size()); assertTrue(ListUtils.equalsUnordered(listExpected, listActual)); }
Example #25
Source File: RdflintParserTurtleTest.java From rdflint with MIT License | 5 votes |
@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 #26
Source File: UndefinedSubjectValidator.java From rdflint with MIT License | 5 votes |
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 #27
Source File: GraphWrapperStarTest.java From RDFstarTools with Apache License 2.0 | 5 votes |
@Test public void stringParse5_RedundancyAugmented() { final String x = "<<<s> <p> <o>>> <p2> <o2> , <o3> ."; final Graph g = RDFStarUtils.createRedundancyAugmentedGraphFromTurtleStarSnippet(x); assertEquals( 3, g.size() ); }
Example #28
Source File: JenaNodeUtil.java From shacl with Apache License 2.0 | 5 votes |
public static List<Node> getObjects(Node subject, Node predicate, Graph graph) { List<Node> results = new LinkedList<>(); ExtendedIterator<Triple> it = graph.find(subject, predicate, Node.ANY); while(it.hasNext()) { results.add(it.next().getObject()); } return results; }
Example #29
Source File: ClassMetadata.java From shacl with Apache License 2.0 | 5 votes |
public synchronized ClassPropertyMetadata getProperty(PathMetadata pm, Graph graph) { ClassPropertyMetadata result = properties.get(pm); if(result == null) { result = new ClassPropertyMetadata(classNode, pm.getPredicate(), pm.isInverse(), graph); properties.put(pm, result); } return result; }
Example #30
Source File: QueryIterTripleStarPatternTest.java From RDFstarTools with Apache License 2.0 | 5 votes |
protected Graph createTestGraph() { final String ttlsString = "@prefix ex: <http://example.com/> ." + "<< ex:s ex:p ex:o >> ex:m1 ex:x1 . " + "<< ex:s ex:p ex:o >> ex:m1 ex:x2 . " + "ex:x1 ex:m1 << ex:s ex:p ex:o >> . " + "ex:x2 ex:m2 << ex:s ex:p ex:o >> . "; return RDFStarUtils.createRedundancyAugmentedGraphFromTurtleStarSnippet(ttlsString); }