001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: OntModelSpecAssembler.java,v 1.12 2008/01/31 12:30:49 chris-dollin Exp $
005: */
006:
007: package com.hp.hpl.jena.assembler.assemblers;
008:
009: import java.lang.reflect.*;
010:
011: import com.hp.hpl.jena.assembler.*;
012: import com.hp.hpl.jena.assembler.exceptions.ReasonerClashException;
013: import com.hp.hpl.jena.ontology.*;
014: import com.hp.hpl.jena.rdf.model.*;
015: import com.hp.hpl.jena.reasoner.ReasonerFactory;
016: import com.hp.hpl.jena.shared.NotFoundException;
017:
018: /**
019: An OntModelSpecAssembler constructs OntModelSpec's from their
020: RDF description. The description allows the document manager, the
021: reasoner factory, the ont language, and the import model getter to
022: be specified: the default values will be those of OWL_MEM_RDFS_INF,
023: unless the root is ja:SPOO for some constant SPOO of OntModelSpec,
024: in which case the defaults are taken from there.
025: */
026: public class OntModelSpecAssembler extends AssemblerBase implements
027: Assembler {
028: private static final OntModelSpec DEFAULT = OntModelSpec.OWL_MEM_RDFS_INF;
029:
030: public Object open(Assembler a, Resource root, Mode irrelevant) {
031: checkType(root, JA.OntModelSpec);
032: OntModelSpec spec = new OntModelSpec(getDefault(root));
033: OntDocumentManager dm = getDocumentManager(a, root);
034: ReasonerFactory rf = getReasonerFactory(a, root);
035: String lang = getLanguage(a, root);
036: ModelGetter source = getModelSource(a, root);
037: if (dm != null)
038: spec.setDocumentManager(dm);
039: if (rf != null)
040: spec.setReasonerFactory(rf);
041: if (lang != null)
042: spec.setLanguage(lang);
043: if (source != null)
044: spec.setImportModelGetter(source);
045: return spec;
046: }
047:
048: private ModelGetter getModelSource(Assembler a, Resource root) {
049: Resource source = getUniqueResource(root, JA.importSource);
050: return source == null ? null : (ModelGetter) a.open(source);
051: }
052:
053: private String getLanguage(Assembler a, Resource root) {
054: Resource lang = getUniqueResource(root, JA.ontLanguage);
055: return lang == null ? null : lang.getURI();
056: }
057:
058: private ReasonerFactory getReasonerFactory(Assembler a,
059: Resource root) {
060: Resource rf = getUniqueResource(root, JA.reasonerFactory);
061: Resource ru = getUniqueResource(root, JA.reasonerURL);
062: if (ru != null && rf != null)
063: throw new ReasonerClashException(root);
064: if (ru != null)
065: return ReasonerFactoryAssembler.getReasonerFactoryByURL(
066: root, ru);
067: return rf == null ? null : (ReasonerFactory) a.open(rf);
068: }
069:
070: private OntDocumentManager getDocumentManager(Assembler a,
071: Resource root) {
072: Resource dm = getUniqueResource(root, JA.documentManager);
073: return dm == null ? null : (OntDocumentManager) a.open(dm);
074: }
075:
076: /**
077: Answer the default OntModelSpec for this root, which will be
078: <code>DEFAULT</code> unless <code>root</code> has the JA
079: namespace and a local name which is the name of an OntModelSpec
080: constant (in OntModelSpec), in which case it's that constant's value.
081: */
082: private OntModelSpec getDefault(Resource root) {
083: if (root.isURIResource() && root.getNameSpace().equals(JA.uri)) {
084: OntModelSpec oms = getOntModelSpecField(root.getLocalName());
085: return oms == null ? DEFAULT : oms;
086: } else {
087: Resource like = getUniqueResource(root, JA.likeBuiltinSpec);
088: return like == null ? DEFAULT
089: : getRequiredOntModelSpecField(like.getLocalName());
090: }
091: }
092:
093: private OntModelSpec getRequiredOntModelSpecField(String name) {
094: OntModelSpec result = getOntModelSpecField(name);
095: if (result == null)
096: throw new NotFoundException(name);
097: return result;
098: }
099:
100: /**
101: Answer the OntModelSpec in the OntModelSpec class with the given
102: member name, or null if there isn't one.
103: */
104: public static OntModelSpec getOntModelSpecField(String name) {
105: try {
106: Class omc = OntModelSpec.class;
107: Field f = omc.getField(name);
108: int mods = f.getModifiers();
109: if (f.getType() == omc && isConstant(mods))
110: return (OntModelSpec) f.get(null);
111: } catch (Exception e) {
112: }
113: return null;
114: }
115:
116: protected static boolean isConstant(int mods) {
117: return Modifier.isPublic(mods) && Modifier.isFinal(mods)
118: && Modifier.isStatic(mods);
119: }
120: }
121:
122: /*
123: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
124: * All rights reserved.
125: *
126: * Redistribution and use in source and binary forms, with or without
127: * modification, are permitted provided that the following conditions
128: * are met:
129: * 1. Redistributions of source code must retain the above copyright
130: * notice, this list of conditions and the following disclaimer.
131: * 2. Redistributions in binary form must reproduce the above copyright
132: * notice, this list of conditions and the following disclaimer in the
133: * documentation and/or other materials provided with the distribution.
134: * 3. The name of the author may not be used to endorse or promote products
135: * derived from this software without specific prior written permission.
136: *
137: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
138: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
139: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
140: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
141: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
142: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
143: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
144: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
145: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
146: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
147: */
|