public void generate() throws Exception {

    outputFile.createNewFile();
    GenBlock fileBlock = new GenBlock();

    generateMainHeader(fileBlock);

    ElementDefn root = getRootDefinition();
    generateResourceHeader(fileBlock, root);

    // first loop through and generate all of the embedded types from this
    // schema
    for (Iterator<ElementDefn> iterator = root.getElements().iterator(); iterator.hasNext(); ) {
      ElementDefn elementDefinition = iterator.next();
      generateEmbeddedType(outputFile.getParentFile(), fileBlock, elementDefinition);
    }

    generatePostEmbedded(fileBlock, root);

    // next loop through and generate the filed elements
    for (Iterator<ElementDefn> iterator = root.getElements().iterator(); iterator.hasNext(); ) {
      ElementDefn elementDefinition = iterator.next();
      if (!elementDefinition.isXmlAttribute() || xmlAttributeAsField)
        generateElement(fileBlock, elementDefinition);
    }

    generateResourceFooter(fileBlock);
    generateMainFooter(fileBlock);

    Writer modelFile = new BufferedWriter(new FileWriter(outputFile));
    modelFile.write(fileBlock.toString());
    modelFile.flush();
    modelFile.close();
  }
예제 #2
0
  private void genRange(
      Section section, String tn, String en, ElementDefn e, TypeRef tr, boolean datatype)
      throws Exception {
    // metadata
    if (e.isModifier())
      section.triple(
          "fhir:" + tn + "." + en, "fhir:hasFlag", complex().predicate("a", "fhir:isModifier"));
    if (e.isXmlAttribute())
      section.triple(
          "fhir:" + tn + "." + en, "fhir:hasFlag", complex().predicate("a", "fhir:isXmlAtribute"));
    if (e.hasMustSupport() && e.isMustSupport())
      section.triple(
          "fhir:" + tn + "." + en, "fhir:hasFlag", complex().predicate("a", "fhir:isMustSupport"));
    if (e.hasSummaryItem() && e.isSummaryItem())
      section.triple(
          "fhir:" + tn + "." + en, "fhir:hasFlag", complex().predicate("a", "fhir:isSummaryItem"));
    if (!Utilities.noString(e.getW5()))
      section.triple(
          "fhir:" + tn + "." + en, "fhir:w5", complex().predicate("a", "fhir:w5\\#" + e.getW5()));
    if (e.hasMeaningWhenMissing())
      section.triple(
          "fhir:" + tn + "." + en, "fhir:missingMeaning", literal(e.getMeaningWhenMissing()));

    // cardinality
    cardinality(
        section,
        "fhir:" + tn + "." + en,
        e.getMinCardinality().toString(),
        e.getMaxCardinality() == Integer.MAX_VALUE ? "*" : e.getMaxCardinality().toString());
    //    section.triple("fhir:"+tn+"."+en, "fhir:minCardinality",
    // literal(e.getMinCardinality().toString()));
    //    section.triple("fhir:"+tn+"."+en, "fhir:maxCardinality", literal(e.getMaxCardinality() ==
    // Integer.MAX_VALUE ? "*" : e.getMaxCardinality().toString()));

    // now in OWL:
    if (e.getMinCardinality() > 0)
      section.triple(
          "fhir:" + tn,
          "rdfs:subClassOf",
          complex()
              .predicate("a", "owl:Restriction")
              .predicate("owl:onProperty", "fhir:" + tn + "." + en)
              .predicate(
                  "owl:minCardinality",
                  literalTyped(e.getMinCardinality().toString(), "nonNegativeInteger")));
    if (e.getMaxCardinality() < Integer.MAX_VALUE)
      section.triple(
          "fhir:" + tn,
          "rdfs:subClassOf",
          complex()
              .predicate("a", "owl:Restriction")
              .predicate("owl:onProperty", "fhir:" + tn + "." + en)
              .predicate(
                  "owl:maxCardinality",
                  literalTyped(e.getMaxCardinality().toString(), "nonNegativeInteger")));

    // define
    if (tr == null) {
      section.triple("fhir:" + tn + "." + en, "rdfs:range", "fhir:" + e.getDeclaredTypeName());
      anonTypes.push(new AnonTypeInfo(section, e.getDeclaredTypeName(), e, datatype));
    } else if (tr.getName().startsWith("@")) {
      ElementDefn r = getElementForPath(tr.getName().substring(1));
      section.triple("fhir:" + tn + "." + en, "rdfs:range", "fhir:" + r.getDeclaredTypeName());
    } else {
      if (e.hasBinding()) {
        BindingSpecification bs = e.getBinding();
        if (bs.getValueSet() != null) {
          String bn = getPNameForUri(bs.getValueSet().getUrl());
          section.triple("fhir:" + tn + "." + en, "rdfs:range", processType(tr.getName()));
          section.triple("fhir:" + tn + "." + en, "fhir:binding", bn);
          if (!bn.startsWith("vs:")) // a v3 valueset
          valuesets.put(bn, bs.getValueSet());
        } else if (!Utilities.noString(bs.getReference())) {
          section.triple("fhir:" + tn + "." + en, "rdfs:range", processType(tr.getName()));
          section.triple("fhir:" + tn + "." + en, "fhir:binding", "<" + bs.getReference() + ">");
        }
        //        if (bs.hasMax())
        // need to figure out how this should be represented in Turtle
        section.triple(
            "fhir:" + tn + "." + en,
            "fhir:bindingStrength",
            complex().predicate("a", "fhir:binding-strength\\#" + bs.getStrength().toCode()));
      } else section.triple("fhir:" + tn + "." + en, "rdfs:range", processType(tr.getName()));
    }
    if (e.getDefaultValue() != null) {
      if (e.getDefaultValue() instanceof DecimalType)
        section.triple(
            "fhir:" + tn + "." + en,
            "fhir:default",
            complex()
                .predicate("a", "fhir:decimal")
                .predicate(
                    "fhir:value", literal(((DecimalType) e.getDefaultValue()).asStringValue())));
      else if (e.getDefaultValue() instanceof BooleanType)
        section.triple(
            "fhir:" + tn + "." + en,
            "fhir:default",
            complex()
                .predicate("a", "fhir:boolean")
                .predicate(
                    "fhir:value", literal(((BooleanType) e.getDefaultValue()).asStringValue())));
      else if (e.getDefaultValue() instanceof IntegerType)
        section.triple(
            "fhir:" + tn + "." + en,
            "fhir:default",
            complex()
                .predicate("a", "fhir:integer")
                .predicate(
                    "fhir:value", literal(((IntegerType) e.getDefaultValue()).asStringValue())));
      else if (e.getDefaultValue() instanceof CodeType)
        section.triple(
            "fhir:" + tn + "." + en,
            "fhir:default",
            complex()
                .predicate("a", "fhir:integer")
                .predicate(
                    "fhir:value", literal(((CodeType) e.getDefaultValue()).asStringValue())));
      else
        throw new Error(
            "default of type " + e.getDefaultValue().getClass().getName() + " not handled yet");
    }
  }