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],))
def controlsMajorityWithMax(strategic,owns): controlDict = dict() unknownControlDict = dict() for x in dlvhex.getInputAtoms(): if x.tuple()[0] == strategic and x.isTrue(): for y in dlvhex.getInputAtoms(): if y.tuple()[0] == owns and x.tuple()[1] == y.tuple()[1]: if y.tuple()[2].value() in controlDict: newval = str(int(controlDict[y.tuple()[2].value()]) + int(y.tuple()[3].value()[1:])) controlDict[y.tuple()[2].value()] = newval else: controlDict[y.tuple()[2].value()] = y.tuple()[3].value()[1:] if y.tuple()[2].value() in unknownControlDict: newval = str(int(unknownControlDict[y.tuple()[2].value()]) + int(y.tuple()[3].value()[1:])) unknownControlDict[y.tuple()[2].value()] = newval else: unknownControlDict[y.tuple()[2].value()] = y.tuple()[3].value()[1:] elif x.tuple()[0] == strategic and not x.isFalse(): for y in dlvhex.getInputAtoms(): if y.tuple()[0] == owns and x.tuple()[1] == y.tuple()[1]: if y.tuple()[2].value() in unknownControlDict: newval = str(int(unknownControlDict[y.tuple()[2].value()]) + int(y.tuple()[3].value()[1:])) unknownControlDict[y.tuple()[2].value()] = newval else: unknownControlDict[y.tuple()[2].value()] = y.tuple()[3].value()[1:] for c in unknownControlDict: if c in controlDict and int(controlDict[c]) > 5000000 and int(unknownControlDict[c]) + 400 < 10000000: dlvhex.output((c, )) elif int(unknownControlDict[c]) > 5000000 and (c not in controlDict or int(controlDict[c]) + 400 < 10000000): dlvhex.outputUnknown((c, ))
def numberOfBalls(assignment, min, max): true = 0 false = 0 unknown = 0 premisse = () for x in dlvhex.getInputAtoms(): if x.isTrue(): true = true + 1 elif x.isFalse(): false = false + 1 else: unknown = unknown + 1 v = 0 if true >= min.intValue() and (true + unknown) <= max.intValue(): # external atom is true dlvhex.output(()) elif (true + unknown) >= min.intValue() and true <= max.intValue(): # external atom can be true dlvhex.outputUnknown(()) else: # else case applies: (true + unknown) < min.intValue() or true > max.intValue() # # external atom is certainly not true v = 0
def numberOfBallsGE(assignment, min): true = 0 false = 0 unknown = 0 premisse = () for x in dlvhex.getInputAtoms(): if x.isTrue(): true = true + 1 elif x.isFalse(): false = false + 1 else: unknown = unknown + 1 v = 0 if true >= min.intValue(): # external atom is true dlvhex.output(()) elif (true + unknown) >= min.intValue(): # external atom can be true dlvhex.outputUnknown(()) else: # else case applies: if (true + unknown) < min.intValue() # # external v = 0
def isEmpty(assignment): true = 0 false = 0 unknown = 0 premisse = () for x in dlvhex.getInputAtoms(): if x.isTrue(): true = true + 1 elif x.isFalse(): false = false + 1 else: unknown = unknown + 1 if true > 0: # external atom is true dlvhex.output(()) elif (true + unknown) > 0: # external atom can be true dlvhex.outputUnknown(()) else: # else case applies: (true + unknown) < min.intValue() or true > max.intValue() # # external atom is certainly not true v = 0
def adjacent(path,nd): edges = {} nodes = [] file = open(path.value()[1:len(path.value())-1]) for node in file: nodes.append(node) for i in range(0,len(nodes)/2): first = nodes.pop() second = nodes.pop() if first[:-1] not in edges: edges[first[:-1]] = [second[:-1]] else: edges[first[:-1]].append(second[:-1]) true_nodes = [] for x in dlvhex.getInputAtoms(): if x.tuple()[0] == nd and x.isTrue(): if x.tuple()[1].value() in edges: for nd2 in edges[x.tuple()[1].value()]: dlvhex.output( (nd2,) ) true_nodes.append(nd2) for x in dlvhex.getInputAtoms(): if x.tuple()[0] == nd and not x.isFalse() and not x.isTrue(): if x.tuple()[1].value() in edges: for nd2 in edges[x.tuple()[1].value()]: if nd2 not in true_nodes: dlvhex.outputUnknown( (nd2,) )
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(())
def isFunctionTerm(term): logging.debug('isFunctionTerm got ' + repr(term)) pinp = shp.parseTerm(term.value()) logging.debug('parseTerm {}'.format(repr(pinp))) if len(pinp) > 1: # yes it is dlvhex.output(())
def concat(strs): needquote = any(['"' in s for s in strs]) unquoted = [s.strip('"') for s in strs] result = ''.join(unquoted) if needquote: result = '"' + result + '"' dlvhex.output((result, ))
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 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 pyRank8(l, polarity): l = ast.literal_eval(l.value()[1:-1]) if type(l) != tuple or len(l) != 4: return (type_, id_, x, y) = l if (polarity == '1' and y == 8) or (polarity == '0' and y != 8): dlvhex.output(())
def functionDecompose(term, narg): logging.debug('functionDecompose got {} and {}'.format(repr(term), narg)) pinp = shp.parseTerm(term.value()) logging.debug('parseTerm {}'.format(repr(pinp))) argidx = narg.intValue() if argidx < len(pinp): dlvhex.output((shp.shallowprint(pinp[argidx]), ))
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 move_right(x): (pos, end, places) = parse(x) if pos < end: y = output(pos + 1, end, places) # print('move_right',x) # print('move_right',y) dlvhex.output((y, ))
def pyForward(l): l = ast.literal_eval(l.value()[1:-1]) if type(l) != tuple or len(l) != 4: return (type_, id_, x, y) = l if y < 8: dlvhex.output(('"' + str((type_, id_, x, y + 1)) + '"', ))
def adjacent(path, nd): edges = {} nodes = [] file = open(path.value()[1:len(path.value()) - 1]) for node in file: nodes.append(node) for i in range(0, len(nodes) / 2): first = nodes.pop() second = nodes.pop() if first[:-1] not in edges: edges[first[:-1]] = [second[:-1]] else: edges[first[:-1]].append(second[:-1]) true_nodes = [] for x in dlvhex.getInputAtoms(): if x.tuple()[0] == nd and x.isTrue(): if x.tuple()[1].value() in edges: for nd2 in edges[x.tuple()[1].value()]: dlvhex.output((nd2, )) true_nodes.append(nd2) for x in dlvhex.getInputAtoms(): if x.tuple()[0] == nd and not x.isFalse() and not x.isTrue(): if x.tuple()[1].value() in edges: for nd2 in edges[x.tuple()[1].value()]: if nd2 not in true_nodes: dlvhex.outputUnknown((nd2, ))
def subgraph(vertices, edge): trueVertices = [] unknownVertices = [] falseVertices = [] for x in dlvhex.getInputAtoms(): if x.tuple()[0] == vertices and x.isTrue(): trueVertices.append(x.tuple()[1].value()) elif x.tuple()[0] == vertices and not x.isFalse(): unknownVertices.append(x.tuple()[1].value()) else: falseVertices.append(x.tuple()[1].value()) for x in dlvhex.getInputAtoms(): if x.tuple()[0] == edge and x.isTrue(): if x.tuple()[1].value() in trueVertices and x.tuple()[2].value( ) in trueVertices: dlvhex.output((x.tuple()[1].value(), x.tuple()[2].value())) elif x.tuple()[1].value() not in falseVertices and x.tuple( )[2].value() not in falseVertices: dlvhex.outputUnknown( (x.tuple()[1].value(), x.tuple()[2].value())) elif x.tuple()[0] == edge and not x.isFalse(): if x.tuple()[1].value() in trueVertices and x.tuple()[2].value( ) in trueVertices: dlvhex.outputUnknown( (x.tuple()[1].value(), x.tuple()[2].value())) elif x.tuple()[1].value() not in falseVertices and x.tuple( )[2].value() not in falseVertices: dlvhex.outputUnknown( (x.tuple()[1].value(), x.tuple()[2].value()))
def preferences(selected, p): trueGroups = [] unknownGroups = [] falseGroups = [] for x in dlvhex.getInputAtoms(): if x.tuple()[0] == selected and x.isTrue(): trueGroups.append(x.tuple()[1].value()) elif x.tuple()[0] == selected and not x.isFalse(): unknownGroups.append(x.tuple()[1].value()) elif x.tuple()[0] == selected: falseGroups.append(x.tuple()[1].value()) for x in dlvhex.getInputAtoms(): if x.tuple()[0] == p and x.isTrue(): if x.tuple()[1].value() in trueGroups: dlvhex.output((x.tuple()[2].value(), x.tuple()[3].value())) elif x.tuple()[1].value() not in falseGroups: dlvhex.outputUnknown( (x.tuple()[2].value(), x.tuple()[3].value())) elif x.tuple()[0] == p and not x.isFalse(): if x.tuple()[1].value() in trueGroups: dlvhex.outputUnknown( (x.tuple()[2].value(), x.tuple()[3].value())) elif x.tuple()[1].value() not in falseGroups: dlvhex.outputUnknown( (x.tuple()[2].value(), x.tuple()[3].value()))
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 partialTest(assignment): true = 0 false = 0 unknown = 0 premisse = () for x in dlvhex.getInputAtoms(): if x.isTrue(): true = true + 1 # premisse = premisse + (x, ) # print "true input atom:", x.value() elif x.isFalse(): false = false + 1 # premisse = premisse + (x.negate(), ) # print "false input atom:", x.value() else: unknown = unknown + 1 # print "unknown input atom:", x.value() v = 0 if true > 1: # dlvhex.learn(premisse + (dlvhex.storeOutputAtom((), False).negate(), )) dlvhex.output(()) elif true + unknown > 1: dlvhex.outputUnknown(())
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], ))
def concat(strs): needquote = any(['"' in s.value() for s in strs]) unquoted = [s.value().strip('"') for s in strs] result = ''.join(unquoted) if needquote: result = '"' + result + '"' dlvhex.output((dlvhex.storeConstant(result), ))
def functionDecomposeN(inp, N): logging.debug('functionDecomposeN got {} and {}'.format(repr(inp), N)) pinp = shp.parseTerm(inp.value()) logging.debug('parseTerm {}'.format(repr(pinp))) if len(pinp) == N + 1: otuple = [shp.shallowprint(x) for x in pinp] dlvhex.output(tuple(otuple))
def edges(currentNode): # hardcoded source for graph g = nx.read_weighted_edgelist("example_4_pathfind.graph", nodetype=str,create_using=nx.DiGraph()) # output successors for node in g.successors(currentNode.value()): dlvhex.output( (node,) )
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 strategic(strategic, controlled_by): trueStrategic = [] falseStrategic = [] for x in dlvhex.getInputAtoms(): if x.tuple()[0] == strategic and x.isTrue(): trueStrategic.append(x.tuple()[1].value()) elif x.tuple()[0] == strategic and x.isFalse(): falseStrategic.append(x.tuple()[1].value()) for x in dlvhex.getInputAtoms(): if x.tuple()[0] == controlled_by and x.isTrue(): if x.tuple()[2].value() in trueStrategic and x.tuple()[3].value( ) in trueStrategic and x.tuple()[4].value( ) in trueStrategic and x.tuple()[5].value() in trueStrategic: dlvhex.output((x.tuple()[1].value(), )) elif x.tuple()[2].value() not in falseStrategic and x.tuple( )[3].value() not in falseStrategic and x.tuple()[4].value( ) not in falseStrategic and x.tuple()[5].value( ) not in falseStrategic: dlvhex.outputUnknown((x.tuple()[1].value(), )) elif x.tuple()[0] == controlled_by and not x.isFalse(): if x.tuple()[2].value() in trueStrategic and x.tuple()[3].value( ) in trueStrategic and x.tuple()[4].value( ) in trueStrategic and x.tuple()[5].value() in trueStrategic: dlvhex.outputUnknown((x.tuple()[1].value(), )) elif x.tuple()[2].value() not in falseStrategic and x.tuple( )[3].value() not in falseStrategic and x.tuple()[4].value( ) not in falseStrategic and x.tuple()[5].value( ) not in falseStrategic: dlvhex.outputUnknown((x.tuple()[1].value(), ))
def edges(fileName, currentNode): # Sources will be loaded from the file g = nx.read_weighted_edgelist(fileName.value().strip('"'), nodetype=str, create_using=nx.DiGraph()) # Output successor nodes of the current node including weight for node in g.successors(currentNode.value()): weight = g[currentNode.value()][node]["weight"] # produce one output tuple dlvhex.output((node, int(weight)))
def concat(tup): # start with empty string and sequentialy append all inputs ret = "" for x in tup: ret = ret + x.value() # output the final string dlvhex.output((ret, ))
def concat(tup): # start with empty string ret = "" for x in tup: # append all input constants in sequence ret = ret + x.value() # output the final string dlvhex.output( (ret,) )
def edges(currentNode): # hardcoded source for graph g = nx.read_weighted_edgelist("example_4_pathfind.graph", nodetype=str, create_using=nx.DiGraph()) # output successors for node in g.successors(currentNode.value()): dlvhex.output((node, ))
def pyPawn(l, polarity): l = ast.literal_eval(l.value()[1:-1]) if type(l) != tuple or len(l) != 4: return (type_, id_, x, y) = l if (polarity == '1' and type_ == 'p') or (polarity == '0' and type_ != 'p'): dlvhex.output(())
def concat(tup): # start with empty string and append all input constants in sequence ret = "" for x in tup: ret = ret + x.value() # output the final string dlvhex.output((ret, ))
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 functionCompose(args): logging.debug('functionCompose got ' + repr(args)) if len(args) == 1: dlvhex.output((args[0], )) else: apred = args[0].value() avalues = [a.value() for a in args[1:]] dlvhex.output(("{}({})".format(apred, ','.join(avalues)), ))
def pyHead(l): l = ast.literal_eval(l.value()[1:-1]) if type(l) != list or len(l) == 0: return hexval = str(l[0]) if type(l[0]) == str: hexval = "'" + hexval + "'" dlvhex.output(('"' + hexval + '"', )) return
def friendsOfDegree(personOfInterest): # graph of the friends will be loaded from the external file g = nx.read_weighted_edgelist("example_2_1.edgelist", nodetype=str,create_using=nx.DiGraph()) # Take successor nodes of the node of interest. friendList = g.successors(personOfInterest.value()) # Output the successor nodes for node in friendList: dlvhex.output((node,)) # outputs direct fiends
def get_max_pollution(routeID): if routes is not None : if routeID.intValue() <= len(routes) : dlvhex.output((max(getPollutionValues(routes[routeID.intValue()-1].points)),)) else : raise ValueError('Wrong routeID in get_max_pollution') else : raise ValueError('get_max_pollution is called before routes is initialized')
def edges(currentNode): # Sources will be loaded from the file g = nx.read_weighted_edgelist("example_4_3.edgelist", nodetype=str,create_using=nx.DiGraph()) # Take successor nodes of the current node friendList = g.successors(currentNode.value()) # Output the successors for node in friendList: dlvhex.output((node,)) #sends city and weight to the output dlvhex.output((currentNode.value(),)) #sends city and weight to the output
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 needFuel(visit, edge): d = 0 for x in dlvhex.getInputAtoms(): if x.isTrue() and x.tuple()[0].value() == visit.value(): cur = x.tuple()[1] prev = getPredec(x.tuple()[2].intValue(), visit) if prev != 0: d = d + getCost(prev, cur, edge) if d > 10: dlvhex.output(())
def someSelectedPartial(selected): unknown = False for x in dlvhex.getInputAtoms(): if x.tuple()[0] == selected and x.isTrue(): dlvhex.output(()) return elif not x.isFalse(): unknown = True if unknown: dlvhex.outputUnknown(())
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 get_routes_data(starting_point, ending_point, cost_mode, number_of_routes): initRoutes(starting_point.value(), ending_point.value(), cost_mode.value(), number_of_routes.intValue()) if routes is not None : routeNumber = 1 for route in routes : # print((routeNumber, route.time, route.distance)) dlvhex.output((routeNumber, route.time, route.distance)) routeNumber +=1
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)
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(())
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 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 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(())
def cost(query): sorted = [] for stop in dlvhex.getInputAtoms(): if (stop.isTrue()): index = stop.tuple()[1].intValue() while(len(sorted) <= index): sorted.append(0) sorted[index] = stop t = () for stop in sorted: t = t + ( (stop.tuple()[2].value(), stop.tuple()[3].value(), datetime(stop.tuple()[4].intValue(), stop.tuple()[5].intValue(), stop.tuple()[6].intValue())), ) price = getPrice(t) if price == -1: dlvhex.output(("error", )) else: dlvhex.output((price, ))
def contains(pred,elem): # if False: # dlvhex.output(()) outputFalse = False outputTrue = False for x in dlvhex.getInputAtoms(): if x.tuple()[0] == pred and x.tuple()[1].value() == elem.value() and x.isTrue(): print("true") outputTrue = True dlvhex.output(()) elif x.tuple()[0] == pred and x.tuple()[1].value() == elem.value() and x.isFalse(): print("false") outputFalse = True if not outputFalse and not outputTrue: print("unknown") dlvhex.outputUnknown(())
def generalizedSubsetSum(x,y,b): true = 0 false = 0 unknown = 0 for x in dlvhex.getInputAtoms(): if x.isTrue(): true = true + int(x.tuple()[2].value()) elif x.isFalse(): false = false + int(x.tuple()[2].value()) else: unknown = unknown + int(x.tuple()[2].value()) if true > b.intValue() or true + unknown < b.intValue(): dlvhex.output(()) elif true != b.intValue() or unknown != 0: dlvhex.outputUnknown(())