完整指南)
LikeC4 视图 include/exclude 谓词与通配符*、_、**完整指南【免费下载链接】likec4Visualize, collaborate, and evolve the software architecture with always actual and live diagrams from your code项目地址: https://gitcode.com/GitHub_Trending/li/likec4本篇技术指南聚焦 LikeC4 视图views中include/exclude谓词的三类通配符语义*、_、**与作用域视图scoped view的 include 基集行为结合本仓库 like-c4.langium 文法定义与 FqnRefParser.ts 解析实现讲清立即子级 vs 递归后代的边界、where过滤组合、关系谓词-/-的邻居扩展规则以及常见误用。读完你将能精确控制任意层级模型的元素与关系显示写出可维护、可预期的视图定义。三种通配符语义*、_、**LikeC4 的视图谓词中定义了三种通配符形式*、_和**它们的作用范围截然不同。理解三者差异是掌握 LikeC4 视图过滤的第一步。*— 立即子级与直接包含关系views { view container-overview { include * } }include *的语义要点包含作用域元素的直接子级向下仅一层包含同一作用域下定义的直接关系direct relationships defined at the same scope不包含孙级或更深的后代链条grandchildren / descendant chains。从文法上看ViewRulePredicate: (isInclude?include | exclude) exprsExpressions见 like-c4.langium中谓词统一解析为表达式列表而在解析层带. *选择器的 Fqn 引用被归一化为selector: children见 FqnRefParser.ts这也印证了*只代表子级而非所有后代。_— 匿名匹配器匹配任意元素views { view with-predicates { include * where kind is service exclude _ where tag is #deprecated } }_匹配模型中的任意元素any element in the model通常与where谓词配合按 kind、tag 或 metadata 进行筛选典型用法exclude _ where tag is #deprecated—— 排除所有打了 deprecated 标签的元素。在解析实现中._选择器被映射为selector: expanded见 FqnRefParser.ts与*children和**descendants在语义上有本质区别。**— 递归下降所有后代views { view full-tree { include ** } }递归包含所有后代孙级、曾孙级……直至叶子不是include *的同义形式后者仅立即子级当你希望在单个视图中呈现完整嵌套层级时使用。对应源码中.**选择器被映射为selector: descendants见 FqnRefParser.ts并在 fqn-index.ts 的descendants索引中按全限定名Fqn查询所有后代节点。作用域视图Scoped View的 include 基集语义在作用域视图中view name of parent { ... }include的基集base set有特殊含义views { view backend-overview of cloud.backend { include * // 基集cloud.backend 其立即子级 include - cloud.backend // 追加指向作用域的内向关系 include - * // 追加子级出发的外向关系 } }关键事实作用域视图中的include * 作用域父元素 仅直接子级*上下文内的孙级不会被包含需要include **或显式 FQN 包含-、-等关系谓词可以进一步扩展出现的邻居元素。仓库中 issue-912.spec.ts 的测试用例即验证了include b1, b1.*, b2, b2.*这类父级 子级组合的解析行为可作为作用域/非作用域视图混合使用的参考。常见过滤模式展示容器及其组件views { view container-details of cloud.backend.api { include * // api父级 全部组件 include - api // 追加外部调用方 } }递归展示后代views { view system-tree of cloud { include ** // cloud 的全部后代完整树 } }按 kind 过滤views { view services-only { include * where kind is service include ** where kind is service // 任意层级的全部 service } }按 tag 过滤views { view critical-view { include * where tag is #critical exclude _ where tag is #deprecated } }按 metadata 过滤真实仓库示例本仓库 metadata-views/views.c4 提供了完整的元数据过滤实战样例展示了where谓词与metadata组合的多种写法views { view metadataProdOnly of metadataExample { title Production Only include * where metadata.environment is production } view metadataNotStaging of metadataExample { title Exclude Staging include * exclude * where metadata.environment is staging } view metadataHasVersion of metadataExample { title Elements with Version include * where metadata.version } view metadataProdComponents of metadataExample { title Prod Components (not databases) include * where metadata.environment is production and kind is not database } view metadataCritical of metadataExample { title Critical Components include * where metadata.critical is true } }这些示例展示了where谓词的表达能力可比较字符串is production、判断存在性where metadata.version、布尔值is true、以及用and组合条件并叠加kind is not database这样的否定 kind 过滤。混合多级通配符的真实用法cloud-system/views.c4 是一个同时运用*、cloud.*与exclude的典型视图views { view cloud of cloud { title The Cloud System include *, ui.*, next.*, legacy.* exclude supportUser, ui.supportPanel, next - legacy ... } }其中ui.*、next.*、legacy.*表示某元素下的直接子级children 选择器与裸*当前作用域下直接子级 直接关系形成互补exclude则同时支持元素 FQN 与关系表达式next - legacy说明 exclude 与 include 共享同一套表达式语法。关系谓词扩展Relationship Predicate Expansion在作用域视图中可以通过关系谓词扩展出现的邻居元素views { view backend-with-neighbors of cloud.backend { include * // 基集backend 作用域 直接子级 include - cloud.backend // 追加来自作用域外部的内向边 } }关键点这不会改变元素的基集base set of elements——它只是包含以可见元素为终点的关系。为了让这些边在视觉上可渲染拥有入边incoming edges的作用域邻居元素也会被一并带入。总结三种关系谓词的扩展方向形式含义效果include - elementelement 的内向关系显示外部来源include element -element 的外向关系显示目标include - elementelement 的双向关系显示对称依赖通配符**与显式 include 的取舍使用**递归views { view all-elements { include ** } }自动包含全部后代适用于层级深度未知或会变化的场景。使用* 显式 FQNviews { view two-levels { include * // 第 1 层 include cloud.* // cloud 之下的第 2 层 } }更显式需要事先了解层级结构适合需要精细粒度控制的场景。常见错误Common Mistakes错误写法正确写法原因include **却只想要立即子级include ***是递归的单层请用*作用域视图中忘记追加关系过滤include *include - parent不显式包含时关系可能不会自动渲染误以为include *会包含孙级改用include ***仅限立即子级总结表形式匹配内容作用域上下文*直接子级 同级关系向下一层_任意元素配合where过滤按 kind、tag、metadata 匹配**递归所有后代任意深度include - elementelement 的内向关系显示外部来源include element -element 的外向关系显示目标include - elementelement 的双向关系显示对称依赖作用域视图中的要点include *建立的基集 作用域父元素 立即子级-与-关系谓词可以带入渲染这些关系所需的邻居元素。进阶全局谓词组复用当多个视图需要重复使用相同的 include/exclude 规则时LikeC4 文法还提供了predicateGroup机制见 like-c4.langiumpredicateGroup name { // 这里可以写任意 ViewRulePredicate }全局谓词组内嵌ViewRulePredicate*可在视图规则中通过global predicate name引用见 ViewRuleGlobalPredicateRef把*、**、where过滤等常用组合抽成可复用的谓词单元减少多视图间的重复代码。这一机制与本指南中的通配符语义完全正交可与任意谓词组合使用。【免费下载链接】likec4Visualize, collaborate, and evolve the software architecture with always actual and live diagrams from your code项目地址: https://gitcode.com/GitHub_Trending/li/likec4创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考