Skip to content

Commit

Permalink
feat(cognitarium): add support for blank nodes in describe
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Jun 17, 2023
1 parent f8602a1 commit 8e736f1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
13 changes: 7 additions & 6 deletions contracts/okp4-cognitarium/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub mod query {
VarOrNodeOrLiteral, WhereCondition, IRI,
};
use crate::querier::{PlanBuilder, QueryEngine};
use crate::rdf::{self, expand_uri, Atom, TripleWriter};
use crate::rdf::{self, expand_uri, Atom, Subject, TripleWriter};

pub fn store(deps: Deps) -> StdResult<StoreResponse> {
STORE.load(deps.storage).map(|s| s.into())
Expand Down Expand Up @@ -214,12 +214,13 @@ pub mod query {
let subject = match subject_value {
Value::URI {
value: IRI::Full(uri),
} => uri,
} => Subject::NamedNode(uri),
Value::URI {
value: IRI::Prefixed(curie),
} => expand_uri(curie, &query.prefixes)?,
} => Subject::NamedNode(expand_uri(curie, &query.prefixes)?),
Value::BlankNode { value: id } => Subject::BlankNode(id.clone()),
_ => Err(StdError::generic_err(format!(
"Unexpected value: {subject_value:?} (this was unexpected)"
"Unexpected subject value: {subject_value:?} (this was unexpected)"
)))?,
};

Expand All @@ -232,7 +233,7 @@ pub mod query {
value: IRI::Prefixed(curie),
} => expand_uri(curie, &query.prefixes)?,
_ => Err(StdError::generic_err(format!(
"Unexpected value: {predicate_value:?} (this was unexpected)"
"Unexpected predicate value: {predicate_value:?} (this was unexpected)"
)))?,
};

Expand Down Expand Up @@ -266,7 +267,7 @@ pub mod query {
} => rdf::Value::LiteralDatatype(value, expand_uri(curie, &query.prefixes)?),
Value::BlankNode { value } => rdf::Value::BlankNode(value),
_ => Err(StdError::generic_err(format!(
"Unexpected value: {object_value:?} (this was unexpected)"
"Unexpected object value: {object_value:?} (this was unexpected)"
)))?,
};

Expand Down
28 changes: 22 additions & 6 deletions contracts/okp4-cognitarium/src/rdf/atom.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
use rio_api::model::{Literal, NamedNode, Triple};
use std::fmt;

#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub enum Subject {
NamedNode(String),
BlankNode(String),
}

impl fmt::Display for Subject {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Subject::NamedNode(s) => write!(f, "{s}"),
Subject::BlankNode(s) => write!(f, "{s}"),
}
}
}

#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub enum Value {
NamedNode(String),
Expand All @@ -7,9 +25,6 @@ pub enum Value {
LiteralDatatype(String, String),
}

use std::fmt;

use rio_api::model::{Literal, NamedNode, Triple};
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand All @@ -24,7 +39,7 @@ impl fmt::Display for Value {

#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub struct Atom {
pub subject: String,
pub subject: Subject,
pub property: String,
pub value: Value,
}
Expand All @@ -42,8 +57,9 @@ impl std::fmt::Display for Atom {
impl<'a> From<&'a Atom> for Triple<'a> {
fn from(atom: &'a Atom) -> Self {
Triple {
subject: NamedNode {
iri: atom.subject.as_str(),
subject: match &atom.subject {
Subject::NamedNode(s) => NamedNode { iri: s.as_str() },
Subject::BlankNode(s) => NamedNode { iri: s.as_str() },
}
.into(),
predicate: NamedNode {
Expand Down

0 comments on commit 8e736f1

Please sign in to comment.