public static void collectPredicates(Model m, Collection target) throws ModelException { for (Enumeration en = m.elements(); en.hasMoreElements(); ) { Statement st = (Statement) en.nextElement(); target.add(st.predicate()); } }
public static void collectLiterals(Model m, Collection target) throws ModelException { for (Enumeration en = m.elements(); en.hasMoreElements(); ) { Statement st = (Statement) en.nextElement(); if (st.object() instanceof Literal) target.add(st.object()); } }
public static Hashtable getNodes(Model m) throws ModelException { Hashtable t = new Hashtable(); for (Enumeration en = m.elements(); en.hasMoreElements(); ) { Statement s = (Statement) en.nextElement(); t.put(s.subject(), s.subject()); t.put(s.object(), s.object()); } return t; }
// FIXME: use digest instead of size static void getReachable(Resource r, Model m, Model result) throws ModelException { int oldSize = result.size(); Model directlyReachable = m.find(r, null, null); SetOperations.unite(result, directlyReachable); if (result.size() == oldSize) return; for (Enumeration en = directlyReachable.elements(); en.hasMoreElements(); ) { Statement t = (Statement) en.nextElement(); if (t.object() instanceof Resource) getReachable((Resource) t.object(), m, result); } }
// returns list of statements public static void replaceMult(Statement st, NodeFactory f, Map o2n, Collection result) throws ModelException { List l1 = new ArrayList(); replaceMultSPO(st, f, o2n, l1, st.subject(), 0); List l2 = new ArrayList(); for (int i = 0; i < l1.size(); i++) replaceMultSPO((Statement) l1.get(i), f, o2n, l2, st.predicate(), 1); for (int i = 0; i < l2.size(); i++) replaceMultSPO((Statement) l2.get(i), f, o2n, result, st.object(), 2); }
/** returns true if old triples from r were removed */ public static boolean setUniqueObject( Model r, Resource subject, Resource predicate, RDFNode object) throws ModelException { Model old = r.find(subject, predicate, null); SetOperations.subtract(r, old); Statement stmt = get1(old); if (subject == null && stmt != null) subject = stmt.subject(); if (predicate == null && stmt != null) predicate = stmt.predicate(); r.add(r.getNodeFactory().createStatement(subject, predicate, object)); return !old.isEmpty(); }
public static List getObjects(Model m, Resource subject, Resource predicate) throws ModelException { List result = new ArrayList(); if (m == null || m.size() == 0) return result; for (Enumeration en = m.find(subject, predicate, null).elements(); en.hasMoreElements(); ) { Statement st = (Statement) en.nextElement(); result.add(st.object()); } return result; }
/** Removes all triples which have something to do with the given namespace */ public static Model removeNamespace(String ns, Model m) throws ModelException { Model res = m.duplicate(); for (Enumeration en = m.duplicate().elements(); en.hasMoreElements(); ) { Statement t = (Statement) en.nextElement(); if (t.subject().toString().startsWith(ns) || t.predicate().toString().startsWith(ns) || t.object().toString().startsWith(ns)) { // System.err.println("REMOVING TRIPLE: " + t); res.remove(t); } } return res; }
// FIXME: what if reification is an endless loop? public static void collectNamespaces(Statement st, Collection target) throws ModelException { if (st.subject() instanceof Statement) collectNamespaces((Statement) st.subject(), target); else collectNamespaces(st.subject(), target); collectNamespaces(st.predicate(), target); if (st.object() instanceof Statement) collectNamespaces((Statement) st.object(), target); else if (st.object() instanceof Resource) collectNamespaces((Resource) st.object(), target); }
public static Statement replaceNamespace( Statement st, String o, String n, NodeFactory f, Map o2n, Set resourcesToIgnore) throws ModelException { boolean replaced = false; Resource subj = st.subject(); Resource pred = st.predicate(); RDFNode obj = st.object(); if (obj instanceof Resource && !(obj instanceof Statement) && o.equals(((Resource) obj).getNamespace()) && (resourcesToIgnore == null || !resourcesToIgnore.contains(obj))) { replaced = true; Resource r = f.createResource(n, ((Resource) obj).getLocalName()); if (o2n != null) o2n.put(obj, r); obj = r; } if (o.equals(subj.getNamespace()) && (resourcesToIgnore == null || !resourcesToIgnore.contains(subj))) { replaced = true; Resource r = f.createResource(n, subj.getLocalName()); if (o2n != null) o2n.put(subj, r); subj = r; } if (o.equals(pred.getNamespace()) && (resourcesToIgnore == null || !resourcesToIgnore.contains(pred))) { replaced = true; Resource r = f.createResource(n, pred.getLocalName()); if (o2n != null) o2n.put(pred, r); pred = r; } return replaced ? f.createStatement(subj, pred, obj) : st; }
public static Statement replaceResources(Statement st, NodeFactory f, Map o2n) throws ModelException { boolean replaced = false; Resource subj = st.subject(); Resource pred = st.predicate(); RDFNode obj = st.object(); Object n = null; if (obj instanceof Statement) { n = obj; obj = replaceResources((Statement) obj, f, o2n); replaced = n != obj; } else if ((n = o2n.get(obj)) != null) { replaced = true; obj = (RDFNode) n; } if (subj instanceof Statement) { n = subj; subj = replaceResources((Statement) subj, f, o2n); replaced = n != subj; } if ((n = o2n.get(subj)) != null) { replaced = true; subj = (Resource) n; } if ((n = o2n.get(pred)) != null) { replaced = true; pred = (Resource) n; } return replaced ? f.createStatement(subj, pred, obj) : st; }
public static void collectResources(Model m, Collection target) throws ModelException { for (Enumeration en = m.elements(); en.hasMoreElements(); ) { Statement st = (Statement) en.nextElement(); if (!(st.object() instanceof Literal) && !(st.object() instanceof Statement)) target.add(st.object()); target.add(st.subject()); target.add(st.predicate()); } }
// returns list of statements protected static void replaceMultSPO( Statement st, NodeFactory f, Map o2n, Collection result, RDFNode toReplace, int position) throws ModelException { Collection replacements; if (toReplace instanceof Statement) { List l = new ArrayList(); replaceMult((Statement) toReplace, f, o2n, l); if (l.size() == 1 && toReplace == l.get(0)) { result.add(st); return; // keep the same } else replacements = l; } else { Object ro = o2n.get(toReplace); if (ro instanceof Collection) replacements = (Collection) ro; else if (ro != null) { replacements = new ArrayList(); replacements.add(ro); } else { // no replacement needed result.add(st); // keep the same statement return; } } for (Iterator it = replacements.iterator(); it.hasNext(); ) { Statement rs = null; Object rr = it.next(); switch (position) { case 0: rs = f.createStatement((Resource) rr, st.predicate(), st.object()); break; case 1: rs = f.createStatement(st.subject(), (Resource) rr, st.object()); break; case 2: rs = f.createStatement(st.subject(), st.predicate(), (RDFNode) rr); break; } result.add(rs); } }
/** * ** Returns true if the specified key attribute exists in the table ** @param altIndexName The * alternate index name, or null to use the primary index ** @param whereKeyType The partial key * match type ** @return True if the specified key attribute exists in the table, false otherwise */ protected boolean _exists(String altIndexName, int whereKeyType) throws SQLException, DBException { /* key fields */ boolean usePrimaryKey = StringTools.isBlank(altIndexName); DBField kfld[] = usePrimaryKey ? this.getKeyFields() : this.getAltKeyFields(altIndexName); if (ListTools.isEmpty(kfld)) { throw new DBException("No keys found!"); } /* check last key for "auto_increment" */ if (whereKeyType == DBWhere.KEY_FULL) { DBField lastField = kfld[kfld.length - 1]; if (lastField.isAutoIncrement() && !this.getFieldValues().hasFieldValue(lastField.getName())) { // full key requested and last key is auto_increment, which is missing return false; } } // DBSelect: SELECT <Keys> FROM <TableName> <KeyWhere> String firstKey = kfld[0].getName(); DBSelect<gDBR> dsel = new DBSelect<gDBR>(this.getFactory()); dsel.setSelectedFields(firstKey); dsel.setWhere(this._getWhereClause(altIndexName, whereKeyType)); /* get keyed record */ DBConnection dbc = null; Statement stmt = null; ResultSet rs = null; boolean exists = false; try { dbc = DBConnection.getDefaultConnection(); stmt = dbc.execute(dsel.toString()); // may throw DBException rs = stmt.getResultSet(); exists = rs.next(); } catch (SQLException sqe) { if (sqe.getErrorCode() == DBFactory.SQLERR_TABLE_NOTLOCKED) { // MySQL: This case has been seen on rare occasions. Not sure what causes it. Print.logError("SQL Lock Error: " + sqe); Print.logError("Hackery! Forcing lock on table: " + this.getTableName()); if (DBProvider.lockTableForRead(this.getTableName(), true)) { // may throw DBException stmt = dbc.execute(dsel.toString()); // may throw SQLException, DBException rs = stmt.getResultSet(); // SQLException exists = rs.next(); // SQLException DBProvider.unlockTables(); // DBException } } else { throw sqe; } } finally { if (rs != null) { try { rs.close(); } catch (Throwable t) { } } if (stmt != null) { try { stmt.close(); } catch (Throwable t) { } } DBConnection.release(dbc); } return exists; }