Skip to content

Commit

Permalink
test DWF to future impl
Browse files Browse the repository at this point in the history
  • Loading branch information
AZWN committed Oct 18, 2024
1 parent 6e0b881 commit 97427c6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 38 deletions.
43 changes: 24 additions & 19 deletions scopegraphs/src/containers/env.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::future_wrapper::FutureWrapper;
use crate::resolve::{Env, ResolvedPath};
use futures::future::Shared;
use futures::io::empty;
use std::hash::Hash;
use std::rc::Rc;

Expand All @@ -12,7 +11,7 @@ pub trait EnvContainer<'sg, 'rslv, LABEL: 'sg, DATA: 'sg, DWFO>:
/// Creates a new, container with an empty environment.
fn empty() -> Self;

fn inject_if(data_ok: DWFO, path: ResolvedPath<LABEL, DATA>) -> Self;
fn inject_if(data_ok: DWFO, path: ResolvedPath<'sg, LABEL, DATA>) -> Self;

/// Maps the current container to a new one, based a provided mapping of the underlying environment.
fn flat_map(
Expand All @@ -21,20 +20,20 @@ pub trait EnvContainer<'sg, 'rslv, LABEL: 'sg, DATA: 'sg, DWFO>:
) -> Self;
}

impl<'sg: 'rslv, 'rslv, LABEL, DATA> EnvContainer<'sg, 'rslv, LABEL, DATA, bool>
impl<'sg: 'rslv, 'rslv, LABEL: Eq, DATA: Eq> EnvContainer<'sg, 'rslv, LABEL, DATA, bool>
for Env<'sg, LABEL, DATA>
where
ResolvedPath<'sg, LABEL, DATA>: Hash,
ResolvedPath<'sg, LABEL, DATA>: Hash + Clone,
{
fn empty() -> Self {
Self::new()
}

fn inject_if(data_ok: bool, path: ResolvedPath<LABEL, DATA>) -> Self {
fn inject_if(data_ok: bool, path: ResolvedPath<'sg, LABEL, DATA>) -> Self {
if data_ok {
return Env::single(path).into();
} else {
return empty();
return Env::empty();
}
}

Expand All @@ -46,20 +45,22 @@ where
}
}

impl<'sg: 'rslv, 'rslv, LABEL: 'sg, DATA: 'sg> EnvContainer<'sg, 'rslv, LABEL, DATA, bool>
impl<'sg: 'rslv, 'rslv, LABEL, DATA> EnvContainer<'sg, 'rslv, LABEL, DATA, bool>
for Rc<Env<'sg, LABEL, DATA>>
where
ResolvedPath<'sg, LABEL, DATA>: Hash,
LABEL: 'sg + Eq + Clone,
DATA: 'sg + Eq,
{
fn empty() -> Self {
Self::new(Env::empty())
}

fn inject_if(data_ok: bool, path: ResolvedPath<LABEL, DATA>) -> Self {
fn inject_if(data_ok: bool, path: ResolvedPath<'sg, LABEL, DATA>) -> Self {
if data_ok {
return Env::single(path).into();
} else {
return empty();
return Env::empty().into();
}
}

Expand All @@ -80,22 +81,22 @@ impl<'sg, LABEL: 'sg, DATA: 'sg, E> From<Env<'sg, LABEL, DATA>>
}
}

impl<'sg: 'rslv, 'rslv, LABEL: 'sg, DATA: 'sg, E: 'rslv>
impl<'sg: 'rslv, 'rslv, LABEL: 'sg + Eq, DATA: 'sg + Eq, E: 'rslv>
EnvContainer<'sg, 'rslv, LABEL, DATA, Result<bool, E>> for Result<Env<'sg, LABEL, DATA>, E>
where
ResolvedPath<'sg, LABEL, DATA>: Hash,
ResolvedPath<'sg, LABEL, DATA>: Hash + Clone,
E: Clone,
{
fn empty() -> Self {
Ok(Env::empty())
}

fn inject_if(data_ok: Result<bool, E>, path: ResolvedPath<LABEL, DATA>) -> Self {
fn inject_if(data_ok: Result<bool, E>, path: ResolvedPath<'sg, LABEL, DATA>) -> Self {
data_ok.map(|ok| {
if ok {
return Env::single(path).into();
} else {
return empty();
return Env::empty();
}
})
}
Expand All @@ -108,25 +109,29 @@ where
}
}

impl<'sg: 'rslv, 'rslv, LABEL: 'sg, DATA: 'sg>
impl<'sg: 'rslv, 'rslv, LABEL: 'sg + Eq, DATA: 'sg + Eq>
EnvContainer<'sg, 'rslv, LABEL, DATA, FutureWrapper<'rslv, bool>>
for FutureWrapper<'rslv, Env<'sg, LABEL, DATA>>
where
ResolvedPath<'sg, LABEL, DATA>: Hash,
ResolvedPath<'sg, LABEL, DATA>: Hash + Clone,
LABEL: Clone,
{
fn empty() -> Self {
FutureWrapper::new(std::future::ready(Env::empty()))
}

fn inject_if(data_ok: FutureWrapper<'rslv, bool>, path: ResolvedPath<LABEL, DATA>) -> Self {
data_ok.map(|ok| {
fn inject_if(
data_ok: FutureWrapper<'rslv, bool>,
path: ResolvedPath<'sg, LABEL, DATA>,
) -> Self {
return FutureWrapper::new(async move {
let ok = data_ok.await;
if ok {
return Env::single(path).into();
} else {
return empty();
return Env::empty();
}
})
});
}

fn flat_map(
Expand Down
41 changes: 31 additions & 10 deletions scopegraphs/src/resolve/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::resolve::{
use crate::{Label, Scope, ScopeGraph};
use scopegraphs_regular_expressions::RegexMatcher;

impl<'sg: 'rslv, 'storage, 'rslv, LABEL, DATA, CMPL, PWF, DWF, DWFO, LO, DEq> Resolve<'sg, 'rslv>
impl<'sg: 'rslv, 'storage, 'rslv, LABEL, DATA, CMPL, PWF, DWF, LO, DEq> Resolve<'sg, 'rslv>
for Query<'storage, 'sg, 'rslv, LABEL, DATA, CMPL, PWF, DWF, LO, DEq>
where
'storage: 'sg,
Expand All @@ -36,9 +36,10 @@ where
'rslv,
LABEL,
DATA,
>>::EnvContainer: EnvContainer<'sg, 'rslv, LABEL, DATA, DWFO> + Debug,
>>::EnvContainer:
EnvContainer<'sg, 'rslv, LABEL, DATA, <DWF as DataWellformedness<DATA>>::Output> + Debug,
PWF: for<'a> RegexMatcher<&'a LABEL> + 'rslv,
DWF: DataWellformedness<DATA, DWFO> + 'rslv,
DWF: DataWellformedness<DATA> + 'rslv,

// DWF : DataWellFormedNess<DATA, Completeness::DWF>
LO: LabelOrder<LABEL> + 'rslv,
Expand Down Expand Up @@ -105,7 +106,7 @@ type EnvC<'sg, 'rslv, CMPL, LABEL, DATA> = <<<CMPL as Completeness<LABEL, DATA>>

type EnvCache<LABEL, ENVC> = RefCell<HashMap<EdgeOrData<LABEL>, Rc<ENVC>>>;

impl<'storage, 'sg: 'rslv, 'rslv, LABEL, DATA, CMPL, DWF, LO, DEq, DWFO>
impl<'storage, 'sg: 'rslv, 'rslv, LABEL, DATA, CMPL, DWF, LO, DEq>
ResolutionContext<'storage, 'sg, 'rslv, LABEL, DATA, CMPL, DWF, LO, DEq>
where
LABEL: Label + Debug + Hash,
Expand All @@ -120,9 +121,10 @@ where
'rslv,
LABEL,
DATA,
>>::EnvContainer: EnvContainer<'sg, 'rslv, LABEL, DATA, DWFO> + Debug,
>>::EnvContainer:
EnvContainer<'sg, 'rslv, LABEL, DATA, <DWF as DataWellformedness<DATA>>::Output> + Debug,
DEq: DataEquivalence<DATA>,
DWF: DataWellformedness<DATA, DWFO>,
DWF: DataWellformedness<DATA>,
LO: LabelOrder<LABEL>,
Path<LABEL>: Clone,
{
Expand Down Expand Up @@ -308,8 +310,13 @@ where
let path = path.clone().resolve(data);
let data_ok = self.data_wellformedness.data_wf(data);

return <EnvC<'sg, 'rslv, CMPL, LABEL, DATA> as EnvContainer<'sg, 'rslv, LABEL, DATA>>
::inject_if(data_ok, path);
return <EnvC<'sg, 'rslv, CMPL, LABEL, DATA> as EnvContainer<
'sg,
'rslv,
LABEL,
DATA,
<DWF as DataWellformedness<DATA>>::Output,
>>::inject_if(data_ok, path);
}

/// Computes the edges in `edges` for which no 'greater' edge exists.
Expand Down Expand Up @@ -339,6 +346,16 @@ where
log::info!("smaller({:?}, {:?}) = {:?}", edge, edges, smaller);
smaller
}

fn empty_env_container() -> EnvC<'sg, 'rslv, CMPL, LABEL, DATA> {
<EnvC<'sg, 'rslv, CMPL, LABEL, DATA> as EnvContainer<
'sg,
'rslv,
LABEL,
DATA,
<DWF as DataWellformedness<DATA>>::Output,
>>::empty()
}
}

#[cfg(test)]
Expand All @@ -348,6 +365,7 @@ mod tests {
use crate::{
add_scope,
completeness::{ExplicitClose, FutureCompleteness, ImplicitClose, UncheckedCompleteness},
future_wrapper::FutureWrapper,
query_regex,
resolve::{DataWellformedness, Resolve, ResolvedPath},
storage::Storage,
Expand Down Expand Up @@ -386,8 +404,11 @@ mod tests {
}
}

fn matcher(n: &'a str) -> impl DataWellformedness<Self, bool> {
|data: &Self| data.matches(n)
fn matcher(n: &'a str) -> impl DataWellformedness<Self> {
|data: &Self| {
let matches = data.matches(n);
return FutureWrapper::new(async move { return matches });
}
}

fn from_default(name: &'a str) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion scopegraphs/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ impl<'sg, 'storage, 'rslv, LABEL: Label, DATA, CMPL, PWF, DWF, LO, DEq>
new_data_wellformedness: NDWF,
) -> Query<'sg, 'storage, 'rslv, LABEL, DATA, CMPL, PWF, NDWF, LO, DEq>
where
NDWF: DataWellformedness<DATA, DWFO> + 'rslv,
NDWF: DataWellformedness<DATA> + 'rslv,
{
Query {
_phantom: PhantomData,
Expand Down
19 changes: 11 additions & 8 deletions scopegraphs/src/resolve/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,31 @@ use crate::resolve::EdgeOrData;
/// When executing the query, needs to be compatible with
/// the completeness strategy for the underlying scope graph.
///
pub trait DataWellformedness<DATA, O> {
pub trait DataWellformedness<DATA> {
type Output;
/// returns true if the data is well-formed.
fn data_wf(&self, data: &DATA) -> O;
fn data_wf(&self, data: &DATA) -> Self::Output;
}

impl<DATA, T, O> DataWellformedness<DATA, O> for T
impl<DATA, T, O> DataWellformedness<DATA> for T
where
for<'sg> T: Fn(&'sg DATA) -> O,
{
type Output = O;

fn data_wf(&self, data: &DATA) -> O {
self(data)
}
}

/// When you don't specify the data well-formedness, this is the default.
/// It considers all data well-formed.
#[derive(Default)]
pub struct DefaultDataWellformedness {}

impl<DATA, O: From<bool>> DataWellformedness<DATA, O> for DefaultDataWellformedness {
fn data_wf(&self, _data: &DATA) -> O {
From::from(true) // match all data by default
impl<DATA> DataWellformedness<DATA> for DefaultDataWellformedness {
type Output = bool;

fn data_wf(&self, _data: &DATA) -> bool {
true // match all data by default
}
}

Expand Down

0 comments on commit 97427c6

Please sign in to comment.