Skip to content

Commit

Permalink
feat: seed generation
Browse files Browse the repository at this point in the history
GitOrigin-RevId: a1dc5773313434fec57d0913ba33969c0ffa2f31
  • Loading branch information
christos-h authored and oq-bot committed Apr 23, 2021
1 parent 152c1eb commit 955714b
Show file tree
Hide file tree
Showing 19 changed files with 173 additions and 93 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "synth-core"
version = "0.4.2"
version = "0.4.3"
authors = [
"Damien Broka <[email protected]>",
"Christos Hadjiaslanis <[email protected]>"
Expand Down
38 changes: 19 additions & 19 deletions core/src/compile/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{HashMap, HashSet, VecDeque};
use std::collections::{BTreeMap, BTreeSet, VecDeque};
use std::iter::{DoubleEndedIterator, FromIterator, IntoIterator};

use anyhow::{Context, Result};
Expand Down Expand Up @@ -42,7 +42,7 @@ pub trait Compile {
}

pub struct StructuredState<'a> {
children: HashMap<String, CompilerState<'a>>,
children: BTreeMap<String, CompilerState<'a>>,
ordering: Vec<String>,
}

Expand Down Expand Up @@ -84,7 +84,7 @@ impl<'a> std::iter::Extend<(String, CompilerState<'a>)> for StructuredState<'a>
impl<'a> Default for StructuredState<'a> {
fn default() -> Self {
Self {
children: HashMap::new(),
children: BTreeMap::new(),
ordering: Vec::new(),
}
}
Expand All @@ -94,7 +94,7 @@ pub mod structured_state {
use super::*;

pub struct IntoIter<'a> {
children: HashMap<String, CompilerState<'a>>,
children: BTreeMap<String, CompilerState<'a>>,
iter: std::vec::IntoIter<String>,
}

Expand Down Expand Up @@ -236,7 +236,7 @@ pub struct CompilerState<'a> {
src: Source<'a>,
compiled: OutputState<Graph>,
scope: StructuredState<'a>,
refs: HashSet<Scope>,
refs: BTreeSet<Scope>,
}

impl<'a> std::iter::Extend<(String, CompilerState<'a>)> for CompilerState<'a> {
Expand All @@ -246,7 +246,7 @@ impl<'a> std::iter::Extend<(String, CompilerState<'a>)> for CompilerState<'a> {
}
}

#[derive(Default, Clone, Eq, PartialEq, Hash)]
#[derive(Default, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct Scope(VecDeque<String>);

impl std::fmt::Display for Scope {
Expand Down Expand Up @@ -400,7 +400,7 @@ impl<'a> CompilerState<'a> {
src: source,
compiled: OutputState::default(),
scope: StructuredState::default(),
refs: HashSet::default(),
refs: BTreeSet::default(),
}
}

Expand Down Expand Up @@ -566,7 +566,7 @@ impl<'a> NamespaceCompiler<'a> {
continue;
}

let mut drivers = HashMap::new();
let mut drivers = BTreeMap::new();

let ctx = self
.vtable
Expand Down Expand Up @@ -641,7 +641,7 @@ impl<'a> NamespaceCompiler<'a> {
pub struct ContentCompiler<'c, 'a: 'c> {
scope: Scope,
cursor: &'c mut CompilerState<'a>,
drivers: &'c mut HashMap<Scope, Driver<Graph>>,
drivers: &'c mut BTreeMap<Scope, Driver<Graph>>,
vtable: &'c mut Symbols,
}

Expand Down Expand Up @@ -700,14 +700,14 @@ impl<'c, 'a: 'c> Compiler<'a> for ContentCompiler<'c, 'a> {
}

pub struct ReferenceFactory<G: Generator> {
issued: HashSet<Scope>,
issued: BTreeSet<Scope>,
src: Option<Cursored<G>>,
}

impl<G: Generator> Default for ReferenceFactory<G> {
fn default() -> Self {
Self {
issued: HashSet::new(),
issued: BTreeSet::new(),
src: None,
}
}
Expand Down Expand Up @@ -740,7 +740,7 @@ where
}

pub struct LocalTable<G: Generator> {
locals: HashMap<Scope, ReferenceFactory<G>>,
locals: BTreeMap<Scope, ReferenceFactory<G>>,
}

impl<G> Default for LocalTable<G>
Expand All @@ -749,7 +749,7 @@ where
{
fn default() -> Self {
Self {
locals: HashMap::new(),
locals: BTreeMap::new(),
}
}
}
Expand All @@ -760,14 +760,14 @@ where
GeneratorOutput<G>: Clone,
{
/// @brokad: fix panic
fn extract(&self) -> HashMap<Scope, Cursored<G>> {
fn extract(&self) -> BTreeMap<Scope, Cursored<G>> {
self.locals
.iter()
.map(|(scope, factory)| (scope.clone(), factory.src.as_ref().unwrap().clone()))
.collect()
}

fn closure(&self, what: &Scope) -> HashSet<Scope> {
fn closure(&self, what: &Scope) -> BTreeSet<Scope> {
let what_root = what.root();
self.locals
.iter()
Expand Down Expand Up @@ -850,8 +850,8 @@ where
}

pub struct Symbols<G: Generator = Graph> {
flattened: HashSet<Scope>,
storage: HashMap<Scope, LocalTable<G>>,
flattened: BTreeSet<Scope>,
storage: BTreeMap<Scope, LocalTable<G>>,
}

impl<G> Symbols<G>
Expand All @@ -861,8 +861,8 @@ where
{
fn new() -> Self {
Self {
flattened: HashSet::new(),
storage: HashMap::new(),
flattened: BTreeSet::new(),
storage: BTreeMap::new(),
}
}

Expand Down
30 changes: 19 additions & 11 deletions core/src/compile/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use synth_gen::prelude::*;

use std::cell::RefCell;
use std::collections::HashMap;
use std::collections::BTreeMap;
use std::ops::Range;
use std::rc::Rc;

Expand All @@ -18,7 +18,7 @@ struct UniqueRange<Idx> {
range: Range<Idx>,
}

impl Eq for UniqueRange<usize> { }
impl Eq for UniqueRange<usize> {}

impl PartialEq for UniqueRange<usize> {
fn eq(&self, other: &Self) -> bool {
Expand All @@ -44,7 +44,7 @@ struct UniqueMarker {
marker: Marker,
}

impl Eq for UniqueMarker { }
impl Eq for UniqueMarker {}

impl PartialEq for UniqueMarker {
fn eq(&self, other: &Self) -> bool {
Expand Down Expand Up @@ -409,8 +409,8 @@ where
}

pub struct Scoped<G: Generator> {
pub(super) cursors: HashMap<Scope, Cursored<G>>,
pub(super) drivers: HashMap<Scope, Driver<G>>,
pub(super) cursors: BTreeMap<Scope, Cursored<G>>,
pub(super) drivers: BTreeMap<Scope, Driver<G>>,
pub(super) order: Vec<Scope>,
pub(super) src: Box<G>,
pub(super) is_complete: bool,
Expand Down Expand Up @@ -487,13 +487,14 @@ pub mod tests {
view.next(&mut rng);
}
assert!(
(threshold > 0 && driver.src.deref().borrow().buf.len() < initial_buf_len) ||
(threshold == 0 && driver.src.deref().borrow().buf.len() == initial_buf_len)
(threshold > 0 && driver.src.deref().borrow().buf.len() < initial_buf_len)
|| (threshold == 0 && driver.src.deref().borrow().buf.len() == initial_buf_len)
);
}
// Multiple views
for threshold in vec![0, 1, 10] {
struct TestView<G> where
struct TestView<G>
where
G: Generator,
{
view: Rc<RefCell<View<G>>>,
Expand Down Expand Up @@ -521,13 +522,20 @@ pub mod tests {
while view_pos.len() > 0 {
let i: usize = (rng.next_u32() as usize) % view_pos.len();
view_pos[i].pos += 1;
view_pos[i].borrow_mut().view.deref().borrow_mut().next(&mut rng);
view_pos[i]
.borrow_mut()
.view
.deref()
.borrow_mut()
.next(&mut rng);
if view_pos[i].borrow().pos >= driver.src.deref().borrow().buf.len() - threshold {
view_pos.remove(i);
}
}
assert!((threshold > 0 && driver.src.deref().borrow().buf.len() < initial_buf_len) ||
(threshold == 0 && driver.src.deref().borrow().buf.len() == initial_buf_len));
assert!(
(threshold > 0 && driver.src.deref().borrow().buf.len() < initial_buf_len)
|| (threshold == 0 && driver.src.deref().borrow().buf.len() == initial_buf_len)
);
}
}
}
4 changes: 2 additions & 2 deletions core/src/graph/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::BTreeMap;
use std::ops::Try;

use anyhow::{Context, Result};
Expand Down Expand Up @@ -182,7 +182,7 @@ derive_from! {
Number(Number),
String(String),
DateTime(ChronoValue),
Object(HashMap<String, Value>),
Object(BTreeMap<String, Value>),
Array(Vec<Value>),
}
}
Expand Down
45 changes: 23 additions & 22 deletions core/src/graph/object.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use super::prelude::*;

use std::collections::BTreeMap;
use synth_gen::value::Map;

pub struct KeyValueOrNothing {
inner: Concatenate<JustToken<String>, Graph>,
p: f64,
active: bool
active: bool,
}

impl Generator for KeyValueOrNothing {
Expand All @@ -14,38 +15,38 @@ impl Generator for KeyValueOrNothing {
type Return = Option<(String, Result<Value, Error>)>;

fn next<R: Rng>(&mut self, rng: &mut R) -> GeneratorState<Self::Yield, Self::Return> {
if ! self.active {
if !self.active {
if self.p >= rng.gen() {
self.active = true;
self.next(rng)
} else {
GeneratorState::Complete(None)
}
} else {
let next = self.inner.next(rng);
if next.is_complete() {
self.active = false;
}
next.map_complete(|c| Some(c))
}
self.active = true;
self.next(rng)
} else {
GeneratorState::Complete(None)
}
} else {
let next = self.inner.next(rng);
if next.is_complete() {
self.active = false;
}
next.map_complete(|c| Some(c))
}
}
}

impl KeyValueOrNothing {
pub fn new_with(key: &str, content: Graph, freq: f64) -> Self {
Self {
inner: content.with_key(key.to_string().yield_token()),
p: freq,
active: false
}
Self {
inner: content.with_key(key.to_string().yield_token()),
p: freq,
active: false,
}
}

pub fn always(key: &str, content: Graph) -> Self {
Self::new_with(key, content, 1.0)
Self::new_with(key, content, 1.0)
}

pub fn sometimes(key: &str, content: Graph) -> Self {
Self::new_with(key, content, 0.5)
Self::new_with(key, content, 0.5)
}
}

Expand All @@ -66,7 +67,7 @@ impl Generator for ObjectNode {
self.0.next(rng).map_complete(|kv| {
kv.into_iter()
.filter_map(|m_kv| m_kv.map(|(k, vr)| vr.map(|v| (k, v))))
.collect::<Result<HashMap<_, _>, Error>>()
.collect::<Result<BTreeMap<_, _>, Error>>()
.map(|hm| hm.into())
})
}
Expand Down
23 changes: 13 additions & 10 deletions core/src/graph/string/faker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,16 @@ impl RandFaker {
format!("_{}", uuid::Uuid::new_v4().to_simple().to_string())
}

fn generate<'p, R>(&self, python: Python<'p>) -> PyResult<R>
fn generate<'p, R>(&self, python: Python<'p>, seed: u64) -> PyResult<R>
where
R: FromPyObject<'p>,
{
let faker_instance = python
.import("__main__")
.and_then(|main| main.getattr(&self.faker_instance_name))?;
// Try seeding here
let faker_class = python.import("faker")?.get("Faker")?;
faker_class.call_method("seed", (seed.to_object(python),), None)?;
faker_instance
.call_method(
&self.generator,
Expand Down Expand Up @@ -102,15 +105,15 @@ impl Generator for RandFaker {

type Return = Result<Never, Error>;

fn next<R: Rng>(&mut self, _rng: &mut R) -> GeneratorState<Self::Yield, Self::Return> {
fn next<R: Rng>(&mut self, rng: &mut R) -> GeneratorState<Self::Yield, Self::Return> {
let gil = Python::acquire_gil();
match self.generate::<String>(gil.python()) {
Ok(output) => GeneratorState::Yielded(output),
Err(err) => GeneratorState::Complete(Err(failed_crate!(
target: Release,
"a call to faker failed: {}",
err
)))
}
match self.generate::<String>(gil.python(), rng.next_u64()) {
Ok(output) => GeneratorState::Yielded(output),
Err(err) => GeneratorState::Complete(Err(failed_crate!(
target: Release,
"a call to faker failed: {}",
err
))),
}
}
}
Loading

0 comments on commit 955714b

Please sign in to comment.