From bc8ed42bda4baa3aafd978c9c198c498b0bb1704 Mon Sep 17 00:00:00 2001 From: Arnaud Mimart <33665250+amimart@users.noreply.github.com> Date: Thu, 25 May 2023 15:40:47 +0200 Subject: [PATCH] feat(cognitarium): impl map query var to msg value --- .../okp4-cognitarium/src/querier/variable.rs | 49 +++++++++++++++++-- .../okp4-cognitarium/src/state/namespaces.rs | 5 +- .../okp4-cognitarium/src/state/triples.rs | 8 +++ contracts/okp4-cognitarium/src/storer.rs | 5 +- 4 files changed, 59 insertions(+), 8 deletions(-) diff --git a/contracts/okp4-cognitarium/src/querier/variable.rs b/contracts/okp4-cognitarium/src/querier/variable.rs index 57057816..9be9c4bf 100644 --- a/contracts/okp4-cognitarium/src/querier/variable.rs +++ b/contracts/okp4-cognitarium/src/querier/variable.rs @@ -1,6 +1,6 @@ -use crate::msg::Value; +use crate::msg::{Value, IRI}; use crate::state::{Literal, Object, Predicate, Subject}; -use cosmwasm_std::{StdError, StdResult}; +use cosmwasm_std::StdResult; #[derive(Eq, PartialEq, Debug, Clone)] pub enum ResolvedVariable { @@ -48,11 +48,50 @@ impl ResolvedVariable { }) } - pub fn as_value(&self, ns_fn: F) -> StdResult + pub fn as_value(&self, ns_fn: &mut F) -> StdResult where - F: n(u128) -> StdResult, + F: FnMut(u128) -> StdResult, { - Err(StdError::generic_err("mescouilles")) + Ok(match self { + ResolvedVariable::Subject(subject) => match subject { + Subject::Named(named) => named.as_iri(ns_fn).map(|iri| Value::URI { + value: IRI::Full(iri), + })?, + Subject::Blank(blank) => Value::BlankNode { + value: blank.to_string(), + }, + }, + ResolvedVariable::Predicate(predicate) => { + predicate.as_iri(ns_fn).map(|iri| Value::URI { + value: IRI::Full(iri), + })? + } + ResolvedVariable::Object(object) => match object { + Object::Named(named) => Value::URI { + value: IRI::Full(named.as_iri(ns_fn)?), + }, + Object::Blank(blank) => Value::BlankNode { + value: blank.to_string(), + }, + Object::Literal(literal) => match literal { + Literal::Simple { value } => Value::Literal { + value: value.clone(), + lang: None, + datatype: None, + }, + Literal::I18NString { value, language } => Value::Literal { + value: value.clone(), + lang: Some(language.clone()), + datatype: None, + }, + Literal::Typed { value, datatype } => Value::Literal { + value: value.clone(), + lang: None, + datatype: Some(datatype.as_iri(ns_fn).map(IRI::Full)?), + }, + }, + }, + }) } } diff --git a/contracts/okp4-cognitarium/src/state/namespaces.rs b/contracts/okp4-cognitarium/src/state/namespaces.rs index af05ab57..8e17c424 100644 --- a/contracts/okp4-cognitarium/src/state/namespaces.rs +++ b/contracts/okp4-cognitarium/src/state/namespaces.rs @@ -7,6 +7,9 @@ pub const NAMESPACE_KEY_INCREMENT: Item = Item::new("namespace_key"); #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] pub struct Namespace { + /// The namespace value. + pub value: String, + /// The unique, incremented key issues to reference this namespace from a triple IRI. pub key: u128, @@ -15,7 +18,7 @@ pub struct Namespace { } pub struct NamespaceIndexes<'a> { - key: UniqueIndex<'a, u128, Namespace, String>, + pub key: UniqueIndex<'a, u128, Namespace, String>, } impl IndexList for NamespaceIndexes<'_> { diff --git a/contracts/okp4-cognitarium/src/state/triples.rs b/contracts/okp4-cognitarium/src/state/triples.rs index aedeadb6..27253226 100644 --- a/contracts/okp4-cognitarium/src/state/triples.rs +++ b/contracts/okp4-cognitarium/src/state/triples.rs @@ -1,4 +1,5 @@ use blake3::Hash; +use cosmwasm_std::StdResult; use cw_storage_plus::{Index, IndexList, IndexedMap, MultiIndex}; use serde::{Deserialize, Serialize}; @@ -127,6 +128,13 @@ impl Node { key } + + pub fn as_iri(&self, ns_fn: &mut F) -> StdResult + where + F: FnMut(u128) -> StdResult, + { + ns_fn(self.namespace).map(|ns| vec![ns.as_str(), self.value.as_str()].join("")) + } } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] diff --git a/contracts/okp4-cognitarium/src/storer.rs b/contracts/okp4-cognitarium/src/storer.rs index 87a99ed3..e8ad3dd3 100644 --- a/contracts/okp4-cognitarium/src/storer.rs +++ b/contracts/okp4-cognitarium/src/storer.rs @@ -109,7 +109,7 @@ impl<'a> TripleStorer<'a> { } None => { let mut namespace = match namespaces().load(self.storage, ns_str.clone()) { - Err(StdError::NotFound { .. }) => Ok(self.allocate_namespace()), + Err(StdError::NotFound { .. }) => Ok(self.allocate_namespace(ns_str.clone())), Ok(n) => Ok(n), Err(e) => Err(e), }?; @@ -121,10 +121,11 @@ impl<'a> TripleStorer<'a> { } } - fn allocate_namespace(&mut self) -> Namespace { + fn allocate_namespace(&mut self, value: String) -> Namespace { self.store.stat.namespace_count += Uint128::one(); let ns = Namespace { + value, key: self.ns_key_inc_offset, counter: 0u128, };