Commit 1397e3e8 by dongshufeng

change example

parent ba8a8619
...@@ -7,4 +7,5 @@ pub const POINT_DF_NAME: &str = "point_terminal_phase"; ...@@ -7,4 +7,5 @@ pub const POINT_DF_NAME: &str = "point_terminal_phase";
pub const DYN_TOPO_DF_NAME: &str = "dyn_topo"; pub const DYN_TOPO_DF_NAME: &str = "dyn_topo";
pub const DEV_TOPO_DF_NAME: &str = "dev_topo"; pub const DEV_TOPO_DF_NAME: &str = "dev_topo";
pub const DEV_CONDUCTOR_DF_NAME: &str = "dev_ohm"; pub const DEV_CONDUCTOR_DF_NAME: &str = "dev_ohm";
pub const SHUNT_MEAS_DF_NAME: &str = "shunt_meas";
pub const TN_INPUT_DF_NAME: &str = "tn_input"; pub const TN_INPUT_DF_NAME: &str = "tn_input";
use std::collections::HashMap; use std::collections::HashMap;
use csv::StringRecordsIter; use csv::StringRecordsIter;
use mems::model::dev::PsRsrType; use mems::model::dev::{MeasPhase, PsRsrType};
pub fn read_points(records: &mut StringRecordsIter<&[u8]>) -> Result<Vec<Vec<u64>>, String> { pub fn read_point_terminal(records: &mut StringRecordsIter<&[u8]>,
mut meas_phase: Option<&mut Vec<MeasPhase>>) -> Result<Vec<Vec<u64>>, String> {
let mut points = Vec::new(); let mut points = Vec::new();
// 按行读取csv // 按行读取csv
let mut row = 0; let mut row = 0;
...@@ -12,18 +13,23 @@ pub fn read_points(records: &mut StringRecordsIter<&[u8]>) -> Result<Vec<Vec<u64 ...@@ -12,18 +13,23 @@ pub fn read_points(records: &mut StringRecordsIter<&[u8]>) -> Result<Vec<Vec<u64
points.push(vec![0u64; 2]); points.push(vec![0u64; 2]);
let mut col = 0; let mut col = 0;
for str in record.iter() { for str in record.iter() {
if let Ok(id) = str.parse() { if col < 2 {
points[row][col] = id; if let Ok(id) = str.parse() {
} else { points[row][col] = id;
return Err(format!("Wrong point input, row {row} col {col}")); } else {
return Err(format!("Wrong point input, row {row} col {col}"));
}
} else if meas_phase.is_some() {
meas_phase.as_mut().unwrap().push(MeasPhase::from(str))
} }
col += 1; col += 1;
if col == 2 { if col == 3 {
break; break;
} }
} }
if col != 2 { if col != 3 {
return Err(format!("Wrong point input, expected col at least 2, actual {col}")); return Err(format!("Wrong point input, expected col at least 3, actual {col}"));
} }
} }
Some(Err(e)) => { Some(Err(e)) => {
...@@ -38,7 +44,7 @@ pub fn read_points(records: &mut StringRecordsIter<&[u8]>) -> Result<Vec<Vec<u64 ...@@ -38,7 +44,7 @@ pub fn read_points(records: &mut StringRecordsIter<&[u8]>) -> Result<Vec<Vec<u64
Ok(points) Ok(points)
} }
pub fn read_terminals(records: &mut StringRecordsIter<&[u8]>) -> Result<Vec<Vec<u64>>, String> { pub fn read_terminal_cn_dev(records: &mut StringRecordsIter<&[u8]>) -> Result<Vec<Vec<u64>>, String> {
let mut terminals: Vec<Vec<u64>> = Vec::new(); let mut terminals: Vec<Vec<u64>> = Vec::new();
// 按行读取csv // 按行读取csv
let mut row = 0; let mut row = 0;
...@@ -74,10 +80,11 @@ pub fn read_terminals(records: &mut StringRecordsIter<&[u8]>) -> Result<Vec<Vec< ...@@ -74,10 +80,11 @@ pub fn read_terminals(records: &mut StringRecordsIter<&[u8]>) -> Result<Vec<Vec<
Ok(terminals) Ok(terminals)
} }
pub fn read_edges(records: &mut StringRecordsIter<&[u8]>) pub fn read_static_topo(records: &mut StringRecordsIter<&[u8]>,
-> Result<(Vec<Vec<u64>>, HashMap<u64, bool>), String> { mut normal_open: Option<&mut HashMap<u64, bool>>,
mut dev_type: Option<&mut HashMap<u64, u16>>)
-> Result<Vec<Vec<u64>>, String> {
let mut edges = Vec::new(); let mut edges = Vec::new();
let mut normal_open = HashMap::new();
let mut row = 0; let mut row = 0;
let swich_type = PsRsrType::Switch as u16; let swich_type = PsRsrType::Switch as u16;
// 按行读取csv // 按行读取csv
...@@ -100,20 +107,26 @@ pub fn read_edges(records: &mut StringRecordsIter<&[u8]>) ...@@ -100,20 +107,26 @@ pub fn read_edges(records: &mut StringRecordsIter<&[u8]>)
} else { } else {
return Err(format!("Wrong static topology input, row {row} col {col}")); return Err(format!("Wrong static topology input, row {row} col {col}"));
} }
} else if col == 4 && is_switch { } else if col == 4 && is_switch && normal_open.is_some() {
if let Ok(b) = str.parse::<bool>() { if let Ok(b) = str.parse::<bool>() {
normal_open.insert(edges[row][2], b); normal_open.as_mut().unwrap().insert(edges[row][2], b);
} else {
return Err(format!("Wrong static topology input, row {row} col {col}"));
}
} else if col == 5 && is_switch && dev_type.is_some() {
if let Ok(v) = str.parse::<u16>() {
dev_type.as_mut().unwrap().insert(edges[row][2], v);
} else { } else {
return Err(format!("Wrong static topology input, row {row} col {col}")); return Err(format!("Wrong static topology input, row {row} col {col}"));
} }
} }
col += 1; col += 1;
if col == 5 { if col == 6 {
break; break;
} }
} }
if col != 5 { if col != 5 {
return Err(format!("Wrong static topology input, expected col at least 5, actual {col}")); return Err(format!("Wrong static topology input, expected col at least 6, actual {col}"));
} }
} }
Some(Err(e)) => { Some(Err(e)) => {
...@@ -125,5 +138,5 @@ pub fn read_edges(records: &mut StringRecordsIter<&[u8]>) ...@@ -125,5 +138,5 @@ pub fn read_edges(records: &mut StringRecordsIter<&[u8]>)
} }
row += 1; row += 1;
} }
Ok((edges, normal_open)) Ok(edges)
} }
\ No newline at end of file
...@@ -3,7 +3,7 @@ use std::collections::HashMap; ...@@ -3,7 +3,7 @@ use std::collections::HashMap;
use arrow_schema::{DataType, Field, Schema}; use arrow_schema::{DataType, Field, Schema};
use ds_common::{DEV_TOPO_DF_NAME, DYN_TOPO_DF_NAME, POINT_DF_NAME, STATIC_TOPO_DF_NAME, TERMINAL_DF_NAME}; use ds_common::{DEV_TOPO_DF_NAME, DYN_TOPO_DF_NAME, POINT_DF_NAME, STATIC_TOPO_DF_NAME, TERMINAL_DF_NAME};
use ds_common::static_topo::{read_edges, read_points, read_terminals}; use ds_common::static_topo::{read_static_topo, read_point_terminal, read_terminal_cn_dev};
use eig_domain::DataUnit; use eig_domain::DataUnit;
use mems::model::{get_df_from_in_plugin, get_meas_from_plugin_input, get_wasm_result, ModelType, PluginInput, PluginOutput}; use mems::model::{get_df_from_in_plugin, get_meas_from_plugin_input, get_wasm_result, ModelType, PluginInput, PluginOutput};
...@@ -24,7 +24,7 @@ pub unsafe fn run(ptr: i32, len: u32) -> u64 { ...@@ -24,7 +24,7 @@ pub unsafe fn run(ptr: i32, len: u32) -> u64 {
// source, target, dev // source, target, dev
let mut edges: Vec<Vec<u64>> = vec![]; let mut edges: Vec<Vec<u64>> = vec![];
// switch id to normal open // switch id to normal open
let mut normal_open: HashMap<u64, bool> = HashMap::with_capacity(0); let mut normal_open: HashMap<u64, bool> = HashMap::new();
// terminal, cn, dev // terminal, cn, dev
let mut terminals: Vec<Vec<u64>> = vec![]; let mut terminals: Vec<Vec<u64>> = vec![];
// point, terminal // point, terminal
...@@ -41,17 +41,17 @@ pub unsafe fn run(ptr: i32, len: u32) -> u64 { ...@@ -41,17 +41,17 @@ pub unsafe fn run(ptr: i32, len: u32) -> u64 {
let mut records = rdr.records(); let mut records = rdr.records();
// 开始读取输入的static topology DataFrame // 开始读取输入的static topology DataFrame
if input.dfs[i] == STATIC_TOPO_DF_NAME { if input.dfs[i] == STATIC_TOPO_DF_NAME {
match read_edges(&mut records) { match read_static_topo(&mut records, Some(&mut normal_open), None) {
Ok(v) => (edges, normal_open) = v, Ok(v) => edges = v,
Err(s) => error = Some(s), Err(s) => error = Some(s),
} }
} else if input.dfs[i] == TERMINAL_DF_NAME { } else if input.dfs[i] == TERMINAL_DF_NAME {
match read_terminals(&mut records) { match read_terminal_cn_dev(&mut records) {
Ok(v) => terminals = v, Ok(v) => terminals = v,
Err(s) => error = Some(s), Err(s) => error = Some(s),
} }
} else if input.dfs[i] == POINT_DF_NAME { } else if input.dfs[i] == POINT_DF_NAME {
match read_points(&mut records) { match read_point_terminal(&mut records, None) {
Ok(v) => points = v, Ok(v) => points = v,
Err(s) => error = Some(s), Err(s) => error = Some(s),
} }
......
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use arrow_schema::{DataType, Field, Schema}; use arrow_schema::{DataType, Field, Schema};
use ds_common::{DEV_TOPO_DF_NAME, DYN_TOPO_DF_NAME, POINT_DF_NAME}; use ds_common::{DEV_TOPO_DF_NAME, DYN_TOPO_DF_NAME, POINT_DF_NAME, STATIC_TOPO_DF_NAME, TERMINAL_DF_NAME};
use ds_common::dyn_topo::{read_dev_topo, read_dyn_topo}; use ds_common::dyn_topo::{read_dev_topo, read_dyn_topo};
use ds_common::static_topo::read_points; use ds_common::static_topo::{read_point_terminal, read_static_topo, read_terminal_cn_dev};
use eig_domain::DataUnit; use eig_domain::DataUnit;
use mems::model::{get_meas_from_plugin_input, get_wasm_result, PluginInput, PluginOutput}; use mems::model::{get_meas_from_plugin_input, get_wasm_result, PluginInput, PluginOutput};
use mems::model::dev::PsRsrType; use mems::model::dev::{MeasPhase, PsRsrType};
#[no_mangle] #[no_mangle]
pub unsafe fn run(ptr: i32, len: u32) -> u64 { pub unsafe fn run(ptr: i32, len: u32) -> u64 {
...@@ -22,11 +22,18 @@ pub unsafe fn run(ptr: i32, len: u32) -> u64 { ...@@ -22,11 +22,18 @@ pub unsafe fn run(ptr: i32, len: u32) -> u64 {
if let Err(s) = &r1 { if let Err(s) = &r1 {
error = Some(s.clone()); error = Some(s.clone());
} }
let mut dyn_topo: Vec<Vec<u64>>; // static topo
// terminal, cn, tn, dev
let mut dev_topo: Vec<Vec<u64>> = vec![];
// point, terminal // point, terminal
let mut points: Vec<Vec<u64>> = vec![]; let mut points: Vec<Vec<u64>> = vec![];
let mut meas_phase: Vec<MeasPhase> = vec![];
// terminal, cn, dev
let mut terminals: Vec<Vec<u64>> = vec![];
// dev id to device type
let mut dev_type: HashMap<u64, u16> = HashMap::new();
// dynamic topo
let mut dyn_topo: Vec<Vec<u64>>;
// terminal, cn, tn, dev
let mut dyn_dev_topo: Vec<Vec<u64>> = vec![];
for i in 0..input.dfs_len.len() { for i in 0..input.dfs_len.len() {
let size = input.dfs_len[i] as usize; let size = input.dfs_len[i] as usize;
let end = from + size; let end = from + size;
...@@ -43,14 +50,24 @@ pub unsafe fn run(ptr: i32, len: u32) -> u64 { ...@@ -43,14 +50,24 @@ pub unsafe fn run(ptr: i32, len: u32) -> u64 {
} }
} else if input.dfs[i] == DEV_TOPO_DF_NAME { } else if input.dfs[i] == DEV_TOPO_DF_NAME {
match read_dev_topo(&mut records) { match read_dev_topo(&mut records) {
Ok(v) => dev_topo = v, Ok(v) => dyn_dev_topo = v,
Err(s) => { Err(s) => {
error = Some(s); error = Some(s);
break; break;
} }
} }
} else if input.dfs[i] == STATIC_TOPO_DF_NAME {
match read_static_topo(&mut records, None, Some(&mut dev_type)) {
Ok(_) => {},
Err(s) => error = Some(s),
}
} else if input.dfs[i] == TERMINAL_DF_NAME {
match read_terminal_cn_dev(&mut records) {
Ok(v) => terminals = v,
Err(s) => error = Some(s),
}
} else if input.dfs[i] == POINT_DF_NAME { } else if input.dfs[i] == POINT_DF_NAME {
match read_points(&mut records) { match read_point_terminal(&mut records, Some(&mut meas_phase)) {
Ok(v) => points = v, Ok(v) => points = v,
Err(s) => error = Some(s), Err(s) => error = Some(s),
} }
...@@ -64,33 +81,45 @@ pub unsafe fn run(ptr: i32, len: u32) -> u64 { ...@@ -64,33 +81,45 @@ pub unsafe fn run(ptr: i32, len: u32) -> u64 {
}; };
get_wasm_result(output) get_wasm_result(output)
} else { } else {
let mut csv_str = String::from("cn,tn\n");
let type1 = PsRsrType::SyncGenerator as u16; let type1 = PsRsrType::SyncGenerator as u16;
let type2 = PsRsrType::Load as u16; let type2 = PsRsrType::Load as u16;
let type3 = PsRsrType::ShuntCompensator as u16; let type3 = PsRsrType::ShuntCompensator as u16;
let shunt_types = [type1, type2, type3]; let shunt_types = [type1, type2, type3];
let (meas, units) = r1.unwrap();
let mut point_terminal = HashMap::with_capacity(points.len());
let mut terminal_with_shunt_dev = HashSet::new(); let mut terminal_with_shunt_dev = HashSet::new();
for v in points { for v in terminals {
point_terminal.insert(v[0], v[1]); let terminal = v[0];
let dev_id = v[2];
if let Some(dev_type) = dev_type.get(&dev_id) {
if shunt_types.contains(&dev_type) {
terminal_with_shunt_dev.insert(terminal);
}
}
}
let mut point_terminal = HashMap::with_capacity(points.len());
for i in 0..points.len() {
let point_id = points[i][0];
let terminal = points[i][1];
if terminal_with_shunt_dev.contains(&terminal) {
let phase = meas_phase[i].to_string();
csv_str.push_str(&format!("{point_id},{terminal},{phase}\n"));
point_terminal.insert(point_id, (terminal, meas_phase[i].clone()));
}
} }
for v in dev_topo { let (meas, units) = r1.unwrap();
for v in dyn_dev_topo {
let terminal = v[0]; let terminal = v[0];
let tn = v[2]; let tn = v[2];
let dev = v[3]; let dev = v[3];
let dev_type = v[4] as u16; let dev_type = v[4] as u16;
if shunt_types.contains(&dev_type) {
terminal_with_shunt_dev.insert(terminal);
}
} }
// 开始处理开关量 // 开始处理开关量
for m in meas { for m in meas {
if let Some(terminal) = point_terminal.get(&m.point_id) { if let Some((terminal, phase)) = point_terminal.get(&m.point_id) {
if terminal_with_shunt_dev.contains(terminal) { if terminal_with_shunt_dev.contains(terminal) {
if let Some(unit) = units.get(&m.point_id) { if let Some(unit) = units.get(&m.point_id) {
match unit { match unit {
DataUnit::OnOrOff => {}
DataUnit::A => {} DataUnit::A => {}
DataUnit::V => {} DataUnit::V => {}
DataUnit::kV => {} DataUnit::kV => {}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论