从JSON嵌套树生成If-Else条件语句?

时间:2018-12-05 07:38:59

标签: json tree conditional

在这里,我的问题是我想从流程图中生成一个存储过程,然后再为此做一个清晰的嵌套结构。我制作了一个嵌套的JSON树,其条件是我的SP将取决于if面临括号关闭的问题。这是我的代码,我只是希望它能为我提供带有正确的右括号和右括号的嵌套结构。此外,它还应适用于嵌套决策

<script>
    var flow = [
        {
            'name': 'Process-1',
            'text': 'Text of enter code here1',
            'parent': "Process"
        },
        {
            'name': 'Desicion-1',
            'text': 'procCode = 1 ',
            'parent': "Process",
            'yesChild': 'Process-3',
            'noChild': 'Process-4'
        },

        {
            'name': 'Process-2',
            'text': 'Text of 2',
            'parent': "Desicion-1"
        },
        {
            'name': 'Process-3',
            'text': 'Text of 3',
            'parent': "Desicion-1"
        },
        {
            'name': 'Process-4',
            'text': 'Text of 4',
            'parent': "Process-3"
        }

    ];
    var buildtree = function () {
        var source = [];
        var childs = [];
        var counter =0;
        // build hierarchical source.
        for (i = 0; i < flow.length; i++) {
            var item = flow[i];
            var text = item["text"];
            var parent = item["parent"];
            var id = item["name"];
            if (childs[parent]) {
                if (parent.indexOf("D") != -1){
                    if(counter == 0) {
                        var item = {YesCondition: item};
                        counter++;
                    }else if(counter == 1){
                        var item = {NoCondition: item};
                        counter =0;
                    }
                }else
                    var item = {node: item};
                if (!childs[parent].childs) {
                    childs[parent].childs = [];
                }
                childs[parent].childs[childs[parent].childs.length] = item;
                childs[id] = item;
            }
            else {
                childs[id] = {node: item};
                source[id] = childs[id];
            }
        }
        return source;
    }
    var source = buildtree();

    var proCode='';
    //called with every property and its value
    var count='';
    var Start = "";
    var Desicion = false;
    var Process = false;
    var YesCondition = false;
    var NoCondition = false;
    var cond='';
    function process(key, value) {

        if (Desicion) {
            if ( key == 'text') {
                Start += "if("+ value+"){\n";
                Desicion = false;
            }
        }
        if (Process) {
            if (key == 'text') {
                    Start += value + "\n"+"}"  ;
                Process = false;

            }
        }
        if (key == 'name') {

            if (value.indexOf("D") != -1) {
                Desicion = true;
            } else if (value.indexOf("P") != -1) {
                Process = true;
            }
        }
        if (key == 'YesCondition') {
            YesCondition = true;
        }if (key == 'NoCondition') {

            NoCondition = true;

            Start +="}"+"\n"+"else{"+"\n";
        }
        console.log(key + " : " + value);

    }
    function traverse(o,func) {
        for (var i in o) {
            func.apply(this,[i,o[i]]);
            if (o[i] !== null && typeof(o[i])=="object") {
                //going one step down in the object tree!!
                traverse(o[i],func);
            }
        }
    }
    traverse(source,process);
    jQuery("#jqxTree").html(Start);
</script>

0 个答案:

没有答案