/** * Returns a Resource in the specified model that is an instance of the specified type with the * specified ID. The Resource will be created with an appropriate URI (@link {@link * #OUTPUT_URI_PATTERN}). * * @param model the model in which to create the new Resource * @param type the type (class) of the new instance * @param id the id of the new instance * @return a Resource view of the new instance */ public static Resource createInstance(Model model, Resource type, String id) { Matcher matcher = TYPE_PATTERN.matcher(type.getURI()); if (matcher.find()) { String namespace = matcher.group(1); String uri = getURI(namespace, id); Resource lsrnNode = model.createResource(uri, type); SIOUtils.createAttribute(lsrnNode, SIO.has_identifier, getIdentifierClass(namespace), id); return lsrnNode; } else { throw new IllegalArgumentException( "at present this method only works with LSRN database record classes"); } }
/** * Adds the LSRN SIO identifier structure to an LSRN-typed Resource. Attempts to use the URL of * the Resource to determine the ID. * * @param lsrnNode */ public static void addIdentifier(Resource lsrnNode) { if (lsrnNode.isURIResource()) { Matcher idMatcher = ID_PATTERN.matcher(lsrnNode.getURI()); if (idMatcher.find()) { String id = idMatcher.group(1); for (Resource type : RdfUtils.getTypes(lsrnNode).toList()) { if (!type.isURIResource()) continue; Matcher typeMatcher = TYPE_PATTERN.matcher(type.getURI()); if (typeMatcher.find()) { String idTypeURI = OUTPUT_ID_TYPE_PATTERN.replace("$NS", typeMatcher.group(1)); SIOUtils.createAttribute( lsrnNode, SIO.has_identifier, ResourceFactory.createResource(idTypeURI), id); } } } } }