Esempio n. 1
0
def controls(controlsStk):
    controlDict = dict()

    for x in dlvhex.getTrueInputAtoms():
        if x.tuple()[1].value() in controlDict:
            if x.tuple()[3].value() in controlDict[x.tuple()[1].value()]:
                newval = str(
                    int(controlDict[x.tuple()[1].value()][
                        x.tuple()[3].value()]) + int(x.tuple()[4].value()))
                controlDict[x.tuple()[1].value()][x.tuple()
                                                  [3].value()] = newval
            else:
                controlDict[x.tuple()[1].value()][
                    x.tuple()[3].value()] = x.tuple()[4].value()
        else:
            controlDict[x.tuple()[1].value()] = dict()
            controlDict[x.tuple()[1].value()][
                x.tuple()[3].value()] = x.tuple()[4].value()

    unknownControlDict = dict()

    for x in dlvhex.getInputAtoms():
        if x not in dlvhex.getTrueInputAtoms():
            if x.tuple()[1].value() in unknownControlDict:
                if x.tuple()[3].value() in unknownControlDict[x.tuple()
                                                              [1].value()]:
                    newval = str(
                        int(unknownControlDict[x.tuple()[1].value()][
                            x.tuple()[3].value()]) + int(x.tuple()[4].value()))
                    unknownControlDict[x.tuple()[1].value()][
                        x.tuple()[3].value()] = newval
                else:
                    unknownControlDict[x.tuple()[1].value()][
                        x.tuple()[3].value()] = x.tuple()[4].value()
            else:
                unknownControlDict[x.tuple()[1].value()] = dict()
                unknownControlDict[x.tuple()[1].value()][
                    x.tuple()[3].value()] = x.tuple()[4].value()

    for company1 in controlDict:
        for company2 in controlDict[company1]:
            if int(controlDict[company1][company2]) > 50:
                dlvhex.output((company1, company2))

    for company1 in unknownControlDict:
        for company2 in unknownControlDict[company1]:
            if company1 in controlDict and company2 in controlDict[company1]:
                if int(unknownControlDict[company1][company2] +
                       controlDict[company1][company2]) > 50:
                    dlvhex.outputUnknown((company1, company2))
            else:
                if int(unknownControlDict[company1][company2]) > 50:
                    dlvhex.outputUnknown((company1, company2))
Esempio n. 2
0
def id(p):
    for x in dlvhex.getTrueInputAtoms():
        tup = x.tuple()
        if len(tup) != 2:
            raise Exception(
                "this external atom processes only arity 1 predicate inputs")
        dlvhex.output((tup[1], ))
Esempio n. 3
0
def greaterOrEqual(p, idx, bound):
	sum = 0
	for x in dlvhex.getTrueInputAtoms():
		if x.tuple()[0] == p:
			sum += x.tuple()[idx.intValue()].intValue()
	if sum >= bound.intValue():
		dlvhex.output(())
Esempio n. 4
0
def greaterOrEqual(p, idx, bound):
	sum = 0
	for x in dlvhex.getTrueInputAtoms():
		if x.tuple()[0] == p:
			sum += x.tuple()[idx.intValue()].intValue()
	if sum >= bound.intValue():
		dlvhex.output(())
Esempio n. 5
0
def rq(predic):
  for x in dlvhex.getTrueInputAtoms():
    io = {'ind':'money','amalB':'goggles','altD':'yogamat','gansD':'money'}
    inp = x.tuple()[1].value()
    if predic == x.tuple()[0]:
      if inp in io:
        dlvhex.output((io[inp],))
Esempio n. 6
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
Esempio n. 7
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(())
Esempio n. 8
0
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], ));
Esempio n. 9
0
def controls(controlsStk):
	controlDict = dict()

	for x in dlvhex.getTrueInputAtoms():
		if x.tuple()[1].value() in controlDict:
			if x.tuple()[3].value() in controlDict[x.tuple()[1].value()]:
				newval = str(int(controlDict[x.tuple()[1].value()][x.tuple()[3].value()]) + int(x.tuple()[4].value()))
				controlDict[x.tuple()[1].value()][x.tuple()[3].value()] = newval
			else:
				controlDict[x.tuple()[1].value()][x.tuple()[3].value()] = x.tuple()[4].value()
		else:
			controlDict[x.tuple()[1].value()] = dict()
			controlDict[x.tuple()[1].value()][x.tuple()[3].value()] = x.tuple()[4].value()

	unknownControlDict = dict()

	for x in dlvhex.getInputAtoms():
		if x not in dlvhex.getTrueInputAtoms():
			if x.tuple()[1].value() in unknownControlDict:
				if x.tuple()[3].value() in unknownControlDict[x.tuple()[1].value()]:
					newval = str(int(unknownControlDict[x.tuple()[1].value()][x.tuple()[3].value()]) + int(x.tuple()[4].value()))
					unknownControlDict[x.tuple()[1].value()][x.tuple()[3].value()] = newval
				else:
					unknownControlDict[x.tuple()[1].value()][x.tuple()[3].value()] = x.tuple()[4].value()
			else:
				unknownControlDict[x.tuple()[1].value()] = dict()
				unknownControlDict[x.tuple()[1].value()][x.tuple()[3].value()] = x.tuple()[4].value()


	for company1 in controlDict:
		for company2 in controlDict[company1]:
			if int(controlDict[company1][company2]) > 50:
				dlvhex.output((company1,company2))

	for company1 in unknownControlDict:
		for company2 in unknownControlDict[company1]:
			if company1 in controlDict and company2 in controlDict[company1]:
				if int(unknownControlDict[company1][company2] + controlDict[company1][company2]) > 50:
					dlvhex.outputUnknown((company1,company2))
			else:
				if int(unknownControlDict[company1][company2]) > 50:
					dlvhex.outputUnknown((company1,company2))
Esempio n. 10
0
def testSetMinus(p, q):
    pset, qset = set(), set()
    for x in dlvhex.getTrueInputAtoms():
        tup = x.tuple()
        if tup[0].value() == p.value():
            pset.add(tup[1].value())
        elif tup[0].value() == q.value():
            qset.add(tup[1].value())
    rset = pset - qset
    for r in rset:
        dlvhex.output((r, ))
Esempio n. 11
0
def sortandmap(p, q):
  # get all tuples
  tuples = [ x.tuple() for x in dlvhex.getTrueInputAtoms() ]
  # p and q tuples
  ptuples = filter(lambda x: x[0] == p, tuples)
  qtuples = filter(lambda x: x[0] == q, tuples)
  # sorted p and q extensions
  pext = sorted(map(lambda x: x[1], ptuples))
  qext = sorted(map(lambda x: x[1], qtuples))
  # output all pairs
  for out in zip(pext, qext):
    dlvhex.output(out)
Esempio n. 12
0
def testSetMinus(p, q):
    # is true for all constants in extension of p but not in extension of q
    pset, qset = set(), set()
    for x in dlvhex.getTrueInputAtoms():
        tup = x.tuple()
        if tup[0].value() == p.value():
            pset.add(tup[1].value())
        elif tup[0].value() == q.value():
            qset.add(tup[1].value())
    rset = pset - qset
    for r in rset:
        dlvhex.output((r, ))
Esempio n. 13
0
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,) )
Esempio n. 14
0
def strategicConflict(conflicting,strategic):
	trueList = []
	falseList = []

	for x in dlvhex.getInputAtoms():
		if x.tuple()[0] == strategic and x.isTrue():
			trueList.append(x.tuple()[1])
		elif x.tuple()[0] == strategic and x.isFalse():
			falseList.append(x.tuple()[1])

	for x in dlvhex.getTrueInputAtoms():
		if x.tuple()[0] == conflicting:
			if (x.tuple()[1] in trueList) and (x.tuple()[2] in trueList):
				dlvhex.output(())
			elif (x.tuple()[1] not in falseList) and (x.tuple()[2] not in falseList):
				dlvhex.outputUnknown(())
Esempio n. 15
0
def testNonmon2(p):
    pset = set()
    for x in dlvhex.getTrueInputAtoms():
        tup = x.tuple()
        pset.add(tup[1].intValue())
    mapping = {
        frozenset([]): [2],
        frozenset([1]): [2],
        frozenset([2]): [],
        frozenset([1, 2]): [1, 2],
    }
    pset = frozenset(pset)
    if pset not in mapping:
        raise Exception(
            "testNonmon2 is supposed to handle only input domain {1,2}")
    for o in mapping[pset]:
        dlvhex.output((o, ))
Esempio n. 16
0
 def extension(self):
   '''
   returns a sequence of tuples of true atoms with predicate using this ClingoID
   fails if this ClingoID does not hold a constant
   '''
   if self.symlit.sym.type != clingo.SymbolType.Function or self.symlit.sym.arguments != []:
     raise Exception("cannot call extension() on term that is not a constant. was called on {}".format(self.__value))
   # extract all true atoms with matching predicate name
   ret_atoms = [
     x for x in dlvhex.getTrueInputAtoms()
     if x.symlit.sym.type == clingo.SymbolType.Function and x.symlit.sym.name == self.__value ]
   # convert into tuples of ClingoIDs without literal (they are terms, not atoms)
   ret = frozenset([
     tuple([ClingoID(self.ccontext, SymLit(term, None)) for term in x.symlit.sym.arguments])
     for x in ret_atoms ])
   #logging.warning("extension of {} returned {}".format(self.__value, repr(ret)))
   return ret
Esempio n. 17
0
def strategicConflict(conflicting, strategic):
    trueList = []
    falseList = []

    for x in dlvhex.getInputAtoms():
        if x.tuple()[0] == strategic and x.isTrue():
            trueList.append(x.tuple()[1])
        elif x.tuple()[0] == strategic and x.isFalse():
            falseList.append(x.tuple()[1])

    for x in dlvhex.getTrueInputAtoms():
        if x.tuple()[0] == conflicting:
            if (x.tuple()[1] in trueList) and (x.tuple()[2] in trueList):
                dlvhex.output(())
            elif (x.tuple()[1] not in falseList) and (x.tuple()[2]
                                                      not in falseList):
                dlvhex.outputUnknown(())
Esempio n. 18
0
def isCollision(location_predicate):
    """External predicate to detect if the plan is collision-free."""

    # Get all locations at all time steps
    dlvhex_input = [dlvhex.getValue(i) for i in dlvhex.getTrueInputAtoms()]

    # Get states from input
    plan = utils.get_task_plan(dlvhex_input)

    # Get information of continuous state and its discretization
    config_info, discrete_info = utils.get_dataframes(plan)
    verbose, col_thresh = utils.read_settings()

    # Check for collisions in the plan
    if not collision.colliding(
            plan,
            config_info,
            discrete_info,
            verbose=verbose,
            collision_threshold=col_thresh,
    ):
        return None

    return dlvhex.output(())
Esempio n. 19
0
def rq(loc_pred):
  for x in dlvhex.getTrueInputAtoms():
    arg = x.tuple()[1].value() # get argument
    if loc_pred == x.tuple()[0]: # check predicate
      if arg in RES:
        dlvhex.output( (RES[arg],) )
Esempio n. 20
0
def cnt(p):
	c = 0
	for x in dlvhex.getTrueInputAtoms():
		c = c + 1
	dlvhex.output((c, ))
Esempio n. 21
0
 def getTrueInputAtoms(self):
     return self._adapt(dlvhex.getTrueInputAtoms())
Esempio n. 22
0
def testEven(pred1, pred2):
    true = [x for x in dlvhex.getTrueInputAtoms()]
    num = len(true)
    if num % 2 == 0:
        dlvhex.output(())
Esempio n. 23
0
def testC(pred):
    for atom in dlvhex.getTrueInputAtoms():
        for x in atom.tuple()[1:]:
            # output arguments of input predicate
            dlvhex.output((x.value(), ))
Esempio n. 24
0
def testB(pred1, pred2):
    if len(dlvhex.getTrueInputAtoms()) <= 1:
        dlvhex.output(('bar', ))
    else:
        dlvhex.output(('foo', ))
Esempio n. 25
0
def testA(pred):
    if len(dlvhex.getTrueInputAtoms()) == 0:
        dlvhex.output(('foo', ))
    else:
        dlvhex.output(('bar', ))
Esempio n. 26
0
def id(p):
    for x in dlvhex.getTrueInputAtoms():
        dlvhex.output((x.tuple()[1], ))
Esempio n. 27
0
def id2(p,x):
	y=1
	for a in dlvhex.getTrueInputAtoms():
		y=y+1
Esempio n. 28
0
def id2(p, x):
    y = 1
    for a in dlvhex.getTrueInputAtoms():
        y = y + 1
Esempio n. 29
0
def id(p):
	for x in dlvhex.getTrueInputAtoms():
		dlvhex.output((x.tuple()[1], ))
Esempio n. 30
0
def cnt(p):
	c = 0
	for x in dlvhex.getTrueInputAtoms():
		c = c + 1
	dlvhex.output((c, ))