private static URIRef coherceStringURIRef(boolean nullable, String str) { URIRef tmp=null; try { tmp=uriRef(new URI(str)); } catch (URISyntaxException e) { if(!nullable) { throw new IllegalArgumentException("Cannot crete URIRef from string '"+str+"': invalid URI",e); } } return tmp; }
private static URIRef coherceURIRef(Object object, boolean nullable) { URIRef result=null; if(object instanceof URIRef) { result=(URIRef)object; } else if(object instanceof QName) { result=uriRef((QName)object); } else if(object instanceof URI) { result=uriRef((URI)object); } else if(object instanceof URL) { result=uriRef((URL)object); } else if(object instanceof String) { result=coherceStringURIRef(nullable, (String)object); } return result; }
public static URIRef uriRef(String identity) { Objects.requireNonNull(identity, IDENTITY_PARAM); try { return uriRef(new URI(identity)); } catch (URISyntaxException e) { throw new IllegalArgumentException("Cannot create URIRef from '"+identity+"'",e); } }
public static URIRef uriRef(URL identity) { Objects.requireNonNull(identity, IDENTITY_PARAM); return uriRef(identity.toString()); }
public static URIRef uriRef(QName identity) { Objects.requireNonNull(identity, IDENTITY_PARAM); return uriRef(identity.getNamespaceURI() + identity.getLocalPart()); }
@Override public void visitExternalIndividual(ExternalIndividual individual) { this.resource=uriRef(individual.id()); }
public URIRef parseURI(org.openrdf.model.URI uri) { return RDFModelDSL.uriRef(uri.stringValue()); }
private static URIRef predicate(Property property) { return RDFModelDSL.uriRef(property.getNamespace()+property.getName()); }
private URIRef toURIRef(Property property) { return RDFModelDSL.uriRef(property.getNamespace()+property.getName()); }
public <T> Resource<?> createIdentity(T object) { return RDFModelDSL.uriRef(URI.create(String.format("urn:%03X:%08X", ctr++,System.identityHashCode(object)))); }
public Resource<?> parseResource(org.openrdf.model.Resource subject) { Resource<?> result; if(subject instanceof org.openrdf.model.URI) { result=RDFModelDSL.uriRef(subject.stringValue()); } else { result=RDFModelDSL.blankNode(subject.stringValue()); } return result; }
private void addType(Resource<?> identity, Type type) { URIRef object = RDFModelDSL.uriRef(type.getNamespace()+type.getName()); log("Adding type '%s' to object '%s'...",object,source); logTriple(source,RDF_TYPE,object); session.addTriple(identity, RDF_TYPE, object); }
@Override public <T> Resource<?> enumeratedIdentity(T object) { if(object instanceof TransactionOperation) { TransactionOperation to=(TransactionOperation)object; return RDFModelDSL.uriRef("http://delicias.dia.fi.upm.es/alm-istack/transactions/transactions#"+to.name()); } throw new IllegalStateException("Unknown enumerated object '"+object+"'"); }
private URIRef resolveManagedIndividualId(ManagedIndividualId target) { ManagedIndividualId id=target; URI indirectId=id.indirectId(); if(indirectId!=null) { id=ManagedIndividualId.createId(id.name(),id.managerId()); } URI path=TripleSetBuilder.this.resourceResolver.resolveResource(id); if(path==null) { return null; } else if(indirectId!=null) { path=path.resolve(indirectId); } return uriRef(TripleSetBuilder.this.base.resolve(path)); }
@Test public void testReplace() throws Exception { Map<Node,Node> replacements=new HashMap<Node, Node>(); replacements.put(i1, blankNode()); replacements.put(i2, uriRef(DATA+"ind2")); System.out.println("After replacing ("+replacements+"):"); System.out.println(replace(sut,replacements)); } @Test
@Test public void testJSON() throws Exception { Iterable<Triple> content= tripleSet( triple( uriRef("example"), uriRef("http://www.example.org/vocab#size"), literal(3)), triple( uriRef("example"), uriRef("http://www.example.org/vocab#property"), uriRef("example/test")), triple( uriRef("example/test"), uriRef("http://www.example.org/vocab#propertyInverse"), uriRef("example")), triple( uriRef("example"), uriRef("http://www.example.org/vocab#otherProperty"), literal("27")) ); RDFContext rdfContext = RDFContext.createContext(URI.create("http://www.example.org/")); StringWriter writer = new StringWriter(); rdfContext.serialize(content, Format.JSON_LD, writer); System.out.println(writer.toString()); }
@Test public void testSerialize() throws Exception { InmutableTripleSet toSerialize=replace(sut,i1,uriRef("resources/defects/1")).add(complexLiterals); Namespaces namespaces = new Namespaces(). addPrefix("ex", VOCABULARY). addPrefix("rdf", RDF). addPrefix("xsd", XSD); URI baseURI = URI.create(DATA); Format format = Format.TURTLE; String serialization = serialize(toSerialize, baseURI, namespaces, format); System.out.println("Serialization :"); System.out.println(serialization); InmutableTripleSet parse = deserialize(serialization,baseURI.resolve("new"),format); System.out.println("Deserialization:"); System.out.println(parse); }