Search in sources :

Example 1 with ModelComparer

use of org.hl7.fhir.rdf.ModelComparer 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 2 with ModelComparer

use of org.hl7.fhir.rdf.ModelComparer 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)

Aggregations

StringReader (java.io.StringReader)2 Model (org.apache.jena.rdf.model.Model)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonObject (com.google.gson.JsonObject)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 ModelComparer (org.hl7.fhir.rdf.ModelComparer)1 CSFileInputStream (org.hl7.fhir.utilities.CSFileInputStream)1 TextFile (org.hl7.fhir.utilities.TextFile)1