From 527dd89e103b582be04c66df78137b2cbb05e449 Mon Sep 17 00:00:00 2001 From: ARCJ137442 <61109168+ARCJ137442@users.noreply.github.com> Date: Tue, 9 Apr 2024 21:16:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20:sparkles:=20=E3=80=8C=E5=8E=9F?= =?UTF-8?q?=E7=94=9F=E3=80=8D=E8=BD=AC=E8=AF=91=E5=99=A8=EF=BC=9B=E6=96=B0?= =?UTF-8?q?=E7=9A=84=E3=80=8C=E5=8E=9F=E7=94=9FIL-1=E3=80=8D=E7=A4=BA?= =?UTF-8?q?=E4=BE=8BCIN?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加【以NAVM指令作为输入,输出JSON式NAVM输出】的「原生」CIN转译器支持;基于「原生」转译器,示例性支持NAVM所开发的「原生IL-1」运行时(仅能跑通「简单演绎推理」) --- Cargo.lock | 6 ++-- Cargo.toml | 2 +- src/bin/babelnar_cli/config_launcher.rs | 3 +- src/bin/babelnar_cli/main.rs | 33 ++++++++++++++++++++++ src/cin_implements/mod.rs | 5 +++- src/cin_implements/native/mod.rs | 9 ++++++ src/cin_implements/native/translators.rs | 27 ++++++++++++++++++ src/lib.rs | 2 ++ src/tests/cli/config/cin_native_il_1.hjson | 16 +++++++++++ 9 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 src/cin_implements/native/mod.rs create mode 100644 src/cin_implements/native/translators.rs create mode 100644 src/tests/cli/config/cin_native_il_1.hjson diff --git a/Cargo.lock b/Cargo.lock index 90c63c3..eed7d3c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -73,7 +73,7 @@ checksum = "f1fdabc7756949593fe60f30ec81974b613357de856987752631dea1e3394c80" [[package]] name = "babel_nar" -version = "0.18.1" +version = "0.19.0" dependencies = [ "anyhow", "clap", @@ -568,11 +568,13 @@ dependencies = [ [[package]] name = "navm" -version = "0.9.1" +version = "0.11.0" dependencies = [ "anyhow", "nar_dev_utils", "narsese", + "serde", + "serde_json", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 0fc3d69..952ec23 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "babel_nar" -version = "0.18.1" +version = "0.19.0" edition = "2021" description = """ Implementation and application supports of the NAVM model diff --git a/src/bin/babelnar_cli/config_launcher.rs b/src/bin/babelnar_cli/config_launcher.rs index 365fb65..a754a9c 100644 --- a/src/bin/babelnar_cli/config_launcher.rs +++ b/src/bin/babelnar_cli/config_launcher.rs @@ -7,7 +7,7 @@ use crate::{ use anyhow::{anyhow, Result}; use babel_nar::{ cin_implements::{ - common::generate_command, cxin_js, nars_python, ona, openjunars, opennars, pynars, + common::generate_command, cxin_js, nars_python, native, ona, openjunars, opennars, pynars, }, cli_support::{cin_search::name_match::name_match, io::readline_iter::ReadlineIter}, eprintln_cli, println_cli, @@ -199,6 +199,7 @@ pub type TranslatorDict<'a> = &'a [( /// 输入转译器的索引字典 /// * 🚩静态存储映射,后续遍历可有序可无序 pub const TRANSLATOR_DICT: TranslatorDict = &[ + ("Native", native::input_translate, native::output_translate), ( "OpenNARS", opennars::input_translate, diff --git a/src/bin/babelnar_cli/main.rs b/src/bin/babelnar_cli/main.rs index 1c2c303..bfb6c5f 100644 --- a/src/bin/babelnar_cli/main.rs +++ b/src/bin/babelnar_cli/main.rs @@ -321,6 +321,39 @@ mod tests { } } + /// 测试/原生IL-1 + mod native_il_1 { + use super::*; + + cin_tests! { + NATIVE_IL_1; + + /// 简单演绎 + /// * 📝✅【2024-04-09 21:12:10】成功 + nal_de => NAL_SIMPLE_DEDUCTION + + /// 高阶演绎 + /// * 📝❌【2024-04-09 21:12:32】失败:尚不支持 + nal_hi => NAL_HIGHER_DEDUCTION + + /// 自变量消除 + /// * 📝❌【2024-04-09 21:12:32】失败:尚不支持 + nal_ie => NAL_I_VAR_ELIMINATION + + /// 时间归纳 + /// * 📝❌【2024-04-09 21:12:32】失败:尚不支持 + nal_te => NAL_TEMPORAL_INDUCTION + + /// 简单操作 + /// * 📝❌【2024-04-09 21:12:32】失败:尚不支持 + nal_so => NAL_SIMPLE_OPERATION + + /// 操作 + /// * 📝❌【2024-04-09 21:12:32】失败:尚不支持 + nal_op => NAL_OPERATION + } + } + // ! ❌【2024-04-07 14:39:20】接口完成度不高的NARS-Python、OpenJunars暂不进行测试 /// 测试入口/带Websocket Shell diff --git a/src/cin_implements/mod.rs b/src/cin_implements/mod.rs index 5ab3cfd..29b9f6c 100644 --- a/src/cin_implements/mod.rs +++ b/src/cin_implements/mod.rs @@ -6,7 +6,7 @@ //! * ❌【2024-03-25 13:36:30】集成测试`cargo t --all-features`中未能解决 //! * ❗// ! ↑【少用乃至不用这条命令】 //! -//! ? 【2024-03-25 12:48:08】如何兼顾「复用」「性能」与「简洁」 +//! * 🚩【2024-04-09 20:47:04】基本完成对「复用」「性能」与「简洁」的兼顾 //! * 📌复用:将OpenNARS、ONA抽象成「基于jar的启动逻辑」「基于exe的启动逻辑」等方式,以便后续重复使用 //! * 📄case:目前ONA、NARS-Python都是基于exe的启动方式 //! * 📌性能:避免过多的封装、粗暴复合导致的空间浪费 @@ -22,6 +22,9 @@ util::mods! { // 共用代码 pub common; + // 原生 + pub native; + // OpenNARS pub opennars; diff --git a/src/cin_implements/native/mod.rs b/src/cin_implements/native/mod.rs new file mode 100644 index 0000000..f9889ff --- /dev/null +++ b/src/cin_implements/native/mod.rs @@ -0,0 +1,9 @@ +//! NAVM原生的虚拟机(转译器) +//! * ✨Cmd输入转译:直接将[`Cmd`]转换为字符串形式 +//! * ✨NAVM_JSON输出转译:基于[`serde_json`]直接从JSON字符串读取[`Output`] +//! * 📌没有固定的启动器:仅通过「命令行启动器」即可启动 + +util::mods! { + // 输入输出转译 + pub pub translators; +} diff --git a/src/cin_implements/native/translators.rs b/src/cin_implements/native/translators.rs new file mode 100644 index 0000000..bfd42da --- /dev/null +++ b/src/cin_implements/native/translators.rs @@ -0,0 +1,27 @@ +//! 输入输出转译 +//! * ✨Cmd输入转译:直接将[`Cmd`]转换为字符串形式 +//! * ✨NAVM_JSON输出转译:基于[`serde_json`]直接从JSON字符串读取[`Output`] + +use anyhow::Result; +use navm::{cmd::Cmd, output::Output}; +extern crate serde_json; + +/// Cmd输入转译 +/// * 🚩直接将[`Cmd`]转换为字符串形式 +/// * 📌总是成功 +pub fn input_translate(cmd: Cmd) -> Result { + Ok(cmd.to_string()) +} + +/// NAVM_JSON输出转译 +/// * 🚩基于[`serde_json`]直接从JSON字符串读取[`Output`] +pub fn output_translate(content_raw: String) -> Result { + match serde_json::from_str(&content_raw) { + // 解析成功⇒返回 + Ok(output) => Ok(output), + // 解析失败⇒转为`OTHER` + Err(..) => Ok(Output::OTHER { + content: content_raw, + }), + } +} diff --git a/src/lib.rs b/src/lib.rs index e9589a2..6f24266 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,6 +74,8 @@ pub mod tests { PYNARS = "./src/tests/cli/config/cin_pynars.hjson" /// CXinJS CXIN_JS = "./src/tests/cli/config/cin_cxin_js.hjson" + /// 原生IL-1 + NATIVE_IL_1 = "./src/tests/cli/config/cin_native_il_1.hjson" /// 预引入/NAL测试环境 PRELUDE_TEST = "./src/tests/cli/config/prelude_test.hjson" diff --git a/src/tests/cli/config/cin_native_il_1.hjson b/src/tests/cli/config/cin_native_il_1.hjson new file mode 100644 index 0000000..35597d4 --- /dev/null +++ b/src/tests/cli/config/cin_native_il_1.hjson @@ -0,0 +1,16 @@ +#hjson +// * 🎯用于测试原生「IL-1」运行时 +// * ✨基于NAVM,纯Rust编写 +{ + // 使用「原生」输入输出转译器 + translators: native + command: { + // * ⚠️必须前缀`./`以指定是「启动当前工作目录下的exe文件」 + cmd: ./native-IL-1.exe + cmdArgs: [] + // * 🚩现在基于「固定位置的CIN程序包」运行测试 + // * 回溯路径:config(`./`) => cli => tests => src => BabelNAR.rs / executables + currentDir: ./../../../../executables + } + autoRestart: true +} \ No newline at end of file