Example #1
0
        private void CreatePropertyCondition(XbimQueryBuilder qBuilder, XmlNode propGroupNode)
        {
            GroupRule pGrpRule = GetGroupRule(propGroupNode);
            //apply logical operators according to the "select"

            XmlNodeList pSets = ((XmlElement)propGroupNode).GetElementsByTagName("propertySet");

            foreach (XmlNode pSet in pSets)
            {
                string   pSetName     = GetName(pSet);
                NameRule pSetNameRule = GetNameRule(pSet);

                XmlNode  propNode     = ((XmlElement)pSet).GetElementsByTagName("property").Item(0);
                string   propName     = GetName(propNode);
                NameRule propNameRule = GetNameRule(propNode);
                if (string.IsNullOrEmpty(propName))
                {
                    errLog.WriteLine("Name of the property must be specified. Property set: '" + pSetName + "', element: '" + eName + "'.");
                    continue;
                }

                XmlNode   valNode = (propNode as XmlElement).GetElementsByTagName("value").Item(0);
                string    value   = valNode.InnerText;
                ValueRule valRule = GetValueRule(valNode);

                if (string.IsNullOrEmpty(value))
                {
                    errLog.WriteLine("Value of the property '" + propName + "' must be specified. Property set: '" + pSetName + "', element: '" + eName + ".");
                    continue;
                }

                qBuilder.AddPropertyCondition(pSetName, pSetNameRule, propName, propNameRule, value, valRule, pGrpRule);
            }
        }
Example #2
0
        private ValueRule GetValueRule(XmlNode node)
        {
            XmlAttribute attr = node.Attributes["type"];
            string       type = "is"; //default value

            if (attr != null && !String.IsNullOrEmpty(attr.Value))
            {
                type = attr.Value;
            }

            ValueRule result = ValueRule.IS;

            if (type.ToLower() == "isnot")
            {
                result = ValueRule.IS_NOT;
            }
            if (type.ToLower() == "contains")
            {
                result = ValueRule.CONTAINS;
            }
            if (type.ToLower() == "notcontains")
            {
                result = ValueRule.NOT_CONTAINS;
            }
            if (type.ToLower() == "greater")
            {
                result = ValueRule.GREATER_THAN;
            }
            if (type.ToLower() == "less")
            {
                result = ValueRule.LESS_THAN;
            }

            return(result);
        }
        private void TestPropertyNameWithNullEquivalent(string Value)
        {
            // Arrange
            var rule = new ValueRule("TestRule");

            // Act
            rule.Property(Value);
        }
        private void TestNotEqualsWithNullEquivalent(string Value)
        {
            // Arrange
            var r = new ValueRule("RuleName").Property("APropertyName");

            // Act
            r.NotEquals(Value);
        }
Example #5
0
        public void TestWhereClause()
        {
            VersionRule rule = new VersionRule("Field");
            ValueRule   val  = rule.GetWhereClause(3);

            Assert.AreEqual(3, val.Value);
            Assert.AreEqual(ActionRule.Check, val.Action);
        }
Example #6
0
        public void TestWhereClause()
        {
            CreationDateRule rule = new CreationDateRule("Field");
            ValueRule        val  = rule.GetWhereClause(null);

            Assert.IsNull(val.Value);
            Assert.AreEqual(ActionRule.DoNothing, val.Action);
        }
Example #7
0
        public void TestInsertValue()
        {
            CreationDateRule rule = new CreationDateRule("Field");
            ValueRule        val  = rule.GetInsertValue(null);

            Assert.IsNotNull(val.Value);
            Assert.AreEqual(ActionRule.Update, val.Action);
        }
Example #8
0
        public void TestInsertValue()
        {
            VersionRule rule = new VersionRule("Field");
            ValueRule   val  = rule.GetInsertValue(null);

            Assert.AreEqual(1, val.Value);
            Assert.AreEqual(ActionRule.Update, val.Action);
        }
        public void PropertyNameThrowsExceptionWhenPropertyNameCalledTwice()
        {
            // Arrange
            var rule = new ValueRule("TestRule").Property("ANane").Equals("A Value");

            // Act
            rule.Property("BName");
        }
        public void NotEqualsThrowsExceptionIfPropertyNameIsNotSet()
        {
            // Arrange
            var rule = new ValueRule("TestRule");

            // Act
            rule.NotEquals("A Value");
        }
        public void EqualsThrowsExceptionIfComparisonAlreadySetup()
        {
            // Arrange
            var rule = new ValueRule("A Rule").Property("A name").Equals("A Value");

            // Act
            rule.Equals("B Value");
        }
Example #12
0
        public void TestUpdateValue()
        {
            VersionRule rule = new VersionRule("Field");
            ValueRule   val  = rule.GetUpdateValue(2);

            Assert.AreEqual(1, val.Value);
            Assert.AreEqual(ActionRule.IncrementalUpdate, val.Action);
        }
Example #13
0
        public void TestUpdateValue()
        {
            ModificationDateRule rule = new ModificationDateRule("Field");
            ValueRule            val  = rule.GetUpdateValue(null);

            Assert.IsNotNull(val.Value);
            Assert.AreEqual(ActionRule.Update, val.Action);
        }
Example #14
0
 public void TestWhereClause()
 {
     using (new TestSecurityContext()) {
         CreationUserRule rule = new CreationUserRule("Field");
         ValueRule        val  = rule.GetWhereClause(null);
         Assert.IsNull(val.Value);
         Assert.AreEqual(ActionRule.DoNothing, val.Action);
     }
 }
Example #15
0
 public void TestInsertValue()
 {
     using (new TestSecurityContext()) {
         CreationUserRule rule = new CreationUserRule("Field");
         ValueRule        val  = rule.GetInsertValue(null);
         Assert.IsNotNull(val.Value);
         Assert.AreEqual(ActionRule.Update, val.Action);
     }
 }
        public void ConstructorPopulatesRuleName()
        {
            // Arrange
            var ruleName = "This is a rule name.";
            // Act
            var rule = new ValueRule(ruleName);

            // Assert
            Assert.AreEqual(ruleName, rule.RuleName);
        }
Example #17
0
        /// <summary>
        ///     Tries the get value.
        /// </summary>
        /// <typeparam name="T">Strong type of the value.</typeparam>
        /// <param name="rule">The rule.</param>
        /// <param name="input">The input.</param>
        /// <param name="value">The value.</param>
        /// <returns><c>true</c> if rule matches the input, <c>false</c> otherwise.</returns>
        public static bool TryGetValue <T>(this ValueRule <T> rule, string input, out T value)
        {
            if (rule.Match(input))
            {
                return(rule.ParseTree(input).TryGetValue(out value));
            }

            value = default(T);
            return(false);
        }
Example #18
0
        public static bool EvaluatePropertyValue(IPersistIfcEntity element, string pSetName, NameRule pSetNameRule, string propertyName, NameRule propNameRule, string value, ValueRule valueRule)
        {
            Dictionary<IfcLabel, Dictionary<IfcIdentifier, IfcValue>> allProperties = null;

            if (string.IsNullOrEmpty(propertyName)) return false;

            //properties could be defined in IfcTypeObject as HasProperties
            IfcTypeObject typeObject = element as IfcTypeObject;
            if (typeObject != null)
            {
                allProperties = typeObject.GetAllPropertySingleValues();
            }
            //or properties could be defined in IfcObject in IsDefinedBy
            IfcObject ifcObject = element as IfcObject;
            if (ifcObject != null)
            {
                allProperties = ifcObject.GetAllPropertySingleValues();
            }
            //or properties could be defined for material as ExtendedMaterialProperties
            IfcMaterial material = element as IfcMaterial;
            if (material != null)
            {
                allProperties = material.GetAllPropertySingleValues();
            }

            //getting properties is not supported otherwise
            if (allProperties != null)
            {
                foreach (var p in allProperties)
                {
                    //if pSetName is null all property sets are inspected
                    if (pSetName != null)
                    {
                        if (!IsRightName(pSetName, p.Key, pSetNameRule))
                        {
                            continue;
                        }
                    }
                    foreach (var prop in p.Value)
                    {
                        //if name is not specified all values are returned
                       if (IsRightName(propertyName, prop.Key, propNameRule))
                        {
                           Type t = ((ExpressType)(prop.Value)).UnderlyingSystemType;
                           Expression right = XbimQueryFactory.PromoteToConstant(t, value);
                           Expression left = XbimQueryFactory.PromoteToConstant(t, prop.Value.ToString()); //todo: this should be more sofisticated than 'ToString()'
                           Expression eval = CreateValueExpression(left, right, valueRule);

                           return Expression.Lambda<Func<bool>>(eval).Compile()();
                        }
                    }
                }
            }
            return false;
        }
        private void TestNOTINWithNullEquivalent(string Value)
        {
            // Arrange
            var r = new ValueRule("ARuleName").Property("APropertyName");

            // Act
            r.NotIn(new List <string>(1)
            {
                Value
            });
        }
        public void NotInThrowsExceptionIfPropertyNameIsNotSet()
        {
            // Arrange
            var rule = new ValueRule("TestRule");

            // Act
            rule.NotIn(new List <string>()
            {
                "A Value"
            });
        }
        public void IsValidThrowsExceptionWhenComparisonNotSet()
        {
            // Arrange
            var rule = new ValueRule("TestRule").Property("APropertyName");

            // Act
            rule.IsValid(new Dictionary <string, string>(1)
            {
                { "APropertyName", "A Value" }
            });
        }
        public void PropertyNameSetsPropertyName()
        {
            // Arrange
            var r = new ValueRule("ARuleName");

            // Act
            r.Property("APropertyName");

            // Assert
            Assert.AreEqual("APropertyName", r.PropertyName);
        }
        public void NotInThrowsExceptionIfComparisonAlreadySetup()
        {
            // Arrange
            var rule = new ValueRule("A Rule").Property("A name").Equals("A Value");

            // Act
            rule.NotIn(new List <string>(1)
            {
                "A Value"
            });
        }
        public void IsValidReturnsTrueWhenNotRequiredAndValueIsWhiteSpace()
        {
            // Arrange
            var rule = new ValueRule("TestRule").Property("APropertyName").Equals("Value");
            // Act
            var result = rule.IsValid(new Dictionary <string, string>(1)
            {
                { "APropertyName", "    " }
            });

            // Assert
            Assert.IsTrue(result);
        }
        public void IsValidReturnsTrueWhenNotEqualsIsTrue()
        {
            // Arrange
            var rule = new ValueRule("TestRule").Property("APropertyName").NotEquals("I Am Valid");
            // Act
            var result = rule.IsValid(new Dictionary <string, string>(1)
            {
                { "APropertyName", "I Am NOT Valid" }
            });

            // Assert
            Assert.IsTrue(result);
        }
        public void IsValidReturnsFalseWhenRequiredButValueIsWhiteSpace()
        {
            // Arrange
            var rule = new ValueRule("TestRule").Property("APropertyName").Equals("Value").Required();
            // Act
            var result = rule.IsValid(new Dictionary <string, string>(1)
            {
                { "APropertyName", "    " }
            });

            // Assert
            Assert.IsFalse(result);
            Assert.AreEqual("APropertyName is required, but is null, an empty string, or whitespace.",
                            rule.ErrorMessage);
        }
        public void IsValidReturnsFalseWhenNotEqualsIsFalse()
        {
            // Arrange
            var rule = new ValueRule("TestRule").Property("APropertyName").NotEquals("hello");
            // Act
            var result = rule.IsValid(new Dictionary <string, string>(1)
            {
                { "APropertyName", "hello" }
            });

            // Assert
            Assert.IsFalse(result);
            Assert.AreEqual("APropertyName value of 'hello' is invalid.",
                            rule.ErrorMessage);
        }
        public void NotEqualsCreatedValueComparison()
        {
            // Arrange
            var r = new ValueRule("ARuleName").Property("APropertyName");

            // Act
            var returnedRule = r.NotEquals("A Value");

            // Assert
            Assert.AreSame(r, returnedRule);
            Assert.IsNotNull(r.Comparison);
            Assert.AreEqual("APropertyName", r.Comparison.PropertyName);
            Assert.AreEqual(Enums.ComparisonOperators.NOTEQ, r.Comparison.ComparisonOperator);
            Assert.AreEqual(1, r.Comparison.ValidValues.Count);
            Assert.AreEqual("A Value", r.Comparison.ValidValues[0]);
        }
        public void IsValidReturnsTrueWhenNotInIsTrue()
        {
            // Arrange
            var rule = new ValueRule("TestRule").Property("APropertyName").NotIn(new List <string>()
            {
                "one", "two", "three"
            });
            // Act
            var result = rule.IsValid(new Dictionary <string, string>(1)
            {
                { "APropertyName", "four" }
            });

            // Assert
            Assert.IsTrue(result);
        }
        public void IsValidReturnsFalseWhenInIsFalse()
        {
            // Arrange
            var rule = new ValueRule("TestRule").Property("APropertyName").In(new List <string>()
            {
                "one", "two", "three"
            });
            // Act
            var result = rule.IsValid(new Dictionary <string, string>(1)
            {
                { "APropertyName", "four" }
            });

            // Assert
            Assert.IsFalse(result);
            Assert.AreEqual("APropertyName value of 'four' is invalid.",
                            rule.ErrorMessage);
        }
        public void NotInSanitizesValueList()
        {
            // Arrange
            var r = new ValueRule("ARuleName").Property("APropertyName");

            // Act
            var returnedRule = r.NotIn(new List <string>(5)
            {
                "A Value", null, string.Empty, "   ", "B Value"
            });

            // Assert
            Assert.AreSame(r, returnedRule);
            Assert.IsNotNull(r.Comparison);
            Assert.AreEqual("APropertyName", r.Comparison.PropertyName);
            Assert.AreEqual(Enums.ComparisonOperators.NOTIN, r.Comparison.ComparisonOperator);
            Assert.AreEqual(2, r.Comparison.ValidValues.Count);
            Assert.AreEqual("A Value", r.Comparison.ValidValues[0]);
            Assert.AreEqual("B Value", r.Comparison.ValidValues[1]);
        }
Example #32
0
 private static Expression CreateValueExpression(Expression left, Expression right, ValueRule valueRule)
 {
     switch (valueRule)
     {
         case ValueRule.IS: return XbimQueryFactory.GenerateEqual(left, right);
         case ValueRule.IS_NOT: return XbimQueryFactory.GenerateNotEqual(left, right);
         case ValueRule.CONTAINS: return XbimQueryFactory.GenerateContains(left, right);
         case ValueRule.NOT_CONTAINS: return Expression.Not(XbimQueryFactory.GenerateContains(left, right));
         case ValueRule.GREATER_THAN: return XbimQueryFactory.GenerateGreaterThan(left, right);
         case ValueRule.LESS_THAN: return XbimQueryFactory.GenerateLessThan(left, right);
         default:
             throw new Exception("Unexpected enumeration value: " + Enum.GetName(typeof(GroupRule), valueRule));
     }
 }
Example #33
0
        public void AddPropertyCondition(string pSetName, NameRule pSetNameRule, string propertyName, NameRule propNameRule, string value, ValueRule valueRule, GroupRule groupRule)
        {
            Expression psn = Expression.Constant(pSetName);
            Expression psnRl = Expression.Constant(pSetNameRule);
            Expression pn = Expression.Constant(propertyName);
            Expression pnRl = Expression.Constant(propNameRule);
            Expression valConst = Expression.Constant(value);
            Expression valRul = Expression.Constant(valueRule);

            MethodInfo getPropMeth = typeof(PropertyHelper).GetMethod("EvaluatePropertyValue");
            if (getPropMeth == null) throw new Exception("Wrong method definition.");
            //public static IfcValue GetPropertyValue(IPersistIfcEntity element, string pSetName, NameRule pSetNameRule, string propertyName, NameRule propNameRule)
            Expression property = Expression.Call(getPropMeth, new Expression[] {input ,psn, psnRl, pn, pnRl, valConst, valRul}); //bool result of the test

            AddToGroup(property, groupRule);
        }
Example #34
0
        public void AddAttributeCondition(string attributName, string value, ValueRule valueRule, GroupRule groupRule)
        {
            if (attributName == null) { Err("Attribute name not specified"); return; }

            //check attribut existence in the specified type
            PropertyInfo attr = Type.GetProperty(attributName);
            if (attr == null) { Err("Attribute name '" + attributName + "' does not exist in specified type '" + Type.Name + "'."); return; }

            //create expression to access attribute
            Expression attrExp = Expression.Property(input, attr);

            //get value from the string
            Expression val = XbimQueryFactory.PromoteToConstant(attr.PropertyType, value);
            if (val == null) { Err("Value '" + value + "' could not be converted to the type of the attribute '" + attributName + "'."); return; }

            //create binary expression according to valueRule
            Expression expression = CreateValueExpression(attrExp, val, valueRule);
            if (expression == null) { Err("Value rule'" + Enum.GetName(typeof(ValueRule), valueRule)+ "' cannot be applied on the attribute '" + attributName + "'."); return; }

            //add resulting expression to the group
            AddToGroup(expression, groupRule);
        }
 public RequiredIfBoolValueAttribute(string dependentProperty, bool targetValue, ValueRule valueRule)
 {
     this.DependentProperty = dependentProperty;
     this.TargetValue = targetValue;
     this.ValueRule = valueRule;
 }