From 61ff0782de781381157327f3b83a6b0d4c7fd715 Mon Sep 17 00:00:00 2001 From: Arnaud Mimart <33665250+amimart@users.noreply.github.com> Date: Wed, 28 Feb 2024 11:28:42 +0100 Subject: [PATCH] feat(cognitarium): reuse where clause in construct empty --- contracts/okp4-cognitarium/src/contract.rs | 283 +++++++++++---------- 1 file changed, 147 insertions(+), 136 deletions(-) diff --git a/contracts/okp4-cognitarium/src/contract.rs b/contracts/okp4-cognitarium/src/contract.rs index fd87c327..0e1776f3 100644 --- a/contracts/okp4-cognitarium/src/contract.rs +++ b/contracts/okp4-cognitarium/src/contract.rs @@ -274,12 +274,23 @@ pub mod query { prefixes, r#where, } = query; - if construct.is_empty() { - return Ok(ConstructResponse { - format, - data: Binary::from(vec![]), - }); - } + + let construct = if construct.is_empty() { + r#where + .iter() + .map(|t| match t { + WhereCondition::Simple(SimpleWhereCondition::TriplePattern(t)) => { + TripleConstructTemplate { + subject: t.subject.clone(), + predicate: t.predicate.clone(), + object: t.object.clone(), + } + } + }) + .collect() + } else { + construct + }; let mut id_issuer = IdentifierIssuer::new("a", 0u128); let construct: Vec<_> = construct @@ -2149,139 +2160,139 @@ mod tests { fn proper_construct() { let id = "https://ontology.okp4.space/dataverse/dataspace/metadata/dcf48417-01c5-4b43-9bc7-49e54c028473"; let cases = vec![ - // ( - // InsertData { - // format: Some(DataFormat::RDFXml), - // data: read_test_data("sample.rdf.xml"), - // }, - // QueryMsg::Construct { - // query: ConstructQuery { - // prefixes: vec![], - // construct: vec![], - // r#where: vec![WhereCondition::Simple(TriplePattern(msg::TriplePattern { - // subject: VarOrNode::Node(NamedNode(Full(id.to_string()))), - // predicate: VarOrNamedNode::NamedNode(Full( - // "https://ontology.okp4.space/core/hasTag".to_string(), - // )), - // object: VarOrNodeOrLiteral::Variable("o".to_string()), - // }))], - // }, - // format: None, - // }, - // ConstructResponse { - // format: DataFormat::Turtle, - // data: Binary::from( - // " \"Test\" , \"OKP4\" .\n".to_string().as_bytes().to_vec()), - // }, - // ), - ( - InsertData { - format: Some(DataFormat::RDFXml), - data: read_test_data("sample.rdf.xml"), - }, - QueryMsg::Construct { - query: ConstructQuery { - prefixes: vec![ - Prefix { prefix: "my-ns".to_string(), namespace: "https://my-ns.org/".to_string() }, - Prefix { prefix: "metadata-dataset".to_string(), namespace: "https://ontology.okp4.space/dataverse/dataset/metadata/".to_string() }, - ], - construct: vec![ - msg::TripleConstructTemplate { - subject: VarOrNode::Node(NamedNode(Prefixed("my-ns:instance-1".to_string()))), - predicate: VarOrNamedNode::NamedNode(Full( - "https://my-ns/predicate/tag".to_string(), - )), - object: VarOrNodeOrLiteral::Variable("o".to_string()), - } - ], - r#where: vec![WhereCondition::Simple(TriplePattern(msg::TriplePattern { - subject: VarOrNode::Node(NamedNode(Full(id.to_string()))), - predicate: VarOrNamedNode::NamedNode(Full( - "https://ontology.okp4.space/core/hasTag".to_string(), - )), - object: VarOrNodeOrLiteral::Variable("o".to_string()), - }))], - }, - format: Some(DataFormat::NTriples), + ( + InsertData { + format: Some(DataFormat::RDFXml), + data: read_test_data("sample.rdf.xml"), + }, + QueryMsg::Construct { + query: ConstructQuery { + prefixes: vec![], + construct: vec![], + r#where: vec![WhereCondition::Simple(TriplePattern(msg::TriplePattern { + subject: VarOrNode::Node(NamedNode(Full(id.to_string()))), + predicate: VarOrNamedNode::NamedNode(Full( + "https://ontology.okp4.space/core/hasTag".to_string(), + )), + object: VarOrNodeOrLiteral::Variable("o".to_string()), + }))], + }, + format: None, + }, + ConstructResponse { + format: DataFormat::Turtle, + data: Binary::from( + " \"Test\" , \"OKP4\" .\n".to_string().as_bytes().to_vec()), + }, + ), + ( + InsertData { + format: Some(DataFormat::RDFXml), + data: read_test_data("sample.rdf.xml"), + }, + QueryMsg::Construct { + query: ConstructQuery { + prefixes: vec![ + Prefix { prefix: "my-ns".to_string(), namespace: "https://my-ns.org/".to_string() }, + Prefix { prefix: "metadata-dataset".to_string(), namespace: "https://ontology.okp4.space/dataverse/dataset/metadata/".to_string() }, + ], + construct: vec![ + msg::TripleConstructTemplate { + subject: VarOrNode::Node(NamedNode(Prefixed("my-ns:instance-1".to_string()))), + predicate: VarOrNamedNode::NamedNode(Full( + "https://my-ns/predicate/tag".to_string(), + )), + object: VarOrNodeOrLiteral::Variable("o".to_string()), + } + ], + r#where: vec![WhereCondition::Simple(TriplePattern(msg::TriplePattern { + subject: VarOrNode::Node(NamedNode(Full(id.to_string()))), + predicate: VarOrNamedNode::NamedNode(Full( + "https://ontology.okp4.space/core/hasTag".to_string(), + )), + object: VarOrNodeOrLiteral::Variable("o".to_string()), + }))], + }, + format: Some(DataFormat::NTriples), + }, + ConstructResponse { + format: DataFormat::NTriples, + data: Binary::from( + " \"Test\" .\n \"OKP4\" .\n".to_string().as_bytes().to_vec()), + }, + ), + ( + InsertData { + format: Some(DataFormat::Turtle), + data: read_test_data("blank-nodes.ttl"), + }, + QueryMsg::Construct { + query: ConstructQuery { + prefixes: vec![ + Prefix { prefix: "core".to_string(), namespace: "https://ontology.okp4.space/core/".to_string() }, + Prefix { prefix: "metadata-dataset".to_string(), namespace: "https://ontology.okp4.space/dataverse/dataset/metadata/".to_string() }, + ], + construct: vec![ + msg::TripleConstructTemplate { + subject: VarOrNode::Node(BlankNode("my-metadata".to_string())), + predicate: VarOrNamedNode::NamedNode(Full( + "https://my-ns/predicate/tcov".to_string(), + )), + object: VarOrNodeOrLiteral::Variable("tcov".to_string()), }, - ConstructResponse { - format: DataFormat::NTriples, - data: Binary::from( - " \"Test\" .\n \"OKP4\" .\n".to_string().as_bytes().to_vec()), + msg::TripleConstructTemplate { + subject: VarOrNode::Node(BlankNode("my-metadata".to_string())), + predicate: VarOrNamedNode::NamedNode(Full( + "https://my-ns/predicate/info".to_string(), + )), + object: VarOrNodeOrLiteral::Variable("info".to_string()), }, - ), - ( - InsertData { - format: Some(DataFormat::Turtle), - data: read_test_data("blank-nodes.ttl"), + msg::TripleConstructTemplate { + subject: VarOrNode::Variable("tcov".to_string()), + predicate: VarOrNamedNode::Variable("tcov_p".to_string()), + object: VarOrNodeOrLiteral::Variable("tcov_o".to_string()), }, - QueryMsg::Construct { - query: ConstructQuery { - prefixes: vec![ - Prefix { prefix: "core".to_string(), namespace: "https://ontology.okp4.space/core/".to_string() }, - Prefix { prefix: "metadata-dataset".to_string(), namespace: "https://ontology.okp4.space/dataverse/dataset/metadata/".to_string() }, - ], - construct: vec![ - msg::TripleConstructTemplate { - subject: VarOrNode::Node(BlankNode("my-metadata".to_string())), - predicate: VarOrNamedNode::NamedNode(Full( - "https://my-ns/predicate/tcov".to_string(), - )), - object: VarOrNodeOrLiteral::Variable("tcov".to_string()), - }, - msg::TripleConstructTemplate { - subject: VarOrNode::Node(BlankNode("my-metadata".to_string())), - predicate: VarOrNamedNode::NamedNode(Full( - "https://my-ns/predicate/info".to_string(), - )), - object: VarOrNodeOrLiteral::Variable("info".to_string()), - }, - msg::TripleConstructTemplate { - subject: VarOrNode::Variable("tcov".to_string()), - predicate: VarOrNamedNode::Variable("tcov_p".to_string()), - object: VarOrNodeOrLiteral::Variable("tcov_o".to_string()), - }, - msg::TripleConstructTemplate { - subject: VarOrNode::Variable("info".to_string()), - predicate: VarOrNamedNode::Variable("info_p".to_string()), - object: VarOrNodeOrLiteral::Variable("info_o".to_string()), - } - ], - r#where: vec![ - WhereCondition::Simple(TriplePattern(msg::TriplePattern { - subject: VarOrNode::Node(NamedNode(Prefixed("metadata-dataset:80b1f84e-86dc-4730-b54f-701ad9b1888a".to_string()))), - predicate: VarOrNamedNode::NamedNode(Prefixed( - "core:hasTemporalCoverage".to_string(), - )), - object: VarOrNodeOrLiteral::Variable("tcov".to_string()), - })), - WhereCondition::Simple(TriplePattern(msg::TriplePattern { - subject: VarOrNode::Node(NamedNode(Prefixed("metadata-dataset:80b1f84e-86dc-4730-b54f-701ad9b1888a".to_string()))), - predicate: VarOrNamedNode::NamedNode(Prefixed( - "core:hasInformations".to_string(), - )), - object: VarOrNodeOrLiteral::Variable("info".to_string()), - })), - WhereCondition::Simple(TriplePattern(msg::TriplePattern { - subject: VarOrNode::Variable("tcov".to_string()), - predicate: VarOrNamedNode::Variable("tcov_p".to_string()), - object: VarOrNodeOrLiteral::Variable("tcov_o".to_string()), - })), - WhereCondition::Simple(TriplePattern(msg::TriplePattern { - subject: VarOrNode::Variable("info".to_string()), - predicate: VarOrNamedNode::Variable("info_p".to_string()), - object: VarOrNodeOrLiteral::Variable("info_o".to_string()), - })) - ], - }, - format: Some(DataFormat::NTriples), - }, - ConstructResponse { - format: DataFormat::NTriples, - data: Binary::from( - " .\n .\n .\n \"this is a dataset\" .\n .\n .\n .\n \"this is a dataset\" .\n .\n .\n \"2022-01-01T00:00:00+00:00\"^^ .\n \"this is a dataset\" .\n".to_string().as_bytes().to_vec()), - }, - ), + msg::TripleConstructTemplate { + subject: VarOrNode::Variable("info".to_string()), + predicate: VarOrNamedNode::Variable("info_p".to_string()), + object: VarOrNodeOrLiteral::Variable("info_o".to_string()), + } + ], + r#where: vec![ + WhereCondition::Simple(TriplePattern(msg::TriplePattern { + subject: VarOrNode::Node(NamedNode(Prefixed("metadata-dataset:80b1f84e-86dc-4730-b54f-701ad9b1888a".to_string()))), + predicate: VarOrNamedNode::NamedNode(Prefixed( + "core:hasTemporalCoverage".to_string(), + )), + object: VarOrNodeOrLiteral::Variable("tcov".to_string()), + })), + WhereCondition::Simple(TriplePattern(msg::TriplePattern { + subject: VarOrNode::Node(NamedNode(Prefixed("metadata-dataset:80b1f84e-86dc-4730-b54f-701ad9b1888a".to_string()))), + predicate: VarOrNamedNode::NamedNode(Prefixed( + "core:hasInformations".to_string(), + )), + object: VarOrNodeOrLiteral::Variable("info".to_string()), + })), + WhereCondition::Simple(TriplePattern(msg::TriplePattern { + subject: VarOrNode::Variable("tcov".to_string()), + predicate: VarOrNamedNode::Variable("tcov_p".to_string()), + object: VarOrNodeOrLiteral::Variable("tcov_o".to_string()), + })), + WhereCondition::Simple(TriplePattern(msg::TriplePattern { + subject: VarOrNode::Variable("info".to_string()), + predicate: VarOrNamedNode::Variable("info_p".to_string()), + object: VarOrNodeOrLiteral::Variable("info_o".to_string()), + })) + ], + }, + format: Some(DataFormat::NTriples), + }, + ConstructResponse { + format: DataFormat::NTriples, + data: Binary::from( + " .\n .\n .\n \"this is a dataset\" .\n .\n .\n .\n \"this is a dataset\" .\n .\n .\n \"2022-01-01T00:00:00+00:00\"^^ .\n \"this is a dataset\" .\n".to_string().as_bytes().to_vec()), + }, + ), ]; for (data, q, expected) in cases {