Skip to content
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 7 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions crates/coordinator/dances/src/dance_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ pub enum ResponseStatusCode {
Unauthorized, // 401
Forbidden, // 403 -- use this for authorization / permission errors
NotFound, // 404
Conflict, // 409 -- use this when request denied due to a conflict with the current state of the resource
ServerError, // 500
NotImplemented, // 501
Conflict, // 409 -- use this when request denied due to a conflict with the current state of the resource
ServerError, // 500
NotImplemented, // 501
ServiceUnavailable, // 503
}

Expand Down Expand Up @@ -70,7 +70,8 @@ impl From<HolonError> for ResponseStatusCode {
HolonError::CacheError(_) => ResponseStatusCode::ServerError,
HolonError::NotAccessible(_, _) => ResponseStatusCode::Conflict,
HolonError::ValidationError(_) => ResponseStatusCode::BadRequest,
HolonError::GuardError(_) => ResponseStatusCode::BadRequest,
HolonError::Utf8Conversion(_, _) => ResponseStatusCode::ServerError,
HolonError::HashConversion(_, _) => ResponseStatusCode::ServerError,
HolonError::UnexpectedValueType(_, _) => ResponseStatusCode::ServerError,
}
}
Expand Down
36 changes: 18 additions & 18 deletions crates/coordinator/dances/src/dancer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,25 +178,25 @@ fn process_dispatch_result(dispatch_result: Result<ResponseBody, HolonError>) ->
Err(error) => {
// If the dispatch_result is an error, extract the associated string value
let error_message = match error.clone() {
HolonError::EmptyField(msg)
| HolonError::InvalidParameter(msg)
| HolonError::HolonNotFound(msg)
| HolonError::CommitFailure(msg)
| HolonError::WasmError(msg)
| HolonError::RecordConversion(msg)
| HolonError::InvalidHolonReference(msg)
| HolonError::IndexOutOfRange(msg)
| HolonError::NotImplemented(msg)
| HolonError::MissingStagedCollection(msg)
| HolonError::FailedToBorrow(msg)
| HolonError::UnableToAddHolons(msg)
| HolonError::InvalidRelationship(msg, _)
| HolonError::NotAccessible(msg, _)
| HolonError::GuardError(msg)
| HolonError::UnexpectedValueType(msg, _)
| HolonError::CacheError(msg) => msg,
HolonError::EmptyField(_)
| HolonError::InvalidParameter(_)
| HolonError::HolonNotFound(_)
| HolonError::CommitFailure(_)
| HolonError::WasmError(_)
| HolonError::RecordConversion(_)
| HolonError::InvalidHolonReference(_)
| HolonError::IndexOutOfRange(_)
| HolonError::NotImplemented(_)
| HolonError::MissingStagedCollection(_)
| HolonError::FailedToBorrow(_)
| HolonError::UnableToAddHolons(_)
| HolonError::InvalidRelationship(_, _)
| HolonError::NotAccessible(_, _)
| HolonError::UnexpectedValueType(_, _)
| HolonError::Utf8Conversion(_, _)
| HolonError::HashConversion(_, _)
| HolonError::CacheError(_) => error.to_string(),
HolonError::ValidationError(validation_error) => validation_error.to_string(),

};

// Construct DanceResponse with error details
Expand Down
2 changes: 1 addition & 1 deletion crates/coordinator/dances/src/holon_dance_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ pub fn build_with_properties_dance_request(
/// - Holons -- will be replaced by SmartCollection once supported
///
pub fn get_all_holons_dance(
_context: &HolonsContext,
context: &HolonsContext,
_request: DanceRequest,
) -> Result<ResponseBody, HolonError> {
// TODO: add support for descriptor parameter
Expand Down
23 changes: 10 additions & 13 deletions crates/coordinator/descriptors/src/relationship_descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use holons::context::HolonsContext;
use holons::holon_error::HolonError;

use crate::descriptor_types::{DeletionSemantic};
use holons::holon_collection::HolonCollection;
use holons::holon_reference::HolonReference;
use holons::relationship::RelationshipName;


use holons::staged_reference::StagedReference;
use shared_types_holon::{BaseType, PropertyName};
use shared_types_holon::value_types::{BaseValue, MapBoolean, MapInteger, MapString};

use crate::descriptor_types::DeletionSemantic;

use crate::type_descriptor::define_type_descriptor;

/// This function defines and stages (but does not persist) a new RelationshipDescriptor.
Expand Down Expand Up @@ -37,7 +41,9 @@ pub fn define_relationship_type(
max_target_cardinality: MapInteger,
deletion_semantic: DeletionSemantic,
affinity: MapInteger,
target_holon_type: Option<HolonReference>,
_source_for: HolonCollection, // TODO: switch type to HolonReference
_target_for: HolonCollection, // TODO: switch type to HolonReference
has_supertype: Option<StagedReference>,
_has_inverse: Option<StagedReference>,

) -> Result<StagedReference, HolonError> {
Expand Down Expand Up @@ -80,15 +86,6 @@ pub fn define_relationship_type(
BaseValue::IntegerValue(affinity),
)?;

// Add its relationships
if let Some(descriptor_ref) = target_holon_type {
staged_reference
.add_related_holons(
context,
RelationshipName(MapString("TARGET_HOLON_TYPE".to_string())),
vec![descriptor_ref])?
};

Ok(staged_reference)
Ok(staged_reference)

}
2 changes: 1 addition & 1 deletion crates/coordinator/descriptors/src/semantic_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn set_semantic_version(major: i64, minor: i64, patch: i64) -> Result<Holon,

// TODO: Implement and debug the following function
// pub fn define_semantic_version_descriptor(
// schema: &RelationshipTarget,
// schema: &HolonCollection,
//
// ) -> Holon {
//
Expand Down
82 changes: 44 additions & 38 deletions crates/coordinator/descriptors/src/type_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use holons::holon_error::HolonError;
use holons::holon_reference::HolonReference;
use holons::relationship::RelationshipName;
use holons::staged_reference::StagedReference;
use crate::descriptor_types::TypeDescriptor;
use crate::semantic_version::set_semantic_version;
use shared_types_holon::holon_node::PropertyName;
use shared_types_holon::value_types::{BaseType, BaseValue, MapBoolean, MapEnumValue, MapString};

Expand Down Expand Up @@ -46,10 +48,11 @@ pub fn define_type_descriptor(

// ---------------- GET A NEW (EMPTY) HOLON -------------------------------
let mut descriptor = Holon::new();

// Define a default semantic_version as a String Property
let initial_version = MapString(SemanticVersion::default().to_string());


// ---------------- USE THE INTERNAL HOLONS API TO ADD TYPE_HEADER PROPERTIES -----------------
descriptor
.with_property_value(
Expand Down Expand Up @@ -90,44 +93,47 @@ pub fn define_type_descriptor(
BaseValue::StringValue(initial_version),
)?;

// Stage the new TypeDescriptor

debug!("{:#?}", descriptor.clone());

let staged_reference = context
.commit_manager
.borrow_mut()
.stage_new_holon(descriptor.clone())?;


staged_reference
.add_related_holons(
context,
RelationshipName(MapString("COMPONENT_OF".to_string())),
vec![schema.clone()])?;

if let Some(descriptor_ref) = described_by {
staged_reference
.add_related_holons(
context,
RelationshipName(MapString("DESCRIBED_BY".to_string())),
vec![descriptor_ref])?
};
if let Some(is_subtype_of_ref) = is_subtype_of {
staged_reference
.add_related_holons(
context,
RelationshipName(MapString("IS_SUBTYPE_OF".to_string())),
vec![is_subtype_of_ref])?
};
if let Some(owned_by_ref) = owned_by {
staged_reference
.add_related_holons(
context,
RelationshipName(MapString("OWNED_BY".to_string())),
vec![owned_by_ref])?
};

// Define a default semantic_version
let _version = set_semantic_version(0, 0, 1);

let staged_reference = context.commit_manager.borrow_mut().stage_new_holon(descriptor.clone())?;
// Add the outbound relationships shared by all TypeDescriptors
// let version_target = define_local_target(&version);

// descriptor
// .add_related_holon(
// RelationshipName(MapString("COMPONENT_OF".to_string())),
// schema_target,
// )
// .add_related_holon(
// RelationshipName(MapString("VERSION".to_string())),
// version_target,
// );

// TODO: If has_supertype is supplied, populate that relationship

// if let Some(supertype) = has_supertype {
// let supertype_reference = HolonReference::Local(LocalHolonReference::from_holon(supertype.0.clone()));
// descriptor.add_related_holon(
// RelationshipName(MapString("HAS_SUPERTYPE".to_string())),
// HolonCollection::ZeroOrOne(Some(supertype_reference)),
// );
// }
// // TODO: If described_by is supplied, populate that relationship
// if let Some(is_described_by) = described_by {
// let described_by_reference = HolonReference::Local(LocalHolonReference::from_holon(is_described_by.0.clone()));
//
// descriptor
// .add_related_holon(
// RelationshipName(MapString("DESCRIBED_BY".to_string())),
// HolonCollection::ZeroOrOne(Some(described_by_reference)),
// );
// }
//TODO: Populate owned_by relationship
// descriptor.add_related_holon(
// RelationshipName(MapString("OWNED_BY".to_string())),
// owned_by.clone(),

Ok(staged_reference)

Expand Down
33 changes: 9 additions & 24 deletions crates/coordinator/holons/src/commit_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use std::rc::Rc;
// use crate::cache_manager::HolonCacheManager;
use crate::context::HolonsContext;
use crate::holon::{Holon, HolonState};
use crate::holon_collection::HolonCollection;
use crate::holon_error::HolonError;
use crate::relationship::RelationshipMap;
use crate::relationship::RelationshipTarget;
use crate::smart_reference::SmartReference;
use crate::staged_reference::StagedReference;
use shared_types_holon::{MapInteger, MapString};
Expand Down Expand Up @@ -216,24 +216,16 @@ impl CommitManager {
holon.property_map = existing_holon.get_property_map(context)?;

// Iterate through existing holon's RelationshipMap
// For each RelationshipTarget, create a new StagedCollection in the new holon, from the existing holon's SmartCollection
// For each HolonCollection, create a new StagedCollection in the new holon, from the existing holon's SmartCollection
let existing_relationship_map = existing_holon.get_relationship_map(context)?;
holon.relationship_map = RelationshipMap::new();
for (relationship_name, relationship_target) in existing_relationship_map.0 {
let mut new_relationship_target = RelationshipTarget {
editable: None,
cursors: Vec::new(),
};
// for now populate 0th cursor
new_relationship_target.stage_collection(
staged_reference.clone_reference(),
relationship_target.cursors[0].clone(),
);
for (relationship_name, holon_collection) in existing_relationship_map.0 {
holon_collection.into_staged()?;

holon
.relationship_map
.0
.insert(relationship_name, new_relationship_target);
.insert(relationship_name, holon_collection);
}

Ok(staged_reference)
Expand Down Expand Up @@ -373,24 +365,17 @@ impl CommitManager {
holon.property_map = existing_holon.get_property_map(context)?;

// Iterate through existing holon's RelationshipMap
// For each RelationshipTarget, create a new StagedCollection in the new holon, from the existing holon's SmartCollection
// For each HolonCollection, create a new StagedCollection in the new holon, from the existing holon's SmartCollection
let existing_relationship_map = existing_holon.get_relationship_map(context)?;
holon.relationship_map = RelationshipMap::new();
for (relationship_name, relationship_target) in existing_relationship_map.0 {
let mut new_relationship_target = RelationshipTarget {
editable: None,
cursors: Vec::new(),
};
for (relationship_name, holon_collection) in existing_relationship_map.0 {
// *Note: temp implementation, populate 0th cursor. TODO: set strategy for how to determine which SmartCollection (cursor) to choose
new_relationship_target.stage_collection(
staged_reference.clone_reference(),
relationship_target.cursors[0].clone(),
);
holon_collection.into_staged()?;

holon
.relationship_map
.0
.insert(relationship_name, new_relationship_target);
.insert(relationship_name, holon_collection);
}

Ok(staged_reference)
Expand Down
21 changes: 12 additions & 9 deletions crates/coordinator/holons/src/holon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use hdk::prelude::*;

use shared_types_holon::holon_node::{HolonNode, PropertyMap, PropertyName, PropertyValue};
use shared_types_holon::{BaseType, HolonId, MapString, ValueType};
use shared_types_holon::BaseType::Relationship;

use shared_types_holon::value_types::BaseValue;

Expand All @@ -16,7 +17,7 @@ use crate::helpers::get_holon_node_from_record;
use crate::holon_error::HolonError;
use crate::holon_node::UpdateHolonNodeInput;
use crate::holon_node::*;
use crate::relationship::RelationshipMap;
use crate::relationship::{build_relationship_map_from_smartlinks, RelationshipMap};
use crate::smart_reference::SmartReference;

#[derive(Debug)]
Expand Down Expand Up @@ -88,7 +89,7 @@ pub trait HolonFieldGettable {
property_name: &PropertyName,
) -> Result<PropertyValue, HolonError>;

fn get_key(&mut self, context: &HolonsContext) -> Result<Option<MapString>, HolonError>;
fn get_key(&self, context: &HolonsContext) -> Result<Option<MapString>, HolonError>;

// fn query_relationship(&self, context: HolonsContext, relationship_name: RelationshipName, query_spec: Option<QuerySpec>-> SmartCollection;
}
Expand Down Expand Up @@ -138,11 +139,13 @@ impl Holon {
/// returns a HolonError::UnexpectedValueType.
pub fn get_key(&self) -> Result<Option<MapString>, HolonError> {
self.is_accessible(AccessType::Read)?;
let key = self.property_map.get(&PropertyName(MapString("key".to_string())));
let key = self
.property_map
.get(&PropertyName(MapString("key".to_string())));
if let Some(key) = key {
let string_value: String = key
.try_into()
.map_err(|_| HolonError::UnexpectedValueType(format!("{:?}", key), "MapString".to_string()))?;
let string_value: String = key.try_into().map_err(|_| {
HolonError::UnexpectedValueType(format!("{:?}", key), "MapString".to_string())
})?;
Ok(Some(MapString(string_value)))
} else {
Ok(None)
Expand Down Expand Up @@ -306,7 +309,7 @@ impl Holon {
/// If the staged holon is `Fetched`, `New`, or `Changed` commit does nothing.
///
/// If the staged holon is `Saved`, commit_relationship iterates through the holon's
/// `relationship_map` and calls commit on each member's RelationshipTarget.
/// `relationship_map` and calls commit on each member's HolonCollection.
///
/// If all commits are successful, the function returns a clone a self. Otherwise, the
/// function returns an error.
Expand All @@ -319,9 +322,9 @@ impl Holon {
Some(record) => {
let source_holon_id = record.action_address().clone();
// Iterate through the holon's relationship map, invoking commit on each
for (name, target) in self.relationship_map.0.clone() {
for (name, holon_collection) in self.relationship_map.0.clone() {
debug!("COMMITTING {:#?} relationship", name.clone());
target.commit_relationship(
holon_collection.commit_relationship(
context,
HolonId::from(source_holon_id.clone()),
name.clone(),
Expand Down
Loading
Loading