Skip to content

Commit

Permalink
feat: ✨ 初步支持NARS-Python(直接exe启动)与OpenJunars(通过Julia运行时);重命名「CIN实现」目录;…
Browse files Browse the repository at this point in the history
…完善文档

1. ✅初步完成对现有常见NARS实现的启动器
2. 📌目前仍然只有OpenNARS、ONA、PyNARS能真正投入实际使用
  - 其它都多少存在「IO困难」或「进程残留」问题
  - 📄已在自述文档中说明
  • Loading branch information
ARCJ137442 committed Mar 25, 2024
1 parent b474933 commit 453105e
Show file tree
Hide file tree
Showing 24 changed files with 444 additions and 41 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"rust-analyzer.cargo.features": "all",
"editor.formatOnSave": true,
"cSpell.words": [
"Errno",
"runpy",
"traceback"
]
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

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

13 changes: 9 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "babel_nar"
version = "0.4.0"
version = "0.5.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down Expand Up @@ -38,8 +38,13 @@ features = [] # ! 【2024-03-21 09:24:51】暂时没有特性
default = []
# 大杂烩
bundled = [
"implements"
"cin_implements"
]
# 各个独立的特性 #
# 具体接口实现:OpenNARS、ONA、NARS-Python、OpenJunars、PyNARS……
implements = []
# 具体接口实现:
# ✅OpenNARS
# ✅ONA
# ✅PyNARS
# ✅NARS-Python(不稳定)
# ✅OpenJunars(不稳定)
cin_implements = []
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

该项目使用[语义化版本 2.0.0](https://semver.org/)进行版本号管理。

[**NAVM.rs**](https://github.com/ARCJ137442/NAVM.rs)**运行时**
[**NAVM.rs**](https://github.com/ARCJ137442/NAVM.rs)**运行时**[CIN](#cin-computer-implement-of-nars)启动器

- 前身为[**BabelNAR.jl**](https://github.com/ARCJ137442/BabelNAR.jl)
- 🎯为「非公理虚拟机模型」提供程序实现
- 🎯为各CIN实现**统一输入输出**形式
- 为「非公理虚拟机模型」提供程序实现
- ✨统一各[CIN](#cin-computer-implement-of-nars)**输入输出**形式,聚合使用各大NARS实现

## 概念

Expand All @@ -22,7 +22,26 @@

🔗参考[**NAVM.jl**的对应部分](https://github.com/ARCJ137442/navm.jl?tab=readme-ov-file#commonnarsese)

## 各CIN对接情况

🕒最后更新时间:【2024-03-25 14:10:36】

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

注:

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

## 参考

- [BabelNAR](https://github.com/ARCJ137442/BabelNAR.jl)
- [BabelNAR_Implements](https://github.com/ARCJ137442/BabelNAR_Implements.jl)
- [NAVM.rs](https://github.com/ARCJ137442/NAVM.rs)
34 changes: 34 additions & 0 deletions src/cin_implements/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//! 对各CIN实现「非公理虚拟机」模型
//! * 🎯基于「NAVM指令/NAVM输出↔字符串」的转换
//!
//! ! ⚠️关键问题:进程残留
//! * ✅单元测试中利用`taskkill`初步解决
//! * ❌【2024-03-25 13:36:30】集成测试`cargo t --all-features`中未能解决
//! * ❗// ! ↑【少用乃至不用这条命令】
//!
//! ? 【2024-03-25 12:48:08】如何兼顾「复用」「性能」与「简洁」
//! * 📌复用:将OpenNARS、ONA抽象成「基于jar的启动逻辑」「基于exe的启动逻辑」等方式,以便后续重复使用
//! * 📄case:目前ONA、NARS-Python都是基于exe的启动方式
//! * 📌性能:避免过多的封装、粗暴复合导致的空间浪费
//! * 📄case:「启动器套启动器」在尝试抽象出「exe启动器」时,因为「没法预先指定转译器」在「复用『设置转译器』函数」时
//! * ❌不希望在「exe启动器」「jar启动器」中重复套【包含一长串函数闭包】
//! * 📌简洁:代码简明易懂,方便调用方使用
//! * 📄case:期望能有形如`ONA::new(path).launch()`的语法
//! * 💭不希望出现「强行模拟」的情况,如`mod ONA {pub fn new(..) {..}}`
//! * ❌不希望因此再全小写/封装命名空间,如`impls::ona::new`
//! * ❓目前的问题:在Rust基于「特征」的组合式设计哲学下,如何进行兼顾三者的优秀设计
// OpenNARS
pub mod opennars;

// ONA
pub mod ona;

// NARS-Python
pub mod nars_python;

// PyNARS
pub mod pynars;

// OpenJunars
pub mod open_junars;
48 changes: 48 additions & 0 deletions src/cin_implements/nars_python/launcher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//! NARS-Python运行时的启动器
//! * 🎯允许NARS-Python对原先运行时特别配置功能,同时也支持为NARS-Python定制配置
//! * 🚩只憎加「启动器」类型,而不增加「运行时」类型
//! * ✨不同启动器可以启动到相同运行时
use super::{input_translate, output_translate};
use crate::runtime::{CommandVm, CommandVmRuntime};
use navm::vm::VmLauncher;
use std::path::PathBuf;

// ! NARS-Python作为一个独立的`main.exe`,没有默认的启动参数

/// NARS-Python运行时启动器
/// * 🎯配置NARS-Python专有的东西
/// * 🚩基于exe文件启动NARS-Python exe
/// * 🚩【2024-03-25 08:51:30】目前保留原有缩写的大小写风格,与OpenNARS、PyNARS一致
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct NARSPython {
/// exe文件路径
exe_path: PathBuf,
}

// ! 🚩【2024-03-25 09:37:22】目前暂时不提取至「VmExe」:参考`impl_runtime`根目录说明

impl NARSPython {
/// 构造函数
pub fn new(exe_path: impl Into<PathBuf>) -> Self {
Self {
// 转换为路径
exe_path: exe_path.into(),
}
}
}

/// 启动到「命令行运行时」
impl VmLauncher<CommandVmRuntime> for NARSPython {
fn launch(self) -> CommandVmRuntime {
// 构造指令,并启动虚拟机
CommandVm::new(self.exe_path)
// * 🚩固定的「输入输出转换器」
.input_translator(input_translate)
.output_translator(output_translate)
// 🔥启动
.launch()
}
}

// ! 单元测试见[`super`]
40 changes: 40 additions & 0 deletions src/cin_implements/nars_python/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//! 「非公理虚拟机」的NARS-Python运行时
//! * 🚩只提供「一行启动」的功能封装
//! * 🎯无需自行配置「输入输出转译器」
// 转译器
util::mod_and_pub_use! {
// 转译器
translators
// 启动器
launcher
}

/// 单元测试
#[cfg(test)]
mod tests {
use super::*;
use crate::runtime::{test::EXE_PATH_NARS_PYTHON, CommandVmRuntime};
use navm::vm::{VmLauncher, VmRuntime};

#[test]
fn test() {
// 从别的地方获取exe路径
let exe_path = EXE_PATH_NARS_PYTHON;
// 一行代码启动NARS-Python
let vm = NARSPython::new(exe_path).launch();
// 运行专有测试
_test_nars_python(vm)
}

/// 测试/NARS-Python
pub(crate) fn _test_nars_python(vm: CommandVmRuntime) {
// TODO: 实际的测试代码

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

// 终止虚拟机运行时
vm.terminate().expect("无法终止虚拟机");
}
}
63 changes: 63 additions & 0 deletions src/cin_implements/nars_python/translators.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//! NARS-Python在「命令行运行时」的转译器
//! * 🎯维护与NARS-Python exe的交互
//! * 📌基于命令行输入输出的字符串读写
//! * ✨NAVM指令→字符串
//! * ✨字符串→NAVM输出
use navm::{
cmd::Cmd,
output::{Operation, Output},
};
use util::ResultS;

/// NARS-Python的「输入转译」函数
/// * 🎯用于将统一的「NAVM指令」转译为「NARS-Python输入」
///
/// TODO: ⚠️其有一种不同的语法,需要细致解析
pub fn input_translate(cmd: Cmd) -> ResultS<String> {
let content = match cmd {
// 直接使用「末尾」,此时将自动格式化任务(可兼容「空预算」的形式)
Cmd::NSE(..) => cmd.tail(),
// CYC指令:运行指定周期数
// ! NARS-Python Shell同样是自动步进的
Cmd::CYC(n) => n.to_string(),
// 其它类型
_ => return Err(format!("该指令类型暂不支持:{cmd:?}")),
};
// 转译
Ok(content)
}

/// NARS-Python的「输出转译」函数
/// * 🎯用于将NARS-Python Shell的输出(字符串)转译为「NAVM输出」
/// * 🚩直接根据选取的「头部」进行匹配
pub fn output_translate(content: String) -> ResultS<Output> {
// 根据冒号分隔一次,然后得到「头部」
let head = content.split_once(':').unwrap_or(("", "")).0.to_lowercase();
// 根据「头部」生成输出
let output = match &*head {
// TODO: 有待适配
"answer" => Output::ANSWER {
content_raw: content,
// TODO: 有待捕获转译
narsese: None,
},
"derived" => Output::OUT {
content_raw: content,
// TODO: 有待捕获转译
narsese: None,
},
"input" => Output::IN { content },
"exe" => Output::EXE {
content_raw: content,
// TODO: 有待捕获转译
operation: Operation::new("UNKNOWN", [].into_iter()),
},
"err" | "error" => Output::ERROR {
description: content,
},
_ => Output::OTHER { content },
};
// 返回
Ok(output)
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ pub fn output_translate(content: String) -> ResultS<Output> {
narsese: None,
},
"input" => Output::IN { content },
"anticipate" => Output::ANTICIPATE {
content_raw: content,
// TODO: 有待捕获转译
narsese: None,
},
"exe" => Output::EXE {
content_raw: content,
// TODO: 有待捕获转译
Expand Down
61 changes: 61 additions & 0 deletions src/cin_implements/open_junars/launcher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//! Julia模块 启动器
//! * 📌OpenJunars运行时的启动器
//! * 🎯允许OpenJunars对原先运行时特别配置功能,同时也支持为OpenJunars定制配置
//! * 🚩只憎加「启动器」类型,而不增加「运行时」类型
//! * ✨不同启动器可以启动到相同运行时
//! * 🚩通过`julia`运行`.jl`脚本启动
use super::{input_translate, output_translate};
use crate::runtime::{CommandVm, CommandVmRuntime};
use navm::vm::VmLauncher;
use std::{path::PathBuf, process::Command};

/// 启动Julia运行时的命令
const COMMAND_JULIA: &str = "julia";

/// ! Julia启动脚本无需附加参数
/// OpenJunars运行时启动器
/// * 🎯配置OpenJunars专有的东西
/// * 🎯以Julia模块形式启动OpenJunars
/// * 📌没有内置的「音量」配置
/// * 🚩【2024-03-25 08:55:07】基于Julia模块文件启动OpenJunars
/// * 默认预置指令:``julia [`.jl`脚本文件路径]``
/// * 🚩【2024-03-25 09:15:07】删去[`Default`]派生:因为可能导致无效的路径
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct VmJulia {
/// Julia脚本文件路径
jl_path: PathBuf,
}

/// 兼容性别名
#[doc(alias = "VmJulia")]
pub type OpenJunars = VmJulia;

impl VmJulia {
pub fn new(jl_path: impl Into<PathBuf>) -> Self {
Self {
// 转换为路径
jl_path: jl_path.into(),
}
}
}

/// 启动到「命令行运行时」
impl VmLauncher<CommandVmRuntime> for VmJulia {
fn launch(self) -> CommandVmRuntime {
// 构造指令
let mut command = Command::new(COMMAND_JULIA);
command.arg(self.jl_path);

// 构造并启动虚拟机
CommandVm::from_io_process(command.into())
// * 🚩固定的「输入输出转换器」
.input_translator(input_translate)
.output_translator(output_translate)
// 🔥启动
.launch()
}
}

// ! 单元测试见[`super`]
Loading

0 comments on commit 453105e

Please sign in to comment.