@Override public void newIndividual(String individualURI, String individualTypeURI, String graphURI) throws RDFServiceException { StringBuffer containsQuery = new StringBuffer("ASK { \n"); if (graphURI != null) { containsQuery.append(" GRAPH <" + graphURI + "> { "); } containsQuery.append("<"); containsQuery.append(individualURI); containsQuery.append("> "); containsQuery.append("?p ?o"); if (graphURI != null) { containsQuery.append(" } \n"); } containsQuery.append("\n}"); if (sparqlAskQuery(containsQuery.toString())) { throw new RDFServiceException("individual already exists"); } else { Triple triple = new Triple( Node.createURI(individualURI), RDF.type.asNode(), Node.createURI(individualTypeURI)); // addTriple(triple, graphURI); ChangeSet cs = this.manufactureChangeSet(); cs.addAddition( new ByteArrayInputStream(sparqlTriple(triple).getBytes()), ModelSerializationFormat.N3, graphURI); changeSetUpdate(cs); } }
private String doRename(String oldNamespace, String newNamespace) { String uri = null; String result = null; Integer counter = 0; Boolean namespacePresent = false; RDFService rdfService = ModelAccess.on(getServletContext()).getRDFService(); try { Model baseOntModel = RDFServiceGraph.createRDFServiceModel(new RDFServiceGraph(rdfService, ABOX_ASSERTIONS)); OntModel ontModel = ModelAccess.on(getServletContext()).getOntModel(); List<String> urisToChange = new LinkedList<String>(); ontModel.enterCriticalSection(Lock.READ); try { Iterator<Individual> indIter = ontModel.listIndividuals(); while (indIter.hasNext()) { Individual ind = indIter.next(); String namespace = ind.getNameSpace(); if (namespace != null) { if (oldNamespace.equals(namespace)) { uri = ind.getURI(); urisToChange.add(uri); namespacePresent = true; } } } } finally { ontModel.leaveCriticalSection(); } if (!namespacePresent) { result = "no resources renamed"; return result; } for (String oldURIStr : urisToChange) { long time1 = System.currentTimeMillis(); Resource res = baseOntModel.getResource(oldURIStr); long time2 = System.currentTimeMillis(); String newURIStr = null; Pattern p = Pattern.compile(oldNamespace); String candidateString = res.getURI(); Matcher matcher = p.matcher(candidateString); newURIStr = matcher.replaceFirst(newNamespace); long time3 = System.currentTimeMillis(); log.debug("time to get new uri: " + Long.toString(time3 - time2)); log.debug("Renaming " + oldURIStr + " to " + newURIStr); String whereClause = "} WHERE { \n" + " GRAPH <" + ABOX_ASSERTIONS + "> { \n" + " { <" + oldURIStr + "> ?p <" + oldURIStr + "> } \n " + " UNION \n" + " { <" + oldURIStr + "> ?q ?o } \n " + " UNION \n" + " { ?s ?r <" + oldURIStr + "> } \n" + " } \n" + "}"; String removeQuery = "CONSTRUCT { \n" + " <" + oldURIStr + "> ?p <" + oldURIStr + "> . \n " + " <" + oldURIStr + "> ?q ?o . \n " + " ?s ?r <" + oldURIStr + "> \n" + whereClause; String addQuery = "CONSTRUCT { \n" + " <" + newURIStr + "> ?p <" + newURIStr + "> . \n " + " <" + newURIStr + "> ?q ?o . \n " + " ?s ?r <" + newURIStr + "> \n" + whereClause; try { ChangeSet cs = rdfService.manufactureChangeSet(); cs.addAddition( rdfService.sparqlConstructQuery(addQuery, RDFService.ModelSerializationFormat.N3), RDFService.ModelSerializationFormat.N3, ABOX_ASSERTIONS); cs.addRemoval( rdfService.sparqlConstructQuery(removeQuery, RDFService.ModelSerializationFormat.N3), RDFService.ModelSerializationFormat.N3, ABOX_ASSERTIONS); rdfService.changeSetUpdate(cs); } catch (RDFServiceException e) { throw new RuntimeException(e); } long time4 = System.currentTimeMillis(); log.debug(" time to rename : " + Long.toString(time4 - time3)); log.debug(" time for one resource: " + Long.toString(time4 - time1)); counter++; } result = counter.toString() + " resources renamed"; return result; } finally { if (rdfService != null) { rdfService.close(); } } }