示例#1
0
def sccdToGeneric(sccd, platform):
    succesfull_generation = False
    generator = GenericGenerator(platform)
    sccd.accept(generator)
    generic = generator.get()
    Logger.showInfo("Classes <" + ", ".join(sccd.class_names) +
                    "> have been converted to generic language constructs.")
    return generic
示例#2
0
def genericToTarget(generic, target_language, output_file):
    try:
        f = FileWriter(output_file)
        if target_language == "javascript":
            writer = JavascriptWriter(f)
        elif target_language == "python":
            writer = PythonWriter(f)
        else:
            raise Exception("Language not supported")
        generic.accept(writer)
        Logger.showInfo(
            "Generic language constructs have been converted to target language constructs and have been written to file '"
            + output_file + "'.")
    finally:
        f.close()
示例#3
0
    def __init__(self, input_file):
        diagram_dir = os.path.dirname(input_file)
        tree = ET.parse(input_file)
        self.root = tree.getroot()
        self.name = self.root.get("name", "")
        self.author = self.root.get("author", "")
        descriptions = self.root.findall("description")
        self.language = self.root.get("language", "")
        if descriptions : 
            self.description = descriptions[0].text
        else :
            self.description = ""

        xml_classes = self.root.findall("class")
        # make sure at least one class is given
        if not xml_classes :
            raise CompilerException("Found no classes to compile.")

        # check if class diagram is valid
        # unique class names
        self.class_names = []
        substituted_xml_classes = []
        default_substituted_xml_classes = []
        for xml_class in xml_classes :
            class_src = xml_class.get("src", "")
            class_default = xml_class.get("default", "")
            if class_src != "":
                if not os.path.isabs(class_src):
                    class_src = os.path.join(diagram_dir, class_src)
                substituted_xml_class = ET.parse(class_src).getroot()
            else:
                substituted_xml_class = xml_class

            if class_default.lower() == "true":
                default_substituted_xml_classes.append(substituted_xml_class)

            name = substituted_xml_class.get("name", "")
            if name == "" :
                raise CompilerException("Missing or emtpy class name.")
            if name in self.class_names :
                raise CompilerException("Found 2 classes with the same name : " + name + ".")
            self.class_names.append(name)
            substituted_xml_classes.append(substituted_xml_class)
    
        # process in and output ports
        inports = self.root.findall("inport")
        names = []
        for xml_inport in inports :
            name = xml_inport.get("name", "")
            if name in names :
                raise CompilerException("Found 2 INPorts with the same name : " + name + ".")
            names.append(name)
        self.inports = names
        
        outports = self.root.findall("outport")
        names = []
        for xml_outport in outports :
            name = xml_outport.get("name", "")
            if name in names :
                raise CompilerException("Found 2 OUTPorts with the same name : " + name + ".")
            names.append(name)
        self.outports = names
            
        
        # any inital import code that has to come at the top of the generate file
        tops = self.root.findall("top")
        self.includes = []
        if len(tops) == 1 :
            self.top = tops[0].text
        elif len(tops) > 1 : 
            raise CompilerException("Class diagram can only have one <top> element.")
        else :
            self.top = ""
        
        # process each class in diagram
        self.classes = []
        default_classes = []
    
        for xml_class in substituted_xml_classes:
            processed_class = None
            try :
                processed_class = Class(xml_class, self)
            except CompilerException as e :
                e.message = "Class <" + xml_class.get("name", "") + "> failed compilation. " + e.message
                raise e
    
            # let user know this class was successfully loaded
            Logger.showInfo("Class <" + processed_class.name + "> has been successfully loaded.")
            self.classes.append(processed_class)

            if xml_class in default_substituted_xml_classes:
                default_classes.append(processed_class)
            
        if not default_classes or len(default_classes) > 1:
            if len(self.classes) == 1 :
                Logger.showInfo("Only one class given. Using <" + self.classes[0].getName() + "> as the default class.")
                default_classes.append(self.classes[0])
            else :
                raise CompilerException("Provide one and only one default class to instantiate on start up.")
        self.default_class = default_classes[0]

        # check if there's a test
        self.test = None
        test_nodes = self.root.findall("test")
        if test_nodes:
            test_node = test_nodes[0]

            input_nodes = test_node.findall("input")
            if input_nodes:
                input_node = input_nodes[0]
                test_input = DiagramTestInput(input_node)
            else:
                test_input = None

            expected_nodes = test_node.findall("expected")
            if expected_nodes:
                expected_node = expected_nodes[0]
                test_expected = DiagramTestExpected(expected_node)
            else:
                test_expected = None

            self.test = DiagramTest(test_input, test_expected)