#!/usr/bin/env python import sys import glob import os import datetime from the_semantic_db_code import * from the_semantic_db_functions import * from the_semantic_db_processor import * # starting .sw directory: sw_file_dir = "sw-examples" # check it exists, if not create it: if not os.path.exists(sw_file_dir): print("Creating " + sw_file_dir + " directory.") os.makedirs(sw_file_dir) print("Welcome!") C = context_list("sw console") old_help_string = """ q, quit, exit : quit the agent. h, help : print this message context : print list of context's context string : set current context to string dump : print current context dump multi : print context list dump self : print what we know about the default ket/sp dump ket/sp : print what we know about the given ket/sp load file.sw : load file.sw save file.sw : save current context to file.sw save multi file.sw : save context list to file.sw files : show the available .sw files create inverse : create inverse for current context create multi inverse : create inverse for all context in context list x = foo: bah : set x (the default ket) to |foo: bah> id : display the default ket/superposition s, store : set x to the result of the last computation . : repeat last computation if none of the above : process_input_line(C,line,x) """ help_string = """ q, quit, exit quit the agent. h, help print this message context print list of context's context string set current context to string reset reset back to completely empty console Warning! you will lose all unsaved work! dump print current context dump exact print current context in exact mode dump multi print context list dump self print what we know about the default ket/sp dump ket/sp print what we know about the given ket/sp display (relatively) readable display of current context display ket/sp (relatively) readable display about what we know for the ket/sp freq convert current context to frequency list mfreq convert context list to frequency list load file.sw load file.sw save file.sw save current context to file.sw save multi file.sw save context list to file.sw files show the available .sw files cd change and create if necessary the .sw directory ls, dir, dirs show the available directories create inverse create inverse for current context create multi inverse create inverse for all context in context list x = foo: bah set x (the default ket) to |foo: bah> id display the default ket/superposition s, store set x to the result of the last computation . repeat last computation i interactive history history show last 30 commands history n show last n commands save history save console history to file -- comment ignore, this is just a comment line. if none of the above process_input_line(C,line,x) """ x = ket("",0) stored_line = "" command_history = [] command_history_file = "sa-console-command-history.txt" # file where we save the command history. Might be interesting. # save history function: def save_history(history,history_file): print("saving history ... ") try: f = open(history_file,'a') today = str(datetime.date.today()) f.write(today + "\n") for line in history: f.write(" " + line + "\n") f.write("\n") f.close() print("Done.") except: print("failed!") # the interactive semantic agent: while True: line = input("\nsa: ") if line == "i": n = 30 if len(command_history) > 0: count = min(len(command_history),n) history = command_history[-count:] for k,line in enumerate(history): print(" " + str(k) + ") " + line) selection = input("\nEnter your selection: ") try: selection = int(selection) line = history[selection] print("Your selection:",line,"\n") except: continue else: print("history is empty") continue command_history.append(line) # exit the agent: if line in ['q','quit','exit']: # save history before we go: save_history(command_history,command_history_file) print("\nBye!") break if line in ['h','help']: print(help_string) elif line == "context": print(C.show_context_list()) # switch context: elif line.startswith("context "): name = line[8:] C.set(name) print(C.dump_universe()) elif line == "reset": check = input("\n Warning! This will erase all unsaved work! Are you sure? (y/n): ") if len(check) > 0 and check[0] == 'y': C = context_list("sw console") print("\n Gone ... ") elif line == "dump": print(C.dump_universe()) elif line == "dump exact": print(C.dump_universe(True)) elif line == "dump multi": print(C.dump_multiverse()) elif line == "dump self": print(C.dump_sp_rules(x)) elif line.startswith("dump "): var = line[5:] print("var:",var,"\n") try: sp = extract_compound_superposition(C,var)[0] print(C.dump_sp_rules(sp)) except: continue elif line == "display": print(C.display_all()) elif line.startswith("display "): var = line[8:] print("var:",var,"\n") try: sp = extract_compound_superposition(C,var)[0] print(C.display_sp(sp)) except: continue elif line == "freq": result = C.to_freq_list() print(result) elif line == "mfreq": print(C.multiverse_to_freq_list()) elif line.startswith("load "): name = line[5:] name = sw_file_dir + "/" + name # load and save files to the sw_file_dir. print("loading sw file:",name) load_sw(C,name) elif line == "save history": # save history: save_history(command_history,command_history_file) elif line.startswith("save multi "): name = line[11:] name = sw_file_dir + "/" + name # load and save files to the sw_file_dir. print("saving context list to:",name) save_sw_multi(C,name) elif line.startswith("save "): name = line[5:] name = sw_file_dir + "/" + name # load and save files to the sw_file_dir. print("saving current context to:",name) save_sw(C,name) elif line == "files": sep = " " max_len = 0 data = [] for file in glob.glob(sw_file_dir + "/*.sw"): base = os.path.basename(file) max_len = max(max_len,len(base)) data.append([base,extract_sw_stats(file)]) print() for file,stats in data: print(" " + file.ljust(max_len) + sep + stats) elif line.startswith("cd "): sw_file_dir = line[3:] # check it exists, if not create it: if not os.path.exists(sw_file_dir): print("Creating " + sw_file_dir + " directory.") os.makedirs(sw_file_dir) elif line in ['ls','dir','dirs']: print("directory list:") for dir in [d for d in os.listdir('.') if os.path.isdir(d) and not d.startswith("__")]: prefix = " " if dir == sw_file_dir: prefix = "* " sw_count = len(glob.glob(dir + "/*.sw")) print(prefix + dir + " (" + str(sw_count) + ")") elif line == "create inverse": C.create_universe_inverse() elif line == "create multi inverse": C.create_multiverse_inverse() elif line.startswith("x = "): var = line[4:] try: x = extract_compound_superposition(C,var)[0] except: x = ket(var) elif line == "id": print(x) elif line in ['s','store']: # set x to the result of the last computation. x = result print("stored:",x) elif line.startswith("--"): continue elif line.startswith("history"): try: n = int(line[8:]) except: n = 30 if len(command_history) > 0: count = min(len(command_history),n) for line in command_history[-count:]: print(" " + line) else: if line == ".": line = stored_line stored_line = line result = process_input_line(C,line,x) # maybe handling of comment lines should be in here? print(result)