001: package org.ontoware.rdfreactor.runtime;
002:
003: import java.lang.reflect.Array;
004: import java.util.ArrayList;
005: import java.util.List;
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.impl.TriplePatternImpl;
012: import org.ontoware.rdf2go.model.node.Node;
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: public class Base {
021:
022: static Logger log = LoggerFactory.getLogger(Base.class);
023:
024: public static void add(Model model, Resource resourceSubject,
025: URI propertyURI, Object value) {
026: assertOpen(model);
027: Resource rdfResource = RDFReactorRuntime
028: .genericResource2RDF2Goresource(model, resourceSubject);
029: Node node = RDFReactorRuntime.java2node(model, value);
030: model.addStatement(rdfResource, propertyURI, node);
031: }
032:
033: public static void add(Model model, Resource resourceSubject,
034: URI propertyURI, Object value, int maxCardinality)
035: throws CardinalityException {
036: assertOpen(model);
037: Resource rdfResource = RDFReactorRuntime
038: .genericResource2RDF2Goresource(model, resourceSubject);
039:
040: long count = countPropertyValues(model, rdfResource,
041: propertyURI);
042: if (count < maxCardinality) {
043: Node node = RDFReactorRuntime.java2node(model, value);
044: model.addStatement(rdfResource, propertyURI, node);
045: } else
046: throw new CardinalityException(
047: "Adding this value would violate maxCardinality = "
048: + maxCardinality + " for property "
049: + propertyURI);
050: }
051:
052: private static void assertOpen(Model model) {
053: if (!model.isOpen()) {
054: throw new RuntimeException("Model is not open");
055: }
056: }
057:
058: public static long countPropertyValues(Model model,
059: Resource resourceSubject, URI propertyURI) {
060: assertOpen(model);
061: ClosableIterator<Statement> it = model.findStatements(
062: resourceSubject, propertyURI, Variable.ANY);
063: long count = 0;
064: while (it.hasNext()) {
065: it.next();
066: count++;
067: }
068: it.close();
069: return count;
070: }
071:
072: public static void createInstance(Model model, URI classURI,
073: Resource resource) {
074: assertOpen(model);
075: model.addStatement(resource, RDF.type, classURI);
076: }
077:
078: /**
079: * @param model
080: * RDF2Go model
081: * @param uri
082: * instance identifier
083: * @return an instance of Restriction or null if none existst
084: * @throws Exception
085: * if Model causes problems
086: */
087: @SuppressWarnings("unchecked")
088: public static <T> T getInstance(Model model, Resource resource,
089: Class<?> returnType) {
090: return (T) RDFReactorRuntime.node2javatype(model, resource,
091: returnType);
092: }
093:
094: /**
095: * Removes rdf:type rdfsClass
096: *
097: * @param model
098: * @param rdfsClass
099: * @param resource
100: */
101: public static void deleteInstance(Model model, URI rdfsClass,
102: Resource resource) {
103: assertOpen(model);
104: Resource rdfResource = RDFReactorRuntime
105: .genericResource2RDF2Goresource(model, resource);
106: model.removeStatement(rdfResource, RDF.type, rdfsClass);
107: }
108:
109: public static Object get(Model model, Resource resourceSubject,
110: URI propertyURI, java.lang.Class<?> returnType)
111: throws RDFDataException, ModelRuntimeException {
112: assertOpen(model);
113: Resource rdfResource = RDFReactorRuntime
114: .genericResource2RDF2Goresource(model, resourceSubject);
115: Node node = ResourceUtils.getSingleValue(model, rdfResource,
116: propertyURI);
117: return RDFReactorRuntime.node2javatype(model, node, returnType);
118: }
119:
120: public static Node get_asNode(Model model,
121: Resource instanceResource, URI propertyURI) {
122: assertOpen(model);
123: Resource rdfResource = RDFReactorRuntime
124: .genericResource2RDF2Goresource(model, instanceResource);
125: ClosableIterator<Node> it = getAll(model, rdfResource,
126: propertyURI, Node.class);
127: if (it.hasNext()) {
128: Node result = it.next();
129: return result;
130: } else {
131: return null;
132: }
133: }
134:
135: public static <T> ClosableIterator<T> getAll(Model model,
136: Resource resourceSubject, URI propertyURI,
137: Class<T> returnType) {
138: assertOpen(model);
139: Resource rdfResource = RDFReactorRuntime
140: .genericResource2RDF2Goresource(model, resourceSubject);
141: ClosableIterator<Statement> it = model.findStatements(
142: rdfResource, propertyURI, Variable.ANY);
143: return new ConvertingClosableIterator<T>(
144: new ProjectingIterator<Node>(it,
145: ProjectingIterator.projection.Object), model,
146: returnType);
147: }
148:
149: public static <T> ReactorResult<T> getAll_as(Model model,
150: Resource resourceSubject, URI propertyURI,
151: Class<T> returnType) {
152: assertOpen(model);
153: Resource rdfResource = RDFReactorRuntime
154: .genericResource2RDF2Goresource(model, resourceSubject);
155: return new ReactorResult<T>(model, new TriplePatternImpl(
156: rdfResource, propertyURI, Variable.ANY,
157: TriplePatternImpl.SPO.OBJECT), returnType);
158: }
159:
160: /**
161: * Convenience method for ClosableIterator<T> getAll.
162: *
163: * @param <T>
164: * @param model
165: * @param resourceSubject
166: * @param propertyURI
167: * @param returnType
168: * @return
169: */
170: public static <T> List<T> getAll_asList(Model model,
171: Resource resourceSubject, URI propertyURI,
172: Class<T> returnType) {
173: assertOpen(model);
174: Resource rdfResource = RDFReactorRuntime
175: .genericResource2RDF2Goresource(model, resourceSubject);
176: ClosableIterator<T> it = getAll(model, rdfResource,
177: propertyURI, returnType);
178: return asList(it);
179: }
180:
181: public static <T> T[] getAll_asArray(Model model,
182: Resource resourceSubject, URI propertyURI,
183: Class<T> returnType) {
184: assertOpen(model);
185: Resource rdfResource = RDFReactorRuntime
186: .genericResource2RDF2Goresource(model, resourceSubject);
187: ClosableIterator<T> it = getAll(model, rdfResource,
188: propertyURI, returnType);
189: return asArray(it, returnType);
190: }
191:
192: public static ClosableIterator<Node> getAll_asNode(Model model,
193: Resource resourceSubject, URI propertyURI) {
194: assertOpen(model);
195: Resource rdfResource = RDFReactorRuntime
196: .genericResource2RDF2Goresource(model, resourceSubject);
197: ClosableIterator<Statement> it = model.findStatements(
198: rdfResource, propertyURI, Variable.ANY);
199: return new ProjectingIterator<Node>(it,
200: ProjectingIterator.projection.Object);
201: }
202:
203: public static List<Node> getAll_asNodeList(Model model,
204: Resource resourceSubject, URI propertyURI) {
205: assertOpen(model);
206: Resource rdfResource = RDFReactorRuntime
207: .genericResource2RDF2Goresource(model, resourceSubject);
208: ClosableIterator<Node> it = getAll_asNode(model, rdfResource,
209: propertyURI);
210: return asList(it);
211: }
212:
213: public static ClosableIterator<Resource> getAll_Inverse(
214: Model model, URI propertyURI, Object value) {
215: assertOpen(model);
216: Node node = RDFReactorRuntime.java2node(model, value);
217: ClosableIterator<Statement> it = model.findStatements(
218: Variable.ANY, propertyURI, node);
219: return new ProjectingIterator<Resource>(it,
220: ProjectingIterator.projection.Subject);
221: }
222:
223: public static <T> ReactorResult<T> getAll_Inverse_as(Model model,
224: URI propertyURI, Object value, Class<T> returnType) {
225: assertOpen(model);
226: Node node = RDFReactorRuntime.java2node(model, value);
227: return new ReactorResult<T>(model, new TriplePatternImpl(
228: Variable.ANY, propertyURI, node,
229: TriplePatternImpl.SPO.SUBJECT), returnType);
230: }
231:
232: // public static <T> ClosableIterator<T> getAllAs(Model model,
233: // Resource instanceResource, URI propertyURI, Class<?> returnType) {
234: // assertOpen(model);
235: // ClosableIterator<Statement> it = model.findStatements(instanceResource,
236: // propertyURI, Variable.ANY);
237: // return new ProjectingIterator<T>(it,
238: // ProjectingIterator.projection.Object);
239: // }
240:
241: /**
242: * Return all instances of the given class.
243: *
244: * @param model -
245: * underlying RDF2Go model
246: * @param classURI -
247: * URI of the (RDFS/OWL) class.
248: * @return all instances in the model
249: */
250: public static <T> ClosableIterator<T> getAllInstances(Model model,
251: URI classURI, Class<T> returnType) {
252: assertOpen(model);
253: ClosableIterator<Statement> it = model.findStatements(
254: Variable.ANY, RDF.type, classURI);
255: return new ConvertingClosableIterator<T>(
256: new ProjectingIterator<Node>(it,
257: ProjectingIterator.projection.Subject), model,
258: returnType);
259: }
260:
261: public static <T> java.util.List<T> getAllInstances_asList(
262: Model model, URI classURI, Class<T> returnType) {
263: assertOpen(model);
264: ClosableIterator<T> it = getAllInstances(model, classURI,
265: returnType);
266: return asList(it);
267: }
268:
269: public static <T> T[] getAllInstances_asArray(Model model,
270: URI classURI, Class<T> returnType) {
271: assertOpen(model);
272: ClosableIterator<T> it = getAllInstances(model, classURI,
273: returnType);
274: return asArray(it, returnType);
275: }
276:
277: public static <T> ReactorResult<T> getAllInstances_as(Model model,
278: URI classURI, Class<T> returnType) {
279: assertOpen(model);
280: return new ReactorResult<T>(model, new TriplePatternImpl(
281: Variable.ANY, RDF.type, classURI,
282: TriplePatternImpl.SPO.SUBJECT), returnType);
283:
284: }
285:
286: public static Resource getInverse(Model model, URI propertyURI,
287: Object value) {
288: assertOpen(model);
289: Node valueNode = RDFReactorRuntime.java2node(model, value);
290: ClosableIterator<Statement> it = model.findStatements(
291: Variable.ANY, propertyURI, valueNode);
292: Resource result = null;
293: if (it.hasNext()) {
294: result = it.next().getSubject();
295: }
296: if (it.hasNext()) {
297: throw new RDFDataException(
298: "Found more than one inverse of " + propertyURI
299: + " i.e. mroe than one match for (*,"
300: + propertyURI + "," + valueNode);
301: }
302: it.close();
303: return result;
304: }
305:
306: public static boolean hasInstance(Model model, Resource classURI,
307: Resource resourceSubject) {
308: assertOpen(model);
309: Resource rdfResource = RDFReactorRuntime
310: .genericResource2RDF2Goresource(model, resourceSubject);
311: return model.contains(rdfResource, RDF.type, classURI);
312: }
313:
314: public static boolean hasValue(Model model,
315: Resource resourceSubject, URI propertyURI) {
316: assertOpen(model);
317: Resource rdfResource = RDFReactorRuntime
318: .genericResource2RDF2Goresource(model, resourceSubject);
319: return model.contains(rdfResource, propertyURI, Variable.ANY);
320: }
321:
322: public static boolean hasValue(Model model,
323: Resource resourceSubject, URI propertyURI, Object value) {
324: assertOpen(model);
325: Resource rdfResource = RDFReactorRuntime
326: .genericResource2RDF2Goresource(model, resourceSubject);
327: Node node = RDFReactorRuntime.java2node(model, value);
328: return model.contains(rdfResource, propertyURI, node);
329: }
330:
331: public static void remove(Model model, Resource resourceSubject,
332: URI propertyURI, Object value) {
333: assertOpen(model);
334: Resource rdfResource = RDFReactorRuntime
335: .genericResource2RDF2Goresource(model, resourceSubject);
336: Node node = RDFReactorRuntime.java2node(model, value);
337: model.removeStatement(rdfResource, propertyURI, node);
338:
339: // FIXME this was old an buggy, fix all similar issues
340: // assertOpen(model);
341: // Node node = RDFReactorRuntime.java2node(model, value);
342: // model.removeStatement(resourceSubject, propertyURI, node);
343: }
344:
345: public static void remove(Model model, Resource resourceSubject,
346: URI propertyURI, Object value, int minCardinality)
347: throws CardinalityException {
348: assertOpen(model);
349: Resource rdfResource = RDFReactorRuntime
350: .genericResource2RDF2Goresource(model, resourceSubject);
351: long count = countPropertyValues(model, rdfResource,
352: propertyURI);
353: if (count > minCardinality)
354: remove(model, rdfResource, propertyURI, value);
355: else
356: throw new CardinalityException("Must have at least "
357: + minCardinality + " values for property "
358: + propertyURI);
359: }
360:
361: public static void removeAll(Model model, Resource resourceSubject,
362: URI propertyURI) {
363: assertOpen(model);
364: synchronized (model) {
365: removeAll_unsynchronized(model, resourceSubject,
366: propertyURI);
367: }
368: }
369:
370: private static void removeAll_unsynchronized(Model model,
371: Resource resourceSubject, URI propertyURI) {
372: assertOpen(model);
373: Resource rdfResource = RDFReactorRuntime
374: .genericResource2RDF2Goresource(model, resourceSubject);
375: model.removeStatements(rdfResource, propertyURI, Variable.ANY);
376: }
377:
378: public static void set(Model model, Resource resourceSubject,
379: URI propertyURI, Object object) {
380: assertOpen(model);
381: synchronized (model) {
382: Resource rdfResource = RDFReactorRuntime
383: .genericResource2RDF2Goresource(model,
384: resourceSubject);
385: removeAll_unsynchronized(model, rdfResource, propertyURI);
386: Node node = RDFReactorRuntime.java2node(model, object);
387: model.addStatement(rdfResource, propertyURI, node);
388: }
389: }
390:
391: public static <T> List<T> asList(ClosableIterator<T> it) {
392: ArrayList<T> list = new ArrayList<T>();
393: while (it.hasNext()) {
394: list.add(it.next());
395: }
396: it.close();
397: return list;
398: }
399:
400: @SuppressWarnings("unchecked")
401: public static <T> T[] asArray(ClosableIterator<T> it,
402: Class<T> returnType) {
403: Object[] resultAsArray = (Object[]) Array.newInstance(
404: returnType, 0);
405: return (T[]) asList(it).toArray(resultAsArray);
406: }
407:
408: /**
409: * Cast .this object to the given target Java type.
410: *
411: * @param targetType -
412: * Java type to which to cast this object
413: * @return converted object
414: */
415: public static Object castTo(Model model, Resource resource,
416: Class<?> targetType) {
417: return RDFReactorRuntime.node2javatype(model, resource,
418: targetType);
419: }
420:
421: public static boolean has(Model model, Resource resourceSubject,
422: URI propertyURI) {
423: Resource rdfResource = RDFReactorRuntime
424: .genericResource2RDF2Goresource(model, resourceSubject);
425: ClosableIterator<Statement> it = model.findStatements(
426: rdfResource, propertyURI, Variable.ANY);
427: boolean result = it.hasNext();
428: it.close();
429: return result;
430: }
431:
432: /**
433: * Delete all (this, *, *)
434: *
435: * @param model
436: * @param instanceResource
437: */
438: public static void deleteAllProperties(Model model,
439: Resource instanceResource) {
440: model.removeStatements(instanceResource, Variable.ANY,
441: Variable.ANY);
442: }
443:
444: }
|