Skip to content

Commit

Permalink
refactor(cognitarium): deduplicate bound var resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Aug 22, 2024
1 parent e179988 commit 9f389d4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 40 deletions.
57 changes: 26 additions & 31 deletions contracts/axone-cognitarium/src/querier/expression.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::msg;
use crate::querier::mapper::iri_as_string;
use crate::querier::variable::HasBoundVariables;
use crate::querier::ResolvedVariables;
use crate::state::NamespaceSolver;
use cosmwasm_std::{StdError, StdResult};
use std::cmp::Ordering;
use std::collections::{BTreeSet, HashMap};
use std::collections::HashMap;

#[derive(Eq, PartialEq, Debug, Clone)]
pub enum Expression {
Expand All @@ -20,36 +21,6 @@ pub enum Expression {
}

impl Expression {
pub fn bound_variables(&self) -> BTreeSet<usize> {
let mut vars = BTreeSet::new();
self.lookup_bound_variables(&mut |v| {
vars.insert(v);
});
vars
}

pub fn lookup_bound_variables(&self, callback: &mut impl FnMut(usize)) {
match self {
Expression::Constant(_) => {}
Expression::Variable(v) => {
callback(*v);
}
Expression::And(exprs) | Expression::Or(exprs) => {
exprs
.iter()
.for_each(|e| e.lookup_bound_variables(callback));
}
Expression::Equal(left, right)
| Expression::Greater(left, right)
| Expression::GreaterOrEqual(left, right)
| Expression::Less(left, right)
| Expression::LessOrEqual(left, right) => {
left.lookup_bound_variables(callback);
right.lookup_bound_variables(callback);
}
}
}

pub fn evaluate<'a>(
&self,
vars: &'a ResolvedVariables,
Expand Down Expand Up @@ -97,6 +68,30 @@ impl Expression {
}
}

impl HasBoundVariables for Expression {
fn lookup_bound_variables(&self, callback: &mut impl FnMut(usize)) {
match self {
Expression::Constant(_) => {}
Expression::Variable(v) => {
callback(*v);
}
Expression::And(exprs) | Expression::Or(exprs) => {
exprs
.iter()
.for_each(|e| e.lookup_bound_variables(callback));
}
Expression::Equal(left, right)
| Expression::Greater(left, right)
| Expression::GreaterOrEqual(left, right)
| Expression::Less(left, right)
| Expression::LessOrEqual(left, right) => {
left.lookup_bound_variables(callback);
right.lookup_bound_variables(callback);
}
}
}
}

#[derive(Eq, PartialEq, Debug, Clone)]
pub enum Term {
String(String),
Expand Down
13 changes: 4 additions & 9 deletions contracts/axone-cognitarium/src/querier/plan.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::querier::expression::Expression;
use crate::querier::variable::HasBoundVariables;
use crate::state::{Object, Predicate, Subject};
use std::collections::BTreeSet;

Expand Down Expand Up @@ -90,16 +91,10 @@ impl QueryNode {
bound_variables: Vec::new(),
}
}
}

pub fn bound_variables(&self) -> BTreeSet<usize> {
let mut vars = BTreeSet::new();
self.lookup_bound_variables(&mut |v| {
vars.insert(v);
});
vars
}

pub fn lookup_bound_variables(&self, callback: &mut impl FnMut(usize)) {
impl HasBoundVariables for QueryNode {
fn lookup_bound_variables(&self, callback: &mut impl FnMut(usize)) {
match self {
QueryNode::TriplePattern {
subject,
Expand Down
1 change: 1 addition & 0 deletions contracts/axone-cognitarium/src/querier/plan_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::msg::{Node, TriplePattern, VarOrNamedNode, VarOrNode, VarOrNodeOrLite
use crate::querier::expression::{Expression, Term};
use crate::querier::mapper::{iri_as_node, literal_as_object};
use crate::querier::plan::{PatternValue, PlanVariable, QueryNode, QueryPlan};
use crate::querier::variable::HasBoundVariables;
use crate::state::{
HasCachedNamespaces, Namespace, NamespaceQuerier, NamespaceResolver, Object, Predicate, Subject,
};
Expand Down
13 changes: 13 additions & 0 deletions contracts/axone-cognitarium/src/querier/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::querier::expression::Term;
use crate::state::{Literal, NamespaceSolver, Object, Predicate, Subject};
use axone_rdf::normalize::IdentifierIssuer;
use cosmwasm_std::StdResult;
use std::collections::BTreeSet;

#[derive(Eq, PartialEq, Debug, Clone)]
pub enum ResolvedVariable {
Expand Down Expand Up @@ -173,6 +174,18 @@ impl ResolvedVariables {
}
}

pub trait HasBoundVariables {
fn bound_variables(&self) -> BTreeSet<usize> {
let mut vars = BTreeSet::new();
self.lookup_bound_variables(&mut |v| {
vars.insert(v);
});
vars
}

fn lookup_bound_variables(&self, callback: &mut impl FnMut(usize));
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 9f389d4

Please sign in to comment.