Skip to content

Commit

Permalink
feat: ✨ 基于「NAVM输出」的「详细输出」支持
Browse files Browse the repository at this point in the history
为`(e)print_cli!`在NAVM输出中使用前缀「%」实现「详细输出」,基于「NAVM输出」自身的「[# Narsese #]」与「[% NARS操作 %]」增加「CIN无关」的提示性输出(但总体信息量不比原始输出多)
  • Loading branch information
ARCJ137442 committed Apr 13, 2024
1 parent d69a55a commit a957a58
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 20 deletions.
33 changes: 23 additions & 10 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "babel_nar"
version = "0.20.3"
version = "0.21.0"
edition = "2021"
description = """
Implementation and application supports of the NAVM model
Expand All @@ -25,7 +25,7 @@ repository = "https://github.com/ARCJ137442/BabelNAR.rs"
# 用于错误处理
thiserror = "1.0.58"
anyhow = "1.0.81"
clearscreen = "2.0.1"
clearscreen = "3.0.0"

[dependencies.nar_dev_utils]
# 【2024-03-13 21:17:55】实用库现在独立为`nar_dev_utils`
Expand Down
5 changes: 5 additions & 0 deletions src/bin/babelnar_cli/runtime_manage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ where
fn add_output_listener(output_cache: &mut OutputCache) {
output_cache.output_handlers.add_handler(|output| {
// 打印输出
// * 🚩【2024-04-13 17:57:32】暂不启用「详细输出」模式:尚未解决「详细输出后过长,但因信息取舍不能省掉『原始信息』」的问题
// * 💭CIN的「原始输出」总是信息量相对最多的(NAVM输出只取其中一个规则的子集)
// * 📌因此,开启「详细模式」必定造成「信息冗余」
// TODO: 💡或许后续可用配置开关「详细模式/纯NAVM输出模式」,以实现「自定义输出形式」
// * ✨这样的形式也方便调用其exe的其它外部程序解析exe输出(更为规范化)
println_cli!(&output);
// 继续返回
Some(output)
Expand Down
117 changes: 109 additions & 8 deletions src/cli_support/io/output_print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
//! * 参考链接:<https://github.com/ARCJ137442/BabelNAR_Implements/blob/master/scripts/console.jl#L160>
use colored::Colorize;
use nar_dev_utils::manipulate;
use narsese::conversion::string::impl_lexical::format_instances::FORMAT_ASCII;
use navm::output::Output;
use std::fmt::Display;

Expand Down Expand Up @@ -87,8 +89,77 @@ impl OutputType<'_> {
/// 从NAVM输出格式化
/// * 🎯封装「从NAVM输出打印」
#[inline(always)]
pub fn format_from_navm_output(out: &Output) -> impl Display {
OutputType::from(out).format_line(out.raw_content().trim_end())
pub fn format_navm_output(out: &Output) -> impl Display {
let message = manipulate!(
// 新建字符串对象
String::new()
// 格式化头部
=> Self::format_navm_output_type(out, _)
// 格式化原始内容
=> Self::format_navm_output_content(out, _)
);
// 载入着色
OutputType::from(out).to_colored_str(message)
}

/// 从NAVM输出格式化(详细)
/// * 🎯封装「从NAVM输出打印」
/// * ✨提供「解析出的Narsese」与「解析出的NARS操作」信息
#[inline(always)]
pub fn format_from_navm_output_verbose(out: &Output) -> impl Display {
let message = manipulate!(
// 新建字符串对象
String::new()
// 格式化头部
=> Self::format_navm_output_type(out, _)
// 详细格式化:Narsese、NARS操作
=> Self::format_navm_output_verbose(out, _)
// 格式化原始内容
=> Self::format_navm_output_content(out, _)
);
// 载入
OutputType::from(out).to_colored_str(message)
}

/// 从NAVM输出格式化(详细)
/// * 🎯封装「从NAVM输出打印」逻辑
/// * 🚩基于「流式添加内容」的做法
/// * 📄`[OUT]`
#[inline(always)]
fn format_navm_output_type(out: &Output, out_message: &mut String) {
// 返回创建的字符串
*out_message += "[";
*out_message += out.type_name();
*out_message += "] "; // ! 🚩使用尾缀空格,以避免「非必要连续空格」
}

/// 从NAVM输出格式化(详细)
/// * 🎯封装「从NAVM输出打印」逻辑
/// * 🚩基于「流式添加内容」的做法
/// * 📄`[# <A --> B>. #]`
#[inline(always)]
fn format_navm_output_verbose(out: &Output, out_message: &mut String) {
// * 🚩先添加Narsese
if let Some(narsese) = out.get_narsese() {
*out_message += "[# ";
*out_message += &(FORMAT_ASCII.format(narsese));
*out_message += " #]";
*out_message += " "; // 🚩使用尾缀空格,以避免「非必要连续空格」
}
// * 🚩再添加操作
if let Some(operation) = out.get_operation() {
*out_message += "[% ";
// 🚩↓使用尾缀空格,以避免「非必要连续空格」
*out_message += &operation.to_string();
*out_message += " %]";
*out_message += " "; // 🚩使用尾缀空格,以避免「非必要连续空格」
}
}

/// * 📄ONA:`Input: G3! :|: occurrenceTime=37 Priority=1.000000 Truth: frequency=1.000000, confidence=0.900000`
fn format_navm_output_content(out: &Output, out_message: &mut String) {
// 最后添加原始内容
*out_message += out.get_content().trim();
}

/// 基于[`colored`]的输出美化
Expand Down Expand Up @@ -130,8 +201,16 @@ impl OutputType<'_> {
/// ✨格式化打印NAVM输出
/// * 🎯BabelNAR CLI
#[inline]
pub fn print_from_navm_output(out: &Output) {
println!("{}", Self::format_from_navm_output(out));
pub fn print_navm_output(out: &Output) {
println!("{}", Self::format_navm_output(out));
}

/// ✨格式化打印NAVM输出(详细)
/// * 🎯BabelNAR CLI
/// * 🎯附带debug效果(检验「输出转译是否成功达到预期」)
#[inline]
pub fn print_navm_output_verbose(out: &Output) {
println!("{}", Self::format_from_navm_output_verbose(out));
}

/// ✨格式化打印CLI输出(标准错误)
Expand All @@ -144,34 +223,56 @@ impl OutputType<'_> {
/// ✨格式化打印NAVM输出(标准错误)
/// * 🎯BabelNAR CLI
#[inline]
pub fn eprint_from_navm_output(out: &Output) {
eprintln!("{}", Self::format_from_navm_output(out));
pub fn eprint_navm_output(out: &Output) {
eprintln!("{}", Self::format_navm_output(out));
}

/// ✨格式化打印NAVM输出(标准错误)(详细)
/// * 🎯BabelNAR CLI
/// * 🎯附带debug效果(检验「输出转译是否成功达到预期」)
#[inline]
pub fn eprint_navm_output_verbose(out: &Output) {
eprintln!("{}", Self::format_from_navm_output_verbose(out));
}
}

/// 快捷打印宏
#[macro_export]
macro_rules! println_cli {
// 消息 | ✨可格式化
([$enum_type_name:ident] $($tail:tt)*) => {
// 调用内部函数
$crate::cli_support::io::output_print::OutputType::$enum_type_name.print_line(&format!($($tail)*));
};
// NAVM输出 表达式
($navm_output:expr) => {
// 调用内部函数
$crate::cli_support::io::output_print::OutputType::print_from_navm_output($navm_output);
$crate::cli_support::io::output_print::OutputType::print_navm_output($navm_output);
};
// NAVM输出 表达式 | 🪄详细
(% $navm_output:expr) => {
// 调用内部函数
$crate::cli_support::io::output_print::OutputType::print_navm_output_verbose($navm_output);
};
}

/// 快捷打印宏/标准错误
#[macro_export]
macro_rules! eprintln_cli {
// 消息 | ✨可格式化
([$enum_type_name:ident] $($tail:tt)*) => {
// 调用内部函数
$crate::cli_support::io::output_print::OutputType::$enum_type_name.eprint_line(&format!($($tail)*));
};
// NAVM输出 表达式
($navm_output:expr) => {
// 调用内部函数
$crate::cli_support::io::output_print::OutputType::eprint_from_navm_output($navm_output);
$crate::cli_support::io::output_print::OutputType::eprint_navm_output($navm_output);
};
// NAVM输出 表达式 | 🪄详细
(% $navm_output:expr) => {
// 调用内部函数
$crate::cli_support::io::output_print::OutputType::eprint_navm_output_verbose($navm_output);
};
}

Expand Down

0 comments on commit a957a58

Please sign in to comment.