Skip to content

Commit

Permalink
Updated grid benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
Aklakan committed Mar 13, 2024
1 parent 98687b0 commit e63e574
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

import org.locationtech.jts.geom.Envelope;

public record Cell2D(Grid2D grid, int row, int col, Envelope envelope) {}
public record Cell2D(Grid2D grid, int row, int col, Envelope envelope) {
public int id() {
return row * grid.sizeY + col;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,6 @@ public class CmdBenchGenGridDataGen
@ArgGroup(exclusive = false)
protected GridOffsets gridOffsets = new GridOffsets();

static class GridOffsets {
@Option(names = "--minX", required = true, defaultValue = "-180")
protected double minX;

@Option(names = "--maxX", required = true, defaultValue = "180")
protected double maxX;

@Option(names = "--minY", required = true, defaultValue = "-90")
protected double minY;

@Option(names = "--maxY", required = true, defaultValue = "90")
protected double maxY;
}

@Option(names = "--rows", required = true, defaultValue = "2")
protected int rows;

Expand Down Expand Up @@ -126,16 +112,16 @@ public static Node toNode(Geometry geom) {
// }

public static String genGraphName(int id) {
return "https://www.example.org/graph/" + id;
return "http://www.example.org/graph/" + id;
}

@Override
public Integer call() throws Exception {
Grid2D grid = Grid2D.newBuilder()
.setMinX(gridOffsets.minX)
.setMaxX(gridOffsets.maxX)
.setMinX(gridOffsets.minY)
.setMaxX(gridOffsets.maxY)
.setMinY(gridOffsets.minY)
.setMaxY(gridOffsets.maxY)
.setRowCount(rows)
.setColCount(cols)
.build();
Expand All @@ -146,9 +132,9 @@ public Integer call() throws Exception {
// Always make cells a bit smaller to avoid corner cases with touching envelopes
double ratio = 0.95 * ratioBase;
Node graph = NodeFactory.createURI(genGraphName(g));
return grid.stream().flatMap(cell -> {
Node feature = NodeFactory.createURI("https://www.example.org/feature/" + g + "/" + cell.row() + "/" + cell.col());
Node geom = NodeFactory.createURI("https://www.example.org/geometry/" + g + "/" + cell.row() + "/" + cell.col());
return grid.stream(true).flatMap(cell -> {
Node feature = NodeFactory.createURI("http://www.example.org/feature/" + g + "/" + cell.row() + "/" + cell.col());
Node geom = NodeFactory.createURI("http://www.example.org/geometry/" + g + "/" + cell.row() + "/" + cell.col());

Geometry cellGeom = toGeometry(cell.envelope());
Geometry scaledCellGeom = ratio != 1.0
Expand All @@ -168,7 +154,7 @@ public Integer call() throws Exception {
try (OutputStream out = StdIo.openStdOutWithCloseShield()) {
StreamRDF writer = StreamRDFWriter.getWriterStream(out, RDFFormat.NQUADS);
writer.start();
quads.forEach(writer::quad);
quads.sequential().forEach(writer::quad);
writer.finish();
out.flush();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import org.aksw.commons.io.util.StdIo;
import org.aksw.commons.picocli.CmdCommonBase;
import org.aksw.jenax.arq.util.syntax.QueryUtils;
import org.aksw.jenax.arq.util.var.Vars;
import org.apache.jena.geosparql.implementation.GeometryWrapper;
import org.apache.jena.geosparql.implementation.jts.CustomGeometryFactory;
import org.apache.jena.geosparql.implementation.vocabulary.Geo;
Expand All @@ -14,6 +16,13 @@
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.riot.out.NodeFmtLib;
import org.apache.jena.sparql.algebra.Algebra;
import org.apache.jena.sparql.algebra.Op;
import org.apache.jena.sparql.algebra.OpAsQuery;
import org.apache.jena.sparql.algebra.Transform;
import org.apache.jena.sparql.algebra.TransformCopy;
import org.apache.jena.sparql.algebra.Transformer;
import org.apache.jena.sparql.algebra.op.OpGraph;
import org.locationtech.jts.geom.Envelope;

import picocli.CommandLine.ArgGroup;
Expand All @@ -28,33 +37,43 @@ public class CmdBenchGenGridQueryGen
@ArgGroup(exclusive = false)
protected GridOffsets gridOffsets = new GridOffsets();

static class GridOffsets {
@Option(names = "--minX", required = true, defaultValue = "-180")
protected double minX;

@Option(names = "--maxX", required = true, defaultValue = "180")
protected double maxX;

@Option(names = "--minY", required = true, defaultValue = "-90")
protected double minY;

@Option(names = "--maxY", required = true, defaultValue = "90")
protected double maxY;
}

@Option(names = "--rows", required = true, defaultValue = "2")
protected int rows;

@Option(names = "--cols", required = true, defaultValue = "2")
protected int cols;

@Option(names = "--graphs", required = true, defaultValue = "2")
protected int graphs;

// @ArgGroup(exclusive = true, multiplicity = "0..1")
// public GraphSpec outputSpec;
//
// public static class GraphSpec {
// /**
// * sparql-pattern file
// *
// */
// @Option(names = { "-o", "--out-file" }, description = "output file")
// public String outFile;
//
// @Option(names = { "--io", }, description = "overwrites argument file on success with output; use with care")
// public String inOutFile = null;
// }

@Option(names = "--allGraphs", required = true, defaultValue = "false")
protected boolean allGraphs;

@Option(names = { "-u", "--union-default-graph" }, defaultValue = "false", fallbackValue = "true", description = "If --allGraphs is given. Use union default graph rather than ?g.")
protected boolean unionDefaultGraphMode;

public static Query genQuery(Cell2D cell, String fixedGraph) {
@Option(names = "--reverse", required = true, defaultValue = "false", fallbackValue = "true", description = "Generate queries by traversing grid cells from the last to the first.")
protected boolean reverse;

String g = fixedGraph == null ? "?g" : NodeFmtLib.strNT(NodeFactory.createURI(fixedGraph));
public static Query genQuery(Cell2D cell, Node graphNode) {

String gStr = graphNode == null ? "?dummy" : NodeFmtLib.strNT(graphNode);
// NodeFmtLib.strNT(NodeFactory.createURI(fixedGraph));

String queryStr = """
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
Expand All @@ -71,11 +90,26 @@ public static Query genQuery(Cell2D cell, String fixedGraph) {
}
"""
.replace("$CELLGEOM$", NodeFmtLib.strNT(toNode(cell.envelope())))
.replace("$GRAPH$", g)
.replace("$GRAPH$", gStr)
;

System.out.println(queryStr);
Query result = QueryFactory.create(queryStr);

// Drop GRAPH ?foo { } blocks if no graph is given
if (graphNode == null) {
Transform xform = new TransformCopy() {
@Override
public Op transform(OpGraph opGraph, Op subOp) {
return subOp;
}
};

Op op = Algebra.compile(result);
op = Transformer.transform(xform, op);
Query tmp = OpAsQuery.asQuery(op);
result = QueryUtils.restoreQueryForm(tmp, result);
}

return result;
}

Expand All @@ -88,18 +122,29 @@ public Integer call() throws Exception {
Grid2D grid = Grid2D.newBuilder()
.setMinX(gridOffsets.minX)
.setMaxX(gridOffsets.maxX)
.setMinX(gridOffsets.minY)
.setMaxX(gridOffsets.maxY)
.setMinY(gridOffsets.minY)
.setMaxY(gridOffsets.maxY)
.setRowCount(rows)
.setColCount(cols)
.build();

String fixedGraph = allGraphs ? null : CmdBenchGenGridDataGen.genGraphName(0);

Stream<Query> queries = grid.stream().map(cell -> genQuery(cell, fixedGraph));
Stream<Query> queries = grid.stream(!reverse).map(cell -> {
Node graphNode;
if (allGraphs) {
graphNode = unionDefaultGraphMode
? null
: Vars.g;
} else {
int cellId = cell.id();
int gid = cellId % graphs;
graphNode = NodeFactory.createURI(CmdBenchGenGridDataGen.genGraphName(gid));
}
Query r = genQuery(cell, graphNode);
return r;
});

try (PrintWriter writer = new PrintWriter(StdIo.openStdOutWithCloseShield())) {
queries.forEach(query -> writer.println(query));
queries.sequential().forEach(query -> writer.println(query));
writer.flush();
}
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public Integer call() throws Exception {
Stream<Quad> quads =
IntStream.range(0, graphs).boxed().flatMap(g -> {
Node graph = NodeFactory.createURI("https://www.example.org/graph/" + g);
return grid.stream().flatMap(cell -> {
return grid.stream(true).flatMap(cell -> {
Node feature = NodeFactory.createURI("https://www.example.org/feature/" + graph + "/" + cell.row() + "/" + cell.col());
Node geom = NodeFactory.createURI("https://www.example.org/geometry/" + graph + "/" + cell.row() + "/" + cell.col());
Node wkt = toNode(cell.envelope());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ public Grid2D(double worldMinX, double worldMaxX, double worldMinY, double world
this.deltaY = (worldMaxY - worldMinY) / sizeY;
}

public Stream<Cell2D> stream() {
return IntStream.range(0, sizeY).mapToObj(row -> row).flatMap(row ->
IntStream.range(0, sizeX).mapToObj(col -> cellOf(row, col)));
public static IntStream range(int startInclusive, int endExclusive, boolean isForward) {
return isForward
? IntStream.range(startInclusive, endExclusive)
: IntStream.iterate(endExclusive - 1, i -> i >= startInclusive, i -> i - 1);
}

public Stream<Cell2D> stream(boolean isForward) {
return range(0, sizeY, isForward).mapToObj(row -> row).flatMap(row ->
range(0, sizeX, isForward).mapToObj(col -> cellOf(row, col)));
}

public Cell2D cellOf(int row, int col) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.aksw.bench.geo.cmd;

import picocli.CommandLine.Option;

public class GridOffsets {
@Option(names = "--minX", defaultValue = "-90") // -180
protected double minX;

@Option(names = "--maxX", defaultValue = "90") // 180
protected double maxX;

@Option(names = "--minY", defaultValue = "-90")
protected double minY;

@Option(names = "--maxY", defaultValue = "90")
protected double maxY;

public double getMinX() {
return minX;
}

public void setMinX(double minX) {
this.minX = minX;
}

public double getMaxX() {
return maxX;
}

public void setMaxX(double maxX) {
this.maxX = maxX;
}

public double getMinY() {
return minY;
}

public void setMinY(double minY) {
this.minY = minY;
}

public double getMaxY() {
return maxY;
}

public void setMaxY(double maxY) {
this.maxY = maxY;
}
}

0 comments on commit e63e574

Please sign in to comment.