def pystream_single(args, unk): iparser = instalparser.makeInstalParser() iparser.instal_input = open(args.input_files[0], "r") if args.output_file: iparser.instal_output = open(args.output_file, "w") iparser.mode = "default" idocument = "" idocument = idocument + iparser.instal_input.read(-1) iparser.instal_parse(idocument) if args.domain_file: iparser.print_domain(args.domain_file) iparser.instal_print_all() iparser.instal_input.close() if args.output_file: iparser.instal_output.close() ##### start of multi-shot solving cycle if args.output_file and args.fact_file: institution = ["externals.lp", "time.lp", args.output_file] initial_state = open(args.fact_file, "r").readlines() print(initial_state) sensor = Sensor(iparser, dummy, initial_state, institution, args) # print("initially:") # for i in initial_state: # print(i.name(),i.args()) # print("events from:",args.event_file) # with open(args.event_file,'r') as events: # for e in events: for event in fileinput.input(unk): sensor.solve(event)
def pysolve(): args=getargs() iparser = instalparser.makeInstalParser() if args.instal_file: f = open(args.instal_file,'r') if args.output_file and args.instal_file: iparser.instal_output = open(args.output_file,'w') if args.mode_option and args.instal_file: if args.mode_option in ('s','S'): iparser.mode = "single" elif args.mode_option in ('c','C'): iparser.mode = "composite" else: sys.stderr.write("Mode option can only be s/S or c/C \n") # set up default mode to single if not args.mode_option and args.instal_file: iparser.mode = "default" idocument = "" if args.instal_file: idocument = idocument + f.read(-1) if args.instal_file: iparser.instal_parse(idocument) if args.domain_file and args.instal_file: print_domain(iparser, args.domain_file) if args.time: print_time(iparser, args.time) if args.instal_file: iparser.instal_print_all() if args.instal_file: f.close() if args.output_file: iparser.instal_output.close() # set up to solve if args.output_file and args.query_file: # print("building solver\n") ctl = gringo.Control() # print("loading model\n") ctl.load(args.output_file) # print("loading query\n") ctl.load(args.query_file) # print("grounding program\n") ctl.ground([("base", [])]) # print("running solver\n") # ctl.solve(on_model=lambda m: terms=str(m)) ctl.solve(on_model=get_holdsat) # print("done\n") processAnswerSet(terms) # print(dictByTerm) # print(dictByWhen) for when in dictByWhen: print(when) for what in dictByWhen[when]: # print(what,dictByWhen[when][what]['what'], # 'in',dictByWhen[when][what]['where']) for x in dictByWhen[when][what]: print(what+'('+x['what']+','+x['where']+')')
def instal_compile(args): if len(args.input_files) == 1: mode = "single" else: mode = "composite" all_lists = {} output_files = args.lp_files for ifile in args.ial_files: if mode == "composite": ofile = os.path.basename(ifile) ofile = ofile.replace(".ial", ".lp") ofile = args.output_file + "/" + ofile else: ofile = args.output_file output_files = [ofile] + output_files iparser = instalparser.makeInstalParser() instal_compile_file(args, mode, ifile, ofile, iparser) if mode == "composite": all_lists[iparser.names["institution"]] = [ iparser.exevents, iparser.inevents, iparser.vievents, iparser.fluents, iparser.obligation_fluents, iparser.noninertial_fluents, ] iparser.instal_input.close() if args.bridge_file: ofile = os.path.basename(args.bridge_file) ofile = ofile.replace(".ial", ".lp") ofile = args.output_file + "/" + ofile output_files = [ofile] + output_files bparser = bridgeparser.makeInstalParser() bparser.all_lists = all_lists instal_compile_file(args, "composite", args.bridge_file, ofile, bparser) return output_files
if args.bridge_file and not args.instal_files_list: sys.stderr.write("Argument Error: bridge instal needs the participiting instal files (-il) \n") exit(-1) if args.mode_option not in ('s', 'S', 'c', 'C'): sys.stderr.write("Argument Error: mode option (-m) can only be s/S or c/C \n") exit(-1) #####-----------process single instal file ----------################# # set up output defaults #print_part1 = True #print_part2 = True iparser = instalparser.makeInstalParser() if args.instal_file: f = open(args.instal_file,'r') #if args.part1_file: # p1 = open(args.part1_file,'r') #if args.part2_file: # if not(args.part1_file): # sys.stderr.write("Part2 parameter can only be used in conjunction with a part1") # p2 = open(args.part2_file,'r') # exit(-1) if args.output_file and args.instal_file: iparser.instal_output = open(args.output_file,'w')
def pyinstal(): args,unk=getargs() bparser = bridgeparser.makeInstalParser() iparser = instalparser.makeInstalParser() if args.instal_file: f = open(args.instal_file,'r') if args.output_file and args.instal_file: iparser.instal_output = open(args.output_file,'w') if args.mode_option and args.instal_file: if args.mode_option in ('s','S'): iparser.mode = "single" elif args.mode_option in ('c','C'): iparser.mode = "composite" else: sys.stderr.write("Mode option can only be s/S or c/C \n") # set up default mode to single if not args.mode_option and args.instal_file: iparser.mode = "default" idocument = "" if args.instal_file: idocument = idocument + f.read(-1) if args.instal_file: iparser.instal_parse(idocument) if args.domain_file and args.instal_file: iparser.print_domain(args.domain_file) if args.instal_file: iparser.instal_print_all() if args.instal_file: f.close() if args.output_file: iparser.instal_output.close() #####-----------process list of instal files ----------################# parser_dict = {} output_dict = {} input_dict = {} bparser.all_lists = {} if args.instal_files_list: for name in args.instal_files_list: parser= instalparser.makeInstalParser() f = open(name,'r') document = "" document = document + f.read(-1) hIn = string.rfind(name,'/') + 1 # get the index of '/' to extract the file name only name = name[hIn:] name = name.replace('.ial','') input_dict[name] = f #print name #name_of_parser = name + '_parser' parser.mode = "composite" if args.output_file: name_of_output = args.output_file + '/' + name + '.lp' parser.instal_output = open(name_of_output,'w') else: name_of_output = name + '.lp' parser.instal_output = open(name_of_output,'w') parser.instal_parse(document) parser.print_domain(args.domain_file) parser.instal_print_all() bparser.all_lists[name] = [parser.exevents, parser.inevents, parser.vievents, parser.fluents, parser.obligation_fluents, parser.noninertial_fluents] # JAP: can't see why copy is needed since these are all instance vars. # hangover from pre-class version? # all_lists[name] = [copy.deepcopy(parser.exevents), # copy.deepcopy(parser.inevents), # copy.deepcopy(parser.vievents), # copy.deepcopy(parser.fluents), # copy.deepcopy(parser.obligation_fluents), # copy.deepcopy(parser.noninertial_fluents)] f.close() parser_dict[name] = parser output_dict[name] = name_of_output # parser.clear_all_dicts() #####-----------process bridge file ----------################# if args.bridge_file: # collect all_lists from each institution parser for bridge parser # bparser.all_lists = all_lists # print(bparser.all_lists,file=sys.stderr) # set input and output for bridge file bf = open(args.bridge_file,'r') bdocument = "" bdocument = bdocument + bf.read(-1) name = args.bridge_file hIn = string.rfind(name,'/') + 1 # get the index of '/' to extract the file name only name = name[hIn:] boutput = name.replace('.ial','.lp') #boutput = args.bridge_file.replace('.ial','.lp') if args.output_file: boutput = args.output_file + '/' + boutput bparser.instal_output = open(boutput,'w') else: bparser.instal_output = open(boutput,'w') bparser.instal_parse(bdocument) bparser.mode = "composite" if args.bridge_domain_file: bparser.print_domain(args.bridge_domain_file) bparser.instal_print_all() #####-----------closing files ----------################# for name, f in input_dict.iteritems(): f.close() for name, parser in parser_dict.iteritems(): parser.instal_output.close() if args.bridge_file: bf.close() bparser.instal_output.close()
def pystream(): args, unk = getargs() if len(args.input_files) == 1: pystream_single(args, unk) return #####-----------process list of instal files ----------################# if not (args.output_file): sys.stderr.write("Output directory required for composite mode\n") exit(-1) if not (os.path.isdir(args.output_file)): sys.stderr.write("-o argument must be a directory in composite mode\n") parser_dict = {} output_dict = {} input_dict = {} bparser = bridgeparser.makeInstalParser() bparser.all_lists = {} if args.instal_files_list: for name in args.instal_files_list: parser = instalparser.makeInstalParser() parser.instal_input = open(name, "r") parser.mode = "composite" document = "" document = document + parser.instal_input.read(-1) hIn = string.rfind(name, "/") + 1 # get the index of '/' to extract the file name only name = name[hIn:] name = name.replace(".ial", "") input_dict[name] = parser.instal_input # print name # name_of_parser = name + '_parser' if args.output_file: name_of_output = args.output_file + "/" + name + ".lp" parser.instal_output = open(name_of_output, "w") else: name_of_output = name + ".lp" parser.instal_output = open(name_of_output, "w") parser.instal_parse(document) parser.print_domain(args.domain_file) parser.instal_print_all() bparser.all_lists[name] = [ parser.exevents, parser.inevents, parser.vievents, parser.fluents, parser.obligation_fluents, parser.noninertial_fluents, ] parser.instal_input.close() parser_dict[name] = parser output_dict[name] = name_of_output #####-----------process bridge file ----------################# if args.bridge_file: # set input and output for bridge file bf = open(args.bridge_file, "r") bdocument = "" bdocument = bdocument + bf.read(-1) name = args.bridge_file hIn = string.rfind(name, "/") + 1 # get the index of '/' to extract the file name only name = name[hIn:] boutput = name.replace(".ial", ".lp") # boutput = args.bridge_file.replace('.ial','.lp') if args.output_file: boutput = args.output_file + "/" + boutput bparser.instal_output = open(boutput, "w") else: bparser.instal_output = open(boutput, "w") bparser.instal_parse(bdocument) bparser.mode = "composite" if args.bridge_domain_file: bparser.print_domain(args.bridge_domain_file) bparser.instal_print_all() #####-----------closing files ----------################# for name, f in input_dict.iteritems(): f.close() for name, parser in parser_dict.iteritems(): parser.instal_output.close() if args.bridge_file: bf.close() bparser.instal_output.close() ##### start of multi-shot solving cycle if args.composite and args.output_file: # and args.fact_file: institution = ["externals.lp", "bridge_externals.lp", "time.lp", boutput] + output_dict.values() # print("files = ",institution) # facts = open(args.fact_file,'r') initial_state = [] # initially(iparser.names["institution"],facts.readlines()) sensor = Sensor(bparser, dummy, initial_state, institution, args) # print("initially:") # for i in initial_state: # print(i.name(),i.args()) # print("events from:",args.event_file) # with open(args.event_file,'r') as events: # for e in events: for event in fileinput.input(unk): # observation = parse_term(event) if args.verbose > 1: print(sensor.cycle, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") sensor.solve(event) if args.verbose > 1: print(sensor.cycle, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") sensor.cycle += 1 if args.verbose > 0: print("")