1 package org.simantics.scl.compiler.tests.unit;
3 import java.lang.reflect.Constructor;
4 import java.lang.reflect.Field;
5 import java.lang.reflect.Method;
7 import org.junit.Assert;
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;
26 public class TestCHRCodeGenerator {
28 public void testCodeGenerator() throws Throwable {
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);
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);
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();
52 CHRCodeGenerator.generateStore(moduleBuilder, ruleset);
54 MutableClassLoader classLoader = environment.getMutableClassLoader();
55 classLoader.addClasses(moduleBuilder.getClasses());
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");
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);
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);
84 removeMethod.invoke(fact2, store);
86 Object f1 = getMethod.invoke(store, 1);
87 Assert.assertEquals(fact1, f1);
88 Object f2 = nextField.get(f1);
89 Assert.assertEquals(null, f2);
91 addMethod.invoke(fact2, store);
92 removeMethod.invoke(fact1, store);
94 Object f1 = getMethod.invoke(store, 1);
95 Assert.assertEquals(fact2, f1);
96 Object f2 = nextField.get(f1);
97 Assert.assertEquals(null, f2);
99 removeMethod.invoke(fact2, store);
101 Object f1 = getMethod.invoke(store, 1);
102 Assert.assertEquals(null, f1);
104 } catch(Throwable e) {