Ejemplo n.º 1
0
            public Action(string[] tok, IEnumerable <SimulationLink> links, string label)
            {
                _label = label;

                int ntokens = tok.Length;

                Values k;

                if (ntokens != 6)
                {
                    throw new ENException(ErrorCode.Err201);
                }

                SimulationLink link = links.FirstOrDefault(simLink => simLink.Link.Name.Equals(tok[2], StringComparison.OrdinalIgnoreCase));

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

                if (link.Type == LinkType.CV)
                {
                    throw new ENException(ErrorCode.Err207);
                }

                var    s = (Values)(-1);
                double x = double.NaN;

                if (EnumsTxt.TryParse(tok[5], out k) && k > Values.IS_NUMBER)
                {
                    s = k;
                }
                else
                {
                    if (!tok[5].ToDouble(out x) || x < 0.0)
                    {
                        throw new ENException(ErrorCode.Err202);
                    }
                }

                if (!double.IsNaN(x) && link.Type == LinkType.GPV)
                {
                    throw new ENException(ErrorCode.Err202);
                }

                if (!double.IsNaN(x) && link.Type == LinkType.PIPE)
                {
                    s = x == 0.0 ? Values.IS_CLOSED : Values.IS_OPEN;
                    x = double.NaN;
                }

                _link    = link;
                _status  = s;
                _setting = x;
            }
Ejemplo n.º 2
0
        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;
        }
Ejemplo n.º 3
0
            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;
            }