001: package org.ontoware.rdfreactor.runtime;
002:
003: import java.util.HashSet;
004: import java.util.Iterator;
005: import java.util.Set;
006:
007: import org.ontoware.aifbcommons.collection.ClosableIterator;
008: import org.ontoware.rdf2go.exception.ModelRuntimeException;
009: import org.ontoware.rdf2go.model.Model;
010: import org.ontoware.rdf2go.model.Statement;
011: import org.ontoware.rdf2go.model.node.Node;
012: import org.ontoware.rdf2go.model.node.Resource;
013: import org.ontoware.rdf2go.model.node.URI;
014: import org.ontoware.rdf2go.model.node.Variable;
015: import org.slf4j.Logger;
016: import org.slf4j.LoggerFactory;
017:
018: public class ResourceUtils {
019:
020: private static Logger log = LoggerFactory
021: .getLogger(ResourceUtils.class);
022:
023: /**
024: * Check if the resource identified by resourceID, has a property
025: * identidified by propertyURI which has at least one value.
026: *
027: * @param model -
028: * the underlying RDF2Go model
029: * @param resource -
030: * must be an URI or a BlankNode
031: * @param propertyURI -
032: * URI of the property
033: * @return true if the property has at least one value defined
034: * @throws Exception
035: */
036: public static boolean containsAnyValue(Model model,
037: Resource resource, URI propertyURI)
038: throws ModelRuntimeException {
039:
040: synchronized (model) {
041: ClosableIterator<? extends Statement> it = model
042: .findStatements(resource, propertyURI, Variable.ANY);
043: boolean result = it.hasNext();
044: it.close();
045: return result;
046: }
047: }
048:
049: /**
050: * Delete a resource from the model, meaning: delete all (resource, *, *)
051: * triples from the model
052: *
053: * @param model -
054: * the underlying RDF2Go model
055: * @param resource -
056: * URI or BlankNode of the resource
057: * @throws Exception
058: */
059: @Patrolled
060: public static void delete(Model model, Resource resource)
061: throws ModelRuntimeException {
062: // delete triple (this.uri, ANY, ANY )
063: synchronized (model) {
064: ClosableIterator<? extends Statement> it = model
065: .findStatements(resource, Variable.ANY,
066: Variable.ANY);
067: Set<Statement> temp = new HashSet<Statement>();
068: while (it.hasNext()) {
069: temp.add(it.next());
070: }
071: it.close();
072: // now, delete
073: Iterator<Statement> tempIterator = temp.iterator();
074: while (tempIterator.hasNext()) {
075: model.removeStatement(tempIterator.next());
076: }
077: }
078: }
079:
080: /**
081: * @param model
082: * @param subject
083: * @param propertyURI
084: * @return the single value of the given property, or null
085: * @throws RDFDataException if multiple values are found
086: */
087: public static Node getSingleValue(Model model, Resource subject,
088: URI propertyURI) {
089: log.debug("looking for ( <" + subject + "> <" + propertyURI
090: + "> *)");
091:
092: Node result = null;
093: synchronized (model) {
094: // get value
095: ClosableIterator<? extends Statement> it;
096: it = model.findStatements(subject, propertyURI,
097: Variable.ANY);
098: if (it.hasNext()) {
099: Statement o = it.next();
100: if (it.hasNext()) {
101: log
102: .warn("Found more than one value. Listing values: ");
103: log.warn("Found " + o.getObject());
104: while (it.hasNext()) {
105: log.warn("Found " + it.next().getObject());
106: }
107: throw new RDFDataException(
108: "Found more than one value for property "
109: + propertyURI + " and resource "
110: + subject
111: + " but you asked for *the* value.");
112: }
113: result = o.getObject();
114: } else {
115: log.debug("no matching nodes found");
116: }
117: it.close();
118: }
119: return result;
120: }
121:
122: }
|