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