简体   繁体   English

使用 Yaml 文件作为简单的脚本记录

[英]Using Yaml file as a simple script record

I'm trying to create a simple way to make scripts to execute some tasks one by one - something similar to Gcodes in CNC machines.我正在尝试创建一种简单的方法来制作脚本以逐个执行某些任务 - 类似于 CNC 机器中的 Gcodes。 On first fire I get YAML and I'm trying to create a schema that allows me to create multiple very similar (or identical) lines, but I bounced off the wall.第一次触发时,我得到了 YAML 并且我正在尝试创建一个模式,该模式允许我创建多个非常相似(或相同)的行,但是我从墙上反弹了。 With the in YAML, every object should be unique.在 YAML 中,每个对象都应该是唯一的。 How I should prepare the schema?我应该如何准备架构? Is it possible to repeat these objects?是否可以重复这些对象?

For reading, I use the cpp-yaml lib.为了阅读,我使用cpp-yaml库。

This is an example of my YAML:这是我的 YAML 示例:

Drawer:
  uid: W1
  cmd: out

Drawer:
  uid: W2
  cmd: in

Heater:
  id: H1
  duty: 5
  temp: 4
  time: 2

Heater:
  id: H2
  duty: 1
  temp: 33
  time: 15

As a schema.json I tried both the following configurations:作为 schema.json,我尝试了以下两种配置:

"properties": {
    "Heater": {

and

"definitions": {
   "Acctuator": {

If you want the tasks to be executed one by one, a mapping is the wrong data structure.如果想让任务一个一个执行,映射就是错误的数据结构。 YAML specifically defines that the order of mapping keys is a presentation detail and must not convey content information. YAML 特别定义了映射键的顺序是一个表示细节,不能传达内容信息。 So if you want the tasks to be executed in a specific order, use a sequence:因此,如果您希望以特定顺序执行任务,请使用序列:

- !Drawer
  uid: W1
  cmd: out

- !Drawer
  uid: W2
  cmd: in

- !Heater
  id: H1
  duty: 5
  temp: 4
  time: 2

- !Heater
  id: H2
  duty: 1
  temp: 33
  time: 15

I used YAML tags for specifying the type of task, which is how YAML distinguishes between different data types.我使用 YAML 标签来指定任务的类型,这就是 YAML 区分不同数据类型的方式。 You could instead use an embedded mapping in each sequence item, eg:您可以改为在每个序列项中使用嵌入式映射,例如:

- Drawer:
    uid: W1
    cmd: out

If you want to use JSON Schema to describe your file's content, you can't use YAML tags because they are not a JSON feature.如果您想使用 JSON Schema 来描述文件的内容,则不能使用 YAML 标签,因为它们不是 JSON 功能。 However afaik the only implementation of JSON Schema that works on YAML files is in JavaScript, so you can't really use it in C++ anyway.然而,在 YAML 文件上工作的 JSON Schema 的唯一实现是在 JavaScript 中,所以无论如何你不能真正在 C++ 中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM