001    /**
002     * ConceptType.java
003     * jCOLIBRI2 framework. 
004     * @author Juan A. Recio-García.
005     * GAIA - Group for Artificial Intelligence Applications
006     * http://gaia.fdi.ucm.es
007     * 11/01/2007
008     */
009    package jcolibri.datatypes;
010    
011    import jcolibri.connector.TypeAdaptor;
012    import jcolibri.exception.OntologyAccessException;
013    import jcolibri.util.OntoBridgeSingleton;
014    
015    /**
016     * Represents a Concept of an ontology.<p>
017     * This class uses OntoBridge to connect with the ontology.
018     * <p>
019     * It can manage both short or long names to localize a concept:<br>
020     * For example: In the gaia.fdi.ucm.es/ontologies/Restaurant.owl ontology, the long name of the concept "wine" is 
021     * "gaia.fdi.ucm.es/ontologies/Restaurant.owl#wine. 
022     * The short name is just "wine" or "restaurant:wine" if "restaurant" is a defined prefix for that ontology.
023     * 
024     * @author Juan A. Recio-García.
025     * @version 2.0
026     * @see jcolibri.connector.TypeAdaptor
027     */
028    public class ConceptType implements TypeAdaptor {
029    
030            private String name;
031            private boolean useShortName = true;
032            
033            public ConceptType()
034            {
035                    
036            }
037            
038            /**
039             * Creates a ConceptType looking for the concept in the ontology with the same name.<p>
040             * The useShortName param allows using short or long names. For example:<br>
041             * In the gaia.fdi.ucm.es/ontologies/Restaurant.owl ontology, the long name of the concept "wine" is "gaia.fdi.ucm.es/ontologies/Restaurant.owl#wine. 
042             * The short name is just "wine" or "restaurant:wine" if "restaurant" is a defined prefix for that ontology.
043             * @param concept Name of the concept in the ontology
044             * @param useShortName Use long or short name
045             * @throws OntologyAccessException
046             */
047            public ConceptType(String concept, boolean useShortName) throws OntologyAccessException
048            {
049                    this.useShortName = useShortName; 
050                    fromString(concept);
051            }
052            
053            /**
054             * Creates a ConceptType connected with the concept in the ontology. It uses a short name format. 
055             * @param concept Name of the concept in the ontology
056             * @throws OntologyAccessException
057             */
058            public ConceptType(String concept) throws OntologyAccessException
059            {
060                    fromString(concept);
061            }
062            
063            public void fromString(String content) throws OntologyAccessException {
064                    name = getCorrectRepresentation(content);
065                    if(!OntoBridgeSingleton.getOntoBridge().existsInstance(name))
066                            throw new jcolibri.exception.OntologyAccessException("Instance: "+ name +" not found in loaded ontologies. Check names or OntoBridge configuration.");
067            }
068            
069            public String toString()
070            {
071                    return getCorrectRepresentation(name);
072            }
073    
074            /**
075             * Indicates if using a long or short name.
076             * @param yesno
077             */
078            public void useShortName(boolean yesno)
079            {
080                    useShortName = yesno;
081            }
082            
083            private String getCorrectRepresentation(String n)
084            {
085                    if(useShortName)
086                    {
087                            if(!isShort(n))
088                                    return OntoBridgeSingleton.getOntoBridge().getShortName(n);
089                    }else
090                    {
091                            if(isShort(n))
092                                    return OntoBridgeSingleton.getOntoBridge().getURI(n);
093                    }
094                    return n;
095            }
096            
097            private boolean isShort(String n)
098            {
099                    return n.indexOf('#')==-1;
100            }
101    }