Giter Site home page Giter Site logo

rdf-rs's Introduction

rdf-rs

Note: This project is work in progress and currently not stable.

rdf is a library for the Resource Description Framework (RDF) and SPARQL implemented in Rust.

This project is a way for me to learn Rust and combine it with my interests in semantic web technologies.

Usage

Add this to your Cargo.toml:

[dependencies]
rdf = "0.1.4"

Basic Examples

RDF triples can be stored and represented in a graph.

use rdf::graph::Graph;
use rdf::uri::Uri;
use rdf::triple::Triple;

let mut graph = Graph::new(None);
let subject = graph.create_blank_node();
let predicate = graph.create_uri_node(&Uri::new("http://example.org/show/localName".to_string()));
let object = graph.create_blank_node();
let triple = Triple::new(&subject, &predicate, &object);

graph.add_triple(&triple);

RDF graphs can be serialized to a supported format.

use rdf::writer::n_triples_writer::NTriplesWriter;
use rdf::writer::rdf_writer::RdfWriter;
use rdf::graph::Graph;
use rdf::uri::Uri;
use rdf::triple::Triple;

let writer = NTriplesWriter::new();

let mut graph = Graph::new(None);
let subject = graph.create_blank_node();
let predicate = graph.create_uri_node(&Uri::new("http://example.org/show/localName".to_string()));
let object = graph.create_blank_node();
let triple = Triple::new(&subject, &predicate, &object);

graph.add_triple(&triple);
assert_eq!(writer.write_to_string(&graph).unwrap(),
           "_:auto0 <http://example.org/show/localName> _:auto1 .\n".to_string());

RDF syntax can also be parsed and transformed into an RDF graph.

use rdf::reader::turtle_parser::TurtleParser;
use rdf::reader::rdf_parser::RdfParser;
use rdf::uri::Uri;

let input = "@base <http://example.org/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

<http://www.w3.org/2001/sw/RDFCore/ntriples/> rdf:type foaf:Document ;
        <http://purl.org/dc/terms/title> \"N-Triples\"@en-US ;
        foaf:maker _:art .";

let mut reader = TurtleParser::from_string(input.to_string());
match reader.decode() {
  Ok(graph) => {
    assert_eq!(graph.count(), 3);
    assert_eq!(graph.namespaces().len(), 2);
    assert_eq!(graph.base_uri(), &Some(Uri::new("http://example.org/".to_string())))
  },
  Err(_) => assert!(false)
}

Current State

Currently rdf-rs provides basic data structures for representing RDF graphs, triples and nodes. The following formats can be parsed and serialized:

  • Turtle
  • N-Triples

Future Work and Ideas

  • Support querying with SPARQL
  • Add support for more formats
  • More comprehensive Uri data structure

rdf-rs's People

Contributors

iddan avatar jervenbolleman avatar marcantoine-arnaud avatar scholtzan avatar sparika avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rdf-rs's Issues

parsing failure on many valid ttl file.

Hi,

Because i'm searching a rust rdf library for a project in LV2 context, I looked at your's and tested it by parsing all lv2 related ttl on my system. While those file are valid, the parsing failed for a majority of them with various error message. I suggest to use the W3C turtle test suit to better know what are the commonly expected result.

PS: why it's not possible to post an issue on the the media-io which is the repo referenced in crates.io ? does that mean this project is dead ?

Readers fail to parse escaped characters

Related to #4 , it appears that both turtle and ntriples reader are not able to process literal containing escaped character with a \.

@base <file> .
_:138df0aa-cf7c-4457-5377-26957f4df295 <number> "1571909016132" ;
        <escape> "\"escaped\"string" ;

Errors are the following and disappear when removing the line containing escaped characters.

[2019-11-21T10:26:17Z ERROR program] Invalid token while reading Turtle triples.
[2019-11-21T10:20:35Z ERROR program] Error while parsing NTriples syntax.

Any idea as to where to look ?

Characters are not actually escaped

The RdfSyntaxSpecs::escape_literal has no actual effect on the litteral being escaped. The function uses https://doc.rust-lang.org/std/string/struct.String.html#method.replace which returns a String but this String is not affected to the escaped_literal variable.

Besides, the related tests are incorrect :

        let node = Node::LiteralNode {
            literal: "literal ' \" ".to_string(),
            data_type: None,
            language: None,
        };

        assert_eq!(
            formatter.format_node(&node),
            "\"literal \' \" \"".to_string()
        );

As the litteral is enclosed with ", ' and \' are equivalent, the test also compares \" to \", and it is missing the test for escaping \ so the test does not demonstrate escaping.

I suggest testing against

        assert_eq!(
            formatter.format_node(&node),
            "\"literal \\' \\\\ \\\" \"".to_string()
        );

I suggest to replace the use of RdfSyntaxSpecs::escape_literal with https://doc.servo.org/std/str/struct.EscapeDefault.html or https://doc.servo.org/std/str/struct.EscapeDebug.html.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.