@Test public void testNodePrettyPrinting() { assertEquals("\"foo\"", PrettyPrinter.toString(Node.createLiteral("foo"))); assertEquals("\"foo\"@en", PrettyPrinter.toString(Node.createLiteral("foo", "en", null))); assertEquals("\"1\"^^xsd:int", PrettyPrinter.toString(Node.createLiteral("1", null, XSDDatatype.XSDint))); assertEquals("\"1\"^^xsd:int", PrettyPrinter.toString(Node.createLiteral("1", null, XSDDatatype.XSDint), PrefixMapping.Standard)); assertEquals("_:foo", PrettyPrinter.toString(Node.createAnon(new AnonId("foo")))); assertEquals("<http://example.org/>", PrettyPrinter.toString(Node.createURI("http://example.org/"))); assertEquals("<" + RDF.type.getURI() + ">", PrettyPrinter.toString(RDF.type.asNode(), new PrefixMappingImpl())); assertEquals("rdf:type", PrettyPrinter.toString(RDF.type.asNode(), PrefixMapping.Standard)); assertEquals("?x", PrettyPrinter.toString(Node.createVariable("x"))); assertEquals("?ANY", PrettyPrinter.toString(Node.ANY)); }
public void testNodePrettyPrinting() { assertEquals("\"foo\"", PrettyPrinter.toString(Node.createLiteral("foo"))); assertEquals("\"foo\"@en", PrettyPrinter.toString(Node.createLiteral("foo", "en", null))); assertEquals("\"1\"^^<" + XSDDatatype.XSDint.getURI() + ">", PrettyPrinter.toString(Node.createLiteral("1", null, XSDDatatype.XSDint))); assertEquals("\"1\"^^xsd:int", PrettyPrinter.toString(Node.createLiteral("1", null, XSDDatatype.XSDint), PrefixMapping.Standard)); assertEquals("_:foo", PrettyPrinter.toString(Node.createAnon(new AnonId("foo")))); assertEquals("<http://example.org/>", PrettyPrinter.toString(Node.createURI("http://example.org/"))); assertEquals("<" + RDF.type.getURI() + ">", PrettyPrinter.toString(RDF.type.asNode(), new PrefixMappingImpl())); assertEquals("rdf:type", PrettyPrinter.toString(RDF.type.asNode(), PrefixMapping.Standard)); assertEquals("?x", PrettyPrinter.toString(Node.createVariable("x"))); assertEquals("?ANY", PrettyPrinter.toString(Node.ANY)); }
public Triple getTriple(){ Resource _sbj, _prd; RDFNode _obj; if (isSubjectBlank) _sbj = ModelFactory.createDefaultModel().createResource(new AnonId(this.subject)); else _sbj = ModelFactory.createDefaultModel().createResource(subject); _prd = ModelFactory.createDefaultModel().createProperty(predicate); if (isObjectBlank) _obj = ModelFactory.createDefaultModel().createResource(new AnonId(this.object)); else if (isObjectLiteral){ _obj = ModelFactory.createDefaultModel().createTypedLiteral(this.object, this.objectDataType); } else _obj = ModelFactory.createDefaultModel().createResource(object); return new Triple(_sbj.asNode(), _prd.asNode(), _obj.asNode()); }
@Test public void testFixedNodeMakerToString() { assertEquals("Fixed(\"foo\")", new FixedNodeMaker(Node.createLiteral("foo")).toString()); assertEquals("Fixed(\"foo\"@en)", new FixedNodeMaker(Node.createLiteral("foo", "en", null)).toString()); assertEquals("Fixed(\"1\"^^xsd:int)", new FixedNodeMaker(Node.createLiteral("1", null, XSDDatatype.XSDint)).toString()); assertEquals("Fixed(_:foo)", new FixedNodeMaker(Node.createAnon(new AnonId("foo"))).toString()); assertEquals("Fixed(<http://example.org/>)", new FixedNodeMaker(Node.createURI("http://example.org/")).toString()); }
public void testFixedNodeMakerToString() { assertEquals("Fixed(\"foo\")", new FixedNodeMaker(Node.createLiteral("foo"), true).toString()); assertEquals("Fixed(\"foo\"@en)", new FixedNodeMaker(Node.createLiteral("foo", "en", null), true).toString()); assertEquals("Fixed(\"1\"^^<" + XSDDatatype.XSDint.getURI() + ">)", new FixedNodeMaker(Node.createLiteral("1", null, XSDDatatype.XSDint), true).toString()); assertEquals("Fixed(_:foo)", new FixedNodeMaker(Node.createAnon(new AnonId("foo")), true).toString()); assertEquals("Fixed(<http://example.org/>)", new FixedNodeMaker(Node.createURI("http://example.org/"), true).toString()); }
@Override public BlankNode createBlankNode(String id) { // this.modificationCount++; // should be unique across models AnonId anonid = AnonId.create(id); return new JenaBlankNode(com.hp.hpl.jena.graph.NodeFactory.createAnon(anonid)); }
public static Resource externalize(final Identifiable identifiable, final Model model) { final Identifier id = identifiable.getIdentifier(); if (id.isUri()) { return model.createResource(id.toString()); } else { return model.createResource(AnonId.create(id.toString())); } }
/** * Get the id of blank node. */ @Override public AnonId getBlankNodeId() { Transaction tx = graphDb.beginTx(); //AnonId id = AnonId.create(String.valueOf(node.getId())); AnonId id = AnonId.create(delegate.getProperty(NeoGraph.PROPERTY_URI).toString()); tx.success(); return id; }
public void addDerivedRelation(Resource sourceProcess, Resource coeff, Resource destinationProcess, int exponent, Resource sourceRelation) { sourceProcess.addProperty(Datatype.hasDerivedRelation, model.createResource(Datatype.getURI() + AnonId.create().toString()) .addProperty(RDF.type, Datatype.DerivedRelation) .addProperty(Datatype.isDerivedFrom, sourceRelation) .addProperty(Datatype.hasOriginProcess, sourceProcess) .addProperty(Datatype.hasWeightCoefficient, coeff) .addProperty(Datatype.hasDestinationProcess, destinationProcess) .addProperty(Datatype.exponent, model.createTypedLiteral(exponent))); }
protected void writeCalculatedElementaryFlow(Resource process, ElementaryFlow flow) { process.addProperty(Datatype.hasCalculatedFlow, model.createResource(Datatype.getURI() + AnonId.create().toString()) .addProperty(Datatype.hasElementaryFlowType, model.createResource(Datatype.getURI() + flow.getType().getId())) .addProperty(Datatype.value, model.createTypedLiteral(flow.getValue().value)) .addProperty(Datatype.uncertainty, model.createTypedLiteral(flow.getValue().uncertainty)) .addProperty(RDF.type, Datatype.CalculateElementaryFlow)); }
protected void writeImpact(Resource process, Impact impact) { process.addProperty(Datatype.hasImpact, model.createResource(Datatype.getURI() + AnonId.create().toString()) .addProperty(Datatype.hasImpactType, model.createResource(Datatype.getURI() + impact.getType().getId())) .addProperty(Datatype.value, model.createTypedLiteral(impact.getValue().value)) .addProperty(Datatype.uncertainty, model.createTypedLiteral(impact.getValue().uncertainty)) .addProperty(RDF.type, Datatype.Impact)); }
/** * Convert a Mulgara Value to a Jena graph node. * @param obj The Mulgara value to convert. * @return A new Jena graph node. */ static Node o2n(org.jrdf.graph.Node obj) { if (obj == null) return Node.ANY; // testing for org.jrdf.graph.URIReference if (obj.isURIReference()) { URIReference uRef = (URIReference)obj; if (skolemizedBlankNodes) { String x = uRef.getURI().toString(); if (x.startsWith(bNodeScheme)) { x = x.substring(bNodeScheme.length()); Node n = Node.createAnon(new AnonId(x)); return n; } } return Node.createURI(uRef.getURI().toString()); } // testing for org.jrdf.graph.Literal if (obj.isLiteral()) { Literal literal = (Literal)obj; if (literal.getDatatypeURI() != null) { RDFDatatype type = TypeMapper.getInstance().getSafeTypeByName(literal.getDatatypeURI().toString()) ; return Node.createLiteral(literal.getLexicalForm(), null, type); } return Node.createLiteral(literal.getLexicalForm(), literal.getLanguage(), null); } if (obj.isBlankNode()) { BlankNodeImpl b = (BlankNodeImpl)obj; // check if this was a Jena-allocated node Node jenaNode = valuesToNodes.get(b); // if not known, then create a Jena node from the Mulgara ID return jenaNode != null ? jenaNode : Node.createAnon(new AnonId(b.getID())); } throw new RuntimeException("Can't convert to Jena Node : "+obj) ; }
/** * Retrieve the anonymous node for a value, creating a new one if none exists * for it yet. * * @param value The value to retrieve the anonymous node for. * @return An anonymous node which is unique for the value. */ private com.hp.hpl.jena.graph.Node getAnon(BlankNode value) { com.hp.hpl.jena.graph.Node n = anonMap.get(value); if (n == null) { String nodeId = ((BlankNode)value).toString(); n = com.hp.hpl.jena.graph.Node.createAnon(new AnonId(nodeId)); addAnon((BlankNode)value, n); // tell the JrdfFactory as well jrdfFactory.addAnon(n, (BlankNode) value); } return n; }
protected Node createNode(String iri) { // Is it a bNode label? i.e. <_:xyz> if ( isBNodeIRI(iri) ) { String s = iri.substring(bNodeLabelStart.length()) ; Node n = Node.createAnon(new AnonId(s)) ; return n ; } return Node.createURI(iri) ; }
/** * @param value an RDF::Value instance * @return a Jena node */ public static Node newNode(RubyObject value) { // TODO String className = value.callMethod("class").toString(); if (className.equals("RDF::Node")) { return Node.createAnon(AnonId.create(value.callMethod("id").toString())); } if (className.equals("RDF::URI")) { return Node.createURI(value.toString()); } if (className.equals("RDF::Literal")) { return Node.createLiteral(value.callMethod("value").toString()); // FIXME } return null; }
/** * Convert the given Sesame Resource to a Jena Resource * @param theRes the sesame resource to convert * @return the sesame resource as a jena resource */ public static com.hp.hpl.jena.rdf.model.Resource asJenaResource(org.openrdf.model.Resource theRes) { if (theRes == null) { return null; } else if (theRes instanceof URI) { return asJenaURI( (URI) theRes); } else { return mInternalModel.createResource(new AnonId(((BNode) theRes).getID())); } }
public Triple getTriple(){ Resource _sbj, _prd; RDFNode _obj; if (isSubjectBlank) _sbj = ModelFactory.createDefaultModel().createResource(new AnonId(this.subject)); else _sbj = ModelFactory.createDefaultModel().createResource(subject); _prd = ModelFactory.createDefaultModel().createProperty(predicate); if (isObjectBlank) _obj = ModelFactory.createDefaultModel().createResource(new AnonId(this.object)); else if (isObjectLiteral) _obj = ModelFactory.createDefaultModel().createLiteral(object); else _obj = ModelFactory.createDefaultModel().createResource(object); return new Triple(_sbj.asNode(), _prd.asNode(), _obj.asNode()); }
List<Statement> createProblemModel(){ List<Statement> lst = new ArrayList<Statement>(); Resource anon = ModelFactory.createDefaultModel().createResource(AnonId.create()); lst.add(new StatementImpl(anon, RDF.subject, Commons.asRDFNode(q.getSubject()))); lst.add(new StatementImpl(anon, RDF.predicate, Commons.asRDFNode(q.getPredicate()))); lst.add(new StatementImpl(anon, RDF.object, Commons.asRDFNode(q.getObject()))); return lst; }
Model createProblemModel(){ Model m = ModelFactory.createDefaultModel(); Resource gen = Commons.generateURI(); m.add(gen, RDF.type, DQMPROB.OntologyHijackingException); Resource anon = m.createResource(AnonId.create()); m.add(gen, DQMPROB.hijackedTripleStatement, anon); m.add(anon, RDF.subject, Commons.asRDFNode(q.getSubject())); m.add(anon, RDF.predicate, Commons.asRDFNode(q.getPredicate())); m.add(anon, RDF.object, Commons.asRDFNode(q.getObject())); return m; }
Model createProblemModel(){ Model m = ModelFactory.createDefaultModel(); Resource gen = Commons.generateURI(); if (type == 'd') m.add(gen, RDF.type, DQMPROB.IncorrectDomain); if (type == 'r') m.add(gen, RDF.type, DQMPROB.IncorrectRange); if (type == 'x') m.add(gen, RDF.type, DQMPROB.IncorrectDomain); if (type == 'u') m.add(gen, RDF.type, DQMPROB.IncorrectRange); Resource anon = m.createResource(AnonId.create()); m.add(gen, DQMPROB.problematicTriple, anon); m.add(anon, RDF.subject, Commons.asRDFNode(q.getSubject())); m.add(anon, RDF.predicate, Commons.asRDFNode(q.getPredicate())); m.add(anon, RDF.object, Commons.asRDFNode(q.getObject())); try{ return m; } catch (Exception e){ logger.error("Error adding to problem list :" + e.getMessage()); return ModelFactory.createDefaultModel(); } }
Model createProblemModel(){ Model m = ModelFactory.createDefaultModel(); Resource gen = Commons.generateURI(); if (type == 'd') m.add(gen, RDF.type, DQMPROB.IncorrectDomain); if (type == 'r') m.add(gen, RDF.type, DQMPROB.IncorrectRange); if (type == 'x') m.add(gen, RDF.type, DQMPROB.UnknownType); if (type == 'u') m.add(gen, RDF.type, DQMPROB.UnknownType); Resource anon = m.createResource(AnonId.create()); m.add(gen, DQMPROB.problematicTriple, anon); m.add(anon, RDF.subject, Commons.asRDFNode(q.getSubject())); m.add(anon, RDF.predicate, Commons.asRDFNode(q.getPredicate())); m.add(anon, RDF.object, Commons.asRDFNode(q.getObject())); try{ return m; } catch (Exception e){ logger.error("Error adding to problem list :" + e.getMessage()); return ModelFactory.createDefaultModel(); } }
/** * <p>Method that overrides {@link com.hp.hpl.jena.rdf.model.impl.ModelCom#listStatements()} * This allows us to iterate over the relational database and then Jena's RDB model</p> */ public StmtIterator listStatements() { List<Triple> d2rqList = new ArrayList<Triple>(); ExtendedIterator<Triple> d2rqIter = graph.find( Node.ANY, Node.ANY, Node.ANY ); while( d2rqIter.hasNext() ) { Triple t = d2rqIter.next(); String blankNodeColumnString = null; String[] blankNodeColumns; if( ( blankNodeColumnString = ( (GraphD2RQRW) graph ).getBlankNodeColumns( t.getPredicate().getLocalName() ) ) != null ) { Node s = t.getSubject(), p = t.getPredicate(), o = t.getObject(); blankNodeColumns = blankNodeColumnString.split( " " ); String[] blankNodeObjects = o.getLiteralValue().toString().split( "/" ); String blankNode = baseURI + p.getLocalName() + "@@" + o.getLiteralValue().toString(); d2rqList.add( Triple.create( s, p, Node.createAnon( AnonId.create( blankNode ) ) ) ); //TODO need to see how to identify the column type and create the object based on that type for( int i = 0; i < blankNodeColumns.length; i++ ) d2rqList.add( Triple.create( Node.createAnon( AnonId.create( blankNode ) ), Node.createURI( p.toString().substring( 0, ( p.toString().lastIndexOf( '/' ) + 1 ) ) + blankNodeColumns[i] ) , Node.createLiteral( blankNodeObjects[blankNodeObjects.length - 1 - i] ) ) ); } else d2rqList.add( t ); } if( rdbModel != null ) return IteratorFactory.asStmtIterator( rdbModel.getGraph().find( Node.ANY, Node.ANY, Node.ANY).andThen( d2rqList.iterator() ), this ); else return IteratorFactory.asStmtIterator( d2rqList.iterator(), this ); }
public static Resource createAnonResource(AnonId id) { return m.createResource(id); }