Example #1
0
def parity(p):

	if dlvhex.learnSupportSets():
		pos = ()
		for x in dlvhex.getInputAtoms():
			pos = pos + (False, )

		# special case: no input
		if pos == ():
			# always true
			dlvhex.learn(dlvhex.storeOutputAtom(()).negate(), )

		else:
			pos = pos[:-1]
			pos = list(pos)
			overflow = False 
			while not overflow:
				ng = ()
				# enumerate all combinations except for the last element (which is then definite)
				last = False
				for i in range(0, len(pos)):
					if pos[i] == True:
						ng = ng + (dlvhex.getInputAtoms()[i], )
						last = not last
					else:
						ng = ng + (dlvhex.getInputAtoms()[i].negate(), )

				# add last element with a sign such that the partiy is even
				if last:
					ng = ng + (dlvhex.getInputAtoms()[-1], )
				else:
        	                        ng = ng + (dlvhex.getInputAtoms()[-1].negate(), )

				# generate nogood which implies that the external atom is true
				supset = ng + (dlvhex.storeOutputAtom(()).negate(), )
				dlvhex.learn(supset)

				# go to next combination and check if we have an overflow, i.e., all combinations have been enumerated
				inc=0
				pos[inc] = not pos[inc]
				while not overflow and not pos[inc]:
					inc = inc + 1
                                        if inc >= len(pos):
                                                overflow = True
					else:
						pos[inc] = not pos[inc]

	even = True
	for atom in dlvhex.getInputAtoms():
		if atom.isTrue():
			even = not even
	if even:
		dlvhex.output(())
Example #2
0
def parity(p):

	if dlvhex.learnSupportSets():
		pos = ()
		for x in dlvhex.getInputAtoms():
			pos = pos + (False, )

		# special case: no input
		if pos == ():
			# always true
			dlvhex.learn(dlvhex.storeOutputAtom(()).negate(), )

		else:
			pos = pos[:-1]
			pos = list(pos)
			overflow = False 
			while not overflow:
				ng = ()
				# enumerate all combinations except for the last element (which is then definite)
				last = False
				for i in range(0, len(pos)):
					if pos[i] == True:
						ng = ng + (dlvhex.getInputAtoms()[i], )
						last = not last
					else:
						ng = ng + (dlvhex.getInputAtoms()[i].negate(), )

				# add last element with a sign such that the partiy is even
				if last:
					ng = ng + (dlvhex.getInputAtoms()[-1], )
				else:
        	                        ng = ng + (dlvhex.getInputAtoms()[-1].negate(), )

				# generate nogood which implies that the external atom is true
				supset = ng + (dlvhex.storeOutputAtom(()).negate(), )
				dlvhex.learn(supset)

				# go to next combination and check if we have an overflow, i.e., all combinations have been enumerated
				inc=0
				pos[inc] = not pos[inc]
				while not overflow and not pos[inc]:
					inc = inc + 1
                                        if inc >= len(pos):
                                                overflow = True
					else:
						pos[inc] = not pos[inc]

	even = True
	for atom in dlvhex.getInputAtoms():
		if atom.isTrue():
			even = not even
	if even:
		dlvhex.output(())
Example #3
0
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)
Example #4
0
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(())
Example #5
0
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)
Example #6
0
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)
Example #7
0
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(())
Example #8
0
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)
Example #9
0
 def storeOutputAtom(self, otuple):
     # all the otuple elements are ISymbol s
     #logging.info("jsci.storeOutputAtom %s", otuple)
     s = dlvhex.storeOutputAtom([x.hid for x in otuple])
     #logging.info(" got symbol %s", s)
     r = jpype.JObject(JavaSymbolImpl(s), ISymbol)
     #logging.info("jsci.storeOutputAtom %s returns %s with type %s", otuple, repr(r), type(r))
     return r
Example #10
0
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(())
Example #11
0
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
Example #12
0
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)
Example #13
0
def someSelectedLearning(selected):
    for x in dlvhex.getInputAtoms():
        if x.tuple()[0] == selected and x.isTrue():
            dlvhex.output(())
            nogood = [x, dlvhex.storeOutputAtom(()).negate()]
            dlvhex.learn(nogood)