Skip to content

Commit

Permalink
refactor(cognitarium)!: use var or named node as msg pattern predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Feb 28, 2024
1 parent 7dfb48e commit a194d1c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 62 deletions.
4 changes: 2 additions & 2 deletions contracts/okp4-cognitarium/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ pub mod query {
VarOrNamedNode::Variable(var) => {
let select = TriplePattern {
subject: VarOrNode::Variable(var.clone()),
predicate: VarOrNode::Variable(format!("{var}{p}")),
predicate: VarOrNamedNode::Variable(format!("{var}{p}")),
object: VarOrNodeOrLiteral::Variable(format!("{var}{o}")),
};

Expand All @@ -210,7 +210,7 @@ pub mod query {
VarOrNamedNode::NamedNode(iri) => {
let select = TriplePattern {
subject: VarOrNode::Node(Node::NamedNode(iri.clone())),
predicate: VarOrNode::Variable(p),
predicate: VarOrNamedNode::Variable(p),
object: VarOrNodeOrLiteral::Variable(o),
};

Expand Down
2 changes: 1 addition & 1 deletion contracts/okp4-cognitarium/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ pub struct TriplePattern {
/// The subject of the triple pattern.
pub subject: VarOrNode,
/// The predicate of the triple pattern.
pub predicate: VarOrNode,
pub predicate: VarOrNamedNode,
/// The object of the triple pattern.
pub object: VarOrNodeOrLiteral,
}
Expand Down
4 changes: 2 additions & 2 deletions contracts/okp4-cognitarium/src/querier/engine.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::msg::{
Node, SelectItem, TriplePattern, VarOrNamedNode, VarOrNamedNodeOrLiteral, VarOrNode,
SelectItem, TriplePattern, VarOrNamedNode, VarOrNamedNodeOrLiteral, VarOrNode,
VarOrNodeOrLiteral,
};
use crate::querier::mapper::{iri_as_node, literal_as_object, node_as_predicate};
use crate::querier::mapper::{iri_as_node, literal_as_object};
use crate::querier::plan::{PatternValue, QueryNode, QueryPlan};
use crate::querier::variable::{ResolvedVariable, ResolvedVariables};
use crate::rdf::Atom;
Expand Down
20 changes: 3 additions & 17 deletions contracts/okp4-cognitarium/src/querier/mapper.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
use crate::msg::{Literal, Node, IRI};
use crate::msg::{Literal, IRI};
use crate::state;
use crate::state::{NamespaceResolver, Object, Predicate};
use cosmwasm_std::{StdError, StdResult, Storage};
use crate::state::{NamespaceResolver, Object};
use cosmwasm_std::{StdResult, Storage};
use okp4_rdf::uri::{expand_uri, explode_iri};
use std::collections::HashMap;

pub fn node_as_predicate(
ns_resolver: &mut NamespaceResolver,
storage: &dyn Storage,
prefixes: &HashMap<String, String>,
node: Node,
) -> StdResult<Predicate> {
match node {
Node::NamedNode(iri) => iri_as_node(ns_resolver, storage, prefixes, iri),
Node::BlankNode(_) => Err(StdError::generic_err(
"Predicate pattern must be a named node",
)),
}
}

pub fn literal_as_object(
ns_resolver: &mut NamespaceResolver,
storage: &dyn Storage,
Expand Down
71 changes: 31 additions & 40 deletions contracts/okp4-cognitarium/src/querier/plan_builder.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::msg::{
Node, SimpleWhereCondition, TriplePattern, VarOrNode, VarOrNodeOrLiteral, WhereClause,
WhereCondition,
Node, SimpleWhereCondition, TriplePattern, VarOrNamedNode, VarOrNode, VarOrNodeOrLiteral,
WhereClause, WhereCondition,
};
use crate::querier::mapper::{iri_as_node, literal_as_object, node_as_predicate};
use crate::querier::mapper::{iri_as_node, literal_as_object};
use crate::querier::plan::{PatternValue, PlanVariable, QueryNode, QueryPlan};
use crate::state::{HasCachedNamespaces, Namespace, NamespaceResolver, Object, Predicate, Subject};
use cosmwasm_std::{StdError, StdResult, Storage};
Expand Down Expand Up @@ -152,14 +152,17 @@ impl<'a> PlanBuilder<'a> {
})
}

fn build_predicate_pattern(&mut self, value: VarOrNode) -> StdResult<PatternValue<Predicate>> {
fn build_predicate_pattern(
&mut self,
value: VarOrNamedNode,
) -> StdResult<PatternValue<Predicate>> {
Ok(match value {
VarOrNode::Variable(v) => PatternValue::Variable(self.resolve_basic_variable(v)),
VarOrNode::Node(n) => PatternValue::Constant(node_as_predicate(
VarOrNamedNode::Variable(v) => PatternValue::Variable(self.resolve_basic_variable(v)),
VarOrNamedNode::NamedNode(iri) => PatternValue::Constant(iri_as_node(
&mut self.ns_resolver,
self.storage,
self.prefixes,
n,
iri,
)?),
})
}
Expand Down Expand Up @@ -317,7 +320,7 @@ mod test {
(
TriplePattern {
subject: VarOrNode::Variable("s".to_string()),
predicate: VarOrNode::Variable("p".to_string()),
predicate: VarOrNamedNode::Variable("p".to_string()),
object: VarOrNodeOrLiteral::Variable("o".to_string()),
},
Ok(QueryNode::TriplePattern {
Expand All @@ -329,9 +332,9 @@ mod test {
(
TriplePattern {
subject: VarOrNode::Node(Node::BlankNode("1".to_string())),
predicate: VarOrNode::Node(Node::NamedNode(IRI::Full(
predicate: VarOrNamedNode::NamedNode(IRI::Full(
"http://okp4.space/hasTitle".to_string(),
))),
)),
object: VarOrNodeOrLiteral::Node(Node::BlankNode("2".to_string())),
},
Ok(QueryNode::TriplePattern {
Expand All @@ -348,7 +351,7 @@ mod test {
subject: VarOrNode::Node(Node::NamedNode(IRI::Full(
"http://okp4.space/123456789".to_string(),
))),
predicate: VarOrNode::Variable("p".to_string()),
predicate: VarOrNamedNode::Variable("p".to_string()),
object: VarOrNodeOrLiteral::Node(Node::NamedNode(IRI::Full(
"http://okp4.space/1234567892".to_string(),
))),
Expand All @@ -368,7 +371,7 @@ mod test {
(
TriplePattern {
subject: VarOrNode::Variable("p".to_string()),
predicate: VarOrNode::Variable("s".to_string()),
predicate: VarOrNamedNode::Variable("s".to_string()),
object: VarOrNodeOrLiteral::Literal(Literal::Simple("simple".to_string())),
},
Ok(QueryNode::TriplePattern {
Expand All @@ -382,7 +385,7 @@ mod test {
(
TriplePattern {
subject: VarOrNode::Variable("s".to_string()),
predicate: VarOrNode::Variable("p".to_string()),
predicate: VarOrNamedNode::Variable("p".to_string()),
object: VarOrNodeOrLiteral::Literal(Literal::LanguageTaggedString {
value: "tagged".to_string(),
language: "en".to_string(),
Expand All @@ -400,7 +403,7 @@ mod test {
(
TriplePattern {
subject: VarOrNode::Variable("s".to_string()),
predicate: VarOrNode::Variable("p".to_string()),
predicate: VarOrNamedNode::Variable("p".to_string()),
object: VarOrNodeOrLiteral::Literal(Literal::TypedValue {
value: "typed".to_string(),
datatype: IRI::Full("http://okp4.space/type".to_string()),
Expand All @@ -423,7 +426,7 @@ mod test {
subject: VarOrNode::Node(Node::NamedNode(IRI::Full(
"notexisting#outch".to_string(),
))),
predicate: VarOrNode::Variable("p".to_string()),
predicate: VarOrNamedNode::Variable("p".to_string()),
object: VarOrNodeOrLiteral::Variable("o".to_string()),
},
Ok(QueryNode::Noop {
Expand All @@ -433,9 +436,9 @@ mod test {
(
TriplePattern {
subject: VarOrNode::Variable("s".to_string()),
predicate: VarOrNode::Node(Node::NamedNode(IRI::Full(
predicate: VarOrNamedNode::NamedNode(IRI::Full(
"notexisting#outch".to_string(),
))),
)),
object: VarOrNodeOrLiteral::Variable("o".to_string()),
},
Ok(QueryNode::Noop {
Expand All @@ -445,7 +448,7 @@ mod test {
(
TriplePattern {
subject: VarOrNode::Variable("s".to_string()),
predicate: VarOrNode::Variable("p".to_string()),
predicate: VarOrNamedNode::Variable("p".to_string()),
object: VarOrNodeOrLiteral::Node(Node::NamedNode(IRI::Full(
"notexisting#outch".to_string(),
))),
Expand Down Expand Up @@ -485,26 +488,14 @@ mod test {
vec![],
Err(StdError::generic_err("Empty basic graph pattern")),
),
(
None,
None,
vec![TriplePattern {
subject: VarOrNode::Variable("subject".to_string()),
predicate: VarOrNode::Node(Node::BlankNode("_".to_string())),
object: VarOrNodeOrLiteral::Variable("object".to_string()),
}],
Err(StdError::generic_err(
"Predicate pattern must be a named node",
)),
),
(
None,
None,
vec![TriplePattern {
subject: VarOrNode::Node(Node::NamedNode(IRI::Full(
"notexisting#outch".to_string(),
))),
predicate: VarOrNode::Variable("predicate".to_string()),
predicate: VarOrNamedNode::Variable("predicate".to_string()),
object: VarOrNodeOrLiteral::Variable("object".to_string()),
}],
Ok(QueryPlan {
Expand All @@ -522,7 +513,7 @@ mod test {
None,
vec![TriplePattern {
subject: VarOrNode::Variable("subject".to_string()),
predicate: VarOrNode::Variable("predicate".to_string()),
predicate: VarOrNamedNode::Variable("predicate".to_string()),
object: VarOrNodeOrLiteral::Variable("object".to_string()),
}],
Ok(QueryPlan {
Expand All @@ -543,7 +534,7 @@ mod test {
None,
vec![TriplePattern {
subject: VarOrNode::Variable("subject".to_string()),
predicate: VarOrNode::Variable("predicate".to_string()),
predicate: VarOrNamedNode::Variable("predicate".to_string()),
object: VarOrNodeOrLiteral::Variable("object".to_string()),
}],
Ok(QueryPlan {
Expand All @@ -567,7 +558,7 @@ mod test {
Some(20usize),
vec![TriplePattern {
subject: VarOrNode::Variable("subject".to_string()),
predicate: VarOrNode::Variable("predicate".to_string()),
predicate: VarOrNamedNode::Variable("predicate".to_string()),
object: VarOrNodeOrLiteral::Variable("object".to_string()),
}],
Ok(QueryPlan {
Expand All @@ -591,7 +582,7 @@ mod test {
Some(50usize),
vec![TriplePattern {
subject: VarOrNode::Variable("subject".to_string()),
predicate: VarOrNode::Variable("predicate".to_string()),
predicate: VarOrNamedNode::Variable("predicate".to_string()),
object: VarOrNodeOrLiteral::Variable("object".to_string()),
}],
Ok(QueryPlan {
Expand Down Expand Up @@ -619,17 +610,17 @@ mod test {
vec![
TriplePattern {
subject: VarOrNode::Variable("var1".to_string()),
predicate: VarOrNode::Variable("var2".to_string()),
predicate: VarOrNamedNode::Variable("var2".to_string()),
object: VarOrNodeOrLiteral::Variable("var3".to_string()),
},
TriplePattern {
subject: VarOrNode::Variable("var4".to_string()),
predicate: VarOrNode::Variable("var5".to_string()),
predicate: VarOrNamedNode::Variable("var5".to_string()),
object: VarOrNodeOrLiteral::Variable("var6".to_string()),
},
TriplePattern {
subject: VarOrNode::Variable("var1".to_string()),
predicate: VarOrNode::Variable("var5".to_string()),
predicate: VarOrNamedNode::Variable("var5".to_string()),
object: VarOrNodeOrLiteral::Node(Node::BlankNode("blank".to_string())),
},
],
Expand Down Expand Up @@ -670,12 +661,12 @@ mod test {
vec![
TriplePattern {
subject: VarOrNode::Node(Node::BlankNode("1".to_string())),
predicate: VarOrNode::Variable("1".to_string()),
predicate: VarOrNamedNode::Variable("1".to_string()),
object: VarOrNodeOrLiteral::Node(Node::BlankNode("2".to_string())),
},
TriplePattern {
subject: VarOrNode::Node(Node::BlankNode("1".to_string())),
predicate: VarOrNode::Variable("1".to_string()),
predicate: VarOrNamedNode::Variable("1".to_string()),
object: VarOrNodeOrLiteral::Variable("2".to_string()),
},
],
Expand Down

0 comments on commit a194d1c

Please sign in to comment.