Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
zheyxu committed Nov 27, 2023
1 parent 4b24369 commit 48566c2
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 149 deletions.
73 changes: 36 additions & 37 deletions example/example_1.dart
@@ -1,51 +1,50 @@
import 'package:rdflib/rdflib.dart';

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

// Initialize a Graph
Graph g = Graph();

// Create a new URIRef instance for a person
final donna = URIRef('http://example.org/donna');
// Define namespaces for later use
Namespace shData = Namespace(ns: 'http://silo.net.au/data/SOLID-Health#');
Namespace shOnto =
Namespace(ns: 'http://sii.cecs.anu.edu.au/onto/SOLID-Health#');

// Create a subject
URIRef newAssessTab = shData.withAttr('AssessmentTab-p43623-20220727T120913');

// Add triples to the Graph using [Graph.addTriplesToGroups] method
g.addTripleToGroups(donna, RDF.type, FOAF.Person);
g.addTripleToGroups(donna, FOAF.nick, Literal('donna', lang: 'en'));
g.addTripleToGroups(donna, FOAF.name, Literal('Donna Fales'));
g.addTripleToGroups(donna, FOAF.mbox, URIRef('mailto:donna@example.org'));
// Adding a duplicate triple should be ignored
g.addTripleToGroups(donna, FOAF.mbox, URIRef('mailto:donna@example.org'));
// Add the entity to the Graph, equivalent to
// g.addTripleToGroups(newAssessTab, rdf.typ, owl:NamedIndividual)
g.addNamedIndividualToGroups(newAssessTab);

// Create another URIRef instance
final ed = URIRef('http://example.org/edward');
// Add using a Triple type
Triple t1 = Triple(
sub: newAssessTab, pre: RDF.type, obj: shOnto.withAttr('AssessmentTab'));
g.addTripleToGroups(t1.sub, t1.pre, t1.obj);

// Add triples to the Graph
g.addTripleToGroups(ed, RDF.type, FOAF.Person);
g.addTripleToGroups(ed, FOAF.nick, Literal('ed', datatype: XSD.string));
g.addTripleToGroups(ed, FOAF.name, Literal('Edward Scissorhands'));
// Add directly using sub, pre, and obj
g.addTripleToGroups(
newAssessTab, shData.withAttr('asthmaControl'), 'Poor Control');
g.addTripleToGroups(
newAssessTab, shOnto.withAttr('diastolicBloodPressure'), '75');
g.addTripleToGroups(
ed, FOAF.mbox, Literal('mailto:ed@example.org', datatype: XSD.anyURI));
newAssessTab, shOnto.withAttr('systolicBloodPressure'), Literal('125.0'));

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

g.addNamedIndividualToGroups(newSeeAndDoTab);
g.addNamedIndividualToGroups(newSeeAndDoOption);

// Link two triple individuals by a relation
g.addObjectProperty(
newSeeAndDoTab, shOnto.withAttr('hasSeeAndDoOption'), newSeeAndDoOption);

// Bind the long namespace to shorter string for better readability
g.bind('example', Namespace(ns: 'http://example.org/'));
// Bind to shorter abbreviations for readability
g.bind('sh-data', shData);
g.bind('sh-onto', shOnto);

// Serialize the Graph to the standard turtle format, and the result is stored
// in [Graph.serializedString]
// Serialize the graph for output
g.serialize(format: 'ttl', abbr: 'short');
print('-------\nSerialized content:\n${g.serializedString}');

// Print out every added triple in the graph by iterating through the set
print('-------\nTriples updated in the graph:');
for (Triple t in g.triples) {
print(t);
}

// Print out each person's mailbox value
print('-------\nMailboxes:');
for (var sub in g.subjects(a, FOAF.Person)) {
for (var mbox in g.objects(sub, FOAF.mbox)) {
print('${sub}\'s mailbox: ${mbox.value}');
}
}
print(g.serializedString);
}
62 changes: 19 additions & 43 deletions example/example_2.dart
@@ -1,50 +1,26 @@
import 'package:rdflib/rdflib.dart';

main() {
// Initialize a Graph
Graph g = Graph();

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

// Create a subject
URIRef newAssessTab = shData.withAttr('AssessmentTab-p43623-20220727T120913');

// Add the entity to the Graph, equivalent to
// g.addTripleToGroups(newAssessTab, rdf.typ, owl:NamedIndividual)
g.addNamedIndividualToGroups(newAssessTab);
import 'dart:io';

// Add using a Triple type
Triple t1 = Triple(
sub: newAssessTab, pre: RDF.type, obj: shOnto.withAttr('AssessmentTab'));
g.addTripleToGroups(t1.sub, t1.pre, t1.obj);

// Add directly using sub, pre, and obj
g.addTripleToGroups(
newAssessTab, shData.withAttr('asthmaControl'), 'Poor Control');
g.addTripleToGroups(
newAssessTab, shOnto.withAttr('diastolicBloodPressure'), '75');
g.addTripleToGroups(
newAssessTab, shOnto.withAttr('systolicBloodPressure'), Literal('125.0'));

URIRef newSeeAndDoTab = shData.withAttr('SeeAndDoTab-p43623-20220727T120913');
URIRef newSeeAndDoOption =
shData.withAttr('SeeAndDoOption-p43623-20220727T120913-fitnessDrive');
import 'package:rdflib/rdflib.dart';

g.addNamedIndividualToGroups(newSeeAndDoTab);
g.addNamedIndividualToGroups(newSeeAndDoOption);
main() async {
String filePath = 'example/sample_ttl_1.ttl';
// Read file content to a local String
String fileContents = await File(filePath).readAsStringSync();
print('-------Original file-------\n$fileContents');

// Link two triple individuals by a relation
g.addObjectProperty(
newSeeAndDoTab, shOnto.withAttr('hasSeeAndDoOption'), newSeeAndDoOption);
// create a graph to read turtle file and store info
Graph g = Graph();

// Bind to shorter abbreviations for readability
g.bind('sh-data', shData);
g.bind('sh-onto', shOnto);
// Parse with the new method [Graph.parseTurtle] instead of [Graph.parse] (deprecated)
g.parseTurtle(fileContents);

// Serialize the graph for output
// Serialize the Graph for output
g.serialize(format: 'ttl', abbr: 'short');
print(g.serializedString);
print('-------Serialized String--------\n${g.serializedString}');

// Print out full format of triples (will use shorthand in serialization/export)
print('--------All triples in the graph-------');
for (Triple t in g.triples) {
print(t);
}
}
42 changes: 25 additions & 17 deletions example/example_3.dart
@@ -1,26 +1,34 @@
import 'dart:io';
import 'package:http/http.dart' as http;

import 'package:rdflib/rdflib.dart';

main() async {
String filePath = 'example/sample_ttl_1.ttl';
// Read file content to a local String
String fileContents = await File(filePath).readAsStringSync();
print('-------Original file-------\n$fileContents');
// https://github.com/anusii/rdflib/blob/main/example/sample_acl_1.acl
// https://raw.githubusercontent.com/anusii/rdflib/main/example/sample_acl_1.acl
var url = Uri.https('raw.githubusercontent.com',
'anusii/rdflib/main/example/sample_acl_1.acl');
// Get the contents of the acl file
var res = await http.get(url);
String aclContents = res.body;
print('-------Original ACL Contents-------\n${res.body}\n');

// create a graph to read turtle file and store info
// Initialize a Graph to store all the info
Graph g = Graph();

// Parse with the new method [Graph.parseTurtle] instead of [Graph.parse] (deprecated)
g.parseTurtle(fileContents);

// Serialize the Graph for output
// Parse the contents and update the triples
g.parseTurtle(aclContents);
g.serialize(format: 'ttl', abbr: 'short');
print('-------Serialized String--------\n${g.serializedString}');
print('-------Serialized ACL Contents------\n${g.serializedString}\n');

// Print out full format of triples (will use shorthand in serialization/export)
print('--------All triples in the graph-------');
for (Triple t in g.triples) {
print(t);
}
// Add 'zack' to the ACL file
g.addTripleToGroups('<#zack>', a, 'acl:Authorization');
// Specify which document/fold
g.addTripleToGroups('<#zack>', 'acl:accessTo', '<./README>');
// Specify the target by its webID card
g.addTripleToGroups('<#zack>', 'acl:agent',
'<https://solid.dev.yarrabah.net/zack-collins/profile/card#me>');
// Grant him access to Read only
g.addTripleToGroups('<#zack>', 'acl:mode', 'acl:Read');
// Need to serialize before exporting
g.serialize(format: 'ttl', abbr: 'short');
print('-------Serialized ACL Contents (New)------\n${g.serializedString}\n');
}
43 changes: 18 additions & 25 deletions example/example_4.dart
@@ -1,34 +1,27 @@
import 'package:http/http.dart' as http;
import 'dart:io';

import 'package:rdflib/rdflib.dart';

main() async {
// https://github.com/anusii/rdflib/blob/main/example/sample_acl_1.acl
// https://raw.githubusercontent.com/anusii/rdflib/main/example/sample_acl_1.acl
var url = Uri.https('raw.githubusercontent.com',
'anusii/rdflib/main/example/sample_acl_1.acl');
// Get the contents of the acl file
var res = await http.get(url);
String aclContents = res.body;
print('-------Original ACL Contents-------\n${res.body}\n');
String filePath = 'example/sample_ttl_4.ttl';
// Read file content to a local String
String fileContents = await File(filePath).readAsStringSync();

// Initialize a Graph to store all the info
print('-------Original file-------\n$fileContents');

// create a graph to read turtle file and store info
Graph g = Graph();
// Parse the contents and update the triples
g.parseTurtle(aclContents);
g.serialize(format: 'ttl', abbr: 'short');
print('-------Serialized ACL Contents------\n${g.serializedString}\n');

// Add 'zack' to the ACL file
g.addTripleToGroups('<#zack>', a, 'acl:Authorization');
// Specify which document/fold
g.addTripleToGroups('<#zack>', 'acl:accessTo', '<./README>');
// Specify the target by its webID card
g.addTripleToGroups('<#zack>', 'acl:agent',
'<https://solid.dev.yarrabah.net/zack-collins/profile/card#me>');
// Grant him access to Read only
g.addTripleToGroups('<#zack>', 'acl:mode', 'acl:Read');
// Need to serialize before exporting
// Parse with the new method [Graph.parseTurtle] instead of [Graph.parse] (deprecated)
g.parseTurtle(fileContents);

// Serialize the Graph for output
g.serialize(format: 'ttl', abbr: 'short');
print('-------Serialized ACL Contents (New)------\n${g.serializedString}\n');
print('-------Serialized String--------\n${g.serializedString}');

// Print out full format of triples (will use shorthand in serialization/export)
print('--------All triples in the graph-------');
for (Triple t in g.triples) {
print(t);
}
}
27 changes: 0 additions & 27 deletions example/example_5.dart

This file was deleted.

51 changes: 51 additions & 0 deletions example/main.dart
@@ -0,0 +1,51 @@
import 'package:rdflib/rdflib.dart';

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

// Initialize a Graph
Graph g = Graph();

// Create a new URIRef instance for a person
final donna = URIRef('http://example.org/donna');

// Add triples to the Graph using [Graph.addTriplesToGroups] method
g.addTripleToGroups(donna, RDF.type, FOAF.Person);
g.addTripleToGroups(donna, FOAF.nick, Literal('donna', lang: 'en'));
g.addTripleToGroups(donna, FOAF.name, Literal('Donna Fales'));
g.addTripleToGroups(donna, FOAF.mbox, URIRef('mailto:donna@example.org'));
// Adding a duplicate triple should be ignored
g.addTripleToGroups(donna, FOAF.mbox, URIRef('mailto:donna@example.org'));

// Create another URIRef instance
final ed = URIRef('http://example.org/edward');

// Add triples to the Graph
g.addTripleToGroups(ed, RDF.type, FOAF.Person);
g.addTripleToGroups(ed, FOAF.nick, Literal('ed', datatype: XSD.string));
g.addTripleToGroups(ed, FOAF.name, Literal('Edward Scissorhands'));
g.addTripleToGroups(
ed, FOAF.mbox, Literal('mailto:ed@example.org', datatype: XSD.anyURI));

// Bind the long namespace to shorter string for better readability
g.bind('example', Namespace(ns: 'http://example.org/'));

// Serialize the Graph to the standard turtle format, and the result is stored
// in [Graph.serializedString]
g.serialize(format: 'ttl', abbr: 'short');
print('-------\nSerialized content:\n${g.serializedString}');

// Print out every added triple in the graph by iterating through the set
print('-------\nTriples updated in the graph:');
for (Triple t in g.triples) {
print(t);
}

// Print out each person's mailbox value
print('-------\nMailboxes:');
for (var sub in g.subjects(a, FOAF.Person)) {
for (var mbox in g.objects(sub, FOAF.mbox)) {
print('${sub}\'s mailbox: ${mbox.value}');
}
}
}
File renamed without changes.

0 comments on commit 48566c2

Please sign in to comment.