예제 #1
0
        private RuleEngineItem createRuleEngineItem(string fullElementName, string op, string right, string dateType, string bitWiseOperation)
        {
            IoTHubAliasEventMessageReceiver receiver = new IoTHubAliasEventMessageReceiver("test");

            RuleEngineItem rei = new RuleEngineItem();

            rei.ElementName    = fullElementName;
            rei.DataType       = AlarmRuleItemEngineUtility.GetSupportDataType(dateType);
            rei.OrderOperation = bitWiseOperation;
            rei.Result         = false;

            if (rei.DataType == SupportDataTypeEnum.String &&
                (string.IsNullOrEmpty(right) || right.ToLower().Equals("null")))
            {
                right = "null";
            }

            if (rei.DataType == SupportDataTypeEnum.Numeric || rei.DataType == SupportDataTypeEnum.Bool)
            {
                rei.Equality = receiver.CreateCompiledRuleFuncTest(rei.DataType, op, right);
            }
            else
            {
                // SupportDataTypeEnum.String
                rei.StringRightValue     = right;
                rei.StringEqualOperation = op;
            }

            return(rei);
        }
예제 #2
0
        private void runSingleRuleItem(RuleEngineItem ruleEngineItem, JObject payload)
        {
            string elementName = ruleEngineItem.ElementName;

            try
            {
                var value = payload[elementName];

                DynamicMessageElement dm = new DynamicMessageElement();
                dm.Name = elementName;
                switch (ruleEngineItem.DataType)
                {
                case SupportDataTypeEnum.Bool:
                    dm.StringValue = value.ToString().ToLower();
                    break;

                case SupportDataTypeEnum.String:
                    dm.StringValue = (string)value;
                    if (string.IsNullOrEmpty(dm.StringValue))
                    {
                        dm.StringValue = "null";
                    }
                    break;

                case SupportDataTypeEnum.Numeric:
                    dm.DecimalValue = (decimal)value;
                    break;

                default:
                    throw new NotSupportedException();
                }

                if (ruleEngineItem.DataType == SupportDataTypeEnum.Numeric || ruleEngineItem.DataType == SupportDataTypeEnum.Bool)
                {
                    ruleEngineItem.Result = ruleEngineItem.Equality(dm);
                }
                else
                {
                    // SupportDataTypeEnum.String
                    bool equal = string.Equals(dm.StringValue, ruleEngineItem.StringRightValue);
                    if (ruleEngineItem.StringEqualOperation.Equals("="))
                    {
                        ruleEngineItem.Result = equal;
                    }
                    else if (ruleEngineItem.StringEqualOperation.Equals("!="))
                    {
                        ruleEngineItem.Result = !equal;
                    }
                    else
                    {
                        throw new ArgumentNullException("String equal operation is not supported - " + ruleEngineItem.StringEqualOperation);
                    }
                }
            }
            catch (Exception)
            {
                ruleEngineItem.Result = false;
            }
        }
예제 #3
0
        private Dictionary <string, RuleEngineItem> createRuleEngineItemDictionary(List <RuleEngineItem> ruleEngineItems)
        {
            Dictionary <string, RuleEngineItem> ruleEngineItemDictionary = new Dictionary <string, RuleEngineItem>();

            for (int i = 0; i < ruleEngineItems.Count; i++)
            {
                RuleEngineItem rei = ruleEngineItems[i];

                ruleEngineItemDictionary.Add(rei.ElementName + "-" + i, rei);
            }

            return(ruleEngineItemDictionary);
        }
예제 #4
0
        private bool compileBitWiseRules(int offset, Dictionary <string, RuleEngineItem> ruleEngineItems)
        {
            RuleEngineItem rei = ruleEngineItems.ElementAt(offset).Value;

            if (offset == 0)
            {
                return(rei.Result);
            }
            else
            {
                offset--;
                RuleEngineItem previousRei = ruleEngineItems.ElementAt(offset).Value;
                return(AlarmRuleItemEngineUtility.ComplieBoolRule(rei.Result, previousRei.OrderOperation, compileBitWiseRules(offset, ruleEngineItems)));
            }
        }
예제 #5
0
        private bool runSingleRuleItem(SupportDataTypeEnum testDataType, string elementName, string op, string right, JObject testObject)
        {
            MessageProcessorFactoryModel msgProcessorFactoryModel = new MessageProcessorFactoryModel();

            IoTHubAliasEventMessageReceiver    receiver            = new IoTHubAliasEventMessageReceiver("test");
            Func <DynamicMessageElement, bool> func                = receiver.CreateCompiledRuleFuncTest(testDataType, op, right);
            SfMessageEventProcessor            sfMsgEventProcessor = new SfMessageEventProcessor(msgProcessorFactoryModel);

            RuleEngineItem ruleEngineItem = new RuleEngineItem
            {
                ElementName    = elementName,
                DataType       = testDataType,
                OrderOperation = "end",
                Result         = false,
                Equality       = func
            };

            sfMsgEventProcessor.RunSingleRuleItemTest(ruleEngineItem, testObject);

            return(ruleEngineItem.Result);
        }
예제 #6
0
        private Dictionary <string, RuleEngineItem> createRuleEngineItem(EventRuleCatalog eventRuleCatalog)
        {
            List <DetailForRuleEngineModel> detailForRuleEngineModelList = new List <DetailForRuleEngineModel>();

            foreach (EventRuleItem erItem in eventRuleCatalog.EventRuleItem)
            {
                DetailForRuleEngineModel returnData = new DetailForRuleEngineModel();
                returnData.Id = erItem.Id;
                returnData.EventRuleCatalogId = eventRuleCatalog.Id;
                returnData.Ordering           = erItem.Ordering;
                returnData.MessageElementId   = erItem.MessageElementId;
                returnData.EqualOperation     = erItem.EqualOperation;
                returnData.Value                  = erItem.Value;
                returnData.BitWiseOperation       = erItem.BitWiseOperation;
                returnData.MessageElementDataType = erItem.MessageElement1.ElementDataType;

                if (erItem.MessageElement != null)
                {
                    returnData.MessageElementFullName = erItem.MessageElement.ElementName + "_" + erItem.MessageElement1.ElementName;
                }
                else
                {
                    returnData.MessageElementFullName = erItem.MessageElement1.ElementName;
                }

                detailForRuleEngineModelList.Add(returnData);
            }

            Dictionary <string, RuleEngineItem> ruleEngineItems = new Dictionary <string, RuleEngineItem>();

            int index = 0;

            foreach (var detailForRuleEngineModel in detailForRuleEngineModelList)
            {
                RuleEngineItem rei = new RuleEngineItem();
                rei.ElementName    = detailForRuleEngineModel.MessageElementFullName;
                rei.DataType       = AlarmRuleItemEngineUtility.GetSupportDataType(detailForRuleEngineModel.MessageElementDataType);
                rei.OrderOperation = detailForRuleEngineModel.BitWiseOperation;
                rei.Result         = false;

                _consoleLog.MessageEventDebug("--ElementName={0}, BitWiseOperation={1}", rei.ElementName, rei.OrderOperation);

                if (rei.DataType == SupportDataTypeEnum.String &&
                    (string.IsNullOrEmpty(detailForRuleEngineModel.Value) || detailForRuleEngineModel.Value.ToLower().Equals("null")))
                {
                    detailForRuleEngineModel.Value = "null";
                }

                if (rei.DataType == SupportDataTypeEnum.Numeric || rei.DataType == SupportDataTypeEnum.Bool)
                {
                    rei.Equality = createCompiledRuleFunc(rei.DataType, detailForRuleEngineModel.EqualOperation, detailForRuleEngineModel.Value);
                }
                else
                {
                    // SupportDataTypeEnum.String
                    rei.Equality         = null;
                    rei.StringRightValue = detailForRuleEngineModel.Value;

                    if (detailForRuleEngineModel.EqualOperation.Equals("=") || detailForRuleEngineModel.EqualOperation.Equals("!="))
                    {
                        rei.StringEqualOperation = detailForRuleEngineModel.EqualOperation;
                    }
                    else
                    {
                        throw new ArgumentNullException("String equal operation is not supported - " + detailForRuleEngineModel.EqualOperation);
                    }

                    _consoleLog.MessageEventDebug("----ruleText=({0} {1} {2})", rei.ElementName, detailForRuleEngineModel.EqualOperation, rei.StringRightValue);
                }

                // Add the index to avoid the duplicate key
                ruleEngineItems.Add(rei.ElementName + "-" + index, rei);
                index++;
            }

            return(ruleEngineItems);
        }