本文整理匯總了Java中org.apache.jena.rdf.model.Model.read方法的典型用法代碼示例。如果您正苦於以下問題:Java Model.read方法的具體用法?Java Model.read怎麽用?Java Model.read使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.jena.rdf.model.Model
的用法示例。
在下文中一共展示了Model.read方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createObject
import org.apache.jena.rdf.model.Model; //導入方法依賴的package包/類
/**
* Creates a new object.
*
* This originally used application/octet-stream;qs=1001 as a workaround
* for JERSEY-2636, to ensure requests without a Content-Type get routed here.
* This qs value does not parse with newer versions of Jersey, as qs values
* must be between 0 and 1. We use qs=1.000 to mark where this historical
* anomaly had been.
*
* @param contentDisposition the content Disposition value
* @param requestContentType the request content type
* @param slug the slug value
* @param requestBodyStream the request body stream
* @param link the link value
* @param digest the digest header
* @return 201
* @throws InvalidChecksumException if invalid checksum exception occurred
* @throws IOException if IO exception occurred
* @throws MalformedRdfException if malformed rdf exception occurred
*/
@POST
@Consumes({MediaType.APPLICATION_OCTET_STREAM + ";qs=1.000", WILDCARD})
@Produces({TURTLE_WITH_CHARSET + ";qs=1.0", JSON_LD + ";qs=0.8",
N3_WITH_CHARSET, N3_ALT2_WITH_CHARSET, RDF_XML, NTRIPLES, TEXT_PLAIN_WITH_CHARSET,
TURTLE_X, TEXT_HTML_WITH_CHARSET, "*/*"})
public Response createObject(@HeaderParam(CONTENT_DISPOSITION) final ContentDisposition contentDisposition,
@HeaderParam(CONTENT_TYPE) final MediaType requestContentType,
@HeaderParam("Slug") final String slug,
@ContentLocation final InputStream requestBodyStream,
@HeaderParam(LINK) final String link,
@HeaderParam("Digest") final String digest)
throws InvalidChecksumException, IOException, MalformedRdfException {
LOGGER.info("POST: {}", externalPath);
final ContainerService containerService = getContainerService();
final URI resourceUri = createFromPath(externalPath);
//check that resource exists
if (!containerService.exists(resourceUri)) {
if (!isRoot(resourceUri)) {
return status(NOT_FOUND).build();
} else {
createRoot();
}
}
final String newResourceName = slug == null ? UUID.randomUUID().toString() : slug;
final String resourcePath = (isRoot(resourceUri) ? "" : resourceUri.getPath());
final URI newResourceUri = createFromPath(resourcePath + "/" + newResourceName);
final Container container = containerService.findOrCreate(newResourceUri);
final Model model = ModelFactory.createDefaultModel();
model.read(requestBodyStream, container.getIdentifier().toString(), "TTL");
final Stream<Triple> triples = model.listStatements().toList().stream().map(Statement::asTriple);
container.updateTriples(triples);
return created(toExternalURI(container.getIdentifier(), headers)).build();
}
示例2: toTriple
import org.apache.jena.rdf.model.Model; //導入方法依賴的package包/類
/**
* This method converts a string (in n-triples format) into a Jena triple
*
* @param string containing single n-triples triple
* @return Triple
*/
public static Triple toTriple(final String string) {
// Create Jena model
Model inputModel = createDefaultModel();
try {
// Load model with arg string (expecting n-triples)
inputModel = inputModel.read(new StringInputStream(string), null, strLangNTriples);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
// Since there is only one statement, get it
final Statement stmt = inputModel.listStatements().nextStatement();
// Return the Jena triple which the statement represents
return stmt.asTriple();
}
示例3: createModel
import org.apache.jena.rdf.model.Model; //導入方法依賴的package包/類
@Override
protected Model createModel(String dbId) {
File ntriplesFile = getNTriplesPath(dbId).toFile();
if (!ntriplesFile.exists()) {
logger.warn(marker, "Can't find NT file {}", ntriplesFile);
} else {
try (InputStream is = new FileInputStream(ntriplesFile)) {
Model model = ModelFactory.createDefaultModel();
model.read(is, null, "N-TRIPLES");
return model;
} catch (Exception e) {
throw new StarGraphException(e);
}
}
return null;
}
示例4: loadOEModelToTdb
import org.apache.jena.rdf.model.Model; //導入方法依賴的package包/類
/**
* Fetch a model from Ontology Editor, load and return as TDB-backed Jena model.
*
* @param endpoint
* @param tDbDirectoryPath
* @return
* @throws OEConnectionException
* @throws IOException
*/
public static Model loadOEModelToTdb(OEModelEndpoint endpoint, String tDbDirectoryPath) throws IOException, OEConnectionException {
Preconditions.checkNotNull(endpoint);
Preconditions.checkArgument(!Strings.isNullOrEmpty(tDbDirectoryPath));
if (logger.isDebugEnabled()) {
logger.debug("OEModelEndpoint: {}", endpoint);
logger.debug("TDB Dir path : {}", tDbDirectoryPath);
}
String modelAsTTL = endpoint.fetchData();
Dataset dataset = TDBFactory.createDataset(tDbDirectoryPath);
Model model = dataset.getNamedModel(endpoint.modelIri);
model.read(new ByteArrayInputStream(modelAsTTL.getBytes()), "TTL");
return model;
}
示例5: testLocalDiff
import org.apache.jena.rdf.model.Model; //導入方法依賴的package包/類
@Test
public void testLocalDiff() throws Exception {
Model firstModel = ModelFactory.createDefaultModel();
firstModel.read("Playpen2.ttl", "TTL");
Model secondModel = ModelFactory.createDefaultModel();
secondModel.add(firstModel);
Resource res1 = secondModel.createResource(resIri1);
res1.addProperty(RDF.type, SKOS.Concept);
client.setCurrentModel(firstModel);
client.setPendingModel(secondModel);
assertEquals(firstModel, client.getCurrentModel());
assertEquals(secondModel, client.getPendingModel());
RDFDifference diff = client.getBatchDiff();
assertTrue(diff.getInLeftOnly().size() == 0);
assertTrue(diff.getInRightOnly().size() == 1);
assertFalse(diff.getInLeftOnly().containsResource(res1));
assertFalse(diff.getInLeftOnly().contains(res1, RDF.type, SKOS.Concept));
assertTrue(diff.getInRightOnly().containsResource(res1));
assertTrue(diff.getInRightOnly().contains(res1, RDF.type, SKOS.Concept));
}
示例6: main
import org.apache.jena.rdf.model.Model; //導入方法依賴的package包/類
public static void main(String[] args) throws IOException {
PropertyConfigurator.configure("log4j.info");
BufferedReader reader = new BufferedReader(new FileReader(args[0]));
FileOutputStream out = new FileOutputStream(new File("/usr/local/RAID/geonames.nt"));
String buffer = null;
int count = 0;
while ((buffer = reader.readLine()) != null) {
if (++count % 2 == 0) {
logger.debug("processing xml: " + buffer);
Model model = ModelFactory.createDefaultModel() ;
model.read(new StringReader(buffer), null, "RDF/XML");
model.write(out, "N-TRIPLE");
} else {
logger.info("skipping: " + buffer);
}
}
reader.close();
}
示例7: createOrReplaceObjectRdf
import org.apache.jena.rdf.model.Model; //導入方法依賴的package包/類
/**
* Create a resource at a specified path, or replace triples with provided RDF.
*
* @param requestContentType the request content type
* @param requestBodyStream the request body stream
* @param contentDisposition the content disposition value
* @param ifMatch the if-match value
* @param links the link values
* @param digest the digest header
* @return 204
* @throws InvalidChecksumException if invalid checksum exception occurred
* @throws MalformedRdfException if malformed rdf exception occurred
* @throws UnsupportedAlgorithmException
*/
@PUT
@Consumes
public Response createOrReplaceObjectRdf(
@HeaderParam(CONTENT_TYPE) final MediaType requestContentType,
@ContentLocation final InputStream requestBodyStream,
@HeaderParam(CONTENT_DISPOSITION) final ContentDisposition contentDisposition,
@HeaderParam("If-Match") final String ifMatch,
@HeaderParam(LINK) final List<String> links,
@HeaderParam("Digest") final String digest)
throws InvalidChecksumException, MalformedRdfException, UnsupportedAlgorithmException {
final URI internalUri = createFromPath(externalPath);
if (isRoot(internalUri)) {
//method not allowed if root
return Response.status(METHOD_NOT_ALLOWED).build();
}
final ContainerService containerService = getContainerService();
//create resource if exists
if (!containerService.exists(internalUri)) {
final Container container = containerService.findOrCreate(internalUri);
final Model model = ModelFactory.createDefaultModel();
model.read(requestBodyStream, null, "TTL");
final Stream<Triple> triples = model.listStatements().toList().stream().map(Statement::asTriple);
container.updateTriples(triples);
return created(toExternalURI(container.getIdentifier(), headers)).build();
} else {
//TODO delete resource, create resource, and update triples.
return noContent().build();
}
}
示例8: loadOEModelToMem
import org.apache.jena.rdf.model.Model; //導入方法依賴的package包/類
/**
* Fetch a model from Ontology Editor, load and return as memory-backed Jena model.
*
* @param endpoint
* @return
* @throws OEConnectionException
* @throws IOException
*/
public static Model loadOEModelToMem(OEModelEndpoint endpoint) throws IOException, OEConnectionException {
Preconditions.checkNotNull(endpoint);
logger.debug("OEModelEndpoint base URL: {}", endpoint.toString());
String modelAsTTL = endpoint.fetchData();
logger.debug("Downloaded model: {}", modelAsTTL);
Model model = ModelFactory.createDefaultModel();
model.read(new ByteArrayInputStream(modelAsTTL.getBytes()), null, "TTL");
return model;
}
示例9: testVocabularyManagement
import org.apache.jena.rdf.model.Model; //導入方法依賴的package包/類
@Test
public void testVocabularyManagement() throws Exception {
RESTResource resource;
String ontoId;
Map<String,String> parameters = new HashMap<String,String>();
// POST vocabulary
String ontoUri = "http://purl.oclc.org/NET/ssnx/qu/qu-rec20";
InputStream uriStream = new ByteArrayInputStream(ontoUri.getBytes("UTF-8"));
resource = vch.post(new URI(baseUri + "/vocab"), parameters, uriStream);
ontoId = resource.path;
Assert.assertTrue("QU ontology not registered", VocabularyUtils.containsVocabulary(ontoUri));
// GET vocabulary by SPARQL query
parameters.clear();
parameters.put("query", "?s ?p ?o");
resource = vch.get(new URI(baseUri + "/vocab"), parameters);
JsonValue ontoIds = JSON.parseAny(resource.content);
Assert.assertTrue("Vocabulary collection is not an array", ontoIds.isArray());
Assert.assertTrue("Vocabulary imports were not added", ontoIds.getAsArray().size() > 1);
Assert.assertTrue("QU ontology not found", ontoIds.getAsArray().contains(new JsonString(ontoId)));
// GET vocabulary by id
VocabularyHandler vh = new VocabularyHandler(ontoId, ThingDirectory.servers);
resource = vh.get(new URI(baseUri + ontoId), null);
ByteArrayInputStream byteStream = new ByteArrayInputStream(resource.content.getBytes());
Model m = ModelFactory.createDefaultModel();
m.read(byteStream, "", "Turtle");
Assert.assertFalse("QU ontology definition is not valid", m.isEmpty());
// DELETE vocabulary
vh.delete(new URI(baseUri + ontoId), null, null);
Assert.assertFalse("QU ontology not deleted", VocabularyUtils.containsVocabulary(ontoUri));
}
示例10: dereference
import org.apache.jena.rdf.model.Model; //導入方法依賴的package包/類
/**
* Get the triples that describe the entity identified by the URI.
* @param uri the URI of the entity
* @return JENA model containing the triples
* @throws DereferencingFailedException
*/
public Model dereference(URI uri) throws DereferencingFailedException{
logger.debug("Dereferencing " + uri + "...");
Model model = null;
// check if already cached
if(useCache()){
model = loadFromDisk(uri);
}
// if we got nothing from cache
if (model == null) {
model = ModelFactory.createDefaultModel();
try {
URLConnection conn = uri.toURL().openConnection();
conn.setRequestProperty("Accept", contentType);
InputStream is = conn.getInputStream();
model.read(is, null, RDFLanguages.contentTypeToLang(contentType).getLabel());
is.close();
if (useCache()) {
writeToDisk(uri, model);
}
} catch (IOException e) {
throw new DereferencingFailedException(uri, e);
}
}
logger.debug("Done. Got " + model.size() + " triples for " + uri);
return model;
}
示例11: loadFromDisk
import org.apache.jena.rdf.model.Model; //導入方法依賴的package包/類
private Model loadFromDisk(URI uri){
File cachedFile = getCacheFile(uri);
if(cachedFile.exists()){
Model model = ModelFactory.createDefaultModel();
try(InputStream is = new BufferedInputStream(new FileInputStream(cachedFile))){
model.read(is, null, cacheFileLanguage.getLabel());
return model;
} catch (IOException e) {
logger.error("Failed loading from disk.", e);
}
}
return null;
}
示例12: init
import org.apache.jena.rdf.model.Model; //導入方法依賴的package包/類
@BeforeClass
public static void init() {
String kb = "<http://dbpedia.org/datatypes/squareKilometre> <http://www.w3.org/2000/01/rdf-schema#label> \"square kilometre\"@en .";
Model model = ModelFactory.createDefaultModel();
model.read(new StringReader(kb), null, Lang.TURTLE.getName());
conv = new LiteralConverter(new DefaultIRIConverter(model));
}
示例13: createModel
import org.apache.jena.rdf.model.Model; //導入方法依賴的package包/類
private Model createModel(final AwsProxyResponse getResponse) {
final Model model = ModelFactory.createDefaultModel();
final String responseBody = getResponse.getBody();
model.read(new ByteArrayInputStream(responseBody.getBytes(Charset.defaultCharset())), "", "TTL");
return model;
}
示例14: main
import org.apache.jena.rdf.model.Model; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
String triples = "@prefix dbr: <http://dbpedia.org/resource/>." + "@prefix dbo: <http://dbpedia.org/ontology/>."
+ "@prefix xsd: <http://www.w3.org/2001/XMLSchema#>."
+ "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>."
+ "dbr:Albert_Einstein rdf:type dbo:Physican, dbo:Philosopher;" + "dbo:birthPlace dbr:Ulm;"
+ "dbo:birthDate \"1879-03-14\"^^xsd:date;" + "dbo:academicAdvisor dbr:Heinrich_Friedrich_Weber;"
// + "dbo:almaMater dbr:ETH_Zurich."; //fix from ex-aluno to
// ex-instituto.
// + "dbo:almaMater dbr:University_of_Zurich;"
+ "dbo:award dbr:Max_Planck_Medal;"
// + "dbo:award
// dbr:Time_100:_The_Most_Important_People_of_the_Century;"
// + "dbo:award dbr:Copley_Medal;"
// + "dbo:award dbr:Nobel_Prize_in_Physics;"
// + "dbo:award
// dbr:Barnard_Medal_for_Meritorious_Service_to_Science;"
// + "dbo:award dbr:Matteucci_Medal;"
// + "dbo:award dbr:Royal_Society.";
+ "dbo:citizenship dbr:Austria-Hungary.";
// + "dbo:citizenship dbr:Kingdom_of_Württemberg;"
// + "dbo:citizenship dbr:Statelessness;"
// + "dbo:citizenship dbr:Switzerland;"
// + "dbo:doctoralAdvisor dbr:Alfred_Kleiner.";
// + "dbo:field dbr:Physics;"
// + "dbo:field dbr:Philosophy;"
// + "dbo:influenced dbr:Nathan_Rosen;"
// + "dbo:influenced dbr:Leo_Szilard;"
// + "dbo:influenced dbr:Ernst_G._Straus;"
// + "dbo:knownFor dbr:Bose–Einstein_condensate;"
// + "dbo:knownFor dbr:Brownian_motion;"
// + "dbo:knownFor dbr:EPR_paradox;"
// + "dbo:knownFor dbr:General_relativity;"
// + "dbo:knownFor dbr:Photoelectric_effect;"
// + "dbo:knownFor dbr:Special_relativity;"
// + "dbo:knownFor dbr:Cosmological_constant;"
// + "dbo:knownFor dbr:Mass–energy_equivalence;"
// + "dbo:knownFor dbr:Gravitational_wave;"
// + "dbo:knownFor dbr:Einstein_field_equations;"
// + "dbo:knownFor dbr:Classical_unified_field_theories;"
// + "dbo:knownFor dbr:Bose–Einstein_statistics;"
// + "dbo:residence dbr:Switzerland;"
// + "dbo:spouse dbr:Elsa_Einstein;"
// + "dbo:spouse dbr:Mileva_Marić;"
// + "dbo:deathPlace dbr:Princeton,_New_Jersey;"
// + "dbo:deathDate \"1955-04-18\"^^xsd:date .";
// + "dbr:Ulm rdf:type dbo:city.";
// + "dbo:country dbr:Germany.";
// + "";
// + "dbo:federalState :Baden_Württemberg ."
// + ":Leipzig a dbo:City;"
// + "dbo:country :Germany;"
// + "dbo:federalState :Saxony .";
// String triples =
// "@prefix dbr: <http://dbpedia.org/resource/>."
// + "@prefix dbo: <http://dbpedia.org/ontology/>."
// + "@prefix xsd: <http://www.w3.org/2001/XMLSchema#>."
// + "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>."
// + "dbr:Angela_Merkel rdf:type dbo:Scientist, dbo:OfficeHolder;"
// + "dbo:birthPlace dbr:Hamburg.";
// //+ "dbo:birthDate \"1954-07-17\"^^xsd:date,"
// //+ "dbo:studiedIn dbr:Leipzig."
// + "dbr:Hamburg rdf:type dbo:City;"
// + "dbo:country dbr:Germany.";
// //+ "dbr:Leipzig rdf:type dbo:City,"
// //+ "dbo:country dbr:Germany,"
// //+ "dbo:federalState dbr:Saxony.";
Model model = ModelFactory.createDefaultModel();
model.read(new ByteArrayInputStream(triples.getBytes()), null, "TURTLE");
DocumentGeneratorPortuguese gen = new DocumentGeneratorPortuguese(
SparqlEndpoint.create("http://pt.dbpedia.org/sparql", ""), "cache");
String document = gen.generateDocument(model);
System.out.println(document);
}