Java 创建具有未定义子对象数的层次结构对象

Java 创建具有未定义子对象数的层次结构对象,java,hierarchy,Java,Hierarchy,我目前正在开发一个“代码解析器”,将阀映射格式(.vmf文件)解析为java可读对象 在vmf文件中 对象有两种类型:类和属性 类具有名称,并且可以包含其他类和属性 属性有一个名称和数量不限的值 因此,我创建了一个VMFClass对象类和一个VMFProperty对象类。 我用自己创建的HierarchyObjects创建了一个列表,其中包含VMFClass/VMFProperty对象、一个UUID和parentUUID。 VMFClass对象包含两个列表,一个是子类,一个是属性 我的问题是

我目前正在开发一个“代码解析器”,将阀映射格式(.vmf文件)解析为java可读对象

在vmf文件中

  • 对象有两种类型:类和属性
  • 类具有名称,并且可以包含其他类和属性
  • 属性有一个名称和数量不限的值
因此,我创建了一个
VMFClass
对象类和一个
VMFProperty
对象类。 我用自己创建的
HierarchyObject
s创建了一个列表,其中包含
VMFClass
/
VMFProperty
对象、一个UUID和parentUUID。
VMFClass
对象包含两个列表,一个是子类,一个是属性

我的问题是,我不知道如何实现一个类包含它的所有子类,因为我不知道子类有多少子类等等

这是我的代码():

层次对象

package net.minecraft.sourcecraftreloaded.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HierarchyObject {
    private static Map<Long, Long> usedUUIDs = new HashMap<>();
    private long parentUUID;
    private long UUID;
    private Object object;

    /**
     * 
     * @param Object
     * @param parent -1 is maximum level
     */
    public HierarchyObject(Object object, long parent) {
        this.object = object;
        this.parentUUID = parent;
        while (true) {
            long random = (long) (Math.random() * Long.MAX_VALUE);
            if (usedUUIDs.containsKey(random)) {
                this.UUID = random;
                usedUUIDs.put(random, parent);
                break;
            }
        }
    }

    public long getUUID() {
        return UUID;
    }
    public long getParentUUID() {
        return parentUUID;
    }

    public static long getParentUUIDbyUUID(long UUID) {
        if (usedUUIDs.containsKey(UUID)) {
            return usedUUIDs.get(UUID);
        }
        return -1;
    }

    public Object getObject() {
        return object;
    }

    public static boolean hasChild(long UUID){
        if(usedUUIDs.containsValue(UUID)){
            return true;
        }
        if(UUID == -1){
            return true;
        }
        return false;
    }

    public boolean hasChild(){
        return hasChild(this.UUID);
    }

    public static long[] getChildUUIDs(long UUID){
        if(hasChild(UUID)){
            List<Long> cUUIDs = new ArrayList<>();
            for(int i = 0; i < usedUUIDs.size(); i++){
                for (Map.Entry<Long, Long> e : usedUUIDs.entrySet()) {
                    if(e.getValue().longValue() == UUID){
                        cUUIDs.add(e.getKey());
                    }
                }
            }
            return ListUtils.toPrimitivebyList(cUUIDs);
        }
        return null;
    }
}
VMFClass

package net.minecraft.sourcecraftreloaded.source;

import java.util.List;

public class VMFClass{
    private List<VMFClass> classes;
    private List<VMFProperty> properties;
    private String name;

    public VMFClass(String name, List<VMFClass> classes, List<VMFProperty> properties) {
        this.name = name;
        this.classes = classes;
        this.properties = properties;
    }

    public String getName() {
        return name;
    }
    public List<VMFClass> getClasses() {
        return classes;
    }
    public List<VMFProperty> getProperties() {
        return properties;
    }
    public void add(VMFClass vmfclass) {
        classes.add(vmfclass);
    }
    public void add(VMFProperty vmfproperty) {
        properties.add(vmfproperty);
    }
    public void remove(VMFClass vmfclass) {
        classes.remove(vmfclass);
    }
    public void remove(VMFProperty vmfproperty) {
        properties.remove(vmfproperty);
    }

    @Override
    public boolean equals(Object paramObject){
        if(paramObject instanceof VMFClass){
            return ((VMFClass)paramObject).properties.equals(this.properties) && ((VMFClass)paramObject).classes.equals(this.classes) && ((VMFClass)paramObject).name.equals(this.name);
        }
        return false;
    }
}

//TODO部分是层次结构对象“转换”为实际对象的地方。

如果您只是想查找特定类或接口的所有子类,这可能会对您有所帮助

概述 在我看来,你的课堂布局过于复杂

让我们试着简化它

您所描述的VMF模型本质上是一个链表树

以下是模型的外观:

                    [.vmf file] (root)
                       /    \
                 _____/      \ _____
                /                   \                
               /                     \
           (VMFClass)               (VMFClass)
             /     \                   /    \
            /       \                 /      \
           /         \               /        \
      (VMFClass)   (VMFProperties) (VMFClass)  (VMFProperties)    
       /      \                    
      /        \                
     /          \
 (VMFClass)   (VMFProperties) 
你需要什么

  • 解析器类(在您的例子中,您有
    VMFObject
    ,但是让我们调用这个类
    VMFParser
  • 您拥有的
    VMFClass
    VMFProperty
    类都很好
你不需要的

  • HierarchyObject
    类。
    VMFParser
    可以是层次结构的主控制器和容器(例如链表树模型)
  • 所有UUID(父、子等等)都是复杂的事情,但我明白为什么会有它们。您不需要它们来跟踪层次结构-Java将为我们做到这一点
VMFClass
这与其说是回答,不如说是评论。我已经有了所有子类的列表。。。我只需要一个算法将它们组合成一个“单一”对象对不起,我没有添加注释的特权。如果您将所有子对象组合成一个列表…,您要寻找的对象是什么来“组合”它们?像ANTLR4这样的解析器生成器非常适合这样的东西,但除此之外-为什么您需要层次结构对象?难道VMFClass不能简单地把指针(我敢使用C++术语:)指向它的“父”对象,并且有一个子VMFC类的集合和VMFFraskS的集合吗?ANTLR4看起来对我很好,最终会研究到…基本上这就是我想要的,但问题是,既然这些子类可以有其他的子类,我们回到我的问题上来。。。如何实现这一点?我在第一条评论中写道:只需添加
受保护的VMFClass父级到VMFClass.java。如果为null,则表示它是root。我假设
私有列表类是子类的集合。我还假设,通过类,您指的是已解析的实体,而不是java类?让我们把所有其他奇怪的代码放在一边,这与我们无关。基本上你需要某种树结构。树节点可以是您的HierarchyObject,但它缺少获取其子节点或父节点的必要方法。添加这些方法或切换到javax.swing.tree中的类(这些类用于一般用途,而不仅仅用于Java swing)。顺便说一句,我不太了解UUID(如果它真的是RFC 4122 UUID)系统是如何工作的。也许你想用它们来定位一个节点,但是你缺少一个注册表,这在OOP中并不是一个好的编码风格。这就像模拟java运行时的任务。谢谢你的回答。我真的很感激。编辑:现在我明白了。这看起来很好使用这个方法所有的类似乎都是顶级类。。。你可以看看athttps://developer.valvesoftware.com/wiki/Valve_Map_Format#The_structure_of_.vmf 如果你还没有看到。我将编辑我的答案,使之适应树形结构。
package net.minecraft.sourcecraftreloaded.source;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import net.minecraft.sourcecraftreloaded.utils.HierarchyObject;

public class VMFObject {
    private String rawfile = "";
    private List<VMFClass> toplevelclasses;

    private static final String INVALID_CHARS = "\\*,;<>|?=`´#'+~^°!§$%&()[].:-_";

    public VMFObject(List<VMFClass> toplevelclasses) {
        this.toplevelclasses = toplevelclasses;
    }

    public VMFObject() {
        this(new ArrayList<VMFClass>());
    }

    public void write(File file) {
        VMFWriter.write(file, rawfile);
    }

    public VMFObject read(File file) throws VMFParsingException {
        this.rawfile = VMFReader.read(file);
        parse();
        return this;
    }
    public List<VMFClass> getClasses() {
        return toplevelclasses;
    }

    private void parse() throws VMFParsingException {
        evaluate();
        get();
    }

    private void evaluate() throws VMFParsingException {
        char[] textchars = rawfile.toCharArray();
        int[] c = new int[]{0, 0, 0};
        int line = 0;
        int linepos = 0;
        for (int i : textchars) {
            linepos++;
            if (textchars[i] == '\n') {
                line++;
                linepos = 0;
                c[3] = 0;
                if (c[3] % 2 != 0) {
                    throw new VMFParsingException("Invalid quotes on line" + line + ":" + linepos);
                }
            }
            if (textchars[i] == '{') {
                c[1]++;
            }
            if (textchars[i] == '}') {
                c[2]++;
            }
            if (textchars[i] == '"') {
                c[3]++;
                if (c[1] - c[2] == 0) {

                }
            }
            if (textchars[i] == '/' && textchars[i + 1] == '/') {
                while (true) {
                    i++;
                    if (textchars[i] == '\n') {
                        break;
                    }
                }
            }
            if (textchars[i] == '/' && textchars[i + 1] == ' ') {
                throw new VMFParsingException("Invalid Character '/' on line" + line + ":" + linepos);
            }
            if (INVALID_CHARS.indexOf(textchars[i]) != -1) {
                throw new VMFParsingException("Invalid Character '" + textchars[i] + "' on line" + line + ":" + linepos);
            }
        }
        if (c[1] != c[2]) {
            throw new VMFParsingException("Unbalanced brackets in vmf File");
        }
    }

    public void add(VMFClass vmfclass) {
        toplevelclasses.add(vmfclass);
    }

    private void get() throws VMFParsingException {
        List<HierarchyObject> content = new ArrayList<>();
        long curparent = -1;
        String[] text = rawfile.split("\n");
        for (int i = 0; i < text.length; i++) {
            String line = text[i].trim();
            if (line.startsWith("//")) {
                continue;
            } else {
                byte quotec = 0;
                char[] linechar = line.toCharArray();
                boolean readp = false;
                List<String> reads = new ArrayList<>();
                byte creads = 0;
                for (int y = 0; y < linechar.length; y++) {
                    if (linechar[y] == '/' && linechar[y + 1] == '/') {
                        break;
                    }
                    if (linechar[y] == '"') {
                        quotec++;
                        if (quotec % 2 == 0) {
                            readp = false;
                            creads++;
                        } else {
                            readp = true;
                        }
                    }
                    if (readp) {
                        reads.set(creads, reads.get(creads) + linechar[y]);
                    }
                    if (linechar[y] == '{') {
                        HierarchyObject object = new HierarchyObject(new VMFClass(line.substring(line.substring(0, y).lastIndexOf(' '), y).trim(), null, null), curparent);
                        content.add(object);
                        curparent = object.getUUID();
                    }
                    if (linechar[y] == '}') {
                        curparent = HierarchyObject.getParentUUIDbyUUID(curparent);
                    }

                }
                content.add(new HierarchyObject(new VMFProperty(reads.remove(0), reads.toArray(new String[reads.size()])), curparent));
            }
        }
        buildObject(content);
    }

    private void buildObject(List<HierarchyObject> content) {
        long curUUID = -1;
        for(int i = 0; i < HierarchyObject.getChildUUIDs(curUUID).length; i++){
            HierarchyObject.getChildUUIDs(curUUID);
        }
        //TODO implement
    }
}
                    [.vmf file] (root)
                       /    \
                 _____/      \ _____
                /                   \                
               /                     \
           (VMFClass)               (VMFClass)
             /     \                   /    \
            /       \                 /      \
           /         \               /        \
      (VMFClass)   (VMFProperties) (VMFClass)  (VMFProperties)    
       /      \                    
      /        \                
     /          \
 (VMFClass)   (VMFProperties) 
public class VMFClass
{
    // name of the class
    private String name;

    // reference back up to the parent
    private VMFClass parentClass = null;

    // all direct children go here
    private List<VMFClass> children = new ArrayList<VMFClass>(); 

    // I don't think you need a list of properties here since your VMFProperty class holds onto an array of properties
    private VMFProperty properties;

    // set the parent of this class
    public void setParent (VMFClass parent)
    {
        this.parentClass = parent;
    }

    // get the direct children
    public List<VMFClass> getChildren()
    {
        return this.children;
    }

    // rest of methods...
}
class VMFParser
{
    private String rawfile = "";

    // this is really the container for everything - think of it as the file shell
    private VMFClass root = new VMFClass("root", null, null);

    // construct yourself with the file
    public VMFParser (String fileName)
    {
        this.rawfile = fileName;
    }

    public void parse ()
    {
        // all the parsing code goes here
        read();
        evaluate();
        get();

        // now at this point your hierarchy is built and stored in the   
        // root object in this class.

        // Use the traverse method to go through it
    }

    private void get() throws VMFParsingException
    {
        // keep a reference to the current VMFClass parent
        // starts out as root
        VMFClass currParentClass = root;

        // main parse loop
        for (...)
        {
            // if you find a class
            VMFClass currClass = new VMFClass(/* params here */);

            // add this class to the parent
            currParentClass.add(currClass);

            // set the parent of this class
            currClass.setParent(currParentClass);

            // if you find a property
            // parse and add all the properties to the property
            VMFProperty property = new VMFProperty (/* value params here */);

            // attach this property to the last VMF class that got parsed
            currClass.setPoperties(property);

            // If you nest deeper into classes, then the parent becomes the current class
            currParentClass = currClass;

            // If you go back out of a class
            currParentClass = currClass.getParent();
        }
    }

    // Traverse the hierarchy
    public void traverse ()
    {
        traverseTree(root);
    }

    private void traverseTree (VMFClass root)
    {
        System.out.println("Class Name: " + root.getName());

        // print out any properties
        VMFProperty prop = root.getProperty();

        if (prop != null)
        {
            System.out.println("Property Name: " + prop.getName());

            String [] props = prop.getValues();
            for (String s: props)
            {
                System.out.println("Value: " + s);
            }
        }

        // get all child classes
        List<VMFClass> children = root.getChildren();
        for (VMFClass c: children)
        {
            traverseTree(c);
        }   
    }
}
public static void main(String[] args)
{
    VMFParser vmfParser = null;
    try
    {
        vmfParser = new VMFParser("myFile.vmf");
        vmfParser.parse();

        // access the vmfParser for the hierarchy
        vmfParser.traverse();
    }
    catch (VMFParsingException vpe)
    {
        // do something here
        vpe.printStackTrace();
    }
    finally
    {
        // clean up...
    }
}