001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: */
005:
006: package com.hp.hpl.jena.db.impl;
007:
008: import java.net.UnknownHostException;
009: import java.rmi.server.UID;
010:
011: import com.hp.hpl.jena.graph.*;
012: import com.hp.hpl.jena.util.iterator.*;
013: import com.hp.hpl.jena.vocabulary.DB;
014:
015: /**
016: *
017: * A base class for DB property information in a persistent store.
018: *
019: * This is written in the style of enhanced nodes - no state is
020: * stored in the DBStoreDesc, instead all state is in the
021: * underlying graph and this is just provided as a convenience.
022: *
023: * (We don't use enhanced nodes because, since we control everything
024: * in the persistent store system description, we can avoid any
025: * need to handle polymorhphism).
026: *
027: *
028: * @author csayers
029: * @version $Revision: 1.20 $
030: */
031: public abstract class DBProp {
032:
033: protected SpecializedGraph graph = null;
034: protected Node self = null;
035:
036: public DBProp(SpecializedGraph g) {
037: graph = g;
038: self = generateNodeURI();
039: }
040:
041: public DBProp(SpecializedGraph g, Node n) {
042: graph = g;
043: self = n;
044: }
045:
046: public Node getNode() {
047: return self;
048: }
049:
050: /**
051: Utility method for creating a CompletionFlag; its value lies in having a short name!
052: @return a new SpecializedGraph.CompletionFlag() set to <code>false</code>.
053: */
054: protected static SpecializedGraph.CompletionFlag newComplete() {
055: return new SpecializedGraph.CompletionFlag();
056: }
057:
058: /**
059: Add (self, predicate, literal(value)) to the graph.
060: */
061: protected void putPropString(Node_URI predicate, String value) {
062: putPropNode(predicate, Node.createLiteral(value));
063: }
064:
065: /**
066: Add (self, predicate, node) to the graph.
067: */
068: protected void putPropNode(Node_URI predicate, Node node) {
069: graph.add(Triple.create(self, predicate, node), newComplete());
070: }
071:
072: /**
073: Answer the single string s such that (self, predicate, literal(s)) is in the
074: graph, or null if there's no such s.
075: */
076: protected String getPropString(Node predicate) {
077: Node n = getPropNode(predicate);
078: return n == null ? null : n.getLiteralLexicalForm();
079: }
080:
081: /**
082: Answer the single node n such that (subject, predicate, n) is in the
083: graph, or null if there's no such n.
084: */
085: protected Node getPropNode(Node subject, Node predicate) {
086: ClosableIterator it = graph.find(subject, predicate, Node.ANY,
087: newComplete());
088: Node result = it.hasNext() ? ((Triple) it.next()).getObject()
089: : null;
090: it.close();
091: return result;
092: }
093:
094: /**
095: Answer the single node n such that (self, predicate, n) is in the
096: graph, or null if there's no such n.
097: */
098: protected Node getPropNode(Node predicate) {
099: return getPropNode(self, predicate);
100: }
101:
102: protected void remove() {
103: SpecializedGraph.CompletionFlag complete = newComplete();
104: ClosableIterator it = graph.find(self, null, null, complete);
105: while (it.hasNext())
106: graph.delete((Triple) it.next(), complete);
107: it.close();
108: self = null;
109: graph = null;
110: }
111:
112: void showGraph() {
113: SpecializedGraph.CompletionFlag complete = newComplete();
114: ExtendedIterator it = graph.find(self, null, null, complete);
115: while (it.hasNext())
116: System.err.println(">> " + it.next());
117: }
118:
119: public static ExtendedIterator listTriples(SpecializedGraph g,
120: Node self) {
121: // Get all the triples about the requested node.
122: return g.find(self, null, null, newComplete());
123: }
124:
125: /**
126: @deprecated no uses remaining in Jena codebase
127: */
128: protected static Node findProperty(Graph graph, Node_URI predicate) {
129: ClosableIterator it = graph.find(null, predicate, null);
130: Node result = null;
131: if (it.hasNext())
132: result = ((Triple) it.next()).getObject();
133: it.close();
134: return result;
135: }
136:
137: public static String generateUniqueID() {
138: UID uid = new UID();
139: String hostname;
140: try {
141: hostname = java.net.InetAddress.getLocalHost()
142: .getHostAddress().toString();
143: } catch (UnknownHostException e) {
144: hostname = "localhost";
145: }
146: return (hostname + uid.toString()).replace('.', '_').replace(
147: ':', '_').replace('-', '_');
148: }
149:
150: public static Node generateNodeURI() {
151: String generateUniqueID = null;
152: return Node.createURI(DB.uri + generateUniqueID());
153: }
154:
155: }
156:
157: /*
158: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
159: * All rights reserved.
160: *
161: * Redistribution and use in source and binary forms, with or without
162: * modification, are permitted provided that the following conditions
163: * are met:
164: * 1. Redistributions of source code must retain the above copyright
165: * notice, this list of conditions and the following disclaimer.
166: * 2. Redistributions in binary form must reproduce the above copyright
167: * notice, this list of conditions and the following disclaimer in the
168: * documentation and/or other materials provided with the distribution.
169: * 3. The name of the author may not be used to endorse or promote products
170: * derived from this software without specific prior written permission.
171:
172: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
173: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
174: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
175: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
176: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
177: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
178: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
179: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
180: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
181: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
182: */
|