private void processOutputModelRequest(VitroRequest vreq, HttpServletResponse response) { String modelNameStr = vreq.getParameter("modelName"); Model model = getModel(modelNameStr, vreq); JenaOutputUtils.setNameSpacePrefixes(model, vreq.getWebappDaoFactory()); model.enterCriticalSection(Lock.READ); try { OutputStream out = response.getOutputStream(); response.setContentType("application/x-turtle"); // out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".getBytes()); model.write(out, "TTL"); out.flush(); out.close(); } catch (com.hp.hpl.jena.shared.CannotEncodeCharacterException cece) { // there's got to be a better way to do this byte[] badCharBytes = String.valueOf(cece.getBadChar()).getBytes(); String errorMsg = "Cannot encode character with byte values: (decimal) "; for (int i = 0; i < badCharBytes.length; i++) { errorMsg += badCharBytes[i]; } throw new RuntimeException(errorMsg, cece); } catch (Exception e) { log.error(e, e); } finally { model.leaveCriticalSection(); } }
private void doLoadRDFData( String modelName, String docLoc, String filePath, String language, ModelMaker modelMaker) { Model m = modelMaker.getModel(modelName); m.enterCriticalSection(Lock.WRITE); try { if ((docLoc != null) && (docLoc.length() > 0)) { m.read(docLoc, language); } else if ((filePath != null) && (filePath.length() > 0)) { File file = new File(filePath); File[] files; if (file.isDirectory()) { files = file.listFiles(); } else { files = new File[1]; files[0] = file; } for (int i = 0; i < files.length; i++) { File currentFile = files[i]; log.info("Reading file " + currentFile.getName()); FileInputStream fis; try { fis = new FileInputStream(currentFile); m.read(fis, null, language); fis.close(); } catch (IOException ioe) { throw new RuntimeException(ioe); } } } } finally { m.leaveCriticalSection(); } }
@Override public List<Node> getPredicates(Node subject, Node object) { Model graph = null; GraphConnection graphConnection = null; try { graphConnection = openGraph(); graph = graphConnection.getGraph(); graph.enterCriticalSection(Lock.READ); SimpleSelector selector = getJenaSelector(graph, new StatementImpl(subject, null, object)); StmtIterator it = graph.listStatements(selector); List<Statement> statements = getNXRelationsStatements(graph, it.toList()); List<Node> res = new ArrayList<Node>(); for (Statement stmt : statements) { Node predicate = stmt.getPredicate(); if (!res.contains(predicate)) { // remove duplicates res.add(predicate); } } return res; } finally { if (graph != null) { graph.leaveCriticalSection(); } if (graphConnection != null) { graphConnection.close(); } } }
@Override public void remove(List<Statement> statements) { Model graph = null; GraphConnection graphConnection = null; try { graphConnection = openGraph(); graph = graphConnection.getGraph(); graph.enterCriticalSection(Lock.WRITE); for (Statement nuxStmt : statements) { com.hp.hpl.jena.graph.Node subject = getJenaNode(nuxStmt.getSubject()); com.hp.hpl.jena.graph.Node predicate = getJenaNode(nuxStmt.getPredicate()); com.hp.hpl.jena.graph.Node object = getJenaNode(nuxStmt.getObject()); Triple jenaTriple = Triple.create(subject, predicate, object); com.hp.hpl.jena.rdf.model.Statement jenaStmt = graph.asStatement(jenaTriple); graph.remove(jenaStmt); // remove properties RSIterator it = graph.listReifiedStatements(jenaStmt); while (it.hasNext()) { ReifiedStatement rs = it.nextRS(); rs.removeProperties(); } // remove quadlets graph.removeAllReifications(jenaStmt); // graph.removeReification(reifiedStmt); } } finally { if (graph != null) { graph.leaveCriticalSection(); } if (graphConnection != null) { graphConnection.close(); } } }
@Override public void clear() { Model graph = null; GraphConnection graphConnection = null; try { graphConnection = openGraph(); graph = graphConnection.getGraph(); graph.enterCriticalSection(Lock.READ); graph.removeAll(); // XXX AT: remove reification quadlets explicitly RSIterator it = graph.listReifiedStatements(); List<ReifiedStatement> rss = new ArrayList<ReifiedStatement>(); while (it.hasNext()) { rss.add(it.nextRS()); } for (ReifiedStatement rs : rss) { graph.removeReification(rs); } } finally { if (graph != null) { graph.leaveCriticalSection(); } if (graphConnection != null) { graphConnection.close(); } } }
@Override public List<Node> getSubjects(Node predicate, Node object) { Model graph = null; GraphConnection graphConnection = null; try { graphConnection = openGraph(); graph = graphConnection.getGraph(); graph.enterCriticalSection(Lock.READ); SimpleSelector selector = getJenaSelector(graph, new StatementImpl(null, predicate, object)); ResIterator it = graph.listSubjectsWithProperty(selector.getPredicate(), selector.getObject()); List<Node> res = new ArrayList<Node>(); while (it.hasNext()) { res.add(getNXRelationsNode(it.nextResource().asNode())); } return res; } finally { if (graph != null) { graph.leaveCriticalSection(); } if (graphConnection != null) { graphConnection.close(); } } }
private long doExecuteSparql(VitroRequest vreq) { OntModel jenaOntModel = ModelAccess.on(getServletContext()).getOntModel(); OntModel source = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM); String[] sourceModel = vreq.getParameterValues("sourceModelName"); for (int i = 0; i < sourceModel.length; i++) { Model m = getModel(sourceModel[i], vreq); source.addSubModel(m); } Model destination = getModel(vreq.getParameter("destinationModelName"), vreq); String sparqlQueryStr = vreq.getParameter("sparqlQueryStr"); String savedQueryURIStr = vreq.getParameter("savedQuery"); String queryStr; if (savedQueryURIStr.length() == 0) { log.debug("Using entered query"); queryStr = sparqlQueryStr; } else { Property queryStrProp = ResourceFactory.createProperty(SPARQL_QUERYSTR_PROP); jenaOntModel.enterCriticalSection(Lock.READ); try { Individual ind = jenaOntModel.getIndividual(savedQueryURIStr); log.debug("Using query " + savedQueryURIStr); queryStr = ((Literal) ind.getPropertyValue(queryStrProp)).getLexicalForm(); queryStr = StringEscapeUtils.unescapeHtml( queryStr); // !!! We need to turn off automatic HTML-escaping for data property // editing. } finally { jenaOntModel.leaveCriticalSection(); } } Model tempModel = ModelFactory.createDefaultModel(); Query query = SparqlQueryUtils.create(queryStr); QueryExecution qexec = QueryExecutionFactory.create(query, source); try { qexec.execConstruct(tempModel); } catch (QueryExecException qee) { qexec.execDescribe(tempModel); } destination.enterCriticalSection(Lock.WRITE); try { if (destination instanceof OntModel) { ((OntModel) destination).getBaseModel().notifyEvent(new EditEvent(null, true)); } else { destination.notifyEvent(new EditEvent(null, true)); } destination.add(tempModel); } finally { if (destination instanceof OntModel) { ((OntModel) destination).getBaseModel().notifyEvent(new EditEvent(null, false)); } else { destination.notifyEvent(new EditEvent(null, false)); } destination.leaveCriticalSection(); } return tempModel.size(); }
public void doCleanLiterals(Model model) { Model retractionsModel = ModelFactory.createDefaultModel(); Model additionsModel = ModelFactory.createDefaultModel(); model.enterCriticalSection(Lock.WRITE); try { ClosableIterator<Statement> closeIt = model.listStatements(); try { for (Iterator<Statement> stmtIt = closeIt; stmtIt.hasNext(); ) { Statement stmt = stmtIt.next(); if (stmt.getObject().isLiteral()) { Literal lit = (Literal) stmt.getObject(); String lex = lit.getLexicalForm(); char[] chars = lex.toCharArray(); char[] cleanChars = new char[chars.length]; int cleanPos = 0; boolean badChar = false; for (int i = 0; i < chars.length; i++) { if (java.lang.Character.getNumericValue(chars[i]) > 31 && java.lang.Character.isDefined(chars[i])) { cleanChars[cleanPos] = chars[i]; cleanPos++; } else { log.error("Bad char in " + lex); log.error("Numeric value " + java.lang.Character.getNumericValue(chars[i])); badChar = true; } } String cleanLex = new String(cleanChars); if (badChar) { retractionsModel.add(stmt); Literal newLit = null; if (lit.getLanguage() != null && lit.getLanguage().length() > 0) { newLit = additionsModel.createLiteral(cleanLex, lit.getLanguage()); } else if (lit.getDatatype() != null) { newLit = additionsModel.createTypedLiteral(cleanLex, lit.getDatatype()); } else { newLit = additionsModel.createLiteral(cleanLex); } additionsModel.add(stmt.getSubject(), stmt.getPredicate(), newLit); } } } } finally { closeIt.close(); } model.remove(retractionsModel); model.add(additionsModel); log.debug("Cleaned " + additionsModel.size() + " literals"); } finally { model.leaveCriticalSection(); } }
@Override public QueryResult query(String queryString, String language, String baseURI) { Model graph = null; GraphConnection graphConnection = null; QueryResult res = null; QueryExecution qe = null; try { graphConnection = openGraph(); graph = graphConnection.getGraph(); graph.enterCriticalSection(Lock.READ); log.debug(String.format("Running query %s", queryString)); // XXX AT: ignore language for now if (language != null && !language.equals("sparql")) { log.warn(String.format("Unknown language %s for query, using SPARQL", language)); } Query query = QueryFactory.create(queryString); query.setBaseURI(baseURI); qe = QueryExecutionFactory.create(query, graph); res = new QueryResultImpl(0, new ArrayList<String>(), new ArrayList<Map<String, Node>>()); ResultSet jenaResults = qe.execSelect(); Integer count = 0; List<String> variableNames = jenaResults.getResultVars(); List<Map<String, Node>> nuxResults = new ArrayList<Map<String, Node>>(); while (jenaResults.hasNext()) { QuerySolution soln = jenaResults.nextSolution(); Map<String, Node> nuxSol = new HashMap<String, Node>(); for (String varName : variableNames) { RDFNode x = soln.get(varName); nuxSol.put(varName, getNXRelationsNode(x.asNode())); } nuxResults.add(nuxSol); count++; } res = new QueryResultImpl(count, variableNames, nuxResults); } finally { if (qe != null) { // Important - free up resources used running the query qe.close(); } if (graph != null) { graph.leaveCriticalSection(); } if (graphConnection != null) { graphConnection.close(); } } return res; }
private void doRenameBNodes( VitroRequest vreq, String namespaceEtc, boolean patternBoolean, String pattern, String[] sourceModel) { OntModel source = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM); String property = vreq.getParameter("property"); Boolean csv2rdf = false; try { csv2rdf = Boolean.parseBoolean(vreq.getParameter("csv2rdf")); } catch (Exception e) { log.error(e, e); } if (csv2rdf) { source.addSubModel((Model) vreq.getSession().getAttribute("csv2rdfResult")); } else { for (int i = 0; i < sourceModel.length; i++) { Model m = getModel(sourceModel[i], vreq); source.addSubModel(m); } } Model destination = (csv2rdf) ? ModelFactory.createDefaultModel() : getModel(vreq.getParameter("destinationModelName"), vreq); JenaIngestUtils utils = new JenaIngestUtils(); destination.enterCriticalSection(Lock.WRITE); try { if (!patternBoolean) { destination.add(utils.renameBNodes(source, namespaceEtc, vreq.getJenaOntModel())); } else { destination.add( utils.renameBNodesByPattern( source, namespaceEtc, vreq.getJenaOntModel(), pattern, property)); } if (csv2rdf) { Model ultimateDestination = getModel(vreq.getParameter("destinationModelName"), vreq); ultimateDestination.add(destination); } } finally { destination.leaveCriticalSection(); } }
@Override public void add(List<Statement> statements) { Model graph = null; GraphConnection graphConnection = null; try { graphConnection = openGraph(); graph = graphConnection.getGraph(); graph.enterCriticalSection(Lock.WRITE); for (Statement nuxStmt : statements) { com.hp.hpl.jena.graph.Node subject = getJenaNode(nuxStmt.getSubject()); com.hp.hpl.jena.graph.Node predicate = getJenaNode(nuxStmt.getPredicate()); com.hp.hpl.jena.graph.Node object = getJenaNode(nuxStmt.getObject()); Triple jenaTriple = Triple.create(subject, predicate, object); com.hp.hpl.jena.rdf.model.Statement jenaStmt = graph.asStatement(jenaTriple); // properties Map<Resource, Node[]> properties = nuxStmt.getProperties(); if (properties == null || properties.isEmpty()) { // no properties graph.add(jenaStmt); } else { List<com.hp.hpl.jena.rdf.model.Statement> stmts = new ArrayList<com.hp.hpl.jena.rdf.model.Statement>(); stmts.add(jenaStmt); // create reified statement if it does not exist com.hp.hpl.jena.graph.Node reifiedStmt = graph.getAnyReifiedStatement(jenaStmt).asNode(); for (Map.Entry<Resource, Node[]> property : properties.entrySet()) { com.hp.hpl.jena.graph.Node prop = getJenaNode(property.getKey()); for (Node node : property.getValue()) { com.hp.hpl.jena.graph.Node value = getJenaNode(node); Triple propTriple = Triple.create(reifiedStmt, prop, value); stmts.add(graph.asStatement(propTriple)); } } graph.add(stmts); } } } finally { if (graph != null) { graph.leaveCriticalSection(); } if (graphConnection != null) { graphConnection.close(); } } }
/** * Returns the number of statements in the graph. * * <p>XXX AT: this size may not be equal to the number of statements retrieved via getStatements() * because it counts each statement property. * * @return integer number of statements in the graph */ @Override public Long size() { Model graph = null; GraphConnection graphConnection = null; try { graphConnection = openGraph(); graph = graphConnection.getGraph(); graph.enterCriticalSection(Lock.READ); return graph.size(); } finally { if (graph != null) { graph.leaveCriticalSection(); } if (graphConnection != null) { graphConnection.close(); } } }
private void doSmushSingleModel(VitroRequest vreq) { OntModel source = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM); String[] sourceModel = vreq.getParameterValues("sourceModelName"); for (int i = 0; i < sourceModel.length; i++) { Model m = getModel(sourceModel[i], vreq); source.addSubModel(m); } Model destination = getModel(vreq.getParameter("destinationModelName"), vreq); String propertyURIStr = vreq.getParameter("propertyURI"); Property prop = ResourceFactory.createProperty(propertyURIStr); JenaIngestUtils utils = new JenaIngestUtils(); destination.enterCriticalSection(Lock.WRITE); try { destination.add(utils.smushResources(source, prop)); } finally { destination.leaveCriticalSection(); } }
@Override public List<Statement> getStatements() { Model graph = null; GraphConnection graphConnection = null; try { graphConnection = openGraph(); graph = graphConnection.getGraph(); graph.enterCriticalSection(Lock.READ); StmtIterator it = graph.listStatements(); return getNXRelationsStatements(graph, it.toList()); } finally { if (graph != null) { graph.leaveCriticalSection(); } if (graphConnection != null) { graphConnection.close(); } } }
@Override public boolean write(OutputStream out, String lang, String base) { Model graph = null; GraphConnection graphConnection = null; try { graphConnection = openGraph(); graph = graphConnection.getGraph(); graph.enterCriticalSection(Lock.WRITE); graph.write(out, lang, base); // default to true return true; } finally { if (graph != null) { graph.leaveCriticalSection(); } if (graphConnection != null) { graphConnection.close(); } } }
@Override public boolean read(InputStream in, String lang, String base) { // XXX AT: maybe update namespaces in case some new appeared Model graph = null; GraphConnection graphConnection = null; try { graphConnection = openGraph(); graph = graphConnection.getGraph(); graph.enterCriticalSection(Lock.READ); graph.read(in, base, lang); // default to true return true; } finally { if (graph != null) { graph.leaveCriticalSection(); } if (graphConnection != null) { graphConnection.close(); } } }
@Override public boolean hasStatement(Statement statement) { if (statement == null) { return false; } Model graph = null; GraphConnection graphConnection = null; try { graphConnection = openGraph(); graph = graphConnection.getGraph(); graph.enterCriticalSection(Lock.READ); SimpleSelector selector = getJenaSelector(graph, statement); return graph.contains(selector.getSubject(), selector.getPredicate(), selector.getObject()); } finally { if (graph != null) { graph.leaveCriticalSection(); } if (graphConnection != null) { graphConnection.close(); } } }
@Override public boolean hasResource(Resource resource) { if (resource == null) { return false; } Model graph = null; GraphConnection graphConnection = null; try { graphConnection = openGraph(); graph = graphConnection.getGraph(); graph.enterCriticalSection(Lock.READ); com.hp.hpl.jena.graph.Node jenaNodeInst = getJenaNode(resource); RDFNode jenaNode = graph.asRDFNode(jenaNodeInst); return graph.containsResource(jenaNode); } finally { if (graph != null) { graph.leaveCriticalSection(); } if (graphConnection != null) { graphConnection.close(); } } }
private void tryRemove(Statement stmt) { boolean removeFromPellet = false; if (pipeOpen && reasonerConfiguration.getReasonOnAllDatatypePropertyStatements() && stmt.getObject().isLiteral()) { removeFromPellet = true; } else if (pipeOpen && hasCardinalityPredicate(stmt)) { // see comment on this method removeFromPellet = true; } else if (stmt.getObject().isResource()) { if (pipeOpen) { if (reasonerConfiguration.getQueryForAllObjectProperties() && stmt.getPredicate().equals(RDF.type) && stmt.getObject().equals(OWL.ObjectProperty)) { deletedObjectProperties.enterCriticalSection(Lock.WRITE); try { deletedObjectProperties.add(stmt); } finally { deletedObjectProperties.leaveCriticalSection(); } } if (reasonerConfiguration.getQueryForAllDatatypeProperties() && stmt.getPredicate().equals(RDF.type) && stmt.getObject().equals(OWL.DatatypeProperty)) { deletedDataProperties.enterCriticalSection(Lock.WRITE); try { deletedDataProperties.add(stmt); } finally { deletedDataProperties.leaveCriticalSection(); } } removeFromPellet = false; boolean denied = false; ObjectPropertyStatementPattern stPat = ObjectPropertyStatementPatternFactory.getPattern( stmt.getSubject(), stmt.getPredicate(), (Resource) stmt.getObject()); if (inferenceDrivingPatternDenySet != null) { for (Iterator<ObjectPropertyStatementPattern> i = inferenceDrivingPatternDenySet.iterator(); i.hasNext(); ) { ObjectPropertyStatementPattern pat = i.next(); if (pat.matches(stPat)) { denied = true; break; } } } if (!denied) { if (inferenceDrivingPatternAllowSet == null) { removeFromPellet = true; } else { // TODO: O(1) implementation of this List<ObjectPropertyStatementPattern> patList = this.inferenceDrivingPatternMap.get(stmt.getPredicate()); if (patList != null) { for (Iterator<ObjectPropertyStatementPattern> i = patList.iterator(); i.hasNext(); ) { ObjectPropertyStatementPattern pat = i.next(); if (pat.matches(stPat)) { removeFromPellet = true; break; } } } } } } } if (removeFromPellet) { String valueStr = (stmt.getObject().isResource()) ? ((Resource) stmt.getObject()).getLocalName() : ((Literal) stmt.getObject()).getLexicalForm(); log.info( "Removing from Pellet: " + stmt.getSubject().getLocalName() + " " + stmt.getPredicate().getLocalName() + " " + valueStr); removalModel.enterCriticalSection(Lock.WRITE); try { removalModel.add(stmt); } finally { removalModel.leaveCriticalSection(); } } }
private void tryAdd(Statement stmt) { boolean sendToPellet = false; if (pipeOpen && reasonerConfiguration.getReasonOnAllDatatypePropertyStatements() && stmt.getObject().isLiteral()) { sendToPellet = true; } else if (pipeOpen && hasCardinalityPredicate(stmt)) { // see comment on this method sendToPellet = true; } else if ((stmt.getObject().isResource()) && !((stmt.getPredicate().getURI().indexOf(VitroVocabulary.vitroURI) == 0))) { if (pipeOpen) { sendToPellet = false; boolean denied = false; ObjectPropertyStatementPattern stPat = ObjectPropertyStatementPatternFactory.getPattern( stmt.getSubject(), stmt.getPredicate(), (Resource) stmt.getObject()); if (inferenceDrivingPatternDenySet != null) { for (Iterator<ObjectPropertyStatementPattern> i = inferenceDrivingPatternDenySet.iterator(); i.hasNext(); ) { ObjectPropertyStatementPattern pat = i.next(); if (pat.matches(stPat)) { denied = true; break; } } } if (!denied) { if (inferenceDrivingPatternAllowSet == null) { sendToPellet = true; } else { // TODO: O(1) implementation of this List<ObjectPropertyStatementPattern> patList = this.inferenceDrivingPatternMap.get(stmt.getPredicate()); if (patList != null) { for (Iterator<ObjectPropertyStatementPattern> i = patList.iterator(); i.hasNext(); ) { ObjectPropertyStatementPattern pat = i.next(); if (pat.matches(stPat)) { sendToPellet = true; break; } } } } } } } if (sendToPellet) { // long startTime = System.currentTimeMillis(); String valueStr = (stmt.getObject().isResource()) ? ((Resource) stmt.getObject()).getLocalName() : ((Literal) stmt.getObject()).getLexicalForm(); if (log.isDebugEnabled()) { log.debug("Adding to Pellet: " + renderStatement(stmt)); } additionModel.enterCriticalSection(Lock.WRITE); try { additionModel.add(stmt); } finally { additionModel.leaveCriticalSection(); } } else { if (log.isDebugEnabled()) { log.debug("Not adding to Pellet: " + renderStatement(stmt)); } } }
public void doProcessStrings(VitroRequest vreq) { try { String className = vreq.getParameter("className"); String methodName = vreq.getParameter("methodName"); String propertyName = vreq.getParameter("propertyName"); String newPropertyName = vreq.getParameter("newPropertyName"); // for now, we'll make the destination and source models the same Model destination = getModel(vreq.getParameter("destinationModelName"), vreq); String processModel = vreq.getParameter("processModel"); Model savedAdditionsModel = null; Model savedRetractionsModel = null; String additionsModelStr = vreq.getParameter("additionsModel"); if ((additionsModelStr != null) && (additionsModelStr.length() > 0)) { savedAdditionsModel = getModel(additionsModelStr, vreq); } String retractionsModelStr = vreq.getParameter("retractionsModel"); if ((retractionsModelStr != null) && (retractionsModelStr.length() > 0)) { savedRetractionsModel = getModel(retractionsModelStr, vreq); } Model additionsModel = ModelFactory.createDefaultModel(); Model retractionsModel = ModelFactory.createDefaultModel(); Class<?> stringProcessorClass = Class.forName(className); Object processor = stringProcessorClass.newInstance(); Class<?>[] methArgs = {String.class}; Method meth = stringProcessorClass.getMethod(methodName, methArgs); Property prop = ResourceFactory.createProperty(propertyName); Property newProp = ResourceFactory.createProperty(newPropertyName); destination.enterCriticalSection(Lock.READ); try { ClosableIterator<Statement> closeIt = destination.listStatements((Resource) null, prop, (RDFNode) null); for (Iterator<Statement> stmtIt = closeIt; stmtIt.hasNext(); ) { Statement stmt = stmtIt.next(); if (stmt.getObject().isLiteral()) { Literal lit = (Literal) stmt.getObject(); String lex = lit.getLexicalForm(); Object[] args = {lex}; String newLex = ""; try { newLex = (String) meth.invoke(processor, args); } catch (InvocationTargetException e) { throw new RuntimeException(e); } if (!newLex.equals(lex)) { retractionsModel.add(stmt); if (newLex.length() > 0) { Literal newLit = null; if (lit.getLanguage() != null && lit.getLanguage().length() > 0) { newLit = additionsModel.createLiteral(newLex, lit.getLanguage()); } else if (lit.getDatatype() != null) { newLit = additionsModel.createTypedLiteral(newLex, lit.getDatatype()); } else { newLit = additionsModel.createLiteral(newLex); } additionsModel.add(stmt.getSubject(), newProp, newLit); } } } } if (processModel != null) { destination.add(additionsModel); destination.remove(retractionsModel); } if (savedAdditionsModel != null) { savedAdditionsModel.add(additionsModel); } if (savedRetractionsModel != null) { savedRetractionsModel.add(retractionsModel); } } finally { destination.leaveCriticalSection(); } } catch (Exception e) { throw new RuntimeException(e); } }