Writing RDF formated strings to sesame repository

I can't figure put how to write a couple of RDF N3 formated String ('s) to my sesame repo. I have the following ArrayList<String> that contains for each index a row of the following text(a row ends with an ; ):

@prefix gstruct: <http://cs.lth.se/ontologies/gstruct.owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

gstruct:sentence_1  rdf:type    gstruct:Sentence ;
    gstruct:inx 1 ;
    gstruct:words
        [gstruct:id 1 ; gstruct:form "The" ; gstruct:lemma "the" ; gstruct:pos "DT" ; gstruct:head "4" ; gstruct:deprel "NMOD"],
        [gstruct:id 2 ; gstruct:form "Series" ; gstruct:lemma "series" ; gstruct:pos "NNP" ; gstruct:head "3" ; gstruct:deprel "NMOD"],
        [gstruct:id 3 ; gstruct:form "800" ; gstruct:lemma "800" ; gstruct:pos "CD" ; gstruct:head "4" ; gstruct:deprel "NMOD"],
        [gstruct:id 4 ; gstruct:form "Terminator" ; gstruct:lemma "terminator" ; gstruct:pos "NNP" ; gstruct:head "5" ; gstruct:deprel "SBJ"],
        [gstruct:id 5 ; gstruct:form "is" ; gstruct:lemma "be" ; gstruct:pos "VBZ" ; gstruct:head "0" ; gstruct:deprel "ROOT"],
        [gstruct:id 6 ; gstruct:form "a" ; gstruct:lemma "a" ; gstruct:pos "DT" ; gstruct:head "7" ; gstruct:deprel "NMOD"],
        [gstruct:id 7 ; gstruct:form "type" ; gstruct:lemma "type" ; gstruct:pos "NN" ; gstruct:head "5" ; gstruct:deprel "PRD"],
        [gstruct:id 8 ; gstruct:form "of" ; gstruct:lemma "of" ; gstruct:pos "IN" ; gstruct:head "7" ; gstruct:deprel "NMOD"],
        [gstruct:id 9 ; gstruct:form "Terminator" ; gstruct:lemma "terminator" ; gstruct:pos "NN" ; gstruct:head "10" ; gstruct:deprel "NMOD"],
        [gstruct:id 10 ; gstruct:form "mass" ; gstruct:lemma "mass" ; gstruct:pos "NN" ; gstruct:head "8" ; gstruct:deprel "PMOD"],
        [gstruct:id 11 ; gstruct:form "produced" ; gstruct:lemma "produce" ; gstruct:pos "VBN" ; gstruct:head "7" ; gstruct:deprel "APPO"],
        [gstruct:id 12 ; gstruct:form "by" ; gstruct:lemma "by" ; gstruct:pos "IN" ; gstruct:head "11" ; gstruct:deprel "LGS"],
        [gstruct:id 13 ; gstruct:form "Skynet" ; gstruct:lemma "skynet" ; gstruct:pos "NNP" ; gstruct:head "12" ; gstruct:deprel "PMOD"],
        [gstruct:id 14 ; gstruct:form "." ; gstruct:lemma "." ; gstruct:pos "." ; gstruct:head "5" ; gstruct:deprel "P"];
    gstruct:predicates
        [gstruct:id 7 ; gstruct:predsense "type.01" ; gstruct:args
            [gstruct:id 7 ;  gstruct:argtype "A2"],
            [gstruct:id 8 ;  gstruct:argtype "A1"]],
        [gstruct:id 10 ; gstruct:predsense "mass.02" ; gstruct:args
            [gstruct:id 9 ;  gstruct:argtype "A1"]],
        [gstruct:id 11 ; gstruct:predsense "produce.01" ; gstruct:args
            [gstruct:id 7 ;  gstruct:argtype "A1"],
            [gstruct:id 12 ;  gstruct:argtype "A0"]].

I want to use RepositoryConnection add method to add this and also add a URI and a context in form of a URL. I managed to figure out how to create the URI but i can not manage to figure out what i mentioned above and how to make my URL which is a String to a Resource object.

Upvotes: 1

Views: 110

Answers (1)

Jeen Broekstra
Jeen Broekstra

Reputation: 22053

I'm not sure why you have this as a List with each line as a separate entry, because that is a rather awkward way to process it. For one thing, as Joshua also remarks, the lines are dependent on each other: while the whole may be legal N3 syntax, each line by itself is not a legal N3 string.

The simplest way to add this data into a Sesame repository is probably to just convert this into one single String, and then load it as follows:

String data = "@prefix gstruct: ..."; // full N3 data
conn.add(new StringReader(data), "", RDFFormat.N3);

If you want to add the data to a specifc context within the repository:

 URI context = conn.getValueFactory().createURI("http://example.org/context");
 conn.add(new StringReader(data), "", RDFFormat.N3, context);

Assuming the N3 data you have actually comes from a file on disk, you can optimize this further: instead of creating a String object first and then using a StringReader, you can just input the file object itself:

File file = new File("/path/to/file.n3");
conn.add(file, "", RDFFormat.N3, context);

As an aside: N3 syntax is a not-fully-standardized format, and you may find that different parsers/writers support slightly different subsets of it. The data you posted doesn't look exotic so you should be alright with this, but if you have the choice, I would recommend switching to a different syntax format that is more fully standardized, like Turtle or N-Triples.

Turtle syntax is actually very similar to N3 (your posted data is valid Turtle as-is), so switching should require minimal to no changes.

Upvotes: 1

Related Questions