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,10 +3,9 @@ package ca.uhn.fhir.model.api;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public abstract class ValueSetEnumeration { 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 String myCode;
private final int myOrdinal; private final int myOrdinal;
@ -25,32 +24,31 @@ public abstract class ValueSetEnumeration {
} }
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 Map<String, ValueSetEnumeration> myCodeMap = new HashMap<String, ValueSetEnumeration>();
private String myValueSetIdentifier;
private int myNextOrdinal = 0; private int myNextOrdinal = 0;
private String myValueSetIdentifier;
public CodeMap(String theValueSetIdentifier) { public CodeMap(String theValueSetIdentifier) {
myValueSetIdentifier = theValueSetIdentifier; myValueSetIdentifier = theValueSetIdentifier;
} }
public int getNextOrdinal() {
return myNextOrdinal;
}
public int nextOrdinal() {
return myNextOrdinal++;
}
public void addCode(ValueSetEnumeration theValueSetEnumeration) { public void addCode(ValueSetEnumeration theValueSetEnumeration) {
myCodeMap.put(theValueSetEnumeration.getCode(), theValueSetEnumeration); myCodeMap.put(theValueSetEnumeration.getCode(), theValueSetEnumeration);
} }
public int nextOrdinal() {
return myNextOrdinal++;
} }
public String getCode() {
return myCode;
} }
} }

View File

@ -12,6 +12,7 @@ import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
@ -48,6 +49,7 @@ public abstract class BaseParser {
private String myDirectory; private String myDirectory;
private ArrayList<Extension> myExtensions; private ArrayList<Extension> myExtensions;
private String myOutputFile; private String myOutputFile;
private List<Resource> myResources = new ArrayList<Resource>();
public void parse() throws Exception { public void parse() throws Exception {
File baseDir = new File(myDirectory); File baseDir = new File(myDirectory);
@ -55,7 +57,11 @@ public abstract class BaseParser {
throw new Exception(myDirectory + " does not exist or is not a directory"); throw new Exception(myDirectory + " does not exist or is not a directory");
} }
File resourceSpreadsheetFile = new File(baseDir, getFilename()); for (File nextFile : baseDir.listFiles()) {
if (isSpreadsheet(nextFile.getAbsolutePath())) {
ourLog.info("Scanning file: {}", nextFile.getAbsolutePath());
File resourceSpreadsheetFile = nextFile;
if (resourceSpreadsheetFile.exists() == false) { if (resourceSpreadsheetFile.exists() == false) {
throw new Exception(resourceSpreadsheetFile.getAbsolutePath() + " does not exist"); throw new Exception(resourceSpreadsheetFile.getAbsolutePath() + " does not exist");
} }
@ -72,13 +78,14 @@ public abstract class BaseParser {
Element resourceRow = (Element) rows.item(1); Element resourceRow = (Element) rows.item(1);
Resource resource = new Resource(); Resource resource = new Resource();
myResources.add(resource);
parseBasicElements(resourceRow, resource); parseBasicElements(resourceRow, resource);
Map<String, BaseElement> elements = new HashMap<String, BaseElement>(); Map<String, BaseElement> elements = new HashMap<String, BaseElement>();
elements.put(resource.getElementName(), resource); elements.put(resource.getElementName(), resource);
// Map<String,String> blockFullNameToShortName = new HashMap<String,String>(); // Map<String,String> blockFullNameToShortName = new HashMap<String,String>();
for (int i = 2; i < rows.getLength(); i++) { for (int i = 2; i < rows.getLength(); i++) {
Element nextRow = (Element) rows.item(i); Element nextRow = (Element) rows.item(i);
@ -93,8 +100,8 @@ public abstract class BaseParser {
if (StringUtils.isBlank(type) || type.startsWith("=")) { if (StringUtils.isBlank(type) || type.startsWith("=")) {
elem = new ResourceBlock(); elem = new ResourceBlock();
} else if (type.startsWith("@")) { } else if (type.startsWith("@")) {
// type = type.substring(type.lastIndexOf('.')+1); // type = type.substring(type.lastIndexOf('.')+1);
elem=new ResourceBlockCopy(); elem = new ResourceBlockCopy();
} else if (type.equals("*")) { } else if (type.equals("*")) {
elem = new AnyChild(); elem = new AnyChild();
} else { } else {
@ -119,10 +126,12 @@ public abstract class BaseParser {
} }
write(resource); }
} }
ourLog.info("Parsed {} resources", myResources.size());
}
@ -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) { for (Annotation next : theAnnotations) {
if (theClass.equals(next.annotationType())) { if (theClass.equals(next.annotationType())) {
return (ca.uhn.fhir.model.api.annotation.SimpleSetter.Parameter) next; return (ca.uhn.fhir.model.api.annotation.SimpleSetter.Parameter) next;
@ -235,8 +245,6 @@ public abstract class BaseParser {
w.close(); w.close();
} }
protected abstract String getFilename();
protected abstract String getTemplate(); protected abstract String getTemplate();
protected void parseBasicElements(Element theRowXml, BaseElement theTarget) { protected void parseBasicElements(Element theRowXml, BaseElement theTarget) {
@ -245,7 +253,6 @@ public abstract class BaseParser {
theTarget.setElementName(name); theTarget.setElementName(name);
String cardValue = cellValue(theRowXml, myColCard); String cardValue = cellValue(theRowXml, myColCard);
if (cardValue != null && cardValue.contains("..")) { if (cardValue != null && cardValue.contains("..")) {
String[] split = cardValue.split("\\.\\."); String[] split = cardValue.split("\\.\\.");
@ -290,4 +297,8 @@ public abstract class BaseParser {
return null; 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 { public class DatatypeParser extends BaseParser {
private String myDatatypeName;
@Override @Override
protected String getTemplate() { protected String getTemplate() {
return "/dt_composite.vm"; return "/dt_composite.vm";
} }
public void setDatatypeName(String theString) {
myDatatypeName=theString;
}
@Override
protected String getFilename() {
return myDatatypeName.toLowerCase() + ".xml";
}
// @Override // @Override
// protected void parseBasicElements(Element theRowXml, BaseElement theTarget) { // protected void parseBasicElements(Element theRowXml, BaseElement theTarget) {

View File

@ -5,160 +5,116 @@ import java.util.List;
public class ResourceParser extends BaseParser { 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 @Override
protected String getTemplate() { protected String getTemplate() {
return "/resource.vm"; return "/resource.vm";
} }
@Override
protected boolean isSpreadsheet(String theFileName) {
return theFileName.endsWith("spreadsheet.xml");
}
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
ResourceParser p = new ResourceParser(); // ResourceParser p = new ResourceParser();
p.setAllDatatypes(new ArrayList<String>()); // 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");
// p.setDirectory("src/test/resources/res"); // 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"); // // TODO: this needs to be properly populated
// ArrayList<Extension> exts = new ArrayList<Extension>(); // p.getAllDatatypes().add("String");
// Extension ext1 = new Extension("foo1", "http://foo/1", "string"); // p.getAllDatatypes().add("Date");
// exts.add(ext1); // p.getAllDatatypes().add("DateTime");
// 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.setDirectory("src/test/resources/res");
// p.setExtensions(exts); //// 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.parse();
//
// p.setDirectory("src/test/resources/res");
p.setResourceName("medication"); // p.setResourceName("substance");
p.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/resource/Medication.java"); // p.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/resource/Substance.java");
p.parse(); // p.parse();
//
p.setDirectory("src/test/resources/res"); //
p.setResourceName("substance"); // p.setDirectory("src/test/resources/res");
p.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/resource/Substance.java"); // p.setResourceName("valueset");
p.parse(); // p.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/resource/ValueSetTm.java");
// p.parse();
//
p.setDirectory("src/test/resources/res"); // p.setDirectory("src/test/resources/res");
p.setResourceName("valueset"); // p.setResourceName("observation");
p.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/resource/ValueSetTm.java"); // p.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/resource/Observation.java");
p.parse(); // p.parse();
//
p.setDirectory("src/test/resources/res"); // p.setResourceName("profile");
p.setResourceName("observation"); // p.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/resource/Profile.java");
p.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/resource/Observation.java"); // p.parse();
p.parse(); //
// p.setResourceName("device");
p.setResourceName("profile"); // p.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/resource/Device.java");
p.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/resource/Profile.java"); // p.parse();
p.parse(); //
// p.setResourceName("group");
p.setResourceName("device"); // p.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/resource/Group.java");
p.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/resource/Device.java"); // p.parse();
p.parse(); //
// p.setResourceName("location");
p.setResourceName("group"); // p.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/resource/Location.java");
p.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/resource/Group.java"); // p.parse();
p.parse(); //
// p.setResourceName("organization");
p.setResourceName("location"); // p.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/resource/Organization.java");
p.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/resource/Location.java"); // p.parse();
p.parse(); //
// p.setResourceName("patient");
p.setResourceName("organization"); // p.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/resource/Patient.java");
p.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/resource/Organization.java"); // p.parse();
p.parse(); //
// p.setResourceName("specimen");
p.setResourceName("patient"); // p.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/resource/Specimen.java");
p.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/resource/Patient.java"); // p.parse();
p.parse(); //
// p.setResourceName("practitioner");
p.setResourceName("specimen"); // p.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/resource/Practitioner.java");
p.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/resource/Specimen.java"); // p.parse();
p.parse(); //
// DatatypeParser d = new DatatypeParser();
p.setResourceName("practitioner"); // d.setDirectory("src/test/resources/dt");
p.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/resource/Practitioner.java"); // d.setDatatypeName("humanname");
p.parse(); // d.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/composite/HumanNameDt.java");
// d.parse();
DatatypeParser d = new DatatypeParser(); //
d.setDirectory("src/test/resources/dt"); // d.setDatatypeName("contact");
d.setDatatypeName("humanname"); // d.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/composite/ContactDt.java");
d.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/composite/HumanNameDt.java"); // d.parse();
d.parse(); //
// d.setDatatypeName("address");
d.setDatatypeName("contact"); // d.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/composite/AddressDt.java");
d.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/composite/ContactDt.java"); // d.parse();
d.parse(); //
// d.setDatatypeName("narrative");
d.setDatatypeName("address"); // d.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/composite/NarrativeDt.java");
d.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/composite/AddressDt.java"); // d.parse();
d.parse(); //
// d.setDatatypeName("quantity");
d.setDatatypeName("narrative"); // d.setOutputFile(basePath + "/ca/uhn/fhir/model/dstu/composite/QuantityDt.java");
d.setOutputFile("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/composite/NarrativeDt.java"); // d.parse();
d.parse();
d.setDatatypeName("quantity");
d.setOutputFile("../hapi-fhir-base/src/main/java/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 { 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("../hapi-fhir-base/src/main/java/ca/uhn/fhir/model/dstu/valueset/");
p.setOutputDirectory("target/generated/valuesets/ca/uhn/fhir/model/dstu/valueset"); // p.setOutputDirectory("target/generated/valuesets/ca/uhn/fhir/model/dstu/valueset");
p.parse(); // p.parse();
} }
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ValueSetParser.class); 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")); String string = IOUtils.toString(new FileReader(myDirectory + "valuesets.xml"));
Bundle bundle = new FhirContext(ValueSet.class).newXmlParser().parseBundle(string); Bundle bundle = new FhirContext(ValueSet.class).newXmlParser().parseBundle(string);
int vsCount = 0; int vsCount = 0;
int conceptCount = 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()) { for (BundleEntry next : bundle.getEntries()) {
ValueSet nextVs = (ValueSet) next.getResource(); ValueSet nextVs = (ValueSet) next.getResource();
conceptCount += nextVs.getDefine().getConcept().size(); conceptCount += nextVs.getDefine().getConcept().size();
@ -74,16 +72,14 @@ public class ValueSetParser {
vs.getCodes().add(new Code(nextCodeValue, nextCodeDisplay, nextCodeDefinition)); vs.getCodes().add(new Code(nextCodeValue, nextCodeDisplay, nextCodeDefinition));
} }
if (vsCount > 5) { // if (vsCount > 5) {
break; // break;
// }
} }
}
write(valueSets);
} }
private void write(List<ValueSetTm> theValueSets) throws IOException { public void write(List<ValueSetTm> theValueSets) throws IOException {
for (ValueSetTm nextValueSetTm : theValueSets) { for (ValueSetTm nextValueSetTm : theValueSets) {
write(nextValueSetTm); write(nextValueSetTm);
} }
@ -128,10 +124,14 @@ public class ValueSetParser {
if (next.startsWith("(") && next.endsWith(")")) { if (next.startsWith("(") && next.endsWith(")")) {
continue; continue;
} }
if (StringUtils.isAllUpperCase(next)) { next = next.replace("/", "");
next = WordUtils.capitalize(next); 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(); return b.toString();
} }

View File

@ -65,7 +65,7 @@ public class ValueSetTm {
private String myDisplay; private String myDisplay;
public Code(String theCode, String theDisplay, String theDefinition) { public Code(String theCode, String theDisplay, String theDefinition) {
myCode =theCode.trim(); myCode = theCode.trim();
myDisplay = theDisplay; myDisplay = theDisplay;
myDefinition = theDefinition; myDefinition = theDefinition;
} }
@ -75,10 +75,27 @@ public class ValueSetTm {
} }
public String getCodeEnumValue() { 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))) { if (!Character.isJavaIdentifierStart(retVal.charAt(0))) {
retVal = '_' + retVal; retVal = '_' + retVal;
} }
return retVal; return retVal;
} }

View File

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