public interface AnnotationType
Annotation
.
AnnotationType
instances can be used to validate an
Annotation
to check that it has the appropriate
properties and that they are of the right type.
// annType is going to define ID as being a single String and // AC as being a list of Strings and accept any other properties at all AnnotationType annType = new AnnotationType.Impl(); annType.setDefaultConstraints(PropertyConstraint.ANY, Cardinality.ANY) annType.setConstraints("ID", new PropertyConstraint.ByClass(String.class), CardinalityConstraint.ONE ); annType.setConstraint("AC", new PropertyConstraint.ByClass(String.class), CardinalityConstraint.ANY ); Annotation ann = ...; if(annType.accepts(ann)) { // we have proved that ann contains one ID property that is a String System.out.println("ID: " + (String) ann.getProperty("ID")); // we know that the AC property is potentialy more than one String - // let's use getProperty on AnnotationType to make sure that the // values get unpacked cleanly System.out.println("AC:"); for(Iterator i = annType.getProperty(ann, "AC"); i.hasNext(); ) { System.out.println("\t" + (String) i.next()); } } else { throw new IllegalArgumentException( "Expecting an annotation conforming to: " annType + " but got: " + ann ); }
AnnotationType is a constraint-based language for describing sets of Annotation bundles. It works by assuming that any given Annotation may have any set of properties defined. If it matches a particular AnnotationType instance, then each defined property must be of a value that is acceptable to the type, and each undefined property must be allowed to be absend in the type.
AnnotationType
imposes a data model on Annotations
where the value of each `slot' is considered as a Collection
.
Values which aren't actually Collection
instances are treated
as singleton sets. Undefined properties are treated as Collection.EMPTY_SET
.
The logic to validate a complete slot in an Annotation
is
encapsulated by a CollectionConstraint
object. In the common case,
slots are validated by CollectionConstraint.AllValuesIn
which
ensures that all members of the collection match a specified PropertyConstraint
,
and that the size of the collection matches a cardinality constraint. This is the
most important type of constraint when defining datatypes. However, when using
AnnotationTypes
to specify a query over a set of Annotations, it may be more
useful to ask whether a slot contains a given value: For example
// Test that gene_name contains the value "BRCA2", ignoring other values (synonyms) // which might be present in that slot. AnnotationType at = new AnnotationType.Impl(); at.setConstraint( "gene_name", new CollectionConstraint.Contains( new PropertyConstraint.ExactValue("BRCA2"), CardinalityConstraint.ONE ) );
It is usually left up to the AnnotationType instance to work out how multiple values should be packed into a single property slot in an Annotation instance. Commonly, things that are allowed a cardinality of 1 will store one value directly in the slot. Things that allow multiple values (and optionaly things with one value) will usualy store them within a Collection in the slot. This complexity is hidden from you if you use the accessor methods built into AnnotationType, setProperty(), addProperty(), removeProperty() and getProperty().
Using AnnotationType instances that you have been provided with e.g. from UnigeneTools.LIBRARY_ANNOTATIONAnnotationType
instances can be used as queries
to select from a set of Annotations based on the value of one
or more properties. Commonly, this is used in conjunction
with FeatureFilter.ByAnnotationType
.
Make AnnotationType instances that describe what should and
should not appear in an Annotation bundle
Constrain FeatureFilter schemas by Annotation associated with
the features
Provide meta-data to the tag-value parser for automatically
generating object representations of flat-files
Implementing your own AnnotationType implementations to reflect
frame, schema or ontology definitions. For example, dynamically
reflect an RDMBS schema or DAML/Oil deffinition as an
AnnotationType.Modifier and Type | Interface and Description |
---|---|
static class |
AnnotationType.Abstract
An abstract base class useful for implementing AnnotationType
instances.
|
static class |
AnnotationType.Impl
An implementation of
AnnotationType . |
Modifier and Type | Field and Description |
---|---|
static AnnotationType |
ANY
The type that accepts all annotations and is the supertype of all
other annotations.
|
static AnnotationType |
NONE
The type that accepts no annotations at all and is the subtype of all
other annotations.
|
Modifier and Type | Method and Description |
---|---|
void |
addProperty(Annotation ann,
Object property,
Object value)
Add a value to the specified property slot.
|
String |
getComment()
Get the comment for the whole AnnotationType.
|
String |
getComment(Object property)
Get the comment for a particular property.
|
CollectionConstraint |
getConstraint(Object key)
Retrieve the constraint that will be applied to all
properties with a given key.
|
CollectionConstraint |
getDefaultConstraint()
Get the CollectionConstraint that will be applied to all properties without
an explicit binding.
|
Set |
getProperties()
Retrieve the set of properties for which constraints have been explicity specified.
|
Collection |
getProperty(Annotation ann,
Object property)
Get the Collection of values associated with an Annotation bundle
according to the type we believe it to be.
|
boolean |
instanceOf(Annotation ann)
Validate an Annotation against this AnnotationType.
|
void |
removeProperty(Annotation ann,
Object property,
Object value)
Remove a value from the specified property slot.
|
void |
setComment(Object property,
String comment)
Set the comment for a particular property.
|
void |
setComment(String comment)
Set the comment for the whole AnnotationType.
|
void |
setConstraint(Object key,
CollectionConstraint con)
Specifies the constraint to apply to the specified property.
|
void |
setConstraints(Object key,
PropertyConstraint con,
Location card)
Set the constraints associated with a property.
|
void |
setDefaultConstraint(CollectionConstraint cc)
Specifies the default constraint to apply to properties where no
other constraint is specified.
|
void |
setDefaultConstraints(PropertyConstraint pc,
Location cc)
Set the constraints that will apply to all properties without an
explicitly defined set of constraints.
|
void |
setProperty(Annotation ann,
Object property,
Object value)
Set the property in an annotation bundle according to the type we believe
it should be.
|
boolean |
subTypeOf(AnnotationType subType)
See if an AnnotationType is a specialisation of this type.
|
static final AnnotationType ANY
static final AnnotationType NONE
boolean instanceOf(Annotation ann)
ann
- the Annotation to validate.boolean subTypeOf(AnnotationType subType)
See if an AnnotationType is a specialisation of this type.
An AnnotationType is a sub-type if it restricts each of the properties of the super-type to a type that can be cast to the type in the super-type. Note that this is not always a cast in the pure Java sense; it may include checks on the number and type of members in collections or other criteria.
subType
- an AnnotationType to check.CollectionConstraint getConstraint(Object key)
Retrieve the constraint that will be applied to all properties with a given key.
For an Annotation
to be accepted, each key in
getProperties() must be present in the annotation and each of the
values associated with those properties must match the
constraint.
key
- the property to be validated.void setConstraints(Object key, PropertyConstraint con, Location card)
con
, and the number of members must match card
.
It implicitly constructs a CollectionConstraint.AllValuesIn
instance.
When you are building your own AnnotationType
key
- the name of the property to constraincon
- the PropertyConstraint to enforcecard
- the CardinalityConstraint to enforcevoid setConstraint(Object key, CollectionConstraint con)
key
- the name of the property to constraincon
- the constraint to apply to this slot.void setDefaultConstraints(PropertyConstraint pc, Location cc)
con
, and the number of members must match card
.
It implicitly constructs a CollectionConstraint.AllValuesIn
instance.pc
- the default PropertyConstraintcc
- the default CardinalityConstraintvoid setDefaultConstraint(CollectionConstraint cc)
cc
- The default constraint.CollectionConstraint getDefaultConstraint()
If you want to find out exactly what constraint will be applied to properties with no explicitly defined constraints
Set getProperties()
void setProperty(Annotation ann, Object property, Object value) throws ChangeVetoException
ann
- the Annotation to modifyproperty
- the property key Objectvalue
- the property value ObjectChangeVetoException
- if the value could not be accepted by this
annotation type for that property key, or if the Annotation could
not be modifiedvoid addProperty(Annotation ann, Object property, Object value) throws ChangeVetoException
ann
- the Annotation to modifyproperty
- the property key Objectvalue
- the property value ObjectChangeVetoException
- if the value could not be accepted by this
annotation type for that property key, or if the Annotation could
not be modifiedCollection getProperty(Annotation ann, Object property) throws ChangeVetoException
ann
- the Annotation to accessproperty
- the property key ObjectChangeVetoException
- if the value could not be removedvoid removeProperty(Annotation ann, Object property, Object value) throws ChangeVetoException
ann
- the Annotation to modifyproperty
- the property key Objectvalue
- the property value ObjectChangeVetoException
- if the Annotation could
not be modifiedvoid setComment(String comment)
comment
- the new commentString getComment()
void setComment(Object property, String comment)
property
- the property to comment oncomment
- the commentString getComment(Object property)
property
- the property to get a comment forCopyright © 2014 BioJava. All rights reserved.