public override bool Evaluate()
        {
            if (context.Contents.Count == 0)
                return succ.Continue(context, fail);

            ContinuationAppender appender = new ContinuationAppender(context, succ);
            EvaluateAll evalall = new EvaluateAll(salience, argumentMode, appender.AsIndex(1), isUserInput);
            Evaluator eval = new Evaluator(salience, argumentMode, appender.AsIndex(0), evalall, isUserInput);

            return eval.Continue(context, fail);
        }
Beispiel #2
0
        public override bool Evaluate()
        {
            if (context.Contents.Count == 0)
            {
                return(succ.Continue(context, fail));
            }

            ContinuationAppender appender = new ContinuationAppender(context, succ);
            EvaluateAll          evalall  = new EvaluateAll(salience, argumentMode, appender.AsIndex(1), isUserInput);
            Evaluator            eval     = new Evaluator(salience, argumentMode, appender.AsIndex(0), evalall, isUserInput);

            return(eval.Continue(context, fail));
        }
Beispiel #3
0
        public static void Main(string[] args)
        {
            int c;
            MainClass main = new MainClass();

            LongOpt[] longopts = new LongOpt[] {
                new LongOpt("help", Argument.No, null, 'h'),
                new LongOpt("verbose", Argument.No, null, 'v'),
                new LongOpt("conf", Argument.Required, null, 'c'),
                new LongOpt("tag", Argument.No, null, 2),
                new LongOpt("parse", Argument.No, null, 3),
                new LongOpt("knows", Argument.No, null, 4),
                new LongOpt("spell", Argument.No, null, 'z')
            };
            Getopt g = new Getopt("DataTemple", args, "hvszc:I:P:O:T:i:p:t:", longopts);

            bool acted = false;
            List<PatternTemplateSource> dicta = new List<PatternTemplateSource>();

            string input = null;
            string output = null;
            string template = null;
            while ((c = g.getopt()) != -1)
                switch (c) {
                case 1: {
                    Console.WriteLine("I see you have return in order set and that " +
                        "a non-option argv element was just found " +
                        "with the value '" + g.Optarg + "'");
                    break;
                }
                case 2: {
                    acted = true;
                    if (main.tagger == null) {
                        Console.WriteLine("Use the -c option before --tag or --parse");
                        continue;
                    }
                    List<KeyValuePair<string, string>> tokens = main.tagger.TagString(input);
                    foreach (KeyValuePair<string, string> token in tokens)
                        Console.Write(token.Key + "/" + token.Value + " ");
                    Console.WriteLine("");
                    break;
                }
                case 3: {
                    acted = true;
                    if (main.parser == null) {
                        Console.WriteLine("Use the -c option before --tag or --parse");
                        continue;
                    }
                    Console.WriteLine(main.parser.Parse(input));
                    break;
                }
                case 4: {
                    DictumMaker maker = new DictumMaker(main.basectx, "testing");
                    dicta.Add(maker.MakeDictum("%sentence %noun %is %adj", "@know %noun @HasProperty %adj @SubjectTense %is"));
                    dicta.Add(maker.MakeDictum("%sentence %event %attime", "@know %event @AtTime %attime"));
                    dicta.Add(maker.MakeDictum("%sentence %event %inall", "@know %event @InLocation %inall"));
                    dicta.Add(maker.MakeDictum("%sentence %noun %inall", "@know %noun @InLocation %inall"));
                    dicta.Add(maker.MakeDictum("%sentence %noun %is a %noun", "@know %noun1 @IsA %noun2 @SubjectTense %is"));
                    dicta.Add(maker.MakeDictum("%sentence %noun %will %verb1 * to %verb2 *", "@know %verbx1 @Subject %noun @SubjectTense %will %verb1 @ActiveObjects *1 @know %verbx2 @Subject %noun @SubjectTense %verb2 @ActiveObjects *2 @know %verbx2 @Condition %verbx1"));
                    break;
                }
                case 'v': {
                    main.verbose++;
                    break;
                }
                case 'h': {
                    Console.WriteLine("The documentation is currently at \n" + DOCS_URL);
                    break;
                }
                case 's': {
                    main.serialmode = true;
                    break;
                }
                case 'z': {
                    if (!main.initialized) {
                        Console.WriteLine("Use the -c option before -z");
                        continue;
                    }

                    SpellingBeeWordComparer wordComparer = new SpellingBeeWordComparer(main.plugenv.GetConfigDirectory("datadirectory"));
                    main.basectx.Map["$Compare"] = wordComparer;
                    main.tryToRescueMatch = new CorrectSpellingsRescueMatch(main.tryToRescueMatch, wordComparer, main.parser, 100);
                    break;
                }
                case 'c': {
                    main.Initialize(g.Optarg);
                    break;
                }
                case 'I': {
                    input = g.Optarg;
                    break;
                }
                case 'i': {
                    StreamReader file = new StreamReader(g.Optarg);
                    input = file.ReadToEnd();
                    break;
                }
                case 'P': {
                    if (!main.initialized) {
                        Console.WriteLine("Use the -c option before -P");
                        continue;
                    }
                    Context context = Interpreter.ParseCommands(main.basectx, g.Optarg);
                    IContinuation cont = new Evaluator(100.0, ArgumentMode.ManyArguments, main, new NopCallable(), true);
                    cont.Continue(context, new NopCallable());

                    main.RunToEnd();
                    break;
                }
                case 'p': {
                    if (!main.initialized) {
                        Console.WriteLine("Use the -c option before -p");
                        continue;
                    }
                    foreach (string line in File.ReadAllLines(g.Optarg)) {
                        if (line.Trim().Length == 0 || line.Trim().StartsWith("#"))
                            continue;
                        Context context = Interpreter.ParseCommands(main.basectx, line);
                        IContinuation cont = new Evaluator(100.0, ArgumentMode.ManyArguments, main, new NopCallable(), true);
                        cont.Continue(context, new NopCallable());

                        main.RunToEnd();
                    }
                    break;
                }
                case 'T': {
                    if (!main.initialized) {
                        Console.WriteLine("Use the -c option before -T");
                        continue;
                    }
                    template = g.Optarg;
                    if (template != null && output != null) {
                        DictumMaker maker = new DictumMaker(main.basectx, "testing");
                        dicta.Add(maker.MakeDictum(template, output));

                        template = output = null;
                    }
                    break;
                }
                case 'O': {
                    if (!main.initialized) {
                        Console.WriteLine("Use the -c option before -O");
                        continue;
                    }

                    output = g.Optarg;
                    if (template != null && output != null) {
                        DictumMaker maker = new DictumMaker(main.basectx, "testing");
                        dicta.Add(maker.MakeDictum(template, output));

                        template = output = null;
                    }
                    break;
                }
                case 't': {
                    if (!main.initialized) {
                        Console.WriteLine("Use the -c option before -t");
                        continue;
                    }

                    bool nextTemplate = true;
                    foreach (string line in File.ReadAllLines(g.Optarg)) {
                        string trimline = line.Trim();
                        if (trimline.Length == 0 || trimline.StartsWith("#"))
                            continue;

                        if (nextTemplate) {
                            template = trimline;
                            nextTemplate = false;
                        } else {
                            output = trimline;
                            DictumMaker maker = new DictumMaker(main.basectx, "testing");
                            dicta.Add(maker.MakeDictum(template, output));
                            nextTemplate = true;
                        }
                    }

                    template = output = null;
                    break;
                }
            }

            if (dicta.Count != 0)
                main.DoMatching(dicta, input);
            else if (!acted)
                Console.WriteLine("Nothing to do.  Add -tag, -parse, or -t or -T and -O");
        }
        public static bool EvaluateDefinition(Context context, IContinuation succ, IFailure fail, params object[] args)
        {
            double salience = (double) args[0];
            Context definition = (Context)args[1];

            Evaluator eval = new Evaluator(salience, ArgumentMode.ManyArguments, succ, new NopCallable(), true);
            eval.Continue(definition, fail);

            return true;
        }
        public override bool Evaluate()
        {
            List<IContent> contents = context.Contents;

            if (contents.Count == 0) {
                // nothing to do!
                succ.Continue(context, fail);
                aftersucc.Continue(new Context(context, null), fail);
                return true;
            }

            // Look ahead until first recursion
            int ii;
            for (ii = 0; ii < contents.Count; ii++)
                if ((contents[ii] is Special) && !(contents[ii].Name.StartsWith("*") || contents[ii].Name.StartsWith("_")))
                    break;

            if (ii > 0 && argumentMode == ArgumentMode.SingleArgument) {
                succ.Continue(new Context(context, contents.GetRange(0, 1)), fail);
                aftersucc.Continue(context.ChildRange(1), fail);
                return true;
            }

            if (ii == contents.Count)
            {
                // everything was done
                succ.Continue(context, fail);
                aftersucc.Continue(new Context(context, null), fail);
                return true;
            }

            if (contents[ii] == Special.EndDelimSpecial)
            {
                // everything to the # is done
                Context value = new Context(context, contents.GetRange(0, ii));
                Context after = new Context(context, contents.GetRange(ii + 1, contents.Count - ii - 1));
                succ.Continue(value, fail);
                aftersucc.Continue(after, fail);
                return true;
            }

            IContent content = contents[ii];

            object element = null;
            try {
                element = context.Lookup(content.Name);
            } catch (Exception ex) {
                if (isUserInput)
                    throw new UserException("The variable '" + content.Name + "' is unknown", ex);
                else
                    throw ex;
            }
            if (element is CallAgent)
            {
                IContinuation mysucc = succ;
                if (ii > 0)
                    mysucc = new ContextAppender(salience, context, ii, succ);
                List<IContent> sublst = context.Contents.GetRange(ii + 1, context.Contents.Count - ii - 1);

                if (((CallAgent)element).ArgumentOptions == ArgumentMode.NoArugments)
                {
                    if (argumentMode == ArgumentMode.SingleArgument)
                    {
                        ContinueToCallAgent.Instantiate((CallAgent)element, new Context(context, null), mysucc, fail);
                        aftersucc.Continue(new Context(context, sublst), fail);
                        return true;
                    }
                    else
                    {
                        ContinuationAppender evalappend = new ContinuationAppender(context, mysucc);

                        ContinueToCallAgent.Instantiate((CallAgent)element, new Context(context, null), evalappend.AsIndex(0), fail);

                        Evaluator eval = new Evaluator(salience, ArgumentMode.ManyArguments, evalappend.AsIndex(1), aftersucc, isUserInput);
                        eval.Continue(new Context(context, sublst), fail);

                        return true;
                    }
                }
                else if (((CallAgent)element).ArgumentOptions == ArgumentMode.RemainderUnevaluated)
                {
                    ContinueToCallAgent.Instantiate((CallAgent) element, new Context(context, sublst), mysucc, fail);
                    aftersucc.Continue(new Context(context, new List<IContent>()), fail);
                    return true;
                } else if (((CallAgent)element).ArgumentOptions == ArgumentMode.DelimitedUnevaluated) {
                    int jj;
                    for (jj = 0; jj < contents.Count; jj++)
                        if (contents[jj] == Special.EndDelimSpecial)
                            break;

                    List<IContent> before = context.Contents.GetRange(ii + 1, jj - 1);
                    List<IContent> after;
                    if (jj < context.Contents.Count - 1)
                        after = context.Contents.GetRange(jj + 1, context.Contents.Count - jj - 1);
                    else
                        after = new List<IContent>();

                    ContinuationAppender evalappend = new ContinuationAppender(context, mysucc);

                    ContinueToCallAgent.Instantiate((CallAgent) element, new Context(context, before), evalappend.AsIndex(0), fail);
                    Evaluator aftereval = new Evaluator(salience, ArgumentMode.ManyArguments, evalappend.AsIndex(1), aftersucc, isUserInput);
                    aftereval.Continue(new Context(context, after), fail);
                    return true;
                } else {
                    if (argumentMode == ArgumentMode.SingleArgument)
                    {
                        ContinueToCallAgent callagent = new ContinueToCallAgent((CallAgent)element, mysucc);
                        Evaluator eval = new Evaluator(salience, ((CallAgent) element).ArgumentOptions, callagent, aftersucc, isUserInput);
                        eval.Continue(new Context(context, sublst), fail);
                        return true;
                    }
                    else
                    {
                        ContinuationAppender evalappend = new ContinuationAppender(context, mysucc);

                        ContinueToCallAgent callagent = new ContinueToCallAgent((CallAgent)element, evalappend.AsIndex(0));

                        Evaluator aftereval = new Evaluator(salience, ArgumentMode.ManyArguments, evalappend.AsIndex(1), aftersucc, isUserInput);

                        Evaluator eval = new Evaluator(salience, ((CallAgent) element).ArgumentOptions, callagent, aftereval, isUserInput);
                        eval.Continue(new Context(context, sublst), fail);
                        return true;
                    }
                }
            } else if (element is Codelet) {
                IContinuation mysucc = succ;
                if (ii > 0)
                    mysucc = new ContextAppender(salience, context, ii, succ);
                List<IContent> sublst = context.Contents.GetRange(ii + 1, context.Contents.Count - ii - 1);

                coderack.AddCodelet((Codelet) element, "Codelet in content");

                Evaluator eval = new Evaluator(salience, argumentMode, mysucc, aftersucc, isUserInput);
                eval.Continue(new Context(context, sublst), fail);
                return true;
            } else {
                Context result = new Context(context, new List<IContent>(context.Contents.GetRange(0, ii)));
                if (element is IContent)
                    result.Contents.Add((IContent) element);
                else if (element is List<IContent>)
                    result.Contents.AddRange((List<IContent>) element);
                else
                    result.Contents.Add(new Value(element));

                if (argumentMode == ArgumentMode.SingleArgument)
                {
                    succ.Continue(result, fail);
                    aftersucc.Continue(context.ChildRange(1), fail);
                    return true;
                }
                else
                {
                    IContinuation appender = new ContextAppender(salience, result, result.Contents.Count, succ);
                    List<IContent> sublst = context.Contents.GetRange(ii + 1, context.Contents.Count - ii - 1);

                    Evaluator eval = new Evaluator(salience, argumentMode, appender, aftersucc, isUserInput);
                    eval.Continue(new Context(context, sublst), fail);
                    return true;
                }
            }
        }
        public override bool Produce(Context context, IContinuation succ, IFailure fail)
        {
            // Evaluate all children
            ContinueToCallAgent cont = CallAgentWrapper.MakeContinuation(ConstructSentence, succ, 100.0, 10, 10);

            ContinuationAppender appender = new ContinuationAppender(context, cont);

            Evaluator eval = new Evaluator(salience, ArgumentMode.ManyArguments, appender.AsIndex(0), appender.AsIndex(1), true);

            eval.Continue(context, fail);

            return true;
        }
 public bool Continue(object value, IFailure fail)
 {
     Context context = (Context) value;
     // Did we successfully match everything?  If so, evaluate the template
     if (Matcher.IsRemainderOptional(context.Contents))
     {
         Evaluator eval = new Evaluator(salience, ArgumentMode.ManyArguments, succ, new NopCallable(), true);
         Context child = new Context(context, template.Contents);
         child.Map["$production"] = true;
         return eval.Continue(child, fail);
     }
     else
     {
         // we didn't match everything
         fail.Fail("Context is not empty", succ);
         return true;
     }
 }
        public override bool Evaluate()
        {
            List <IContent> contents = context.Contents;

            if (contents.Count == 0)
            {
                // nothing to do!
                succ.Continue(context, fail);
                aftersucc.Continue(new Context(context, null), fail);
                return(true);
            }

            // Look ahead until first recursion
            int ii;

            for (ii = 0; ii < contents.Count; ii++)
            {
                if ((contents[ii] is Special) && !(contents[ii].Name.StartsWith("*") || contents[ii].Name.StartsWith("_")))
                {
                    break;
                }
            }

            if (ii > 0 && argumentMode == ArgumentMode.SingleArgument)
            {
                succ.Continue(new Context(context, contents.GetRange(0, 1)), fail);
                aftersucc.Continue(context.ChildRange(1), fail);
                return(true);
            }

            if (ii == contents.Count)
            {
                // everything was done
                succ.Continue(context, fail);
                aftersucc.Continue(new Context(context, null), fail);
                return(true);
            }

            if (contents[ii] == Special.EndDelimSpecial)
            {
                // everything to the # is done
                Context value = new Context(context, contents.GetRange(0, ii));
                Context after = new Context(context, contents.GetRange(ii + 1, contents.Count - ii - 1));
                succ.Continue(value, fail);
                aftersucc.Continue(after, fail);
                return(true);
            }

            IContent content = contents[ii];

            object element = null;

            try {
                element = context.Lookup(content.Name);
            } catch (Exception ex) {
                if (isUserInput)
                {
                    throw new UserException("The variable '" + content.Name + "' is unknown", ex);
                }
                else
                {
                    throw ex;
                }
            }
            if (element is CallAgent)
            {
                IContinuation mysucc = succ;
                if (ii > 0)
                {
                    mysucc = new ContextAppender(salience, context, ii, succ);
                }
                List <IContent> sublst = context.Contents.GetRange(ii + 1, context.Contents.Count - ii - 1);

                if (((CallAgent)element).ArgumentOptions == ArgumentMode.NoArugments)
                {
                    if (argumentMode == ArgumentMode.SingleArgument)
                    {
                        ContinueToCallAgent.Instantiate((CallAgent)element, new Context(context, null), mysucc, fail);
                        aftersucc.Continue(new Context(context, sublst), fail);
                        return(true);
                    }
                    else
                    {
                        ContinuationAppender evalappend = new ContinuationAppender(context, mysucc);

                        ContinueToCallAgent.Instantiate((CallAgent)element, new Context(context, null), evalappend.AsIndex(0), fail);

                        Evaluator eval = new Evaluator(salience, ArgumentMode.ManyArguments, evalappend.AsIndex(1), aftersucc, isUserInput);
                        eval.Continue(new Context(context, sublst), fail);

                        return(true);
                    }
                }
                else if (((CallAgent)element).ArgumentOptions == ArgumentMode.RemainderUnevaluated)
                {
                    ContinueToCallAgent.Instantiate((CallAgent)element, new Context(context, sublst), mysucc, fail);
                    aftersucc.Continue(new Context(context, new List <IContent>()), fail);
                    return(true);
                }
                else if (((CallAgent)element).ArgumentOptions == ArgumentMode.DelimitedUnevaluated)
                {
                    int jj;
                    for (jj = 0; jj < contents.Count; jj++)
                    {
                        if (contents[jj] == Special.EndDelimSpecial)
                        {
                            break;
                        }
                    }

                    List <IContent> before = context.Contents.GetRange(ii + 1, jj - 1);
                    List <IContent> after;
                    if (jj < context.Contents.Count - 1)
                    {
                        after = context.Contents.GetRange(jj + 1, context.Contents.Count - jj - 1);
                    }
                    else
                    {
                        after = new List <IContent>();
                    }

                    ContinuationAppender evalappend = new ContinuationAppender(context, mysucc);

                    ContinueToCallAgent.Instantiate((CallAgent)element, new Context(context, before), evalappend.AsIndex(0), fail);
                    Evaluator aftereval = new Evaluator(salience, ArgumentMode.ManyArguments, evalappend.AsIndex(1), aftersucc, isUserInput);
                    aftereval.Continue(new Context(context, after), fail);
                    return(true);
                }
                else
                {
                    if (argumentMode == ArgumentMode.SingleArgument)
                    {
                        ContinueToCallAgent callagent = new ContinueToCallAgent((CallAgent)element, mysucc);
                        Evaluator           eval      = new Evaluator(salience, ((CallAgent)element).ArgumentOptions, callagent, aftersucc, isUserInput);
                        eval.Continue(new Context(context, sublst), fail);
                        return(true);
                    }
                    else
                    {
                        ContinuationAppender evalappend = new ContinuationAppender(context, mysucc);

                        ContinueToCallAgent callagent = new ContinueToCallAgent((CallAgent)element, evalappend.AsIndex(0));

                        Evaluator aftereval = new Evaluator(salience, ArgumentMode.ManyArguments, evalappend.AsIndex(1), aftersucc, isUserInput);

                        Evaluator eval = new Evaluator(salience, ((CallAgent)element).ArgumentOptions, callagent, aftereval, isUserInput);
                        eval.Continue(new Context(context, sublst), fail);
                        return(true);
                    }
                }
            }
            else if (element is Codelet)
            {
                IContinuation mysucc = succ;
                if (ii > 0)
                {
                    mysucc = new ContextAppender(salience, context, ii, succ);
                }
                List <IContent> sublst = context.Contents.GetRange(ii + 1, context.Contents.Count - ii - 1);

                coderack.AddCodelet((Codelet)element, "Codelet in content");

                Evaluator eval = new Evaluator(salience, argumentMode, mysucc, aftersucc, isUserInput);
                eval.Continue(new Context(context, sublst), fail);
                return(true);
            }
            else
            {
                Context result = new Context(context, new List <IContent>(context.Contents.GetRange(0, ii)));
                if (element is IContent)
                {
                    result.Contents.Add((IContent)element);
                }
                else if (element is List <IContent> )
                {
                    result.Contents.AddRange((List <IContent>)element);
                }
                else
                {
                    result.Contents.Add(new Value(element));
                }

                if (argumentMode == ArgumentMode.SingleArgument)
                {
                    succ.Continue(result, fail);
                    aftersucc.Continue(context.ChildRange(1), fail);
                    return(true);
                }
                else
                {
                    IContinuation   appender = new ContextAppender(salience, result, result.Contents.Count, succ);
                    List <IContent> sublst   = context.Contents.GetRange(ii + 1, context.Contents.Count - ii - 1);

                    Evaluator eval = new Evaluator(salience, argumentMode, appender, aftersucc, isUserInput);
                    eval.Continue(new Context(context, sublst), fail);
                    return(true);
                }
            }
        }