001package org.hl7.fhir.dstu3.terminologies;
002
003import static org.apache.commons.lang3.StringUtils.isNotBlank;
004
005import java.io.FileNotFoundException;
006import java.io.IOException;
007
008/*
009 * Copyright (c) 2011+, HL7, Inc
010 * All rights reserved.
011 * 
012 * Redistribution and use in source and binary forms, with or without modification,
013 * are permitted provided that the following conditions are met:
014 * 
015 * Redistributions of source code must retain the above copyright notice, this
016 * list of conditions and the following disclaimer.
017 * Redistributions in binary form must reproduce the above copyright notice,
018 * this list of conditions and the following disclaimer in the documentation
019 * and/or other materials provided with the distribution.
020 * Neither the name of HL7 nor the names of its contributors may be used to
021 * endorse or promote products derived from this software without specific
022 * prior written permission.
023 * 
024 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
025 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
026 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
027 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
028 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
029 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
030 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
031 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
032 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
033 * POSSIBILITY OF SUCH DAMAGE.
034 * 
035 */
036
037import java.util.ArrayList;
038import java.util.HashMap;
039import java.util.HashSet;
040import java.util.List;
041import java.util.Map;
042import java.util.Set;
043import java.util.UUID;
044
045import org.apache.commons.lang3.NotImplementedException;
046import org.hl7.fhir.dstu3.context.IWorkerContext;
047import org.hl7.fhir.dstu3.model.CodeSystem;
048import org.hl7.fhir.dstu3.model.CodeSystem.CodeSystemContentMode;
049import org.hl7.fhir.dstu3.model.CodeSystem.ConceptDefinitionComponent;
050import org.hl7.fhir.dstu3.model.CodeSystem.ConceptDefinitionDesignationComponent;
051import org.hl7.fhir.dstu3.model.DateTimeType;
052import org.hl7.fhir.dstu3.model.ExpansionProfile;
053import org.hl7.fhir.dstu3.model.Factory;
054import org.hl7.fhir.dstu3.model.PrimitiveType;
055import org.hl7.fhir.dstu3.model.Type;
056import org.hl7.fhir.dstu3.model.UriType;
057import org.hl7.fhir.dstu3.model.ValueSet;
058import org.hl7.fhir.dstu3.model.ValueSet.ConceptReferenceComponent;
059import org.hl7.fhir.dstu3.model.ValueSet.ConceptReferenceDesignationComponent;
060import org.hl7.fhir.dstu3.model.ValueSet.ConceptSetComponent;
061import org.hl7.fhir.dstu3.model.ValueSet.ConceptSetFilterComponent;
062import org.hl7.fhir.dstu3.model.ValueSet.FilterOperator;
063import org.hl7.fhir.dstu3.model.ValueSet.ValueSetComposeComponent;
064import org.hl7.fhir.dstu3.model.ValueSet.ValueSetExpansionComponent;
065import org.hl7.fhir.dstu3.model.ValueSet.ValueSetExpansionContainsComponent;
066import org.hl7.fhir.dstu3.model.ValueSet.ValueSetExpansionParameterComponent;
067import org.hl7.fhir.dstu3.utils.ToolingExtensions;
068import org.hl7.fhir.exceptions.FHIRException;
069import org.hl7.fhir.exceptions.FHIRFormatError;
070import org.hl7.fhir.exceptions.NoTerminologyServiceException;
071import org.hl7.fhir.exceptions.TerminologyServiceException;
072import org.hl7.fhir.utilities.Utilities;
073
074public class ValueSetExpanderSimple implements ValueSetExpander {
075
076  private List<ValueSetExpansionContainsComponent> codes = new ArrayList<ValueSet.ValueSetExpansionContainsComponent>();
077  private List<ValueSetExpansionContainsComponent> roots = new ArrayList<ValueSet.ValueSetExpansionContainsComponent>();
078  private Map<String, ValueSetExpansionContainsComponent> map = new HashMap<String, ValueSet.ValueSetExpansionContainsComponent>();
079  private IWorkerContext context;
080  private boolean canBeHeirarchy = true;
081  private Set<String> excludeKeys = new HashSet<String>();
082  private Set<String> excludeSystems = new HashSet<String>();
083  private ValueSetExpanderFactory factory;
084  private ValueSet focus;
085  private int maxExpansionSize = 500;
086
087  private int total;
088
089  public ValueSetExpanderSimple(IWorkerContext context, ValueSetExpanderFactory factory) {
090    super();
091    this.context = context;
092    this.factory = factory;
093  }
094
095  public void setMaxExpansionSize(int theMaxExpansionSize) {
096    maxExpansionSize = theMaxExpansionSize;
097  }
098
099  private ValueSetExpansionContainsComponent addCode(String system, String code, String display, ValueSetExpansionContainsComponent parent, List<ConceptDefinitionDesignationComponent> designations,
100      ExpansionProfile profile, boolean isAbstract, boolean inactive, List<ValueSet> filters) {
101    if (filters != null && !filters.isEmpty() && !filterContainsCode(filters, system, code))
102      return null;
103    ValueSetExpansionContainsComponent n = new ValueSet.ValueSetExpansionContainsComponent();
104    n.setSystem(system);
105    n.setCode(code);
106    if (isAbstract)
107      n.setAbstract(true);
108    if (inactive)
109      n.setInactive(true);
110
111    if (profile.getIncludeDesignations() && designations != null) {
112      for (ConceptDefinitionDesignationComponent t : designations) {
113        ToolingExtensions.addLanguageTranslation(n, t.getLanguage(), t.getValue());
114      }
115    }
116    ConceptDefinitionDesignationComponent t = profile.hasLanguage() ? getMatchingLang(designations, profile.getLanguage()) : null;
117    if (t == null)
118      n.setDisplay(display);
119    else
120      n.setDisplay(t.getValue());
121
122    String s = key(n);
123    if (map.containsKey(s) || excludeKeys.contains(s)) {
124      canBeHeirarchy = false;
125    } else {
126      codes.add(n);
127      map.put(s, n);
128      total++;
129    }
130    if (canBeHeirarchy && parent != null) {
131      parent.getContains().add(n);
132    } else {
133      roots.add(n);
134    }
135    return n;
136  }
137
138  private boolean filterContainsCode(List<ValueSet> filters, String system, String code) {
139    for (ValueSet vse : filters)
140      if (expansionContainsCode(vse.getExpansion().getContains(), system, code))
141        return true;
142    return false;
143  }
144
145  private boolean expansionContainsCode(List<ValueSetExpansionContainsComponent> contains, String system, String code) {
146    for (ValueSetExpansionContainsComponent cc : contains) {
147      if (system.equals(cc.getSystem()) && code.equals(cc.getCode()))
148        return true;
149      if (expansionContainsCode(cc.getContains(), system, code))
150        return true;
151    }
152    return false;
153  }
154
155  private ConceptDefinitionDesignationComponent getMatchingLang(List<ConceptDefinitionDesignationComponent> list, String lang) {
156    for (ConceptDefinitionDesignationComponent t : list)
157      if (t.getLanguage().equals(lang))
158        return t;
159    for (ConceptDefinitionDesignationComponent t : list)
160      if (t.getLanguage().startsWith(lang))
161        return t;
162    return null;
163  }
164
165  private void addCodeAndDescendents(CodeSystem cs, String system, ConceptDefinitionComponent def, ValueSetExpansionContainsComponent parent, ExpansionProfile profile, List<ValueSet> filters)
166      throws FHIRException {
167    if (!CodeSystemUtilities.isDeprecated(cs, def)) {
168      ValueSetExpansionContainsComponent np = null;
169      boolean abs = CodeSystemUtilities.isNotSelectable(cs, def);
170      boolean inc = CodeSystemUtilities.isInactive(cs, def);
171      if (canBeHeirarchy || !abs)
172        np = addCode(system, def.getCode(), def.getDisplay(), parent, def.getDesignation(), profile, abs, inc, filters);
173      for (ConceptDefinitionComponent c : def.getConcept())
174        addCodeAndDescendents(cs, system, c, np, profile, filters);
175    } else
176      for (ConceptDefinitionComponent c : def.getConcept())
177        addCodeAndDescendents(cs, system, c, null, profile, filters);
178
179  }
180
181  private void addCodes(ValueSetExpansionComponent expand, List<ValueSetExpansionParameterComponent> params, ExpansionProfile profile, List<ValueSet> filters) throws ETooCostly {
182//    if (expand != null) {
183      if (expand.getContains().size() > maxExpansionSize)
184        throw new ETooCostly("Too many codes to display (>" + Integer.toString(expand.getContains().size()) + ")");
185      for (ValueSetExpansionParameterComponent p : expand.getParameter()) {
186        if (!existsInParams(params, p.getName(), p.getValue()))
187          params.add(p);
188      }
189
190      copyImportContains(expand.getContains(), null, profile, filters);
191//    }
192  }
193
194  private void excludeCode(String theSystem, String theCode) {
195    ValueSetExpansionContainsComponent n = new ValueSet.ValueSetExpansionContainsComponent();
196    n.setSystem(theSystem);
197    n.setCode(theCode);
198    String s = key(n);
199    excludeKeys.add(s);
200  }
201
202  private void excludeCodes(ConceptSetComponent exc, List<ValueSetExpansionParameterComponent> params) throws TerminologyServiceException {
203    if (exc.hasSystem() && exc.getConcept().size() == 0 && exc.getFilter().size() == 0) {
204      excludeSystems.add(exc.getSystem());
205    }
206
207    if (exc.hasValueSet())
208      throw new Error("Processing Value set references in exclude is not yet done");
209    // importValueSet(imp.getValue(), params, profile);
210
211    CodeSystem cs = context.fetchCodeSystem(exc.getSystem());
212    if ((cs == null || cs.getContent() != CodeSystemContentMode.COMPLETE) && context.supportsSystem(exc.getSystem())) {
213      excludeCodes(context.expandVS(exc, false), params);
214      return;
215    }
216
217    for (ConceptReferenceComponent c : exc.getConcept()) {
218      excludeCode(exc.getSystem(), c.getCode());
219    }
220
221    if (exc.getFilter().size() > 0)
222      throw new NotImplementedException("not done yet");
223  }
224
225  private void excludeCodes(ValueSetExpansionComponent expand, List<ValueSetExpansionParameterComponent> params) {
226    for (ValueSetExpansionContainsComponent c : expand.getContains()) {
227      excludeCode(c.getSystem(), c.getCode());
228    }
229  }
230
231  private boolean existsInParams(List<ValueSetExpansionParameterComponent> params, String name, Type value) {
232    for (ValueSetExpansionParameterComponent p : params) {
233      if (p.getName().equals(name) && PrimitiveType.compareDeep(p.getValue(), value, false))
234        return true;
235    }
236    return false;
237  }
238
239  @Override
240  public ValueSetExpansionOutcome expand(ValueSet source, ExpansionProfile profile) {
241
242    if (profile == null)
243      profile = makeDefaultExpansion();
244    try {
245      focus = source.copy();
246      focus.setExpansion(new ValueSet.ValueSetExpansionComponent());
247      focus.getExpansion().setTimestampElement(DateTimeType.now());
248      focus.getExpansion().setIdentifier(Factory.createUUID());
249      if (!profile.getUrl().startsWith("urn:uuid:"))
250        focus.getExpansion().addParameter().setName("profile").setValue(new UriType(profile.getUrl()));
251
252      if (source.hasCompose())
253        handleCompose(source.getCompose(), focus.getExpansion().getParameter(), profile);
254
255      if (canBeHeirarchy) {
256        for (ValueSetExpansionContainsComponent c : roots) {
257          focus.getExpansion().getContains().add(c);
258        }
259      } else {
260        for (ValueSetExpansionContainsComponent c : codes) {
261          if (map.containsKey(key(c)) && !c.getAbstract()) { // we may have added abstract codes earlier while we still thought it might be heirarchical, but later we gave up, so now ignore them
262            focus.getExpansion().getContains().add(c);
263            c.getContains().clear(); // make sure any heirarchy is wiped
264          }
265        }
266      }
267
268      if (total > 0) {
269        focus.getExpansion().setTotal(total);
270      }
271
272      return new ValueSetExpansionOutcome(focus);
273    } catch (RuntimeException e) {
274      // TODO: we should put something more specific instead of just Exception below, since
275      // it swallows bugs.. what would be expected to be caught there?
276      throw e;
277    } catch (Exception e) {
278      // well, we couldn't expand, so we'll return an interface to a checker that can check membership of the set
279      // that might fail too, but it might not, later.
280      return new ValueSetExpansionOutcome(new ValueSetCheckerSimple(source, factory, context), e.getMessage(), TerminologyServiceErrorClass.UNKNOWN);
281    }
282  }
283
284  private ExpansionProfile makeDefaultExpansion() {
285    ExpansionProfile res = new ExpansionProfile();
286    res.setUrl("urn:uuid:" + UUID.randomUUID().toString().toLowerCase());
287    res.setExcludeNested(true);
288    res.setIncludeDesignations(false);
289    return res;
290  }
291
292  private void addToHeirarchy(List<ValueSetExpansionContainsComponent> target, List<ValueSetExpansionContainsComponent> source) {
293    for (ValueSetExpansionContainsComponent s : source) {
294      target.add(s);
295    }
296  }
297
298  private String getCodeDisplay(CodeSystem cs, String code) throws TerminologyServiceException {
299    ConceptDefinitionComponent def = getConceptForCode(cs.getConcept(), code);
300    if (def == null)
301      throw new TerminologyServiceException("Unable to find code '" + code + "' in code system " + cs.getUrl());
302    return def.getDisplay();
303  }
304
305  private ConceptDefinitionComponent getConceptForCode(List<ConceptDefinitionComponent> clist, String code) {
306    for (ConceptDefinitionComponent c : clist) {
307      if (code.equals(c.getCode()))
308        return c;
309      ConceptDefinitionComponent v = getConceptForCode(c.getConcept(), code);
310      if (v != null)
311        return v;
312    }
313    return null;
314  }
315
316  private void handleCompose(ValueSetComposeComponent compose, List<ValueSetExpansionParameterComponent> params, ExpansionProfile profile)
317      throws ETooCostly, FileNotFoundException, IOException, FHIRException {
318    // Exclude comes first because we build up a map of things to exclude
319    for (ConceptSetComponent inc : compose.getExclude())
320      excludeCodes(inc, params);
321    canBeHeirarchy = !profile.getExcludeNested() && excludeKeys.isEmpty() && excludeSystems.isEmpty();
322    boolean first = true;
323    for (ConceptSetComponent inc : compose.getInclude()) {
324      if (first == true)
325        first = false;
326      else
327        canBeHeirarchy = false;
328      includeCodes(inc, params, profile);
329    }
330
331  }
332
333  private ValueSet importValueSet(String value, List<ValueSetExpansionParameterComponent> params, ExpansionProfile profile)
334      throws ETooCostly, TerminologyServiceException, FileNotFoundException, IOException, FHIRFormatError {
335    if (value == null)
336      throw new TerminologyServiceException("unable to find value set with no identity");
337    ValueSet vs = context.fetchResource(ValueSet.class, value);
338    if (vs == null)
339      throw new TerminologyServiceException("Unable to find imported value set " + value);
340    ValueSetExpansionOutcome vso = factory.getExpander().expand(vs, profile);
341    if (vso.getError() != null)
342      throw new TerminologyServiceException("Unable to expand imported value set: " + vso.getError());
343    if (vso.getService() != null)
344      throw new TerminologyServiceException("Unable to expand imported value set " + value);
345    if (vs.hasVersion())
346      if (!existsInParams(params, "version", new UriType(vs.getUrl() + "|" + vs.getVersion())))
347        params.add(new ValueSetExpansionParameterComponent().setName("version").setValue(new UriType(vs.getUrl() + "|" + vs.getVersion())));
348    for (ValueSetExpansionParameterComponent p : vso.getValueset().getExpansion().getParameter()) {
349      if (!existsInParams(params, p.getName(), p.getValue()))
350        params.add(p);
351    }
352    canBeHeirarchy = false; // if we're importing a value set, we have to be combining, so we won't try for a heirarchy
353    return vso.getValueset();
354  }
355
356  private void copyImportContains(List<ValueSetExpansionContainsComponent> list, ValueSetExpansionContainsComponent parent, ExpansionProfile profile, List<ValueSet> filter) {
357    for (ValueSetExpansionContainsComponent c : list) {
358      ValueSetExpansionContainsComponent np = addCode(c.getSystem(), c.getCode(), c.getDisplay(), parent, null, profile, c.getAbstract(), c.getInactive(), filter);
359      copyImportContains(c.getContains(), np, profile, filter);
360    }
361  }
362
363  private void includeCodes(ConceptSetComponent inc, List<ValueSetExpansionParameterComponent> params, ExpansionProfile profile) throws ETooCostly, FileNotFoundException, IOException, FHIRException {
364    List<ValueSet> imports = new ArrayList<ValueSet>();
365    for (UriType imp : inc.getValueSet())
366      imports.add(importValueSet(imp.getValue(), params, profile));
367
368    if (!inc.hasSystem()) {
369      if (imports.isEmpty()) // though this is not supposed to be the case
370        return;
371      ValueSet base = imports.get(0);
372      imports.remove(0);
373      copyImportContains(base.getExpansion().getContains(), null, profile, imports);
374    } else {
375      CodeSystem cs = context.fetchCodeSystem(inc.getSystem());
376      if ((cs == null || cs.getContent() != CodeSystemContentMode.COMPLETE) && context.supportsSystem(inc.getSystem())) {
377        addCodes(context.expandVS(inc, canBeHeirarchy), params, profile, imports);
378        return;
379      }
380
381      if (cs == null) {
382        if (context.isNoTerminologyServer())
383          throw new NoTerminologyServiceException("unable to find code system " + inc.getSystem().toString());
384        else
385          throw new TerminologyServiceException("unable to find code system " + inc.getSystem().toString());
386      }
387      if (cs.getContent() != CodeSystemContentMode.COMPLETE)
388        throw new TerminologyServiceException("Code system " + inc.getSystem().toString() + " is incomplete");
389      if (cs.hasVersion())
390        if (!existsInParams(params, "version", new UriType(cs.getUrl() + "|" + cs.getVersion())))
391          params.add(new ValueSetExpansionParameterComponent().setName("version").setValue(new UriType(cs.getUrl() + "|" + cs.getVersion())));
392
393      if (inc.getConcept().size() == 0 && inc.getFilter().size() == 0) {
394        // special case - add all the code system
395        for (ConceptDefinitionComponent def : cs.getConcept()) {
396          addCodeAndDescendents(cs, inc.getSystem(), def, null, profile, imports);
397        }
398      }
399
400      if (!inc.getConcept().isEmpty()) {
401        canBeHeirarchy = false;
402        for (ConceptReferenceComponent c : inc.getConcept()) {
403          addCode(inc.getSystem(), c.getCode(), Utilities.noString(c.getDisplay()) ? getCodeDisplay(cs, c.getCode()) : c.getDisplay(), null, convertDesignations(c.getDesignation()), profile, false,
404              CodeSystemUtilities.isInactive(cs, c.getCode()), imports);
405        }
406      }
407      if (inc.getFilter().size() > 1) {
408        canBeHeirarchy = false; // which will bt the case if we get around to supporting this
409        throw new TerminologyServiceException("Multiple filters not handled yet"); // need to and them, and this isn't done yet. But this shouldn't arise in non loinc and snomed value sets
410      }
411      if (inc.getFilter().size() == 1) {
412        ConceptSetFilterComponent fc = inc.getFilter().get(0);
413        if ("concept".equals(fc.getProperty()) && fc.getOp() == FilterOperator.ISA) {
414          // special: all codes in the target code system under the value
415          ConceptDefinitionComponent def = getConceptForCode(cs.getConcept(), fc.getValue());
416          if (def == null)
417            throw new TerminologyServiceException("Code '" + fc.getValue() + "' not found in system '" + inc.getSystem() + "'");
418          addCodeAndDescendents(cs, inc.getSystem(), def, null, profile, imports);
419        } else if ("concept".equals(fc.getProperty()) && fc.getOp() == FilterOperator.DESCENDENTOF) {
420          // special: all codes in the target code system under the value
421          ConceptDefinitionComponent def = getConceptForCode(cs.getConcept(), fc.getValue());
422          if (def == null)
423            throw new TerminologyServiceException("Code '" + fc.getValue() + "' not found in system '" + inc.getSystem() + "'");
424          for (ConceptDefinitionComponent c : def.getConcept())
425            addCodeAndDescendents(cs, inc.getSystem(), c, null, profile, imports);
426        } else if ("display".equals(fc.getProperty()) && fc.getOp() == FilterOperator.EQUAL) {
427          // gg; note: wtf is this: if the filter is display=v, look up the code 'v', and see if it's diplsay is 'v'?
428          canBeHeirarchy = false;
429          ConceptDefinitionComponent def = getConceptForCode(cs.getConcept(), fc.getValue());
430          if (def != null) {
431            if (isNotBlank(def.getDisplay()) && isNotBlank(fc.getValue())) {
432              if (def.getDisplay().contains(fc.getValue())) {
433                addCode(inc.getSystem(), def.getCode(), def.getDisplay(), null, def.getDesignation(), profile, CodeSystemUtilities.isNotSelectable(cs, def), CodeSystemUtilities.isInactive(cs, def),
434                    imports);
435              }
436            }
437          }
438        } else
439          throw new NotImplementedException("Search by property[" + fc.getProperty() + "] and op[" + fc.getOp() + "] is not supported yet");
440      }
441    }
442  }
443
444  private List<ConceptDefinitionDesignationComponent> convertDesignations(List<ConceptReferenceDesignationComponent> list) {
445    List<ConceptDefinitionDesignationComponent> res = new ArrayList<CodeSystem.ConceptDefinitionDesignationComponent>();
446    for (ConceptReferenceDesignationComponent t : list) {
447      ConceptDefinitionDesignationComponent c = new ConceptDefinitionDesignationComponent();
448      c.setLanguage(t.getLanguage());
449      c.setUse(t.getUse());
450      c.setValue(t.getValue());
451    }
452    return res;
453  }
454
455  private String key(String uri, String code) {
456    return "{" + uri + "}" + code;
457  }
458
459  private String key(ValueSetExpansionContainsComponent c) {
460    return key(c.getSystem(), c.getCode());
461  }
462
463}