java绘图 -- The DOT Language

2024-03-29 06:48
文章标签 java 绘图 language dot

本文主要是介绍java绘图 -- The DOT Language,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章转自 http://blog.csdn.net/xiajian2010/article/details/23746995

缘起

在学习著名的Graphviz的工具中dot时,看到这篇语言描述,不长,就翻译了一下。翻译方法依然是带监督的机器学习,可惜的就是这个监督是不可反馈的。

正文

1. Introduction

The following is an abstract grammar defining the DOT language. Terminals are shown in bold font and nonterminals in italics. Literal characters are given in single quotes. Parentheses ( and ) indicate grouping when needed. Square brackets [ and ] enclose optional items. Vertical bars | separate alternatives.

下面是一个抽象的语法定义了DOT的语言。 终结符以粗体字体表示和非终结符用斜体表示,文字字符在单引号中给出,圆括号表示在需要的时候进行分组,方括号包含可选项目,竖线|分隔可选项。备注:下面的这种定义语言语法的表达式称为扩展巴科斯范式Extended Backus–Naur Form,EBNF),关于EBNF的介绍可以参考编译原理相关的书籍(国外的龙书,虎书,鲸书,国内的看陈火旺或冯博琴的),当然,如果仅仅是了解的话,可以看看wikipedia中的介绍(Extended Backus–Naur Form)。

graph

:

[ strict ] (graph | digraph) [ ID ] '{' stmt_list '}'

stmt_list

:

[ stmt [ ';' ] [ stmt_list ] ]

stmt

:

node_stmt

 

|

edge_stmt

 

|

attr_stmt

 

|

ID '=' ID

 

|

subgraph

attr_stmt

:

(graph | node | edge) attr_list

attr_list

:

'[' [ a_list ] ']' [ attr_list ]

a_list

:

ID [ '=' ID ] [ ',' ] [ a_list ]

edge_stmt

:

(node_id | subgraph) edgeRHS [ attr_list ]

edgeRHS

:

edgeop (node_id | subgraph) [ edgeRHS ]

node_stmt

:

node_id [ attr_list ]

node_id

:

ID [ port ]

port

:

':' ID [ ':' compass_pt ]

 

|

':' compass_pt

subgraph

:

[ subgraph [ ID ] ] '{' stmt_list '}'

compass_pt

:

(n | ne | e | se | s | sw | w | nw | c | _)

The keywords node, edge, graph, digraph, subgraph, and strict are case-independent. Note also that the allowed compass point values are not keywords, so these strings can be used elsewhere as ordinary identifiers and, conversely, the parser will actually accept any identifier。

关键字node,edge,graph,digraph,subgraph以及strict说是case-independent。需要注意的是允许compass点值不是关键字,因此这些字符串可在其他地方用作普通的标识符,并且相反地,解析器实际上接受任何标识符。(这里不是很明白在说什么)

An ID is one of the following:

  • l Any string of alphabetic ([a-zA-Z\200-\377]) characters, underscores ('_') or digits ([0-9]), not beginning with a digit;
  • l ID标识符可为下列之一:字母表([\ 377 A-ZA-Z \ 200])中的任何字符串,下划线(“_”)或数字([0-9]),但不可以数字开头; 注:这里的定义和C/C++中关于标识符的定义类似,实际上dot语言就是使用的一种类似C的语法,注释,标识符都很相似,而且dot语言(GraphViz)就是由CC++的发明者所在的AT&T开发的。
  • l a numeral [-]?(.[0-9]+ | [0-9]+(.[0-9]*)? );
  • l 数字[-]?(.[0-9]+ | [0-9]+(.[0-9]*)? );
  • l any double-quoted string ("...") possibly containing escaped quotes (\");
  • l 可能包含转义引号(\”)的任意双引号引用的字符串
  • l HTML字符串(<...>an HTML string (<...>).

(备注:上面的内容中包含正则表达式,?表示01个,+表示一个或多个,*表示0个或多个)

An ID is just a string; the lack of quote characters in the first two forms is just for simplicity. There is no semantic difference between abc_2 and "abc_2", or between 2.34 and "2.34". Obviously, to use a keyword as an ID, it must be quoted. Note that, in HTML strings, angle brackets must occur in matched pairs, and unescaped newlines are allowed. In addition, the content must be legal XML, so that the special XML escape sequences for ", &, <, and > may be necessary in order to embed these characters in attribute values or raw text.

ID只是一个字符串第一二种不使用引号形式仅仅是为了简单起见。还有就是,abc_2“abc_2”2.34“2.34”之间没有语义差异 。很显然,使用关键字作为ID时,则必须使用引号。需要注意的是,在HTML中的字符串,尖括号必须成对出现,并且允许未转义的换行符。此外,其内容必须是合法的XML此时特殊的XML转义序列“,&,<>可能是必要的,以便在属性值或原始文本中嵌入这些字符。

Both quoted strings and HTML strings are scanned as a unit, so any embedded comments will be treated as part of the strings.

引号字符串和HTML字符串都被视为一个单元,所以任何嵌入的注释将被当作字符串的一部分。

An edgeop is -> in directed graphs and -- in undirected graphs.

edgop->时表示有向图,--表示无向图。

The language supports C++-style comments: /* */ and //. In addition, a line beginning with a '#' character is considered a line output from a C preprocessor (e.g., # 34 to indicate line 34 ) and discarded.

dot语言支持C++风格的注释:/**///此外,以''字符开头的行被看作C预处理器的输出(例如,#34表示第34行),dot将忽视它

Semicolons aid readability but are not required except in the rare case that a named subgraph with no body immediately preceeds an anonymous subgraph, since the precedence rules cause this sequence to be parsed as a subgraph with a heading and a body. Also, any amount of whitespace may be inserted between terminals.

分号可以提高可读性,但不是必需的,除非在极少数情况下,一个没有身体的命名子图立刻被处理为匿名子图,因为优先级规则导致该序列被解析为一个带有标题和身体的子图。此外,在终结符之间可以插入任意数量的空白符。

As another aid for readability, dot allows single logical lines to span multiple physical lines using the standard C convention of a backslash immediately preceding a newline character. In addition, double-quoted strings can be concatenated using a '+' operator. As HTML strings can contain newline characters, they do not support the concatenation operator.

另一个增强可读性的是dot允许单个逻辑行使用标准C\加换行来跨越多个物理行。此外,双引号字符串可以使用“+”操作符来串连。由于HTML字符串可以包含换行符,所以HTML不支持连接运算符。

2. 子图和群集 Subgraphs and Clusters

Subgraphs play three roles in Graphviz. First, a subgraph can be used to represent graph structure, indicating that certain nodes and edges should be grouped together. This is the usual role for subgraphs and typically specifies semantic information about the graph components.

子图在Graphviz中发挥的三个作用。首先,将子图可用于表示某些节点和边应该被组合在一起的图形结构。这是子图通常被指定关于图形元件的语义信息。

In the second role, a subgraph can provide a context for setting attributes. For example, a subgraph could specify that blue is the default color for all nodes defined in it. In the context of graph drawing, a more interesting example is:

在第二个角色,子图可以提供设置属性的上下文。例如,子图可以对其中定义的所有节点将蓝色用作默认颜色。在图形绘制的上下文中,一个更有趣的例子是:


[plain] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. subgraph {   
  2. rank = same; A; B; C;   
  3. }   

This (anonymous) subgraph specifies that the nodes A, B and C should all be placed on the same rank if drawn using dot.

这(匿名)子图指定dot绘制的节点ABC都应该放在在同一等级。

The third role for subgraphs directly involves how the graph will be laid out by certain layout engines. If the name of the subgraph begins with cluster, Graphviz notes the subgraph as a special cluster subgraph. If supported, the layout engine will do the layout so that the nodes belonging to the cluster are drawn together, with the entire drawing of the cluster contained within a bounding rectangle. Note that, for good and bad, cluster subgraphs are not part of the DOT language, but solely a syntactic convention adhered to by certain of the layout engines.

子图的第三个角色直接涉及图形将如何被某些布局引擎布局。如果子图命令以cluster开头,Graphviz将子图作为一种特殊的集群子图(cluster subgraph)。如果布局引擎支持集群子图,引擎在执行的布局时,将属于该集群中的节点绘制在一起并将其包含在一个矩形边框中。注意集群子图不是DOT语言的一部分,仅仅是该语法约定被某些布局引擎的坚持。

3. 词法和语义注释 Lexical and Semantic Notes

If a default attribute is defined using a node, edge, or graph statement, or by an attribute assignment not attached to a node or edge, any object of the appropriate type defined afterwards will inherit this attribute value. This holds until the default attribute is set to a new value, from which point the new value is used. Objects defined before a default attribute is set will have an empty string value attached to the attribute once the default attribute definition is made.

如果在节点,边,图形语句或没有附加到任何到节点或边上属性赋值语句中定义了默认属性,此后,任何适当的类型的对象都将继承这个属性值。在被设置为一个新的值前,默认属性将一直保持。对象在默认属性被设置前定义时,对象的该属性为空的字符串值,在默认属性设置后,被覆盖为新值

Note, in particular, that a subgraph receives the attribute settings of its parent graph at the time of its definition. This can be useful; for example, one can assign a font to the root graph and all subgraphs will also use the font. For some attributes, however, this property is undesirable. If one attaches a label to the root graph, it is probably not the desired effect to have the label used by all subgraphs. Rather than listing the graph attribute at the top of the graph, and the resetting the attribute as needed in the subgraphs, one can simple defer the attribute definition if the graph until the appropriate subgraphs have been defined.

请注意,特别是子图在定义时接收父图的属性设置。这很有用的例如,在根图中指定字体,所有子图也将使用该字体。然而,对于某些属性,这是不可取的。如果一个标签附加到根图中,没必要所有子图都使用这个标签。与其在图的顶部列出的图表属性,不如根据需要在子图中设置属性。简单的延迟属性定义,直到适当的子图中定义。

If an edge belongs to a cluster, its endpoints belong to that cluster. Thus, where you put an edge can effect a layout, as clusters are sometimes laid out recursively.

如果边属于群集,其端点也属于该集群。因此,由于集群有时会递归布局,将边缘放到其中可以影响布局。

There are certain restrictions on subgraphs and clusters. First, at present, the names of a graph and it subgraphs share the same namespace. Thus, each subgraph must have a unique name. Second, although nodes can belong to any number of subgraphs, it is assumed clusters form a strict hierarchy when viewed as subsets of nodes and edges.

在子图和集群中存在一些限制。 首先,目前,图的名称和它的子图共享同一命名空间。因此,每个子图必须有一个唯一的名称。第二,虽然节点可以属于任意数量的子图,当集群被看作节点和边的子集,假设集群形成严格的层次结构。

4. 字符编码 Character encodings

The DOT language assumes at least the ascii character set. Quoted strings, both ordinary and HTML-like, may contain non-ascii characters. In most cases, these strings are uninterpreted: they simply serve as unique identifiers or values passed through untouched. Labels, however, are meant to be displayed, which requires that the software be able to compute the size of the text and determine the appropriate glyphs. For this, it needs to know what character encoding is used.

DOT的语言假定至少是ASCII字符集。带引号的字符串(包括普通和类似HTML的字符串)可能包含非ASCII字符。在大多数情况下,这些字符串是不解释:他们只是充当唯一标识符或传递中不变的值。但是,标签必须要显示,这就要求该软件能够计算的文本的大小,并确定适当的字形。为此,它需要知道使用的字符编码。

By default, DOT assumes the UTF-8 character encoding. It also accepts the Latin1 (ISO-8859-1) character set, assuming the input graph uses the charset attribute to specify this. For graphs using other character sets, there are usually programs, such as iconv, which will translate from one character set to another.

默认情况下,dot假设使用UTF-8字符编码。在输入图中使用的字符集属性指定时,dot也接受了拉丁文(ISO-8859-1)字符集。使用其他字符集的图,存在一些如iconv的软件,可以将转换字符集。

 Another way to avoid non-ascii characters in labels is to use HTML entities for special characters. During label evaluation, these entities are translated into the underlying character. This table shows the supported entities, with their Unicode value, a typical glyph, and the HTML entity name. Thus, to include a lower-case Greek beta into a string, one can use the ascii sequence β. In general, one should only use entities that are allowed in the output character set, and for which there is a glyph in the font.

另一种方式避免在标签中使用非ASCII字符是使用HTML实体的特殊字符。在标签的计算中,这些实体被翻译成、底层字符。 下表显示了支持的实体,表中展示了Unicode值,典型的字形和HTML实体名称。因此,为包括小写希腊的β成一个字符串,可以使用ASCII字符序列&beta。一般情况下,应该只使用被允许在输出字符集中以及在在字体的字形中的。

 

In quoted strings in DOT, the only escaped character is double-quote ("). That is, in quoted strings, the dyad \" is converted to "; all other characters are left unchanged. In particular, \\ remains \\. Layout engines may apply additional escape sequences.

在dot带引号的字符串,唯一的转义字符是双引号(),也就是说,在带引号的字符串,\“转换为”;所有其它字符都保持不变。特别是,\\依然是\\。布局引擎可能会应用额外转义序列。

后记

Dot语言来自http://hughesbennett.co.uk/Graphviz的引用文档,除了dot语言外,还有一系列的文档,例如结点和边的属性结点的形状颜色的名字,此外hughesbennet网站本身很有趣,国外作者还真是热心加性情。后来发现,hughesbennet上的dot语言及其相关属性的文档来自GraphViz官方,但是,官方的文档的排版不如hughesbennet中的简洁大方。

Graphviz的中文处理问题:

1.保存为UTF8格式

2.设置中文字体,一些中文字体的对应的名字如下:

黑体:SimHei 宋体:SimSun 新宋体:NSimSun 仿宋:FangSong 楷体:KaiTi

仿宋_GB2312FangSong_GB2312 楷体_GB2312KaiTi_GB2312

此外,Ubuntu中系统安装了那些字体可以通过命令fc-list查看。

参考文献:

维基百科Graphviz:http://en.wikipedia.org/wiki/Graphviz

Graphviz项目的主页:http://graphviz.org/ 

官方文档地址http://www.graphviz.org/Documentation.php

Graphvizhttp://hughesbennett.co.uk/Graphviz

Graphviz中文乱码问题

使用 Graphviz 生成自动化系统图:http://www.ibm.com/developerworks/cn/aix/library/au-aix-graphviz/

这篇关于java绘图 -- The DOT Language的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/857896

相关文章

Spring Boot配置和使用两个数据源的实现步骤

《SpringBoot配置和使用两个数据源的实现步骤》本文详解SpringBoot配置双数据源方法,包含配置文件设置、Bean创建、事务管理器配置及@Qualifier注解使用,强调主数据源标记、代... 目录Spring Boot配置和使用两个数据源技术背景实现步骤1. 配置数据源信息2. 创建数据源Be

Spring Boot 3.x 中 WebClient 示例详解析

《SpringBoot3.x中WebClient示例详解析》SpringBoot3.x中WebClient是响应式HTTP客户端,替代RestTemplate,支持异步非阻塞请求,涵盖GET... 目录Spring Boot 3.x 中 WebClient 全面详解及示例1. WebClient 简介2.

Java中使用 @Builder 注解的简单示例

《Java中使用@Builder注解的简单示例》@Builder简化构建但存在复杂性,需配合其他注解,导致可变性、抽象类型处理难题,链式编程非最佳实践,适合长期对象,避免与@Data混用,改用@G... 目录一、案例二、不足之处大多数同学使用 @Builder 无非就是为了链式编程,然而 @Builder

在IntelliJ IDEA中高效运行与调试Spring Boot项目的实战步骤

《在IntelliJIDEA中高效运行与调试SpringBoot项目的实战步骤》本章详解SpringBoot项目导入IntelliJIDEA的流程,教授运行与调试技巧,包括断点设置与变量查看,奠定... 目录引言:为良驹配上好鞍一、为何选择IntelliJ IDEA?二、实战:导入并运行你的第一个项目步骤1

Spring Boot从main方法到内嵌Tomcat的全过程(自动化流程)

《SpringBoot从main方法到内嵌Tomcat的全过程(自动化流程)》SpringBoot启动始于main方法,创建SpringApplication实例,初始化上下文,准备环境,刷新容器并... 目录1. 入口:main方法2. SpringApplication初始化2.1 构造阶段3. 运行阶

Spring Boot3.0新特性全面解析与应用实战

《SpringBoot3.0新特性全面解析与应用实战》SpringBoot3.0作为Spring生态系统的一个重要里程碑,带来了众多令人兴奋的新特性和改进,本文将深入解析SpringBoot3.0的... 目录核心变化概览Java版本要求提升迁移至Jakarta EE重要新特性详解1. Native Ima

Java中的xxl-job调度器线程池工作机制

《Java中的xxl-job调度器线程池工作机制》xxl-job通过快慢线程池分离短时与长时任务,动态降级超时任务至慢池,结合异步触发和资源隔离机制,提升高频调度的性能与稳定性,支撑高并发场景下的可靠... 目录⚙️ 一、调度器线程池的核心设计 二、线程池的工作流程 三、线程池配置参数与优化 四、总结:线程

Spring Boot 与微服务入门实战详细总结

《SpringBoot与微服务入门实战详细总结》本文讲解SpringBoot框架的核心特性如快速构建、自动配置、零XML与微服务架构的定义、演进及优缺点,涵盖开发环境准备和HelloWorld实战... 目录一、Spring Boot 核心概述二、微服务架构详解1. 微服务的定义与演进2. 微服务的优缺点三

我们来说说Java LockSupport 的 park 和 unpark

《我们来说说JavaLockSupport的park和unpark》LockSupport是JDK底层线程阻塞工具,通过park/unpark实现线程阻塞与唤醒,避免死锁,与Object的w... 目录一、LockSupport1.1、LockSupport函数列表1.2、基本使用先 park 再 unpa

SpringBoot集成MyBatis实现SQL拦截器的实战指南

《SpringBoot集成MyBatis实现SQL拦截器的实战指南》这篇文章主要为大家详细介绍了SpringBoot集成MyBatis实现SQL拦截器的相关知识,文中的示例代码讲解详细,有需要的小伙伴... 目录一、为什么需要SQL拦截器?二、MyBATis拦截器基础2.1 核心接口:Interceptor