Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(hyperswitch_constraint_graph): Removal of lifetime from the Constraint Graph framework #5132

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/euclid/src/dssa/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ fn perform_condition_analyses(

fn perform_context_analyses(
context: &types::ConjunctiveContext<'_>,
knowledge_graph: &ConstraintGraph<'_, dir::DirValue>,
knowledge_graph: &ConstraintGraph<dir::DirValue>,
) -> Result<(), types::AnalysisError> {
perform_condition_analyses(context)?;
let mut memo = Memoization::new();
Expand All @@ -222,7 +222,7 @@ fn perform_context_analyses(

pub fn analyze<O: EuclidAnalysable + EuclidDirFilter>(
program: ast::Program<O>,
knowledge_graph: Option<&ConstraintGraph<'_, dir::DirValue>>,
knowledge_graph: Option<&ConstraintGraph<dir::DirValue>>,
) -> Result<vir::ValuedProgram<O>, types::AnalysisError> {
let dir_program = ast::lowering::lower_program(program)?;

Expand Down
30 changes: 15 additions & 15 deletions crates/euclid/src/dssa/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ pub trait CgraphExt {
ctx: &AnalysisContext,
memo: &mut cgraph::Memoization<dir::DirValue>,
cycle_map: &mut cgraph::CycleCheck,
domains: Option<&[&str]>,
domains: Option<&[String]>,
) -> Result<(), cgraph::GraphError<dir::DirValue>>;

fn value_analysis(
Expand All @@ -230,7 +230,7 @@ pub trait CgraphExt {
ctx: &AnalysisContext,
memo: &mut cgraph::Memoization<dir::DirValue>,
cycle_map: &mut cgraph::CycleCheck,
domains: Option<&[&str]>,
domains: Option<&[String]>,
) -> Result<(), cgraph::GraphError<dir::DirValue>>;

fn check_value_validity(
Expand All @@ -239,7 +239,7 @@ pub trait CgraphExt {
analysis_ctx: &AnalysisContext,
memo: &mut cgraph::Memoization<dir::DirValue>,
cycle_map: &mut cgraph::CycleCheck,
domains: Option<&[&str]>,
domains: Option<&[String]>,
) -> Result<bool, cgraph::GraphError<dir::DirValue>>;

fn key_value_analysis(
Expand All @@ -248,7 +248,7 @@ pub trait CgraphExt {
ctx: &AnalysisContext,
memo: &mut cgraph::Memoization<dir::DirValue>,
cycle_map: &mut cgraph::CycleCheck,
domains: Option<&[&str]>,
domains: Option<&[String]>,
) -> Result<(), cgraph::GraphError<dir::DirValue>>;

fn assertion_analysis(
Expand All @@ -257,7 +257,7 @@ pub trait CgraphExt {
analysis_ctx: &AnalysisContext,
memo: &mut cgraph::Memoization<dir::DirValue>,
cycle_map: &mut cgraph::CycleCheck,
domains: Option<&[&str]>,
domains: Option<&[String]>,
) -> Result<(), AnalysisError<dir::DirValue>>;

fn negation_analysis(
Expand All @@ -266,25 +266,25 @@ pub trait CgraphExt {
analysis_ctx: &mut AnalysisContext,
memo: &mut cgraph::Memoization<dir::DirValue>,
cycle_map: &mut cgraph::CycleCheck,
domains: Option<&[&str]>,
domains: Option<&[String]>,
) -> Result<(), AnalysisError<dir::DirValue>>;

fn perform_context_analysis(
&self,
ctx: &types::ConjunctiveContext<'_>,
memo: &mut cgraph::Memoization<dir::DirValue>,
domains: Option<&[&str]>,
domains: Option<&[String]>,
) -> Result<(), AnalysisError<dir::DirValue>>;
}

impl CgraphExt for cgraph::ConstraintGraph<'_, dir::DirValue> {
impl CgraphExt for cgraph::ConstraintGraph<dir::DirValue> {
fn key_analysis(
&self,
key: dir::DirKey,
ctx: &AnalysisContext,
memo: &mut cgraph::Memoization<dir::DirValue>,
cycle_map: &mut cgraph::CycleCheck,
domains: Option<&[&str]>,
domains: Option<&[String]>,
) -> Result<(), cgraph::GraphError<dir::DirValue>> {
self.value_map
.get(&cgraph::NodeValue::Key(key))
Expand All @@ -307,7 +307,7 @@ impl CgraphExt for cgraph::ConstraintGraph<'_, dir::DirValue> {
ctx: &AnalysisContext,
memo: &mut cgraph::Memoization<dir::DirValue>,
cycle_map: &mut cgraph::CycleCheck,
domains: Option<&[&str]>,
domains: Option<&[String]>,
) -> Result<(), cgraph::GraphError<dir::DirValue>> {
self.value_map
.get(&cgraph::NodeValue::Value(val))
Expand All @@ -330,7 +330,7 @@ impl CgraphExt for cgraph::ConstraintGraph<'_, dir::DirValue> {
analysis_ctx: &AnalysisContext,
memo: &mut cgraph::Memoization<dir::DirValue>,
cycle_map: &mut cgraph::CycleCheck,
domains: Option<&[&str]>,
domains: Option<&[String]>,
) -> Result<bool, cgraph::GraphError<dir::DirValue>> {
let maybe_node_id = self.value_map.get(&cgraph::NodeValue::Value(val));

Expand Down Expand Up @@ -365,7 +365,7 @@ impl CgraphExt for cgraph::ConstraintGraph<'_, dir::DirValue> {
ctx: &AnalysisContext,
memo: &mut cgraph::Memoization<dir::DirValue>,
cycle_map: &mut cgraph::CycleCheck,
domains: Option<&[&str]>,
domains: Option<&[String]>,
) -> Result<(), cgraph::GraphError<dir::DirValue>> {
self.key_analysis(val.get_key(), ctx, memo, cycle_map, domains)
.and_then(|_| self.value_analysis(val, ctx, memo, cycle_map, domains))
Expand All @@ -377,7 +377,7 @@ impl CgraphExt for cgraph::ConstraintGraph<'_, dir::DirValue> {
analysis_ctx: &AnalysisContext,
memo: &mut cgraph::Memoization<dir::DirValue>,
cycle_map: &mut cgraph::CycleCheck,
domains: Option<&[&str]>,
domains: Option<&[String]>,
) -> Result<(), AnalysisError<dir::DirValue>> {
positive_ctx.iter().try_for_each(|(value, metadata)| {
self.key_value_analysis((*value).clone(), analysis_ctx, memo, cycle_map, domains)
Expand All @@ -391,7 +391,7 @@ impl CgraphExt for cgraph::ConstraintGraph<'_, dir::DirValue> {
analysis_ctx: &mut AnalysisContext,
memo: &mut cgraph::Memoization<dir::DirValue>,
cycle_map: &mut cgraph::CycleCheck,
domains: Option<&[&str]>,
domains: Option<&[String]>,
) -> Result<(), AnalysisError<dir::DirValue>> {
let mut keywise_metadata: FxHashMap<dir::DirKey, Vec<&Metadata>> = FxHashMap::default();
let mut keywise_negation: FxHashMap<dir::DirKey, FxHashSet<&dir::DirValue>> =
Expand Down Expand Up @@ -449,7 +449,7 @@ impl CgraphExt for cgraph::ConstraintGraph<'_, dir::DirValue> {
&self,
ctx: &types::ConjunctiveContext<'_>,
memo: &mut cgraph::Memoization<dir::DirValue>,
domains: Option<&[&str]>,
domains: Option<&[String]>,
) -> Result<(), AnalysisError<dir::DirValue>> {
let mut analysis_ctx = AnalysisContext::from_dir_values(
ctx.iter()
Expand Down
2 changes: 1 addition & 1 deletion crates/euclid/src/dssa/truth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use once_cell::sync::Lazy;

use crate::{dssa::graph::euclid_graph_prelude, frontend::dir};

pub static ANALYSIS_GRAPH: Lazy<hyperswitch_constraint_graph::ConstraintGraph<'_, dir::DirValue>> =
pub static ANALYSIS_GRAPH: Lazy<hyperswitch_constraint_graph::ConstraintGraph<dir::DirValue>> =
Lazy::new(|| {
knowledge! {
// Payment Method should be `Card` for a CardType to be present
Expand Down
6 changes: 3 additions & 3 deletions crates/euclid_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ use wasm_bindgen::prelude::*;
use crate::utils::JsResultExt;
type JsResult = Result<JsValue, JsValue>;

struct SeedData<'a> {
cgraph: hyperswitch_constraint_graph::ConstraintGraph<'a, dir::DirValue>,
struct SeedData {
cgraph: hyperswitch_constraint_graph::ConstraintGraph<dir::DirValue>,
connectors: Vec<ast::ConnectorChoice>,
}

static SEED_DATA: OnceCell<SeedData<'_>> = OnceCell::new();
static SEED_DATA: OnceCell<SeedData> = OnceCell::new();
static SEED_FOREX: OnceCell<currency_conversion_types::ExchangeRates> = OnceCell::new();

/// This function can be used by the frontend to educate wasm about the forex rates data.
Expand Down
54 changes: 36 additions & 18 deletions crates/hyperswitch_constraint_graph/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,42 @@ use crate::{
},
};

pub enum DomainIdOrIdentifier<'a> {
pub enum DomainIdOrIdentifier {
DomainId(DomainId),
DomainIdentifier(DomainIdentifier<'a>),
DomainIdentifier(DomainIdentifier),
}

impl<'a> From<&'a str> for DomainIdOrIdentifier<'a> {
fn from(value: &'a str) -> Self {
impl From<String> for DomainIdOrIdentifier {
fn from(value: String) -> Self {
Self::DomainIdentifier(DomainIdentifier::new(value))
}
}

impl From<DomainId> for DomainIdOrIdentifier<'_> {
impl From<DomainIdentifier> for DomainIdOrIdentifier {
fn from(value: DomainIdentifier) -> Self {
Self::DomainIdentifier(value)
}
}

impl From<DomainId> for DomainIdOrIdentifier {
fn from(value: DomainId) -> Self {
Self::DomainId(value)
}
}
#[derive(Debug)]
pub struct ConstraintGraphBuilder<'a, V: ValueNode> {
domain: DenseMap<DomainId, DomainInfo<'a>>,
pub struct ConstraintGraphBuilder<V: ValueNode> {
domain: DenseMap<DomainId, DomainInfo>,
nodes: DenseMap<NodeId, Node<V>>,
edges: DenseMap<EdgeId, Edge>,
domain_identifier_map: FxHashMap<DomainIdentifier<'a>, DomainId>,
domain_identifier_map: FxHashMap<DomainIdentifier, DomainId>,
value_map: FxHashMap<NodeValue<V>, NodeId>,
edges_map: FxHashMap<(NodeId, NodeId, Option<DomainId>), EdgeId>,
node_info: DenseMap<NodeId, Option<&'static str>>,
node_metadata: DenseMap<NodeId, Option<Arc<dyn Metadata>>>,
}

#[allow(clippy::new_without_default)]
impl<'a, V> ConstraintGraphBuilder<'a, V>
impl<V> ConstraintGraphBuilder<V>
where
V: ValueNode,
{
Expand All @@ -58,7 +64,7 @@ where
}
}

pub fn build(self) -> ConstraintGraph<'a, V> {
pub fn build(self) -> ConstraintGraph<V> {
ConstraintGraph {
domain: self.domain,
domain_identifier_map: self.domain_identifier_map,
Expand All @@ -72,7 +78,7 @@ where

fn retrieve_domain_from_identifier(
&self,
domain_ident: DomainIdentifier<'_>,
domain_ident: DomainIdentifier,
) -> Result<DomainId, GraphError<V>> {
self.domain_identifier_map
.get(&domain_ident)
Expand All @@ -82,7 +88,7 @@ where

pub fn make_domain(
&mut self,
domain_identifier: &'a str,
domain_identifier: String,
domain_description: &str,
) -> Result<DomainId, GraphError<V>> {
let domain_identifier = DomainIdentifier::new(domain_identifier);
Expand All @@ -93,7 +99,7 @@ where
.map_or_else(
|| {
let domain_id = self.domain.push(DomainInfo {
domain_identifier,
domain_identifier: domain_identifier.clone(),
domain_description: domain_description.to_string(),
});
self.domain_identifier_map
Expand Down Expand Up @@ -123,7 +129,7 @@ where
})
}

pub fn make_edge<'short, T: Into<DomainIdOrIdentifier<'short>>>(
pub fn make_edge<T: Into<DomainIdOrIdentifier>>(
&mut self,
pred_id: NodeId,
succ_id: NodeId,
Expand Down Expand Up @@ -188,7 +194,7 @@ where
nodes: &[(NodeId, Relation, Strength)],
info: Option<&'static str>,
metadata: Option<M>,
domain: Option<&str>,
domain: Option<String>,
) -> Result<NodeId, GraphError<V>> {
nodes
.iter()
Expand All @@ -202,7 +208,13 @@ where
.push(metadata.map(|meta| -> Arc<dyn Metadata> { Arc::new(meta) }));

for (node_id, relation, strength) in nodes {
self.make_edge(*node_id, aggregator_id, *strength, *relation, domain)?;
self.make_edge(
*node_id,
aggregator_id,
*strength,
*relation,
domain.clone(),
)?;
}

Ok(aggregator_id)
Expand All @@ -213,7 +225,7 @@ where
nodes: &[(NodeId, Relation, Strength)],
info: Option<&'static str>,
metadata: Option<M>,
domain: Option<&str>,
domain: Option<String>,
) -> Result<NodeId, GraphError<V>> {
nodes
.iter()
Expand All @@ -227,7 +239,13 @@ where
.push(metadata.map(|meta| -> Arc<dyn Metadata> { Arc::new(meta) }));

for (node_id, relation, strength) in nodes {
self.make_edge(*node_id, aggregator_id, *strength, *relation, domain)?;
self.make_edge(
*node_id,
aggregator_id,
*strength,
*relation,
domain.clone(),
)?;
}

Ok(aggregator_id)
Expand Down
Loading
Loading