@Test /** * Reads from TRIG with Jena API into Dataset 1, transforms one named graph from that Dataset into * Signingframework's API GraphCollection and writes it with Signingframework's API, reads the * result with Jena API into Dataset 2, and checks if the specified named graph model from Dataset * 1 is isomorphic with the same named graph model from Dataset 2. */ public void modelToGraphCollectionTest() throws Exception { for (String resourceFile : RESOURCE_FILES) { // prepare the input Dataset containg the Model to be converted InputStream is = this.getClass().getResourceAsStream(resourceFile); File outFile = testFolder.newFile(); // use this when debugging: // File outFile = File.createTempFile("won", ".trig"); // System.out.println(outFile); Dataset dataset = DatasetFactory.createMem(); RDFDataMgr.read(dataset, is, RDFFormat.TRIG.getLang()); is.close(); // test the convertion from the Model to the NamedGraph String modelName = dataset.listNames().next(); Model model = dataset.getNamedModel(modelName); // the method to be tested GraphCollection gc = ModelConverter.modelToGraphCollection(modelName, dataset); TriGPlusWriter.writeFile(gc, outFile.getAbsolutePath(), false); // check that the resulting graph collection is a representation // of the converted model. For this, read the resulting graph collection // as a Model with Jena API InputStream is2 = new FileInputStream(outFile); Dataset dataset2 = DatasetFactory.createMem(); RDFDataMgr.read(dataset2, is2, RDFFormat.TRIG.getLang()); is2.close(); Model model2 = dataset2.getNamedModel(modelName); File outFile2 = testFolder.newFile(); // use this when debugging: // File outFile2 = File.createTempFile("won", ".trig"); // System.out.println(outFile2); OutputStream os = new FileOutputStream(outFile2); RDFDataMgr.write(os, dataset2, RDFFormat.TRIG.getLang()); os.close(); // check that the model obtained from resulting graph collection is // a representation of the original converted model. Assert.assertTrue(model.listStatements().hasNext() && model2.listStatements().hasNext()); Assert.assertTrue(model.isIsomorphicWith(model2)); } }
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); }
@Test /** * Reads from TRIG with Jena API into Dataset 1, transforms one named Model from that Dataset into * Signingframework's API GraphCollection with one NamedGraph, transforms (converts) that * NamedGraph into Jena's Model, and checks if the resulting Model is the same as original Model. */ public void namedGraphToModelTest() throws Exception { for (String resourceFile : RESOURCE_FILES) { // prepare GraphCollection with NamedGraph to be converted: InputStream is = this.getClass().getResourceAsStream(resourceFile); Dataset dataset = DatasetFactory.createMem(); RDFDataMgr.read(dataset, is, RDFFormat.TRIG.getLang()); is.close(); String modelName = dataset.listNames().next(); Model model1 = dataset.getNamedModel(modelName); // this method is not tested here and used just for input // generation and to make it easier Namedgraph<->Model comparison // (but it's tested in other method, see modelToGraphCollectionTest()) GraphCollection gc = ModelConverter.modelToGraphCollection(modelName, dataset); LinkedList<NamedGraph> graphs = gc.getGraphs(); String graphName = null; for (NamedGraph g : graphs) { if (!g.getName().isEmpty() && g.getName().contains(modelName)) { graphName = g.getName(); break; } } // use this when debugging: // File outFile0 = File.createTempFile("won", ".trig"); // System.out.println(outFile0); // OutputStream os0 = new FileOutputStream(outFile0); // TriGPlusWriter.writeFile(gc, outFile0.getAbsolutePath(), false); // os0.close(); // test convert from NamedGraph of GraphCollection into Model Model model2 = ModelConverter.namedGraphToModel(graphName, gc); Dataset dataset2 = DatasetFactory.createMem(); dataset2.addNamedModel(modelName, model2); // TODO maybe chng the API so that the prefix map is taken care of in the converter: // if it makes sense from the the usage of this in Assembler point of view dataset2.getDefaultModel().setNsPrefixes(dataset2.getNamedModel(modelName).getNsPrefixMap()); File outFile = testFolder.newFile(); // use this when debugging: // File outFile = File.createTempFile("won", ".trig"); // System.out.println(outFile); OutputStream os = new FileOutputStream(outFile); RDFDataMgr.write(os, dataset2, RDFFormat.TRIG.getLang()); os.close(); // make sure that the original Model that was used to generate test input // GraphCollection with NamedGraph is isomorphic with the Model after // conversion is applied: Assert.assertTrue(model1.listStatements().hasNext() && model2.listStatements().hasNext()); Assert.assertTrue(model1.isIsomorphicWith(model2)); } }
public static DatasetGraph initFuseki(URL rdfFile, String datasetName) { URI rdfFileUri = new File(rdfFile.getFile()).toURI(); DatasetGraph datasetGraph = DatasetGraphFactory.createMem(); RDFDataMgr.read(datasetGraph, rdfFileUri.toString()); ServerConfig serverConfig = FusekiConfig.defaultConfiguration(datasetName, datasetGraph, true); SPARQLServer fusekiServer = new SPARQLServer(serverConfig); Fuseki.setServer(fusekiServer); fusekiServer.start(); return datasetGraph; }
public static Model readModel(String modelFile) throws FileNotFoundException { FileInputStream fin = null; try { Model model = ModelFactory.createDefaultModel(); fin = new FileInputStream(modelFile); RDFDataMgr.read(model, fin, Lang.NT); return model; } finally { IOUtils.closeQuietly(fin); } }
protected static Model jsonLdAsJenaModel(InputStream jsonIn, URI base) throws IOException, RiotException { Model model = ModelFactory.createDefaultModel(); RDFDataMgr.read(model, jsonIn, base.toASCIIString(), Lang.JSONLD); return model; // // Object input = JSONUtils.fromInputStream(jsonIn); // JSONLDTripleCallback callback = new JenaTripleCallback(); // Model model = (Model)JSONLD.toRDF(input, callback, new // Options(base.toASCIIString())); // return model; }
/** Read the RDF model from files. */ public static void readSemanticModelFiles() { logger.debug("Reading the model from a file"); // Read the model to an existing model String dataDir = UQasarUtil.getDataDirPath(); String modelPath = "file:///" + dataDir + ONTOLOGYFILE; // String modelPath = // "file:///C:/nyrhinen/Programme/jboss-as-7.1.1.Final/standalone/data/uq-ontology-model.rdf"; OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); RDFDataMgr.read(model, modelPath); // Test output to standard output // RDFDataMgr.write(System.out, uqModel, RDFFormat.RDFXML_PRETTY); logger.debug("Model read from file " + modelPath); UQasarUtil.setUqModel(model); System.out.println("Reading done."); }
/** Harvests all the triplets from each URI in the @rdfUris list */ private void harvestFromDumps() { for (String uri : rdfUris) { if (uri.isEmpty()) continue; logger.info("Harvesting uri [{}]", uri); Model model = ModelFactory.createDefaultModel(); try { RDFDataMgr.read(model, uri.trim(), RDFLanguages.RDFXML); BulkRequestBuilder bulkRequest = client.prepareBulk(); addModelToES(model, bulkRequest, true); } catch (RiotException re) { logger.error("Illegal xml character [{}]", re.getLocalizedMessage()); } catch (Exception e) { logger.error( "Exception when harvesting url: {}. Details: {}", uri, e.getLocalizedMessage()); } } }