Skip to content

Commit d843e1f

Browse files
committed
Added test for EdgeTypePool. Added some javadoc to EdgeTypePool
1 parent e60b026 commit d843e1f

File tree

6 files changed

+102
-16
lines changed

6 files changed

+102
-16
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ of having them make modifications exclusively for you, or provide you
168168
with facilities for running those works, provided that you comply with
169169
the terms of this License in conveying all material for which you do
170170
not control copyright. Those thus making or running the covered works
171-
for you must do so exclusively on your behalf, under your direction
171+
for you must do so exclusively on your behalf, under your edgeType
172172
and control, on terms that prohibit them from making any copies of
173173
your copyrighted material outside their relationship with you.
174174

README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ are correlated, and with a Zipf degree distribution.
2929
"name" : "country",
3030
"type" : "String",
3131
"generator" : {
32-
"name" : "org.dama.datasynth.generators.CDFGenerator",
32+
"name" : "org.dama.datasynth.generators.CumulativeDistributionGenerator",
3333
"requires" : [],
3434
"init" : ["/dicLocations.txt",1,5," "]
3535
}
@@ -38,7 +38,7 @@ are correlated, and with a Zipf degree distribution.
3838
"name" : "name",
3939
"type" : "String",
4040
"generator" : {
41-
"name" : "org.dama.datasynth.generators.CorrellationGenerator",
41+
"name" : "org.dama.datasynth.generators.UniformCorrelationGenerator",
4242
"requires" : ["person.country"],
4343
"init" : ["/namesByCountry.txt"," "]
4444
}
@@ -49,7 +49,7 @@ are correlated, and with a Zipf degree distribution.
4949
"edges" : [
5050
{
5151
"name" : "friendship",
52-
"direction" : "undirected",
52+
"edgeType" : "undirected",
5353
"source" : "person",
5454
"target" : "person",
5555
"sourceCardinality" : {
@@ -77,7 +77,7 @@ One of the most important elements in DataSynth are generators. A Generator is a
7777
class that representing some sort of "functor" (a callable object). This object
7878
is used to generate the values of a given type, for instance, for a particular attribute. For instance, the
7979
"country" attribute values for the persons is created using the
80-
"org.dama.datasynth.generators.CDFGenerator" generator, wich basically generates
80+
"org.dama.datasynth.generators.CumulativeDistributionGenerator" generator, wich basically generates
8181
values from a dictionary, containing the values and a cummulative distribution
8282
function. A Generator implements two methods: "initialize", and "run". The
8383
method "initialize", is called onece and as its name indicates is used to
@@ -177,11 +177,11 @@ So basically, given a schema specified using the json, DataSynth produces Schnap
177177

178178
```
179179
person.oid = genids(10000);
180-
f = init('org.dama.datasynth.generators.CDFGenerator', '/dicLocations.txt', 1, 5,' ');
180+
f = init('org.dama.datasynth.generators.CumulativeDistributionGenerator', '/dicLocations.txt', 1, 5,' ');
181181
rparams = union(person.oid);
182182
person.country = map(f,rparams);
183183
184-
f = init('org.dama.datasynth.generators.CorrellationGenerator','/namesByCountry.txt', ' ');
184+
f = init('org.dama.datasynth.generators.UniformCorrelationGenerator','/namesByCountry.txt', ' ');
185185
rparams = union(person.oid, person.country);
186186
person.name = map(f,rparams);
187187
@@ -240,10 +240,10 @@ The Signature specifies that this solver is used to solve vertices of type Attri
240240
signature : {
241241
@A = Edge;
242242
@A->source.name == @A->target.name;
243-
@A.direction == 'undirected';
243+
@A.edgeType == 'undirected';
244244
}
245245
```
246-
This signature states that given a vertex in the dependency graph of type Edge, and this vertex source name equals vertex target name, and vertex's attribute direction equals 'undirected', then this solver can generate code to generate that vertex. The syntax "->X" specifies navigating an edge of name X (i.e. source, target, whatever) on the dependency graph, and ".Y" specifies the value of the property with name "Y". For instance, @A->source.name means that given the Edge vertex A, we go to the source attribute (person.oid in this case) and retrieve its name.
246+
This signature states that given a vertex in the dependency graph of type Edge, and this vertex source name equals vertex target name, and vertex's attribute edgeType equals 'undirected', then this solver can generate code to generate that vertex. The syntax "->X" specifies navigating an edge of name X (i.e. source, target, whatever) on the dependency graph, and ".Y" specifies the value of the property with name "Y". For instance, @A->source.name means that given the Edge vertex A, we go to the source attribute (person.oid in this case) and retrieve its name.
247247

248248
The same syntax is used for the place holders within the snippet. The Solver instantiator just generates code by substitution the proper values obtained from the dependency graph.
249249

src/main/java/org/dama/datasynth/test/matching/EdgeTypePool.java

+25-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
/**
66
* Created by aprat on 3/03/17.
77
*/
8-
public class EdgeTypePool<XType extends Comparable<XType>, YType extends Comparable<YType>> {
8+
public class EdgeTypePool< XType extends Comparable<XType>,
9+
YType extends Comparable<YType>> {
910

10-
public class Entry {
11+
static public class Entry< XType extends Comparable<XType>,
12+
YType extends Comparable<YType>> {
1113

1214
private XType xvalue;
1315
private YType yvalue;
@@ -35,9 +37,10 @@ public void setYvalue(YType yvalue) {
3537
}
3638

3739
// fields
38-
private LinkedList<Entry> entries = new LinkedList<Entry>();
40+
private LinkedList<Entry> entries = new LinkedList<Entry>();
3941

4042
public EdgeTypePool(JointDistribution<XType, YType> distribution, long numEdges, long seed) {
43+
4144
for(JointDistribution.Entry<XType,YType> entry : distribution.getEntries()) {
4245
long numToInsert = (long)(entry.getProbability()*numEdges);
4346
for(long i = 0; i < numToInsert; i+=1) {
@@ -49,10 +52,19 @@ public EdgeTypePool(JointDistribution<XType, YType> distribution, long numEdges,
4952
Collections.shuffle(entries,random);
5053
}
5154

55+
/**
56+
* Picks a random edge from the pool
57+
* @return A randomly choosen edge. null if no remaining edges.
58+
*/
5259
public Entry pickRandomEdge() {
5360
return entries.removeFirst();
5461
};
5562

63+
/**
64+
* Picks a random edge from the pool whose x value is the given one
65+
* @param xvalue The given x value
66+
* @return A random edge whose x value is the given one. null if such edge does not exist.
67+
*/
5668
public Entry pickRandomEdgeX(XType xvalue) {
5769
ListIterator<Entry> iterator = entries.listIterator();
5870
while(iterator.hasNext()) {
@@ -65,6 +77,11 @@ public Entry pickRandomEdgeX(XType xvalue) {
6577
return null;
6678
};
6779

80+
/**
81+
* Picks a random edge from the pool whose y value is the given one
82+
* @param yvalue The given y value
83+
* @return A random edge whose y value is the given one. null if such edge does not exist.
84+
*/
6885
public Entry pickRandomEdgeY(YType yvalue) {
6986
ListIterator<Entry> iterator = entries.listIterator();
7087
while(iterator.hasNext()) {
@@ -77,6 +94,11 @@ public Entry pickRandomEdgeY(YType yvalue) {
7794
return null;
7895
};
7996

97+
/**
98+
* Removes a random edge from the pool whose x and y values are the given ones
99+
* @param xvalue The given x value
100+
* @param yvalue The given y value
101+
*/
80102
public void removeEdge(XType xvalue, YType yvalue) {
81103
ListIterator<Entry> iterator = entries.listIterator();
82104
while(iterator.hasNext()) {

src/main/java/org/dama/datasynth/test/matching/JointDistribution.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
/**
99
* Created by aprat on 2/03/17.
1010
*/
11-
public class JointDistribution<XType extends Comparable<XType>, YType extends Comparable<YType>> {
11+
public class JointDistribution< XType extends Comparable<XType>,
12+
YType extends Comparable<YType>> {
1213

13-
public static class Entry<XType extends Comparable<XType>,YType extends Comparable<YType>> {
14+
public static class Entry< XType extends Comparable<XType>,
15+
YType extends Comparable<YType>> {
1416
private XType xvalue;
1517
private YType yvalue;
1618
private double probability;
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.dama.datasynth.test.matching;
22

3+
import org.dama.datasynth.common.Types;
34
import org.dama.datasynth.test.graphreader.types.Graph;
45

56
import java.util.ArrayList;
@@ -9,10 +10,12 @@
910
* Created by aprat on 2/03/17.
1011
*/
1112
public class Matching {
12-
/*public static <XType, YType> void run(Graph graph,
13+
public static <XType extends Comparable<XType>, YType extends Comparable<YType>> void run(Graph graph,
1314
ArrayList<XType> tableX,
1415
ArrayList<YType> tableY,
1516
JointDistribution<XType,YType> distribution ) {
1617

17-
}*/
18+
EdgeTypePool<String,String> edgeTypePool = null;
19+
20+
}
1821
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.dama.datasynth.test.matching;
2+
3+
import org.junit.Test;
4+
5+
import java.io.ByteArrayInputStream;
6+
import java.io.InputStream;
7+
import java.util.HashMap;
8+
9+
import static org.junit.Assert.assertTrue;
10+
11+
/**
12+
* Created by aprat on 3/03/17.
13+
*/
14+
public class EdgeTypePoolTest {
15+
16+
@Test
17+
public void testDistribution() {
18+
String str = new String( "Male Female 0.7\n" +
19+
"Male Male 0.15\n" +
20+
"Female Female 0.15");
21+
22+
InputStream stream = new ByteArrayInputStream(str.getBytes());
23+
JointDistribution<String, String> distribution = new JointDistribution<String,String>();
24+
distribution.load(stream," ", (String s) -> s, (String s) -> s );
25+
26+
EdgeTypePool<String, String> edgeTypePool = new EdgeTypePool(distribution,10000L,123456789L);
27+
28+
HashMap<Integer, Integer> counts = new HashMap<Integer,Integer>();
29+
counts.put(0,0);
30+
counts.put(1,0);
31+
counts.put(2,0);
32+
33+
int numberObserved = 0;
34+
for(int i = 0; i < 10000; i+=1, numberObserved+=1) {
35+
EdgeTypePool.Entry<String,String> entry = edgeTypePool.pickRandomEdge();
36+
if(entry != null) {
37+
if (entry.getXvalue().compareTo("Male") == 0 && entry.getYvalue().compareTo("Female") == 0) {
38+
counts.put(0, counts.get(0) + 1);
39+
continue;
40+
}
41+
42+
if (entry.getXvalue().compareTo("Male") == 0 && entry.getYvalue().compareTo("Male") == 0) {
43+
counts.put(1, counts.get(1) + 1);
44+
continue;
45+
}
46+
47+
if (entry.getXvalue().compareTo("Female") == 0 && entry.getYvalue().compareTo("Female") == 0) {
48+
counts.put(2, counts.get(2) + 1);
49+
continue;
50+
}
51+
}
52+
}
53+
54+
assertTrue(Math.abs(numberObserved - (double) 10000 ) <= 3);
55+
assertTrue(Math.abs(counts.get(0)/(double)10000 - 0.7) < 0.001);
56+
assertTrue(Math.abs(counts.get(1)/(double)10000 - 0.15) < 0.001);
57+
assertTrue(Math.abs(counts.get(2)/(double)10000 - 0.15) < 0.001);
58+
}
59+
}

0 commit comments

Comments
 (0)