001: /*
002: * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package org.mandarax.rdf;
020:
021: import java.util.*;
022: import com.hp.hpl.jena.rdf.model.Container;
023: import com.hp.hpl.jena.rdf.model.RDFNode;
024: import com.hp.hpl.jena.rdf.model.StmtIterator;
025: import com.hp.hpl.jena.rdf.model.Statement;
026: import com.hp.hpl.jena.rdf.model.Resource;
027: import com.hp.hpl.jena.rdf.model.Property;
028: import org.mandarax.kernel.ClauseSetException;
029:
030: /**
031: * RDFContainer is a wrapper for RDF Containers (BAG,ALT,SEQ).
032: * @author <A HREF="mailto:paschke@in.tum.de">Adrian Paschke</A> <A HREF="mailto:j.b.dietrich@massey.ac.nz">Jens Dietrich</A>
033: * @version 1.1 <01 August 2004>
034: * @since 0.1
035: */
036: public class RDFContainer implements Collection, RDFLogger {
037:
038: //Constants to map RDFContainers on Java Collection List or Set
039: public static final int SEQ = 0;
040: public static final int ALT = 1;
041: public static final int BAG = 2;
042: private int type = SEQ; // RDF container type can be sequence, alternative or bag
043: private Container delegate = null;
044:
045: /**
046: * Default constructor.
047: * @param c the wrapped jena container
048: */
049: public RDFContainer(Container c) throws ClauseSetException {
050: super ();
051: delegate = c;
052: if (c.isAlt())
053: type = ALT;
054: else if (c.isBag())
055: type = BAG;
056: else if (c.isSeq())
057: type = SEQ;
058: else
059: throw new ClauseSetException(
060: "Unknown RDF container type encountered");
061: }
062:
063: /**
064: * Returns an iterator over the elements in the internal collection.
065: * @return an Iterator over the elements in the internal collection.
066: */
067: public Iterator iterator() {
068: return delegate.iterator();
069: }
070:
071: /**
072: * Returns the number of elements in the internal collection.
073: * @return the number of elements in the internal collection.
074: */
075: public int size() {
076: return delegate.size();
077: }
078:
079: /**
080: * Adds the specified element to the collection.
081: * Not supported, this is a "read only" view on a container.
082: * @param o the element to be added to the collection.
083: * @return true if the collection changed as a result of the call.
084: */
085: public boolean add(Object o) {
086: throw new UnsupportedOperationException(
087: "This is an unmodifiable collection");
088: }
089:
090: /**
091: * Adds all of the elements in the specified collection to this collection.
092: * @param col the elements to be inserted into the collection.
093: * @return true if the collection changed as a result of the call.
094: */
095: public boolean addAll(Collection col) {
096: throw new UnsupportedOperationException(
097: "This is an unmodifiable collection");
098: }
099:
100: /**
101: * Retains only the elements in the collection that are contained in the specified collection.
102: * @param col the elements to be retained in the collection.
103: * @return true if the collection changed as a result of the call.
104: */
105: public boolean retainAll(Collection col) {
106: throw new UnsupportedOperationException(
107: "This is an unmodifiable collection");
108: }
109:
110: /**
111: * Removes all the collection's elements that are also contained in the specified collection.
112: * @param col the elements to be removed from this collection.
113: * @return true if the collection changed as a result of the call.
114: */
115: public boolean removeAll(Collection col) {
116: throw new UnsupportedOperationException(
117: "This is an unmodifiable collection");
118: }
119:
120: /**
121: * Returns true if this collection contains all of the elements in the specified collection.
122: * Not yet supported (but could be done), would enforce initializing the entire collections.
123: * @param col the collection to be checked for containment in the internal collection.
124: * @return true if this collection contains all of the elements in the specified collection.
125: */
126: public boolean containsAll(Collection col) {
127: throw new UnsupportedOperationException(
128: "This operation is not supported");
129: }
130:
131: /**
132: * Returns true if this collection contains the specified element.
133: * @param o the element whose presence in this collection is to be tested.
134: * @return true if this collection contains the specified element.
135: */
136: public boolean contains(Object o) {
137: return delegate.contains((RDFNode) o);
138: }
139:
140: /**
141: * Removes all of the elements from the internal collection.
142: * This collection will be empty after this method returns unless it throws an exception.
143: */
144: public void clear() {
145: throw new UnsupportedOperationException(
146: "This is an unmodifiable collection");
147: }
148:
149: /**
150: * Overwrite the equals method of the Collection Interface.
151: * @param o is the object the new method is invoked from.
152: * @return the result of dispatching the new method represented by RDFContainer's method field on object o with parameters equal_value.
153: * @throws Exception if the new method can not be invoked correctly.
154: */
155: public boolean equals(Object o) {
156:
157: if (!super .equals(o)) {
158:
159: if (o instanceof RDFContainer) {
160:
161: // test if the rdf subject and the rdf predicat in both container models are equal
162: for (StmtIterator sti1 = delegate.getModel()
163: .listStatements((Resource) null,
164: (Property) null, (RDFNode) delegate); sti1
165: .hasNext();)
166: for (StmtIterator sti2 = ((RDFContainer) o).delegate
167: .getModel()
168: .listStatements(
169: (Resource) null,
170: (Property) null,
171: (RDFNode) ((RDFContainer) o).delegate); sti2
172: .hasNext();) {
173: Statement st1 = sti1.nextStatement();
174: Statement st2 = sti2.nextStatement();
175: if ((!st1.getSubject().equals(st2.getSubject()))
176: || (!st1.getPredicate().equals(
177: st2.getPredicate())))
178: return false;
179: }
180: // test if both container contents are equal
181: for (Iterator it = delegate.iterator(); it.hasNext();) {
182: if (!((RDFContainer) o).delegate
183: .contains((RDFNode) it.next()))
184: return false;
185: }
186: return true;
187:
188: //return delegate.equals(((RDFContainer)o).delegate);
189: } else
190: return false;
191:
192: } else
193: return true;
194:
195: }
196:
197: /**
198: * Get the hash code for an object.
199: * @return the hash code
200: */
201: public int hashCode() {
202: return delegate == null ? 0 : delegate.hashCode();
203: }
204:
205: /**
206: * Gets the RDFContainer type. One of the public constants.
207: * @return the RDFContainer type.
208: */
209: public int getType() {
210: return type;
211: }
212:
213: /**
214: * Returns true if the RDFContainer type is BAG.
215: * @return true if the RDFContainer type is BAG.
216: */
217: public boolean isBag() {
218: return type == BAG;
219: }
220:
221: /**
222: * Returns true if the RDFContainer type is ALT.
223: * @return true if the RDFContainer type is ALT.
224: */
225: public boolean isAlt() {
226: return type == ALT;
227: }
228:
229: /**
230: * Returns true if the RDFContainer type is SEQ.
231: * @return true if the RDFContainer type is SEQ.
232: */
233: public boolean isSeq() {
234: return type == SEQ;
235: }
236:
237: /**
238: * Returns an array containing all of the elements in the collection.
239: * Not yet supported, would enforge reading the entire collection.
240: * @return an array containing all of the elements in this collection.
241: */
242: public Object[] toArray() {
243: throw new UnsupportedOperationException(
244: "This operation is not supported");
245: }
246:
247: /**
248: * Returns true if this collection contains no elements.
249: * @return true if this collection contains no elements.
250: */
251: public boolean isEmpty() {
252: return delegate.size() == 0;
253: }
254:
255: /**
256: * Removes a single instance of the specified element from the collection.
257: * @param o the element to be removed from this collection, if present.
258: * @return true if this collection changed as a result of the call.
259: */
260: public boolean remove(Object o) {
261: throw new UnsupportedOperationException(
262: "This is an unmodifiable collection");
263: }
264:
265: /**
266: * Returns an array containing all of the elements in this collection;
267: * the runtime type of the returned array is that of the specified array.
268: * @param a the array into which the elements of this collection are to be stored, if it is big enough;
269: * otherwise, a new array of the same runtime type is allocated for this purpose.
270: * @return an array containing the elements of this collection.
271: */
272: public Object[] toArray(Object[] a) {
273: throw new UnsupportedOperationException(
274: "This operation is not supported");
275: }
276:
277: }
|