def wsConnect(new_url): # acthex.environment().connectWebsockets(new_url) new_url_s = str(new_url).replace('\"', '') #print("d1: " + new_url_s) if len(new_url_s) == 0 or new_url_s == acthex.environment().ws_url: return else: acthex.environment().ws_url = new_url_s print("Connecting to: " + new_url_s) ws_conn = websocket.WebSocketApp(new_url_s, on_message=on_message, on_error=on_error, on_close=on_close) ws_conn.on_open = on_open acthex.environment().ws_connection = ws_conn wst = threading.Thread(target=ws_conn.run_forever) wst.daemon = True wst.start()
def wsClose(url): if acthex.environment().ws_connection is None or acthex.environment( ).ws_connection.sock is None: return if acthex.environment().ws_connection.sock.connected: acthex.environment().ws_connection.close()
def persistenceByPred(pred): #logging.debug('persistenceByPred:'+repr(pred)) assert (isinstance(pred, acthex.ID)) assert (isinstance(acthex.environment(), PersistenceEnvironment)) predstr = pred.value() for a in acthex.environment().atoms: logging.debug("in environment: " + repr(a)) if a[0] == predstr: # output tuple with single term containing full atom term = '{}({})'.format(a[0], ','.join(a[1:])) logging.debug('persistenceByPred output:' + repr(term)) acthex.output((term, ))
def wsSend(*inputs): message = "" for txt in inputs: message = message + " " + str(txt).replace('\"', '') #message = " ".join(inputs) #for message in inputs: # ws.send(message) # time.sleep(1) #print("Will send: " + message) if acthex.environment().ws_connection is None or acthex.environment( ).ws_connection.sock is None: return if acthex.environment().ws_connection.sock.connected: acthex.environment().ws_connection.send(message)
def main(): code = 1 try: config = hexlite.Configuration() args = interpretArguments(sys.argv[1:], config) # import API here to fail early if clingo module does not exist app.importClingoAPI() # now set additional paths for plugin imports setPaths(hexlite.flatten(args.pluginpath)) plugins = loadPlugins(hexlite.flatten(args.plugin)) program = loadProgram(hexlite.flatten(args.acthexfiles)) pcontext = hexlite.ProgramContext() if config.stats: pcontext.stats = hexlite.Statistics() # (per default, a dummy that does nothing is used) with pcontext.stats.context('rewriting'): hexlite.rewriter.classifyEAtomsInstallRewritingHandlers(pcontext) pr = acthex.rewriter.ProgramRewriter(pcontext, program, plugins, config) rewritten, facts = pr.rewrite() stringifiedFacts = frozenset(ast.normalizeFacts(facts)) try: for iteration in itertools.count(): logging.info("acthex iteration %d", iteration) envstr = str(acthex.environment()) if len(envstr) > 0: sys.stdout.write("Environment: " + envstr + '\n') # evaluate program, collect answer set in callback acthexcallback = ActhexModelCallback(stringifiedFacts, config) callbacks = [acthexcallback] if len(dlvhex.modelCallbacks) > 0: # additional model callbacks callbacks += [ cb(stringifiedFacts, config) for cb in dlvhex.modelCallbacks ] solvecode = hexlite.clingobackend.execute( pcontext, rewritten, facts, plugins, config, callbacks) # find and execute actions on environment with pcontext.stats.context('executing actions'): acthex.actionmanager.executeActions( acthexcallback.optimal_model) except acthex.IterationExit: logging.info("got acthex iteration exit exception") # ignore code as dlvhex does code = 0 pcontext.stats.display('final') except SystemExit: pass except: logging.error('Exception: ' + traceback.format_exc()) logging.error( 'For reporting bugs, unexpected behavior, or suggestions, please report an issue here: ' + 'https://github.com/hexhex/hexlite/issues') sys.exit(code)
def persistenceUnset(atom): assert (isinstance(atom, acthex.ID)) assert (isinstance(acthex.environment(), PersistenceEnvironment)) acthex.environment().persistence_set_unset(atom, False)
def wsConnected(url): if acthex.environment().ws_connection is not None and acthex.environment( ).ws_connection.sock.connected: acthex.output(())
def sqlN(query, N): assert (isinstance(query, acthex.ID)) assert (isinstance(acthex.environment(), PSQLEnvironment)) acthex.environment().sql(query.value().strip('"'), N)
def sortVal(): assert(isinstance(acthex.environment(), PersistenceEnvironment)) for idx, val in enumerate(acthex.environment().sequence): acthex.output( (idx, val) )
def sortDisplay(): assert(isinstance(acthex.environment(), SortEnvironment)) acthex.environment().display()
def sortSwap(i, j): assert(isinstance(i, acthex.ID)) assert(isinstance(j, acthex.ID)) assert(isinstance(acthex.environment(), SortEnvironment)) acthex.environment().swap(i.intValue(), j.intValue())