Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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("")
Ejemplo n.º 3
0
class InstalTest(object):

    def temporary_text_file(self,text):
        (fd,path) = mkstemp() # returns a handle/path pair
        file = open(path,'w')
        print(text,file=file)
        file.close()
        os.close(fd)
        return path

    def __init__(self,instal_texts,domain_texts,fact_text,options=""):
	#instal_texts is a list of strings
	ipath = []
	for i in instal_texts:
		ipath.append(self.temporary_text_file(i))

        dpath = []
	for d in domain_texts:
		dpath.append(self.temporary_text_file(d))
        fpath = self.temporary_text_file(fact_text)
        opath = mkdtemp()

	#-o needs to be a directory if >1 input file and a .lp file otherwise. This is a hack but it crashes otherwise.
	if len(instal_texts) > 1:
		opath_end = "/"
	else:
		opath_end = "/tmp.lp"

	#Construct argument list using temp files
        argparser = instalargparse.buildargparser()
	topass = ['-o',opath+opath_end,'-f',fpath,options]
	topass += ['-i'] + ipath
	topass += ['-d'] + dpath
        (args,unk) = argparser.parse_known_args(topass)
        # assumes input files are .ial not .lp
        args.ial_files = args.input_files
        args.lp_files = []
        model_files = instal_compile(args)
        initial_state = instal_state_facts(args)
        domain_facts = instal_domain_facts(args)
        self.sensor = Sensor(dummy,initial_state,model_files,domain_facts,args)

	# Clean up files. 
	for i in ipath:
        	os.remove(i)
	for d in dpath:
        	os.remove(d)
        os.remove(fpath)
        shutil.rmtree(opath)

    def syntax_check(self,data,terms):
        good = True;
        for d in data:
            t = parse_term(d)
            if not(t.name() in terms):
                print("Warning: unrecognized term",t)
                good = False
        return good;

    def run_test(self,event,initially=[],
                 holds=[],notholds=[],occurs=[],notoccurs=[],name=None):
        if name is None: #Default name to the event if no name set
            name = event
        # check for simple typos in term inputs
        self.syntax_check(initially+holds+notholds+[event]+occurs+notoccurs,
                          ["observed","occurred","holdsat"])
        self.sensor.holdsat += map(parse_term,initially)
        self.sensor.solve(event)
        event_sat = self.sensor.check_events(event,occurs,notoccurs)
        fact_sat = self.sensor.check_facts(event,holds,notholds)
        if (event_sat and fact_sat): # and (present != [] or absent != []):
            print("Satisfied({n}) {name}"
                  .format(name=name,n=self.sensor.cycle))