Skip to content

Commit b17f962

Browse files
committed
Move to local placer
1 parent 1374e21 commit b17f962

File tree

4 files changed

+82
-83
lines changed

4 files changed

+82
-83
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
derive_more = "1.0.0"
9+
derive_more = { version = "1.0.0", features = ["deref"] }
1010
disjoint-set = "0.0.2"
1111
eyre = "0.6.8"
1212
fastnbt = "2"

src/graph/logic/mod.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
use std::ops::Deref;
1+
use crate::transform::logic::LogicGraphTransformer;
22

33
use super::Graph;
44

55
pub mod builder;
66

7-
#[derive(Debug, Clone, Deref)]
7+
#[derive(Debug, Clone, derive_more::Deref)]
88
pub struct LogicGraph {
99
#[deref]
1010
pub graph: Graph,
1111
}
12+
13+
impl LogicGraph {
14+
pub fn prepare_place(self) -> eyre::Result<Self> {
15+
let mut transform = LogicGraphTransformer::new(self);
16+
transform.decompose_and()?;
17+
transform.remove_double_neg_expression();
18+
Ok(transform.finish())
19+
}
20+
}

src/transform/component/mod.rs

+7-40
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// Generate general World3D Component from LogicGraph
22

33
use crate::{
4-
graph::logic::LogicGraph,
4+
graph::{logic::LogicGraph, world::builder::PlaceBound},
55
world::{position::DimSize, world::World3D},
66
};
77

8+
use super::placer::PlacedNode;
9+
810
// struct IOTemplate {}
911

1012
struct Template {
@@ -15,43 +17,8 @@ struct Template {
1517
// todo!()
1618
// }
1719

18-
// 가능한 모든 경우의 수를 생성하는 것을 목표로한다.
19-
pub fn generate(graph: &LogicGraph) -> World3D {
20-
let inputs = graph.inputs();
21-
todo!()
22-
}
23-
24-
#[cfg(test)]
25-
mod tests {
26-
27-
use crate::{
28-
graph::{
29-
graphviz::ToGraphvizGraph,
30-
logic::{builder::LogicGraphBuilder, LogicGraph},
31-
},
32-
nbt::{NBTRoot, ToNBT},
33-
transform::{component::generate, logic::LogicGraphTransformer},
34-
};
35-
36-
fn build_graph_from_stmt(stmt: &str, output: &str) -> eyre::Result<LogicGraph> {
37-
LogicGraphBuilder::new(stmt.to_string()).build(output.to_string())
38-
}
39-
40-
#[test]
41-
fn test_generate_component_and() -> eyre::Result<()> {
42-
let logic_graph = build_graph_from_stmt("a&b", "c")?;
43-
44-
let mut transform = LogicGraphTransformer::new(logic_graph);
45-
transform.decompose_and()?;
46-
transform.remove_double_neg_expression();
47-
let logic_graph = transform.finish();
48-
println!("{}", logic_graph.to_graphviz());
49-
50-
let world3d = generate(&logic_graph);
51-
52-
let nbt: NBTRoot = world3d.to_nbt();
53-
nbt.save("test/and-gate-new.nbt");
54-
55-
Ok(())
56-
}
20+
struct GenerationState {
21+
world: World3D,
22+
// for cache
23+
nodes: Vec<PlacedNode>,
5724
}

src/transform/placer/mod.rs

+63-40
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
use std::{collections::HashSet, iter::repeat_with};
22

3-
use petgraph::stable_graph::NodeIndex;
3+
use eyre::ensure;
44

55
use crate::{
66
graph::{
7+
logic::LogicGraph,
78
world::{
89
builder::{PlaceBound, PropagateType},
910
WorldGraph,
1011
},
11-
GraphNodeId, GraphNodeKind, SubGraphWithGraph,
12+
GraphNodeKind,
1213
},
1314
world::{block::Block, position::Position, world::World3D},
1415
};
@@ -35,61 +36,52 @@ impl PlacedNode {
3536
.into_iter()
3637
.collect()
3738
}
39+
40+
pub fn has_conflict_with(&self, other: &Self) -> bool {
41+
todo!()
42+
}
43+
44+
// pub fn
3845
}
3946

40-
pub struct LocalPlacer<'a> {
41-
graph: SubGraphWithGraph<'a>,
42-
try_count: usize,
43-
max_width_size: usize,
44-
max_height_size: usize,
47+
pub struct LocalPlacer {
48+
graph: LogicGraph,
4549
}
4650

4751
pub const K_MAX_LOCAL_PLACE_NODE_COUNT: usize = 25;
4852

49-
impl<'a> LocalPlacer<'a> {
50-
// you should not pass side effected sub-graph
51-
pub fn new(
52-
graph: SubGraphWithGraph<'a>,
53-
try_count: usize,
54-
max_width_size: Option<usize>,
55-
max_height_size: Option<usize>,
56-
) -> eyre::Result<Self> {
57-
assert!(try_count > 0);
58-
59-
Self::verify(&graph);
60-
61-
Ok(Self {
62-
graph,
63-
try_count,
64-
max_width_size: max_width_size.unwrap_or(usize::MAX),
65-
max_height_size: max_height_size.unwrap_or(usize::MAX),
66-
})
53+
impl LocalPlacer {
54+
pub fn new(graph: LogicGraph) -> eyre::Result<Self> {
55+
let result = Self { graph };
56+
result.verify()?;
57+
Ok(result)
6758
}
6859

69-
pub fn verify(graph: &SubGraphWithGraph<'a>) {
70-
assert!(graph.nodes.len() > 0);
71-
assert!(graph.nodes.len() <= K_MAX_LOCAL_PLACE_NODE_COUNT);
72-
73-
for node_id in &graph.nodes {
74-
assert!(matches!(
75-
graph.graph.find_node_by_id(*node_id).unwrap().kind,
76-
GraphNodeKind::Logic { .. }
77-
));
60+
fn verify(&self) -> eyre::Result<()> {
61+
ensure!(self.graph.nodes.len() > 0, "");
62+
ensure!(self.graph.nodes.len() <= K_MAX_LOCAL_PLACE_NODE_COUNT, "");
63+
64+
for node_id in &self.graph.nodes {
65+
ensure!(
66+
matches!(
67+
self.graph.find_node_by_id(node_id.id).unwrap().kind,
68+
GraphNodeKind::Logic { .. }
69+
),
70+
""
71+
);
7872
}
79-
}
8073

81-
pub fn generate(&mut self) -> WorldGraph {
82-
let try_count = self.try_count;
74+
Ok(())
75+
}
8376

84-
// TODO: make parallel
77+
pub fn generate(&mut self) -> World3D {
8578
repeat_with(|| self.next_place())
86-
.take(try_count)
8779
.min_by_key(|(_, c)| *c)
8880
.unwrap()
8981
.0
9082
}
9183

92-
fn next_place(&mut self) -> (WorldGraph, usize) {
84+
fn next_place(&mut self) -> (World3D, usize) {
9385
todo!()
9486
}
9587
}
@@ -109,3 +101,34 @@ impl<'a> LocalPlacerCostEstimator<'a> {
109101
todo!()
110102
}
111103
}
104+
105+
#[cfg(test)]
106+
mod tests {
107+
108+
use crate::{
109+
graph::{
110+
graphviz::ToGraphvizGraph,
111+
logic::{builder::LogicGraphBuilder, LogicGraph},
112+
},
113+
nbt::{NBTRoot, ToNBT},
114+
transform::placer::LocalPlacer,
115+
};
116+
117+
fn build_graph_from_stmt(stmt: &str, output: &str) -> eyre::Result<LogicGraph> {
118+
LogicGraphBuilder::new(stmt.to_string()).build(output.to_string())
119+
}
120+
121+
#[test]
122+
fn test_generate_component_and() -> eyre::Result<()> {
123+
let logic_graph = build_graph_from_stmt("a&b", "c")?.prepare_place()?;
124+
println!("{}", logic_graph.to_graphviz());
125+
126+
let mut placer = LocalPlacer::new(logic_graph)?;
127+
let world3d = placer.generate();
128+
129+
let nbt: NBTRoot = world3d.to_nbt();
130+
nbt.save("test/and-gate-new.nbt");
131+
132+
Ok(())
133+
}
134+
}

0 commit comments

Comments
 (0)