]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/chr/ExampleStore.java
Merge "Improved Statement API in Simantics/DB"
[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.CHRHashIndex;
4 import org.simantics.scl.runtime.chr.Fact;
5 import org.simantics.scl.runtime.chr.FactActivationQueue;
6
7 public class ExampleStore {
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     public FactActivationQueue queue = new FactActivationQueue(2);
37     
38     private ExampleFact ExampleFact_temp = new ExampleFact();
39         
40     public ExampleFact getExampleFact_bf(int c0) {
41         ExampleFact_temp.c0 = c0;
42         return (ExampleFact)ExampleFact_bfIndex.getEqual(ExampleFact_temp);
43     }
44     
45     public static class ExampleFact implements Fact {
46         public int id;
47         public int c0; // key
48         public int c1;
49         public ExampleFact bfPrev;
50         public ExampleFact bfNext;
51         
52         public ExampleFact() {
53         }
54         
55         public ExampleFact(int c0, int c1) {
56             this.c0 = c0;
57             this.c1 = c1;
58         }
59         
60         public void add(ExampleStore store) {
61             bfNext = (ExampleFact)store.ExampleFact_bfIndex.addFreshAndReturnOld(this);
62             if(bfNext != null)
63                 bfNext.bfPrev = this;
64         }
65         
66         public void remove(ExampleStore store) {
67             if(bfPrev == null) {
68                 if(bfNext == null)
69                     store.ExampleFact_bfIndex.removeKnownToExistKey(this);
70                 else {
71                     bfNext.bfPrev = null;
72                     store.ExampleFact_bfIndex.replaceKnownToExistKey(this, bfNext);
73                 }
74             }
75             else {
76                 bfPrev.bfNext = bfNext;
77                 if(bfNext != null)
78                     bfNext.bfPrev = bfPrev;
79             }
80         }
81
82         @Override
83         public int activate(Object context, int priority) {
84             return -1;
85         }
86
87         @Override
88         public boolean isAlive() {
89             return id >= 0;
90         }
91     }
92
93 }