需要制作一个java程序,将UML图转换为netbeans上的java类

时间:2015-03-17 17:32:43

标签: java netbeans uml

我只是想快速创建一个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(); 

            }

        }


       }

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

1 个答案:

答案 0 :(得分:0)

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

此任务有可用的工具,但您也可以从头开始。 你的开始是o.k.你正在阅读你的输入并试图直接输出你得到的东西。您可能希望将代码拆分为多个部分,而不是采用这一步骤:

  1. 扫描输入并将相关的部件类和属性名称读入相应的java类结构jclass / jattributes / joperations
  2. 进行必要的修改(在您的情况下,此步骤可能是不必要的,因为您的所有类和属性名称似乎都有效)
  3. 从结构中创建java代码
  4. 鉴于您的输入有限,您可能不会对这种方法走得太远 - 您目前只有可见性名称和属性类型,例如没有文件。作为练习,它可能是o.k.

    这是一些粗略的代码,为您提供如何继续的提示:

    /**
     * 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 
    }