Skip to content

Commit

Permalink
add tuple terms
Browse files Browse the repository at this point in the history
  • Loading branch information
matzemathics committed Dec 1, 2023
1 parent c3d7b8a commit d98a901
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 3 deletions.
1 change: 1 addition & 0 deletions nemo-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ fn constant_to_python<'a>(py: Python<'a>, v: &Constant) -> PyResult<&'a PyAny> {
Ok(Py::new(py, lit)?.to_object(py).into_ref(py))
})(),
Constant::MapLiteral(_map) => todo!("maps are not yet supported"),
Constant::TupleLiteral(_tuple) => todo!("tuples are not yet supported"),
}
}

Expand Down
1 change: 1 addition & 0 deletions nemo-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ impl NemoResults {
Constant::StringLiteral(s) => JsValue::from(s),
Constant::RdfLiteral(lit) => JsValue::from(lit.to_string()),
Constant::MapLiteral(_map) => todo!("maps are not yet supported"),
Constant::TupleLiteral(_tuple) => todo!("tuples are not yet supported"),
},
PrimitiveLogicalValueT::String(s) => JsValue::from(String::from(s)),
PrimitiveLogicalValueT::Integer(i) => JsValue::from(i64::from(i)),
Expand Down
3 changes: 3 additions & 0 deletions nemo/src/model/rule_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ pub use constraint::*;

mod map;
pub use map::*;

mod tuple;
pub use tuple::*;
6 changes: 5 additions & 1 deletion nemo/src/model/rule_model/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
program_analysis::variable_order::VariableOrder,
};

use super::{Aggregate, Identifier, Map, NumericLiteral, RdfLiteral};
use super::{Aggregate, Identifier, Map, NumericLiteral, RdfLiteral, Tuple};

/// Variable that can be bound to a specific value
#[derive(Debug, Eq, PartialEq, Hash, Clone, PartialOrd, Ord)]
Expand Down Expand Up @@ -84,6 +84,8 @@ pub enum Constant {
RdfLiteral(RdfLiteral),
/// A map literal.
MapLiteral(Map),
/// A tuple literal
TupleLiteral(Tuple),
}

impl Constant {
Expand All @@ -95,6 +97,7 @@ impl Constant {
Self::StringLiteral(_) => Some(PrimitiveType::String),
Self::NumericLiteral(nl) => Some(nl.primitive_type()),
Self::MapLiteral(_) => None,
Self::TupleLiteral(_) => None,
}
}

Expand Down Expand Up @@ -145,6 +148,7 @@ impl Display for Constant {
Constant::StringLiteral(literal) => write!(f, "\"{}\"", literal),
Constant::RdfLiteral(literal) => write!(f, "{}", literal),
Constant::MapLiteral(term) => write!(f, "{term}"),
Constant::TupleLiteral(tuple) => write!(f, "{tuple}"),
}
}
}
Expand Down
55 changes: 55 additions & 0 deletions nemo/src/model/rule_model/tuple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::{ops::Deref, sync::Arc};

use super::Constant;

/// A Tuple: a [Constant]
#[derive(Debug, Eq, PartialEq, Clone, PartialOrd, Ord)]
pub struct Tuple {
values: Arc<[Constant]>,
}

impl Default for Tuple {
fn default() -> Self {
Self {
values: [].into_iter().collect(),
}
}
}

impl Tuple {
/// Returns the size of the tuple.
pub fn arity(&self) -> usize {
self.values.len()
}
}

impl Deref for Tuple {
type Target = [Constant];

fn deref(&self) -> &[Constant] {
&self.values
}
}

impl FromIterator<Constant> for Tuple {
fn from_iter<T: IntoIterator<Item = Constant>>(iter: T) -> Self {
Self {
values: iter.into_iter().collect(),
}
}
}

impl std::fmt::Display for Tuple {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "(")?;
f.write_str(
&self
.iter()
.map(|x| ToString::to_string(x))
.by_ref()
.intersperse(", ".into())
.collect::<String>(),
)?;
write!(f, ")")
}
}
13 changes: 11 additions & 2 deletions nemo/src/model/types/primitive_logical_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use nemo_physical::{
};

use crate::model::{
Constant, Identifier, Map, NumericLiteral, RdfLiteral, XSD_DECIMAL, XSD_DOUBLE, XSD_INTEGER,
Constant, Identifier, Map, NumericLiteral, RdfLiteral, Tuple, XSD_DECIMAL, XSD_DOUBLE,
XSD_INTEGER,
};

use super::{error::InvalidRuleTermConversion, primitive_types::PrimitiveType};
Expand All @@ -23,6 +24,7 @@ const DOUBLE_PREFIX: &str = "DO:";
const CONSTANT_PREFIX: &str = "CO:";
const DATATYPE_VALUE_PREFIX: &str = "DV:";
const MAP_VALUE_PREFIX: &str = "MP:";
const TUPLE_VALUE_PREFIX: &str = "TP:";

/// The prefix used to indicate constants that are Nulls
pub const LOGICAL_NULL_PREFIX: &str = "__Null#";
Expand Down Expand Up @@ -188,6 +190,12 @@ impl From<Map> for PhysicalString {
}
}

impl From<Tuple> for PhysicalString {
fn from(value: Tuple) -> Self {
format!("{TUPLE_VALUE_PREFIX}{value}").into()
}
}

impl From<LogicalInteger> for LogicalString {
fn from(value: LogicalInteger) -> Self {
value.0.to_string().into()
Expand Down Expand Up @@ -330,7 +338,8 @@ impl TryFrom<Constant> for PhysicalString {
Constant::RdfLiteral(RdfLiteral::DatatypeValue { value, datatype }) => {
Ok(DatatypeValue(value, datatype).into())
}
Constant::MapLiteral(value) => Ok(value.into()),
Constant::MapLiteral(value) => Ok(PhysicalString::from(value)),
Constant::TupleLiteral(tuple) => Ok(PhysicalString::from(tuple)),
}
}
}
Expand Down

0 comments on commit d98a901

Please sign in to comment.