001: package org.ontoware.rdfreactor.runtime;
002:
003: import org.ontoware.rdf2go.model.Model;
004: import org.ontoware.rdf2go.model.node.BlankNode;
005: import org.ontoware.rdf2go.model.node.DatatypeLiteral;
006: import org.ontoware.rdf2go.model.node.LanguageTagLiteral;
007: import org.ontoware.rdf2go.model.node.Literal;
008: import org.ontoware.rdf2go.model.node.Node;
009: import org.ontoware.rdf2go.model.node.Resource;
010: import org.ontoware.rdf2go.model.node.URI;
011: import org.ontoware.rdf2go.vocabulary.RDF;
012: import org.slf4j.Logger;
013: import org.slf4j.LoggerFactory;
014:
015: /**
016: * <b>ReactorBaseImpl</b> is the base class for instances of classes from the
017: * RDFS/OWL schema generated by the code generator. <br>
018: *
019: * If a class inheriting from ReactorBaseImpl is instantiated, it represents an
020: * instance of a class from an RDFS/OWL schema. The properties of this class
021: * instance are stored in an RDF model using RDF2Go (via the static Bridge
022: * object) as triples of the form: (this.instanceIdentifier, key, value). <br>
023: *
024: * Besides methods for putting, getting and removing properties, the Map<URI,
025: * Object> Interface is also supported for handling of all properties of this
026: * instance in one Map, backed by the actual RDF2Go model.
027: *
028: * <br>
029: * RDF Reactor uses the following naming:
030: *
031: * <b>resource</b> - instance of an RDF schema class, identified by the
032: * resource ID (an URI or BlankNode), almost all statements about the resource
033: * use the resource ID as the object
034: *
035: * <b>property</b> - a property belongs to a resource, represented by the
036: * predicate of a statement about a resource
037: *
038: * <b>value</b> - value of a property of a resource, represented by the object
039: * of the statement with the property as predicate and the resource ID as the
040: * subject
041: *
042: * @author $Author: xamde $
043: * @version $Id: ReactorBaseImpl.java,v 1.24 2006/12/05 19:47:28 xamde Exp $
044: *
045: *
046: */
047: public class ReactorRuntimeEntity implements Resource {
048:
049: private static Logger log = LoggerFactory
050: .getLogger(ReactorRuntimeEntity.class);
051:
052: /**
053: * the underlying RDF2Go model in which the triples representing the
054: * properties of this object are saved
055: */
056: protected Model model;
057:
058: /**
059: * the URI of the RDFS class from which this object is an instance
060: */
061: protected URI classURI;
062:
063: /**
064: * the identifier of this instance is a URI or a BlankNode. It is used as
065: * the Subject of all triples representing this instance in the RDF model.
066: */
067: private Resource instanceIdentifier;
068:
069: /**
070: * Constructor: create a ReactorBaseImpl for the RDFS/OWL schema class
071: * identified by classURI, with instanceIdentifier as the identifing URL or
072: * BlankNode.
073: *
074: * @param model,
075: * the underlying RDF2Go model
076: * @param classURI,
077: * URI of the RDFS/OWL class from which this object is an
078: * instance
079: * @param instanceIdentifier,
080: * has to be an URI or URL or BlankNode
081: * @param write
082: * if true, the triple (this, rdf:type, classURI) is written to
083: * the model (in addition to any other triples denoting
084: * properties of the instance)
085: */
086: public ReactorRuntimeEntity(Model model, URI classURI,
087: Resource instanceIdentifier, boolean write) {
088: this .model = model;
089: this .classURI = classURI;
090: this .instanceIdentifier = (Resource) instanceIdentifier;
091:
092: // this can lead to concurrenty exceptions when used in
093: // iterators on the model
094: if (write) {
095: try {
096: // add type information only if not present
097: if (!model.contains(this .instanceIdentifier, RDF.type,
098: classURI)) {
099: log.debug("adding type information: "
100: + this .instanceIdentifier + " a "
101: + classURI);
102: Base.add(model, instanceIdentifier, RDF.type,
103: classURI);
104:
105: }
106: } catch (Exception e) {
107: throw new RuntimeException(e);
108: }
109: }
110: }
111:
112: /**
113: * Constructor: create a ReactorBaseImpl for the RDFS/OWL schema class
114: * identified by classURI, with instanceIdentifier as the identifing URL or
115: * BlankNode. Don't write (this, rdf:type, classURI) for this instance to the
116: * model.
117: *
118: * @param model,
119: * the underlying RDF2Go model
120: * @param classURI,
121: * URI of the RDFS/OWL class from which this object is an
122: * instance
123: * @param instanceIdentifier,
124: * has to be an URI or URL or BlankNode
125: */
126: public ReactorRuntimeEntity(Model model, URI classURI,
127: Resource instanceIdentifier) {
128: // FIXME: default true or false?
129: this (model, classURI, instanceIdentifier, false);
130: }
131:
132: /**
133: * implements
134: *
135: * @see ReactorEntity
136: */
137: public Resource getResource() {
138: return this .instanceIdentifier;
139: }
140:
141: public Model getModel() {
142: return this .model;
143: }
144:
145: /**
146: * @return the URI of the RDFS schema class of which this object is an
147: * instance
148: */
149: public URI getRDFSClassURI() {
150: return this .classURI;
151: }
152:
153: // //////////////////////////////////////
154: // override some java.lang.Object methods
155:
156: /**
157: * implement
158: *
159: * @see Object methods
160: */
161: public boolean equals(Object other) {
162:
163: if (other instanceof ReactorRuntimeEntity) {
164: return ((ReactorRuntimeEntity) other).getResource().equals(
165: this .getResource());
166: } else if (other instanceof URI) {
167: return this .getResource().equals(other);
168: } else
169: return false;
170: }
171:
172: /**
173: * implement
174: *
175: * @see Object methods
176: */
177: public int hashCode() {
178: return this .instanceIdentifier.hashCode();
179: }
180:
181: /**
182: * implement
183: *
184: * @see Object methods
185: * @return a string representation of the instance identifier (URI or blank
186: * node). Representations are dependant on the used RDF2Go adaptor.
187: */
188: public String toString() {
189: return this .instanceIdentifier.toString();
190: }
191:
192: /**
193: * Cast .this object to the given target Java type.
194: *
195: * @param targetType -
196: * Java type to which to cast this object
197: * @return converted object
198: */
199: public Object castTo(Class<?> targetType) {
200: return Base.castTo(model, instanceIdentifier, targetType);
201: }
202:
203: public boolean isInstanceof(URI classURI) {
204: return Base.hasInstance(model, classURI, instanceIdentifier);
205: }
206:
207: public BlankNode asBlankNode() throws ClassCastException {
208: return instanceIdentifier.asBlankNode();
209: }
210:
211: public DatatypeLiteral asDatatypeLiteral()
212: throws ClassCastException {
213: return instanceIdentifier.asDatatypeLiteral();
214: }
215:
216: public LanguageTagLiteral asLanguageTagLiteral()
217: throws ClassCastException {
218: return instanceIdentifier.asLanguageTagLiteral();
219: }
220:
221: public Literal asLiteral() throws ClassCastException {
222: return instanceIdentifier.asLiteral();
223: }
224:
225: public Resource asResource() throws ClassCastException {
226: return instanceIdentifier.asResource();
227: }
228:
229: public URI asURI() throws ClassCastException {
230: return instanceIdentifier.asURI();
231: }
232:
233: public int compareTo(Node o) {
234: return instanceIdentifier.compareTo(o);
235: }
236:
237: public String toSPARQL() {
238: return instanceIdentifier.toSPARQL();
239: }
240:
241: }
|