use of com.nedap.archie.rm.datastructures.PointEvent in project openEHR_SDK by ehrbase.
the class UnflattenerTest method testUnflattenAltEvent.
@Test
public void testUnflattenAltEvent() {
AlternativeEventsComposition alternativeEventsComposition = buildAlternativeEventsComposition();
Unflattener cut = new Unflattener(new TestDataTemplateProvider());
Composition actual = (Composition) cut.unflatten(alternativeEventsComposition);
assertThat(actual).isNotNull();
assertThat(actual.getContent()).size().isEqualTo(1);
Observation actualObservation = (Observation) actual.getContent().get(0);
assertThat(actualObservation.getData().getEvents()).size().isEqualTo(3);
assertThat(actualObservation.getData().getEvents()).extracting(e -> (Class) e.getClass()).containsExactlyInAnyOrder(PointEvent.class, PointEvent.class, IntervalEvent.class);
List<PointEvent> pointEvents = actualObservation.getData().getEvents().stream().filter(e -> PointEvent.class.isAssignableFrom(e.getClass())).map(e -> (PointEvent) e).collect(Collectors.toList());
assertThat(pointEvents).extracting(p -> p.getTime().getValue(), p -> ((DvQuantity) ((Element) p.getData().getItems().get(0)).getValue()).getMagnitude(), p -> ((DvQuantity) ((Element) p.getData().getItems().get(0)).getValue()).getUnits()).containsExactlyInAnyOrder(new Tuple(OffsetDateTime.of(1990, 11, 02, 12, 00, 00, 00, ZoneOffset.UTC), 30d, "kg"), new Tuple(OffsetDateTime.of(2013, 11, 02, 12, 00, 00, 00, ZoneOffset.UTC), 55d, "kg"));
List<IntervalEvent> intervalEvents = actualObservation.getData().getEvents().stream().filter(e -> IntervalEvent.class.isAssignableFrom(e.getClass())).map(e -> (IntervalEvent) e).collect(Collectors.toList());
assertThat(intervalEvents).extracting(p -> p.getTime().getValue(), p -> ((DvQuantity) ((Element) p.getData().getItems().get(0)).getValue()).getMagnitude(), p -> ((DvQuantity) ((Element) p.getData().getItems().get(0)).getValue()).getUnits(), p -> p.getMathFunction().getValue(), p -> p.getWidth().getValue()).containsExactlyInAnyOrder(new Tuple(OffsetDateTime.of(2015, 11, 02, 12, 00, 00, 00, ZoneOffset.UTC), 60d, "kg", "mean", Duration.ofDays(30)));
}
use of com.nedap.archie.rm.datastructures.PointEvent in project openEHR_SDK by ehrbase.
the class ClassGenerator method build.
private TypeSpec.Builder build(ClassGeneratorContext context, WebTemplateNode next) {
String className = defaultNamingStrategy.buildClassName(context, next, false, false);
context.currentFieldNameMap.push(new HashMap<>());
context.nodeDeque.push(next);
context.unFilteredNodeDeque.push(next);
TypeSpec.Builder classBuilder = TypeSpec.classBuilder(className);
if (StringUtils.isBlank(context.currentMainClass)) {
context.currentMainClass = className;
}
classBuilder.addModifiers(Modifier.PUBLIC);
classBuilder.addAnnotation(AnnotationSpec.builder(Entity.class).build());
if (next.isArchetype()) {
AnnotationSpec archetypeAnnotation = AnnotationSpec.builder(Archetype.class).addMember(Archetype.VALUE, "$S", next.getNodeId()).build();
classBuilder.addAnnotation(archetypeAnnotation);
}
AnnotationSpec generatedAnnotation = buildGeneratedAnnotation();
classBuilder.addAnnotation(generatedAnnotation);
classBuilder.addSuperinterface(findRMInterface(next));
if (next.getChildren().stream().anyMatch(n -> n.getRmType().equals("EVENT"))) {
WebTemplateNode event = next.getChildren().stream().filter(n -> n.getRmType().equals("EVENT")).findAny().orElseThrow();
Walker.EventHelper eventHelper = new Walker.EventHelper(event).invoke();
WebTemplateNode pointEvent = eventHelper.getPointEvent();
WebTemplateNode intervalEvent = eventHelper.getIntervalEvent();
next.getChildren().add(intervalEvent);
next.getChildren().add(pointEvent);
next.getChildren().remove(event);
}
Map<String, List<WebTemplateNode>> choices = next.getChoicesInChildren();
List<WebTemplateNode> children = next.getChildren().stream().filter(c -> choices.values().stream().flatMap(List::stream).noneMatch(l -> l.equals(c))).collect(Collectors.toList());
for (WebTemplateNode child : children) {
Deque<WebTemplateNode> filtersNodes = pushToUnfiltered(context, child);
String relativPath = context.nodeDeque.peek().buildRelativePath(child);
if (child.getChildren().isEmpty() && !choices.containsKey(child.getAqlPath())) {
addSimpleField(context, classBuilder, relativPath, child);
} else if (!choices.containsKey(child.getAqlPath())) {
addComplexField(context, classBuilder, relativPath, child);
}
if (!CollectionUtils.isEmpty(filtersNodes)) {
filtersNodes.forEach(n -> context.unFilteredNodeDeque.poll());
}
}
for (List<WebTemplateNode> choice : choices.values()) {
WebTemplateNode node = choice.get(0);
WebTemplateNode relativeNode = buildRelativeNode(context, node);
Deque<WebTemplateNode> filtersNodes = pushToUnfiltered(context, node);
TypeSpec interfaceSpec;
TypeName interfaceClassName;
if (context.currentTypeSpec.containsKey(relativeNode)) {
interfaceSpec = context.currentTypeSpec.get(relativeNode);
String interfacePackage = context.currentPackageName + "." + context.currentMainClass.toLowerCase() + DEFINITION_PACKAGE;
context.classes.put(interfacePackage, interfaceSpec);
interfaceClassName = ClassName.get(interfacePackage, interfaceSpec.name);
} else {
List<Pair<TypeSpec.Builder, WebTemplateNode>> builders = new ArrayList<>();
for (WebTemplateNode child : choice) {
TypeSpec.Builder build = build(context, child);
builders.add(new ImmutablePair<>(build, child));
}
TypeSpec.Builder interfaceBuilder = TypeSpec.interfaceBuilder(defaultNamingStrategy.buildClassName(context, choice.get(0), true, false)).addModifiers(Modifier.PUBLIC);
interfaceBuilder.addAnnotation(buildGeneratedAnnotation());
Set<FieldSpec> cowmenField = null;
for (Set<FieldSpec> fields : builders.stream().map(Pair::getLeft).map(s -> s.fieldSpecs).map(HashSet::new).collect(Collectors.toList())) {
if (cowmenField == null) {
cowmenField = fields;
} else {
cowmenField = SetUtils.intersection(cowmenField, fields);
}
}
if (cowmenField == null) {
cowmenField = Collections.emptySet();
}
cowmenField.forEach(f -> {
interfaceBuilder.addMethod(buildGetter(f, true));
interfaceBuilder.addMethod(buildSetter(f, true));
});
interfaceSpec = interfaceBuilder.build();
context.currentTypeSpec.put(relativeNode, interfaceSpec);
String interfacePackage = context.currentPackageName + "." + context.currentMainClass.toLowerCase() + DEFINITION_PACKAGE;
context.classes.put(interfacePackage, interfaceSpec);
interfaceClassName = ClassName.get(interfacePackage, interfaceSpec.name);
TypeName finalInterfaceClassName = interfaceClassName;
builders.forEach(pair -> {
TypeSpec.Builder builder = pair.getKey().addSuperinterface(finalInterfaceClassName).addAnnotation(AnnotationSpec.builder(OptionFor.class).addMember(OptionFor.VALUE, "$S", pair.getRight().getRmType()).build());
context.classes.put(interfacePackage, builder.build());
});
}
if (choice.stream().anyMatch(WebTemplateNode::isMulti)) {
interfaceClassName = ParameterizedTypeName.get(ClassName.get(List.class), interfaceClassName);
}
String relativPath = FlatPath.removeStart(new FlatPath(node.getAqlPath()), new FlatPath(next.getAqlPath())).toString();
addField(context, classBuilder, relativPath, node, interfaceClassName, new ValueSet(ValueSet.LOCAL, ValueSet.LOCAL, Collections.emptySet()), true);
if (!CollectionUtils.isEmpty(filtersNodes)) {
filtersNodes.forEach(n -> context.unFilteredNodeDeque.poll());
}
}
if (next.isArchetype()) {
context.currentArchetypeName.poll();
}
if (children.isEmpty() && choices.isEmpty()) {
addSimpleField(context, classBuilder, "", next);
}
context.currentFieldNameMap.poll();
context.nodeDeque.poll();
context.unFilteredNodeDeque.poll();
return classBuilder;
}
use of com.nedap.archie.rm.datastructures.PointEvent in project openEHR_SDK by ehrbase.
the class UnflattenerTest method testUnflattenEhrbaseMultiOccurrenceDeV1.
@Test
public void testUnflattenEhrbaseMultiOccurrenceDeV1() {
Unflattener cut = new Unflattener(new TestDataTemplateProvider());
EhrbaseMultiOccurrenceDeV1Composition dto = TestData.buildEhrbaseMultiOccurrenceDeV1();
Composition rmObject = (Composition) cut.unflatten(dto);
assertThat(rmObject).isNotNull();
assertThat(rmObject.getArchetypeDetails().getTemplateId().getValue()).isEqualTo("ehrbase_multi_occurrence.de.v1");
List<Object> observationList = rmObject.itemsAtPath("/content[openEHR-EHR-OBSERVATION.body_temperature.v2]");
assertThat(observationList).size().isEqualTo(2);
Observation observation1 = (Observation) observationList.get(0);
List<Object> objects = observation1.itemsAtPath("/data[at0002]/events");
assertThat(objects).extracting(o -> ((PointEvent) o)).extracting(p -> (DvQuantity) p.itemAtPath("/data[at0001]/items[at0004]/value")).extracting(DvQuantity::getMagnitude).containsExactlyInAnyOrder(11d, 22d);
DvCodedText dvCodedText = (DvCodedText) observation1.itemAtPath("/protocol[at0020]/items[at0021]/value");
assertThat(dvCodedText.getValue()).isEqualTo("Forehead");
Observation observation2 = (Observation) observationList.get(1);
DvText dvText = (DvText) observation2.itemAtPath("/protocol[at0020]/items[at0021]/value");
assertFalse(dvText instanceof DvCodedText);
assertThat(dvText.getValue()).isEqualTo("location");
}
use of com.nedap.archie.rm.datastructures.PointEvent in project openEHR_SDK by ehrbase.
the class UnflattenerTest method testUnflattenSingleEvent.
@Test
public void testUnflattenSingleEvent() {
Unflattener cut = new Unflattener(new TestDataTemplateProvider());
KorpergrosseComposition dto = new KorpergrosseComposition();
dto.setGrosseLange(new GrosseLangeObservation());
dto.getGrosseLange().setGrosseLangeMagnitude(22d);
Composition rmObject = (Composition) cut.unflatten(dto);
assertThat(rmObject).isNotNull();
Object event = rmObject.itemAtPath("/content[openEHR-EHR-OBSERVATION.height.v2]/data[at0001]/events[at0002]");
assertThat(event).isNotNull();
assertThat(event.getClass()).isEqualTo(PointEvent.class);
Object quantity = ((PointEvent) event).itemAtPath("/data[at0003]/items[at0004]/value");
assertThat(quantity).isNotNull();
assertThat(quantity.getClass()).isEqualTo(DvQuantity.class);
assertThat(((DvQuantity) quantity).getMagnitude()).isEqualTo(22d);
}
Aggregations