1 package org.simantics.scl.compiler.elaboration.java;
4 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.*;
6 import org.cojen.classfile.TypeDesc;
7 import org.simantics.scl.compiler.constants.generic.CallJava;
8 import org.simantics.scl.compiler.constants.generic.MethodRef.ObjectMethodRef;
9 import org.simantics.scl.compiler.constants.generic.MethodRef.StaticMethodRef;
10 import org.simantics.scl.compiler.constants.generic.ParameterStackItem;
11 import org.simantics.scl.compiler.constants.generic.StackItem;
12 import org.simantics.scl.compiler.constants.generic.ThreadLocalStackItem;
13 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
14 import org.simantics.scl.compiler.elaboration.expressions.EApply;
15 import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
16 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
17 import org.simantics.scl.compiler.elaboration.expressions.Expression;
18 import org.simantics.scl.compiler.elaboration.expressions.Variable;
19 import org.simantics.scl.compiler.elaboration.query.QAtom;
20 import org.simantics.scl.compiler.elaboration.query.QConjunction;
21 import org.simantics.scl.compiler.elaboration.query.Query;
22 import org.simantics.scl.compiler.elaboration.query.compilation.EnforcingContext;
23 import org.simantics.scl.compiler.elaboration.query.compilation.QueryCompilationContext;
24 import org.simantics.scl.compiler.elaboration.relations.AbstractRelation;
25 import org.simantics.scl.compiler.elaboration.relations.SCLEntityType;
26 import org.simantics.scl.compiler.internal.codegen.effects.EffectConstructor;
27 import org.simantics.scl.compiler.internal.codegen.effects.ThreadLocalVariable;
28 import org.simantics.scl.compiler.internal.codegen.utils.Constants;
29 import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilderBase;
30 import org.simantics.scl.compiler.module.ConcreteModule;
31 import org.simantics.scl.compiler.types.TCon;
32 import org.simantics.scl.compiler.types.TVar;
33 import org.simantics.scl.compiler.types.Type;
34 import org.simantics.scl.compiler.types.Types;
35 import org.simantics.scl.compiler.types.kinds.Kinds;
36 import org.simantics.scl.runtime.minigraph.Minigraph;
38 public class MinigraphModule extends ConcreteModule {
39 public static final String MODULE = "Minigraph";
40 // public static final TCon RESOURCE = Types.con(MODULE, "Resource");
41 public static final TCon RESOURCE = Types.INTEGER;
42 public static final TCon GRAPH = Types.con(MODULE, "Graph");
44 private static final String THREAD_LOCAL_VARIABLE_NAME = "graph";
45 private static final TypeDesc MINIGRAPH = TypeDesc.forClass(Minigraph.class);
47 private static final CallJava CLAIM_METHOD = new CallJava(
50 Types.tupleConstructor(0),
51 new Type[] { RESOURCE, RESOURCE, RESOURCE },
53 new ThreadLocalStackItem(THREAD_LOCAL_VARIABLE_NAME, MINIGRAPH),
54 new ParameterStackItem(0, RESOURCE),
55 new ParameterStackItem(1, RESOURCE),
56 new ParameterStackItem(2, RESOURCE),
58 new ObjectMethodRef(false, MethodBuilderBase.getClassName(MINIGRAPH), "claim", TypeDesc.VOID, new TypeDesc[] {
59 TypeDesc.INT, TypeDesc.INT, TypeDesc.INT
64 private static final CallJava HAS_STATEMENT_METHOD = new CallJava(
68 new Type[] { RESOURCE, RESOURCE, RESOURCE },
70 new ThreadLocalStackItem(THREAD_LOCAL_VARIABLE_NAME, MINIGRAPH),
71 new ParameterStackItem(0, RESOURCE),
72 new ParameterStackItem(1, RESOURCE),
73 new ParameterStackItem(2, RESOURCE),
75 new ObjectMethodRef(false, MethodBuilderBase.getClassName(MINIGRAPH), "hasStatement", TypeDesc.BOOLEAN, new TypeDesc[] {
76 TypeDesc.INT, TypeDesc.INT, TypeDesc.INT
81 private static final CallJava GET_OBJECTS_METHOD = new CallJava(
84 Types.vector(RESOURCE),
85 new Type[] { RESOURCE, RESOURCE },
87 new ThreadLocalStackItem(THREAD_LOCAL_VARIABLE_NAME, MINIGRAPH),
88 new ParameterStackItem(0, RESOURCE),
89 new ParameterStackItem(1, RESOURCE),
91 new ObjectMethodRef(false, MethodBuilderBase.getClassName(MINIGRAPH), "getObjects", TypeDesc.INT.toArrayType(), new TypeDesc[] {
92 TypeDesc.INT, TypeDesc.INT
97 private static final CallJava BLANK_METHOD = new CallJava(
101 new Type[] { Types.UNIT },
103 new ThreadLocalStackItem(THREAD_LOCAL_VARIABLE_NAME, MINIGRAPH)
105 new ObjectMethodRef(false, MethodBuilderBase.getClassName(MINIGRAPH), "blank", TypeDesc.INT, Constants.EMPTY_TYPEDESC_ARRAY),
109 private static final CallJava GET_SUBJECTS_METHOD = new CallJava(
112 Types.vector(RESOURCE),
113 new Type[] { RESOURCE, RESOURCE },
115 new ThreadLocalStackItem(THREAD_LOCAL_VARIABLE_NAME, MINIGRAPH),
116 new ParameterStackItem(0, RESOURCE),
117 new ParameterStackItem(1, RESOURCE),
119 new ObjectMethodRef(false, MethodBuilderBase.getClassName(MINIGRAPH), "getSubjects", TypeDesc.INT.toArrayType(), new TypeDesc[] {
120 TypeDesc.INT, TypeDesc.INT
125 private static final CallJava RESOURCE_METHOD = new CallJava(
129 new Type[] { Types.STRING },
131 new ThreadLocalStackItem(THREAD_LOCAL_VARIABLE_NAME, MINIGRAPH),
132 new ParameterStackItem(0, Types.STRING),
134 new ObjectMethodRef(false, MethodBuilderBase.getClassName(MINIGRAPH), "getResource", TypeDesc.INT, new TypeDesc[] {
140 private static final CallJava GET_URI_METHOD = new CallJava(
144 new Type[] { RESOURCE },
146 new ThreadLocalStackItem(THREAD_LOCAL_VARIABLE_NAME, MINIGRAPH),
147 new ParameterStackItem(0, RESOURCE),
149 new ObjectMethodRef(false, MethodBuilderBase.getClassName(MINIGRAPH), "getUri", TypeDesc.STRING, new TypeDesc[] {
155 private static final TVar A = Types.var(Kinds.STAR);
156 private static final TVar E = Types.var(Kinds.EFFECT);
157 private static final Type F = Types.functionE(Types.PUNIT, Types.union(new Type[] {GRAPH, E}), A);
158 private static final CallJava WITH_GRAPH_METHOD = new CallJava(
160 Types.union(new Type[] {Types.PROC, E}),
164 new ParameterStackItem(0, F),
166 new StaticMethodRef(MethodBuilderBase.getClassName(MINIGRAPH), "withGraph", TypeDesc.OBJECT, new TypeDesc[] {
172 public static final MinigraphModule INSTANCE = new MinigraphModule();
174 private MinigraphModule() {
176 //addTypeConstructor("Resource", new StandardTypeConstructor(RESOURCE, Kinds.STAR, TypeDesc.INT));
177 EffectConstructor effect = new EffectConstructor(Types.con(MODULE, "Graph"));
178 effect.addThreadLocalVariable(new ThreadLocalVariable(THREAD_LOCAL_VARIABLE_NAME, MINIGRAPH));
179 addEffectConstructor("Graph", effect);
180 addValue("resource", RESOURCE_METHOD);
181 addValue("blank", BLANK_METHOD);
182 addValue("withGraph", WITH_GRAPH_METHOD);
183 addValue("uriOf", GET_URI_METHOD);
184 addRelation("Statement", new AbstractRelation() {
187 public TVar[] getTypeVariables() {
188 return TVar.EMPTY_ARRAY;
192 public double getSelectivity(int boundVariabes) {
193 switch(boundVariabes) {
194 case BBF: return 10.0;
195 case FBB: return 10.0;
196 case BBB: return 0.95;
197 default: return Double.POSITIVE_INFINITY;
202 public int getRequiredVariablesMask() {
207 public Type[] getParameterTypes() {
208 return new Type[] { RESOURCE, RESOURCE, RESOURCE };
212 public Expression generateEnforce(long location, EnforcingContext context,
213 Type[] typeParameters,
214 Variable[] parameters) {
215 return new EApply(new ELiteral(CLAIM_METHOD),
217 new EVariable(parameters[0]),
218 new EVariable(parameters[1]),
219 new EVariable(parameters[2])
224 public void generate(long location,
225 QueryCompilationContext context,
226 Type[] typeParameters, Variable[] parameters, int boundVariables) {
227 switch(boundVariables) {
229 context.iterateVector(parameters[2],
230 new EApply(new ELiteral(GET_OBJECTS_METHOD),
232 new EVariable(parameters[0]),
233 new EVariable(parameters[1]),
237 context.iterateVector(parameters[0],
238 new EApply(new ELiteral(GET_SUBJECTS_METHOD),
240 new EVariable(parameters[2]),
241 new EVariable(parameters[1]),
246 new EApply(new ELiteral(HAS_STATEMENT_METHOD),
248 new EVariable(parameters[0]),
249 new EVariable(parameters[1]),
250 new EVariable(parameters[2])
253 default: throw new IllegalArgumentException();
258 public String toString() {
262 addEntityType("Resource", new SCLEntityType() {
264 public Query generateQuery(TranslationContext context, Variable base,
265 AttributeBinding[] attributeBindings) {
266 Query[] queries = new Query[attributeBindings.length];
267 for(int i=0;i<attributeBindings.length;++i) {
268 AttributeBinding binding = attributeBindings[i];
269 queries[i] = new QAtom(getRelation("Statement"),
271 apply(constant(getValue("resource")), string(((ResourceAttribute)binding.attribute).name)),
272 var(binding.variable));
274 return new QConjunction(queries);
278 public Attribute getAttribute(String name) {
279 return new ResourceAttribute(name);
284 private static class ResourceAttribute implements SCLEntityType.Attribute {
287 public ResourceAttribute(String name) {