@Test public void economizingTriplesEconomizesTheNodes() { Node s1a = Node.createURI("http://example.com/s1"); Node s1b = Node.createURI("http://example.com/s1"); Node p1a = Node.createURI("http://example.com/p1"); Node p1b = Node.createURI("http://example.com/p1"); Node o1a = Node.createLiteral("55", XSDDatatype.XSDint); Node o1b = Node.createLiteral("55", XSDDatatype.XSDint); assertTrue(o1a != o1b); Triple t1 = new Triple(s1a, p1a, o1a); Triple t2 = new Triple(s1b, p1b, o1b); assertTrue(t1 != t2); assertEquals(t1, t2); Economizer<Triple> e = new TripleEconomizer(); Triple t3 = e.economize(t1); Triple t4 = e.economize(t2); assertTrue(t3.getSubject() == t4.getSubject()); assertTrue(t3.getObject() == t4.getObject()); assertTrue(t3.getPredicate() == t4.getPredicate()); assertTrue(s1a.equals(t3.getSubject())); assertTrue(p1a.equals(t3.getPredicate())); assertTrue(o1a.equals(t3.getObject())); }
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); }
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; }
/* @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 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); }
/** * 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; }
public static String str(Triple t) { return serialize(t.getSubject()) + " " + serialize(t.getPredicate()) + " " + serialize(t.getObject()); }
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); } }
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); }
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(); }
@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()); } }
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 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(); }
/** * 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); }
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 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 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 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; }
@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()); }
public List<AlphaResultUnion> calculateAlphaSTG( Collection<Triple> triples, AbstractConceptMapping cm) throws Exception { List<AlphaResultUnion> alphaResultUnionList = new Vector<AlphaResultUnion>(); Triple firstTriple = triples.iterator().next(); Node tpSubject = firstTriple.getSubject(); SQLLogicalTable alphaSubject = this.calculateAlphaSubject(tpSubject, cm); // String logicalTableAlias = cm.getLogicalTableAlias(); String logicalTableAlias = alphaSubject.getAlias(); // if(logicalTableAlias == null || logicalTableAlias.equals("")) { // cm.setLogicalTableAlias(alphaSubject.getAlias()); // } // mapping projection of corresponding predicates for (Triple tp : triples) { Node tpPredicate = tp.getPredicate(); List<SQLJoinTable> alphaPredicateObjects = new Vector<SQLJoinTable>(); List<SQLLogicalTable> alphaPredicateObjects2 = new Vector<SQLLogicalTable>(); if (tpPredicate.isURI()) { String tpPredicateURI = tpPredicate.getURI(); Collection<String> mappedClassURIs = cm.getMappedClassURIs(); boolean processableTriplePattern = true; if (tp.getObject().isURI()) { String objectURI = tp.getObject().getURI(); if (RDF.type.getURI().equals(tpPredicateURI) && mappedClassURIs.contains(objectURI)) { processableTriplePattern = false; } } if (processableTriplePattern) { List<SQLJoinTable> alphaPredicateObjectAux = calculateAlphaPredicateObjectSTG(tp, cm, tpPredicateURI, logicalTableAlias); if (alphaPredicateObjectAux != null) { alphaPredicateObjects.addAll(alphaPredicateObjectAux); } List<SQLLogicalTable> alphaPredicateObjectAux2 = calculateAlphaPredicateObjectSTG2(tp, cm, tpPredicateURI, logicalTableAlias); if (alphaPredicateObjectAux2 != null) { alphaPredicateObjects2.addAll(alphaPredicateObjectAux2); } AlphaResult alphaResult = new AlphaResult(alphaSubject, alphaPredicateObjects, tpPredicateURI); // alphaResult.setAlphaPredicateObjects2(alphaPredicateObjectAux2); AlphaResultUnion alphaTP = new AlphaResultUnion(alphaResult); alphaResultUnionList.add(alphaTP); } } else if (tpPredicate.isVariable()) { Collection<AbstractPropertyMapping> pms = cm.getPropertyMappings(); AlphaResultUnion alphaTP = new AlphaResultUnion(); for (AbstractPropertyMapping pm : pms) { String tpPredicateURI = pm.getMappedPredicateName(); List<SQLJoinTable> alphaPredicateObjectAux = calculateAlphaPredicateObjectSTG(tp, cm, tpPredicateURI, logicalTableAlias); if (alphaPredicateObjectAux != null) { alphaPredicateObjects.addAll(alphaPredicateObjectAux); } List<SQLLogicalTable> alphaPredicateObjectAux2 = calculateAlphaPredicateObjectSTG2(tp, cm, tpPredicateURI, logicalTableAlias); if (alphaPredicateObjectAux2 != null) { alphaPredicateObjects2.addAll(alphaPredicateObjectAux2); } AlphaResult alphaResult = new AlphaResult(alphaSubject, alphaPredicateObjects, tpPredicateURI); // alphaResult.setAlphaPredicateObjects2(alphaPredicateObjectAux2); alphaTP.add(alphaResult); } if (alphaTP != null) { alphaResultUnionList.add(alphaTP); } } else { String errorMessage = "Predicate has to be either an URI or a variable"; throw new QueryTranslationException(errorMessage); } } return alphaResultUnionList; }
public static void addVarsFromTriple(Collection<Var> acc, Triple t) { addVar(acc, t.getSubject()); addVar(acc, t.getPredicate()); addVar(acc, t.getObject()); }
public static TripleWithHash create(int n, String s) { Triple t = triple(s); return new TripleWithHash(n, t.getSubject(), t.getPredicate(), t.getObject()); }
/** Delete a triple - return true if it was deleted, false if it didn't exist */ public boolean delete(Triple triple) { return table.deleteRow(triple.getSubject(), triple.getPredicate(), triple.getObject()); }
public boolean add(Triple triple) { return table.addRow(triple.getSubject(), triple.getPredicate(), triple.getObject()); }
public double match(Triple t) { return match( Item.createNode(t.getSubject()), Item.createNode(t.getPredicate()), Item.createNode(t.getObject())); }