1 package org.simantics.scl.runtime.minigraph;
3 import java.util.ArrayList;
4 import java.util.Collections;
7 import org.simantics.scl.runtime.SCLContext;
8 import org.simantics.scl.runtime.function.Function;
9 import org.simantics.scl.runtime.tuple.Tuple0;
11 import gnu.trove.impl.Constants;
12 import gnu.trove.map.hash.TIntObjectHashMap;
13 import gnu.trove.map.hash.TLongObjectHashMap;
14 import gnu.trove.map.hash.TObjectIntHashMap;
15 import gnu.trove.procedure.TIntProcedure;
16 import gnu.trove.set.hash.TIntHashSet;
18 public class Minigraph {
20 private static final int[] EMPTY_INT_ARRAY = new int[0];
22 TIntObjectHashMap<TIntHashSet> predicatesPerSubject = new TIntObjectHashMap<TIntHashSet>();
23 TLongObjectHashMap<Object> objectsPerSubjectPredicate = new TLongObjectHashMap<Object>();
25 TIntObjectHashMap<String> idToUri = new TIntObjectHashMap<String>();
26 TObjectIntHashMap<String> uriToId = new TObjectIntHashMap<String>(Constants.DEFAULT_CAPACITY,
27 Constants.DEFAULT_LOAD_FACTOR, -1);
28 TIntObjectHashMap<String> values = new TIntObjectHashMap<String>();
30 int resourceCount = 0;
33 private static long combine(int a, int b) {
34 return (((long)a)<<32) | ((long)b);
37 private static int[] toArray(Object obj) {
39 return EMPTY_INT_ARRAY;
40 else if(obj instanceof Integer)
41 return new int[] { (Integer)obj };
43 return ((TIntHashSet)obj).toArray();
46 private static void add(TIntObjectHashMap<TIntHashSet> map, int key, int value) {
47 TIntHashSet set = map.get(key);
49 set = new TIntHashSet();
55 private static void remove(TIntObjectHashMap<TIntHashSet> map, int key, int value) {
56 TIntHashSet set = map.get(key);
61 private static void add(TLongObjectHashMap<Object> map, long key, int value) {
62 Object obj = map.get(key);
64 map.put(key, Integer.valueOf(value));
65 else if(obj instanceof Integer) {
66 TIntHashSet set = new TIntHashSet();
67 set.add((Integer)obj);
72 ((TIntHashSet)obj).add(value);
75 private static boolean remove(TLongObjectHashMap<Object> map, long key, int value) {
76 Object obj = map.get(key);
79 else if(obj instanceof Integer) {
80 if(value != ((Integer)obj).intValue())
86 TIntHashSet set = (TIntHashSet)obj;
87 if(!set.remove(value))
90 map.put(key, Integer.valueOf(set.iterator().next()));
96 return resourceCount++;
99 public int getResource(String uri) {
100 int id = uriToId.get(uri);
103 idToUri.put(resourceCount, uri);
104 uriToId.put(uri, resourceCount);
105 return resourceCount++;
108 public String getUri(int r) {
109 String uri = idToUri.get(r);
116 public void rawClaim(int s, int p, int o) {
117 add(predicatesPerSubject, s, p);
118 add(objectsPerSubjectPredicate, combine(s, p), o);
121 public void claim(int s, int p, int o) {
123 int inv = getPossibleObject(p, InverseOf);
128 public void rawDeny(int s, int p, int o) {
129 if(remove(objectsPerSubjectPredicate, combine(s, p), o))
130 remove(predicatesPerSubject, s, p);
133 public void deny(int s, int p, int o) {
135 int inv = getPossibleObject(p, InverseOf);
140 public int[] getObjects(int s, int p) {
141 return toArray(objectsPerSubjectPredicate.get(combine(s, p)));
144 public int[] getSubjects(int o, int p) {
145 int inv = getPossibleObject(p, InverseOf);
147 return getObjects(o, inv);
149 return EMPTY_INT_ARRAY;
152 public int getPossibleObject(int s, int p) {
153 Object obj = objectsPerSubjectPredicate.get(combine(s, p));
154 if(obj instanceof Integer)
155 return ((Integer)obj).intValue();
160 public boolean hasStatement(int s, int p, int o) {
161 Object obj = objectsPerSubjectPredicate.get(combine(s, p));
164 else if(obj instanceof Integer)
165 return o == ((Integer)obj).intValue();
167 return ((TIntHashSet)obj).contains(o);
170 public List<Statement> getStatements(final int s) {
171 TIntHashSet preds = predicatesPerSubject.get(s);
173 return Collections.<Statement>emptyList();
175 final ArrayList<Statement> statements = new ArrayList<Statement>();
176 preds.forEach(new TIntProcedure() {
178 public boolean execute(final int p) {
179 Object obj = objectsPerSubjectPredicate.get(combine(s, p));
180 if(obj instanceof Integer)
181 statements.add(new Statement(s, p, (Integer)obj));
183 ((TIntHashSet)obj).forEach(new TIntProcedure() {
185 public boolean execute(int o) {
186 statements.add(new Statement(s, p, o));
196 public void setValue(int s, String value) {
197 values.put(s, value);
200 private void initializeLayer0() {
201 InverseOf = getResource("Layer0/InverseOf");
202 rawClaim(InverseOf, InverseOf, InverseOf);
209 public static void main(String[] args) {
210 Minigraph g = new Minigraph();
211 g.claim(10, g.InverseOf, 11);
212 g.claim(12, g.InverseOf, 13);
216 System.out.println(g.getStatements(2));
219 public static Object withGraph(Function f) {
220 Object oldGraph = SCLContext.getCurrent().put("graph", new Minigraph());
222 return f.apply(Tuple0.INSTANCE);
224 SCLContext.getCurrent().put("graph", oldGraph);