Skip to content

Commit

Permalink
Change RDF list representation
Browse files Browse the repository at this point in the history
  • Loading branch information
ckindermann committed Dec 12, 2024
1 parent f109033 commit 643dc36
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 91 deletions.
32 changes: 12 additions & 20 deletions src/ofn_2_ldtab/class_translation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn translate_named_entity(v: &Value) -> Value {

pub fn get_object(v: &Value) -> Value {
let o: Value = translate(&v);
let d: String = String::from(util::translate_datatype(&v).as_str().unwrap());
let d: String = String::from(util::translate_datatype(&o).as_str().unwrap());

json!({"object" : o,
"datatype" : d})
Expand Down Expand Up @@ -336,31 +336,23 @@ pub fn translate_exact_qualified_cardinality(v: &Value) -> Value {
}

pub fn translate_list(v: &[Value]) -> Value {
//TODO: refactor common parts
if v.len() == 1 {
let first_o: Value = get_object(&v[0]);
let rest_o: Value = get_object(&json!("rdf:nil"));

json!({"rdf:first" : vec![first_o],
"rdf:rest" : vec![rest_o]})
} else {
//let first: Value = translate(&v[0]);
let rest: Value = translate_list(&v[1..]); //datatype is necessarily _JSON?

let first_o: Value = get_object(&v[0]);
let rest_o: Value = json!({"object" : rest, "datatype" : String::from("_JSON")});
//let rest_o : Value = get_object(rest);
//
json!({"rdf:first" : vec![first_o],
"rdf:rest" : vec![rest_o]})
let mut list = Vec::new();

for e in v.iter() {
let e_object: Value = get_object(&e);
list.push(e_object);

}

Value::Array(list)
}

pub fn translate_intersection_of(v: &Value) -> Value {
let operands: Value = translate_list(&(v.as_array().unwrap())[1..]);

let operands_o: Value = json!({"object" : operands,
"datatype" : "_JSON"});
"datatype" : "_JSONLIST"});

let type_o: Value = get_object(&json!("owl:Class"));

Expand All @@ -373,7 +365,7 @@ pub fn translate_union_of(v: &Value) -> Value {

//let operands_o : Value = get_object(operands);
let operands_o: Value = json!({"object" : operands,
"datatype" : "_JSON"});
"datatype" : "_JSONLIST"});
let type_o: Value = get_object(&json!("owl:Class"));

json!({"rdf:type" : vec![type_o],
Expand All @@ -385,7 +377,7 @@ pub fn translate_one_of(v: &Value) -> Value {

//let operands_o : Value = get_object(operands);
let operands_o: Value = json!({"object" : operands,
"datatype" : "_JSON"});
"datatype" : "_JSONLIST"});
let type_o: Value = get_object(&json!("owl:Class"));

json!({"rdf:type" : vec![type_o],
Expand Down
54 changes: 20 additions & 34 deletions src/ofn_2_ldtab/property_translation.rs
Original file line number Diff line number Diff line change
@@ -1,62 +1,48 @@
use serde_json::{Value};
use crate::ofn_2_ldtab::util;
use serde_json::json;
use crate::ofn_2_ldtab::util as util;

pub fn translate(v : &Value) -> Value {
use serde_json::Value;

pub fn translate(v: &Value) -> Value {
match v[0].as_str() {
Some("ObjectInverseOf") => translate_inverse_of(v),
Some("ObjectInverseOf") => translate_inverse_of(v),
Some(_) => panic!(),
//None => owl::OWL::Named(String::from(v.as_str().unwrap())),
None => translate_named_entity(&v),
}
}

pub fn translate_named_entity(v: &Value) -> Value {
let o: String = String::from(v.as_str().unwrap());
json!(o)
let o: String = String::from(v.as_str().unwrap());
json!(o)
}

pub fn translate_inverse_of(v : &Value) -> Value {

let argument: Value = translate(&v[1]);
pub fn translate_inverse_of(v: &Value) -> Value {
let argument: Value = translate(&v[1]);

let argument_o : Value = json!({"object" : argument,
let argument_o: Value = json!({"object" : argument,
"datatype" : String::from(util::translate_datatype(&v[1]).as_str().unwrap())});

json!({"owl:inverseOf" : vec![argument_o]})
}
json!({"owl:inverseOf" : vec![argument_o]})
}

pub fn get_object(v : &Value) -> Value {
pub fn get_object(v: &Value) -> Value {
let o: Value = translate(&v);
let d: String = String::from(util::translate_datatype(&v).as_str().unwrap());

json!({"object" : o,
"datatype" : d})
"datatype" : d})
}

pub fn translate_list(v : &[Value]) -> Value {

//TODO: refactor common parts
if v.len() == 1 {
pub fn translate_list(v: &[Value]) -> Value {

let first_o : Value = get_object(&v[0]);
let rest_o : Value = get_object(&json!("rdf:nil"));
let mut list = Vec::new();

for e in v.iter() {
let e_object: Value = get_object(&e);
list.push(e_object);

json!({"rdf:first" : vec![first_o],
"rdf:rest" : vec![rest_o]})
} else {
}

//let first: Value = translate(&v[0]);
let rest: Value = translate_list(&v[1..]);//datatype is necessarily _JSON?
Value::Array(list)

let first_o : Value = get_object(&v[0]);
let rest_o : Value = json!({"object" : rest, "datatype" : String::from("_JSON")});
//let rest_o : Value = get_object(rest);
//
json!({"rdf:first" : vec![first_o],
"rdf:rest" : vec![rest_o]})
}
}

47 changes: 10 additions & 37 deletions src/ofn_2_ldtab/rule_translation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,13 @@ pub fn translate_body(v: &Value) -> Value {
let array = v.as_array().unwrap();
let args = array[1..].to_vec();

let mut map = Map::new();

//TODO: change list representation
for arg in args.iter().rev() {
if map.is_empty() { //first element
let rest_o: Value = get_object(&json!("rdf:nil"));
let type_o: Value = get_object(&json!("swrl:AtomList"));
map.insert(String::from("rdf:rest"),json!(vec![rest_o]));
map.insert(String::from("rdf:type"),json!(vec![type_o]));
map.insert(String::from("rdf:first"),json!(vec![translate(arg)]));
} else {
let inner = map.clone();
map.clear();
let type_o: Value = get_object(&json!("swrl:AtomList"));
map.insert(String::from("rdf:type"),json!(vec![type_o]));
map.insert(String::from("rdf:first"),json!(vec![translate(arg)]));
map.insert(String::from("rdf:rest"),json!(vec![json!(inner)]));
}
let mut list = Vec::new();

for arg in args.iter() {
list.push(translate(arg));
}

json!(vec![map])
json!(list)
}

//same as body
Expand All @@ -92,24 +78,11 @@ pub fn translate_head(v: &Value) -> Value {
let array = v.as_array().unwrap();
let args = array[1..].to_vec();

let mut map = Map::new();

for arg in args.iter().rev() {
if map.is_empty() { //first element
let rest_o: Value = get_object(&json!("rdf:nil"));
let type_o: Value = get_object(&json!("swrl:AtomList"));
map.insert(String::from("rdf:rest"),json!(vec![rest_o]));
map.insert(String::from("rdf:type"),json!(vec![type_o]));
map.insert(String::from("rdf:first"),json!(vec![translate(arg)]));
} else {
let inner = map.clone();
map.clear();
let type_o: Value = get_object(&json!("swrl:AtomList"));
map.insert(String::from("rdf:type"),json!(vec![type_o]));
map.insert(String::from("rdf:first"),json!(vec![translate(arg)]));
map.insert(String::from("rdf:rest"),json!(vec![json!(inner)]));
}
let mut list = Vec::new();

for arg in args.iter() {
list.push(translate(arg));
}

json!(vec![map])
json!(list)
}

0 comments on commit 643dc36

Please sign in to comment.