def testSetMinus2(p, q): for x in p.extension(): if not x in q.extension(): dlvhex.learn((dlvhex.storeAtom((p, ) + x), dlvhex.storeAtom((q, ) + x).negate(), dlvhex.storeOutputAtom(x).negate())) dlvhex.output(x)
def testSetMinus2(p, q): for x in p.extension(): if not x in q.extension(): dlvhex.learn(( dlvhex.storeAtom((p, ) + x), dlvhex.storeAtom((q, ) + x).negate(), dlvhex.storeOutputAtom(x).negate() )) dlvhex.output(x)
def testSetMinus(p, q): premisse = () outputatoms = () input = dlvhex.getInputAtoms() for x in input: tup = x.tuple() if tup[0].value() == p.value(): # keep true monotonic input atoms if dlvhex.isTrue(x): premisse = (x, ) + premisse if x.isTrue() and not dlvhex.isTrue(dlvhex.storeAtom((q, tup[1]))): outputatoms = (dlvhex.storeOutputAtom((tup[1], )), ) + outputatoms dlvhex.output((tup[1], )) if tup[0].value() == q.value(): # keep false antimonotonic input atoms if not dlvhex.isTrue(x): premisse = (x.negate(), ) + premisse # learn one nogood for each output atom for x in outputatoms: dlvhex.learn((x.negate(), ) + premisse)
def aOrNotB(a,b): if dlvhex.learnSupportSets(): dlvhex.learn(( dlvhex.storeAtom((a, )), dlvhex.storeOutputAtom(()).negate() # then () is in the output )); dlvhex.learn(( dlvhex.storeAtom((b, )).negate(), dlvhex.storeOutputAtom(()).negate() # then () is in the output )); else: aIsTrue = dlvhex.isTrue(dlvhex.storeAtom((a, ))) bIsFalse = dlvhex.isFalse(dlvhex.storeAtom((b, ))) if aIsTrue or bIsFalse: dlvhex.output(())
def testSetMinus(p, q): premisse = () outputatoms = () input = dlvhex.getInputAtoms() for x in input: tup = x.tuple() if tup[0].value() == p.value(): # keep true monotonic input atoms if dlvhex.isTrue(x): premisse = (x, ) + premisse if not dlvhex.isTrue(dlvhex.storeAtom((q, tup[1]))): outputatoms = (dlvhex.storeOutputAtom((tup[1], )), ) + outputatoms dlvhex.output((tup[1], )) if tup[0].value() == q.value(): # keep false antimonotonic input atoms if not dlvhex.isTrue(x): premisse = (x.negate(), ) + premisse # learn one nogood for each output atom for x in outputatoms: dlvhex.learn((x.negate(), ) + premisse)
def testSetMinusPartial(p, q): # compute the set difference of the extension of p minus the one of q input = dlvhex.getInputAtoms() for x in input: tup = x.tuple() # for each possible input atom p(x) (according to the grouding, it is not necessarily true in the current input) if tup[0].value() == p.value(): qAtom = dlvhex.storeAtom((q, tup[1])) # if p(x) is true and the corresponding atom q(x) is not true (i.e., false or undefined) if dlvhex.isTrue(x) and not dlvhex.isTrue(qAtom): # if q(x) is false, then x is definitely in the output if not dlvhex.isInputAtom(qAtom) or dlvhex.isFalse(qAtom): # print "Definitely true: " + tup[1].value() dlvhex.output((tup[1], )) # if q(x) is undefined, then x might be in the output else: # print "Could be true: " + tup[1].value() dlvhex.outputUnknown((tup[1], )) v = 0 # if p(x) is undefined and q(x) is not true (i.e., false or undefined), then x might be in the output if not dlvhex.isTrue(x) and not dlvhex.isFalse( x) and not dlvhex.isTrue(qAtom): # print "Could be true: " + tup[1].value() dlvhex.outputUnknown((tup[1], )) v = 0
def testSetMinusPartial(p, q): # compute the set difference of the extension of p minus the one of q input = dlvhex.getInputAtoms() for x in input: tup = x.tuple() # for each possible input atom p(x) (according to the grouding, it is not necessarily true in the current input) if tup[0].value() == p.value(): qAtom = dlvhex.storeAtom((q, tup[1])) # if p(x) is true and the corresponding atom q(x) is not true (i.e., false or undefined) if dlvhex.isTrue(x) and not dlvhex.isTrue(qAtom): # if q(x) is false, then x is definitely in the output if not dlvhex.isInputAtom(qAtom) or dlvhex.isFalse(qAtom): # print "Definitely true: " + tup[1].value() dlvhex.output((tup[1], )) # if q(x) is undefined, then x might be in the output else: # print "Could be true: " + tup[1].value() dlvhex.outputUnknown((tup[1], )) v=0 # if p(x) is undefined and q(x) is not true (i.e., false or undefined), then x might be in the output if not dlvhex.isTrue(x) and not dlvhex.isFalse(x) and not dlvhex.isTrue(qAtom): # print "Could be true: " + tup[1].value() dlvhex.outputUnknown((tup[1], )) v=0
def main(): h1 = dlvhex.storeAtom(("q", "X")) h2 = dlvhex.storeAtom(("r", "X")) b = dlvhex.storeExternalAtom("concat", ("a", "b"), ("X", )) f = dlvhex.storeAtom(("p", "a")) r = dlvhex.storeRule((h1, h2, ), (b, ), ()); a = dlvhex.evaluateSubprogram(((f, ), (r, ))) prog = dlvhex.loadSubprogram("examples/3col.hex") print("Evaluating the program:") print(dlvhex.getValue(prog[1])) print("Facts:") print(dlvhex.getValue(prog[0])) ans = dlvhex.evaluateSubprogram(prog) for x in ans: print("Answer set:", dlvhex.getValue(x))
def storeAtom(self, tuple_): # all the tuple_ elements are ISymbol s #logging.info("jsci.storeAtom %s", tuple_) s = dlvhex.storeAtom([x.hid for x in tuple_]) #logging.info(" got symbol %s", s) r = jpype.JObject(JavaSymbolImpl(s), ISymbol) #logging.info("jsci.storeAtom %s returns %s with type %s", tuple_, repr(r), type(r)) return r
def neg(p): if dlvhex.learnSupportSets(): dlvhex.learn(( dlvhex.storeAtom((p, )).negate(), # if p is true dlvhex.storeOutputAtom(()).negate() # then () is in the output )); else: for x in dlvhex.getTrueInputAtoms(): return dlvhex.output(())
def setdiff(p,q): # for all input atoms (over p or q) for x in dlvhex.getTrueInputAtoms(): # check if the predicate is p if x.tuple()[0] == p: # check if the atom with predicate # being changed to q is NOT in the input if dlvhex.isFalse(dlvhex.storeAtom((q, x.tuple()[1]))): # then the element is in the output dlvhex.output((x.tuple()[1], ));
def id(p): if dlvhex.learnSupportSets(): dlvhex.learn(( dlvhex.storeAtom((p, )), # if p is true for some X dlvhex.storeOutputAtom(()).negate() # then () is in the output )); else: for x in dlvhex.getTrueInputAtoms(): dlvhex.output(()) return
def setdiff(p, q): # go over all input atoms (p or q) for x in dlvhex.getTrueInputAtoms(): # get predicate/argument of atom pred, arg = x.tuple() # pred(arg) # check if x is of form p(arg) if pred == p: # produce atom q(arg) qatom = dlvhex.storeAtom( (q, arg) ) # check q(arg) is NOT in input if dlvhex.isFalse(qatom): # then put arg into the output dlvhex.output( (arg,) )
def testSetMinusLearn(p, q): # is true for all constants in extension of p but not in extension of q # (same as testSetMinus) # uses learning pe = p.extension() #logging.error("ATOM got pe {}".format(repr(pe))) #for y in pe: # logging.error("ATOM y in pe {} {} {}".format(str(y), repr(y[0].symlit.lit), hash(y))) qe = q.extension() #logging.error("ATOM got qe {}".format(repr(qe))) #for y in qe: # logging.error("ATOM y in qe {} {} {}".format(str(y), repr(y[0].symlit.lit), hash(y))) for x in pe: #logging.error("ATOM x = {} {}".format(repr(x), x.__class__)) if x not in qe: #logging.error("ATOM {} not in qe".format(x)) # learn that it is not allowed that p(x) and -q(x) and this atom is false for x nogood = (dlvhex.storeAtom((p, ) + x), dlvhex.storeAtom((q, ) + x).negate(), dlvhex.storeOutputAtom(x).negate()) #logging.error("ATOM nogood {}".format(repr(nogood))) dlvhex.learn(nogood) #logging.error("ATOM output {}".format(repr(x))) dlvhex.output(x)