Java Code Examples for org.hl7.fhir.dstu3.model.ElementDefinition#getMin()

The following examples show how to use org.hl7.fhir.dstu3.model.ElementDefinition#getMin() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProfileUtilities.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private String getCardinality(ElementDefinition ed, List<ElementDefinition> list) {
  int min = ed.getMin();
  int max = !ed.hasMax() || ed.getMax().equals("*") ? Integer.MAX_VALUE : Integer.parseInt(ed.getMax());
  while (ed != null && ed.getPath().contains(".")) {
    ed = findParent(ed, list);
    if (ed.getMax().equals("0"))
      max = 0;
    else if (!ed.getMax().equals("1") && !ed.hasSlicing())
      max = Integer.MAX_VALUE;
    if (ed.getMin() == 0)
      min = 0;
  }
  return Integer.toString(min)+".."+(max == Integer.MAX_VALUE ? "*" : Integer.toString(max));
}
 
Example 2
Source File: NarrativeGenerator.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private List<PropertyWrapper> splitExtensions(StructureDefinition profile, List<PropertyWrapper> children) throws UnsupportedEncodingException, IOException, FHIRException {
  List<PropertyWrapper> results = new ArrayList<PropertyWrapper>();
  Map<String, PropertyWrapper> map = new HashMap<String, PropertyWrapper>();
  for (PropertyWrapper p : children)
    if (p.getName().equals("extension") || p.getName().equals("modifierExtension")) {
      // we're going to split these up, and create a property for each url
      if (p.hasValues()) {
        for (BaseWrapper v : p.getValues()) {
          Extension ex  = (Extension) v.getBase();
          String url = ex.getUrl();
          StructureDefinition ed = context.fetchResource(StructureDefinition.class, url);
          if (p.getName().equals("modifierExtension") && ed == null)
            throw new DefinitionException("Unknown modifier extension "+url);
          PropertyWrapper pe = map.get(p.getName()+"["+url+"]");
          if (pe == null) {
            if (ed == null) {
              if (url.startsWith("http://hl7.org/fhir"))
                throw new DefinitionException("unknown extension "+url);
              // System.out.println("unknown extension "+url);
              pe = new PropertyWrapperDirect(new Property(p.getName()+"["+url+"]", p.getTypeCode(), p.getDefinition(), p.getMinCardinality(), p.getMaxCardinality(), ex));
            } else {
              ElementDefinition def = ed.getSnapshot().getElement().get(0);
              pe = new PropertyWrapperDirect(new Property(p.getName()+"["+url+"]", "Extension", def.getDefinition(), def.getMin(), def.getMax().equals("*") ? Integer.MAX_VALUE : Integer.parseInt(def.getMax()), ex));
              ((PropertyWrapperDirect) pe).wrapped.setStructure(ed);
            }
            results.add(pe);
          } else
            pe.getValues().add(v);
        }
      }
    } else
      results.add(p);
  return results;
}
 
Example 3
Source File: ProfileUtilitiesTests.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
/**
 * Change one cardinality.
 * 
 * @param context2
 * @
 * @throws EOperationOutcome 
 */
private void testCardinalityChange() throws EOperationOutcome, Exception {
  StructureDefinition focus = new StructureDefinition();
  StructureDefinition base = context.fetchResource(StructureDefinition.class, "http://hl7.org/fhir/StructureDefinition/Patient").copy();
  focus.setUrl(Utilities.makeUuidUrn());
  focus.setBaseDefinition(base.getUrl());
  focus.setType(base.getType());
  focus.setDerivation(TypeDerivationRule.CONSTRAINT);
  ElementDefinition id = focus.getDifferential().addElement();
  id.setPath("Patient.identifier");
  id.setMin(1);
  List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
  new ProfileUtilities(context, messages, null).generateSnapshot(base, focus, focus.getUrl(), "Simple Test" );

  boolean ok = base.getSnapshot().getElement().size() == focus.getSnapshot().getElement().size();
  for (int i = 0; i < base.getSnapshot().getElement().size(); i++) {
    if (ok) {
      ElementDefinition b = base.getSnapshot().getElement().get(i);
      ElementDefinition f = focus.getSnapshot().getElement().get(i);
      if (!f.hasBase() || !b.getPath().equals(f.getBase().getPath())) 
        ok = false;
      else {
        f.setBase(null);
        if (f.getPath().equals("Patient.identifier")) {
          ok = f.getMin() == 1;
          if (ok)
            f.setMin(0);
        }
        ok = ok && Base.compareDeep(b, f, true);
      }
    }
  }
  
  if (!ok) {
    compareXml(base, focus);
    throw new FHIRException("Snap shot generation chenge cardinality test failed");
  } else 
    System.out.println("Snap shot generation chenge cardinality test passed");
}
 
Example 4
Source File: ShExGenerator.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
/**
 * Generate a ShEx element definition
 * @param sd Containing structure definition
 * @param ed Containing element definition
 * @return ShEx definition
 */
private String genElementDefinition(StructureDefinition sd, ElementDefinition ed) {
  String id = ed.hasBase() ? ed.getBase().getPath() : ed.getPath();
  String shortId = id.substring(id.lastIndexOf(".") + 1);
  String defn;
  ST element_def;
  String card = ("*".equals(ed.getMax()) ? (ed.getMin() == 0 ? "*" : "+") : (ed.getMin() == 0 ? "?" : "")) + ";";

  if(id.endsWith("[x]")) {
    element_def = ed.getType().size() > 1? tmplt(INNER_SHAPE_TEMPLATE) : tmplt(ELEMENT_TEMPLATE);
    element_def.add("id", "");
  } else {
    element_def = tmplt(ELEMENT_TEMPLATE);
    element_def.add("id", "fhir:" + (id.charAt(0) == id.toLowerCase().charAt(0)? shortId : id) + " ");
  }

  List<ElementDefinition> children = ProfileUtilities.getChildList(sd, ed);
  if (children.size() > 0) {
    innerTypes.add(new ImmutablePair<StructureDefinition, ElementDefinition>(sd, ed));
    defn = simpleElement(sd, ed, id);
  } else if(id.endsWith("[x]")) {
    defn = genChoiceTypes(sd, ed, id);
  }
  else if (ed.getType().size() == 1) {
    // Single entry
    defn = genTypeRef(sd, ed, id, ed.getType().get(0));
  } else if (ed.getContentReference() != null) {
    // Reference to another element
    String ref = ed.getContentReference();
    if(!ref.startsWith("#"))
      throw new AssertionError("Not equipped to deal with absolute path references: " + ref);
    String refPath = null;
    for(ElementDefinition ed1: sd.getSnapshot().getElement()) {
      if(ed1.getId() != null && ed1.getId().equals(ref.substring(1))) {
        refPath = ed1.getPath();
        break;
      }
    }
    if(refPath == null)
      throw new AssertionError("Reference path not found: " + ref);
    // String typ = id.substring(0, id.indexOf(".") + 1) + ed.getContentReference().substring(1);
    defn = simpleElement(sd, ed, refPath);
  } else if(id.endsWith("[x]")) {
    defn = genChoiceTypes(sd, ed, id);
  } else {
    // TODO: Refactoring required here
    element_def = genAlternativeTypes(ed, id, shortId);
    element_def.add("id", id.charAt(0) == id.toLowerCase().charAt(0)? shortId : id);
    element_def.add("card", card);
    addComment(element_def, ed);
    return element_def.render();
  }
  element_def.add("defn", defn);
  element_def.add("card", card);
  addComment(element_def, ed);
  return element_def.render();
}
 
Example 5
Source File: QuestionnaireBuilder.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
private void buildGroup(QuestionnaireItemComponent group, StructureDefinition profile, ElementDefinition element,
    List<ElementDefinition> parents, List<QuestionnaireResponse.QuestionnaireResponseItemComponent> answerGroups) throws FHIRException {
 group.setLinkId(element.getPath()); // todo: this will be wrong when we start slicing
 group.setText(element.getShort()); // todo - may need to prepend the name tail... 
 if (element.getComment() != null) {
 	Questionnaire.QuestionnaireItemComponent display = new Questionnaire.QuestionnaireItemComponent();
 	display.setType(QuestionnaireItemType.DISPLAY);
 	display.setText(element.getComment());
 	group.addItem(display);
 }
 group.setType(QuestionnaireItemType.GROUP);
 ToolingExtensions.addFlyOver(group, element.getDefinition());
  group.setRequired(element.getMin() > 0);
  if (element.getMin() > 0)
  	ToolingExtensions.addMin(group, element.getMin());
  group.setRepeats(!element.getMax().equals("1"));
  if (!element.getMax().equals("*"))
  	ToolingExtensions.addMax(group, Integer.parseInt(element.getMax()));

  for (org.hl7.fhir.dstu3.model.QuestionnaireResponse.QuestionnaireResponseItemComponent ag : answerGroups) {
    ag.setLinkId(group.getLinkId());
    ag.setText(group.getText());
  }

  // now, we iterate the children
  List<ElementDefinition> list = ProfileUtilities.getChildList(profile, element);
  for (ElementDefinition child : list) {

    if (!isExempt(element, child) && !parents.contains(child)) {
		List<ElementDefinition> nparents = new ArrayList<ElementDefinition>();
      nparents.addAll(parents);
      nparents.add(child);
      QuestionnaireItemComponent childGroup = group.addItem();
      childGroup.setType(QuestionnaireItemType.GROUP);

      List<QuestionnaireResponse.QuestionnaireResponseItemComponent> nResponse = new ArrayList<QuestionnaireResponse.QuestionnaireResponseItemComponent>();
      processExisting(child.getPath(), answerGroups, nResponse);
      // if the element has a type, we add a question. else we add a group on the basis that
      // it will have children of its own
      if (child.getType().isEmpty() || isAbstractType(child.getType())) 
        buildGroup(childGroup, profile, child, nparents, nResponse);
      else if (isInlineDataType(child.getType()))
        buildGroup(childGroup, profile, child, nparents, nResponse); // todo: get the right children for this one...
      else
        buildQuestion(childGroup, profile, child, child.getPath(), nResponse);
    }
  }
}
 
Example 6
Source File: QuestionnaireBuilder.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
private void buildQuestion(QuestionnaireItemComponent group, StructureDefinition profile, ElementDefinition element, String path, List<QuestionnaireResponse.QuestionnaireResponseItemComponent> answerGroups) throws FHIRException {
    group.setLinkId(path);

    // in this context, we don't have any concepts to mark...
    group.setText(element.getShort()); // prefix with name?
    group.setRequired(element.getMin() > 0);
   if (element.getMin() > 0)
   	ToolingExtensions.addMin(group, element.getMin());
    group.setRepeats(!element.getMax().equals('1'));
   if (!element.getMax().equals("*"))
   	ToolingExtensions.addMax(group, Integer.parseInt(element.getMax()));

    for (QuestionnaireResponse.QuestionnaireResponseItemComponent ag : answerGroups) {
      ag.setLinkId(group.getLinkId());
      ag.setText(group.getText());
    }

    if (!Utilities.noString(element.getComment())) 
      ToolingExtensions.addFlyOver(group, element.getDefinition()+" "+element.getComment());
    else
      ToolingExtensions.addFlyOver(group, element.getDefinition());

    if (element.getType().size() > 1 || element.getType().get(0).getCode().equals("*")) {
      List<TypeRefComponent> types = expandTypeList(element.getType());
      Questionnaire.QuestionnaireItemComponent q = addQuestion(group, QuestionnaireItemType.CHOICE, element.getPath(), "_type", "type", null, makeTypeList(profile, types, element.getPath()));
        for (TypeRefComponent t : types) {
          Questionnaire.QuestionnaireItemComponent sub = q.addItem();
          sub.setType(QuestionnaireItemType.GROUP);
          sub.setLinkId(element.getPath()+"._"+t.getUserData("text"));
          sub.setText((String) t.getUserData("text"));
          // always optional, never repeats

          List<QuestionnaireResponse.QuestionnaireResponseItemComponent> selected = new ArrayList<QuestionnaireResponse.QuestionnaireResponseItemComponent>();
          selectTypes(profile, sub, t, answerGroups, selected);
          processDataType(profile, sub, element, element.getPath()+"._"+t.getUserData("text"), t, selected);
        }
    } else
      // now we have to build the question panel for each different data type
      processDataType(profile, group, element, element.getPath(), element.getType().get(0), answerGroups);

}
 
Example 7
Source File: ProfileUtilitiesTests.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
/**
 * we're going to slice Patient.extension and refer to extension by profile
 * 
 * implicit: whether to rely on implicit extension slicing
 */
private void testSlicingExtension(boolean implicit) throws EOperationOutcome, Exception {
  
  StructureDefinition focus = new StructureDefinition();
  StructureDefinition base = context.fetchResource(StructureDefinition.class, "http://hl7.org/fhir/StructureDefinition/Patient").copy();
  focus.setUrl(Utilities.makeUuidUrn());
  focus.setBaseDefinition(base.getUrl());
  focus.setType(base.getType());
  focus.setDerivation(TypeDerivationRule.CONSTRAINT);
  
  // set the slice up
  ElementDefinition id;
  if (!implicit) {
    id = focus.getDifferential().addElement();
    id.setPath("Patient.extension");
    id.getSlicing().setOrdered(false).setRules(SlicingRules.OPEN).addDiscriminator().setPath("url").setType(DiscriminatorType.VALUE);
    id.setMax("3");
  }
  // first slice: 
  id = focus.getDifferential().addElement();
  id.setPath("Patient.extension");
  id.setSliceName("name1");
  id.addType().setCode("Extension").setProfile("http://hl7.org/fhir/StructureDefinition/patient-birthTime");
  id.setMin(1);
  
  // second slice:
  id = focus.getDifferential().addElement();
  id.setPath("Patient.extension");
  id.setSliceName("name2");
  id.addType().setCode("Extension").setProfile("http://hl7.org/fhir/StructureDefinition/patient-mothersMaidenName");    
  
  List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
  ProfileUtilities pu = new ProfileUtilities(context, messages, null);
  pu.generateSnapshot(base, focus, focus.getUrl(), "Simple Test" );

  // 2 different: extension slices 
  boolean ok = base.getSnapshot().getElement().size() == focus.getSnapshot().getElement().size() - 2;
  for (int i = 0; i < base.getSnapshot().getElement().size(); i++) {
    if (ok) {
      ElementDefinition b = base.getSnapshot().getElement().get(i);
      ElementDefinition f = focus.getSnapshot().getElement().get(i <= 7 ? i : i + 2);
      if (!f.hasBase() || !b.getPath().equals(f.getBase().getPath())) 
        ok = false;
      else {
        f.setBase(null);
        if (f.getPath().equals("Patient.extension")) {
          ok = f.hasSlicing() && (implicit || f.getMax().equals("3"));
          if (ok) {
            f.setSlicing(null);
            f.setMaxElement(b.getMaxElement());
          }
        }            
        if (!f.getPath().equals("Patient.extension")) // no compare that because the definitions get overwritten 
          ok = Base.compareDeep(b, f, true);
      }
    }
  }
  // now, check that the slices we skipped are correct:
  if (ok) {
    ElementDefinition d1 = focus.getSnapshot().getElement().get(8);
    ElementDefinition d2 = focus.getSnapshot().getElement().get(9);
    ok = d1.hasType() && d1.getType().get(0).hasProfile() && d2.hasType() && d2.getType().get(0).hasProfile() && !Base.compareDeep(d1.getType(), d2.getType(), true) &&
          d1.getMin() == 1 && d2.getMin() == 0 && d1.getMax().equals("1") && d2.getMax().equals("1");
    if (ok) {
      d1.getType().clear();
      d2.getType().clear();
      d1.setSliceName("x");
      d2.setSliceName("x");
      d1.setMin(0);
    }
    ok = Base.compareDeep(d1, d2, true);
    // for throughness, we could check against extension too, but this is not done now.
  }
  
  if (!ok) {
    compareXml(base, focus);
    throw new FHIRException("Snap shot generation slicing extensions simple ("+(implicit ? "implicit" : "not implicit")+") failed");
  } else 
    System.out.println("Snap shot generation slicing extensions simple ("+(implicit ? "implicit" : "not implicit")+") passed");
}