Search in sources :

Example 1 with Turtle

use of org.hl7.fhir.r4.utils.formats.Turtle 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 2 with Turtle

use of org.hl7.fhir.r4.utils.formats.Turtle in project org.hl7.fhir.core by hapifhir.

the class TurtleParser method parseChildren.

private void parseChildren(Turtle src, String path, TTLComplex object, Element context, boolean primitive) throws FHIRFormatError, DefinitionException {
    List<Property> properties = context.getProperty().getChildProperties(context.getName(), null);
    Set<String> processed = new HashSet<String>();
    if (primitive)
        processed.add(FHIR_URI_BASE + "value");
    // first pass: process the properties
    for (Property property : properties) {
        if (property.isChoice()) {
            for (TypeRefComponent type : property.getDefinition().getType()) {
                String eName = property.getName().substring(0, property.getName().length() - 3) + Utilities.capitalize(type.getCode());
                parseChild(src, object, context, processed, property, path, getFormalName(property, eName));
            }
        } else {
            parseChild(src, object, context, processed, property, path, getFormalName(property));
        }
    }
    // second pass: check for things not processed
    if (policy != ValidationPolicy.NONE) {
        for (String u : object.getPredicates().keySet()) {
            if (!processed.contains(u)) {
                TTLObject n = object.getPredicates().get(u);
                logError(n.getLine(), n.getCol(), path, IssueType.STRUCTURE, "Unrecognised predicate '" + u + "'", IssueSeverity.ERROR);
            }
        }
    }
}
Also used : TypeRefComponent(org.hl7.fhir.dstu3.model.ElementDefinition.TypeRefComponent) TTLObject(org.hl7.fhir.dstu3.utils.formats.Turtle.TTLObject) HashSet(java.util.HashSet)

Example 3 with Turtle

use of org.hl7.fhir.r4.utils.formats.Turtle in project org.hl7.fhir.core by hapifhir.

the class TurtleParser method parseResource.

private void parseResource(Turtle src, String npath, TTLComplex object, Element context, Property property, String name, TTLObject e) throws FHIRFormatError, DefinitionException {
    TTLComplex obj;
    if (e instanceof TTLComplex)
        obj = (TTLComplex) e;
    else if (e instanceof TTLURL) {
        String url = ((TTLURL) e).getUri();
        obj = src.getObject(url);
        if (obj == null) {
            logError(e.getLine(), e.getCol(), npath, IssueType.INVALID, "reference to " + url + " cannot be resolved", IssueSeverity.FATAL);
            return;
        }
    } else
        throw new FHIRFormatError("Wrong type for resource");
    TTLObject type = obj.getPredicates().get("http://www.w3.org/2000/01/rdf-schema#type");
    if (type == null) {
        logError(object.getLine(), object.getCol(), npath, IssueType.INVALID, "Unknown resource type (missing rdfs:type)", IssueSeverity.FATAL);
        return;
    }
    if (type instanceof TTLList) {
        // this is actually broken - really we have to look through the structure definitions at this point
        for (TTLObject tobj : ((TTLList) type).getList()) {
            if (tobj instanceof TTLURL && ((TTLURL) tobj).getUri().startsWith(FHIR_URI_BASE)) {
                type = tobj;
                break;
            }
        }
    }
    if (!(type instanceof TTLURL)) {
        logError(object.getLine(), object.getCol(), npath, IssueType.INVALID, "Unexpected datatype for rdfs:type)", IssueSeverity.FATAL);
        return;
    }
    String rt = ((TTLURL) type).getUri();
    String ns = rt.substring(0, rt.lastIndexOf("/"));
    rt = rt.substring(rt.lastIndexOf("/") + 1);
    StructureDefinition sd = getDefinition(object.getLine(), object.getCol(), ns, rt);
    if (sd == null)
        return;
    Element n = new Element(tail(name), property).markLocation(object.getLine(), object.getCol());
    context.getChildren().add(n);
    n.updateProperty(new Property(this.context, sd.getSnapshot().getElement().get(0), sd), SpecialElement.fromProperty(n.getProperty()), property);
    n.setType(rt);
    parseChildren(src, npath, obj, n, false);
}
Also used : TTLComplex(org.hl7.fhir.dstu3.utils.formats.Turtle.TTLComplex) StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) SpecialElement(org.hl7.fhir.dstu3.elementmodel.Element.SpecialElement) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) TTLURL(org.hl7.fhir.dstu3.utils.formats.Turtle.TTLURL) TTLObject(org.hl7.fhir.dstu3.utils.formats.Turtle.TTLObject) TTLList(org.hl7.fhir.dstu3.utils.formats.Turtle.TTLList)

Example 4 with Turtle

use of org.hl7.fhir.r4.utils.formats.Turtle in project org.hl7.fhir.core by hapifhir.

the class TurtleTests method test_processresponse_example.

@Test
public void test_processresponse_example() throws FileNotFoundException, IOException, Exception {
    System.out.println("processresponse-example.ttl");
    new Turtle().parse(TextFile.fileToString("C:\\work\\org.hl7.fhir\\build\\publish\\processresponse-example.ttl"));
}
Also used : Turtle(org.hl7.fhir.dstu3.utils.formats.Turtle) Test(org.junit.jupiter.api.Test)

Example 5 with Turtle

use of org.hl7.fhir.r4.utils.formats.Turtle in project org.hl7.fhir.core by hapifhir.

the class TurtleTests method test_practitioner_example_f203_jvg.

@Test
public void test_practitioner_example_f203_jvg() throws FileNotFoundException, IOException, Exception {
    System.out.println("practitioner-example-f203-jvg.ttl");
    new Turtle().parse(TextFile.fileToString("C:\\work\\org.hl7.fhir\\build\\publish\\practitioner-example-f203-jvg.ttl"));
}
Also used : Turtle(org.hl7.fhir.dstu3.utils.formats.Turtle) Test(org.junit.jupiter.api.Test)

Aggregations

Turtle (org.hl7.fhir.dstu3.utils.formats.Turtle)337 Test (org.junit.jupiter.api.Test)334 IOException (java.io.IOException)13 Turtle (org.hl7.fhir.utilities.turtle.Turtle)8 TTLObject (org.hl7.fhir.utilities.turtle.Turtle.TTLObject)8 FHIRException (org.hl7.fhir.exceptions.FHIRException)6 TTLList (org.hl7.fhir.utilities.turtle.Turtle.TTLList)6 HashSet (java.util.HashSet)4 TTLObject (org.hl7.fhir.dstu3.utils.formats.Turtle.TTLObject)4 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)4 TTLObject (org.hl7.fhir.r4.utils.formats.Turtle.TTLObject)4 SpecialElement (org.hl7.fhir.r4b.elementmodel.Element.SpecialElement)4 NamedElement (org.hl7.fhir.r4b.elementmodel.ParserBase.NamedElement)4 SpecialElement (org.hl7.fhir.r5.elementmodel.Element.SpecialElement)4 NamedElement (org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement)4 Section (org.hl7.fhir.utilities.turtle.Turtle.Section)4 Subject (org.hl7.fhir.utilities.turtle.Turtle.Subject)4 TTLURL (org.hl7.fhir.utilities.turtle.Turtle.TTLURL)4 FileNotFoundException (java.io.FileNotFoundException)3 SpecialElement (org.hl7.fhir.dstu3.elementmodel.Element.SpecialElement)3