001/** 002 * Instance.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 */ 009package es.ucm.fdi.gaia.jcolibri.datatypes; 010 011import org.apache.logging.log4j.LogManager; 012 013import es.ucm.fdi.gaia.jcolibri.connector.TypeAdaptor; 014import es.ucm.fdi.gaia.jcolibri.exception.OntologyAccessException; 015import es.ucm.fdi.gaia.jcolibri.util.OntoBridgeSingleton; 016 017/** 018 * Represents an Instance of an ontology.<p> 019 * This class uses OntoBridge to connect with the ontology. 020 * <p> 021 * It can manage both short or long names to localize an instance:<br> 022 * For example: In the gaia.fdi.ucm.es/ontologies/Restaurant.owl ontology, the long name of the concept "wine" is 023 * "gaia.fdi.ucm.es/ontologies/Restaurant.owl#wine. 024 * The short name is just "wine" or "restaurant:wine" if "restaurant" is a defined prefix for that ontology. 025 * 026 * @author Juan A. Recio-Garc�a. 027 * @version 2.0 028 * @see TypeAdaptor 029 */ 030public class Instance implements TypeAdaptor { 031 032 private String name; 033 private boolean useShortName = true; 034 035 public static Instance createInstance(String instanceName, String parentConcept) 036 { 037 try { 038 OntoBridgeSingleton.getOntoBridge().createInstance(parentConcept, instanceName); 039 return new Instance(instanceName); 040 } catch (OntologyAccessException e) { 041 LogManager.getLogger(Instance.class).error(e); 042 } 043 return null; 044 } 045 046 public Instance() 047 { 048 } 049 050 /** 051 * Creates an Instance looking for the instance in the ontology with the same name.<p> 052 * The useShortName param allows using short or long names. For example:<br> 053 * 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. 054 * The short name is just "wine" or "restaurant:wine" if "restaurant" is a defined prefix for that ontology. 055 * @param instance Name of the concept in the ontology 056 * @param useShortName Use long or short name 057 * @throws OntologyAccessException 058 */ 059 public Instance(String instance, boolean useShortName) throws OntologyAccessException 060 { 061 this.useShortName = useShortName; 062 fromString(instance); 063 } 064 065 /** 066 * Creates an instance connected with the instance in the ontology. It uses a short name format. 067 * @param instance Name of the concept in the ontology 068 * @throws OntologyAccessException 069 */ 070 public Instance(String instance) throws OntologyAccessException 071 { 072 fromString(instance); 073 } 074 075 public void fromString(String content) throws OntologyAccessException { 076 name = getCorrectRepresentation(content); 077 if(!OntoBridgeSingleton.getOntoBridge().existsInstance(name)) 078 throw new OntologyAccessException("Instance: "+ name +" not found in loaded ontologies. Check names or OntoBridge configuration."); 079 } 080 081 public String toString() 082 { 083 return getCorrectRepresentation(name); 084 } 085 086 public boolean equals(Object o) 087 { 088 if(!(o instanceof Instance)) 089 return false; 090 091 Instance i = (Instance)o; 092 093 String otherName = i.name; 094 if(isShort(otherName)) 095 otherName = OntoBridgeSingleton.getOntoBridge().getURI(otherName); 096 097 String myName = name; 098 if(isShort(myName)) 099 myName = OntoBridgeSingleton.getOntoBridge().getURI(myName); 100 101 return myName.equals(otherName); 102 } 103 104 public int hashCode() 105 { 106 return OntoBridgeSingleton.getOntoBridge().getURI(name).hashCode(); 107 } 108 109 /** 110 * Indicates if using a long or short name. 111 * @param yesno 112 */ 113 public void useShortName(boolean yesno) 114 { 115 useShortName = yesno; 116 } 117 118 private String getCorrectRepresentation(String n) 119 { 120 if(useShortName) 121 { 122 if(!isShort(n)) 123 return OntoBridgeSingleton.getOntoBridge().getShortName(n); 124 }else 125 { 126 if(isShort(n)) 127 return OntoBridgeSingleton.getOntoBridge().getURI(n); 128 } 129 return n; 130 } 131 132 private boolean isShort(String n) 133 { 134 return n.indexOf('#')==-1; 135 } 136}