Skip to content

Commit

Permalink
dcmolecular: Implementation of DC Molecular Field element
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrdz committed Feb 25, 2024
1 parent 68b8e6f commit db5676b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
3 changes: 2 additions & 1 deletion libdonet/src/dcfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub struct DCField {
/// is always implemented as a remote procedure call (RPC). Unlike
/// attribute fields, atomic fields cannot be declared within structs.
///
/// DC Molecular Fields represent a collection of DC Attribute or
/// DC Molecular Fields represent a collection of one or more
/// DC Atomic Fields as one field under one identifier. The parameters
/// of a molecular field are the parameters of all the fields it
/// represents, joined together in the order in which they were declared
Expand Down Expand Up @@ -141,6 +141,7 @@ impl DCFieldInterface for DCField {
}
}

/// Accumulates the properties of this DC element into the file hash.
fn generate_hash(&self, hashgen: &mut DCHashGenerator) {
self.keyword_list.generate_hash(hashgen);
self.field_type.generate_hash(hashgen);
Expand Down
1 change: 1 addition & 0 deletions libdonet/src/dclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl DClassInterface for DClass {
}
}

/// Accumulates the properties of this DC element into the file hash.
fn generate_hash(&mut self, hashgen: &mut DCHashGenerator) {
hashgen.add_string(self.get_name());
hashgen.add_int(self.get_num_parents().try_into().unwrap());
Expand Down
62 changes: 61 additions & 1 deletion libdonet/src/dcmolecular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,69 @@
//! Data model for a DC Molecular field, which represents
//! a form of a field 'alias' for a collection of fields.
use crate::dcfield::DCField;
use crate::dcatomic::{DCAtomicField, DCAtomicFieldInterface};
use crate::dcfield::{DCField, DCFieldInterface};
use crate::dclass::DClass;
use crate::dctype::{DCTypeDefinition, DCTypeDefinitionInterface};
use crate::hashgen::DCHashGenerator;
use std::ops::Deref;
use std::sync::{Arc, Mutex, MutexGuard};

/// An abstract field which provides an interface to access
/// multiple atomic fields under one field and one identifier.
#[derive(Debug)]
pub struct DCMolecularField {
base_field: DCField,
atomic_fields: Vec<Arc<Mutex<DCAtomicField>>>,
}

pub trait DCMolecularFieldInterface {
fn new(name: &str, parent: Arc<Mutex<DClass>>) -> Self;
fn generate_hash(&self, hashgen: &mut DCHashGenerator);

fn add_atomic_field(&mut self, atomic_ptr: Arc<Mutex<DCAtomicField>>);

fn get_num_atomics(&self) -> usize;
fn get_atomic_field(&self, index: usize) -> Option<Arc<Mutex<DCAtomicField>>>;
}

impl DCMolecularFieldInterface for DCMolecularField {
fn new(name: &str, parent: Arc<Mutex<DClass>>) -> Self {
Self {
base_field: {
let mut new_field = DCField::new(name, DCTypeDefinition::new());
new_field.set_parent_dclass(parent);
new_field
},
atomic_fields: vec![],
}
}

/// Accumulates the properties of this DC element into the file hash.
fn generate_hash(&self, hashgen: &mut DCHashGenerator) {
self.base_field.generate_hash(hashgen);

hashgen.add_int(self.atomic_fields.len().try_into().unwrap());

for atomic_ptr in &self.atomic_fields {
let new_ptr: Arc<Mutex<DCAtomicField>> = atomic_ptr.clone();
let mutex_ref: &Mutex<DCAtomicField> = new_ptr.deref();
let atomic_field: MutexGuard<'_, DCAtomicField> = mutex_ref.lock().unwrap();

atomic_field.generate_hash(hashgen);
}
}

fn add_atomic_field(&mut self, atomic_ptr: Arc<Mutex<DCAtomicField>>) {
self.atomic_fields.push(atomic_ptr);
}

#[inline(always)]
fn get_num_atomics(&self) -> usize {
self.atomic_fields.len()
}

fn get_atomic_field(&self, index: usize) -> Option<Arc<Mutex<DCAtomicField>>> {
self.atomic_fields.get(index).cloned()
}
}
12 changes: 7 additions & 5 deletions libdonet/src/dcparameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use std::sync::Arc;
pub struct DCParameter {
parent: Arc<DCAtomicField>,
base_type: DCTypeDefinition,
name: String,
identifier: String,
type_alias: String,
default_value: Vec<u8>,
has_default_value: bool,
Expand All @@ -43,7 +43,7 @@ pub trait DCParameterInterface {
fn get_default_value(&self) -> Vec<u8>;

fn set_type(&mut self, dtype: DCTypeDefinition) -> Result<(), ()>;
fn set_name(&mut self, name: &str) -> Result<(), ()>;
fn set_identifier(&mut self, name: &str) -> Result<(), ()>;
fn set_default_value(&mut self, v: Vec<u8>) -> Result<(), ()>;
}

Expand All @@ -52,7 +52,7 @@ impl DCParameterInterface for DCParameter {
Self {
parent: method,
base_type: dtype,
name: match name {
identifier: match name {
Some(n) => n.to_owned(),
None => String::new(),
},
Expand All @@ -67,6 +67,7 @@ impl DCParameterInterface for DCParameter {
self.base_type.generate_hash(hashgen);
}

#[inline(always)]
fn get_atomic_field(&self) -> Arc<DCAtomicField> {
self.parent.clone() // clone new arc pointer
}
Expand All @@ -76,6 +77,7 @@ impl DCParameterInterface for DCParameter {
self.has_default_value
}

#[inline(always)]
fn get_default_value(&self) -> Vec<u8> {
self.default_value.clone()
}
Expand All @@ -85,8 +87,8 @@ impl DCParameterInterface for DCParameter {
Ok(())
}

fn set_name(&mut self, name: &str) -> Result<(), ()> {
self.name = name.to_owned();
fn set_identifier(&mut self, name: &str) -> Result<(), ()> {
self.identifier = name.to_owned();
Ok(())
}

Expand Down

0 comments on commit db5676b

Please sign in to comment.