-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
relationship_descriptor -> name, RelationshipTarget -> HolonCollection #108
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
77c94b3
relationship_descriptor -> name, RelationshipTarget -> HolonCollection
dauphin3 bc2bda9
fix 110 all tests passing
dauphin3 fc2fd3c
Merge pull request #113 from evomimic/110-unify-smartcollection-and-s…
dauphin3 cb11420
build_relationship_map_from_smartlinks
dauphin3 79776a0
minor refactoring and cleanup, test and sweetest pass
evomimic 455b4db
Merge branch 'main' into 105-implement-get_relationships_from_links
evomimic e115136
resolved some merge conflict resolution problems
evomimic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
evomimic marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
use crate::context::HolonsContext; | ||
use crate::holon::{AccessType, HolonFieldGettable}; | ||
use crate::holon_error::HolonError; | ||
use crate::holon_reference::HolonReference; | ||
use crate::relationship::RelationshipName; | ||
use crate::smart_link_manager::{create_smart_link, SmartLinkInput}; | ||
use hdk::prelude::*; | ||
use shared_types_holon::{HolonId, MapString}; | ||
use std::collections::BTreeMap; | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)] | ||
pub enum CollectionState { | ||
Fetched, // links have been fetched from the persistent store for this collection | ||
Staged, // the links for this collection have not been persisted | ||
Saved, // a staged collection for which SmartLinks have been successfully committed | ||
Abandoned, // a previously staged collection that was abandoned prior to being committed | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)] | ||
pub struct HolonCollection { | ||
state: CollectionState, | ||
members: Vec<HolonReference>, | ||
keyed_index: BTreeMap<MapString, usize>, // usize is an index into the members vector | ||
} | ||
|
||
impl HolonCollection { | ||
pub fn new_staged() -> Self { | ||
HolonCollection { | ||
state: CollectionState::Staged, | ||
members: Vec::new(), | ||
keyed_index: BTreeMap::new(), | ||
} | ||
} | ||
pub fn new_existing() -> Self { | ||
HolonCollection { | ||
state: CollectionState::Fetched, | ||
members: Vec::new(), | ||
keyed_index: BTreeMap::new(), | ||
} | ||
} | ||
pub fn is_accessible(&self, access_type: AccessType) -> Result<(), HolonError> { | ||
match access_type { | ||
AccessType::Read => { | ||
if self.state == CollectionState::Abandoned { | ||
Err(HolonError::NotAccessible( | ||
"Read".to_string(), | ||
format!("{:?}", self.state), | ||
)) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
AccessType::Write => match self.state { | ||
CollectionState::Staged => Ok(()), | ||
_ => Err(HolonError::NotAccessible( | ||
"Write".to_string(), | ||
format!("{:?}", self.state), | ||
)), | ||
}, | ||
} | ||
} | ||
|
||
pub fn into_staged(&self) -> Result<HolonCollection, HolonError> { | ||
self.is_accessible(AccessType::Read)?; | ||
if self.state == CollectionState::Fetched { | ||
Ok(HolonCollection { | ||
state: CollectionState::Staged, | ||
members: self.members.clone(), | ||
keyed_index: self.keyed_index.clone(), | ||
}) | ||
} else { | ||
Err(HolonError::InvalidParameter("CollectionState".to_string())) | ||
} | ||
} | ||
|
||
pub fn get_by_key(&self, key: &MapString) -> Result<Option<HolonReference>, HolonError> { | ||
self.is_accessible(AccessType::Read)?; | ||
let index = self.keyed_index.get(key); | ||
if let Some(index) = index { | ||
Ok(Some(self.members[*index].clone())) | ||
} else { | ||
Ok(None) | ||
} | ||
} | ||
|
||
pub fn add_references( | ||
&mut self, | ||
context: &HolonsContext, | ||
holons: Vec<HolonReference>, | ||
) -> Result<(), HolonError> { | ||
self.is_accessible(AccessType::Write)?; | ||
|
||
for holon in holons { | ||
let index = self.members.len(); | ||
self.members.push(holon.clone()); | ||
let key = holon.get_key(context)?; | ||
if let Some(key) = key { | ||
self.keyed_index.insert(key, index); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
/// This method creates smartlinks from the specified source_id for the specified relationship name | ||
/// to each holon its collection that has a holon_id. | ||
pub fn add_smartlinks_for_collection( | ||
&self, | ||
context: &HolonsContext, | ||
source_id: HolonId, | ||
name: RelationshipName, | ||
) -> Result<(), HolonError> { | ||
debug!( | ||
"Calling commit on each HOLON_REFERENCE in the collection for {:#?}.", | ||
name.0.clone() | ||
); | ||
for holon_reference in self.members.clone() { | ||
// Only commit references to holons with id's (i.e., Saved) | ||
if let Ok(target_id) = holon_reference.get_holon_id(context) { | ||
let input = SmartLinkInput { | ||
from_address: source_id.clone(), | ||
to_address: target_id, | ||
relationship_name: name.clone(), | ||
}; | ||
create_smart_link(input)?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// The method | ||
pub fn commit_relationship( | ||
&self, | ||
context: &HolonsContext, | ||
source_id: HolonId, | ||
name: RelationshipName, | ||
) -> Result<(), HolonError> { | ||
self.add_smartlinks_for_collection(context, source_id.clone(), name.clone())?; | ||
|
||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The is_accessible function logic looks correct, but it is a bit of work to translate between the requirements and the code. The "READ" check explicitly checks the collection_state that errors, else returns OK on anything else, whereas the "WRITE" checks for the collection_state that is OK and errors on anything else.
It may be a bit more maintainable and easier to update if requirements change by having both arms use similar logic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-implemented in minor refactoring and cleanup, test and sweetest pass