use of org.hl7.fhir.dstu2.model.ExpressionNode.Function in project drug-formulary-ri by HL7-DaVinci.
the class ExampleServerDstu3IT method testWebsocketSubscription.
@Test
public void testWebsocketSubscription() throws Exception {
/*
* Create subscription
*/
Subscription subscription = new Subscription();
subscription.setReason("Monitor new neonatal function (note, age will be determined by the monitor)");
subscription.setStatus(Subscription.SubscriptionStatus.REQUESTED);
subscription.setCriteria("Observation?status=final");
Subscription.SubscriptionChannelComponent channel = new Subscription.SubscriptionChannelComponent();
channel.setType(Subscription.SubscriptionChannelType.WEBSOCKET);
channel.setPayload("application/json");
subscription.setChannel(channel);
MethodOutcome methodOutcome = ourClient.create().resource(subscription).execute();
IIdType mySubscriptionId = methodOutcome.getId();
// Wait for the subscription to be activated
waitForSize(1, () -> ourClient.search().forResource(Subscription.class).where(Subscription.STATUS.exactly().code("active")).cacheControl(new CacheControlDirective().setNoCache(true)).returnBundle(Bundle.class).execute().getEntry().size());
/*
* Attach websocket
*/
WebSocketClient myWebSocketClient = new WebSocketClient();
SocketImplementation mySocketImplementation = new SocketImplementation(mySubscriptionId.getIdPart(), EncodingEnum.JSON);
myWebSocketClient.start();
URI echoUri = new URI("ws://localhost:" + ourPort + "/hapi-fhir-jpaserver/websocket");
ClientUpgradeRequest request = new ClientUpgradeRequest();
ourLog.info("Connecting to : {}", echoUri);
Future<Session> connection = myWebSocketClient.connect(mySocketImplementation, echoUri, request);
Session session = connection.get(2, TimeUnit.SECONDS);
ourLog.info("Connected to WS: {}", session.isOpen());
/*
* Create a matching resource
*/
Observation obs = new Observation();
obs.setStatus(Observation.ObservationStatus.FINAL);
ourClient.create().resource(obs).execute();
// Give some time for the subscription to deliver
Thread.sleep(2000);
/*
* Ensure that we receive a ping on the websocket
*/
waitForSize(1, () -> mySocketImplementation.myPingCount);
/*
* Clean up
*/
ourClient.delete().resourceById(mySubscriptionId).execute();
}
use of org.hl7.fhir.dstu2.model.ExpressionNode.Function in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method execute.
private List<Base> execute(ExecutionContext context, List<Base> focus, ExpressionNode exp, boolean atEntry) throws PathEngineException {
// System.out.println("Evaluate {'"+exp.toString()+"'} on "+focus.toString());
List<Base> work = new ArrayList<Base>();
switch(exp.getKind()) {
case Name:
if (atEntry && exp.getName().equals("$this"))
work.add(context.getThisItem());
else
for (Base item : focus) {
List<Base> outcome = execute(context, item, exp, atEntry);
for (Base base : outcome) if (base != null)
work.add(base);
}
break;
case Function:
List<Base> work2 = evaluateFunction(context, focus, exp);
work.addAll(work2);
break;
case Constant:
Base b = processConstant(context, exp.getConstant());
if (b != null)
work.add(b);
break;
case Group:
work2 = execute(context, focus, exp.getGroup(), atEntry);
work.addAll(work2);
}
if (exp.getInner() != null)
work = execute(context, work, exp.getInner(), false);
if (exp.isProximal() && exp.getOperation() != null) {
ExpressionNode next = exp.getOpNext();
ExpressionNode last = exp;
while (next != null) {
List<Base> work2 = preOperate(work, last.getOperation());
if (work2 != null)
work = work2;
else if (last.getOperation() == Operation.Is || last.getOperation() == Operation.As) {
work2 = executeTypeName(context, focus, next, false);
work = operate(work, last.getOperation(), work2);
} else {
work2 = execute(context, focus, next, true);
work = operate(work, last.getOperation(), work2);
// System.out.println("Result of {'"+last.toString()+" "+last.getOperation().toCode()+" "+next.toString()+"'}: "+focus.toString());
}
last = next;
next = next.getOpNext();
}
}
// System.out.println("Result of {'"+exp.toString()+"'}: "+work.toString());
return work;
}
use of org.hl7.fhir.dstu2.model.ExpressionNode.Function in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method executeType.
private TypeDetails executeType(ExecutionTypeContext context, TypeDetails focus, ExpressionNode exp, boolean atEntry) throws PathEngineException, DefinitionException {
// System.out.println("Evaluate {'"+exp.toString()+"'} on "+focus.toString());
TypeDetails result = new TypeDetails(null);
switch(exp.getKind()) {
case Name:
if (atEntry && exp.getName().equals("$this"))
result.update(context.getThisItem());
else {
for (String s : focus.getTypes()) {
result.update(executeType(s, exp, atEntry));
}
if (result.hasNoTypes())
throw new PathEngineException("The name " + exp.getName() + " is not valid for any of the possible types: " + focus.describe());
}
break;
case Function:
result.update(evaluateFunctionType(context, focus, exp));
break;
case Constant:
result.addType(readConstantType(context, exp.getConstant()));
break;
case Group:
result.update(executeType(context, focus, exp.getGroup(), atEntry));
}
exp.setTypes(result);
if (exp.getInner() != null) {
result = executeType(context, result, exp.getInner(), false);
}
if (exp.isProximal() && exp.getOperation() != null) {
ExpressionNode next = exp.getOpNext();
ExpressionNode last = exp;
while (next != null) {
TypeDetails work;
if (last.getOperation() == Operation.Is || last.getOperation() == Operation.As)
work = executeTypeName(context, focus, next, atEntry);
else
work = executeType(context, focus, next, atEntry);
result = operateTypes(result, last.getOperation(), work);
last = next;
next = next.getOpNext();
}
exp.setOpTypes(result);
}
return result;
}
use of org.hl7.fhir.dstu2.model.ExpressionNode.Function in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method parseExpression.
private ExpressionNode parseExpression(FHIRLexer lexer, boolean proximal) throws FHIRLexerException {
ExpressionNode result = new ExpressionNode(lexer.nextId());
SourceLocation c = lexer.getCurrentStartLocation();
result.setStart(lexer.getCurrentLocation());
// special:
if (lexer.getCurrent().equals("-")) {
lexer.take();
lexer.setCurrent("-" + lexer.getCurrent());
}
if (lexer.getCurrent().equals("+")) {
lexer.take();
lexer.setCurrent("+" + lexer.getCurrent());
}
if (lexer.isConstant(false)) {
checkConstant(lexer.getCurrent(), lexer);
result.setConstant(lexer.take());
result.setKind(Kind.Constant);
result.setEnd(lexer.getCurrentLocation());
} else if ("(".equals(lexer.getCurrent())) {
lexer.next();
result.setKind(Kind.Group);
result.setGroup(parseExpression(lexer, true));
if (!")".equals(lexer.getCurrent()))
throw lexer.error("Found " + lexer.getCurrent() + " expecting a \")\"");
result.setEnd(lexer.getCurrentLocation());
lexer.next();
} else {
if (!lexer.isToken() && !lexer.getCurrent().startsWith("\""))
throw lexer.error("Found " + lexer.getCurrent() + " expecting a token name");
if (lexer.getCurrent().startsWith("\""))
result.setName(lexer.readConstant("Path Name"));
else
result.setName(lexer.take());
result.setEnd(lexer.getCurrentLocation());
if (!result.checkName())
throw lexer.error("Found " + result.getName() + " expecting a valid token name");
if ("(".equals(lexer.getCurrent())) {
Function f = Function.fromCode(result.getName());
FunctionDetails details = null;
if (f == null) {
details = hostServices != null ? hostServices.resolveFunction(result.getName()) : null;
if (details == null)
throw lexer.error("The name " + result.getName() + " is not a valid function name");
f = Function.Custom;
}
result.setKind(Kind.Function);
result.setFunction(f);
lexer.next();
while (!")".equals(lexer.getCurrent())) {
result.getParameters().add(parseExpression(lexer, true));
if (",".equals(lexer.getCurrent()))
lexer.next();
else if (!")".equals(lexer.getCurrent()))
throw lexer.error("The token " + lexer.getCurrent() + " is not expected here - either a \",\" or a \")\" expected");
}
result.setEnd(lexer.getCurrentLocation());
lexer.next();
checkParameters(lexer, c, result, details);
} else
result.setKind(Kind.Name);
}
ExpressionNode focus = result;
if ("[".equals(lexer.getCurrent())) {
lexer.next();
ExpressionNode item = new ExpressionNode(lexer.nextId());
item.setKind(Kind.Function);
item.setFunction(ExpressionNode.Function.Item);
item.getParameters().add(parseExpression(lexer, true));
if (!lexer.getCurrent().equals("]"))
throw lexer.error("The token " + lexer.getCurrent() + " is not expected here - a \"]\" expected");
lexer.next();
result.setInner(item);
focus = item;
}
if (".".equals(lexer.getCurrent())) {
lexer.next();
focus.setInner(parseExpression(lexer, false));
}
result.setProximal(proximal);
if (proximal) {
while (lexer.isOp()) {
focus.setOperation(ExpressionNode.Operation.fromCode(lexer.getCurrent()));
focus.setOpStart(lexer.getCurrentStartLocation());
focus.setOpEnd(lexer.getCurrentLocation());
lexer.next();
focus.setOpNext(parseExpression(lexer, false));
focus = focus.getOpNext();
}
result = organisePrecedence(lexer, result);
}
return result;
}
use of org.hl7.fhir.dstu2.model.ExpressionNode.Function in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method executeType.
private TypeDetails executeType(ExecutionTypeContext context, TypeDetails focus, ExpressionNode exp, boolean atEntry) throws PathEngineException, DefinitionException {
TypeDetails result = new TypeDetails(null);
switch(exp.getKind()) {
case Name:
if (atEntry && exp.getName().equals("$this"))
result.update(context.getContext());
else {
for (String s : focus.getTypes()) {
result.update(executeType(s, exp, atEntry));
}
if (result.hasNoTypes())
throw new PathEngineException("The name " + exp.getName() + " is not valid for any of the possible types: " + focus.describe());
}
break;
case Function:
result.update(evaluateFunctionType(context, focus, exp));
break;
case Constant:
result.addType(readConstantType(context, exp.getConstant()));
break;
case Group:
result.update(executeType(context, focus, exp.getGroup(), atEntry));
}
exp.setTypes(result);
if (exp.getInner() != null) {
result = executeType(context, result, exp.getInner(), false);
}
if (exp.isProximal() && exp.getOperation() != null) {
ExpressionNode next = exp.getOpNext();
ExpressionNode last = exp;
while (next != null) {
TypeDetails work;
if (last.getOperation() == Operation.Is || last.getOperation() == Operation.As)
work = executeTypeName(context, focus, next, atEntry);
else
work = executeType(context, focus, next, atEntry);
result = operateTypes(result, last.getOperation(), work);
last = next;
next = next.getOpNext();
}
exp.setOpTypes(result);
}
return result;
}
Aggregations