WIP on choice specialization

This commit is contained in:
James Agnew 2024-02-22 11:50:52 -05:00
parent a5eddc3837
commit e824085330
4 changed files with 71 additions and 46 deletions

View File

@ -46,13 +46,15 @@ public class RuntimeChildAny extends RuntimeChildChoiceDefinition {
void sealAndInitialize( void sealAndInitialize(
FhirContext theContext, FhirContext theContext,
Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) { Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
List<Class<? extends IBase>> choiceTypes = new ArrayList<Class<? extends IBase>>(); List<Class<? extends IBase>> choiceTypes = new ArrayList<>();
List<Class<? extends IBase>> spacializationChoiceTypes = new ArrayList<>();
for (Class<? extends IBase> next : theClassToElementDefinitions.keySet()) { for (Class<? extends IBase> next : theClassToElementDefinitions.keySet()) {
if (next.equals(XhtmlDt.class)) { if (next.equals(XhtmlDt.class)) {
continue; continue;
} }
boolean isSpecialization = false;
BaseRuntimeElementDefinition<?> nextDef = theClassToElementDefinitions.get(next); BaseRuntimeElementDefinition<?> nextDef = theClassToElementDefinitions.get(next);
if (nextDef instanceof IRuntimeDatatypeDefinition) { if (nextDef instanceof IRuntimeDatatypeDefinition) {
if (((IRuntimeDatatypeDefinition) nextDef).isSpecialization()) { if (((IRuntimeDatatypeDefinition) nextDef).isSpecialization()) {
@ -60,7 +62,7 @@ public class RuntimeChildAny extends RuntimeChildChoiceDefinition {
* Things like BoundCodeDt shoudn't be considered as valid options for an "any" choice, since * Things like BoundCodeDt shoudn't be considered as valid options for an "any" choice, since
* we'll already have CodeDt as an option * we'll already have CodeDt as an option
*/ */
continue; isSpecialization = true;
} }
} }
@ -68,10 +70,23 @@ public class RuntimeChildAny extends RuntimeChildChoiceDefinition {
|| IDatatype.class.isAssignableFrom(next) || IDatatype.class.isAssignableFrom(next)
|| IBaseDatatype.class.isAssignableFrom(next) || IBaseDatatype.class.isAssignableFrom(next)
|| IBaseReference.class.isAssignableFrom(next)) { || IBaseReference.class.isAssignableFrom(next)) {
if (isSpecialization) {
spacializationChoiceTypes.add(next);
} else {
choiceTypes.add(next); choiceTypes.add(next);
} }
} }
Collections.sort(choiceTypes, new Comparator<Class<?>>() { }
choiceTypes.sort(new ResourceTypeNameComparator());
spacializationChoiceTypes.sort(new ResourceTypeNameComparator());
setChoiceTypes(choiceTypes, spacializationChoiceTypes);
super.sealAndInitialize(theContext, theClassToElementDefinitions);
}
private static class ResourceTypeNameComparator implements Comparator<Class<?>> {
@Override @Override
public int compare(Class<?> theO1, Class<?> theO2) { public int compare(Class<?> theO1, Class<?> theO2) {
boolean o1res = IResource.class.isAssignableFrom(theO1); boolean o1res = IResource.class.isAssignableFrom(theO1);
@ -86,10 +101,5 @@ public class RuntimeChildAny extends RuntimeChildChoiceDefinition {
return 1; return 1;
} }
} }
});
setChoiceTypes(choiceTypes);
super.sealAndInitialize(theContext, theClassToElementDefinitions);
} }
} }

View File

@ -22,12 +22,14 @@ package ca.uhn.fhir.context;
import ca.uhn.fhir.i18n.Msg; import ca.uhn.fhir.i18n.Msg;
import ca.uhn.fhir.model.api.annotation.Child; import ca.uhn.fhir.model.api.annotation.Child;
import ca.uhn.fhir.model.api.annotation.Description; import ca.uhn.fhir.model.api.annotation.Description;
import jakarta.annotation.Nonnull;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBase;
import org.hl7.fhir.instance.model.api.IBaseDatatype; import org.hl7.fhir.instance.model.api.IBaseDatatype;
import org.hl7.fhir.instance.model.api.IBaseReference; import org.hl7.fhir.instance.model.api.IBaseReference;
import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.hl7.fhir.instance.model.api.IPrimitiveType;
import org.thymeleaf.util.Validate;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
@ -45,6 +47,7 @@ public class RuntimeChildChoiceDefinition extends BaseRuntimeDeclaredChildDefini
private Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> myDatatypeToElementDefinition; private Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> myDatatypeToElementDefinition;
private String myReferenceSuffix; private String myReferenceSuffix;
private List<Class<? extends IBaseResource>> myResourceTypes; private List<Class<? extends IBaseResource>> myResourceTypes;
private List<Class<? extends IBase>> mySpecializationChoiceTypes = List.of();
/** /**
* Constructor * Constructor
@ -70,8 +73,11 @@ public class RuntimeChildChoiceDefinition extends BaseRuntimeDeclaredChildDefini
super(theField, theChildAnnotation, theDescriptionAnnotation, theElementName); super(theField, theChildAnnotation, theDescriptionAnnotation, theElementName);
} }
void setChoiceTypes(List<Class<? extends IBase>> theChoiceTypes) { void setChoiceTypes(@Nonnull List<Class<? extends IBase>> theChoiceTypes, @Nonnull List<Class<? extends IBase>> theSpecializationChoiceTypes) {
Validate.notNull(theChoiceTypes, "theChoiceTypes must not be null");
Validate.notNull(theSpecializationChoiceTypes, "theSpecializationChoiceTypes must not be null");
myChoiceTypes = Collections.unmodifiableList(theChoiceTypes); myChoiceTypes = Collections.unmodifiableList(theChoiceTypes);
mySpecializationChoiceTypes = Collections.unmodifiableList(theSpecializationChoiceTypes);
} }
public List<Class<? extends IBase>> getChoices() { public List<Class<? extends IBase>> getChoices() {
@ -96,14 +102,24 @@ public class RuntimeChildChoiceDefinition extends BaseRuntimeDeclaredChildDefini
void sealAndInitialize( void sealAndInitialize(
FhirContext theContext, FhirContext theContext,
Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) { Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
myNameToChildDefinition = new HashMap<String, BaseRuntimeElementDefinition<?>>(); myNameToChildDefinition = new HashMap<>();
myDatatypeToElementName = new HashMap<Class<? extends IBase>, String>(); myDatatypeToElementName = new HashMap<>();
myDatatypeToElementDefinition = new HashMap<Class<? extends IBase>, BaseRuntimeElementDefinition<?>>(); myDatatypeToElementDefinition = new HashMap<>();
myResourceTypes = new ArrayList<Class<? extends IBaseResource>>(); myResourceTypes = new ArrayList<>();
myReferenceSuffix = "Reference"; myReferenceSuffix = "Reference";
for (Class<? extends IBase> next : myChoiceTypes) { sealAndInitializeChoiceTypes(theContext, theClassToElementDefinitions, mySpecializationChoiceTypes, true);
sealAndInitializeChoiceTypes(theContext, theClassToElementDefinitions, myChoiceTypes, false);
myNameToChildDefinition = Collections.unmodifiableMap(myNameToChildDefinition);
myDatatypeToElementName = Collections.unmodifiableMap(myDatatypeToElementName);
myDatatypeToElementDefinition = Collections.unmodifiableMap(myDatatypeToElementDefinition);
myResourceTypes = Collections.unmodifiableList(myResourceTypes);
}
private void sealAndInitializeChoiceTypes(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions, List<Class<? extends IBase>> choiceTypes, boolean theIsSpecilization) {
for (Class<? extends IBase> next : choiceTypes) {
String elementName = null; String elementName = null;
BaseRuntimeElementDefinition<?> nextDef; BaseRuntimeElementDefinition<?> nextDef;
@ -112,8 +128,10 @@ public class RuntimeChildChoiceDefinition extends BaseRuntimeDeclaredChildDefini
elementName = getElementName() + StringUtils.capitalize(next.getSimpleName()); elementName = getElementName() + StringUtils.capitalize(next.getSimpleName());
nextDef = findResourceReferenceDefinition(theClassToElementDefinitions); nextDef = findResourceReferenceDefinition(theClassToElementDefinitions);
if (!theIsSpecilization) {
myNameToChildDefinition.put(getElementName() + "Reference", nextDef); myNameToChildDefinition.put(getElementName() + "Reference", nextDef);
myNameToChildDefinition.put(getElementName() + "Resource", nextDef); myNameToChildDefinition.put(getElementName() + "Resource", nextDef);
}
myResourceTypes.add((Class<? extends IBaseResource>) next); myResourceTypes.add((Class<? extends IBaseResource>) next);
@ -147,6 +165,7 @@ public class RuntimeChildChoiceDefinition extends BaseRuntimeDeclaredChildDefini
} }
// I don't see how elementName could be null here, but eclipse complains.. // I don't see how elementName could be null here, but eclipse complains..
if (!theIsSpecilization) {
if (elementName != null) { if (elementName != null) {
if (myNameToChildDefinition.containsKey(elementName) == false || !nonPreferred) { if (myNameToChildDefinition.containsKey(elementName) == false || !nonPreferred) {
myNameToChildDefinition.put(elementName, nextDef); myNameToChildDefinition.put(elementName, nextDef);
@ -161,6 +180,7 @@ public class RuntimeChildChoiceDefinition extends BaseRuntimeDeclaredChildDefini
elementName = getElementName() + myReferenceSuffix; elementName = getElementName() + myReferenceSuffix;
myNameToChildDefinition.put(elementName, nextDef); myNameToChildDefinition.put(elementName, nextDef);
} }
}
myDatatypeToElementDefinition.put(next, nextDef); myDatatypeToElementDefinition.put(next, nextDef);
@ -175,11 +195,6 @@ public class RuntimeChildChoiceDefinition extends BaseRuntimeDeclaredChildDefini
myDatatypeToElementName.put(next, elementName); myDatatypeToElementName.put(next, elementName);
} }
} }
myNameToChildDefinition = Collections.unmodifiableMap(myNameToChildDefinition);
myDatatypeToElementName = Collections.unmodifiableMap(myDatatypeToElementName);
myDatatypeToElementDefinition = Collections.unmodifiableMap(myDatatypeToElementDefinition);
myResourceTypes = Collections.unmodifiableList(myResourceTypes);
} }
public List<Class<? extends IBaseResource>> getResourceTypes() { public List<Class<? extends IBaseResource>> getResourceTypes() {

View File

@ -82,7 +82,7 @@ public class RuntimeChildDeclaredExtensionDefinition extends RuntimeChildChoiceD
choiceTypes.add(theChildType); choiceTypes.add(theChildType);
} }
setChoiceTypes(choiceTypes); setChoiceTypes(choiceTypes, List.of());
} }
@Override @Override

View File

@ -120,7 +120,7 @@ class ValidatorWrapper {
try { try {
v = new InstanceValidator(theWorkerContext, evaluationCtx, xverManager); v = new InstanceValidator(theWorkerContext, evaluationCtx, xverManager);
} catch (Exception e) { } catch (Exception e) {
throw new ConfigurationException(Msg.code(648) + e); throw new ConfigurationException(Msg.code(648) + e.getMessage(), e);
} }
v.setAssumeValidRestReferences(isAssumeValidRestReferences()); v.setAssumeValidRestReferences(isAssumeValidRestReferences());