繁体   English   中英

需要制作一个Java程序以将UML图转换为NetBeans上的Java类

[英]Need to make a java program to convert UML diagram to java class on netbeans

我只想快速制作一个Java文件(使用Netbeans IDE 8.0)将两个UML图转换为我已经编写的Java文件。

注意:该类实际上不一定必须与所有UML图通用,而必须同时使用这两个。

UML图表为:CarUML.txt

Car
===================
- cost : int
- color : String
- make : String
+ count : int
===================
+ Car ( int cost )
===================

和PetUML.txt

Pet
===================
- species : String
+ isChipped : boolean
- name : String
- age : int
===================
+ Pet ( String name )
===================

我只想要一个文件,这些文件将从它们输出以下内容:

Pet.java

package UMLconv;

public class Pet {
  private String species;
  private String name;
  private int age;

  public Pet (String name) {
    this.name = name;
  }

}

和Car.java

package UMLconv;

public class Car {
  private int cost;
  private String color;
  private String make;
  public int count;

  public Car (int cost) {
    this.cost = cost;
  }

}

这是我目前所拥有的:

import java.util.Scanner;

public class CodeFromPseudo {



    public static void main(String[] args) {



          Scanner scan = new Scanner(
                CodeFromPseudo.class.getResourceAsStream("CarUML.txt"));      

        //Scanner scan = new Scanner(CodeFromPseudo.class.getResourceAsStream("PetUML.txt"));

        String line;

        String [] words;        

        while(scan.hasNextLine()){

            line = scan.next(); 

            System.out.printf("public class %s {\n",line); 

            scan.nextLine();
            scan.nextLine();


            for (int i = 0; i < 4; i++) {       

            words = scan.nextLine().split("\\s+"); 

            System.out.print((words[0].contains("-")) ? "private" : "public"); 

            System.out.printf(" %s %s;\n\n", words[3],words[1]); 

            }

            scan.nextLine();



            words = scan.nextLine().split("\\s+"); 

            System.out.print((words[0].contains("-")) ? "private " : "public "); 

            System.out.printf("%s (%s %s) {\n",words[1],words[3],words[4]); 

            System.out.printf("this.%s = %s;\n",words[4],words[4]); 

            System.out.println("}\n}"); 




            scan.nextLine(); 

            }

        }


       }

不确定我现在缺少什么? 谢谢

本质上,您正在尝试编写代码生成器。 我是BITPlan的首席架构师-一家提供用于创建发电机的工具的公司,请参见http://www.smartgenerator.org

有一些工具可用于此任务,但您也可以从头开始。 好的,您正在阅读输入内容,并尝试直接输出您所得到的内容。 与其执行这一一步方法,不如将代码分成多个部分:

  1. 扫描输入并将相关的零件类和属性名称读取到相应的Java类结构jclass / jattributes / joperations中
  2. 进行必要的修改(在您的情况下,此步骤可能是不必要的,因为所有类和属性名称似乎都有效)
  3. 从结构创建Java代码

鉴于您的输入有限,使用这种方法可能不会走得很远-您目前仅具有可见性名称和属性类型,但是没有文档。 作为练习可能没问题

以下是一些粗略的代码,可提示您如何进行:

/**
 * i represent a Java class with its attribute
 */
public class JClass {
   public String name;
   public String documentation;
   // class JAttribute needs to be defined with
   // visibility, name, type and documentation fields 
   public List<JAttribute> attributes();
   // do the same approache with operations
   public List<JOperation> operations();

   public String asJavaCode {
      String code="";
      code+="/** 
      code+=" * "+documentation;
      code+=" */";
      code+="public class "+name+" {\n";
      code+="//members";
      for (JAttribute attribute:attributes) {
        code+="// "+atttribute.documentation;
        code+=attribute.visibility+" "+attribute.type+" "+attribute.name+";"
        // add generation of getters and setters here as you see fit
      }
      // add operation handling here
      code+="}";
   }
  // add code for adding attributes 
}

暂无
暂无

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

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