Exemple #1
0
 def check_sensor_data(self,event,data,present,absent):
     satisfied = True
     for f in present:
         # print("checking",f,present_state,sensor.holdsat)
         if not(parse_term(f) in data):
             print("Not satisfied({n})\n{event} |="
                   .format(event=event,n=self.cycle),f)
             satisfied = False
     for f in absent:
         # print("checking",f,present_state,sensor.holdsat)
         if (parse_term(f) in data):
             print("Not satisfied({n})\n{event} !|="
                   .format(event=event,n=self.cycle),f)
             satisfied = False
     # print("check_final",satisfied)
     return satisfied
Exemple #2
0
 def solve(self, event):
     if self.args.verbose>1:
         print(self.cycle,"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
     self.observation = parse_term(event)
     for x in self.undo_external:
         if self.args.verbose>1:
             print("unassign",x)
         self.ctl.assign_external(x, False)
     self.undo_external = []
     if self.args.verbose>1:
         print(self.cycle,"----------------------------------------")
     for x in self.holdsat + [self.observation]:
         if self.args.verbose>1:
             print("assign",x)
         self.ctl.assign_external(x, True)
         self.undo_external.append(x)
     self.last_solution = None
     if self.args.verbose>1:
         print(self.cycle,"----------------------------------------")
     self.ctl.solve(on_model=self.on_model)
     # self.print_universe()
     if self.args.verbose>1:
         print(self.cycle,">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
     if self.args.verbose>0: print("")
     self.cycle += 1
     return self.last_solution
Exemple #3
0
    def __init__(self, callback, initially, model_files, domain_facts, args):
        self.holdsat = initially
        self.occurred = []
        self.callback = callback
        self.last_solution = None
        self.cycle = 0
        self.horizon = 1
        self.args = args
        self.observation = None
	self.observed = []
        self.undo_external = []
        self.ctl = Control(['-c', 'horizon={0}'.format(self.horizon)])
        for x in model_files:
            if args.verbose>2: print("loading: ",x)
            self.ctl.load(x)
	parts = []
        for typename,literals in domain_facts.iteritems():
            for l in literals:
                parts += [(typename, [parse_term(l)])]
        if args.verbose>2: print("grounding: ", parts+[("base",[])])
        self.ctl.ground(parts+[("base", [])])
        if args.verbose>2: print("grounded")
	signature_types = [s[0] for s in self.ctl.domains.signatures()]
	from_domain_types = [d for d in domain_facts]
	#Testing for type names in domain file not in grounded file
	for d in from_domain_types:
            if d not in signature_types:
                print("WARNING: Type {} in domain file is not in grounded model.".format(d),file=sys.stderr)
        if args.verbose>2: print("initialized")
Exemple #4
0
 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;
Exemple #5
0
def instal_fact_iaf(iaf_file):
    initlist = []
    finallylist = []
    for init in open(iaf_file, "r").readlines():
        init = init.rstrip()  # should be unnecessary, but parse_term is sensitive
        if init == "":
            continue  # ditto
        term = parse_term(init)
        if term.name() in ["initially"]:  # ,"finally"]:
            where = term.args()[0]
            what = term.args()[1]
            # if term.name()=="initially":
            initlist = ["holdsat(" + str(what) + "," + str(where) + ")"] + initlist
            # else:
            #    finallylist = ["holdsat("+str(what)+","+str(where)+")"] + finallylist
        else:
            sys.write.stderr("WARNING: Fact not recognized:", init)
    return map(parse_term, initlist)  # (initlist,finallylist)
Exemple #6
0
def instal_query():
    argparser = buildargparser()
    # additional parameters for instalquery
    argparser.add_argument(
        '-a', '--answer-set', type=int, default=0,
        help='choose an answer set (default all)')
    argparser.add_argument(
        '-l', '--length', type=int, default=1,
        help='length of trace (default 1)')
    argparser.add_argument(
        '-n', '--number', type=int, default=1,
        help='compute at most <n> models (default 1, 0 for all)')
    args,unk = argparser.parse_known_args()
    check_args(args,unk)
    model_files = instal_compile(args)
    initial_state = instal_state_facts(args)
    domain_facts = instal_domain_facts(args)
    oracle = Oracle(initial_state,model_files,domain_facts,args)
    # delete temporary files
    if os.path.dirname(args.output_file)=='/tmp': 
        if os.path.isdir(args.output_file):
            shutil.rmtree(args.output_file)
        else:
            os.remove(args.output_file)
    observed = []
    # enumerate the events in the query file
    for i,e in enumerate(fileinput.input(args.query)):
        observed += [Fun('observed',[parse_term(e).args()[0],i])]
    # note: events is a list of Fun not strings
    oracle.solve_iter(observed)
    # print("occurred = ",oracle.occurred)
    # print("holdsat = ",oracle.holdsat)
    # write LaTeX visualizations
    if args.trace_file:
        instal_trace(args,oracle.answersets)
    if args.gantt_file:
        instal_gantt(args,oracle.answersets)
    if args.text_file:
        instal_text(args,oracle.answersets)
Exemple #7
0
    def __init__(self, initially, model_files, domain_facts, args):
        self.answersets = {}
        self.cycle = 0
        self.args = args
        self.observations = None
        self.ctl = Control(['-n', str(args.number), '-c', 'horizon={0}'.format(args.length)])
        for x in model_files:
            if args.verbose>2: print("loading: ",x)
            self.ctl.load(x)
	parts = []
        for typename,literals in domain_facts.iteritems():
            for l in literals:
                parts += [(typename, [parse_term(l)])]
        if args.verbose>2: print("grounding: ", parts+[("base",[])])
        self.ctl.ground(parts+[("base", [])])
        if args.verbose>2: print("grounded")
	signature_types = [s[0] for s in self.ctl.domains.signatures()]
	from_domain_types = [d for d in domain_facts]
	#Testing for type names in domain file not in grounded file
	for d in from_domain_types:
            if d not in signature_types:
                print("WARNING: Type {} in domain file is not in grounded model.".format(d),file=sys.stderr)
        if args.verbose>2: print("initialization complete")
def check_trace_for(answer_set, conditions, verbose=0):
    errors = 0
    answer_set = answer_set["state"]
    for h in conditions.get("holdsat",[]):
        found = False
        t = parse_term(h)
        for a in answer_set["holdsat"]:
            if cmp(a, t) == 0:
                found = True
                break
        if found:
            if verbose > 1: print("Holds (correctly)",h)
        else:
            errors += 1
            if verbose > 0: print("Doesn't hold (and should): ",h)

    for h in conditions.get("occurred",[]):
        found = False
        t = parse_term(h)
        for a in answer_set["occurred"]:
            if cmp(a, t) == 0:
                found = True
                break
        if found:
            if verbose > 1: print("Occurred (correctly)",h)
        else:
            errors += 1
            if verbose > 0: print("Didn't occur (and should have): ",h)

    for h in conditions.get("notoccurred",[]):
        if h == "*": #wait this doesn't work because live(inst) - fix.
            exit(-1)
            if len(answer_set["occurred"]) == len(conditions.get("occurred"),[]):
                if verbose > 1: print("No occurred statements other than the specified.")
            else:
                errors += 1
                if verbose > 0: print("Too many occurred statements")
        else:
            continue
            #todo

    for h in conditions.get("notholdsat",[]):
        if h == "*":
            exit(-1)
            if len(answer_set["holdsat"]) == len(conditions.get("holdsat",[])):
                if verbose > 1: print("No holdsat statements other than the specified.")
            else:
                errors += 1
                if verbose > 0: print("Too many holdsats statements")
        else:
            found = False
            t = parse_term(h)
            for a in answer_set["holdsat"]:
                if cmp(a, t) == 0:
                    found = True
                    break
            if not found:
                if verbose > 1: print("Doesn't Hold (correctly)",h)
            else:
                errors += 1
                if verbose > 0: print("Holds (and shouldn't): ",h)

    for h in conditions.get("notoccurred",[]):
        if h == "*": #this doesn't work lol
            if len(answer_set["occurred"]) == len(conditions.get("occurred",[])):
                if verbose > 1: print("No occurred statements other than the specified.")
            else:
                errors += 1
                if verbose > 0: print("Too many occurred statements")
        else:
            found = False
            t = parse_term(h)
            for a in answer_set["occurred"]:
                if cmp(a, t) == 0:
                    found = True
                    break
            if not found:
                if verbose > 1: print("Doesn't occur (correctly)",h)
            else:
                errors += 1
                if verbose > 0: print("Occurs (and shouldn't): ",h)
    return errors
Exemple #9
0
def initially(i, l):
    return map(lambda (f): Fun("holdsat", [parse_term(f), parse_term(i)]), l)