001: /*
002: * Created on 13.02.2005
003: */
004: package org.ontoware.rdfreactor.runtime;
005:
006: import java.util.Iterator;
007:
008: import org.ontoware.aifbcommons.collection.ClosableIterator;
009: import org.ontoware.rdf2go.exception.ModelRuntimeException;
010: import org.ontoware.rdf2go.model.Model;
011: import org.ontoware.rdf2go.model.QueryRow;
012: import org.ontoware.rdf2go.model.node.BlankNode;
013: import org.ontoware.rdf2go.model.node.Resource;
014: import org.ontoware.rdf2go.model.node.URI;
015: import org.ontoware.rdf2go.model.node.Variable;
016: import org.ontoware.rdf2go.vocabulary.RDF;
017: import org.slf4j.Logger;
018: import org.slf4j.LoggerFactory;
019:
020: /**
021: * Runtime model for a java bean property
022: *
023: * For all getters and setters (and all other accessors): - if the object
024: * implements URIEntity it's mapped to rdf:Resource, - otherwise it's mapped to
025: * rdf:Literal by calling "toString" IMPROVE: better Literal handling?
026: *
027: * <br>
028: * RDF Reactor uses the following naming:
029: *
030: * <b>resource</b> - instance of an RDF schema class, identified by the
031: * resource ID (an URI or BlankNode), allmost all statements about the resource
032: * use the resource ID as the object
033: *
034: * <b>property</b> - a property belongs to a resource, represented by the
035: * predicate of a statement about a resource
036: *
037: * <b>value</b> - value of a property of a resource, represented by the object
038: * of the statement with the property as predicate and the resource ID as the
039: * subject
040: *
041: * @author mvo
042: */
043: public class Bridge extends BridgeBase {
044:
045: @SuppressWarnings("unused")
046: private static Logger log = LoggerFactory.getLogger(Bridge.class);
047:
048: /**
049: * Add a value to the property of an object after checking if the object
050: * already had the property with the same value.
051: *
052: * @param model -
053: * the underlying RDF2Go model
054: * @param o -
055: * URI or BlankNode identifying the resource
056: * @param propertyURI -
057: * URI of the property
058: * @param value -
059: * value of the property
060: * @return true if value was already in the model
061: */
062: public static boolean addValue(Model model, Resource o,
063: URI propertyURI, Object value) throws ModelRuntimeException {
064: assert model != null;
065: assert o != null;
066: assert o instanceof URI || o instanceof BlankNode;
067: assert propertyURI != null;
068: assert value != null;
069:
070: boolean contains = containsGivenValue(model, o, propertyURI,
071: value);
072: add(model, o, propertyURI, value);
073: return contains;
074: }
075:
076: /**
077: * Set the value(s) of a property of an object, after removing all values of
078: * the property from the object
079: *
080: * @param model -
081: * the underlying RDF2Go model
082: * @param resourceObject -
083: * URI or BlankNode identifying the object
084: * @param propertyURI -
085: * URI of the resource
086: * @param value -
087: * value(s) of the property, may be an array
088: * @throws ModelRuntimeException
089: */
090: public static void setValue(Model model, Resource resourceObject,
091: URI propertyURI, Object value) {
092: assert value != null;
093: // remove all current values
094: removeAllValues(model, resourceObject, propertyURI);
095: if (value.getClass().isArray()) {
096: Object[] valuearray = (Object[]) value;
097: for (int i = 0; i < valuearray.length; i++) {
098: addValue(model, resourceObject, propertyURI,
099: valuearray[i]);
100: }
101:
102: } else
103: addValue(model, resourceObject, propertyURI, value);
104: }
105:
106: /**
107: * Set the value of a property from a resource after removing all existing
108: * values of the property from that resource.
109: *
110: * @param model -
111: * the underlying RDF2Go model
112: * @param subject -
113: * URI or BlankNode identifying the resource
114: * @param prop -
115: * URI of the property
116: * @param o -
117: * value of the property
118: * @throws Exception
119: */
120: public static void setAllValue(Model model, Resource subject,
121: URI prop, Object[] o) throws ModelRuntimeException {
122: removeAllValues(model, subject, prop);
123: add(model, subject, prop, o);
124:
125: }
126:
127: /**
128: * Update the value of a property from a resource. (Remove old value, add
129: * new value.)
130: *
131: * @param model -
132: * the underlying RDF2Go model
133: * @param resourceIdentifier -
134: * URI or BlankNode identifying the resource
135: * @param propertyURI -
136: * URI of the property
137: * @param _old -
138: * old value of the property
139: * @param _new -
140: * new value of the property
141: * @return true if the property really had the old value
142: * @throws Exception
143: */
144: public static Boolean updateValue(Model model,
145: Resource resourceIdentifier, URI propertyURI, Object _old,
146: Object _new) throws ModelRuntimeException {
147: Boolean result = new Boolean(removeValue(model,
148: resourceIdentifier, propertyURI, _old));
149: addValue(model, resourceIdentifier, propertyURI, _new);
150: return result;
151: }
152:
153: /**
154: * Update the value of a property from a resource. (Remove old value, add
155: * new value.)
156: *
157: * @param model -
158: * the underlying RDF2Go model
159: * @param resourceIdentifier -
160: * BlankNode identifying the resource
161: * @param propertyURI -
162: * URI of the property
163: * @param _old -
164: * old value of the property
165: * @param _new -
166: * new value of the property
167: * @return true if the property really had the old value
168: * @throws Exception
169: */
170: public static boolean updateValue(Model model, BlankNode blankNode,
171: URI propertyURI, Object _old, Object _new)
172: throws ModelRuntimeException {
173: Boolean result = new Boolean(removeValue(model, blankNode,
174: propertyURI, _old));
175: addValue(model, blankNode, propertyURI, _new);
176: return result;
177: }
178:
179: // /////////////////////
180: // utility functions
181:
182: /**
183: * Check if the model contains an instance of the given RDFS/OWL schema
184: * class. It is assumed that every instance of a class has an accompanying
185: * triple of the form (instanceID, rdf:type, classURI) in the model.
186: *
187: * @param model -
188: * the underlying RDF2Go model
189: * @param classURI -
190: * URI of the RDFS/OWL schema class
191: * @return true if the model contains an instance of the class URI
192: */
193: public static boolean containsInstance(Model model, URI classURI) {
194: try {
195: return (model.contains(Variable.ANY, RDF.type, classURI));
196: } catch (Exception e) {
197: throw new RuntimeException(e);
198: }
199: }
200:
201: /**
202: * Check if a resource is an instance of a given RDFS/OWL class.
203: *
204: * @param model -
205: * the underlying RDF2Go model
206: * @param instanceID -
207: * URI or BlankNode of the instance
208: * @param classURI -
209: * URI of the RDFS/OWL class
210: * @return true if the model contains the triple (instanceID; rdf:type,
211: * classURI)
212: * @throws Exception
213: */
214: public static boolean isInstanceOf(Model model,
215: Resource instanceID, URI classURI)
216: throws ModelRuntimeException {
217: return model.contains(instanceID, RDF.type, classURI);
218: }
219:
220: /**
221: * @return java object, typed as desired by 'returnType'
222: * @throws Exception
223: */
224:
225: /**
226: * Use a SPARQL query on the model and wrap the result in a
227: * SparlSingleVariableIterator, to ensure that only single elements are
228: * included in the result. Assume 'x' as variable name.
229: *
230: * @param m -
231: * the underlying RDF2Go model
232: * @param returnType -
233: * the desired Java return type
234: * @param sparqlSelectQuery -
235: * the SPARQL query string
236: * @return SparqlSingleVariableIterator wrapper around the SPARQL query
237: * result
238: * @throws Exception
239: */
240: @SuppressWarnings("unchecked")
241: public static Iterator<? extends Object> getSparqlSelectSingleVariable(
242: Model m, Class returnType, String sparqlSelectQuery)
243: throws ModelRuntimeException {
244:
245: ClosableIterator<QueryRow> it = m.sparqlSelect(
246: sparqlSelectQuery).iterator();
247:
248: return new ObjectResultIterator(m, new ExtractingIterator(m,
249: it, "x"), returnType);
250:
251: }
252:
253: // public static Statement getStatement(Model model, Resource resource, URI p, Object o) {
254: // return new StatementImpl(model.getContextURI(), resource, p, FORD.toNode(o));
255: // }
256:
257: }
|