Skip to content

Commit

Permalink
feat(cognitarium): implement triple mapping with rio api
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Jun 1, 2023
1 parent a38aa79 commit c13f530
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions contracts/okp4-cognitarium/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod contract;
mod error;
pub mod msg;
mod rdf;
pub mod state;

pub use crate::error::ContractError;
11 changes: 11 additions & 0 deletions contracts/okp4-cognitarium/src/rdf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use cosmwasm_std::StdError;

pub fn explode_iri(iri: &str) -> Result<(&str, &str), StdError> {
for delim in ['#', '/'] {
if let Some(index) = iri.rfind(delim) {
return Ok((&iri[..index], &iri[index..]));
}
}

Err(StdError::generic_err("Couldn't extract IRI namespace"))
}
78 changes: 78 additions & 0 deletions contracts/okp4-cognitarium/src/state/triples.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::rdf::explode_iri;
use cosmwasm_std::{StdError, Uint128};
use cw_storage_plus::{Index, IndexList, IndexedMap, MultiIndex};
use rio_api::model::NamedNode;
use serde::{Deserialize, Serialize};

pub struct TripleIndexes<'a> {
Expand Down Expand Up @@ -44,12 +46,38 @@ pub struct Triple {
object: Object,
}

impl<'a> TryFrom<rio_api::model::Triple<'a>> for Triple {
type Error = StdError;

fn try_from(value: rio_api::model::Triple<'a>) -> Result<Self, Self::Error> {
Ok(Triple {
subject: value.subject.try_into()?,
predicate: value.predicate.try_into()?,
object: value.object.try_into()?,
})
}
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub enum Subject {
Named(Node),
Blank(BlankNode),
}

impl<'a> TryFrom<rio_api::model::Subject<'a>> for Subject {
type Error = StdError;

fn try_from(value: rio_api::model::Subject<'a>) -> Result<Self, Self::Error> {
match value {
rio_api::model::Subject::NamedNode(node) => {
Node::try_from(node).map(|n| Subject::Named(n))
}
rio_api::model::Subject::BlankNode(node) => Ok(Subject::Blank(node.id.to_string())),
_ => Err(StdError::generic_err("Not implemented")),
}
}
}

pub type Predicate = Node;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
Expand All @@ -59,6 +87,21 @@ pub enum Object {
Literal(Literal),
}

impl<'a> TryFrom<rio_api::model::Term<'a>> for Object {
type Error = StdError;

fn try_from(value: rio_api::model::Term<'a>) -> Result<Self, Self::Error> {
match value {
rio_api::model::Term::BlankNode(node) => Ok(Object::Blank(node.id.to_string())),
rio_api::model::Term::NamedNode(node) => Node::try_from(node).map(|n| Object::Named(n)),
rio_api::model::Term::Literal(literal) => {
Literal::try_from(literal).map(|l| Object::Literal(l))
}
_ => Err(StdError::generic_err("Not implemented")),
}
}
}

pub type BlankNode = String;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
Expand All @@ -67,9 +110,44 @@ pub struct Node {
pub value: String,
}

impl<'a> TryFrom<NamedNode<'a>> for Node {
type Error = StdError;

fn try_from(value: NamedNode) -> Result<Self, Self::Error> {
explode_iri(value.iri).map(|(ns, v)| Self {
namespace: ns.to_string(),
value: v.to_string(),
})
}
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub enum Literal {
Simple { value: String },
I18NString { value: String, language: String },
Typed { value: String, datatype: Node },
}

impl<'a> TryFrom<rio_api::model::Literal<'a>> for Literal {
type Error = StdError;

fn try_from(value: rio_api::model::Literal<'a>) -> Result<Self, Self::Error> {
match value {
rio_api::model::Literal::Simple { value } => Ok(Literal::Simple {
value: value.to_string(),
}),
rio_api::model::Literal::LanguageTaggedString { value, language } => {
Ok(Literal::I18NString {
value: value.to_string(),
language: language.to_string(),
})
}
rio_api::model::Literal::Typed { value, datatype } => {
Node::try_from(datatype).map(|node| Literal::Typed {
value: value.to_string(),
datatype: node,
})
}
}
}
}

0 comments on commit c13f530

Please sign in to comment.