private CREFModel.ClinicalAssertion TranslateClinicalAssertion(TranslationContext context, Node node)
		{
			// node is expected to be an ActionGroup
			// Action types determine the assertion to be created
				// CreateAction - MissingDataAssertion
				// UpdateAction - MissingDataAssertion
				// MessageAction - OutOfRangeAssertion
				// CollectInformationAction - MissingDataAssertion
				// Any other action cannot be represented

			var assertions = new List<CREFModel.ClinicalAssertion>();

			var subElements = node.Children.FirstOrDefault(c => c.Name == "subElements");

			if (subElements != null)
			{
				foreach (var element in subElements.Children)
				{
					if (element.Name == "simpleAction")
					{
						switch (element.NodeType.GetLocalName())
						{
							case "CreateAction" : assertions.Add(TranslateCreateAction(context, element)); break;
							case "UpdateAction" : assertions.Add(TranslateUpdateAction(context, element)); break;
							case "MessageAction" : assertions.Add(TranslateMessageAction(context, element)); break;
							case "CollectInformationAction" : assertions.Add(TranslateCollectInformationAction(context, element)); break;
							default: throw new NotSupportedException(String.Format("Translation of {0} actions is not supported.", element.NodeType.GetLocalName()));
						}
					}
					else if (element.Name == "actionGroup")
					{
						assertions.Add(TranslateClinicalAssertion(context, element));
					}
					else if (element.Name == "actionGroupReference")
					{
						throw new NotSupportedException("Translation of action group references is not supported.");
					}
				}
			}

			if (assertions.Count > 1)
			{
				var compositeAssertion = new CREFModel.CompositeAssertion();

				compositeAssertion.CompositionType = GetCompositionType(GetGroupSelectionBehavior(node));

				var descriptionNode = node.Children.FirstOrDefault(c => c.Name == "Description");
				if (descriptionNode != null)
				{
					compositeAssertion.Description = descriptionNode.GetAttribute<string>("value");
				}

				compositeAssertion.CompositeAssertionAssertions.AddRange(assertions);

				return compositeAssertion;
			}

			return assertions[0];
		}
		private CREFModel.DynamicRuleDynamicRuleAssertion TranslateAssertion(TranslationContext context, Node node)
		{
			var result = new CREFModel.DynamicRuleDynamicRuleAssertion();

			result.Item = TranslateClinicalAssertion(context, node);

			return result;
		}
Ejemplo n.º 3
0
		public static Node ReadNode(XElement element)
		{
			var nodeName = element.Name.LocalName;
			var nodeType = element.GetSchemaInfo().SchemaType;

			Node result = null;
			ASTNode astResult = null;

			if (IsExpression(nodeType))
			{
				astResult = new ASTNode();
				result = astResult;
			}
			else
			{
				result = new Node();
			}

			result.Name = nodeName;
			result.NodeType = nodeType.QualifiedName.ToString();
			var lineInfo = element as IXmlLineInfo;
			if (lineInfo != null && lineInfo.HasLineInfo())
			{
				result.Line = lineInfo.LineNumber;
				result.LinePos = lineInfo.LinePosition;
			}

			result.Attributes = new Dictionary<string, object>();
			foreach (var a in element.Attributes())
			{
				var schemaInfo = a.GetSchemaInfo();
				if (schemaInfo != null && schemaInfo.SchemaType != null && schemaInfo.SchemaType.QualifiedName != null && schemaInfo.SchemaType.QualifiedName.Name == "QName")
				{
					result.Attributes.Add(a.Name.LocalName, element.ExpandName(a.Value));
				}
				else
				{
					result.Attributes.Add(a.Name.LocalName, a.Value);
				}
			}

			result.Children = new List<Node>();
			foreach (var e in element.Elements())
			{
				if (e.Name.LocalName == "description" && astResult != null)
				{
					astResult.Description = e.Value;
				}
				else
				{
					result.Children.Add(ReadNode(e));
				}
			}

			return result;
		}
        public void ReportMessage(Exception e, Node node)
        {
            var message = e as VerificationException;

            if (message == null)
            {
                message = new VerificationException(node.FormatErrorMessage(e.Message), e);
            }

            _messages.Add(message);
        }
Ejemplo n.º 5
0
        private Symbol ProcessAliasedQuerySource(VerificationContext context, HeD.Engine.Model.Node source)
        {
            var sourceAlias      = source.GetAttribute <String>("alias");
            var sourceExpression = source.Children[0] as ASTNode;

            if (sourceExpression == null)
            {
                throw new InvalidOperationException(String.Format("Could not determine source expression for alias '{0}'.", sourceAlias));
            }

            Verifier.Verify(context, sourceExpression);
            return(new Symbol(sourceAlias, sourceExpression.ResultType));
        }
		private void VerifyExpressionNodes(VerificationContext context, Node node)
		{
			var astNode = node as ASTNode;
			if (astNode != null)
			{
				Verifier.Verify(context, astNode);
			}
			else
			{
				foreach (var child in node.Children)
				{
					VerifyExpressionNodes(context, child);
				}
			}
		}
		private void TranslateBaseClinicalAssertion(TranslationContext context, Node node, CREFModel.ClinicalAssertion assertion)
		{
			var actionIdNode = node.Children.FirstOrDefault(c => c.Name == "actionId");
			if (actionIdNode != null)
			{
				assertion.ID = actionIdNode.GetAttribute<string>("root") + ':' + actionIdNode.GetAttribute<string>("extension", "");
			}

			var textEquivalentNode = node.Children.FirstOrDefault(c => c.Name == "textEquivalent");
			if (textEquivalentNode != null)
			{
				assertion.Description = textEquivalentNode.GetAttribute<string>("value");
			}
		}
Ejemplo n.º 8
0
 private SQLModel.OrderClause TranslateSortClause(TranslationContext context, Node node)
 {
     throw new NotImplementedException();
 }
		private CREFModel.ClinicalAssertion TranslateMessageAction(TranslationContext context, Node node)
		{
			var result = new CREFModel.OutOfRangeAssertion();
			TranslateBaseClinicalAssertion(context, node, result);

			return result;
		}
Ejemplo n.º 10
0
 private Model.Expression TranslateRelationshipClause(TranslationContext context, Node relationship)
 {
     var relatedTableSpecifier = TranslateAliasedQuerySource(context, relationship);
     var relatedConditionNode = (ASTNode)relationship.Children.Where(n => n.Name == "suchThat").Single();
     var relatedCondition = (Model.Expression)context.TranslateNode(relatedConditionNode);
     var relatedSelect = new SQLModel.SelectExpression();
     relatedSelect.SelectClause = new SQLModel.SelectClause();
     relatedSelect.SelectClause.NonProject = true;
     relatedSelect.SelectClause.Distinct = false;
     relatedSelect.FromClause = new SQLModel.CalculusFromClause(relatedTableSpecifier);
     relatedSelect.WhereClause = new SQLModel.WhereClause(relatedCondition);
     var relatedExists = new Model.UnaryExpression("iExists", relatedSelect);
     if (relationship.NodeType.GetLocalName() == "Without")
     {
         return new Model.UnaryExpression("iNot", relatedExists);
     }
     else
     {
         return relatedExists;
     }
 }
Ejemplo n.º 11
0
        private SQLModel.SelectClause TranslateReturnClause(TranslationContext context, Node returnClause)
        {
            var result = new SQLModel.SelectClause();
            result.Distinct = returnClause.GetAttribute<Boolean>("distinct", true);
            var expression = returnClause.Children.Single(n => n.Name == "expression");
            if (expression.NodeType.GetLocalName() == "Tuple")
            {
                foreach (var element in expression.Children)
                {
                    var column =
                        new SQLModel.ColumnExpression
                        (
                            (Model.Expression)context.TranslateNode((ASTNode)element.Children.Single(n => n.Name == "value")),
                            element.GetAttribute<String>("name")
                        );
                }
            }

            return result;
        }
Ejemplo n.º 12
0
        private void VerifyResponseBindings(VerificationContext context, Node node, Dictionary<string, ParameterDef> containers)
        {
            // foreach action
                // If DeclareResponseAction
                    // Create a parameter with that name
                // If CollectInformationAction
                    // Create a property on the appropriate parameter with the correct type
                        // Name = responseBinding.property
                        // Type = responseCardinality = Single ? responseDataType : List<responseDataType>

            try
            {
                switch (node.NodeType.GetLocalName())
                {
                    case "DeclareResponseAction" :
                    {
                        var containerName = node.GetAttribute<string>("name");
                        var container = new ParameterDef { Name = containerName, ParameterType = new ObjectType(containerName + "Type", new List<PropertyDef> { }) };
                        containers.Add(container.Name, container);
                    }
                    break;

                    case "CollectInformationAction" :
                    {
                        var responseBinding = node.Children.FirstOrDefault(c => c.Name == "responseBinding");
                        if (responseBinding != null)
                        {
                            var containerName = responseBinding.GetAttribute<string>("container");
                            if (String.IsNullOrEmpty(containerName))
                            {
                                containerName = "Responses";
                            }

                            if (!containers.ContainsKey(containerName))
                            {
                                throw new InvalidOperationException(String.Format("Could not resolve response container name {0}.", containerName));
                            }

                            var container = containers[containerName];
                            var containerType = (ObjectType)container.ParameterType;

                            DataType responseType = null;
                            var responseName = responseBinding.GetAttribute<string>("property");
                            var documentationConcept = node.Children.FirstOrDefault(c => c.Name == "documentationConcept");
                            if (documentationConcept != null)
                            {
                                var responseDataType = documentationConcept.Children.FirstOrDefault(c => c.Name == "responseDataType");
                                if (responseDataType != null)
                                {
                                    responseType = context.ResolveType(responseDataType.GetAttribute<string>("value"));
                                }
                                else
                                {
                                    responseType = DataTypes.String;
                                }

                                var responseCardinality = documentationConcept.Children.FirstOrDefault(c => c.Name == "responseCardinality");
                                if (responseCardinality != null)
                                {
                                    if (responseCardinality.GetAttribute<string>("value") == "Multiple")
                                    {
                                        responseType = new ListType(responseType);
                                    }
                                }
                            }

                            if (responseType == null)
                            {
                                responseType = DataTypes.String;
                            }

                            if (containerType.Properties.FirstOrDefault(pd => pd.Name == responseName) != null)
                            {
                                throw new InvalidOperationException(String.Format("The response container {0} already has a response named {1}.", container.Name, responseName));
                            }

                            containerType.Properties.Add(new PropertyDef(responseName, responseType));
                        }
                    }
                    break;
                }
            }
            catch (Exception e)
            {
                context.ReportMessage(e, node);
            }

            foreach (var child in node.Children)
            {
                VerifyResponseBindings(context, child, containers);
            }
        }
Ejemplo n.º 13
0
 private SQLModel.TableSpecifier TranslateAliasedQuerySource(TranslationContext context, Node source)
 {
     var expression = (ASTNode)source.Children.Where(n => n.Name == "expression").Single();
     var alias = source.GetAttribute<String>("alias");
     var sqlContext = (SQLTranslationContext)context;
     sqlContext.PushQueryContext();
     try
     {
         return new SQLModel.TableSpecifier(SQLTranslationUtilities.EnsureExpression(context.TranslateNode(expression)), alias);
     }
     finally
     {
         sqlContext.PopQueryContext();
     }
 }
Ejemplo n.º 14
0
		private void TranslateActionSentence(TranslationContext context, Node node, CREFModel.MissingDataAssertion result)
		{
			// If we assert that the source artifact can only use specific types of expressions, and that the expressions must be literal (evaluable at compile-time)
			// then we can translate the basic aspects of the assertion, code, code system, and severity.
			var proposalExpression = node.Children.FirstOrDefault(c => c.Name == "actionSentence") as ASTNode;
			if (proposalExpression != null)
			{
				switch (proposalExpression.ResultType.Name)
				{
					case "SubstanceAdministrationProposal" :
					case "SubstanceAdministrationEvent" :
					{
						var substanceCode = ((Node)proposalExpression).Children.FirstOrDefault(c => c.Name == "property" && c.Attributes.ContainsKey("name") && c.GetAttribute<string>("name") == "substance.substanceCode");
						if (substanceCode != null)
						{
							var codeValue = substanceCode.Children.FirstOrDefault(c => c.Name == "value" && c.NodeType.GetLocalName() == "CodeLiteral");
							if (codeValue != null)
							{
								result.Code = codeValue.GetAttribute<string>("code");
								result.CodeSet = codeValue.GetAttribute<string>("codeSystem");
							}
						}
					}
					break;
				}
			}
		}
Ejemplo n.º 15
0
		private CREFModel.ClinicalAssertion TranslateCollectInformationAction(TranslationContext context, Node node)
		{
			var result = new CREFModel.MissingDataAssertion();
			TranslateBaseClinicalAssertion(context, node, result);

			var documentationConcept = node.Children.FirstOrDefault(c => c.Name == "documentationConcept");
			if (documentationConcept != null)
			{
				var itemCodes = documentationConcept.Children.FirstOrDefault(c => c.Name == "itemCodes");
				if (itemCodes != null)
				{
					var itemCode = itemCodes.Children.FirstOrDefault(c => c.Name == "itemCode");
					if (itemCode != null)
					{
						result.Code = itemCode.GetAttribute<string>("code");
						result.CodeSet = itemCode.GetAttribute<string>("codeSystemName");
					}
				}
			}

			return result;
		}
Ejemplo n.º 16
0
		private CREFModel.ClinicalAssertion TranslateCreateAction(TranslationContext context, Node node)
		{
			var result = new CREFModel.MissingDataAssertion();
			TranslateBaseClinicalAssertion(context, node, result);

			// TODO: Translate the action sentence
				// In general, this will be extremely difficult because it involves figuring out what object type and code
				// is created as part of the resulting guidance, and then using that to set the code on the recommendation.
				// However, it is possible in HeD to actually set the code based on data provided as part of the evaluation.
				// In this case, it is not possible to determine the code to be returned. Manual translation would have to
				// be performed in that case.

				// Another option for translation is to evaluate the action and then just read the code from the appropriate 
				// property on the resulting object.

			TranslateActionSentence(context, node, result);

			return result;
		}
Ejemplo n.º 17
0
		private CREFModel.ClinicalAssertion TranslateUpdateAction(TranslationContext context, Node node)
		{
			var result = new CREFModel.MissingDataAssertion();
			TranslateBaseClinicalAssertion(context, node, result);

			// TODO: Translate the action sentence
				// See the related comment in TranslateCreateAction

			TranslateActionSentence(context, node, result);

			return result;
		}
Ejemplo n.º 18
0
		private string GetGroupSelectionBehavior(Node node)
		{
			var behaviorsNode = node.Children.FirstOrDefault(c => c.Name == "behaviors");
			if (behaviorsNode != null)
			{
				var behaviorNode = node.Children.FirstOrDefault(c => c.Name == "behavior" && c.NodeType.GetLocalName() == "GroupSelectionBehavior");
				if (behaviorNode != null)
				{
					return node.GetAttribute<string>("value");
				}
			}

			return "All";
		}