1 package org.simantics.scl.compiler.elaboration.java;
4 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.apply;
5 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.constant;
6 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.string;
7 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.var;
9 import org.cojen.classfile.TypeDesc;
10 import org.simantics.scl.compiler.constants.generic.CallJava;
11 import org.simantics.scl.compiler.constants.generic.MethodRef.ObjectMethodRef;
12 import org.simantics.scl.compiler.constants.generic.MethodRef.StaticMethodRef;
13 import org.simantics.scl.compiler.constants.generic.ParameterStackItem;
14 import org.simantics.scl.compiler.constants.generic.StackItem;
15 import org.simantics.scl.compiler.constants.generic.ThreadLocalStackItem;
16 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
17 import org.simantics.scl.compiler.elaboration.expressions.EApply;
18 import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
19 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
20 import org.simantics.scl.compiler.elaboration.expressions.Expression;
21 import org.simantics.scl.compiler.elaboration.expressions.Variable;
22 import org.simantics.scl.compiler.elaboration.query.QAtom;
23 import org.simantics.scl.compiler.elaboration.query.QConjunction;
24 import org.simantics.scl.compiler.elaboration.query.Query;
25 import org.simantics.scl.compiler.elaboration.query.compilation.EnforcingContext;
26 import org.simantics.scl.compiler.elaboration.query.compilation.QueryCompilationContext;
27 import org.simantics.scl.compiler.elaboration.relations.AbstractRelation;
28 import org.simantics.scl.compiler.elaboration.relations.SCLEntityType;
29 import org.simantics.scl.compiler.internal.codegen.effects.EffectConstructor;
30 import org.simantics.scl.compiler.internal.codegen.effects.ThreadLocalVariable;
31 import org.simantics.scl.compiler.internal.codegen.utils.Constants;
32 import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilderBase;
33 import org.simantics.scl.compiler.module.ConcreteModule;
34 import org.simantics.scl.compiler.types.TCon;
35 import org.simantics.scl.compiler.types.TVar;
36 import org.simantics.scl.compiler.types.Type;
37 import org.simantics.scl.compiler.types.Types;
38 import org.simantics.scl.compiler.types.kinds.Kinds;
39 import org.simantics.scl.runtime.minigraph.Minigraph;
41 public class MinigraphModule extends ConcreteModule {
42 public static final String MODULE = "Minigraph";
43 // public static final TCon RESOURCE = Types.con(MODULE, "Resource");
44 public static final TCon RESOURCE = Types.INTEGER;
45 public static final TCon GRAPH = Types.con(MODULE, "Graph");
47 private static final String THREAD_LOCAL_VARIABLE_NAME = "graph";
48 private static final TypeDesc MINIGRAPH = TypeDesc.forClass(Minigraph.class);
50 private static final CallJava CLAIM_METHOD = new CallJava(
53 Types.tupleConstructor(0),
54 new Type[] { RESOURCE, RESOURCE, RESOURCE },
56 new ThreadLocalStackItem(THREAD_LOCAL_VARIABLE_NAME, MINIGRAPH),
57 new ParameterStackItem(0, RESOURCE),
58 new ParameterStackItem(1, RESOURCE),
59 new ParameterStackItem(2, RESOURCE),
61 new ObjectMethodRef(false, MethodBuilderBase.getClassName(MINIGRAPH), "claim", TypeDesc.VOID, new TypeDesc[] {
62 TypeDesc.INT, TypeDesc.INT, TypeDesc.INT
67 private static final CallJava HAS_STATEMENT_METHOD = new CallJava(
71 new Type[] { RESOURCE, RESOURCE, RESOURCE },
73 new ThreadLocalStackItem(THREAD_LOCAL_VARIABLE_NAME, MINIGRAPH),
74 new ParameterStackItem(0, RESOURCE),
75 new ParameterStackItem(1, RESOURCE),
76 new ParameterStackItem(2, RESOURCE),
78 new ObjectMethodRef(false, MethodBuilderBase.getClassName(MINIGRAPH), "hasStatement", TypeDesc.BOOLEAN, new TypeDesc[] {
79 TypeDesc.INT, TypeDesc.INT, TypeDesc.INT
84 private static final CallJava GET_OBJECTS_METHOD = new CallJava(
87 Types.vector(RESOURCE),
88 new Type[] { RESOURCE, RESOURCE },
90 new ThreadLocalStackItem(THREAD_LOCAL_VARIABLE_NAME, MINIGRAPH),
91 new ParameterStackItem(0, RESOURCE),
92 new ParameterStackItem(1, RESOURCE),
94 new ObjectMethodRef(false, MethodBuilderBase.getClassName(MINIGRAPH), "getObjects", TypeDesc.INT.toArrayType(), new TypeDesc[] {
95 TypeDesc.INT, TypeDesc.INT
100 private static final CallJava BLANK_METHOD = new CallJava(
104 new Type[] { Types.UNIT },
106 new ThreadLocalStackItem(THREAD_LOCAL_VARIABLE_NAME, MINIGRAPH)
108 new ObjectMethodRef(false, MethodBuilderBase.getClassName(MINIGRAPH), "blank", TypeDesc.INT, Constants.EMPTY_TYPEDESC_ARRAY),
112 private static final CallJava GET_SUBJECTS_METHOD = new CallJava(
115 Types.vector(RESOURCE),
116 new Type[] { RESOURCE, RESOURCE },
118 new ThreadLocalStackItem(THREAD_LOCAL_VARIABLE_NAME, MINIGRAPH),
119 new ParameterStackItem(0, RESOURCE),
120 new ParameterStackItem(1, RESOURCE),
122 new ObjectMethodRef(false, MethodBuilderBase.getClassName(MINIGRAPH), "getSubjects", TypeDesc.INT.toArrayType(), new TypeDesc[] {
123 TypeDesc.INT, TypeDesc.INT
128 private static final CallJava RESOURCE_METHOD = new CallJava(
132 new Type[] { Types.STRING },
134 new ThreadLocalStackItem(THREAD_LOCAL_VARIABLE_NAME, MINIGRAPH),
135 new ParameterStackItem(0, Types.STRING),
137 new ObjectMethodRef(false, MethodBuilderBase.getClassName(MINIGRAPH), "getResource", TypeDesc.INT, new TypeDesc[] {
143 private static final CallJava GET_URI_METHOD = new CallJava(
147 new Type[] { RESOURCE },
149 new ThreadLocalStackItem(THREAD_LOCAL_VARIABLE_NAME, MINIGRAPH),
150 new ParameterStackItem(0, RESOURCE),
152 new ObjectMethodRef(false, MethodBuilderBase.getClassName(MINIGRAPH), "getUri", TypeDesc.STRING, new TypeDesc[] {
158 private static final TVar A = Types.var(Kinds.STAR);
159 private static final TVar E = Types.var(Kinds.EFFECT);
160 private static final Type F = Types.functionE(Types.PUNIT, Types.union(new Type[] {GRAPH, E}), A);
161 private static final CallJava WITH_GRAPH_METHOD = new CallJava(
163 Types.union(new Type[] {Types.PROC, E}),
167 new ParameterStackItem(0, F),
169 new StaticMethodRef(MethodBuilderBase.getClassName(MINIGRAPH), "withGraph", TypeDesc.OBJECT, new TypeDesc[] {
175 public static final MinigraphModule INSTANCE = new MinigraphModule();
177 private MinigraphModule() {
179 //addTypeConstructor("Resource", new StandardTypeConstructor(RESOURCE, Kinds.STAR, TypeDesc.INT));
180 EffectConstructor effect = new EffectConstructor(Types.con(MODULE, "Graph"));
181 effect.addThreadLocalVariable(new ThreadLocalVariable(THREAD_LOCAL_VARIABLE_NAME, MINIGRAPH));
182 addEffectConstructor("Graph", effect);
183 addValue("resource", RESOURCE_METHOD);
184 addValue("blank", BLANK_METHOD);
185 addValue("withGraph", WITH_GRAPH_METHOD);
186 addValue("uriOf", GET_URI_METHOD);
187 addRelation("Statement", new AbstractRelation() {
190 public TVar[] getTypeVariables() {
191 return TVar.EMPTY_ARRAY;
195 public double getSelectivity(int boundVariabes) {
196 switch(boundVariabes) {
197 case BBF: return 10.0;
198 case FBB: return 10.0;
199 case BBB: return 0.95;
200 default: return Double.POSITIVE_INFINITY;
205 public int getRequiredVariablesMask() {
210 public Type[] getParameterTypes() {
211 return new Type[] { RESOURCE, RESOURCE, RESOURCE };
215 public Expression generateEnforce(long location, EnforcingContext context,
216 Type[] typeParameters,
217 Variable[] parameters) {
218 return new EApply(new ELiteral(CLAIM_METHOD),
220 new EVariable(parameters[0]),
221 new EVariable(parameters[1]),
222 new EVariable(parameters[2])
227 public void generate(long location,
228 QueryCompilationContext context,
229 Type[] typeParameters, Variable[] parameters, int boundVariables) {
230 switch(boundVariables) {
232 context.iterateVector(parameters[2],
233 new EApply(new ELiteral(GET_OBJECTS_METHOD),
235 new EVariable(parameters[0]),
236 new EVariable(parameters[1]),
240 context.iterateVector(parameters[0],
241 new EApply(new ELiteral(GET_SUBJECTS_METHOD),
243 new EVariable(parameters[2]),
244 new EVariable(parameters[1]),
249 new EApply(new ELiteral(HAS_STATEMENT_METHOD),
251 new EVariable(parameters[0]),
252 new EVariable(parameters[1]),
253 new EVariable(parameters[2])
256 default: throw new IllegalArgumentException();
261 public String toString() {
265 addEntityType("Resource", new SCLEntityType() {
267 public Query generateQuery(TranslationContext context, Variable base,
268 AttributeBinding[] attributeBindings) {
269 Query[] queries = new Query[attributeBindings.length];
270 for(int i=0;i<attributeBindings.length;++i) {
271 AttributeBinding binding = attributeBindings[i];
272 queries[i] = new QAtom(getRelation("Statement"),
274 apply(constant(getValue("resource")), string(((ResourceAttribute)binding.attribute).name)),
275 var(binding.variable));
277 return new QConjunction(queries);
281 public Attribute getAttribute(String name) {
282 return new ResourceAttribute(name);
287 private static class ResourceAttribute implements SCLEntityType.Attribute {
290 public ResourceAttribute(String name) {