public static String str(Triple t) { return serialize(t.getSubject()) + " " + serialize(t.getPredicate()) + " " + serialize(t.getObject()); }
/** * Returns a collection of RDFStatements that match the described subject * * @param subject Subject * @return collection of RDFStatements */ public Collection<RDFStatement> getStatements(String subject) { if (subject == null || subject.isEmpty()) { return null; } List<RDFStatement> statement = null; try { // define a describe query String query = String.format("DESCRIBE %s FROM < %s >", subject, this.graphName); logger.debug("Query: \n{}", query); Query sparqlQuery = QueryFactory.create(query); VirtuosoQueryExecution vqe = VirtuosoQueryExecutionFactory.create(sparqlQuery, this.graph); // execute the query and get the graph Model model = vqe.execDescribe(); Graph queriedGraph = model.getGraph(); // itreate over the retrieved triples, and place them inside a list ExtendedIterator<Triple> iter = queriedGraph.find(Node.ANY, Node.ANY, Node.ANY); statement = new ArrayList<>(); while (iter.hasNext()) { Triple t = (Triple) iter.next(); RDFStatement stmt = new RDFStatement( t.getSubject().toString(), t.getPredicate().toString(), t.getObject().toString()); statement.add(stmt); } } catch (Exception ex) { logger.error("Exception occured while querying for statements", ex); } return statement; }
@Override public Op transform(OpQuadPattern quadPattern) { Node gNode = quadPattern.getGraphNode(); Node g = substitute(gNode, binding); BasicPattern triples = new BasicPattern(); for (Triple triple : quadPattern.getBasicPattern()) { Node s = substitute(triple.getSubject(), binding); Node p = substitute(triple.getPredicate(), binding); Node o = substitute(triple.getObject(), binding); Triple t = new Triple(s, p, o); triples.add(t); } // Pure quading. // for ( Iterator iter = quadPattern.getQuads().iterator() ; // iter.hasNext() ; ) // { // Quad quad = (Quad)iter.next() ; // if ( ! quad.getGraph().equals(gNode) ) // throw new // ARQInternalErrorException("Internal error: quads block is not uniform over the graph node") // ; // Node s = substitute(quad.getSubject(), binding) ; // Node p = substitute(quad.getPredicate(), binding) ; // Node o = substitute(quad.getObject(), binding) ; // Triple t = new Triple(s, p, o) ; // triples.add(t) ; // } return new OpQuadPattern(g, triples); }
public static void outputPlain(IndentedWriter out, Triple triple, SerializationContext naming) { // No tag output(out, triple.getSubject(), naming); out.print(" "); output(out, triple.getPredicate(), naming); out.print(" "); output(out, triple.getObject(), naming); }
@Override public void addGraph(Node gn, Graph g) { // Convert to quads. // super.addGraph(gn, g) ; ExtendedIterator<Triple> iter = g.find(Node.ANY, Node.ANY, Node.ANY); for (; iter.hasNext(); ) { Triple t = iter.next(); add(gn, t.getSubject(), t.getPredicate(), t.getObject()); } }
protected static String sparqlTriple(Triple triple) { StringBuffer serializedTriple = new StringBuffer(); serializedTriple.append(sparqlNodeUpdate(triple.getSubject(), "")); serializedTriple.append(" "); serializedTriple.append(sparqlNodeUpdate(triple.getPredicate(), "")); serializedTriple.append(" "); serializedTriple.append(sparqlNodeUpdate(triple.getObject(), "")); serializedTriple.append(" ."); return serializedTriple.toString(); }
public DBPattern(Triple pat, Mapping varMap) { pattern = pat; sources = new ArrayList(); isBusy = false; isConnected = false; isStmt = isReif = false; S = nodeToElement(pattern.getSubject(), varMap); P = nodeToElement(pattern.getPredicate(), varMap); O = nodeToElement(pattern.getObject(), varMap); Scost = elementCost(S); Pcost = elementCost(P); Ocost = elementCost(O); }
ReificationStatementMask(Triple t) { mask = HasNada; Node p = t.getPredicate(); if (p != null) { if (p.equals(RDF.Nodes.subject)) mask = HasSubj; else if (p.equals(RDF.Nodes.predicate)) mask = HasPred; else if (p.equals(RDF.Nodes.object)) mask = HasObj; else if (p.equals(RDF.Nodes.type)) { Node o = t.getObject(); if (o.equals(RDF.Nodes.Statement)) mask = HasType; } } }
public void testDeleteThenNextThrowsCME() { TripleBunch b = getBunch(); b.add(Triple.create("a P b")); b.add(Triple.create("c Q d")); Iterator it = b.iterator(); it.next(); b.remove(Triple.create("a P b")); try { it.next(); fail("should have thrown ConcurrentModificationException"); } catch (ConcurrentModificationException e) { pass(); } }
public NTriplesFileLuceneSyntacticIndexCreator( InputStream nTriplesStream, String indexPath, String searchField) throws IOException { // setup the index Directory directory = FSDirectory.open(new File(indexPath)); // setup the index analyzer Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_43); IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_43, analyzer); indexWriterConfig.setRAMBufferSizeMB(1024.0); indexWriterConfig.setOpenMode(OpenMode.CREATE); IndexWriter writer = new IndexWriter(directory, indexWriterConfig); System.out.println("Creating index ..."); // setup the index fields, here two fields, for URI and text FieldType stringType = new FieldType(StringField.TYPE_STORED); stringType.setStoreTermVectors(false); FieldType textType = new FieldType(TextField.TYPE_STORED); textType.setStoreTermVectors(false); Set<Document> documents = new HashSet<Document>(); Iterator<Triple> iterator = RiotReader.createIteratorTriples(nTriplesStream, Lang.NTRIPLES, null); Triple triple; String text; String uri; Document doc; int i = 0; while (iterator.hasNext()) { triple = iterator.next(); uri = triple.getSubject().getURI(); text = triple.getObject().getLiteralLexicalForm(); doc = new Document(); doc.add(new Field("uri", uri, stringType)); doc.add(new Field(searchField, text, textType)); writer.addDocument(doc); if (i++ % 10000 == 0) { // writer.commit(); System.out.println(i); } } writer.commit(); writer.close(); }
/* @see com.hp.hpl.jena.sparql.syntax.ElementVisitorBase#visit(com.hp.hpl.jena.sparql.syntax.ElementTriplesBlock) */ @Override public void visit(final ElementTriplesBlock elementtriplesblock) { for (final Triple t : elementtriplesblock.getPattern()) { try { if (t.getPredicate().isVariable()) throw new IllegalArgumentException( "Variables cannot be used in predicate position in ABoxQuery"); Atom atom = null; final String predURI = t.getPredicate().getURI(); if (RDF.type.getURI().equals(predURI)) { if (t.getObject().isVariable()) throw new IllegalArgumentException( "Variables cannot be used as objects of rdf:type triples in ABoxQuery"); final OWLClass c = owlModel.createClass(new URI(t.getObject().getURI())); final SWRLIndividualObject arg = makeIndividalObject(t.getSubject()); atom = swrlFactory.createClassAtom(c, arg); } else { final OWLProperty p = owlModel.getProperty(new URI(predURI)); if (p == null) throw new IllegalArgumentException( predURI + " is unknown [Object|Datatype]Property in the backing OWL model."); else if (p.isDatatypeProperty()) { final OWLDataProperty dp = owlModel.createDataProperty(p.getURI()); final SWRLIndividualObject arg1 = makeIndividalObject(t.getSubject()); final SWRLDataObject arg2 = makeDataObject(t.getObject()); atom = swrlFactory.createDataPropertyAtom(dp, arg1, arg2); } else if (p.isObjectProperty()) { final OWLObjectProperty op = owlModel.createObjectProperty(p.getURI()); final SWRLIndividualObject arg1 = makeIndividalObject(t.getSubject()); final SWRLIndividualObject arg2 = makeIndividalObject(t.getObject()); atom = swrlFactory.createIndividualPropertyAtom(op, arg1, arg2); } // else it could be a annotation property, which are not relevant anyway } if (atom != null) atoms = atoms.cons(atom); } catch (final URISyntaxException e) { throw new IllegalArgumentException( e.getInput() + " appearing in the query string is not a valid URI!"); } } }
@Override public void remove(List<Statement> statements) { Model graph = null; GraphConnection graphConnection = null; try { graphConnection = openGraph(); graph = graphConnection.getGraph(); graph.enterCriticalSection(Lock.WRITE); for (Statement nuxStmt : statements) { com.hp.hpl.jena.graph.Node subject = getJenaNode(nuxStmt.getSubject()); com.hp.hpl.jena.graph.Node predicate = getJenaNode(nuxStmt.getPredicate()); com.hp.hpl.jena.graph.Node object = getJenaNode(nuxStmt.getObject()); Triple jenaTriple = Triple.create(subject, predicate, object); com.hp.hpl.jena.rdf.model.Statement jenaStmt = graph.asStatement(jenaTriple); graph.remove(jenaStmt); // remove properties RSIterator it = graph.listReifiedStatements(jenaStmt); while (it.hasNext()) { ReifiedStatement rs = it.nextRS(); rs.removeProperties(); } // remove quadlets graph.removeAllReifications(jenaStmt); // graph.removeReification(reifiedStmt); } } finally { if (graph != null) { graph.leaveCriticalSection(); } if (graphConnection != null) { graphConnection.close(); } } }
public static String RenderTemplatePage(Bindings b, String templateName) throws IOException { MediaType mt = MediaType.TEXT_HTML; Resource config = model.createResource("eh:/root"); Mode prefixMode = Mode.PreferPrefixes; ShortnameService sns = new StandardShortnameService(); List<Resource> noResults = CollectionUtils.list(root.inModel(model)); Graph resultGraph = graphModel.getGraph(); resultGraph.getPrefixMapping().setNsPrefix("api", API.NS); resultGraph.add(Triple.create(root.asNode(), API.items.asNode(), RDF.nil.asNode())); APIResultSet rs = new APIResultSet(resultGraph, noResults, true, true, "details", View.ALL); VelocityRenderer vr = new VelocityRenderer(mt, null, config, prefixMode, sns); VelocityRendering vx = new VelocityRendering(b, rs, vr); VelocityEngine ve = vx.createVelocityEngine(); VelocityContext vc = vx.createVelocityContext(b); ByteArrayOutputStream bos = new ByteArrayOutputStream(); Writer w = new OutputStreamWriter(bos, "UTF-8"); Template t = ve.getTemplate(templateName); t.merge(vc, w); w.close(); return bos.toString(); }
@Override public void augmentBlock( TripleCollector arg0, String annotationBodyVar, String annotationHeadVar) { // This assumes subject indexing arg0.addTriple( Triple.create(Var.alloc(annotationBodyVar), TEXTMATCH, Node.createLiteral(searchTerm))); }
private static int getIndex(Triple triple) { String u = triple.getPredicate().getURI(); // Must be _nnn. Matcher m = pattern.matcher(u); if (!m.find()) return NOT_FOUND; String index = m.group(1); return Integer.parseInt(index); }
/** * Return an equivalent triple pattern with standardized variable names (?s, ?p, ?o). This * method is needed to ensure that functionally equivalent triple patterns do not get assigned * to different entries in the cache. * * @param triplePattern * @return a Triple that is equivalent to triplePattern, but with standard variable names */ protected Triple standardizeVarNames(Triple triplePattern) { Node s = triplePattern.getSubject(); Node p = triplePattern.getPredicate(); Node o = triplePattern.getObject(); // make the key consistent, regardless of user's chosen variable names if (s.isVariable()) { s = NodeCreateUtils.create("?s"); } if (p.isVariable()) { p = NodeCreateUtils.create("?p"); } if (o.isVariable()) { o = NodeCreateUtils.create("?o"); } return new Triple(s, p, o); }
@Override public void add(List<Statement> statements) { Model graph = null; GraphConnection graphConnection = null; try { graphConnection = openGraph(); graph = graphConnection.getGraph(); graph.enterCriticalSection(Lock.WRITE); for (Statement nuxStmt : statements) { com.hp.hpl.jena.graph.Node subject = getJenaNode(nuxStmt.getSubject()); com.hp.hpl.jena.graph.Node predicate = getJenaNode(nuxStmt.getPredicate()); com.hp.hpl.jena.graph.Node object = getJenaNode(nuxStmt.getObject()); Triple jenaTriple = Triple.create(subject, predicate, object); com.hp.hpl.jena.rdf.model.Statement jenaStmt = graph.asStatement(jenaTriple); // properties Map<Resource, Node[]> properties = nuxStmt.getProperties(); if (properties == null || properties.isEmpty()) { // no properties graph.add(jenaStmt); } else { List<com.hp.hpl.jena.rdf.model.Statement> stmts = new ArrayList<com.hp.hpl.jena.rdf.model.Statement>(); stmts.add(jenaStmt); // create reified statement if it does not exist com.hp.hpl.jena.graph.Node reifiedStmt = graph.getAnyReifiedStatement(jenaStmt).asNode(); for (Map.Entry<Resource, Node[]> property : properties.entrySet()) { com.hp.hpl.jena.graph.Node prop = getJenaNode(property.getKey()); for (Node node : property.getValue()) { com.hp.hpl.jena.graph.Node value = getJenaNode(node); Triple propTriple = Triple.create(reifiedStmt, prop, value); stmts.add(graph.asStatement(propTriple)); } } graph.add(stmts); } } } finally { if (graph != null) { graph.leaveCriticalSection(); } if (graphConnection != null) { graphConnection.close(); } } }
public void testAddDuringAppThrowsCME() { final TripleBunch b = getBunch(); b.add(Triple.create("a P b")); b.add(Triple.create("c Q d")); StageElement se = new StageElement() { public void run(Domain current) { b.add(Triple.create("S P O")); } }; try { b.app(new Domain(0), se, mob); fail(" should throw CME"); } catch (ConcurrentModificationException e) { pass(); } }
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("Triple Bindings are: \n"); Set<Triple> triples = this.bindingMap.keySet(); for (Triple triple : triples) { sb.append("* " + triple.toString() + "\n"); for (TripleMap tm : this.bindingMap.get(triple)) { sb.append(" Triplemap: " + tm + "\n"); for (PO po : tm.getPos()) { sb.append( " PO:" + po.getPredicate().toString() + " " + po.getObject().toString() + "\n"); } } } return sb.toString(); }
protected ElementGroup getWhereClauseWithBlankNodeFilter(Triple triplePattern) { Node s = triplePattern.getSubject(); Node o = triplePattern.getObject(); ElementGroup whereClause = new ElementGroup(); whereClause.addTriplePattern(triplePattern); if (s.isVariable()) { whereClause.addElementFilter( new ElementFilter(new E_LogicalNot(new E_IsBlank(new ExprVar(s))))); } if (o.isVariable()) { whereClause.addElementFilter( new ElementFilter(new E_LogicalNot(new E_IsBlank(new ExprVar(o))))); } return whereClause; }
public static Triple substitute(Triple triple, Binding binding, int index) { if (isNotNeeded(binding)) return triple; Node s = triple.getSubject(); Node p = triple.getPredicate(); Node o = triple.getObject(); Node s1 = substitute(s, binding); Node p1 = substitute(p, binding); Node o1 = substitute(o, binding); Triple t = triple; s1 = generateCountableNode(index, s, s1); p1 = generateCountableNode(index, p, p1); o1 = generateCountableNode(index, o, o1); t = new Triple(s1, p1, o1); return t; }
public static Collection<Node> containerMembers(Graph graph, Node container, Node containerType) { if (!isContainer(graph, container, containerType)) return null; ExtendedIterator<Triple> iter = graph.find(container, Node.ANY, Node.ANY); SortedMap<Integer, Node> triples = new TreeMap<Integer, Node>(order); try { for (; iter.hasNext(); ) { Triple t = iter.next(); int index = getIndex(t); if (index == NOT_FOUND) continue; // Insert triples.put(new Integer(index), t.getObject()); } } finally { iter.close(); } return triples.values(); }
public OntModel applyRenamings() { OntModel result = ModelFactory.createMem(); Graph graph = result.getGraph(); Iterator it = model.getGraph().find(Triple.ANY); while (it.hasNext()) { Triple t = (Triple) it.next(); Node s = t.getSubject().isURI() ? rename(t.getSubject()) : null; Node o = t.getObject().isURI() ? rename(t.getObject()) : null; if (s != null || o != null) t = Triple.create( s != null ? s : t.getSubject(), t.getPredicate(), o != null ? o : t.getObject()); graph.add(t); } return result; }
public SPDXChecksum(Model spdxModel, Node checksumNode) throws InvalidSPDXAnalysisException { this.model = spdxModel; this.checksumNode = checksumNode; if (checksumNode.isBlank()) { checksumResource = model.createResource(checksumNode.getBlankNodeId()); } else if (checksumNode.isURI()) { checksumResource = model.createResource(checksumNode.getURI()); } else { throw (new InvalidSPDXAnalysisException("Checksum node can not be a literal")); } // Algorithm Node p = spdxModel .getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_CHECKSUM_ALGORITHM) .asNode(); Triple m = Triple.createMatch(checksumNode, p, null); ExtendedIterator<Triple> tripleIter = spdxModel.getGraph().find(m); while (tripleIter.hasNext()) { Triple t = tripleIter.next(); if (t.getObject().isLiteral()) { // The following is for compatibility with rdf generated with older // versions of the tool this.algorithm = t.getObject().toString(false); } else if (t.getObject().isURI()) { this.algorithm = URI_TO_ALGORITHM.get(t.getObject().getURI()); if (this.algorithm == null) { this.algorithm = "UNKNOWN"; } } else { throw (new InvalidSPDXAnalysisException( "Invalid checksum algorithm - must be one of the defined algorithms supported by SPDX.")); } } // value p = spdxModel .getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_CHECKSUM_VALUE) .asNode(); m = Triple.createMatch(checksumNode, p, null); tripleIter = spdxModel.getGraph().find(m); while (tripleIter.hasNext()) { Triple t = tripleIter.next(); this.value = t.getObject().toString(false); } }
private static RdfStream getRdfStreamFromResource(final String resourcePath, final Lang lang) { final Model model = createDefaultModel(); RDFDataMgr.read(model, WebACRolesProviderTest.class.getResourceAsStream(resourcePath), lang); final List<Triple> triples = new ArrayList<>(); model .listStatements() .forEachRemaining( x -> { final Triple t = x.asTriple(); if (t.getObject().isURI() && t.getObject().getURI().startsWith(FEDORA_URI_PREFIX)) { triples.add( new Triple( t.getSubject(), t.getPredicate(), createURI( FEDORA_PREFIX + t.getObject().getURI().substring(FEDORA_URI_PREFIX.length())))); } else { triples.add(t); } }); return new RdfStream(triples); }
@Override public void delete(Graph g) { Model[] model = separateStatementsWithBlankNodes(g); deleteModel(model[1] /*statements without blank nodes*/); // replace blank nodes in remaining statements with variables StringBuffer patternBuff = new StringBuffer(); Iterator<Triple> tripIt = g.find(null, null, null); while (tripIt.hasNext()) { Triple t = tripIt.next(); patternBuff.append(SparqlGraph.sparqlNodeDelete(t.getSubject(), null)); patternBuff.append(" "); patternBuff.append(SparqlGraph.sparqlNodeDelete(t.getPredicate(), null)); patternBuff.append(" "); patternBuff.append(SparqlGraph.sparqlNodeDelete(t.getObject(), null)); patternBuff.append(" .\n"); } StringBuffer queryBuff = new StringBuffer(); String graphURI = graph.getGraphURI(); queryBuff.append( "DELETE { " + ((graphURI != null) ? "GRAPH <" + graphURI + "> { " : "") + " \n"); queryBuff.append(patternBuff); if (graphURI != null) { queryBuff.append(" } \n"); } queryBuff.append("} WHERE { \n"); if (graphURI != null) { queryBuff.append(" GRAPH <" + graphURI + "> { \n"); } queryBuff.append(patternBuff); if (graphURI != null) { queryBuff.append(" } \n"); } queryBuff.append("} \n"); log.debug(queryBuff.toString()); graph.executeUpdate(queryBuff.toString()); }
private static int countContainerMember( Graph graph, Node container, Node containerType, Node member, boolean stopEarly) { if (graph == null) { Log.warn(GraphContainerUtils.class, "containerMember called with null graph"); return 0; } if (container == null) { Log.warn(GraphContainerUtils.class, "containerMember called with null list"); return 0; } if (member == null) { Log.warn(GraphContainerUtils.class, "containerMember called with null member"); return 0; } if (!isContainer(graph, container, containerType)) return 0; int count = 0; ExtendedIterator<Triple> iter = graph.find(container, Node.ANY, member); try { for (; iter.hasNext(); ) { Triple t = iter.next(); Node p = t.getPredicate(); String u = p.getURI(); if (u.matches(membershipPattern)) { count++; if (stopEarly) return count; } } } finally { iter.close(); } return count; }
public SPhraseSpec getNLForTriple(Triple t) { SPhraseSpec p = nlgFactory.createClause(); // process predicate then return subject is related to if (t.getPredicate().isVariable()) { if (t.getSubject().isVariable()) { p.setSubject(t.getSubject().toString()); } else { p.setSubject(getNPPhrase(t.getSubject().toString(), false)); } p.setVerb("be related via " + t.getPredicate().toString() + " to"); if (t.getObject().isVariable()) { p.setObject(t.getObject().toString()); } else { p.setObject(getNPPhrase(t.getObject().toString(), false)); } } else { NLGElement subj; if (t.getSubject().isVariable()) { subj = nlgFactory.createWord(t.getSubject().toString(), LexicalCategory.NOUN); } else { subj = nlgFactory.createWord( uriConverter.convert(t.getSubject().toString()), LexicalCategory.NOUN); } // subj.setFeature(Feature.POSSESSIVE, true); // PhraseElement np = nlgFactory.createNounPhrase(subj, // getEnglishLabel(t.getPredicate().toString())); p.setSubject( realiser.realise(subj) + "\'s " + uriConverter.convert(t.getPredicate().toString())); p.setVerb("be"); if (t.getObject().isVariable()) { p.setObject(t.getObject().toString()); } else if (t.getObject().isLiteral()) { p.setObject(t.getObject().getLiteralLexicalForm()); } else { p.setObject(getNPPhrase(t.getObject().toString(), false)); } } p.setFeature(Feature.TENSE, Tense.PRESENT); return p; }
public SPhraseSpec getNLForTriple2(Triple t) { SPhraseSpec p = nlgFactory.createClause(); // process subject if (t.getSubject().isVariable()) { p.setSubject(t.getSubject().toString()); } else { p.setSubject(getNPPhrase(t.getSubject().toString(), false)); } // process predicate if (t.getPredicate().isVariable()) { p.setVerb("be related via " + t.getPredicate().toString() + " to"); } else { p.setVerb(getVerbFrom(t.getPredicate())); } // process object if (t.getObject().isVariable()) { p.setObject(t.getObject().toString()); } else if (t.getObject().isLiteral()) { p.setObject(t.getObject().getLiteralLexicalForm()); } else { p.setObject(getNPPhrase(t.getObject().toString(), false)); } p.setFeature(Feature.TENSE, Tense.PRESENT); return p; }
public static void addVarsFromTriple(Collection<Var> acc, Triple t) { addVar(acc, t.getSubject()); addVar(acc, t.getPredicate()); addVar(acc, t.getObject()); }