]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/chr/ExampleStore.java
(refs #7250) Refactoring CHR implementation
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / codegen / chr / ExampleStore.java
1 package org.simantics.scl.compiler.internal.codegen.chr;
2
3 import org.simantics.scl.runtime.chr.CHRFact;
4 import org.simantics.scl.runtime.chr.CHRHashIndex;
5 import org.simantics.scl.runtime.chr.CHRRuntimeRuleset;
6
7 public class ExampleStore extends CHRRuntimeRuleset {
8     /*
9      * constraint ExampleFact Integer Integer where
10      *     index(bf)
11      *     
12      * =>
13      * 
14      * data Store       // class Module$123
15      * data ExampleFact // class Module$123/ExampleFact
16      * 
17      * add     :: Store -> ExampleFact -> <Proc> ()
18      * remove  :: Store -> ExampleFact -> <Proc> ()
19      * get_bf  :: Store -> Integer -> <Proc> ExampleFact
20      * next_bf :: ExampleFact -> <Proc> Maybe ExampleFact
21      * idOf    :: ExampleFact -> <Proc> Integer
22      * isAlive :: ExampleFact -> <Proc> Boolean
23      */
24     
25     CHRHashIndex ExampleFact_bfIndex = new CHRHashIndex() {
26         @Override
27         protected boolean keyEquals(Object a, Object b) {
28             return ((ExampleFact)a).c0 == ((ExampleFact)b).c0;
29         }
30         @Override
31         protected int keyHashCode(Object key) {
32             return ((ExampleFact)key).c0;
33         }
34     };
35     
36     private ExampleFact ExampleFact_temp = new ExampleFact();
37         
38     public ExampleFact getExampleFact_bf(int c0) {
39         ExampleFact_temp.c0 = c0;
40         return (ExampleFact)ExampleFact_bfIndex.getEqual(ExampleFact_temp);
41     }
42     
43     public static class ExampleFact extends CHRFact {
44         public int c0; // key
45         public int c1;
46         public ExampleFact bfPrev;
47         public ExampleFact bfNext;
48         
49         public ExampleFact() {
50         }
51         
52         public ExampleFact(int c0, int c1) {
53             this.c0 = c0;
54             this.c1 = c1;
55         }
56         
57         public void add(ExampleStore store) {
58             bfNext = (ExampleFact)store.ExampleFact_bfIndex.addFreshAndReturnOld(this);
59             if(bfNext != null)
60                 bfNext.bfPrev = this;
61         }
62         
63         public void remove(ExampleStore store) {
64             if(bfPrev == null) {
65                 if(bfNext == null)
66                     store.ExampleFact_bfIndex.removeKnownToExistKey(this);
67                 else {
68                     bfNext.bfPrev = null;
69                     store.ExampleFact_bfIndex.replaceKnownToExistKey(this, bfNext);
70                 }
71             }
72             else {
73                 bfPrev.bfNext = bfNext;
74                 if(bfNext != null)
75                     bfNext.bfPrev = bfPrev;
76             }
77         }
78     }
79
80 }