Merge pull request #1110 from balhoff/rdf-wg-r5-with-generated

Implement R5 RDF format changes
This commit is contained in:
Grahame Grieve 2023-02-24 10:21:19 +11:00 committed by GitHub
commit 521e72a5ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 1013 additions and 970 deletions

View File

@ -29,8 +29,8 @@ public class RdfParser extends RdfParserBase {
private void composeEnum(Complex parent, String parentType, String name, Enumeration<? extends Enum> value, int index) { private void composeEnum(Complex parent, String parentType, String name, Enumeration<? extends Enum> value, int index) {
if (value == null) if (value == null)
return; return;
Complex t = parent.predicate("fhir:"+parentType+"."+name); Complex t = parent.predicate("fhir:"+name, index > -1);
t.predicate("fhir:value", ttlLiteral(value.asStringValue())); t.predicate("fhir:v", ttlLiteral(value.asStringValue()), false);
composeElement(t, parentType, name, value, index); composeElement(t, parentType, name, value, index);
decorateCode(t, value); decorateCode(t, value);
} }
@ -39,24 +39,24 @@ public class RdfParser extends RdfParserBase {
protected void composeDate(Complex parent, String parentType, String name, DateType value, int index) { protected void composeDate(Complex parent, String parentType, String name, DateType value, int index) {
if (value == null) if (value == null)
return; return;
Complex t = parent.predicate("fhir:"+parentType+"."+name); Complex t = parent.predicate("fhir:"+name, index > -1);
t.predicate("fhir:value", ttlLiteral(value.asStringValue())); t.predicate("fhir:v", ttlLiteral(value.asStringValue()), false);
composeElement(t, parentType, name, value, index); composeElement(t, parentType, name, value, index);
} }
protected void composeDateTime(Complex parent, String parentType, String name, DateTimeType value, int index) { protected void composeDateTime(Complex parent, String parentType, String name, DateTimeType value, int index) {
if (value == null) if (value == null)
return; return;
Complex t = parent.predicate("fhir:"+parentType+"."+name); Complex t = parent.predicate("fhir:"+name, index > -1);
t.predicate("fhir:value", ttlLiteral(value.asStringValue())); t.predicate("fhir:v", ttlLiteral(value.asStringValue()), false);
composeElement(t, parentType, name, value, index); composeElement(t, parentType, name, value, index);
} }
protected void composeCode(Complex parent, String parentType, String name, CodeType value, int index) { protected void composeCode(Complex parent, String parentType, String name, CodeType value, int index) {
if (value == null) if (value == null)
return; return;
Complex t = parent.predicate("fhir:"+parentType+"."+name); Complex t = parent.predicate("fhir:"+name, index > -1);
t.predicate("fhir:value", ttlLiteral(value.asStringValue())); t.predicate("fhir:v", ttlLiteral(value.asStringValue()), false);
composeElement(t, parentType, name, value, index); composeElement(t, parentType, name, value, index);
decorateCode(t, value); decorateCode(t, value);
} }
@ -64,136 +64,136 @@ public class RdfParser extends RdfParserBase {
protected void composeString(Complex parent, String parentType, String name, StringType value, int index) { protected void composeString(Complex parent, String parentType, String name, StringType value, int index) {
if (value == null) if (value == null)
return; return;
Complex t = parent.predicate("fhir:"+parentType+"."+name); Complex t = parent.predicate("fhir:"+name, index > -1);
t.predicate("fhir:value", ttlLiteral(value.asStringValue())); t.predicate("fhir:v", ttlLiteral(value.asStringValue()), false);
composeElement(t, parentType, name, value, index); composeElement(t, parentType, name, value, index);
} }
protected void composeInteger(Complex parent, String parentType, String name, IntegerType value, int index) { protected void composeInteger(Complex parent, String parentType, String name, IntegerType value, int index) {
if (value == null) if (value == null)
return; return;
Complex t = parent.predicate("fhir:"+parentType+"."+name); Complex t = parent.predicate("fhir:"+name, index > -1);
t.predicate("fhir:value", ttlLiteral(value.asStringValue())); t.predicate("fhir:v", ttlLiteral(value.asStringValue()), false);
composeElement(t, parentType, name, value, index); composeElement(t, parentType, name, value, index);
} }
protected void composeInteger64(Complex parent, String parentType, String name, Integer64Type value, int index) { protected void composeInteger64(Complex parent, String parentType, String name, Integer64Type value, int index) {
if (value == null) if (value == null)
return; return;
Complex t = parent.predicate("fhir:"+parentType+"."+name); Complex t = parent.predicate("fhir:"+name, index > -1);
t.predicate("fhir:value", ttlLiteral(value.asStringValue())); t.predicate("fhir:v", ttlLiteral(value.asStringValue()), false);
composeElement(t, parentType, name, value, index); composeElement(t, parentType, name, value, index);
} }
protected void composeOid(Complex parent, String parentType, String name, OidType value, int index) { protected void composeOid(Complex parent, String parentType, String name, OidType value, int index) {
if (value == null) if (value == null)
return; return;
Complex t = parent.predicate("fhir:"+parentType+"."+name); Complex t = parent.predicate("fhir:"+name, index > -1);
t.predicate("fhir:value", ttlLiteral(value.asStringValue())); t.predicate("fhir:v", ttlLiteral(value.asStringValue()), false);
composeElement(t, parentType, name, value, index); composeElement(t, parentType, name, value, index);
} }
protected void composeCanonical(Complex parent, String parentType, String name, CanonicalType value, int index) { protected void composeCanonical(Complex parent, String parentType, String name, CanonicalType value, int index) {
if (value == null) if (value == null)
return; return;
Complex t = parent.predicate("fhir:"+parentType+"."+name); Complex t = parent.predicate("fhir:"+name, index > -1);
t.predicate("fhir:value", ttlLiteral(value.asStringValue())); t.predicate("fhir:v", ttlLiteral(value.asStringValue()), false);
composeElement(t, parentType, name, value, index); composeElement(t, parentType, name, value, index);
} }
protected void composeUri(Complex parent, String parentType, String name, UriType value, int index) { protected void composeUri(Complex parent, String parentType, String name, UriType value, int index) {
if (value == null) if (value == null)
return; return;
Complex t = parent.predicate("fhir:"+parentType+"."+name); Complex t = parent.predicate("fhir:"+name, index > -1);
t.predicate("fhir:value", ttlLiteral(value.asStringValue())); t.predicate("fhir:v", ttlLiteral(value.asStringValue()), false);
composeElement(t, parentType, name, value, index); composeElement(t, parentType, name, value, index);
} }
protected void composeUuid(Complex parent, String parentType, String name, UuidType value, int index) { protected void composeUuid(Complex parent, String parentType, String name, UuidType value, int index) {
if (value == null) if (value == null)
return; return;
Complex t = parent.predicate("fhir:"+parentType+"."+name); Complex t = parent.predicate("fhir:"+name, index > -1);
t.predicate("fhir:value", ttlLiteral(value.asStringValue())); t.predicate("fhir:v", ttlLiteral(value.asStringValue()), false);
composeElement(t, parentType, name, value, index); composeElement(t, parentType, name, value, index);
} }
protected void composeUrl(Complex parent, String parentType, String name, UrlType value, int index) { protected void composeUrl(Complex parent, String parentType, String name, UrlType value, int index) {
if (value == null) if (value == null)
return; return;
Complex t = parent.predicate("fhir:"+parentType+"."+name); Complex t = parent.predicate("fhir:"+name, index > -1);
t.predicate("fhir:value", ttlLiteral(value.asStringValue())); t.predicate("fhir:v", ttlLiteral(value.asStringValue()), false);
composeElement(t, parentType, name, value, index); composeElement(t, parentType, name, value, index);
} }
protected void composeInstant(Complex parent, String parentType, String name, InstantType value, int index) { protected void composeInstant(Complex parent, String parentType, String name, InstantType value, int index) {
if (value == null) if (value == null)
return; return;
Complex t = parent.predicate("fhir:"+parentType+"."+name); Complex t = parent.predicate("fhir:"+name, index > -1);
t.predicate("fhir:value", ttlLiteral(value.asStringValue())); t.predicate("fhir:v", ttlLiteral(value.asStringValue()), false);
composeElement(t, parentType, name, value, index); composeElement(t, parentType, name, value, index);
} }
protected void composeBoolean(Complex parent, String parentType, String name, BooleanType value, int index) { protected void composeBoolean(Complex parent, String parentType, String name, BooleanType value, int index) {
if (value == null) if (value == null)
return; return;
Complex t = parent.predicate("fhir:"+parentType+"."+name); Complex t = parent.predicate("fhir:"+name, index > -1);
t.predicate("fhir:value", ttlLiteral(value.asStringValue())); t.predicate("fhir:v", ttlLiteral(value.asStringValue()), false);
composeElement(t, parentType, name, value, index); composeElement(t, parentType, name, value, index);
} }
protected void composeBase64Binary(Complex parent, String parentType, String name, Base64BinaryType value, int index) { protected void composeBase64Binary(Complex parent, String parentType, String name, Base64BinaryType value, int index) {
if (value == null) if (value == null)
return; return;
Complex t = parent.predicate("fhir:"+parentType+"."+name); Complex t = parent.predicate("fhir:"+name, index > -1);
t.predicate("fhir:value", ttlLiteral(value.asStringValue())); t.predicate("fhir:v", ttlLiteral(value.asStringValue()), false);
composeElement(t, parentType, name, value, index); composeElement(t, parentType, name, value, index);
} }
protected void composeUnsignedInt(Complex parent, String parentType, String name, UnsignedIntType value, int index) { protected void composeUnsignedInt(Complex parent, String parentType, String name, UnsignedIntType value, int index) {
if (value == null) if (value == null)
return; return;
Complex t = parent.predicate("fhir:"+parentType+"."+name); Complex t = parent.predicate("fhir:"+name, index > -1);
t.predicate("fhir:value", ttlLiteral(value.asStringValue())); t.predicate("fhir:v", ttlLiteral(value.asStringValue()), false);
composeElement(t, parentType, name, value, index); composeElement(t, parentType, name, value, index);
} }
protected void composeMarkdown(Complex parent, String parentType, String name, MarkdownType value, int index) { protected void composeMarkdown(Complex parent, String parentType, String name, MarkdownType value, int index) {
if (value == null) if (value == null)
return; return;
Complex t = parent.predicate("fhir:"+parentType+"."+name); Complex t = parent.predicate("fhir:"+name, index > -1);
t.predicate("fhir:value", ttlLiteral(value.asStringValue())); t.predicate("fhir:v", ttlLiteral(value.asStringValue()), false);
composeElement(t, parentType, name, value, index); composeElement(t, parentType, name, value, index);
} }
protected void composeTime(Complex parent, String parentType, String name, TimeType value, int index) { protected void composeTime(Complex parent, String parentType, String name, TimeType value, int index) {
if (value == null) if (value == null)
return; return;
Complex t = parent.predicate("fhir:"+parentType+"."+name); Complex t = parent.predicate("fhir:"+name, index > -1);
t.predicate("fhir:value", ttlLiteral(value.asStringValue())); t.predicate("fhir:v", ttlLiteral(value.asStringValue()), false);
composeElement(t, parentType, name, value, index); composeElement(t, parentType, name, value, index);
} }
protected void composeId(Complex parent, String parentType, String name, IdType value, int index) { protected void composeId(Complex parent, String parentType, String name, IdType value, int index) {
if (value == null) if (value == null)
return; return;
Complex t = parent.predicate("fhir:"+parentType+"."+name); Complex t = parent.predicate("fhir:"+name, index > -1);
t.predicate("fhir:value", ttlLiteral(value.asStringValue())); t.predicate("fhir:v", ttlLiteral(value.asStringValue()), false);
composeElement(t, parentType, name, value, index); composeElement(t, parentType, name, value, index);
} }
protected void composePositiveInt(Complex parent, String parentType, String name, PositiveIntType value, int index) { protected void composePositiveInt(Complex parent, String parentType, String name, PositiveIntType value, int index) {
if (value == null) if (value == null)
return; return;
Complex t = parent.predicate("fhir:"+parentType+"."+name); Complex t = parent.predicate("fhir:"+name, index > -1);
t.predicate("fhir:value", ttlLiteral(value.asStringValue())); t.predicate("fhir:v", ttlLiteral(value.asStringValue()), false);
composeElement(t, parentType, name, value, index); composeElement(t, parentType, name, value, index);
} }
protected void composeDecimal(Complex parent, String parentType, String name, DecimalType value, int index) { protected void composeDecimal(Complex parent, String parentType, String name, DecimalType value, int index) {
if (value == null) if (value == null)
return; return;
Complex t = parent.predicate("fhir:"+parentType+"."+name); Complex t = parent.predicate("fhir:"+name, index > -1);
t.predicate("fhir:value", ttlLiteral(value.asStringValue())); t.predicate("fhir:v", ttlLiteral(value.asStringValue()), false);
composeElement(t, parentType, name, value, index); composeElement(t, parentType, name, value, index);
} }

View File

@ -108,7 +108,7 @@ public class JavaParserRdfGenerator extends JavaBaseGenerator {
composer.append(" if (Utilities.noString(parentType))\r\n"); composer.append(" if (Utilities.noString(parentType))\r\n");
composer.append(" t = parent;\r\n"); composer.append(" t = parent;\r\n");
composer.append(" else {\r\n"); composer.append(" else {\r\n");
composer.append(" t = parent.predicate(\"fhir:\"+parentType+'.'+name);\r\n"); composer.append(" t = parent.predicate(\"fhir:\"+name,index > -1);\r\n");
composer.append(" }\r\n"); composer.append(" }\r\n");
composer.append(" compose"+ti.getAncestorName()+"(t, \""+ti.getDefn().getName()+"\", name, element, index);\r\n"); composer.append(" compose"+ti.getAncestorName()+"(t, \""+ti.getDefn().getName()+"\", name, element, index);\r\n");
if (tn.equals("Coding")) if (tn.equals("Coding"))

View File

@ -333,22 +333,24 @@ public class TurtleParser extends ParserBase {
} }
String subjId = genSubjectId(e); String subjId = genSubjectId(e);
String ontologyId = subjId.replace(">", ".ttl>"); Subject subject;
Section ontology = ttl.section("ontology header"); if (hasModifierExtension(e))
ontology.triple(ontologyId, "a", "owl:Ontology"); subject = section.triple(subjId, "a", "fhir:_" + e.getType());
ontology.triple(ontologyId, "owl:imports", "fhir:fhir.ttl"); else
if(ontologyId.startsWith("<" + FHIR_URI_BASE)) subject = section.triple(subjId, "a", "fhir:" + e.getType());
ontology.triple(ontologyId, "owl:versionIRI", ontologyId.replace(FHIR_URI_BASE, FHIR_VERSION_BASE));
Subject subject = section.triple(subjId, "a", "fhir:" + e.getType()); subject.linkedPredicate("fhir:nodeRole", "fhir:treeRoot", linkResolver == null ? null : linkResolver.resolvePage("rdf.html#tree-root"), null);
subject.linkedPredicate("fhir:nodeRole", "fhir:treeRoot", linkResolver == null ? null : linkResolver.resolvePage("rdf.html#tree-root"), null);
for (Element child : e.getChildren()) { for (Element child : e.getChildren()) {
composeElement(section, subject, child, null); composeElement(section, subject, child, null);
} }
} }
private boolean hasModifierExtension(Element e) {
return e.getChildren().stream().anyMatch(p -> p.getName().equals("modifierExtension"));
}
protected String getURIType(String uri) { protected String getURIType(String uri) {
if(uri.startsWith("<" + FHIR_URI_BASE)) if(uri.startsWith("<" + FHIR_URI_BASE))
if(uri.substring(FHIR_URI_BASE.length() + 1).contains("/")) if(uri.substring(FHIR_URI_BASE.length() + 1).contains("/"))
@ -415,17 +417,18 @@ public class TurtleParser extends ParserBase {
Complex t; Complex t;
if (element.getSpecial() == SpecialElement.BUNDLE_ENTRY && parent != null && parent.getNamedChildValue("fullUrl") != null) { if (element.getSpecial() == SpecialElement.BUNDLE_ENTRY && parent != null && parent.getNamedChildValue("fullUrl") != null) {
String url = "<"+parent.getNamedChildValue("fullUrl")+">"; String url = "<"+parent.getNamedChildValue("fullUrl")+">";
ctxt.linkedPredicate("fhir:"+en, url, linkResolver == null ? null : linkResolver.resolveProperty(element.getProperty()), comment); ctxt.linkedPredicate("fhir:"+en, url, linkResolver == null ? null : linkResolver.resolveProperty(element.getProperty()), comment, element.getProperty().isList());
t = section.subject(url); t = section.subject(url);
} else { } else {
t = ctxt.linkedPredicate("fhir:"+en, linkResolver == null ? null : linkResolver.resolveProperty(element.getProperty()), comment); t = ctxt.linkedPredicate("fhir:"+en, linkResolver == null ? null : linkResolver.resolveProperty(element.getProperty()), comment, element.getProperty().isList());
} }
if (element.getProperty().getName().endsWith("[x]") && !element.hasValue()) {
t.linkedPredicate("a", "fhir:" + element.fhirType(), linkResolver == null ? null : linkResolver.resolveType(element.fhirType()), null);
}
if (element.getSpecial() != null) if (element.getSpecial() != null)
t.linkedPredicate("a", "fhir:"+element.fhirType(), linkResolver == null ? null : linkResolver.resolveType(element.fhirType()), null); t.linkedPredicate("a", "fhir:"+element.fhirType(), linkResolver == null ? null : linkResolver.resolveType(element.fhirType()), null);
if (element.hasValue()) if (element.hasValue())
t.linkedPredicate("fhir:value", ttlLiteral(element.getValue(), element.getType()), linkResolver == null ? null : linkResolver.resolveType(element.getType()), null); t.linkedPredicate("fhir:v", ttlLiteral(element.getValue(), element.getType()), linkResolver == null ? null : linkResolver.resolveType(element.getType()), null);
if (element.getProperty().isList() && (!element.isResource() || element.getSpecial() == SpecialElement.CONTAINED))
t.linkedPredicate("fhir:index", Integer.toString(element.getIndex()), linkResolver == null ? null : linkResolver.resolvePage("rdf.html#index"), null);
if ("Coding".equals(element.getType())) if ("Coding".equals(element.getType()))
decorateCoding(t, element, section); decorateCoding(t, element, section);
@ -463,37 +466,27 @@ public class TurtleParser extends ParserBase {
private String getFormalName(Element element) { private String getFormalName(Element element) {
String en = null; String en = null;
if (element.getSpecial() == null) { if (element.getSpecial() == null)
if (element.getProperty().getDefinition().hasBase()) en = element.getProperty().getName();
en = element.getProperty().getDefinition().getBase().getPath();
}
else if (element.getSpecial() == SpecialElement.BUNDLE_ENTRY) else if (element.getSpecial() == SpecialElement.BUNDLE_ENTRY)
en = "Bundle.entry.resource"; en = "resource";
else if (element.getSpecial() == SpecialElement.BUNDLE_OUTCOME) else if (element.getSpecial() == SpecialElement.BUNDLE_OUTCOME)
en = "Bundle.entry.response.outcome"; en = "outcome";
else if (element.getSpecial() == SpecialElement.PARAMETER) else if (element.getSpecial() == SpecialElement.PARAMETER)
en = element.getElementProperty().getDefinition().getPath(); en = element.getElementProperty().getDefinition().getPath();
else // CONTAINED else // CONTAINED
en = "DomainResource.contained"; en = "contained";
if (en == null) if (en == null)
en = element.getProperty().getDefinition().getPath(); en = element.getProperty().getName();
boolean doType = false;
if (en.endsWith("[x]")) {
en = en.substring(0, en.length()-3);
doType = true;
}
if (doType || (element.getProperty().getDefinition().getType().size() > 1 && !allReference(element.getProperty().getDefinition().getType())))
en = en + Utilities.capitalize(element.getType());
return en;
}
private boolean allReference(List<TypeRefComponent> types) { if (en.endsWith("[x]"))
for (TypeRefComponent t : types) { en = en.substring(0, en.length()-3);
if (!t.getCode().equals("Reference"))
return false; if (hasModifierExtension(element))
} return "_" + en;
return true; else
return en;
} }
static public String ttlLiteral(String value, String type) { static public String ttlLiteral(String value, String type) {
@ -550,10 +543,14 @@ public class TurtleParser extends ParserBase {
else else
t.linkedPredicate("a", "sct:" + urlescape(code), null, null); t.linkedPredicate("a", "sct:" + urlescape(code), null, null);
} else if ("http://loinc.org".equals(system)) { } else if ("http://loinc.org".equals(system)) {
t.prefix("loinc", "http://loinc.org/rdf#"); t.prefix("loinc", "https://loinc.org/rdf/");
t.linkedPredicate("a", "loinc:"+urlescape(code).toUpperCase(), null, null); t.linkedPredicate("a", "loinc:"+urlescape(code).toUpperCase(), null, null);
} else if ("https://www.nlm.nih.gov/mesh".equals(system)) {
t.prefix("mesh", "http://id.nlm.nih.gov/mesh/");
t.linkedPredicate("a", "mesh:"+urlescape(code), null, null);
} }
} }
private void generateLinkedPredicate(Complex t, String code) throws FHIRException { private void generateLinkedPredicate(Complex t, String code) throws FHIRException {
Expression expression = SnomedExpressions.parse(code); Expression expression = SnomedExpressions.parse(code);

View File

@ -122,10 +122,10 @@ public abstract class RdfParserBase extends ParserBase implements IParser {
return; return;
if ("http://snomed.info/sct".equals(element.getSystem())) { if ("http://snomed.info/sct".equals(element.getSystem())) {
t.prefix("sct", "http://snomed.info/sct/"); t.prefix("sct", "http://snomed.info/sct/");
t.predicate("a", "sct:"+element.getCode()); t.predicate("a", "sct:"+element.getCode(), false);
} else if ("http://snomed.info/sct".equals(element.getSystem())) { } else if ("http://snomed.info/sct".equals(element.getSystem())) {
t.prefix("loinc", "http://loinc.org/rdf#"); t.prefix("loinc", "http://loinc.org/rdf#");
t.predicate("a", "loinc:"+element.getCode()); t.predicate("a", "loinc:"+element.getCode(), false);
} }
} }

View File

@ -72,22 +72,35 @@ public class Turtle {
protected List<Predicate> predicates = new ArrayList<Predicate>(); protected List<Predicate> predicates = new ArrayList<Predicate>();
public Complex predicate(String predicate, String object) { public Complex predicate(String predicate, String object) {
predicateSet.add(predicate); return predicate(predicate, object, false);
objectSet.add(object);
return predicate(predicate, new StringType(object));
} }
public Complex linkedPredicate(String predicate, String object, String link, String comment) { public Complex predicate(String predicate, String object, boolean asList) {
predicateSet.add(predicate);
objectSet.add(object);
return predicate(predicate, new StringType(object), asList);
}
public Complex linkedPredicate(String predicate, String object, String link, String comment) {
return linkedPredicate(predicate, object, link, comment, false);
}
public Complex linkedPredicate(String predicate, String object, String link, String comment, boolean asList) {
predicateSet.add(predicate); predicateSet.add(predicate);
objectSet.add(object); objectSet.add(object);
return linkedPredicate(predicate, new StringType(object), link, comment); return linkedPredicate(predicate, new StringType(object), link, comment, asList);
} }
public Complex predicate(String predicate, Triple object) { public Complex predicate(String predicate, Triple object) {
return predicate(predicate, object, false);
}
public Complex predicate(String predicate, Triple object, boolean asList) {
Predicate p = getPredicate(predicate); Predicate p = getPredicate(predicate);
if (p == null) { if (p == null) {
p = new Predicate(); p = new Predicate();
p.predicate = predicate; p.predicate = predicate;
p.asList = asList;
predicateSet.add(predicate); predicateSet.add(predicate);
predicates.add(p); predicates.add(p);
} }
@ -95,7 +108,7 @@ public class Turtle {
objectSet.add(((StringType) object).value); objectSet.add(((StringType) object).value);
p.objects.add(object); p.objects.add(object);
return this; return this;
} }
protected Predicate getPredicate(String predicate) { protected Predicate getPredicate(String predicate) {
for (Predicate p : predicates) for (Predicate p : predicates)
@ -105,12 +118,17 @@ public class Turtle {
} }
public Complex linkedPredicate(String predicate, Triple object, String link, String comment) { public Complex linkedPredicate(String predicate, Triple object, String link, String comment) {
return linkedPredicate(predicate, object, link, comment, false);
}
public Complex linkedPredicate(String predicate, Triple object, String link, String comment, boolean asList) {
Predicate p = getPredicate(predicate); Predicate p = getPredicate(predicate);
if (p == null) { if (p == null) {
p = new Predicate(); p = new Predicate();
p.predicate = predicate; p.predicate = predicate;
p.link = link; p.link = link;
p.comment = comment; p.comment = comment;
p.asList = asList;
predicateSet.add(predicate); predicateSet.add(predicate);
predicates.add(p); predicates.add(p);
} }
@ -120,17 +138,25 @@ public class Turtle {
return this; return this;
} }
public Complex predicate(String predicate) { public Complex predicate(String predicate) {
predicateSet.add(predicate); return predicate(predicate, false);
Complex c = complex(); }
predicate(predicate, c);
return c;
}
public Complex linkedPredicate(String predicate, String link, String comment) { public Complex predicate(String predicate, boolean asList) {
predicateSet.add(predicate);
Complex c = complex();
predicate(predicate, c, asList);
return c;
}
public Complex linkedPredicate(String predicate, String link, String comment) {
return linkedPredicate(predicate, link, comment, false);
}
public Complex linkedPredicate(String predicate, String link, String comment, boolean asList) {
predicateSet.add(predicate); predicateSet.add(predicate);
Complex c = complex(); Complex c = complex();
linkedPredicate(predicate, c, link, comment); linkedPredicate(predicate, c, link, comment, asList);
return c; return c;
} }
@ -144,6 +170,7 @@ public class Turtle {
protected String link; protected String link;
protected List<Triple> objects = new ArrayList<Turtle.Triple>(); protected List<Triple> objects = new ArrayList<Turtle.Triple>();
protected String comment; protected String comment;
protected boolean asList = false;
public String getPredicate() { public String getPredicate() {
return predicate; return predicate;
@ -183,15 +210,15 @@ public class Turtle {
public void comment(String comment) { public void comment(String comment) {
if (!Utilities.noString(comment)) { if (!Utilities.noString(comment)) {
predicate("rdfs:comment", literal(comment)); predicate("rdfs:comment", literal(comment), false);
predicate("dcterms:description", literal(comment)); predicate("dcterms:description", literal(comment), false);
} }
} }
public void label(String label) { public void label(String label) {
if (!Utilities.noString(label)) { if (!Utilities.noString(label)) {
predicate("rdfs:label", literal(label)); predicate("rdfs:label", literal(label), false);
predicate("dc:title", literal(label)); predicate("dc:title", literal(label), false);
} }
} }
@ -445,7 +472,7 @@ public class Turtle {
writer.ln(); writer.ln();
if (!section.comments.isEmpty()) { if (!section.comments.isEmpty()) {
for (String s : section.comments) { for (String s : section.comments) {
writer.ln("# "+s); writer.ln("# "+formatMultilineComment(s));
} }
writer.ln(); writer.ln();
} }
@ -464,10 +491,13 @@ public class Turtle {
writer.write(" "); writer.write(" ");
boolean first = true; boolean first = true;
for (Triple o : p.getObjects()) { for (Triple o : p.getObjects()) {
if (first) if (first) {
first = false; first = false;
else if (p.asList) writer.write("( ");
writer.write(", "); } else {
if (!p.asList) writer.write(", ");
else writer.write(" ");
}
if (o instanceof StringType) if (o instanceof StringType)
writer.write(((StringType) o).value); writer.write(((StringType) o).value);
else { else {
@ -478,10 +508,11 @@ public class Turtle {
writer.write("]"); writer.write("]");
} }
} }
String comment = p.comment == null? "" : " # "+p.comment; String comment = p.comment == null? "" : " # "+formatMultilineComment(p.comment);
if (p.asList) writer.write(" )");
i++; i++;
if (i < sbj.predicates.size()) if (i < sbj.predicates.size())
writer.write(";"+comment+"\r\n "); writer.write(" ;"+comment+"\r\n ");
else { else {
if (Utilities.noString(sbj.id)) if (Utilities.noString(sbj.id))
writer.write("]"); writer.write("]");
@ -491,6 +522,11 @@ public class Turtle {
} }
} }
private String formatMultilineComment(String s) {
return s.replace("\n", "\n#");
}
private void commitSection(StringBuilder b, Section section) throws Exception { private void commitSection(StringBuilder b, Section section) throws Exception {
b.append("# - "+section.name+" "+Utilities.padLeft("", '-', 75-section.name.length())+"\r\n"); b.append("# - "+section.name+" "+Utilities.padLeft("", '-', 75-section.name.length())+"\r\n");
b.append("\r\n"); b.append("\r\n");
@ -511,10 +547,12 @@ public class Turtle {
b.append(" "); b.append(" ");
boolean first = true; boolean first = true;
for (Triple o : p.getObjects()) { for (Triple o : p.getObjects()) {
if (first) if (first) {
first = false; first = false;
else if (p.asList) b.append("( ");
b.append(", "); } else
if (!p.asList) b.append(", ");
else b.append(" ");
if (o instanceof StringType) if (o instanceof StringType)
b.append(Utilities.escapeXml(((StringType) o).value)); b.append(Utilities.escapeXml(((StringType) o).value));
else { else {
@ -525,12 +563,13 @@ public class Turtle {
b.append("]"); b.append("]");
} }
} }
String comment = p.comment == null? "" : " # "+p.comment; String comment = p.comment == null? "" : " # "+formatMultilineComment(p.comment);
if (p.asList) b.append(" )");
i++; i++;
if (i < sbj.predicates.size()) if (i < sbj.predicates.size())
b.append(";"+Utilities.escapeXml(comment)+"\r\n "); b.append(" ;"+Utilities.escapeXml(comment)+"\r\n ");
else else
b.append("."+Utilities.escapeXml(comment)+"\r\n\r\n"); b.append(" ."+Utilities.escapeXml(comment)+"\r\n\r\n");
} }
} }
} }
@ -566,8 +605,11 @@ public class Turtle {
if (first) { if (first) {
first = false; first = false;
writer.write(left+" "+po.getPredicate()+" "); writer.write(left+" "+po.getPredicate()+" ");
} else if (po.asList) writer.write("( ");
writer.write(", "); } else {
if (!po.asList) writer.write(", ");
else writer.write(" ");
}
if (o instanceof StringType) if (o instanceof StringType)
writer.write(((StringType) o).value); writer.write(((StringType) o).value);
else { else {
@ -578,9 +620,10 @@ public class Turtle {
writer.write(" ]"); writer.write(" ]");
} }
} }
if (po.asList) writer.write(" )");
i++; i++;
if (i < complex.predicates.size()) if (i < complex.predicates.size())
writer.write(";"); writer.write(" ;");
if (!Utilities.noString(po.comment)) if (!Utilities.noString(po.comment))
writer.write(" # "+escape(po.comment, false)); writer.write(" # "+escape(po.comment, false));
} }
@ -602,9 +645,11 @@ public class Turtle {
for (Triple o : po.getObjects()) { for (Triple o : po.getObjects()) {
if (first) { if (first) {
first = false; first = false;
b.append(left+" "+po.makelink()+" "); if (po.asList) b.append(left+"( ");
b.append(po.makelink()+" ");
} else } else
b.append(", "); if (!po.asList) b.append(", ");
else b.append(" ");
if (o instanceof StringType) if (o instanceof StringType)
b.append(Utilities.escapeXml(((StringType) o).value)); b.append(Utilities.escapeXml(((StringType) o).value));
else { else {
@ -615,9 +660,10 @@ public class Turtle {
b.append(" ]"); b.append(" ]");
} }
} }
if (po.asList) b.append(" )");
i++; i++;
if (i < complex.predicates.size()) if (i < complex.predicates.size())
b.append(";"); b.append(" ;");
if (!Utilities.noString(po.comment)) if (!Utilities.noString(po.comment))
b.append(" # "+Utilities.escapeXml(escape(po.comment, false))); b.append(" # "+Utilities.escapeXml(escape(po.comment, false)));
} }