001: package org.ontoware.rdfreactor.runtime;
002:
003: import java.util.Map;
004: import java.util.WeakHashMap;
005:
006: import org.ontoware.rdf2go.exception.ModelRuntimeException;
007: import org.ontoware.rdf2go.model.Model;
008: import org.ontoware.rdf2go.model.node.DatatypeLiteral;
009: import org.ontoware.rdf2go.model.node.LanguageTagLiteral;
010: import org.ontoware.rdf2go.model.node.Node;
011: import org.ontoware.rdf2go.model.node.Resource;
012: import org.ontoware.rdf2go.model.node.URI;
013: import org.ontoware.rdf2go.model.node.Variable;
014: import org.ontoware.rdf2go.model.node.impl.URIImpl;
015: import org.ontoware.rdf2go.vocabulary.RDF;
016:
017: /**
018: *
019: * Java Object <---> Node, Resource
020: *
021: * TODO: test weak references
022: *
023: * @author voelkel
024: *
025: */
026: public class FORD {
027:
028: private static WeakHashMap<Resource, Object> r2o = new WeakHashMap<Resource, Object>();
029:
030: private static WeakHashMap<Object, Resource> o2r = new WeakHashMap<Object, Resource>();
031:
032: private static WeakHashMap<Object, Model> o2m = new WeakHashMap<Object, Model>();
033:
034: public static Object create(Model model, Class returnType,
035: Resource name, boolean write) {
036:
037: try {
038: Object o = returnType.newInstance();
039:
040: // check for RDF annotation
041: if (write) {
042: try {
043: URI rdfsClassURI = getRDFasURI(o);
044: model.addStatement(name, RDF.type, rdfsClassURI);
045: // now forget the annotation and never us it again?
046: } catch (ModelRuntimeException e) {
047: throw new RuntimeException(e);
048: }
049: }
050: r2o.put(name, o);
051: o2r.put(o, name);
052: o2m.put(o, model);
053: return o;
054: } catch (InstantiationException e) {
055: // TODO Auto-generated catch block
056: throw new RuntimeException(e);
057: } catch (IllegalAccessException e) {
058: // TODO Auto-generated catch block
059: throw new RuntimeException(e);
060: }
061:
062: }
063:
064: public static URI getRDFasURI(Object o)
065: throws ModelRuntimeException {
066: String annotation = o.getClass().getAnnotation(
067: org.ontoware.rdfreactor.annotation.RDF.class).value();
068: return new URIImpl(annotation);
069: }
070:
071: public static Object get(Object obj, URI p, Class returnType) {
072: try {
073: return Bridge.getValue(o2m.get(obj), o2r.get(obj), p,
074: returnType);
075: } catch (RDFDataException e) {
076: // TODO Auto-generated catch block
077: throw new RuntimeException(e);
078: } catch (ModelRuntimeException e) {
079: // TODO Auto-generated catch block
080: throw new RuntimeException(e);
081: }
082: }
083:
084: public static void set(Object obj, URI p, Object value) {
085: Bridge.setValue(o2m.get(obj), o2r.get(obj), p, toNode(value));
086: }
087:
088: public static void add(Object obj, URI p, Object value) {
089: // TODO Auto-generated method stub
090:
091: }
092:
093: public static Node toNode(Object o) {
094: return null;
095: // IMPROVE
096: }
097:
098: public static void remove(Object obj, URI p, Object value) {
099: // TODO Auto-generated method stub
100:
101: }
102:
103: public static Object[] getAll(Object obj, URI p, Class<?> returnType) {
104: // TODO Auto-generated method stub
105: return null;
106: }
107:
108: public static Object[] getAllInstances(Object obj,
109: Class<?> returnType) {
110: // TODO use annot
111: return null;
112: }
113:
114: public static Map<URI, Object> asMap(Object obj) {
115: return new ReactorMap(o2m.get(obj), o2r.get(obj));
116: }
117:
118: public static boolean removeAll(Object obj, URI p) {
119: Resource r = o2r.get(obj);
120: Model m = o2m.get(r);
121:
122: try {
123: return Bridge.removeAllValues(m, r, p);
124: } catch (Exception e) {
125: throw new RuntimeException(e);
126: }
127:
128: }
129:
130: /**
131: * @param model
132: * RDF2Go model
133: * @return true if any statement (this,*,*) is contained in given model
134: */
135: public static boolean in(Object obj, Model model) {
136: try {
137: return model.contains(o2r.get(obj), Variable.ANY,
138: Variable.ANY);
139: } catch (Exception e) {
140: throw new RuntimeException(e);
141: }
142: }
143:
144: public static Model getModel(Object obj) {
145: return o2m.get(obj);
146: }
147:
148: /**
149: * remove all (this, rdf:type, ANY) statements
150: */
151: public void delete(Object obj) {
152: try {
153: ResourceUtils.delete(o2m.get(obj), o2r.get(obj));
154: } catch (Exception e) {
155: throw new RuntimeException(e);
156: }
157: }
158:
159: // /**
160: // * @param prop
161: // * property URI
162: // * @param o
163: // * @return a statement (this, uri, object) and thus bridges the gap between
164: // * the OO and RDF worlds.
165: // */
166: // public Statement getStatement(Object obj, URI p, Object o) {
167: // return Bridge.getStatement(o2m.get(obj), o2r.get(obj), p, o);
168: // }
169:
170: public boolean equals(Object obj, Object other) {
171:
172: if (obj instanceof ResourceEntity) {
173: ResourceEntity resourceEntity = (ResourceEntity) obj;
174: if (other instanceof URI)
175: return resourceEntity.getResource().equals(other);
176: else if (other instanceof ResourceEntity)
177: return resourceEntity.getResource().equals(
178: ((ResourceEntity) other).getResource());
179: else
180: return false;
181: } else if (obj instanceof URI) {
182: // return true if o has same uri
183: if (other instanceof ResourceEntity) {
184: return ((ResourceEntity) other).getResource().equals(
185: obj);
186: } else if (other instanceof URI) {
187: return obj.equals(other);
188: } else
189: return false;
190: } else if (obj instanceof String) {
191: // return true if o has same value
192: return obj.toString().equals(other.toString());
193: } else if (obj instanceof LanguageTagLiteral
194: || obj instanceof DatatypeLiteral) {
195: // IMPROVE: better literal handling
196: return obj.equals(other);
197: } else
198: // o is never == blank node or variable
199: return false;
200: }
201:
202: }
|