001: /*****************************************************************************
002: * Source code information
003: * -----------------------
004: * Original author Ian Dickinson, HP Labs Bristol
005: * Author email Ian.Dickinson@hp.com
006: * Package Jena 2
007: * Web http://sourceforge.net/projects/jena/
008: * Created 02-Apr-2003
009: * Filename $RCSfile: AbstractProfile.java,v $
010: * Revision $Revision: 1.10 $
011: * Release status $State: Exp $
012: *
013: * Last modified on $Date: 2008/01/02 12:08:03 $
014: * by $Author: andy_seaborne $
015: *
016: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
017: * (see footer for full conditions)
018: *****************************************************************************/package com.hp.hpl.jena.ontology.impl;
019:
020: // Imports
021: ///////////////
022: import com.hp.hpl.jena.rdf.model.*;
023: import com.hp.hpl.jena.graph.*;
024: import com.hp.hpl.jena.enhanced.*;
025: import com.hp.hpl.jena.util.*;
026: import com.hp.hpl.jena.ontology.*;
027:
028: import java.util.*;
029:
030: /**
031: * <p>
032: * Abstract base class to provide shared implementation for ontology language profiles.
033: * </p>
034: *
035: * @author Ian Dickinson, HP Labs
036: * (<a href="mailto:Ian.Dickinson@hp.com" >email</a>)
037: * @version CVS $Id: AbstractProfile.java,v 1.10 2008/01/02 12:08:03 andy_seaborne Exp $
038: */
039: public abstract class AbstractProfile implements Profile {
040: // Constants
041: //////////////////////////////////
042:
043: // Static variables
044: //////////////////////////////////
045:
046: // Instance variables
047: //////////////////////////////////
048:
049: /** Map of aliases for resources */
050: protected OneToManyMap m_aliasesMap;
051:
052: // Constructors
053: //////////////////////////////////
054:
055: // External signature methods
056: //////////////////////////////////
057:
058: /**
059: * <p>
060: * Answer true if the given resource has an alias in this profile.
061: * </p>
062: *
063: * @param res A resource (including properties) to test for an alias
064: * @return True if there is an alias for <code>res</code>
065: */
066: public boolean hasAliasFor(Resource res) {
067: return aliasMap().containsKey(res);
068: }
069:
070: /**
071: * <p>
072: * Answer an alias for the given resource. If there is more than
073: * one such alias, a choice is made non-deterministically between the
074: * alternatives.
075: * </p>
076: *
077: * @param res A resource (including properties) to test for an alias
078: * @return The alias for <code>res</code>, or one of the aliases for <code>res</code> if more
079: * than one is defined, or null if no alias is defined for <code>res</code>.
080: *
081: */
082: public Resource getAliasFor(Resource res) {
083: return (Resource) aliasMap().get(res);
084: }
085:
086: /**
087: * <p>
088: * Answer an iterator over the defined aliases for a resource.
089: * </p>
090: *
091: * @param res A resource (including properties)
092: * @return An iterator over the aliases for <code>res</code>. If there are
093: * no aliases, the empty iterator is returned.
094: */
095: public Iterator listAliasesFor(Resource res) {
096: return aliasMap().getAll(res);
097: }
098:
099: /**
100: Utility method: answer true iff the enhanced graph contains some triple which
101: has n as subject, p.asNode() as predicate, and any object.
102:
103: @param g an enhanced graph to search for triples
104: @param n some node
105: @param p a property containing a predicate node
106: @return true iff the graph contains (n, p, X) for some X
107: */
108: public static boolean containsSome(EnhGraph g, Node n, Property p) {
109: return g.asGraph().contains(n, p.asNode(), Node.ANY);
110: }
111:
112: // Internal implementation methods
113: //////////////////////////////////
114:
115: /**
116: * Answer a table of binary mappings denoting that one resource is the
117: * alias for another (for example daml:Class and rdfs:Class).
118: */
119: protected abstract Resource[][] aliasTable();
120:
121: /**
122: * <p>
123: * Prepare the local alias map by reading the alias table from the concrete sub-class.
124: * </p>
125: */
126: protected OneToManyMap aliasMap() {
127: if (m_aliasesMap == null) {
128: // aliases map not prepared yet, so initialise using the data from
129: // the concrete profile class
130: m_aliasesMap = new OneToManyMap();
131: Resource[][] aliases = aliasTable();
132:
133: for (int i = 0; i < aliases.length; i++) {
134: // since alias relationship is symmetric, we record both directions
135: m_aliasesMap.put(aliases[i][0], aliases[i][1]);
136: m_aliasesMap.put(aliases[i][1], aliases[i][0]);
137: }
138: }
139:
140: return m_aliasesMap;
141: }
142:
143: //==============================================================================
144: // Inner class definitions
145: //==============================================================================
146:
147: }
148:
149: /*
150: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
151: All rights reserved.
152:
153: Redistribution and use in source and binary forms, with or without
154: modification, are permitted provided that the following conditions
155: are met:
156:
157: 1. Redistributions of source code must retain the above copyright
158: notice, this list of conditions and the following disclaimer.
159:
160: 2. Redistributions in binary form must reproduce the above copyright
161: notice, this list of conditions and the following disclaimer in the
162: documentation and/or other materials provided with the distribution.
163:
164: 3. The name of the author may not be used to endorse or promote products
165: derived from this software without specific prior written permission.
166:
167: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
168: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
169: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
170: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
171: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
172: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
173: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
174: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
175: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
176: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
177: */
|