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