如何控制 Jena Turtle 输出?
How do I control Jena Turtle output?
我正在尝试将 FHIR/RDF 模型写入 Turtle (TTL) 输出文件,并使用 ShEx 根据参考模式对其进行验证。我创建了一个 Jena 模型并使用 RDFDataMgr class 将其写出。问题在于生成的输出看起来不像 FHIR 示例中使用的 Turtle 输出。具体来说,它在有 none 的地方添加了尖括号;它不使用 Turtle 惯用的 subject-"a" 类型语法;最重要的是,它无法使用 FHIR 验证工具进行验证。我已经通读了 Jena 文档,没有发现比我正在使用的更精细的输出控制,所以我希望在该领域有更深入了解的人可以指出我应该做些什么来获得预期的输出。有点长post;感谢阅读。
我的代码:
Model model = ModelFactory.createDefaultModel();
HashMap<String,String> prefixes = new HashMap<>();
prefixes.put("rdf" ,"http://www.w3.org/1999/02/22-rdf-syntax-ns#");
prefixes.put("rdfs" ,"http://www.w3.org/2000/01/rdf-schema#");
prefixes.put("owl" ,"http://www.w3.org/2002/07/owl#");
prefixes.put("xsd" ,"http://www.w3.org/2001/XMLSchema#");
prefixes.put("fhir" ,"http://hl7.org/fhir/");
prefixes.put("loinc" ,"http://loinc.org/rdf#");
prefixes.put("sct" ,"http://snomed.info/id#");
for (String key : prefixes.keySet())
model.setNsPrefix(key, prefixes.get(key));
Resource root = model.getResource("fhir:Patient/" + patient.identifier);
root.addProperty(model.createProperty("rdf:type"),"fhir:Patient");
root.addProperty(model.createProperty("fhir:nodeRole"),"fhir:treeRoot");
root.addProperty(model.createProperty("fhir:Resource.id"), patient.identifier);
root.addProperty(
model.createProperty("fhir:Patient.identifier"),
model.createResource().addProperty(model.createProperty("fhir:index"), "0")
.addProperty(
model.createProperty("fhir:Identifier.value"),
model.createResource()
.addProperty(model.createProperty("fhir:index"), "0")
.addProperty(model.createProperty("fhir:value"), patient.identifier)));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
RDFDataMgr.write(byteArrayOutputStream, model, RDFFormat.TURTLE);
String outputMessageText = new String(byteArrayOutputStream.toByteArray());
我的输出:
@prefix fhir: <http://hl7.org/fhir/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix sct: <http://snomed.info/id#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix loinc: <http://loinc.org/rdf#> .
<fhir:Patient/6789012345>
<fhir:Patient.birthDate> [ <fhir:value>
"\"07/01/1970\"^^xsd:date" ] ;
<fhir:Patient.gender> [ <fhir:value>
"male" ] ;
<fhir:Patient.identifier> [ <fhir:Identifier.value> [ <fhir:index> "0" ;
<fhir:value> "6789012345"
] ;
<fhir:index> "0"
] ;
<fhir:Patient.name> [ <fhir:HumanName.given> [ <fhir:index> "0" ;
<fhir:value> "Simmons, Henry"
] ;
<fhir:index> "0"
] ;
<fhir:Resource.id> "6789012345" ;
<fhir:nodeRole> "fhir:treeRoot" ;
<rdf:type> "fhir:Patient" .
然后是 FHIR 文档中的示例(摘录):
@prefix fhir: <http://hl7.org/fhir/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
# - resource
<http://hl7.org/fhir/Patient/pat1> a fhir:Patient;
fhir:nodeRole fhir:treeRoot;
fhir:Resource.id [ fhir:value "pat1"];
fhir:Patient.identifier [
fhir:index 0;
fhir:Identifier.use [ fhir:value "usual" ];
fhir:Identifier.type [
fhir:CodeableConcept.coding [
fhir:index 0;
fhir:Coding.system [ fhir:value "http://terminology.hl7.org/CodeSystem/v2-0203" ];
fhir:Coding.code [ fhir:value "MR" ]
]
];
fhir:Identifier.system [ fhir:value "urn:oid:0.1.2.3.4.5.6.7" ];
fhir:Identifier.value [ fhir:value "654321" ]
];
最后,上海交易所:
<Patient> CLOSED {
a [fhir:Patient];
fhir:nodeRole [fhir:treeRoot]?;
fhir:Resource.id @<id>?;
fhir:Resource.meta @<Meta>?;
fhir:Resource.implicitRules @<uri>?;
fhir:Resource.language @<code>?;
fhir:DomainResource.text @<Narrative>?;
fhir:DomainResource.contained @<Resource>*;
fhir:DomainResource.extension @<Extension>*;
fhir:DomainResource.modifierExtension @<Extension>*;
fhir:Patient.identifier @<Identifier>*;
fhir:Patient.active @<boolean>?;
fhir:Patient.name @<HumanName>*;
事实证明,耶拿惯用的方法如下:
Model model = ModelFactory.createDefaultModel();
model.setNsPrefix("fhir", "http://hl7.org/fhir/");
Resource root = model.getResource(model.expandPrefix("fhir:Patient/" + patient.identifier));
感谢楼主提供正确答案,一小时后删除
我正在尝试将 FHIR/RDF 模型写入 Turtle (TTL) 输出文件,并使用 ShEx 根据参考模式对其进行验证。我创建了一个 Jena 模型并使用 RDFDataMgr class 将其写出。问题在于生成的输出看起来不像 FHIR 示例中使用的 Turtle 输出。具体来说,它在有 none 的地方添加了尖括号;它不使用 Turtle 惯用的 subject-"a" 类型语法;最重要的是,它无法使用 FHIR 验证工具进行验证。我已经通读了 Jena 文档,没有发现比我正在使用的更精细的输出控制,所以我希望在该领域有更深入了解的人可以指出我应该做些什么来获得预期的输出。有点长post;感谢阅读。
我的代码:
Model model = ModelFactory.createDefaultModel();
HashMap<String,String> prefixes = new HashMap<>();
prefixes.put("rdf" ,"http://www.w3.org/1999/02/22-rdf-syntax-ns#");
prefixes.put("rdfs" ,"http://www.w3.org/2000/01/rdf-schema#");
prefixes.put("owl" ,"http://www.w3.org/2002/07/owl#");
prefixes.put("xsd" ,"http://www.w3.org/2001/XMLSchema#");
prefixes.put("fhir" ,"http://hl7.org/fhir/");
prefixes.put("loinc" ,"http://loinc.org/rdf#");
prefixes.put("sct" ,"http://snomed.info/id#");
for (String key : prefixes.keySet())
model.setNsPrefix(key, prefixes.get(key));
Resource root = model.getResource("fhir:Patient/" + patient.identifier);
root.addProperty(model.createProperty("rdf:type"),"fhir:Patient");
root.addProperty(model.createProperty("fhir:nodeRole"),"fhir:treeRoot");
root.addProperty(model.createProperty("fhir:Resource.id"), patient.identifier);
root.addProperty(
model.createProperty("fhir:Patient.identifier"),
model.createResource().addProperty(model.createProperty("fhir:index"), "0")
.addProperty(
model.createProperty("fhir:Identifier.value"),
model.createResource()
.addProperty(model.createProperty("fhir:index"), "0")
.addProperty(model.createProperty("fhir:value"), patient.identifier)));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
RDFDataMgr.write(byteArrayOutputStream, model, RDFFormat.TURTLE);
String outputMessageText = new String(byteArrayOutputStream.toByteArray());
我的输出:
@prefix fhir: <http://hl7.org/fhir/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix sct: <http://snomed.info/id#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix loinc: <http://loinc.org/rdf#> .
<fhir:Patient/6789012345>
<fhir:Patient.birthDate> [ <fhir:value>
"\"07/01/1970\"^^xsd:date" ] ;
<fhir:Patient.gender> [ <fhir:value>
"male" ] ;
<fhir:Patient.identifier> [ <fhir:Identifier.value> [ <fhir:index> "0" ;
<fhir:value> "6789012345"
] ;
<fhir:index> "0"
] ;
<fhir:Patient.name> [ <fhir:HumanName.given> [ <fhir:index> "0" ;
<fhir:value> "Simmons, Henry"
] ;
<fhir:index> "0"
] ;
<fhir:Resource.id> "6789012345" ;
<fhir:nodeRole> "fhir:treeRoot" ;
<rdf:type> "fhir:Patient" .
然后是 FHIR 文档中的示例(摘录):
@prefix fhir: <http://hl7.org/fhir/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
# - resource
<http://hl7.org/fhir/Patient/pat1> a fhir:Patient;
fhir:nodeRole fhir:treeRoot;
fhir:Resource.id [ fhir:value "pat1"];
fhir:Patient.identifier [
fhir:index 0;
fhir:Identifier.use [ fhir:value "usual" ];
fhir:Identifier.type [
fhir:CodeableConcept.coding [
fhir:index 0;
fhir:Coding.system [ fhir:value "http://terminology.hl7.org/CodeSystem/v2-0203" ];
fhir:Coding.code [ fhir:value "MR" ]
]
];
fhir:Identifier.system [ fhir:value "urn:oid:0.1.2.3.4.5.6.7" ];
fhir:Identifier.value [ fhir:value "654321" ]
];
最后,上海交易所:
<Patient> CLOSED {
a [fhir:Patient];
fhir:nodeRole [fhir:treeRoot]?;
fhir:Resource.id @<id>?;
fhir:Resource.meta @<Meta>?;
fhir:Resource.implicitRules @<uri>?;
fhir:Resource.language @<code>?;
fhir:DomainResource.text @<Narrative>?;
fhir:DomainResource.contained @<Resource>*;
fhir:DomainResource.extension @<Extension>*;
fhir:DomainResource.modifierExtension @<Extension>*;
fhir:Patient.identifier @<Identifier>*;
fhir:Patient.active @<boolean>?;
fhir:Patient.name @<HumanName>*;
事实证明,耶拿惯用的方法如下:
Model model = ModelFactory.createDefaultModel();
model.setNsPrefix("fhir", "http://hl7.org/fhir/");
Resource root = model.getResource(model.expandPrefix("fhir:Patient/" + patient.identifier));
感谢楼主提供正确答案,一小时后删除