use crate::{
entity::{Judgement, PunctuatedSentenceRef, Punctuation, Question, Stamp},
global::ClockTime,
inference::Evidential,
language::Term,
util::ToDisplayAndBrief,
};
use anyhow::Result;
use nar_dev_utils::matches_or;
use narsese::lexical::Sentence as LexicalSentence;
use serde::{Deserialize, Serialize};
pub trait Sentence: ToDisplayAndBrief + Evidential {
fn sentence_clone<'s, 'sentence: 's>(&'s self) -> impl Sentence + 'sentence;
fn content(&self) -> &Term;
fn content_mut(&mut self) -> &mut Term;
#[inline(always)]
fn clone_content(&self) -> Term {
self.content().clone()
}
type Judgement: Judgement;
type Question: Question;
fn as_punctuated_ref(&self) -> PunctuatedSentenceRef<Self::Judgement, Self::Question>;
#[doc(alias = "type")]
#[doc(alias = "sentence_type")]
#[inline]
fn punctuation(&self) -> Punctuation {
self.as_punctuated_ref().into()
}
fn is_judgement(&self) -> bool {
matches!(
self.as_punctuated_ref(),
PunctuatedSentenceRef::Judgement(..)
)
}
fn as_judgement(&self) -> Option<&Self::Judgement> {
matches_or! {
?self.as_punctuated_ref(),
PunctuatedSentenceRef::Judgement(j) => j
}
}
fn unwrap_judgement(&self) -> &Self::Judgement {
self.as_judgement().unwrap()
}
fn is_question(&self) -> bool {
matches!(
self.as_punctuated_ref(),
PunctuatedSentenceRef::Question(..)
)
}
fn as_question(&self) -> Option<&Self::Question> {
matches_or! {
?self.as_punctuated_ref(),
PunctuatedSentenceRef::Question(q) => q
}
}
fn unwrap_question(&self) -> &Self::Question {
self.as_question().unwrap()
}
#[inline(always)]
fn contain_query_var(&self) -> bool {
self.content().contain_var_q()
}
#[doc(alias = "to_key_string")]
fn to_key(&self) -> String;
fn sentence_to_display(&self) -> String;
fn sentence_to_display_brief(&self) -> String {
self.to_key() + &self.stamp_to_display()
}
fn sentence_to_display_long(&self) -> String {
self.sentence_to_display()
}
fn sentence_to_lexical(&self) -> LexicalSentence;
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct SentenceInner {
content: Term,
stamp: Stamp,
}
impl SentenceInner {
pub fn content(&self) -> &Term {
&self.content
}
pub fn content_mut(&mut self) -> &mut Term {
&mut self.content
}
pub fn stamp(&self) -> &Stamp {
&self.stamp
}
pub fn stamp_mut(&mut self) -> &mut Stamp {
&mut self.stamp
}
}
impl SentenceInner {
pub fn new(content: Term, stamp: Stamp) -> Self {
Self { content, stamp }
}
pub fn from_lexical(
lexical: LexicalSentence,
stamp_current_serial: ClockTime,
stamp_time: ClockTime,
) -> Result<Self> {
let LexicalSentence { term, stamp, .. } = lexical;
let content = Term::try_from(term)?;
let stamp = Stamp::from_lexical(stamp, stamp_current_serial, stamp_time)?;
Ok(Self::new(content, stamp))
}
}