Skip to content

Commit

Permalink
feat: implement more operations
Browse files Browse the repository at this point in the history
  • Loading branch information
JabobKrauskopf committed Sep 19, 2024
1 parent eb9db88 commit 41413df
Show file tree
Hide file tree
Showing 9 changed files with 533 additions and 165 deletions.
44 changes: 34 additions & 10 deletions crates/medmodels-core/src/medrecord/datatypes/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::{
Trim, TrimEnd, TrimStart, Uppercase,
};
use crate::errors::MedRecordError;
use chrono::NaiveDateTime;
use chrono::{DateTime, NaiveDateTime};
use medmodels_utils::implement_from_for_wrapper;
use serde::{Deserialize, Serialize};
use std::{
Expand Down Expand Up @@ -210,9 +210,17 @@ impl Add for MedRecordValue {
(MedRecordValue::DateTime(value), MedRecordValue::Bool(rhs)) => Err(
MedRecordError::AssertionError(format!("Cannot add {} to {}", rhs, value)),
),
(MedRecordValue::DateTime(value), MedRecordValue::DateTime(rhs)) => Err(
MedRecordError::AssertionError(format!("Cannot add {} to {}", rhs, value)),
),
(MedRecordValue::DateTime(value), MedRecordValue::DateTime(rhs)) => {
Ok(DateTime::from_timestamp(
value.and_utc().timestamp() + rhs.and_utc().timestamp(),
0,
)
.ok_or(MedRecordError::AssertionError(
"Invalid timestamp".to_string(),
))?
.naive_utc()
.into())
}
(MedRecordValue::DateTime(value), MedRecordValue::Null) => Err(
MedRecordError::AssertionError(format!("Cannot add None to {}", value)),
),
Expand Down Expand Up @@ -327,9 +335,17 @@ impl Sub for MedRecordValue {
(MedRecordValue::DateTime(value), MedRecordValue::Bool(rhs)) => Err(
MedRecordError::AssertionError(format!("Cannot subtract {} from {}", rhs, value)),
),
(MedRecordValue::DateTime(value), MedRecordValue::DateTime(rhs)) => Err(
MedRecordError::AssertionError(format!("Cannot subtract {} from {}", rhs, value)),
),
(MedRecordValue::DateTime(value), MedRecordValue::DateTime(rhs)) => {
Ok(DateTime::from_timestamp(
value.and_utc().timestamp() - rhs.and_utc().timestamp(),
0,
)
.ok_or(MedRecordError::AssertionError(
"Invalid timestamp".to_string(),
))?
.naive_utc()
.into())
}
(MedRecordValue::DateTime(value), MedRecordValue::Null) => Err(
MedRecordError::AssertionError(format!("Cannot subtract None from {}", value)),
),
Expand Down Expand Up @@ -621,9 +637,17 @@ impl Div for MedRecordValue {
(MedRecordValue::DateTime(value), MedRecordValue::String(other)) => Err(
MedRecordError::AssertionError(format!("Cannot divide {} by {}", value, other)),
),
(MedRecordValue::DateTime(value), MedRecordValue::Int(other)) => Err(
MedRecordError::AssertionError(format!("Cannot divide {} by {}", value, other)),
),
(MedRecordValue::DateTime(value), MedRecordValue::Int(other)) => {
Ok(DateTime::from_timestamp(
(value.and_utc().timestamp() as f64 / other as f64).floor() as i64,
0,
)
.ok_or(MedRecordError::AssertionError(
"Invalid timestamp".to_string(),
))?
.naive_utc()
.into())
}
(MedRecordValue::DateTime(value), MedRecordValue::Float(other)) => Err(
MedRecordError::AssertionError(format!("Cannot divide {} by {}", value, other)),
),
Expand Down
6 changes: 4 additions & 2 deletions crates/medmodels-core/src/medrecord/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1924,7 +1924,7 @@ mod test {
(
"0".into(),
"1".into(),
HashMap::from([("time".into(), 4.into())]),
HashMap::from([("time".into(), "Hallo".into())]),
),
(
"0".into(),
Expand All @@ -1943,7 +1943,9 @@ mod test {
.unwrap();

let nodes = medrecord.select_edges(|edge| {
edge.attribute("time").less_than(2);
let attribute = edge.attribute("time");
attribute.is_int();
attribute.max().less_than(6);
});

println!("\n{:?}", nodes.collect::<Vec<_>>());
Expand Down
8 changes: 4 additions & 4 deletions crates/medmodels-core/src/medrecord/querying/edges/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
querying::{
nodes::NodeOperand,
traits::{DeepClone, ReadWriteOrPanic},
values::{Context, MedRecordValuesOperand},
values::{Context, MultipleValuesOperand},
wrapper::Wrapper,
},
CardinalityWrapper, EdgeIndex, Group, MedRecordAttribute,
Expand Down Expand Up @@ -52,8 +52,8 @@ impl EdgeOperand {
})
}

pub fn attribute(&mut self, attribute: MedRecordAttribute) -> Wrapper<MedRecordValuesOperand> {
let operand = Wrapper::<MedRecordValuesOperand>::new(
pub fn attribute(&mut self, attribute: MedRecordAttribute) -> Wrapper<MultipleValuesOperand> {
let operand = Wrapper::<MultipleValuesOperand>::new(
Context::EdgeOperand(self.deep_clone()),
attribute,
);
Expand Down Expand Up @@ -116,7 +116,7 @@ impl Wrapper<EdgeOperand> {
self.0.read_or_panic().evaluate(medrecord)
}

pub fn attribute<A>(&self, attribute: A) -> Wrapper<MedRecordValuesOperand>
pub fn attribute<A>(&self, attribute: A) -> Wrapper<MultipleValuesOperand>
where
A: Into<MedRecordAttribute>,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
querying::{
nodes::NodeOperand,
traits::{DeepClone, ReadWriteOrPanic},
values::MedRecordValuesOperand,
values::MultipleValuesOperand,
wrapper::Wrapper,
},
CardinalityWrapper, EdgeIndex, Group, MedRecordAttribute, MedRecordValue,
Expand All @@ -16,7 +16,7 @@ use std::collections::HashSet;
#[derive(Debug, Clone)]
pub enum EdgeOperation {
Attribute {
operand: Wrapper<MedRecordValuesOperand>,
operand: Wrapper<MultipleValuesOperand>,
},

InGroup {
Expand Down Expand Up @@ -112,7 +112,7 @@ impl EdgeOperation {
fn evaluate_attribute<'a>(
medrecord: &'a MedRecord,
edge_indices: impl Iterator<Item = &'a EdgeIndex> + 'a,
operand: Wrapper<MedRecordValuesOperand>,
operand: Wrapper<MultipleValuesOperand>,
) -> MedRecordResult<impl Iterator<Item = &'a EdgeIndex>> {
let values = Self::get_values(
medrecord,
Expand Down
8 changes: 4 additions & 4 deletions crates/medmodels-core/src/medrecord/querying/nodes/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
querying::{
edges::EdgeOperand,
traits::{DeepClone, ReadWriteOrPanic},
values::{Context, MedRecordValuesOperand},
values::{Context, MultipleValuesOperand},
wrapper::{CardinalityWrapper, Wrapper},
},
Group, MedRecordAttribute, NodeIndex,
Expand Down Expand Up @@ -52,8 +52,8 @@ impl NodeOperand {
})
}

pub fn attribute(&mut self, attribute: MedRecordAttribute) -> Wrapper<MedRecordValuesOperand> {
let operand = Wrapper::<MedRecordValuesOperand>::new(
pub fn attribute(&mut self, attribute: MedRecordAttribute) -> Wrapper<MultipleValuesOperand> {
let operand = Wrapper::<MultipleValuesOperand>::new(
Context::NodeOperand(self.deep_clone()),
attribute,
);
Expand Down Expand Up @@ -116,7 +116,7 @@ impl Wrapper<NodeOperand> {
self.0.read_or_panic().evaluate(medrecord)
}

pub fn attribute(&mut self, attribute: MedRecordAttribute) -> Wrapper<MedRecordValuesOperand> {
pub fn attribute(&mut self, attribute: MedRecordAttribute) -> Wrapper<MultipleValuesOperand> {
self.0.write_or_panic().attribute(attribute)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
querying::{
edges::EdgeOperand,
traits::{DeepClone, ReadWriteOrPanic},
values::MedRecordValuesOperand,
values::MultipleValuesOperand,
wrapper::{CardinalityWrapper, Wrapper},
},
Group, MedRecord, MedRecordAttribute, MedRecordValue, NodeIndex,
Expand All @@ -15,7 +15,7 @@ use roaring::RoaringBitmap;
#[derive(Debug, Clone)]
pub enum NodeOperation {
Attribute {
operand: Wrapper<MedRecordValuesOperand>,
operand: Wrapper<MultipleValuesOperand>,
},

InGroup {
Expand Down Expand Up @@ -111,7 +111,7 @@ impl NodeOperation {
fn evaluate_attribute<'a>(
medrecord: &'a MedRecord,
node_indices: impl Iterator<Item = &'a NodeIndex> + 'a,
operand: Wrapper<MedRecordValuesOperand>,
operand: Wrapper<MultipleValuesOperand>,
) -> MedRecordResult<impl Iterator<Item = &'a NodeIndex>> {
let values = Self::get_values(
medrecord,
Expand Down
2 changes: 1 addition & 1 deletion crates/medmodels-core/src/medrecord/querying/values/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod operand;
mod operation;

pub use operand::{Context, MedRecordValuesOperand};
pub use operand::{Context, MultipleValuesOperand};
Loading

0 comments on commit 41413df

Please sign in to comment.