Exemple #1
0
def parse_input(formula):

    print "----- Parsing ", repr(formula), '-----'
    char_stream = antlr3.ANTLRStringStream(formula)
    lexer = LTLMPLexer(char_stream)
    tokens = antlr3.CommonTokenStream(lexer)
    parser = LTLMPParser(tokens)
    r = parser.start()
    ast_root = r.tree # root of the AST(Type: CommonTree(antlr))

    if opts.showprogress:
        print "----- Parsing Done -----"

    if opts.debug:
        print "----- Tree Parsing -----"

    nodes = antlr3.tree.CommonTreeNodeStream(ast_root)
    nodes.setTokenStream(tokens)
    evaltree = LTLMPTree(nodes)
    ltlmp_root = evaltree.expr()
    if opts.debug:
        print "----- Tree Parsing Done -----"

    return ltlmp_root
Exemple #2
0
def satmain(ltlmp_formula, opts):
    total_result = {'sat':False, 'time':[], 'graphs':[], 'total_wg_time':0.0 }
    print "----- Parsing ", repr(ltlmp_formula), '-----'
    char_stream = antlr3.ANTLRStringStream(ltlmp_formula)
    lexer = LTLMPLexer(char_stream)
    tokens = antlr3.CommonTokenStream(lexer)
    parser = LTLMPParser(tokens)
    r = parser.start()
    ast_root = r.tree # root of the AST(Type: CommonTree(antlr))
    if opts.showprogress:
        print "----- Parsing Done -----"

    nodes = antlr3.tree.CommonTreeNodeStream(ast_root)
    nodes.setTokenStream(tokens)
    evaltree = LTLMPTree(nodes)
    if opts.debug:
        print "----- Tree Parsing -----"
    ltlmp_root = evaltree.expr()
    if opts.showprogress:
        print "----- Tree Parsing Done -----"
        print '----- AST Parse Result -----'
        print tmtc.ltl_out(ltlmp_root)
        print '----- End AST Parse Reslt-----'
        print '----- LTLMP to Nomal Form List -----'
    # from tree_ltl
    # the pairs of (ltlmp-NF, )
    pairList = tltl.get_MPNF_list(ltlmp_root)
    
    if opts.debug:
        print '----- Printing MP-NF Result -----'
        for pair in pairList:
            print '  --',tmtc.ltl_out(pair[0]),'--'
            for mp in pair[1]:
                print '   ',tmtc.mp_out(mp)
            
    # improve LTL tree
    # TODO: this is future work

    # LTL2BA: LTL to never-claim(nvc)
    # LTL2TGBA: LTL to Transition-based Generalized BA
    # LTLTrans: 
    MPNF_List = []   # MP Normal Form  Sentence? list
    GraphList = []   # 
    MPList = []      # MP-Constraints List according to Graph
    MPPropsList = [] # Props(var) used in MP-Constraints according to Graph
    if opts.tgba:
        for pair in pairList:
            g = graph_tgba(pair, opts)
            if g:
                MPNF_List.append(g)
    elif opts.ltltrans:
        for pair in pairList:
            g = graph_ltltrans(pair, opts)
            if g:
                MPNF_List.append(g)
    else:
        for pair in pairList:
            g = graph_ba(pair, opts)
            if g:
                MPNF_List.append(g)
    if opts.showprogress:
        print '---- Weighted Graph generating ----'

    wg_st = time.time()

     
    print '  -------- temporal result ---------'
    for mpnf in MPNF_List:
        res_graph = {'nodes':0, 'edges':0, 'scclist':{'nodes':[], 'edges':[]} }
        res_graph['edges'] = nx.number_of_edges(mpnf['graph'])
        res_graph['nodes'] = nx.number_of_nodes(mpnf['graph'])
        print str(res_graph['nodes'])+'  --   Nodes'
        print str(res_graph['edges'])+'  --   Edges'
        st_gtime = time.time()
        mpnf['mdg'] = get_weighted_graph(mpnf['graph'], mpnf['mp-prop-pair'], opts)
        # print mpnf['mdg']
        en_gtime = time.time()
        res_graph['mnodes'] = nx.number_of_nodes(mpnf['mdg'])
        res_graph['medges'] = nx.number_of_edges(mpnf['mdg'])
        res_graph['time'] = str(en_gtime-st_gtime)+ ' -- Weighted Graph Generating Time: '
        mpnf['graph_result'] = res_graph
        print str(en_gtime-st_gtime)+ ' -- Weighted Graph Generating Time: '
        
    if opts.showprogress:
        print '----- end weighted Graph ----'
        
    wg_en = time.time()
    total_result['total_wg_time'] = wg_en - wg_st
    '  -- Total Weighted Graph Generating Time:  '
    print str(wg_en - wg_st) + '  -- Total Weighted Graph Generating Time:  '
    print '  -------- temporal result ---------'
    for mpnf in MPNF_List:
        print str(mpnf['graph_result']['mnodes'])+'  --   Nodes'
        print str(mpnf['graph_result']['medges'])+'  --   Edges'

    """
    if opts.pdebug:
        print ' pdebug: ---- Generated MultiDiGraph ----'
        if opts.tgba:
            print ' TGBA Acc Cond: ',MDGraphList[i].graph['acccond']
        for i in range(len(MDGraphList)):
            edges = MDGraphList[i].edges(data=True)
            for edge in edges:
                print '    [',
                print edge[0]+'  --->  '+edge[1] + '\tWeight: ', edge[2]['weight'],
                if opts.tgba:
                    print '\t',edge[2]['acc'],
                print ' ]'
                """
                
    if opts.showprogress:
        print '---- SMT input generating ----'

    # for SCC which includes 'accept' state'
    #     do SAT 
    # convert Weighted Graph to SMT-Solver Sentence
    SMTCodesExtList = []
    for mpnf in MPNF_List:
        mpnf['Code/SCC'] = get_SMTCode(mpnf['mdg'], mpnf['mp-prop-pair'], opts)
        
    if opts.showprogress:
        print '---- solving SAT ----'

    # temp
    print '  -------- temporal result ---------'
    for mpnf in MPNF_List:
        # mpnf- 'Code/SCC' - 'AccSCCs' - 'sat-time'
        for accscc in mpnf['Code/SCC']['AccSCCs']:
            print str(accscc['nodes'])+'  --  AccSCC Nodes'
            print str(accscc['edges'])+'  --  AccSCC Edges'
        print str(mpnf['graph_result']['nodes'])+'  --   Nodes'
        print str(mpnf['graph_result']['edges'])+'  --   Edges'
        
    unsat = 0
    for mpnf in MPNF_List:
        # SAT for Each MPNF
        for accscc in mpnf['Code/SCC']['AccSCCs']:
            # SAT for each SCC
            st = time.time()
            smt_result = do_sat(accscc['SMTCode'], opts)
            en = time.time()
            accscc['sat_time'] = en - st
            if smt_result['sat']:
                print "   SCC-SAT found"
                total_result['sat']=True
                return process_result(MPNF_List, total_result)
            else:
                print '      SCC-partial UNSAT'
                unsat = 1
        print '    SCC-total-UNSAT'
    if unsat ==1:
        print '  Totally UNSAT'
        total_result['sat'] = False
        return process_result(MPNF_List, total_result)
        # do SAT-checking for Each SCC
    else:
        print '  all through UNSAT, no BA/TGBA-acceptable SCC, noSMTcode'
        total_result['sat'] = False
        return process_result(MPNF_List, total_result)