Skip to content

Commit

Permalink
Added add_dna to rust client
Browse files Browse the repository at this point in the history
  • Loading branch information
fayeed committed Nov 24, 2023
1 parent 5cdb6e6 commit 0cbfa01
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
6 changes: 3 additions & 3 deletions cli/src/perspectives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub enum PerspectiveFunctions {
Repl { id: String },

/// Set Social DNA of given perspective with SDNA code from file
SetDna { id: String, file: String },
AddDna { id: String, name: String, file: String, dna_type: String },

/// Get all defined Subject classes
SubjectClasses { id: String },
Expand Down Expand Up @@ -196,11 +196,11 @@ pub async fn run(ad4m_client: Ad4mClient, command: Option<PerspectiveFunctions>)
//let _ = perspectives::run_watch(cap_token, id);
repl_loop(ad4m_client.perspectives.get(id).await?).await?;
}
PerspectiveFunctions::SetDna { id, file } => {
PerspectiveFunctions::AddDna { id, file, name, dna_type} => {
let dna = std::fs::read_to_string(file.clone())
.with_context(|| anyhow!("Could not read provided SDNA file {}", file))?;
let perspective = ad4m_client.perspectives.get(id).await?;
perspective.set_dna(dna).await?;
perspective.add_dna(name, dna, dna_type).await?;
println!("SDNA set successfully");
}
PerspectiveFunctions::SubjectClasses { id } => {
Expand Down
42 changes: 42 additions & 0 deletions rust-client/src/perspective_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,48 @@ impl PerspectiveProxy {
.await
}

pub async fn add_dna(&self, name: String, dna: String, dna_type: String) -> Result<()> {
let mut predicate = "ad4m://has_custom_dna";

if dna_type == "subject_class" {
predicate = "ad4m://has_subject_class"
} else if dna_type == "flow" {
predicate = "ad4m://has_flow"
}

let literal_name = Literal::from_string(name);

let links = self.get(
Some("ad4m://self".into()),
Some(literal_name.to_url().unwrap()),
Some(predicate.into()),
None,
None,
None,
)
.await?;

if links.len() == 0 {
self.set_single_target(
"ad4m://self".into(),
predicate.into(),
literal_name.to_url().unwrap(),
)
.await?;
}

let literal = Literal::from_string(dna);

self.add_link(
literal_name.to_url().unwrap(),
literal.to_url().unwrap(),
Some("ad4m://sdna".into()),
Some("shared".to_string())
)
.await?;
Ok(())
}

pub async fn get_dna(&self) -> Result<Vec<String>> {
self.get(
Some("ad4m://sdna".into()),
Expand Down

0 comments on commit 0cbfa01

Please sign in to comment.