1 package org.simantics.graph.tests.diff;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.Random;
7 import org.simantics.databoard.Bindings;
8 import org.simantics.databoard.type.Datatype;
9 import org.simantics.graph.diff.Diff;
10 import org.simantics.graph.representation.External;
11 import org.simantics.graph.representation.Identity;
12 import org.simantics.graph.representation.Internal;
13 import org.simantics.graph.representation.Optional;
14 import org.simantics.graph.representation.TransferableGraph1;
15 import org.simantics.graph.representation.Value;
17 public class TestDiff {
19 public static TransferableGraph1 load(String name) throws IOException {
20 InputStream stream = TestDiff.class.getResourceAsStream(name);
21 Bindings.getSerializerUnchecked(Datatype.class).skip(stream);
22 TransferableGraph1 result = (TransferableGraph1)TransferableGraph1.SERIALIZER.deserialize(stream);
27 public static int[] randomPermutation(int size) {
28 Random rand = new Random();
29 int[] permutation = new int[size];
30 for(int i=0;i<size;++i)
32 for(int i=size-1;i>0;--i) {
33 int j = rand.nextInt(i+1);
34 int temp = permutation[i];
35 permutation[i] = permutation[j];
36 permutation[j] = temp;
41 public static void shuffle(TransferableGraph1 tg) {
42 int[] permutation = randomPermutation(tg.resourceCount);
45 int[] statements = tg.statements;
46 for(int i=0;i<statements.length;++i) {
47 int r = statements[i];
49 statements[i] = permutation[r];
53 for(Identity id : tg.identities) {
54 id.resource = permutation[id.resource];
55 if(id.definition instanceof External) {
56 External def = (External)id.definition;
57 def.parent = permutation[def.parent];
59 else if(id.definition instanceof Internal) {
60 Internal def = (Internal)id.definition;
61 def.parent = permutation[def.parent];
63 else if(id.definition instanceof Optional) {
64 Optional def = (Optional)id.definition;
65 def.parent = permutation[def.parent];
69 for(Value value : tg.values) {
70 value.resource = permutation[value.resource];
74 public static void main(String[] args) throws Exception {
75 TransferableGraph1 graphOld = load("layer0.tg");
76 TransferableGraph1 graphNew = load("layer0.tg");
78 long begin = System.nanoTime();
79 Diff diff = new Diff(graphOld, graphNew);
80 /*TransferableGraphDelta1 result =*/ diff.diff();
81 long end = System.nanoTime();
83 System.out.println("Total time: " + (end - begin)*1e-6 + "ms");