Exemplo n.º 1
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data      = GetData(line);
            var material  = data["material"].Value;
            var type      = data["type"].Value;
            var perimeter = data["perimeter"].Value;

            var building = material == "stone" ? "stone-wall-line" : "palisade-wall";

            var conditions = new List <string>();
            var actions    = new List <string>();

            if (type == "walls")
            {
                conditions.Add($"can-build-wall {perimeter} {building}");

                actions.Add($"build-wall {perimeter} {building}");
            }
            else
            {
                conditions.Add($"building-type-count-total {building} > 0");
                conditions.Add($"can-build-gate {perimeter}");
                conditions.Add("building-type-count-total gate < 5");

                actions.Add($"build-gate {perimeter}");
            }

            var rule = new Defrule(conditions, actions);

            context.AddToScript(context.ApplyStacks(rule));
        }
Exemplo n.º 2
0
        public override void Parse(string line, TranspilerContext context)
        {
            context.UsingVolatileGoal(gathererGoal =>
            {
                context.UsingVolatileGoal(farmGoal =>
                {
                    var infoRule = new Defrule(
                        new[]
                    {
                        "true",
                    },
                        new[]
                    {
                        $"up-get-fact unit-type-count villager {gathererGoal}",
                        $"up-modify-goal {gathererGoal} s:* sn-food-gatherer-percentage",
                        $"up-modify-goal {gathererGoal} c:/ 100",
                        $"up-get-fact building-type-count-total farm {farmGoal}",
                    });

                    var buildRule = new Defrule(
                        new[]
                    {
                        $"up-compare-goal {farmGoal} g:< {gathererGoal}",
                        "can-build farm",
                    },
                        new[]
                    {
                        "build farm"
                    });

                    context.AddToScript(context.ApplyStacks(infoRule));
                    context.AddToScript(context.ApplyStacks(buildRule));
                });
            });
        }
Exemplo n.º 3
0
        public override void Parse(string line, TranspilerContext context)
        {
            var location = GetData(line)["location"].Value;
            var rule     = new Defrule(new[] { "true" }, new[] { $"up-send-scout group-type-land-explore scout-{location}" });

            context.AddToScript(context.ApplyStacks(rule));
        }
Exemplo n.º 4
0
        public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
        {
            bool removed = true;

            if (params_Renamed.Length == 1)
            {
                String  rl    = params_Renamed[0].StringValue;
                Defrule defrl = (Defrule)engine.CurrentFocus.findRule(rl);
                if (defrl != null)
                {
                    engine.CurrentFocus.removeRule(defrl, engine, engine.WorkingMemory);
                }
                else
                {
                    removed = false;
                }
            }
            else
            {
                removed = false;
            }
            DefaultReturnVector ret = new DefaultReturnVector();
            DefaultReturnValue  rv  = new DefaultReturnValue(Constants.BOOLEAN_OBJECT, removed);

            ret.addReturnValue(rv);
            return(ret);
        }
Exemplo n.º 5
0
 /// <summary>
 /// method does the actual work of creating a CLIPSParser and parsing
 /// the file.
 /// </summary>
 /// <param name="engine">The engine.</param>
 /// <param name="ins">The ins.</param>
 /// <param name="rv">The rv.</param>
 public virtual void parse(Rete engine, Stream ins, DefaultReturnVector rv)
 {
     try
     {
         CLIPSParser parser = new CLIPSParser(engine, ins);
         Object      expr   = null;
         while ((expr = parser.basicExpr()) != null)
         {
             if (expr is Defrule)
             {
                 Defrule rl = (Defrule)expr;
                 engine.RuleCompiler.addRule(rl);
             }
             else if (expr is Deftemplate)
             {
                 Deftemplate dft = (Deftemplate)expr;
                 engine.CurrentFocus.addTemplate(dft, engine, engine.WorkingMemory);
             }
             else if (expr is IFunction)
             {
                 IFunction fnc = (IFunction)expr;
                 fnc.executeFunction(engine, null);
             }
         }
         if (rv != null)
         {
             rv.addReturnValue(new DefaultReturnValue(Constants.BOOLEAN_OBJECT, true));
         }
     }
     catch (ParseException e)
     {
         engine.writeMessage(e.Message + Constants.LINEBREAK, Constants.DEFAULT_OUTPUT);
     }
 }
Exemplo n.º 6
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data     = GetData(line);
            var style    = data["style"].Value;
            var headroom = data["headroom"].Value.ReplaceIfNullOrEmpty(DefaultHeadroom.ToString());

            var building = style == "houses" ? "house" : Game.YurtId.ToString();

            var conditions = new[]
            {
                "population-headroom != 0",
                $"up-pending-objects c: {building} < 2",
                $"can-build {building}",
                $"housing-headroom < {headroom}",
            };

            var actions = new[]
            {
                $"build {building}",
            };

            var rule = new Defrule(conditions, actions);

            context.AddToScript(context.ApplyStacks(rule));
        }
Exemplo n.º 7
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data     = GetData(line);
            var amount   = int.Parse(data["amount"].Value);
            var fromList = data["fromlist"].Value.Split(" and ");
            var toList   = data["tolist"].Value.Split(" and ");

            var conditions = new List <string>()
            {
                "true"
            };
            var actions = new List <string>()
            {
                "do-nothing"
            };

            foreach (var resource in fromList)
            {
                conditions.Add($"strategic-number sn-{resource}-gatherer-percentage >= {amount / fromList.Length}");
                actions.Add($"up-modify-sn sn-{resource}-gatherer-percentage c:- {amount / fromList.Length}");
            }

            foreach (var resource in toList)
            {
                conditions.Add($"strategic-number sn-{resource}-gatherer-percentage <= {100 - amount / toList.Length}");
                actions.Add($"up-modify-sn sn-{resource}-gatherer-percentage c:+ {amount / toList.Length}");
            }

            var rule = new Defrule(conditions, actions);

            context.AddToScript(context.ApplyStacks(rule));
        }
Exemplo n.º 8
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data     = GetData(line);
            var amount   = data["amount"].Value;
            var building = data["building"].Value;

            var rule = new Defrule(new[] { "true" }, new[] { $"up-assign-builders c: {building} c: {amount}" });

            context.AddToScript(context.ApplyStacks(rule));
        }
Exemplo n.º 9
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data = GetData(line);
            var type = data["type"].Value;
            var name = data["name"].Value;

            var rule = new Defrule(new[] { "true" }, new[] { $"delete-{type} {name}" });

            context.AddToScript(context.ApplyStacks(rule));
        }
Exemplo n.º 10
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data = GetData(line);

            var amount   = data["amount"].Value;
            var resource = data["resource"].Value;
            var player   = data["player"].Value;

            var rule = new Defrule(new[] { "true" }, new[] { $"tribute-to-player {player} {resource} {amount}" });

            context.AddToScript(context.ApplyStacks(rule));
        }
Exemplo n.º 11
0
        /// <summary> When Clear is called, the module needs to Clear all the internal lists
        /// for rules and activations. The handle to Rete should not be nulled.
        /// </summary>
        public virtual void clear()
        {
            activations.clear();
            IEnumerator itr = rules.Values.GetEnumerator();

            while (itr.MoveNext())
            {
                Defrule rl = (Defrule)itr.Current;
                rl.clear();
            }
            rules.Clear();
            deftemplates.Clear();
        }
Exemplo n.º 12
0
        public override void Parse(string line, TranspilerContext context)
        {
            var perimeter = GetData(line)["perimeter"].Value;

            var rule = new Defrule(
                new[]
            {
                "true",
            },
                new[]
            {
                $"enable-wall-placement {perimeter}",
                "disable-self",
            });

            context.AddToScript(context.ApplyStacks(rule));
        }
Exemplo n.º 13
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data      = GetData(line);
            var operation = data["operation"].Value;
            var amount    = data["amount"].Value;

            var rule = new Defrule();

            if (operation == "set")
            {
                rule.Actions.Add(new Action($"set-strategic-number sn-maximum-town-size {amount}"));
            }
            else
            {
                rule.Actions.Add(new Action($"up-modify-sn sn-maximum-town-size c:{(operation == "increase" ? "+" : "-")} {amount}"));
            }

            context.AddToScript(context.ApplyStacks(rule));
        }
Exemplo n.º 14
0
        protected void parse(Rete engine, CLIPSParser parser, IList factlist)
        {
            Object itm = null;

            try
            {
                while ((itm = parser.basicExpr()) != null)
                {
                    // System.Console.WriteLine("obj is " + itm.getClass().Name);
                    if (itm is Defrule)
                    {
                        Defrule rule = (Defrule)itm;
                        engine.RuleCompiler.addRule(rule);
                    }
                    else if (itm is Deftemplate)
                    {
                        Deftemplate dt = (Deftemplate)itm;
                        Console.WriteLine("template=" + dt.Name);
                        engine.declareTemplate(dt);
                    }
                    else if (itm is FunctionAction)
                    {
                        FunctionAction fa = (FunctionAction)itm;
                    }
                    else if (itm is IFunction)
                    {
                        IReturnVector rv  = ((IFunction)itm).executeFunction(engine, null);
                        IEnumerator   itr = rv.Iterator;
                        while (itr.MoveNext())
                        {
                            IReturnValue rval = (IReturnValue)itr.Current;
                            Console.WriteLine(rval.StringValue);
                        }
                    }
                }
            }
            catch
            {
                // Console.WriteLine(e.Message);
                parser.ReInit(Console.OpenStandardInput());
            }
        }
Exemplo n.º 15
0
        public override void Parse(string line, TranspilerContext context)
        {
            var amount = GetData(line)["amount"].Value;

            Defrule rule;

            if (string.IsNullOrEmpty(amount))
            {
                rule = new Defrule(
                    new[] { "true" },
                    new[] { "attack-now" });
            }
            else
            {
                rule = new Defrule(
                    new[] { $"military-population >= {amount}" },
                    new[] { "attack-now" });
            }

            context.AddToScript(context.ApplyStacks(rule));
        }
Exemplo n.º 16
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data         = GetData(line);
            var action       = data["action"].Value;
            var resource     = data["resource"].Value;
            var testResource = data["testresource"].Value;
            var comparison   = data["comparison"].Value;
            var amount       = data["amount"].Value;

            var rule = new Defrule(
                new[]
            {
                $"{testResource}-amount {comparison} {amount}",
                $"can-{action}-commodity {resource}",
            },
                new[]
            {
                $"{action}-commodity {resource}",
            });

            context.AddToScript(context.ApplyStacks(rule));
        }
Exemplo n.º 17
0
        public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
        {
            bool add = true;

            if (params_Renamed.Length == 1 && params_Renamed[0].Value is Defrule)
            {
                Defrule rl = (Defrule)params_Renamed[0].Value;
                if (!engine.CurrentFocus.containsRule(rl))
                {
                    add = engine.RuleCompiler.addRule(rl);
                }
            }
            else
            {
                add = false;
            }
            DefaultReturnVector ret = new DefaultReturnVector();
            DefaultReturnValue  rv  = new DefaultReturnValue(Constants.BOOLEAN_OBJECT, add);

            ret.addReturnValue(rv);
            return(ret);
        }
Exemplo n.º 18
0
 public abstract string Format(Defrule rule, IConditionFormat conditionFormat);