.
This commit is contained in:
+31
-25
@@ -1,16 +1,14 @@
|
||||
use oxigraph::model::{Literal, TermRef};
|
||||
use oxilangtag::LanguageTag;
|
||||
use std::sync::LazyLock;
|
||||
use spargebra::algebra::{Expression, Function, GraphPattern};
|
||||
use spargebra::term::Variable;
|
||||
use std::sync::LazyLock;
|
||||
|
||||
pub static ENGLISH_TAG: LazyLock<LanguageTag<String>> = LazyLock::new(|| {
|
||||
LanguageTag::parse("en".to_string()).unwrap()
|
||||
});
|
||||
pub static ENGLISH_TAG: LazyLock<LanguageTag<String>> =
|
||||
LazyLock::new(|| LanguageTag::parse("en".to_string()).unwrap());
|
||||
|
||||
pub static ENGLISH_OR_UNTAGGED: LazyLock<LanguageCondition> = LazyLock::new(|| {
|
||||
LanguageCondition::ExactMatchOrUntagged(ENGLISH_TAG.clone())
|
||||
});
|
||||
pub static ENGLISH_OR_UNTAGGED: LazyLock<LanguageCondition> =
|
||||
LazyLock::new(|| LanguageCondition::ExactMatchOrUntagged(ENGLISH_TAG.clone()));
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum LanguageCondition {
|
||||
@@ -54,20 +52,23 @@ impl LanguageCondition {
|
||||
pub fn filter(&self, variable: impl Into<String>) -> String {
|
||||
let variable = variable.into();
|
||||
|
||||
let exact = |language: &LanguageTag<String>| Expression::FunctionCall(
|
||||
Function::LangMatches, vec![
|
||||
Expression::FunctionCall(Function::Lang, vec![
|
||||
Expression::Variable(Variable::new_unchecked(&variable))
|
||||
]),
|
||||
Expression::Literal(Literal::new_simple_literal(language.to_string()))
|
||||
],
|
||||
);
|
||||
|
||||
let untagged = Expression::Not(
|
||||
Box::new(Expression::FunctionCall(Function::HasLang, vec![
|
||||
Expression::Variable(Variable::new_unchecked(&variable))
|
||||
]))
|
||||
);
|
||||
let exact = |language: &LanguageTag<String>| {
|
||||
Expression::FunctionCall(
|
||||
Function::LangMatches,
|
||||
vec![
|
||||
Expression::FunctionCall(
|
||||
Function::Lang,
|
||||
vec![Expression::Variable(Variable::new_unchecked(&variable))],
|
||||
),
|
||||
Expression::Literal(Literal::new_simple_literal(language.to_string())),
|
||||
],
|
||||
)
|
||||
};
|
||||
|
||||
let untagged = Expression::Not(Box::new(Expression::FunctionCall(
|
||||
Function::HasLang,
|
||||
vec![Expression::Variable(Variable::new_unchecked(&variable))],
|
||||
)));
|
||||
|
||||
match self {
|
||||
Self::ExactMatchOnly(language) => Some(exact(language)),
|
||||
@@ -77,9 +78,14 @@ impl LanguageCondition {
|
||||
)),
|
||||
Self::UntaggedOnly => Some(untagged),
|
||||
Self::AnyOrNone => None,
|
||||
}.map(|expr| GraphPattern::Filter {
|
||||
expr,
|
||||
inner: Box::new(GraphPattern::Bgp { patterns: vec![] }),
|
||||
}.to_string()).unwrap_or_default()
|
||||
}
|
||||
.map(|expr| {
|
||||
GraphPattern::Filter {
|
||||
expr,
|
||||
inner: Box::new(GraphPattern::Bgp { patterns: vec![] }),
|
||||
}
|
||||
.to_string()
|
||||
})
|
||||
.unwrap_or_default()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user