Search in sources :

Example 1 with Resource

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

the class RDF4jUtils method valueToTerm.

public Term valueToTerm(Value value) {
    if (value instanceof Resource) {
        return DefaultTermFactory.instance().createConstant(urizer.output(value.toString()));
    } else {
        //  Literal
        org.eclipse.rdf4j.model.Literal l = (org.eclipse.rdf4j.model.Literal) value;
        URI uri = URIUtils.createURI(l.getDatatype().toString());
        String label = l.getLabel();
        if (uri.equals(URIUtils.RDF_LANG_STRING)) {
            String opt = l.getLanguage();
            label += "@";
            label += opt;
        }
        return DefaultTermFactory.instance().createLiteral(uri, label);
    }
}
Also used : Literal(fr.lirmm.graphik.graal.api.core.Literal) Resource(org.eclipse.rdf4j.model.Resource) URI(fr.lirmm.graphik.util.URI)

Example 2 with Resource

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

the class LocalRepositoryManagerTest method testModifySystemRepository.

@Test
public void testModifySystemRepository() {
    RepositoryConfig config = manager.getRepositoryConfig(TEST_REPO);
    manager.addRepositoryConfig(new RepositoryConfig(SystemRepository.ID, new SystemRepositoryConfig()));
    manager.shutDown();
    manager = new LocalRepositoryManager(datadir);
    manager.initialize();
    try (RepositoryConnection con = manager.getSystemRepository().getConnection()) {
        Model model = new TreeModel();
        config.setTitle("Changed");
        config.export(model, con.getValueFactory().createBNode());
        Resource ctx = RepositoryConfigUtil.getContext(con, config.getID());
        con.begin();
        con.clear(ctx);
        con.add(model, ctx == null ? con.getValueFactory().createBNode() : ctx);
        con.commit();
    }
    assertEquals("Changed", manager.getRepositoryConfig(TEST_REPO).getTitle());
}
Also used : SailRepositoryConfig(org.eclipse.rdf4j.repository.sail.config.SailRepositoryConfig) ProxyRepositoryConfig(org.eclipse.rdf4j.repository.sail.config.ProxyRepositoryConfig) RepositoryConfig(org.eclipse.rdf4j.repository.config.RepositoryConfig) RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) TreeModel(org.eclipse.rdf4j.model.impl.TreeModel) Model(org.eclipse.rdf4j.model.Model) TreeModel(org.eclipse.rdf4j.model.impl.TreeModel) Resource(org.eclipse.rdf4j.model.Resource) Test(org.junit.Test)

Example 3 with Resource

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

the class TurtleParser method parseImplicitBlank.

/**
 * Parses an implicit blank node. This method parses the token <tt>[]</tt>
 * and predicateObjectLists that are surrounded by square brackets.
 */
protected Resource parseImplicitBlank() throws IOException, RDFParseException, RDFHandlerException {
    verifyCharacterOrFail(readCodePoint(), "[");
    Resource bNode = createNode();
    if (subject != null) {
        reportStatement(subject, predicate, bNode);
    }
    skipWSC();
    int c = readCodePoint();
    if (c != ']') {
        unread(c);
        // Remember current subject and predicate
        Resource oldSubject = subject;
        IRI oldPredicate = predicate;
        // generated bNode becomes subject
        subject = bNode;
        // Enter recursion with nested predicate-object list
        skipWSC();
        parsePredicateObjectList();
        skipWSC();
        // Read closing bracket
        verifyCharacterOrFail(readCodePoint(), "]");
        // Restore previous subject and predicate
        subject = oldSubject;
        predicate = oldPredicate;
    }
    return bNode;
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Resource(org.eclipse.rdf4j.model.Resource)

Example 4 with Resource

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

the class TriXWriter method handleStatement.

public void handleStatement(Statement st) throws RDFHandlerException {
    if (!writingStarted) {
        throw new RDFHandlerException("Document writing has not yet been started");
    }
    try {
        Resource context = st.getContext();
        if (inActiveContext && !contextsEquals(context, currentContext)) {
            // Close currently active context
            xmlWriter.endTag(CONTEXT_TAG);
            inActiveContext = false;
        }
        if (!inActiveContext) {
            // Open new context
            xmlWriter.startTag(CONTEXT_TAG);
            if (context != null) {
                writeValue(context);
            }
            currentContext = context;
            inActiveContext = true;
        }
        xmlWriter.startTag(TRIPLE_TAG);
        writeValue(st.getSubject());
        writeValue(st.getPredicate());
        writeValue(st.getObject());
        xmlWriter.endTag(TRIPLE_TAG);
    } catch (IOException e) {
        throw new RDFHandlerException(e);
    }
}
Also used : RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) Resource(org.eclipse.rdf4j.model.Resource) IOException(java.io.IOException)

Example 5 with Resource

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

the class TestTurtleParser method rdfXmlLoadedFromInsideAJarResolvesRelativeUris.

@Test
public void rdfXmlLoadedFromInsideAJarResolvesRelativeUris() throws Exception {
    URL zipfileUrl = TestTurtleParser.class.getResource("sample-with-turtle-data.zip");
    assertNotNull("The sample-with-turtle-data.zip file must be present for this test", zipfileUrl);
    String url = "jar:" + zipfileUrl + "!/index.ttl";
    RDFParser parser = new TurtleParser();
    StatementCollector sc = new StatementCollector();
    parser.setRDFHandler(sc);
    InputStream in = new URL(url).openStream();
    parser.parse(in, url);
    in.close();
    Collection<Statement> stmts = sc.getStatements();
    assertThat(stmts, Matchers.<Statement>iterableWithSize(2));
    Iterator<Statement> iter = stmts.iterator();
    Statement stmt1 = iter.next(), stmt2 = iter.next();
    assertEquals(vf.createIRI("http://www.example.com/#"), stmt1.getSubject());
    assertEquals(vf.createIRI("http://www.example.com/ns/#document-about"), stmt1.getPredicate());
    Resource res = (Resource) stmt1.getObject();
    String resourceUrl = res.stringValue();
    assertThat(resourceUrl, CoreMatchers.startsWith("jar:" + zipfileUrl + "!"));
    URL javaUrl = new URL(resourceUrl);
    assertEquals("jar", javaUrl.getProtocol());
    InputStream uc = javaUrl.openStream();
    assertEquals("The resource stream should be empty", -1, uc.read());
    uc.close();
    assertEquals(res, stmt2.getSubject());
    assertEquals(DC.TITLE, stmt2.getPredicate());
    assertEquals(vf.createLiteral("Empty File"), stmt2.getObject());
}
Also used : StatementCollector(org.eclipse.rdf4j.rio.helpers.StatementCollector) InputStream(java.io.InputStream) Statement(org.eclipse.rdf4j.model.Statement) Resource(org.eclipse.rdf4j.model.Resource) RDFParser(org.eclipse.rdf4j.rio.RDFParser) URL(java.net.URL) Test(org.junit.Test)

Aggregations

Resource (org.eclipse.rdf4j.model.Resource)90 IRI (org.eclipse.rdf4j.model.IRI)37 Value (org.eclipse.rdf4j.model.Value)30 Test (org.junit.Test)16 Statement (org.eclipse.rdf4j.model.Statement)15 Model (org.eclipse.rdf4j.model.Model)12 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)12 BNode (org.eclipse.rdf4j.model.BNode)11 IOException (java.io.IOException)9 Literal (org.eclipse.rdf4j.model.Literal)9 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)7 StringWriter (java.io.StringWriter)6 ParsedIRI (org.eclipse.rdf4j.common.net.ParsedIRI)6 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)6 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)6 TreeModel (org.eclipse.rdf4j.model.impl.TreeModel)6 RepositoryConnection (org.eclipse.rdf4j.repository.RepositoryConnection)6 RDFWriter (org.eclipse.rdf4j.rio.RDFWriter)6 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)5 ArrayList (java.util.ArrayList)4