Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 84 additions & 7 deletions contracts/okp4-cognitarium/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ pub fn query(deps: Deps<'_>, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
QueryMsg::Describe { query, format } => to_binary(&query::describe(
deps,
query,
format.unwrap_or(DataFormat::Turtle),
format.unwrap_or(DataFormat::default()),
)?),
QueryMsg::Construct { query, format } => to_binary(&query::construct(
deps,
query,
format.unwrap_or(DataFormat::default()),
)?),
}
}
Expand All @@ -90,9 +95,9 @@ pub mod query {

use super::*;
use crate::msg::{
DescribeQuery, DescribeResponse, Node, Prefix, SelectItem, SelectQuery, SelectResponse,
SimpleWhereCondition, StoreResponse, TriplePattern, Value, VarOrNamedNode, VarOrNode,
VarOrNodeOrLiteral, WhereCondition,
ConstructQuery, DescribeQuery, DescribeResponse, Node, Prefix, SelectItem, SelectQuery,
SelectResponse, SimpleWhereCondition, StoreResponse, TriplePattern, Value, VarOrNamedNode,
VarOrNode, VarOrNodeOrLiteral, WhereCondition,
};
use crate::querier::{PlanBuilder, QueryEngine};
use crate::rdf::{self, Atom, TripleWriter};
Expand Down Expand Up @@ -224,6 +229,14 @@ pub mod query {
data: Binary::from(out),
})
}

pub fn construct(
_deps: Deps<'_>,
_query: ConstructQuery,
_format: DataFormat,
) -> StdResult<SelectResponse> {
Err(StdError::generic_err("Not implemented"))
}
}

#[cfg(test)]
Expand All @@ -232,12 +245,13 @@ mod tests {
use crate::error::StoreError;
use crate::msg::ExecuteMsg::InsertData;
use crate::msg::Node::NamedNode;
use crate::msg::QueryMsg::Construct;
use crate::msg::SimpleWhereCondition::TriplePattern;
use crate::msg::IRI::{Full, Prefixed};
use crate::msg::{
DescribeQuery, DescribeResponse, Head, Literal, Prefix, Results, SelectItem, SelectQuery,
SelectResponse, StoreLimitsInput, StoreLimitsInputBuilder, StoreResponse, Value,
VarOrNamedNode, VarOrNode, VarOrNodeOrLiteral, WhereCondition,
ConstructQuery, DescribeQuery, DescribeResponse, Head, Literal, Prefix, Results,
SelectItem, SelectQuery, SelectResponse, StoreLimitsInput, StoreLimitsInputBuilder,
StoreResponse, Value, VarOrNamedNode, VarOrNode, VarOrNodeOrLiteral, WhereCondition,
};
use crate::state::{
namespaces, triples, Namespace, Node, Object, StoreLimits, StoreStat, Subject, Triple,
Expand Down Expand Up @@ -1354,4 +1368,67 @@ mod tests {
);
}
}

#[test]
fn proper_construct() {
let id = "https://ontology.okp4.space/dataverse/dataspace/metadata/dcf48417-01c5-4b43-9bc7-49e54c028473";
let cases = vec![(
ConstructQuery {
prefixes: vec![],
construct: vec![msg::TriplePattern {
subject: VarOrNode::Node(NamedNode(Full(id.to_string()))),
predicate: VarOrNode::Variable("p".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: VarOrNode::Node(NamedNode(Full(
"https://ontology.okp4.space/core/hasTopic".to_string(),
))),
object: VarOrNodeOrLiteral::Node(NamedNode(Full(
"https://ontology.okp4.space/thesaurus/topic/Test".to_string(),
))),
}))],
},
0,
)];

for case in cases {
let mut deps = mock_dependencies();

let info = mock_info("owner", &[]);
instantiate(
deps.as_mut(),
mock_env(),
info.clone(),
InstantiateMsg {
limits: StoreLimitsInput::default(),
},
)
.unwrap();

execute(
deps.as_mut(),
mock_env(),
info.clone(),
InsertData {
format: Some(DataFormat::RDFXml),
data: read_test_data("sample.rdf.xml"),
},
)
.unwrap();

let res = query(
deps.as_ref(),
mock_env(),
Construct {
query: case.0,
format: Some(DataFormat::default()),
},
);

assert!(res.is_err());
assert_eq!(res.err().unwrap(), StdError::generic_err("Not implemented"));
}
}
}
40 changes: 40 additions & 0 deletions contracts/okp4-cognitarium/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,26 @@ pub enum QueryMsg {
/// If not provided, the default format is [Turtle](https://www.w3.org/TR/turtle/) format.
format: Option<DataFormat>,
},

/// # Construct
///
/// Returns the resources matching the criteria defined by the provided query as a set of RDF
/// triples serialized in the provided format.
#[returns(ConstructResponse)]
Construct {
/// The query to execute.
query: ConstructQuery,
/// The format in which the triples are serialized.
/// If not provided, the default format is [Turtle](https://www.w3.org/TR/turtle/) format.
format: Option<DataFormat>,
},
}

/// # DataFormat
/// Represents the format in which the data are serialized, for example when returned by a query or
/// when inserted in the store.
#[cw_serde]
#[derive(Default)]
pub enum DataFormat {
/// # RDF XML
/// Output in [RDF/XML](https://www.w3.org/TR/rdf-syntax-grammar/) format.
Expand All @@ -121,6 +135,7 @@ pub enum DataFormat {
/// # Turtle
/// Output in [Turtle](https://www.w3.org/TR/turtle/) format.
#[serde(rename = "turtle")]
#[default]
Turtle,
/// # N-Triples
/// Output in [N-Triples](https://www.w3.org/TR/n-triples/) format.
Expand Down Expand Up @@ -308,6 +323,16 @@ pub struct DescribeResponse {
pub data: Binary,
}

/// # ConstructResponse
/// Represents the response of a [QueryMsg::Construct] query.
#[cw_serde]
pub struct ConstructResponse {
/// The format of the data.
pub format: DataFormat,
/// The data serialized in the specified format.
pub data: Binary,
}

/// # Head
/// Represents the head of a [SelectResponse].
#[cw_serde]
Expand Down Expand Up @@ -389,6 +414,21 @@ pub struct DescribeQuery {
pub r#where: WhereClause,
}

/// # ConstructQuery
/// Represents a CONSTRUCT query over the triple store, allowing to retrieve a set of triples
/// serialized in a specific format.
#[cw_serde]
pub struct ConstructQuery {
/// The prefixes used in the query.
pub prefixes: Vec<Prefix>,
/// The triples to construct.
/// If nothing is provided, the patterns from the `where` clause are used for construction.
pub construct: Vec<TriplePattern>,
/// The WHERE clause.
/// This clause is used to specify the triples to construct using variable bindings.
pub r#where: WhereClause,
}

/// # Prefix
/// Represents a prefix in a [SelectQuery]. A prefix is a shortcut for a namespace used in the query.
#[cw_serde]
Expand Down
29 changes: 29 additions & 0 deletions docs/okp4-cognitarium.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,27 @@ Returns a description of the resource identified by the provided IRI as a set of
|`describe.format`|**[DataFormat](#dataformat)\|null**. The format in which the triples are serialized. If not provided, the default format is [Turtle](https://www.w3.org/TR/turtle/) format.|
|`describe.query`|*(Required.) * **[DescribeQuery](#describequery)**. The query to execute.|

### QueryMsg::Construct

Returns the resources matching the criteria defined by the provided query as a set of RDF triples serialized in the provided format.

|parameter|description|
|----------|-----------|
|`construct`|*(Required.) * **object**. |
|`construct.format`|**[DataFormat](#dataformat)\|null**. The format in which the triples are serialized. If not provided, the default format is [Turtle](https://www.w3.org/TR/turtle/) format.|
|`construct.query`|*(Required.) * **[ConstructQuery](#constructquery)**. The query to execute.|

## Responses

### construct

Represents the response of a [QueryMsg::Construct] query.

|property|description|
|----------|-----------|
|`data`|*(Required.) * **[Binary](#binary)**. The data serialized in the specified format.|
|`format`|*(Required.) * **[DataFormat](#dataformat)**. The format of the data.|

### describe

Represents the response of a [QueryMsg::Describe] query.
Expand Down Expand Up @@ -143,6 +162,16 @@ An RDF [blank node](https://www.w3.org/TR/rdf11-concepts/#dfn-blank-node).
|----------|-----------|
|`blank_node`|*(Required.) * **string**. |

### ConstructQuery

Represents a CONSTRUCT query over the triple store, allowing to retrieve a set of triples serialized in a specific format.

|property|description|
|----------|-----------|
|`construct`|*(Required.) * **Array&lt;[TriplePattern](#triplepattern)&gt;**. The triples to construct. If nothing is provided, the patterns from the `where` clause are used for construction.|
|`prefixes`|*(Required.) * **Array&lt;[Prefix](#prefix)&gt;**. The prefixes used in the query.|
|`where`|*(Required.) * **Array&lt;[WhereCondition](#wherecondition)&gt;**. The WHERE clause. This clause is used to specify the triples to construct using variable bindings.|

### DataFormat

Represents the format in which the data are serialized, for example when returned by a query or when inserted in the store.
Expand Down