Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FML transform performance tuning #1704

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ public Base setProperty(int hash, String name, Base value) throws FHIRException
} else {
Element ne = new Element(child).setFormat(format);
children.add(ne);
numberChildren();
ne.index = children.getSizeByName(ne.getListName()) - 1;
childForValue = ne;
break;
}
Expand Down Expand Up @@ -563,7 +563,7 @@ public Element makeElement(String name) throws FHIRException {
} else {
Element ne = new Element(child).setFormat(format);
children.add(ne);
numberChildren();
ne.index = children.getSizeByName(ne.getListName()) - 1;
return ne;
}
}
Expand All @@ -573,6 +573,7 @@ public Element makeElement(String name) throws FHIRException {
if (p.getName().equals(name)) {
Element ne = new Element(name, p).setFormat(format);
children.add(ne);
ne.index = children.getSizeByName(ne.getListName()) - 1;
return ne;
} else if (p.getDefinition().isChoice() && name.startsWith(p.getName().replace("[x]", ""))) {
String type = name.substring(p.getName().length()-3);
Expand All @@ -582,6 +583,7 @@ public Element makeElement(String name) throws FHIRException {
Element ne = new Element(name, p).setFormat(format);
ne.setType(type);
children.add(ne);
ne.index = children.getSizeByName(ne.getListName()) - 1;
return ne;

}
Expand All @@ -605,6 +607,7 @@ public Element forceElement(String name) throws FHIRException {
if (p.getName().equals(name)) {
Element ne = new Element(name, p).setFormat(format);
children.add(ne);
ne.index = children.getSizeByName(ne.getListName()) - 1;
return ne;
}
}
Expand Down Expand Up @@ -1649,4 +1652,4 @@ public boolean isElided() {
return this.elided;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
import org.hl7.fhir.exceptions.DefinitionException;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.exceptions.FHIRFormatError;
import org.hl7.fhir.r5.conformance.profile.ProfileUtilities;
import org.hl7.fhir.r5.context.ContextUtilities;
import org.hl7.fhir.r5.context.IWorkerContext;
import org.hl7.fhir.r5.elementmodel.Manager.FhirFormat;
import org.hl7.fhir.r5.formats.IParser.OutputStyle;
import org.hl7.fhir.r5.model.StructureDefinition;

Expand Down Expand Up @@ -144,7 +145,11 @@ public static ParserBase makeParser(IWorkerContext context, FhirFormat format) {
}

public static Element build(IWorkerContext context, StructureDefinition sd) {
Property p = new Property(context, sd.getSnapshot().getElementFirstRep(), sd);
return build(context, sd, new ProfileUtilities(context, null, null));
}

public static Element build(IWorkerContext context, StructureDefinition sd, ProfileUtilities profileUtilities) {
Property p = new Property(context, sd.getSnapshot().getElementFirstRep(), sd, profileUtilities, new ContextUtilities(context));
Element e = new Element(p.getName(), p);
e.setPath(sd.getType());
return e;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.hl7.fhir.r5.utils.structuremap;

import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r5.conformance.profile.ProfileUtilities;
import org.hl7.fhir.r5.model.Base;
import org.hl7.fhir.r5.model.Coding;

Expand All @@ -10,7 +11,7 @@ public interface ITransformerServices {
// public boolean validateByValueSet(Coding code, String valuesetId);
public void log(String message); // log internal progress

public Base createType(Object appInfo, String name) throws FHIRException;
public Base createType(Object appInfo, String name, ProfileUtilities profileUtilities) throws FHIRException;

public Base createResource(Object appInfo, Base res, boolean atRootofTransform); // an already created resource is provided; this is to identify/store it

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1787,7 +1787,7 @@ else if (srcVar != null) {
}
}
}
Base res = services != null ? services.createType(context.getAppInfo(), tn) : typeFactory(tn);
Base res = services != null ? services.createType(context.getAppInfo(), tn, profileUtilities) : typeFactory(tn);
if (res.isResource() && !res.fhirType().equals("Parameters")) {
// res.setIdBase(tgt.getParameter().size() > 1 ? getParamString(vars, tgt.getParameter().get(0)) : UUID.randomUUID().toString().toLowerCase());
if (services != null)
Expand Down Expand Up @@ -1928,7 +1928,7 @@ private Base typeFactory(String tn) {
if (sd == null) {
throw new FHIRException("Unable to create type "+tn);
} else {
return Manager.build(worker, sd);
return Manager.build(worker, sd, profileUtilities);
}
} else {
return ResourceFactory.createResourceOrType(tn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.List;

import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r5.conformance.profile.ProfileUtilities;
import org.hl7.fhir.r5.context.SimpleWorkerContext;
import org.hl7.fhir.r5.elementmodel.Element;
import org.hl7.fhir.r5.elementmodel.Manager;
Expand Down Expand Up @@ -52,6 +53,7 @@ public void testCast() throws IOException, FHIRException {
StructureMap structureMap = scu.parse(fileMap, "cast");
Element target = Manager.build(context, scu.getTargetType(structureMap));
scu.transform(null, source, structureMap, target);
checkNumberChildren(target, "");
FHIRPathEngine fp = new FHIRPathEngine(context);
Assertions.assertEquals("implicit",fp.evaluateToString(target, "extension[0].value"));
Assertions.assertEquals("explicit",fp.evaluateToString(target, "extension[1].value"));
Expand All @@ -67,6 +69,7 @@ public void testDateOpVariables() throws IOException, FHIRException {
StructureMap structureMap = scu.parse(fileMap, "qr2patfordates");
Element target = Manager.build(context, scu.getTargetType(structureMap));
scu.transform(null, source, structureMap, target);
checkNumberChildren(target, "");
FHIRPathEngine fp = new FHIRPathEngine(context);
assertEquals("2023-10-26", fp.evaluateToString(target, "birthDate"));
assertEquals("2023-09-20T13:19:13.502Z", fp.evaluateToString(target, "deceased"));
Expand All @@ -81,6 +84,7 @@ public void testWhereClause() throws IOException, FHIRException {
StructureMap structureMap = scu.parse(fileMap, "whereclause");
Element target = Manager.build(context, scu.getTargetType(structureMap));
scu.transform(null, source, structureMap, target);
checkNumberChildren(target, "");
FHIRPathEngine fp = new FHIRPathEngine(context);
assertEquals("true", fp.evaluateToString(target, "rest.resource.interaction.where(code='create').exists()"));
}
Expand Down Expand Up @@ -134,13 +138,37 @@ public void testSourceElementDelimiter() throws IOException, FHIRException {
Assertions.assertEquals("-quote", structureMap.getGroup().get(0).getRule().get(1).getSourceFirstRep().getElement());
Assertions.assertEquals("-backtick", structureMap.getGroup().get(0).getRule().get(2).getSourceFirstRep().getElement());
}

// assert indices are equal to Element.numberChildren()
private void checkNumberChildren(Element e, String indent) {
System.out.println(indent + e + ", index: " + e.getIndex());
String last = "";
int index = 0;
for (Element child : e.getChildren()) {
if (child.getProperty().isList()) {
if (last.equals(child.getName())) {
index++;
} else {
last = child.getName();
index = 0;
}
// child.index = index;
Assertions.assertEquals(index, child.getIndex());
} else {
// child.index = -1;
Assertions.assertEquals(-1, child.getIndex());
}
checkNumberChildren(child, indent + " ");
}
}


@Override
public void log(String message) {
}

@Override
public Base createType(Object appInfo, String name) throws FHIRException {
public Base createType(Object appInfo, String name, ProfileUtilities profileUtilities) throws FHIRException {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ public <T> T[] toArray(T[] a) {

@Override
public boolean add(T e) {
map = null;
addToMap(e);
return list.add(e);
}
public void add(int index, T e) {
addToMap(e);
list.add(index, e);
map = null;
}

@Override
Expand All @@ -74,7 +74,9 @@ public boolean containsAll(Collection<?> c) {

@Override
public boolean addAll(Collection<? extends T> c) {
map = null;
for(T e : c) {
addToMap(e);
}
return list.addAll(c);
}

Expand Down Expand Up @@ -115,6 +117,14 @@ public List<T> getByName(String name) {
}
return res;
}

public int getSizeByName(String name) {
if (map == null) {
buildMap();
}
List<T> l = map.get(name);
return l == null ? 0 : l.size();
}

public T get(int c) {
return list.get(c);
Expand All @@ -123,14 +133,22 @@ public T get(int c) {
private void buildMap() {
map = new HashMap<>();
for (T child : list) {
String n = child.getListName();
List<T> l = map.get(n);
if (l == null) {
l = new ArrayList<>();
map.put(n,l);
}
l.add(child);
addToMap(child);
}
}

private void addToMap(T child) {
if (map == null) {
// map will be re-built anyway in next call to getByName
return;
}
String n = child.getListName();
List<T> l = map.get(n);
if (l == null) {
l = new ArrayList<>();
map.put(n,l);
}
l.add(child);
}

public void sort(Comparator<? super T> sorter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;

import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r5.conformance.profile.ProfileUtilities;
import org.hl7.fhir.r5.context.SimpleWorkerContext;
import org.hl7.fhir.r5.elementmodel.Manager;
import org.hl7.fhir.r5.model.Base;
Expand Down Expand Up @@ -34,9 +35,9 @@ public void log(String message) {
}

@Override
public Base createType(Object appInfo, String name) throws FHIRException {
public Base createType(Object appInfo, String name, ProfileUtilities profileUtilities) throws FHIRException {
StructureDefinition sd = context.fetchResource(StructureDefinition.class, name);
return Manager.build(context, sd);
return Manager.build(context, sd, profileUtilities);
}

@Override
Expand Down
Loading