001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: SimpleGraphMaker.java,v 1.18 2008/01/02 12:05:17 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.graph.impl;
008:
009: import com.hp.hpl.jena.graph.*;
010: import com.hp.hpl.jena.mem.*;
011: import com.hp.hpl.jena.shared.*;
012: import com.hp.hpl.jena.util.iterator.*;
013: import com.hp.hpl.jena.vocabulary.*;
014:
015: import java.util.*;
016:
017: /**
018: @author hedgehog
019:
020: A SimpleGraphFactory produces memory-based graphs and records them
021: in a local map.
022: */
023:
024: public class SimpleGraphMaker extends BaseGraphMaker {
025: /**
026: Initialise a SimpleGraphMaker with the given style.
027: @param style the reification style of all the graphs we create
028: */
029: public SimpleGraphMaker(ReificationStyle style) {
030: super (style);
031: }
032:
033: /**
034: Initialise a SimpleGraphMaker with reification style Minimal
035: */
036: public SimpleGraphMaker() {
037: this (ReificationStyle.Minimal);
038: }
039:
040: /**
041: Answer the RDFS class of this Maker
042: @return JenaModelSpec.MemMakerClass [as node]
043: */
044: public Node getMakerClass() {
045: return JenaModelSpec.MemMakerSpec.asNode();
046: }
047:
048: /**
049: Augment the general description of a GraphMaker with any Simple triples [ie none]
050: */
051: protected void augmentDescription(Graph d, Node self) {
052: }
053:
054: /**
055: The mapping from the names of graphs to the Graphs themselves.
056: */
057: private Map graphs = new HashMap();
058:
059: public Graph create() {
060: return Factory.createGraphMem();
061: }
062:
063: /**
064: Create a graph and record it with the given name in the local map.
065: */
066: public Graph createGraph(String name, boolean strict) {
067: GraphMemBase already = (GraphMemBase) graphs.get(name);
068: if (already == null) {
069: Graph result = Factory.createGraphMem(style);
070: graphs.put(name, result);
071: return result;
072: } else if (strict)
073: throw new AlreadyExistsException(name);
074: else
075: return already.openAgain();
076: }
077:
078: /**
079: Open (aka find) a graph with the given name in the local map.
080: */
081: public Graph openGraph(String name, boolean strict) {
082: GraphMemBase already = (GraphMemBase) graphs.get(name);
083: if (already == null)
084: if (strict)
085: throw new DoesNotExistException(name);
086: else
087: return createGraph(name, true);
088: else
089: return already.openAgain();
090: }
091:
092: public Graph openGraph() {
093: return getGraph();
094: }
095:
096: /**
097: Remove the mapping from name to any graph from the local map.
098: */
099: public void removeGraph(String name) {
100: if (!graphs.containsKey(name))
101: throw new DoesNotExistException(name);
102: graphs.remove(name);
103: }
104:
105: /**
106: Return true iff we have a graph with the given name
107: */
108: public boolean hasGraph(String name) {
109: return graphs.containsKey(name);
110: }
111:
112: /**
113: Close this factory - we choose to do nothing.
114: */
115: public void close() { /* nothing to do */
116: }
117:
118: public ExtendedIterator listGraphs() {
119: return WrappedIterator.create(graphs.keySet().iterator());
120: }
121: }
122:
123: /*
124: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
125: All rights reserved.
126:
127: Redistribution and use in source and binary forms, with or without
128: modification, are permitted provided that the following conditions
129: are met:
130:
131: 1. Redistributions of source code must retain the above copyright
132: notice, this list of conditions and the following disclaimer.
133:
134: 2. Redistributions in binary form must reproduce the above copyright
135: notice, this list of conditions and the following disclaimer in the
136: documentation and/or other materials provided with the distribution.
137:
138: 3. The name of the author may not be used to endorse or promote products
139: derived from this software without specific prior written permission.
140:
141: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
142: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
143: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
144: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
145: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
146: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
147: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
148: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
149: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
150: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
151: */
|