Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zy/38 rdflib navigation #40

Merged
merged 10 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,15 @@ main() {

// 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)) {
for (var sub in g.subjects(pre: a, obj: FOAF.Person)) {
for (var mbox in g.objects(sub: sub, pre: FOAF.mbox)) {
print('${sub}\'s mailbox: ${mbox.value}');
}
}

// Print out predicates of triples having donna
print('-------\nDonna predicates values:');
for (Triple tri in g.matchTriples(donna.value)) {
print(tri.pre.value);
}
}
100 changes: 95 additions & 5 deletions lib/src/graph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -270,27 +270,117 @@ class Graph {
}

/// Finds all subjects which have a certain predicate and object.
Set<URIRef> subjects(URIRef pre, dynamic obj) {
///
/// If `pre` is provided, it checks for the predicate.
/// If `obj` is provided, it checks for the object.
/// If both are provided, it checks for both predicate and object.
/// If neither is provided, it returns all subjects in the triples.
Set<URIRef> subjects({URIRef? pre, dynamic obj}) {
zheyxu marked this conversation as resolved.
Show resolved Hide resolved
// Initialize an empty set to store the subjects.
Set<URIRef> subs = {};

// Iterate over all triples in the graph.
for (Triple t in triples) {
if (t.pre == pre && t.obj == obj) {
// Check if the pre condition matches, if provided.
bool preMatches = pre == null || t.pre == pre;
// Check if the obj condition matches, if provided.
bool objMatches = obj == null || t.obj == obj;

// If both conditions match (or are not provided), add the subject.
if (preMatches && objMatches) {
subs.add(t.sub);
}
}

// Return the set of subjects.
return subs;
}

/// Finds all objects which have a certain subject and predicate.
Set objects(URIRef sub, URIRef pre) {
Set objs = {};
///
/// If `sub` is provided, it checks for the subject.
/// If `pre` is provided, it checks for the predicate.
/// If both are provided, it checks for both subject and predicate.
/// If neither is provided, it returns all objects in the triples.
Set<dynamic> objects({URIRef? sub, URIRef? pre}) {
// Initialize an empty set to store the objects.
Set<dynamic> objs = {};

// Iterate over all triples in the graph.
for (Triple t in triples) {
if (t.sub == sub && t.pre == pre) {
// Check if the sub condition matches, if provided.
bool subMatches = sub == null || t.sub == sub;
// Check if the pre condition matches, if provided.
bool preMatches = pre == null || t.pre == pre;

// If both conditions match (or are not provided), add the object.
if (subMatches && preMatches) {
objs.add(t.obj);
}
}

// Return the set of objects.
return objs;
}

/// Finds all predicates which have a certain subject and object.
///
/// If `sub` is provided, it checks for the subject.
/// If `obj` is provided, it checks for the object.
/// If both are provided, it checks for both subject and object.
/// If neither is provided, it returns all predicates in the triples.
Set<URIRef> predicates({URIRef? sub, dynamic obj}) {
// Initialize an empty set to store the predicates.
Set<URIRef> pres = {};

// Iterate over all triples in the graph.
for (Triple t in triples) {
// Check if the sub condition matches, if provided.
bool subMatches = sub == null || t.sub == sub;
// Check if the obj condition matches, if provided.
bool objMatches = obj == null || t.obj == obj;

// If both conditions match (or are not provided), add the predicate.
if (subMatches && objMatches) {
pres.add(t.pre);
}
}

// Return the set of predicates.
return pres;
}

/// Finds all triples which have a certain value.
///
/// This method checks if the given [value] matches any of the components
/// (subject, predicate, or object) of the triples in the graph. It returns
/// a set of triples where any of these components matches the [value].
///
/// Example usage:
/// ```dart
/// final value = 'exampleValue';
/// final matchingTriples = matchTriples(value);
/// print(matchingTriples);
/// ```
Set<Triple> matchTriples(String value) {
// Initialize an empty set to store the matching triples.
Set<Triple> matchingTriples = {};

// Iterate over all triples in the graph.
for (Triple t in triples) {
// Check if the subject, predicate, or object matches the given value.
if (t.sub.value == value ||
t.pre.value == value ||
t.obj.toString() == value) {
// If any component matches, add the triple to the set of matching triples.
matchingTriples.add(t);
}
}

// Return the set of matching triples.
return matchingTriples;
}

/// Parses file and update graph accordingly.
@Deprecated('Use [Graph.parseTurtle] instead for parsing a turtle string')
parse(String filePath) async {
Expand Down
Loading