.
This commit is contained in:
+50
-3
@@ -1,6 +1,9 @@
|
||||
use oxigraph::model::TermRef;
|
||||
use oxigraph::model::{Literal, TermRef};
|
||||
use oxilangtag::LanguageTag;
|
||||
use std::sync::LazyLock;
|
||||
use spargebra::algebra::{Expression, Function, GraphPattern};
|
||||
use spargebra::term::{NamedNode, NamedNodePattern, TermPattern, TriplePattern, Variable};
|
||||
use tracing::debug;
|
||||
|
||||
pub const ENGLISH_PRIMARY: &str = "en";
|
||||
|
||||
@@ -10,7 +13,7 @@ pub static ENGLISH_OR_UNTAGGED: LazyLock<LanguageCondition> = LazyLock::new(|| {
|
||||
)
|
||||
});
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum LanguageCondition {
|
||||
ExactMatchOnly(LanguageTag<String>),
|
||||
ExactMatchOrUntagged(LanguageTag<String>),
|
||||
@@ -49,7 +52,51 @@ impl LanguageCondition {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_filter_expression(&self, var: &str) -> String {
|
||||
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}");
|
||||
|
||||
match self {
|
||||
LanguageCondition::ExactMatchOnly(language) => {
|
||||
format!(r#"FILTER(langMATCHES(LANG(?{var}), "{language}"))"#)
|
||||
|
||||
Reference in New Issue
Block a user