private static void Main(string[] args) { string location = AppDomain.CurrentDomain.BaseDirectory; Console.WriteLine("Starting in " + location); //// Set up the static Logger from XML ... string loggingFileLocation = Path.Combine(location, "log4net.config"); FileInfo loggingFileInfo = new FileInfo(loggingFileLocation); if (!loggingFileInfo.Exists) { throw new ApplicationException("Cannot start without our log4net file"); } log4net.Config.XmlConfigurator.ConfigureAndWatch(loggingFileInfo); var log = LogManager.GetLogger("Global"); log.Info(" *** STARTING ***\r\n\r\n"); log.Info(" Please wait while the dictionaries and rules are loaded."); log.Info(" Since WordNet is included in this demo that may take up to a minute."); log.Info(""); // ---------------------- // The Wordnet add-on now builds its dictionary in RAM on load and contains interface definitions for // all possible Synsets in Wordnet. // The Abodit NLP Engine can use any dependency injection container but for the moment // the only adapter implemented is for Autofac. I can provide details on how to implement // IDependencyScope and IDependencyResolver if you need to use a different container. // // You should create your container with the registrations needed by your rule classes, // token factories and lexeme generators. // // Although not required (you can inject any classes you like) it's typical to inject // two objects into your rules. Here we inject just one (a collector) that contains // the other one (a remembered history). Interfaces are provided for these two common // objects. // A history object allowed rules to look back on a conversation to find things of a given // type (interface) in order to respond to apply responses to questions, clatifications, ... var rh = new RememberedHistory(); // A collector object can be either synchronous or asynchronous. A synchronous collector // collects all the responses from the call to nlp.Execute and then returns them, you might // use this approach for an email-based collector. An asynchronous collector provides a way // for rules to continue executing even after the call to nlp.Execute() has completed, you // should use this approach for any chat-like connections. Rules can thus fire off tasks that // keep running and provide messages back to the user about progress on a long task, or updates // when something changes. var collector = new ConsoleCollector(rh); // Create the Dependency Injection container and get an instance of the NLP engine var nlp = Initialization.NLPInstance; collector.Say("Starting... please wait..."); // Use the async call so loading tokens and rules happens in the background // and the UI isn't blocked. // Create a logger for NLP (calls through to log4net in this case) var logger = new LogFacade(); nlp.InitializeAsync(logger).ContinueWith((t) => { collector.Say("OK, I'm ready now"); collector.Say("Hello"); Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("> "); Console.ResetColor(); }); // You can pass additional state objects into the Execute method and they will find // their way into your rule classes by way of constructor injection or you can // just rely on your dependency injection framework to supply them. // // Using an object like this passed into the Execute method is useful when you have // dependencies that vary on a per-user or a per-request basis, e.g. an IUser object // var extraState = new ExtraStateInformation(); // Turn off all debugging NLP.Debugging = false; var conversationSequence = extraState.ConversationSequence().GetEnumerator(); while (extraState.Running) { // Rather simplistic approach to telling you stuff one line at a time string line = Console.ReadLine(); bool success = nlp.Execute(line, collector, extraState); if (!success) { collector.Say("Sorry, didn't understand that"); // Implement a very simple state machine to push the conversation along // each time the user doesn't enter a valid sentence if (conversationSequence.MoveNext()) { Console.WriteLine(); Console.WriteLine(conversationSequence.Current); } } else { Console.WriteLine(); } Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("> "); Console.ResetColor(); } Thread.Sleep(4000); }
public static int Main(string[] args) { var trace = false; string output_filename = null; var show_help = false; var use_precompiled = true; var options = new OptionSet { { "o=|output", "Write output to file instead of standard output.", v => output_filename = v }, { "t|trace-parsing", "Produce a trace of the parse process.", v => trace = v != null }, { "p|no-precomp", "do not use precompiled libraries", v => use_precompiled = v == null }, { "h|help", "show this message and exit", v => show_help = v != null } }; List <string> files; try { files = options.Parse(args); } catch (OptionException e) { Console.Error.Write(AppDomain.CurrentDomain.FriendlyName + ": "); Console.Error.WriteLine(e.Message); Console.Error.WriteLine("Try “" + AppDomain.CurrentDomain.FriendlyName + " --help” for more information."); return(1); } if (show_help) { Console.WriteLine("Usage: " + AppDomain.CurrentDomain.FriendlyName + " input.o_0"); Console.WriteLine("Run a Flabbergast file and display the “value” attribute."); Console.WriteLine(); Console.WriteLine("Options:"); options.WriteOptionDescriptions(Console.Out); return(1); } if (files.Count != 1) { Console.Error.WriteLine("Exactly one Flabbergast script must be given."); return(1); } var assembly_builder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("Print"), AssemblyBuilderAccess.Run); CompilationUnit.MakeDebuggable(assembly_builder); var module_builder = assembly_builder.DefineDynamicModule("PrintModule", true); var unit = new CompilationUnit(module_builder, true); var collector = new ConsoleCollector(); var task_master = new ConsoleTaskMaster(); var resource_finder = new ResourcePathFinder(); resource_finder.PrependPath(Path.Combine(Path.GetDirectoryName(Path.GetFullPath(files[0])), "lib")); resource_finder.AddDefault(); task_master.AddUriHandler(new CurrentInformation(false)); task_master.AddUriHandler(BuiltInLibraries.INSTANCE); task_master.AddUriHandler(SettingsHandler.INSTANCE); var db_handler = new DbUriHandler(); db_handler.Finder = resource_finder; task_master.AddUriHandler(db_handler); task_master.AddUriHandler(EnvironmentUriHandler.INSTANCE); task_master.AddUriHandler(HttpHandler.INSTANCE); task_master.AddUriHandler(FtpHandler.INSTANCE); task_master.AddUriHandler(FileHandler.INSTANCE); var resource_handler = new ResourceHandler(); resource_handler.Finder = resource_finder; task_master.AddUriHandler(resource_handler); if (use_precompiled) { var precomp = new LoadPrecompiledLibraries(); precomp.Finder = resource_finder; task_master.AddUriHandler(precomp); } var dyncomp = new DynamicallyCompiledLibraries(collector); dyncomp.Finder = resource_finder; task_master.AddUriHandler(dyncomp); var parser = Parser.Open(files[0]); parser.Trace = trace; var run_type = parser.ParseFile(collector, unit, "Printer"); if (run_type != null) { var computation = (Computation)Activator.CreateInstance(run_type, task_master); var filewriter = new PrintResult(task_master, computation, output_filename); filewriter.Slot(); task_master.Run(); task_master.ReportCircularEvaluation(); return(filewriter.Success ? 0 : 1); } return(1); }