A pure Dart package for working with RDF (resource description framework).

Overview

RDFLib

A pure Dart package for working with RDF (resource description framework).

Features

  • Create triple instances (with data types)
  • Create a graph to store triples without duplicates
  • Find triples based on criteria
  • Export graph to turtle ttl format (default)
    • Export to encrypted turtle ttl file with AES encryption
  • Bind long namespace with customized shortened name for readability
  • Include reserved vocabulary of OWL 2
  • Parse local turtle ttl file and store triples in the graph in memory
    • Parse encrypted turtle ttl file which is encrypted using AES

Getting started

Refer to the code example below, or go to /example to find out more!

For testing the rdflib package

# create a dart project for testing
dart create test_rdflib
cd test_rdflib
# install rdflib as the dependency with dart pub add
dart pub add rdflib
# copy the following code to /bin/test_rdflib.dart
# run the file with dart
dart run

Usage

Head over to our GitHub repo to check out more examples!

1. General usage

The following code snippet shows how to:

  1. Create a Graph instance;
  2. Create and store triples with different data types;
  3. Find entities based on customized criteria;
  4. Bind shorted string to long namespace;
  5. Export graph data to turtle file;
import 'dart:io';
import 'package:rdflib/rdflib.dart';

main() async {
  /// the following example is modified from <https://rdflib.readthedocs.io/en/stable/gettingstarted.html#a-more-extensive-example>
  Graph g = Graph();

  URIRef example = URIRef.fullUri('http://example.org');

  // subject and predicate should be a valid URIRef instance
  URIRef donna = example.slash('donna');

  g.add(Triple(sub: donna, pre: RDF.type, obj: FOAF.Person));

  // add duplicated record
  g.add(Triple(sub: donna, pre: RDF.type, obj: FOAF.Person));
  g.add(Triple(sub: donna, pre: FOAF.nick, obj: Literal('donna', lang: 'en')));
  g.add(Triple(sub: donna, pre: FOAF.name, obj: Literal('Donna Fales')));

  // add duplicated record
  g.add(Triple(sub: donna, pre: FOAF.name, obj: Literal('Donna Fales')));
  g.add(Triple(
      sub: donna,
      pre: FOAF.mbox,
      obj: URIRef.fullUri('mailto:[email protected]')));

  // add another in the graph
  URIRef ed = example.slash('edward');
  g.add(Triple(sub: ed, pre: RDF.type, obj: FOAF.Person));
  g.add(Triple(
      sub: ed, pre: FOAF.nick, obj: Literal('ed', datatype: XSD.string)));
  g.add(Triple(sub: ed, pre: FOAF.name, obj: 'Edward Scissorhands'));
  g.add(Triple(
      sub: ed,
      pre: FOAF.mbox,
      obj: Literal('[email protected]', datatype: XSD.anyURI)));

  // TEST triples should print correctly
  print('-' * 30);
  for (Triple t in g.triples) {
    // duplicated records will not be added and printed out
    print(t);
  }

  print('-' * 30);

  // TEST correct subjects/objects should print out
  for (URIRef s in g.subjects(RDF.type, FOAF.Person)) {
    for (var o in g.objects(s, FOAF.mbox)) {
      // should print out URIRef(mailto:[email protected]) and
      // Literal([email protected], datatype: URIRef(http://www.w3.org/2001/XMLSchema#anyURI))
      print(o);
    }
  }

  // bind 'foaf' to FOAF for easy readout
  g.bind('foaf', FOAF(ns: FOAF.foaf));

  /// uncomment the following line to test binding a customized namespace
  /// g.bind('example', Namespace(ns: 'http://example.org/'));

  // export graph to turtle format, create directory if it doesn't exist
  String currentPath = Directory.current.path;
  String examplePath = '$currentPath/example';
  if (!await Directory(examplePath).exists()) {
    await Directory(examplePath).create(recursive: true);
  }
  g.serialize(format: 'ttl', dest: '$examplePath/ex1.ttl');
  // can also export to an encrypted file (will add .enc before .ttl in file name)
  g.serialize(
      format: 'ttl',
      dest: '$examplePath/ex1.ttl',
      encrypt: 'AES',
      passphrase: 'helloworld!');
}

2. SOLID Health Ontology Example

import '../lib/rdflib.dart';

main() {
  Graph g = Graph();

  Namespace shData = Namespace(ns: 'http://silo.net.au/data/SOLID-Health#');
  Namespace shOnto =
  Namespace(ns: 'http://sii.cecs.anu.edu.au/onto/SOLID-Health#');

  URIRef newAssessTab = shData.withAttr('AssessmentTab-p43623-20220727T120913');
  g.addNamedIndividual(newAssessTab);

  // Literal string
  g.add(Triple(
      sub: newAssessTab, pre: RDF.type, obj: shOnto.withAttr('AssessmentTab')));

  // Literal string
  g.add(Triple(
      sub: newAssessTab,
      pre: shOnto.withAttr('asthmaControl'),
      obj: Literal('Poor Control')));

  // Literal integer
  g.add(Triple(
      sub: newAssessTab,
      pre: shOnto.withAttr('diastolicBloodPressure'),
      obj: Literal('75')));

  // Literal float/double
  g.add(Triple(
      sub: newAssessTab,
      pre: shOnto.withAttr('systolicBloodPressure'),
      obj: Literal('125.0')));

  URIRef newSeeAndDoTab = shData.withAttr('SeeAndDoTab-p43623-20220727T120913');
  URIRef newSeeAndDoOption =
  shData.withAttr('SeeAndDoOption-p43623-20220727T120913-fitnessDrive');

  g.addNamedIndividual(newSeeAndDoTab);
  g.addNamedIndividual(newSeeAndDoOption);
  // link two triple individuals
  g.addObjectProperty(
      newSeeAndDoTab, shOnto.withAttr('hasSeeAndDoOption'), newSeeAndDoOption);

  /// binding for readability
  g.bind('sh-data', shData);
  g.bind('sh-onto', shOnto);

  g.serialize(dest: 'example/ex2.ttl');
}

3. Parsing local turtle file

import 'package:rdflib/rdflib.dart';

main() async {
  String filePath = 'example/ex1.ttl';
  // create a graph to read turtle file and store info
  Graph g = Graph();
  // wait for it to complete parsing
  await g.parse(filePath);
  // full format of triples (will use shorthand in serialization/export)
  for (Triple t in g.triples) {
    print(t);
  }
  // export it to a new file (should be equivalent to the original one)
  g.serialize(format: 'ttl', dest: 'example/ex3.ttl');
}

3.1 Parsing encrypted local turtle file

import 'package:rdflib/rdflib.dart';

main() async {
  /// create a new graph to hold the data
  Graph g = Graph();

  /// need to use await keyword
  await g.parseEncrypted('example/ex1.enc.ttl', passphrase: 'helloworld!');
  print('Contexts:\n${g.contexts}');
  print('Data:\n${g.triples}');

  /// serialize it to specified location, should be equivalent to original file
  g.serialize(format: 'ttl', dest: 'example/ex1.dec.ttl');
}

Additional information

Useful resources

  1. RDFLib
  2. Introduction to RDF

How to contribute

Make a pull request on our GitHub repo!

Acknowledgement

This rdflib dart package is modelled on the RDFLib.

Comments
  • RDF: Serialise/parse to optionally encrypt/decrypt

    RDF: Serialise/parse to optionally encrypt/decrypt

    For a Trust No ONe context, allow optional encryption key argument to parse() and serialise() to decrypt/encrypt.

    Perhaps use ttc for encrypted ttl files.

    opened by gjwgit 7
  • RDFLib: Create test files for terminal and combinator parsers using `petitparser`

    RDFLib: Create test files for terminal and combinator parsers using `petitparser`

    petitparser package provides readily made terminal parsers such as letter(), digit(), string() that can be used to parse simple snippets. To take a step further, we can also combine parsers together to get more complicated functions such as chaining parsers, specifying the repeated time a pattern should occur in a string, etc.

    For starters, refer to #625 for the tasks of parsing IRIREF and PN_CHARS_U, and use combinator parsers to parse different strings.

    opened by tian3rd 4
  • RDFLib: fix ':' and '@base' in namespace prefixes

    RDFLib: fix ':' and '@base' in namespace prefixes

    When the turtle file contains : or @base in the namespace prefixes, they'll be ignored in current RDFLib implementation. Need to save them in the graph, and parse and serialize correctly in the Graph class.

    opened by tian3rd 4
  • RDFLib: Create github actions for pushes to main branch

    RDFLib: Create github actions for pushes to main branch

    It's a better practice to adopt CI/CD workflows early in developing a package.

    Currently there's already some GitHub actions setup in the pods project, e.g., https://github.com/anusii/pods/blob/main/.github/workflows/dart.yml.

    Also refer to https://docs.github.com/en/actions/quickstart for a jump start.

    opened by tian3rd 2
  • RDFLib: support parsing long text string

    RDFLib: support parsing long text string

    In addition to read ttl data from a file, we should also be able to parse long string which is read into memory/variable. This is useful for Indi app which we get the full long text string from the http respond body.

    opened by tian3rd 2
  • RDF: test `rdfgraph` package with SOLID-Health-Ontology-Example

    RDF: test `rdfgraph` package with SOLID-Health-Ontology-Example

    Had a Zoom meeting with @Zheyuan and @Sergio last Friday discussing about how to develop the rdfgraph and integrate it in our formapp.

    For me, the focus this week would be adding three more essential methods in the graph:

    • [x] Add data type property
    • [x] Add named individual
    • [x] Add object property
    • [x] Read ttl turtle file and store triples in the graph (related to #593 )

    The above three functions in the graph will be enough for @Zheyuan to test the sample turtle data file created by Sergio.

    opened by tian3rd 2
  • RDF: fix the issue of using encryption/decryption hashed key as the real key for `AES`

    RDF: fix the issue of using encryption/decryption hashed key as the real key for `AES`

    Just found out that in the encryption/decryption process, the methods use the hashed key as the real key, and this key is actually stored in the encrypted .enc.ttl file in plaintext. This is not right. The right way to do it is to use the original key, but needs to find a way to convert different length to a fixed 32-long in order to use AES.

    • [ ] ~~Convert any key to a unique 32 length key~~
    • [x] Use this secret key to encrypt and decrypt
    opened by tian3rd 1
  • RDFLib: navigation in `Graph`

    RDFLib: navigation in `Graph`

    Add properties such as:

    • [ ] g.value to find out any triple that has a single rdf:value
    • [ ] g.subjects to extract all subjects in the graph
    • [ ] g.predicates
    • [ ] g.objects
    opened by tian3rd 0
  • RDFLib: support `Graph` operations like merging, intersection of different `Graph`s

    RDFLib: support `Graph` operations like merging, intersection of different `Graph`s

    When we want to merge two graphs together, or find out the differences between two graphs, it's useful to do that using set operations. We can add some basic methods in the Graph class to achieve similar results.

    opened by tian3rd 0
  • RDFLib: use `GrammarDefinition` for all turtle parsers, and extend and evaluate the results of parsing.

    RDFLib: use `GrammarDefinition` for all turtle parsers, and extend and evaluate the results of parsing.

    • [x] First, create a class ExpressionDefinition that extends GrammarDefinition, and its purpose is to include all 50 turtle rules inside as parsers.

    • [x] Second, for most common turtle files such as .acl and our .ttl file in PODS repo, create a EvaluatorDefinition class whose main purpose is to attach custom production actions to interpret the outcomes of the parsers. With rdflib classes such as URIRef, Graph, we can save the results in the graph.

    opened by tian3rd 2
  • RDFLib: complete the terminal/combinator parsers for turtle grammar and have them all tested

    RDFLib: complete the terminal/combinator parsers for turtle grammar and have them all tested

    • [x] Based on the grammar (https://www.w3.org/TR/turtle/#sec-grammar-grammar, 50 rules in total), write the corresponding terminal or combinator parser for each of them.
    • [x] Have each of the rules tested in the test file

    The next step is to integrate the parsers into the more flexible GrammarDefinition classes, and interpret them into triples, prefix namespaces, and finally save all the information in the Graph. Will create another issue for this next step.

    opened by tian3rd 8
  • RDFLib: Use grammar definitions as building blocks to define grammar rules for turtle

    RDFLib: Use grammar definitions as building blocks to define grammar rules for turtle

    For even more complicated grammar rules, I find it hard and messy to process the parsed result with just terminal and combinator parsers (see #11 ).

    One step further is to use built-in class GrammarDefinition in petitparser. The advantage is that I can dissect the bigger problem of parsing the whole turtle file with tens of rules into smaller chunks. GrammarDefinition gives us a way to build small blocks one by one, and we can extend different expression definitions by customizing the specific production actions.

    This also has synergy with the already written classes in the rdflib package in the way that we can use the methods already there to evaluate the parsed results.

    opened by tian3rd 3
Extract pubspec details (such as package version, author and description) into Dart code.

build_pubspec This package helps you convert fields from your pubspec.yaml file into Dart code. Based on the fields in your pubspec, this package will

dartside.dev 9 Jul 15, 2021
Imports resource files as string or binary literals in Dart code.

resource_importer Imports resource files as string or binary literals in Dart code. What? resource_importer probably is best described with an example

James D. Lin 0 Dec 3, 2022
Resource monitor - A flutter plugin for Android and IOS to monitor CPU and RAM usage of device.

resource_monitor A flutter plugin for Android and IOS to monitor CPU and RAM usage of device. TODO Return overall system cpu/memory usage. Implement A

Skandar Munir 1 Nov 11, 2022
BreakingBad-App - A description of the Braking Bad series, using flutter bloc

breaking_bad The application is a description of the Braking Bad series Using Bl

Mohamed Hossam 0 Jan 9, 2022
Flying Fish is full-stack Dart framework - a semi-opinionated framework for building applications exclusively using Dart and Flutter

Flying Fish is full-stack Dart framework - a semi-opinionated framework for building applications exclusively using Dart and Flutter.

Flutter Fish 3 Dec 27, 2022
This package helps developer to sort the flutter/dart packages and plugins alphabetically, This makes it easier when managing too many packages and when working with teams

Package helps to sort the flutter/dart packages and plugins alphabetically, This makes it easier when managing too many packages and when working with

DANCHE 7 Dec 21, 2022
Loop-dart - How to working loop in Dart

How to working loop in Dart list var list = [1, 2, 3, 4, 5, 6]; using for each

Code 041 1 Feb 18, 2022
Intel Corporation 238 Dec 24, 2022
Animated dialog box - A pure dart package for showing animated alert box.

animated_dialog_box A pure dart package for showing animated alert box. Getting Started https://github.com/Shubham-Narkhede/animated_dialog_box/blob/m

Shubham-Narkhede 10 Jul 24, 2022
A simple package for working with multithreading, using an interface similar to Task in C#.

A simple package for working with multithreading, using an interface similar to Task in C#.

Gleb Batykov 11 Oct 24, 2022
Flutter package implements Sign Google redirect(working for incognito mode)

google_sign_in_web_redirect Flutter package implements Sign Google redirect(working for incognito mode). Usage Import the package dependencies: goog

null 2 Dec 15, 2022
Life-saving helpers for working with JavaScript libraries when compiling Dart/Flutter to Web.

dartified Life-saving helpers for working with JavaScript libraries when compiling Dart/Flutter to Web. Features The functions included in this librar

Hammed Oyedele 2 Aug 19, 2022
The ROHD Verification Framework is a hardware verification framework built upon ROHD for building testbenches.

ROHD Verification Framework The ROHD Verification Framework (ROHD-VF) is a verification framework built upon the Rapid Open Hardware Development (ROHD

Intel Corporation 18 Dec 20, 2022
Pure Dart Client for Nakama Server 🌟🥰🤩

Nakama Flutter Client ?? ?? ?? ?? Nakama is an open-source scalable game server. This is a Flutter client for Nakama written in pure dart and supports

Oliver Brunsmann 57 Dec 6, 2022
Lightweight and blazing fast key-value database written in pure Dart.

Fast, Enjoyable & Secure NoSQL Database Hive is a lightweight and blazing fast key-value database written in pure Dart. Inspired by Bitcask. Documenta

HiveDB 3.4k Dec 30, 2022
Lightweight and blazing fast key-value database written in pure Dart.

Fast, Enjoyable & Secure NoSQL Database Hive is a lightweight and blazing fast key-value database written in pure Dart. Inspired by Bitcask. Documenta

HiveDB 3.4k Dec 30, 2022
A simple day / night switcher widget made in pure Dart.

DayNightSwitcher Just a simple simple day / night switcher widget made in pure Dart. It allows you to quickly show a beautiful dark mode switcher widg

Hugo Delaunay 38 Oct 28, 2022
Dart port of FormCoreJS: A minimal pure functional language based on self dependent types.

FormCore.js port to Dart. So far only the parser and typechecker have been ported i.e. the FormCore.js file in the original repo. (Original readme fro

Modestas Valauskas 2 Jan 28, 2022
A pure Dart utility library that checks for an internet connection by opening a socket to a list of specified addresses, each with individual port and timeout. Defaults are provided for convenience.

data_connection_checker A pure Dart utility library that checks for an internet connection by opening a socket to a list of specified addresses, each

Kristiyan Mitev 103 Nov 29, 2022