简体   繁体   English

Java中是否有参数树实现?

[英]Is there a Parameter Tree implementation in Java?

Java program takes a long list of inputs(parameters), churns a bit and spits some output. Java程序需要一长串输入(参数),搅动一点并吐出一些输出。

I need a way to organize these parameters in a sane way so in the input txt file I want to write them like this: 我需要一种合理地组织这些参数的方法,因此在输入txt文件中,我想这样写它们:

parameter1 = 12
parameter2 = 10
strategy1.parameter1 = "goofy"
strategy2.parameter4 = 100.0

Then read this txt file, turn it into a Java object I can pass around to objects when I instantiate them. 然后读取该txt文件,将其转换为Java对象,实例化它们时可以传递给它们。

I now pyqtgraph has ParameterTree which is handy to use; 我现在pyqtgraph有ParameterTree ,它很容易使用; is there something similar in Java? Java中有类似的东西吗? I am sure others must have had the same need so I don't want to reinvent the wheel. 我确信其他人也有同样的需求,所以我不想重新发明轮子。

(other tree structures would also be fine, of course, I just wanted something easy to read) (其他树形结构也可以,当然,我只想简单易读)

One way is to turn input.txt into input.json : 一种方法是将input.txt转换为input.json

{
    "parameter1": 12,
    "parameter2": 10,
    "strategy1": {
        "parameter1": "goofy"
    },
    "strategy2": {
        "parameter4": 100.0
    }
}

Then use Jackson to deserialize input.json into one of these: 然后使用Jackson来将input.json反序列化为以下之一:

  1. A Map<String, Object> instance, which you could navigate in depth to get all your parameters 一个Map<String, Object>实例,您可以对其进行深入导航以获取所有参数
  2. An instance of some class of your own that mimics input.json 's structure, where your parameters would reside 您自己的某个类的实例,该实例模仿input.json的结构,您的参数将驻留在该结构中
  3. A JsonNode instance that would be the root of the tree 一个JsonNode实例,它将是树的根

(1) has the advantage that it's easy and you don't have to create any class to read the parameters, however you'd need to traverse the map, downcast the values you get from it, and you'd need to know the keys in advance (keys match json object's attribute names). (1)的优点是操作简便,您无需创建任何类即可读取参数,但是您需要遍历该地图,向下转换从该地图获得的值,并且您需要知道预先设置键(键与json对象的属性名称匹配)。

(2) has the advantage that everything would be correctly typed upon deserialization; (2)具有在反序列化时可以正确键入所有内容的优点; no need to downcast anything, since every type would be a field of your own classes which represent the structure of the parameters. 无需转换任何内容,因为每种类型都是您自己的类的一个字段,它们代表参数的结构。 However, if the structure of your input.json file changed, you would need to change the structure of your classes as well. 但是,如果input.json文件的结构发生了变化,则还需要更改类的结构。

(3) is the most flexible of all, and I believe it's the option that is closest to what you have in mind, nonetheless is the most tedious to work with, since it's too low-level. (3)是所有方法中最灵活的一种,我认为这是最接近您所想的方法,但由于它太低级,因此它是最乏味的处理方法。 Please refer to this article for further details. 请参阅本文以获取更多详细信息。

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

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