Search in sources :

Example 31 with org.hl7.fhir.r5.model

use of org.hl7.fhir.r5.model in project kindling by HL7.

the class DefinitionsUsageTracker method usage.

private void usage(org.hl7.fhir.r5.elementmodel.Element instance, ElementDefn definition, String path) throws Exception {
    definition.setCoveredByExample(true);
    for (Element c : instance.getChildren()) {
        String p = c.getProperty().getDefinition().getPath();
        ElementDefn ed = definitions.getElementByPath(p.split("\\."), "example usage", true);
        if (ed != null)
            usage(c, ed, path + "." + c.getName());
    // else if (!c.getName().equals("extension"))
    // System.out.println("error finding "+c.getName()+" at "+path);
    }
}
Also used : Element(org.hl7.fhir.r5.elementmodel.Element) ElementDefn(org.hl7.fhir.definitions.model.ElementDefn)

Example 32 with org.hl7.fhir.r5.model

use of org.hl7.fhir.r5.model in project kindling by HL7.

the class ExampleInspector method checkJsonLd.

private void checkJsonLd() throws IOException {
    String s1 = "{\r\n" + "  \"@type\": \"fhir:Claim\",\r\n" + "  \"@id\": \"http://hl7.org/fhir/Claim/760152\",\r\n" + "  \"decimal\": 123.45,\r\n" + "  \"@context\": {\r\n" + "    \"fhir\": \"http://hl7.org/fhir/\",\r\n" + "    \"xsd\": \"http://www.w3.org/2001/XMLSchema#\",\r\n" + "    \"decimal\": {\r\n" + "      \"@id\": \"fhir:value\",\r\n" + "      \"@type\": \"xsd:decimal\"\r\n" + "    }\r\n" + "  }\r\n" + "}\r\n";
    String s2 = "{\r\n" + "  \"@type\": \"fhir:Claim\",\r\n" + "  \"@id\": \"http://hl7.org/fhir/Claim/760152\",\r\n" + "  \"decimal\": \"123.45\",\r\n" + "  \"@context\": {\r\n" + "    \"fhir\": \"http://hl7.org/fhir/\",\r\n" + "    \"xsd\": \"http://www.w3.org/2001/XMLSchema#\",\r\n" + "    \"decimal\": {\r\n" + "      \"@id\": \"fhir:value\",\r\n" + "      \"@type\": \"xsd:decimal\"\r\n" + "    }\r\n" + "  }\r\n" + "}\r\n";
    Model m1 = ModelFactory.createDefaultModel();
    Model m2 = ModelFactory.createDefaultModel();
    m1.read(new StringReader(s1), null, "JSON-LD");
    m2.read(new StringReader(s2), null, "JSON-LD");
    List<String> diffs = new ModelComparer().setModel1(m1, "j1").setModel2(m2, "j2").compare();
    if (!diffs.isEmpty()) {
        System.out.println("not isomorphic");
        for (String s : diffs) {
            System.out.println("  " + s);
        }
    }
}
Also used : Model(org.apache.jena.rdf.model.Model) StringReader(java.io.StringReader) ModelComparer(org.hl7.fhir.rdf.ModelComparer)

Example 33 with org.hl7.fhir.r5.model

use of org.hl7.fhir.r5.model in project kindling by HL7.

the class ExampleInspector method fetch.

@Override
public Element fetch(IResourceValidator validator, Object appContext, String url) throws IOException, FHIRException {
    String[] parts = url.split("\\/");
    if (parts.length == 2 && definitions.hasResource(parts[0])) {
        ResourceDefn r = definitions.getResourceByName(parts[0]);
        for (Example e : r.getExamples()) {
            if (e.getElement() == null && e.hasXml()) {
                e.setElement(new org.hl7.fhir.r5.elementmodel.XmlParser(context).parse(e.getXml()));
                if (e.getElement().getProperty().getStructure().getBaseDefinition().contains("MetadataResource")) {
                    String urle = e.getElement().getChildValue("url");
                    String v = e.getElement().getChildValue("url");
                    if (urle != null && urle.startsWith("http://hl7.org/fhir") && !version.toCode().equals(v)) {
                        e.getElement().setChildValue("version", version.toCode());
                    }
                }
            }
            if (e.getElement() != null) {
                if (e.getElement().fhirType().equals("Bundle")) {
                    for (Base b : e.getElement().listChildrenByName("entry")) {
                        if (b.getChildByName("resource").hasValues()) {
                            Element res = (Element) b.getChildByName("resource").getValues().get(0);
                            if (res.fhirType().equals(parts[0]) && parts[1].equals(res.getChildValue("id"))) {
                                return res;
                            }
                        }
                    }
                } else if (e.getElement().fhirType().equals(parts[0]) && e.getId().equals(parts[1])) {
                    return e.getElement();
                }
            }
        }
        try {
            if (parts[0].equals("StructureDefinition"))
                return new ObjectConverter(context).convert(context.fetchResourceWithException(StructureDefinition.class, "http://hl7.org/fhir/" + parts[0] + "/" + parts[1]));
            if (parts[0].equals("OperationDefinition"))
                return new ObjectConverter(context).convert(context.fetchResourceWithException(OperationDefinition.class, "http://hl7.org/fhir/" + parts[0] + "/" + parts[1]));
            if (parts[0].equals("SearchParameter"))
                return new ObjectConverter(context).convert(context.fetchResourceWithException(SearchParameter.class, "http://hl7.org/fhir/" + parts[0] + "/" + parts[1]));
            if (parts[0].equals("ValueSet"))
                return new ObjectConverter(context).convert(context.fetchResourceWithException(ValueSet.class, "http://hl7.org/fhir/" + parts[0] + "/" + parts[1]));
            if (parts[0].equals("CodeSystem"))
                return new ObjectConverter(context).convert(context.fetchResourceWithException(CodeSystem.class, "http://hl7.org/fhir/" + parts[0] + "/" + parts[1]));
        } catch (Exception e) {
            return null;
        }
        return null;
    } else
        return null;
}
Also used : XmlParser(org.hl7.fhir.r5.formats.XmlParser) ObjectConverter(org.hl7.fhir.r5.elementmodel.ObjectConverter) Example(org.hl7.fhir.definitions.model.Example) Element(org.hl7.fhir.r5.elementmodel.Element) ResourceDefn(org.hl7.fhir.definitions.model.ResourceDefn) Base(org.hl7.fhir.r5.model.Base) URISyntaxException(java.net.URISyntaxException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) NotImplementedException(org.apache.commons.lang3.NotImplementedException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) ValidationException(org.everit.json.schema.ValidationException) MalformedURLException(java.net.MalformedURLException) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 34 with org.hl7.fhir.r5.model

use of org.hl7.fhir.r5.model in project kindling by HL7.

the class ExampleInspector method validateRDF.

private void validateRDF(String fttl, String fjld, String rt) throws FileNotFoundException, IOException {
    if (VALIDATE_RDF && new File(fjld).exists()) {
        FileInputStream f = new FileInputStream(fjld);
        int size = f.available();
        f.close();
        if (size > 1000000)
            return;
        // replace @context with the contents of the right context file
        JsonObject json = (JsonObject) new com.google.gson.JsonParser().parse(TextFile.fileToString(fjld));
        json.remove("@context");
        json.add("@context", jsonLdDefns.get("@context"));
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String jcnt = gson.toJson(json);
        // TextFile.stringToFile(jcnt, "c:\\temp\\jsonld\\"+rt+".jsonld");
        // parse to a model
        Model mj = ModelFactory.createDefaultModel();
        mj.read(new StringReader(jcnt), null, "JSON-LD");
        // read turtle file into Jena
        Model mt = RDFDataMgr.loadModel(fttl);
        // use ShEx to validate turtle file - TODO
        shex.validate(mt);
    // List<String> diffs = new ModelComparer().setModel1(mt, "ttl").setModel2(mj, "json").compare();
    // if (!diffs.isEmpty()) {
    // System.out.println("not isomorphic");
    // for (String s : diffs) {
    // System.out.println("  "+s);
    // }
    // RDFDataMgr.write(new FileOutputStream("c:\\temp\\json.nt"), mj, RDFFormat.NTRIPLES_UTF8);
    // RDFDataMgr.write(new FileOutputStream("c:\\temp\\ttl.nt"), mt, RDFFormat.NTRIPLES_UTF8);
    // }
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Model(org.apache.jena.rdf.model.Model) StringReader(java.io.StringReader) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile) CSFileInputStream(org.hl7.fhir.utilities.CSFileInputStream) FileInputStream(java.io.FileInputStream)

Example 35 with org.hl7.fhir.r5.model

use of org.hl7.fhir.r5.model in project kindling by HL7.

the class SpecNPMPackageGenerator method makeResourceList5.

private List<ResourceEntry> makeResourceList5(Map<String, byte[]> files, String version, List<ResourceEntry> res) throws FHIRFormatError, IOException {
    for (String k : files.keySet()) {
        if (k.endsWith(".xml")) {
            Bundle b = (Bundle) new org.hl7.fhir.r5.formats.XmlParser().parse(files.get(k));
            for (org.hl7.fhir.r5.model.Bundle.BundleEntryComponent be : b.getEntry()) {
                if (be.hasResource()) {
                    ResourceEntry e = new ResourceEntry();
                    e.type = be.getResource().fhirType();
                    e.id = be.getResource().getId();
                    e.json = new org.hl7.fhir.r5.formats.JsonParser().composeBytes(be.getResource());
                    e.xml = new org.hl7.fhir.r5.formats.XmlParser().composeBytes(be.getResource());
                    e.conf = true;
                    if (be.getResource() instanceof org.hl7.fhir.r5.model.CanonicalResource)
                        e.canonical = ((org.hl7.fhir.r5.model.CanonicalResource) be.getResource()).getUrl();
                    res.add(e);
                }
            }
        }
    }
    return null;
}
Also used : XmlParser(org.hl7.fhir.r5.formats.XmlParser) Bundle(org.hl7.fhir.r5.model.Bundle) JsonParser(org.hl7.fhir.r5.formats.JsonParser)

Aggregations

Test (org.junit.jupiter.api.Test)435 Turtle (org.hl7.fhir.dstu3.utils.formats.Turtle)334 Test (org.junit.Test)289 ArrayList (java.util.ArrayList)112 FHIRException (org.hl7.fhir.exceptions.FHIRException)111 IOException (java.io.IOException)84 List (java.util.List)73 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)70 Date (java.util.Date)68 FileOutputStream (java.io.FileOutputStream)66 File (java.io.File)61 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)61 CodeableReference (org.hl7.fhir.r5.model.CodeableReference)58 InputStream (java.io.InputStream)55 TableModel (org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.TableModel)51 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)50 Bundle (org.hl7.fhir.dstu3.model.Bundle)49 IBundleProvider (ca.uhn.fhir.rest.api.server.IBundleProvider)48 Collectors (java.util.stream.Collectors)48 Arrays (java.util.Arrays)46