diff --git a/.github/workflows/Build.yml b/.github/workflows/Build.yml index 59d6fce..179b614 100644 --- a/.github/workflows/Build.yml +++ b/.github/workflows/Build.yml @@ -12,42 +12,40 @@ jobs: build-binaries: strategy: matrix: - target: [ - { runner: "macos-11", arch: "x86_64-apple-darwin" }, - { runner: "macos-11", arch: "aarch64-apple-darwin" }, - { runner: "windows-2022", arch: "x86_64-pc-windows-msvc" }, - { runner: "ubuntu-20.04", arch: "x86_64-unknown-linux-gnu" }, - { runner: "ubuntu-20.04", arch: "x86_64-unknown-linux-musl" }, - ] + target: + - { runner: "macos-latest", arch: "x86_64-apple-darwin" } + - { runner: "macos-latest", arch: "aarch64-apple-darwin" } + - { runner: "windows-2022", arch: "x86_64-pc-windows-msvc" } + - { runner: "ubuntu-latest", arch: "x86_64-unknown-linux-gnu" } + - { runner: "ubuntu-latest", arch: "x86_64-unknown-linux-musl" } + runs-on: ${{ matrix.target.runner }} + steps: - name: Check out repository code - uses: actions/checkout@v3 + uses: actions/checkout@v6 - name: Install musl-tools (MUSL) - if: ${{ matrix.target.runner == 'ubuntu-20.04' && matrix.target.arch == 'x86_64-unknown-linux-musl' }} + if: ${{ startsWith(matrix.target.runner, 'ubuntu') && matrix.target.arch == 'x86_64-unknown-linux-musl' }} run: | sudo apt-get update - sudo apt-get install musl-tools + sudo apt-get install -y musl-tools - - name: Install latest rust toolchain - uses: actions-rs/toolchain@v1 + - name: Install latest Rust toolchain + uses: dtolnay/rust-toolchain@stable with: - toolchain: stable - target: ${{ matrix.target.arch }} - default: true - override: true + targets: ${{ matrix.target.arch }} - name: Build binary using cross (Linux) - if: ${{ matrix.target.runner == 'ubuntu-20.04' }} + if: ${{ startsWith(matrix.target.runner, 'ubuntu') }} run: | cargo install cross --git https://github.com/cross-rs/cross - sudo systemctl start docker + sudo systemctl start docker || true cross build --release --target ${{ matrix.target.arch }} cp target/${{ matrix.target.arch }}/release/wiring_rs wiring-${{ matrix.target.arch }} - - name: Build binary using cargo (MacOS) - if: ${{ matrix.target.runner == 'macos-11' }} + - name: Build binary using cargo (macOS) + if: ${{ startsWith(matrix.target.runner, 'macos') }} run: | cargo build --release --target ${{ matrix.target.arch }} cp target/${{ matrix.target.arch }}/release/wiring_rs wiring-${{ matrix.target.arch }} @@ -57,41 +55,10 @@ jobs: run: | cargo build --release --target ${{ matrix.target.arch }} cp target\${{ matrix.target.arch }}\release\wiring_rs.exe wiring-${{ matrix.target.arch }} - #cp target\${{ matrix.target.arch }}\release\wiring.exe wiring-${{ matrix.target.arch }}.exe - name: Upload binary artifacts - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: ${{ matrix.target.arch }}-bin path: wiring-${{ matrix.target.arch }} - #path: man-parse-rust/target/${{ matrix.platform.target }}/release/man-parse-rust if-no-files-found: error - - # - name: Upload binary to release - # uses: svenstaro/upload-release-action@v2 - # with: - # file: ontodev_valve-${{ matrix.target.arch }}* - # file_glob: true - # tag: ${{ github.ref }} - # overwrite: true - # - # cargo-publish: - # needs: build-binaries - # runs-on: ubuntu-20.04 - # steps: - # - name: Check out repository code - # uses: actions/checkout@v3 - # - # - name: Install latest rust toolchain - # uses: actions-rs/toolchain@v1 - # with: - # toolchain: stable - # target: ${{ matrix.target.arch }} - # default: true - # override: true - # - # - name: Publish to crates.io - # env: - # CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} - # run: | - # cargo publish diff --git a/src/constants.rs b/src/constants.rs new file mode 100644 index 0000000..f1bd6df --- /dev/null +++ b/src/constants.rs @@ -0,0 +1,117 @@ +// -- RDF ---------------------------------------------------------------------- +pub const RDF_FIRST: &str = ""; +pub const RDF_LIST: &str = ""; +pub const RDF_NIL: &str = ""; +pub const RDF_REST: &str = ""; +pub const RDF_TYPE: &str = ""; + +// -- LDTab datatype markers --------------------------------------------------- +pub const LDTAB_IRI: &str = "_IRI"; +pub const LDTAB_JSON_LIST: &str = "_JSONLIST"; +pub const LDTAB_JSON_MAP: &str = "_JSONMAP"; + +// -- RDFS --------------------------------------------------------------------- +pub const RDFS_DATATYPE: &str = ""; +pub const RDFS_DOMAIN: &str = ""; +pub const RDFS_LABEL: &str = ""; +pub const RDFS_RANGE: &str = ""; +pub const RDFS_SUB_CLASS_OF: &str = ""; +pub const RDFS_SUB_PROPERTY_OF: &str = ""; + +// -- XSD ---------------------------------------------------------------------- +pub const XSD_BOOLEAN: &str = ""; +pub const XSD_BYTE: &str = ""; +pub const XSD_DECIMAL: &str = ""; +pub const XSD_INT: &str = ""; +pub const XSD_INTEGER: &str = ""; +pub const XSD_LONG: &str = ""; +pub const XSD_NEGATIVE_INTEGER: &str = ""; +pub const XSD_NON_NEGATIVE_INTEGER: &str = ""; +pub const XSD_NON_POSITIVE_INTEGER: &str = ""; +pub const XSD_POSITIVE_INTEGER: &str = ""; +pub const XSD_SHORT: &str = ""; +pub const XSD_STRING: &str = ""; +pub const XSD_UNSIGNED_BYTE: &str = ""; +pub const XSD_UNSIGNED_INT: &str = ""; +pub const XSD_UNSIGNED_LONG: &str = ""; +pub const XSD_UNSIGNED_SHORT: &str = ""; + +// -- OWL classes / types ------------------------------------------------------ +pub const OWL_AXIOM: &str = ""; +pub const OWL_ALL_DIFFERENT: &str = ""; +pub const OWL_ALL_DISJOINT_CLASSES: &str = ""; +pub const OWL_ALL_DISJOINT_PROPERTIES: &str = ""; +pub const OWL_ALL_SAME_AS: &str = ""; +pub const OWL_ANNOTATION_PROPERTY: &str = ""; +pub const OWL_ASYMMETRIC_PROPERTY: &str = ""; +pub const OWL_CLASS: &str = ""; +pub const OWL_DATATYPE_PROPERTY: &str = ""; +pub const OWL_FUNCTIONAL_PROPERTY: &str = ""; +pub const OWL_INVERSE_FUNCTIONAL_PROPERTY: &str = ""; +pub const OWL_IRREFLEXIVE_PROPERTY: &str = ""; +pub const OWL_NAMED_INDIVIDUAL: &str = ""; +pub const OWL_NEGATIVE_PROPERTY_ASSERTION: &str = ""; +pub const OWL_OBJECT_PROPERTY: &str = ""; +pub const OWL_ONTOLOGY: &str = ""; +pub const OWL_REAL: &str = ""; +pub const OWL_RATIONAL: &str = ""; +pub const OWL_REFLECTIVE_PROPERTY: &str = ""; +pub const OWL_RESTRICTION: &str = ""; +pub const OWL_SYMMETRIC_PROPERTY: &str = ""; +pub const OWL_TRANSITIVE_PROPERTY: &str = ""; + +// -- OWL properties ----------------------------------------------------------- +pub const OWL_ALL_VALUES_FROM: &str = ""; +pub const OWL_ASSERTION_PROPERTY: &str = ""; +pub const OWL_CARDINALITY: &str = ""; +pub const OWL_COMPLEMENT_OF: &str = ""; +pub const OWL_DIFFERENT_FROM: &str = ""; +pub const OWL_DISJOINT_UNION_OF: &str = ""; +pub const OWL_DISJOINT_WITH: &str = ""; +pub const OWL_DISTINCT_MEMBERS: &str = ""; +pub const OWL_EQUIVALENT_CLASS: &str = ""; +pub const OWL_EQUIVALENT_PROPERTY: &str = ""; +pub const OWL_HAS_KEY: &str = ""; +pub const OWL_HAS_SELF: &str = ""; +pub const OWL_HAS_VALUE: &str = ""; +pub const OWL_IMPORTS: &str = ""; +pub const OWL_INTERSECTION_OF: &str = ""; +pub const OWL_INVERSE_OF: &str = ""; +pub const OWL_MAX_CARDINALITY: &str = ""; +pub const OWL_MAX_QUALIFIED_CARDINALITY: &str = ""; +pub const OWL_MEMBERS: &str = ""; +pub const OWL_MIN_CARDINALITY: &str = ""; +pub const OWL_MIN_QUALIFIED_CARDINALITY: &str = ""; +pub const OWL_ON_CLASS: &str = ""; +pub const OWL_ON_DATA_RANGE: &str = ""; +pub const OWL_ON_PROPERTY: &str = ""; +pub const OWL_ONE_OF: &str = ""; +pub const OWL_PROPERTY_CHAIN_AXIOM: &str = ""; +pub const OWL_PROPERTY_DISJOINT_WITH: &str = ""; +pub const OWL_QUALIFIED_CARDINALITY: &str = ""; +pub const OWL_SAME_AS: &str = ""; +pub const OWL_SOME_VALUES_FROM: &str = ""; +pub const OWL_SOURCE_INDIVIDUAL: &str = ""; +pub const OWL_TARGET_INDIVIDUAL: &str = ""; +pub const OWL_TARGET_VALUE: &str = ""; +pub const OWL_UNION_OF: &str = ""; +pub const OWL_VERSION_IRI: &str = ""; + +// -- SWRL --------------------------------------------------------------------- +pub const SWRL_ARGUMENT1: &str = ""; +pub const SWRL_ARGUMENT2: &str = ""; +pub const SWRL_ARGUMENTS: &str = ""; +pub const SWRL_BODY: &str = ""; +pub const SWRL_BUILTIN: &str = ""; +pub const SWRL_BUILTIN_ATOM: &str = ""; +pub const SWRL_CLASS_ATOM: &str = ""; +pub const SWRL_CLASS_PREDICATE: &str = ""; +pub const SWRL_DATA_RANGE: &str = ""; +pub const SWRL_DATA_RANGE_ATOM: &str = ""; +pub const SWRL_DATAVALUED_PROPERTY_ATOM: &str = ""; +pub const SWRL_DIFFERENT_INDIVIDUALS_ATOM: &str = ""; +pub const SWRL_HEAD: &str = ""; +pub const SWRL_IMP: &str = ""; +pub const SWRL_INDIVIDUAL_PROPERTY_ATOM: &str = ""; +pub const SWRL_PROPERTY_PREDICATE: &str = ""; +pub const SWRL_SAME_INDIVIDUAL_ATOM: &str = ""; diff --git a/src/ldtab_2_ofn/axiom_translation.rs b/src/ldtab_2_ofn/axiom_translation.rs index afd53de..274a2b4 100644 --- a/src/ldtab_2_ofn/axiom_translation.rs +++ b/src/ldtab_2_ofn/axiom_translation.rs @@ -1,3 +1,4 @@ +use crate::constants::*; use crate::ldtab_2_ofn::class_translation; use crate::owl::thick_triple as owl; use serde_json::Value; @@ -353,7 +354,7 @@ pub fn translate_disjoint_with(subject: &owl::OWL, object: &owl::OWL) -> Value { /// /// let axiom : Value = translation::translate_disjoint_union(&union_owl, /// &operands_owl); -/// let axiom_expected_string = r#"["DisjointUnionOf","obo:IAO_0000120","obo:IAO_0000121","obo:IAO_0000122"]"#; +/// let axiom_expected_string = r#"["DisjointUnion","obo:IAO_0000120","obo:IAO_0000121","obo:IAO_0000122"]"#; /// let axiom_expected : Value = serde_json::from_str(axiom_expected_string).unwrap(); /// /// assert_eq!(axiom, axiom_expected); @@ -362,7 +363,7 @@ pub fn translate_disjoint_union(union: &owl::OWL, operands: &owl::OWL) -> Value let lhs = class_translation::translate(union); let rhs = class_translation::translate(operands); - let operator = Value::String("DisjointUnionOf".into()); + let operator = Value::String("DisjointUnion".into()); match rhs { Value::Array(mut arr) => { @@ -391,27 +392,27 @@ pub fn translate_disjoint_union(union: &owl::OWL, operands: &owl::OWL) -> Value ///``` pub fn get_ofn_operator(op: &str) -> Value { match op { - "" => Value::String(String::from("Datatype")), - "" => Value::String(String::from("Class")), - "owl:ObjectProperty" => Value::String(String::from("ObjectProperty")), - "owl:DatatypeProperty" => Value::String(String::from("DataProperty")), - "owl:AnnotationProperty" => Value::String(String::from("AnnotationProperty")), - "owl:NamedIndividual" => Value::String(String::from("NamedIndividual")), + RDFS_DATATYPE => Value::String(String::from("Datatype")), + OWL_CLASS => Value::String(String::from("Class")), + OWL_OBJECT_PROPERTY => Value::String(String::from("ObjectProperty")), + OWL_DATATYPE_PROPERTY => Value::String(String::from("DataProperty")), + OWL_ANNOTATION_PROPERTY => Value::String(String::from("AnnotationProperty")), + OWL_NAMED_INDIVIDUAL => Value::String(String::from("NamedIndividual")), - "owl:FunctionalProperty" => Value::String(String::from("FunctionalProperty")), + OWL_FUNCTIONAL_PROPERTY => Value::String(String::from("FunctionalProperty")), - "owl:InverseFunctionalProperty" => { + OWL_INVERSE_FUNCTIONAL_PROPERTY => { Value::String(String::from("InverseObjectFunctionalProperty")) } - "owl:ReflexiveProperty" => Value::String(String::from("ReflexiveObjectProperty")), - "owl:IrreflexiveProperty" => Value::String(String::from("IrreflexiveObjectProperty")), - "owl:SymmetricProperty" => Value::String(String::from("SymmetricObjectProperty")), - "owl:AsymmetricProperty" => Value::String(String::from("AsymmetricObjectProperty")), - "owl:TransitiveProperty" => Value::String(String::from("TransitiveObjectProperty")), + OWL_REFLECTIVE_PROPERTY => Value::String(String::from("ReflexiveObjectProperty")), + OWL_IRREFLEXIVE_PROPERTY => Value::String(String::from("IrreflexiveObjectProperty")), + OWL_SYMMETRIC_PROPERTY => Value::String(String::from("SymmetricObjectProperty")), + OWL_ASYMMETRIC_PROPERTY => Value::String(String::from("AsymmetricObjectProperty")), + OWL_TRANSITIVE_PROPERTY => Value::String(String::from("TransitiveObjectProperty")), - "" => Value::String(String::from("DifferentIndividuals")), + OWL_ALL_DIFFERENT => Value::String(String::from("DifferentIndividuals")), - "owl:Ontology" => Value::String(String::from("ThinTriple")), + OWL_ONTOLOGY => Value::String(String::from("ThinTriple")), _ => Value::String(String::from("ClassAssertion")), } @@ -428,7 +429,7 @@ pub fn get_ofn_operator(op: &str) -> Value { /// use wiring_rs::owl::thick_triple as owl; /// /// let lhs = "\"obo:IAO_0000120\""; -/// let rhs = "\"owl:FunctionalProperty\""; +/// let rhs = "\"\""; /// /// let lhs_owl : owl::OWL = serde_json::from_str(lhs).unwrap(); /// let rhs_owl : owl::OWL = serde_json::from_str(rhs).unwrap(); diff --git a/src/ldtab_2_ofn/class_translation.rs b/src/ldtab_2_ofn/class_translation.rs index 0fec20c..d625f90 100644 --- a/src/ldtab_2_ofn/class_translation.rs +++ b/src/ldtab_2_ofn/class_translation.rs @@ -1,3 +1,4 @@ +use crate::constants::*; use crate::ldtab_2_ofn::property_translation; use crate::owl::thick_triple as owl; use serde_json::Value; @@ -221,7 +222,7 @@ pub fn translate_has_self(exp: &owl::HasSelf) -> Value { /// let min_cardinality_owl : owl::OWL = serde_json::from_str(min_cardinality).unwrap(); /// /// let axiom : Value = translation::translate(&min_cardinality_owl); -/// let axiom_expected_string = r#"["MinCardinality","\"1\"^^xsd:int","obo:IAO_0000120"]"#; +/// let axiom_expected_string = r#"["MinCardinality","\"1\"^^","obo:IAO_0000120"]"#; /// let axiom_expected : Value = serde_json::from_str(axiom_expected_string).unwrap(); /// /// assert_eq!(axiom, axiom_expected); @@ -252,7 +253,7 @@ pub fn translate_min_cardinality(exp: &owl::MinCardinality) -> Value { /// let min_cardinality_owl : owl::OWL = serde_json::from_str(min_cardinality).unwrap(); /// /// let axiom : Value = translation::translate(&min_cardinality_owl); -/// let axiom_expected_string = r#"["ObjectMinCardinality","\"1\"^^xsd:int","obo:IAO_0000120","obo:IAO_0000121"]"#; +/// let axiom_expected_string = r#"["ObjectMinCardinality","\"1\"^^","obo:IAO_0000120","obo:IAO_0000121"]"#; /// let axiom_expected : Value = serde_json::from_str(axiom_expected_string).unwrap(); /// /// assert_eq!(axiom, axiom_expected); @@ -316,7 +317,7 @@ pub fn translate_data_min_qualified_cardinality(exp: &owl::MinDataQualifiedCardi /// let max_cardinality_owl : owl::OWL = serde_json::from_str(max_cardinality).unwrap(); /// /// let axiom : Value = translation::translate(&max_cardinality_owl); -/// let axiom_expected_string = r#"["MaxCardinality","\"1\"^^xsd:int","obo:IAO_0000120"]"#; +/// let axiom_expected_string = r#"["MaxCardinality","\"1\"^^","obo:IAO_0000120"]"#; /// let axiom_expected : Value = serde_json::from_str(axiom_expected_string).unwrap(); /// /// assert_eq!(axiom, axiom_expected); @@ -347,7 +348,7 @@ pub fn translate_max_cardinality(exp: &owl::MaxCardinality) -> Value { /// let max_cardinality_owl : owl::OWL = serde_json::from_str(max_cardinality).unwrap(); /// /// let axiom : Value = translation::translate(&max_cardinality_owl); -/// let axiom_expected_string = r#"["ObjectMaxCardinality","\"1\"^^xsd:int","obo:IAO_0000120","obo:IAO_0000121"]"#; +/// let axiom_expected_string = r#"["ObjectMaxCardinality","\"1\"^^","obo:IAO_0000120","obo:IAO_0000121"]"#; /// let axiom_expected : Value = serde_json::from_str(axiom_expected_string).unwrap(); /// /// assert_eq!(axiom, axiom_expected); @@ -412,7 +413,7 @@ pub fn translate_data_max_qualified_cardinality(exp: &owl::MaxDataQualifiedCardi /// let exact_cardinality_owl : owl::OWL = serde_json::from_str(exact_cardinality).unwrap(); /// /// let axiom : Value = translation::translate(&exact_cardinality_owl); -/// let axiom_expected_string = r#"["ExactCardinality","\"1\"^^xsd:int","obo:IAO_0000120"]"#; +/// let axiom_expected_string = r#"["ExactCardinality","\"1\"^^","obo:IAO_0000120"]"#; /// let axiom_expected : Value = serde_json::from_str(axiom_expected_string).unwrap(); /// /// assert_eq!(axiom, axiom_expected); @@ -443,7 +444,7 @@ pub fn translate_exact_cardinality(exp: &owl::ExactCardinality) -> Value { /// let exact_cardinality_owl : owl::OWL = serde_json::from_str(exact_cardinality).unwrap(); /// /// let axiom : Value = translation::translate(&exact_cardinality_owl); -/// let axiom_expected_string = r#"["ObjectExactCardinality","\"1\"^^xsd:int","obo:IAO_0000120","obo:IAO_0000121"]"#; +/// let axiom_expected_string = r#"["ObjectExactCardinality","\"1\"^^","obo:IAO_0000120","obo:IAO_0000121"]"#; /// let axiom_expected : Value = serde_json::from_str(axiom_expected_string).unwrap(); /// /// assert_eq!(axiom, axiom_expected); @@ -549,7 +550,7 @@ pub fn translate_distinct_members(exp: &owl::DistinctMembers) -> Value { }; let operator = match rdf_type.as_str() { - "" => Value::String(String::from("DifferentIndividuals")), + OWL_ALL_DIFFERENT => Value::String(String::from("DifferentIndividuals")), _ => Value::String(String::from("Error")), }; @@ -586,7 +587,7 @@ pub fn translate_list(exp: &owl::RDFList) -> Value { let mut rest = translate(&exp.rdf_rest[0].object); //base case for RDF lists - if rest.is_string() && rest.as_str().unwrap() == "" { + if rest.is_string() && rest.as_str().unwrap() == RDF_NIL { let mut v = Vec::new(); v.push(first); Value::Array(v) @@ -624,7 +625,7 @@ pub fn check_class_type(v: &Option>) -> bool { match &t.object { //look for an owl:Class owl::OWL::Named(s) => { - if s == "" { + if s == OWL_CLASS { res = true } } @@ -649,7 +650,7 @@ pub fn check_data_range_type(v: &Option>) -> bool { //check all types match &t.object { owl::OWL::Named(s) => { - if s == "" { + if s == RDFS_DATATYPE { res = true } } diff --git a/src/ldtab_2_ofn/translation.rs b/src/ldtab_2_ofn/translation.rs index fd02f97..625e290 100644 --- a/src/ldtab_2_ofn/translation.rs +++ b/src/ldtab_2_ofn/translation.rs @@ -1,3 +1,4 @@ +use crate::constants::*; use crate::ldtab_2_ofn::annotation_translation; use crate::ldtab_2_ofn::axiom_translation; use crate::util::parser; @@ -70,53 +71,53 @@ fn translate_triple(subject: &str, predicate: &str, object: &str) -> Value { let object_json = parser::parse_thick_triple_object(object); match predicate_json.as_str() { - "" => { + RDFS_SUB_CLASS_OF => { axiom_translation::translate_subclass_of_axiom(&subject_json, &object_json) } - "" => { + OWL_EQUIVALENT_CLASS => { axiom_translation::translate_equivalent_class(&subject_json, &object_json) } - "" => axiom_translation::translate_disjoint_classes(&object_json), - "" => { + OWL_ALL_DISJOINT_CLASSES => axiom_translation::translate_disjoint_classes(&object_json), + OWL_DISJOINT_UNION_OF => { axiom_translation::translate_disjoint_union(&subject_json, &object_json) } - "" => { + OWL_DISJOINT_WITH => { axiom_translation::translate_disjoint_with(&subject_json, &object_json) } - "" => axiom_translation::translate_rdf_type(&subject_json, &object_json), - "" => axiom_translation::translate_domain(&subject_json, &object_json), - "" => axiom_translation::translate_range(&subject_json, &object_json), - "" => { + RDF_TYPE => axiom_translation::translate_rdf_type(&subject_json, &object_json), + RDFS_DOMAIN => axiom_translation::translate_domain(&subject_json, &object_json), + RDFS_RANGE => axiom_translation::translate_range(&subject_json, &object_json), + OWL_INVERSE_OF => { axiom_translation::translate_inverse_object_properties(&subject_json, &object_json) } - "" => { + OWL_EQUIVALENT_PROPERTY => { axiom_translation::translate_equivalent_properties(&subject_json, &object_json) } - "" => { + OWL_PROPERTY_DISJOINT_WITH => { axiom_translation::translate_property_disjoint_with(&subject_json, &object_json) } - "" => { + OWL_ALL_DISJOINT_PROPERTIES => { axiom_translation::translate_all_disjoint_properties(&subject_json, &object_json) } - "" => { + RDFS_SUB_PROPERTY_OF => { axiom_translation::translate_sub_property_of(&subject_json, &object_json) } - "" => { + OWL_ALL_DIFFERENT => { axiom_translation::translate_all_different(&subject_json, &object_json) } - "" => { + OWL_DIFFERENT_FROM => { axiom_translation::translate_different_from(&subject_json, &object_json) } - "" => axiom_translation::translate_same_as(&subject_json, &object_json), - "" => axiom_translation::translate_all_same_as(&subject_json, &object_json), - "" => { + OWL_SAME_AS => axiom_translation::translate_same_as(&subject_json, &object_json), + OWL_ALL_SAME_AS => axiom_translation::translate_all_same_as(&subject_json, &object_json), + OWL_PROPERTY_CHAIN_AXIOM => { axiom_translation::translate_property_chain(&subject_json, &object_json) } - "" => { + OWL_NEGATIVE_PROPERTY_ASSERTION => { axiom_translation::translate_negative_property_assertion(&subject_json, &object_json) } - "" => axiom_translation::translate_has_key(&subject_json, &object_json), - "" => axiom_translation::translate_import(&subject_json, &object_json), + OWL_HAS_KEY => axiom_translation::translate_has_key(&subject_json, &object_json), + OWL_IMPORTS => axiom_translation::translate_import(&subject_json, &object_json), _ => axiom_translation::translate_thin_triple(subject, predicate, object), } diff --git a/src/lib.rs b/src/lib.rs index f41e6b6..893cae3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +pub mod constants; pub mod thick_2_ofn; pub mod util; pub mod ofn_typing; diff --git a/src/main.rs b/src/main.rs index 192c4c3..f6208d6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ #![warn(clippy::all, clippy::pedantic)] +pub mod constants; pub mod thick_2_ofn; pub mod ofn_2_thick; pub mod ofn_2_man; diff --git a/src/ofn_2_ldtab/annotation_translation.rs b/src/ofn_2_ldtab/annotation_translation.rs index 2a54d27..f867789 100644 --- a/src/ofn_2_ldtab/annotation_translation.rs +++ b/src/ofn_2_ldtab/annotation_translation.rs @@ -2,21 +2,36 @@ use regex::Regex; use serde_json::json; use serde_json::Value; use std::collections::HashMap; +use std::sync::LazyLock; + +use crate::constants::*; + +static RE_SIMPLE_STRING: LazyLock = + LazyLock::new(|| Regex::new("^\"(?s)(.*)\"$").unwrap()); +static RE_LANG_TAG: LazyLock = + LazyLock::new(|| Regex::new("^\"(?s)(.*)\"@(.*)$").unwrap()); +static RE_TYPED_LITERAL: LazyLock = + LazyLock::new(|| Regex::new("^\"(?s)(.*)\"\\^\\^(.*)$").unwrap()); +static RE_LITERAL: LazyLock = + LazyLock::new(|| Regex::new("^\"(?s)(.+)\"(.*)$").unwrap()); +static RE_URI: LazyLock = + LazyLock::new(|| Regex::new("^<(.+)>$").unwrap()); +static RE_CURIE: LazyLock = + LazyLock::new(|| Regex::new("^(.+):(.+)$").unwrap()); pub fn is_annotation(v: &Value) -> bool { - match v.clone() { - Value::Array(x) => { - match x[0].as_str() { - Some("Annotation") => true, - //Some("AnnotationList") => true, //NB: this shouldn't occur - Some(_) => false, - None => false, - } - } - _ => false, - } + matches!( + v, + Value::Array(xs) + if xs.get(0) + .and_then(Value::as_str) + == Some("Annotation") + ) } + + + pub fn has_annotation(v: &Value) -> bool { match v.clone() { Value::Array(x) => is_annotation(&x[1]), //look into second argument @@ -57,17 +72,10 @@ pub fn get_annotations(v: &Value) -> Vec { pub fn is_literal(value: &Value) -> bool { // Ensure the Value is a string if let Some(s) = value.as_str() { - // Regex for a simple quoted string - let simple_string_re = Regex::new("^\"(?s)(.*)\"$").unwrap(); - // Regex for a string with a language tag (e.g., "hello"@en) - let lang_tag_re = Regex::new("^\"(?s)(.*)\"@(.*)$").unwrap(); - // Regex for a string with a datatype IRI or CURIE (e.g., "42"^^ or "42"^^prefix:suffix) - let iri_or_curie_re = Regex::new("^\"(?s)(.*)\"\\^\\^(.*)$").unwrap(); - - // Check if the string matches any of the forms - return simple_string_re.is_match(s) - || lang_tag_re.is_match(s) - || iri_or_curie_re.is_match(s); + // Check if the string matches any of the literal forms + return RE_SIMPLE_STRING.is_match(s) + || RE_LANG_TAG.is_match(s) + || RE_TYPED_LITERAL.is_match(s); } false } @@ -80,7 +88,7 @@ pub fn translate_literal(s: &str) -> Value { let text = lexical_form[1..].to_string(); // drop the leading " return json!({ "object": text, - "meta": "owl:Axiom", + "meta": OWL_AXIOM, "datatype": datatype }); } @@ -90,7 +98,7 @@ pub fn translate_literal(s: &str) -> Value { let text = lexical_form[1..].to_string(); // drop the leading " return json!({ "object": text, - "meta": "owl:Axiom", + "meta": OWL_AXIOM, "datatype": format!("@{}", lang) }); } @@ -102,8 +110,8 @@ pub fn translate_literal(s: &str) -> Value { let text = &s[1..s.len()-1]; return json!({ "object": text, - "meta": "owl:Axiom", - "datatype": "xsd:string" + "meta": OWL_AXIOM, + "datatype": XSD_STRING }); } @@ -111,8 +119,8 @@ pub fn translate_literal(s: &str) -> Value { // TODO: this case should not occur json!({ "object": s, - "meta": "owl:Axiom", - "datatype": "xsd:string" + "meta": OWL_AXIOM, + "datatype": XSD_STRING }) } @@ -121,20 +129,16 @@ pub fn translate_literal(s: &str) -> Value { pub fn translate_value(v: &Value) -> Value { let s = v.as_str().unwrap(); - let literal = Regex::new("^\"(?s)(.+)\"(.*)$").unwrap(); - let uri = Regex::new("^<(.+)>$").unwrap(); - let curie = Regex::new("^(.+):(.+)$").unwrap(); - - if literal.is_match(s) { + if RE_LITERAL.is_match(s) { translate_literal(s) - } else if uri.is_match(s) { + } else if RE_URI.is_match(s) { json!({"object" : s, - "meta" : "owl:Axiom", - "datatype" : "_IRI"}) - } else if curie.is_match(s) { + "meta" : OWL_AXIOM, + "datatype" : LDTAB_IRI}) + } else if RE_CURIE.is_match(s) { json!({"object" : s, - "meta" : "owl:Axiom", - "datatype" : "_IRI"}) + "meta" : OWL_AXIOM, + "datatype" : LDTAB_IRI}) } else { json!("ERROR") } diff --git a/src/ofn_2_ldtab/axiom_translation.rs b/src/ofn_2_ldtab/axiom_translation.rs index 3239732..fb2ba39 100644 --- a/src/ofn_2_ldtab/axiom_translation.rs +++ b/src/ofn_2_ldtab/axiom_translation.rs @@ -1,22 +1,23 @@ use crate::ofn_2_ldtab::annotation_translation; use crate::ofn_2_ldtab::class_translation; +use crate::constants::*; use crate::ofn_2_ldtab::property_translation; use crate::ofn_2_ldtab::rule_translation; use crate::ofn_2_ldtab::util; -use rand::Rng; use serde_json::json; use serde_json::Value; -use sha2::{Digest, Sha256}; -use std::collections::hash_map::DefaultHasher; -use std::hash::{Hash, Hasher}; - -//TODO -// -// == [1] == -//top level translation is not always correct: -//- "subject":{"datatype":"_IRI","object":"http://purl.obolibrary.org/obo/OBI_0000301"}} -//should just be -//- "subject":"http://purl.obolibrary.org/obo/OBI_0000301" + + +/// Splits an annotated axiom into its logical part and translated annotations. +/// Returns `(owl, annotation)` where `owl` is the axiom with annotations stripped, +/// and `annotation` is the JSON representation of the annotations. +fn split_axiom(v: &Value) -> (Value, Value) { + let owl = annotation_translation::get_owl(v); + let annotations = annotation_translation::get_annotations(v); + let annotation = annotation_translation::translate_annotations(&annotations); + (owl, annotation) +} + pub fn translate_declaration(v: &Value) -> Value { let owl = annotation_translation::get_owl(v); @@ -35,10 +36,7 @@ pub fn translate_declaration(v: &Value) -> Value { } pub fn translate_class_declaration(v: &Value) -> Value { - //split annotations from logical structure - let owl = annotation_translation::get_owl(v); - let ofn_annotations = annotation_translation::get_annotations(v); - let annotation = annotation_translation::translate_annotations(&ofn_annotations); + let (owl, annotation) = split_axiom(v); //unwrap declaration let unwrapped_declaration = owl[1].clone(); @@ -51,19 +49,16 @@ pub fn translate_class_declaration(v: &Value) -> Value { "retraction":"0", "graph":"graph", "subject":class, - "predicate":"", - "object":"", - "datatype":"_IRI", + "predicate":RDF_TYPE, + "object":OWL_CLASS, + "datatype":LDTAB_IRI, "annotation":annotation }); triple } pub fn translate_ontology_import(v: &Value) -> Value { - //split annotations from logical structure - let owl = annotation_translation::get_owl(v); - let ofn_annotations = annotation_translation::get_annotations(v); - let annotation = annotation_translation::translate_annotations(&ofn_annotations); + let (owl, annotation) = split_axiom(v); let subject = class_translation::translate(&owl[1]); let object = class_translation::translate(&owl[2]); @@ -72,9 +67,9 @@ pub fn translate_ontology_import(v: &Value) -> Value { "retraction":"0", "graph":"graph", "subject": subject, - "predicate":"", + "predicate":OWL_IMPORTS, "object": object, - "datatype":"_IRI", + "datatype":LDTAB_IRI, "annotation":annotation }) } @@ -98,10 +93,7 @@ pub fn translate_ontology_annotation(v: &Value) -> Value { } pub fn translate_class_assertion_axiom(v: &Value) -> Value { - //split annotations from logical structure - let owl = annotation_translation::get_owl(v); - let ofn_annotations = annotation_translation::get_annotations(v); - let annotation = annotation_translation::translate_annotations(&ofn_annotations); + let (owl, annotation) = split_axiom(v); //translate OWL classes let class = class_translation::translate(&owl[1]); @@ -112,19 +104,16 @@ pub fn translate_class_assertion_axiom(v: &Value) -> Value { "retraction":"0", "graph":"graph", "subject":individual, - "predicate":"", + "predicate":RDF_TYPE, "object":class, - "datatype":"_IRI", + "datatype":LDTAB_IRI, "annotation":annotation }); triple } pub fn translate_object_property_assertion_axiom(v: &Value) -> Value { - //split annotations from logical structure - let owl = annotation_translation::get_owl(v); - let ofn_annotations = annotation_translation::get_annotations(v); - let annotation = annotation_translation::translate_annotations(&ofn_annotations); + let (owl, annotation) = split_axiom(v); //translate OWL classes let property = property_translation::translate(&owl[1]); @@ -138,22 +127,34 @@ pub fn translate_object_property_assertion_axiom(v: &Value) -> Value { "subject":from, "predicate":property, "object":to, - "datatype":"_IRI", + "datatype":LDTAB_IRI, "annotation":annotation }); triple } pub fn translate_data_property_assertion_axiom(v: &Value) -> Value { - //split annotations from logical structure - let owl = annotation_translation::get_owl(v); - let ofn_annotations = annotation_translation::get_annotations(v); - let annotation = annotation_translation::translate_annotations(&ofn_annotations); + let (owl, annotation) = split_axiom(v); //translate OWL classes let property = property_translation::translate(&owl[1]); let from = class_translation::translate(&owl[2]); - let to = class_translation::translate(&owl[3]); + + let to: Value = if let Some(s) = owl[3].as_str() { + if let Some((literal, datatype)) = s.split_once("^^") { + json!({ "object": unquote_once(literal), "datatype": datatype }) + } else if let Some((literal, language)) = s.split_once('@') { + json!({ "object": unquote_once(literal), "datatype": format!("@{language}") }) + } else { + json!({ "object": unquote_once(s), "datatype": XSD_STRING }) + } +} else { + Value::Null + }; + + let literal = to.get("object").unwrap().as_str().unwrap(); + let datatype = to.get("datatype").unwrap().as_str().unwrap(); + let triple = json!({ "assertion":"1", @@ -161,100 +162,99 @@ pub fn translate_data_property_assertion_axiom(v: &Value) -> Value { "graph":"graph", "subject":from, "predicate":property, - "object":to, - "datatype":"_IRI", + "object":literal, + "datatype":datatype, "annotation":annotation }); triple } pub fn translate_negative_object_property_assertion_axiom(v: &Value) -> Value { - //split annotations from logical structure - let owl = annotation_translation::get_owl(v); - let ofn_annotations = annotation_translation::get_annotations(v); - let annotation = annotation_translation::translate_annotations(&ofn_annotations); + let (owl, annotation) = split_axiom(v); //translate OWL classes let property = property_translation::translate(&owl[1]); let from = class_translation::translate(&owl[2]); let to = class_translation::translate(&owl[3]); - let blank_node = json!({"predicate":"", - "object": {"":[{"object":from}], - "":[{"object":property}], - "":[{"object":to}]}, - "datatype":"_IRI"}); - - let blank_sorted = util::sort_value(&blank_node); - let blank_string = blank_sorted.to_string(); + //TODO reuse blank node object + let blank_node = json!({RDF_TYPE:[{"object" : OWL_NEGATIVE_PROPERTY_ASSERTION, "datatype" : LDTAB_IRI}], + OWL_SOURCE_INDIVIDUAL:[{"object":from, "datatype":LDTAB_IRI}], + OWL_ASSERTION_PROPERTY:[{"object":property, "datatype":LDTAB_IRI}], + OWL_TARGET_INDIVIDUAL:[{"object":to, "datatype":LDTAB_IRI}]}); - let mut hasher = Sha256::new(); - hasher.update(blank_string.as_bytes()); - let blank_node_hash = hasher.finalize(); - let blank_node_id = format!("", blank_node_hash); + let blank_node_id = util::generate_blank_node_id(&blank_node); let triple = json!({ "assertion":"1", "retraction":"0", "graph":"graph", "subject":blank_node_id, - "predicate":"", - "object":{ - "":[{"object":from}], - "":[{"object":property}], - "":[{"object":to}] - }, - "datatype":"_IRI", + "predicate":OWL_NEGATIVE_PROPERTY_ASSERTION, + "object": {RDF_TYPE:[{"object" : OWL_NEGATIVE_PROPERTY_ASSERTION, "datatype" : LDTAB_IRI}], + OWL_SOURCE_INDIVIDUAL:[{"object":from, "datatype":LDTAB_IRI}], + OWL_ASSERTION_PROPERTY:[{"object":property, "datatype":LDTAB_IRI}], + OWL_TARGET_INDIVIDUAL:[{"object":to, "datatype":LDTAB_IRI}]}, + "datatype":LDTAB_JSON_MAP, "annotation":annotation }); triple } +fn unquote_once(s: &str) -> &str { + s.strip_prefix('"') + .and_then(|t| t.strip_suffix('"')) + .unwrap_or(s) +} + pub fn translate_negative_data_property_assertion_axiom(v: &Value) -> Value { - //split annotations from logical structure - let owl = annotation_translation::get_owl(v); - let ofn_annotations = annotation_translation::get_annotations(v); - let annotation = annotation_translation::translate_annotations(&ofn_annotations); + let (owl, annotation) = split_axiom(v); //translate OWL classes let property = property_translation::translate(&owl[1]); let from = class_translation::translate(&owl[2]); - let to = class_translation::translate(&owl[3]); - let blank_node = json!({"predicate":"", - "object": {"":[{"object":from}], - "":[{"object":property}], - "":[{"object":to}]}, - "datatype":"_IRI"}); + //TODO: handle data values properly + let to: Value = if let Some(s) = owl[3].as_str() { + if let Some((literal, datatype)) = s.split_once("^^") { + json!({ "object": unquote_once(literal), "datatype": datatype }) + } else if let Some((literal, language)) = s.split_once('@') { + json!({ "object": unquote_once(literal), "datatype": format!("@{language}") }) + } else { + json!({ "object": unquote_once(s), "datatype": XSD_STRING }) + } +} else { + Value::Null + }; + + let literal = to.get("object").unwrap().as_str().unwrap(); + let datatype = to.get("datatype").unwrap().as_str().unwrap(); - let blank_sorted = util::sort_value(&blank_node); - let blank_string = blank_sorted.to_string(); + let blank_node = json!({RDF_TYPE:[{"object" : OWL_NEGATIVE_PROPERTY_ASSERTION, "datatype" : LDTAB_IRI}], + OWL_SOURCE_INDIVIDUAL:[{"object":from, "datatype":LDTAB_IRI}], + OWL_ASSERTION_PROPERTY:[{"object":property, "datatype":LDTAB_IRI}], + OWL_TARGET_VALUE:[{"object":literal, "datatype":datatype }]}); - let mut hasher = Sha256::new(); - hasher.update(blank_string.as_bytes()); - let blank_node_hash = hasher.finalize(); - let blank_node_id = format!("", blank_node_hash); + let blank_node_id = util::generate_blank_node_id(&blank_node); let triple = json!({ "assertion":"1", "retraction":"0", "graph":"graph", "subject":blank_node_id, - "predicate":"", - "object":{ - "":[{"object":from}], - "":[{"object":property}], - "":[{"object":to}] - }, - "datatype":"_IRI", + "predicate":OWL_NEGATIVE_PROPERTY_ASSERTION, + "object":{ RDF_TYPE:[{"object" : OWL_NEGATIVE_PROPERTY_ASSERTION, "datatype" : LDTAB_IRI}], + OWL_SOURCE_INDIVIDUAL:[{"object":from, "datatype":LDTAB_IRI}], + OWL_ASSERTION_PROPERTY:[{"object":property, "datatype":LDTAB_IRI}], + OWL_TARGET_VALUE:[{"object":literal, "datatype":datatype }]}, + "datatype":LDTAB_JSON_MAP, "annotation":annotation }); triple } pub fn translate_same_individuals_axiom(v: &Value) -> Value { - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); + let (owl, annotation) = split_axiom(v); let number_of_operands = (owl.as_array().unwrap())[1..].len(); @@ -262,92 +262,75 @@ pub fn translate_same_individuals_axiom(v: &Value) -> Value { //TODO check that class_translation supports individuals let lhs = class_translation::translate(&owl[1]); let rhs = class_translation::translate(&owl[2]); - let annotation = annotation_translation::translate_annotations(&annotations); let triple = json!({ "assertion":"1", "retraction":"0", "graph":"graph", //TODO "subject":lhs, - "predicate":"", + "predicate":OWL_SAME_AS, "object":rhs, "datatype":util::translate_datatype(&json!(rhs)), "annotation":annotation}); triple } else { let operands: Value = class_translation::translate_list(&(owl.as_array().unwrap())[1..]); - let annotation = annotation_translation::translate_annotations(&annotations); //NB: IRIs are not expanded by wiring - this is LDTab's responsibility - let blank_node = json!({"predicate":"", - "object": {"":[{"object":operands, "datatype":"_JSONLIST"}]}, - "datatype":"_JSONMAP"}); - - let blank_sorted = util::sort_value(&blank_node); - let blank_string = blank_sorted.to_string(); + let blank_node = json!({"predicate":OWL_ALL_SAME_AS, + "object": {OWL_MEMBERS:[{"object":operands, "datatype":LDTAB_JSON_LIST}]}, + "datatype":LDTAB_JSON_MAP}); - let mut hasher = Sha256::new(); - hasher.update(blank_string.as_bytes()); - let blank_node_hash = hasher.finalize(); - let blank_node_id = format!("", blank_node_hash); + let blank_node_id = util::generate_blank_node_id(&blank_node); let triple = json!({"assertion":"1", "retraction":"0", "graph":"graph", //TODO "subject":blank_node_id, - //"predicate":"", - "predicate":"", //this is LDtab specific - "object": {"":[{"object":operands, "datatype":"_JSONLIST"}]}, //TODO remove datatype - "datatype":"_JSONMAP", + //"predicate":OWL_SAME_AS, + "predicate":OWL_ALL_SAME_AS, //this is LDtab specific + "object": {OWL_MEMBERS:[{"object":operands, "datatype":LDTAB_JSON_LIST}]}, //TODO remove datatype + "datatype":LDTAB_JSON_MAP, "annotation":annotation}); triple } } pub fn translate_different_individuals_axiom(v: &Value) -> Value { - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); + let (owl, annotation) = split_axiom(v); let number_of_operands = (owl.as_array().unwrap())[1..].len(); if number_of_operands == 2 { let lhs = class_translation::translate(&owl[1]); let rhs = class_translation::translate(&owl[2]); - let annotation = annotation_translation::translate_annotations(&annotations); let triple = json!({ "assertion":"1", "retraction":"0", "graph":"graph", //TODO "subject":lhs, - "predicate":"", + "predicate":OWL_DIFFERENT_FROM, "object":rhs, "datatype":util::translate_datatype(&json!(rhs)), "annotation":annotation}); triple } else { let operands: Value = class_translation::translate_list(&(owl.as_array().unwrap())[1..]); - let annotation = annotation_translation::translate_annotations(&annotations); - //NB: IRIs are not expanded by wiring - this is LDTab's responsibility - let blank_node = json!({"predicate":"", - "object": {"":[{"object":operands, "datatype":"_JSONLIST"}]}, - "datatype":"_JSONMAP"}); + //TODO: this object should be reused + let blank_node = json!({OWL_DISTINCT_MEMBERS:[{"object":operands, "datatype":LDTAB_JSON_LIST}], + RDF_TYPE:[{"datatype":LDTAB_IRI,"object":OWL_ALL_DIFFERENT}]}); - let blank_sorted = util::sort_value(&blank_node); - let blank_string = blank_sorted.to_string(); - - let mut hasher = Sha256::new(); - hasher.update(blank_string.as_bytes()); - let blank_node_hash = hasher.finalize(); - let blank_node_id = format!("", blank_node_hash); + let blank_node_id = util::generate_blank_node_id(&blank_node); let triple = json!({"assertion":"1", "retraction":"0", "graph":"graph", //TODO "subject":blank_node_id, - "predicate":"", - "object": {"":[{"object":operands, "datatype":"_JSONLIST"}]}, - "datatype":"_JSONMAP", + "predicate":OWL_ALL_DIFFERENT, + "object": {OWL_DISTINCT_MEMBERS:[{"object":operands, "datatype":LDTAB_JSON_LIST}], + RDF_TYPE:[{"datatype":LDTAB_IRI,"object":OWL_ALL_DIFFERENT}]}, + "datatype":LDTAB_JSON_MAP, "annotation":annotation}); triple } @@ -355,9 +338,7 @@ pub fn translate_different_individuals_axiom(v: &Value) -> Value { pub fn translate_object_property_declaration(v: &Value) -> Value { //split annotations from logical structure - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); - let annotation = annotation_translation::translate_annotations(&annotations); + let (owl, annotation) = split_axiom(v); //unwrap declaration let unwrapped_declaration = owl[1].clone(); @@ -369,9 +350,9 @@ pub fn translate_object_property_declaration(v: &Value) -> Value { "retraction":"0", "graph":"graph", "subject":property, - "predicate":"", - "object":"", - "datatype":"_IRI", + "predicate":RDF_TYPE, + "object":OWL_OBJECT_PROPERTY, + "datatype":LDTAB_IRI, "annotation":annotation }); triple @@ -379,9 +360,7 @@ pub fn translate_object_property_declaration(v: &Value) -> Value { pub fn translate_data_property_declaration(v: &Value) -> Value { //split annotations from logical structure - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); - let annotation = annotation_translation::translate_annotations(&annotations); + let (owl, annotation) = split_axiom(v); //unwrap declaration let unwrapped_declaration = owl[1].clone(); @@ -393,9 +372,9 @@ pub fn translate_data_property_declaration(v: &Value) -> Value { "retraction":"0", "graph":"graph", "subject":property, - "predicate":"", - "object":"", - "datatype":"_IRI", + "predicate":RDF_TYPE, + "object":OWL_DATATYPE_PROPERTY, + "datatype":LDTAB_IRI, "annotation":annotation }); triple @@ -403,9 +382,7 @@ pub fn translate_data_property_declaration(v: &Value) -> Value { pub fn translate_annotation_property_declaration(v: &Value) -> Value { //split annotations from logical structure - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); - let annotation = annotation_translation::translate_annotations(&annotations); + let (owl, annotation) = split_axiom(v); //unwrap declaration let unwrapped_declaration = owl[1].clone(); @@ -417,9 +394,9 @@ pub fn translate_annotation_property_declaration(v: &Value) -> Value { "retraction":"0", "graph":"graph", "subject":property, - "predicate":"", - "object":"", - "datatype":"_IRI", + "predicate":RDF_TYPE, + "object":OWL_ANNOTATION_PROPERTY, + "datatype":LDTAB_IRI, "annotation":annotation }); triple @@ -428,9 +405,7 @@ pub fn translate_annotation_property_declaration(v: &Value) -> Value { //TODO: test this pub fn translate_datatype_definition(v: &Value) -> Value { //split annotations from logical structure - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); - let annotation = annotation_translation::translate_annotations(&annotations); + let (owl, annotation) = split_axiom(v); //TODO: check this (should just be a string) let lhs = owl[1].clone(); @@ -441,9 +416,9 @@ pub fn translate_datatype_definition(v: &Value) -> Value { "retraction":"0", "graph":"graph", "subject":lhs, - "predicate":"", + "predicate":OWL_EQUIVALENT_CLASS, "object":rhs, - "datatype":"_IRI", + "datatype":LDTAB_IRI, "annotation":annotation }); triple @@ -451,9 +426,7 @@ pub fn translate_datatype_definition(v: &Value) -> Value { pub fn translate_datatype_declaration(v: &Value) -> Value { //split annotations from logical structure - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); - let annotation = annotation_translation::translate_annotations(&annotations); + let (owl, annotation) = split_axiom(v); //unwrap declaration let unwrapped_declaration = owl[1].clone(); @@ -466,9 +439,9 @@ pub fn translate_datatype_declaration(v: &Value) -> Value { "retraction":"0", "graph":"graph", "subject":datatype, - "predicate":"", - "object":"", - "datatype":"_IRI", + "predicate":RDF_TYPE, + "object":RDFS_DATATYPE, + "datatype":LDTAB_IRI, "annotation":annotation }); triple @@ -476,9 +449,7 @@ pub fn translate_datatype_declaration(v: &Value) -> Value { pub fn translate_individual_declaration(v: &Value) -> Value { //split annotations from logical structure - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); - let annotation = annotation_translation::translate_annotations(&annotations); + let (owl, annotation) = split_axiom(v); //unwrap declaration let unwrapped_declaration = owl[1].clone(); @@ -491,9 +462,9 @@ pub fn translate_individual_declaration(v: &Value) -> Value { "retraction":"0", "graph":"graph", "subject":individual, - "predicate":"", - "object":"", - "datatype":"_IRI", + "predicate":RDF_TYPE, + "object":OWL_NAMED_INDIVIDUAL, + "datatype":LDTAB_IRI, "annotation":annotation }); triple @@ -501,8 +472,7 @@ pub fn translate_individual_declaration(v: &Value) -> Value { pub fn translate_sub_object_property(v: &Value) -> Value { //split annotations from logical structure - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); + let (owl, annotation) = split_axiom(v); //SubObjectPropertyOf( ObjectPropertyChain( OPE1 ... OPEn ) OPE ) //is translated as @@ -512,27 +482,25 @@ pub fn translate_sub_object_property(v: &Value) -> Value { if owl[1].is_array() && owl[1][0].as_str().unwrap().eq("ObjectPropertyChain") { let sub = property_translation::translate_list(&(owl[1].as_array().unwrap())[1..]); let sup = property_translation::translate(&owl[2]); - let annotation = annotation_translation::translate_annotations(&annotations); json!({ "assertion":"1", "retraction":"0", "graph":"graph", "subject":sup, - "predicate":"", + "predicate":OWL_PROPERTY_CHAIN_AXIOM, "object":sub, - "datatype":"_JSONLIST", + "datatype":LDTAB_JSON_LIST, "annotation":annotation }) } else { let sub = property_translation::translate(&owl[1]); let sup = property_translation::translate(&owl[2]); - let annotation = annotation_translation::translate_annotations(&annotations); json!({ "assertion":"1", "retraction":"0", "graph":"graph", "subject":sub, - "predicate":"", + "predicate":RDFS_SUB_PROPERTY_OF, "object":sup, "datatype":util::translate_datatype(&json!(sup)), "annotation":annotation @@ -541,19 +509,16 @@ pub fn translate_sub_object_property(v: &Value) -> Value { } pub fn translate_sub_data_property(v: &Value) -> Value { - //split annotations from logical structure - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); + let (owl, annotation) = split_axiom(v); let sub = property_translation::translate(&owl[1]); let sup = property_translation::translate(&owl[2]); - let annotation = annotation_translation::translate_annotations(&annotations); json!({ "assertion":"1", "retraction":"0", "graph":"graph", "subject":sub, - "predicate":"", + "predicate":RDFS_SUB_PROPERTY_OF, "object":sup, "datatype":util::translate_datatype(&json!(sup)), "annotation":annotation @@ -561,21 +526,17 @@ pub fn translate_sub_data_property(v: &Value) -> Value { } pub fn translate_subclass_of_axiom(v: &Value) -> Value { - //split annotations from logical structure - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); + let (owl, annotation) = split_axiom(v); - //translate OWL classes let subclass = class_translation::translate(&owl[1]); let superclass = class_translation::translate(&owl[2]); - let annotation = annotation_translation::translate_annotations(&annotations); let triple = json!({ "assertion":"1", "retraction":"0", "graph":"graph", "subject":subclass, - "predicate":"", + "predicate":RDFS_SUB_CLASS_OF, "object":superclass, "datatype":util::translate_datatype(&json!(superclass)), "annotation":annotation @@ -584,21 +545,17 @@ pub fn translate_subclass_of_axiom(v: &Value) -> Value { } pub fn translate_sub_annotation_property_of_axiom(v: &Value) -> Value { - //split annotations from logical structure - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); + let (owl, annotation) = split_axiom(v); - //translate OWL classes let lhs = property_translation::translate(&owl[1]); let rhs = property_translation::translate(&owl[2]); - let annotation = annotation_translation::translate_annotations(&annotations); let triple = json!({ "assertion":"1", "retraction":"0", "graph":"graph", "subject":lhs, - "predicate":"", + "predicate":RDFS_SUB_PROPERTY_OF, "object":rhs, "datatype":util::translate_datatype(&json!(rhs)), "annotation":annotation @@ -619,7 +576,7 @@ pub fn translate_disjoint_classes_axiom(v: &Value) -> Value { "retraction":"0", "graph":"graph", "subject":lhs, - "predicate":"", + "predicate":OWL_DISJOINT_WITH, "object": rhs, "datatype":util::translate_datatype(&json!(rhs)), "annotation":annotation}); @@ -628,48 +585,38 @@ pub fn translate_disjoint_classes_axiom(v: &Value) -> Value { let operands: Value = class_translation::translate_list(&(owl.as_array().unwrap())[1..]); let annotation = annotation_translation::translate_annotations(&annotations); - let blank_node = json!({"":[{"object":operands, "datatype":"_JSONLIST"}],"":[{"datatype":"_IRI","object":""}]}); + let blank_node = json!({OWL_MEMBERS:[{"object":operands, "datatype":LDTAB_JSON_LIST}],RDF_TYPE:[{"datatype":LDTAB_IRI,"object":OWL_ALL_DISJOINT_CLASSES}]}); //"annotation":annotation}); - let blank_sorted = util::sort_value(&blank_node); - let blank_string = blank_sorted.to_string(); - - println!("Blank node: {}", blank_string); - - let mut hasher = Sha256::new(); - hasher.update(blank_string.as_bytes()); - let blank_node_hash = hasher.finalize(); - let blank_node_id = format!("", blank_node_hash); + let blank_node_id = util::generate_blank_node_id(&blank_node); let triple = json!({"assertion":"1", "retraction":"0", "graph":"graph", "subject":blank_node_id, - "predicate":"", - "object": {"":[{"object":operands, "datatype":"_JSONLIST"}],"":[{"datatype":"_IRI","object":""}]}, - "datatype": "_JSONMAP", + "predicate":OWL_ALL_DISJOINT_CLASSES, + "object": {OWL_MEMBERS:[{"object":operands, "datatype":LDTAB_JSON_LIST}],RDF_TYPE:[{"datatype":LDTAB_IRI,"object":OWL_ALL_DISJOINT_CLASSES}]}, + "datatype": LDTAB_JSON_MAP, "annotation":annotation}); triple } } pub fn translate_disjoint_union_of_axiom(v: &Value) -> Value { - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); + let (owl, annotation) = split_axiom(v); let lhs = class_translation::translate(&owl[1]); let operands: Value = class_translation::translate_list(&(owl.as_array().unwrap())[2..]); - let annotation = annotation_translation::translate_annotations(&annotations); let triple = json!({ "assertion":"1", "retraction":"0", "graph":"graph", //TODO "subject":lhs, - "predicate":"", + "predicate":OWL_DISJOINT_UNION_OF, "object":operands, - "datatype": "_JSONLIST", + "datatype": LDTAB_JSON_LIST, "annotation":annotation}); triple } @@ -677,66 +624,55 @@ pub fn translate_disjoint_union_of_axiom(v: &Value) -> Value { //TODO:: equivalent classe (we have a custom encoding for this and need a case distinction //between binary axioms and n-ary axioms) pub fn translate_equivalent_classes_axiom(v: &Value) -> Value { - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); + let (owl, annotation) = split_axiom(v); let number_of_operands = (owl.as_array().unwrap())[1..].len(); if number_of_operands == 2 { let lhs = class_translation::translate(&owl[1]); let rhs = class_translation::translate(&owl[2]); - let annotation = annotation_translation::translate_annotations(&annotations); let triple = json!({ "assertion":"1", "retraction":"0", "graph":"graph", //TODO "subject":lhs, - "predicate":"", + "predicate":OWL_EQUIVALENT_CLASS, "object":rhs, "datatype":util::translate_datatype(&json!(rhs)), "annotation":annotation}); triple } else { let operands: Value = class_translation::translate_list(&(owl.as_array().unwrap())[1..]); - let annotation = annotation_translation::translate_annotations(&annotations); - let blank_node = json!({"predicate":"", - "object": {"":[{"object":operands, "datatype":"_JSONLIST"}]}, - "datatype":"_JSONMAP"}); + let blank_node = json!({"predicate":OWL_EQUIVALENT_CLASS, + "object": {OWL_MEMBERS:[{"object":operands, "datatype":LDTAB_JSON_LIST}]}, + "datatype":LDTAB_JSON_MAP}); - let blank_sorted = util::sort_value(&blank_node); - let blank_string = blank_sorted.to_string(); - - let mut hasher = Sha256::new(); - hasher.update(blank_string.as_bytes()); - let blank_node_hash = hasher.finalize(); - let blank_node_id = format!("", blank_node_hash); + let blank_node_id = util::generate_blank_node_id(&blank_node); let triple = json!({"assertion":"1", "retraction":"0", "graph":"graph", //TODO "subject":blank_node_id, - "predicate":"", - "object": {"":[{"object":operands, "datatype":"_JSONLIST"}]}, //TODO remove datatype - "datatype":"_JSONMAP", + "predicate":OWL_EQUIVALENT_CLASS, + "object": {OWL_MEMBERS:[{"object":operands, "datatype":LDTAB_JSON_LIST}]}, //TODO remove datatype + "datatype":LDTAB_JSON_MAP, "annotation":annotation}); triple } } pub fn translate_object_property_domain_axiom(v: &Value) -> Value { - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); + let (owl, annotation) = split_axiom(v); let property = property_translation::translate(&owl[1]); let domain = class_translation::translate(&owl[2]); - let annotation = annotation_translation::translate_annotations(&annotations); let triple = json!({"assertion":"1", "retraction":"0", "graph":"graph", //TODO "subject":property, - "predicate":"", + "predicate":RDFS_DOMAIN, "object":domain, "datatype": util::translate_datatype(&json!(domain)), "annotation":annotation}); @@ -744,18 +680,16 @@ pub fn translate_object_property_domain_axiom(v: &Value) -> Value { } pub fn translate_object_property_range_axiom(v: &Value) -> Value { - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); + let (owl, annotation) = split_axiom(v); let property = property_translation::translate(&owl[1]); let range = class_translation::translate(&owl[2]); - let annotation = annotation_translation::translate_annotations(&annotations); let triple = json!({"assertion":"1", "retraction":"0", "graph":"graph", //TODO "subject":property, - "predicate":"", + "predicate":RDFS_RANGE, "object":range, "datatype": util::translate_datatype(&json!(range)), "annotation":annotation}); @@ -763,18 +697,16 @@ pub fn translate_object_property_range_axiom(v: &Value) -> Value { } pub fn translate_annotation_property_domain_axiom(v: &Value) -> Value { - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); + let (owl, annotation) = split_axiom(v); let property = property_translation::translate(&owl[1]); let domain = class_translation::translate(&owl[2]); //TODO IRI - let annotation = annotation_translation::translate_annotations(&annotations); let triple = json!({"assertion":"1", "retraction":"0", "graph":"graph", //TODO "subject":property, - "predicate":"", + "predicate":RDFS_DOMAIN, "object":domain, "datatype": util::translate_datatype(&json!(domain)), "annotation":annotation}); @@ -782,18 +714,16 @@ pub fn translate_annotation_property_domain_axiom(v: &Value) -> Value { } pub fn translate_annotation_property_range_axiom(v: &Value) -> Value { - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); + let (owl, annotation) = split_axiom(v); let property = property_translation::translate(&owl[1]); let range = class_translation::translate(&owl[2]); - let annotation = annotation_translation::translate_annotations(&annotations); let triple = json!({"assertion":"1", "retraction":"0", "graph":"graph", //TODO "subject":property, - "predicate":"", + "predicate":RDFS_RANGE, "object":range, "datatype": util::translate_datatype(&json!(range)), "annotation":annotation}); @@ -802,66 +732,55 @@ pub fn translate_annotation_property_range_axiom(v: &Value) -> Value { //TODO test n-ary case pub fn translate_equivalent_properties_axiom(v: &Value) -> Value { - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); + let (owl, annotation) = split_axiom(v); let number_of_operands = (owl.as_array().unwrap())[1..].len(); if number_of_operands == 2 { let lhs = property_translation::translate(&owl[1]); let rhs = property_translation::translate(&owl[2]); - let annotation = annotation_translation::translate_annotations(&annotations); let triple = json!({ "assertion":"1", "retraction":"0", "graph":"graph", //TODO "subject":lhs, - "predicate":"", + "predicate":OWL_EQUIVALENT_PROPERTY, "object":rhs, "datatype":util::translate_datatype(&json!(rhs)), "annotation":annotation}); triple } else { let operands: Value = property_translation::translate_list(&(owl.as_array().unwrap())[1..]); - let annotation = annotation_translation::translate_annotations(&annotations); - - let blank_node = json!({"predicate":"", - "object": {"":[{"object":operands, "datatype":"_JSONLIST"}]}, - "datatype":"_JSONMAP"}); - let blank_sorted = util::sort_value(&blank_node); - let blank_string = blank_sorted.to_string(); + let blank_node = json!({"predicate":OWL_EQUIVALENT_PROPERTY, + "object": {OWL_MEMBERS:[{"object":operands, "datatype":LDTAB_JSON_LIST}]}, + "datatype":LDTAB_JSON_MAP}); - let mut hasher = Sha256::new(); - hasher.update(blank_string.as_bytes()); - let blank_node_hash = hasher.finalize(); - let blank_node_id = format!("", blank_node_hash); + let blank_node_id = util::generate_blank_node_id(&blank_node); let triple = json!({"assertion":"1", "retraction":"0", "graph":"graph", //TODO "subject":blank_node_id, - "predicate":"", //TODO AllEquivalentProperties? - "object": {"":[{"object":operands, "datatype":"_JSONLIST"}]}, //TODO remove datatype - "datatype":"_JSONMAP", + "predicate":OWL_EQUIVALENT_PROPERTY, //TODO AllEquivalentProperties? + "object": {OWL_MEMBERS:[{"object":operands, "datatype":LDTAB_JSON_LIST}]}, //TODO remove datatype + "datatype":LDTAB_JSON_MAP, "annotation":annotation}); triple } } pub fn translate_data_property_domain_axiom(v: &Value) -> Value { - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); + let (owl, annotation) = split_axiom(v); let property = property_translation::translate(&owl[1]); let domain = class_translation::translate(&owl[2]); - let annotation = annotation_translation::translate_annotations(&annotations); let triple = json!({"assertion":"1", "retraction":"0", "graph":"graph", //TODO "subject":property, - "predicate":"", + "predicate":RDFS_DOMAIN, "object":domain, "datatype": util::translate_datatype(&json!(domain)), "annotation":annotation}); @@ -869,19 +788,17 @@ pub fn translate_data_property_domain_axiom(v: &Value) -> Value { } pub fn translate_inverse_properties_axiom(v: &Value) -> Value { - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); + let (owl, annotation) = split_axiom(v); let lhs = property_translation::translate(&owl[1]); let rhs = property_translation::translate(&owl[2]); - let annotation = annotation_translation::translate_annotations(&annotations); let datatype = util::translate_datatype(&json!(rhs)); let triple = json!({"assertion":"1", "retraction":"0", "graph":"graph", //TODO "subject":lhs, - "predicate":"", + "predicate":OWL_INVERSE_OF, "object":rhs, "datatype": datatype, "annotation":annotation}); @@ -889,144 +806,128 @@ pub fn translate_inverse_properties_axiom(v: &Value) -> Value { } pub fn translate_functional_property_axiom(v: &Value) -> Value { - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); + let (owl, annotation) = split_axiom(v); let argument = property_translation::translate(&owl[1]); - let annotation = annotation_translation::translate_annotations(&annotations); let triple = json!({"assertion":"1", "retraction":"0", "graph":"graph", //TODO "subject":argument, - "predicate":"", - "object":"", + "predicate":RDF_TYPE, + "object":OWL_FUNCTIONAL_PROPERTY, "datatype": util::translate_datatype(&json!(argument)), "annotation":annotation}); triple } pub fn translate_inverse_functional_object_property_axiom(v: &Value) -> Value { - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); + let (owl, annotation) = split_axiom(v); let argument = property_translation::translate(&owl[1]); - let annotation = annotation_translation::translate_annotations(&annotations); let triple = json!({"assertion":"1", "retraction":"0", "graph":"graph", //TODO "subject":argument, - "predicate":"", - "object":"", + "predicate":RDF_TYPE, + "object":OWL_INVERSE_FUNCTIONAL_PROPERTY, "datatype": util::translate_datatype(&json!(argument)), "annotation":annotation}); triple } pub fn translate_reflexive_object_property_axiom(v: &Value) -> Value { - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); + let (owl, annotation) = split_axiom(v); let argument = property_translation::translate(&owl[1]); - let annotation = annotation_translation::translate_annotations(&annotations); let triple = json!({"assertion":"1", "retraction":"0", "graph":"graph", //TODO "subject":argument, - "predicate":"", - "object":"", + "predicate":RDF_TYPE, + "object":OWL_REFLECTIVE_PROPERTY, "datatype": util::translate_datatype(&json!(argument)), "annotation":annotation}); triple } pub fn translate_irreflexive_object_property_axiom(v: &Value) -> Value { - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); + let (owl, annotation) = split_axiom(v); let argument = property_translation::translate(&owl[1]); - let annotation = annotation_translation::translate_annotations(&annotations); let triple = json!({"assertion":"1", "retraction":"0", "graph":"graph", //TODO "subject":argument, - "predicate":"", - "object":"", + "predicate":RDF_TYPE, + "object":OWL_IRREFLEXIVE_PROPERTY, "datatype": util::translate_datatype(&json!(argument)), "annotation":annotation}); triple } pub fn translate_symmetric_object_property_axiom(v: &Value) -> Value { - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); + let (owl, annotation) = split_axiom(v); let argument = property_translation::translate(&owl[1]); - let annotation = annotation_translation::translate_annotations(&annotations); let triple = json!({"assertion":"1", "retraction":"0", "graph":"graph", //TODO "subject":argument, - "predicate":"", - "object":"", + "predicate":RDF_TYPE, + "object":OWL_SYMMETRIC_PROPERTY, "datatype": util::translate_datatype(&json!(argument)), "annotation":annotation}); triple } pub fn translate_asymmetric_object_property_axiom(v: &Value) -> Value { - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); + let (owl, annotation) = split_axiom(v); let argument = property_translation::translate(&owl[1]); - let annotation = annotation_translation::translate_annotations(&annotations); let triple = json!({"assertion":"1", "retraction":"0", "graph":"graph", //TODO "subject":argument, - "predicate":"", - "object":"", + "predicate":RDF_TYPE, + "object":OWL_ASYMMETRIC_PROPERTY, "datatype": util::translate_datatype(&json!(argument)), "annotation":annotation}); triple } pub fn translate_transitive_object_property_axiom(v: &Value) -> Value { - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); + let (owl, annotation) = split_axiom(v); let argument = property_translation::translate(&owl[1]); - let annotation = annotation_translation::translate_annotations(&annotations); let triple = json!({"assertion":"1", "retraction":"0", "graph":"graph", //TODO "subject":argument, - "predicate":"", - "object":"", + "predicate":RDF_TYPE, + "object":OWL_TRANSITIVE_PROPERTY, "datatype": util::translate_datatype(&json!(argument)), "annotation":annotation}); triple } pub fn translate_data_property_range_axiom(v: &Value) -> Value { - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); + let (owl, annotation) = split_axiom(v); let property = property_translation::translate(&owl[1]); let range = class_translation::translate(&owl[2]); - let annotation = annotation_translation::translate_annotations(&annotations); let triple = json!({"assertion":"1", "retraction":"0", "graph":"graph", //TODO "subject":property, - "predicate":"", + "predicate":RDFS_RANGE, "object":range, "datatype": util::translate_datatype(&json!(range)), "annotation":annotation}); @@ -1034,89 +935,81 @@ pub fn translate_data_property_range_axiom(v: &Value) -> Value { } pub fn translate_disjoint_properties_axiom(v: &Value) -> Value { - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); + let (owl, annotation) = split_axiom(v); let number_of_operands = (owl.as_array().unwrap())[1..].len(); if number_of_operands == 2 { let lhs = property_translation::translate(&owl[1]); let rhs = property_translation::translate(&owl[2]); - let annotation = annotation_translation::translate_annotations(&annotations); let triple = json!({ "assertion":"1", "retraction":"0", "graph":"graph", //TODO "subject":lhs, - "predicate":"", + "predicate":OWL_PROPERTY_DISJOINT_WITH, "object":rhs, "datatype":util::translate_datatype(&json!(rhs)), "annotation":annotation}); triple } else { let operands: Value = property_translation::translate_list(&(owl.as_array().unwrap())[1..]); - let annotation = annotation_translation::translate_annotations(&annotations); - - let blank_node = json!({"predicate":"", - "object": {"":[{"object":operands, "datatype":"_JSONLIST"}]}, - "datatype":"_JSONMAP"}); - let blank_sorted = util::sort_value(&blank_node); - let blank_string = blank_sorted.to_string(); + let blank_node = json!({"predicate":OWL_ALL_DISJOINT_PROPERTIES, + "object": {OWL_MEMBERS:[{"object":operands, "datatype":LDTAB_JSON_LIST}]}, + "datatype":LDTAB_JSON_MAP}); - let mut hasher = Sha256::new(); - hasher.update(blank_string.as_bytes()); - let blank_node_hash = hasher.finalize(); - let blank_node_id = format!("", blank_node_hash); + let blank_node_id = util::generate_blank_node_id(&blank_node); let triple = json!({"assertion":"1", "retraction":"0", "graph":"graph", //TODO "subject":blank_node_id, - "predicate":"", - "object": {"":[{"object":operands, "datatype":"_JSONLIST"}]}, //TODO remove datatype - "datatype":"_JSONMAP", + "predicate":OWL_ALL_DISJOINT_PROPERTIES, + "object": {OWL_MEMBERS:[{"object":operands, "datatype":LDTAB_JSON_LIST}]}, //TODO remove datatype + "datatype":LDTAB_JSON_MAP, "annotation":annotation}); triple } } pub fn translate_has_key_axiom(v: &Value) -> Value { - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); + let (owl, annotation) = split_axiom(v); - let operands: Value = property_translation::translate_list(&(owl.as_array().unwrap())[1..]); - let annotation = annotation_translation::translate_annotations(&annotations); - - let blank_node = json!({"predicate":"", - "object": {"":[{"object":operands, "datatype":"_JSONLIST"}]}, - "datatype": "_JSONMAP"}); + let class = class_translation::translate(&owl[1]); + let ops = property_translation::translate_list(&owl[2].as_array().unwrap()); + let dps = property_translation::translate_list(&owl[3].as_array().unwrap()); + + let mut operands = ops; + if !dps.is_null() { + if operands.is_null() { + operands = dps; + } else if operands.is_array() && dps.is_array() { + let mut ops_array = operands.as_array().unwrap().clone(); + let dps_array = dps.as_array().unwrap(); + ops_array.extend(dps_array.iter().cloned()); + operands = Value::Array(ops_array); + } + } - let blank_sorted = util::sort_value(&blank_node); - let blank_string = blank_sorted.to_string(); + //let operands: Value = property_translation::translate_list(&(owl.as_array().unwrap())[1..]); - let mut hasher = Sha256::new(); - hasher.update(blank_string.as_bytes()); - let blank_node_hash = hasher.finalize(); - let blank_node_id = format!("", blank_node_hash); let triple = json!({"assertion":"1", "retraction":"0", "graph":"graph", //TODO - "subject":blank_node_id, - "predicate":"", - "object": {"":operands, "datatype":"_JSONLIST"}, //TODO remove datatype - "datatype": "_JSONMAP", + "subject":class, + "predicate":OWL_HAS_KEY, + "object": operands, + "datatype": LDTAB_JSON_LIST, "annotation":annotation}); triple } pub fn translate_annotation_assertion_axiom(v: &Value) -> Value { //TODO: check order - let owl = annotation_translation::get_owl(v); - let annotations = annotation_translation::get_annotations(v); - let annotation = annotation_translation::translate_annotations(&annotations); + let (owl, annotation) = split_axiom(v); //if annotation_translation::has_annotation(v) { // println!("Input: {:?}",v); @@ -1154,19 +1047,19 @@ pub fn translate_annotation_assertion_axiom(v: &Value) -> Value { "subject":from, "predicate":property, "object":to, - "datatype":"_IRI", + "datatype":LDTAB_IRI, "annotation":annotation }); triple } } -fn merge_json(a: &mut Value, b: Value) { +fn _merge_json(a: &mut Value, b: Value) { match (a, b) { (Value::Object(a_map), Value::Object(b_map)) => { for (k, v) in b_map { // Merge if key exists, otherwise insert - merge_json(a_map.entry(k).or_insert(Value::Null), v); + _merge_json(a_map.entry(k).or_insert(Value::Null), v); } } (a, b) => { @@ -1183,7 +1076,7 @@ fn remove_meta(value: &mut Value) { Value::Object(map) => { // First, remove all "meta" keys that have the value "owl:Axiom" let mut keys_to_remove = Vec::new(); - for (k, v) in map.iter() { + for (k, _v) in map.iter() { if k == "meta" { keys_to_remove.push(k.clone()); } @@ -1228,29 +1121,21 @@ pub fn translate_rule(v: &Value) -> Value { map.remove("meta"); } - let mut blank_node = json!({"":[{"datatype":"_IRI", "object" :""}], - "":[{"datatype":"_JSONLIST", "object" : body}], - "":[{"datatype":"_JSONLIST", "object" :head}]}); - merge_json(&mut blank_node, anno_blan.clone()); - - let blank_sorted = util::sort_value(&blank_node); - let blank_string = blank_sorted.to_string(); - - //println!("blank_node: {}", blank_string); + let blank_node = json!({RDF_TYPE:[{"datatype":LDTAB_IRI, "object" :SWRL_IMP}], + SWRL_BODY:[{"datatype":LDTAB_JSON_LIST, "object" : body}], + SWRL_HEAD:[{"datatype":LDTAB_JSON_LIST, "object" :head}]}); + //merge_json(&mut blank_node, anno_blan.clone()); - let mut hasher = Sha256::new(); - hasher.update(blank_string.as_bytes()); - let blank_node_hash = hasher.finalize(); - let blank_node_id = format!("", blank_node_hash); + let blank_node_id = util::generate_blank_node_id(&blank_node); let blank_node_type = json!({ "assertion":"1", "retraction":"0", "graph":"graph", "subject": blank_node_id, - "predicate":"", - "object":"", - "datatype":"_IRI", + "predicate":RDF_TYPE, + "object":SWRL_IMP, + "datatype":LDTAB_IRI, "annotation": Value::Null }); @@ -1259,9 +1144,9 @@ pub fn translate_rule(v: &Value) -> Value { "retraction":"0", "graph":"graph", "subject": blank_node_id, - "predicate":"", + "predicate":SWRL_BODY, "object":body, - "datatype":"_JSONLIST", + "datatype":LDTAB_JSON_LIST, "annotation": Value::Null }); @@ -1270,9 +1155,9 @@ pub fn translate_rule(v: &Value) -> Value { "retraction":"0", "graph":"graph", "subject":blank_node_id, - "predicate":"", + "predicate":SWRL_HEAD, "object":head, - "datatype":"_JSONLIST", + "datatype":LDTAB_JSON_LIST, "annotation": Value::Null }); @@ -1313,9 +1198,9 @@ pub fn translate_ontology(v: &Value) -> Value { "retraction":"0", "graph":"graph", "subject":iri, - "predicate":"", + "predicate":OWL_VERSION_IRI, "object":viri, - "datatype":"_IRI", + "datatype":LDTAB_IRI, "annotation": Value::Null }); triple @@ -1329,9 +1214,9 @@ pub fn translate_doc_iri(v: &Value) -> Value { "retraction":"0", "graph":"graph", "subject":"ontology",//TODO - "predicate":"", + "predicate":OWL_VERSION_IRI, "object":iri, - "datatype":"_IRI", + "datatype":LDTAB_IRI, "annotation":"" }); triple diff --git a/src/ofn_2_ldtab/class_translation.rs b/src/ofn_2_ldtab/class_translation.rs index f8637e8..edac9d0 100644 --- a/src/ofn_2_ldtab/class_translation.rs +++ b/src/ofn_2_ldtab/class_translation.rs @@ -1,3 +1,4 @@ +use crate::constants::*; use crate::ofn_2_ldtab::property_translation; use crate::ofn_2_ldtab::util; use serde_json::json; @@ -75,9 +76,57 @@ pub fn translate_named_entity(v: &Value) -> Value { json!(o) } + +pub fn strip_rdf_literal(v: &Value) -> Value { + match v.as_str() { + Some(s) if s.starts_with('"') => { + let bytes = s.as_bytes(); + let mut i = 1usize; + let mut escaped = false; + + while i < bytes.len() { + match bytes[i] { + b'\\' if !escaped => { escaped = true; } + b'"' if !escaped => { + // Found closing quote + return json!(&s[1..i]); + } + _ => { escaped = false; } + } + i += 1; + } + // No closing quote found → return unchanged + json!(s) + } + Some(s) => json!(s), // not a quoted literal + None => v.clone(), // not a string + } +} + + pub fn get_object(v: &Value) -> Value { - let o: Value = translate(&v); + let mut o: Value = translate(v); let d: String = String::from(util::translate_datatype(&o).as_str().unwrap()); + if d == XSD_STRING || + d == XSD_BOOLEAN || + d == XSD_INTEGER || + d == XSD_DECIMAL || + d == XSD_NON_NEGATIVE_INTEGER || + d == XSD_NON_POSITIVE_INTEGER || + d == XSD_POSITIVE_INTEGER || + d == XSD_NEGATIVE_INTEGER || + d == XSD_LONG || + d == XSD_INT || + d == XSD_SHORT || + d == XSD_BYTE || + d == XSD_UNSIGNED_LONG || + d == XSD_UNSIGNED_INT || + d == XSD_UNSIGNED_SHORT || + d == XSD_UNSIGNED_BYTE || + d == OWL_REAL || + d == OWL_RATIONAL { + o = strip_rdf_literal(&o); + }; json!({"object" : o, "datatype" : d}) @@ -94,7 +143,7 @@ pub fn get_cardinality_object(v: &Value) -> Value { } else if s.chars().all(|c| c.is_ascii_digit()) { json!({ "object": s, - "datatype": "" + "datatype": XSD_NON_NEGATIVE_INTEGER }) } else { json!({ "error": "Invalid format" }) @@ -108,12 +157,12 @@ pub fn get_cardinality_object(v: &Value) -> Value { pub fn translate_some_values_from(v: &Value) -> Value { let property_o: Value = get_object(&v[1]); let filler_o: Value = get_object(&v[2]); - let type_o: Value = get_object(&json!("")); + let type_o: Value = get_object(&json!(OWL_RESTRICTION)); //build struct - let res: Value = json!({"" : vec![type_o], - "" : vec![property_o], - "" : vec![filler_o]}); + let res: Value = json!({RDF_TYPE : vec![type_o], + OWL_ON_PROPERTY : vec![property_o], + OWL_SOME_VALUES_FROM : vec![filler_o]}); //return type res @@ -122,37 +171,37 @@ pub fn translate_some_values_from(v: &Value) -> Value { pub fn translate_all_values_from(v: &Value) -> Value { let property_o: Value = get_object(&v[1]); let filler_o: Value = get_object(&v[2]); - let type_o: Value = get_object(&json!("")); + let type_o: Value = get_object(&json!(OWL_RESTRICTION)); - json!({"" : vec![type_o], - "" : vec![property_o], - "": vec![filler_o]}) + json!({RDF_TYPE : vec![type_o], + OWL_ON_PROPERTY : vec![property_o], + OWL_ALL_VALUES_FROM: vec![filler_o]}) } pub fn translate_has_value(v: &Value) -> Value { let property_o: Value = get_object(&v[1]); let filler_o: Value = get_object(&v[2]); - let type_o: Value = get_object(&json!("")); + let type_o: Value = get_object(&json!(OWL_RESTRICTION)); - json!({"" : vec![type_o], - "" : vec![property_o], - "" : vec![filler_o]}) + json!({RDF_TYPE : vec![type_o], + OWL_ON_PROPERTY : vec![property_o], + OWL_HAS_VALUE : vec![filler_o]}) } pub fn translate_has_self(v: &Value) -> Value { let property_o: Value = get_object(&v[1]); - let has_self_o: Value = get_object(&json!("true^^xsd:boolean")); - let type_o: Value = get_object(&json!("")); + let has_self_o: Value = get_object(&json!(format!("\"true\"^^{}", XSD_BOOLEAN))); + let type_o: Value = get_object(&json!(OWL_RESTRICTION)); - json!({"" : vec![type_o], - "" : vec![property_o], - "" : vec![has_self_o]}) + json!({RDF_TYPE : vec![type_o], + OWL_ON_PROPERTY : vec![property_o], + OWL_HAS_SELF : vec![has_self_o]}) } pub fn translate_object_min_cardinality(v: &Value) -> Value { let cardinality_o: Value = get_cardinality_object(&v[1]); let property_o: Value = get_object(&v[2]); - let type_o: Value = get_object(&json!("")); + let type_o: Value = get_object(&json!(OWL_RESTRICTION)); let ofn = v.as_array().unwrap(); let is_qualified = ofn.len() == 4; @@ -160,14 +209,14 @@ pub fn translate_object_min_cardinality(v: &Value) -> Value { if is_qualified { let filler_o: Value = get_object(&v[3]); - json!({"" : vec![type_o], - "" : vec![property_o], - "" : vec![cardinality_o], - "" : vec![filler_o]}) + json!({RDF_TYPE : vec![type_o], + OWL_ON_PROPERTY : vec![property_o], + OWL_MIN_QUALIFIED_CARDINALITY : vec![cardinality_o], + OWL_ON_CLASS : vec![filler_o]}) } else { - json!({"" : vec![type_o], - "" : vec![property_o], - "" : vec![cardinality_o]}) + json!({RDF_TYPE : vec![type_o], + OWL_ON_PROPERTY : vec![property_o], + OWL_MIN_CARDINALITY : vec![cardinality_o]}) } } @@ -176,17 +225,17 @@ pub fn translate_min_cardinality(v: &Value) -> Value { let cardinality_o: Value = get_cardinality_object(&v[1]); let property_o: Value = get_object(&v[2]); - let type_o: Value = get_object(&json!("")); + let type_o: Value = get_object(&json!(OWL_RESTRICTION)); - json!({"" : vec![type_o], - "" : vec![property_o], - "" : vec![cardinality_o]}) + json!({RDF_TYPE : vec![type_o], + OWL_ON_PROPERTY : vec![property_o], + OWL_MIN_CARDINALITY : vec![cardinality_o]}) } pub fn translate_data_min_cardinality(v: &Value) -> Value { let cardinality_o: Value = get_cardinality_object(&v[1]); let property_o: Value = get_object(&v[2]); - let type_o: Value = get_object(&json!("")); + let type_o: Value = get_object(&json!(OWL_RESTRICTION)); let ofn = v.as_array().unwrap(); let is_qualified = ofn.len() == 4; @@ -194,14 +243,14 @@ pub fn translate_data_min_cardinality(v: &Value) -> Value { if is_qualified { let filler_o: Value = get_object(&v[3]); - json!({"" : vec![type_o], - "" : vec![property_o], - "" : vec![cardinality_o], - "" : vec![filler_o]}) + json!({RDF_TYPE : vec![type_o], + OWL_ON_PROPERTY : vec![property_o], + OWL_MIN_QUALIFIED_CARDINALITY : vec![cardinality_o], + OWL_ON_DATA_RANGE : vec![filler_o]}) } else { - json!({"" : vec![type_o], - "" : vec![property_o], - "" : vec![cardinality_o]}) + json!({RDF_TYPE : vec![type_o], + OWL_ON_PROPERTY : vec![property_o], + OWL_MIN_CARDINALITY : vec![cardinality_o]}) } } @@ -209,28 +258,28 @@ pub fn translate_min_qualified_cardinality(v: &Value) -> Value { let property_o: Value = get_object(&v[1]); let cardinality_o: Value = get_cardinality_object(&v[2]); let filler_o: Value = get_object(&v[3]); - let type_o: Value = get_object(&json!("")); + let type_o: Value = get_object(&json!(OWL_RESTRICTION)); - json!({"" : vec![type_o], - "" : vec![property_o], - "" : vec![cardinality_o], - "" : vec![filler_o] }) + json!({RDF_TYPE : vec![type_o], + OWL_ON_PROPERTY : vec![property_o], + OWL_MIN_QUALIFIED_CARDINALITY : vec![cardinality_o], + OWL_ON_CLASS : vec![filler_o] }) } pub fn translate_max_cardinality(v: &Value) -> Value { let cardinality_o: Value = get_cardinality_object(&v[1]); let property_o: Value = get_object(&v[2]); - let type_o: Value = get_object(&json!("")); + let type_o: Value = get_object(&json!(OWL_RESTRICTION)); - json!({"" : vec![type_o], - "" : vec![property_o], - "" : vec![cardinality_o]}) + json!({RDF_TYPE : vec![type_o], + OWL_ON_PROPERTY : vec![property_o], + OWL_MAX_CARDINALITY : vec![cardinality_o]}) } pub fn translate_object_max_cardinality(v: &Value) -> Value { let cardinality_o: Value = get_cardinality_object(&v[1]); let property_o: Value = get_object(&v[2]); - let type_o: Value = get_object(&json!("")); + let type_o: Value = get_object(&json!(OWL_RESTRICTION)); let ofn = v.as_array().unwrap(); let is_qualified = ofn.len() == 4; @@ -238,21 +287,21 @@ pub fn translate_object_max_cardinality(v: &Value) -> Value { if is_qualified { let filler_o: Value = get_object(&v[3]); - json!({"" : vec![type_o], - "" : vec![property_o], - "" : vec![cardinality_o], - "" : vec![filler_o]}) + json!({RDF_TYPE : vec![type_o], + OWL_ON_PROPERTY : vec![property_o], + OWL_MAX_QUALIFIED_CARDINALITY : vec![cardinality_o], + OWL_ON_CLASS : vec![filler_o]}) } else { - json!({"" : vec![type_o], - "" : vec![property_o], - "" : vec![cardinality_o]}) + json!({RDF_TYPE : vec![type_o], + OWL_ON_PROPERTY : vec![property_o], + OWL_MAX_CARDINALITY : vec![cardinality_o]}) } } pub fn translate_data_max_cardinality(v: &Value) -> Value { let cardinality_o: Value = get_cardinality_object(&v[1]); let property_o: Value = get_object(&v[2]); - let type_o: Value = get_object(&json!("")); + let type_o: Value = get_object(&json!(OWL_RESTRICTION)); let ofn = v.as_array().unwrap(); let is_qualified = ofn.len() == 4; @@ -260,14 +309,14 @@ pub fn translate_data_max_cardinality(v: &Value) -> Value { if is_qualified { let filler_o: Value = get_object(&v[3]); - json!({"" : vec![type_o], - "" : vec![property_o], - "" : vec![cardinality_o], - "" : vec![filler_o]}) + json!({RDF_TYPE : vec![type_o], + OWL_ON_PROPERTY : vec![property_o], + OWL_MAX_QUALIFIED_CARDINALITY : vec![cardinality_o], + OWL_ON_DATA_RANGE : vec![filler_o]}) } else { - json!({"" : vec![type_o], - "" : vec![property_o], - "" : vec![cardinality_o]}) + json!({RDF_TYPE : vec![type_o], + OWL_ON_PROPERTY : vec![property_o], + OWL_MAX_CARDINALITY : vec![cardinality_o]}) } } @@ -275,28 +324,28 @@ pub fn translate_max_qualified_cardinality(v: &Value) -> Value { let property_o: Value = get_object(&v[1]); let cardinality_o: Value = get_cardinality_object(&v[2]); let filler_o: Value = get_object(&v[3]); - let type_o: Value = get_object(&json!("")); + let type_o: Value = get_object(&json!(OWL_RESTRICTION)); - json!({"" : vec![type_o], - "" : vec![property_o], - "" : vec![cardinality_o], - "" : vec![filler_o] }) + json!({RDF_TYPE : vec![type_o], + OWL_ON_PROPERTY : vec![property_o], + OWL_MAX_QUALIFIED_CARDINALITY : vec![cardinality_o], + OWL_ON_CLASS : vec![filler_o] }) } pub fn translate_exact_cardinality(v: &Value) -> Value { let cardinality_o: Value = get_cardinality_object(&v[1]); let property_o: Value = get_object(&v[2]); - let type_o: Value = get_object(&json!("")); + let type_o: Value = get_object(&json!(OWL_RESTRICTION)); - json!({"" : vec![type_o], - "" : vec![property_o], - "" : vec![cardinality_o]}) + json!({RDF_TYPE : vec![type_o], + OWL_ON_PROPERTY : vec![property_o], + OWL_CARDINALITY : vec![cardinality_o]}) } pub fn translate_object_exact_cardinality(v: &Value) -> Value { let cardinality_o: Value = get_cardinality_object(&v[1]); let property_o: Value = get_object(&v[2]); - let type_o: Value = get_object(&json!("")); + let type_o: Value = get_object(&json!(OWL_RESTRICTION)); let ofn = v.as_array().unwrap(); let is_qualified = ofn.len() == 4; @@ -304,21 +353,21 @@ pub fn translate_object_exact_cardinality(v: &Value) -> Value { if is_qualified { let filler_o: Value = get_object(&v[3]); - json!({"" : vec![type_o], - "" : vec![property_o], - "" : vec![cardinality_o], - "" : vec![filler_o]}) + json!({RDF_TYPE : vec![type_o], + OWL_ON_PROPERTY : vec![property_o], + OWL_QUALIFIED_CARDINALITY : vec![cardinality_o], + OWL_ON_CLASS : vec![filler_o]}) } else { - json!({"" : vec![type_o], - "" : vec![property_o], - "" : vec![cardinality_o]}) + json!({RDF_TYPE : vec![type_o], + OWL_ON_PROPERTY : vec![property_o], + OWL_CARDINALITY : vec![cardinality_o]}) } } pub fn translate_data_exact_cardinality(v: &Value) -> Value { let cardinality_o: Value = get_cardinality_object(&v[1]); let property_o: Value = get_object(&v[2]); - let type_o: Value = get_object(&json!("")); + let type_o: Value = get_object(&json!(OWL_RESTRICTION)); let ofn = v.as_array().unwrap(); let is_qualified = ofn.len() == 4; @@ -326,14 +375,14 @@ pub fn translate_data_exact_cardinality(v: &Value) -> Value { if is_qualified { let filler_o: Value = get_object(&v[3]); - json!({"" : vec![type_o], - "" : vec![property_o], - "" : vec![cardinality_o], - "" : vec![filler_o]}) + json!({RDF_TYPE : vec![type_o], + OWL_ON_PROPERTY : vec![property_o], + OWL_QUALIFIED_CARDINALITY : vec![cardinality_o], + OWL_ON_DATA_RANGE : vec![filler_o]}) } else { - json!({"" : vec![type_o], - "" : vec![property_o], - "" : vec![cardinality_o]}) + json!({RDF_TYPE : vec![type_o], + OWL_ON_PROPERTY : vec![property_o], + OWL_CARDINALITY : vec![cardinality_o]}) } } @@ -343,12 +392,12 @@ pub fn translate_exact_qualified_cardinality(v: &Value) -> Value { let property_o: Value = get_object(&v[1]); let cardinality_o: Value = get_cardinality_object(&v[2]); let filler_o: Value = get_object(&v[3]); - let type_o: Value = get_object(&json!("")); + let type_o: Value = get_object(&json!(OWL_RESTRICTION)); - json!({"" : vec![type_o], - "" : vec![property_o], - "" : vec![cardinality_o], - "" : vec![filler_o] }) + json!({RDF_TYPE : vec![type_o], + OWL_ON_PROPERTY : vec![property_o], + OWL_QUALIFIED_CARDINALITY : vec![cardinality_o], + OWL_ON_CLASS : vec![filler_o] }) } pub fn translate_list(v: &[Value]) -> Value { @@ -366,12 +415,12 @@ 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" : "_JSONLIST"}); + "datatype" : LDTAB_JSON_LIST}); - let type_o: Value = get_object(&json!("")); + let type_o: Value = get_object(&json!(OWL_CLASS)); - json!({"" : vec![type_o], - "" : vec![operands_o]}) + json!({RDF_TYPE : vec![type_o], + OWL_INTERSECTION_OF : vec![operands_o]}) } pub fn translate_union_of(v: &Value) -> Value { @@ -379,11 +428,11 @@ pub fn translate_union_of(v: &Value) -> Value { //let operands_o : Value = get_object(operands); let operands_o: Value = json!({"object" : operands, - "datatype" : "_JSONLIST"}); - let type_o: Value = get_object(&json!("")); + "datatype" : LDTAB_JSON_LIST}); + let type_o: Value = get_object(&json!(OWL_CLASS)); - json!({"" : vec![type_o], - "" : vec![operands_o]}) + json!({RDF_TYPE : vec![type_o], + OWL_UNION_OF : vec![operands_o]}) } pub fn translate_one_of(v: &Value) -> Value { @@ -391,17 +440,17 @@ pub fn translate_one_of(v: &Value) -> Value { //let operands_o : Value = get_object(operands); let operands_o: Value = json!({"object" : operands, - "datatype" : "_JSONLIST"}); - let type_o: Value = get_object(&json!("")); + "datatype" : LDTAB_JSON_LIST}); + let type_o: Value = get_object(&json!(OWL_CLASS)); - json!({"" : vec![type_o], - "" : vec![operands_o]}) + json!({RDF_TYPE : vec![type_o], + OWL_ONE_OF : vec![operands_o]}) } pub fn translate_complement_of(v: &Value) -> Value { let argument_o: Value = get_object(&v[1]); - let type_o: Value = get_object(&json!("")); + let type_o: Value = get_object(&json!(OWL_CLASS)); - json!({"" : vec![type_o], - "" : vec![argument_o]}) + json!({RDF_TYPE : vec![type_o], + OWL_COMPLEMENT_OF : vec![argument_o]}) } diff --git a/src/ofn_2_ldtab/property_translation.rs b/src/ofn_2_ldtab/property_translation.rs index 5732dfb..93cb605 100644 --- a/src/ofn_2_ldtab/property_translation.rs +++ b/src/ofn_2_ldtab/property_translation.rs @@ -1,3 +1,4 @@ +use crate::constants::*; use crate::ofn_2_ldtab::util; use serde_json::json; use serde_json::Value; @@ -22,7 +23,7 @@ pub fn translate_inverse_of(v: &Value) -> Value { let argument_o: Value = json!({"object" : argument, "datatype" : String::from(util::translate_datatype(&argument).as_str().unwrap())}); - json!({"" : vec![argument_o]}) + json!({OWL_INVERSE_OF : vec![argument_o]}) } pub fn get_object(v: &Value) -> Value { diff --git a/src/ofn_2_ldtab/rule_translation.rs b/src/ofn_2_ldtab/rule_translation.rs index 8e3a7c7..cbbf2bc 100644 --- a/src/ofn_2_ldtab/rule_translation.rs +++ b/src/ofn_2_ldtab/rule_translation.rs @@ -1,8 +1,7 @@ -use crate::ofn_2_ldtab::class_translation; -use crate::ofn_2_ldtab::property_translation; +use crate::constants::*; use crate::ofn_2_ldtab::util; use serde_json::json; -use serde_json::{Map, Value}; +use serde_json::Value; pub fn translate(v: &Value) -> Value { match v[0].as_str() { @@ -10,19 +9,19 @@ pub fn translate(v: &Value) -> Value { Some("Head") => translate_head(v), Some("ObjectPropertyAtom") => translate_object_property_atom(v), Some("Variable") => translate_variable(v), + Some("ClassAtom") => translate_class_atom(v), + Some("SameIndividualAtom") => translate_same_individual_atom(v), + Some("DifferentIndividualsAtom") => translate_different_individuals_atom(v), + Some("DataRangeAtom") => translate_data_range_atom(v), + Some("BuiltInAtom") => translate_builtin_atom(v), + Some(_) => { println!("Error: {}", v); json!("TODO")} , //None => owl::OWL::Named(String::from(v.as_str().unwrap())), - //Some("Variable") => axiom_translation::translate_ontology(v), - //Some("SameIndividualAtom") => axiom_translation::translate_ontology(v), - //Some("DifferentIndividualsAtom") => axiom_translation::translate_ontology(v), - //Some("DataRangeAtom") => axiom_translation::translate_ontology(v), - //Some("ClassAtom") => axiom_translation::translate_ontology(v), - //Some("BuiltInAtom") => axiom_translation::translate_ontology(v), - // + None => translate_named_entity(&v), } } @@ -48,16 +47,86 @@ pub fn translate_variable(v: &Value) -> Value { translate_named_entity(&v[1]) } +pub fn translate_same_individual_atom(v: &Value) -> Value { + let type_o = get_object(&json!(SWRL_SAME_INDIVIDUAL_ATOM)); + let arg1_o = get_object(&v[1]); + let arg2_o = get_object(&v[2]); + json!( {"datatype" : LDTAB_JSON_MAP, + "object": {RDF_TYPE : vec![type_o], + SWRL_ARGUMENT1 : vec![arg1_o], + SWRL_ARGUMENT2 : vec![arg2_o]}}) +} + +pub fn translate_different_individuals_atom(v: &Value) -> Value { + let type_o = get_object(&json!(SWRL_DIFFERENT_INDIVIDUALS_ATOM)); + let arg1_o = get_object(&v[1]); + let arg2_o = get_object(&v[2]); + json!( {"datatype" : LDTAB_JSON_MAP, + "object": {RDF_TYPE : vec![type_o], + SWRL_ARGUMENT1 : vec![arg1_o], + SWRL_ARGUMENT2 : vec![arg2_o]}}) +} + +pub fn translate_data_range_atom(v: &Value) -> Value { + let type_o = get_object(&json!(SWRL_DATA_RANGE_ATOM)); + let datarange_o = get_object(&v[1]); + let arg1_o = get_object(&v[2]); + json!( {"datatype" : LDTAB_JSON_MAP, + "object": {RDF_TYPE : vec![type_o], + SWRL_DATA_RANGE : vec![datarange_o], + SWRL_ARGUMENT1 : vec![arg1_o]}}) +} + +pub fn translate_data_property_atom(v: &Value) -> Value { + let type_o = get_object(&json!(SWRL_DATAVALUED_PROPERTY_ATOM)); + let property_o = get_object(&v[1]); + let arg1_o = get_object(&v[2]); + let arg2_o = get_object(&v[3]); + json!( {"datatype" : LDTAB_JSON_MAP, + "object": {RDF_TYPE : vec![type_o], + SWRL_PROPERTY_PREDICATE : vec![property_o], + SWRL_ARGUMENT1 : vec![arg1_o], + SWRL_ARGUMENT2 : vec![arg2_o]}}) +} + +pub fn translate_builtin_atom(v: &Value) -> Value { + let type_o = get_object(&json!(SWRL_BUILTIN_ATOM)); + let builtin_o = get_object(&v[1]); + + let array = v.as_array().unwrap(); + let args = array[2..].to_vec(); + + let mut arg_objects = Vec::new(); + for arg in args.iter() { + arg_objects.push(get_object(arg)); + } + + json!( {"datatype" : LDTAB_JSON_MAP, + "object": {RDF_TYPE : vec![type_o], + SWRL_BUILTIN : vec![builtin_o], + SWRL_ARGUMENTS : arg_objects}}) +} + +pub fn translate_class_atom(v: &Value) -> Value { + let type_o = get_object(&json!(SWRL_CLASS_ATOM)); + let class_o = get_object(&v[1]); + let arg1_o = get_object(&v[2]); + json!( {"datatype" : LDTAB_JSON_MAP, + "object": {RDF_TYPE : vec![type_o], + SWRL_CLASS_PREDICATE : vec![class_o], + SWRL_ARGUMENT1 : vec![arg1_o]}}) +} + pub fn translate_object_property_atom(v: &Value) -> Value { - let type_o = get_object(&json!("")); + let type_o = get_object(&json!(SWRL_INDIVIDUAL_PROPERTY_ATOM)); let property_o = get_object(&v[1]); let arg1_o = get_object(&v[2]); let arg2_o = get_object(&v[3]); - json!( {"datatype" : "_JSONMAP", - "object": {"" : vec![type_o], - "" : vec![property_o], - "" : vec![arg1_o], - "" : vec![arg2_o]}}) + json!( {"datatype" : LDTAB_JSON_MAP, + "object": {RDF_TYPE : vec![type_o], + SWRL_PROPERTY_PREDICATE : vec![property_o], + SWRL_ARGUMENT1 : vec![arg1_o], + SWRL_ARGUMENT2 : vec![arg2_o]}}) } pub fn translate_body(v: &Value) -> Value { diff --git a/src/ofn_2_ldtab/translation.rs b/src/ofn_2_ldtab/translation.rs index 861d267..9a95127 100644 --- a/src/ofn_2_ldtab/translation.rs +++ b/src/ofn_2_ldtab/translation.rs @@ -13,12 +13,13 @@ use serde_json::Value; /// println!("{}", thick_triple); /// pub fn ofn_2_thick_triple(v: &Value) -> Value { + let ldtab_triple = match v[0].as_str() { Some("Declaration") => axiom_translation::translate_declaration(v), Some("DatatypeDefinition") => axiom_translation::translate_datatype_definition(v), Some("SubClassOf") => axiom_translation::translate_subclass_of_axiom(v), Some("DisjointClasses") => axiom_translation::translate_disjoint_classes_axiom(v), - Some("DisjointUnionOf") => axiom_translation::translate_disjoint_union_of_axiom(v), + Some("DisjointUnion") => axiom_translation::translate_disjoint_union_of_axiom(v), Some("EquivalentClasses") => axiom_translation::translate_equivalent_classes_axiom(v), Some("SubObjectPropertyOf") => axiom_translation::translate_sub_object_property(v), Some("SubDataPropertyOf") => axiom_translation::translate_sub_data_property(v), @@ -73,7 +74,7 @@ pub fn ofn_2_thick_triple(v: &Value) -> Value { axiom_translation::translate_negative_object_property_assertion_axiom(v) } Some("DataPropertyAssertion") => { - axiom_translation::translate_object_property_assertion_axiom(v) + axiom_translation::translate_data_property_assertion_axiom(v) } Some("NegativeDataPropertyAssertion") => { axiom_translation::translate_negative_data_property_assertion_axiom(v) @@ -97,7 +98,7 @@ pub fn ofn_2_thick_triple(v: &Value) -> Value { Some("DLSafeRule") => axiom_translation::translate_rule(v), - Some(_) => panic!(), + Some(_) => panic!("Unsupported axiom type: {}", v[0]), None => panic!(), }; diff --git a/src/ofn_2_ldtab/util.rs b/src/ofn_2_ldtab/util.rs index 74cefcc..09b2da2 100644 --- a/src/ofn_2_ldtab/util.rs +++ b/src/ofn_2_ldtab/util.rs @@ -1,38 +1,55 @@ +use crate::constants::*; use regex::Regex; use serde_json::json; use serde_json::{Map, Value}; +use sha2::{Digest, Sha256}; use std::collections::HashMap; +use std::sync::LazyLock; + +static RE_LANGUAGE_TAG: LazyLock = + LazyLock::new(|| Regex::new("^\"(.+)\"@(.*)$").unwrap()); +static RE_DATATYPE: LazyLock = + LazyLock::new(|| Regex::new("^\"(.+)\"\\^\\^(.*)$").unwrap()); +static RE_LITERAL: LazyLock = + LazyLock::new(|| Regex::new("^\"(.+)\"(.*)$").unwrap()); +static RE_URI: LazyLock = + LazyLock::new(|| Regex::new("^<(.+)>$").unwrap()); +static RE_CURIE: LazyLock = + LazyLock::new(|| Regex::new("^(.+):(.+)$").unwrap()); + +/// Generates a blank node ID for an LDTab object. +pub fn generate_blank_node_id(v: &Value) -> String { + let sorted = sort_value(v); + let s = sorted.to_string(); + let mut hasher = Sha256::new(); + hasher.update(s.as_bytes()); + let hash = hasher.finalize(); + format!("", hash) +} pub fn translate_literal(s: &str) -> Value { - let language_tag = Regex::new("^\"(.+)\"@(.*)$").unwrap(); - let datatype = Regex::new("^\"(.+)\"\\^\\^(.*)$").unwrap(); - - if language_tag.is_match(s) { - match language_tag.captures(s) { + if RE_LANGUAGE_TAG.is_match(s) { + match RE_LANGUAGE_TAG.captures(s) { Some(x) => json!(format!("@{}", &x[2])), None => json!("Error"), } - } else if datatype.is_match(s) { - match datatype.captures(s) { + } else if RE_DATATYPE.is_match(s) { + match RE_DATATYPE.captures(s) { Some(x) => json!(format!("{}", &x[2])), None => json!("Error"), } } else { - json!("_plain") + json!(XSD_STRING) } } pub fn translate_string(s: &str) -> Value { - let literal = Regex::new("^\"(.+)\"(.*)$").unwrap(); - let uri = Regex::new("^<(.+)>$").unwrap(); - let curie = Regex::new("^(.+):(.+)$").unwrap(); - - if literal.is_match(s) { + if RE_LITERAL.is_match(s) { translate_literal(s) - } else if uri.is_match(s) { - json!("_IRI") - } else if curie.is_match(s) { - json!("_IRI") + } else if RE_URI.is_match(s) { + json!(LDTAB_IRI) + } else if RE_CURIE.is_match(s) { + json!(LDTAB_IRI) } else { json!("ERROR") } @@ -41,8 +58,8 @@ pub fn translate_string(s: &str) -> Value { pub fn translate_datatype(v: &Value) -> Value { match v { Value::String(s) => translate_string(&s), - Value::Array(_x) => json!("_JSONLIST"), - Value::Object(_x) => json!("_JSONMAP"), + Value::Array(_x) => json!(LDTAB_JSON_LIST), + Value::Object(_x) => json!(LDTAB_JSON_MAP), _ => json!("error"), } //check array & object @@ -67,15 +84,15 @@ pub fn sort_object(v: &Map) -> Value { //sort nested values for (key, value) in v.iter() { - let mut sorted_value = Value::Null; + let sorted_value; if key == "object" && v.contains_key("datatype") - && v.get("datatype").unwrap() == &json!("_JSONLIST") + && v.get("datatype").unwrap() == &json!(LDTAB_JSON_LIST) { //check if value is none match value.as_array() { - Some(val) => {} + Some(_val) => {} None => { println!("NOT AN ARRAY {:?}", value); } diff --git a/src/ofn_2_man/axiom_translation.rs b/src/ofn_2_man/axiom_translation.rs index a15bb6f..08de7be 100644 --- a/src/ofn_2_man/axiom_translation.rs +++ b/src/ofn_2_man/axiom_translation.rs @@ -35,7 +35,7 @@ pub fn translate_disjoint_union_of_axiom(v : &Value) -> String { let lhs: String = class_translation::translate(&v[1]); let operands: Vec = (&(v.as_array().unwrap())[2..]).into_iter().map(|x| class_translation::translate(&x)).collect(); let merged = operands.join(", "); - format!("Class: {} DisjointUnionOf: {}", lhs, merged) + format!("Class: {} DisjointUnion: {}", lhs, merged) } diff --git a/src/ofn_2_man/translation.rs b/src/ofn_2_man/translation.rs index 4c247d3..5b82bd3 100644 --- a/src/ofn_2_man/translation.rs +++ b/src/ofn_2_man/translation.rs @@ -18,7 +18,7 @@ pub fn ofn_2_man(v : &Value) -> String { match v[0].as_str() { Some("SubClassOf") => axiom_translation::translate_subclass_of_axiom(v), Some("DisjointClasses") => axiom_translation::translate_disjoint_classes_axiom(v), - Some("DisjointUnionOf") => axiom_translation::translate_disjoint_union_of_axiom(v), + Some("DisjointUnion") => axiom_translation::translate_disjoint_union_of_axiom(v), Some("EquivalentClasses") => axiom_translation::translate_equivalent_classes_axiom(v), Some("ThinTriple") => axiom_translation::translate_thin_triple(v), diff --git a/src/ofn_2_rdfa/axiom_translation.rs b/src/ofn_2_rdfa/axiom_translation.rs index cdb9ebe..62a88a9 100644 --- a/src/ofn_2_rdfa/axiom_translation.rs +++ b/src/ofn_2_rdfa/axiom_translation.rs @@ -1,3 +1,4 @@ +use crate::constants::*; use serde_json::{Value}; use serde_json::json; use std::collections::HashMap; @@ -28,20 +29,20 @@ pub fn is_named_class(ofn: &Value) -> bool { pub fn get_type(ofn: &Value) -> &str { match ofn[0].as_str() { - Some("ObjectSomeValuesFrom") => "owl:Restriction", - Some("ObjectAllValuesFrom") => "owl:Restriction", - Some("ObjectHasValue") => "owl:Restriction", - Some("ObjectMinCardinality") => "owl:Restriction", - Some("ObjectMinQualifiedCardinality") => "owl:Restriction", - Some("ObjectMaxCardinality") => "owl:Restriction", - Some("ObjectMaxQualifiedCardinality") => "owl:Restriction", - Some("ObjectExactCardinality") => "owl:Restriction", - Some("ObjectExactQualifiedCardinality") => "owl:Restriction", - Some("ObjectHasSelf") => "owl:Restriction", - Some("ObjectIntersectionOf") => "owl:Class", - Some("ObjectUnionOf") => "owl:Class", - Some("ObjectOneOf") => "owl:Class", - Some("ObjectComplementOf") => "owl:Class", + Some("ObjectSomeValuesFrom") => OWL_RESTRICTION, + Some("ObjectAllValuesFrom") => OWL_RESTRICTION, + Some("ObjectHasValue") => OWL_RESTRICTION, + Some("ObjectMinCardinality") => OWL_RESTRICTION, + Some("ObjectMinQualifiedCardinality") => OWL_RESTRICTION, + Some("ObjectMaxCardinality") => OWL_RESTRICTION, + Some("ObjectMaxQualifiedCardinality") => OWL_RESTRICTION, + Some("ObjectExactCardinality") => OWL_RESTRICTION, + Some("ObjectExactQualifiedCardinality") => OWL_RESTRICTION, + Some("ObjectHasSelf") => OWL_RESTRICTION, + Some("ObjectIntersectionOf") => OWL_CLASS, + Some("ObjectUnionOf") => OWL_CLASS, + Some("ObjectOneOf") => OWL_CLASS, + Some("ObjectComplementOf") => OWL_CLASS, None => ofn.as_str().unwrap(), Some(_) => panic!(), } @@ -59,6 +60,6 @@ pub fn type_opening(ofn: &Value) -> Value { pub fn translate_subclass_of_axiom(sub: &Value, sup: &Value, subject_2_label: &HashMap) -> Value { let opening = type_opening(sub); let sub_class = class_translation::translate(sub, subject_2_label, None); - let sup_class = class_translation::translate(sup, subject_2_label, Some("rdfs:subClassOf")); + let sup_class = class_translation::translate(sup, subject_2_label, Some(RDFS_SUB_CLASS_OF)); json!(["div", opening, sub_class, " SubClassOf ", sup_class]) } diff --git a/src/ofn_2_rdfa/class_translation.rs b/src/ofn_2_rdfa/class_translation.rs index ab929a1..0cd7da9 100644 --- a/src/ofn_2_rdfa/class_translation.rs +++ b/src/ofn_2_rdfa/class_translation.rs @@ -1,3 +1,4 @@ +use crate::constants::*; use serde_json::{Value}; use serde_json::json; use std::collections::HashMap; @@ -7,20 +8,20 @@ use std::collections::HashMap; pub fn get_type(ofn: &Value) -> &str { match ofn[0].as_str() { - Some("ObjectSomeValuesFrom") => "owl:Restriction", - Some("ObjectAllValuesFrom") => "owl:Restriction", - Some("ObjectHasValue") => "owl:Restriction", - Some("ObjectMinCardinality") => "owl:Restriction", - Some("ObjectMinQualifiedCardinality") => "owl:Restriction", - Some("ObjectMaxCardinality") => "owl:Restriction", - Some("ObjectMaxQualifiedCardinality") => "owl:Restriction", - Some("ObjectExactCardinality") => "owl:Restriction", - Some("ObjectExactQualifiedCardinality") => "owl:Restriction", - Some("ObjectHasSelf") => "owl:Restriction", - Some("ObjectIntersectionOf") => "owl:Class", - Some("ObjectUnionOf") => "owl:Class", - Some("ObjectOneOf") => "owl:Class", - Some("ObjectComplementOf") => "owl:Class", + Some("ObjectSomeValuesFrom") => OWL_RESTRICTION, + Some("ObjectAllValuesFrom") => OWL_RESTRICTION, + Some("ObjectHasValue") => OWL_RESTRICTION, + Some("ObjectMinCardinality") => OWL_RESTRICTION, + Some("ObjectMinQualifiedCardinality") => OWL_RESTRICTION, + Some("ObjectMaxCardinality") => OWL_RESTRICTION, + Some("ObjectMaxQualifiedCardinality") => OWL_RESTRICTION, + Some("ObjectExactCardinality") => OWL_RESTRICTION, + Some("ObjectExactQualifiedCardinality") => OWL_RESTRICTION, + Some("ObjectHasSelf") => OWL_RESTRICTION, + Some("ObjectIntersectionOf") => OWL_CLASS, + Some("ObjectUnionOf") => OWL_CLASS, + Some("ObjectOneOf") => OWL_CLASS, + Some("ObjectComplementOf") => OWL_CLASS, Some(_) => ofn.as_str().unwrap(), None => panic!(), } @@ -116,23 +117,23 @@ pub fn translate_list(arguments: Vec, subject_2_label: &HashMap json!(["span",{"property":p},["span",{"property":"owl:intersectionOf","typeof":"rdf:List"},ops]]), - None => json!(["span",["span",{"property":"owl:intersectionOf","typeof":"rdf:List"},ops]]) + Some(p) => json!(["span",{"property":p},["span",{"property":OWL_INTERSECTION_OF,"typeof":RDF_LIST},ops]]), + None => json!(["span",["span",{"property":OWL_INTERSECTION_OF,"typeof":RDF_LIST},ops]]) } } @@ -161,8 +162,8 @@ pub fn translate_union_of(ofn: &Value, subject_2_label: &HashMap, let ops = translate_list(operands, subject_2_label, modifier); match rdfa_property { - Some(p) => json!(["span",{"property":p},["span",{"property":"owl:unionOf","typeof":"rdf:List"},ops]]), - None => json!(["span",["span",{"property":"owl:unionOf","typeof":"rdf:List"},ops]]) + Some(p) => json!(["span",{"property":p},["span",{"property":OWL_UNION_OF,"typeof":RDF_LIST},ops]]), + None => json!(["span",["span",{"property":OWL_UNION_OF,"typeof":RDF_LIST},ops]]) } } @@ -175,8 +176,8 @@ pub fn translate_one_of(ofn: &Value, subject_2_label: &HashMap, r //TODO: we need to check for OneOf operator when adding parenthesis //currently, we translate ObjectOneOf(a,b,c) to ({a,b,c}) instead of {a,b,c} match rdfa_property { - Some(p) => json!(["span",{"property":p},["span",{"property":"owl:oneOf","typeof":"rdf:List"},"{",ops,"}"]]), - None => json!(["span",["span",{"property":"owl:oneOf","typeof":"rdf:List"},"{",ops,"}"]]) + Some(p) => json!(["span",{"property":p},["span",{"property":OWL_ONE_OF,"typeof":RDF_LIST},"{",ops,"}"]]), + None => json!(["span",["span",{"property":OWL_ONE_OF,"typeof":RDF_LIST},"{",ops,"}"]]) } } @@ -185,16 +186,16 @@ pub fn translate_one_of(ofn: &Value, subject_2_label: &HashMap, r pub fn render_restriction_base(prop: &Value, modifier: &Value, filler: &Value, rdfa_property: Option<&str>) -> Value { match rdfa_property { - Some(p) => json!(["span", {"property":p, "typeof":"owl:Restriction"}, prop, modifier, filler]), - None => json!(["span",{"typeof":"owl:Restriction"}, prop, modifier, filler]), + Some(p) => json!(["span", {"property":p, "typeof":OWL_RESTRICTION}, prop, modifier, filler]), + None => json!(["span",{"typeof":OWL_RESTRICTION}, prop, modifier, filler]), } } pub fn render_restriction_nested(prop: &Value, modifier: &Value, filler: &Value, rdfa_property: Option<&str>) -> Value { match rdfa_property { - Some(p) => json!(["span", {"property":p, "typeof":"owl:Restriction"}, prop, modifier, "(", filler, ")"]), - None => json!(["span",{"typeof":"owl:Restriction"}, prop, modifier, "(", filler, ")"]), + Some(p) => json!(["span", {"property":p, "typeof":OWL_RESTRICTION}, prop, modifier, "(", filler, ")"]), + None => json!(["span",{"typeof":OWL_RESTRICTION}, prop, modifier, "(", filler, ")"]), } } @@ -205,8 +206,8 @@ pub fn render_qualified_cardinality_restriction_base(prop: &Value, rdfa_property: Option<&str>) -> Value { match rdfa_property { - Some(p) => json!(["span", {"property":p, "typeof":"owl:Restriction"}, prop, modifier, cardinality, " ", filler]), - None => json!(["span",{"typeof":"owl:Restriction"}, prop, modifier, cardinality, " ", filler]), + Some(p) => json!(["span", {"property":p, "typeof":OWL_RESTRICTION}, prop, modifier, cardinality, " ", filler]), + None => json!(["span",{"typeof":OWL_RESTRICTION}, prop, modifier, cardinality, " ", filler]), } } @@ -217,8 +218,8 @@ pub fn render_qualified_cardinality_restriction_nested(prop: &Value, rdfa_property: Option<&str>) -> Value { match rdfa_property { - Some(p) => json!(["span", {"property":p, "typeof":"owl:Restriction"}, prop, modifier, cardinality, " ", "(", filler, ")"]), - None => json!(["span",{"typeof":"owl:Restriction"}, prop, modifier, cardinality, " ", "(", filler, ")"]), + Some(p) => json!(["span", {"property":p, "typeof":OWL_RESTRICTION}, prop, modifier, cardinality, " ", "(", filler, ")"]), + None => json!(["span",{"typeof":OWL_RESTRICTION}, prop, modifier, cardinality, " ", "(", filler, ")"]), } } @@ -257,7 +258,7 @@ pub fn label_substitution(named_class: &str, subject_2_label: &HashMap, rdfa_property: Option<&str>) -> Value { //TODO: use propertytranslation? - let argument = translate(&ofn[1], subject_2_label, Some("owl:complementOf")); + let argument = translate(&ofn[1], subject_2_label, Some(OWL_COMPLEMENT_OF)); if ofn[1].is_array() { match rdfa_property { @@ -275,7 +276,7 @@ pub fn translate_complement_of(ofn: &Value, subject_2_label: &HashMap, rdfa_property: Option<&str>) -> Value { //TODO: use propertytranslation? - let argument = translate(&ofn[1], subject_2_label, Some("owl:inverseOf")); + let argument = translate(&ofn[1], subject_2_label, Some(OWL_INVERSE_OF)); if ofn[1].is_array() { match rdfa_property { @@ -294,9 +295,9 @@ pub fn translate_inverse_of(ofn: &Value, subject_2_label: &HashMap, rdfa_property: Option<&str>) -> Value { //TODO: use propertytranslation? - let property = translate(&ofn[1], subject_2_label, Some("owl:onProperty")); + let property = translate(&ofn[1], subject_2_label, Some(OWL_ON_PROPERTY)); let modifier = Value::String(String::from(" some ")); - let filler = translate(&ofn[2], subject_2_label, Some("owl:someValuesFrom")); + let filler = translate(&ofn[2], subject_2_label, Some(OWL_SOME_VALUES_FROM)); //check whether the filler of this expression is atomic or nested further if ofn[2].is_array() { @@ -310,9 +311,9 @@ pub fn translate_some_values_from(ofn: &Value, subject_2_label: &HashMap, rdfa_property: Option<&str>) -> Value { //TODO: use propertytranslation? - let property = translate(&ofn[1], subject_2_label, Some("owl:onProperty")); + let property = translate(&ofn[1], subject_2_label, Some(OWL_ON_PROPERTY)); let modifier = Value::String(String::from(" value ")); - let filler = translate(&ofn[2], subject_2_label, Some("owl:hasValue")); + let filler = translate(&ofn[2], subject_2_label, Some(OWL_HAS_VALUE)); //check whether the filler of this expression is atomic or nested further if ofn[2].is_array() { @@ -326,9 +327,9 @@ pub fn translate_has_value(ofn: &Value, subject_2_label: &HashMap pub fn translate_has_self(ofn: &Value, subject_2_label: &HashMap, rdfa_property: Option<&str>) -> Value { //TODO: use propertytranslation? - let property = translate(&ofn[1], subject_2_label, Some("owl:onProperty")); + let property = translate(&ofn[1], subject_2_label, Some(OWL_ON_PROPERTY)); let modifier = Value::String(String::from(" some Self ")); - let filler = json!(["span", {"property":"owl:hasSelf", "hidden":"true"}, "true^^xsd:boolean"]); + let filler = json!(["span", {"property":OWL_HAS_SELF, "hidden":"true"}, format!("true^^{XSD_BOOLEAN}")]); //check whether the filler of this expression is atomic or nested further if ofn[2].is_array() { @@ -342,9 +343,9 @@ pub fn translate_has_self(ofn: &Value, subject_2_label: &HashMap, pub fn translate_all_values_from(ofn: &Value, subject_2_label: &HashMap, rdfa_property: Option<&str>) -> Value { //TODO: use propertytranslation? - let property = translate(&ofn[1], subject_2_label, Some("owl:onProperty")); + let property = translate(&ofn[1], subject_2_label, Some(OWL_ON_PROPERTY)); let modifier = Value::String(String::from(" only ")); - let filler = translate(&ofn[2], subject_2_label, Some("owl:allValuesFrom")); + let filler = translate(&ofn[2], subject_2_label, Some(OWL_ALL_VALUES_FROM)); if ofn[2].is_array() { render_restriction_nested(&property, &modifier, &filler, rdfa_property) @@ -356,7 +357,7 @@ pub fn translate_all_values_from(ofn: &Value, subject_2_label: &HashMap, rdfa_property: Option<&str>) -> Value { //TODO: use propertytranslation? - let property = translate(&ofn[1], subject_2_label, Some("owl:onProperty")); + let property = translate(&ofn[1], subject_2_label, Some(OWL_ON_PROPERTY)); let modifier = Value::String(String::from(" min ")); //encode cardinality @@ -364,9 +365,9 @@ pub fn translate_min_cardinality(ofn: &Value, subject_2_label: &HashMap, rdfa_property: Option<&str>) -> Value { //TODO: use propertytranslation? - let property = translate(&ofn[1], subject_2_label, Some("owl:onProperty")); + let property = translate(&ofn[1], subject_2_label, Some(OWL_ON_PROPERTY)); let modifier = Value::String(String::from(" min ")); //encode cardinality @@ -386,9 +387,9 @@ pub fn translate_min_qualified_cardinality(ofn: &Value, subject_2_label: &HashMa //I am expecting OFN S-expressions to follow OWL functional syntax //so, datatypes for numbers do not need to be added //number.push_str("^^xsd:nonNegativeInteger"); - let card = json!(["span", {"property":"owl:minQualifiedCardinality"}, number]); + let card = json!(["span", {"property":OWL_MIN_QUALIFIED_CARDINALITY}, number]); - let filler = translate(&ofn[3], subject_2_label, Some("owl:onClass")); + let filler = translate(&ofn[3], subject_2_label, Some(OWL_ON_CLASS)); if ofn[2].is_array() { render_qualified_cardinality_restriction_nested(&property, &modifier, &card, &filler, rdfa_property) @@ -400,7 +401,7 @@ pub fn translate_min_qualified_cardinality(ofn: &Value, subject_2_label: &HashMa pub fn translate_max_cardinality(ofn: &Value, subject_2_label: &HashMap, rdfa_property: Option<&str>) -> Value { //TODO: use propertytranslation? - let property = translate(&ofn[1], subject_2_label, Some("owl:onProperty")); + let property = translate(&ofn[1], subject_2_label, Some(OWL_ON_PROPERTY)); let modifier = Value::String(String::from(" max ")); //encode cardinality @@ -408,9 +409,9 @@ pub fn translate_max_cardinality(ofn: &Value, subject_2_label: &HashMap, rdfa_property: Option<&str>) -> Value { //TODO: use propertytranslation? - let property = translate(&ofn[1], subject_2_label, Some("owl:onProperty")); + let property = translate(&ofn[1], subject_2_label, Some(OWL_ON_PROPERTY)); let modifier = Value::String(String::from(" max ")); //encode cardinality @@ -430,9 +431,9 @@ pub fn translate_max_qualified_cardinality(ofn: &Value, subject_2_label: &HashMa //I am expecting OFN S-expressions to follow OWL functional syntax //so, datatypes for numbers do not need to be added //number.push_str("^^xsd:nonNegativeInteger"); - let card = json!(["span", {"property":"owl:maxQualifiedCardinality"}, number]); + let card = json!(["span", {"property":OWL_MAX_QUALIFIED_CARDINALITY}, number]); - let filler = translate(&ofn[3], subject_2_label, Some("owl:onClass")); + let filler = translate(&ofn[3], subject_2_label, Some(OWL_ON_CLASS)); if ofn[2].is_array() { render_qualified_cardinality_restriction_nested(&property, &modifier, &card, &filler, rdfa_property) @@ -444,7 +445,7 @@ pub fn translate_max_qualified_cardinality(ofn: &Value, subject_2_label: &HashMa pub fn translate_exact_cardinality(ofn: &Value, subject_2_label: &HashMap, rdfa_property: Option<&str>) -> Value { //TODO: use propertytranslation? - let property = translate(&ofn[1], subject_2_label, Some("owl:onProperty")); + let property = translate(&ofn[1], subject_2_label, Some(OWL_ON_PROPERTY)); let modifier = Value::String(String::from(" exactly ")); //encode cardinality @@ -452,9 +453,9 @@ pub fn translate_exact_cardinality(ofn: &Value, subject_2_label: &HashMap, rdfa_property: Option<&str>) -> Value { //TODO: use propertytranslation? - let property = translate(&ofn[1], subject_2_label, Some("owl:onProperty")); + let property = translate(&ofn[1], subject_2_label, Some(OWL_ON_PROPERTY)); let modifier = Value::String(String::from(" exactly ")); //encode cardinality @@ -474,9 +475,9 @@ pub fn translate_exact_qualified_cardinality(ofn: &Value, subject_2_label: &Hash //I am expecting OFN S-expressions to follow OWL functional syntax //so, datatypes for numbers do not need to be added //number.push_str("^^xsd:nonNegativeInteger"); - let card = json!(["span", {"property":"owl:qualifiedCardinality"}, number]); + let card = json!(["span", {"property":OWL_QUALIFIED_CARDINALITY}, number]); - let filler = translate(&ofn[3], subject_2_label, Some("owl:onClass")); + let filler = translate(&ofn[3], subject_2_label, Some(OWL_ON_CLASS)); if ofn[2].is_array() { render_qualified_cardinality_restriction_nested(&property, &modifier, &card, &filler, rdfa_property) diff --git a/src/ofn_2_rdfa/translation.rs b/src/ofn_2_rdfa/translation.rs index b25d5d3..2d8e4e0 100644 --- a/src/ofn_2_rdfa/translation.rs +++ b/src/ofn_2_rdfa/translation.rs @@ -18,7 +18,7 @@ pub fn ofn_2_rdfa(v : &Value, subject_2_label: &HashMap) -> Value match v[0].as_str() { Some("SubClassOf") => axiom_translation::translate_subclass_of_axiom(&v[1], &v[2], subject_2_label), Some("DisjointClasses") => json!("todo"), - Some("DisjointUnionOf") => json!("todo"), + Some("DisjointUnion") => json!("todo"), Some("EquivalentClasses") => json!("todo"), Some(_) => panic!(), None => panic!(), diff --git a/src/ofn_2_thick/axiom_translation.rs b/src/ofn_2_thick/axiom_translation.rs index ab0c7ef..89a315f 100644 --- a/src/ofn_2_thick/axiom_translation.rs +++ b/src/ofn_2_thick/axiom_translation.rs @@ -1,3 +1,4 @@ +use crate::constants::*; use crate::ofn_2_thick::class_translation; use crate::owl::thick_triple as owl; use rand::Rng; @@ -11,7 +12,7 @@ pub fn translate_subclass_of_axiom(v: &Value) -> Value { let superclass = class_translation::translate(&v[2]); let triple = json!({"subject":subclass, - "predicate":"rdfs:subClassOf", + "predicate":RDFS_SUB_CLASS_OF, "object":superclass}); triple } @@ -25,8 +26,8 @@ pub fn translate_disjoint_classes_axiom(v: &Value) -> Value { let operands: owl::OWL = class_translation::translate_list(&(v.as_array().unwrap())[1..]); let triple = json!({"subject":blank_node, - "predicate":"owl:AllDisjointClasses", - "object": {"owl:members":operands}}); + "predicate":OWL_ALL_DISJOINT_CLASSES, + "object": {OWL_MEMBERS:operands}}); triple } @@ -35,7 +36,7 @@ pub fn translate_disjoint_union_of_axiom(v: &Value) -> Value { let operands: owl::OWL = class_translation::translate_list(&(v.as_array().unwrap())[2..]); let triple = json!({"subject":lhs, - "predicate":"owl:disjointUnionOf", + "predicate":OWL_DISJOINT_UNION_OF, "object":operands}); triple } @@ -49,7 +50,7 @@ pub fn translate_equivalent_classes_axiom(v: &Value) -> Value { let rhs = class_translation::translate(&v[2]); let triple = json!({"subject":lhs, - "predicate":"owl:equivalentClass", + "predicate":OWL_EQUIVALENT_CLASS, "object":rhs}); triple } else { @@ -59,7 +60,7 @@ pub fn translate_equivalent_classes_axiom(v: &Value) -> Value { let operands: owl::OWL = class_translation::translate_list(&(v.as_array().unwrap())[1..]); let triple = json!({"subject":blank_node, - "predicate":"owl:equivalentClass", + "predicate":OWL_EQUIVALENT_CLASS, "object":operands}); triple } diff --git a/src/ofn_2_thick/class_translation.rs b/src/ofn_2_thick/class_translation.rs index 5945ad5..a52d4fd 100644 --- a/src/ofn_2_thick/class_translation.rs +++ b/src/ofn_2_thick/class_translation.rs @@ -1,3 +1,4 @@ +use crate::constants::*; use crate::ofn_2_thick::property_translation; use crate::owl::thick_triple as owl; use serde_json::Value; @@ -57,7 +58,7 @@ pub fn translate_some_values_from(v: &Value) -> owl::OWL { //build objects let property_o: owl::Object = get_object(property); let filler_o: owl::Object = get_object(filler); - let type_o: owl::Object = get_object(owl::OWL::Named("owl:Restriction".to_string())); + let type_o: owl::Object = get_object(owl::OWL::Named(OWL_RESTRICTION.to_string())); //build struct let res: owl::SomeValuesFrom = owl::SomeValuesFrom { @@ -75,7 +76,7 @@ pub fn translate_all_values_from(v: &Value) -> owl::OWL { let property_o: owl::Object = get_object(property); let filler_o: owl::Object = get_object(filler); - let type_o: owl::Object = get_object(owl::OWL::Named("owl:Restriction".to_string())); + let type_o: owl::Object = get_object(owl::OWL::Named(OWL_RESTRICTION.to_string())); let res: owl::AllValuesFrom = owl::AllValuesFrom { rdf_type: Some(vec![type_o]), @@ -91,7 +92,7 @@ pub fn translate_has_value(v: &Value) -> owl::OWL { let property_o: owl::Object = get_object(property); let filler_o: owl::Object = get_object(filler); - let type_o: owl::Object = get_object(owl::OWL::Named("owl:Restriction".to_string())); + let type_o: owl::Object = get_object(owl::OWL::Named(OWL_RESTRICTION.to_string())); let res: owl::HasValue = owl::HasValue { rdf_type: Some(vec![type_o]), @@ -105,8 +106,8 @@ pub fn translate_has_self(v: &Value) -> owl::OWL { let property: owl::OWL = translate(&v[1]); let property_o: owl::Object = get_object(property); - let has_self_o: owl::Object = get_object(owl::OWL::Named("true^^xsd:boolean".to_string())); - let type_o: owl::Object = get_object(owl::OWL::Named("owl:Restriction".to_string())); + let has_self_o: owl::Object = get_object(owl::OWL::Named(format!("true^^{XSD_BOOLEAN}"))); + let type_o: owl::Object = get_object(owl::OWL::Named(OWL_RESTRICTION.to_string())); let res: owl::HasSelf = owl::HasSelf { rdf_type: Some(vec![type_o]), @@ -122,7 +123,7 @@ pub fn translate_min_cardinality(v: &Value) -> owl::OWL { let property_o: owl::Object = get_object(property); let cardinality_o: owl::Object = get_object(cardinliaty); - let type_o: owl::Object = get_object(owl::OWL::Named("owl:Restriction".to_string())); + let type_o: owl::Object = get_object(owl::OWL::Named(OWL_RESTRICTION.to_string())); let res: owl::MinCardinality = owl::MinCardinality { rdf_type: Some(vec![type_o]), @@ -140,7 +141,7 @@ pub fn translate_min_qualified_cardinality(v: &Value) -> owl::OWL { let property_o: owl::Object = get_object(property); let cardinality_o: owl::Object = get_object(cardinliaty); let filler_o: owl::Object = get_object(filler); - let type_o: owl::Object = get_object(owl::OWL::Named("owl:Restriction".to_string())); + let type_o: owl::Object = get_object(owl::OWL::Named(OWL_RESTRICTION.to_string())); let res: owl::MinObjectQualifiedCardinality = owl::MinObjectQualifiedCardinality { rdf_type: Some(vec![type_o]), @@ -157,7 +158,7 @@ pub fn translate_max_cardinality(v: &Value) -> owl::OWL { let property_o: owl::Object = get_object(property); let cardinality_o: owl::Object = get_object(cardinliaty); - let type_o: owl::Object = get_object(owl::OWL::Named("owl:Restriction".to_string())); + let type_o: owl::Object = get_object(owl::OWL::Named(OWL_RESTRICTION.to_string())); let res: owl::MaxCardinality = owl::MaxCardinality { rdf_type: Some(vec![type_o]), @@ -175,7 +176,7 @@ pub fn translate_max_qualified_cardinality(v: &Value) -> owl::OWL { let property_o: owl::Object = get_object(property); let cardinality_o: owl::Object = get_object(cardinliaty); let filler_o: owl::Object = get_object(filler); - let type_o: owl::Object = get_object(owl::OWL::Named("owl:Restriction".to_string())); + let type_o: owl::Object = get_object(owl::OWL::Named(OWL_RESTRICTION.to_string())); let res: owl::MaxObjectQualifiedCardinality = owl::MaxObjectQualifiedCardinality { rdf_type: Some(vec![type_o]), @@ -192,7 +193,7 @@ pub fn translate_exact_cardinality(v: &Value) -> owl::OWL { let property_o: owl::Object = get_object(property); let cardinality_o: owl::Object = get_object(cardinliaty); - let type_o: owl::Object = get_object(owl::OWL::Named("owl:Restriction".to_string())); + let type_o: owl::Object = get_object(owl::OWL::Named(OWL_RESTRICTION.to_string())); let res: owl::ExactCardinality = owl::ExactCardinality { rdf_type: Some(vec![type_o]), @@ -210,7 +211,7 @@ pub fn translate_exact_qualified_cardinality(v: &Value) -> owl::OWL { let property_o: owl::Object = get_object(property); let cardinality_o: owl::Object = get_object(cardinliaty); let filler_o: owl::Object = get_object(filler); - let type_o: owl::Object = get_object(owl::OWL::Named("owl:Restriction".to_string())); + let type_o: owl::Object = get_object(owl::OWL::Named(OWL_RESTRICTION.to_string())); let res: owl::ExactObjectQualifiedCardinality = owl::ExactObjectQualifiedCardinality { rdf_type: Some(vec![type_o]), @@ -225,7 +226,7 @@ pub fn translate_list(v: &[Value]) -> owl::OWL { //TODO: refactor common parts if v.len() == 1 { let first: owl::OWL = translate(&v[0]); - let rest = owl::OWL::Named("rdf:nil".to_string()); + let rest = owl::OWL::Named(RDF_NIL.to_string()); let first_o: owl::Object = get_object(first); let rest_o: owl::Object = get_object(rest); @@ -254,7 +255,7 @@ pub fn translate_intersection_of(v: &Value) -> owl::OWL { let operands: owl::OWL = translate_list(&(v.as_array().unwrap())[1..]); let operands_o: owl::Object = get_object(operands); - let type_o: owl::Object = get_object(owl::OWL::Named("owl:Class".to_string())); + let type_o: owl::Object = get_object(owl::OWL::Named(OWL_CLASS.to_string())); let res: owl::IntersectionOf = owl::IntersectionOf { rdf_type: Some(vec![type_o]), @@ -267,7 +268,7 @@ pub fn translate_union_of(v: &Value) -> owl::OWL { let operands: owl::OWL = translate_list(&(v.as_array().unwrap())[1..]); let operands_o: owl::Object = get_object(operands); - let type_o: owl::Object = get_object(owl::OWL::Named("owl:Class".to_string())); + let type_o: owl::Object = get_object(owl::OWL::Named(OWL_CLASS.to_string())); let res: owl::UnionOf = owl::UnionOf { rdf_type: Some(vec![type_o]), @@ -280,7 +281,7 @@ pub fn translate_one_of(v: &Value) -> owl::OWL { let operands: owl::OWL = translate_list(&(v.as_array().unwrap())[1..]); let operands_o: owl::Object = get_object(operands); - let type_o: owl::Object = get_object(owl::OWL::Named("owl:Class".to_string())); + let type_o: owl::Object = get_object(owl::OWL::Named(OWL_CLASS.to_string())); let res: owl::OneOf = owl::OneOf { rdf_type: Some(vec![type_o]), @@ -293,7 +294,7 @@ pub fn translate_complement_of(v: &Value) -> owl::OWL { let argument: owl::OWL = translate(&v[1]); let argument_o: owl::Object = get_object(argument); - let type_o: owl::Object = get_object(owl::OWL::Named("owl:Class".to_string())); + let type_o: owl::Object = get_object(owl::OWL::Named(OWL_CLASS.to_string())); let res: owl::ComplementOf = owl::ComplementOf { rdf_type: Some(vec![type_o]), diff --git a/src/ofn_2_thick/translation.rs b/src/ofn_2_thick/translation.rs index ab78a0a..48d09fc 100644 --- a/src/ofn_2_thick/translation.rs +++ b/src/ofn_2_thick/translation.rs @@ -5,7 +5,7 @@ pub fn ofn_2_thick(v: &Value) -> Value { match v[0].as_str() { Some("SubClassOf") => axiom_translation::translate_subclass_of_axiom(v), Some("DisjointClasses") => axiom_translation::translate_disjoint_classes_axiom(v), - Some("DisjointUnionOf") => axiom_translation::translate_disjoint_union_of_axiom(v), + Some("DisjointUnion") => axiom_translation::translate_disjoint_union_of_axiom(v), Some("EquivalentClasses") => axiom_translation::translate_equivalent_classes_axiom(v), Some(_) => panic!(), None => panic!(), diff --git a/src/ofn_labeling/axiom_translation.rs b/src/ofn_labeling/axiom_translation.rs index 0e73787..012173b 100644 --- a/src/ofn_labeling/axiom_translation.rs +++ b/src/ofn_labeling/axiom_translation.rs @@ -42,7 +42,7 @@ pub fn translate_disjoint_union_of_axiom(v : &Value, m : &HashMap let lhs = class_translation::translate(&v[1], m); let operands : Value = class_translation::translate_list(&(v.as_array().unwrap())[2..], m); - let operator = Value::String(String::from("DisjointUnionOf")); + let operator = Value::String(String::from("DisjointUnion")); let v = vec![operator, lhs, operands]; Value::Array(v) } diff --git a/src/ofn_labeling/labeling.rs b/src/ofn_labeling/labeling.rs index 57bc5ec..dad1880 100644 --- a/src/ofn_labeling/labeling.rs +++ b/src/ofn_labeling/labeling.rs @@ -5,6 +5,7 @@ use std::fs::File; use std::io::{prelude::*, BufReader}; use serde_json::{Value}; use std::collections::HashMap; +use crate::constants::*; pub fn substitute(v : &Value, e2l : &HashMap) -> Value { @@ -51,7 +52,7 @@ fn is_labeling_triple(t: &str) -> bool { let predicate : &str = thick_triple["predicate"].as_str().unwrap(); - predicate.eq("rdfs:label") + predicate.eq(RDFS_LABEL) } fn get_label_mapping(t: &str) -> (String, String) { diff --git a/src/ofn_labeling/translation.rs b/src/ofn_labeling/translation.rs index a370375..02bf1b4 100644 --- a/src/ofn_labeling/translation.rs +++ b/src/ofn_labeling/translation.rs @@ -24,7 +24,7 @@ pub fn label_ofn(v: &Value, m : &HashMap) -> Value { match v[0].as_str() { Some("SubClassOf") => axiom_translation::translate_subclass_of_axiom(v,m), Some("DisjointClasses") => axiom_translation::translate_disjoint_classes_axiom(v,m), - Some("DisjointUnionOf") => axiom_translation::translate_disjoint_union_of_axiom(v,m), + Some("DisjointUnion") => axiom_translation::translate_disjoint_union_of_axiom(v,m), Some("EquivalentClasses") => axiom_translation::translate_equivalent_classes_axiom(v,m), Some("ThinTriple") => axiom_translation::translate_thin_triple(v,m), diff --git a/src/ofn_typing/axiom_translation.rs b/src/ofn_typing/axiom_translation.rs index 653e692..982604b 100644 --- a/src/ofn_typing/axiom_translation.rs +++ b/src/ofn_typing/axiom_translation.rs @@ -1,4 +1,5 @@ use serde_json::{Value}; +use crate::constants::*; use crate::ofn_typing::class_translation as class_translation; //TODO: class translation use crate::ofn_typing::property_translation as property_translation; use crate::util::signature as signature; @@ -42,7 +43,7 @@ pub fn translate_disjoint_union_of_axiom(v : &Value, m : &HashMap Value::String(x) => { if m.contains_key(&x) { let types = m.get(&x).unwrap(); - if types.contains("owl:ObjectProperty") { + if types.contains(OWL_OBJECT_PROPERTY) { is_object_property = true; } - if types.contains("owl:DatatypeProperty") { + if types.contains(OWL_DATATYPE_PROPERTY) { is_data_property = true; } - if types.contains("owl:AnnotationProperty") { + if types.contains(OWL_ANNOTATION_PROPERTY) { is_annotation_property = true; } } @@ -390,7 +391,7 @@ pub fn translate_thin_triple(v : &Value, m : &HashMap>) - match v[2].as_str() { - Some("rdf:type") => class_translation::translate_rdf_type(v,m), + Some(RDF_TYPE) => class_translation::translate_rdf_type(v,m), //TODO: translate annotation (and then check what kind of annotation) _ => class_translation::translate_assertion(v,m), } diff --git a/src/ofn_typing/class_translation.rs b/src/ofn_typing/class_translation.rs index adecbe2..0d815a6 100644 --- a/src/ofn_typing/class_translation.rs +++ b/src/ofn_typing/class_translation.rs @@ -1,4 +1,5 @@ use serde_json::{Value}; +use crate::constants::*; use crate::ofn_typing::property_translation as property_translation; use std::collections::HashMap; use std::collections::HashSet; @@ -71,7 +72,7 @@ pub fn is_named_individual(v :&Value, m : &HashMap>) -> let s = v.as_str().unwrap(); match m.get(s) { - Some(set) => set.contains("owl:NamedIndividual"), + Some(set) => set.contains(OWL_NAMED_INDIVIDUAL), _ => false, } @@ -140,7 +141,7 @@ pub fn is_class_expression(v : &Value, m : &HashMap>) -> pub fn type_look_up(s : &str, m: &HashMap>) -> bool { match m.get(s) { - Some(set) => set.contains("owl:Class"), + Some(set) => set.contains(OWL_CLASS), _ => false, } } @@ -163,21 +164,21 @@ pub fn translate_rdf_type(v : &Value, m : &HashMap>) -> match v[3].as_str() { //declarations - Some("owl:Class") => translate_declaration(v,m), - Some("rdfs:Datatype") => translate_declaration(v,m), - Some("owl:ObjectProperty") => translate_declaration(v,m), - Some("owl:DatatypeProperty") => translate_declaration(v,m), - Some("owl:AnnotationProperty") => translate_declaration(v,m), - Some("owl:NamedIndividual") => translate_declaration(v,m), + Some(x) if x == OWL_CLASS => translate_declaration(v,m), + Some(x) if x == RDFS_DATATYPE => translate_declaration(v,m), + Some(x) if x == OWL_OBJECT_PROPERTY => translate_declaration(v,m), + Some(x) if x == OWL_DATATYPE_PROPERTY => translate_declaration(v,m), + Some(x) if x == OWL_ANNOTATION_PROPERTY => translate_declaration(v,m), + Some(x) if x == OWL_NAMED_INDIVIDUAL => translate_declaration(v,m), //property axioms - Some("owl:FunctionalProperty") => translate_functional_property(v,m), - Some("owl:InverseFunctionalProperty") => translate_inverse_functional_property(v,m), - Some("owl:ReflexiveProperty") => translate_reflexive_property(v,m), - Some("owl:IrreflexiveProperty") => translate_irreflexive_property(v,m), - Some("owl:SymmetricProperty") => translate_symmetric_property(v,m), - Some("owl:AsymmetricProperty") => translate_asymmetric_property(v,m), - Some("owl:TransitiveProperty") => translate_transitive_property(v,m), + Some(x) if x == OWL_FUNCTIONAL_PROPERTY => translate_functional_property(v,m), + Some(x) if x == OWL_INVERSE_FUNCTIONAL_PROPERTY => translate_inverse_functional_property(v,m), + Some(x) if x == OWL_REFLECTIVE_PROPERTY => translate_reflexive_property(v,m), + Some(x) if x == OWL_IRREFLEXIVE_PROPERTY => translate_irreflexive_property(v,m), + Some(x) if x == OWL_SYMMETRIC_PROPERTY => translate_symmetric_property(v,m), + Some(x) if x == OWL_ASYMMETRIC_PROPERTY => translate_asymmetric_property(v,m), + Some(x) if x == OWL_TRANSITIVE_PROPERTY => translate_transitive_property(v,m), _ => translate_class_assertion(v,m), @@ -292,12 +293,12 @@ pub fn translate_declaration(v : &Value, _m : &HashMap>) let entity = Value::String(String::from(s)); let operator = match v[3].as_str() { - Some("owl:Class") => Value::String(String::from("Class")), - Some("rdfs:Datatype") => Value::String(String::from("Datatype")), - Some("owl:ObjectProperty") => Value::String(String::from("ObjectProperty")), - Some("owl:DatatypeProperty") => Value::String(String::from("DataProperty")), - Some("owl:AnnotationProperty") => Value::String(String::from("AnnotationProperty")), - Some("owl:NamedIndividual") => Value::String(String::from("NamedIndividual")), + Some(x) if x == OWL_CLASS => Value::String(String::from("Class")), + Some(x) if x == RDFS_DATATYPE => Value::String(String::from("Datatype")), + Some(x) if x == OWL_OBJECT_PROPERTY => Value::String(String::from("ObjectProperty")), + Some(x) if x == OWL_DATATYPE_PROPERTY => Value::String(String::from("DataProperty")), + Some(x) if x == OWL_ANNOTATION_PROPERTY => Value::String(String::from("AnnotationProperty")), + Some(x) if x == OWL_NAMED_INDIVIDUAL => Value::String(String::from("NamedIndividual")), _ => panic!("Unknown type in declaration") }; diff --git a/src/ofn_typing/property_translation.rs b/src/ofn_typing/property_translation.rs index 37556d0..2c32944 100644 --- a/src/ofn_typing/property_translation.rs +++ b/src/ofn_typing/property_translation.rs @@ -1,3 +1,4 @@ +use crate::constants::*; use serde_json::{Value}; use std::collections::HashMap; use std::collections::HashSet; @@ -30,7 +31,7 @@ pub fn is_data_property(v : &Value, m : &HashMap>) -> bo }; match m.get(&key) { - Some(set) => set.contains("owl:DatatypeProperty"),//we are using JSON Strings here + Some(set) => set.contains(OWL_DATATYPE_PROPERTY),//we are using JSON Strings here _ => false, } } @@ -43,14 +44,14 @@ pub fn is_annotation_property(v : &Value, m : &HashMap>) }; match m.get(&key) { - Some(set) => set.contains("owl:AnnotationProperty"), + Some(set) => set.contains(OWL_ANNOTATION_PROPERTY), _ => false, } } pub fn object_type_look_up(s : String, m: &HashMap>) -> bool { match m.get(&s) { - Some(set) => set.contains("owl:ObjectProperty"), + Some(set) => set.contains(OWL_OBJECT_PROPERTY), _ => false, } } diff --git a/src/ofn_typing/translation.rs b/src/ofn_typing/translation.rs index 1289286..2e62370 100644 --- a/src/ofn_typing/translation.rs +++ b/src/ofn_typing/translation.rs @@ -69,7 +69,7 @@ pub fn get_owl(v : &Value) -> Value { /// /// let mut entity_2_type = HashMap::new(); /// let entity = String::from("obo:OBI_0500000"); -/// let entity_type = String::from("owl:Class"); +/// let entity_type = String::from(""); /// let mut types = HashSet::new(); /// types.insert(entity_type); /// entity_2_type.insert(entity, types); @@ -88,7 +88,7 @@ pub fn type_ofn(v: &Value, m : &HashMap>) -> Value { match v[0].as_str() { Some("SubClassOf") => axiom_translation::translate_subclass_of_axiom(v,m), Some("DisjointClasses") => axiom_translation::translate_disjoint_classes_axiom(v,m), - Some("DisjointUnionOf") => axiom_translation::translate_disjoint_union_of_axiom(v,m), + Some("DisjointUnion") => axiom_translation::translate_disjoint_union_of_axiom(v,m), Some("EquivalentClasses") => axiom_translation::translate_equivalent_classes_axiom(v,m), Some("ClassAssertion") => axiom_translation::translate_class_assertion(v,m), Some("DataPropertyDomain") => axiom_translation::translate_data_property_domain(v,m), diff --git a/src/ofn_typing/typing.rs b/src/ofn_typing/typing.rs index ee7f2b3..b0b04b8 100644 --- a/src/ofn_typing/typing.rs +++ b/src/ofn_typing/typing.rs @@ -3,6 +3,7 @@ use std::io::{prelude::*, BufReader}; use serde_json::{Value}; use std::collections::HashMap; use std::collections::HashSet; +use crate::constants::*; //returns a map from entity names to their types //TODO: think about whether you want this mapping to work on Values too @@ -31,7 +32,7 @@ fn is_typing_triple(t: &str) -> bool { //let predicate : String = thick_triple["predicate"].to_string(); //predicate == "\"rdf:type\"" let predicate : &str = thick_triple["predicate"].as_str().unwrap(); - predicate.eq("rdf:type") + predicate.eq(RDF_TYPE) } fn get_type_mapping(t: &str) -> (String, String) { diff --git a/src/owl/thick_triple.rs b/src/owl/thick_triple.rs index 7bbecba..3e391b5 100644 --- a/src/owl/thick_triple.rs +++ b/src/owl/thick_triple.rs @@ -6,27 +6,27 @@ use serde::{Deserialize, Serialize}; // #[derive(Debug,Serialize, Deserialize,Clone,Hash)] pub struct RDFList { - //#[serde(rename = "rdf:type")] + //#[serde(rename = "")] //pub rdf_type: Option>, - #[serde(rename = "rdf:first")] + #[serde(rename = "")] pub rdf_first: Vec, - #[serde(rename = "rdf:rest")] + #[serde(rename = "")] pub rdf_rest: Vec, } #[derive(Debug,Serialize, Deserialize,Clone,Hash)] pub struct Members { - #[serde(rename = "rdf:type")]//TODO: the type is *not* optional for 'owl:members'? + #[serde(rename = "")]//TODO: the type is *not* optional for 'owl:members'? pub rdf_type: Option>, - #[serde(rename = "owl:members")] + #[serde(rename = "")] pub members: Vec, } #[derive(Debug,Serialize, Deserialize,Clone,Hash)] pub struct DistinctMembers { - #[serde(rename = "rdf:type")] + #[serde(rename = "")] pub rdf_type: Option>, - #[serde(rename = "owl:distinctMembers")] + #[serde(rename = "")] pub distinct_members: Vec, } @@ -36,143 +36,143 @@ pub struct DistinctMembers { #[derive(Debug,Serialize, Deserialize,Clone,Hash)] pub struct SomeValuesFrom { - #[serde(rename = "rdf:type")] + #[serde(rename = "")] pub rdf_type: Option>, - #[serde(rename = "owl:onProperty")] + #[serde(rename = "")] pub owl_on_property: Vec, - #[serde(rename = "owl:someValuesFrom")] + #[serde(rename = "")] pub owl_some_values_from: Vec, } #[derive(Debug,Serialize, Deserialize,Clone,Hash)] pub struct AllValuesFrom { - #[serde(rename = "rdf:type")] + #[serde(rename = "")] pub rdf_type: Option>, - #[serde(rename = "owl:onProperty")] + #[serde(rename = "")] pub owl_on_property: Vec, - #[serde(rename = "owl:allValuesFrom")] + #[serde(rename = "")] pub owl_all_values_from: Vec, } #[derive(Debug,Serialize, Deserialize,Clone,Hash)] pub struct HasValue { - #[serde(rename = "rdf:type")] + #[serde(rename = "")] pub rdf_type: Option>, - #[serde(rename = "owl:onProperty")] + #[serde(rename = "")] pub owl_on_property: Vec, - #[serde(rename = "owl:hasValue")] + #[serde(rename = "")] pub owl_has_value: Vec, } #[derive(Debug,Serialize, Deserialize,Clone,Hash)] pub struct MinCardinality { - #[serde(rename = "rdf:type")] + #[serde(rename = "")] pub rdf_type: Option>, - #[serde(rename = "owl:onProperty")] + #[serde(rename = "")] pub owl_on_property: Vec, - #[serde(rename = "owl:minCardinality")] + #[serde(rename = "")] pub owl_min_cardinality: Vec, } #[derive(Debug,Serialize, Deserialize,Clone,Hash)] pub struct MinObjectQualifiedCardinality { - #[serde(rename = "rdf:type")] + #[serde(rename = "")] pub rdf_type: Option>, - #[serde(rename = "owl:onProperty")] + #[serde(rename = "")] pub owl_on_property: Vec, - #[serde(rename = "owl:minQualifiedCardinality")] + #[serde(rename = "")] pub owl_min_qualified_cardinality: Vec, - #[serde(rename = "owl:onClass")] + #[serde(rename = "")] pub owl_on_class: Vec, } #[derive(Debug,Serialize, Deserialize,Clone,Hash)] pub struct MinDataQualifiedCardinality { - #[serde(rename = "rdf:type")] + #[serde(rename = "")] pub rdf_type: Option>, - #[serde(rename = "owl:onProperty")] + #[serde(rename = "")] pub owl_on_property: Vec, - #[serde(rename = "owl:minQualifiedCardinality")] + #[serde(rename = "")] pub owl_min_qualified_cardinality: Vec, - #[serde(rename = "owl:onDataRange")] + #[serde(rename = "")] pub owl_on_datarange: Vec, } #[derive(Debug,Serialize, Deserialize,Clone,Hash)] pub struct MaxCardinality { - #[serde(rename = "rdf:type")] + #[serde(rename = "")] pub rdf_type: Option>, - #[serde(rename = "owl:onProperty")] + #[serde(rename = "")] pub owl_on_property: Vec, - #[serde(rename = "owl:maxCardinality")] + #[serde(rename = "")] pub owl_max_cardinality: Vec, } #[derive(Debug,Serialize, Deserialize,Clone,Hash)] pub struct MaxObjectQualifiedCardinality { - #[serde(rename = "rdf:type")] + #[serde(rename = "")] pub rdf_type: Option>, - #[serde(rename = "owl:onProperty")] + #[serde(rename = "")] pub owl_on_property: Vec, - #[serde(rename = "owl:maxQualifiedCardinality")] + #[serde(rename = "")] pub owl_max_qualified_cardinality: Vec, - #[serde(rename = "owl:onClass")] + #[serde(rename = "")] pub owl_on_class: Vec, } #[derive(Debug,Serialize, Deserialize,Clone,Hash)] pub struct MaxDataQualifiedCardinality { - #[serde(rename = "rdf:type")] + #[serde(rename = "")] pub rdf_type: Option>, - #[serde(rename = "owl:onProperty")] + #[serde(rename = "")] pub owl_on_property: Vec, - #[serde(rename = "owl:maxQualifiedCardinality")] + #[serde(rename = "")] pub owl_max_qualified_cardinality: Vec, - #[serde(rename = "owl:onDataRange")] + #[serde(rename = "")] pub owl_on_datarange: Vec, } #[derive(Debug,Serialize, Deserialize,Clone,Hash)] pub struct ExactCardinality { - #[serde(rename = "rdf:type")] + #[serde(rename = "")] pub rdf_type: Option>, - #[serde(rename = "owl:onProperty")] + #[serde(rename = "")] pub owl_on_property: Vec, - #[serde(rename = "owl:cardinality")] + #[serde(rename = "")] pub owl_cardinality: Vec, } #[derive(Debug,Serialize, Deserialize,Clone,Hash)] pub struct ExactObjectQualifiedCardinality { - #[serde(rename = "rdf:type")] + #[serde(rename = "")] pub rdf_type: Option>, - #[serde(rename = "owl:onProperty")] + #[serde(rename = "")] pub owl_on_property: Vec, - #[serde(rename = "owl:qualifiedCardinality")] + #[serde(rename = "")] pub owl_qualified_cardinality: Vec, - #[serde(rename = "owl:onClass")] + #[serde(rename = "")] pub owl_on_class: Vec, } #[derive(Debug,Serialize, Deserialize,Clone,Hash)] pub struct ExactDataQualifiedCardinality { - #[serde(rename = "rdf:type")] + #[serde(rename = "")] pub rdf_type: Option>, - #[serde(rename = "owl:onProperty")] + #[serde(rename = "")] pub owl_on_property: Vec, - #[serde(rename = "owl:qualifiedCardinality")] + #[serde(rename = "")] pub owl_qualified_cardinality: Vec, - #[serde(rename = "owl:onDataRange")] + #[serde(rename = "")] pub owl_on_datarange: Vec, } #[derive(Debug,Serialize, Deserialize,Clone,Hash)] pub struct HasSelf { - #[serde(rename = "rdf:type")] + #[serde(rename = "")] pub rdf_type: Option>, - #[serde(rename = "owl:onProperty")] + #[serde(rename = "")] pub owl_on_property: Vec, - #[serde(rename = "owl:hasSelf")] + #[serde(rename = "")] pub owl_has_self: Vec, } @@ -182,39 +182,39 @@ pub struct HasSelf { #[derive(Debug,Serialize, Deserialize,Clone,Hash)] pub struct IntersectionOf { - #[serde(rename = "rdf:type")] + #[serde(rename = "")] pub rdf_type: Option>, - #[serde(rename = "owl:intersectionOf")] + #[serde(rename = "")] pub owl_intersection_of: Vec, } #[derive(Debug,Serialize, Deserialize,Clone,Hash)] pub struct UnionOf { - #[serde(rename = "rdf:type")] + #[serde(rename = "")] pub rdf_type: Option>, - #[serde(rename = "owl:unionOf")] + #[serde(rename = "")] pub owl_union_of: Vec, } #[derive(Debug,Serialize, Deserialize,Clone,Hash)] pub struct OneOf { - #[serde(rename = "rdf:type")] + #[serde(rename = "")] pub rdf_type: Option>, - #[serde(rename = "owl:oneOf")] + #[serde(rename = "")] pub owl_one_of: Vec, } #[derive(Debug,Serialize, Deserialize,Clone,Hash)] pub struct ComplementOf { - #[serde(rename = "rdf:type")] + #[serde(rename = "")] pub rdf_type: Option>, - #[serde(rename = "owl:complementOf")] + #[serde(rename = "")] pub owl_complement_of: Vec, } #[derive(Debug,Serialize, Deserialize,Clone,Hash)] pub struct NegativeObjectPropertyAssertion { - #[serde(rename = "rdf:type")] + #[serde(rename = "")] pub rdf_type: Option>, #[serde(rename = "owl:sourceIndividual")] pub source_individual: Vec, @@ -226,7 +226,7 @@ pub struct NegativeObjectPropertyAssertion { #[derive(Debug,Serialize, Deserialize,Clone,Hash)] pub struct NegativeDataPropertyAssertion { - #[serde(rename = "rdf:type")] + #[serde(rename = "")] pub rdf_type: Option>, #[serde(rename = "owl:sourceIndividual")] pub source_individual: Vec, @@ -242,7 +242,7 @@ pub struct NegativeDataPropertyAssertion { #[derive(Debug,Serialize, Deserialize,Clone,Hash)] pub struct InverseOf { - #[serde(rename = "owl:inverseOf")] + #[serde(rename = "")] pub owl_inverse_of: Vec, } diff --git a/src/thick_2_ofn/axiom_translation.rs b/src/thick_2_ofn/axiom_translation.rs index 9c08c0b..b9e4134 100644 --- a/src/thick_2_ofn/axiom_translation.rs +++ b/src/thick_2_ofn/axiom_translation.rs @@ -68,7 +68,7 @@ pub fn translate_disjoint_union(u: &str, ops: &str) -> Value { let lhs: Value = class_translation::translate(&union); let mut rhs: Value = class_translation::translate(&operands); - let operator = Value::String(String::from("DisjointUnionOf")); + let operator = Value::String(String::from("DisjointUnion")); let mut union = vec![operator]; union.push(lhs); let arguments = rhs.as_array_mut().unwrap(); diff --git a/src/thick_2_ofn/class_translation.rs b/src/thick_2_ofn/class_translation.rs index 887983c..ecfb387 100644 --- a/src/thick_2_ofn/class_translation.rs +++ b/src/thick_2_ofn/class_translation.rs @@ -1,3 +1,4 @@ +use crate::constants::*; use crate::owl::thick_triple as owl; use crate::thick_2_ofn::property_translation; use serde_json::Value; @@ -229,7 +230,7 @@ pub fn translate_rdf_list(s: &owl::RDFList) -> Value { let mut rest = translate(&s.rdf_rest[0].object); //base case for RDF lists - if rest.is_string() && rest.as_str().unwrap() == "rdf:nil" { + if rest.is_string() && rest.as_str().unwrap() == RDF_NIL { let mut v = Vec::new(); v.push(first); Value::Array(v) @@ -255,7 +256,7 @@ pub fn check_class_type(v: &Option>) -> bool { match &t.object { //look for an owl:Class owl::OWL::Named(s) => { - if s == "owl:Class" { + if s == OWL_CLASS { res = true } } diff --git a/src/thick_2_ofn/translation.rs b/src/thick_2_ofn/translation.rs index b833d3f..ccd3514 100644 --- a/src/thick_2_ofn/translation.rs +++ b/src/thick_2_ofn/translation.rs @@ -1,3 +1,4 @@ +use crate::constants::*; use crate::thick_2_ofn::axiom_translation; use serde_json::Value; @@ -6,10 +7,10 @@ use serde_json::Value; //Consider the following trick triple as an example: // //{"subject": "ex:A", -// "predicate": "rdfs:subClassOf", -// "object": {"owl:onProperty":[{"object":"ex:prop"}], -// "owl:someValuesFrom":[{"object":"ex:B"}], -// "rdf:type":[{"object":"owl:Restriction"}]}} +// "predicate": "", +// "object": {"":[{"object":"ex:prop"}], +// "":[{"object":"ex:B"}], +// "":[{"object":""}]}} // //Without type information about either the property or the filler of the existential restriction, //we cannot decide whether it is @@ -29,15 +30,15 @@ pub fn thick_2_ofn(thick_triple: &Value) -> Value { let predicate = thick_triple["predicate"].as_str(); match predicate { - Some("rdfs:subClassOf") => axiom_translation::translate_subclass_of_axiom(subj, obj), - Some("owl:equivalentClass") => axiom_translation::translate_equivalent_class(subj, obj), - Some("owl:AllDisjointClasses") => { - let members_helper: String = thick_triple["object"]["owl:members"].to_string(); + Some(x) if x == RDFS_SUB_CLASS_OF => axiom_translation::translate_subclass_of_axiom(subj, obj), + Some(x) if x == OWL_EQUIVALENT_CLASS => axiom_translation::translate_equivalent_class(subj, obj), + Some(x) if x == OWL_ALL_DISJOINT_CLASSES => { + let members_helper: String = thick_triple["object"][OWL_MEMBERS].to_string(); let members: &str = members_helper.as_str(); axiom_translation::translate_disjoint_classes(members) } - Some("owl:disjointUnionOf") => axiom_translation::translate_disjoint_union(subj, obj), - Some("owl:disjointWith") => axiom_translation::translate_disjoint_with(subj, obj), + Some(x) if x == OWL_DISJOINT_UNION_OF => axiom_translation::translate_disjoint_union(subj, obj), + Some(x) if x == OWL_DISJOINT_WITH => axiom_translation::translate_disjoint_with(subj, obj), Some(_) => axiom_translation::translate_thin_triple(&thick_triple), None => Value::String(String::from("Fail")), } diff --git a/src/util/parser.rs b/src/util/parser.rs index 77b3011..ec34078 100644 --- a/src/util/parser.rs +++ b/src/util/parser.rs @@ -1,4 +1,5 @@ use serde_json::{Value, Result as SResult}; +use crate::constants::*; use crate::owl::thick_triple as tt; use std::fs::File; use std::io::{prelude::*, BufReader}; @@ -8,13 +9,13 @@ use std::io::{prelude::*, BufReader}; /// /// #Examples /// -/// let object = r#"{"owl:someValuesFrom": [{"object": "obo:OBI_0500000", +/// let object = r#"{"": [{"object": "obo:OBI_0500000", /// "datatype":"_iri", /// "meta":null}], -/// "rdf:type": [{"object": "owl:Restriction", +/// "": [{"object": "", /// "datatype":"_iri", /// "meta":null}], -/// "owl:onProperty": [{"object": "obo:BFO_0000050", +/// "": [{"object": "obo:BFO_0000050", /// "datatype":"_iri", /// "meta":null}]}"#; /// @@ -99,7 +100,7 @@ fn is_thick_triple(v: &Value) -> bool { if nesting { true } else { - v["predicate"].as_str().unwrap().eq("rdfs:subClassOf") + v["predicate"].as_str().unwrap().eq(RDFS_SUB_CLASS_OF) } } @@ -130,11 +131,11 @@ fn is_class_expression_axiom(v: &Value) -> bool { let predicate = v["predicate"].as_str(); match predicate { - Some("rdfs:subClassOf") => true, - Some("owl:equivalentClass") => true, - Some("owl:disjointWith") => true, - Some("owl:AllDisjointClasses") => true, - Some("owl:disjointUnionOf") => true, + Some(x) if x == RDFS_SUB_CLASS_OF => true, + Some(x) if x == OWL_EQUIVALENT_CLASS => true, + Some(x) if x == OWL_DISJOINT_WITH => true, + Some(x) if x == OWL_ALL_DISJOINT_CLASSES => true, + Some(x) if x == OWL_DISJOINT_UNION_OF => true, Some(_) => false, None => false, } diff --git a/src/util/signature.rs b/src/util/signature.rs index 1a043cd..788f9f4 100644 --- a/src/util/signature.rs +++ b/src/util/signature.rs @@ -102,7 +102,7 @@ pub fn extract(v: &Value) -> Vec { let res = match v[0].as_str() { Some("SubClassOf") => translate_subclass_of(v), Some("DisjointClasses") => translate_disjoint_classes(v), - Some("DisjointUnionOf") => translate_disjoint_union_of(v), + Some("DisjointUnion") => translate_disjoint_union_of(v), Some("EquivalentClasses") => translate_equivalent_classes(v), Some("ObjectSomeValuesFrom") => translate_some_values_from(v), Some("DataSomeValuesFrom") => translate_some_values_from(v), diff --git a/src/util/structural_identity.rs b/src/util/structural_identity.rs index e2fdcf8..08c3807 100644 --- a/src/util/structural_identity.rs +++ b/src/util/structural_identity.rs @@ -13,7 +13,7 @@ pub fn translate(v : &Value, match owl_operator.as_str() { "\"SubClassOf\"" => translate_subclass_of(v,m,t), "\"DisjointClasses\"" => translate_disjoint_classes(v,m,t), - "\"DisjointUnionOf\"" => translate_disjoint_union_of(v,m,t), + "\"DisjointUnion\"" => translate_disjoint_union_of(v,m,t), "\"EquivalentClasses\"" => translate_equivalent_classes(v,m,t), "\"ObjectSomeValuesFrom\"" => translate_some_values_from(v,m,t), "\"ObjectAllValuesFrom\"" => translate_all_values_from(v,m,t), @@ -68,7 +68,7 @@ pub fn translate_disjoint_union_of(v : &Value, let lhs = translate(&v[1], m, t); let mut operands : Value = translate_list(&(v.as_array().unwrap())[2..], m, t); - let operator = Value::String(String::from("DisjointUnionOf")); + let operator = Value::String(String::from("DisjointUnion")); let mut union = vec![operator, lhs]; let arguments = operands.as_array_mut().unwrap(); diff --git a/tests/thick_ofn_thick_tests.rs b/tests/thick_ofn_thick_tests.rs index 874874f..c48b79c 100644 --- a/tests/thick_ofn_thick_tests.rs +++ b/tests/thick_ofn_thick_tests.rs @@ -32,7 +32,7 @@ fn round_trip(input: &str) -> bool { #[test] fn sub_class_of_axiom() { let subclass_of = r#" {"subject": "ex:subClass", - "predicate": "rdfs:subClassOf", + "predicate": "", "object": "ex:superClass", "datatype" : "_IRI", "annotation" : {}, @@ -44,9 +44,9 @@ fn sub_class_of_axiom() { #[test] fn test_n_ary_equivalence_axiom() { - let equivalence = r#" {"subject": "", - "predicate": "owl:equivalentClass", - "object": {"owl:members" : [{"object" : [ + let equivalence = r#" {"subject": "", + "predicate": "", + "object": {"" : [{"object" : [ {"object": "ex:equivalent1", "datatype" : "_IRI"}, {"object": "ex:equivalent2", "datatype" : "_IRI"}, {"object": "ex:equivalent3", "datatype" : "_IRI"}], @@ -64,7 +64,7 @@ fn test_n_ary_equivalence_axiom() { fn binary_equivalence_axiom() { let equivalence = r#" {"subject": "ex:lhs", - "predicate": "owl:equivalentClass", + "predicate": "", "object": "ex:rhs", "datatype" : "_IRI", "annotation" : {}, @@ -77,7 +77,7 @@ fn binary_equivalence_axiom() { #[test] fn disjoint_union_axiom() { let disjoint_union = r#" {"subject": "ex:disjointUnion", - "predicate": "owl:disjointUnionOf", + "predicate": "", "object": [{"object": "ex:u1", "datatype" : "_IRI"}, {"object": "ex:u2", "datatype" : "_IRI"}, {"object": "ex:u3", "datatype" : "_IRI"}], @@ -92,9 +92,9 @@ fn disjoint_union_axiom() { #[test] fn complement_of_expression() { let complement_of = r#" {"subject": "ex:complementOf", - "predicate": "owl:equivalentClass", - "object": {"rdf:type": [{"object": "owl:Class", "datatype": "_IRI"}], - "owl:complementOf": [{"object": "ex:complement", "datatype": "_IRI"}]}, + "predicate": "", + "object": {"": [{"object": "", "datatype": "_IRI"}], + "": [{"object": "ex:complement", "datatype": "_IRI"}]}, "datatype" : "_JSONMAP", "annotation" : {}, "graph" : "graph", @@ -106,10 +106,10 @@ fn complement_of_expression() { #[test] fn some_values_from() { let existential_restriction = r#" {"subject": "ex:existential", - "predicate": "rdfs:subClassOf", - "object": {"rdf:type": [{"object": "owl:Restriction", "datatype": "_IRI"}], - "owl:onProperty": [{"object": "ex:pExistential", "datatype": "_IRI"}], - "owl:someValuesFrom": [{"object": "ex:existentialFiller", "datatype": "_IRI"}]}, + "predicate": "", + "object": {"": [{"object": "", "datatype": "_IRI"}], + "": [{"object": "ex:pExistential", "datatype": "_IRI"}], + "": [{"object": "ex:existentialFiller", "datatype": "_IRI"}]}, "datatype" : "_JSONMAP", "annotation" : {}, "graph" : "graph", @@ -121,10 +121,10 @@ fn some_values_from() { #[test] fn has_self() { let has_self = r#" {"subject": "ex:hasSelf", - "predicate": "rdfs:subClassOf", - "object": {"rdf:type": [{"object": "owl:Restriction", "datatype": "_IRI"}], - "owl:onProperty": [{"object": "ex:pHasSelf", "datatype": "_IRI"}], - "owl:hasSelf": [{"object": "true^^xsd:boolean", "datatype": "_IRI"}]}, + "predicate": "", + "object": {"": [{"object": "", "datatype": "_IRI"}], + "": [{"object": "ex:pHasSelf", "datatype": "_IRI"}], + "": [{"object": "true", "datatype": ""}]}, "datatype" : "_JSONMAP", "annotation" : {}, "graph" : "graph", @@ -136,10 +136,10 @@ fn has_self() { #[test] fn has_value() { let has_value = r#" {"subject": "ex:hasValue", - "predicate": "rdfs:subClassOf", - "object": {"rdf:type": [{"object": "owl:Restriction", "datatype": "_IRI"}], - "owl:onProperty": [{"object": "ex:pHasValue", "datatype": "_IRI"}], - "owl:hasValue": [{"object": "ex:a1", "datatype": "_IRI"}]}, + "predicate": "", + "object": {"": [{"object": "", "datatype": "_IRI"}], + "": [{"object": "ex:pHasValue", "datatype": "_IRI"}], + "": [{"object": "ex:a1", "datatype": "_IRI"}]}, "datatype" : "_JSONMAP", "annotation" : {}, "graph" : "graph", @@ -151,10 +151,10 @@ fn has_value() { #[test] fn max_cardinality() { let max_cardinality = r#" {"subject": "ex:maxCardinality", - "predicate": "rdfs:subClassOf", - "object": {"rdf:type": [{"object": "owl:Restriction", "datatype": "_IRI"}], - "owl:onProperty": [{"object": "ex:pMaxCardinality", "datatype": "_IRI"}], - "owl:maxCardinality": [{"object": "1", "datatype": "xsd:nonNegativeInteger"}]}, + "predicate": "", + "object": {"": [{"object": "", "datatype": "_IRI"}], + "": [{"object": "ex:pMaxCardinality", "datatype": "_IRI"}], + "": [{"object": "1", "datatype": ""}]}, "datatype" : "_JSONMAP", "annotation" : {}, "graph" : "graph", @@ -166,11 +166,11 @@ fn max_cardinality() { #[test] fn max_qualified_cardinality() { let max_qualified_cardinality = r#" {"subject": "ex:maxQualifiedCardinality", - "predicate": "rdfs:subClassOf", - "object": {"rdf:type": [{"object": "owl:Restriction", "datatype": "_IRI"}], - "owl:onProperty": [{"object": "ex:pMaxQualifiedCardinality", "datatype": "_IRI"}], - "owl:maxQualifiedCardinality": [{"object": "1", "datatype": "xsd:nonNegativeInteger"}], - "owl:onClass": [{"object": "ex:maxCardinalityFiller", "datatype": "_IRI"}]}, + "predicate": "", + "object": {"": [{"object": "", "datatype": "_IRI"}], + "": [{"object": "ex:pMaxQualifiedCardinality", "datatype": "_IRI"}], + "": [{"object": "1", "datatype": ""}], + "": [{"object": "ex:maxCardinalityFiller", "datatype": "_IRI"}]}, "datatype" : "_JSONMAP", "annotation" : {}, @@ -183,10 +183,10 @@ fn max_qualified_cardinality() { #[test] fn min_cardinality() { let min_cardinality = r#" {"subject": "ex:minCardinality", - "predicate": "owl:equivalentClass", - "object": {"rdf:type": [{"object": "owl:Restriction", "datatype": "_IRI"}], - "owl:onProperty": [{"object": "ex:pMinCardinality", "datatype": "_IRI"}], - "owl:minCardinality": [{"object": "1", "datatype" : "xsd:nonNegativeInteger"}]}, + "predicate": "", + "object": {"": [{"object": "", "datatype": "_IRI"}], + "": [{"object": "ex:pMinCardinality", "datatype": "_IRI"}], + "": [{"object": "1", "datatype" : ""}]}, "datatype" : "_JSONMAP", "annotation" : {}, "graph" : "graph", @@ -198,11 +198,11 @@ fn min_cardinality() { #[test] fn min_qualified_cardinality() { let min_qualified_cardinality = r#" {"subject": "ex:minQualifiedCardinality", - "predicate": "rdfs:subClassOf", - "object": {"rdf:type": [{"object": "owl:Restriction", "datatype": "_IRI"}], - "owl:onProperty": [{"object": "ex:pMinQualifiedCardinality", "datatype": "_IRI"}], - "owl:minQualifiedCardinality": [{"object": "1", "datatype" : "xsd:nonNegativeInteger"}], - "owl:onClass": [{"object": "ex:minCardinalityFiller", "datatype": "_IRI"}]}, + "predicate": "", + "object": {"": [{"object": "", "datatype": "_IRI"}], + "": [{"object": "ex:pMinQualifiedCardinality", "datatype": "_IRI"}], + "": [{"object": "1", "datatype" : ""}], + "": [{"object": "ex:minCardinalityFiller", "datatype": "_IRI"}]}, "datatype" : "_JSONMAP", "annotation" : {}, "graph" : "graph", @@ -215,10 +215,10 @@ fn min_qualified_cardinality() { #[test] fn exact_cardinality_expression() { let exact_cardinality = r#" {"subject": "ex:exactCardinality", - "predicate": "rdfs:subClassOf", - "object": {"rdf:type": [{"object": "owl:Restriction", "datatype": "_IRI"}], - "owl:onProperty": [{"object": "ex:pExactCardinality", "datatype": "_IRI"}], - "owl:cardinality": [{"object": "2", "datatype" : "xsd:nonNegativeInteger"}]}, + "predicate": "", + "object": {"": [{"object": "", "datatype": "_IRI"}], + "": [{"object": "ex:pExactCardinality", "datatype": "_IRI"}], + "": [{"object": "2", "datatype" : ""}]}, "datatype" : "_JSONMAP", "annotation" : {}, "graph" : "graph", @@ -231,11 +231,11 @@ fn exact_cardinality_expression() { #[test] fn exact_qualified_cardinality_expression() { let exact_qualified_cardinality = r#" {"subject": "ex:exactQualifiedCardinality", - "predicate": "rdfs:subClassOf", - "object": {"rdf:type": [{"object": "owl:Restriction", "datatype": "_IRI"}], - "owl:onProperty": [{"object": "ex:pExactQualifiedCardinality", "datatype": "_IRI"}], - "owl:qualifiedCardinality": [{"object": "2", "datatype" : "xsd:nonNegativeInteger"}], - "owl:onClass": [{"object": "ex:exactQualifiedCardinalityFiller", "datatype" : "_IRI"}]}, + "predicate": "", + "object": {"": [{"object": "", "datatype": "_IRI"}], + "": [{"object": "ex:pExactQualifiedCardinality", "datatype": "_IRI"}], + "": [{"object": "2", "datatype" : ""}], + "": [{"object": "ex:exactQualifiedCardinalityFiller", "datatype" : "_IRI"}]}, "datatype" : "_JSONMAP", "annotation" : {}, "graph" : "graph", @@ -248,12 +248,12 @@ fn exact_qualified_cardinality_expression() { #[test] fn intersection_expression() { let intersection = r#" {"subject": "ex:intersection", - "predicate": "rdfs:subClassOf", - "object": {"owl:intersectionOf": [{"object" : [{"object" : "ex:I1", "datatype": "_IRI"}, + "predicate": "", + "object": {"": [{"object" : [{"object" : "ex:I1", "datatype": "_IRI"}, {"object" : "ex:I2", "datatype": "_IRI"}, {"object" : "ex:I3", "datatype": "_IRI"}], "datatype": "_JSONLIST"}], - "rdf:type": [{"object": "owl:Class", "datatype" : "_IRI"}]}, + "": [{"object": "", "datatype" : "_IRI"}]}, "datatype" : "_JSONMAP", "annotation" : {}, "graph" : "graph", @@ -266,9 +266,9 @@ fn intersection_expression() { #[test] fn one_of() { let one_of = r#" {"subject": "ex:oneOf", - "predicate": "rdfs:subClassOf", - "object": {"rdf:type": [{"object": "owl:Class", "datatype": "_IRI"}], - "owl:oneOf": [{"object" : [{"object" : "ex:a1", "datatype": "_IRI"}, + "predicate": "", + "object": {"": [{"object": "", "datatype": "_IRI"}], + "": [{"object" : [{"object" : "ex:a1", "datatype": "_IRI"}, {"object" : "ex:a2", "datatype": "_IRI"}, {"object" : "ex:a3", "datatype": "_IRI"}], "datatype": "_JSONLIST"}]}, @@ -286,9 +286,9 @@ fn one_of() { #[test] fn union_of() { let union_of = r#" {"subject": "ex:union", - "predicate": "rdfs:subClassOf", - "object": {"rdf:type": [{"object": "owl:Class", "datatype": "_IRI"}], - "owl:unionOf": [{"object" : [{"object" : "ex:u1", "datatype": "_IRI"}, + "predicate": "", + "object": {"": [{"object": "", "datatype": "_IRI"}], + "": [{"object" : [{"object" : "ex:u1", "datatype": "_IRI"}, {"object" : "ex:u2", "datatype": "_IRI"}, {"object" : "ex:u3", "datatype": "_IRI"}], "datatype": "_JSONLIST"}]},