]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/unit/TestCHRCodeGenerator.java
87bd50aa25fa7beda114fb343321a98912a88b4f
[simantics/platform.git] / tests / org.simantics.scl.compiler.tests / src / org / simantics / scl / compiler / tests / unit / TestCHRCodeGenerator.java
1 package org.simantics.scl.compiler.tests.unit;
2
3 import java.lang.reflect.Constructor;
4 import java.lang.reflect.Field;
5 import java.lang.reflect.Method;
6
7 import org.junit.Assert;
8 import org.junit.Test;
9 import org.simantics.scl.compiler.compilation.CompilationContext;
10 import org.simantics.scl.compiler.elaboration.chr.CHRRuleset;
11 import org.simantics.scl.compiler.elaboration.chr.relations.CHRConstraint;
12 import org.simantics.scl.compiler.elaboration.chr.relations.CHRConstraint.IndexInfo;
13 import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
14 import org.simantics.scl.compiler.errors.Locations;
15 import org.simantics.scl.compiler.internal.codegen.chr.CHRCodeGenerator;
16 import org.simantics.scl.compiler.internal.codegen.types.JavaTypeTranslator;
17 import org.simantics.scl.compiler.internal.codegen.utils.JavaNamingPolicy;
18 import org.simantics.scl.compiler.internal.codegen.utils.ModuleBuilder;
19 import org.simantics.scl.compiler.module.repository.ModuleRepository;
20 import org.simantics.scl.compiler.runtime.MutableClassLoader;
21 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
22 import org.simantics.scl.compiler.tests.InitialRepository;
23 import org.simantics.scl.compiler.types.Type;
24 import org.simantics.scl.compiler.types.Types;
25
26 public class TestCHRCodeGenerator {
27     @Test
28     public void testCodeGenerator() throws Throwable {
29         try {
30             ModuleRepository repository = InitialRepository.getInitialRepository();
31             RuntimeEnvironment environment = repository.createRuntimeEnvironment(EnvironmentSpecification.of("StandardLibrary", "", "Builtin", ""), getClass().getClassLoader());
32             JavaNamingPolicy policy = new JavaNamingPolicy("Expression");
33             JavaTypeTranslator jtt = new JavaTypeTranslator(environment.getEnvironment());
34             CompilationContext compilationContext = new CompilationContext();
35             compilationContext.environment = environment.getEnvironment();
36             compilationContext.javaTypeTranslator = jtt;
37             compilationContext.namingPolicy = policy;
38             ModuleBuilder moduleBuilder = new ModuleBuilder(policy, jtt);
39             
40             CHRRuleset ruleset = new CHRRuleset(); 
41             CHRConstraint exampleFact = new CHRConstraint(Locations.NO_LOCATION, "ExampleFact", new Type[] { Types.INTEGER, Types.INTEGER });
42             ruleset.constraints.add(exampleFact);
43     
44             System.out.println("==============================================================================================");
45             ruleset.initializeCodeGeneration(compilationContext);
46             exampleFact.indices.put(0, new IndexInfo(0, "ff", null, null));
47             exampleFact.indices.put(1, new IndexInfo(1, "bf", null, null));
48             exampleFact.indices.put(2, new IndexInfo(2, "fb", null, null));
49             exampleFact.indices.put(3, new IndexInfo(3, "bb", null, null));
50             exampleFact.setMayBeRemoved();
51
52             CHRCodeGenerator.generateStore(moduleBuilder, ruleset);
53             
54             MutableClassLoader classLoader = environment.getMutableClassLoader();
55             classLoader.addClasses(moduleBuilder.getClasses());
56             
57             String storeClassName = ruleset.storeClassName.replace('/', '.');
58             Class<?> storeClass = classLoader.loadClass(storeClassName);
59             Class<?> factClass = classLoader.loadClass(storeClassName+"$ExampleFact");
60             Constructor<?> factConstructor = factClass.getConstructor(int.class, int.class, int.class);
61             Method addMethod = factClass.getMethod("add", storeClass);
62             Method removeMethod = factClass.getMethod("remove", storeClass);
63             Method getMethod = storeClass.getMethod("ExampleFact$bf", int.class);
64             Field nextField = factClass.getField("bfNext");
65             
66             Object store = storeClass.newInstance();
67             Object fact1 = factConstructor.newInstance(0, 1,2);
68             Object fact2 = factConstructor.newInstance(0, 1,3);
69             Object fact3 = factConstructor.newInstance(0, 2,4);
70             Object fact4 = factConstructor.newInstance(0, 1,4);
71             addMethod.invoke(fact1, store);
72             addMethod.invoke(fact4, store);
73             addMethod.invoke(fact2, store);
74             addMethod.invoke(fact3, store);
75             removeMethod.invoke(fact4, store);
76             {
77                 Object f1 = getMethod.invoke(store, 1);
78                 Assert.assertEquals(fact2, f1);
79                 Object f2 = nextField.get(f1);
80                 Assert.assertEquals(fact1, f2);
81                 Object f3 = nextField.get(f2);
82                 Assert.assertEquals(null, f3);
83             }
84             removeMethod.invoke(fact2, store);
85             {
86                 Object f1 = getMethod.invoke(store, 1);
87                 Assert.assertEquals(fact1, f1);
88                 Object f2 = nextField.get(f1);
89                 Assert.assertEquals(null, f2);
90             }
91             addMethod.invoke(fact2, store);
92             removeMethod.invoke(fact1, store);
93             {
94                 Object f1 = getMethod.invoke(store, 1);
95                 Assert.assertEquals(fact2, f1);
96                 Object f2 = nextField.get(f1);
97                 Assert.assertEquals(null, f2);
98             }
99             removeMethod.invoke(fact2, store);
100             {
101                 Object f1 = getMethod.invoke(store, 1);
102                 Assert.assertEquals(null, f1);
103             }
104         } catch(Throwable e) {
105             e.printStackTrace();
106             throw e;
107         }
108     }
109         
110 }