Skip to content

Commit

Permalink
feat: ✨ 大幅推进CIN转译工作,部分解决「方言问题」
Browse files Browse the repository at this point in the history
🏗️OpenNARS:基本实现除「特定方言语法」外的Narsese解析
- 顺带一并更新实用库,新增并应用「管道宏」`pipe!`
🏗️ONA:同OpenNARS
✅NARS-Python:基本实现输入转译,但未有成功截获到输出
- 为此引入可选依赖`lazy_static`
  • Loading branch information
ARCJ137442 committed Mar 25, 2024
1 parent 453105e commit a24c12b
Show file tree
Hide file tree
Showing 10 changed files with 411 additions and 58 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"editor.formatOnSave": true,
"cSpell.words": [
"Errno",
"rfind",
"runpy",
"traceback"
"traceback",
"tstate"
]
}
52 changes: 49 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
[package]
name = "babel_nar"
version = "0.5.0"
version = "0.6.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
regex = "1.10.4"

# 用于实现「静态含闭包常量」
# * 🎯初次引入:NARS-Python 方言格式
# * 🔗:https://stackoverflow.com/questions/73260997/rust-boxed-closure-in-global-variable
[dependencies.lazy_static]
version = "1.4.0"
optional = true

[dependencies.nar_dev_utils]
# 【2024-03-13 21:17:55】实用库现在独立为`nar_dev_utils`
Expand Down Expand Up @@ -38,7 +46,8 @@ features = [] # ! 【2024-03-21 09:24:51】暂时没有特性
default = []
# 大杂烩
bundled = [
"cin_implements"
"cin_implements",
"lazy_static" # 这个「词法Narsese」也在用
]
# 各个独立的特性 #
# 具体接口实现:
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,22 @@

## 各CIN对接情况

🕒最后更新时间:【2024-03-25 14:10:36
🕒最后更新时间:【2024-03-26 01:43:28

| CIN | 实现方法 | 进程安全 | 输入转译 | 输出转译 |
| :---------- | :---------: | :--: | :--: | :--: |
| OpenNARS | `java -jar` ||| 🚧 |
| ONA | 直接启动exe ||| 🚧 |
| PyNARS | `python -m` || 🚧 | 🚧 |
| NARS-Python | 直接启动exe || 🚧 | 🚧 |
| NARS-Python | 直接启动exe || | |
| OpenJunars | `julia` ||||

注:

- 🚧输入输出转译功能仍然在从[BabelNAR_Implements](https://github.com/ARCJ137442/BabelNAR_Implements.jl)迁移
- ❓NARS-Python的exe界面可能会在终止后延时关闭
- ❌基于`julia`启动OpenJunars脚本`launch.jl`时,对「输出捕获」尚未有成功记录
- ❌目前对NARS-Python的「输出捕获」尚未有成功记录

## 参考

Expand Down
36 changes: 36 additions & 0 deletions src/cin_implements/nars_python/dialect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//! 用于存储NARS-Python的方言格式
//! * 🚩【2024-03-26 01:31:44】本质上就是陈述括弧改变了而已
use narsese::conversion::string::impl_lexical::{
format_instances::create_format_ascii, NarseseFormat,
};
use narsese::lexical::Narsese;

#[cfg(feature = "lazy_static")]
lazy_static::lazy_static! {
/// NARS-Python的方言格式
/// * 🚩仅在`lazy_static`启用时开启
pub static ref FORMAT: NarseseFormat = create_format_nars_python();
}

pub fn create_format_nars_python() -> NarseseFormat {
let mut f = create_format_ascii();
f.statement.brackets = ("(".into(), ")".into());
f
}

/// 获取NARS-Python的方言格式
/// * 🚩使用`lazy_static`定义的静态常量,无需重复初始化
/// * 🚩否则总是创建一个新的「Narsese格式」
#[cfg(feature = "lazy_static")]
pub fn format_in_nars_python(narsese: &Narsese) -> String {
FORMAT.format_narsese(narsese)
}

/// 获取NARS-Python的方言格式
/// * 🚩否则总是创建一个新的「Narsese格式」
#[cfg(not(feature = "lazy_static"))]
pub fn format_in_nars_python(narsese: &Narsese) -> String {
// 创建格式,并立即格式化Narsese
create_format_nars_python().format_narsese(narsese)
}
55 changes: 51 additions & 4 deletions src/cin_implements/nars_python/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// 转译器
util::mod_and_pub_use! {
// 方言(Narsese格式)
dialect
// 转译器
translators
// 启动器
Expand All @@ -15,7 +17,11 @@ util::mod_and_pub_use! {
mod tests {
use super::*;
use crate::runtime::{test::EXE_PATH_NARS_PYTHON, CommandVmRuntime};
use navm::vm::{VmLauncher, VmRuntime};
use narsese::conversion::string::impl_lexical::shortcuts::*;
use navm::{
cmd::Cmd,
vm::{VmLauncher, VmRuntime},
};

#[test]
fn test() {
Expand All @@ -28,13 +34,54 @@ mod tests {
}

/// 测试/NARS-Python
pub(crate) fn _test_nars_python(vm: CommandVmRuntime) {
// TODO: 实际的测试代码
/// * ❌【2024-03-26 01:42:14】目前还没法真正截取到输出
pub(crate) fn _test_nars_python(mut vm: CommandVmRuntime) {
// 等待几秒钟,让exe的界面显示出来
std::thread::sleep(std::time::Duration::from_secs(2));

vm.input_cmd(Cmd::NSE(nse_task!(<A --> B>.)))
.expect("无法输入NAVM指令");
vm.input_cmd(Cmd::NSE(nse_task!(<B --> C>.)))
.expect("无法输入NAVM指令");
vm.input_cmd(Cmd::NSE(nse_task!(<A --> C>?)))
.expect("无法输入NAVM指令");

// 等待四秒钟,让exe的界面显示出来
std::thread::sleep(std::time::Duration::from_secs(4));

// 终止虚拟机运行时
vm.terminate().expect("无法终止虚拟机");
}

/* // ! 【2024-03-26 01:44:27】NARS-Python输出崩溃的内容:
running 1 test
Started process: 65784
Traceback (most recent call last):
File "main.py", line 122, in <module>
File "main.py", line 118, in main
File "NARS.py", line 54, in run
File "NARS.py", line 63, in do_working_cycle
File "InputChannel.py", line 74, in process_pending_sentence
File "InputChannel.py", line 87, in process_sentence
File "NARS.py", line 247, in process_task
File "NARS.py", line 323, in process_question_task
File "NARS.py", line 491, in process_sentence_semantic_inference
File "NARSInferenceEngine.py", line 73, in do_semantic_inference_two_premise
AttributeError: 'NoneType' object has no attribute 'frequency'
[38676] Failed to execute script 'main' due to unhandled exception!
Fatal Python error: could not acquire lock for <_io.BufferedReader name='<stdin>'> at interpreter shutdown, possibly due to daemon threads
Python runtime state: finalizing (tstate=00000213FB525D60)
Thread 0x00017e0c (most recent call first):
File "InputChannel.py", line 25 in get_user_input
File "threading.py", line 870 in run
File "threading.py", line 932 in _bootstrap_inner
File "threading.py", line 890 in _bootstrap
Current thread 0x00013918 (most recent call first):
<no Python frame>
成功: 已终止 PID 为 65784 的进程。
test cin_implements::nars_python::tests::test ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 10 filtered out; finished in 6.56s
*/
}
17 changes: 13 additions & 4 deletions src/cin_implements/nars_python/translators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,30 @@
//! * 📌基于命令行输入输出的字符串读写
//! * ✨NAVM指令→字符串
//! * ✨字符串→NAVM输出
//!
//! ## 输出样例
//!
//! * `EXE: ^left based on desirability: 0.9`
//! * `PROCESSED GOAL: SentenceID:2081:ID ({SELF} --> [SAFE])! :|: %1.00;0.03%from SentenceID:2079:ID ({SELF} --> [SAFE])! :|: %1.00;0.00%,SentenceID:2080:ID ({SELF} --> [SAFE])! :|: %1.00;0.02%,`
//! * `PREMISE IS TRUE: ((*,{SELF}) --> ^right)`
//! * `PREMISE IS SIMPLIFIED ({SELF} --> [SAFE]) FROM (&|,({SELF} --> [SAFE]),((*,{SELF}) --> ^right))`
use narsese::lexical::Narsese;
use navm::{
cmd::Cmd,
output::{Operation, Output},
};
use util::ResultS;

use super::format_in_nars_python;

/// NARS-Python的「输入转译」函数
/// * 🎯用于将统一的「NAVM指令」转译为「NARS-Python输入」
///
/// TODO: ⚠️其有一种不同的语法,需要细致解析
pub fn input_translate(cmd: Cmd) -> ResultS<String> {
let content = match cmd {
// 直接使用「末尾」,此时将自动格式化任务(可兼容「空预算」的形式)
Cmd::NSE(..) => cmd.tail(),
// 使用「末尾」将自动格式化任务(可兼容「空预算」的形式)
// * ✅【2024-03-26 01:44:49】目前采用特定的「方言格式」解决格式化问题
Cmd::NSE(narsese) => format_in_nars_python(&Narsese::Task(narsese)),
// CYC指令:运行指定周期数
// ! NARS-Python Shell同样是自动步进的
Cmd::CYC(n) => n.to_string(),
Expand Down
Loading

0 comments on commit a24c12b

Please sign in to comment.