private void ParsePremise(
     string[] tok,
     Rulewords logop,
     IEnumerable <SimulationNode> nodes,
     IEnumerable <SimulationLink> links)
 {
     _pchain.Add(new Premise(tok, logop, nodes, links));
 }
        private void ParseAction(Rulewords state, string[] tok, IEnumerable <SimulationLink> links)
        {
            Action a = new Action(tok, links, _label);

            if (state == Rulewords.THEN)
            {
                _tchain.Insert(0, a);
            }
            else
            {
                _fchain.Insert(0, a);
            }
        }
Exemple #3
0
        public static bool TryParse(string text, out Rulewords result)
        {
            if (text.Match(Keywords.wr_RULE))
            {
                result = Rulewords.RULE;
            }
            else if (text.Match(Keywords.wr_IF))
            {
                result = Rulewords.IF;
            }
            else if (text.Match(Keywords.wr_AND))
            {
                result = Rulewords.AND;
            }
            else if (text.Match(Keywords.wr_OR))
            {
                result = Rulewords.ERROR;
            }
            else if (text.Match(Keywords.wr_THEN))
            {
                result = Rulewords.THEN;
            }
            else if (text.Match(Keywords.wr_ELSE))
            {
                result = Rulewords.ELSE;
            }
            else if (text.Match(Keywords.wr_PRIORITY))
            {
                result = Rulewords.PRIORITY;
            }
            else if (text.Match(string.Empty))
            {
                result = Rulewords.ERROR;
            }
            else
            {
                result = (Rulewords)(-1);
                return(false);
            }

            return(true);
        }
        public SimulationRule(Rule rule, IList <SimulationLink> links, IList <SimulationNode> nodes)
        {
            _label = rule.Name;

            double tempPriority = 0.0;

            Rulewords ruleState = Rulewords.RULE;

            foreach (string line in rule.Code)
            {
                string[]  tok = line.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
                Rulewords key;

                if (!EnumsTxt.TryParse(tok[0], out key))
                {
                    throw new ENException(ErrorCode.Err201);
                }

                switch (key)
                {
                case Rulewords.IF:
                    if (ruleState != Rulewords.RULE)
                    {
                        throw new ENException(ErrorCode.Err221);
                    }
                    ruleState = Rulewords.IF;
                    ParsePremise(tok, Rulewords.AND, nodes, links);
                    break;

                case Rulewords.AND:
                    switch (ruleState)
                    {
                    case Rulewords.IF:
                        ParsePremise(tok, Rulewords.AND, nodes, links);
                        break;

                    case Rulewords.THEN:
                    case Rulewords.ELSE:
                        ParseAction(ruleState, tok, links);
                        break;

                    default:
                        throw new ENException(ErrorCode.Err221);
                    }
                    break;

                case Rulewords.OR:
                    if (ruleState == Rulewords.IF)
                    {
                        ParsePremise(tok, Rulewords.OR, nodes, links);
                    }
                    else
                    {
                        throw new ENException(ErrorCode.Err221);
                    }
                    break;

                case Rulewords.THEN:
                    if (ruleState != Rulewords.IF)
                    {
                        throw new ENException(ErrorCode.Err221);
                    }
                    ruleState = Rulewords.THEN;
                    ParseAction(ruleState, tok, links);
                    break;

                case Rulewords.ELSE:
                    if (ruleState != Rulewords.THEN)
                    {
                        throw new ENException(ErrorCode.Err221);
                    }
                    ruleState = Rulewords.ELSE;
                    ParseAction(ruleState, tok, links);
                    break;

                case Rulewords.PRIORITY: {
                    if (ruleState != Rulewords.THEN && ruleState != Rulewords.ELSE)
                    {
                        throw new ENException(ErrorCode.Err221);
                    }

                    ruleState = Rulewords.PRIORITY;

                    if (!tok[1].ToDouble(out tempPriority))
                    {
                        throw new ENException(ErrorCode.Err202);
                    }

                    break;
                }

                default:
                    throw new ENException(ErrorCode.Err201);
                }
            }

            _priority = tempPriority;
        }
            public Premise(string[] tok, Rulewords lOp, IEnumerable <SimulationNode> nodes, IEnumerable <SimulationLink> links)
            {
                Objects   loType;
                Varwords  lVar;
                object    lObj;
                Operators lROp;

                if (tok.Length != 5 && tok.Length != 6)
                {
                    throw new ENException(ErrorCode.Err201);
                }

                EnumsTxt.TryParse(tok[1], out loType);

                if (loType == Objects.SYSTEM)
                {
                    EnumsTxt.TryParse(tok[2], out lVar);

                    switch (lVar)
                    {
                    case Varwords.DEMAND:
                    case Varwords.TIME:
                    case Varwords.CLOCKTIME:
                        lObj = Objects.SYSTEM;
                        break;

                    default:
                        throw new ENException(ErrorCode.Err201);
                    }
                }
                else
                {
                    if (!EnumsTxt.TryParse(tok[3], out lVar))
                    {
                        throw new ENException(ErrorCode.Err201);
                    }

                    switch (loType)
                    {
                    case Objects.NODE:
                    case Objects.JUNC:
                    case Objects.RESERV:
                    case Objects.TANK:
                        loType = Objects.NODE;
                        break;

                    case Objects.LINK:
                    case Objects.PIPE:
                    case Objects.PUMP:
                    case Objects.VALVE:
                        loType = Objects.LINK;
                        break;

                    default:
                        throw new ENException(ErrorCode.Err201);
                    }

                    if (loType == Objects.NODE)
                    {
                        SimulationNode node = nodes.FirstOrDefault(simNode => simNode.Node.Name.Equals(tok[2], StringComparison.OrdinalIgnoreCase));

                        if (node == null)
                        {
                            throw new ENException(ErrorCode.Err203);
                        }

                        switch (lVar)
                        {
                        case Varwords.DEMAND:
                        case Varwords.HEAD:
                        case Varwords.GRADE:
                        case Varwords.LEVEL:
                        case Varwords.PRESSURE:
                            break;

                        case Varwords.FILLTIME:
                        case Varwords.DRAINTIME:
                            if (node is SimulationTank)
                            {
                                throw new ENException(ErrorCode.Err201);
                            }
                            break;

                        default:
                            throw new ENException(ErrorCode.Err201);
                        }
                        lObj = node;
                    }
                    else
                    {
                        SimulationLink link = links
                                              .FirstOrDefault(simLink => simLink.Link.Name.Equals(tok[2], StringComparison.OrdinalIgnoreCase));

                        if (link == null)
                        {
                            throw new ENException(ErrorCode.Err204);
                        }

                        switch (lVar)
                        {
                        case Varwords.FLOW:
                        case Varwords.STATUS:
                        case Varwords.SETTING:
                            break;

                        default:
                            throw new ENException(ErrorCode.Err201);
                        }
                        lObj = link;
                    }
                }

                Operators op;

                if (!EnumsTxt.TryParse(loType == Objects.SYSTEM ? tok[3] : tok[4], out op))
                {
                    throw new ENException(ErrorCode.Err201);
                }

                switch (op)
                {
                case Operators.IS:
                    lROp = Operators.EQ;
                    break;

                case Operators.NOT:
                    lROp = Operators.NE;
                    break;

                case Operators.BELOW:
                    lROp = Operators.LT;
                    break;

                case Operators.ABOVE:
                    lROp = Operators.GT;
                    break;

                default:
                    lROp = op;
                    break;
                }

                // BUG: Baseform bug lStat == Rule.Values.IS_NUMBER
                Values lStat = Values.IS_NUMBER;
                double lVal  = double.NaN;

                if (lVar == Varwords.TIME || lVar == Varwords.CLOCKTIME)
                {
                    lVal = tok.Length == 6
                        ? Utilities.GetHour(tok[4], tok[5])
                        : Utilities.GetHour(tok[4]);

                    lVal *= 3600;

                    if (lVal < 0.0)
                    {
                        throw new ENException(ErrorCode.Err202);
                    }
                }
                else
                {
                    Values k;

                    if (!EnumsTxt.TryParse(tok[tok.Length - 1], out k) || lStat <= Values.IS_NUMBER)
                    {
                        if (lStat == (Values)(-1) || lStat <= Values.IS_NUMBER)
                        {
                            if (!tok[tok.Length - 1].ToDouble(out lVal))
                            {
                                throw new ENException(ErrorCode.Err202);
                            }

                            if (lVar == Varwords.FILLTIME || lVar == Varwords.DRAINTIME)
                            {
                                lVal *= 3600.0;
                            }
                        }
                    }
                    else
                    {
                        lStat = k;
                    }
                }

                _status   = lStat;
                _value    = lVal;
                logop     = lOp;
                _relop    = lROp;
                _variable = lVar;
                _object   = lObj;
            }