ValueSet building

This commit is contained in:
jamesagnew 2014-02-28 16:30:34 -05:00
parent 1fab951815
commit 7926387786
9 changed files with 260 additions and 264 deletions

View File

@ -3,54 +3,52 @@ package ca.uhn.fhir.model.api;
import java.util.HashMap;
import java.util.Map;
public abstract class ValueSetEnumeration {
private static final Map<Class<? extends ValueSetEnumeration>, CodeMap> myClassToCodeMap = new HashMap<>();
private static final Map<Class<? extends ValueSetEnumeration>, CodeMap> myClassToCodeMap = new HashMap<Class<? extends ValueSetEnumeration>, ValueSetEnumeration.CodeMap>();
private final String myCode;
private final int myOrdinal;
public ValueSetEnumeration(String theCode, String theValueSetIdentifier) {
myCode = theCode;
CodeMap codeMap = myClassToCodeMap.get(getClass());
if (codeMap == null) {
codeMap = new CodeMap(theValueSetIdentifier);
myClassToCodeMap.put(getClass(), codeMap);
}
myOrdinal = codeMap.nextOrdinal();
myOrdinal = codeMap.nextOrdinal();
codeMap.addCode(this);
}
private static class CodeMap
{
public String getCode() {
return myCode;
}
public int getOrdinal() {
return myOrdinal;
}
private static class CodeMap {
private Map<String, ValueSetEnumeration> myCodeMap = new HashMap<String, ValueSetEnumeration>();
private String myValueSetIdentifier;
private int myNextOrdinal = 0;
private String myValueSetIdentifier;
public CodeMap(String theValueSetIdentifier) {
myValueSetIdentifier = theValueSetIdentifier;
}
public int getNextOrdinal() {
return myNextOrdinal;
public void addCode(ValueSetEnumeration theValueSetEnumeration) {
myCodeMap.put(theValueSetEnumeration.getCode(), theValueSetEnumeration);
}
public int nextOrdinal() {
return myNextOrdinal++;
}
public void addCode(ValueSetEnumeration theValueSetEnumeration) {
myCodeMap.put(theValueSetEnumeration.getCode(), theValueSetEnumeration);
}
}
public String getCode() {
return myCode;
}
}

View File

@ -12,6 +12,7 @@ import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.ObjectUtils;
@ -48,6 +49,7 @@ public abstract class BaseParser {
private String myDirectory;
private ArrayList<Extension> myExtensions;
private String myOutputFile;
private List<Resource> myResources = new ArrayList<Resource>();
public void parse() throws Exception {
File baseDir = new File(myDirectory);
@ -55,75 +57,82 @@ public abstract class BaseParser {
throw new Exception(myDirectory + " does not exist or is not a directory");
}
File resourceSpreadsheetFile = new File(baseDir, getFilename());
if (resourceSpreadsheetFile.exists() == false) {
throw new Exception(resourceSpreadsheetFile.getAbsolutePath() + " does not exist");
for (File nextFile : baseDir.listFiles()) {
if (isSpreadsheet(nextFile.getAbsolutePath())) {
ourLog.info("Scanning file: {}", nextFile.getAbsolutePath());
File resourceSpreadsheetFile = nextFile;
if (resourceSpreadsheetFile.exists() == false) {
throw new Exception(resourceSpreadsheetFile.getAbsolutePath() + " does not exist");
}
Document file = XMLUtils.parse(new FileInputStream(resourceSpreadsheetFile), false);
Element dataElementsSheet = (Element) file.getElementsByTagName("Worksheet").item(0);
NodeList tableList = dataElementsSheet.getElementsByTagName("Table");
Element table = (Element) tableList.item(0);
NodeList rows = table.getElementsByTagName("Row");
Element defRow = (Element) rows.item(0);
parseFirstRow(defRow);
Element resourceRow = (Element) rows.item(1);
Resource resource = new Resource();
myResources.add(resource);
parseBasicElements(resourceRow, resource);
Map<String, BaseElement> elements = new HashMap<String, BaseElement>();
elements.put(resource.getElementName(), resource);
// Map<String,String> blockFullNameToShortName = new HashMap<String,String>();
for (int i = 2; i < rows.getLength(); i++) {
Element nextRow = (Element) rows.item(i);
String name = cellValue(nextRow, 0);
if (name == null || name.startsWith("!")) {
continue;
}
String type = cellValue(nextRow, myColType);
Child elem;
if (StringUtils.isBlank(type) || type.startsWith("=")) {
elem = new ResourceBlock();
} else if (type.startsWith("@")) {
// type = type.substring(type.lastIndexOf('.')+1);
elem = new ResourceBlockCopy();
} else if (type.equals("*")) {
elem = new AnyChild();
} else {
elem = new Child();
}
parseBasicElements(nextRow, elem);
elements.put(elem.getName(), elem);
BaseElement parent = elements.get(elem.getElementParentName());
if (parent == null) {
throw new Exception("Can't find element " + elem.getElementParentName() + " - Valid values are: " + elements.keySet());
}
parent.addChild(elem);
/*
* Find simple setters
*/
if (elem instanceof Child) {
scanForSimpleSetters(elem);
}
}
}
}
Document file = XMLUtils.parse(new FileInputStream(resourceSpreadsheetFile), false);
Element dataElementsSheet = (Element) file.getElementsByTagName("Worksheet").item(0);
NodeList tableList = dataElementsSheet.getElementsByTagName("Table");
Element table = (Element) tableList.item(0);
NodeList rows = table.getElementsByTagName("Row");
Element defRow = (Element) rows.item(0);
parseFirstRow(defRow);
Element resourceRow = (Element) rows.item(1);
Resource resource = new Resource();
parseBasicElements(resourceRow, resource);
Map<String, BaseElement> elements = new HashMap<String, BaseElement>();
elements.put(resource.getElementName(), resource);
// Map<String,String> blockFullNameToShortName = new HashMap<String,String>();
ourLog.info("Parsed {} resources", myResources.size());
for (int i = 2; i < rows.getLength(); i++) {
Element nextRow = (Element) rows.item(i);
String name = cellValue(nextRow, 0);
if (name == null || name.startsWith("!")) {
continue;
}
String type = cellValue(nextRow, myColType);
Child elem;
if (StringUtils.isBlank(type) || type.startsWith("=")) {
elem = new ResourceBlock();
} else if (type.startsWith("@")) {
// type = type.substring(type.lastIndexOf('.')+1);
elem=new ResourceBlockCopy();
} else if (type.equals("*")) {
elem = new AnyChild();
} else {
elem = new Child();
}
parseBasicElements(nextRow, elem);
elements.put(elem.getName(), elem);
BaseElement parent = elements.get(elem.getElementParentName());
if (parent == null) {
throw new Exception("Can't find element " + elem.getElementParentName() + " - Valid values are: " + elements.keySet());
}
parent.addChild(elem);
/*
* Find simple setters
*/
if (elem instanceof Child) {
scanForSimpleSetters(elem);
}
}
write(resource);
}
private void scanForSimpleSetters(Child theElem) {
@ -148,7 +157,7 @@ public abstract class BaseParser {
ss.setDatatype(childDt.getSimpleName());
ss.setSuffix(simpleSetter.suffix());
theElem.getSimpleSetters().add(ss);
Annotation[][] paramAnn = nextConstructor.getParameterAnnotations();
Class<?>[] paramTypes = nextConstructor.getParameterTypes();
for (int i = 0; i < paramTypes.length; i++) {
@ -160,7 +169,8 @@ public abstract class BaseParser {
}
}
private ca.uhn.fhir.model.api.annotation.SimpleSetter.Parameter findAnnotation(Class<?> theBase, Annotation[] theAnnotations, Class<ca.uhn.fhir.model.api.annotation.SimpleSetter.Parameter> theClass) {
private ca.uhn.fhir.model.api.annotation.SimpleSetter.Parameter findAnnotation(Class<?> theBase, Annotation[] theAnnotations,
Class<ca.uhn.fhir.model.api.annotation.SimpleSetter.Parameter> theClass) {
for (Annotation next : theAnnotations) {
if (theClass.equals(next.annotationType())) {
return (ca.uhn.fhir.model.api.annotation.SimpleSetter.Parameter) next;
@ -235,8 +245,6 @@ public abstract class BaseParser {
w.close();
}
protected abstract String getFilename();
protected abstract String getTemplate();
protected void parseBasicElements(Element theRowXml, BaseElement theTarget) {
@ -244,7 +252,6 @@ public abstract class BaseParser {
theTarget.setName(name);
theTarget.setElementName(name);
String cardValue = cellValue(theRowXml, myColCard);
if (cardValue != null && cardValue.contains("..")) {
@ -290,4 +297,8 @@ public abstract class BaseParser {
return null;
}
protected boolean isSpreadsheet(String theFileName) {
return true;
}
}

View File

@ -6,21 +6,11 @@ import ca.uhn.fhir.starter.model.BaseElement;
public class DatatypeParser extends BaseParser {
private String myDatatypeName;
@Override
protected String getTemplate() {
return "/dt_composite.vm";
}
public void setDatatypeName(String theString) {
myDatatypeName=theString;
}
@Override
protected String getFilename() {
return myDatatypeName.toLowerCase() + ".xml";
}
// @Override
// protected void parseBasicElements(Element theRowXml, BaseElement theTarget) {

View File

@ -5,160 +5,116 @@ import java.util.List;
public class ResourceParser extends BaseParser {
private String myResourceName;
private List<String> myAllDatatypes;
private void setResourceName(String theString) {
myResourceName = theString;
}
public void setAllDatatypes(List<String> theAllDatatypes) {
myAllDatatypes = theAllDatatypes;
}
@Override
protected String getFilename() {
return myResourceName + "-spreadsheet.xml";
}
// @Override
// protected void parseBasicElements(Element theRowXml, BaseElement
// theTarget) {
// String name = cellValue(theRowXml, 0);
// theTarget.setName(name);
//
// int lastDot = name.lastIndexOf('.');
// if (lastDot == -1) {
// theTarget.setElementName(name);
// } else {
// String elementName = name.substring(lastDot + 1);
// String elementParentName = name.substring(0, lastDot);
// theTarget.setElementName(elementName);
// theTarget.setElementParentName(elementParentName);
// }
//
// String cardValue = cellValue(theRowXml, 1);
// if (cardValue != null && cardValue.contains("..")) {
// String[] split = cardValue.split("\\.\\.");
// theTarget.setCardMin(split[0]);
// theTarget.setCardMax(split[1]);
// }
//
// String type = cellValue(theRowXml, 5);
// theTarget.setTypeFromString(type);
//
// theTarget.setBinding(cellValue(theRowXml, 6));
// theTarget.setShortName(cellValue(theRowXml, 7));
// theTarget.setDefinition(cellValue(theRowXml, 8));
// theTarget.setRequirement(cellValue(theRowXml, 9));
// theTarget.setV2Mapping(cellValue(theRowXml, 14));
// }
@Override
protected String getTemplate() {
return "/resource.vm";
}
@Override
protected boolean isSpreadsheet(String theFileName) {
return theFileName.endsWith("spreadsheet.xml");
}
public static void main(String[] args) throws Exception {
ResourceParser p = new ResourceParser();
p.setAllDatatypes(new ArrayList<String>());
p.setDirectory("src/test/resources/res");
// TODO: this needs to be properly populated
p.getAllDatatypes().add("String");
p.getAllDatatypes().add("Date");
p.getAllDatatypes().add("DateTime");
// ResourceParser p = new ResourceParser();
// p.setAllDatatypes(new ArrayList<String>());
// p.setDirectory("src/test/resources/res");
// p.setResourceName("patient");
// p.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/resource/ResourceWithExtensionsA.java");
// ArrayList<Extension> exts = new ArrayList<Extension>();
// Extension ext1 = new Extension("foo1", "http://foo/1", "string");
// exts.add(ext1);
// Extension ext2 = new Extension("bar1", "http://bar/1", new Extension("bar11", "http://bar/1/1", "date"), new Extension("bar12", "http://bar/1/2", "date"));
// exts.add(ext2);
// p.setExtensions(exts);
//
// // TODO: this needs to be properly populated
// p.getAllDatatypes().add("String");
// p.getAllDatatypes().add("Date");
// p.getAllDatatypes().add("DateTime");
//
//// p.setDirectory("src/test/resources/res");
//// p.setResourceName("patient");
//// p.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/resource/ResourceWithExtensionsA.java");
//// ArrayList<Extension> exts = new ArrayList<Extension>();
//// Extension ext1 = new Extension("foo1", "http://foo/1", "string");
//// exts.add(ext1);
//// Extension ext2 = new Extension("bar1", "http://bar/1", new Extension("bar11", "http://bar/1/1", "date"), new Extension("bar12", "http://bar/1/2", "date"));
//// exts.add(ext2);
//// p.setExtensions(exts);
//// p.parse();
//
//// String basePath="../hapi-fhir-base/src/main/java";
// String basePath="target/generated/valuesets";
//
// p.setResourceName("medication");
// p.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/resource/Medication.java");
// p.parse();
p.setResourceName("medication");
p.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/resource/Medication.java");
p.parse();
p.setDirectory("src/test/resources/res");
p.setResourceName("substance");
p.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/resource/Substance.java");
p.parse();
p.setDirectory("src/test/resources/res");
p.setResourceName("valueset");
p.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/resource/ValueSetTm.java");
p.parse();
p.setDirectory("src/test/resources/res");
p.setResourceName("observation");
p.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/resource/Observation.java");
p.parse();
p.setResourceName("profile");
p.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/resource/Profile.java");
p.parse();
p.setResourceName("device");
p.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/resource/Device.java");
p.parse();
p.setResourceName("group");
p.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/resource/Group.java");
p.parse();
p.setResourceName("location");
p.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/resource/Location.java");
p.parse();
p.setResourceName("organization");
p.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/resource/Organization.java");
p.parse();
p.setResourceName("patient");
p.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/resource/Patient.java");
p.parse();
p.setResourceName("specimen");
p.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/resource/Specimen.java");
p.parse();
p.setResourceName("practitioner");
p.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/resource/Practitioner.java");
p.parse();
DatatypeParser d = new DatatypeParser();
d.setDirectory("src/test/resources/dt");
d.setDatatypeName("humanname");
d.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/composite/HumanNameDt.java");
d.parse();
d.setDatatypeName("contact");
d.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/composite/ContactDt.java");
d.parse();
d.setDatatypeName("address");
d.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/composite/AddressDt.java");
d.parse();
d.setDatatypeName("narrative");
d.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/composite/NarrativeDt.java");
d.parse();
d.setDatatypeName("quantity");
d.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/composite/QuantityDt.java");
d.parse();
//
// p.setDirectory("src/test/resources/res");
// p.setResourceName("substance");
// p.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/resource/Substance.java");
// p.parse();
//
//
// p.setDirectory("src/test/resources/res");
// p.setResourceName("valueset");
// p.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/resource/ValueSetTm.java");
// p.parse();
//
// p.setDirectory("src/test/resources/res");
// p.setResourceName("observation");
// p.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/resource/Observation.java");
// p.parse();
//
// p.setResourceName("profile");
// p.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/resource/Profile.java");
// p.parse();
//
// p.setResourceName("device");
// p.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/resource/Device.java");
// p.parse();
//
// p.setResourceName("group");
// p.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/resource/Group.java");
// p.parse();
//
// p.setResourceName("location");
// p.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/resource/Location.java");
// p.parse();
//
// p.setResourceName("organization");
// p.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/resource/Organization.java");
// p.parse();
//
// p.setResourceName("patient");
// p.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/resource/Patient.java");
// p.parse();
//
// p.setResourceName("specimen");
// p.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/resource/Specimen.java");
// p.parse();
//
// p.setResourceName("practitioner");
// p.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/resource/Practitioner.java");
// p.parse();
//
// DatatypeParser d = new DatatypeParser();
// d.setDirectory("src/test/resources/dt");
// d.setDatatypeName("humanname");
// d.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/composite/HumanNameDt.java");
// d.parse();
//
// d.setDatatypeName("contact");
// d.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/composite/ContactDt.java");
// d.parse();
//
// d.setDatatypeName("address");
// d.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/composite/AddressDt.java");
// d.parse();
//
// d.setDatatypeName("narrative");
// d.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/composite/NarrativeDt.java");
// d.parse();
//
// d.setDatatypeName("quantity");
// d.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/composite/QuantityDt.java");
// d.parse();
}
private List<String> getAllDatatypes() {
return myAllDatatypes;
}
}

View File

@ -0,0 +1,24 @@
package ca.uhn.fhir.starter;
import java.io.FileNotFoundException;
import java.io.IOException;
public class TinderMojo {
public static void main(String[] args) throws Exception {
ValueSetParser vsp = new ValueSetParser();
vsp.setDirectory("src/test/resources/vs/");
vsp.parse();
DatatypeParser dtp = new DatatypeParser();
dtp.setDirectory("src/test/resources/dt");
dtp.parse();
ResourceParser rp = new ResourceParser();
rp.setDirectory("src/test/resources/res");
rp.parse();
}
}

View File

@ -34,23 +34,21 @@ public class ValueSetParser {
public static void main(String[] args) throws FileNotFoundException, IOException {
ValueSetParser p = new ValueSetParser();
p.setDirectory("src/test/resources/vs/");
// p.setOutputDirectory("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/valueset/");
p.setOutputDirectory("target/generated/valuesets/ca/uhn/fhir/model/dstu/valueset");
p.parse();
// p.setOutputDirectory("target/generated/valuesets/ca/uhn/fhir/model/dstu/valueset");
// p.parse();
}
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ValueSetParser.class);
private void parse() throws FileNotFoundException, IOException {
public void parse() throws FileNotFoundException, IOException {
String string = IOUtils.toString(new FileReader(myDirectory + "valuesets.xml"));
Bundle bundle = new FhirContext(ValueSet.class).newXmlParser().parseBundle(string);
int vsCount = 0;
int conceptCount = 0;
List<ca.uhn.fhir.starter.model.ValueSetTm> valueSets = new ArrayList<>();
List<ca.uhn.fhir.starter.model.ValueSetTm> valueSets = new ArrayList<ValueSetTm>();
for (BundleEntry next : bundle.getEntries()) {
ValueSet nextVs = (ValueSet) next.getResource();
conceptCount += nextVs.getDefine().getConcept().size();
@ -74,16 +72,14 @@ public class ValueSetParser {
vs.getCodes().add(new Code(nextCodeValue, nextCodeDisplay, nextCodeDefinition));
}
if (vsCount > 5) {
break;
}
// if (vsCount > 5) {
// break;
// }
}
write(valueSets);
}
private void write(List<ValueSetTm> theValueSets) throws IOException {
public void write(List<ValueSetTm> theValueSets) throws IOException {
for (ValueSetTm nextValueSetTm : theValueSets) {
write(nextValueSetTm);
}
@ -128,10 +124,14 @@ public class ValueSetParser {
if (next.startsWith("(") && next.endsWith(")")) {
continue;
}
if (StringUtils.isAllUpperCase(next)) {
next = WordUtils.capitalize(next);
next = next.replace("/", "");
next = next.replace("-", "");
next = next.replace(',', '_');
next = next.replace('.', '_');
if (next.contains(" ")) {
next = WordUtils.capitalizeFully(next);
}
b.append(WordUtils.capitalize(next));
b.append(next);
}
return b.toString();
}

View File

@ -65,7 +65,7 @@ public class ValueSetTm {
private String myDisplay;
public Code(String theCode, String theDisplay, String theDefinition) {
myCode =theCode.trim();
myCode = theCode.trim();
myDisplay = theDisplay;
myDefinition = theDefinition;
}
@ -75,10 +75,27 @@ public class ValueSetTm {
}
public String getCodeEnumValue() {
String retVal = myCode.toUpperCase().replace(' ', '_');
String retVal = myCode.toUpperCase().replace(' ', '_').replace('-', '_').replace('/', '_').replace('.', '_');
if ("=".equals(retVal)) {
retVal = "EQUALS";
}
if ("<=".equals(retVal)) {
retVal = "LESSTHAN_OR_EQUALS";
}
if ("<".equals(retVal)) {
retVal = "LESSTHAN";
}
if (">=".equals(retVal)) {
retVal = "GREATERTHAN_OR_EQUALS";
}
if (">".equals(retVal)) {
retVal = "GREATERTHAN";
}
if (!Character.isJavaIdentifierStart(retVal.charAt(0))) {
retVal = '_' + retVal;
}
return retVal;
}

View File

@ -9,13 +9,13 @@ public class ${valueSet.className} extends ValueSetEnumeration {
* Identifier for this Value Set:
* ${valueSet.id}
*/
public static final String IDENTIFIER = "${valueSet.id}";
public static final String VALUESET_IDENTIFIER = "${valueSet.id}";
/**
* Constructor - should not be called directly
*/
protected ${valueSet.className}(String theCode) {
super(theCode, IDENTIFIER);
super(theCode, VALUESET_IDENTIFIER);
}
#foreach ($code in $valueSet.codes)