Search in sources :

Example 41 with Statement

use of org.eclipse.rdf4j.model.Statement in project opentheso by miledrousset.

the class WriteRdfFileTest method writeRio.

@Test
public void writeRio() {
    FileOutputStream out = null;
    try {
        // a collection of several RDF statements
        Graph myGraph = null;
        out = new FileOutputStream("test_unesco2.rdf");
        try {
            RDFWriter writer = Rio.createWriter(RDFFormat.RDFXML, out);
            writer.startRDF();
            for (Statement st : myGraph) {
                writer.handleStatement(st);
            }
            writer.endRDF();
        } catch (RDFHandlerException e) {
        // oh no, do something!
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(WriteRdfFileTest.class.getName()).log(Level.SEVERE, null, ex);
    // oh no, do something!
    } finally {
        try {
            out.close();
        } catch (IOException ex) {
            Logger.getLogger(WriteRdfFileTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
Also used : Graph(org.eclipse.rdf4j.model.Graph) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) Statement(org.eclipse.rdf4j.model.Statement) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) RDFWriter(org.eclipse.rdf4j.rio.RDFWriter) IOException(java.io.IOException) Test(org.junit.Test)

Example 42 with Statement

use of org.eclipse.rdf4j.model.Statement in project molgenis by molgenis.

the class EntityModelWriterTest method testAddStatementsForEntityType.

@Test
public void testAddStatementsForEntityType() {
    Model model = new LinkedHashModel();
    Resource subject = valueFactory.createIRI("http://example.org/subject");
    LabeledResource object = new LabeledResource("http://example.org/object", "object");
    LabeledResource codeSystem = new LabeledResource("ex:object");
    SemanticTag<EntityType, LabeledResource, LabeledResource> tag = new SemanticTag<>("tagId", entityType, Relation.isAssociatedWith, object, codeSystem);
    when(tagService.getTagsForEntity(entityType)).thenReturn(singletonList(tag));
    writer.addStatementsForEntityTags(model, subject, entityType);
    Statement statement = valueFactory.createStatement(subject, TYPE, valueFactory.createIRI("http://example.org/object"));
    assertEquals(newArrayList(model), singletonList(statement));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) LabeledResource(org.molgenis.data.semantic.LabeledResource) Statement(org.eclipse.rdf4j.model.Statement) Model(org.eclipse.rdf4j.model.Model) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) Resource(org.eclipse.rdf4j.model.Resource) LabeledResource(org.molgenis.data.semantic.LabeledResource) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) SemanticTag(org.molgenis.data.semantic.SemanticTag) Test(org.testng.annotations.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 43 with Statement

use of org.eclipse.rdf4j.model.Statement in project opentheso by miledrousset.

the class json_ld_first_test method start_test.

@Test
public void start_test() throws FileNotFoundException, IOException, JsonLdError {
    System.out.println("");
    System.out.println("");
    System.out.println("*******************************************************");
    System.out.println("*                     JSON LD                         *");
    System.out.println("*******************************************************");
    InputStream inputStream = new FileInputStream("input_test.json");
    // Read the file into an Object (The type of this object will be a List, Map, String, Boolean,
    // Number or null depending on the root object in the file).
    Object jsonObject = JsonUtils.fromInputStream(inputStream);
    // Create a context JSON map containing prefixes and definitions
    Map context = new HashMap();
    // Customise context...
    // Create an instance of JsonLdOptions with the standard JSON-LD options
    JsonLdOptions options = new JsonLdOptions();
    // Customise options...
    // Call whichever JSONLD function you want! (e.g. compact)
    Object compact = JsonLdProcessor.compact(jsonObject, context, options);
    // Print out the result (or don't, it's your call!)
    System.out.println(JsonUtils.toPrettyString(compact));
    System.out.println("*******************************************************");
    System.out.println("*                     org.JSON                        *");
    System.out.println("*******************************************************");
    inputStream = new FileInputStream("input_test.json");
    JSONObject obj = new JSONObject(IOUtils.toString(inputStream));
    System.out.println(obj.getString("name"));
    obj.put("test", "writeIsWorking");
    System.out.println(obj.getString("test"));
    System.out.println("");
    System.out.println(obj.toString());
    System.out.println("");
    System.out.println("*******************************************************");
    System.out.println("*                     RDJF WRITE                      *");
    System.out.println("*******************************************************");
    ValueFactory vf = SimpleValueFactory.getInstance();
    // Create a new RDF model containing information about the painting "The Potato Eaters"
    ModelBuilder builder = new ModelBuilder();
    Model model = builder.setNamespace("ex", "http://example.org/").subject("ex:PotatoEaters").add("ex:creationDate", vf.createLiteral("1885-04-01T00:00:00Z", XMLSchema.DATETIME)).add("ex:peopleDepicted", 5).build();
    Rio.write(model, System.out, RDFFormat.JSONLD);
    System.out.println("");
    System.out.println("");
    System.out.println("*******************************************************");
    System.out.println("*                     RDJF READ                       *");
    System.out.println("*******************************************************");
    inputStream = new FileInputStream("input_test.json");
    model = Rio.parse(inputStream, "", RDFFormat.JSONLD);
    Rio.write(model, System.out, RDFFormat.JSONLD);
    System.out.println("");
    for (Statement st : model) {
        // we want to see the object values of each statement
        Value value = st.getObject();
        IRI property = st.getPredicate();
        if (property.getLocalName().equals("name")) {
            Literal title = (Literal) value;
            System.out.println("name: " + title.getLabel());
        }
    }
    System.out.println("");
    System.out.println("");
    System.out.println("");
    System.out.println("");
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) HashMap(java.util.HashMap) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Statement(org.eclipse.rdf4j.model.Statement) ValueFactory(org.eclipse.rdf4j.model.ValueFactory) SimpleValueFactory(org.eclipse.rdf4j.model.impl.SimpleValueFactory) FileInputStream(java.io.FileInputStream) ModelBuilder(org.eclipse.rdf4j.model.util.ModelBuilder) JsonLdOptions(com.github.jsonldjava.core.JsonLdOptions) JSONObject(org.primefaces.json.JSONObject) Literal(org.eclipse.rdf4j.model.Literal) Model(org.eclipse.rdf4j.model.Model) Value(org.eclipse.rdf4j.model.Value) JSONObject(org.primefaces.json.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 44 with Statement

use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.

the class TriGHandlingTest method writeTriG.

/**
 * Helper method to write the given model to TriG and return an InputStream containing the results.
 *
 * @param statements
 * @return An {@link InputStream} containing the results.
 * @throws RDFHandlerException
 */
private InputStream writeTriG(Model statements) throws RDFHandlerException {
    StringWriter writer = new StringWriter();
    RDFWriter trigWriter = new TriGWriter(writer);
    trigWriter.startRDF();
    for (Statement nextStatement : statements) {
        trigWriter.handleStatement(nextStatement);
    }
    trigWriter.endRDF();
    return new ByteArrayInputStream(writer.toString().getBytes(Charset.forName("UTF-8")));
}
Also used : StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) Statement(org.eclipse.rdf4j.model.Statement) RDFWriter(org.eclipse.rdf4j.rio.RDFWriter) TriGWriter(org.eclipse.rdf4j.rio.trig.TriGWriter)

Example 45 with Statement

use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.

the class LinkedHashModel method readObject.

private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
    // Read in any hidden serialization magic
    s.defaultReadObject();
    // Read in size
    int size = s.readInt();
    values = new HashMap<Value, ModelNode>(size * 2);
    statements = new LinkedHashSet<ModelStatement>(size);
    // Read in all elements
    for (int i = 0; i < size; i++) {
        Statement st = (Statement) s.readObject();
        add(st);
    }
}
Also used : Statement(org.eclipse.rdf4j.model.Statement) Value(org.eclipse.rdf4j.model.Value)

Aggregations

Statement (org.eclipse.rdf4j.model.Statement)74 IRI (org.eclipse.rdf4j.model.IRI)17 Test (org.junit.Test)17 Model (org.eclipse.rdf4j.model.Model)16 Literal (org.eclipse.rdf4j.model.Literal)15 Resource (org.eclipse.rdf4j.model.Resource)15 Value (org.eclipse.rdf4j.model.Value)13 RDFWriter (org.eclipse.rdf4j.rio.RDFWriter)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ArrayList (java.util.ArrayList)10 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)8 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)8 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)8 StringWriter (java.io.StringWriter)7 BNode (org.eclipse.rdf4j.model.BNode)6 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)6 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)6 RepositoryConnection (org.eclipse.rdf4j.repository.RepositoryConnection)6 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)6 IOException (java.io.IOException)5