List<String> domain =
kAMStore.getAnnotationTypeDomainValues(kamInfo,
annotationType);
if (hasItems(domain)) {
RDFList enumres = model.createList();
enumres.setStrict(true);
// iterate domain to pre-build RDF resources and associate domain value
Resource[] itemResources = new Resource[domain.size()];
String[] domainValues =
domain.toArray(new String[domain.size()]);
for (int i = 0; i < domainValues.length; i++) {
itemResources[i] = model
.createResource(new AnonId(UUID
.randomUUID().toString()));
itemResources[i].addProperty(RDF.type, RDF.List);
itemResources[i].addProperty(RDF.first,
model.createTypedLiteral(domainValues[i]));
}
// iterate item resources to link them
for (int i = 0; i < itemResources.length; i++) {
if ((i + 1) < itemResources.length) {
// link to next domain value
itemResources[i].addProperty(RDF.rest,
itemResources[i + 1]);
} else {
// link to RDF.nil indicating end of list
itemResources[i].addProperty(RDF.rest, RDF.nil);
}
enumres = enumres.with(itemResources[i]);
}
// associate annotation to the domain list
annotationDefResource.addProperty(hasList, enumres);
}
break;
case REGULAR_EXPRESSION:
// read list domain for internal pattern annotation
domain =
kAMStore.getAnnotationTypeDomainValues(kamInfo,
annotationType);
if (domain.size() != 1) {
throw new IllegalStateException(
"Expecting a single, Regular Expression pattern, but received: "
+ domain);
}
annotationDefResource =
model
.createResource(new AnonId(UUID
.randomUUID().toString()));
// associate metadata of annotation type
annotationDefResource.addProperty(hasName,
model.createTypedLiteral(annotationType.getName()));
annotationDefResource.addProperty(hasDescription,
model.createTypedLiteral(annotationType
.getDescription()));
annotationDefResource.addProperty(hasType, model
.createTypedLiteral(annotationType
.getAnnotationDefinitionType()
.getDisplayValue()));
annotationDefResource
.addProperty(hasUsage, model
.createTypedLiteral(annotationType
.getUsage()));
String patternDomain = domain.get(0);
// associate annotation to the domain pattern
annotationDefResource.addProperty(hasPattern,
model.createTypedLiteral(patternDomain));
break;
case URL:
if (annotationType.getUrl() == null) {
throw new IllegalStateException(
"Expecting non-null URL for external annotation");
}
// associate annotation to the URL where the domain is defined
annotationDefResource =
model.createResource(annotationType.getUrl());
annotationDefResource.addProperty(hasName,
model.createTypedLiteral(annotationType.getName()));
annotationDefResource.addProperty(hasURL,
model.createTypedLiteral(annotationType.getUrl()));
break;
default:
throw new UnsupportedOperationException(
"Annotation definition type not supported: "
+ annotationType
.getAnnotationDefinitionType());
}
annotationDefResource.addProperty(RDF.type,
AnnotationDefinition);
// associate document with annotation definition as a defines relationship
docResource.addProperty(defines, annotationDefResource);
annotationDefinitions.put(annotationType.getId(),
annotationDefResource);
}
kamResource.addProperty(composedOf, docResource);
documents.put(doc.getId(), docResource);
}
Map<KamNode, Resource> kamNodeResources =
new HashMap<Kam.KamNode, Resource>();
Map<Integer, Resource> termResources = new HashMap<Integer, Resource>();
// handle kam nodes first
Iterator<KamNode> kamNodes = kam.getNodes().iterator();
while (kamNodes.hasNext()) {
KamNode kamNode = kamNodes.next();
// TODO: This is a sanity check that should be removed as LIST is not a KAM function type
if (FunctionEnum.LIST == kamNode.getFunctionType()) {
System.err.println("Invalid KAM node type found: "
+ kamNode.getFunctionType().getDisplayValue());
continue;
}
Resource functionResource = model.getResource(KAMVocabulary
.resourceForFunction(kamNode.getFunctionType()).getURI());
// Retrieve seen KAMNode Resource or create a new Blank Node for it.
Resource kamNodeResource;
if (kamNodeResources.containsKey(kamNode)) {
kamNodeResource = kamNodeResources.get(kamNode);
} else {
kamNodeResource = model.createResource(new AnonId(UUID
.randomUUID().toString()));
// associate kam node with kam resource
kamResource.addProperty(composedOf, kamNodeResource);
}
// node type KAMNode
kamNodeResource.addProperty(RDF.type, KAMNode);
// node hasFunction Function
kamNodeResource.addProperty(hasFunction, functionResource);
// node hasId "1"^^xsd:int
kamNodeResource.addLiteral(hasId,
model.createTypedLiteral(kamNode.getId()));
// node hasLabel "p(EG:207)"^^xsd:string
kamNodeResource.addLiteral(hasLabel,
model.createTypedLiteral(kamNode.getLabel()));
// hold on to kam node resources
kamNodeResources.put(kamNode, kamNodeResource);
// handle terms for this KAMNode
// TODO Support nested terms instead of just Parameters!
List<BelTerm> terms = kAMStore.getSupportingTerms(kamNode);
for (BelTerm term : terms) {
Resource termResource = model.createResource(new AnonId(UUID
.randomUUID().toString()));
termResource.addProperty(RDF.type, Term);
termResource.addProperty(hasId,
model.createTypedLiteral(term.getId()));
termResource.addProperty(hasLabel,
model.createTypedLiteral(term.getLabel()));
termResource.addProperty(hasFunction,
resourceForFunction(kamNode.getFunctionType()));
RDFList argumentres = model.createList();
List<TermParameter> termParameters =
kAMStore.getTermParameters(kamInfo, term);
TermParameter[] tparray = termParameters
.toArray(new TermParameter[termParameters.size()]);
Resource[] tpresarray = new Resource[tparray.length];
// iterate term parameters to pre-build RDF resources and associate Parameter
for (int i = 0; i < tparray.length; i++) {
tpresarray[i] = model.createResource(new AnonId(UUID
.randomUUID().toString()));
tpresarray[i].addProperty(RDF.type, RDF.List);
Resource parameterResource =
model
.createResource(new AnonId(UUID
.randomUUID().toString()));
parameterResource.addProperty(RDF.type, Parameter);
parameterResource
.addProperty(hasValue, model
.createTypedLiteral(tparray[i]
.getParameterValue()));
if (tparray[i].getNamespace() != null) {
parameterResource.addProperty(hasNamespace, model
.getResource(tparray[i].getNamespace()
.getResourceLocation()));
}
tpresarray[i].addProperty(RDF.first, parameterResource);
}
// iterate argument resources to link them
for (int i = 0; i < tpresarray.length; i++) {
if ((i + 1) < tpresarray.length) {
// link to next term argument
tpresarray[i].addProperty(RDF.rest, tpresarray[i + 1]);
} else {
// link to RDF.nil indicating end of list
tpresarray[i].addProperty(RDF.rest, RDF.nil);
}
argumentres = argumentres.with(tpresarray[i]);
}
// associate term resource to the arguments list
termResource.addProperty(hasArguments, argumentres);