Skip to content

Commit

Permalink
fix drepr inference; minor change in the backend interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Binh Vu committed Mar 3, 2020
1 parent 74115e8 commit 966157c
Show file tree
Hide file tree
Showing 55 changed files with 1,208 additions and 54 deletions.
2 changes: 1 addition & 1 deletion drepr/Cargo.lock

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

12 changes: 10 additions & 2 deletions drepr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,20 @@ On MacOS, cargo produces dynamic linking for libstd (`@rpath\libstd...`)

You can check linking on MacOS using otool

### Awared Issues
### Known Issues

1. It is very slow to build

Rust-cpython is very slow to build. You can disable the python feature if you are in the debug mode using `--features "disable-python readers/disable-python"` flag.
However, it only works if you are in the crate folder, cargo does not support passing `features` flag in the workspace folder yet ([see more](https://github.com/rust-lang/cargo/issues/5015))
However, it only works if you are in the crate folder, cargo does not support passing `features` flag in the workspace folder yet ([see more](https://github.com/rust-lang/cargo/issues/5015)).
For example, you need to be in the engine folder to set the flag.

Note: if you run test, disable python features will also significantly improve the building time but remember to change the working directory. Here is one example of the command:

```.env
cargo test --features "disable-python readers/disable-python" --package engine --test main alignments::inference::test_infer_func::smoke_test -- --exact
```



# Useful commands
Expand Down
2 changes: 1 addition & 1 deletion drepr/engine/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[package]
name = "engine"
version = "1.0.7" # ___PKG_VERSION___: DO NOT MODIFY the version here. Update it via version_manager.py!
version = "1.0.8" # ___PKG_VERSION___: DO NOT MODIFY the version here. Update it via version_manager.py!
authors = ["Binh Vu <[email protected]>"]
edition = "2018"

Expand Down
13 changes: 8 additions & 5 deletions drepr/engine/src/alignments/dfs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use petgraph::visit::{GraphRef, IntoNeighbors, Visitable, VisitMap};
use fnv::FnvHashSet;
use std::hash::Hash;

/// THE FOLLOWING CODE IS COPIED FROM: https://docs.rs/petgraph/0.4.13/src/petgraph/visit/traversal.rs.html#38-43
///
/// so that DFS also yield the parent node
///
/// Visit nodes of a graph in a depth-first-search (DFS) emitting nodes in
/// preorder (when they are first discovered).
Expand Down Expand Up @@ -42,7 +44,7 @@ pub struct CustomedDfs<N, VM> {
}

impl<N, VM> CustomedDfs<N, VM>
where N: Copy + PartialEq,
where N: Copy + PartialEq + Eq + Hash,
VM: VisitMap<N>,
{
/// Create a new **Dfs**, using the graph's visitor map, and put **start**
Expand Down Expand Up @@ -75,18 +77,19 @@ impl<N, VM> CustomedDfs<N, VM>
}

/// Return the next node in the dfs, or **None** if the traversal is done.
pub fn next<G>(&mut self, graph: G) -> Option<(N, N)>
pub fn next<G>(&mut self, graph: G, revisit: &FnvHashSet<N>) -> Option<(N, N)>
where G: IntoNeighbors<NodeId=N>,
{
if let Some((parent_node, node)) = self.stack.pop() {
for succ in graph.neighbors(node) {
if self.discovered.visit(succ) {
if self.discovered.visit(succ) || revisit.contains(&succ) {
self.stack.push((node, succ));
}
}

return Some((parent_node, node));
}
None
}


}
20 changes: 13 additions & 7 deletions drepr/engine/src/alignments/inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use petgraph::prelude::*;
use crate::lang::{AlignedDim, Alignment, Cardinality, Description, RangeAlignment};

use super::dfs::CustomedDfs;
use fnv::FnvHashSet;

pub struct AlignmentInference<'a> {
desc: &'a Description,
Expand Down Expand Up @@ -161,28 +162,33 @@ impl<'a> AlignmentInference<'a> {
let mut new_incoming_edges = vec![];

let mut dfs = CustomedDfs::new(&mg, u0);
if dfs.next(&mg).is_some() {
let mut revisit = FnvHashSet::default();

if dfs.next(&mg, &revisit).is_some() {
// call next first to skip the u0
loop {
// recording the length of the current stack
// so that we know if we need to stop from exploring further from the next node
// we can pop all of its children
let stack_len = dfs.stack.len();
let (u1, u2) = match dfs.next(&mg) {
let (u1, u2) = match dfs.next(&mg, &revisit) {
None => break,
Some((u1, u2)) => (u1, u2)
};
if !mg.contains_edge(u0, u2) {

if mg.contains_edge(u0, u1) && !mg.contains_edge(u0, u2) {
// try to infer alignment function between u0 and u2
match self.infer_func(u0, u1, u2) {
None => {
// don't haven't find any, hence we have to stop from exploring u2
// haven't found any, hence we have to stop from exploring u2
// plus 1 because we take into account the u2 node, which was popped
for _ in 0..(dfs.stack.len() + 1 - stack_len) {
// remove all children of u2
dfs.stack.pop();
}
// mark this u2 as re-visited because it may be discovered from other nodes
// we should not have infinite recursive loop here
revisit.insert(u2);
continue;
}
Some(afuncs) => {
Expand All @@ -198,7 +204,7 @@ impl<'a> AlignmentInference<'a> {
}
}
}

n_new_edges += new_incoming_edges.len() + new_outgoing_edges.len();

for ui in new_outgoing_edges {
Expand All @@ -219,7 +225,7 @@ impl<'a> AlignmentInference<'a> {
/// Infer an alignment function of xid and zid given alignments between (xid, yid) and (yid, zid)
///
/// If there is only one way to join values of xid and zid, then the chain join will be the correct one
fn infer_func(&self, xid: usize, yid: usize, zid: usize) -> Option<Vec<Alignment>> {
pub fn infer_func(&self, xid: usize, yid: usize, zid: usize) -> Option<Vec<Alignment>> {
let f = &self.aligns[xid][yid];
let g = &self.aligns[yid][zid];

Expand Down
6 changes: 5 additions & 1 deletion drepr/engine/src/executors/classes_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use crate::executors::classes_map::generic_algo::generic_class_map;
use crate::executors::preprocessing::exec_preprocessing;
use crate::lang::{Description, Resource};
use crate::writers::stream_writer::{OutputFormat};
use crate::writers::stream_writer::{GraphJSONWriter, TTLStreamWriter, GraphPyWriter};
use crate::writers::stream_writer::{GraphJSONWriter, TTLStreamWriter};
#[cfg(not(feature = "disable-python"))]
use crate::writers::stream_writer::GraphPyWriter;
use crate::writers::stream_writer::stream_writer::{StreamWriterResult, WriteResult};
use crate::execution_plans::classes_map_plan::class_map_plan::ClassMapExecStrategy;
#[cfg(feature = "enable-exec-macro-cls-map")]
Expand Down Expand Up @@ -85,6 +87,7 @@ pub fn classes_map(resource_files: &[PhysicalResource], desc: &Description, plan
&format!("{}.edge", fpath),
&desc.semantic_model))
}
#[cfg(not(feature = "disable-python"))]
PhysicalOutput::File { fpath: _, format: OutputFormat::GraphPy } => {
unimplemented!()
}
Expand All @@ -94,6 +97,7 @@ pub fn classes_map(resource_files: &[PhysicalResource], desc: &Description, plan
PhysicalOutput::Memory { format: OutputFormat::GraphJSON } => {
Box::new(GraphJSONWriter::write2str(&desc.semantic_model))
}
#[cfg(not(feature = "disable-python"))]
PhysicalOutput::Memory { format: OutputFormat::GraphPy } => {
Box::new(GraphPyWriter::write2mem(&desc.semantic_model))
}
Expand Down
2 changes: 1 addition & 1 deletion drepr/engine/src/lang/alignment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use readers::{is_enum_type_impl, as_enum_type_impl, into_enum_type_impl};
pub mod range_alignment;
pub mod value_alignment;

#[derive(Deserialize, Debug, Clone, Serialize)]
#[derive(Deserialize, Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "type")]
pub enum Alignment {
#[serde(rename = "range")]
Expand Down
4 changes: 2 additions & 2 deletions drepr/engine/src/lang/alignment/range_alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use crate::lang::alignment::Cardinality;
use hashbrown::HashSet;
use std::iter::FromIterator;

#[derive(Debug, Clone, Deserialize, PartialEq, Serialize)]
#[derive(Debug, Clone, Deserialize, PartialEq, Eq, Serialize)]
pub struct AlignedDim {
#[serde(rename = "source")]
pub source_dim: usize,
#[serde(rename = "target")]
pub target_dim: usize,
}

#[derive(Deserialize, Debug, Clone, Serialize)]
#[derive(Deserialize, Debug, Clone, PartialEq, Eq, Serialize)]
pub struct RangeAlignment {
pub source: usize,
pub target: usize,
Expand Down
2 changes: 1 addition & 1 deletion drepr/engine/src/lang/alignment/value_alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
use crate::lang::alignment::Cardinality;
use crate::lang::description::Description;

#[derive(Deserialize, Serialize, Debug, Clone)]
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
pub struct ValueAlignment {
pub source: usize,
pub target: usize,
Expand Down
3 changes: 3 additions & 0 deletions drepr/engine/src/writers/stream_writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ pub use self::stream_class_writer::StreamClassWriter;
pub use self::stream_writer::{StreamWriter};
pub use self::turtle::TTLStreamWriter;
pub use self::graph_json::GraphJSONWriter;
#[cfg(not(feature = "disable-python"))]
pub use self::graph_py::GraphPyWriter;

pub mod turtle;
pub mod graph_json;
#[cfg(not(feature = "disable-python"))]
pub mod graph_py;
pub mod stream_writer;
pub mod stream_class_writer;
Expand Down Expand Up @@ -58,6 +60,7 @@ pub enum OutputFormat {
TTL,
#[serde(rename = "graph_json")]
GraphJSON,
#[cfg(not(feature = "disable-python"))]
#[serde(rename = "graph_py")]
GraphPy
}
Expand Down
1 change: 1 addition & 0 deletions drepr/engine/tests/alignments/inference/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod test_infer_func;
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"alignments": [
{
"pair": [9, 8],
"aligns": [
{"type": "range", "source": 9, "target": 8, "aligned_dims": [
{"source": 3, "target": 3}, {"source": 4, "target": 4}
]}
]
},
{
"pair": [9, 0],
"aligns": [
{"type": "range", "source": 9, "target": 0, "aligned_dims": [
{"source": 3, "target": 2}
]}
]
},
{
"pair": [8, 1],
"aligns": [
{"type": "range", "source": 8, "target": 1, "aligned_dims": [
{"source": 4, "target": 2}
]}
]
},
{
"pair": [9, 1],
"aligns": [
{"type": "range", "source": 9, "target": 1, "aligned_dims": [
{"source": 4, "target": 2}
]}
]
}
],
"infer_funcs": [
{
"triple": [9, 8, 1],
"aligns": [
{"type": "range", "source": 9, "target": 1, "aligned_dims": [
{"source": 4, "target": 2}
]}
]
}
]
}
Loading

0 comments on commit 966157c

Please sign in to comment.