]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/translation/RelationRepository.java
(refs #7250) Refactoring CHR implementation
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / parsing / translation / RelationRepository.java
1 package org.simantics.scl.compiler.internal.parsing.translation;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5
6 import org.simantics.scl.compiler.elaboration.errors.NotPatternException;
7 import org.simantics.scl.compiler.internal.parsing.declarations.DAnnotationAst;
8 import org.simantics.scl.compiler.internal.parsing.declarations.DRelationAst;
9
10 import gnu.trove.map.hash.THashMap;
11
12 public class RelationRepository {
13     THashMap<String, ArrayList<DRelationAst>> relations = 
14             new THashMap<String, ArrayList<DRelationAst>>();
15     THashMap<String, ArrayList<DAnnotationAst>> annotations = 
16             new THashMap<String, ArrayList<DAnnotationAst>>();
17             
18     public String add(DRelationAst relation) throws NotPatternException {
19         String name = relation.lhs.getPatternHead().name;
20         ArrayList<DRelationAst> vs = relations.get(name);
21         if(vs == null) {
22             vs = new ArrayList<DRelationAst>(2);
23             relations.put(name, vs);
24         }
25         vs.add(relation);
26         return name;
27     }
28     
29     public void addDefinitions(String name, ArrayList<DRelationAst> relationList) {
30         ArrayList<DRelationAst> vs = relations.get(name);
31         if(vs == null) {
32             vs = new ArrayList<DRelationAst>(relationList.size());
33             relations.put(name, vs);
34         }
35         vs.addAll(relationList);
36     }
37     
38     public void addAnnotation(String name, DAnnotationAst annotation) {
39         ArrayList<DAnnotationAst> as = annotations.get(name);
40         if(as == null) {
41             as = new ArrayList<DAnnotationAst>(2);
42             annotations.put(name, as);
43         }
44         as.add(annotation);
45     }
46     
47     public void addAnnotations(String name, ArrayList<DAnnotationAst> annotationList) {
48         ArrayList<DAnnotationAst> as = annotations.get(name);
49         if(as == null) {
50             as = new ArrayList<DAnnotationAst>(annotationList.size());
51             annotations.put(name, as);
52         }
53         as.addAll(annotationList);
54     }
55     
56     public Collection<String> getRelationNames() {
57         return relations.keySet();
58     }
59
60     public ArrayList<DRelationAst> getDefinition(String name) {
61         return relations.get(name);
62     }
63
64     public ArrayList<DAnnotationAst> getAnnotations(String name) {
65         return annotations.get(name);
66     }
67
68     public void addFrom(RelationRepository repo, String oldName, String newName) {
69         addDefinitions(newName, repo.getDefinition(oldName));
70         
71         ArrayList<DAnnotationAst> as = repo.getAnnotations(oldName);
72         if(as != null)
73             addAnnotations(newName, as);
74     }
75 }