-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ 初步支持NARS-Python(直接exe启动)与OpenJunars(通过Julia运行时);重命名「CIN实现」目录;…
…完善文档 1. ✅初步完成对现有常见NARS实现的启动器 2. 📌目前仍然只有OpenNARS、ONA、PyNARS能真正投入实际使用 - 其它都多少存在「IO困难」或「进程残留」问题 - 📄已在自述文档中说明
- Loading branch information
1 parent
b474933
commit 453105e
Showing
24 changed files
with
444 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("无法终止虚拟机"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`] |
Oops, something went wrong.