001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030/** 031 * 032 */ 033package org.openimaj.rdf.owl2java; 034 035import java.util.ArrayList; 036import java.util.List; 037 038import org.openimaj.rdf.serialize.RelationList; 039import org.openimaj.rdf.serialize.TripleList; 040import org.openimaj.util.pair.IndependentPair; 041import org.openrdf.model.Statement; 042import org.openrdf.model.URI; 043import org.openrdf.model.Value; 044import org.openrdf.model.impl.StatementImpl; 045import org.openrdf.model.impl.URIImpl; 046 047/** 048 * Top level class that all top-level superclasses generated from an ontology 049 * will inherit from. This allows us to add functionality to all generated 050 * classes in one single place. 051 * <p> 052 * This class currently provides a single access point for: 053 * <ul> 054 * <li>retrieving and setting the URI of an instance</li> 055 * <li>adding unvalidated triples</li> 056 * <li>adding unvalidated relations</li> 057 * </ul> 058 * <p> 059 * During serialization, an unvalidated triples list will be sent directly 060 * to the triple sink and the field avoided. Unvalidated relations will be 061 * serialized in turn and sent to triple sink. 062 * 063 * @author David Dupplaw (dpd@ecs.soton.ac.uk) 064 * @created 15 Nov 2012 065 * @version $Author$, $Revision$, $Date$ 066 */ 067public abstract class Something 068{ 069 /** The URI of this instance */ 070 private String uri; 071 072 /** Whether unvalidated tuples are allowed to be added to the class */ 073 private boolean allowUnvalidatedTriples = true; 074 075 /** Any unvalidated tuples that have been added to this class */ 076 @TripleList 077 private final List<Statement> unvalidatedTriples = new ArrayList<Statement>(); 078 079 /** Whether to allow unvalidated graphs to be linked to this class */ 080 private boolean allowUnvalidatedRelations = true; 081 082 /** Any unvalidated relations that have been added to this class */ 083 @RelationList 084 private final List<IndependentPair<URI,Object>> unvalidatedRelations = 085 new ArrayList<IndependentPair<URI,Object>>(); 086 087 /** 088 * Get the URI of this instance 089 * @return The URI of this instance 090 */ 091 public String getURI() 092 { 093 return this.uri; 094 } 095 096 /** 097 * Set the URI of this instance. 098 * @param uri The URI of this instance 099 */ 100 public void setURI( final String uri ) 101 { 102 this.uri = uri; 103 } 104 105 /** 106 * Add an unvalidated tuple to this subgraph. 107 * @param predicate The predicate 108 * @param object The object 109 * @throws IllegalArgumentException if this subgraph does not allow 110 * unvalidated tuples to be added 111 */ 112 public void addUnvalidatedTuple( final URI predicate, final Value object ) 113 throws IllegalArgumentException 114 { 115 if( this.allowUnvalidatedTriples ) 116 this.unvalidatedTriples.add( new StatementImpl( new URIImpl( this.uri ), 117 predicate, object ) ); 118 else throw new IllegalArgumentException( "Adding tuples to a validated subgraph" ); 119 } 120 121 /** 122 * Set whether to allow unvalidated triples in the subgraph. 123 * @param tf TRUE to allow unvalidated triples, FALSE otherwise 124 */ 125 public void setAllowUnvalidatedTriples( final boolean tf ) 126 { 127 this.allowUnvalidatedTriples = tf; 128 } 129 130 /** 131 * Add an unvalidated relation to this subgraph. 132 * @param predicate The predicate 133 * @param object The object 134 * @throws IllegalArgumentException if this subgraph does not allow 135 * unvalidated tuples to be added 136 */ 137 public void addUnvalidatedRelation( final URI predicate, final Object object ) 138 throws IllegalArgumentException 139 { 140 if( this.allowUnvalidatedRelations ) 141 this.unvalidatedRelations.add( new IndependentPair<URI, Object>( 142 predicate, object ) ); 143 else throw new IllegalArgumentException( "Adding relations to a validated subgraph" ); 144 } 145 146 /** 147 * Set whether to allow unvalidated relations in the subgraph. 148 * @param tf TRUE to allow unvalidated relations, FALSE otherwise 149 */ 150 public void setAllowUnvalidatedRelations( final boolean tf ) 151 { 152 this.allowUnvalidatedRelations = tf; 153 } 154}