/// <summary> /// Evaluates the request and returns a response. /// </summary> /// <param name="input">The input request.</param> /// <returns>The result of evaluating the given input.</returns> public evaluateResponse Evaluate(evaluate input) { var inputScopingEntityId = input.interactionId.scopingEntityId; if (input.interactionId.scopingEntityId != "SAMPLE-CLIENT") { throw new DSSExceptionWrapper ( new UnrecognizedScopingEntityException { errorMessage = new List <string> { "Unknown client" }, scopingEntityId = input.interactionId.scopingEntityId } ); } if (input.evaluationRequest.dataRequirementItemData.Count > 1) { throw new ArgumentOutOfRangeException("Example service implementation can only process requests with a single data requirement item container."); } var dri = input.evaluationRequest.dataRequirementItemData[0]; if (!SemanticSignifiers.AreEqual(dri.data.informationModelSSId, SemanticSignifiers.CDSInputId)) { throw new DSSExceptionWrapper ( new UnrecognizedScopedEntityException { errorMessage = new List <string> { "Unknown semantic signifier. Semantic signifier must be 'org.hl7.cds', 'cdss:r1', '1.0'" }, entityType = EntityType.SEMANTIC_SIGNIFIER, entityId = dri.data.informationModelSSId } ); } var payload = Packager.DecodeRequestPayload(dri.data.base64EncodedPayload); EngineResponse engineResponse = null; try { // Pass payload to rule engine for evaluation here... // NOTE: This could be done a per KM basis as well, depends on whether the engine // is aggregating results, or keeping results separate. engineResponse = GetSampleEngineResponse(payload); } catch (CDSExecutionMessageWrapper m) { // Catch any execution exceptions and convert them to DSS exceptions throw new DSSExceptionWrapper(m.ExecutionMessage.ToDSSException()); } // Package the engine's response in a DSS evaluate response. return (new evaluateResponse { requestId = input.interactionId, responseId = new InteractionIdentifier { scopingEntityId = "org.hl7.cds", interactionId = Guid.NewGuid().ToString("N"), submissionTime = DateTime.Now }, evaluationResponse = new EvaluationResponse { finalKMEvaluationResponse = new List <FinalKMEvaluationResponse> { new FinalKMEvaluationResponse { kmEvaluationResultData = new List <KMEvaluationResultData> { new KMEvaluationResultData { evaluationResultId = new ItemIdentifier { itemId = Guid.NewGuid().ToString("N") }, data = new SemanticPayload { informationModelSSId = SemanticSignifiers.CDSActionGroupResponseId, base64EncodedPayload = Packager.EncodeActionGroupResponsePayload((CDSActionGroupResponse)engineResponse.Response) } } }, kmId = new EntityIdentifier { businessId = Guid.NewGuid().ToString("N"), scopingEntityId = inputScopingEntityId, version = "1" }, warning = ( from m in engineResponse.Messages select new Warning { value = new SemanticPayload { informationModelSSId = SemanticSignifiers.CDSExecutionMessageId, base64EncodedPayload = Packager.EncodeExecutionMessagePayload(m) } } ).ToList() } } } }); }
static void Main(string[] args) { // Prepare for a simple HttpPost request var client = new HttpClient(); client.BaseAddress = new Uri("http://localhost:61924/"); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml")); // Get the body of the request var vMRRequest = GetSampleRequest(); // Construct the DSS-level request, base-64 encoding the vMR request within it. var evaluate = new evaluate { interactionId = new InteractionIdentifier { interactionId = Guid.NewGuid().ToString("N"), scopingEntityId = "SAMPLE-CLIENT", submissionTime = DateTime.Now.ToUniversalTime() }, evaluationRequest = new EvaluationRequest { clientLanguage = "XXX", clientTimeZoneOffset = "XXX", dataRequirementItemData = new List <DataRequirementItemData> { new DataRequirementItemData { driId = new ItemIdentifier { itemId = "RequiredDataId" }, data = new SemanticPayload { informationModelSSId = SemanticSignifiers.CDSInputId, base64EncodedPayload = Packager.EncodeRequestPayload(vMRRequest) } } }, kmEvaluationRequest = new List <KMEvaluationRequest> { new KMEvaluationRequest { kmId = new EntityIdentifier { scopingEntityId = "org.hl7.cds", businessId = "NQF-0068", version = "1.0" } } } } }; // Post the request and retrieve the response var response = client.PostAsXmlAsync("api/evaluation", evaluate).Result; if (response.IsSuccessStatusCode) { Console.WriteLine("Evaluation succeeded."); var evaluateResponse = response.Content.ReadAsAsync <evaluateResponse>().Result; var evaluationResponse = evaluateResponse.evaluationResponse.finalKMEvaluationResponse.First(); if (evaluationResponse.kmId.scopingEntityId != "SAMPLE-CLIENT") { Console.WriteLine("Evaluation did not return the input scoping entity Id."); } var evaluationResult = evaluationResponse.kmEvaluationResultData.First(); if (!SemanticSignifiers.AreEqual(evaluationResult.data.informationModelSSId, SemanticSignifiers.CDSActionGroupResponseId)) { Console.WriteLine("Evaluation did not return an action group response."); } var actionGroupResponse = Packager.DecodeActionGroupResponsePayload(evaluationResult.data.base64EncodedPayload); var createAction = actionGroupResponse.actionGroup.subElements.Items.First() as CreateAction; if (createAction == null) { Console.WriteLine("Result does not include a CreateAction."); } else { var proposalLiteral = createAction.actionSentence as elm.Instance; if (proposalLiteral == null) { Console.WriteLine("Resulting CreateAction does not have an ELM Instance as the Action Sentence."); } else { if (proposalLiteral.classType.Name != "SubstanceAdministrationProposal") { Console.WriteLine("Resulting proposal is not a substance administration proposal"); } else { Console.WriteLine("Substance Administration Proposed: {0}.", (proposalLiteral.element.Single(e => e.name == "substanceAdministrationGeneralPurpose").value as elm.Code).display); } } } } else { Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); var content = response.Content.ReadAsStringAsync().Result; if (content.Length > 0) { // NOTE: Deserialization here is assuming EvaluationException, need to peek into the Xml stream to determine the actual type. var dssException = DSSExceptionExtensions.DeserializeFromString <EvaluationException>(content); Console.WriteLine(dssException.GetType().Name); Console.WriteLine(String.Join("\r\n", dssException.errorMessage)); if (dssException.value != null) { var cdsMessage = Packager.DecodeExecutionMessagePayload(dssException.value.base64EncodedPayload); Console.WriteLine(cdsMessage.GetType().Name); Console.WriteLine(cdsMessage.message.value); } } } Console.ReadLine(); }