1   package gate.lexicon;
2   
3   import gate.DataStore;
4   import gate.persist.PersistenceException;
5   import gate.security.SecurityException;
6   import gate.LanguageResource;
7   import gate.Resource;
8   import gate.FeatureMap;
9   import java.io.Serializable;
10  import java.util.*;
11  import gate.creole.*;
12  
13  public class OntoLexKBImpl extends AbstractLanguageResource
14      implements OntoLexLR, Serializable {
15  
16    protected HashMap lexIdMap = new HashMap();
17    protected HashMap conceptIdMap = new HashMap();
18    protected Object lexKBIndentifier;
19    protected Object ontologyIdentifier;
20    static final long serialVersionUID = -6008345192731330593L;
21  
22    public OntoLexKBImpl() {
23    }
24  
25    public List getConceptIds(Object lexId) {
26      Object concepts = lexIdMap.get(lexId);
27      if (concepts == null || !(concepts instanceof List))
28        return null;
29      return (List) concepts;
30    }
31  
32    public List getLexIds(Object conceptId) {
33      Object lexItems = conceptIdMap.get(conceptId);
34      if (lexItems == null || !(lexItems instanceof List))
35        return null;
36      return (List) lexItems;
37    }
38  
39    public Set getAllLexIds() {
40      return lexIdMap.keySet();
41    }
42  
43    public Set getAllConceptIds() {
44      return conceptIdMap.keySet();
45    }
46  
47    public void add(Object conceptId, Object lexId) {
48      //first add it to the concept index
49      Object lexItems = conceptIdMap.get(conceptId);
50      List lexItemsList;
51      if (lexItems == null) {
52        lexItemsList = new ArrayList();
53        lexItemsList.add(lexId);
54        conceptIdMap.put(conceptId, lexItemsList);
55      }
56      else {
57        lexItemsList = (List) lexItems;
58        lexItemsList.add(lexId);
59      }
60  
61      //then add it to the lexical index
62      Object conceptItems = lexIdMap.get(lexId);
63      List conceptItemsList;
64      if (conceptItems == null) {
65        conceptItemsList = new ArrayList();
66        conceptItemsList.add(conceptId);
67        lexIdMap.put(lexId, conceptItemsList);
68      }
69      else {
70        conceptItemsList = (List) conceptItems;
71        conceptItemsList.add(conceptId);
72      }
73  
74    }
75  
76    public void removeByConcept(Object conceptId) {
77      //first find the list of lexical Ids that are mapped to this concept ID
78      //and delete the conceptId from their list
79      Object lexIds = conceptIdMap.get(conceptId);
80      if (lexIds == null || !(lexIds instanceof List))
81        return;
82      List lexIdList = (List) lexIds;
83      for (int i=0; i < lexIdList.size(); i++) {
84        Object lexId = lexIdList.get(i);
85        List conceptList = getConceptIds(lexId);
86        if (conceptList != null)
87          conceptList.remove(conceptId);
88      }//for
89  
90      //finally delete the conceptId from the conceptIdMap
91      conceptIdMap.remove(conceptId);
92    }
93  
94    public void removeByLexId(Object lexId) {
95      //first find the list of concept Ids that are mapped to this lexical ID
96      //and delete the lex Id from their list
97      Object conceptIds = lexIdMap.get(lexId);
98      if (conceptIds == null || !(conceptIds instanceof List))
99        return;
100     List conceptIdList = (List) conceptIds;
101     for (int i=0; i < conceptIdList.size(); i++) {
102       Object conceptId = conceptIdList.get(i);
103       List lexList = getLexIds(conceptId);
104       if (lexList != null)
105         lexList.remove(lexId);
106     }//for
107 
108     //finally delete the lexId from the lexIdMap
109     lexIdMap.remove(lexId);
110   }
111 
112   public void remove(Object conceptId, Object lexId) {
113     Object conceptIds = lexIdMap.get(lexId);
114     if (conceptIds != null && (conceptIds instanceof List))
115       ((List) conceptIds).remove(conceptId);
116     Object lexIds = conceptIdMap.get(conceptId);
117     if (lexIds != null && (lexIds instanceof List))
118       ((List) lexIds).remove(lexId);
119   }
120 
121   public boolean isEmpty() {
122     return conceptIdMap.isEmpty() && lexIdMap.isEmpty();
123   }
124 
125   public void clear() {
126     conceptIdMap.clear();
127     lexIdMap.clear();
128   }
129 
130   public Object getLexKBIdentifier() {
131     return this.lexKBIndentifier;
132   }
133 
134   public void setLexKBIdentifier(Object lexId) {
135     this.lexKBIndentifier = lexId;
136   }
137 
138   public Object getOntologyIdentifier() {
139     return this.ontologyIdentifier;
140   }
141 
142   public void setOntologyIdentifier(Object ontoId) {
143     this.ontologyIdentifier = ontoId;
144   }
145 
146   public Resource init() throws gate.creole.ResourceInstantiationException {
147     if (this.ontologyIdentifier == null || this.lexKBIndentifier == null)
148       throw new ResourceInstantiationException(
149         "You must specify the identifiers of the ontology "
150         + "and lexicon for the mapping");
151     return super.init();
152   }
153 
154 }