2026-08-20 20:43:18 -04:00
|
|
|
use oxigraph::model::{Literal, TermRef};
|
2026-07-01 17:42:23 -04:00
|
|
|
use oxilangtag::LanguageTag;
|
2026-08-08 23:50:04 -04:00
|
|
|
use std::sync::LazyLock;
|
2026-08-20 20:43:18 -04:00
|
|
|
use spargebra::algebra::{Expression, Function, GraphPattern};
|
|
|
|
|
use spargebra::term::{NamedNode, NamedNodePattern, TermPattern, TriplePattern, Variable};
|
|
|
|
|
use tracing::debug;
|
2026-07-01 17:42:23 -04:00
|
|
|
|
|
|
|
|
pub const ENGLISH_PRIMARY: &str = "en";
|
|
|
|
|
|
|
|
|
|
pub static ENGLISH_OR_UNTAGGED: LazyLock<LanguageCondition> = LazyLock::new(|| {
|
2026-08-08 23:50:04 -04:00
|
|
|
LanguageCondition::ExactMatchOrUntagged(
|
|
|
|
|
LanguageTag::parse(ENGLISH_PRIMARY.to_string()).unwrap(),
|
|
|
|
|
)
|
2026-07-01 17:42:23 -04:00
|
|
|
});
|
|
|
|
|
|
2026-08-20 20:43:18 -04:00
|
|
|
#[derive(Clone, Debug)]
|
2026-07-01 17:42:23 -04:00
|
|
|
pub enum LanguageCondition {
|
|
|
|
|
ExactMatchOnly(LanguageTag<String>),
|
|
|
|
|
ExactMatchOrUntagged(LanguageTag<String>),
|
|
|
|
|
UntaggedOnly,
|
|
|
|
|
AnyOrNone,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl LanguageCondition {
|
2026-08-05 11:21:25 -04:00
|
|
|
pub fn primary_matches_term<'a>(&self, term: impl Into<TermRef<'a>>) -> bool {
|
|
|
|
|
if let TermRef::Literal(literal) = term.into() {
|
2026-08-08 23:50:04 -04:00
|
|
|
let tag = literal
|
|
|
|
|
.language()
|
2026-07-01 17:42:23 -04:00
|
|
|
.map(LanguageTag::parse_and_normalize)
|
|
|
|
|
.and_then(Result::ok);
|
|
|
|
|
|
|
|
|
|
match (tag, self) {
|
2026-08-08 23:50:04 -04:00
|
|
|
(Some(language), LanguageCondition::ExactMatchOnly(expectation))
|
|
|
|
|
| (Some(language), LanguageCondition::ExactMatchOrUntagged(expectation)) => {
|
|
|
|
|
language.primary_language() == expectation.primary_language()
|
|
|
|
|
}
|
2026-07-01 17:42:23 -04:00
|
|
|
(None, LanguageCondition::ExactMatchOrUntagged(_)) => true,
|
|
|
|
|
(None, LanguageCondition::UntaggedOnly) => true,
|
|
|
|
|
(_, LanguageCondition::AnyOrNone) => true,
|
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-05 11:21:25 -04:00
|
|
|
pub fn primary_language(&self) -> Option<&str> {
|
|
|
|
|
match self {
|
|
|
|
|
LanguageCondition::ExactMatchOnly(tag) => Some(tag.primary_language()),
|
|
|
|
|
LanguageCondition::ExactMatchOrUntagged(tag) => Some(tag.primary_language()),
|
|
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-20 20:43:18 -04:00
|
|
|
pub fn filter(conditions: impl IntoIterator<Item = (String, Self)>) -> String {
|
|
|
|
|
let exact_expression = |language, variable| Expression::FunctionCall(
|
|
|
|
|
Function::LangMatches, vec![
|
|
|
|
|
Expression::FunctionCall(Function::Lang, vec![
|
|
|
|
|
Expression::Variable(Variable::new_unchecked(variable))
|
|
|
|
|
]),
|
|
|
|
|
Expression::Literal(Literal::new_simple_literal(language))
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let untagged_expression = |variable| Expression::Not(
|
|
|
|
|
Box::new(Expression::FunctionCall(Function::HasLang, vec![
|
|
|
|
|
Expression::Variable(Variable::new_unchecked(variable))
|
|
|
|
|
]))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let condition_to_expression = |variable, condition| match condition {
|
|
|
|
|
LanguageCondition::ExactMatchOnly(language) =>
|
|
|
|
|
Some(exact_expression(language.to_string(), variable)),
|
|
|
|
|
LanguageCondition::ExactMatchOrUntagged(language) => {
|
|
|
|
|
Some(Expression::Or(
|
|
|
|
|
Box::new(exact_expression(language.to_string(), variable.clone())),
|
|
|
|
|
Box::new(untagged_expression(variable)),
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
LanguageCondition::UntaggedOnly => Some(untagged_expression(variable)),
|
|
|
|
|
LanguageCondition::AnyOrNone => None,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut iter = conditions.into_iter();
|
|
|
|
|
let first_expression = iter.next()
|
|
|
|
|
.and_then(|(variable, condition)| condition_to_expression(variable, condition));
|
|
|
|
|
let remaining_expressions = iter.filter_map(|(variable, condition)| condition_to_expression(variable, condition))
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
let concatenated_expressions = if let Some(expression) = first_expression {
|
|
|
|
|
if remaining_expressions.is_empty() {
|
|
|
|
|
//Expression::
|
|
|
|
|
}
|
|
|
|
|
} else { String::default() }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn to_sparql_expression(&self, var: &str) -> String {
|
|
|
|
|
let foo =
|
|
|
|
|
debug!("Test: {foo}");
|
|
|
|
|
|
2026-07-01 17:42:23 -04:00
|
|
|
match self {
|
2026-08-08 23:50:04 -04:00
|
|
|
LanguageCondition::ExactMatchOnly(language) => {
|
|
|
|
|
format!(r#"FILTER(langMATCHES(LANG(?{var}), "{language}"))"#)
|
|
|
|
|
}
|
|
|
|
|
LanguageCondition::ExactMatchOrUntagged(language) => {
|
|
|
|
|
format!(r#"FILTER(langMATCHES(LANG(?{var}), "{language}") || !hasLANG(?{var}))"#)
|
|
|
|
|
}
|
2026-07-01 17:42:23 -04:00
|
|
|
LanguageCondition::UntaggedOnly => format!("FILTER(!hasLANG(?{var}))"),
|
|
|
|
|
LanguageCondition::AnyOrNone => "".to_string(),
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-08-08 23:50:04 -04:00
|
|
|
}
|