public static Resource getSubject(Model r, Resource predicate, RDFNode object) throws ModelException { Model m = r.find(null, predicate, object); if (m == null || m.size() == 0) return null; // if(m.size() > 1) // throw new RuntimeException("Model contains more than one triple"); return ((Statement) m.elements().nextElement()).subject(); }
public static RDFNode getObject(Model r, Resource subject, Resource predicate) throws ModelException { Model m = r.find(subject, predicate, null); if (m == null || m.size() == 0) return null; // if(m.size() > 1) // throw new RuntimeException("Model contains more than one triple"); // FIXME: we do not check whether it is a resource or literal return ((Statement) m.elements().nextElement()).object(); }
// 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 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; }
public static boolean isInstanceOf(Model r, Resource i, Resource cls) throws ModelException { return !r.find(i, RDF.type, cls).isEmpty(); }