Search in sources :

Example 51 with Literal

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

the class QueryStringUtilTest method testTypedLiteral.

@Test
public void testTypedLiteral() {
    Literal literal = VF.createLiteral("typed \"literal\"", VF.createIRI("http://example.com/test"));
    assertEquals("\"typed \\\"literal\\\"\"^^<http://example.com/test>", QueryStringUtil.valueToString(literal));
    assertEquals("\"typed \\\"literal\\\"\"^^<http://example.com/test>", valueToStringWithStringBuilder(literal));
}
Also used : Literal(org.eclipse.rdf4j.model.Literal) Test(org.junit.Test)

Example 52 with Literal

use of org.eclipse.rdf4j.model.Literal in project graal by graphik-team.

the class AbstractRDFListener method handleStatement.

@Override
public void handleStatement(Statement st) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(st.toString());
    }
    Predicate predicate = new Predicate(new DefaultURI(st.getPredicate().toString()), 2);
    Term subject = DefaultTermFactory.instance().createConstant(new DefaultURI(st.getSubject().toString()));
    Term object;
    fr.lirmm.graphik.util.URI datatype;
    if (st.getObject() instanceof Literal) {
        Literal l = (Literal) st.getObject();
        if (l.getDatatype() == null) {
            datatype = URIUtils.RDF_LANG_STRING;
        } else {
            datatype = new fr.lirmm.graphik.util.DefaultURI(l.getDatatype().getNamespace(), l.getDatatype().getLocalName());
        }
        String value = l.getLabel();
        if (datatype.equals(URIUtils.RDF_LANG_STRING)) {
            value += "@" + l.getLanguage();
        }
        object = DefaultTermFactory.instance().createLiteral(datatype, value);
    } else {
        object = DefaultTermFactory.instance().createConstant(new DefaultURI(st.getObject().toString()));
    }
    DefaultAtom a = new DefaultAtom(predicate, subject, object);
    this.createAtom(a);
}
Also used : DefaultURI(fr.lirmm.graphik.util.DefaultURI) Literal(org.eclipse.rdf4j.model.Literal) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Term(fr.lirmm.graphik.graal.api.core.Term) DefaultURI(fr.lirmm.graphik.util.DefaultURI) Predicate(fr.lirmm.graphik.graal.api.core.Predicate)

Example 53 with Literal

use of org.eclipse.rdf4j.model.Literal 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 54 with Literal

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

the class WriteRdf4j method writeDate.

private void writeDate(SKOSResource resource) {
    int prop;
    Literal literal;
    for (SKOSDate date : resource.getDateList()) {
        literal = vf.createLiteral(date.getDate(), XMLSchema.DATE);
        prop = date.getProperty();
        switch(prop) {
            case SKOSProperty.created:
                builder.add(DCTERMS.CREATED, literal);
                break;
            case SKOSProperty.modified:
                builder.add(DCTERMS.MODIFIED, literal);
                break;
            case SKOSProperty.date:
                builder.add(DCTERMS.DATE, literal);
                break;
            default:
                break;
        }
    }
}
Also used : Literal(org.eclipse.rdf4j.model.Literal) SKOSDate(mom.trd.opentheso.skosapi.SKOSDate)

Example 55 with Literal

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

the class WriteRdf4j method writeDocumentation.

private void writeDocumentation(SKOSResource resource) {
    int prop;
    for (SKOSDocumentation doc : resource.getDocumentationsList()) {
        prop = doc.getProperty();
        Literal literal = vf.createLiteral(doc.getText(), doc.getLanguage());
        switch(prop) {
            case SKOSProperty.definition:
                builder.add(SKOS.DEFINITION, literal);
                break;
            case SKOSProperty.scopeNote:
                builder.add(SKOS.SCOPE_NOTE, literal);
                break;
            case SKOSProperty.example:
                builder.add(SKOS.EXAMPLE, literal);
                break;
            case SKOSProperty.historyNote:
                builder.add(SKOS.HISTORY_NOTE, literal);
                break;
            case SKOSProperty.editorialNote:
                builder.add(SKOS.EDITORIAL_NOTE, literal);
                break;
            case SKOSProperty.changeNote:
                builder.add(SKOS.CHANGE_NOTE, literal);
                break;
            case SKOSProperty.note:
                builder.add(SKOS.NOTE, literal);
                break;
            default:
                break;
        }
    }
}
Also used : SKOSDocumentation(mom.trd.opentheso.skosapi.SKOSDocumentation) Literal(org.eclipse.rdf4j.model.Literal)

Aggregations

Literal (org.eclipse.rdf4j.model.Literal)96 Test (org.junit.Test)52 IRI (org.eclipse.rdf4j.model.IRI)34 Value (org.eclipse.rdf4j.model.Value)17 BNode (org.eclipse.rdf4j.model.BNode)14 Model (org.eclipse.rdf4j.model.Model)13 Statement (org.eclipse.rdf4j.model.Statement)13 Resource (org.eclipse.rdf4j.model.Resource)9 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)8 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)7 InputStream (java.io.InputStream)6 Date (java.util.Date)6 BindingSet (org.eclipse.rdf4j.query.BindingSet)5 IOException (java.io.IOException)4 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)4 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)4 AbstractQueryResultIOTupleTest (org.eclipse.rdf4j.query.resultio.AbstractQueryResultIOTupleTest)4 QueryResultCollector (org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector)4 SPARQLResultsJSONParser (org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLResultsJSONParser)4 ArrayList (java.util.ArrayList)3