1   package ixa.kaflib;
2   
3   import java.io.Serializable;
4   import java.util.HashMap;
5   import java.util.regex.Matcher;
6   import java.util.regex.Pattern;
7   
8   
9   /**
10   * Manages ID creation. Each ID is created taking into account the annotations of the same type created so far, in a document context. This class keeps a counter for each type of annotation (terms, chunks...).
11   */
12  class IdManager implements Serializable {
13  
14  	public GenericId wfs = new GenericId("w");
15  	public GenericId terms = new GenericId("t");
16  	public GenericId marks = new GenericId("m");
17  	public GenericId mws = new GenericId("t.mw");
18  	public GenericId chunks = new GenericId("c");
19  	public GenericId entities = new GenericId("e");
20  	public GenericId corefs = new GenericId("co");
21  	public GenericId timex3s = new GenericId("tmx");
22  	public GenericId linkedentities = new GenericId("le");
23  	public GenericId properties = new GenericId("p");
24  	public GenericId categories = new GenericId("cat");
25  	public GenericId opinions = new GenericId("o");
26  	public GenericId relations = new GenericId("r");
27  	public GenericId predicates = new GenericId("pr");
28  	public GenericId roles = new GenericId("rl");
29  	public GenericId terminals = new GenericId("ter");
30  	public GenericId nonterminals = new GenericId("nter");
31  	public GenericId edges = new GenericId("tre");
32  	public GenericId ssts = new GenericId("sst");
33  	public GenericId topics = new GenericId("top");
34  	public GenericId tlinks = new GenericId("tlink");
35  	public GenericId clinks = new GenericId("clink");
36  
37  	private HashMap<String, Integer> componentCounter = new HashMap();
38  	boolean inconsistentIdComponent = false;
39  	private static final String COMPONENT_PREFIX = ".";
40  
41  	String getNextComponentId(String termId) {
42  		String newId;
43  		int nextIndex;
44  		if (this.inconsistentIdComponent) {
45  			throw new IllegalStateException("Inconsistent component IDs. Can't create new component IDs.");
46  		}
47  		if (!componentCounter.containsKey(termId)) {
48  			nextIndex = 1;
49  		}
50  		else {
51  			nextIndex = componentCounter.get(termId) + 1;
52  		}
53  		newId = termId + COMPONENT_PREFIX + Integer.toString(nextIndex);
54  		componentCounter.put(termId, nextIndex);
55  		return newId;
56  	}
57  
58  
59  	void updateComponentCounter(String id, String termId) {
60  		int componentInd;
61  		Matcher matcher = Pattern.compile("^" + terms.getPrefix() + "_?\\d+\\" + COMPONENT_PREFIX + "(\\d+)$").matcher(id);
62  		if (!matcher.find()) {
63  			this.inconsistentIdComponent = true;
64  			return;
65  		}
66  		componentInd = Integer.valueOf(matcher.group(1));
67  		componentCounter.put(termId, componentInd);
68  	}
69  
70  }