public override bool ValidValue(object aValue) { Check.Require(aValue != null, string.Format(CommonStrings.XMustNotBeNull, "aValue")); CComplexObject rootDefinition = AmFactory.GetRootDefinition(this); CObject cObjAtTargetPath = Archetype.GetCObjectAtTargetPath(rootDefinition, this.TargetPath); return(cObjAtTargetPath.ValidValue(aValue)); }
public Archetype(ArchetypeId archetypeId, string concept, CComplexObject definition, ArchetypeOntology ontology, CodePhrase originalLanguage, RevisionHistory revisionHistory, bool isControlled) : base(originalLanguage, revisionHistory, isControlled) { Check.Require(archetypeId != null, string.Format(CommonStrings.XMustNotBeNull, "archetypeId")); Check.Require(!string.IsNullOrEmpty(concept) != null, string.Format(CommonStrings.XMustNotBeNullOrEmpty, "concept")); Check.Require(definition != null, string.Format(CommonStrings.XMustNotBeNull, "definition")); Check.Require(ontology != null, string.Format(CommonStrings.XMustNotBeNull, "ontology")); this.archetypeId = archetypeId; this.definition = definition; this.ontology = ontology; }
protected override System.Collections.Generic.List <string> GetPhysicalPaths() { CComplexObject rootDefinition = AmFactory.GetRootDefinition(this); CObject cObjAtTargetPath = Archetype.GetCObjectAtTargetPath(rootDefinition, this.TargetPath); if (cObjAtTargetPath == null) { throw new ApplicationException(string.Format( AmValidationStrings.NoNodeMatchAtPath, this.TargetPath)); } return(cObjAtTargetPath.PhysicalPaths); }
static private CAttribute NameAttributeConstraint(CObject objectConstraint) { Check.Require(objectConstraint != null, string.Format(CommonStrings.XMustNotBeNull, "objectConstraint")); CComplexObject cComplexObject = objectConstraint as CComplexObject; if (cComplexObject != null && cComplexObject.Attributes != null) { foreach (CAttribute attribute in cComplexObject.Attributes) { if (attribute.RmAttributeName == "name") { return(attribute); } } } return(null); }
internal static CObject CObject(string typeName) { DesignByContract.Check.Require(!string.IsNullOrEmpty(typeName), string.Format(CommonStrings.XMustNotBeNullOrEmpty, "typeName")); CObject cObject = null; switch (typeName) { case "C_COMPLEX_OBJECT": cObject = new CComplexObject(); break; case "C_PRIMITIVE_OBJECT": cObject = new CPrimitiveObject(); break; case "ARCHETYPE_INTERNAL_REF": cObject = new ArchetypeInternalRef(); break; case "CONSTRAINT_REF": cObject = new ConstraintRef(); break; case "ARCHETYPE_SLOT": cObject = new ArchetypeSlot(); break; case "C_CODE_PHRASE": cObject = new CCodePhrase(); break; case "C_DV_STATE": cObject = new CDvState(); break; case "C_DV_ORDINAL": cObject = new CDvOrdinal(); break; case "C_DV_QUANTITY": cObject = new CDvQuantity(); break; default: throw new NotSupportedException("type not supported: " + typeName); } DesignByContract.Check.Ensure(cObject != null, "cObject must not be null."); return cObject; }
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); }
void WriteCComplexObject(XmlWriter writer, CComplexObject cComplexObject) { amSerializer.WriteCObject(writer, cComplexObject); string openEhrPrefix = UseOpenEhrPrefix(writer); if (cComplexObject.Attributes != null && cComplexObject.Attributes.Count > 0) { foreach(CAttribute attribute in cComplexObject.Attributes) { string typeAttribute = AmType.GetName(attribute); if (!string.IsNullOrEmpty(openEhrPrefix)) typeAttribute = string.Format("{0}:{1}", openEhrPrefix, typeAttribute); writer.WriteStartElement(openEhrPrefix, "attributes", OpenEhrNamespace); writer.WriteAttributeString( UseXsiPrefix(writer), "type", XsiNamespace, typeAttribute); if (AmType.GetName(attribute) == "C_MULTIPLE_ATTRIBUTE") WriteCMulitpleAttribute(writer, attribute); else WriteCSingleAttribute(writer, attribute); writer.WriteEndElement(); } } }
protected void Validate(CComplexObject cComplexObject) { this.Validate((CDefinedObject)cComplexObject); if (cComplexObject.Attributes != null) { foreach (CAttribute attri in cComplexObject.Attributes) this.Validate(attri); } Invariant(cComplexObject.AnyAllowed() || (cComplexObject.Attributes != null && !cComplexObject.Attributes.IsEmpty()), AmValidationStrings.CComplexObjectAllowAnyXor); }
internal static string GetNameConstraint(CComplexObject cComplexObject) { var name = string.Empty; if (cComplexObject.Attributes == null) return name; foreach (CAttribute attribute in cComplexObject.Attributes) { if (attribute.RmAttributeName != "name" || attribute.Children.Count <= 0) continue; var primativeObject = attribute.Children[0] as CPrimitiveObject; if (primativeObject == null) continue; var cString = primativeObject.Item as CString; if (cString == null || cString.List.Count <= 0) continue; name = cString.List[0]; break; } return name; }
/// <summary> /// returns true if all Nodeids valid for a CComplex object /// </summary> /// <param name="cComplexObj"></param> /// <returns></returns> private bool NodeIdsValid(CComplexObject cComplexObj) { if (cComplexObj.Attributes != null) { foreach (CAttribute attribute in cComplexObj.Attributes) { if (!NodeIdsValid(attribute)) return false; } } return true; }
internal static void PopulateLocatableAttributes(CComplexObject cComplexObject, OpenEhr.RM.Common.Archetyped.Impl.Locatable locatable) { Check.Require(locatable != null, "locatable must not be null."); Check.Require(cComplexObject != null, "cComplexObject must not be null."); string codeString = null; if (string.IsNullOrEmpty(locatable.ArchetypeNodeId)) { CArchetypeRoot archetypeRoot = cComplexObject as CArchetypeRoot; if (archetypeRoot != null) { locatable.ArchetypeNodeId = archetypeRoot.ArchetypeId.Value; codeString = cComplexObject.NodeId; } else { locatable.ArchetypeNodeId = cComplexObject.NodeId; codeString = cComplexObject.NodeId; } } if (locatable.Name == null || string.IsNullOrEmpty(locatable.Name.Value)) { if (string.IsNullOrEmpty(codeString)) codeString = cComplexObject.NodeId; locatable.Name = new DvText(LocalTermDefText(codeString, cComplexObject)); } Check.Ensure(!string.IsNullOrEmpty(locatable.ArchetypeNodeId), "ArchetypeId must not be null or empty."); Check.Ensure(locatable.Name != null && !string.IsNullOrEmpty(locatable.Name.Value), "name must not be null."); }
public static CAttribute GetAttribute(CComplexObject objConstraint, string attributeName) { return objConstraint.GetAttribute(attributeName); }
public CObject ConstraintAtPath(string path) { if (string.IsNullOrEmpty(path)) { return(this); } if (constraintAtPath.Contains(path)) { return(constraintAtPath[path] as CObject); } CObject constraintFound = null; ConstraintPath constraintPath = new ConstraintPath(path); if (path.StartsWith("/") && this.Parent != null) { ArchetypeConstraint rootConstraint = this.ConstraintParent; while (rootConstraint.ConstraintParent != null) { rootConstraint = rootConstraint.ConstraintParent; } CComplexObject cComplexObject = rootConstraint as CComplexObject; if (cComplexObject == null) { throw new ArgumentException(AmValidationStrings.RootConstraintInvalid); } constraintFound = cComplexObject.ConstraintAtPath(path); } else { foreach (CAttribute attribute in this.attributes) { if (attribute.RmAttributeName == constraintPath.FirstStepAttributeName) { System.Collections.Generic.IList <CObject> matchedChildren = new System.Collections.Generic.List <CObject>(); foreach (CObject cObject in attribute.Children) { if (cObject.NodeId == constraintPath.FirstStepNodeId) { matchedChildren.Add(cObject); } else if (cObject is CArchetypeRoot && cObject.ArchetypeNodeId == constraintPath.FirstStepNodeId) { matchedChildren.Add(cObject); } } if (matchedChildren.Count <= 0) { throw new ArgumentException(string.Format(AmValidationStrings. MissingChildrenWithNodeIdX, constraintPath.FirstStepNodeId)); } else if (matchedChildren.Count == 1) { constraintFound = matchedChildren[0]; } else if (!constraintPath.HasNameConstraint()) { throw new ArgumentException(string.Format(AmValidationStrings.PathYNotUniqueAtX, constraintPath.ToString(), constraintPath.FirstStepNodeId)); } else { DvText name = (!string.IsNullOrEmpty(constraintPath.FirstStepNameValue) ? new DvText(constraintPath.FirstStepNameValue) : new DvCodedText(constraintPath.FirstStepName)); foreach (CObject cObject in matchedChildren) { if (CMultipleAttribute.HasNameAttributeConstraint(cObject, name)) { constraintFound = cObject; break; } } } break; } } } if (constraintFound == null) { throw new ArgumentException(string.Format(AmValidationStrings.NoConstraintForPathX, path)); } CComplexObject complexObject = constraintFound as CComplexObject; if (complexObject != null) { constraintFound = complexObject.ConstraintAtPath(constraintPath.NextSteps); } this.constraintAtPath.Add(path, constraintFound); Check.Ensure(constraintFound != null); return(constraintFound); }
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); }
private void ReadXml(CComplexObject cComplexObject) { DesignByContract.Check.Require(cComplexObject != null, string.Format(CommonStrings.XMustNotBeNull, "cComplexObject")); reader.ReadStartElement(); reader.MoveToContent(); this.ReadXmlBase((CObject)cComplexObject); if (reader.LocalName == "attributes") { System.Collections.Generic.List<CAttribute> attrList = new System.Collections.Generic.List<CAttribute>(); do { string attributeType = reader.GetAttribute("type", RmXmlSerializer.XsiNamespace); DesignByContract.Check.Assert(!string.IsNullOrEmpty(attributeType), "attributeType must not be null or empty."); CAttribute attri = AmFactory.CAttribute(attributeType); attri.parent = cComplexObject; this.ReadXml(attri); attrList.Add(attri); } while (reader.LocalName == "attributes" && reader.NodeType != XmlNodeType.EndElement); DesignByContract.Check.Assert(attrList.Count > 0, "attrList must not be empty."); cComplexObject.Attributes = new OpenEhr.AssumedTypes.Set<CAttribute>(attrList); } DesignByContract.Check.Assert(reader.NodeType == System.Xml.XmlNodeType.EndElement, "Expected endElement of CComplextObject"); reader.ReadEndElement(); reader.MoveToContent(); this.archetype.ConstraintRepository.Add(cComplexObject.Path, cComplexObject); }
private void WriteXml(CComplexObject cComplexObj) { Check.Require(cComplexObj != null, string.Format(CommonStrings.XMustNotBeNull, "cComplexObj")); this.WriteXmlBase((CObject)cComplexObj); if (cComplexObj.Attributes != null) { foreach (CAttribute attr in cComplexObj.Attributes) { writer.WriteStartElement(UseOpenEhrPrefix(writer), "attributes", OpenEhrNamespace); string attributeType = AmType.GetName(attr); if (!string.IsNullOrEmpty(UseOpenEhrPrefix(writer))) attributeType = UseOpenEhrPrefix(writer) + ":" + attributeType; writer.WriteAttributeString(UseXsiPrefix(writer), "type", XsiNamespace, attributeType); this.WriteXml(attr); writer.WriteEndElement(); } } }
public void ReadComplexObject(XmlReader reader, CComplexObject cObject) { this.reader = reader; ReadXmlBase(cObject); }
private static CObject GetCObjectByAttributeName(CComplexObject cComplexObject, string attributeName) { foreach (CAttribute attribute in cComplexObject.Attributes) { if (attribute.RmAttributeName == attributeName) return attribute.Children[0]; } return null; }
private static string GetName(CComplexObject cComplexObject) { CComplexObject nameAttribute = GetCObjectByAttributeName(cComplexObject, "name") as CComplexObject; if (nameAttribute != null) { CPrimitiveObject cPrimativeObject = GetCObjectByAttributeName(nameAttribute, "value") as CPrimitiveObject; Check.Assert(cPrimativeObject != null); CString cString = cPrimativeObject.Item as CString; Check.Assert(cString != null); foreach(string name in cString.List) return name; } return string.Empty; }
public static CAttribute GetAttribute(CComplexObject objConstraint, string attributeName) { return(objConstraint.GetAttribute(attributeName)); }
/// <summary> /// an internal static method returning the targetPath corresponding CObject. /// Returns null if the targetPath doesn't have any associated CObject /// </summary> /// <param name="archetypeDefinition"></param> /// <param name="targetPath"></param> /// <returns></returns> internal static CObject GetCObjectAtTargetPath(CComplexObject archetypeDefinition, string targetPath) { CComplexObject cObj = archetypeDefinition; CAttribute attribute = null; Path pathProcessor = new Path(targetPath); do { foreach (CAttribute cAttri in cObj.Attributes) { if (cAttri.RmAttributeName == pathProcessor.CurrentAttribute) { attribute = cAttri; break; } } Check.Assert(attribute != null, string.Format(CommonStrings.XMustNotBeNull, "attribute")); if (attribute.RmAttributeName != pathProcessor.CurrentAttribute) return null; foreach (CObject obj in attribute.Children) { if (obj.NodeId == pathProcessor.CurrentNodeId) { cObj = obj as CComplexObject; break; } } Check.Assert(cObj != null, string.Format(CommonStrings.XMustNotBeNull, "cObj")); if (cObj.NodeId != pathProcessor.CurrentNodeId) return null; } while (pathProcessor.NextStep()); Check.Ensure(cObj.Path == targetPath, "cObj.Path must be the same as this.TargetPath"); return cObj; }