pub struct CompoundTermRef<'a> {
pub inner: &'a Term,
pub components: &'a [Term],
}Expand description
🆕作为「复合词项引用」的词项类型
- 🎯在程序类型层面表示一个「复合词项」(不可变引用)
Fields§
§inner: &'a Term复合词项整体
components: &'a [Term]复合词项的元素列表
Implementations§
Source§impl<'s> CompoundTermRef<'s>
impl<'s> CompoundTermRef<'s>
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
📄OpenNARS CompoundTerm.size
- 🚩直接链接到
TermComponents的属性 - ⚠️对「像」不包括「像占位符」
- 📄
(/, A, _, B)的size为2而非3
- 📄
§📄OpenNARS
get the number of components
Sourcepub fn component_at(self, index: usize) -> Option<&'s Term>
pub fn component_at(self, index: usize) -> Option<&'s Term>
📄OpenNARS CompoundTerm.componentAt
- 🚩直接连接到
TermComponents的方法 - ⚠️对「像」不受「像占位符」位置影响
§📄OpenNARS
get a component by index
Sourcepub unsafe fn component_at_unchecked(&self, index: usize) -> &Term
pub unsafe fn component_at_unchecked(&self, index: usize) -> &Term
📄OpenNARS CompoundTerm.componentAt
- 🆕unsafe版本:若已知词项的组分数,则可经此对症下药
- 🚩直接连接到
TermComponents的方法 - ⚠️对「像」不受「像占位符」位置影响
§Safety
⚠️只有在「确保索引不会越界」才不会引发panic
§📄OpenNARS
get a component by index
Sourcepub fn index_of_component(&self, t: &Term) -> Option<usize>
pub fn index_of_component(&self, t: &Term) -> Option<usize>
🆕改版 CompoundTerm.indexOfComponent
@param t & @return [] index or -1
Sourcepub fn clone_components(&self) -> Vec<Term>
pub fn clone_components(&self) -> Vec<Term>
Sourcepub fn clone_component_refs(&self) -> Vec<&Term>
pub fn clone_component_refs(&self) -> Vec<&Term>
📄OpenNARS CompoundTerm.cloneComponents
- 🚩只拷贝所有元素的引用,无需拷贝其中的值
Sourcepub fn contain_component(&self, component: &Term) -> bool
pub fn contain_component(&self, component: &Term) -> bool
📄OpenNARS CompoundTerm.containComponent
- 🎯检查其是否包含直接组分
- 🚩直接基于已有迭代器方法
§📄OpenNARS
Check whether the compound contains a certain component
Sourcepub fn contain_term(&self, term: &Term) -> bool
pub fn contain_term(&self, term: &Term) -> bool
📄OpenNARS CompoundTerm.containTerm
- 🎯检查其是否递归包含组分
- 🚩直接基于已有迭代器方法:词项 == 组分 || 词项 in 组分
§📄OpenNARS
Recursively check if a compound contains a term
Sourcepub fn contain_all_components(&self, other: &Term) -> bool
pub fn contain_all_components(&self, other: &Term) -> bool
📄OpenNARS CompoundTerm.containAllComponents
- 🎯分情况检查「是否包含所有组分」
- 📌同类⇒检查其是否包含
other的所有组分 - 📌异类⇒检查其是否包含
other作为整体
- 📌同类⇒检查其是否包含
- 🚩直接基于已有迭代器方法
§📄OpenNARS
Check whether the compound contains all components of another term, or that term as a whole
Sourcepub fn as_conditional(self) -> Option<(StatementRef<'s>, CompoundTermRef<'s>)>
pub fn as_conditional(self) -> Option<(StatementRef<'s>, CompoundTermRef<'s>)>
Source§impl CompoundTermRef<'_>
impl CompoundTermRef<'_>
Source§impl<'a> CompoundTermRef<'a>
impl<'a> CompoundTermRef<'a>
Sourcepub fn get_placeholder_index(self) -> usize
pub fn get_placeholder_index(self) -> usize
📄OpenNARS getRelationIndex 属性
- 🎯用于获取「像」的关系索引
- 🆕⚠️现在是获取「占位符位置」
- 📝原先OpenNARS是将「关系词项」放在占位符处的,现在是根据《NAL》原意,将「关系词项」统一放在「第一个词项」处
- 📌所以后续所有的「索引」都变成了「占位符位置」
- 💭【2024-05-11 14:40:15】后续可能会在这点上有隐患——随后要注意这种差别
§Panics
! ⚠️仅限于「像」的TermComponents::MultiIndexed词项
- 若尝试获取「非『像』词项」的关系索引,则会panic
- 🚩【2024-06-12 22:53:09】本来就不应该对「非像词项」调用该函数——严格跟「像」类型绑定
§📄OpenNARS
get the index of the relation in the component list
@return the index of relation
Sourcepub fn get_relation(self) -> &'a Term
pub fn get_relation(self) -> &'a Term
📄OpenNARS getRelation 属性
- 🎯用于获取「像」的「关系词项」
- ⚠️若尝试获取「非『像』词项」的关系词项,则会panic
- 🆕按NARust「索引=占位符索引」的来:总是在索引
0处
§📄OpenNARS
Get the relation term in the Image
@return The term representing a relation
Sourcepub fn get_the_other_component(self) -> Option<&'a Term>
pub fn get_the_other_component(self) -> Option<&'a Term>
📄OpenNARS getTheOtherComponent 属性
- 🎯用于获取「像」的「另一词项」
- ⚠️若尝试获取「非『像』词项」的词项,则会panic
§📄OpenNARS
Get the other term in the Image
@return The term related
Source§impl<'s> CompoundTermRef<'s>
为「复合词项」添加「转换到陈述」的方法
impl<'s> CompoundTermRef<'s>
为「复合词项」添加「转换到陈述」的方法
- 📌依据:陈述 ⊂ 复合词项
Sourcepub fn is_statement(&self) -> bool
pub fn is_statement(&self) -> bool
🆕判断一个复合词项是否为「陈述词项」
- 🚩判断其「内部元素」的个数是否为2
- 📌与
Term::is_statement一致
Sourcepub fn as_statement(self) -> Option<StatementRef<'s>>
pub fn as_statement(self) -> Option<StatementRef<'s>>
🆕将一个复合词项转换为「陈述词项」(不可变引用)
- 🚩转换为Option
- 📌与
Term::as_statement一致
Methods from Deref<Target = Term>§
Sourcepub fn identifier(&self) -> &str
pub fn identifier(&self) -> &str
只读的「标识符」属性
Sourcepub fn components(&self) -> &TermComponents
pub fn components(&self) -> &TermComponents
只读的「组分」属性
Sourcepub fn is_placeholder(&self) -> bool
pub fn is_placeholder(&self) -> bool
判断其是否为「占位符」
- 🎯【2024-04-21 01:04:17】在「词法折叠」中首次使用
Sourcepub fn id_comp(&self) -> (&str, &TermComponents)
pub fn id_comp(&self) -> (&str, &TermComponents)
快捷获取「标识符-组分」二元组
- 🎯用于很多地方的「类型匹配」
Sourcepub fn contain_type(&self, identifier: &str) -> bool
pub fn contain_type(&self, identifier: &str) -> bool
判断「是否包含指定类型的词项」
- 🎯支持「词项」中的方法,递归判断「是否含有变量」
Sourcepub fn for_each_atom(&self, f: &mut impl FnMut(&Term))
pub fn for_each_atom(&self, f: &mut impl FnMut(&Term))
遍历其中所有原子词项
- 🎯找到其中所有的变量
- ⚠️外延像/内涵像 中的占位符
- ⚠️需要传入闭包的可变引用,而非闭包本身
- 📌中间「递归深入」需要重复调用(传入)闭包
- 📄词语、变量
- 📄占位符
Sourcepub fn format_name(&self) -> String
pub fn format_name(&self) -> String
格式化名称
- 🚩以方便打印的「内部方言语法」呈现Narsese
- 📌括号全用 圆括号
- 📌无逗号分隔符
Sourcepub fn to_lexical(&self) -> TermLexical
pub fn to_lexical(&self) -> TermLexical
从「内部Narsese」转换为「词法Narsese」
- 🚩基本无损转换(无需考虑失败情况)
Sourcepub fn format_ascii(&self) -> String
pub fn format_ascii(&self) -> String
转换为显示呈现上的ASCII格式
- 📌对标OpenNARS的默认呈现
- ⚠️【2024-07-02 00:52:54】目前需要「词法Narsese」作为中间格式,可能会有性能损失
Sourcepub fn name(&self) -> String
pub fn name(&self) -> String
模拟Term.getName
- 🆕使用自身内建的「获取名称」方法
- 相较OpenNARS更短
- 仍能满足OpenNARS的需求
- 🎯OpenNARS原有需求
- 📌保证「词项不同 ⇔ 名称不同」
- 📌保证「可用于『概念』『记忆区』的索引」
§📄OpenNARS
Reporting the name of the current Term.
@return The name of the term as a String
Sourcepub fn complexity(&self) -> usize
pub fn complexity(&self) -> usize
模拟Term.getComplexity
- 🚩逻辑 from OpenNARS
- 原子 ⇒ 1
变量 ⇒ 0- 复合 ⇒ 1 + 所有组分复杂度之和
§📄OpenNARS
- The syntactic complexity, for constant atomic Term, is 1.
- The complexity of the term is the sum of those of the components plus 1
The syntactic complexity of a variable is 0, because it does not refer to * any concept.
@return The complexity of the term, an integer
Sourcepub fn is_zero_complexity(&self) -> bool
pub fn is_zero_complexity(&self) -> bool
🆕判断是否为「零复杂度」
- 🎯用于部分「除以复杂度」的函数
Sourcepub fn is_same_type(&self, other: &Self) -> bool
pub fn is_same_type(&self, other: &Self) -> bool
🆕用于替代Java的x.getClass() == y.getClass()
Sourcepub fn instanceof_compound_pure(&self) -> bool
pub fn instanceof_compound_pure(&self) -> bool
🆕用于判断是否为「纯复合词项」
- ⚠️不包括陈述
Sourcepub fn as_compound_type(
&self,
compound_class: impl AsRef<str>,
) -> Option<CompoundTermRef<'_>>
pub fn as_compound_type( &self, compound_class: impl AsRef<str>, ) -> Option<CompoundTermRef<'_>>
Sourcepub fn instanceof_compound(&self) -> bool
pub fn instanceof_compound(&self) -> bool
🆕用于判断是否为「复合词项」
- ⚠️包括陈述
- 📄OpenNARS
instanceof CompoundTerm逻辑
Sourcepub fn instanceof_set_ext(&self) -> bool
pub fn instanceof_set_ext(&self) -> bool
🆕用于判断是否为「外延集」
- 📄OpenNARS
instanceof SetExt逻辑 - 🎯
crate::inference推理规则分派
Sourcepub fn instanceof_set_int(&self) -> bool
pub fn instanceof_set_int(&self) -> bool
🆕用于判断是否为「内涵集」
- 📄OpenNARS
instanceof SetInt逻辑 - 🎯
crate::inference推理规则分派
Sourcepub fn instanceof_set(&self) -> bool
pub fn instanceof_set(&self) -> bool
🆕用于判断是否为「词项集」
- 📄OpenNARS
instanceof SetExt || instanceof SetInt逻辑
Sourcepub fn instanceof_intersection_ext(&self) -> bool
pub fn instanceof_intersection_ext(&self) -> bool
🆕用于判断是否为「外延交」
- 📄OpenNARS
instanceof IntersectionExt逻辑 - 🎯
crate::inference推理规则分派
Sourcepub fn instanceof_intersection_int(&self) -> bool
pub fn instanceof_intersection_int(&self) -> bool
🆕用于判断是否为「内涵交」
- 📄OpenNARS
instanceof IntersectionInt逻辑 - 🎯
crate::inference推理规则分派
Sourcepub fn instanceof_intersection(&self) -> bool
pub fn instanceof_intersection(&self) -> bool
🆕用于判断是否为「词项交集」
- 📄OpenNARS
instanceof IntersectionExt || instanceof IntersectionInt逻辑 - 🎯首次用于[
crate::inference::StructuralRules::__switch_order]
Sourcepub fn instanceof_difference_ext(&self) -> bool
pub fn instanceof_difference_ext(&self) -> bool
🆕用于判断是否为「外延差」
- 📄OpenNARS
instanceof DifferenceExt逻辑 - 🎯
crate::inference推理规则分派
Sourcepub fn instanceof_difference_int(&self) -> bool
pub fn instanceof_difference_int(&self) -> bool
🆕用于判断是否为「内涵差」
- 📄OpenNARS
instanceof DifferenceInt逻辑 - 🎯
crate::inference推理规则分派
Sourcepub fn instanceof_difference(&self) -> bool
pub fn instanceof_difference(&self) -> bool
🆕用于判断是否为「词项差集」
- 📄OpenNARS
instanceof DifferenceExt || instanceof DifferenceInt逻辑
Sourcepub fn instanceof_product(&self) -> bool
pub fn instanceof_product(&self) -> bool
🆕用于判断是否为「乘积」
- 📄OpenNARS
instanceof Product逻辑 - 🎯
crate::inference推理规则分派
Sourcepub fn instanceof_image_ext(&self) -> bool
pub fn instanceof_image_ext(&self) -> bool
🆕用于判断是否为「外延像」
- 📄OpenNARS
instanceof ImageExt逻辑 - 🎯
crate::inference推理规则分派
Sourcepub fn instanceof_image_int(&self) -> bool
pub fn instanceof_image_int(&self) -> bool
🆕用于判断是否为「内涵像」
- 📄OpenNARS
instanceof ImageInt逻辑 - 🎯
crate::inference推理规则分派
Sourcepub fn instanceof_image(&self) -> bool
pub fn instanceof_image(&self) -> bool
🆕用于判断是否为「像」
- 📄OpenNARS
instanceof ImageExt || instanceof ImageInt逻辑
Sourcepub fn instanceof_conjunction(&self) -> bool
pub fn instanceof_conjunction(&self) -> bool
🆕用于判断是否为「合取」
- 📄OpenNARS
instanceof Conjunction逻辑 - 🎯
crate::inference推理规则分派
Sourcepub fn instanceof_disjunction(&self) -> bool
pub fn instanceof_disjunction(&self) -> bool
🆕用于判断是否为「析取」
- 📄OpenNARS
instanceof Disjunction逻辑 - 🎯
crate::inference推理规则分派
Sourcepub fn instanceof_junction(&self) -> bool
pub fn instanceof_junction(&self) -> bool
🆕用于判断是否为「词项差集」
- 📄OpenNARS
instanceof Conjunction || instanceof Disjunction逻辑
Sourcepub fn instanceof_negation(&self) -> bool
pub fn instanceof_negation(&self) -> bool
🆕用于判断是否为「否定」
- 📄OpenNARS
instanceof Negation逻辑 - 🎯
crate::inference推理规则分派
Sourcepub fn is_commutative(&self) -> bool
pub fn is_commutative(&self) -> bool
📄OpenNARS CompoundTerm.isCommutative
- 📌对「零元/一元 词项」默认为「不可交换」
- 📜返回
false - 📄OpenNARS中
Negation的定义(即默认「不可交换」)
- 📜返回
§📄OpenNARS
Check if the order of the components matters
Commutative CompoundTerms: Sets, Intersections Commutative Statements: Similarity, Equivalence (except the one with a temporal order) Commutative CompoundStatements: Disjunction, Conjunction (except the one with a temporal order)
Sourcepub fn structural_match(&self, other: &Self) -> bool
pub fn structural_match(&self, other: &Self) -> bool
判断和另一词项是否「结构匹配」
- 🎯变量替换中的模式匹配
- 🚩类型匹配 & 组分匹配
- ⚠️非递归:不会递归比较「组分是否对应匹配」
Sourcepub fn is_compound(&self) -> bool
pub fn is_compound(&self) -> bool
🆕判断是否真的是「复合词项」
- 🚩通过判断「内部元素枚举」的类型实现
- 🎯用于后续「作为复合词项」使用
- ✨以此在程序层面表示「复合词项」类型
Sourcepub fn as_compound(&self) -> Option<CompoundTermRef<'_>>
pub fn as_compound(&self) -> Option<CompoundTermRef<'_>>
🆕尝试将词项作为「复合词项」
- 📌通过判断「内部元素枚举」的类型实现
- 🚩在其内部元素不是「复合词项」时,会返回
None
Sourcepub fn as_compound_and(
&self,
predicate: impl FnOnce(&CompoundTermRef<'_>) -> bool,
) -> Option<CompoundTermRef<'_>>
pub fn as_compound_and( &self, predicate: impl FnOnce(&CompoundTermRef<'_>) -> bool, ) -> Option<CompoundTermRef<'_>>
🆕尝试将词项作为「复合词项」
- 📌通过判断「内部元素枚举」的类型实现
- 🚩在其内部元素不是「复合词项」时,会返回
None
Sourcepub unsafe fn as_compound_unchecked(&self) -> CompoundTermRef<'_>
pub unsafe fn as_compound_unchecked(&self) -> CompoundTermRef<'_>
🆕尝试将词项作为「复合词项」(未检查)
- 🚩通过判断「内部元素枚举」的类型实现
§Safety
- ⚠️代码是不安全的:必须在解包前已经假定是「复合词项」
- 📄逻辑参考自
Option::unwrap_unchecked
Sourcepub fn instanceof_variable(&self) -> bool
pub fn instanceof_variable(&self) -> bool
用于判断是否为「变量词项」
- 📄OpenNARS
instanceof Variable逻辑 - 🎯判断「是否内含变量」
Sourcepub fn instanceof_variable_i(&self) -> bool
pub fn instanceof_variable_i(&self) -> bool
🆕用于判断「是否为独立变量」
Sourcepub fn instanceof_variable_d(&self) -> bool
pub fn instanceof_variable_d(&self) -> bool
🆕用于判断「是否为非独变量」
Sourcepub fn instanceof_variable_q(&self) -> bool
pub fn instanceof_variable_q(&self) -> bool
🆕用于判断「是否为查询变量」
Sourcepub fn as_variable(&self) -> Option<usize>
pub fn as_variable(&self) -> Option<usize>
尝试匹配出「变量」,并返回其中的编号(若有)
Sourcepub fn is_constant(&self) -> bool
pub fn is_constant(&self) -> bool
📄OpenNARS Term.isConstant 属性
-
🚩检查其是否为「常量」:自身是否「不含变量」
-
🎯决定其是否能成为一个「概念」(被作为「概念」存入记忆区)
-
❓OpenNARS中在「构造语句」时又会将
isConstant属性置为true,这是为何- 📝被
Sentence(..)调用的CompoundTerm.renameVariables()会直接将词项「视作常量」 - 💭这似乎是被认为「即便全是变量,只要是【被作为语句输入过】的,就会被认作是『常量』」
- 📝然后这个「是否常量」会在「记忆区」中被认作「是否能从中获取概念」的依据:
if (!term.isConstant()) { return null; }
- 📝被
-
🚩【2024-04-21 23:46:12】现在变为「只读属性」:接受OpenNARS中有关「设置语句时/替换变量后 变为『常量』」的设定
- 💫【2024-04-22 00:03:10】后续仍然有一堆复杂逻辑要考虑
-
✅【2024-06-19 02:06:12】跟随最新改版更新,删去字段并铺开实现此功能
-
♻️【2024-06-26 02:07:27】重构修正:禁止「占位符」作为「常量词项」
-
♻️【2024-07-31 21:41:49】修正:不再将查询变量计入「常量词项」
§📄OpenNARS
Check whether the current Term can name a Concept.
- A Term is constant by default
- A variable is not constant
- (for
CompoundTerm) check if the term contains free variable
Sourcepub fn contain_var(&self) -> bool
pub fn contain_var(&self) -> bool
📄OpenNARS Variable.containVar 方法
- 🚩检查其是否「包含变量」
- 自身为「变量词项」或者其包含「变量词项」
- 🎯用于决定复合词项是否为「常量」
- 📝OpenNARS中对于复合词项的
isConstant属性采用「惰性获取」的机制isConstant作为!Variable.containVar(name)进行初始化
- 🆕实现方法:不同于OpenNARS「直接从字符串中搜索子串」的方式,基于递归方法设计
§📄OpenNARS
Check whether a string represent a name of a term that contains a variable
Sourcepub fn contain_var_i(&self) -> bool
pub fn contain_var_i(&self) -> bool
📄OpenNARS Variable.containVarI 方法
- 🎯判断「是否包含指定类型的变量」
- 🚩通过「判断是否包含指定标识符的词项」完成判断
Sourcepub fn contain_var_d(&self) -> bool
pub fn contain_var_d(&self) -> bool
📄OpenNARS Variable.containVarD 方法
- 🎯判断「是否包含指定类型的变量」
- 🚩通过「判断是否包含指定标识符的词项」完成判断
Sourcepub fn contain_var_q(&self) -> bool
pub fn contain_var_q(&self) -> bool
📄OpenNARS Variable.containVarQ 方法
- 🎯判断「是否包含指定类型的变量」
- 🚩通过「判断是否包含指定标识符的词项」完成判断
Sourcepub fn get_variable_type(&self) -> &str
pub fn get_variable_type(&self) -> &str
📄OpenNARS Variable.getType 方法
- 🎯在OpenNARS中仅用于「判断变量类型相等」
- 🚩归并到「判断词项标识符相等」
§📄OpenNARS
Get the type of the variable
Sourcepub fn instanceof_statement(&self) -> bool
pub fn instanceof_statement(&self) -> bool
🆕用于判断是否为「陈述词项」
- 📄OpenNARS
instanceof Statement逻辑
Sourcepub fn instanceof_inheritance(&self) -> bool
pub fn instanceof_inheritance(&self) -> bool
🆕用于判断是否为「继承」
- 📄OpenNARS
instanceof Inheritance逻辑 - 📝OpenNARS中「继承」与「实例」「属性」「实例属性」没有继承关系
- 🎯[
crate::inference::RuleTables]推理规则分派
Sourcepub fn instanceof_similarity(&self) -> bool
pub fn instanceof_similarity(&self) -> bool
🆕用于判断是否为「相似」
- 📄OpenNARS
instanceof Similarity逻辑 - 🎯[
crate::inference::RuleTables]推理规则分派
Sourcepub fn instanceof_implication(&self) -> bool
pub fn instanceof_implication(&self) -> bool
🆕用于判断是否为「蕴含」
- 📄OpenNARS
instanceof Implication逻辑 - 🎯[
crate::inference::RuleTables]推理规则分派
Sourcepub fn instanceof_equivalence(&self) -> bool
pub fn instanceof_equivalence(&self) -> bool
🆕用于判断是否为「等价」
- 📄OpenNARS
instanceof Equivalence逻辑 - 🎯[
crate::inference::RuleTables]推理规则分派
Sourcepub fn is_statement(&self) -> bool
pub fn is_statement(&self) -> bool
🆕判断一个词项是否为「陈述词项」
- 🚩判断其「内部元素」的个数是否为2,并且要判断其标识符
- 🚩【2024-09-07 14:59:00】现在采用更严格的条件——需要判断是否为「陈述系词」
Sourcepub fn as_statement(&self) -> Option<StatementRef<'_>>
pub fn as_statement(&self) -> Option<StatementRef<'_>>
🆕将一个复合词项转换为「陈述词项」(不可变引用)
- 🚩转换为Option
- 🚩【2024-09-07 14:59:00】现在采用更严格的条件——需要判断是否为「陈述系词」
Sourcepub fn as_statement_type(
&self,
statement_class: impl AsRef<str>,
) -> Option<StatementRef<'_>>
pub fn as_statement_type( &self, statement_class: impl AsRef<str>, ) -> Option<StatementRef<'_>>
Trait Implementations§
Source§impl<'a> Clone for CompoundTermRef<'a>
impl<'a> Clone for CompoundTermRef<'a>
Source§fn clone(&self) -> CompoundTermRef<'a>
fn clone(&self) -> CompoundTermRef<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<'a> Debug for CompoundTermRef<'a>
impl<'a> Debug for CompoundTermRef<'a>
Source§impl Deref for CompoundTermRef<'_>
向词项本身的自动解引用
impl Deref for CompoundTermRef<'_>
向词项本身的自动解引用
- 🎯让「复合词项引用」可以被看作是一个普通的词项
Source§impl Display for CompoundTermRef<'_>
转发「呈现」方法到「内部词项」
impl Display for CompoundTermRef<'_>
转发「呈现」方法到「内部词项」
Source§impl<'s> From<CompoundTermRefMut<'s>> for CompoundTermRef<'s>
可变引用 ⇒ 不可变引用
impl<'s> From<CompoundTermRefMut<'s>> for CompoundTermRef<'s>
可变引用 ⇒ 不可变引用