Ejemplo n.º 1
0
        private int Position(AssertionContext contextObj)
        {
            DesignByContract.Check.Require(this.functionName.Equals("position", StringComparison.InvariantCultureIgnoreCase),
                                           "functionName must be position");

            AssertionContext parent = contextObj.Parent;

            System.Collections.IList list = parent.Data as System.Collections.IList;
            if (list != null)
            {
                return(list.IndexOf(contextObj.Data));
            }

            AssumedTypes.IList assumedList = parent.Data as AssumedTypes.IList;
            if (assumedList != null)
            {
                for (int i = 0; i < assumedList.Count; i++)
                {
                    if (contextObj.Data.Equals(assumedList[i]))
                    {
                        return(i + 1);
                    }
                }
            }

            return(-1);
        }
Ejemplo n.º 2
0
        public override bool ValidValue(object dataValue)
        {
            Check.Require(dataValue != null, string.Format(CommonStrings.XMustNotBeNull, "dataValue"));

            AssumedTypes.IAggregate aggregate = dataValue as AssumedTypes.IAggregate;
            Check.Require(aggregate != null, string.Format(
                              AmValidationStrings.XMustImplementY, dataValue.GetType().ToString(), "IAggregate"));

            bool result = true;

            aggregate.Constraint = this;
            AssumedTypes.IList iList = dataValue as AssumedTypes.IList;

            if (iList == null)
            {
                throw new ApplicationException(string.Format(AmValidationStrings.XMustImplementY, dataValue.GetType().ToString(), "IList"));
            }

            if (!Cardinality.Interval.Has(iList.Count))
            {
                result = false;
                ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.CardinalityOutOfBounds, iList.Count, RmAttributeName));
            }

            result &= Cardinality.IsOrdered ? IsOrderedChildrenValid(iList) : IsUnorderedChildrenValid(iList);
            result &= IsOccurrencesValid(iList);
            return(result);
        }
Ejemplo n.º 3
0
        public object ItemAtPath(object obj)
        {
            object itemAtPath = this.pathExpr.Evaluate(obj);

            AssumedTypes.IList iList = itemAtPath as AssumedTypes.IList;

            // if itemAtPath is not a list, return it.
            if (iList == null)
            {
                return(itemAtPath);
            }

            //// if itemAtPath is a list, generate LocatableList when the items are locatable.
            //// otherwise return the list
            LocatableList <Locatable> locatableList = null;


            AssumedTypes.List <object> items = iList as AssumedTypes.List <object>;
            if (items != null)
            {
                return(items);
            }
            else
            {
                items = new OpenEhr.AssumedTypes.List <object>();
            }

            foreach (object o in iList)
            {
                items.Add(o);
            }

            return(items);
        }
Ejemplo n.º 4
0
        private object GetAttributeObject(string attributeName, AssumedTypes.IList list)
        {
            if (list == null || list.Count == 0)
            {
                return(null);
            }

            AssumedTypes.List <object> attrObjects = new OpenEhr.AssumedTypes.List <object>();
            foreach (object obj in list)
            {
                object attributeObj = CallGetAttributeObject(attributeName, obj);
                if (attributeObj != null)
                {
                    attrObjects.Add(attributeObj);
                }
            }

            if (attrObjects.Count > 1)
            {
                return(attrObjects);
            }
            if (attrObjects.Count == 1)
            {
                return(attrObjects[0]);
            }

            return(null);
        }
Ejemplo n.º 5
0
        public override object ItemAtPath(string path)
        {
            Check.Require(!string.IsNullOrEmpty(path), "Path must not be null or empty.");

            object itemInDictionary = null;
            Path   pathObject       = new Path(path);


            if (itemInDictionary == null)
            {
                throw new PathNotExistException(path);
            }

            if (itemInDictionary is AssumedTypes.IList)
            {
                AssumedTypes.IList list = itemInDictionary as AssumedTypes.IList;

                if (list.Count > 1)
                {
                    throw new PathNotUniqueException(path);
                }
                else
                {
                    return(list[0]);
                }
            }
            else
            {
                return(itemInDictionary);
            }
        }
Ejemplo n.º 6
0
        private bool IsOccurrencesValid(AssumedTypes.IList dataChildren)
        {
            Check.Require(dataChildren != null, string.Format(CommonStrings.XMustNotBeNull, "dataChildren"));

            bool result = true;

            System.Collections.Generic.List <MatchingItems> matches = new System.Collections.Generic.List <MatchingItems>();
            AcceptValidationError previousErrorDelegate             = ValidationContext.AcceptError;

            try
            {
                ValidationContext.AcceptError = null;

                foreach (CObject constraint in Children)
                {
                    if (constraint.Occurrences.Lower > 0 || !constraint.Occurrences.UpperUnbounded)
                    {
                        matches.Add(new MatchingItems(dataChildren, constraint));
                    }
                }
            }
            finally
            {
                ValidationContext.AcceptError = previousErrorDelegate;
            }

            foreach (MatchingItems match in matches)
            {
                match.RemoveItemsAlreadyMatchedByNameAndNodeId(matches);

                CObject constraint = match.Constraint;
                int     lower      = match.Lower;
                long    upper      = match.Upper;
                int     actual     = match.Count;

                if (actual < lower)
                {
                    result = false;
                    ValidationContext.AcceptValidationError(constraint, string.Format(AmValidationStrings.NotEnoughOccurrences, constraint.NodeId, lower, actual));
                }

                if (actual > upper)
                {
                    result = false;
                    ValidationContext.AcceptValidationError(constraint, string.Format(AmValidationStrings.TooManyOccurrences, constraint.NodeId, upper, actual));
                }
            }

            return(result);
        }
Ejemplo n.º 7
0
            public MatchingItems(AssumedTypes.IList dataChildren, CObject constraint) : base()
            {
                Check.Require(dataChildren != null, string.Format(CommonStrings.XMustNotBeNull, "dataChildren"));
                Check.Require(constraint != null, string.Format(CommonStrings.XMustNotBeNull, "constraint"));

                Constraint    = constraint;
                NameAttribute = NameAttributeConstraint(constraint);
                Lower         = constraint.Occurrences.Lower;
                Upper         = constraint.Occurrences.UpperUnbounded ? long.MaxValue : constraint.Occurrences.Upper;

                CArchetypeRoot archetypeRoot = constraint as CArchetypeRoot;

                NodeId = archetypeRoot != null ? archetypeRoot.ArchetypeId.Value : constraint.NodeId;

                if (!(constraint is ArchetypeSlot))
                {
                    OpenEhr.AssumedTypes.Impl.ILocatableList locatableItems = dataChildren as OpenEhr.AssumedTypes.Impl.ILocatableList;

                    if (locatableItems != null)
                    {
                        Check.Assert(!string.IsNullOrEmpty(NodeId), string.Format(CommonStrings.XMustNotBeNullOrEmpty, "NodeId"));

                        if (locatableItems.Contains(NodeId))
                        {
                            foreach (OpenEhr.RM.Common.Archetyped.Impl.Locatable locatable in (System.Collections.IEnumerable)locatableItems[NodeId])
                            {
                                if (NameAttribute == null || NameAttribute.ValidValue(locatable.Name))
                                {
                                    Add(locatable);
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (object item in dataChildren)
                        {
                            IRmType rmType = item as IRmType;

                            if (rmType != null && constraint.IsSameRmType(rmType))
                            {
                                Add(item);
                            }
                        }
                    }
                }

                Check.Ensure(Constraint == constraint);
            }
Ejemplo n.º 8
0
        private object CallGetAttributeObject(string attributeName, object obj)
        {
            AttributeDictionaryPathable pathable = obj as AttributeDictionaryPathable;

            if (pathable != null)
            {
                return(pathable.GetAttributeValue(attributeName));
            }

            AssumedTypes.IList assumedList = obj as AssumedTypes.IList;
            if (assumedList != null)
            {
                return(GetAttributeObject(attributeName, assumedList));
            }

            System.Collections.IList list = obj as System.Collections.IList;
            if (list != null)
            {
                return(GetAttributeObject(attributeName, list));
            }

            string openEhrV1AttributeName = GetOpenEhrV1AttributeName(attributeName, obj);

            System.ComponentModel.PropertyDescriptorCollection propertyDescriptorCollection =
                System.ComponentModel.TypeDescriptor.GetProperties(obj);

            System.ComponentModel.PropertyDescriptor property =
                propertyDescriptorCollection.Find(openEhrV1AttributeName, true);

            if (property == null)
            {
                property = propertyDescriptorCollection.Find(attributeName, false);
            }

            if (property == null)
            {
                return(null);
            }

            object attributeObj = property.GetValue(obj);

            return(attributeObj);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// An abstract function evaluating the obj against the ExprItem.
        /// </summary>
        /// <param name="obj">The object to be evaluated</param>
        /// <returns>Can be an object, a list of object or boolean</returns>
        internal object Evaluate(object obj)
        {
            OpenEhr.Paths.AssertionContext contextObj = new OpenEhr.Paths.AssertionContext(obj, null);

            OpenEhr.Paths.AssertionContext returnedObject = Evaluate(contextObj);
            if (returnedObject == null)
            {
                return(null);
            }

            object result = returnedObject.Data;

            AssumedTypes.IList iList = result as AssumedTypes.IList;
            if (iList != null && iList.Count == 1)
            {
                return(iList[0]);
            }

            return(result);
        }
        public override object ItemAtPath(string path)
        {
            DesignByContract.Check.Require(!string.IsNullOrEmpty(path), "Path must not be null or empty.");

            object itemInDictionary = null;

            if (this.itemAtPathDictionary != null && this.itemAtPathDictionary.ContainsKey(path))
            {
                itemInDictionary = itemAtPathDictionary[path];
            }
            else
            {
                itemInDictionary = ItemAtPathUtil(path);
            }

            if (itemInDictionary == null)
            {
                throw new PathNotExistException(path);
            }

            AssumedTypes.IList list = itemInDictionary as AssumedTypes.IList;
            if (list != null)
            {
                if (list.Count > 1)
                {
                    throw new PathNotUniqueException(path);
                }
                else
                {
                    return(list[0]);
                }
            }
            else
            {
                return(itemInDictionary);
            }
        }
Ejemplo n.º 11
0
        private bool IsOrderedChildrenValid(AssumedTypes.IList dataChildren)
        {
            Check.Require(dataChildren != null, string.Format(CommonStrings.XMustNotBeNull, "children"));

            int  n      = 0;
            bool result = true;

            foreach (object dataItem in dataChildren)
            {
                int        startingPoint  = n;
                CObject    matchedCObject = null;
                string     dataItemRmType = ((IRmType)dataItem).GetRmTypeName();
                ILocatable locatable      = dataItem as ILocatable;

                while (n < Children.Count)
                {
                    CObject eachChild = Children[n];

                    if (locatable == null || locatable.ArchetypeNodeId == eachChild.ArchetypeNodeId)
                    {
                        if (eachChild.IsSameRmType(dataItem as IRmType))
                        {
                            matchedCObject = eachChild;
                            CComplexObject complexObject = eachChild as CComplexObject;

                            if (complexObject != null)
                            {
                                CAttribute nameAttribute = NameAttributeConstraint(complexObject);

                                if (nameAttribute != null)
                                {
                                    bool nameMatched = false;
                                    AcceptValidationError previousErrorDelegate = ValidationContext.AcceptError;

                                    try
                                    {
                                        ValidationContext.AcceptError = null;
                                        nameMatched = nameAttribute.ValidValue(locatable.Name);
                                    }
                                    finally
                                    {
                                        ValidationContext.AcceptError = previousErrorDelegate;
                                    }

                                    if (nameMatched)
                                    {
                                        break;
                                    }
                                    else
                                    {
                                        n++;
                                        continue;
                                    }
                                }
                            }

                            break;
                        }
                    }

                    n++;
                }

                if (matchedCObject == null)
                {
                    n = startingPoint;
                    bool validationResult = true;

                    if (MatchedWithSlot(locatable, out validationResult))
                    {
                        result = validationResult;
                    }
                    else
                    {
                        result = false;
                        ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.YNotAllowedByAttributeXConstraint, RmAttributeName, dataItemRmType));
                    }
                }
                else if (!matchedCObject.ValidValue(dataItem))
                {
                    result = false;
                }
            }

            return(result);
        }
Ejemplo n.º 12
0
        private bool IsUnorderedChildrenValid(AssumedTypes.IList dataChildren)
        {
            Check.Require(dataChildren != null, string.Format(CommonStrings.XMustNotBeNull, "children"));

            bool result = true;

            foreach (object dataItem in dataChildren)
            {
                System.Collections.Generic.List <CObject> matchedChildren
                    = new System.Collections.Generic.List <CObject>();
                System.Collections.Generic.List <ArchetypeSlot> slots
                    = new System.Collections.Generic.List <ArchetypeSlot>();

                IRmType rmType = dataItem as IRmType;
                Check.Assert(rmType != null, string.Format(AmValidationStrings.XMustImplementY, dataItem.GetType().ToString(), "IRmType"));

                ILocatable locatable = dataItem as ILocatable;

                // get all child constraint objects with this data item's node_id
                foreach (CObject eachChild in Children)
                {
                    if (locatable == null || locatable.ArchetypeNodeId == eachChild.ArchetypeNodeId)
                    {
                        if (eachChild.IsSameRmType(rmType))
                        {
                            matchedChildren.Add(eachChild);
                        }
                    }
                }

                bool matchedWithSlot = false;

                if (matchedChildren.Count == 0)
                {
                    bool validationResult = true;
                    matchedWithSlot = MatchedWithSlot(locatable, out validationResult);

                    if (matchedWithSlot)
                    {
                        result &= validationResult;
                    }
                    else
                    {
                        // child constraint object not found for this data item
                        result = false;
                        string     errorRmTypeName   = rmType.GetRmTypeName();
                        ILocatable locatableDataItem = dataItem as ILocatable;

                        if (locatableDataItem != null)
                        {
                            ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.ItemXWithIdYNotAllowedByAttributeZ, errorRmTypeName, locatableDataItem.ArchetypeNodeId, RmAttributeName));
                        }
                        else
                        {
                            ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.ItemXNotAllowedByAttributeY, errorRmTypeName, RmAttributeName));
                        }
                    }
                }

                CObject unnamedMatchedObject = null;
                bool    validResult          = false;

                // attempt to match data item against child constraint objects with a name attribute constraint
                foreach (CObject matchedObject in matchedChildren)
                {
                    CComplexObject complexObject = matchedObject as CComplexObject;

                    if (complexObject == null)
                    {
                        throw new ApplicationException(AmValidationStrings.MultiAttributeChildNotComplexObj);
                    }

                    CAttribute nameAttribute = NameAttributeConstraint(complexObject);

                    if (nameAttribute != null)
                    {
                        bool nameAttributeFound = false;
                        AcceptValidationError previousErrorDelegate = ValidationContext.AcceptError;

                        try
                        {
                            ValidationContext.AcceptError = null;
                            nameAttributeFound            = nameAttribute.ValidValue(locatable.Name);
                        }
                        finally
                        {
                            ValidationContext.AcceptError = previousErrorDelegate;
                        }

                        if (nameAttributeFound)
                        {
                            validResult = matchedObject.ValidValue(dataItem);

                            if (validResult)
                            {
                                break;
                            }
                            else
                            {
                                result = false;
                            }
                        }
                    }
                    else
                    {
                        // keep child constraint object with no name attribute constraint for later
                        if (unnamedMatchedObject != null)
                        {
                            throw new ApplicationException(AmValidationStrings.ExpectingOnlyOneUnnamedChild);
                        }

                        unnamedMatchedObject = matchedObject;
                    }
                }

                // no matching named object constraint, so attempt to validate against unnamed object constraint
                if (!validResult && !matchedWithSlot)
                {
                    if (unnamedMatchedObject == null)
                    {
                        result = false;
                        ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.NotAllowedByAttributeXConstraint, RmAttributeName));
                    }
                    else if (!unnamedMatchedObject.ValidValue(dataItem))
                    {
                        result = false;
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 13
0
        private AssertionContext ProcessPathPartWithWildcardForArId(AssertionContext contextObj, PathStep pathStep)
        {
            DesignByContract.Check.Require(pathStep.Attribute == "//*", "anyAttribute value must be //*.");

            Locatable locatable = contextObj.Data as Locatable;

            if (locatable != null)
            {
                ArchetypedPathProcessor archetypePathProcessor = new ArchetypedPathProcessor(locatable);
                string archetypePathWithWildcardKey            = null;
                if (!string.IsNullOrEmpty(pathStep.ArchetypeNodeId))
                {
                    archetypePathWithWildcardKey = pathStep.Attribute + "[" + pathStep.ArchetypeNodeId + "]";
                }
                else if (!string.IsNullOrEmpty(pathStep.NodePattern))
                {
                    archetypePathWithWildcardKey = pathStep.Attribute + "[{/" + pathStep.NodePattern + "/}]";
                }
                else
                {
                    throw new NotSupportedException(pathStep.Value + " path not supported");
                }
                object obj = null;
                if (!archetypePathProcessor.PathExists(archetypePathWithWildcardKey))
                {
                    return(null);
                }

                if (archetypePathProcessor.PathUnique(archetypePathWithWildcardKey))
                {
                    obj = archetypePathProcessor.ItemAtPath(archetypePathWithWildcardKey);
                }
                else
                {
                    obj = archetypePathProcessor.ItemsAtPath(archetypePathWithWildcardKey);
                }

                if (obj == null)
                {
                    throw new ApplicationException("obj must not be null.");
                }

                return(new AssertionContext(obj, contextObj));
            }

            AssumedTypes.IList ilist = contextObj.Data as AssumedTypes.IList;
            if (ilist == null)
            {
                throw new ApplicationException("only support either locatable or ilist");
            }
            AssumedTypes.List <object> results = new OpenEhr.AssumedTypes.List <object>();
            foreach (Locatable locatableItem in ilist)
            {
                AssertionContext assertionContext = new AssertionContext(locatableItem, contextObj);
                AssertionContext result           = ProcessPathPartWithWildcardForArId(assertionContext, pathStep);
                if (result != null && result.Data != null)
                {
                    results.Add(result.Data);
                }
            }

            if (results.Count > 0)
            {
                return(new AssertionContext(results, contextObj));
            }

            return(null);
        }
Ejemplo n.º 14
0
        internal AssertionContext Evaluate(AssertionContext contextObj)
        {
            if (contextObj == null)
            {
                return(null);
            }

            AssumedTypes.IList assumedIList = contextObj.Data as AssumedTypes.IList;
            if (assumedIList != null)
            {
                AssumedTypes.List <object> results = new OpenEhr.AssumedTypes.List <object>();
                foreach (object obj in assumedIList)
                {
                    AssertionContext assertObj      = new AssertionContext(obj, contextObj);
                    AssertionContext evaluateResult = Evaluate(assertObj);
                    if (evaluateResult != null)
                    {
                        AssumedTypes.List <object> aList = evaluateResult.Data as AssumedTypes.List <object>;
                        if (aList == null)
                        {
                            results.Add(evaluateResult.Data);
                        }
                        else
                        {
                            foreach (object o in aList)
                            {
                                results.Add(o);
                            }
                        }
                    }
                }

                if (results.Count == 0)
                {
                    return(null);
                }

                return(new AssertionContext(results, contextObj));
            }

            System.Collections.IList list = contextObj.Data as System.Collections.IList;
            if (list == null)
            {
                return(EvaluateSingleAttri(contextObj));
            }
            else
            {
                AssumedTypes.List <object> results = new AssumedTypes.List <object>();
                foreach (object obj in list)
                {
                    AssertionContext assertObj      = new AssertionContext(obj, contextObj);
                    AssertionContext evaluateResult = Evaluate(assertObj);
                    if (evaluateResult != null)
                    {
                        results.Add(evaluateResult.Data);
                    }
                }

                if (results.Count == 0)
                {
                    return(null);
                }

                return(new AssertionContext(results, contextObj));
            }
        }